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