Add test for query invalidation after new provider added
[openssl.git] / test / fake_rsaprov.c
1 /*
2  * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * https://www.openssl.org/source/license.html
8  * or in the file LICENSE in the source distribution.
9  */
10
11 #include <string.h>
12 #include <openssl/core_names.h>
13 #include <openssl/rand.h>
14 #include <openssl/provider.h>
15 #include "testutil.h"
16 #include "fake_rsaprov.h"
17
18 static OSSL_FUNC_keymgmt_new_fn fake_rsa_keymgmt_new;
19 static OSSL_FUNC_keymgmt_free_fn fake_rsa_keymgmt_free;
20 static OSSL_FUNC_keymgmt_has_fn fake_rsa_keymgmt_has;
21 static OSSL_FUNC_keymgmt_query_operation_name_fn fake_rsa_keymgmt_query;
22 static OSSL_FUNC_keymgmt_import_fn fake_rsa_keymgmt_import;
23 static OSSL_FUNC_keymgmt_import_types_fn fake_rsa_keymgmt_imptypes;
24
25 static int has_selection;
26 static int imptypes_selection;
27 static int query_id;
28
29 static void *fake_rsa_keymgmt_new(void *provctx)
30 {
31     unsigned char *keydata = OPENSSL_zalloc(1);
32
33     TEST_ptr(keydata);
34
35     /* clear test globals */
36     has_selection = 0;
37     imptypes_selection = 0;
38     query_id = 0;
39
40     return keydata;
41 }
42
43 static void fake_rsa_keymgmt_free(void *keydata)
44 {
45     OPENSSL_free(keydata);
46 }
47
48 static int fake_rsa_keymgmt_has(const void *key, int selection)
49 {
50     /* record global for checking */
51     has_selection = selection;
52
53     return 1;
54 }
55
56
57 static const char *fake_rsa_keymgmt_query(int id)
58 {
59     /* record global for checking */
60     query_id = id;
61
62     return "RSA";
63 }
64
65 static int fake_rsa_keymgmt_import(void *keydata, int selection,
66                                    const OSSL_PARAM *p)
67 {
68     unsigned char *fake_rsa_key = keydata;
69
70     /* key was imported */
71     *fake_rsa_key = 1;
72
73     return 1;
74 }
75
76 static const OSSL_PARAM fake_rsa_import_key_types[] = {
77     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
78     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
79     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
80     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0),
81     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0),
82     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0),
83     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0),
84     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),
85     OSSL_PARAM_END
86 };
87
88 static const OSSL_PARAM *fake_rsa_keymgmt_imptypes(int selection)
89 {
90     /* record global for checking */
91     imptypes_selection = selection;
92
93     return fake_rsa_import_key_types;
94 }
95
96 static void *fake_rsa_gen_init(void *provctx, int selection,
97                                const OSSL_PARAM params[])
98 {
99     unsigned char *gctx = NULL;
100
101     if (!TEST_ptr(gctx = OPENSSL_malloc(1)))
102         return NULL;
103
104     *gctx = 1;
105
106     return gctx;
107 }
108
109 static void *fake_rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
110 {
111     unsigned char *gctx = genctx;
112     static const unsigned char inited[] = { 1 };
113     unsigned char *keydata;
114
115     if (!TEST_ptr(gctx)
116         || !TEST_mem_eq(gctx, sizeof(*gctx), inited, sizeof(inited)))
117         return NULL;
118
119     if (!TEST_ptr(keydata = fake_rsa_keymgmt_new(NULL)))
120         return NULL;
121
122     *keydata = 2;
123     return keydata;
124 }
125
126 static void fake_rsa_gen_cleanup(void *genctx)
127 {
128    OPENSSL_free(genctx);
129 }
130
131 static const OSSL_DISPATCH fake_rsa_keymgmt_funcs[] = {
132     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))fake_rsa_keymgmt_new },
133     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))fake_rsa_keymgmt_free} ,
134     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))fake_rsa_keymgmt_has },
135     { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
136         (void (*)(void))fake_rsa_keymgmt_query },
137     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))fake_rsa_keymgmt_import },
138     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES,
139         (void (*)(void))fake_rsa_keymgmt_imptypes },
140     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))fake_rsa_gen_init },
141     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))fake_rsa_gen },
142     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))fake_rsa_gen_cleanup },
143     { 0, NULL }
144 };
145
146 static const OSSL_ALGORITHM fake_rsa_keymgmt_algs[] = {
147     { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_keymgmt_funcs, "Fake RSA Key Management" },
148     { NULL, NULL, NULL, NULL }
149 };
150
151 static OSSL_FUNC_signature_newctx_fn fake_rsa_sig_newctx;
152 static OSSL_FUNC_signature_freectx_fn fake_rsa_sig_freectx;
153 static OSSL_FUNC_signature_sign_init_fn fake_rsa_sig_sign_init;
154 static OSSL_FUNC_signature_sign_fn fake_rsa_sig_sign;
155
156 static void *fake_rsa_sig_newctx(void *provctx, const char *propq)
157 {
158     unsigned char *sigctx = OPENSSL_zalloc(1);
159
160     TEST_ptr(sigctx);
161
162     return sigctx;
163 }
164
165 static void fake_rsa_sig_freectx(void *sigctx)
166 {
167     OPENSSL_free(sigctx);
168 }
169
170 static int fake_rsa_sig_sign_init(void *ctx, void *provkey,
171                                   const OSSL_PARAM params[])
172 {
173     unsigned char *sigctx = ctx;
174     unsigned char *keydata = provkey;
175
176     /* we must have a ctx */
177     if (!TEST_ptr(sigctx))
178         return 0;
179
180     /* we must have some initialized key */
181     if (!TEST_ptr(keydata) || !TEST_int_gt(keydata[0], 0))
182         return 0;
183
184     /* record that sign init was called */
185     *sigctx = 1;
186     return 1;
187 }
188
189 static int fake_rsa_sig_sign(void *ctx, unsigned char *sig,
190                              size_t *siglen, size_t sigsize,
191                              const unsigned char *tbs, size_t tbslen)
192 {
193     unsigned char *sigctx = ctx;
194
195     /* we must have a ctx and init was called upon it */
196     if (!TEST_ptr(sigctx) || !TEST_int_eq(*sigctx, 1))
197         return 0;
198
199     *siglen = 256;
200     /* record that the real sign operation was called */
201     if (sig != NULL) {
202         if (!TEST_int_ge(sigsize, *siglen))
203             return 0;
204         *sigctx = 2;
205         /* produce a fake signature */
206         memset(sig, 'a', *siglen);
207     }
208
209     return 1;
210 }
211
212 static const OSSL_DISPATCH fake_rsa_sig_funcs[] = {
213     { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))fake_rsa_sig_newctx },
214     { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))fake_rsa_sig_freectx },
215     { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))fake_rsa_sig_sign_init },
216     { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))fake_rsa_sig_sign },
217     { 0, NULL }
218 };
219
220 static const OSSL_ALGORITHM fake_rsa_sig_algs[] = {
221     { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_sig_funcs, "Fake RSA Signature" },
222     { NULL, NULL, NULL, NULL }
223 };
224
225 static const OSSL_ALGORITHM *fake_rsa_query(void *provctx,
226                                             int operation_id,
227                                             int *no_cache)
228 {
229     *no_cache = 0;
230     switch (operation_id) {
231     case OSSL_OP_SIGNATURE:
232         return fake_rsa_sig_algs;
233
234     case OSSL_OP_KEYMGMT:
235         return fake_rsa_keymgmt_algs;
236     }
237     return NULL;
238 }
239
240 /* Functions we provide to the core */
241 static const OSSL_DISPATCH fake_rsa_method[] = {
242     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
243     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fake_rsa_query },
244     { 0, NULL }
245 };
246
247 static int fake_rsa_provider_init(const OSSL_CORE_HANDLE *handle,
248                                   const OSSL_DISPATCH *in,
249                                   const OSSL_DISPATCH **out, void **provctx)
250 {
251     if (!TEST_ptr(*provctx = OSSL_LIB_CTX_new()))
252         return 0;
253     *out = fake_rsa_method;
254     return 1;
255 }
256
257 OSSL_PROVIDER *fake_rsa_start(OSSL_LIB_CTX *libctx)
258 {
259     OSSL_PROVIDER *p;
260
261     if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "fake-rsa",
262                                              fake_rsa_provider_init))
263             || !TEST_ptr(p = OSSL_PROVIDER_try_load(libctx, "fake-rsa", 1)))
264         return NULL;
265
266     return p;
267 }
268
269 void fake_rsa_finish(OSSL_PROVIDER *p)
270 {
271     OSSL_PROVIDER_unload(p);
272 }