xref: /openbsd/usr.bin/ssh/ssh-ecdsa-sk.c (revision 5411e769)
1 /* $OpenBSD: ssh-ecdsa-sk.c,v 1.19 2024/08/15 00:51:51 djm Exp $ */
2 /*
3  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
4  * Copyright (c) 2010 Damien Miller.  All rights reserved.
5  * Copyright (c) 2019 Google Inc.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /* #define DEBUG_SK 1 */
29 
30 #include <sys/types.h>
31 
32 #include <openssl/bn.h>
33 #include <openssl/ec.h>
34 #include <openssl/ecdsa.h>
35 #include <openssl/evp.h>
36 
37 #include <string.h>
38 #include <stdio.h> /* needed for DEBUG_SK only */
39 
40 #include "sshbuf.h"
41 #include "ssherr.h"
42 #include "digest.h"
43 #define SSHKEY_INTERNAL
44 #include "sshkey.h"
45 
46 /* Reuse some ECDSA internals */
47 extern struct sshkey_impl_funcs sshkey_ecdsa_funcs;
48 
49 static void
ssh_ecdsa_sk_cleanup(struct sshkey * k)50 ssh_ecdsa_sk_cleanup(struct sshkey *k)
51 {
52 	sshkey_sk_cleanup(k);
53 	sshkey_ecdsa_funcs.cleanup(k);
54 }
55 
56 static int
ssh_ecdsa_sk_equal(const struct sshkey * a,const struct sshkey * b)57 ssh_ecdsa_sk_equal(const struct sshkey *a, const struct sshkey *b)
58 {
59 	if (!sshkey_sk_fields_equal(a, b))
60 		return 0;
61 	if (!sshkey_ecdsa_funcs.equal(a, b))
62 		return 0;
63 	return 1;
64 }
65 
66 static int
ssh_ecdsa_sk_serialize_public(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)67 ssh_ecdsa_sk_serialize_public(const struct sshkey *key, struct sshbuf *b,
68     enum sshkey_serialize_rep opts)
69 {
70 	int r;
71 
72 	if ((r = sshkey_ecdsa_funcs.serialize_public(key, b, opts)) != 0)
73 		return r;
74 	if ((r = sshkey_serialize_sk(key, b)) != 0)
75 		return r;
76 
77 	return 0;
78 }
79 
80 static int
ssh_ecdsa_sk_serialize_private(const struct sshkey * key,struct sshbuf * b,enum sshkey_serialize_rep opts)81 ssh_ecdsa_sk_serialize_private(const struct sshkey *key, struct sshbuf *b,
82     enum sshkey_serialize_rep opts)
83 {
84 	int r;
85 
86 	if (!sshkey_is_cert(key)) {
87 		if ((r = sshkey_ecdsa_funcs.serialize_public(key,
88 		    b, opts)) != 0)
89 			return r;
90 	}
91 	if ((r = sshkey_serialize_private_sk(key, b)) != 0)
92 		return r;
93 
94 	return 0;
95 }
96 
97 static int
ssh_ecdsa_sk_copy_public(const struct sshkey * from,struct sshkey * to)98 ssh_ecdsa_sk_copy_public(const struct sshkey *from, struct sshkey *to)
99 {
100 	int r;
101 
102 	if ((r = sshkey_ecdsa_funcs.copy_public(from, to)) != 0)
103 		return r;
104 	if ((r = sshkey_copy_public_sk(from, to)) != 0)
105 		return r;
106 	return 0;
107 }
108 
109 static int
ssh_ecdsa_sk_deserialize_public(const char * ktype,struct sshbuf * b,struct sshkey * key)110 ssh_ecdsa_sk_deserialize_public(const char *ktype, struct sshbuf *b,
111     struct sshkey *key)
112 {
113 	int r;
114 
115 	if ((r = sshkey_ecdsa_funcs.deserialize_public(ktype, b, key)) != 0)
116 		return r;
117 	if ((r = sshkey_deserialize_sk(b, key)) != 0)
118 		return r;
119 	return 0;
120 }
121 
122 static int
ssh_ecdsa_sk_deserialize_private(const char * ktype,struct sshbuf * b,struct sshkey * key)123 ssh_ecdsa_sk_deserialize_private(const char *ktype, struct sshbuf *b,
124     struct sshkey *key)
125 {
126 	int r;
127 
128 	if (!sshkey_is_cert(key)) {
129 		if ((r = sshkey_ecdsa_funcs.deserialize_public(ktype,
130 		    b, key)) != 0)
131 			return r;
132 	}
133 	if ((r = sshkey_private_deserialize_sk(b, key)) != 0)
134 		return r;
135 
136 	return 0;
137 }
138 
139 /*
140  * Check FIDO/W3C webauthn signatures clientData field against the expected
141  * format and prepare a hash of it for use in signature verification.
142  *
143  * webauthn signatures do not sign the hash of the message directly, but
144  * instead sign a JSON-like "clientData" wrapper structure that contains the
145  * message hash along with a other information.
146  *
147  * Fortunately this structure has a fixed format so it is possible to verify
148  * that the hash of the signed message is present within the clientData
149  * structure without needing to implement any JSON parsing.
150  */
151 static int
webauthn_check_prepare_hash(const u_char * data,size_t datalen,const char * origin,const struct sshbuf * wrapper,uint8_t flags,const struct sshbuf * extensions,u_char * msghash,size_t msghashlen)152 webauthn_check_prepare_hash(const u_char *data, size_t datalen,
153     const char *origin, const struct sshbuf *wrapper,
154     uint8_t flags, const struct sshbuf *extensions,
155     u_char *msghash, size_t msghashlen)
156 {
157 	int r = SSH_ERR_INTERNAL_ERROR;
158 	struct sshbuf *chall = NULL, *m = NULL;
159 
160 	if ((m = sshbuf_new()) == NULL ||
161 	    (chall = sshbuf_from(data, datalen)) == NULL) {
162 		r = SSH_ERR_ALLOC_FAIL;
163 		goto out;
164 	}
165 	/*
166 	 * Ensure origin contains no quote character and that the flags are
167 	 * consistent with what we received
168 	 */
169 	if (strchr(origin, '\"') != NULL ||
170 	    (flags & 0x40) != 0 /* AD */ ||
171 	    ((flags & 0x80) == 0 /* ED */) != (sshbuf_len(extensions) == 0)) {
172 		r = SSH_ERR_INVALID_FORMAT;
173 		goto out;
174 	}
175 
176 	/*
177 	 * Prepare the preamble to clientData that we expect, poking the
178 	 * challenge and origin into their canonical positions in the
179 	 * structure. The crossOrigin flag and any additional extension
180 	 * fields present are ignored.
181 	 */
182 #define WEBAUTHN_0	"{\"type\":\"webauthn.get\",\"challenge\":\""
183 #define WEBAUTHN_1	"\",\"origin\":\""
184 #define WEBAUTHN_2	"\""
185 	if ((r = sshbuf_put(m, WEBAUTHN_0, sizeof(WEBAUTHN_0) - 1)) != 0 ||
186 	    (r = sshbuf_dtourlb64(chall, m, 0)) != 0 ||
187 	    (r = sshbuf_put(m, WEBAUTHN_1, sizeof(WEBAUTHN_1) - 1)) != 0 ||
188 	    (r = sshbuf_put(m, origin, strlen(origin))) != 0 ||
189 	    (r = sshbuf_put(m, WEBAUTHN_2, sizeof(WEBAUTHN_2) - 1)) != 0)
190 		goto out;
191 #ifdef DEBUG_SK
192 	fprintf(stderr, "%s: received origin: %s\n", __func__, origin);
193 	fprintf(stderr, "%s: received clientData:\n", __func__);
194 	sshbuf_dump(wrapper, stderr);
195 	fprintf(stderr, "%s: expected clientData premable:\n", __func__);
196 	sshbuf_dump(m, stderr);
197 #endif
198 	/* Check that the supplied clientData has the preamble we expect */
199 	if ((r = sshbuf_cmp(wrapper, 0, sshbuf_ptr(m), sshbuf_len(m))) != 0)
200 		goto out;
201 
202 	/* Prepare hash of clientData */
203 	if ((r = ssh_digest_buffer(SSH_DIGEST_SHA256, wrapper,
204 	    msghash, msghashlen)) != 0)
205 		goto out;
206 
207 	/* success */
208 	r = 0;
209  out:
210 	sshbuf_free(chall);
211 	sshbuf_free(m);
212 	return r;
213 }
214 
215 static int
ssh_ecdsa_sk_verify(const struct sshkey * key,const u_char * sig,size_t siglen,const u_char * data,size_t dlen,const char * alg,u_int compat,struct sshkey_sig_details ** detailsp)216 ssh_ecdsa_sk_verify(const struct sshkey *key,
217     const u_char *sig, size_t siglen,
218     const u_char *data, size_t dlen, const char *alg, u_int compat,
219     struct sshkey_sig_details **detailsp)
220 {
221 	ECDSA_SIG *esig = NULL;
222 	EVP_MD_CTX *md_ctx = NULL;
223 	BIGNUM *sig_r = NULL, *sig_s = NULL;
224 	u_char sig_flags;
225 	u_char msghash[32], apphash[32];
226 	u_int sig_counter;
227 	u_char *sigb = NULL, *cp;
228 	int is_webauthn = 0, ret = SSH_ERR_INTERNAL_ERROR, len = 0;
229 	struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
230 	struct sshbuf *webauthn_wrapper = NULL, *webauthn_exts = NULL;
231 	char *ktype = NULL, *webauthn_origin = NULL;
232 	struct sshkey_sig_details *details = NULL;
233 #ifdef DEBUG_SK
234 	char *tmp = NULL;
235 #endif
236 
237 	if (detailsp != NULL)
238 		*detailsp = NULL;
239 	if (key == NULL || key->pkey == NULL ||
240 	    sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
241 	    sig == NULL || siglen == 0)
242 		return SSH_ERR_INVALID_ARGUMENT;
243 
244 	if (key->ecdsa_nid != NID_X9_62_prime256v1)
245 		return SSH_ERR_INTERNAL_ERROR;
246 
247 	/* fetch signature */
248 	if ((b = sshbuf_from(sig, siglen)) == NULL)
249 		return SSH_ERR_ALLOC_FAIL;
250 	if ((details = calloc(1, sizeof(*details))) == NULL) {
251 		ret = SSH_ERR_ALLOC_FAIL;
252 		goto out;
253 	}
254 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
255 		ret = SSH_ERR_INVALID_FORMAT;
256 		goto out;
257 	}
258 	if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0)
259 		is_webauthn = 1;
260 	else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) {
261 		ret = SSH_ERR_INVALID_FORMAT;
262 		goto out;
263 	}
264 	if (sshbuf_froms(b, &sigbuf) != 0 ||
265 	    sshbuf_get_u8(b, &sig_flags) != 0 ||
266 	    sshbuf_get_u32(b, &sig_counter) != 0) {
267 		ret = SSH_ERR_INVALID_FORMAT;
268 		goto out;
269 	}
270 	if (is_webauthn) {
271 		if (sshbuf_get_cstring(b, &webauthn_origin, NULL) != 0 ||
272 		    sshbuf_froms(b, &webauthn_wrapper) != 0 ||
273 		    sshbuf_froms(b, &webauthn_exts) != 0) {
274 			ret = SSH_ERR_INVALID_FORMAT;
275 			goto out;
276 		}
277 	}
278 	if (sshbuf_len(b) != 0) {
279 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
280 		goto out;
281 	}
282 
283 	/* parse signature */
284 	if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
285 	    sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
286 		ret = SSH_ERR_INVALID_FORMAT;
287 		goto out;
288 	}
289 	if (sshbuf_len(sigbuf) != 0) {
290 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
291 		goto out;
292 	}
293 
294 #ifdef DEBUG_SK
295 	fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen);
296 	/* sshbuf_dump_data(data, datalen, stderr); */
297 	fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
298 	free(tmp);
299 	fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
300 	free(tmp);
301 	fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
302 	    __func__, sig_flags, sig_counter);
303 	if (is_webauthn) {
304 		fprintf(stderr, "%s: webauthn origin: %s\n", __func__,
305 		    webauthn_origin);
306 		fprintf(stderr, "%s: webauthn_wrapper:\n", __func__);
307 		sshbuf_dump(webauthn_wrapper, stderr);
308 	}
309 #endif
310 	if ((esig = ECDSA_SIG_new()) == NULL) {
311 		ret = SSH_ERR_ALLOC_FAIL;
312 		goto out;
313 	}
314 	if (!ECDSA_SIG_set0(esig, sig_r, sig_s)) {
315 		ret = SSH_ERR_LIBCRYPTO_ERROR;
316 		goto out;
317 	}
318 	sig_r = sig_s = NULL; /* transferred */
319 
320 	/* Reconstruct data that was supposedly signed */
321 	if ((original_signed = sshbuf_new()) == NULL) {
322 		ret = SSH_ERR_ALLOC_FAIL;
323 		goto out;
324 	}
325 	if (is_webauthn) {
326 		if ((ret = webauthn_check_prepare_hash(data, dlen,
327 		    webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts,
328 		    msghash, sizeof(msghash))) != 0)
329 			goto out;
330 	} else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, dlen,
331 	    msghash, sizeof(msghash))) != 0)
332 		goto out;
333 	/* Application value is hashed before signature */
334 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
335 	    strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
336 		goto out;
337 #ifdef DEBUG_SK
338 	fprintf(stderr, "%s: hashed application:\n", __func__);
339 	sshbuf_dump_data(apphash, sizeof(apphash), stderr);
340 	fprintf(stderr, "%s: hashed message:\n", __func__);
341 	sshbuf_dump_data(msghash, sizeof(msghash), stderr);
342 #endif
343 	if ((ret = sshbuf_put(original_signed,
344 	    apphash, sizeof(apphash))) != 0 ||
345 	    (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
346 	    (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
347 	    (ret = sshbuf_putb(original_signed, webauthn_exts)) != 0 ||
348 	    (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
349 		goto out;
350 	details->sk_counter = sig_counter;
351 	details->sk_flags = sig_flags;
352 #ifdef DEBUG_SK
353 	fprintf(stderr, "%s: signed buf:\n", __func__);
354 	sshbuf_dump(original_signed, stderr);
355 #endif
356 
357 	if ((md_ctx = EVP_MD_CTX_new()) == NULL) {
358 		ret = SSH_ERR_ALLOC_FAIL;
359 		goto out;
360 	}
361 	if ((len = i2d_ECDSA_SIG(esig, NULL)) <= 0) {
362 		len = 0;
363 		ret = SSH_ERR_LIBCRYPTO_ERROR;
364 		goto out;
365 	}
366 	if ((sigb = calloc(1, len)) == NULL) {
367 		ret = SSH_ERR_ALLOC_FAIL;
368 		goto out;
369 	}
370 	cp = sigb; /* ASN1_item_i2d increments the pointer past the object */
371 	if (i2d_ECDSA_SIG(esig, &cp) != len) {
372 		ret = SSH_ERR_LIBCRYPTO_ERROR;
373 		goto out;
374 	}
375 #ifdef DEBUG_SK
376 	fprintf(stderr, "%s: signed hash:\n", __func__);
377 	sshbuf_dump_data(sigb, len, stderr);
378 #endif
379 	/* Verify it */
380 	if (EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL,
381 	    key->pkey) != 1) {
382 		ret = SSH_ERR_LIBCRYPTO_ERROR;
383 		goto out;
384 	}
385 	switch (EVP_DigestVerify(md_ctx, sigb, len,
386 	    sshbuf_ptr(original_signed), sshbuf_len(original_signed))) {
387 	case 1:
388 		ret = 0;
389 		break;
390 	case 0:
391 		ret = SSH_ERR_SIGNATURE_INVALID;
392 		goto out;
393 	default:
394 		ret = SSH_ERR_LIBCRYPTO_ERROR;
395 		goto out;
396 	}
397 	/* success */
398 	if (detailsp != NULL) {
399 		*detailsp = details;
400 		details = NULL;
401 	}
402  out:
403 	explicit_bzero(&sig_flags, sizeof(sig_flags));
404 	explicit_bzero(&sig_counter, sizeof(sig_counter));
405 	explicit_bzero(msghash, sizeof(msghash));
406 	explicit_bzero(apphash, sizeof(apphash));
407 	sshkey_sig_details_free(details);
408 	sshbuf_free(webauthn_wrapper);
409 	sshbuf_free(webauthn_exts);
410 	free(webauthn_origin);
411 	sshbuf_free(original_signed);
412 	sshbuf_free(sigbuf);
413 	sshbuf_free(b);
414 	ECDSA_SIG_free(esig);
415 	BN_clear_free(sig_r);
416 	BN_clear_free(sig_s);
417 	free(ktype);
418 	freezero(sigb, len);
419 	EVP_MD_CTX_free(md_ctx);
420 	return ret;
421 }
422 
423 static const struct sshkey_impl_funcs sshkey_ecdsa_sk_funcs = {
424 	/* .size = */		NULL,
425 	/* .alloc = */		NULL,
426 	/* .cleanup = */	ssh_ecdsa_sk_cleanup,
427 	/* .equal = */		ssh_ecdsa_sk_equal,
428 	/* .ssh_serialize_public = */ ssh_ecdsa_sk_serialize_public,
429 	/* .ssh_deserialize_public = */ ssh_ecdsa_sk_deserialize_public,
430 	/* .ssh_serialize_private = */ ssh_ecdsa_sk_serialize_private,
431 	/* .ssh_deserialize_private = */ ssh_ecdsa_sk_deserialize_private,
432 	/* .generate = */	NULL,
433 	/* .copy_public = */	ssh_ecdsa_sk_copy_public,
434 	/* .sign = */		NULL,
435 	/* .verify = */		ssh_ecdsa_sk_verify,
436 };
437 
438 const struct sshkey_impl sshkey_ecdsa_sk_impl = {
439 	/* .name = */		"sk-ecdsa-sha2-nistp256@openssh.com",
440 	/* .shortname = */	"ECDSA-SK",
441 	/* .sigalg = */		NULL,
442 	/* .type = */		KEY_ECDSA_SK,
443 	/* .nid = */		NID_X9_62_prime256v1,
444 	/* .cert = */		0,
445 	/* .sigonly = */	0,
446 	/* .keybits = */	256,
447 	/* .funcs = */		&sshkey_ecdsa_sk_funcs,
448 };
449 
450 const struct sshkey_impl sshkey_ecdsa_sk_cert_impl = {
451 	/* .name = */		"sk-ecdsa-sha2-nistp256-cert-v01@openssh.com",
452 	/* .shortname = */	"ECDSA-SK-CERT",
453 	/* .sigalg = */		NULL,
454 	/* .type = */		KEY_ECDSA_SK_CERT,
455 	/* .nid = */		NID_X9_62_prime256v1,
456 	/* .cert = */		1,
457 	/* .sigonly = */	0,
458 	/* .keybits = */	256,
459 	/* .funcs = */		&sshkey_ecdsa_sk_funcs,
460 };
461 
462 const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl = {
463 	/* .name = */		"webauthn-sk-ecdsa-sha2-nistp256@openssh.com",
464 	/* .shortname = */	"ECDSA-SK",
465 	/* .sigalg = */		NULL,
466 	/* .type = */		KEY_ECDSA_SK,
467 	/* .nid = */		NID_X9_62_prime256v1,
468 	/* .cert = */		0,
469 	/* .sigonly = */	1,
470 	/* .keybits = */	256,
471 	/* .funcs = */		&sshkey_ecdsa_sk_funcs,
472 };
473