1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert #include <openssl/asn1.h>
11*e0c4386eSCy Schubert #include <openssl/pem.h>
12*e0c4386eSCy Schubert #include "internal/sizes.h"
13*e0c4386eSCy Schubert #include "crypto/evp.h"
14*e0c4386eSCy Schubert #include "testutil.h"
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert /* Collected arguments */
17*e0c4386eSCy Schubert static const char *eecert_filename = NULL; /* For test_x509_file() */
18*e0c4386eSCy Schubert static const char *cacert_filename = NULL; /* For test_x509_file() */
19*e0c4386eSCy Schubert static const char *pubkey_filename = NULL; /* For test_spki_file() */
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubert #define ALGORITHMID_NAME "algorithm-id"
22*e0c4386eSCy Schubert
test_spki_aid(X509_PUBKEY * pubkey,const char * filename)23*e0c4386eSCy Schubert static int test_spki_aid(X509_PUBKEY *pubkey, const char *filename)
24*e0c4386eSCy Schubert {
25*e0c4386eSCy Schubert const ASN1_OBJECT *oid;
26*e0c4386eSCy Schubert X509_ALGOR *alg = NULL;
27*e0c4386eSCy Schubert EVP_PKEY *pkey = NULL;
28*e0c4386eSCy Schubert EVP_KEYMGMT *keymgmt = NULL;
29*e0c4386eSCy Schubert void *keydata = NULL;
30*e0c4386eSCy Schubert char name[OSSL_MAX_NAME_SIZE] = "";
31*e0c4386eSCy Schubert unsigned char *algid_legacy = NULL;
32*e0c4386eSCy Schubert int algid_legacy_len = 0;
33*e0c4386eSCy Schubert static unsigned char algid_prov[OSSL_MAX_ALGORITHM_ID_SIZE];
34*e0c4386eSCy Schubert size_t algid_prov_len = 0;
35*e0c4386eSCy Schubert const OSSL_PARAM *gettable_params = NULL;
36*e0c4386eSCy Schubert OSSL_PARAM params[] = {
37*e0c4386eSCy Schubert OSSL_PARAM_octet_string(ALGORITHMID_NAME,
38*e0c4386eSCy Schubert &algid_prov, sizeof(algid_prov)),
39*e0c4386eSCy Schubert OSSL_PARAM_END
40*e0c4386eSCy Schubert };
41*e0c4386eSCy Schubert int ret = 0;
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert if (!TEST_true(X509_PUBKEY_get0_param(NULL, NULL, NULL, &alg, pubkey))
44*e0c4386eSCy Schubert || !TEST_ptr(pkey = X509_PUBKEY_get0(pubkey)))
45*e0c4386eSCy Schubert goto end;
46*e0c4386eSCy Schubert
47*e0c4386eSCy Schubert if (!TEST_int_ge(algid_legacy_len = i2d_X509_ALGOR(alg, &algid_legacy), 0))
48*e0c4386eSCy Schubert goto end;
49*e0c4386eSCy Schubert
50*e0c4386eSCy Schubert X509_ALGOR_get0(&oid, NULL, NULL, alg);
51*e0c4386eSCy Schubert if (!TEST_int_gt(OBJ_obj2txt(name, sizeof(name), oid, 0), 0))
52*e0c4386eSCy Schubert goto end;
53*e0c4386eSCy Schubert
54*e0c4386eSCy Schubert /*
55*e0c4386eSCy Schubert * We use an internal functions to ensure we have a provided key.
56*e0c4386eSCy Schubert * Note that |keydata| should not be freed, as it's cached in |pkey|.
57*e0c4386eSCy Schubert * The |keymgmt|, however, should, as its reference count is incremented
58*e0c4386eSCy Schubert * in this function.
59*e0c4386eSCy Schubert */
60*e0c4386eSCy Schubert if ((keydata = evp_pkey_export_to_provider(pkey, NULL,
61*e0c4386eSCy Schubert &keymgmt, NULL)) == NULL) {
62*e0c4386eSCy Schubert TEST_info("The public key found in '%s' doesn't have provider support."
63*e0c4386eSCy Schubert " Skipping...",
64*e0c4386eSCy Schubert filename);
65*e0c4386eSCy Schubert ret = 1;
66*e0c4386eSCy Schubert goto end;
67*e0c4386eSCy Schubert }
68*e0c4386eSCy Schubert
69*e0c4386eSCy Schubert if (!TEST_true(EVP_KEYMGMT_is_a(keymgmt, name))) {
70*e0c4386eSCy Schubert TEST_info("The AlgorithmID key type (%s) for the public key found in"
71*e0c4386eSCy Schubert " '%s' doesn't match the key type of the extracted public"
72*e0c4386eSCy Schubert " key.",
73*e0c4386eSCy Schubert name, filename);
74*e0c4386eSCy Schubert ret = 1;
75*e0c4386eSCy Schubert goto end;
76*e0c4386eSCy Schubert }
77*e0c4386eSCy Schubert
78*e0c4386eSCy Schubert if (!TEST_ptr(gettable_params = EVP_KEYMGMT_gettable_params(keymgmt))
79*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable_params, ALGORITHMID_NAME))) {
80*e0c4386eSCy Schubert TEST_info("The %s provider keymgmt appears to lack support for algorithm-id."
81*e0c4386eSCy Schubert " Skipping...",
82*e0c4386eSCy Schubert name);
83*e0c4386eSCy Schubert ret = 1;
84*e0c4386eSCy Schubert goto end;
85*e0c4386eSCy Schubert }
86*e0c4386eSCy Schubert
87*e0c4386eSCy Schubert algid_prov[0] = '\0';
88*e0c4386eSCy Schubert if (!TEST_true(evp_keymgmt_get_params(keymgmt, keydata, params)))
89*e0c4386eSCy Schubert goto end;
90*e0c4386eSCy Schubert algid_prov_len = params[0].return_size;
91*e0c4386eSCy Schubert
92*e0c4386eSCy Schubert /* We now have all the algorithm IDs we need, let's compare them */
93*e0c4386eSCy Schubert if (TEST_mem_eq(algid_legacy, algid_legacy_len,
94*e0c4386eSCy Schubert algid_prov, algid_prov_len))
95*e0c4386eSCy Schubert ret = 1;
96*e0c4386eSCy Schubert
97*e0c4386eSCy Schubert end:
98*e0c4386eSCy Schubert EVP_KEYMGMT_free(keymgmt);
99*e0c4386eSCy Schubert OPENSSL_free(algid_legacy);
100*e0c4386eSCy Schubert return ret;
101*e0c4386eSCy Schubert }
102*e0c4386eSCy Schubert
test_x509_spki_aid(X509 * cert,const char * filename)103*e0c4386eSCy Schubert static int test_x509_spki_aid(X509 *cert, const char *filename)
104*e0c4386eSCy Schubert {
105*e0c4386eSCy Schubert X509_PUBKEY *pubkey = X509_get_X509_PUBKEY(cert);
106*e0c4386eSCy Schubert
107*e0c4386eSCy Schubert return test_spki_aid(pubkey, filename);
108*e0c4386eSCy Schubert }
109*e0c4386eSCy Schubert
test_x509_sig_aid(X509 * eecert,const char * ee_filename,X509 * cacert,const char * ca_filename)110*e0c4386eSCy Schubert static int test_x509_sig_aid(X509 *eecert, const char *ee_filename,
111*e0c4386eSCy Schubert X509 *cacert, const char *ca_filename)
112*e0c4386eSCy Schubert {
113*e0c4386eSCy Schubert const ASN1_OBJECT *sig_oid = NULL;
114*e0c4386eSCy Schubert const X509_ALGOR *alg = NULL;
115*e0c4386eSCy Schubert int sig_nid = NID_undef, dig_nid = NID_undef, pkey_nid = NID_undef;
116*e0c4386eSCy Schubert EVP_MD_CTX *mdctx = NULL;
117*e0c4386eSCy Schubert EVP_PKEY_CTX *pctx = NULL;
118*e0c4386eSCy Schubert EVP_PKEY *pkey = NULL;
119*e0c4386eSCy Schubert unsigned char *algid_legacy = NULL;
120*e0c4386eSCy Schubert int algid_legacy_len = 0;
121*e0c4386eSCy Schubert static unsigned char algid_prov[OSSL_MAX_ALGORITHM_ID_SIZE];
122*e0c4386eSCy Schubert size_t algid_prov_len = 0;
123*e0c4386eSCy Schubert const OSSL_PARAM *gettable_params = NULL;
124*e0c4386eSCy Schubert OSSL_PARAM params[] = {
125*e0c4386eSCy Schubert OSSL_PARAM_octet_string("algorithm-id",
126*e0c4386eSCy Schubert &algid_prov, sizeof(algid_prov)),
127*e0c4386eSCy Schubert OSSL_PARAM_END
128*e0c4386eSCy Schubert };
129*e0c4386eSCy Schubert int ret = 0;
130*e0c4386eSCy Schubert
131*e0c4386eSCy Schubert X509_get0_signature(NULL, &alg, eecert);
132*e0c4386eSCy Schubert X509_ALGOR_get0(&sig_oid, NULL, NULL, alg);
133*e0c4386eSCy Schubert if (!TEST_int_eq(X509_ALGOR_cmp(alg, X509_get0_tbs_sigalg(eecert)), 0))
134*e0c4386eSCy Schubert goto end;
135*e0c4386eSCy Schubert if (!TEST_int_ne(sig_nid = OBJ_obj2nid(sig_oid), NID_undef)
136*e0c4386eSCy Schubert || !TEST_true(OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid))
137*e0c4386eSCy Schubert || !TEST_ptr(pkey = X509_get0_pubkey(cacert)))
138*e0c4386eSCy Schubert goto end;
139*e0c4386eSCy Schubert
140*e0c4386eSCy Schubert if (!TEST_true(EVP_PKEY_is_a(pkey, OBJ_nid2sn(pkey_nid)))) {
141*e0c4386eSCy Schubert TEST_info("The '%s' pubkey can't be used to verify the '%s' signature",
142*e0c4386eSCy Schubert ca_filename, ee_filename);
143*e0c4386eSCy Schubert TEST_info("Signature algorithm is %s (pkey type %s, hash type %s)",
144*e0c4386eSCy Schubert OBJ_nid2sn(sig_nid), OBJ_nid2sn(pkey_nid), OBJ_nid2sn(dig_nid));
145*e0c4386eSCy Schubert TEST_info("Pkey key type is %s", EVP_PKEY_get0_type_name(pkey));
146*e0c4386eSCy Schubert goto end;
147*e0c4386eSCy Schubert }
148*e0c4386eSCy Schubert
149*e0c4386eSCy Schubert if (!TEST_int_ge(algid_legacy_len = i2d_X509_ALGOR(alg, &algid_legacy), 0))
150*e0c4386eSCy Schubert goto end;
151*e0c4386eSCy Schubert
152*e0c4386eSCy Schubert if (!TEST_ptr(mdctx = EVP_MD_CTX_new())
153*e0c4386eSCy Schubert || !TEST_true(EVP_DigestVerifyInit_ex(mdctx, &pctx,
154*e0c4386eSCy Schubert OBJ_nid2sn(dig_nid),
155*e0c4386eSCy Schubert NULL, NULL, pkey, NULL))) {
156*e0c4386eSCy Schubert TEST_info("Couldn't initialize a DigestVerify operation with "
157*e0c4386eSCy Schubert "pkey type %s and hash type %s",
158*e0c4386eSCy Schubert OBJ_nid2sn(pkey_nid), OBJ_nid2sn(dig_nid));
159*e0c4386eSCy Schubert goto end;
160*e0c4386eSCy Schubert }
161*e0c4386eSCy Schubert
162*e0c4386eSCy Schubert if (!TEST_ptr(gettable_params = EVP_PKEY_CTX_gettable_params(pctx))
163*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable_params, ALGORITHMID_NAME))) {
164*e0c4386eSCy Schubert TEST_info("The %s provider keymgmt appears to lack support for algorithm-id"
165*e0c4386eSCy Schubert " Skipping...",
166*e0c4386eSCy Schubert OBJ_nid2sn(pkey_nid));
167*e0c4386eSCy Schubert ret = 1;
168*e0c4386eSCy Schubert goto end;
169*e0c4386eSCy Schubert }
170*e0c4386eSCy Schubert
171*e0c4386eSCy Schubert algid_prov[0] = '\0';
172*e0c4386eSCy Schubert if (!TEST_true(EVP_PKEY_CTX_get_params(pctx, params)))
173*e0c4386eSCy Schubert goto end;
174*e0c4386eSCy Schubert algid_prov_len = params[0].return_size;
175*e0c4386eSCy Schubert
176*e0c4386eSCy Schubert /* We now have all the algorithm IDs we need, let's compare them */
177*e0c4386eSCy Schubert if (TEST_mem_eq(algid_legacy, algid_legacy_len,
178*e0c4386eSCy Schubert algid_prov, algid_prov_len))
179*e0c4386eSCy Schubert ret = 1;
180*e0c4386eSCy Schubert
181*e0c4386eSCy Schubert end:
182*e0c4386eSCy Schubert EVP_MD_CTX_free(mdctx);
183*e0c4386eSCy Schubert /* pctx is free by EVP_MD_CTX_free() */
184*e0c4386eSCy Schubert OPENSSL_free(algid_legacy);
185*e0c4386eSCy Schubert return ret;
186*e0c4386eSCy Schubert }
187*e0c4386eSCy Schubert
test_spki_file(void)188*e0c4386eSCy Schubert static int test_spki_file(void)
189*e0c4386eSCy Schubert {
190*e0c4386eSCy Schubert X509_PUBKEY *pubkey = NULL;
191*e0c4386eSCy Schubert BIO *b = BIO_new_file(pubkey_filename, "r");
192*e0c4386eSCy Schubert int ret = 0;
193*e0c4386eSCy Schubert
194*e0c4386eSCy Schubert if (b == NULL) {
195*e0c4386eSCy Schubert TEST_error("Couldn't open '%s' for reading\n", pubkey_filename);
196*e0c4386eSCy Schubert TEST_openssl_errors();
197*e0c4386eSCy Schubert goto end;
198*e0c4386eSCy Schubert }
199*e0c4386eSCy Schubert
200*e0c4386eSCy Schubert if ((pubkey = PEM_read_bio_X509_PUBKEY(b, NULL, NULL, NULL)) == NULL) {
201*e0c4386eSCy Schubert TEST_error("'%s' doesn't appear to be a SubjectPublicKeyInfo in PEM format\n",
202*e0c4386eSCy Schubert pubkey_filename);
203*e0c4386eSCy Schubert TEST_openssl_errors();
204*e0c4386eSCy Schubert goto end;
205*e0c4386eSCy Schubert }
206*e0c4386eSCy Schubert
207*e0c4386eSCy Schubert ret = test_spki_aid(pubkey, pubkey_filename);
208*e0c4386eSCy Schubert end:
209*e0c4386eSCy Schubert BIO_free(b);
210*e0c4386eSCy Schubert X509_PUBKEY_free(pubkey);
211*e0c4386eSCy Schubert return ret;
212*e0c4386eSCy Schubert }
213*e0c4386eSCy Schubert
test_x509_files(void)214*e0c4386eSCy Schubert static int test_x509_files(void)
215*e0c4386eSCy Schubert {
216*e0c4386eSCy Schubert X509 *eecert = NULL, *cacert = NULL;
217*e0c4386eSCy Schubert BIO *bee = NULL, *bca = NULL;
218*e0c4386eSCy Schubert int ret = 0;
219*e0c4386eSCy Schubert
220*e0c4386eSCy Schubert if ((bee = BIO_new_file(eecert_filename, "r")) == NULL) {
221*e0c4386eSCy Schubert TEST_error("Couldn't open '%s' for reading\n", eecert_filename);
222*e0c4386eSCy Schubert TEST_openssl_errors();
223*e0c4386eSCy Schubert goto end;
224*e0c4386eSCy Schubert }
225*e0c4386eSCy Schubert if ((bca = BIO_new_file(cacert_filename, "r")) == NULL) {
226*e0c4386eSCy Schubert TEST_error("Couldn't open '%s' for reading\n", cacert_filename);
227*e0c4386eSCy Schubert TEST_openssl_errors();
228*e0c4386eSCy Schubert goto end;
229*e0c4386eSCy Schubert }
230*e0c4386eSCy Schubert
231*e0c4386eSCy Schubert if ((eecert = PEM_read_bio_X509(bee, NULL, NULL, NULL)) == NULL) {
232*e0c4386eSCy Schubert TEST_error("'%s' doesn't appear to be a X.509 certificate in PEM format\n",
233*e0c4386eSCy Schubert eecert_filename);
234*e0c4386eSCy Schubert TEST_openssl_errors();
235*e0c4386eSCy Schubert goto end;
236*e0c4386eSCy Schubert }
237*e0c4386eSCy Schubert if ((cacert = PEM_read_bio_X509(bca, NULL, NULL, NULL)) == NULL) {
238*e0c4386eSCy Schubert TEST_error("'%s' doesn't appear to be a X.509 certificate in PEM format\n",
239*e0c4386eSCy Schubert cacert_filename);
240*e0c4386eSCy Schubert TEST_openssl_errors();
241*e0c4386eSCy Schubert goto end;
242*e0c4386eSCy Schubert }
243*e0c4386eSCy Schubert
244*e0c4386eSCy Schubert ret = test_x509_sig_aid(eecert, eecert_filename, cacert, cacert_filename)
245*e0c4386eSCy Schubert & test_x509_spki_aid(eecert, eecert_filename)
246*e0c4386eSCy Schubert & test_x509_spki_aid(cacert, cacert_filename);
247*e0c4386eSCy Schubert end:
248*e0c4386eSCy Schubert BIO_free(bee);
249*e0c4386eSCy Schubert BIO_free(bca);
250*e0c4386eSCy Schubert X509_free(eecert);
251*e0c4386eSCy Schubert X509_free(cacert);
252*e0c4386eSCy Schubert return ret;
253*e0c4386eSCy Schubert }
254*e0c4386eSCy Schubert
255*e0c4386eSCy Schubert typedef enum OPTION_choice {
256*e0c4386eSCy Schubert OPT_ERR = -1,
257*e0c4386eSCy Schubert OPT_EOF = 0,
258*e0c4386eSCy Schubert OPT_X509,
259*e0c4386eSCy Schubert OPT_SPKI,
260*e0c4386eSCy Schubert OPT_TEST_ENUM
261*e0c4386eSCy Schubert } OPTION_CHOICE;
262*e0c4386eSCy Schubert
test_get_options(void)263*e0c4386eSCy Schubert const OPTIONS *test_get_options(void)
264*e0c4386eSCy Schubert {
265*e0c4386eSCy Schubert static const OPTIONS test_options[] = {
266*e0c4386eSCy Schubert OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("file...\n"),
267*e0c4386eSCy Schubert { "x509", OPT_X509, '-', "Test X.509 certificates. Requires two files" },
268*e0c4386eSCy Schubert { "spki", OPT_SPKI, '-', "Test public keys in SubjectPublicKeyInfo form. Requires one file" },
269*e0c4386eSCy Schubert { OPT_HELP_STR, 1, '-',
270*e0c4386eSCy Schubert "file...\tFile(s) to run tests on. All files must be PEM encoded.\n" },
271*e0c4386eSCy Schubert { NULL }
272*e0c4386eSCy Schubert };
273*e0c4386eSCy Schubert return test_options;
274*e0c4386eSCy Schubert }
275*e0c4386eSCy Schubert
setup_tests(void)276*e0c4386eSCy Schubert int setup_tests(void)
277*e0c4386eSCy Schubert {
278*e0c4386eSCy Schubert OPTION_CHOICE o;
279*e0c4386eSCy Schubert int n, x509 = 0, spki = 0, testcount = 0;
280*e0c4386eSCy Schubert
281*e0c4386eSCy Schubert while ((o = opt_next()) != OPT_EOF) {
282*e0c4386eSCy Schubert switch (o) {
283*e0c4386eSCy Schubert case OPT_X509:
284*e0c4386eSCy Schubert x509 = 1;
285*e0c4386eSCy Schubert break;
286*e0c4386eSCy Schubert case OPT_SPKI:
287*e0c4386eSCy Schubert spki = 1;
288*e0c4386eSCy Schubert break;
289*e0c4386eSCy Schubert case OPT_TEST_CASES:
290*e0c4386eSCy Schubert break;
291*e0c4386eSCy Schubert default:
292*e0c4386eSCy Schubert case OPT_ERR:
293*e0c4386eSCy Schubert return 0;
294*e0c4386eSCy Schubert }
295*e0c4386eSCy Schubert }
296*e0c4386eSCy Schubert
297*e0c4386eSCy Schubert /* |testcount| adds all the given test types together */
298*e0c4386eSCy Schubert testcount = x509 + spki;
299*e0c4386eSCy Schubert
300*e0c4386eSCy Schubert if (testcount < 1)
301*e0c4386eSCy Schubert BIO_printf(bio_err, "No test type given\n");
302*e0c4386eSCy Schubert else if (testcount > 1)
303*e0c4386eSCy Schubert BIO_printf(bio_err, "Only one test type may be given\n");
304*e0c4386eSCy Schubert if (testcount != 1)
305*e0c4386eSCy Schubert return 0;
306*e0c4386eSCy Schubert
307*e0c4386eSCy Schubert n = test_get_argument_count();
308*e0c4386eSCy Schubert if (spki && n == 1) {
309*e0c4386eSCy Schubert pubkey_filename = test_get_argument(0);
310*e0c4386eSCy Schubert } else if (x509 && n == 2) {
311*e0c4386eSCy Schubert eecert_filename = test_get_argument(0);
312*e0c4386eSCy Schubert cacert_filename = test_get_argument(1);
313*e0c4386eSCy Schubert }
314*e0c4386eSCy Schubert
315*e0c4386eSCy Schubert if (spki && pubkey_filename == NULL) {
316*e0c4386eSCy Schubert BIO_printf(bio_err, "Missing -spki argument\n");
317*e0c4386eSCy Schubert return 0;
318*e0c4386eSCy Schubert } else if (x509 && (eecert_filename == NULL || cacert_filename == NULL)) {
319*e0c4386eSCy Schubert BIO_printf(bio_err, "Missing -x509 argument(s)\n");
320*e0c4386eSCy Schubert return 0;
321*e0c4386eSCy Schubert }
322*e0c4386eSCy Schubert
323*e0c4386eSCy Schubert if (x509)
324*e0c4386eSCy Schubert ADD_TEST(test_x509_files);
325*e0c4386eSCy Schubert if (spki)
326*e0c4386eSCy Schubert ADD_TEST(test_spki_file);
327*e0c4386eSCy Schubert return 1;
328*e0c4386eSCy Schubert }
329