2 * Copyright 2020 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 {
31 static const char *infile = NULL;
32 static const char *datadir = NULL;
34 static int test_store_open(void)
37 OSSL_STORE_CTX *sctx = NULL;
38 OSSL_STORE_SEARCH *search = NULL;
39 UI_METHOD *ui_method = NULL;
41 ret = TEST_ptr(search = OSSL_STORE_SEARCH_by_alias("nothing"))
42 && TEST_ptr(ui_method= UI_create_method("DummyUI"))
43 && TEST_ptr(sctx = OSSL_STORE_open_ex(infile, NULL, NULL, ui_method,
45 && TEST_false(OSSL_STORE_find(sctx, NULL))
46 && TEST_true(OSSL_STORE_find(sctx, search));
47 UI_destroy_method(ui_method);
48 OSSL_STORE_SEARCH_free(search);
49 OSSL_STORE_close(sctx);
53 static int test_store_search_by_key_fingerprint_fail(void)
56 OSSL_STORE_SEARCH *search = NULL;
58 ret = TEST_ptr_null(search = OSSL_STORE_SEARCH_by_key_fingerprint(
59 EVP_sha256(), NULL, 0));
60 OSSL_STORE_SEARCH_free(search);
64 static int get_params(const char *uri, const char *type)
66 EVP_PKEY *pkey = NULL;
67 OSSL_STORE_CTX *ctx = NULL;
68 OSSL_STORE_INFO *info;
71 ctx = OSSL_STORE_open_ex(uri, NULL, NULL, NULL, NULL, NULL, NULL);
75 while (!OSSL_STORE_eof(ctx)
76 && (info = OSSL_STORE_load(ctx)) != NULL
78 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
79 pkey = OSSL_STORE_INFO_get1_PARAMS(info);
81 OSSL_STORE_INFO_free(info);
86 ret = EVP_PKEY_is_a(pkey, type);
90 OSSL_STORE_close(ctx);
94 static int test_store_get_params(int idx)
100 #ifndef OPENSSL_NO_DH
113 #ifndef OPENSSL_NO_DSA
120 TEST_error("Invalid test index");
124 if (!TEST_true(BIO_snprintf(uri, sizeof(uri), "%s/%s-params.pem",
128 TEST_info("Testing uri: %s", uri);
129 if (!TEST_true(get_params(uri, type)))
136 * This test verifies that calling OSSL_STORE_ATTACH does not set an
137 * "unregistered scheme" error when called.
139 static int test_store_attach_unregistered_scheme(void)
142 OSSL_STORE_CTX *store_ctx;
143 OSSL_PROVIDER *provider;
144 OSSL_LIB_CTX *libctx;
146 libctx = OSSL_LIB_CTX_new();
147 provider = OSSL_PROVIDER_load(libctx, "default");
148 bio = BIO_new_file("test/certs/sm2-root.crt", "r");
150 ret = TEST_ptr(store_ctx = OSSL_STORE_attach(bio, "file", libctx, NULL,
151 NULL, NULL, NULL, NULL)) &&
152 TEST_int_ne(ERR_GET_LIB(ERR_peek_error()), ERR_LIB_OSSL_STORE) &&
153 TEST_int_ne(ERR_GET_REASON(ERR_peek_error()),
154 OSSL_STORE_R_UNREGISTERED_SCHEME);
157 OSSL_STORE_close(store_ctx);
158 OSSL_PROVIDER_unload(provider);
159 OSSL_LIB_CTX_free(libctx);
163 const OPTIONS *test_get_options(void)
165 static const OPTIONS test_options[] = {
166 OPT_TEST_OPTIONS_DEFAULT_USAGE,
167 { "in", OPT_INFILE, '<', },
168 { "data", OPT_DATADIR, 's' },
174 int setup_tests(void)
178 while ((o = opt_next()) != OPT_EOF) {
194 if (datadir == NULL) {
195 TEST_error("No datadir specified");
199 ADD_TEST(test_store_open);
200 ADD_TEST(test_store_search_by_key_fingerprint_fail);
201 ADD_ALL_TESTS(test_store_get_params, 3);
202 ADD_TEST(test_store_attach_unregistered_scheme);