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