util/mknum.pl: Really allow unset ordinals in development
[openssl.git] / test / ossl_store_test.c
1 /*
2  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <limits.h>
11 #include <openssl/store.h>
12 #include <openssl/ui.h>
13 #include "testutil.h"
14
15 #ifndef PATH_MAX
16 # if defined(_WIN32) && defined(_MAX_PATH)
17 #  define PATH_MAX _MAX_PATH
18 # else
19 #  define PATH_MAX 4096
20 # endif
21 #endif
22
23 typedef enum OPTION_choice {
24     OPT_ERR = -1,
25     OPT_EOF = 0,
26     OPT_INPUTDIR,
27     OPT_INFILE,
28     OPT_SM2FILE,
29     OPT_DATADIR,
30     OPT_TEST_ENUM
31 } OPTION_CHOICE;
32
33 static const char *inputdir = NULL;
34 static const char *infile = NULL;
35 static const char *sm2file = NULL;
36 static const char *datadir = NULL;
37
38 static int test_store_open(void)
39 {
40     int ret = 0;
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);
45
46     ret = TEST_ptr(input)
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);
56     OPENSSL_free(input);
57     return ret;
58 }
59
60 static int test_store_search_by_key_fingerprint_fail(void)
61 {
62     int ret;
63     OSSL_STORE_SEARCH *search = NULL;
64
65     ret = TEST_ptr_null(search = OSSL_STORE_SEARCH_by_key_fingerprint(
66                                      EVP_sha256(), NULL, 0));
67     OSSL_STORE_SEARCH_free(search);
68     return ret;
69 }
70
71 static int get_params(const char *uri, const char *type)
72 {
73     EVP_PKEY *pkey = NULL;
74     OSSL_STORE_CTX *ctx = NULL;
75     OSSL_STORE_INFO *info;
76     int ret = 0;
77
78     ctx = OSSL_STORE_open_ex(uri, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
79     if (!TEST_ptr(ctx))
80         goto err;
81
82     while (!OSSL_STORE_eof(ctx)
83             && (info = OSSL_STORE_load(ctx)) != NULL
84             && pkey == NULL) {
85         if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
86             pkey = OSSL_STORE_INFO_get1_PARAMS(info);
87         }
88         OSSL_STORE_INFO_free(info);
89         info = NULL;
90     }
91
92     if (pkey != NULL)
93         ret = EVP_PKEY_is_a(pkey, type);
94     EVP_PKEY_free(pkey);
95
96  err:
97     OSSL_STORE_close(ctx);
98     return ret;
99 }
100
101 static int test_store_get_params(int idx)
102 {
103     const char *type;
104     char uri[PATH_MAX];
105
106     switch(idx) {
107 #ifndef OPENSSL_NO_DH
108     case 0:
109         type = "DH";
110         break;
111     case 1:
112         type = "DHX";
113         break;
114 #else
115     case 0:
116     case 1:
117         return 1;
118 #endif
119     case 2:
120 #ifndef OPENSSL_NO_DSA
121         type = "DSA";
122         break;
123 #else
124         return 1;
125 #endif
126     default:
127         TEST_error("Invalid test index");
128         return 0;
129     }
130
131     if (!TEST_true(BIO_snprintf(uri, sizeof(uri), "%s/%s-params.pem",
132                                 datadir, type)))
133         return 0;
134
135     TEST_info("Testing uri: %s", uri);
136     if (!TEST_true(get_params(uri, type)))
137         return 0;
138
139     return 1;
140 }
141
142 /*
143  * This test verifies that calling OSSL_STORE_ATTACH does not set an
144  * "unregistered scheme" error when called.
145  */
146 static int test_store_attach_unregistered_scheme(void)
147 {
148     int ret;
149     OSSL_STORE_CTX *store_ctx = NULL;
150     OSSL_PROVIDER *provider = NULL;
151     OSSL_LIB_CTX *libctx = NULL;
152     BIO *bio = NULL;
153     char *input = test_mk_file_path(inputdir, sm2file);
154
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);
164
165     BIO_free(bio);
166     OSSL_STORE_close(store_ctx);
167     OSSL_PROVIDER_unload(provider);
168     OSSL_LIB_CTX_free(libctx);
169     OPENSSL_free(input);
170     return ret;
171 }
172
173 const OPTIONS *test_get_options(void)
174 {
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' },
181         { NULL }
182     };
183     return test_options;
184 }
185
186 int setup_tests(void)
187 {
188     OPTION_CHOICE o;
189
190     while ((o = opt_next()) != OPT_EOF) {
191         switch (o) {
192         case OPT_INPUTDIR:
193             inputdir = opt_arg();
194             break;
195         case OPT_INFILE:
196             infile = opt_arg();
197             break;
198         case OPT_SM2FILE:
199             sm2file = opt_arg();
200             break;
201         case OPT_DATADIR:
202             datadir = opt_arg();
203             break;
204         case OPT_TEST_CASES:
205            break;
206         default:
207         case OPT_ERR:
208             return 0;
209         }
210     }
211
212     if (datadir == NULL) {
213         TEST_error("No data directory specified");
214         return 0;
215     }
216     if (inputdir == NULL) {
217         TEST_error("No input directory specified");
218         return 0;
219     }
220
221     if (infile != NULL)
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);
225     if (sm2file != NULL)
226         ADD_TEST(test_store_attach_unregistered_scheme);
227     return 1;
228 }