1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2020-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 <string.h>
11*e0c4386eSCy Schubert #include <limits.h>
12*e0c4386eSCy Schubert #include <openssl/store.h>
13*e0c4386eSCy Schubert #include <openssl/ui.h>
14*e0c4386eSCy Schubert #include "testutil.h"
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert #ifndef PATH_MAX
17*e0c4386eSCy Schubert # if defined(_WIN32) && defined(_MAX_PATH)
18*e0c4386eSCy Schubert # define PATH_MAX _MAX_PATH
19*e0c4386eSCy Schubert # else
20*e0c4386eSCy Schubert # define PATH_MAX 4096
21*e0c4386eSCy Schubert # endif
22*e0c4386eSCy Schubert #endif
23*e0c4386eSCy Schubert
24*e0c4386eSCy Schubert typedef enum OPTION_choice {
25*e0c4386eSCy Schubert OPT_ERR = -1,
26*e0c4386eSCy Schubert OPT_EOF = 0,
27*e0c4386eSCy Schubert OPT_INPUTDIR,
28*e0c4386eSCy Schubert OPT_INFILE,
29*e0c4386eSCy Schubert OPT_SM2FILE,
30*e0c4386eSCy Schubert OPT_DATADIR,
31*e0c4386eSCy Schubert OPT_TEST_ENUM
32*e0c4386eSCy Schubert } OPTION_CHOICE;
33*e0c4386eSCy Schubert
34*e0c4386eSCy Schubert static const char *inputdir = NULL;
35*e0c4386eSCy Schubert static const char *infile = NULL;
36*e0c4386eSCy Schubert static const char *sm2file = NULL;
37*e0c4386eSCy Schubert static const char *datadir = NULL;
38*e0c4386eSCy Schubert
test_store_open(void)39*e0c4386eSCy Schubert static int test_store_open(void)
40*e0c4386eSCy Schubert {
41*e0c4386eSCy Schubert int ret = 0;
42*e0c4386eSCy Schubert OSSL_STORE_CTX *sctx = NULL;
43*e0c4386eSCy Schubert OSSL_STORE_SEARCH *search = NULL;
44*e0c4386eSCy Schubert UI_METHOD *ui_method = NULL;
45*e0c4386eSCy Schubert char *input = test_mk_file_path(inputdir, infile);
46*e0c4386eSCy Schubert
47*e0c4386eSCy Schubert ret = TEST_ptr(input)
48*e0c4386eSCy Schubert && TEST_ptr(search = OSSL_STORE_SEARCH_by_alias("nothing"))
49*e0c4386eSCy Schubert && TEST_ptr(ui_method= UI_create_method("DummyUI"))
50*e0c4386eSCy Schubert && TEST_ptr(sctx = OSSL_STORE_open_ex(input, NULL, NULL, ui_method,
51*e0c4386eSCy Schubert NULL, NULL, NULL, NULL))
52*e0c4386eSCy Schubert && TEST_false(OSSL_STORE_find(sctx, NULL))
53*e0c4386eSCy Schubert && TEST_true(OSSL_STORE_find(sctx, search));
54*e0c4386eSCy Schubert UI_destroy_method(ui_method);
55*e0c4386eSCy Schubert OSSL_STORE_SEARCH_free(search);
56*e0c4386eSCy Schubert OSSL_STORE_close(sctx);
57*e0c4386eSCy Schubert OPENSSL_free(input);
58*e0c4386eSCy Schubert return ret;
59*e0c4386eSCy Schubert }
60*e0c4386eSCy Schubert
test_store_search_by_key_fingerprint_fail(void)61*e0c4386eSCy Schubert static int test_store_search_by_key_fingerprint_fail(void)
62*e0c4386eSCy Schubert {
63*e0c4386eSCy Schubert int ret;
64*e0c4386eSCy Schubert OSSL_STORE_SEARCH *search = NULL;
65*e0c4386eSCy Schubert
66*e0c4386eSCy Schubert ret = TEST_ptr_null(search = OSSL_STORE_SEARCH_by_key_fingerprint(
67*e0c4386eSCy Schubert EVP_sha256(), NULL, 0));
68*e0c4386eSCy Schubert OSSL_STORE_SEARCH_free(search);
69*e0c4386eSCy Schubert return ret;
70*e0c4386eSCy Schubert }
71*e0c4386eSCy Schubert
get_params(const char * uri,const char * type)72*e0c4386eSCy Schubert static int get_params(const char *uri, const char *type)
73*e0c4386eSCy Schubert {
74*e0c4386eSCy Schubert EVP_PKEY *pkey = NULL;
75*e0c4386eSCy Schubert OSSL_STORE_CTX *ctx = NULL;
76*e0c4386eSCy Schubert OSSL_STORE_INFO *info;
77*e0c4386eSCy Schubert int ret = 0;
78*e0c4386eSCy Schubert
79*e0c4386eSCy Schubert ctx = OSSL_STORE_open_ex(uri, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
80*e0c4386eSCy Schubert if (!TEST_ptr(ctx))
81*e0c4386eSCy Schubert goto err;
82*e0c4386eSCy Schubert
83*e0c4386eSCy Schubert while (!OSSL_STORE_eof(ctx)
84*e0c4386eSCy Schubert && (info = OSSL_STORE_load(ctx)) != NULL
85*e0c4386eSCy Schubert && pkey == NULL) {
86*e0c4386eSCy Schubert if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
87*e0c4386eSCy Schubert pkey = OSSL_STORE_INFO_get1_PARAMS(info);
88*e0c4386eSCy Schubert }
89*e0c4386eSCy Schubert OSSL_STORE_INFO_free(info);
90*e0c4386eSCy Schubert info = NULL;
91*e0c4386eSCy Schubert }
92*e0c4386eSCy Schubert
93*e0c4386eSCy Schubert if (pkey != NULL)
94*e0c4386eSCy Schubert ret = EVP_PKEY_is_a(pkey, type);
95*e0c4386eSCy Schubert EVP_PKEY_free(pkey);
96*e0c4386eSCy Schubert
97*e0c4386eSCy Schubert err:
98*e0c4386eSCy Schubert OSSL_STORE_close(ctx);
99*e0c4386eSCy Schubert return ret;
100*e0c4386eSCy Schubert }
101*e0c4386eSCy Schubert
test_store_get_params(int idx)102*e0c4386eSCy Schubert static int test_store_get_params(int idx)
103*e0c4386eSCy Schubert {
104*e0c4386eSCy Schubert const char *type;
105*e0c4386eSCy Schubert const char *urifmt;
106*e0c4386eSCy Schubert char uri[PATH_MAX];
107*e0c4386eSCy Schubert
108*e0c4386eSCy Schubert switch(idx) {
109*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DH
110*e0c4386eSCy Schubert case 0:
111*e0c4386eSCy Schubert type = "DH";
112*e0c4386eSCy Schubert break;
113*e0c4386eSCy Schubert case 1:
114*e0c4386eSCy Schubert type = "DHX";
115*e0c4386eSCy Schubert break;
116*e0c4386eSCy Schubert #else
117*e0c4386eSCy Schubert case 0:
118*e0c4386eSCy Schubert case 1:
119*e0c4386eSCy Schubert return 1;
120*e0c4386eSCy Schubert #endif
121*e0c4386eSCy Schubert case 2:
122*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DSA
123*e0c4386eSCy Schubert type = "DSA";
124*e0c4386eSCy Schubert break;
125*e0c4386eSCy Schubert #else
126*e0c4386eSCy Schubert return 1;
127*e0c4386eSCy Schubert #endif
128*e0c4386eSCy Schubert default:
129*e0c4386eSCy Schubert TEST_error("Invalid test index");
130*e0c4386eSCy Schubert return 0;
131*e0c4386eSCy Schubert }
132*e0c4386eSCy Schubert
133*e0c4386eSCy Schubert urifmt = "%s/%s-params.pem";
134*e0c4386eSCy Schubert #ifdef __VMS
135*e0c4386eSCy Schubert {
136*e0c4386eSCy Schubert char datadir_end = datadir[strlen(datadir) - 1];
137*e0c4386eSCy Schubert
138*e0c4386eSCy Schubert if (datadir_end == ':' || datadir_end == ']' || datadir_end == '>')
139*e0c4386eSCy Schubert urifmt = "%s%s-params.pem";
140*e0c4386eSCy Schubert }
141*e0c4386eSCy Schubert #endif
142*e0c4386eSCy Schubert if (!TEST_true(BIO_snprintf(uri, sizeof(uri), urifmt, datadir, type)))
143*e0c4386eSCy Schubert return 0;
144*e0c4386eSCy Schubert
145*e0c4386eSCy Schubert TEST_info("Testing uri: %s", uri);
146*e0c4386eSCy Schubert if (!TEST_true(get_params(uri, type)))
147*e0c4386eSCy Schubert return 0;
148*e0c4386eSCy Schubert
149*e0c4386eSCy Schubert return 1;
150*e0c4386eSCy Schubert }
151*e0c4386eSCy Schubert
152*e0c4386eSCy Schubert /*
153*e0c4386eSCy Schubert * This test verifies that calling OSSL_STORE_ATTACH does not set an
154*e0c4386eSCy Schubert * "unregistered scheme" error when called.
155*e0c4386eSCy Schubert */
test_store_attach_unregistered_scheme(void)156*e0c4386eSCy Schubert static int test_store_attach_unregistered_scheme(void)
157*e0c4386eSCy Schubert {
158*e0c4386eSCy Schubert int ret;
159*e0c4386eSCy Schubert OSSL_STORE_CTX *store_ctx = NULL;
160*e0c4386eSCy Schubert OSSL_PROVIDER *provider = NULL;
161*e0c4386eSCy Schubert OSSL_LIB_CTX *libctx = NULL;
162*e0c4386eSCy Schubert BIO *bio = NULL;
163*e0c4386eSCy Schubert char *input = test_mk_file_path(inputdir, sm2file);
164*e0c4386eSCy Schubert
165*e0c4386eSCy Schubert ret = TEST_ptr(input)
166*e0c4386eSCy Schubert && TEST_ptr(libctx = OSSL_LIB_CTX_new())
167*e0c4386eSCy Schubert && TEST_ptr(provider = OSSL_PROVIDER_load(libctx, "default"))
168*e0c4386eSCy Schubert && TEST_ptr(bio = BIO_new_file(input, "r"))
169*e0c4386eSCy Schubert && TEST_ptr(store_ctx = OSSL_STORE_attach(bio, "file", libctx, NULL,
170*e0c4386eSCy Schubert NULL, NULL, NULL, NULL, NULL))
171*e0c4386eSCy Schubert && TEST_int_ne(ERR_GET_LIB(ERR_peek_error()), ERR_LIB_OSSL_STORE)
172*e0c4386eSCy Schubert && TEST_int_ne(ERR_GET_REASON(ERR_peek_error()),
173*e0c4386eSCy Schubert OSSL_STORE_R_UNREGISTERED_SCHEME);
174*e0c4386eSCy Schubert
175*e0c4386eSCy Schubert BIO_free(bio);
176*e0c4386eSCy Schubert OSSL_STORE_close(store_ctx);
177*e0c4386eSCy Schubert OSSL_PROVIDER_unload(provider);
178*e0c4386eSCy Schubert OSSL_LIB_CTX_free(libctx);
179*e0c4386eSCy Schubert OPENSSL_free(input);
180*e0c4386eSCy Schubert return ret;
181*e0c4386eSCy Schubert }
182*e0c4386eSCy Schubert
test_get_options(void)183*e0c4386eSCy Schubert const OPTIONS *test_get_options(void)
184*e0c4386eSCy Schubert {
185*e0c4386eSCy Schubert static const OPTIONS test_options[] = {
186*e0c4386eSCy Schubert OPT_TEST_OPTIONS_DEFAULT_USAGE,
187*e0c4386eSCy Schubert { "dir", OPT_INPUTDIR, '/' },
188*e0c4386eSCy Schubert { "in", OPT_INFILE, '<' },
189*e0c4386eSCy Schubert { "sm2", OPT_SM2FILE, '<' },
190*e0c4386eSCy Schubert { "data", OPT_DATADIR, 's' },
191*e0c4386eSCy Schubert { NULL }
192*e0c4386eSCy Schubert };
193*e0c4386eSCy Schubert return test_options;
194*e0c4386eSCy Schubert }
195*e0c4386eSCy Schubert
setup_tests(void)196*e0c4386eSCy Schubert int setup_tests(void)
197*e0c4386eSCy Schubert {
198*e0c4386eSCy Schubert OPTION_CHOICE o;
199*e0c4386eSCy Schubert
200*e0c4386eSCy Schubert while ((o = opt_next()) != OPT_EOF) {
201*e0c4386eSCy Schubert switch (o) {
202*e0c4386eSCy Schubert case OPT_INPUTDIR:
203*e0c4386eSCy Schubert inputdir = opt_arg();
204*e0c4386eSCy Schubert break;
205*e0c4386eSCy Schubert case OPT_INFILE:
206*e0c4386eSCy Schubert infile = opt_arg();
207*e0c4386eSCy Schubert break;
208*e0c4386eSCy Schubert case OPT_SM2FILE:
209*e0c4386eSCy Schubert sm2file = opt_arg();
210*e0c4386eSCy Schubert break;
211*e0c4386eSCy Schubert case OPT_DATADIR:
212*e0c4386eSCy Schubert datadir = opt_arg();
213*e0c4386eSCy Schubert break;
214*e0c4386eSCy Schubert case OPT_TEST_CASES:
215*e0c4386eSCy Schubert break;
216*e0c4386eSCy Schubert default:
217*e0c4386eSCy Schubert case OPT_ERR:
218*e0c4386eSCy Schubert return 0;
219*e0c4386eSCy Schubert }
220*e0c4386eSCy Schubert }
221*e0c4386eSCy Schubert
222*e0c4386eSCy Schubert if (datadir == NULL) {
223*e0c4386eSCy Schubert TEST_error("No data directory specified");
224*e0c4386eSCy Schubert return 0;
225*e0c4386eSCy Schubert }
226*e0c4386eSCy Schubert if (inputdir == NULL) {
227*e0c4386eSCy Schubert TEST_error("No input directory specified");
228*e0c4386eSCy Schubert return 0;
229*e0c4386eSCy Schubert }
230*e0c4386eSCy Schubert
231*e0c4386eSCy Schubert if (infile != NULL)
232*e0c4386eSCy Schubert ADD_TEST(test_store_open);
233*e0c4386eSCy Schubert ADD_TEST(test_store_search_by_key_fingerprint_fail);
234*e0c4386eSCy Schubert ADD_ALL_TESTS(test_store_get_params, 3);
235*e0c4386eSCy Schubert if (sm2file != NULL)
236*e0c4386eSCy Schubert ADD_TEST(test_store_attach_unregistered_scheme);
237*e0c4386eSCy Schubert return 1;
238*e0c4386eSCy Schubert }
239