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/rand.h>
14 #include <openssl/provider.h>
16 #include "fake_rsaprov.h"
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;
25 static int has_selection;
26 static int imptypes_selection;
29 static void *fake_rsa_keymgmt_new(void *provctx)
31 unsigned char *keydata = OPENSSL_zalloc(1);
35 /* clear test globals */
37 imptypes_selection = 0;
43 static void fake_rsa_keymgmt_free(void *keydata)
45 OPENSSL_free(keydata);
48 static int fake_rsa_keymgmt_has(const void *key, int selection)
50 /* record global for checking */
51 has_selection = selection;
57 static const char *fake_rsa_keymgmt_query(int id)
59 /* record global for checking */
65 static int fake_rsa_keymgmt_import(void *keydata, int selection,
68 unsigned char *fake_rsa_key = keydata;
70 /* key was imported */
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),
88 static const OSSL_PARAM *fake_rsa_keymgmt_imptypes(int selection)
90 /* record global for checking */
91 imptypes_selection = selection;
93 return fake_rsa_import_key_types;
96 static void *fake_rsa_gen_init(void *provctx, int selection,
97 const OSSL_PARAM params[])
99 unsigned char *gctx = NULL;
101 if (!TEST_ptr(gctx = OPENSSL_malloc(1)))
109 static void *fake_rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
111 unsigned char *gctx = genctx;
112 static const unsigned char inited[] = { 1 };
113 unsigned char *keydata;
116 || !TEST_mem_eq(gctx, sizeof(*gctx), inited, sizeof(inited)))
119 if (!TEST_ptr(keydata = fake_rsa_keymgmt_new(NULL)))
126 static void fake_rsa_gen_cleanup(void *genctx)
128 OPENSSL_free(genctx);
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 },
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 }
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;
156 static void *fake_rsa_sig_newctx(void *provctx, const char *propq)
158 unsigned char *sigctx = OPENSSL_zalloc(1);
165 static void fake_rsa_sig_freectx(void *sigctx)
167 OPENSSL_free(sigctx);
170 static int fake_rsa_sig_sign_init(void *ctx, void *provkey,
171 const OSSL_PARAM params[])
173 unsigned char *sigctx = ctx;
174 unsigned char *keydata = provkey;
176 /* we must have a ctx */
177 if (!TEST_ptr(sigctx))
180 /* we must have some initialized key */
181 if (!TEST_ptr(keydata) || !TEST_int_gt(keydata[0], 0))
184 /* record that sign init was called */
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)
193 unsigned char *sigctx = ctx;
195 /* we must have a ctx and init was called upon it */
196 if (!TEST_ptr(sigctx) || !TEST_int_eq(*sigctx, 1))
200 /* record that the real sign operation was called */
202 if (!TEST_int_ge(sigsize, *siglen))
205 /* produce a fake signature */
206 memset(sig, 'a', *siglen);
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 },
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 }
225 static const OSSL_ALGORITHM *fake_rsa_query(void *provctx,
230 switch (operation_id) {
231 case OSSL_OP_SIGNATURE:
232 return fake_rsa_sig_algs;
234 case OSSL_OP_KEYMGMT:
235 return fake_rsa_keymgmt_algs;
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 },
247 static int fake_rsa_provider_init(const OSSL_CORE_HANDLE *handle,
248 const OSSL_DISPATCH *in,
249 const OSSL_DISPATCH **out, void **provctx)
251 if (!TEST_ptr(*provctx = OSSL_LIB_CTX_new()))
253 *out = fake_rsa_method;
257 OSSL_PROVIDER *fake_rsa_start(OSSL_LIB_CTX *libctx)
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)))
269 void fake_rsa_finish(OSSL_PROVIDER *p)
271 OSSL_PROVIDER_unload(p);