xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-ecdsa-sk.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: ssh-ecdsa-sk.c,v 1.2 2020/02/27 00:24:40 christos Exp $	*/
2 /* $OpenBSD: ssh-ecdsa-sk.c,v 1.5 2019/11/26 03:04:27 djm Exp $ */
3 /*
4  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
5  * Copyright (c) 2010 Damien Miller.  All rights reserved.
6  * Copyright (c) 2019 Google Inc.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include "includes.h"
29 __RCSID("$NetBSD: ssh-ecdsa-sk.c,v 1.2 2020/02/27 00:24:40 christos Exp $");
30 
31 /* #define DEBUG_SK 1 */
32 
33 #include <sys/types.h>
34 
35 #include <openssl/bn.h>
36 #include <openssl/ec.h>
37 #include <openssl/ecdsa.h>
38 #include <openssl/evp.h>
39 
40 #include <string.h>
41 #include <stdio.h> /* needed for DEBUG_SK only */
42 
43 #include "sshbuf.h"
44 #include "ssherr.h"
45 #include "digest.h"
46 #define SSHKEY_INTERNAL
47 #include "sshkey.h"
48 
49 /* ARGSUSED */
50 int
51 ssh_ecdsa_sk_verify(const struct sshkey *key,
52     const u_char *signature, size_t signaturelen,
53     const u_char *data, size_t datalen, u_int compat,
54     struct sshkey_sig_details **detailsp)
55 {
56 	ECDSA_SIG *sig = NULL;
57 	BIGNUM *sig_r = NULL, *sig_s = NULL;
58 	u_char sig_flags;
59 	u_char msghash[32], apphash[32], sighash[32];
60 	u_int sig_counter;
61 	int ret = SSH_ERR_INTERNAL_ERROR;
62 	struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
63 	char *ktype = NULL;
64 	struct sshkey_sig_details *details = NULL;
65 #ifdef DEBUG_SK
66 	char *tmp = NULL;
67 #endif
68 
69 	if (detailsp != NULL)
70 		*detailsp = NULL;
71 	if (key == NULL || key->ecdsa == NULL ||
72 	    sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
73 	    signature == NULL || signaturelen == 0)
74 		return SSH_ERR_INVALID_ARGUMENT;
75 
76 	if (key->ecdsa_nid != NID_X9_62_prime256v1)
77 		return SSH_ERR_INTERNAL_ERROR;
78 
79 	/* fetch signature */
80 	if ((b = sshbuf_from(signature, signaturelen)) == NULL)
81 		return SSH_ERR_ALLOC_FAIL;
82 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0 ||
83 	    sshbuf_froms(b, &sigbuf) != 0 ||
84 	    sshbuf_get_u8(b, &sig_flags) != 0 ||
85 	    sshbuf_get_u32(b, &sig_counter) != 0) {
86 		ret = SSH_ERR_INVALID_FORMAT;
87 		goto out;
88 	}
89 	if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) {
90 		ret = SSH_ERR_KEY_TYPE_MISMATCH;
91 		goto out;
92 	}
93 	if (sshbuf_len(b) != 0) {
94 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
95 		goto out;
96 	}
97 
98 	/* parse signature */
99 	if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
100 	    sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
101 		ret = SSH_ERR_INVALID_FORMAT;
102 		goto out;
103 	}
104 	if ((sig = ECDSA_SIG_new()) == NULL) {
105 		ret = SSH_ERR_ALLOC_FAIL;
106 		goto out;
107 	}
108 	if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
109 		ret = SSH_ERR_LIBCRYPTO_ERROR;
110 		goto out;
111 	}
112 #ifdef DEBUG_SK
113 	fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen);
114 	/* sshbuf_dump_data(data, datalen, stderr); */
115 	fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
116 	free(tmp);
117 	fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
118 	free(tmp);
119 	fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
120 	    __func__, sig_flags, sig_counter);
121 #endif
122 	sig_r = sig_s = NULL; /* transferred */
123 
124 	if (sshbuf_len(sigbuf) != 0) {
125 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
126 		goto out;
127 	}
128 
129 	/* Reconstruct data that was supposedly signed */
130 	if ((original_signed = sshbuf_new()) == NULL) {
131 		ret = SSH_ERR_ALLOC_FAIL;
132 		goto out;
133 	}
134 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen,
135 	    msghash, sizeof(msghash))) != 0)
136 		goto out;
137 	/* Application value is hashed before signature */
138 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
139 	    strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
140 		goto out;
141 #ifdef DEBUG_SK
142 	fprintf(stderr, "%s: hashed application:\n", __func__);
143 	sshbuf_dump_data(apphash, sizeof(apphash), stderr);
144 	fprintf(stderr, "%s: hashed message:\n", __func__);
145 	sshbuf_dump_data(msghash, sizeof(msghash), stderr);
146 #endif
147 	if ((ret = sshbuf_put(original_signed,
148 	    apphash, sizeof(apphash))) != 0 ||
149 	    (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
150 	    (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
151 	    (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
152 		goto out;
153 	/* Signature is over H(original_signed) */
154 	if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
155 	    sighash, sizeof(sighash))) != 0)
156 		goto out;
157 	if ((details = calloc(1, sizeof(*details))) == NULL) {
158 		ret = SSH_ERR_ALLOC_FAIL;
159 		goto out;
160 	}
161 	details->sk_counter = sig_counter;
162 	details->sk_flags = sig_flags;
163 #ifdef DEBUG_SK
164 	fprintf(stderr, "%s: signed buf:\n", __func__);
165 	sshbuf_dump(original_signed, stderr);
166 	fprintf(stderr, "%s: signed hash:\n", __func__);
167 	sshbuf_dump_data(sighash, sizeof(sighash), stderr);
168 #endif
169 
170 	/* Verify it */
171 	switch (ECDSA_do_verify(sighash, sizeof(sighash), sig, key->ecdsa)) {
172 	case 1:
173 		ret = 0;
174 		break;
175 	case 0:
176 		ret = SSH_ERR_SIGNATURE_INVALID;
177 		goto out;
178 	default:
179 		ret = SSH_ERR_LIBCRYPTO_ERROR;
180 		goto out;
181 	}
182 	/* success */
183 	if (detailsp != NULL) {
184 		*detailsp = details;
185 		details = NULL;
186 	}
187  out:
188 	explicit_bzero(&sig_flags, sizeof(sig_flags));
189 	explicit_bzero(&sig_counter, sizeof(sig_counter));
190 	explicit_bzero(msghash, sizeof(msghash));
191 	explicit_bzero(sighash, sizeof(msghash));
192 	explicit_bzero(apphash, sizeof(apphash));
193 	sshkey_sig_details_free(details);
194 	sshbuf_free(original_signed);
195 	sshbuf_free(sigbuf);
196 	sshbuf_free(b);
197 	ECDSA_SIG_free(sig);
198 	BN_clear_free(sig_r);
199 	BN_clear_free(sig_s);
200 	free(ktype);
201 	return ret;
202 }
203