2 * Copyright 2020-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
12 #include <openssl/store.h>
13 #include <openssl/ui.h>
17 # if defined(_WIN32) && defined(_MAX_PATH)
18 # define PATH_MAX _MAX_PATH
20 # define PATH_MAX 4096
24 typedef enum OPTION_choice {
34 static const char *inputdir = NULL;
35 static const char *infile = NULL;
36 static const char *sm2file = NULL;
37 static const char *datadir = NULL;
39 static int test_store_open(void)
42 OSSL_STORE_CTX *sctx = NULL;
43 OSSL_STORE_SEARCH *search = NULL;
44 UI_METHOD *ui_method = NULL;
45 char *input = test_mk_file_path(inputdir, infile);
48 && TEST_ptr(search = OSSL_STORE_SEARCH_by_alias("nothing"))
49 && TEST_ptr(ui_method= UI_create_method("DummyUI"))
50 && TEST_ptr(sctx = OSSL_STORE_open_ex(input, NULL, NULL, ui_method,
51 NULL, NULL, NULL, NULL))
52 && TEST_false(OSSL_STORE_find(sctx, NULL))
53 && TEST_true(OSSL_STORE_find(sctx, search));
54 UI_destroy_method(ui_method);
55 OSSL_STORE_SEARCH_free(search);
56 OSSL_STORE_close(sctx);
61 static int test_store_search_by_key_fingerprint_fail(void)
64 OSSL_STORE_SEARCH *search = NULL;
66 ret = TEST_ptr_null(search = OSSL_STORE_SEARCH_by_key_fingerprint(
67 EVP_sha256(), NULL, 0));
68 OSSL_STORE_SEARCH_free(search);
72 static int get_params(const char *uri, const char *type)
74 EVP_PKEY *pkey = NULL;
75 OSSL_STORE_CTX *ctx = NULL;
76 OSSL_STORE_INFO *info;
79 ctx = OSSL_STORE_open_ex(uri, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
83 while (!OSSL_STORE_eof(ctx)
84 && (info = OSSL_STORE_load(ctx)) != NULL
86 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
87 pkey = OSSL_STORE_INFO_get1_PARAMS(info);
89 OSSL_STORE_INFO_free(info);
94 ret = EVP_PKEY_is_a(pkey, type);
98 OSSL_STORE_close(ctx);
102 static int test_store_get_params(int idx)
109 #ifndef OPENSSL_NO_DH
122 #ifndef OPENSSL_NO_DSA
129 TEST_error("Invalid test index");
133 urifmt = "%s/%s-params.pem";
136 char datadir_end = datadir[strlen(datadir) - 1];
138 if (datadir_end == ':' || datadir_end == ']' || datadir_end == '>')
139 urifmt = "%s%s-params.pem";
142 if (!TEST_true(BIO_snprintf(uri, sizeof(uri), urifmt, datadir, type)))
145 TEST_info("Testing uri: %s", uri);
146 if (!TEST_true(get_params(uri, type)))
153 * This test verifies that calling OSSL_STORE_ATTACH does not set an
154 * "unregistered scheme" error when called.
156 static int test_store_attach_unregistered_scheme(void)
159 OSSL_STORE_CTX *store_ctx = NULL;
160 OSSL_PROVIDER *provider = NULL;
161 OSSL_LIB_CTX *libctx = NULL;
163 char *input = test_mk_file_path(inputdir, sm2file);
165 ret = TEST_ptr(input)
166 && TEST_ptr(libctx = OSSL_LIB_CTX_new())
167 && TEST_ptr(provider = OSSL_PROVIDER_load(libctx, "default"))
168 && TEST_ptr(bio = BIO_new_file(input, "r"))
169 && TEST_ptr(store_ctx = OSSL_STORE_attach(bio, "file", libctx, NULL,
170 NULL, NULL, NULL, NULL, NULL))
171 && TEST_int_ne(ERR_GET_LIB(ERR_peek_error()), ERR_LIB_OSSL_STORE)
172 && TEST_int_ne(ERR_GET_REASON(ERR_peek_error()),
173 OSSL_STORE_R_UNREGISTERED_SCHEME);
176 OSSL_STORE_close(store_ctx);
177 OSSL_PROVIDER_unload(provider);
178 OSSL_LIB_CTX_free(libctx);
183 const OPTIONS *test_get_options(void)
185 static const OPTIONS test_options[] = {
186 OPT_TEST_OPTIONS_DEFAULT_USAGE,
187 { "dir", OPT_INPUTDIR, '/' },
188 { "in", OPT_INFILE, '<' },
189 { "sm2", OPT_SM2FILE, '<' },
190 { "data", OPT_DATADIR, 's' },
196 int setup_tests(void)
200 while ((o = opt_next()) != OPT_EOF) {
203 inputdir = opt_arg();
222 if (datadir == NULL) {
223 TEST_error("No data directory specified");
226 if (inputdir == NULL) {
227 TEST_error("No input directory specified");
232 ADD_TEST(test_store_open);
233 ADD_TEST(test_store_search_by_key_fingerprint_fail);
234 ADD_ALL_TESTS(test_store_get_params, 3);
236 ADD_TEST(test_store_attach_unregistered_scheme);