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