xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-ecdsa-sk.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: ssh-ecdsa-sk.c,v 1.3 2020/12/04 18:42:50 christos Exp $	*/
2 /* $OpenBSD: ssh-ecdsa-sk.c,v 1.8 2020/06/22 23:44:27 djm Exp $ */
3 
4 /*
5  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
6  * Copyright (c) 2010 Damien Miller.  All rights reserved.
7  * Copyright (c) 2019 Google Inc.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "includes.h"
30 __RCSID("$NetBSD: ssh-ecdsa-sk.c,v 1.3 2020/12/04 18:42:50 christos Exp $");
31 
32 /* #define DEBUG_SK 1 */
33 
34 #include <sys/types.h>
35 
36 #include <openssl/bn.h>
37 #include <openssl/ec.h>
38 #include <openssl/ecdsa.h>
39 #include <openssl/evp.h>
40 
41 #include <string.h>
42 #include <stdio.h> /* needed for DEBUG_SK only */
43 
44 #include "sshbuf.h"
45 #include "ssherr.h"
46 #include "digest.h"
47 #define SSHKEY_INTERNAL
48 #include "sshkey.h"
49 
50 /*
51  * Check FIDO/W3C webauthn signatures clientData field against the expected
52  * format and prepare a hash of it for use in signature verification.
53  *
54  * webauthn signatures do not sign the hash of the message directly, but
55  * instead sign a JSON-like "clientData" wrapper structure that contains the
56  * message hash along with a other information.
57  *
58  * Fortunately this structure has a fixed format so it is possible to verify
59  * that the hash of the signed message is present within the clientData
60  * structure without needing to implement any JSON parsing.
61  */
62 static int
63 webauthn_check_prepare_hash(const u_char *data, size_t datalen,
64     const char *origin, const struct sshbuf *wrapper,
65     uint8_t flags, const struct sshbuf *extensions,
66     u_char *msghash, size_t msghashlen)
67 {
68 	int r = SSH_ERR_INTERNAL_ERROR;
69 	struct sshbuf *chall = NULL, *m = NULL;
70 
71 	if ((m = sshbuf_new()) == NULL ||
72 	    (chall = sshbuf_from(data, datalen)) == NULL) {
73 		r = SSH_ERR_ALLOC_FAIL;
74 		goto out;
75 	}
76 	/*
77 	 * Ensure origin contains no quote character and that the flags are
78 	 * consistent with what we received
79 	 */
80 	if (strchr(origin, '\"') != NULL ||
81 	    (flags & 0x40) != 0 /* AD */ ||
82 	    ((flags & 0x80) == 0 /* ED */) != (sshbuf_len(extensions) == 0)) {
83 		r = SSH_ERR_INVALID_FORMAT;
84 		goto out;
85 	}
86 
87 	/*
88 	 * Prepare the preamble to clientData that we expect, poking the
89 	 * challenge and origin into their canonical positions in the
90 	 * structure. The crossOrigin flag and any additional extension
91 	 * fields present are ignored.
92 	 */
93 #define WEBAUTHN_0	"{\"type\":\"webauthn.get\",\"challenge\":\""
94 #define WEBAUTHN_1	"\",\"origin\":\""
95 #define WEBAUTHN_2	"\""
96 	if ((r = sshbuf_put(m, WEBAUTHN_0, sizeof(WEBAUTHN_0) - 1)) != 0 ||
97 	    (r = sshbuf_dtourlb64(chall, m, 0)) != 0 ||
98 	    (r = sshbuf_put(m, WEBAUTHN_1, sizeof(WEBAUTHN_1) - 1)) != 0 ||
99 	    (r = sshbuf_put(m, origin, strlen(origin))) != 0 ||
100 	    (r = sshbuf_put(m, WEBAUTHN_2, sizeof(WEBAUTHN_2) - 1)) != 0)
101 		goto out;
102 #ifdef DEBUG_SK
103 	fprintf(stderr, "%s: received origin: %s\n", __func__, origin);
104 	fprintf(stderr, "%s: received clientData:\n", __func__);
105 	sshbuf_dump(wrapper, stderr);
106 	fprintf(stderr, "%s: expected clientData premable:\n", __func__);
107 	sshbuf_dump(m, stderr);
108 #endif
109 	/* Check that the supplied clientData has the preamble we expect */
110 	if ((r = sshbuf_cmp(wrapper, 0, sshbuf_ptr(m), sshbuf_len(m))) != 0)
111 		goto out;
112 
113 	/* Prepare hash of clientData */
114 	if ((r = ssh_digest_buffer(SSH_DIGEST_SHA256, wrapper,
115 	    msghash, msghashlen)) != 0)
116 		goto out;
117 
118 	/* success */
119 	r = 0;
120  out:
121 	sshbuf_free(chall);
122 	sshbuf_free(m);
123 	return r;
124 }
125 
126 /* ARGSUSED */
127 int
128 ssh_ecdsa_sk_verify(const struct sshkey *key,
129     const u_char *signature, size_t signaturelen,
130     const u_char *data, size_t datalen, u_int compat,
131     struct sshkey_sig_details **detailsp)
132 {
133 	ECDSA_SIG *sig = NULL;
134 	BIGNUM *sig_r = NULL, *sig_s = NULL;
135 	u_char sig_flags;
136 	u_char msghash[32], apphash[32], sighash[32];
137 	u_int sig_counter;
138 	int is_webauthn = 0, ret = SSH_ERR_INTERNAL_ERROR;
139 	struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
140 	struct sshbuf *webauthn_wrapper = NULL, *webauthn_exts = NULL;
141 	char *ktype = NULL, *webauthn_origin = NULL;
142 	struct sshkey_sig_details *details = NULL;
143 #ifdef DEBUG_SK
144 	char *tmp = NULL;
145 #endif
146 
147 	if (detailsp != NULL)
148 		*detailsp = NULL;
149 	if (key == NULL || key->ecdsa == NULL ||
150 	    sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
151 	    signature == NULL || signaturelen == 0)
152 		return SSH_ERR_INVALID_ARGUMENT;
153 
154 	if (key->ecdsa_nid != NID_X9_62_prime256v1)
155 		return SSH_ERR_INTERNAL_ERROR;
156 
157 	/* fetch signature */
158 	if ((b = sshbuf_from(signature, signaturelen)) == NULL)
159 		return SSH_ERR_ALLOC_FAIL;
160 	if ((details = calloc(1, sizeof(*details))) == NULL) {
161 		ret = SSH_ERR_ALLOC_FAIL;
162 		goto out;
163 	}
164 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
165 		ret = SSH_ERR_INVALID_FORMAT;
166 		goto out;
167 	}
168 	if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0)
169 		is_webauthn = 1;
170 	else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) {
171 		ret = SSH_ERR_INVALID_FORMAT;
172 		goto out;
173 	}
174 	if (sshbuf_froms(b, &sigbuf) != 0 ||
175 	    sshbuf_get_u8(b, &sig_flags) != 0 ||
176 	    sshbuf_get_u32(b, &sig_counter) != 0) {
177 		ret = SSH_ERR_INVALID_FORMAT;
178 		goto out;
179 	}
180 	if (is_webauthn) {
181 		if (sshbuf_get_cstring(b, &webauthn_origin, NULL) != 0 ||
182 		    sshbuf_froms(b, &webauthn_wrapper) != 0 ||
183 		    sshbuf_froms(b, &webauthn_exts) != 0) {
184 			ret = SSH_ERR_INVALID_FORMAT;
185 			goto out;
186 		}
187 	}
188 	if (sshbuf_len(b) != 0) {
189 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
190 		goto out;
191 	}
192 
193 	/* parse signature */
194 	if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
195 	    sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
196 		ret = SSH_ERR_INVALID_FORMAT;
197 		goto out;
198 	}
199 	if (sshbuf_len(sigbuf) != 0) {
200 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
201 		goto out;
202 	}
203 
204 #ifdef DEBUG_SK
205 	fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen);
206 	/* sshbuf_dump_data(data, datalen, stderr); */
207 	fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
208 	free(tmp);
209 	fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
210 	free(tmp);
211 	fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
212 	    __func__, sig_flags, sig_counter);
213 	if (is_webauthn) {
214 		fprintf(stderr, "%s: webauthn origin: %s\n", __func__,
215 		    webauthn_origin);
216 		fprintf(stderr, "%s: webauthn_wrapper:\n", __func__);
217 		sshbuf_dump(webauthn_wrapper, stderr);
218 	}
219 #endif
220 	if ((sig = ECDSA_SIG_new()) == NULL) {
221 		ret = SSH_ERR_ALLOC_FAIL;
222 		goto out;
223 	}
224 	if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
225 		ret = SSH_ERR_LIBCRYPTO_ERROR;
226 		goto out;
227 	}
228 	sig_r = sig_s = NULL; /* transferred */
229 
230 	/* Reconstruct data that was supposedly signed */
231 	if ((original_signed = sshbuf_new()) == NULL) {
232 		ret = SSH_ERR_ALLOC_FAIL;
233 		goto out;
234 	}
235 	if (is_webauthn) {
236 		if ((ret = webauthn_check_prepare_hash(data, datalen,
237 		    webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts,
238 		    msghash, sizeof(msghash))) != 0)
239 			goto out;
240 	} else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen,
241 	    msghash, sizeof(msghash))) != 0)
242 		goto out;
243 	/* Application value is hashed before signature */
244 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
245 	    strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
246 		goto out;
247 #ifdef DEBUG_SK
248 	fprintf(stderr, "%s: hashed application:\n", __func__);
249 	sshbuf_dump_data(apphash, sizeof(apphash), stderr);
250 	fprintf(stderr, "%s: hashed message:\n", __func__);
251 	sshbuf_dump_data(msghash, sizeof(msghash), stderr);
252 #endif
253 	if ((ret = sshbuf_put(original_signed,
254 	    apphash, sizeof(apphash))) != 0 ||
255 	    (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
256 	    (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
257 	    (ret = sshbuf_putb(original_signed, webauthn_exts)) != 0 ||
258 	    (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
259 		goto out;
260 	/* Signature is over H(original_signed) */
261 	if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
262 	    sighash, sizeof(sighash))) != 0)
263 		goto out;
264 	details->sk_counter = sig_counter;
265 	details->sk_flags = sig_flags;
266 #ifdef DEBUG_SK
267 	fprintf(stderr, "%s: signed buf:\n", __func__);
268 	sshbuf_dump(original_signed, stderr);
269 	fprintf(stderr, "%s: signed hash:\n", __func__);
270 	sshbuf_dump_data(sighash, sizeof(sighash), stderr);
271 #endif
272 
273 	/* Verify it */
274 	switch (ECDSA_do_verify(sighash, sizeof(sighash), sig, key->ecdsa)) {
275 	case 1:
276 		ret = 0;
277 		break;
278 	case 0:
279 		ret = SSH_ERR_SIGNATURE_INVALID;
280 		goto out;
281 	default:
282 		ret = SSH_ERR_LIBCRYPTO_ERROR;
283 		goto out;
284 	}
285 	/* success */
286 	if (detailsp != NULL) {
287 		*detailsp = details;
288 		details = NULL;
289 	}
290  out:
291 	explicit_bzero(&sig_flags, sizeof(sig_flags));
292 	explicit_bzero(&sig_counter, sizeof(sig_counter));
293 	explicit_bzero(msghash, sizeof(msghash));
294 	explicit_bzero(sighash, sizeof(msghash));
295 	explicit_bzero(apphash, sizeof(apphash));
296 	sshkey_sig_details_free(details);
297 	sshbuf_free(webauthn_wrapper);
298 	sshbuf_free(webauthn_exts);
299 	free(webauthn_origin);
300 	sshbuf_free(original_signed);
301 	sshbuf_free(sigbuf);
302 	sshbuf_free(b);
303 	ECDSA_SIG_free(sig);
304 	BN_clear_free(sig_r);
305 	BN_clear_free(sig_s);
306 	free(ktype);
307 	return ret;
308 }
309