1 /* $OpenBSD: ssh-ecdsa-sk.c,v 1.8 2020/06/22 23:44:27 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 <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 46 /* 47 * Check FIDO/W3C webauthn signatures clientData field against the expected 48 * format and prepare a hash of it for use in signature verification. 49 * 50 * webauthn signatures do not sign the hash of the message directly, but 51 * instead sign a JSON-like "clientData" wrapper structure that contains the 52 * message hash along with a other information. 53 * 54 * Fortunately this structure has a fixed format so it is possible to verify 55 * that the hash of the signed message is present within the clientData 56 * structure without needing to implement any JSON parsing. 57 */ 58 static int 59 webauthn_check_prepare_hash(const u_char *data, size_t datalen, 60 const char *origin, const struct sshbuf *wrapper, 61 uint8_t flags, const struct sshbuf *extensions, 62 u_char *msghash, size_t msghashlen) 63 { 64 int r = SSH_ERR_INTERNAL_ERROR; 65 struct sshbuf *chall = NULL, *m = NULL; 66 67 if ((m = sshbuf_new()) == NULL || 68 (chall = sshbuf_from(data, datalen)) == NULL) { 69 r = SSH_ERR_ALLOC_FAIL; 70 goto out; 71 } 72 /* 73 * Ensure origin contains no quote character and that the flags are 74 * consistent with what we received 75 */ 76 if (strchr(origin, '\"') != NULL || 77 (flags & 0x40) != 0 /* AD */ || 78 ((flags & 0x80) == 0 /* ED */) != (sshbuf_len(extensions) == 0)) { 79 r = SSH_ERR_INVALID_FORMAT; 80 goto out; 81 } 82 83 /* 84 * Prepare the preamble to clientData that we expect, poking the 85 * challenge and origin into their canonical positions in the 86 * structure. The crossOrigin flag and any additional extension 87 * fields present are ignored. 88 */ 89 #define WEBAUTHN_0 "{\"type\":\"webauthn.get\",\"challenge\":\"" 90 #define WEBAUTHN_1 "\",\"origin\":\"" 91 #define WEBAUTHN_2 "\"" 92 if ((r = sshbuf_put(m, WEBAUTHN_0, sizeof(WEBAUTHN_0) - 1)) != 0 || 93 (r = sshbuf_dtourlb64(chall, m, 0)) != 0 || 94 (r = sshbuf_put(m, WEBAUTHN_1, sizeof(WEBAUTHN_1) - 1)) != 0 || 95 (r = sshbuf_put(m, origin, strlen(origin))) != 0 || 96 (r = sshbuf_put(m, WEBAUTHN_2, sizeof(WEBAUTHN_2) - 1)) != 0) 97 goto out; 98 #ifdef DEBUG_SK 99 fprintf(stderr, "%s: received origin: %s\n", __func__, origin); 100 fprintf(stderr, "%s: received clientData:\n", __func__); 101 sshbuf_dump(wrapper, stderr); 102 fprintf(stderr, "%s: expected clientData premable:\n", __func__); 103 sshbuf_dump(m, stderr); 104 #endif 105 /* Check that the supplied clientData has the preamble we expect */ 106 if ((r = sshbuf_cmp(wrapper, 0, sshbuf_ptr(m), sshbuf_len(m))) != 0) 107 goto out; 108 109 /* Prepare hash of clientData */ 110 if ((r = ssh_digest_buffer(SSH_DIGEST_SHA256, wrapper, 111 msghash, msghashlen)) != 0) 112 goto out; 113 114 /* success */ 115 r = 0; 116 out: 117 sshbuf_free(chall); 118 sshbuf_free(m); 119 return r; 120 } 121 122 /* ARGSUSED */ 123 int 124 ssh_ecdsa_sk_verify(const struct sshkey *key, 125 const u_char *signature, size_t signaturelen, 126 const u_char *data, size_t datalen, u_int compat, 127 struct sshkey_sig_details **detailsp) 128 { 129 ECDSA_SIG *sig = NULL; 130 BIGNUM *sig_r = NULL, *sig_s = NULL; 131 u_char sig_flags; 132 u_char msghash[32], apphash[32], sighash[32]; 133 u_int sig_counter; 134 int is_webauthn = 0, ret = SSH_ERR_INTERNAL_ERROR; 135 struct sshbuf *b = NULL, *sigbuf = NULL, *original_signed = NULL; 136 struct sshbuf *webauthn_wrapper = NULL, *webauthn_exts = NULL; 137 char *ktype = NULL, *webauthn_origin = NULL; 138 struct sshkey_sig_details *details = NULL; 139 #ifdef DEBUG_SK 140 char *tmp = NULL; 141 #endif 142 143 if (detailsp != NULL) 144 *detailsp = NULL; 145 if (key == NULL || key->ecdsa == NULL || 146 sshkey_type_plain(key->type) != KEY_ECDSA_SK || 147 signature == NULL || signaturelen == 0) 148 return SSH_ERR_INVALID_ARGUMENT; 149 150 if (key->ecdsa_nid != NID_X9_62_prime256v1) 151 return SSH_ERR_INTERNAL_ERROR; 152 153 /* fetch signature */ 154 if ((b = sshbuf_from(signature, signaturelen)) == NULL) 155 return SSH_ERR_ALLOC_FAIL; 156 if ((details = calloc(1, sizeof(*details))) == NULL) { 157 ret = SSH_ERR_ALLOC_FAIL; 158 goto out; 159 } 160 if (sshbuf_get_cstring(b, &ktype, NULL) != 0) { 161 ret = SSH_ERR_INVALID_FORMAT; 162 goto out; 163 } 164 if (strcmp(ktype, "webauthn-sk-ecdsa-sha2-nistp256@openssh.com") == 0) 165 is_webauthn = 1; 166 else if (strcmp(ktype, "sk-ecdsa-sha2-nistp256@openssh.com") != 0) { 167 ret = SSH_ERR_INVALID_FORMAT; 168 goto out; 169 } 170 if (sshbuf_froms(b, &sigbuf) != 0 || 171 sshbuf_get_u8(b, &sig_flags) != 0 || 172 sshbuf_get_u32(b, &sig_counter) != 0) { 173 ret = SSH_ERR_INVALID_FORMAT; 174 goto out; 175 } 176 if (is_webauthn) { 177 if (sshbuf_get_cstring(b, &webauthn_origin, NULL) != 0 || 178 sshbuf_froms(b, &webauthn_wrapper) != 0 || 179 sshbuf_froms(b, &webauthn_exts) != 0) { 180 ret = SSH_ERR_INVALID_FORMAT; 181 goto out; 182 } 183 } 184 if (sshbuf_len(b) != 0) { 185 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; 186 goto out; 187 } 188 189 /* parse signature */ 190 if (sshbuf_get_bignum2(sigbuf, &sig_r) != 0 || 191 sshbuf_get_bignum2(sigbuf, &sig_s) != 0) { 192 ret = SSH_ERR_INVALID_FORMAT; 193 goto out; 194 } 195 if (sshbuf_len(sigbuf) != 0) { 196 ret = SSH_ERR_UNEXPECTED_TRAILING_DATA; 197 goto out; 198 } 199 200 #ifdef DEBUG_SK 201 fprintf(stderr, "%s: data: (len %zu)\n", __func__, datalen); 202 /* sshbuf_dump_data(data, datalen, stderr); */ 203 fprintf(stderr, "%s: sig_r: %s\n", __func__, (tmp = BN_bn2hex(sig_r))); 204 free(tmp); 205 fprintf(stderr, "%s: sig_s: %s\n", __func__, (tmp = BN_bn2hex(sig_s))); 206 free(tmp); 207 fprintf(stderr, "%s: sig_flags = 0x%02x, sig_counter = %u\n", 208 __func__, sig_flags, sig_counter); 209 if (is_webauthn) { 210 fprintf(stderr, "%s: webauthn origin: %s\n", __func__, 211 webauthn_origin); 212 fprintf(stderr, "%s: webauthn_wrapper:\n", __func__); 213 sshbuf_dump(webauthn_wrapper, stderr); 214 } 215 #endif 216 if ((sig = ECDSA_SIG_new()) == NULL) { 217 ret = SSH_ERR_ALLOC_FAIL; 218 goto out; 219 } 220 if (!ECDSA_SIG_set0(sig, sig_r, sig_s)) { 221 ret = SSH_ERR_LIBCRYPTO_ERROR; 222 goto out; 223 } 224 sig_r = sig_s = NULL; /* transferred */ 225 226 /* Reconstruct data that was supposedly signed */ 227 if ((original_signed = sshbuf_new()) == NULL) { 228 ret = SSH_ERR_ALLOC_FAIL; 229 goto out; 230 } 231 if (is_webauthn) { 232 if ((ret = webauthn_check_prepare_hash(data, datalen, 233 webauthn_origin, webauthn_wrapper, sig_flags, webauthn_exts, 234 msghash, sizeof(msghash))) != 0) 235 goto out; 236 } else if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen, 237 msghash, sizeof(msghash))) != 0) 238 goto out; 239 /* Application value is hashed before signature */ 240 if ((ret = ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application, 241 strlen(key->sk_application), apphash, sizeof(apphash))) != 0) 242 goto out; 243 #ifdef DEBUG_SK 244 fprintf(stderr, "%s: hashed application:\n", __func__); 245 sshbuf_dump_data(apphash, sizeof(apphash), stderr); 246 fprintf(stderr, "%s: hashed message:\n", __func__); 247 sshbuf_dump_data(msghash, sizeof(msghash), stderr); 248 #endif 249 if ((ret = sshbuf_put(original_signed, 250 apphash, sizeof(apphash))) != 0 || 251 (ret = sshbuf_put_u8(original_signed, sig_flags)) != 0 || 252 (ret = sshbuf_put_u32(original_signed, sig_counter)) != 0 || 253 (ret = sshbuf_putb(original_signed, webauthn_exts)) != 0 || 254 (ret = sshbuf_put(original_signed, msghash, sizeof(msghash))) != 0) 255 goto out; 256 /* Signature is over H(original_signed) */ 257 if ((ret = ssh_digest_buffer(SSH_DIGEST_SHA256, original_signed, 258 sighash, sizeof(sighash))) != 0) 259 goto out; 260 details->sk_counter = sig_counter; 261 details->sk_flags = sig_flags; 262 #ifdef DEBUG_SK 263 fprintf(stderr, "%s: signed buf:\n", __func__); 264 sshbuf_dump(original_signed, stderr); 265 fprintf(stderr, "%s: signed hash:\n", __func__); 266 sshbuf_dump_data(sighash, sizeof(sighash), stderr); 267 #endif 268 269 /* Verify it */ 270 switch (ECDSA_do_verify(sighash, sizeof(sighash), sig, key->ecdsa)) { 271 case 1: 272 ret = 0; 273 break; 274 case 0: 275 ret = SSH_ERR_SIGNATURE_INVALID; 276 goto out; 277 default: 278 ret = SSH_ERR_LIBCRYPTO_ERROR; 279 goto out; 280 } 281 /* success */ 282 if (detailsp != NULL) { 283 *detailsp = details; 284 details = NULL; 285 } 286 out: 287 explicit_bzero(&sig_flags, sizeof(sig_flags)); 288 explicit_bzero(&sig_counter, sizeof(sig_counter)); 289 explicit_bzero(msghash, sizeof(msghash)); 290 explicit_bzero(sighash, sizeof(msghash)); 291 explicit_bzero(apphash, sizeof(apphash)); 292 sshkey_sig_details_free(details); 293 sshbuf_free(webauthn_wrapper); 294 sshbuf_free(webauthn_exts); 295 free(webauthn_origin); 296 sshbuf_free(original_signed); 297 sshbuf_free(sigbuf); 298 sshbuf_free(b); 299 ECDSA_SIG_free(sig); 300 BN_clear_free(sig_r); 301 BN_clear_free(sig_s); 302 free(ktype); 303 return ret; 304 } 305