2 * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
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.
12 #include <openssl/core_names.h>
13 #include <openssl/core_object.h>
14 #include <openssl/rand.h>
15 #include <openssl/provider.h>
17 #include "fake_rsaprov.h"
19 static OSSL_FUNC_keymgmt_new_fn fake_rsa_keymgmt_new;
20 static OSSL_FUNC_keymgmt_free_fn fake_rsa_keymgmt_free;
21 static OSSL_FUNC_keymgmt_has_fn fake_rsa_keymgmt_has;
22 static OSSL_FUNC_keymgmt_query_operation_name_fn fake_rsa_keymgmt_query;
23 static OSSL_FUNC_keymgmt_import_fn fake_rsa_keymgmt_import;
24 static OSSL_FUNC_keymgmt_import_types_fn fake_rsa_keymgmt_imptypes;
25 static OSSL_FUNC_keymgmt_load_fn fake_rsa_keymgmt_load;
27 static int has_selection;
28 static int imptypes_selection;
31 static void *fake_rsa_keymgmt_new(void *provctx)
33 unsigned char *keydata = OPENSSL_zalloc(1);
37 /* clear test globals */
39 imptypes_selection = 0;
45 static void fake_rsa_keymgmt_free(void *keydata)
47 OPENSSL_free(keydata);
50 static int fake_rsa_keymgmt_has(const void *key, int selection)
52 /* record global for checking */
53 has_selection = selection;
59 static const char *fake_rsa_keymgmt_query(int id)
61 /* record global for checking */
67 static int fake_rsa_keymgmt_import(void *keydata, int selection,
70 unsigned char *fake_rsa_key = keydata;
72 /* key was imported */
78 static const OSSL_PARAM fake_rsa_import_key_types[] = {
79 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
80 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
81 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
82 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0),
83 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0),
84 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0),
85 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0),
86 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),
90 static const OSSL_PARAM *fake_rsa_keymgmt_imptypes(int selection)
92 /* record global for checking */
93 imptypes_selection = selection;
95 return fake_rsa_import_key_types;
98 static void *fake_rsa_keymgmt_load(const void *reference, size_t reference_sz)
100 unsigned char *key = NULL;
102 if (reference_sz != sizeof(key))
105 key = *(unsigned char **)reference;
109 /* detach the reference */
110 *(unsigned char **)reference = NULL;
115 static void *fake_rsa_gen_init(void *provctx, int selection,
116 const OSSL_PARAM params[])
118 unsigned char *gctx = NULL;
120 if (!TEST_ptr(gctx = OPENSSL_malloc(1)))
128 static void *fake_rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
130 unsigned char *gctx = genctx;
131 static const unsigned char inited[] = { 1 };
132 unsigned char *keydata;
135 || !TEST_mem_eq(gctx, sizeof(*gctx), inited, sizeof(inited)))
138 if (!TEST_ptr(keydata = fake_rsa_keymgmt_new(NULL)))
145 static void fake_rsa_gen_cleanup(void *genctx)
147 OPENSSL_free(genctx);
150 static const OSSL_DISPATCH fake_rsa_keymgmt_funcs[] = {
151 { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))fake_rsa_keymgmt_new },
152 { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))fake_rsa_keymgmt_free} ,
153 { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))fake_rsa_keymgmt_has },
154 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
155 (void (*)(void))fake_rsa_keymgmt_query },
156 { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))fake_rsa_keymgmt_import },
157 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES,
158 (void (*)(void))fake_rsa_keymgmt_imptypes },
159 { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))fake_rsa_keymgmt_load },
160 { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))fake_rsa_gen_init },
161 { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))fake_rsa_gen },
162 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))fake_rsa_gen_cleanup },
166 static const OSSL_ALGORITHM fake_rsa_keymgmt_algs[] = {
167 { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_keymgmt_funcs, "Fake RSA Key Management" },
168 { NULL, NULL, NULL, NULL }
171 static OSSL_FUNC_signature_newctx_fn fake_rsa_sig_newctx;
172 static OSSL_FUNC_signature_freectx_fn fake_rsa_sig_freectx;
173 static OSSL_FUNC_signature_sign_init_fn fake_rsa_sig_sign_init;
174 static OSSL_FUNC_signature_sign_fn fake_rsa_sig_sign;
176 static void *fake_rsa_sig_newctx(void *provctx, const char *propq)
178 unsigned char *sigctx = OPENSSL_zalloc(1);
185 static void fake_rsa_sig_freectx(void *sigctx)
187 OPENSSL_free(sigctx);
190 static int fake_rsa_sig_sign_init(void *ctx, void *provkey,
191 const OSSL_PARAM params[])
193 unsigned char *sigctx = ctx;
194 unsigned char *keydata = provkey;
196 /* we must have a ctx */
197 if (!TEST_ptr(sigctx))
200 /* we must have some initialized key */
201 if (!TEST_ptr(keydata) || !TEST_int_gt(keydata[0], 0))
204 /* record that sign init was called */
209 static int fake_rsa_sig_sign(void *ctx, unsigned char *sig,
210 size_t *siglen, size_t sigsize,
211 const unsigned char *tbs, size_t tbslen)
213 unsigned char *sigctx = ctx;
215 /* we must have a ctx and init was called upon it */
216 if (!TEST_ptr(sigctx) || !TEST_int_eq(*sigctx, 1))
220 /* record that the real sign operation was called */
222 if (!TEST_int_ge(sigsize, *siglen))
225 /* produce a fake signature */
226 memset(sig, 'a', *siglen);
232 static const OSSL_DISPATCH fake_rsa_sig_funcs[] = {
233 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))fake_rsa_sig_newctx },
234 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))fake_rsa_sig_freectx },
235 { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))fake_rsa_sig_sign_init },
236 { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))fake_rsa_sig_sign },
240 static const OSSL_ALGORITHM fake_rsa_sig_algs[] = {
241 { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_sig_funcs, "Fake RSA Signature" },
242 { NULL, NULL, NULL, NULL }
245 static OSSL_FUNC_store_open_fn fake_rsa_st_open;
246 static OSSL_FUNC_store_settable_ctx_params_fn fake_rsa_st_settable_ctx_params;
247 static OSSL_FUNC_store_set_ctx_params_fn fake_rsa_st_set_ctx_params;
248 static OSSL_FUNC_store_load_fn fake_rsa_st_load;
249 static OSSL_FUNC_store_eof_fn fake_rsa_st_eof;
250 static OSSL_FUNC_store_close_fn fake_rsa_st_close;
252 static const char fake_rsa_scheme[] = "fake_rsa:";
254 static void *fake_rsa_st_open(void *provctx, const char *uri)
256 unsigned char *storectx = NULL;
258 /* First check whether the uri is ours */
259 if (strncmp(uri, fake_rsa_scheme, sizeof(fake_rsa_scheme) - 1) != 0)
262 storectx = OPENSSL_zalloc(1);
263 if (!TEST_ptr(storectx))
266 TEST_info("fake_rsa_open called");
271 static const OSSL_PARAM *fake_rsa_st_settable_ctx_params(void *provctx)
273 static const OSSL_PARAM known_settable_ctx_params[] = {
276 return known_settable_ctx_params;
279 static int fake_rsa_st_set_ctx_params(void *loaderctx,
280 const OSSL_PARAM params[])
285 static int fake_rsa_st_load(void *loaderctx,
286 OSSL_CALLBACK *object_cb, void *object_cbarg,
287 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
289 unsigned char *storectx = loaderctx;
290 OSSL_PARAM params[4];
291 int object_type = OSSL_OBJECT_PKEY;
297 /* Construct a new key using our keymgmt functions */
298 if (!TEST_ptr(key = fake_rsa_keymgmt_new(NULL)))
300 if (!TEST_int_gt(fake_rsa_keymgmt_import(key, 0, NULL), 0))
303 OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
305 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
307 /* The address of the key becomes the octet string */
309 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
311 params[3] = OSSL_PARAM_construct_end();
312 rv = object_cb(params, object_cbarg);
317 TEST_info("fake_rsa_load() called in error state");
321 TEST_info("fake_rsa_load() called in eof state");
325 TEST_info("fake_rsa_load called - rv: %d", rv);
328 fake_rsa_keymgmt_free(key);
334 static int fake_rsa_st_eof(void *loaderctx)
336 unsigned char *storectx = loaderctx;
338 /* just one key for now in the fake_rsa store */
339 return *storectx != 0;
342 static int fake_rsa_st_close(void *loaderctx)
344 OPENSSL_free(loaderctx);
348 static const OSSL_DISPATCH fake_rsa_store_funcs[] = {
349 { OSSL_FUNC_STORE_OPEN, (void (*)(void))fake_rsa_st_open },
350 { OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS,
351 (void (*)(void))fake_rsa_st_settable_ctx_params },
352 { OSSL_FUNC_STORE_SET_CTX_PARAMS, (void (*)(void))fake_rsa_st_set_ctx_params },
353 { OSSL_FUNC_STORE_LOAD, (void (*)(void))fake_rsa_st_load },
354 { OSSL_FUNC_STORE_EOF, (void (*)(void))fake_rsa_st_eof },
355 { OSSL_FUNC_STORE_CLOSE, (void (*)(void))fake_rsa_st_close },
359 static const OSSL_ALGORITHM fake_rsa_store_algs[] = {
360 { "fake_rsa", "provider=fake-rsa", fake_rsa_store_funcs },
364 static const OSSL_ALGORITHM *fake_rsa_query(void *provctx,
369 switch (operation_id) {
370 case OSSL_OP_SIGNATURE:
371 return fake_rsa_sig_algs;
373 case OSSL_OP_KEYMGMT:
374 return fake_rsa_keymgmt_algs;
377 return fake_rsa_store_algs;
382 /* Functions we provide to the core */
383 static const OSSL_DISPATCH fake_rsa_method[] = {
384 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
385 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fake_rsa_query },
389 static int fake_rsa_provider_init(const OSSL_CORE_HANDLE *handle,
390 const OSSL_DISPATCH *in,
391 const OSSL_DISPATCH **out, void **provctx)
393 if (!TEST_ptr(*provctx = OSSL_LIB_CTX_new()))
395 *out = fake_rsa_method;
399 OSSL_PROVIDER *fake_rsa_start(OSSL_LIB_CTX *libctx)
403 if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "fake-rsa",
404 fake_rsa_provider_init))
405 || !TEST_ptr(p = OSSL_PROVIDER_try_load(libctx, "fake-rsa", 1)))
411 void fake_rsa_finish(OSSL_PROVIDER *p)
413 OSSL_PROVIDER_unload(p);