1 /* $OpenBSD: ssh-ecdsa-sk.c,v 1.18 2023/03/08 04:43:12 guenther Exp $ */
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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
28 /* #define DEBUG_SK 1 */
32 #include <sys/types.h>
35 #include <openssl/bn.h>
36 #include <openssl/ec.h>
37 #include <openssl/ecdsa.h>
38 #include <openssl/evp.h>
42 #include <stdio.h> /* needed for DEBUG_SK only */
44 #include "openbsd-compat/openssl-compat.h"
49 #define SSHKEY_INTERNAL
52 #ifndef OPENSSL_HAS_ECC
55 ssh_ecdsa_sk_verify(const struct sshkey *key,
56 const u_char *signature, size_t signaturelen,
57 const u_char *data, size_t datalen, u_int compat,
58 struct sshkey_sig_details **detailsp)
60 return SSH_ERR_FEATURE_UNSUPPORTED;
62 #else /* OPENSSL_HAS_ECC */
64 /* Reuse some ECDSA internals */
65 extern struct sshkey_impl_funcs sshkey_ecdsa_funcs;
68 ssh_ecdsa_sk_cleanup(struct sshkey *k)
71 sshkey_ecdsa_funcs.cleanup(k);
75 ssh_ecdsa_sk_equal(const struct sshkey *a, const struct sshkey *b)
77 if (!sshkey_sk_fields_equal(a, b))
79 if (!sshkey_ecdsa_funcs.equal(a, b))
85 ssh_ecdsa_sk_serialize_public(const struct sshkey *key, struct sshbuf *b,
86 enum sshkey_serialize_rep opts)
90 if ((r = sshkey_ecdsa_funcs.serialize_public(key, b, opts)) != 0)
92 if ((r = sshkey_serialize_sk(key, b)) != 0)
99 ssh_ecdsa_sk_serialize_private(const struct sshkey *key, struct sshbuf *b,
100 enum sshkey_serialize_rep opts)
104 if (!sshkey_is_cert(key)) {
105 if ((r = sshkey_ecdsa_funcs.serialize_public(key,
109 if ((r = sshkey_serialize_private_sk(key, b)) != 0)
116 ssh_ecdsa_sk_copy_public(const struct sshkey *from, struct sshkey *to)
120 if ((r = sshkey_ecdsa_funcs.copy_public(from, to)) != 0)
122 if ((r = sshkey_copy_public_sk(from, to)) != 0)
128 ssh_ecdsa_sk_deserialize_public(const char *ktype, struct sshbuf *b,
133 if ((r = sshkey_ecdsa_funcs.deserialize_public(ktype, b, key)) != 0)
135 if ((r = sshkey_deserialize_sk(b, key)) != 0)
141 ssh_ecdsa_sk_deserialize_private(const char *ktype, struct sshbuf *b,
146 if (!sshkey_is_cert(key)) {
147 if ((r = sshkey_ecdsa_funcs.deserialize_public(ktype,
151 if ((r = sshkey_private_deserialize_sk(b, key)) != 0)
158 * Check FIDO/W3C webauthn signatures clientData field against the expected
159 * format and prepare a hash of it for use in signature verification.
161 * webauthn signatures do not sign the hash of the message directly, but
162 * instead sign a JSON-like "clientData" wrapper structure that contains the
163 * message hash along with a other information.
165 * Fortunately this structure has a fixed format so it is possible to verify
166 * that the hash of the signed message is present within the clientData
167 * structure without needing to implement any JSON parsing.
170 webauthn_check_prepare_hash(const u_char *data, size_t datalen,
171 const char *origin, const struct sshbuf *wrapper,
172 uint8_t flags, const struct sshbuf *extensions,
173 u_char *msghash, size_t msghashlen)
175 int r = SSH_ERR_INTERNAL_ERROR;
176 struct sshbuf *chall = NULL, *m = NULL;
178 if ((m = sshbuf_new()) == NULL ||
179 (chall = sshbuf_from(data, datalen)) == NULL) {
180 r = SSH_ERR_ALLOC_FAIL;
184 * Ensure origin contains no quote character and that the flags are
185 * consistent with what we received
187 if (strchr(origin, '\"') != NULL ||
188 (flags & 0x40) != 0 /* AD */ ||
189 ((flags & 0x80) == 0 /* ED */) != (sshbuf_len(extensions) == 0)) {
190 r = SSH_ERR_INVALID_FORMAT;
195 * Prepare the preamble to clientData that we expect, poking the
196 * challenge and origin into their canonical positions in the
197 * structure. The crossOrigin flag and any additional extension
198 * fields present are ignored.
200 #define WEBAUTHN_0 "{\"type\":\"webauthn.get\",\"challenge\":\""
201 #define WEBAUTHN_1 "\",\"origin\":\""
202 #define WEBAUTHN_2 "\""
203 if ((r = sshbuf_put(m, WEBAUTHN_0, sizeof(WEBAUTHN_0) - 1)) != 0 ||
204 (r = sshbuf_dtourlb64(chall, m, 0)) != 0 ||
205 (r = sshbuf_put(m, WEBAUTHN_1, sizeof(WEBAUTHN_1) - 1)) != 0 ||
206 (r = sshbuf_put(m, origin, strlen(origin))) != 0 ||
207 (r = sshbuf_put(m, WEBAUTHN_2, sizeof(WEBAUTHN_2) - 1)) != 0)
210 fprintf(stderr, "%s: received origin: %s\n", __func__, origin);
211 fprintf(stderr, "%s: received clientData:\n", __func__);
212 sshbuf_dump(wrapper, stderr);
213 fprintf(stderr, "%s: expected clientData premable:\n", __func__);
214 sshbuf_dump(m, stderr);
216 /* Check that the supplied clientData has the preamble we expect */
217 if ((r = sshbuf_cmp(wrapper, 0, sshbuf_ptr(m), sshbuf_len(m))) != 0)
220 /* Prepare hash of clientData */
221 if ((r = ssh_digest_buffer(SSH_DIGEST_SHA256, wrapper,
222 msghash, msghashlen)) != 0)
234 ssh_ecdsa_sk_verify(const struct sshkey *key,
235 const u_char *sig, size_t siglen,
236 const u_char *data, size_t dlen, const char *alg, u_int compat,
237 struct sshkey_sig_details **detailsp)
239 ECDSA_SIG *esig = NULL;
240 BIGNUM *sig_r = NULL, *sig_s = NULL;
242 u_char msghash[32], apphash[32], sighash[32];
244 int is_webauthn = 0, ret = SSH_ERR_INTERNAL_ERROR;
245 struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL;
246 struct sshbuf *webauthn_wrapper = NULL, *webauthn_exts = NULL;
247 char *ktype = NULL, *webauthn_origin = NULL;
248 struct sshkey_sig_details *details = NULL;
253 if (detailsp != NULL)
255 if (key == NULL || key->ecdsa == NULL ||
256 sshkey_type_plain(key->type) != KEY_ECDSA_SK ||
257 sig == NULL || siglen == 0)
258 return SSH_ERR_INVALID_ARGUMENT;
260 if (key->ecdsa_nid != NID_X9_62_prime256v1)
261 return SSH_ERR_INTERNAL_ERROR;
263 /* fetch signature */
264 if ((b = sshbuf_from(sig, siglen)) == NULL)
265 return SSH_ERR_ALLOC_FAIL;
266 if ((details = calloc(1, sizeof(*details))) == NULL) {
267 ret = SSH_ERR_ALLOC_FAIL;
270 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) {
271 ret = SSH_ERR_INVALID_FORMAT;
274 if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0)
276 else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) {
277 ret = SSH_ERR_INVALID_FORMAT;
280 if (sshbuf_froms(b, &sigbuf) != 0 ||
281 sshbuf_get_u8(b, &sig_flags) != 0 ||
282 sshbuf_get_u32(b, &sig_counter) != 0) {
283 ret = SSH_ERR_INVALID_FORMAT;
287 if (sshbuf_get_cstring(b, &webauthn_origin, NULL) != 0 ||
288 sshbuf_froms(b, &webauthn_wrapper) != 0 ||
289 sshbuf_froms(b, &webauthn_exts) != 0) {
290 ret = SSH_ERR_INVALID_FORMAT;
294 if (sshbuf_len(b) != 0) {
295 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
299 /* parse signature */
300 if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 ||
301 sshbuf_get_bignum2(sigbuf, &sig_s) != 0) {
302 ret = SSH_ERR_INVALID_FORMAT;
305 if (sshbuf_len(sigbuf) != 0) {
306 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
311 fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen);
312 /* sshbuf_dump_data(data, datalen, stderr); */
313 fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r)));
315 fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s)));
317 fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n",
318 __func__, sig_flags, sig_counter);
320 fprintf(stderr, "%s: webauthn origin: %s\n", __func__,
322 fprintf(stderr, "%s: webauthn_wrapper:\n", __func__);
323 sshbuf_dump(webauthn_wrapper, stderr);
326 if ((esig = ECDSA_SIG_new()) == NULL) {
327 ret = SSH_ERR_ALLOC_FAIL;
330 if (!ECDSA_SIG_set0(esig, sig_r, sig_s)) {
331 ret = SSH_ERR_LIBCRYPTO_ERROR;
334 sig_r = sig_s = NULL; /* transferred */
336 /* Reconstruct data that was supposedly signed */
337 if ((original_signed = sshbuf_new()) == NULL) {
338 ret = SSH_ERR_ALLOC_FAIL;
342 if ((ret = webauthn_check_prepare_hash(data, dlen,
343 webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts,
344 msghash, sizeof(msghash))) != 0)
346 } else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, dlen,
347 msghash, sizeof(msghash))) != 0)
349 /* Application value is hashed before signature */
350 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application,
351 strlen(key->sk_application), apphash, sizeof(apphash))) != 0)
354 fprintf(stderr, "%s: hashed application:\n", __func__);
355 sshbuf_dump_data(apphash, sizeof(apphash), stderr);
356 fprintf(stderr, "%s: hashed message:\n", __func__);
357 sshbuf_dump_data(msghash, sizeof(msghash), stderr);
359 if ((ret = sshbuf_put(original_signed,
360 apphash, sizeof(apphash))) != 0 ||
361 (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 ||
362 (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 ||
363 (ret = sshbuf_putb(original_signed, webauthn_exts)) != 0 ||
364 (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0)
366 /* Signature is over H(original_signed) */
367 if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed,
368 sighash, sizeof(sighash))) != 0)
370 details->sk_counter = sig_counter;
371 details->sk_flags = sig_flags;
373 fprintf(stderr, "%s: signed buf:\n", __func__);
374 sshbuf_dump(original_signed, stderr);
375 fprintf(stderr, "%s: signed hash:\n", __func__);
376 sshbuf_dump_data(sighash, sizeof(sighash), stderr);
380 switch (ECDSA_do_verify(sighash, sizeof(sighash), esig, key->ecdsa)) {
385 ret = SSH_ERR_SIGNATURE_INVALID;
388 ret = SSH_ERR_LIBCRYPTO_ERROR;
392 if (detailsp != NULL) {
397 explicit_bzero(&sig_flags, sizeof(sig_flags));
398 explicit_bzero(&sig_counter, sizeof(sig_counter));
399 explicit_bzero(msghash, sizeof(msghash));
400 explicit_bzero(sighash, sizeof(msghash));
401 explicit_bzero(apphash, sizeof(apphash));
402 sshkey_sig_details_free(details);
403 sshbuf_free(webauthn_wrapper);
404 sshbuf_free(webauthn_exts);
405 free(webauthn_origin);
406 sshbuf_free(original_signed);
409 ECDSA_SIG_free(esig);
410 BN_clear_free(sig_r);
411 BN_clear_free(sig_s);
416 static const struct sshkey_impl_funcs sshkey_ecdsa_sk_funcs = {
419 /* .cleanup = */ ssh_ecdsa_sk_cleanup,
420 /* .equal = */ ssh_ecdsa_sk_equal,
421 /* .ssh_serialize_public = */ ssh_ecdsa_sk_serialize_public,
422 /* .ssh_deserialize_public = */ ssh_ecdsa_sk_deserialize_public,
423 /* .ssh_serialize_private = */ ssh_ecdsa_sk_serialize_private,
424 /* .ssh_deserialize_private = */ ssh_ecdsa_sk_deserialize_private,
425 /* .generate = */ NULL,
426 /* .copy_public = */ ssh_ecdsa_sk_copy_public,
428 /* .verify = */ ssh_ecdsa_sk_verify,
431 const struct sshkey_impl sshkey_ecdsa_sk_impl = {
432 /* .name = */ "sk-ecdsa-sha2-nistp256@openssh.com",
433 /* .shortname = */ "ECDSA-SK",
434 /* .sigalg = */ NULL,
435 /* .type = */ KEY_ECDSA_SK,
436 /* .nid = */ NID_X9_62_prime256v1,
439 /* .keybits = */ 256,
440 /* .funcs = */ &sshkey_ecdsa_sk_funcs,
443 const struct sshkey_impl sshkey_ecdsa_sk_cert_impl = {
444 /* .name = */ "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com",
445 /* .shortname = */ "ECDSA-SK-CERT",
446 /* .sigalg = */ NULL,
447 /* .type = */ KEY_ECDSA_SK_CERT,
448 /* .nid = */ NID_X9_62_prime256v1,
451 /* .keybits = */ 256,
452 /* .funcs = */ &sshkey_ecdsa_sk_funcs,
455 const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl = {
456 /* .name = */ "webauthn-sk-ecdsa-sha2-nistp256@openssh.com",
457 /* .shortname = */ "ECDSA-SK",
458 /* .sigalg = */ NULL,
459 /* .type = */ KEY_ECDSA_SK,
460 /* .nid = */ NID_X9_62_prime256v1,
463 /* .keybits = */ 256,
464 /* .funcs = */ &sshkey_ecdsa_sk_funcs,
467 #endif /* OPENSSL_HAS_ECC */