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