1 /* 2 * Copyright (c) 2022 Yubico AB. All rights reserved. 3 * Use of this source code is governed by a BSD-style 4 * license that can be found in the LICENSE file. 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8 #include <openssl/bn.h> 9 #include <openssl/ecdsa.h> 10 #include <openssl/obj_mac.h> 11 12 #include "fido.h" 13 #include "fido/es384.h" 14 15 #if OPENSSL_VERSION_NUMBER >= 0x30000000 16 #define get0_EC_KEY(x) EVP_PKEY_get0_EC_KEY((x)) 17 #else 18 #define get0_EC_KEY(x) EVP_PKEY_get0((x)) 19 #endif 20 21 static int 22 decode_coord(const cbor_item_t *item, void *xy, size_t xy_len) 23 { 24 if (cbor_isa_bytestring(item) == false || 25 cbor_bytestring_is_definite(item) == false || 26 cbor_bytestring_length(item) != xy_len) { 27 fido_log_debug("%s: cbor type", __func__); 28 return (-1); 29 } 30 31 memcpy(xy, cbor_bytestring_handle(item), xy_len); 32 33 return (0); 34 } 35 36 static int 37 decode_pubkey_point(const cbor_item_t *key, const cbor_item_t *val, void *arg) 38 { 39 es384_pk_t *k = arg; 40 41 if (cbor_isa_negint(key) == false || 42 cbor_int_get_width(key) != CBOR_INT_8) 43 return (0); /* ignore */ 44 45 switch (cbor_get_uint8(key)) { 46 case 1: /* x coordinate */ 47 return (decode_coord(val, &k->x, sizeof(k->x))); 48 case 2: /* y coordinate */ 49 return (decode_coord(val, &k->y, sizeof(k->y))); 50 } 51 52 return (0); /* ignore */ 53 } 54 55 int 56 es384_pk_decode(const cbor_item_t *item, es384_pk_t *k) 57 { 58 if (cbor_isa_map(item) == false || 59 cbor_map_is_definite(item) == false || 60 cbor_map_iter(item, k, decode_pubkey_point) < 0) { 61 fido_log_debug("%s: cbor type", __func__); 62 return (-1); 63 } 64 65 return (0); 66 } 67 68 es384_pk_t * 69 es384_pk_new(void) 70 { 71 return (calloc(1, sizeof(es384_pk_t))); 72 } 73 74 void 75 es384_pk_free(es384_pk_t **pkp) 76 { 77 es384_pk_t *pk; 78 79 if (pkp == NULL || (pk = *pkp) == NULL) 80 return; 81 82 freezero(pk, sizeof(*pk)); 83 *pkp = NULL; 84 } 85 86 int 87 es384_pk_from_ptr(es384_pk_t *pk, const void *ptr, size_t len) 88 { 89 const uint8_t *p = ptr; 90 EVP_PKEY *pkey; 91 92 if (len < sizeof(*pk)) 93 return (FIDO_ERR_INVALID_ARGUMENT); 94 95 if (len == sizeof(*pk) + 1 && *p == 0x04) 96 memcpy(pk, ++p, sizeof(*pk)); /* uncompressed format */ 97 else 98 memcpy(pk, ptr, sizeof(*pk)); /* libfido2 x||y format */ 99 100 if ((pkey = es384_pk_to_EVP_PKEY(pk)) == NULL) { 101 fido_log_debug("%s: es384_pk_to_EVP_PKEY", __func__); 102 explicit_bzero(pk, sizeof(*pk)); 103 return (FIDO_ERR_INVALID_ARGUMENT); 104 } 105 106 EVP_PKEY_free(pkey); 107 108 return (FIDO_OK); 109 } 110 111 EVP_PKEY * 112 es384_pk_to_EVP_PKEY(const es384_pk_t *k) 113 { 114 BN_CTX *bnctx = NULL; 115 EC_KEY *ec = NULL; 116 EC_POINT *q = NULL; 117 EVP_PKEY *pkey = NULL; 118 BIGNUM *x = NULL; 119 BIGNUM *y = NULL; 120 const EC_GROUP *g = NULL; 121 int ok = -1; 122 123 if ((bnctx = BN_CTX_new()) == NULL) 124 goto fail; 125 126 BN_CTX_start(bnctx); 127 128 if ((x = BN_CTX_get(bnctx)) == NULL || 129 (y = BN_CTX_get(bnctx)) == NULL) 130 goto fail; 131 132 if (BN_bin2bn(k->x, sizeof(k->x), x) == NULL || 133 BN_bin2bn(k->y, sizeof(k->y), y) == NULL) { 134 fido_log_debug("%s: BN_bin2bn", __func__); 135 goto fail; 136 } 137 138 if ((ec = EC_KEY_new_by_curve_name(NID_secp384r1)) == NULL || 139 (g = EC_KEY_get0_group(ec)) == NULL) { 140 fido_log_debug("%s: EC_KEY init", __func__); 141 goto fail; 142 } 143 144 if ((q = EC_POINT_new(g)) == NULL || 145 EC_POINT_set_affine_coordinates_GFp(g, q, x, y, bnctx) == 0 || 146 EC_KEY_set_public_key(ec, q) == 0) { 147 fido_log_debug("%s: EC_KEY_set_public_key", __func__); 148 goto fail; 149 } 150 151 if ((pkey = EVP_PKEY_new()) == NULL || 152 EVP_PKEY_assign_EC_KEY(pkey, ec) == 0) { 153 fido_log_debug("%s: EVP_PKEY_assign_EC_KEY", __func__); 154 goto fail; 155 } 156 157 ec = NULL; /* at this point, ec belongs to evp */ 158 159 ok = 0; 160 fail: 161 if (bnctx != NULL) { 162 BN_CTX_end(bnctx); 163 BN_CTX_free(bnctx); 164 } 165 166 if (ec != NULL) 167 EC_KEY_free(ec); 168 if (q != NULL) 169 EC_POINT_free(q); 170 171 if (ok < 0 && pkey != NULL) { 172 EVP_PKEY_free(pkey); 173 pkey = NULL; 174 } 175 176 return (pkey); 177 } 178 179 int 180 es384_pk_from_EC_KEY(es384_pk_t *pk, const EC_KEY *ec) 181 { 182 BN_CTX *bnctx = NULL; 183 BIGNUM *x = NULL; 184 BIGNUM *y = NULL; 185 const EC_POINT *q = NULL; 186 EC_GROUP *g = NULL; 187 size_t dx; 188 size_t dy; 189 int ok = FIDO_ERR_INTERNAL; 190 int nx; 191 int ny; 192 193 if ((q = EC_KEY_get0_public_key(ec)) == NULL || 194 (g = EC_GROUP_new_by_curve_name(NID_secp384r1)) == NULL || 195 (bnctx = BN_CTX_new()) == NULL) 196 goto fail; 197 198 BN_CTX_start(bnctx); 199 200 if ((x = BN_CTX_get(bnctx)) == NULL || 201 (y = BN_CTX_get(bnctx)) == NULL) 202 goto fail; 203 204 if (EC_POINT_is_on_curve(g, q, bnctx) != 1) { 205 fido_log_debug("%s: EC_POINT_is_on_curve", __func__); 206 ok = FIDO_ERR_INVALID_ARGUMENT; 207 goto fail; 208 } 209 210 if (EC_POINT_get_affine_coordinates_GFp(g, q, x, y, bnctx) == 0 || 211 (nx = BN_num_bytes(x)) < 0 || (size_t)nx > sizeof(pk->x) || 212 (ny = BN_num_bytes(y)) < 0 || (size_t)ny > sizeof(pk->y)) { 213 fido_log_debug("%s: EC_POINT_get_affine_coordinates_GFp", 214 __func__); 215 goto fail; 216 } 217 218 dx = sizeof(pk->x) - (size_t)nx; 219 dy = sizeof(pk->y) - (size_t)ny; 220 221 if ((nx = BN_bn2bin(x, pk->x + dx)) < 0 || (size_t)nx > sizeof(pk->x) || 222 (ny = BN_bn2bin(y, pk->y + dy)) < 0 || (size_t)ny > sizeof(pk->y)) { 223 fido_log_debug("%s: BN_bn2bin", __func__); 224 goto fail; 225 } 226 227 ok = FIDO_OK; 228 fail: 229 EC_GROUP_free(g); 230 231 if (bnctx != NULL) { 232 BN_CTX_end(bnctx); 233 BN_CTX_free(bnctx); 234 } 235 236 return (ok); 237 } 238 239 int 240 es384_pk_from_EVP_PKEY(es384_pk_t *pk, const EVP_PKEY *pkey) 241 { 242 const EC_KEY *ec; 243 244 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC || 245 (ec = get0_EC_KEY(pkey)) == NULL) 246 return (FIDO_ERR_INVALID_ARGUMENT); 247 248 return (es384_pk_from_EC_KEY(pk, ec)); 249 } 250 251 int 252 es384_verify_sig(const fido_blob_t *dgst, EVP_PKEY *pkey, 253 const fido_blob_t *sig) 254 { 255 EVP_PKEY_CTX *pctx = NULL; 256 int ok = -1; 257 258 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) { 259 fido_log_debug("%s: EVP_PKEY_base_id", __func__); 260 goto fail; 261 } 262 263 if ((pctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL || 264 EVP_PKEY_verify_init(pctx) != 1 || 265 EVP_PKEY_verify(pctx, sig->ptr, sig->len, dgst->ptr, 266 dgst->len) != 1) { 267 fido_log_debug("%s: EVP_PKEY_verify", __func__); 268 goto fail; 269 } 270 271 ok = 0; 272 fail: 273 EVP_PKEY_CTX_free(pctx); 274 275 return (ok); 276 } 277 278 int 279 es384_pk_verify_sig(const fido_blob_t *dgst, const es384_pk_t *pk, 280 const fido_blob_t *sig) 281 { 282 EVP_PKEY *pkey; 283 int ok = -1; 284 285 if ((pkey = es384_pk_to_EVP_PKEY(pk)) == NULL || 286 es384_verify_sig(dgst, pkey, sig) < 0) { 287 fido_log_debug("%s: es384_verify_sig", __func__); 288 goto fail; 289 } 290 291 ok = 0; 292 fail: 293 EVP_PKEY_free(pkey); 294 295 return (ok); 296 } 297