xref: /freebsd/contrib/libfido2/regress/rs256.c (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
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/rs256.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 char rsa1024[] = \
27 "-----BEGIN PUBLIC KEY-----\n"
28 "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCw92gn9Ku/bEfFj1AutaZyltpf\n"
29 "zzXrg70kQFymNq+spMt/HlxKiImw8TZU08zWW4ZLE/Ch4JYjMW6ETAdQFhSC63Ih\n"
30 "Wecui0JJ1f+2CsUVg+h7lO1877LZYUpdNiJrbqMb5Yc4N3FPtvdl3NoLIIQsF76H\n"
31 "VRvpjQgkWipRfZ97JQIDAQAB\n"
32 "-----END PUBLIC KEY-----";
33 
34 static char rsa2048[] = \
35 "-----BEGIN PUBLIC KEY-----\n"
36 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApvIq/55ZodBIxzo/8BnE\n"
37 "UQN1fo1hmJ6V20hQHSzJq5tHyxRCcvKikuJ1ZvR4RdZlEzdTdbEfMBdZ8sxve0/U\n"
38 "yYEjH92CG0vgTCYuUaFLJTaWZSvWa96G8Lw+V4VyNFDRCM7sflOaSVH5pAsz8OEc\n"
39 "TLZfM4NhnDsJAM+mQ6X7Tza0sczPchgDA+9KByXo/VIqyuBQs17rlKC2reMa8NkY\n"
40 "rBRQZJLNzi68d5/BHH1flGWE1l8wJ9dr1Ex93H/KdzX+7/28TWUC98nneUo8RfRx\n"
41 "FwUt/EInDMHOORCaCHSs28U/9IUyMjqLB1rxKhIp09yGXMiTrrT+p+Pcn8dO01HT\n"
42 "vQIDAQAB\n"
43 "-----END PUBLIC KEY-----";
44 
45 static char rsa3072[] = \
46 "-----BEGIN PUBLIC KEY-----\n"
47 "MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAwZunKrMs/o92AniLPNTF\n"
48 "Ta4EYfhy5NDmMvQvRFT/eTYItLrOTPmYMap68KLyZYmgz/AdaxAL/992QWre7XTY\n"
49 "gqLwtZT+WsSu7xPHWKTTXrlVohKBeLHQ0I7Zy0NSMUxhlJEMrBAjSyFAS86zWm5w\n"
50 "ctC3pNCqfUKugA07BVj+d5Mv5fziwgMR86kuhkVuMYfsR4IYwX4+va0pyLzxx624\n"
51 "s9nJ107g+A+3MUk4bAto3lruFeeZPUI2AFzFQbGg5By6VtvVi3gKQ7lUNtAr0Onu\n"
52 "I6Fb+yz8sbFcvDpJcu5CXW20GrKMVP4KY5pn2LCajWuZjBl/dXWayPfm4UX5Y2O4\n"
53 "73tzPpUBNwnEdz79His0v80Vmvjwn5IuF2jAoimrBNPJFFwCCuVNy8kgj2vllk1l\n"
54 "RvLOG6hf8VnlDb40QZS3QAQ09xFfF+xlVLb8cHH6wllaAGEM230TrmawpC7xpz4Z\n"
55 "sTuwJwI0AWEi//noMsRz2BuF2fCp//aORYJQU2S8kYk3AgMBAAE=\n"
56 "-----END PUBLIC KEY-----";
57 
58 static const unsigned char rsa2048_raw[] = {
59 	0xa6, 0xf2, 0x2a, 0xff, 0x9e, 0x59, 0xa1, 0xd0,
60 	0x48, 0xc7, 0x3a, 0x3f, 0xf0, 0x19, 0xc4, 0x51,
61 	0x03, 0x75, 0x7e, 0x8d, 0x61, 0x98, 0x9e, 0x95,
62 	0xdb, 0x48, 0x50, 0x1d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7, 0x79, 0x4a, 0x3c, 0x45, 0xf4, 0x71, 0x17,
85 	0x05, 0x2d, 0xfc, 0x42, 0x27, 0x0c, 0xc1, 0xce,
86 	0x39, 0x10, 0x9a, 0x08, 0x74, 0xac, 0xdb, 0xc5,
87 	0x3f, 0xf4, 0x85, 0x32, 0x32, 0x3a, 0x8b, 0x07,
88 	0x5a, 0xf1, 0x2a, 0x12, 0x29, 0xd3, 0xdc, 0x86,
89 	0x5c, 0xc8, 0x93, 0xae, 0xb4, 0xfe, 0xa7, 0xe3,
90 	0xdc, 0x9f, 0xc7, 0x4e, 0xd3, 0x51, 0xd3, 0xbd,
91 	0x01, 0x00, 0x01,
92 };
93 
94 static EVP_PKEY *
95 EVP_PKEY_from_PEM(const char *ptr, size_t len)
96 {
97 	BIO *bio = NULL;
98 	EVP_PKEY *pkey = NULL;
99 
100 	if ((bio = BIO_new(BIO_s_mem())) == NULL) {
101 		warnx("BIO_new");
102 		goto out;
103 	}
104 	if (len > INT_MAX || BIO_write(bio, ptr, (int)len) != (int)len) {
105 		warnx("BIO_write");
106 		goto out;
107 	}
108 	if ((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)) == NULL)
109 		warnx("PEM_read_bio_PUBKEY");
110 out:
111 	BIO_free(bio);
112 
113 	return pkey;
114 }
115 
116 static int
117 rs256_pk_cmp(const char *ptr, size_t len)
118 {
119 	EVP_PKEY *pkA = NULL;
120 	EVP_PKEY *pkB = NULL;
121 	rs256_pk_t *k = NULL;
122 	int r, ok = -1;
123 
124 	if ((pkA = EVP_PKEY_from_PEM(ptr, len)) == NULL) {
125 		warnx("EVP_PKEY_from_PEM");
126 		goto out;
127 	}
128 	if ((k = rs256_pk_new()) == NULL) {
129 		warnx("rs256_pk_new");
130 		goto out;
131 	}
132 	if ((r = rs256_pk_from_EVP_PKEY(k, pkA)) != FIDO_OK) {
133 		warnx("rs256_pk_from_EVP_PKEY: 0x%x", r);
134 		goto out;
135 	}
136 	if ((pkB = rs256_pk_to_EVP_PKEY(k)) == NULL) {
137 		warnx("rs256_pk_to_EVP_PKEY");
138 		goto out;
139 	}
140 	if ((r = EVP_PKEY_cmp(pkA, pkB)) != 1) {
141 		warnx("EVP_PKEY_cmp: %d", r);
142 		goto out;
143 	}
144 
145 	ok = 0;
146 out:
147 	EVP_PKEY_free(pkA);
148 	EVP_PKEY_free(pkB);
149 	rs256_pk_free(&k);
150 
151 	return ok;
152 }
153 
154 static void
155 invalid_size(const char *pem)
156 {
157 	EVP_PKEY *pkey;
158 	rs256_pk_t *pk;
159 
160 	ASSERT_NOT_NULL((pkey = EVP_PKEY_from_PEM(pem, strlen(pem))));
161 	ASSERT_NOT_NULL((pk = rs256_pk_new()));
162 	ASSERT_INVAL(rs256_pk_from_EVP_PKEY(pk, pkey));
163 
164 	EVP_PKEY_free(pkey);
165 	rs256_pk_free(&pk);
166 }
167 
168 static void
169 valid_size(const char *pem, const unsigned char *raw, size_t raw_len)
170 {
171 	EVP_PKEY *pkeyA;
172 	EVP_PKEY *pkeyB;
173 	rs256_pk_t *pkA;
174 	rs256_pk_t *pkB;
175 
176 	ASSERT_NOT_NULL((pkeyA = EVP_PKEY_from_PEM(pem, strlen(pem))));
177 	ASSERT_NOT_NULL((pkA = rs256_pk_new()));
178 	ASSERT_NOT_NULL((pkB = rs256_pk_new()));
179 	ASSERT_OK(rs256_pk_from_EVP_PKEY(pkA, pkeyA));
180 	ASSERT_OK(rs256_pk_from_ptr(pkB, raw, raw_len));
181 	ASSERT_NOT_NULL((pkeyB = rs256_pk_to_EVP_PKEY(pkB)));
182 	assert(EVP_PKEY_cmp(pkeyA, pkeyB) == 1);
183 	assert(rs256_pk_cmp(pem, strlen(pem)) == 0);
184 
185 	EVP_PKEY_free(pkeyA);
186 	EVP_PKEY_free(pkeyB);
187 	rs256_pk_free(&pkA);
188 	rs256_pk_free(&pkB);
189 }
190 
191 int
192 main(void)
193 {
194 	fido_init(0);
195 
196 	invalid_size(rsa1024);
197 	invalid_size(rsa3072);
198 	valid_size(rsa2048, rsa2048_raw, sizeof(rsa2048_raw));
199 
200 	exit(0);
201 }
202