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 #undef NDEBUG 9 10 #include <assert.h> 11 #include <string.h> 12 13 #define _FIDO_INTERNAL 14 15 #include <fido.h> 16 #include <fido/es256.h> 17 18 #include <openssl/bio.h> 19 #include <openssl/pem.h> 20 21 #define ASSERT_NOT_NULL(e) assert((e) != NULL) 22 #define ASSERT_NULL(e) assert((e) == NULL) 23 #define ASSERT_INVAL(e) assert((e) == FIDO_ERR_INVALID_ARGUMENT) 24 #define ASSERT_OK(e) assert((e) == FIDO_OK) 25 26 static const char short_x[] = \ 27 "-----BEGIN PUBLIC KEY-----\n" 28 "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEAAeeHTZj4LEbt7Czs+u5gEZJfnGE\n" 29 "6Z+YLe4AYu7SoGY7IH/2jKifsA7w+lkURL4DL63oEjd3f8foH9bX4eaVug==\n" 30 "-----END PUBLIC KEY-----"; 31 32 static const char short_y[] = \ 33 "-----BEGIN PUBLIC KEY-----\n" 34 "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEL8CWUP1r0tpJ5QmkzLc69O74C/Ti\n" 35 "83hTiys/JFNVkp0ArW3pKt5jNRrgWSZYE4S/D3AMtpqifFXz/FLCzJqojQ==\n" 36 "-----END PUBLIC KEY-----\n"; 37 38 static const char p256k1[] = \ 39 "-----BEGIN PUBLIC KEY-----\n" 40 "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEU1y8c0Jg9FGr3vYChpEo9c4dpkijriYM\n" 41 "QzU/DeskC89hZjLNH1Sj8ra2MsBlVGGJTNPCZSyx8Jo7ERapxdN7UQ==\n" 42 "-----END PUBLIC KEY-----\n"; 43 44 static const char p256v1[] = \ 45 "-----BEGIN PUBLIC KEY-----\n" 46 "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEOwiq14c80b7C1Jzsx5w1zMvk2GgW\n" 47 "5kfGMOKXjwF/U+51ZfBDKehs3ivdeXAJBkxIh7E3iA32s+HyNqk+ntl9fg==\n" 48 "-----END PUBLIC KEY-----\n"; 49 50 static const unsigned char p256k1_raw[] = { 51 0x04, 0x53, 0x5c, 0xbc, 0x73, 0x42, 0x60, 0xf4, 52 0x51, 0xab, 0xde, 0xf6, 0x02, 0x86, 0x91, 0x28, 53 0xf5, 0xce, 0x1d, 0xa6, 0x48, 0xa3, 0xae, 0x26, 54 0x0c, 0x43, 0x35, 0x3f, 0x0d, 0xeb, 0x24, 0x0b, 55 0xcf, 0x61, 0x66, 0x32, 0xcd, 0x1f, 0x54, 0xa3, 56 0xf2, 0xb6, 0xb6, 0x32, 0xc0, 0x65, 0x54, 0x61, 57 0x89, 0x4c, 0xd3, 0xc2, 0x65, 0x2c, 0xb1, 0xf0, 58 0x9a, 0x3b, 0x11, 0x16, 0xa9, 0xc5, 0xd3, 0x7b, 59 0x51, 60 }; 61 62 static const unsigned char p256v1_raw[] = { 63 0x04, 0x3b, 0x08, 0xaa, 0xd7, 0x87, 0x3c, 0xd1, 64 0xbe, 0xc2, 0xd4, 0x9c, 0xec, 0xc7, 0x9c, 0x35, 65 0xcc, 0xcb, 0xe4, 0xd8, 0x68, 0x16, 0xe6, 0x47, 66 0xc6, 0x30, 0xe2, 0x97, 0x8f, 0x01, 0x7f, 0x53, 67 0xee, 0x75, 0x65, 0xf0, 0x43, 0x29, 0xe8, 0x6c, 68 0xde, 0x2b, 0xdd, 0x79, 0x70, 0x09, 0x06, 0x4c, 69 0x48, 0x87, 0xb1, 0x37, 0x88, 0x0d, 0xf6, 0xb3, 70 0xe1, 0xf2, 0x36, 0xa9, 0x3e, 0x9e, 0xd9, 0x7d, 71 0x7e, 72 }; 73 74 static EVP_PKEY * 75 EVP_PKEY_from_PEM(const char *ptr, size_t len) 76 { 77 BIO *bio = NULL; 78 EVP_PKEY *pkey = NULL; 79 80 if ((bio = BIO_new(BIO_s_mem())) == NULL) { 81 warnx("BIO_new"); 82 goto out; 83 } 84 if (len > INT_MAX || BIO_write(bio, ptr, (int)len) != (int)len) { 85 warnx("BIO_write"); 86 goto out; 87 } 88 if ((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)) == NULL) 89 warnx("PEM_read_bio_PUBKEY"); 90 out: 91 BIO_free(bio); 92 93 return pkey; 94 } 95 96 static int 97 es256_pk_cmp(const char *ptr, size_t len) 98 { 99 EVP_PKEY *pkA = NULL; 100 EVP_PKEY *pkB = NULL; 101 es256_pk_t *k = NULL; 102 int r, ok = -1; 103 104 if ((pkA = EVP_PKEY_from_PEM(ptr, len)) == NULL) { 105 warnx("EVP_PKEY_from_PEM"); 106 goto out; 107 } 108 if ((k = es256_pk_new()) == NULL) { 109 warnx("es256_pk_new"); 110 goto out; 111 } 112 if ((r = es256_pk_from_EVP_PKEY(k, pkA)) != FIDO_OK) { 113 warnx("es256_pk_from_EVP_PKEY: 0x%x", r); 114 goto out; 115 } 116 if ((pkB = es256_pk_to_EVP_PKEY(k)) == NULL) { 117 warnx("es256_pk_to_EVP_PKEY"); 118 goto out; 119 } 120 if ((r = EVP_PKEY_cmp(pkA, pkB)) != 1) { 121 warnx("EVP_PKEY_cmp: %d", r); 122 goto out; 123 } 124 125 ok = 0; 126 out: 127 EVP_PKEY_free(pkA); 128 EVP_PKEY_free(pkB); 129 es256_pk_free(&k); 130 131 return ok; 132 } 133 134 static void 135 short_coord(void) 136 { 137 assert(es256_pk_cmp(short_x, sizeof(short_x)) == 0); 138 assert(es256_pk_cmp(short_y, sizeof(short_y)) == 0); 139 } 140 141 static void 142 invalid_curve(const unsigned char *raw, size_t raw_len) 143 { 144 EVP_PKEY *pkey; 145 es256_pk_t *pk; 146 147 ASSERT_NOT_NULL((pkey = EVP_PKEY_from_PEM(p256k1, sizeof(p256k1)))); 148 ASSERT_NOT_NULL((pk = es256_pk_new())); 149 ASSERT_INVAL(es256_pk_from_EVP_PKEY(pk, pkey)); 150 ASSERT_INVAL(es256_pk_from_ptr(pk, raw, raw_len)); 151 ASSERT_NULL(es256_pk_to_EVP_PKEY((const es256_pk_t *)raw)); 152 153 EVP_PKEY_free(pkey); 154 es256_pk_free(&pk); 155 } 156 157 static void 158 full_coord(void) 159 { 160 assert(es256_pk_cmp(p256v1, sizeof(p256v1)) == 0); 161 } 162 163 static void 164 valid_curve(const unsigned char *raw, size_t raw_len) 165 { 166 EVP_PKEY *pkeyA; 167 EVP_PKEY *pkeyB; 168 es256_pk_t *pkA; 169 es256_pk_t *pkB; 170 171 ASSERT_NOT_NULL((pkeyA = EVP_PKEY_from_PEM(p256v1, sizeof(p256v1)))); 172 ASSERT_NOT_NULL((pkA = es256_pk_new())); 173 ASSERT_NOT_NULL((pkB = es256_pk_new())); 174 ASSERT_OK(es256_pk_from_EVP_PKEY(pkA, pkeyA)); 175 ASSERT_OK(es256_pk_from_ptr(pkB, raw, raw_len)); 176 ASSERT_NOT_NULL((pkeyB = es256_pk_to_EVP_PKEY(pkB))); 177 assert(EVP_PKEY_cmp(pkeyA, pkeyB) == 1); 178 179 EVP_PKEY_free(pkeyA); 180 EVP_PKEY_free(pkeyB); 181 es256_pk_free(&pkA); 182 es256_pk_free(&pkB); 183 } 184 185 int 186 main(void) 187 { 188 fido_init(0); 189 190 short_coord(); 191 full_coord(); 192 193 invalid_curve(p256k1_raw, sizeof(p256k1_raw)); /* uncompressed */ 194 invalid_curve(p256k1_raw + 1, sizeof(p256k1_raw) - 1); /* libfido2 */ 195 valid_curve(p256v1_raw, sizeof(p256v1_raw)); /* uncompressed */ 196 valid_curve(p256v1_raw + 1, sizeof(p256v1_raw) - 1); /* libfido2 */ 197 198 exit(0); 199 } 200