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 "includes.h" 31 32 #include <sys/types.h> 33 34 #ifdef WITH_OPENSSL 35 #include <openssl/bn.h> 36 #include <openssl/ec.h> 37 #include <openssl/ecdsa.h> 38 #include <openssl/evp.h> 39 #endif 40 41 #include <string.h> 42 #include <stdio.h> /* needed for DEBUG_SK only */ 43 44 #include "openbsd-compat/openssl-compat.h" 45 46 #include "sshbuf.h" 47 #include "ssherr.h" 48 #include "digest.h" 49 #define SSHKEY_INTERNAL 50 #include "sshkey.h" 51 52 #ifndef OPENSSL_HAS_ECC 53 /* ARGSUSED */ 54 int 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) 59 { 60 return SSH_ERR_FEATURE_UNSUPPORTED; 61 } 62 #else /* OPENSSL_HAS_ECC */ 63 64 /* Reuse some ECDSA internals */ 65 extern struct sshkey_impl_funcs sshkey_ecdsa_funcs; 66 67 static void 68 ssh_ecdsa_sk_cleanup(struct sshkey *k) 69 { 70 sshkey_sk_cleanup(k); 71 sshkey_ecdsa_funcs.cleanup(k); 72 } 73 74 static int 75 ssh_ecdsa_sk_equal(const struct sshkey *a, const struct sshkey *b) 76 { 77 if (!sshkey_sk_fields_equal(a, b)) 78 return 0; 79 if (!sshkey_ecdsa_funcs.equal(a, b)) 80 return 0; 81 return 1; 82 } 83 84 static int 85 ssh_ecdsa_sk_serialize_public(const struct sshkey *key, struct sshbuf *b, 86 enum sshkey_serialize_rep opts) 87 { 88 int r; 89 90 if ((r = sshkey_ecdsa_funcs.serialize_public(key, b, opts)) != 0) 91 return r; 92 if ((r = sshkey_serialize_sk(key, b)) != 0) 93 return r; 94 95 return 0; 96 } 97 98 static int 99 ssh_ecdsa_sk_serialize_private(const struct sshkey *key, struct sshbuf *b, 100 enum sshkey_serialize_rep opts) 101 { 102 int r; 103 104 if (!sshkey_is_cert(key)) { 105 if ((r = sshkey_ecdsa_funcs.serialize_public(key, 106 b, opts)) != 0) 107 return r; 108 } 109 if ((r = sshkey_serialize_private_sk(key, b)) != 0) 110 return r; 111 112 return 0; 113 } 114 115 static int 116 ssh_ecdsa_sk_copy_public(const struct sshkey *from, struct sshkey *to) 117 { 118 int r; 119 120 if ((r = sshkey_ecdsa_funcs.copy_public(from, to)) != 0) 121 return r; 122 if ((r = sshkey_copy_public_sk(from, to)) != 0) 123 return r; 124 return 0; 125 } 126 127 static int 128 ssh_ecdsa_sk_deserialize_public(const char *ktype, struct sshbuf *b, 129 struct sshkey *key) 130 { 131 int r; 132 133 if ((r = sshkey_ecdsa_funcs.deserialize_public(ktype, b, key)) != 0) 134 return r; 135 if ((r = sshkey_deserialize_sk(b, key)) != 0) 136 return r; 137 return 0; 138 } 139 140 static int 141 ssh_ecdsa_sk_deserialize_private(const char *ktype, struct sshbuf *b, 142 struct sshkey *key) 143 { 144 int r; 145 146 if (!sshkey_is_cert(key)) { 147 if ((r = sshkey_ecdsa_funcs.deserialize_public(ktype, 148 b, key)) != 0) 149 return r; 150 } 151 if ((r = sshkey_private_deserialize_sk(b, key)) != 0) 152 return r; 153 154 return 0; 155 } 156 157 /* 158 * Check FIDO/W3C webauthn signatures clientData field against the expected 159 * format and prepare a hash of it for use in signature verification. 160 * 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. 164 * 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. 168 */ 169 static int 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) 174 { 175 int r = SSH_ERR_INTERNAL_ERROR; 176 struct sshbuf *chall = NULL, *m = NULL; 177 178 if ((m = sshbuf_new()) == NULL || 179 (chall = sshbuf_from(data, datalen)) == NULL) { 180 r = SSH_ERR_ALLOC_FAIL; 181 goto out; 182 } 183 /* 184 * Ensure origin contains no quote character and that the flags are 185 * consistent with what we received 186 */ 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; 191 goto out; 192 } 193 194 /* 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. 199 */ 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) 208 goto out; 209 #ifdef DEBUG_SK 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); 215 #endif 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) 218 goto out; 219 220 /* Prepare hash of clientData */ 221 if ((r = ssh_digest_buffer(SSH_DIGEST_SHA256, wrapper, 222 msghash, msghashlen)) != 0) 223 goto out; 224 225 /* success */ 226 r = 0; 227 out: 228 sshbuf_free(chall); 229 sshbuf_free(m); 230 return r; 231 } 232 233 /* ARGSUSED */ 234 static int 235 ssh_ecdsa_sk_verify(const struct sshkey *key, 236 const u_char *sig, size_t siglen, 237 const u_char *data, size_t dlen, const char *alg, u_int compat, 238 struct sshkey_sig_details **detailsp) 239 { 240 ECDSA_SIG *esig = NULL; 241 BIGNUM *sig_r = NULL, *sig_s = NULL; 242 u_char sig_flags; 243 u_char msghash[32], apphash[32], sighash[32]; 244 u_int sig_counter; 245 int is_webauthn = 0, ret = SSH_ERR_INTERNAL_ERROR; 246 struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL; 247 struct sshbuf *webauthn_wrapper = NULL, *webauthn_exts = NULL; 248 char *ktype = NULL, *webauthn_origin = NULL; 249 struct sshkey_sig_details *details = NULL; 250 #ifdef DEBUG_SK 251 char *tmp = NULL; 252 #endif 253 254 if (detailsp != NULL) 255 *detailsp = NULL; 256 if (key == NULL || key->ecdsa == NULL || 257 sshkey_type_plain(key->type) != KEY_ECDSA_SK || 258 sig == NULL || siglen == 0) 259 return SSH_ERR_INVALID_ARGUMENT; 260 261 if (key->ecdsa_nid != NID_X9_62_prime256v1) 262 return SSH_ERR_INTERNAL_ERROR; 263 264 /* fetch signature */ 265 if ((b = sshbuf_from(sig, siglen)) == NULL) 266 return SSH_ERR_ALLOC_FAIL; 267 if ((details = calloc(1, sizeof(*details))) == NULL) { 268 ret = SSH_ERR_ALLOC_FAIL; 269 goto out; 270 } 271 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) { 272 ret = SSH_ERR_INVALID_FORMAT; 273 goto out; 274 } 275 if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0) 276 is_webauthn = 1; 277 else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) { 278 ret = SSH_ERR_INVALID_FORMAT; 279 goto out; 280 } 281 if (sshbuf_froms(b, &sigbuf) != 0 || 282 sshbuf_get_u8(b, &sig_flags) != 0 || 283 sshbuf_get_u32(b, &sig_counter) != 0) { 284 ret = SSH_ERR_INVALID_FORMAT; 285 goto out; 286 } 287 if (is_webauthn) { 288 if (sshbuf_get_cstring(b, &webauthn_origin, NULL) != 0 || 289 sshbuf_froms(b, &webauthn_wrapper) != 0 || 290 sshbuf_froms(b, &webauthn_exts) != 0) { 291 ret = SSH_ERR_INVALID_FORMAT; 292 goto out; 293 } 294 } 295 if (sshbuf_len(b) != 0) { 296 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; 297 goto out; 298 } 299 300 /* parse signature */ 301 if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 || 302 sshbuf_get_bignum2(sigbuf, &sig_s) != 0) { 303 ret = SSH_ERR_INVALID_FORMAT; 304 goto out; 305 } 306 if (sshbuf_len(sigbuf) != 0) { 307 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; 308 goto out; 309 } 310 311 #ifdef DEBUG_SK 312 fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen); 313 /* sshbuf_dump_data(data, datalen, stderr); */ 314 fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r))); 315 free(tmp); 316 fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s))); 317 free(tmp); 318 fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n", 319 __func__, sig_flags, sig_counter); 320 if (is_webauthn) { 321 fprintf(stderr, "%s: webauthn origin: %s\n", __func__, 322 webauthn_origin); 323 fprintf(stderr, "%s: webauthn_wrapper:\n", __func__); 324 sshbuf_dump(webauthn_wrapper, stderr); 325 } 326 #endif 327 if ((esig = ECDSA_SIG_new()) == NULL) { 328 ret = SSH_ERR_ALLOC_FAIL; 329 goto out; 330 } 331 if (!ECDSA_SIG_set0(esig, sig_r, sig_s)) { 332 ret = SSH_ERR_LIBCRYPTO_ERROR; 333 goto out; 334 } 335 sig_r = sig_s = NULL; /* transferred */ 336 337 /* Reconstruct data that was supposedly signed */ 338 if ((original_signed = sshbuf_new()) == NULL) { 339 ret = SSH_ERR_ALLOC_FAIL; 340 goto out; 341 } 342 if (is_webauthn) { 343 if ((ret = webauthn_check_prepare_hash(data, dlen, 344 webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts, 345 msghash, sizeof(msghash))) != 0) 346 goto out; 347 } else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, dlen, 348 msghash, sizeof(msghash))) != 0) 349 goto out; 350 /* Application value is hashed before signature */ 351 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application, 352 strlen(key->sk_application), apphash, sizeof(apphash))) != 0) 353 goto out; 354 #ifdef DEBUG_SK 355 fprintf(stderr, "%s: hashed application:\n", __func__); 356 sshbuf_dump_data(apphash, sizeof(apphash), stderr); 357 fprintf(stderr, "%s: hashed message:\n", __func__); 358 sshbuf_dump_data(msghash, sizeof(msghash), stderr); 359 #endif 360 if ((ret = sshbuf_put(original_signed, 361 apphash, sizeof(apphash))) != 0 || 362 (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 || 363 (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 || 364 (ret = sshbuf_putb(original_signed, webauthn_exts)) != 0 || 365 (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0) 366 goto out; 367 /* Signature is over H(original_signed) */ 368 if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed, 369 sighash, sizeof(sighash))) != 0) 370 goto out; 371 details->sk_counter = sig_counter; 372 details->sk_flags = sig_flags; 373 #ifdef DEBUG_SK 374 fprintf(stderr, "%s: signed buf:\n", __func__); 375 sshbuf_dump(original_signed, stderr); 376 fprintf(stderr, "%s: signed hash:\n", __func__); 377 sshbuf_dump_data(sighash, sizeof(sighash), stderr); 378 #endif 379 380 /* Verify it */ 381 switch (ECDSA_do_verify(sighash, sizeof(sighash), esig, key->ecdsa)) { 382 case 1: 383 ret = 0; 384 break; 385 case 0: 386 ret = SSH_ERR_SIGNATURE_INVALID; 387 goto out; 388 default: 389 ret = SSH_ERR_LIBCRYPTO_ERROR; 390 goto out; 391 } 392 /* success */ 393 if (detailsp != NULL) { 394 *detailsp = details; 395 details = NULL; 396 } 397 out: 398 explicit_bzero(&sig_flags, sizeof(sig_flags)); 399 explicit_bzero(&sig_counter, sizeof(sig_counter)); 400 explicit_bzero(msghash, sizeof(msghash)); 401 explicit_bzero(sighash, sizeof(msghash)); 402 explicit_bzero(apphash, sizeof(apphash)); 403 sshkey_sig_details_free(details); 404 sshbuf_free(webauthn_wrapper); 405 sshbuf_free(webauthn_exts); 406 free(webauthn_origin); 407 sshbuf_free(original_signed); 408 sshbuf_free(sigbuf); 409 sshbuf_free(b); 410 ECDSA_SIG_free(esig); 411 BN_clear_free(sig_r); 412 BN_clear_free(sig_s); 413 free(ktype); 414 return ret; 415 } 416 417 static const struct sshkey_impl_funcs sshkey_ecdsa_sk_funcs = { 418 /* .size = */ NULL, 419 /* .alloc = */ NULL, 420 /* .cleanup = */ ssh_ecdsa_sk_cleanup, 421 /* .equal = */ ssh_ecdsa_sk_equal, 422 /* .ssh_serialize_public = */ ssh_ecdsa_sk_serialize_public, 423 /* .ssh_deserialize_public = */ ssh_ecdsa_sk_deserialize_public, 424 /* .ssh_serialize_private = */ ssh_ecdsa_sk_serialize_private, 425 /* .ssh_deserialize_private = */ ssh_ecdsa_sk_deserialize_private, 426 /* .generate = */ NULL, 427 /* .copy_public = */ ssh_ecdsa_sk_copy_public, 428 /* .sign = */ NULL, 429 /* .verify = */ ssh_ecdsa_sk_verify, 430 }; 431 432 const struct sshkey_impl sshkey_ecdsa_sk_impl = { 433 /* .name = */ "sk-ecdsa-sha2-nistp256@openssh.com", 434 /* .shortname = */ "ECDSA-SK", 435 /* .sigalg = */ NULL, 436 /* .type = */ KEY_ECDSA_SK, 437 /* .nid = */ NID_X9_62_prime256v1, 438 /* .cert = */ 0, 439 /* .sigonly = */ 0, 440 /* .keybits = */ 256, 441 /* .funcs = */ &sshkey_ecdsa_sk_funcs, 442 }; 443 444 const struct sshkey_impl sshkey_ecdsa_sk_cert_impl = { 445 /* .name = */ "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com", 446 /* .shortname = */ "ECDSA-SK-CERT", 447 /* .sigalg = */ NULL, 448 /* .type = */ KEY_ECDSA_SK_CERT, 449 /* .nid = */ NID_X9_62_prime256v1, 450 /* .cert = */ 1, 451 /* .sigonly = */ 0, 452 /* .keybits = */ 256, 453 /* .funcs = */ &sshkey_ecdsa_sk_funcs, 454 }; 455 456 const struct sshkey_impl sshkey_ecdsa_sk_webauthn_impl = { 457 /* .name = */ "webauthn-sk-ecdsa-sha2-nistp256@openssh.com", 458 /* .shortname = */ "ECDSA-SK", 459 /* .sigalg = */ NULL, 460 /* .type = */ KEY_ECDSA_SK, 461 /* .nid = */ NID_X9_62_prime256v1, 462 /* .cert = */ 0, 463 /* .sigonly = */ 1, 464 /* .keybits = */ 256, 465 /* .funcs = */ &sshkey_ecdsa_sk_funcs, 466 }; 467 468 #endif /* OPENSSL_HAS_ECC */ 469