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