1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License");
5e1051a39Sopenharmony_ci * you may not use this file except in compliance with the License.
6e1051a39Sopenharmony_ci * You may obtain a copy of the License at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci * or in the file LICENSE in the source distribution.
9e1051a39Sopenharmony_ci */
10e1051a39Sopenharmony_ci
11e1051a39Sopenharmony_ci#include <string.h>
12e1051a39Sopenharmony_ci#include <openssl/core_names.h>
13e1051a39Sopenharmony_ci#include <openssl/core_object.h>
14e1051a39Sopenharmony_ci#include <openssl/rand.h>
15e1051a39Sopenharmony_ci#include <openssl/provider.h>
16e1051a39Sopenharmony_ci#include "testutil.h"
17e1051a39Sopenharmony_ci#include "fake_rsaprov.h"
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_cistatic OSSL_FUNC_keymgmt_new_fn fake_rsa_keymgmt_new;
20e1051a39Sopenharmony_cistatic OSSL_FUNC_keymgmt_free_fn fake_rsa_keymgmt_free;
21e1051a39Sopenharmony_cistatic OSSL_FUNC_keymgmt_has_fn fake_rsa_keymgmt_has;
22e1051a39Sopenharmony_cistatic OSSL_FUNC_keymgmt_query_operation_name_fn fake_rsa_keymgmt_query;
23e1051a39Sopenharmony_cistatic OSSL_FUNC_keymgmt_import_fn fake_rsa_keymgmt_import;
24e1051a39Sopenharmony_cistatic OSSL_FUNC_keymgmt_import_types_fn fake_rsa_keymgmt_imptypes;
25e1051a39Sopenharmony_cistatic OSSL_FUNC_keymgmt_export_fn fake_rsa_keymgmt_export;
26e1051a39Sopenharmony_cistatic OSSL_FUNC_keymgmt_export_types_fn fake_rsa_keymgmt_exptypes;
27e1051a39Sopenharmony_cistatic OSSL_FUNC_keymgmt_load_fn fake_rsa_keymgmt_load;
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_cistatic int has_selection;
30e1051a39Sopenharmony_cistatic int imptypes_selection;
31e1051a39Sopenharmony_cistatic int exptypes_selection;
32e1051a39Sopenharmony_cistatic int query_id;
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_cistruct fake_rsa_keydata {
35e1051a39Sopenharmony_ci    int selection;
36e1051a39Sopenharmony_ci    int status;
37e1051a39Sopenharmony_ci};
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_cistatic void *fake_rsa_keymgmt_new(void *provctx)
40e1051a39Sopenharmony_ci{
41e1051a39Sopenharmony_ci    struct fake_rsa_keydata *key;
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci    if (!TEST_ptr(key = OPENSSL_zalloc(sizeof(struct fake_rsa_keydata))))
44e1051a39Sopenharmony_ci        return NULL;
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ci    /* clear test globals */
47e1051a39Sopenharmony_ci    has_selection = 0;
48e1051a39Sopenharmony_ci    imptypes_selection = 0;
49e1051a39Sopenharmony_ci    exptypes_selection = 0;
50e1051a39Sopenharmony_ci    query_id = 0;
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ci    return key;
53e1051a39Sopenharmony_ci}
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_cistatic void fake_rsa_keymgmt_free(void *keydata)
56e1051a39Sopenharmony_ci{
57e1051a39Sopenharmony_ci    OPENSSL_free(keydata);
58e1051a39Sopenharmony_ci}
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_cistatic int fake_rsa_keymgmt_has(const void *key, int selection)
61e1051a39Sopenharmony_ci{
62e1051a39Sopenharmony_ci    /* record global for checking */
63e1051a39Sopenharmony_ci    has_selection = selection;
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci    return 1;
66e1051a39Sopenharmony_ci}
67e1051a39Sopenharmony_ci
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_cistatic const char *fake_rsa_keymgmt_query(int id)
70e1051a39Sopenharmony_ci{
71e1051a39Sopenharmony_ci    /* record global for checking */
72e1051a39Sopenharmony_ci    query_id = id;
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ci    return "RSA";
75e1051a39Sopenharmony_ci}
76e1051a39Sopenharmony_ci
77e1051a39Sopenharmony_cistatic int fake_rsa_keymgmt_import(void *keydata, int selection,
78e1051a39Sopenharmony_ci                                   const OSSL_PARAM *p)
79e1051a39Sopenharmony_ci{
80e1051a39Sopenharmony_ci    struct fake_rsa_keydata *fake_rsa_key = keydata;
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ci    /* key was imported */
83e1051a39Sopenharmony_ci    fake_rsa_key->status = 1;
84e1051a39Sopenharmony_ci
85e1051a39Sopenharmony_ci    return 1;
86e1051a39Sopenharmony_ci}
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_cistatic unsigned char fake_rsa_n[] =
89e1051a39Sopenharmony_ci   "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F"
90e1051a39Sopenharmony_ci   "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5"
91e1051a39Sopenharmony_ci   "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93"
92e1051a39Sopenharmony_ci   "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1"
93e1051a39Sopenharmony_ci   "\xF5";
94e1051a39Sopenharmony_ci
95e1051a39Sopenharmony_cistatic unsigned char fake_rsa_e[] = "\x11";
96e1051a39Sopenharmony_ci
97e1051a39Sopenharmony_cistatic unsigned char fake_rsa_d[] =
98e1051a39Sopenharmony_ci    "\x0A\x03\x37\x48\x62\x64\x87\x69\x5F\x5F\x30\xBC\x38\xB9\x8B\x44"
99e1051a39Sopenharmony_ci    "\xC2\xCD\x2D\xFF\x43\x40\x98\xCD\x20\xD8\xA1\x38\xD0\x90\xBF\x64"
100e1051a39Sopenharmony_ci    "\x79\x7C\x3F\xA7\xA2\xCD\xCB\x3C\xD1\xE0\xBD\xBA\x26\x54\xB4\xF9"
101e1051a39Sopenharmony_ci    "\xDF\x8E\x8A\xE5\x9D\x73\x3D\x9F\x33\xB3\x01\x62\x4A\xFD\x1D\x51";
102e1051a39Sopenharmony_ci
103e1051a39Sopenharmony_cistatic unsigned char fake_rsa_p[] =
104e1051a39Sopenharmony_ci    "\x00\xD8\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5"
105e1051a39Sopenharmony_ci    "\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x12"
106e1051a39Sopenharmony_ci    "\x0D";
107e1051a39Sopenharmony_ci
108e1051a39Sopenharmony_cistatic unsigned char fake_rsa_q[] =
109e1051a39Sopenharmony_ci    "\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9"
110e1051a39Sopenharmony_ci    "\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D"
111e1051a39Sopenharmony_ci    "\x89";
112e1051a39Sopenharmony_ci
113e1051a39Sopenharmony_cistatic unsigned char fake_rsa_dmp1[] =
114e1051a39Sopenharmony_ci    "\x59\x0B\x95\x72\xA2\xC2\xA9\xC4\x06\x05\x9D\xC2\xAB\x2F\x1D\xAF"
115e1051a39Sopenharmony_ci    "\xEB\x7E\x8B\x4F\x10\xA7\x54\x9E\x8E\xED\xF5\xB4\xFC\xE0\x9E\x05";
116e1051a39Sopenharmony_ci
117e1051a39Sopenharmony_cistatic unsigned char fake_rsa_dmq1[] =
118e1051a39Sopenharmony_ci    "\x00\x8E\x3C\x05\x21\xFE\x15\xE0\xEA\x06\xA3\x6F\xF0\xF1\x0C\x99"
119e1051a39Sopenharmony_ci    "\x52\xC3\x5B\x7A\x75\x14\xFD\x32\x38\xB8\x0A\xAD\x52\x98\x62\x8D"
120e1051a39Sopenharmony_ci    "\x51";
121e1051a39Sopenharmony_ci
122e1051a39Sopenharmony_cistatic unsigned char fake_rsa_iqmp[] =
123e1051a39Sopenharmony_ci    "\x36\x3F\xF7\x18\x9D\xA8\xE9\x0B\x1D\x34\x1F\x71\xD0\x9B\x76\xA8"
124e1051a39Sopenharmony_ci    "\xA9\x43\xE1\x1D\x10\xB2\x4D\x24\x9F\x2D\xEA\xFE\xF8\x0C\x18\x26";
125e1051a39Sopenharmony_ci
126e1051a39Sopenharmony_ciOSSL_PARAM *fake_rsa_key_params(int priv)
127e1051a39Sopenharmony_ci{
128e1051a39Sopenharmony_ci    if (priv) {
129e1051a39Sopenharmony_ci        OSSL_PARAM params[] = {
130e1051a39Sopenharmony_ci            OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, fake_rsa_n,
131e1051a39Sopenharmony_ci                          sizeof(fake_rsa_n) -1),
132e1051a39Sopenharmony_ci            OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, fake_rsa_e,
133e1051a39Sopenharmony_ci                          sizeof(fake_rsa_e) -1),
134e1051a39Sopenharmony_ci            OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, fake_rsa_d,
135e1051a39Sopenharmony_ci                          sizeof(fake_rsa_d) -1),
136e1051a39Sopenharmony_ci            OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, fake_rsa_p,
137e1051a39Sopenharmony_ci                          sizeof(fake_rsa_p) -1),
138e1051a39Sopenharmony_ci            OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, fake_rsa_q,
139e1051a39Sopenharmony_ci                          sizeof(fake_rsa_q) -1),
140e1051a39Sopenharmony_ci            OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, fake_rsa_dmp1,
141e1051a39Sopenharmony_ci                          sizeof(fake_rsa_dmp1) -1),
142e1051a39Sopenharmony_ci            OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, fake_rsa_dmq1,
143e1051a39Sopenharmony_ci                          sizeof(fake_rsa_dmq1) -1),
144e1051a39Sopenharmony_ci            OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, fake_rsa_iqmp,
145e1051a39Sopenharmony_ci                          sizeof(fake_rsa_iqmp) -1),
146e1051a39Sopenharmony_ci            OSSL_PARAM_END
147e1051a39Sopenharmony_ci        };
148e1051a39Sopenharmony_ci        return OSSL_PARAM_dup(params);
149e1051a39Sopenharmony_ci    } else {
150e1051a39Sopenharmony_ci        OSSL_PARAM params[] = {
151e1051a39Sopenharmony_ci            OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, fake_rsa_n,
152e1051a39Sopenharmony_ci                          sizeof(fake_rsa_n) -1),
153e1051a39Sopenharmony_ci            OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, fake_rsa_e,
154e1051a39Sopenharmony_ci                          sizeof(fake_rsa_e) -1),
155e1051a39Sopenharmony_ci            OSSL_PARAM_END
156e1051a39Sopenharmony_ci        };
157e1051a39Sopenharmony_ci        return OSSL_PARAM_dup(params);
158e1051a39Sopenharmony_ci    }
159e1051a39Sopenharmony_ci}
160e1051a39Sopenharmony_ci
161e1051a39Sopenharmony_cistatic int fake_rsa_keymgmt_export(void *keydata, int selection,
162e1051a39Sopenharmony_ci                                   OSSL_CALLBACK *param_callback, void *cbarg)
163e1051a39Sopenharmony_ci{
164e1051a39Sopenharmony_ci    OSSL_PARAM *params = NULL;
165e1051a39Sopenharmony_ci    int ret;
166e1051a39Sopenharmony_ci
167e1051a39Sopenharmony_ci    if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
168e1051a39Sopenharmony_ci        return 0;
169e1051a39Sopenharmony_ci
170e1051a39Sopenharmony_ci    if (!TEST_ptr(params = fake_rsa_key_params(0)))
171e1051a39Sopenharmony_ci        return 0;
172e1051a39Sopenharmony_ci
173e1051a39Sopenharmony_ci    ret = param_callback(params, cbarg);
174e1051a39Sopenharmony_ci    OSSL_PARAM_free(params);
175e1051a39Sopenharmony_ci    return ret;
176e1051a39Sopenharmony_ci}
177e1051a39Sopenharmony_ci
178e1051a39Sopenharmony_cistatic const OSSL_PARAM fake_rsa_import_key_types[] = {
179e1051a39Sopenharmony_ci    OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
180e1051a39Sopenharmony_ci    OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
181e1051a39Sopenharmony_ci    OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
182e1051a39Sopenharmony_ci    OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0),
183e1051a39Sopenharmony_ci    OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0),
184e1051a39Sopenharmony_ci    OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0),
185e1051a39Sopenharmony_ci    OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0),
186e1051a39Sopenharmony_ci    OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),
187e1051a39Sopenharmony_ci    OSSL_PARAM_END
188e1051a39Sopenharmony_ci};
189e1051a39Sopenharmony_ci
190e1051a39Sopenharmony_cistatic const OSSL_PARAM *fake_rsa_keymgmt_imptypes(int selection)
191e1051a39Sopenharmony_ci{
192e1051a39Sopenharmony_ci    /* record global for checking */
193e1051a39Sopenharmony_ci    imptypes_selection = selection;
194e1051a39Sopenharmony_ci
195e1051a39Sopenharmony_ci    return fake_rsa_import_key_types;
196e1051a39Sopenharmony_ci}
197e1051a39Sopenharmony_ci
198e1051a39Sopenharmony_cistatic const OSSL_PARAM fake_rsa_export_key_types[] = {
199e1051a39Sopenharmony_ci    OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
200e1051a39Sopenharmony_ci    OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
201e1051a39Sopenharmony_ci    OSSL_PARAM_END
202e1051a39Sopenharmony_ci};
203e1051a39Sopenharmony_ci
204e1051a39Sopenharmony_cistatic const OSSL_PARAM *fake_rsa_keymgmt_exptypes(int selection)
205e1051a39Sopenharmony_ci{
206e1051a39Sopenharmony_ci    /* record global for checking */
207e1051a39Sopenharmony_ci    exptypes_selection = selection;
208e1051a39Sopenharmony_ci
209e1051a39Sopenharmony_ci    return fake_rsa_export_key_types;
210e1051a39Sopenharmony_ci}
211e1051a39Sopenharmony_ci
212e1051a39Sopenharmony_cistatic void *fake_rsa_keymgmt_load(const void *reference, size_t reference_sz)
213e1051a39Sopenharmony_ci{
214e1051a39Sopenharmony_ci    struct fake_rsa_keydata *key = NULL;
215e1051a39Sopenharmony_ci
216e1051a39Sopenharmony_ci    if (reference_sz != sizeof(*key))
217e1051a39Sopenharmony_ci        return NULL;
218e1051a39Sopenharmony_ci
219e1051a39Sopenharmony_ci    key = *(struct fake_rsa_keydata **)reference;
220e1051a39Sopenharmony_ci    if (key->status != 1)
221e1051a39Sopenharmony_ci        return NULL;
222e1051a39Sopenharmony_ci
223e1051a39Sopenharmony_ci    /* detach the reference */
224e1051a39Sopenharmony_ci    *(struct fake_rsa_keydata  **)reference = NULL;
225e1051a39Sopenharmony_ci
226e1051a39Sopenharmony_ci    return key;
227e1051a39Sopenharmony_ci}
228e1051a39Sopenharmony_ci
229e1051a39Sopenharmony_cistatic void *fake_rsa_gen_init(void *provctx, int selection,
230e1051a39Sopenharmony_ci                               const OSSL_PARAM params[])
231e1051a39Sopenharmony_ci{
232e1051a39Sopenharmony_ci    unsigned char *gctx = NULL;
233e1051a39Sopenharmony_ci
234e1051a39Sopenharmony_ci    if (!TEST_ptr(gctx = OPENSSL_malloc(1)))
235e1051a39Sopenharmony_ci        return NULL;
236e1051a39Sopenharmony_ci
237e1051a39Sopenharmony_ci    *gctx = 1;
238e1051a39Sopenharmony_ci
239e1051a39Sopenharmony_ci    return gctx;
240e1051a39Sopenharmony_ci}
241e1051a39Sopenharmony_ci
242e1051a39Sopenharmony_cistatic void *fake_rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
243e1051a39Sopenharmony_ci{
244e1051a39Sopenharmony_ci    unsigned char *gctx = genctx;
245e1051a39Sopenharmony_ci    static const unsigned char inited[] = { 1 };
246e1051a39Sopenharmony_ci    struct fake_rsa_keydata *keydata;
247e1051a39Sopenharmony_ci
248e1051a39Sopenharmony_ci    if (!TEST_ptr(gctx)
249e1051a39Sopenharmony_ci        || !TEST_mem_eq(gctx, sizeof(*gctx), inited, sizeof(inited)))
250e1051a39Sopenharmony_ci        return NULL;
251e1051a39Sopenharmony_ci
252e1051a39Sopenharmony_ci    if (!TEST_ptr(keydata = fake_rsa_keymgmt_new(NULL)))
253e1051a39Sopenharmony_ci        return NULL;
254e1051a39Sopenharmony_ci
255e1051a39Sopenharmony_ci    keydata->status = 2;
256e1051a39Sopenharmony_ci    return keydata;
257e1051a39Sopenharmony_ci}
258e1051a39Sopenharmony_ci
259e1051a39Sopenharmony_cistatic void fake_rsa_gen_cleanup(void *genctx)
260e1051a39Sopenharmony_ci{
261e1051a39Sopenharmony_ci   OPENSSL_free(genctx);
262e1051a39Sopenharmony_ci}
263e1051a39Sopenharmony_ci
264e1051a39Sopenharmony_cistatic const OSSL_DISPATCH fake_rsa_keymgmt_funcs[] = {
265e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))fake_rsa_keymgmt_new },
266e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))fake_rsa_keymgmt_free} ,
267e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))fake_rsa_keymgmt_has },
268e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
269e1051a39Sopenharmony_ci        (void (*)(void))fake_rsa_keymgmt_query },
270e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))fake_rsa_keymgmt_import },
271e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_IMPORT_TYPES,
272e1051a39Sopenharmony_ci        (void (*)(void))fake_rsa_keymgmt_imptypes },
273e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))fake_rsa_keymgmt_export },
274e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_EXPORT_TYPES,
275e1051a39Sopenharmony_ci        (void (*)(void))fake_rsa_keymgmt_exptypes },
276e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))fake_rsa_keymgmt_load },
277e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))fake_rsa_gen_init },
278e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))fake_rsa_gen },
279e1051a39Sopenharmony_ci    { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))fake_rsa_gen_cleanup },
280e1051a39Sopenharmony_ci    { 0, NULL }
281e1051a39Sopenharmony_ci};
282e1051a39Sopenharmony_ci
283e1051a39Sopenharmony_cistatic const OSSL_ALGORITHM fake_rsa_keymgmt_algs[] = {
284e1051a39Sopenharmony_ci    { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_keymgmt_funcs, "Fake RSA Key Management" },
285e1051a39Sopenharmony_ci    { NULL, NULL, NULL, NULL }
286e1051a39Sopenharmony_ci};
287e1051a39Sopenharmony_ci
288e1051a39Sopenharmony_cistatic OSSL_FUNC_signature_newctx_fn fake_rsa_sig_newctx;
289e1051a39Sopenharmony_cistatic OSSL_FUNC_signature_freectx_fn fake_rsa_sig_freectx;
290e1051a39Sopenharmony_cistatic OSSL_FUNC_signature_sign_init_fn fake_rsa_sig_sign_init;
291e1051a39Sopenharmony_cistatic OSSL_FUNC_signature_sign_fn fake_rsa_sig_sign;
292e1051a39Sopenharmony_ci
293e1051a39Sopenharmony_cistatic void *fake_rsa_sig_newctx(void *provctx, const char *propq)
294e1051a39Sopenharmony_ci{
295e1051a39Sopenharmony_ci    unsigned char *sigctx = OPENSSL_zalloc(1);
296e1051a39Sopenharmony_ci
297e1051a39Sopenharmony_ci    TEST_ptr(sigctx);
298e1051a39Sopenharmony_ci
299e1051a39Sopenharmony_ci    return sigctx;
300e1051a39Sopenharmony_ci}
301e1051a39Sopenharmony_ci
302e1051a39Sopenharmony_cistatic void fake_rsa_sig_freectx(void *sigctx)
303e1051a39Sopenharmony_ci{
304e1051a39Sopenharmony_ci    OPENSSL_free(sigctx);
305e1051a39Sopenharmony_ci}
306e1051a39Sopenharmony_ci
307e1051a39Sopenharmony_cistatic int fake_rsa_sig_sign_init(void *ctx, void *provkey,
308e1051a39Sopenharmony_ci                                  const OSSL_PARAM params[])
309e1051a39Sopenharmony_ci{
310e1051a39Sopenharmony_ci    unsigned char *sigctx = ctx;
311e1051a39Sopenharmony_ci    struct fake_rsa_keydata *keydata = provkey;
312e1051a39Sopenharmony_ci
313e1051a39Sopenharmony_ci    /* we must have a ctx */
314e1051a39Sopenharmony_ci    if (!TEST_ptr(sigctx))
315e1051a39Sopenharmony_ci        return 0;
316e1051a39Sopenharmony_ci
317e1051a39Sopenharmony_ci    /* we must have some initialized key */
318e1051a39Sopenharmony_ci    if (!TEST_ptr(keydata) || !TEST_int_gt(keydata->status, 0))
319e1051a39Sopenharmony_ci        return 0;
320e1051a39Sopenharmony_ci
321e1051a39Sopenharmony_ci    /* record that sign init was called */
322e1051a39Sopenharmony_ci    *sigctx = 1;
323e1051a39Sopenharmony_ci    return 1;
324e1051a39Sopenharmony_ci}
325e1051a39Sopenharmony_ci
326e1051a39Sopenharmony_cistatic int fake_rsa_sig_sign(void *ctx, unsigned char *sig,
327e1051a39Sopenharmony_ci                             size_t *siglen, size_t sigsize,
328e1051a39Sopenharmony_ci                             const unsigned char *tbs, size_t tbslen)
329e1051a39Sopenharmony_ci{
330e1051a39Sopenharmony_ci    unsigned char *sigctx = ctx;
331e1051a39Sopenharmony_ci
332e1051a39Sopenharmony_ci    /* we must have a ctx and init was called upon it */
333e1051a39Sopenharmony_ci    if (!TEST_ptr(sigctx) || !TEST_int_eq(*sigctx, 1))
334e1051a39Sopenharmony_ci        return 0;
335e1051a39Sopenharmony_ci
336e1051a39Sopenharmony_ci    *siglen = 256;
337e1051a39Sopenharmony_ci    /* record that the real sign operation was called */
338e1051a39Sopenharmony_ci    if (sig != NULL) {
339e1051a39Sopenharmony_ci        if (!TEST_int_ge(sigsize, *siglen))
340e1051a39Sopenharmony_ci            return 0;
341e1051a39Sopenharmony_ci        *sigctx = 2;
342e1051a39Sopenharmony_ci        /* produce a fake signature */
343e1051a39Sopenharmony_ci        memset(sig, 'a', *siglen);
344e1051a39Sopenharmony_ci    }
345e1051a39Sopenharmony_ci
346e1051a39Sopenharmony_ci    return 1;
347e1051a39Sopenharmony_ci}
348e1051a39Sopenharmony_ci
349e1051a39Sopenharmony_cistatic const OSSL_DISPATCH fake_rsa_sig_funcs[] = {
350e1051a39Sopenharmony_ci    { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))fake_rsa_sig_newctx },
351e1051a39Sopenharmony_ci    { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))fake_rsa_sig_freectx },
352e1051a39Sopenharmony_ci    { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))fake_rsa_sig_sign_init },
353e1051a39Sopenharmony_ci    { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))fake_rsa_sig_sign },
354e1051a39Sopenharmony_ci    { 0, NULL }
355e1051a39Sopenharmony_ci};
356e1051a39Sopenharmony_ci
357e1051a39Sopenharmony_cistatic const OSSL_ALGORITHM fake_rsa_sig_algs[] = {
358e1051a39Sopenharmony_ci    { "RSA:rsaEncryption", "provider=fake-rsa", fake_rsa_sig_funcs, "Fake RSA Signature" },
359e1051a39Sopenharmony_ci    { NULL, NULL, NULL, NULL }
360e1051a39Sopenharmony_ci};
361e1051a39Sopenharmony_ci
362e1051a39Sopenharmony_cistatic OSSL_FUNC_store_open_fn fake_rsa_st_open;
363e1051a39Sopenharmony_cistatic OSSL_FUNC_store_settable_ctx_params_fn fake_rsa_st_settable_ctx_params;
364e1051a39Sopenharmony_cistatic OSSL_FUNC_store_set_ctx_params_fn fake_rsa_st_set_ctx_params;
365e1051a39Sopenharmony_cistatic OSSL_FUNC_store_load_fn fake_rsa_st_load;
366e1051a39Sopenharmony_cistatic OSSL_FUNC_store_eof_fn fake_rsa_st_eof;
367e1051a39Sopenharmony_cistatic OSSL_FUNC_store_close_fn fake_rsa_st_close;
368e1051a39Sopenharmony_ci
369e1051a39Sopenharmony_cistatic const char fake_rsa_scheme[] = "fake_rsa:";
370e1051a39Sopenharmony_ci
371e1051a39Sopenharmony_cistatic void *fake_rsa_st_open(void *provctx, const char *uri)
372e1051a39Sopenharmony_ci{
373e1051a39Sopenharmony_ci    unsigned char *storectx = NULL;
374e1051a39Sopenharmony_ci
375e1051a39Sopenharmony_ci    /* First check whether the uri is ours */
376e1051a39Sopenharmony_ci    if (strncmp(uri, fake_rsa_scheme, sizeof(fake_rsa_scheme) - 1) != 0)
377e1051a39Sopenharmony_ci        return NULL;
378e1051a39Sopenharmony_ci
379e1051a39Sopenharmony_ci    storectx = OPENSSL_zalloc(1);
380e1051a39Sopenharmony_ci    if (!TEST_ptr(storectx))
381e1051a39Sopenharmony_ci        return NULL;
382e1051a39Sopenharmony_ci
383e1051a39Sopenharmony_ci    TEST_info("fake_rsa_open called");
384e1051a39Sopenharmony_ci
385e1051a39Sopenharmony_ci    return storectx;
386e1051a39Sopenharmony_ci}
387e1051a39Sopenharmony_ci
388e1051a39Sopenharmony_cistatic const OSSL_PARAM *fake_rsa_st_settable_ctx_params(void *provctx)
389e1051a39Sopenharmony_ci{
390e1051a39Sopenharmony_ci    static const OSSL_PARAM known_settable_ctx_params[] = {
391e1051a39Sopenharmony_ci        OSSL_PARAM_END
392e1051a39Sopenharmony_ci    };
393e1051a39Sopenharmony_ci    return known_settable_ctx_params;
394e1051a39Sopenharmony_ci}
395e1051a39Sopenharmony_ci
396e1051a39Sopenharmony_cistatic int fake_rsa_st_set_ctx_params(void *loaderctx,
397e1051a39Sopenharmony_ci                                      const OSSL_PARAM params[])
398e1051a39Sopenharmony_ci{
399e1051a39Sopenharmony_ci    return 1;
400e1051a39Sopenharmony_ci}
401e1051a39Sopenharmony_ci
402e1051a39Sopenharmony_cistatic int fake_rsa_st_load(void *loaderctx,
403e1051a39Sopenharmony_ci                            OSSL_CALLBACK *object_cb, void *object_cbarg,
404e1051a39Sopenharmony_ci                            OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
405e1051a39Sopenharmony_ci{
406e1051a39Sopenharmony_ci    unsigned char *storectx = loaderctx;
407e1051a39Sopenharmony_ci    OSSL_PARAM params[4];
408e1051a39Sopenharmony_ci    int object_type = OSSL_OBJECT_PKEY;
409e1051a39Sopenharmony_ci    struct fake_rsa_keydata *key = NULL;
410e1051a39Sopenharmony_ci    int rv = 0;
411e1051a39Sopenharmony_ci
412e1051a39Sopenharmony_ci    switch (*storectx) {
413e1051a39Sopenharmony_ci    case 0:
414e1051a39Sopenharmony_ci        /* Construct a new key using our keymgmt functions */
415e1051a39Sopenharmony_ci        if (!TEST_ptr(key = fake_rsa_keymgmt_new(NULL)))
416e1051a39Sopenharmony_ci            break;
417e1051a39Sopenharmony_ci        if (!TEST_int_gt(fake_rsa_keymgmt_import(key, 0, NULL), 0))
418e1051a39Sopenharmony_ci            break;
419e1051a39Sopenharmony_ci        params[0] =
420e1051a39Sopenharmony_ci            OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
421e1051a39Sopenharmony_ci        params[1] =
422e1051a39Sopenharmony_ci            OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
423e1051a39Sopenharmony_ci                                             "RSA", 0);
424e1051a39Sopenharmony_ci        /* The address of the key becomes the octet string */
425e1051a39Sopenharmony_ci        params[2] =
426e1051a39Sopenharmony_ci            OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
427e1051a39Sopenharmony_ci                                              &key, sizeof(*key));
428e1051a39Sopenharmony_ci        params[3] = OSSL_PARAM_construct_end();
429e1051a39Sopenharmony_ci        rv = object_cb(params, object_cbarg);
430e1051a39Sopenharmony_ci        *storectx = 1;
431e1051a39Sopenharmony_ci        break;
432e1051a39Sopenharmony_ci
433e1051a39Sopenharmony_ci    case 2:
434e1051a39Sopenharmony_ci        TEST_info("fake_rsa_load() called in error state");
435e1051a39Sopenharmony_ci        break;
436e1051a39Sopenharmony_ci
437e1051a39Sopenharmony_ci    default:
438e1051a39Sopenharmony_ci        TEST_info("fake_rsa_load() called in eof state");
439e1051a39Sopenharmony_ci        break;
440e1051a39Sopenharmony_ci    }
441e1051a39Sopenharmony_ci
442e1051a39Sopenharmony_ci    TEST_info("fake_rsa_load called - rv: %d", rv);
443e1051a39Sopenharmony_ci
444e1051a39Sopenharmony_ci    if (rv == 0) {
445e1051a39Sopenharmony_ci        fake_rsa_keymgmt_free(key);
446e1051a39Sopenharmony_ci        *storectx = 2;
447e1051a39Sopenharmony_ci    }
448e1051a39Sopenharmony_ci    return rv;
449e1051a39Sopenharmony_ci}
450e1051a39Sopenharmony_ci
451e1051a39Sopenharmony_cistatic int fake_rsa_st_eof(void *loaderctx)
452e1051a39Sopenharmony_ci{
453e1051a39Sopenharmony_ci    unsigned char *storectx = loaderctx;
454e1051a39Sopenharmony_ci
455e1051a39Sopenharmony_ci    /* just one key for now in the fake_rsa store */
456e1051a39Sopenharmony_ci    return *storectx != 0;
457e1051a39Sopenharmony_ci}
458e1051a39Sopenharmony_ci
459e1051a39Sopenharmony_cistatic int fake_rsa_st_close(void *loaderctx)
460e1051a39Sopenharmony_ci{
461e1051a39Sopenharmony_ci    OPENSSL_free(loaderctx);
462e1051a39Sopenharmony_ci    return 1;
463e1051a39Sopenharmony_ci}
464e1051a39Sopenharmony_ci
465e1051a39Sopenharmony_cistatic const OSSL_DISPATCH fake_rsa_store_funcs[] = {
466e1051a39Sopenharmony_ci    { OSSL_FUNC_STORE_OPEN, (void (*)(void))fake_rsa_st_open },
467e1051a39Sopenharmony_ci    { OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS,
468e1051a39Sopenharmony_ci      (void (*)(void))fake_rsa_st_settable_ctx_params },
469e1051a39Sopenharmony_ci    { OSSL_FUNC_STORE_SET_CTX_PARAMS, (void (*)(void))fake_rsa_st_set_ctx_params },
470e1051a39Sopenharmony_ci    { OSSL_FUNC_STORE_LOAD, (void (*)(void))fake_rsa_st_load },
471e1051a39Sopenharmony_ci    { OSSL_FUNC_STORE_EOF, (void (*)(void))fake_rsa_st_eof },
472e1051a39Sopenharmony_ci    { OSSL_FUNC_STORE_CLOSE, (void (*)(void))fake_rsa_st_close },
473e1051a39Sopenharmony_ci    { 0, NULL },
474e1051a39Sopenharmony_ci};
475e1051a39Sopenharmony_ci
476e1051a39Sopenharmony_cistatic const OSSL_ALGORITHM fake_rsa_store_algs[] = {
477e1051a39Sopenharmony_ci    { "fake_rsa", "provider=fake-rsa", fake_rsa_store_funcs },
478e1051a39Sopenharmony_ci    { NULL, NULL, NULL }
479e1051a39Sopenharmony_ci};
480e1051a39Sopenharmony_ci
481e1051a39Sopenharmony_cistatic const OSSL_ALGORITHM *fake_rsa_query(void *provctx,
482e1051a39Sopenharmony_ci                                            int operation_id,
483e1051a39Sopenharmony_ci                                            int *no_cache)
484e1051a39Sopenharmony_ci{
485e1051a39Sopenharmony_ci    *no_cache = 0;
486e1051a39Sopenharmony_ci    switch (operation_id) {
487e1051a39Sopenharmony_ci    case OSSL_OP_SIGNATURE:
488e1051a39Sopenharmony_ci        return fake_rsa_sig_algs;
489e1051a39Sopenharmony_ci
490e1051a39Sopenharmony_ci    case OSSL_OP_KEYMGMT:
491e1051a39Sopenharmony_ci        return fake_rsa_keymgmt_algs;
492e1051a39Sopenharmony_ci
493e1051a39Sopenharmony_ci    case OSSL_OP_STORE:
494e1051a39Sopenharmony_ci        return fake_rsa_store_algs;
495e1051a39Sopenharmony_ci    }
496e1051a39Sopenharmony_ci    return NULL;
497e1051a39Sopenharmony_ci}
498e1051a39Sopenharmony_ci
499e1051a39Sopenharmony_ci/* Functions we provide to the core */
500e1051a39Sopenharmony_cistatic const OSSL_DISPATCH fake_rsa_method[] = {
501e1051a39Sopenharmony_ci    { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
502e1051a39Sopenharmony_ci    { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fake_rsa_query },
503e1051a39Sopenharmony_ci    { 0, NULL }
504e1051a39Sopenharmony_ci};
505e1051a39Sopenharmony_ci
506e1051a39Sopenharmony_cistatic int fake_rsa_provider_init(const OSSL_CORE_HANDLE *handle,
507e1051a39Sopenharmony_ci                                  const OSSL_DISPATCH *in,
508e1051a39Sopenharmony_ci                                  const OSSL_DISPATCH **out, void **provctx)
509e1051a39Sopenharmony_ci{
510e1051a39Sopenharmony_ci    if (!TEST_ptr(*provctx = OSSL_LIB_CTX_new()))
511e1051a39Sopenharmony_ci        return 0;
512e1051a39Sopenharmony_ci    *out = fake_rsa_method;
513e1051a39Sopenharmony_ci    return 1;
514e1051a39Sopenharmony_ci}
515e1051a39Sopenharmony_ci
516e1051a39Sopenharmony_ciOSSL_PROVIDER *fake_rsa_start(OSSL_LIB_CTX *libctx)
517e1051a39Sopenharmony_ci{
518e1051a39Sopenharmony_ci    OSSL_PROVIDER *p;
519e1051a39Sopenharmony_ci
520e1051a39Sopenharmony_ci    if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "fake-rsa",
521e1051a39Sopenharmony_ci                                             fake_rsa_provider_init))
522e1051a39Sopenharmony_ci            || !TEST_ptr(p = OSSL_PROVIDER_try_load(libctx, "fake-rsa", 1)))
523e1051a39Sopenharmony_ci        return NULL;
524e1051a39Sopenharmony_ci
525e1051a39Sopenharmony_ci    return p;
526e1051a39Sopenharmony_ci}
527e1051a39Sopenharmony_ci
528e1051a39Sopenharmony_civoid fake_rsa_finish(OSSL_PROVIDER *p)
529e1051a39Sopenharmony_ci{
530e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(p);
531e1051a39Sopenharmony_ci}
532