1*e7be843bSPierre Pronchery /*
2*e7be843bSPierre Pronchery * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
3*e7be843bSPierre Pronchery *
4*e7be843bSPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e7be843bSPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*e7be843bSPierre Pronchery * in the file LICENSE in the source distribution or at
7*e7be843bSPierre Pronchery * https://www.openssl.org/source/license.html
8*e7be843bSPierre Pronchery */
9*e7be843bSPierre Pronchery
10*e7be843bSPierre Pronchery #include <openssl/pem.h>
11*e7be843bSPierre Pronchery #include <openssl/evp.h>
12*e7be843bSPierre Pronchery #include "testutil.h"
13*e7be843bSPierre Pronchery
14*e7be843bSPierre Pronchery static OSSL_LIB_CTX *libctx = NULL;
15*e7be843bSPierre Pronchery static OSSL_PROVIDER *nullprov = NULL;
16*e7be843bSPierre Pronchery static OSSL_PROVIDER *libprov = NULL;
17*e7be843bSPierre Pronchery static const char *filename = NULL;
18*e7be843bSPierre Pronchery static pem_password_cb passcb;
19*e7be843bSPierre Pronchery
20*e7be843bSPierre Pronchery typedef enum OPTION_choice {
21*e7be843bSPierre Pronchery OPT_ERR = -1,
22*e7be843bSPierre Pronchery OPT_EOF = 0,
23*e7be843bSPierre Pronchery OPT_CONFIG_FILE,
24*e7be843bSPierre Pronchery OPT_PROVIDER_NAME,
25*e7be843bSPierre Pronchery OPT_TEST_ENUM
26*e7be843bSPierre Pronchery } OPTION_CHOICE;
27*e7be843bSPierre Pronchery
test_get_options(void)28*e7be843bSPierre Pronchery const OPTIONS *test_get_options(void)
29*e7be843bSPierre Pronchery {
30*e7be843bSPierre Pronchery static const OPTIONS test_options[] = {
31*e7be843bSPierre Pronchery OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("file\n"),
32*e7be843bSPierre Pronchery { "config", OPT_CONFIG_FILE, '<',
33*e7be843bSPierre Pronchery "The configuration file to use for the libctx" },
34*e7be843bSPierre Pronchery { "provider", OPT_PROVIDER_NAME, 's',
35*e7be843bSPierre Pronchery "The provider to load (The default value is 'default')" },
36*e7be843bSPierre Pronchery { OPT_HELP_STR, 1, '-', "file\tFile to decode.\n" },
37*e7be843bSPierre Pronchery { NULL }
38*e7be843bSPierre Pronchery };
39*e7be843bSPierre Pronchery return test_options;
40*e7be843bSPierre Pronchery }
41*e7be843bSPierre Pronchery
passcb(char * buf,int size,int rwflag,void * userdata)42*e7be843bSPierre Pronchery static int passcb(char *buf, int size, int rwflag, void *userdata)
43*e7be843bSPierre Pronchery {
44*e7be843bSPierre Pronchery strcpy(buf, "pass");
45*e7be843bSPierre Pronchery return strlen(buf);
46*e7be843bSPierre Pronchery }
47*e7be843bSPierre Pronchery
test_decode_nonfipsalg(void)48*e7be843bSPierre Pronchery static int test_decode_nonfipsalg(void)
49*e7be843bSPierre Pronchery {
50*e7be843bSPierre Pronchery int ret = 0;
51*e7be843bSPierre Pronchery EVP_PKEY *privkey = NULL;
52*e7be843bSPierre Pronchery BIO *bio = NULL;
53*e7be843bSPierre Pronchery
54*e7be843bSPierre Pronchery /*
55*e7be843bSPierre Pronchery * Apply the "fips=true" property to all fetches for the libctx.
56*e7be843bSPierre Pronchery * We do this to test that we are using the propq override
57*e7be843bSPierre Pronchery */
58*e7be843bSPierre Pronchery EVP_default_properties_enable_fips(libctx, 1);
59*e7be843bSPierre Pronchery
60*e7be843bSPierre Pronchery if (!TEST_ptr(bio = BIO_new_file(filename, "r")))
61*e7be843bSPierre Pronchery goto err;
62*e7be843bSPierre Pronchery
63*e7be843bSPierre Pronchery /*
64*e7be843bSPierre Pronchery * If NULL is passed as the propq here it uses the global property "fips=true",
65*e7be843bSPierre Pronchery * Which we expect to fail if the decode uses a non FIPS algorithm
66*e7be843bSPierre Pronchery */
67*e7be843bSPierre Pronchery if (!TEST_ptr_null(PEM_read_bio_PrivateKey_ex(bio, &privkey, &passcb, NULL, libctx, NULL)))
68*e7be843bSPierre Pronchery goto err;
69*e7be843bSPierre Pronchery
70*e7be843bSPierre Pronchery /*
71*e7be843bSPierre Pronchery * Pass if we override the libctx global prop query to optionally use fips=true
72*e7be843bSPierre Pronchery * This assumes that the libctx contains the default provider
73*e7be843bSPierre Pronchery */
74*e7be843bSPierre Pronchery if (!TEST_ptr_null(PEM_read_bio_PrivateKey_ex(bio, &privkey, &passcb, NULL, libctx, "?fips=true")))
75*e7be843bSPierre Pronchery goto err;
76*e7be843bSPierre Pronchery
77*e7be843bSPierre Pronchery ret = 1;
78*e7be843bSPierre Pronchery err:
79*e7be843bSPierre Pronchery BIO_free(bio);
80*e7be843bSPierre Pronchery EVP_PKEY_free(privkey);
81*e7be843bSPierre Pronchery return ret;
82*e7be843bSPierre Pronchery }
83*e7be843bSPierre Pronchery
setup_tests(void)84*e7be843bSPierre Pronchery int setup_tests(void)
85*e7be843bSPierre Pronchery {
86*e7be843bSPierre Pronchery const char *prov_name = "default";
87*e7be843bSPierre Pronchery char *config_file = NULL;
88*e7be843bSPierre Pronchery OPTION_CHOICE o;
89*e7be843bSPierre Pronchery
90*e7be843bSPierre Pronchery while ((o = opt_next()) != OPT_EOF) {
91*e7be843bSPierre Pronchery switch (o) {
92*e7be843bSPierre Pronchery case OPT_PROVIDER_NAME:
93*e7be843bSPierre Pronchery prov_name = opt_arg();
94*e7be843bSPierre Pronchery break;
95*e7be843bSPierre Pronchery case OPT_CONFIG_FILE:
96*e7be843bSPierre Pronchery config_file = opt_arg();
97*e7be843bSPierre Pronchery break;
98*e7be843bSPierre Pronchery case OPT_TEST_CASES:
99*e7be843bSPierre Pronchery break;
100*e7be843bSPierre Pronchery default:
101*e7be843bSPierre Pronchery case OPT_ERR:
102*e7be843bSPierre Pronchery return 0;
103*e7be843bSPierre Pronchery }
104*e7be843bSPierre Pronchery }
105*e7be843bSPierre Pronchery
106*e7be843bSPierre Pronchery filename = test_get_argument(0);
107*e7be843bSPierre Pronchery if (!test_get_libctx(&libctx, &nullprov, config_file, &libprov, prov_name))
108*e7be843bSPierre Pronchery return 0;
109*e7be843bSPierre Pronchery
110*e7be843bSPierre Pronchery ADD_TEST(test_decode_nonfipsalg);
111*e7be843bSPierre Pronchery return 1;
112*e7be843bSPierre Pronchery }
113*e7be843bSPierre Pronchery
cleanup_tests(void)114*e7be843bSPierre Pronchery void cleanup_tests(void)
115*e7be843bSPierre Pronchery {
116*e7be843bSPierre Pronchery OSSL_PROVIDER_unload(libprov);
117*e7be843bSPierre Pronchery OSSL_LIB_CTX_free(libctx);
118*e7be843bSPierre Pronchery OSSL_PROVIDER_unload(nullprov);
119*e7be843bSPierre Pronchery }
120