xref: /openbsd-src/usr.bin/ssh/ssh-ecdsa-sk.c (revision 3374c67d44f9b75b98444cbf63020f777792342e)
1 /* $OpenBSD: ssh-ecdsa-sk.c,v 1.17 2022/10/28 00:44:44 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
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
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
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
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
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
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
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
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 /* ARGSUSED */
216 static int
217 ssh_ecdsa_sk_verify(const struct sshkey *key,
218     const u_char *sig, size_t siglen,
219     const u_char *data, size_t dlen, const char *alg, u_int compat,
220     struct sshkey_sig_details **detailsp)
221 {
222 	ECDSA_SIG *esig = NULL;
223 	BIGNUM *sig_r = NULL, *sig_s = NULL;
224 	u_char sig_flags;
225 	u_char msghash[32], apphash[32], sighash[32];
226 	u_int sig_counter;
227 	int is_webauthn = 0, ret = SSH_ERR_INTERNAL_ERROR;
228 	struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
229 	struct sshbuf *webauthn_wrapper = NULL, *webauthn_exts = NULL;
230 	char *ktype = NULL, *webauthn_origin = NULL;
231 	struct sshkey_sig_details *details = NULL;
232 #ifdef DEBUG_SK
233 	char *tmp = NULL;
234 #endif
235 
236 	if (detailsp != NULL)
237 		*detailsp = NULL;
238 	if (key == NULL || key->ecdsa == NULL ||
239 	    sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
240 	    sig == NULL || siglen == 0)
241 		return SSH_ERR_INVALID_ARGUMENT;
242 
243 	if (key->ecdsa_nid != NID_X9_62_prime256v1)
244 		return SSH_ERR_INTERNAL_ERROR;
245 
246 	/* fetch signature */
247 	if ((b = sshbuf_from(sig, siglen)) == NULL)
248 		return SSH_ERR_ALLOC_FAIL;
249 	if ((details = calloc(1, sizeof(*details))) == NULL) {
250 		ret = SSH_ERR_ALLOC_FAIL;
251 		goto out;
252 	}
253 	if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
254 		ret = SSH_ERR_INVALID_FORMAT;
255 		goto out;
256 	}
257 	if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0)
258 		is_webauthn = 1;
259 	else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) {
260 		ret = SSH_ERR_INVALID_FORMAT;
261 		goto out;
262 	}
263 	if (sshbuf_froms(b, &sigbuf) != 0 ||
264 	    sshbuf_get_u8(b, &sig_flags) != 0 ||
265 	    sshbuf_get_u32(b, &sig_counter) != 0) {
266 		ret = SSH_ERR_INVALID_FORMAT;
267 		goto out;
268 	}
269 	if (is_webauthn) {
270 		if (sshbuf_get_cstring(b, &webauthn_origin, NULL) != 0 ||
271 		    sshbuf_froms(b, &webauthn_wrapper) != 0 ||
272 		    sshbuf_froms(b, &webauthn_exts) != 0) {
273 			ret = SSH_ERR_INVALID_FORMAT;
274 			goto out;
275 		}
276 	}
277 	if (sshbuf_len(b) != 0) {
278 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
279 		goto out;
280 	}
281 
282 	/* parse signature */
283 	if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
284 	    sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
285 		ret = SSH_ERR_INVALID_FORMAT;
286 		goto out;
287 	}
288 	if (sshbuf_len(sigbuf) != 0) {
289 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
290 		goto out;
291 	}
292 
293 #ifdef DEBUG_SK
294 	fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen);
295 	/* sshbuf_dump_data(data, datalen, stderr); */
296 	fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
297 	free(tmp);
298 	fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
299 	free(tmp);
300 	fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
301 	    __func__, sig_flags, sig_counter);
302 	if (is_webauthn) {
303 		fprintf(stderr, "%s: webauthn origin: %s\n", __func__,
304 		    webauthn_origin);
305 		fprintf(stderr, "%s: webauthn_wrapper:\n", __func__);
306 		sshbuf_dump(webauthn_wrapper, stderr);
307 	}
308 #endif
309 	if ((esig = ECDSA_SIG_new()) == NULL) {
310 		ret = SSH_ERR_ALLOC_FAIL;
311 		goto out;
312 	}
313 	if (!ECDSA_SIG_set0(esig, sig_r, sig_s)) {
314 		ret = SSH_ERR_LIBCRYPTO_ERROR;
315 		goto out;
316 	}
317 	sig_r = sig_s = NULL; /* transferred */
318 
319 	/* Reconstruct data that was supposedly signed */
320 	if ((original_signed = sshbuf_new()) == NULL) {
321 		ret = SSH_ERR_ALLOC_FAIL;
322 		goto out;
323 	}
324 	if (is_webauthn) {
325 		if ((ret = webauthn_check_prepare_hash(data, dlen,
326 		    webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts,
327 		    msghash, sizeof(msghash))) != 0)
328 			goto out;
329 	} else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, dlen,
330 	    msghash, sizeof(msghash))) != 0)
331 		goto out;
332 	/* Application value is hashed before signature */
333 	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
334 	    strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
335 		goto out;
336 #ifdef DEBUG_SK
337 	fprintf(stderr, "%s: hashed application:\n", __func__);
338 	sshbuf_dump_data(apphash, sizeof(apphash), stderr);
339 	fprintf(stderr, "%s: hashed message:\n", __func__);
340 	sshbuf_dump_data(msghash, sizeof(msghash), stderr);
341 #endif
342 	if ((ret = sshbuf_put(original_signed,
343 	    apphash, sizeof(apphash))) != 0 ||
344 	    (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
345 	    (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
346 	    (ret = sshbuf_putb(original_signed, webauthn_exts)) != 0 ||
347 	    (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
348 		goto out;
349 	/* Signature is over H(original_signed) */
350 	if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
351 	    sighash, sizeof(sighash))) != 0)
352 		goto out;
353 	details->sk_counter = sig_counter;
354 	details->sk_flags = sig_flags;
355 #ifdef DEBUG_SK
356 	fprintf(stderr, "%s: signed buf:\n", __func__);
357 	sshbuf_dump(original_signed, stderr);
358 	fprintf(stderr, "%s: signed hash:\n", __func__);
359 	sshbuf_dump_data(sighash, sizeof(sighash), stderr);
360 #endif
361 
362 	/* Verify it */
363 	switch (ECDSA_do_verify(sighash, sizeof(sighash), esig, key->ecdsa)) {
364 	case 1:
365 		ret = 0;
366 		break;
367 	case 0:
368 		ret = SSH_ERR_SIGNATURE_INVALID;
369 		goto out;
370 	default:
371 		ret = SSH_ERR_LIBCRYPTO_ERROR;
372 		goto out;
373 	}
374 	/* success */
375 	if (detailsp != NULL) {
376 		*detailsp = details;
377 		details = NULL;
378 	}
379  out:
380 	explicit_bzero(&sig_flags, sizeof(sig_flags));
381 	explicit_bzero(&sig_counter, sizeof(sig_counter));
382 	explicit_bzero(msghash, sizeof(msghash));
383 	explicit_bzero(sighash, sizeof(msghash));
384 	explicit_bzero(apphash, sizeof(apphash));
385 	sshkey_sig_details_free(details);
386 	sshbuf_free(webauthn_wrapper);
387 	sshbuf_free(webauthn_exts);
388 	free(webauthn_origin);
389 	sshbuf_free(original_signed);
390 	sshbuf_free(sigbuf);
391 	sshbuf_free(b);
392 	ECDSA_SIG_free(esig);
393 	BN_clear_free(sig_r);
394 	BN_clear_free(sig_s);
395 	free(ktype);
396 	return ret;
397 }
398 
399 static const struct sshkey_impl_funcs sshkey_ecdsa_sk_funcs = {
400 	/* .size = */		NULL,
401 	/* .alloc = */		NULL,
402 	/* .cleanup = */	ssh_ecdsa_sk_cleanup,
403 	/* .equal = */		ssh_ecdsa_sk_equal,
404 	/* .ssh_serialize_public = */ ssh_ecdsa_sk_serialize_public,
405 	/* .ssh_deserialize_public = */ ssh_ecdsa_sk_deserialize_public,
406 	/* .ssh_serialize_private = */ ssh_ecdsa_sk_serialize_private,
407 	/* .ssh_deserialize_private = */ ssh_ecdsa_sk_deserialize_private,
408 	/* .generate = */	NULL,
409 	/* .copy_public = */	ssh_ecdsa_sk_copy_public,
410 	/* .sign = */		NULL,
411 	/* .verify = */		ssh_ecdsa_sk_verify,
412 };
413 
414 const struct sshkey_impl sshkey_ecdsa_sk_impl = {
415 	/* .name = */		"sk-ecdsa-sha2-nistp256@openssh.com",
416 	/* .shortname = */	"ECDSA-SK",
417 	/* .sigalg = */		NULL,
418 	/* .type = */		KEY_ECDSA_SK,
419 	/* .nid = */		NID_X9_62_prime256v1,
420 	/* .cert = */		0,
421 	/* .sigonly = */	0,
422 	/* .keybits = */	256,
423 	/* .funcs = */		&sshkey_ecdsa_sk_funcs,
424 };
425 
426 const struct sshkey_impl sshkey_ecdsa_sk_cert_impl = {
427 	/* .name = */		"sk-ecdsa-sha2-nistp256-cert-v01@openssh.com",
428 	/* .shortname = */	"ECDSA-SK-CERT",
429 	/* .sigalg = */		NULL,
430 	/* .type = */		KEY_ECDSA_SK_CERT,
431 	/* .nid = */		NID_X9_62_prime256v1,
432 	/* .cert = */		1,
433 	/* .sigonly = */	0,
434 	/* .keybits = */	256,
435 	/* .funcs = */		&sshkey_ecdsa_sk_funcs,
436 };
437 
438 const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl = {
439 	/* .name = */		"webauthn-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 = */	1,
446 	/* .keybits = */	256,
447 	/* .funcs = */		&sshkey_ecdsa_sk_funcs,
448 };
449