2 * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/asn1.h>
11 #include <openssl/pem.h>
12 #include "internal/sizes.h"
13 #include "crypto/evp.h"
16 /* Collected arguments */
17 static const char *eecert_filename = NULL; /* For test_x509_file() */
18 static const char *cacert_filename = NULL; /* For test_x509_file() */
19 static const char *pubkey_filename = NULL; /* For test_spki_file() */
21 #define ALGORITHMID_NAME "algorithm-id"
23 static int test_spki_aid(X509_PUBKEY *pubkey, const char *filename)
25 const ASN1_OBJECT *oid;
26 X509_ALGOR *alg = NULL;
27 EVP_PKEY *pkey = NULL;
28 EVP_KEYMGMT *keymgmt = NULL;
30 char name[OSSL_MAX_NAME_SIZE] = "";
31 unsigned char *algid_legacy = NULL;
32 int algid_legacy_len = 0;
33 static unsigned char algid_prov[OSSL_MAX_ALGORITHM_ID_SIZE];
34 size_t algid_prov_len = 0;
35 const OSSL_PARAM *gettable_params = NULL;
36 OSSL_PARAM params[] = {
37 OSSL_PARAM_octet_string(ALGORITHMID_NAME,
38 &algid_prov, sizeof(algid_prov)),
43 if (!TEST_true(X509_PUBKEY_get0_param(NULL, NULL, NULL, &alg, pubkey))
44 || !TEST_ptr(pkey = X509_PUBKEY_get0(pubkey)))
47 if (!TEST_int_ge(algid_legacy_len = i2d_X509_ALGOR(alg, &algid_legacy), 0))
50 X509_ALGOR_get0(&oid, NULL, NULL, alg);
51 if (!TEST_true(OBJ_obj2txt(name, sizeof(name), oid, 0)))
55 * We use an internal functions to ensure we have a provided key.
56 * Note that |keydata| should not be freed, as it's cached in |pkey|.
57 * The |keymgmt|, however, should, as its reference count is incremented
60 if ((keydata = evp_pkey_export_to_provider(pkey, NULL,
61 &keymgmt, NULL)) == NULL) {
62 TEST_info("The public key found in '%s' doesn't have provider support."
69 if (!TEST_true(EVP_KEYMGMT_is_a(keymgmt, name))) {
70 TEST_info("The AlgorithmID key type (%s) for the public key found in"
71 " '%s' doesn't match the key type of the extracted public"
78 if (!TEST_ptr(gettable_params = EVP_KEYMGMT_gettable_params(keymgmt))
79 || !TEST_ptr(OSSL_PARAM_locate_const(gettable_params, ALGORITHMID_NAME))) {
80 TEST_info("The %s provider keymgmt appears to lack support for algorithm-id."
88 if (!TEST_true(evp_keymgmt_get_params(keymgmt, keydata, params)))
90 algid_prov_len = params[0].return_size;
92 /* We now have all the algorithm IDs we need, let's compare them */
93 if (TEST_mem_eq(algid_legacy, algid_legacy_len,
94 algid_prov, algid_prov_len))
98 EVP_KEYMGMT_free(keymgmt);
99 OPENSSL_free(algid_legacy);
103 static int test_x509_spki_aid(X509 *cert, const char *filename)
105 X509_PUBKEY *pubkey = X509_get_X509_PUBKEY(cert);
107 return test_spki_aid(pubkey, filename);
112 * When we gain the ability to get an EVP_SIGNATURE with a complete signature
113 * algorithm name (like "sha1WithRSAEncryption" or its corresponding OID in
114 * text form, "1.2.840.113549.1.1.2"), we won't have to limit this test to
115 * what we have in libcrypto's cross-reference db, i.e. won't have to call
116 * OBJ_find_sigid_algs() to find out the EVP_PKEY_METHOD NID any more.
117 * All we'd have to do is used OBJ_obj2txt() on an ASN1_OBJECT and pass the
120 static int test_x509_sig_aid(X509 *eecert, const char *ee_filename,
121 X509 *cacert, const char *ca_filename)
123 const ASN1_OBJECT *sig_oid = NULL;
124 const X509_ALGOR *alg = NULL;
125 int sig_nid = NID_undef, dig_nid = NID_undef, pkey_nid = NID_undef;
126 EVP_MD_CTX *mdctx = NULL;
127 EVP_PKEY_CTX *pctx = NULL;
128 EVP_PKEY *pkey = NULL;
129 unsigned char *algid_legacy = NULL;
130 int algid_legacy_len = 0;
131 static unsigned char algid_prov[OSSL_MAX_ALGORITHM_ID_SIZE];
132 size_t algid_prov_len = 0;
133 const OSSL_PARAM *gettable_params = NULL;
134 OSSL_PARAM params[] = {
135 OSSL_PARAM_octet_string("algorithm-id",
136 &algid_prov, sizeof(algid_prov)),
141 X509_get0_signature(NULL, &alg, eecert);
142 X509_ALGOR_get0(&sig_oid, NULL, NULL, alg);
143 if (!TEST_int_eq(X509_ALGOR_cmp(alg, X509_get0_tbs_sigalg(eecert)), 0))
145 if (!TEST_int_ne(sig_nid = OBJ_obj2nid(sig_oid), NID_undef)
146 || !TEST_true(OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid))
147 || !TEST_ptr(pkey = X509_get0_pubkey(cacert)))
150 if (!TEST_true(EVP_PKEY_is_a(pkey, OBJ_nid2sn(pkey_nid)))) {
151 TEST_info("The '%s' pubkey can't be used to verify the '%s' signature",
152 ca_filename, ee_filename);
153 TEST_info("Signature algorithm is %s (pkey type %s, hash type %s)",
154 OBJ_nid2sn(sig_nid), OBJ_nid2sn(pkey_nid), OBJ_nid2sn(dig_nid));
155 TEST_info("Pkey key type is %s", EVP_PKEY_get0_type_name(pkey));
159 if (!TEST_int_ge(algid_legacy_len = i2d_X509_ALGOR(alg, &algid_legacy), 0))
162 if (!TEST_ptr(mdctx = EVP_MD_CTX_new())
163 || !TEST_true(EVP_DigestVerifyInit_ex(mdctx, &pctx,
165 NULL, NULL, pkey, NULL))) {
166 TEST_info("Couldn't initialize a DigestVerify operation with "
167 "pkey type %s and hash type %s",
168 OBJ_nid2sn(pkey_nid), OBJ_nid2sn(dig_nid));
172 if (!TEST_ptr(gettable_params = EVP_PKEY_CTX_gettable_params(pctx))
173 || !TEST_ptr(OSSL_PARAM_locate_const(gettable_params, ALGORITHMID_NAME))) {
174 TEST_info("The %s provider keymgmt appears to lack support for algorithm-id"
176 OBJ_nid2sn(pkey_nid));
181 algid_prov[0] = '\0';
182 if (!TEST_true(EVP_PKEY_CTX_get_params(pctx, params)))
184 algid_prov_len = params[0].return_size;
186 /* We now have all the algorithm IDs we need, let's compare them */
187 if (TEST_mem_eq(algid_legacy, algid_legacy_len,
188 algid_prov, algid_prov_len))
192 EVP_MD_CTX_free(mdctx);
193 /* pctx is free by EVP_MD_CTX_free() */
194 OPENSSL_free(algid_legacy);
198 static int test_spki_file(void)
200 X509_PUBKEY *pubkey = NULL;
201 BIO *b = BIO_new_file(pubkey_filename, "r");
205 TEST_error("Couldn't open '%s' for reading\n", pubkey_filename);
206 TEST_openssl_errors();
210 if ((pubkey = PEM_read_bio_X509_PUBKEY(b, NULL, NULL, NULL)) == NULL) {
211 TEST_error("'%s' doesn't appear to be a SubjectPublicKeyInfo in PEM format\n",
213 TEST_openssl_errors();
217 ret = test_spki_aid(pubkey, pubkey_filename);
220 X509_PUBKEY_free(pubkey);
224 static int test_x509_files(void)
226 X509 *eecert = NULL, *cacert = NULL;
227 BIO *bee = NULL, *bca = NULL;
230 if ((bee = BIO_new_file(eecert_filename, "r")) == NULL) {
231 TEST_error("Couldn't open '%s' for reading\n", eecert_filename);
232 TEST_openssl_errors();
235 if ((bca = BIO_new_file(cacert_filename, "r")) == NULL) {
236 TEST_error("Couldn't open '%s' for reading\n", cacert_filename);
237 TEST_openssl_errors();
241 if ((eecert = PEM_read_bio_X509(bee, NULL, NULL, NULL)) == NULL) {
242 TEST_error("'%s' doesn't appear to be a X.509 certificate in PEM format\n",
244 TEST_openssl_errors();
247 if ((cacert = PEM_read_bio_X509(bca, NULL, NULL, NULL)) == NULL) {
248 TEST_error("'%s' doesn't appear to be a X.509 certificate in PEM format\n",
250 TEST_openssl_errors();
254 ret = test_x509_sig_aid(eecert, eecert_filename, cacert, cacert_filename)
255 & test_x509_spki_aid(eecert, eecert_filename)
256 & test_x509_spki_aid(cacert, cacert_filename);
265 typedef enum OPTION_choice {
273 const OPTIONS *test_get_options(void)
275 static const OPTIONS test_options[] = {
276 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("file...\n"),
277 { "x509", OPT_X509, '-', "Test X.509 certificates. Requires two files" },
278 { "spki", OPT_SPKI, '-', "Test public keys in SubjectPublicKeyInfo form. Requires one file" },
279 { OPT_HELP_STR, 1, '-',
280 "file...\tFile(s) to run tests on. All files must be PEM encoded.\n" },
286 int setup_tests(void)
289 int n, x509 = 0, spki = 0, testcount = 0;
291 while ((o = opt_next()) != OPT_EOF) {
307 /* |testcount| adds all the given test types together */
308 testcount = x509 + spki;
311 BIO_printf(bio_err, "No test type given\n");
312 else if (testcount > 1)
313 BIO_printf(bio_err, "Only one test type may be given\n");
317 n = test_get_argument_count();
318 if (spki && n == 1) {
319 pubkey_filename = test_get_argument(0);
320 } else if (x509 && n == 2) {
321 eecert_filename = test_get_argument(0);
322 cacert_filename = test_get_argument(1);
325 if (spki && pubkey_filename == NULL) {
326 BIO_printf(bio_err, "Missing -spki argument\n");
328 } else if (x509 && (eecert_filename == NULL || cacert_filename == NULL)) {
329 BIO_printf(bio_err, "Missing -x509 argument(s)\n");
334 ADD_TEST(test_x509_files);
336 ADD_TEST(test_spki_file);