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 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 },
|
---|
105 | { 0, NULL }
|
---|
106 | };
|
---|
107 |
|
---|
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 }
|
---|
111 | };
|
---|
112 |
|
---|
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;
|
---|
117 |
|
---|
118 | static void *fake_rsa_sig_newctx(void *provctx, const char *propq)
|
---|
119 | {
|
---|
120 | unsigned char *sigctx = OPENSSL_zalloc(1);
|
---|
121 |
|
---|
122 | TEST_ptr(sigctx);
|
---|
123 |
|
---|
124 | return sigctx;
|
---|
125 | }
|
---|
126 |
|
---|
127 | static void fake_rsa_sig_freectx(void *sigctx)
|
---|
128 | {
|
---|
129 | OPENSSL_free(sigctx);
|
---|
130 | }
|
---|
131 |
|
---|
132 | static int fake_rsa_sig_sign_init(void *ctx, void *provkey,
|
---|
133 | const OSSL_PARAM params[])
|
---|
134 | {
|
---|
135 | unsigned char *sigctx = ctx;
|
---|
136 | unsigned char *keydata = provkey;
|
---|
137 |
|
---|
138 | /* we must have a ctx */
|
---|
139 | if (!TEST_ptr(sigctx))
|
---|
140 | return 0;
|
---|
141 |
|
---|
142 | /* we must have some initialized key */
|
---|
143 | if (!TEST_ptr(keydata) || !TEST_int_gt(keydata[0], 0))
|
---|
144 | return 0;
|
---|
145 |
|
---|
146 | /* record that sign init was called */
|
---|
147 | *sigctx = 1;
|
---|
148 | return 1;
|
---|
149 | }
|
---|
150 |
|
---|
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)
|
---|
154 | {
|
---|
155 | unsigned char *sigctx = ctx;
|
---|
156 |
|
---|
157 | /* we must have a ctx and init was called upon it */
|
---|
158 | if (!TEST_ptr(sigctx) || !TEST_int_eq(*sigctx, 1))
|
---|
159 | return 0;
|
---|
160 |
|
---|
161 | *siglen = 256;
|
---|
162 | /* record that the real sign operation was called */
|
---|
163 | if (sig != NULL) {
|
---|
164 | if (!TEST_int_ge(sigsize, *siglen))
|
---|
165 | return 0;
|
---|
166 | *sigctx = 2;
|
---|
167 | /* produce a fake signature */
|
---|
168 | memset(sig, 'a', *siglen);
|
---|
169 | }
|
---|
170 |
|
---|
171 | return 1;
|
---|
172 | }
|
---|
173 |
|
---|
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 },
|
---|
179 | { 0, NULL }
|
---|
180 | };
|
---|
181 |
|
---|
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 }
|
---|
185 | };
|
---|
186 |
|
---|
187 | static const OSSL_ALGORITHM *fake_rsa_query(void *provctx,
|
---|
188 | int operation_id,
|
---|
189 | int *no_cache)
|
---|
190 | {
|
---|
191 | *no_cache = 0;
|
---|
192 | switch (operation_id) {
|
---|
193 | case OSSL_OP_SIGNATURE:
|
---|
194 | return fake_rsa_sig_algs;
|
---|
195 |
|
---|
196 | case OSSL_OP_KEYMGMT:
|
---|
197 | return fake_rsa_keymgmt_algs;
|
---|
198 | }
|
---|
199 | return NULL;
|
---|
200 | }
|
---|
201 |
|
---|
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 },
|
---|
206 | { 0, NULL }
|
---|
207 | };
|
---|
208 |
|
---|
209 | static int fake_rsa_provider_init(const OSSL_CORE_HANDLE *handle,
|
---|
210 | const OSSL_DISPATCH *in,
|
---|
211 | const OSSL_DISPATCH **out, void **provctx)
|
---|
212 | {
|
---|
213 | if (!TEST_ptr(*provctx = OSSL_LIB_CTX_new()))
|
---|
214 | return 0;
|
---|
215 | *out = fake_rsa_method;
|
---|
216 | return 1;
|
---|
217 | }
|
---|
218 |
|
---|
219 | OSSL_PROVIDER *fake_rsa_start(OSSL_LIB_CTX *libctx)
|
---|
220 | {
|
---|
221 | OSSL_PROVIDER *p;
|
---|
222 |
|
---|
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)))
|
---|
226 | return NULL;
|
---|
227 |
|
---|
228 | return p;
|
---|
229 | }
|
---|
230 |
|
---|
231 | void fake_rsa_finish(OSSL_PROVIDER *p)
|
---|
232 | {
|
---|
233 | OSSL_PROVIDER_unload(p);
|
---|
234 | }
|
---|