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 const OSSL_DISPATCH fake_rsa_keymgmt_funcs
[] = {
97 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))fake_rsa_keymgmt_new
},
98 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))fake_rsa_keymgmt_free
} ,
99 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))fake_rsa_keymgmt_has
},
100 { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME
,
101 (void (*)(void))fake_rsa_keymgmt_query
},
102 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))fake_rsa_keymgmt_import
},
103 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
,
104 (void (*)(void))fake_rsa_keymgmt_imptypes
},
108 static const OSSL_ALGORITHM fake_rsa_keymgmt_algs
[] = {
109 { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_keymgmt_funcs
, "Fake RSA Key Management" },
110 { NULL
, NULL
, NULL
, NULL
}
113 static OSSL_FUNC_signature_newctx_fn fake_rsa_sig_newctx
;
114 static OSSL_FUNC_signature_freectx_fn fake_rsa_sig_freectx
;
115 static OSSL_FUNC_signature_sign_init_fn fake_rsa_sig_sign_init
;
116 static OSSL_FUNC_signature_sign_fn fake_rsa_sig_sign
;
118 static void *fake_rsa_sig_newctx(void *provctx
, const char *propq
)
120 unsigned char *sigctx
= OPENSSL_zalloc(1);
127 static void fake_rsa_sig_freectx(void *sigctx
)
129 OPENSSL_free(sigctx
);
132 static int fake_rsa_sig_sign_init(void *ctx
, void *provkey
,
133 const OSSL_PARAM params
[])
135 unsigned char *sigctx
= ctx
;
136 unsigned char *keydata
= provkey
;
138 /* we must have a ctx */
139 if (!TEST_ptr(sigctx
))
142 /* we must have some initialized key */
143 if (!TEST_ptr(keydata
) || !TEST_int_gt(keydata
[0], 0))
146 /* record that sign init was called */
151 static int fake_rsa_sig_sign(void *ctx
, unsigned char *sig
,
152 size_t *siglen
, size_t sigsize
,
153 const unsigned char *tbs
, size_t tbslen
)
155 unsigned char *sigctx
= ctx
;
157 /* we must have a ctx and init was called upon it */
158 if (!TEST_ptr(sigctx
) || !TEST_int_eq(*sigctx
, 1))
162 /* record that the real sign operation was called */
164 if (!TEST_int_ge(sigsize
, *siglen
))
167 /* produce a fake signature */
168 memset(sig
, 'a', *siglen
);
174 static const OSSL_DISPATCH fake_rsa_sig_funcs
[] = {
175 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))fake_rsa_sig_newctx
},
176 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))fake_rsa_sig_freectx
},
177 { OSSL_FUNC_SIGNATURE_SIGN_INIT
, (void (*)(void))fake_rsa_sig_sign_init
},
178 { OSSL_FUNC_SIGNATURE_SIGN
, (void (*)(void))fake_rsa_sig_sign
},
182 static const OSSL_ALGORITHM fake_rsa_sig_algs
[] = {
183 { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_sig_funcs
, "Fake RSA Signature" },
184 { NULL
, NULL
, NULL
, NULL
}
187 static const OSSL_ALGORITHM
*fake_rsa_query(void *provctx
,
192 switch (operation_id
) {
193 case OSSL_OP_SIGNATURE
:
194 return fake_rsa_sig_algs
;
196 case OSSL_OP_KEYMGMT
:
197 return fake_rsa_keymgmt_algs
;
202 /* Functions we provide to the core */
203 static const OSSL_DISPATCH fake_rsa_method
[] = {
204 { OSSL_FUNC_PROVIDER_TEARDOWN
, (void (*)(void))OSSL_LIB_CTX_free
},
205 { OSSL_FUNC_PROVIDER_QUERY_OPERATION
, (void (*)(void))fake_rsa_query
},
209 static int fake_rsa_provider_init(const OSSL_CORE_HANDLE
*handle
,
210 const OSSL_DISPATCH
*in
,
211 const OSSL_DISPATCH
**out
, void **provctx
)
213 if (!TEST_ptr(*provctx
= OSSL_LIB_CTX_new()))
215 *out
= fake_rsa_method
;
219 OSSL_PROVIDER
*fake_rsa_start(OSSL_LIB_CTX
*libctx
)
223 if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx
, "fake-rsa",
224 fake_rsa_provider_init
))
225 || !TEST_ptr(p
= OSSL_PROVIDER_try_load(libctx
, "fake-rsa", 1)))
231 void fake_rsa_finish(OSSL_PROVIDER
*p
)
233 OSSL_PROVIDER_unload(p
);