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