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/eddsa.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 ecdsa[] = \ 27 "-----BEGIN PUBLIC KEY-----\n" 28 "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEOwiq14c80b7C1Jzsx5w1zMvk2GgW\n" 29 "5kfGMOKXjwF/U+51ZfBDKehs3ivdeXAJBkxIh7E3iA32s+HyNqk+ntl9fg==\n" 30 "-----END PUBLIC KEY-----\n"; 31 32 static const char eddsa[] = \ 33 "-----BEGIN PUBLIC KEY-----\n" 34 "MCowBQYDK2VwAyEADt/RHErAxAHxH9FUmsjOhQ2ALl6Y8nE0m3zQxkEE2iM=\n" 35 "-----END PUBLIC KEY-----\n"; 36 37 static const unsigned char eddsa_raw[] = { 38 0x0e, 0xdf, 0xd1, 0x1c, 0x4a, 0xc0, 0xc4, 0x01, 39 0xf1, 0x1f, 0xd1, 0x54, 0x9a, 0xc8, 0xce, 0x85, 40 0x0d, 0x80, 0x2e, 0x5e, 0x98, 0xf2, 0x71, 0x34, 41 0x9b, 0x7c, 0xd0, 0xc6, 0x41, 0x04, 0xda, 0x23, 42 }; 43 44 static EVP_PKEY * 45 EVP_PKEY_from_PEM(const char *ptr, size_t len) 46 { 47 BIO *bio = NULL; 48 EVP_PKEY *pkey = NULL; 49 50 if ((bio = BIO_new(BIO_s_mem())) == NULL) { 51 warnx("BIO_new"); 52 goto out; 53 } 54 if (len > INT_MAX || BIO_write(bio, ptr, (int)len) != (int)len) { 55 warnx("BIO_write"); 56 goto out; 57 } 58 if ((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)) == NULL) 59 warnx("PEM_read_bio_PUBKEY"); 60 out: 61 BIO_free(bio); 62 63 return pkey; 64 } 65 66 static int 67 eddsa_pk_cmp(const char *ptr, size_t len) 68 { 69 EVP_PKEY *pkA = NULL; 70 EVP_PKEY *pkB = NULL; 71 eddsa_pk_t *k = NULL; 72 int r, ok = -1; 73 74 if ((pkA = EVP_PKEY_from_PEM(ptr, len)) == NULL) { 75 warnx("EVP_PKEY_from_PEM"); 76 goto out; 77 } 78 if ((k = eddsa_pk_new()) == NULL) { 79 warnx("eddsa_pk_new"); 80 goto out; 81 } 82 if ((r = eddsa_pk_from_EVP_PKEY(k, pkA)) != FIDO_OK) { 83 warnx("eddsa_pk_from_EVP_PKEY: 0x%x", r); 84 goto out; 85 } 86 if ((pkB = eddsa_pk_to_EVP_PKEY(k)) == NULL) { 87 warnx("eddsa_pk_to_EVP_PKEY"); 88 goto out; 89 } 90 if ((r = EVP_PKEY_cmp(pkA, pkB)) != 1) { 91 warnx("EVP_PKEY_cmp: %d", r); 92 goto out; 93 } 94 95 ok = 0; 96 out: 97 EVP_PKEY_free(pkA); 98 EVP_PKEY_free(pkB); 99 eddsa_pk_free(&k); 100 101 return ok; 102 } 103 104 static void 105 invalid_key(void) 106 { 107 EVP_PKEY *pkey; 108 eddsa_pk_t *pk; 109 110 ASSERT_NOT_NULL((pkey = EVP_PKEY_from_PEM(ecdsa, sizeof(ecdsa)))); 111 ASSERT_NOT_NULL((pk = eddsa_pk_new())); 112 ASSERT_INVAL(eddsa_pk_from_EVP_PKEY(pk, pkey)); 113 114 EVP_PKEY_free(pkey); 115 eddsa_pk_free(&pk); 116 } 117 118 static void 119 valid_key(void) 120 { 121 EVP_PKEY *pkeyA = NULL; 122 EVP_PKEY *pkeyB = NULL; 123 eddsa_pk_t *pkA = NULL; 124 eddsa_pk_t *pkB = NULL; 125 126 #if defined(LIBRESSL_VERSION_NUMBER) 127 /* incomplete support; test what we can */ 128 ASSERT_NULL(EVP_PKEY_from_PEM(eddsa, sizeof(eddsa))); 129 ASSERT_NOT_NULL((pkB = eddsa_pk_new())); 130 ASSERT_INVAL(eddsa_pk_from_ptr(pkB, eddsa_raw, sizeof(eddsa_raw))); 131 ASSERT_NULL(eddsa_pk_to_EVP_PKEY((const eddsa_pk_t *)eddsa_raw)); 132 assert(eddsa_pk_cmp(eddsa, sizeof(eddsa)) < 0); 133 #else 134 ASSERT_NOT_NULL((pkeyA = EVP_PKEY_from_PEM(eddsa, sizeof(eddsa)))); 135 ASSERT_NOT_NULL((pkA = eddsa_pk_new())); 136 ASSERT_NOT_NULL((pkB = eddsa_pk_new())); 137 ASSERT_OK(eddsa_pk_from_EVP_PKEY(pkA, pkeyA)); 138 ASSERT_OK(eddsa_pk_from_ptr(pkB, eddsa_raw, sizeof(eddsa_raw))); 139 ASSERT_NOT_NULL((pkeyB = eddsa_pk_to_EVP_PKEY((const eddsa_pk_t *)eddsa_raw))); 140 assert(EVP_PKEY_cmp(pkeyA, pkeyB) == 1); 141 assert(eddsa_pk_cmp(eddsa, sizeof(eddsa)) == 0); 142 #endif 143 144 EVP_PKEY_free(pkeyA); 145 EVP_PKEY_free(pkeyB); 146 eddsa_pk_free(&pkA); 147 eddsa_pk_free(&pkB); 148 } 149 150 int 151 main(void) 152 { 153 fido_init(0); 154 155 invalid_key(); 156 valid_key(); 157 158 exit(0); 159 } 160