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
11 #include <openssl/store.h>
12 #include <openssl/ui.h>
16 # if defined(_WIN32) && defined(_MAX_PATH)
17 # define PATH_MAX _MAX_PATH
19 # define PATH_MAX 4096
23 typedef enum OPTION_choice {
33 static const char *inputdir = NULL;
34 static const char *infile = NULL;
35 static const char *sm2file = NULL;
36 static const char *datadir = NULL;
38 static int test_store_open(void)
41 OSSL_STORE_CTX *sctx = NULL;
42 OSSL_STORE_SEARCH *search = NULL;
43 UI_METHOD *ui_method = NULL;
44 char *input = test_mk_file_path(inputdir, infile);
47 && TEST_ptr(search = OSSL_STORE_SEARCH_by_alias("nothing"))
48 && TEST_ptr(ui_method= UI_create_method("DummyUI"))
49 && TEST_ptr(sctx = OSSL_STORE_open_ex(input, NULL, NULL, ui_method,
50 NULL, NULL, NULL, NULL))
51 && TEST_false(OSSL_STORE_find(sctx, NULL))
52 && TEST_true(OSSL_STORE_find(sctx, search));
53 UI_destroy_method(ui_method);
54 OSSL_STORE_SEARCH_free(search);
55 OSSL_STORE_close(sctx);
60 static int test_store_search_by_key_fingerprint_fail(void)
63 OSSL_STORE_SEARCH *search = NULL;
65 ret = TEST_ptr_null(search = OSSL_STORE_SEARCH_by_key_fingerprint(
66 EVP_sha256(), NULL, 0));
67 OSSL_STORE_SEARCH_free(search);
71 static int get_params(const char *uri, const char *type)
73 EVP_PKEY *pkey = NULL;
74 OSSL_STORE_CTX *ctx = NULL;
75 OSSL_STORE_INFO *info;
78 ctx = OSSL_STORE_open_ex(uri, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
82 while (!OSSL_STORE_eof(ctx)
83 && (info = OSSL_STORE_load(ctx)) != NULL
85 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
86 pkey = OSSL_STORE_INFO_get1_PARAMS(info);
88 OSSL_STORE_INFO_free(info);
93 ret = EVP_PKEY_is_a(pkey, type);
97 OSSL_STORE_close(ctx);
101 static int test_store_get_params(int idx)
107 #ifndef OPENSSL_NO_DH
120 #ifndef OPENSSL_NO_DSA
127 TEST_error("Invalid test index");
131 if (!TEST_true(BIO_snprintf(uri, sizeof(uri), "%s/%s-params.pem",
135 TEST_info("Testing uri: %s", uri);
136 if (!TEST_true(get_params(uri, type)))
143 * This test verifies that calling OSSL_STORE_ATTACH does not set an
144 * "unregistered scheme" error when called.
146 static int test_store_attach_unregistered_scheme(void)
149 OSSL_STORE_CTX *store_ctx = NULL;
150 OSSL_PROVIDER *provider = NULL;
151 OSSL_LIB_CTX *libctx = NULL;
153 char *input = test_mk_file_path(inputdir, sm2file);
155 ret = TEST_ptr(input)
156 && TEST_ptr(libctx = OSSL_LIB_CTX_new())
157 && TEST_ptr(provider = OSSL_PROVIDER_load(libctx, "default"))
158 && TEST_ptr(bio = BIO_new_file(input, "r"))
159 && TEST_ptr(store_ctx = OSSL_STORE_attach(bio, "file", libctx, NULL,
160 NULL, NULL, NULL, NULL, NULL))
161 && TEST_int_ne(ERR_GET_LIB(ERR_peek_error()), ERR_LIB_OSSL_STORE)
162 && TEST_int_ne(ERR_GET_REASON(ERR_peek_error()),
163 OSSL_STORE_R_UNREGISTERED_SCHEME);
166 OSSL_STORE_close(store_ctx);
167 OSSL_PROVIDER_unload(provider);
168 OSSL_LIB_CTX_free(libctx);
173 const OPTIONS *test_get_options(void)
175 static const OPTIONS test_options[] = {
176 OPT_TEST_OPTIONS_DEFAULT_USAGE,
177 { "dir", OPT_INPUTDIR, '/' },
178 { "in", OPT_INFILE, '<' },
179 { "sm2", OPT_SM2FILE, '<' },
180 { "data", OPT_DATADIR, 's' },
186 int setup_tests(void)
190 while ((o = opt_next()) != OPT_EOF) {
193 inputdir = opt_arg();
212 if (datadir == NULL) {
213 TEST_error("No data directory specified");
216 if (inputdir == NULL) {
217 TEST_error("No input directory specified");
222 ADD_TEST(test_store_open);
223 ADD_TEST(test_store_search_by_key_fingerprint_fail);
224 ADD_ALL_TESTS(test_store_get_params, 3);
226 ADD_TEST(test_store_attach_unregistered_scheme);