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