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