xref: /freebsd/contrib/libfido2/regress/rs256.c (revision 2ccfa855)
1*2ccfa855SEd Maste /*
2*2ccfa855SEd Maste  * Copyright (c) 2022 Yubico AB. All rights reserved.
3*2ccfa855SEd Maste  * Use of this source code is governed by a BSD-style
4*2ccfa855SEd Maste  * license that can be found in the LICENSE file.
5*2ccfa855SEd Maste  * SPDX-License-Identifier: BSD-2-Clause
6*2ccfa855SEd Maste  */
7*2ccfa855SEd Maste 
8*2ccfa855SEd Maste #undef NDEBUG
9*2ccfa855SEd Maste 
10*2ccfa855SEd Maste #include <assert.h>
11*2ccfa855SEd Maste #include <string.h>
12*2ccfa855SEd Maste 
13*2ccfa855SEd Maste #define _FIDO_INTERNAL
14*2ccfa855SEd Maste 
15*2ccfa855SEd Maste #include <fido.h>
16*2ccfa855SEd Maste #include <fido/rs256.h>
17*2ccfa855SEd Maste 
18*2ccfa855SEd Maste #include <openssl/bio.h>
19*2ccfa855SEd Maste #include <openssl/pem.h>
20*2ccfa855SEd Maste 
21*2ccfa855SEd Maste #define ASSERT_NOT_NULL(e)	assert((e) != NULL)
22*2ccfa855SEd Maste #define ASSERT_NULL(e)		assert((e) == NULL)
23*2ccfa855SEd Maste #define ASSERT_INVAL(e)		assert((e) == FIDO_ERR_INVALID_ARGUMENT)
24*2ccfa855SEd Maste #define ASSERT_OK(e)		assert((e) == FIDO_OK)
25*2ccfa855SEd Maste 
26*2ccfa855SEd Maste static char rsa1024[] = \
27*2ccfa855SEd Maste "-----BEGIN PUBLIC KEY-----\n"
28*2ccfa855SEd Maste "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCw92gn9Ku/bEfFj1AutaZyltpf\n"
29*2ccfa855SEd Maste "zzXrg70kQFymNq+spMt/HlxKiImw8TZU08zWW4ZLE/Ch4JYjMW6ETAdQFhSC63Ih\n"
30*2ccfa855SEd Maste "Wecui0JJ1f+2CsUVg+h7lO1877LZYUpdNiJrbqMb5Yc4N3FPtvdl3NoLIIQsF76H\n"
31*2ccfa855SEd Maste "VRvpjQgkWipRfZ97JQIDAQAB\n"
32*2ccfa855SEd Maste "-----END PUBLIC KEY-----";
33*2ccfa855SEd Maste 
34*2ccfa855SEd Maste static char rsa2048[] = \
35*2ccfa855SEd Maste "-----BEGIN PUBLIC KEY-----\n"
36*2ccfa855SEd Maste "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApvIq/55ZodBIxzo/8BnE\n"
37*2ccfa855SEd Maste "UQN1fo1hmJ6V20hQHSzJq5tHyxRCcvKikuJ1ZvR4RdZlEzdTdbEfMBdZ8sxve0/U\n"
38*2ccfa855SEd Maste "yYEjH92CG0vgTCYuUaFLJTaWZSvWa96G8Lw+V4VyNFDRCM7sflOaSVH5pAsz8OEc\n"
39*2ccfa855SEd Maste "TLZfM4NhnDsJAM+mQ6X7Tza0sczPchgDA+9KByXo/VIqyuBQs17rlKC2reMa8NkY\n"
40*2ccfa855SEd Maste "rBRQZJLNzi68d5/BHH1flGWE1l8wJ9dr1Ex93H/KdzX+7/28TWUC98nneUo8RfRx\n"
41*2ccfa855SEd Maste "FwUt/EInDMHOORCaCHSs28U/9IUyMjqLB1rxKhIp09yGXMiTrrT+p+Pcn8dO01HT\n"
42*2ccfa855SEd Maste "vQIDAQAB\n"
43*2ccfa855SEd Maste "-----END PUBLIC KEY-----";
44*2ccfa855SEd Maste 
45*2ccfa855SEd Maste static char rsa3072[] = \
46*2ccfa855SEd Maste "-----BEGIN PUBLIC KEY-----\n"
47*2ccfa855SEd Maste "MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAwZunKrMs/o92AniLPNTF\n"
48*2ccfa855SEd Maste "Ta4EYfhy5NDmMvQvRFT/eTYItLrOTPmYMap68KLyZYmgz/AdaxAL/992QWre7XTY\n"
49*2ccfa855SEd Maste "gqLwtZT+WsSu7xPHWKTTXrlVohKBeLHQ0I7Zy0NSMUxhlJEMrBAjSyFAS86zWm5w\n"
50*2ccfa855SEd Maste "ctC3pNCqfUKugA07BVj+d5Mv5fziwgMR86kuhkVuMYfsR4IYwX4+va0pyLzxx624\n"
51*2ccfa855SEd Maste "s9nJ107g+A+3MUk4bAto3lruFeeZPUI2AFzFQbGg5By6VtvVi3gKQ7lUNtAr0Onu\n"
52*2ccfa855SEd Maste "I6Fb+yz8sbFcvDpJcu5CXW20GrKMVP4KY5pn2LCajWuZjBl/dXWayPfm4UX5Y2O4\n"
53*2ccfa855SEd Maste "73tzPpUBNwnEdz79His0v80Vmvjwn5IuF2jAoimrBNPJFFwCCuVNy8kgj2vllk1l\n"
54*2ccfa855SEd Maste "RvLOG6hf8VnlDb40QZS3QAQ09xFfF+xlVLb8cHH6wllaAGEM230TrmawpC7xpz4Z\n"
55*2ccfa855SEd Maste "sTuwJwI0AWEi//noMsRz2BuF2fCp//aORYJQU2S8kYk3AgMBAAE=\n"
56*2ccfa855SEd Maste "-----END PUBLIC KEY-----";
57*2ccfa855SEd Maste 
58*2ccfa855SEd Maste static const unsigned char rsa2048_raw[] = {
59*2ccfa855SEd Maste 	0xa6, 0xf2, 0x2a, 0xff, 0x9e, 0x59, 0xa1, 0xd0,
60*2ccfa855SEd Maste 	0x48, 0xc7, 0x3a, 0x3f, 0xf0, 0x19, 0xc4, 0x51,
61*2ccfa855SEd Maste 	0x03, 0x75, 0x7e, 0x8d, 0x61, 0x98, 0x9e, 0x95,
62*2ccfa855SEd Maste 	0xdb, 0x48, 0x50, 0x1d, 0x2c, 0xc9, 0xab, 0x9b,
63*2ccfa855SEd Maste 	0x47, 0xcb, 0x14, 0x42, 0x72, 0xf2, 0xa2, 0x92,
64*2ccfa855SEd Maste 	0xe2, 0x75, 0x66, 0xf4, 0x78, 0x45, 0xd6, 0x65,
65*2ccfa855SEd Maste 	0x13, 0x37, 0x53, 0x75, 0xb1, 0x1f, 0x30, 0x17,
66*2ccfa855SEd Maste 	0x59, 0xf2, 0xcc, 0x6f, 0x7b, 0x4f, 0xd4, 0xc9,
67*2ccfa855SEd Maste 	0x81, 0x23, 0x1f, 0xdd, 0x82, 0x1b, 0x4b, 0xe0,
68*2ccfa855SEd Maste 	0x4c, 0x26, 0x2e, 0x51, 0xa1, 0x4b, 0x25, 0x36,
69*2ccfa855SEd Maste 	0x96, 0x65, 0x2b, 0xd6, 0x6b, 0xde, 0x86, 0xf0,
70*2ccfa855SEd Maste 	0xbc, 0x3e, 0x57, 0x85, 0x72, 0x34, 0x50, 0xd1,
71*2ccfa855SEd Maste 	0x08, 0xce, 0xec, 0x7e, 0x53, 0x9a, 0x49, 0x51,
72*2ccfa855SEd Maste 	0xf9, 0xa4, 0x0b, 0x33, 0xf0, 0xe1, 0x1c, 0x4c,
73*2ccfa855SEd Maste 	0xb6, 0x5f, 0x33, 0x83, 0x61, 0x9c, 0x3b, 0x09,
74*2ccfa855SEd Maste 	0x00, 0xcf, 0xa6, 0x43, 0xa5, 0xfb, 0x4f, 0x36,
75*2ccfa855SEd Maste 	0xb4, 0xb1, 0xcc, 0xcf, 0x72, 0x18, 0x03, 0x03,
76*2ccfa855SEd Maste 	0xef, 0x4a, 0x07, 0x25, 0xe8, 0xfd, 0x52, 0x2a,
77*2ccfa855SEd Maste 	0xca, 0xe0, 0x50, 0xb3, 0x5e, 0xeb, 0x94, 0xa0,
78*2ccfa855SEd Maste 	0xb6, 0xad, 0xe3, 0x1a, 0xf0, 0xd9, 0x18, 0xac,
79*2ccfa855SEd Maste 	0x14, 0x50, 0x64, 0x92, 0xcd, 0xce, 0x2e, 0xbc,
80*2ccfa855SEd Maste 	0x77, 0x9f, 0xc1, 0x1c, 0x7d, 0x5f, 0x94, 0x65,
81*2ccfa855SEd Maste 	0x84, 0xd6, 0x5f, 0x30, 0x27, 0xd7, 0x6b, 0xd4,
82*2ccfa855SEd Maste 	0x4c, 0x7d, 0xdc, 0x7f, 0xca, 0x77, 0x35, 0xfe,
83*2ccfa855SEd Maste 	0xef, 0xfd, 0xbc, 0x4d, 0x65, 0x02, 0xf7, 0xc9,
84*2ccfa855SEd Maste 	0xe7, 0x79, 0x4a, 0x3c, 0x45, 0xf4, 0x71, 0x17,
85*2ccfa855SEd Maste 	0x05, 0x2d, 0xfc, 0x42, 0x27, 0x0c, 0xc1, 0xce,
86*2ccfa855SEd Maste 	0x39, 0x10, 0x9a, 0x08, 0x74, 0xac, 0xdb, 0xc5,
87*2ccfa855SEd Maste 	0x3f, 0xf4, 0x85, 0x32, 0x32, 0x3a, 0x8b, 0x07,
88*2ccfa855SEd Maste 	0x5a, 0xf1, 0x2a, 0x12, 0x29, 0xd3, 0xdc, 0x86,
89*2ccfa855SEd Maste 	0x5c, 0xc8, 0x93, 0xae, 0xb4, 0xfe, 0xa7, 0xe3,
90*2ccfa855SEd Maste 	0xdc, 0x9f, 0xc7, 0x4e, 0xd3, 0x51, 0xd3, 0xbd,
91*2ccfa855SEd Maste 	0x01, 0x00, 0x01,
92*2ccfa855SEd Maste };
93*2ccfa855SEd Maste 
94*2ccfa855SEd Maste static EVP_PKEY *
EVP_PKEY_from_PEM(const char * ptr,size_t len)95*2ccfa855SEd Maste EVP_PKEY_from_PEM(const char *ptr, size_t len)
96*2ccfa855SEd Maste {
97*2ccfa855SEd Maste 	BIO *bio = NULL;
98*2ccfa855SEd Maste 	EVP_PKEY *pkey = NULL;
99*2ccfa855SEd Maste 
100*2ccfa855SEd Maste 	if ((bio = BIO_new(BIO_s_mem())) == NULL) {
101*2ccfa855SEd Maste 		warnx("BIO_new");
102*2ccfa855SEd Maste 		goto out;
103*2ccfa855SEd Maste 	}
104*2ccfa855SEd Maste 	if (len > INT_MAX || BIO_write(bio, ptr, (int)len) != (int)len) {
105*2ccfa855SEd Maste 		warnx("BIO_write");
106*2ccfa855SEd Maste 		goto out;
107*2ccfa855SEd Maste 	}
108*2ccfa855SEd Maste 	if ((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)) == NULL)
109*2ccfa855SEd Maste 		warnx("PEM_read_bio_PUBKEY");
110*2ccfa855SEd Maste out:
111*2ccfa855SEd Maste 	BIO_free(bio);
112*2ccfa855SEd Maste 
113*2ccfa855SEd Maste 	return pkey;
114*2ccfa855SEd Maste }
115*2ccfa855SEd Maste 
116*2ccfa855SEd Maste static int
rs256_pk_cmp(const char * ptr,size_t len)117*2ccfa855SEd Maste rs256_pk_cmp(const char *ptr, size_t len)
118*2ccfa855SEd Maste {
119*2ccfa855SEd Maste 	EVP_PKEY *pkA = NULL;
120*2ccfa855SEd Maste 	EVP_PKEY *pkB = NULL;
121*2ccfa855SEd Maste 	rs256_pk_t *k = NULL;
122*2ccfa855SEd Maste 	int r, ok = -1;
123*2ccfa855SEd Maste 
124*2ccfa855SEd Maste 	if ((pkA = EVP_PKEY_from_PEM(ptr, len)) == NULL) {
125*2ccfa855SEd Maste 		warnx("EVP_PKEY_from_PEM");
126*2ccfa855SEd Maste 		goto out;
127*2ccfa855SEd Maste 	}
128*2ccfa855SEd Maste 	if ((k = rs256_pk_new()) == NULL) {
129*2ccfa855SEd Maste 		warnx("rs256_pk_new");
130*2ccfa855SEd Maste 		goto out;
131*2ccfa855SEd Maste 	}
132*2ccfa855SEd Maste 	if ((r = rs256_pk_from_EVP_PKEY(k, pkA)) != FIDO_OK) {
133*2ccfa855SEd Maste 		warnx("rs256_pk_from_EVP_PKEY: 0x%x", r);
134*2ccfa855SEd Maste 		goto out;
135*2ccfa855SEd Maste 	}
136*2ccfa855SEd Maste 	if ((pkB = rs256_pk_to_EVP_PKEY(k)) == NULL) {
137*2ccfa855SEd Maste 		warnx("rs256_pk_to_EVP_PKEY");
138*2ccfa855SEd Maste 		goto out;
139*2ccfa855SEd Maste 	}
140*2ccfa855SEd Maste 	if ((r = EVP_PKEY_cmp(pkA, pkB)) != 1) {
141*2ccfa855SEd Maste 		warnx("EVP_PKEY_cmp: %d", r);
142*2ccfa855SEd Maste 		goto out;
143*2ccfa855SEd Maste 	}
144*2ccfa855SEd Maste 
145*2ccfa855SEd Maste 	ok = 0;
146*2ccfa855SEd Maste out:
147*2ccfa855SEd Maste 	EVP_PKEY_free(pkA);
148*2ccfa855SEd Maste 	EVP_PKEY_free(pkB);
149*2ccfa855SEd Maste 	rs256_pk_free(&k);
150*2ccfa855SEd Maste 
151*2ccfa855SEd Maste 	return ok;
152*2ccfa855SEd Maste }
153*2ccfa855SEd Maste 
154*2ccfa855SEd Maste static void
invalid_size(const char * pem)155*2ccfa855SEd Maste invalid_size(const char *pem)
156*2ccfa855SEd Maste {
157*2ccfa855SEd Maste 	EVP_PKEY *pkey;
158*2ccfa855SEd Maste 	rs256_pk_t *pk;
159*2ccfa855SEd Maste 
160*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkey = EVP_PKEY_from_PEM(pem, strlen(pem))));
161*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pk = rs256_pk_new()));
162*2ccfa855SEd Maste 	ASSERT_INVAL(rs256_pk_from_EVP_PKEY(pk, pkey));
163*2ccfa855SEd Maste 
164*2ccfa855SEd Maste 	EVP_PKEY_free(pkey);
165*2ccfa855SEd Maste 	rs256_pk_free(&pk);
166*2ccfa855SEd Maste }
167*2ccfa855SEd Maste 
168*2ccfa855SEd Maste static void
valid_size(const char * pem,const unsigned char * raw,size_t raw_len)169*2ccfa855SEd Maste valid_size(const char *pem, const unsigned char *raw, size_t raw_len)
170*2ccfa855SEd Maste {
171*2ccfa855SEd Maste 	EVP_PKEY *pkeyA;
172*2ccfa855SEd Maste 	EVP_PKEY *pkeyB;
173*2ccfa855SEd Maste 	rs256_pk_t *pkA;
174*2ccfa855SEd Maste 	rs256_pk_t *pkB;
175*2ccfa855SEd Maste 
176*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkeyA = EVP_PKEY_from_PEM(pem, strlen(pem))));
177*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkA = rs256_pk_new()));
178*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkB = rs256_pk_new()));
179*2ccfa855SEd Maste 	ASSERT_OK(rs256_pk_from_EVP_PKEY(pkA, pkeyA));
180*2ccfa855SEd Maste 	ASSERT_OK(rs256_pk_from_ptr(pkB, raw, raw_len));
181*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkeyB = rs256_pk_to_EVP_PKEY(pkB)));
182*2ccfa855SEd Maste 	assert(EVP_PKEY_cmp(pkeyA, pkeyB) == 1);
183*2ccfa855SEd Maste 	assert(rs256_pk_cmp(pem, strlen(pem)) == 0);
184*2ccfa855SEd Maste 
185*2ccfa855SEd Maste 	EVP_PKEY_free(pkeyA);
186*2ccfa855SEd Maste 	EVP_PKEY_free(pkeyB);
187*2ccfa855SEd Maste 	rs256_pk_free(&pkA);
188*2ccfa855SEd Maste 	rs256_pk_free(&pkB);
189*2ccfa855SEd Maste }
190*2ccfa855SEd Maste 
191*2ccfa855SEd Maste int
main(void)192*2ccfa855SEd Maste main(void)
193*2ccfa855SEd Maste {
194*2ccfa855SEd Maste 	fido_init(0);
195*2ccfa855SEd Maste 
196*2ccfa855SEd Maste 	invalid_size(rsa1024);
197*2ccfa855SEd Maste 	invalid_size(rsa3072);
198*2ccfa855SEd Maste 	valid_size(rsa2048, rsa2048_raw, sizeof(rsa2048_raw));
199*2ccfa855SEd Maste 
200*2ccfa855SEd Maste 	exit(0);
201*2ccfa855SEd Maste }
202