Annotation of src/usr.bin/ssh/ssh-ecdsa-sk.c, Revision 1.16
1.16 ! djm 1: /* $OpenBSD: ssh-ecdsa-sk.c,v 1.15 2022/10/28 00:43:08 djm Exp $ */
1.1 djm 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:
1.10 djm 46: /* Reuse some ECDSA internals */
47: extern struct sshkey_impl_funcs sshkey_ecdsa_funcs;
48:
1.9 djm 49: static void
50: ssh_ecdsa_sk_cleanup(struct sshkey *k)
51: {
1.10 djm 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;
1.9 djm 64: }
65:
1.11 djm 66: static int
67: ssh_ecdsa_sk_serialize_public(const struct sshkey *key, struct sshbuf *b,
1.14 djm 68: enum sshkey_serialize_rep opts)
1.11 djm 69: {
70: int r;
71:
1.14 djm 72: if ((r = sshkey_ecdsa_funcs.serialize_public(key, b, opts)) != 0)
1.11 djm 73: return r;
74: if ((r = sshkey_serialize_sk(key, b)) != 0)
75: return r;
76:
77: return 0;
78: }
79:
1.13 djm 80: static int
1.16 ! djm 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
1.13 djm 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:
1.14 djm 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:
1.7 djm 122: /*
123: * Check FIDO/W3C webauthn signatures clientData field against the expected
124: * format and prepare a hash of it for use in signature verification.
125: *
126: * webauthn signatures do not sign the hash of the message directly, but
127: * instead sign a JSON-like "clientData" wrapper structure that contains the
128: * message hash along with a other information.
129: *
130: * Fortunately this structure has a fixed format so it is possible to verify
131: * that the hash of the signed message is present within the clientData
132: * structure without needing to implement any JSON parsing.
133: */
134: static int
135: webauthn_check_prepare_hash(const u_char *data, size_t datalen,
136: const char *origin, const struct sshbuf *wrapper,
137: uint8_t flags, const struct sshbuf *extensions,
138: u_char *msghash, size_t msghashlen)
139: {
140: int r = SSH_ERR_INTERNAL_ERROR;
141: struct sshbuf *chall = NULL, *m = NULL;
142:
143: if ((m = sshbuf_new()) == NULL ||
144: (chall = sshbuf_from(data, datalen)) == NULL) {
145: r = SSH_ERR_ALLOC_FAIL;
146: goto out;
147: }
148: /*
149: * Ensure origin contains no quote character and that the flags are
150: * consistent with what we received
151: */
152: if (strchr(origin, '\"') != NULL ||
153: (flags & 0x40) != 0 /* AD */ ||
154: ((flags & 0x80) == 0 /* ED */) != (sshbuf_len(extensions) == 0)) {
155: r = SSH_ERR_INVALID_FORMAT;
156: goto out;
157: }
1.8 djm 158:
159: /*
160: * Prepare the preamble to clientData that we expect, poking the
161: * challenge and origin into their canonical positions in the
162: * structure. The crossOrigin flag and any additional extension
163: * fields present are ignored.
164: */
1.7 djm 165: #define WEBAUTHN_0 "{\"type\":\"webauthn.get\",\"challenge\":\""
166: #define WEBAUTHN_1 "\",\"origin\":\""
167: #define WEBAUTHN_2 "\""
168: if ((r = sshbuf_put(m, WEBAUTHN_0, sizeof(WEBAUTHN_0) - 1)) != 0 ||
169: (r = sshbuf_dtourlb64(chall, m, 0)) != 0 ||
170: (r = sshbuf_put(m, WEBAUTHN_1, sizeof(WEBAUTHN_1) - 1)) != 0 ||
171: (r = sshbuf_put(m, origin, strlen(origin))) != 0 ||
172: (r = sshbuf_put(m, WEBAUTHN_2, sizeof(WEBAUTHN_2) - 1)) != 0)
173: goto out;
174: #ifdef DEBUG_SK
175: fprintf(stderr, "%s: received origin: %s\n", __func__, origin);
176: fprintf(stderr, "%s: received clientData:\n", __func__);
177: sshbuf_dump(wrapper, stderr);
178: fprintf(stderr, "%s: expected clientData premable:\n", __func__);
179: sshbuf_dump(m, stderr);
180: #endif
1.8 djm 181: /* Check that the supplied clientData has the preamble we expect */
1.7 djm 182: if ((r = sshbuf_cmp(wrapper, 0, sshbuf_ptr(m), sshbuf_len(m))) != 0)
183: goto out;
184:
185: /* Prepare hash of clientData */
186: if ((r = ssh_digest_buffer(SSH_DIGEST_SHA256, wrapper,
187: msghash, msghashlen)) != 0)
188: goto out;
189:
190: /* success */
191: r = 0;
192: out:
193: sshbuf_free(chall);
194: sshbuf_free(m);
195: return r;
196: }
197:
1.1 djm 198: /* ARGSUSED */
1.15 djm 199: static int
1.1 djm 200: ssh_ecdsa_sk_verify(const struct sshkey *key,
1.15 djm 201: const u_char *sig, size_t siglen,
202: const u_char *data, size_t dlen, const char *alg, u_int compat,
1.4 djm 203: struct sshkey_sig_details **detailsp)
1.1 djm 204: {
1.15 djm 205: ECDSA_SIG *esig = NULL;
1.1 djm 206: BIGNUM *sig_r = NULL, *sig_s = NULL;
207: u_char sig_flags;
208: u_char msghash[32], apphash[32], sighash[32];
209: u_int sig_counter;
1.7 djm 210: int is_webauthn = 0, ret = SSH_ERR_INTERNAL_ERROR;
1.1 djm 211: struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
1.7 djm 212: struct sshbuf *webauthn_wrapper = NULL, *webauthn_exts = NULL;
213: char *ktype = NULL, *webauthn_origin = NULL;
1.4 djm 214: struct sshkey_sig_details *details = NULL;
1.1 djm 215: #ifdef DEBUG_SK
216: char *tmp = NULL;
217: #endif
218:
1.4 djm 219: if (detailsp != NULL)
220: *detailsp = NULL;
1.1 djm 221: if (key == NULL || key->ecdsa == NULL ||
222: sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
1.15 djm 223: sig == NULL || siglen == 0)
1.1 djm 224: return SSH_ERR_INVALID_ARGUMENT;
225:
226: if (key->ecdsa_nid != NID_X9_62_prime256v1)
227: return SSH_ERR_INTERNAL_ERROR;
228:
229: /* fetch signature */
1.15 djm 230: if ((b = sshbuf_from(sig, siglen)) == NULL)
1.1 djm 231: return SSH_ERR_ALLOC_FAIL;
1.6 djm 232: if ((details = calloc(1, sizeof(*details))) == NULL) {
233: ret = SSH_ERR_ALLOC_FAIL;
234: goto out;
235: }
236: if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
237: ret = SSH_ERR_INVALID_FORMAT;
238: goto out;
239: }
1.7 djm 240: if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0)
241: is_webauthn = 1;
242: else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) {
1.6 djm 243: ret = SSH_ERR_INVALID_FORMAT;
244: goto out;
245: }
246: if (sshbuf_froms(b, &sigbuf) != 0 ||
1.2 djm 247: sshbuf_get_u8(b, &sig_flags) != 0 ||
248: sshbuf_get_u32(b, &sig_counter) != 0) {
1.1 djm 249: ret = SSH_ERR_INVALID_FORMAT;
250: goto out;
251: }
1.7 djm 252: if (is_webauthn) {
253: if (sshbuf_get_cstring(b, &webauthn_origin, NULL) != 0 ||
254: sshbuf_froms(b, &webauthn_wrapper) != 0 ||
255: sshbuf_froms(b, &webauthn_exts) != 0) {
256: ret = SSH_ERR_INVALID_FORMAT;
257: goto out;
258: }
259: }
1.1 djm 260: if (sshbuf_len(b) != 0) {
261: ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
262: goto out;
263: }
264:
265: /* parse signature */
266: if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
1.2 djm 267: sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
1.1 djm 268: ret = SSH_ERR_INVALID_FORMAT;
269: goto out;
270: }
1.6 djm 271: if (sshbuf_len(sigbuf) != 0) {
272: ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
1.1 djm 273: goto out;
274: }
1.7 djm 275:
1.1 djm 276: #ifdef DEBUG_SK
1.5 djm 277: fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen);
278: /* sshbuf_dump_data(data, datalen, stderr); */
1.1 djm 279: fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
280: free(tmp);
281: fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
282: free(tmp);
283: fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
284: __func__, sig_flags, sig_counter);
1.7 djm 285: if (is_webauthn) {
286: fprintf(stderr, "%s: webauthn origin: %s\n", __func__,
287: webauthn_origin);
288: fprintf(stderr, "%s: webauthn_wrapper:\n", __func__);
289: sshbuf_dump(webauthn_wrapper, stderr);
290: }
1.1 djm 291: #endif
1.15 djm 292: if ((esig = ECDSA_SIG_new()) == NULL) {
1.6 djm 293: ret = SSH_ERR_ALLOC_FAIL;
294: goto out;
295: }
1.15 djm 296: if (!ECDSA_SIG_set0(esig, sig_r, sig_s)) {
1.6 djm 297: ret = SSH_ERR_LIBCRYPTO_ERROR;
1.1 djm 298: goto out;
299: }
1.6 djm 300: sig_r = sig_s = NULL; /* transferred */
1.1 djm 301:
302: /* Reconstruct data that was supposedly signed */
1.3 djm 303: if ((original_signed = sshbuf_new()) == NULL) {
304: ret = SSH_ERR_ALLOC_FAIL;
305: goto out;
306: }
1.7 djm 307: if (is_webauthn) {
1.15 djm 308: if ((ret = webauthn_check_prepare_hash(data, dlen,
1.7 djm 309: webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts,
310: msghash, sizeof(msghash))) != 0)
311: goto out;
1.15 djm 312: } else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, dlen,
1.1 djm 313: msghash, sizeof(msghash))) != 0)
314: goto out;
315: /* Application value is hashed before signature */
316: if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
317: strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
318: goto out;
319: #ifdef DEBUG_SK
1.5 djm 320: fprintf(stderr, "%s: hashed application:\n", __func__);
321: sshbuf_dump_data(apphash, sizeof(apphash), stderr);
1.1 djm 322: fprintf(stderr, "%s: hashed message:\n", __func__);
323: sshbuf_dump_data(msghash, sizeof(msghash), stderr);
324: #endif
325: if ((ret = sshbuf_put(original_signed,
326: apphash, sizeof(apphash))) != 0 ||
327: (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
328: (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
1.7 djm 329: (ret = sshbuf_putb(original_signed, webauthn_exts)) != 0 ||
1.1 djm 330: (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
331: goto out;
332: /* Signature is over H(original_signed) */
333: if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
334: sighash, sizeof(sighash))) != 0)
335: goto out;
1.4 djm 336: details->sk_counter = sig_counter;
337: details->sk_flags = sig_flags;
1.1 djm 338: #ifdef DEBUG_SK
339: fprintf(stderr, "%s: signed buf:\n", __func__);
340: sshbuf_dump(original_signed, stderr);
341: fprintf(stderr, "%s: signed hash:\n", __func__);
342: sshbuf_dump_data(sighash, sizeof(sighash), stderr);
343: #endif
344:
345: /* Verify it */
1.15 djm 346: switch (ECDSA_do_verify(sighash, sizeof(sighash), esig, key->ecdsa)) {
1.1 djm 347: case 1:
348: ret = 0;
349: break;
350: case 0:
351: ret = SSH_ERR_SIGNATURE_INVALID;
352: goto out;
353: default:
354: ret = SSH_ERR_LIBCRYPTO_ERROR;
355: goto out;
356: }
1.4 djm 357: /* success */
358: if (detailsp != NULL) {
359: *detailsp = details;
360: details = NULL;
361: }
1.1 djm 362: out:
363: explicit_bzero(&sig_flags, sizeof(sig_flags));
364: explicit_bzero(&sig_counter, sizeof(sig_counter));
365: explicit_bzero(msghash, sizeof(msghash));
366: explicit_bzero(sighash, sizeof(msghash));
367: explicit_bzero(apphash, sizeof(apphash));
1.4 djm 368: sshkey_sig_details_free(details);
1.7 djm 369: sshbuf_free(webauthn_wrapper);
370: sshbuf_free(webauthn_exts);
371: free(webauthn_origin);
1.1 djm 372: sshbuf_free(original_signed);
373: sshbuf_free(sigbuf);
374: sshbuf_free(b);
1.15 djm 375: ECDSA_SIG_free(esig);
1.1 djm 376: BN_clear_free(sig_r);
377: BN_clear_free(sig_s);
378: free(ktype);
379: return ret;
380: }
1.9 djm 381:
382: static const struct sshkey_impl_funcs sshkey_ecdsa_sk_funcs = {
383: /* .size = */ NULL,
384: /* .alloc = */ NULL,
385: /* .cleanup = */ ssh_ecdsa_sk_cleanup,
1.10 djm 386: /* .equal = */ ssh_ecdsa_sk_equal,
1.11 djm 387: /* .ssh_serialize_public = */ ssh_ecdsa_sk_serialize_public,
1.14 djm 388: /* .ssh_deserialize_public = */ ssh_ecdsa_sk_deserialize_public,
1.16 ! djm 389: /* .ssh_serialize_private = */ ssh_ecdsa_sk_serialize_private,
1.12 djm 390: /* .generate = */ NULL,
1.13 djm 391: /* .copy_public = */ ssh_ecdsa_sk_copy_public,
1.15 djm 392: /* .sign = */ NULL,
393: /* .verify = */ ssh_ecdsa_sk_verify,
1.9 djm 394: };
395:
396: const struct sshkey_impl sshkey_ecdsa_sk_impl = {
397: /* .name = */ "sk-ecdsa-sha2-nistp256@openssh.com",
398: /* .shortname = */ "ECDSA-SK",
399: /* .sigalg = */ NULL,
400: /* .type = */ KEY_ECDSA_SK,
401: /* .nid = */ NID_X9_62_prime256v1,
402: /* .cert = */ 0,
403: /* .sigonly = */ 0,
404: /* .keybits = */ 256,
405: /* .funcs = */ &sshkey_ecdsa_sk_funcs,
406: };
407:
408: const struct sshkey_impl sshkey_ecdsa_sk_cert_impl = {
409: /* .name = */ "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com",
410: /* .shortname = */ "ECDSA-SK-CERT",
411: /* .sigalg = */ NULL,
412: /* .type = */ KEY_ECDSA_SK_CERT,
413: /* .nid = */ NID_X9_62_prime256v1,
414: /* .cert = */ 1,
415: /* .sigonly = */ 0,
416: /* .keybits = */ 256,
417: /* .funcs = */ &sshkey_ecdsa_sk_funcs,
418: };
419:
420: const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl = {
421: /* .name = */ "webauthn-sk-ecdsa-sha2-nistp256@openssh.com",
422: /* .shortname = */ "ECDSA-SK",
423: /* .sigalg = */ NULL,
424: /* .type = */ KEY_ECDSA_SK,
425: /* .nid = */ NID_X9_62_prime256v1,
426: /* .cert = */ 0,
427: /* .sigonly = */ 1,
428: /* .keybits = */ 256,
429: /* .funcs = */ &sshkey_ecdsa_sk_funcs,
430: };