Home | History | Annotate | Line # | Download | only in providers
      1  1.1  christos /*
      2  1.1  christos  * Copyright 2019-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").  You may not use
      5  1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6  1.1  christos  * in the file LICENSE in the source distribution or at
      7  1.1  christos  * https://www.openssl.org/source/license.html
      8  1.1  christos  */
      9  1.1  christos 
     10  1.1  christos #include <string.h>
     11  1.1  christos #include <stdio.h>
     12  1.1  christos #include <openssl/core.h>
     13  1.1  christos #include <openssl/core_dispatch.h>
     14  1.1  christos #include <openssl/core_names.h>
     15  1.1  christos #include <openssl/params.h>
     16  1.1  christos #include "prov/provider_ctx.h"
     17  1.1  christos #include "prov/implementations.h"
     18  1.1  christos #include "prov/names.h"
     19  1.1  christos #include "prov/providercommon.h"
     20  1.1  christos 
     21  1.1  christos /*
     22  1.1  christos  * Forward declarations to ensure that interface functions are correctly
     23  1.1  christos  * defined.
     24  1.1  christos  */
     25  1.1  christos static OSSL_FUNC_provider_gettable_params_fn legacy_gettable_params;
     26  1.1  christos static OSSL_FUNC_provider_get_params_fn legacy_get_params;
     27  1.1  christos static OSSL_FUNC_provider_query_operation_fn legacy_query;
     28  1.1  christos 
     29  1.1  christos #define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC }
     30  1.1  christos 
     31  1.1  christos #ifdef STATIC_LEGACY
     32  1.1  christos OSSL_provider_init_fn ossl_legacy_provider_init;
     33  1.1  christos # define OSSL_provider_init ossl_legacy_provider_init
     34  1.1  christos #endif
     35  1.1  christos 
     36  1.1  christos /* Parameters we provide to the core */
     37  1.1  christos static const OSSL_PARAM legacy_param_types[] = {
     38  1.1  christos     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
     39  1.1  christos     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
     40  1.1  christos     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
     41  1.1  christos     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
     42  1.1  christos     OSSL_PARAM_END
     43  1.1  christos };
     44  1.1  christos 
     45  1.1  christos static const OSSL_PARAM *legacy_gettable_params(void *provctx)
     46  1.1  christos {
     47  1.1  christos     return legacy_param_types;
     48  1.1  christos }
     49  1.1  christos 
     50  1.1  christos static int legacy_get_params(void *provctx, OSSL_PARAM params[])
     51  1.1  christos {
     52  1.1  christos     OSSL_PARAM *p;
     53  1.1  christos 
     54  1.1  christos     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
     55  1.1  christos     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider"))
     56  1.1  christos         return 0;
     57  1.1  christos     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
     58  1.1  christos     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
     59  1.1  christos         return 0;
     60  1.1  christos     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
     61  1.1  christos     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
     62  1.1  christos         return 0;
     63  1.1  christos     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
     64  1.1  christos     if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
     65  1.1  christos         return 0;
     66  1.1  christos     return 1;
     67  1.1  christos }
     68  1.1  christos 
     69  1.1  christos static const OSSL_ALGORITHM legacy_digests[] = {
     70  1.1  christos #ifndef OPENSSL_NO_MD2
     71  1.1  christos     ALG(PROV_NAMES_MD2, ossl_md2_functions),
     72  1.1  christos #endif
     73  1.1  christos #ifndef OPENSSL_NO_MD4
     74  1.1  christos     ALG(PROV_NAMES_MD4, ossl_md4_functions),
     75  1.1  christos #endif
     76  1.1  christos #ifndef OPENSSL_NO_MDC2
     77  1.1  christos     ALG(PROV_NAMES_MDC2, ossl_mdc2_functions),
     78  1.1  christos #endif /* OPENSSL_NO_MDC2 */
     79  1.1  christos #ifndef OPENSSL_NO_WHIRLPOOL
     80  1.1  christos     ALG(PROV_NAMES_WHIRLPOOL, ossl_wp_functions),
     81  1.1  christos #endif /* OPENSSL_NO_WHIRLPOOL */
     82  1.1  christos #ifndef OPENSSL_NO_RMD160
     83  1.1  christos     ALG(PROV_NAMES_RIPEMD_160, ossl_ripemd160_functions),
     84  1.1  christos #endif /* OPENSSL_NO_RMD160 */
     85  1.1  christos     { NULL, NULL, NULL }
     86  1.1  christos };
     87  1.1  christos 
     88  1.1  christos static const OSSL_ALGORITHM legacy_ciphers[] = {
     89  1.1  christos #ifndef OPENSSL_NO_CAST
     90  1.1  christos     ALG(PROV_NAMES_CAST5_ECB, ossl_cast5128ecb_functions),
     91  1.1  christos     ALG(PROV_NAMES_CAST5_CBC, ossl_cast5128cbc_functions),
     92  1.1  christos     ALG(PROV_NAMES_CAST5_OFB, ossl_cast5128ofb64_functions),
     93  1.1  christos     ALG(PROV_NAMES_CAST5_CFB, ossl_cast5128cfb64_functions),
     94  1.1  christos #endif /* OPENSSL_NO_CAST */
     95  1.1  christos #ifndef OPENSSL_NO_BF
     96  1.1  christos     ALG(PROV_NAMES_BF_ECB, ossl_blowfish128ecb_functions),
     97  1.1  christos     ALG(PROV_NAMES_BF_CBC, ossl_blowfish128cbc_functions),
     98  1.1  christos     ALG(PROV_NAMES_BF_OFB, ossl_blowfish128ofb64_functions),
     99  1.1  christos     ALG(PROV_NAMES_BF_CFB, ossl_blowfish128cfb64_functions),
    100  1.1  christos #endif /* OPENSSL_NO_BF */
    101  1.1  christos #ifndef OPENSSL_NO_IDEA
    102  1.1  christos     ALG(PROV_NAMES_IDEA_ECB, ossl_idea128ecb_functions),
    103  1.1  christos     ALG(PROV_NAMES_IDEA_CBC, ossl_idea128cbc_functions),
    104  1.1  christos     ALG(PROV_NAMES_IDEA_OFB, ossl_idea128ofb64_functions),
    105  1.1  christos     ALG(PROV_NAMES_IDEA_CFB, ossl_idea128cfb64_functions),
    106  1.1  christos #endif /* OPENSSL_NO_IDEA */
    107  1.1  christos #ifndef OPENSSL_NO_SEED
    108  1.1  christos     ALG(PROV_NAMES_SEED_ECB, ossl_seed128ecb_functions),
    109  1.1  christos     ALG(PROV_NAMES_SEED_CBC, ossl_seed128cbc_functions),
    110  1.1  christos     ALG(PROV_NAMES_SEED_OFB, ossl_seed128ofb128_functions),
    111  1.1  christos     ALG(PROV_NAMES_SEED_CFB, ossl_seed128cfb128_functions),
    112  1.1  christos #endif /* OPENSSL_NO_SEED */
    113  1.1  christos #ifndef OPENSSL_NO_RC2
    114  1.1  christos     ALG(PROV_NAMES_RC2_ECB, ossl_rc2128ecb_functions),
    115  1.1  christos     ALG(PROV_NAMES_RC2_CBC, ossl_rc2128cbc_functions),
    116  1.1  christos     ALG(PROV_NAMES_RC2_40_CBC, ossl_rc240cbc_functions),
    117  1.1  christos     ALG(PROV_NAMES_RC2_64_CBC, ossl_rc264cbc_functions),
    118  1.1  christos     ALG(PROV_NAMES_RC2_CFB, ossl_rc2128cfb128_functions),
    119  1.1  christos     ALG(PROV_NAMES_RC2_OFB, ossl_rc2128ofb128_functions),
    120  1.1  christos #endif /* OPENSSL_NO_RC2 */
    121  1.1  christos #ifndef OPENSSL_NO_RC4
    122  1.1  christos     ALG(PROV_NAMES_RC4, ossl_rc4128_functions),
    123  1.1  christos     ALG(PROV_NAMES_RC4_40, ossl_rc440_functions),
    124  1.1  christos # ifndef OPENSSL_NO_MD5
    125  1.1  christos     ALG(PROV_NAMES_RC4_HMAC_MD5, ossl_rc4_hmac_ossl_md5_functions),
    126  1.1  christos # endif /* OPENSSL_NO_MD5 */
    127  1.1  christos #endif /* OPENSSL_NO_RC4 */
    128  1.1  christos #ifndef OPENSSL_NO_RC5
    129  1.1  christos     ALG(PROV_NAMES_RC5_ECB, ossl_rc5128ecb_functions),
    130  1.1  christos     ALG(PROV_NAMES_RC5_CBC, ossl_rc5128cbc_functions),
    131  1.1  christos     ALG(PROV_NAMES_RC5_OFB, ossl_rc5128ofb64_functions),
    132  1.1  christos     ALG(PROV_NAMES_RC5_CFB, ossl_rc5128cfb64_functions),
    133  1.1  christos #endif /* OPENSSL_NO_RC5 */
    134  1.1  christos #ifndef OPENSSL_NO_DES
    135  1.1  christos     ALG(PROV_NAMES_DESX_CBC, ossl_tdes_desx_cbc_functions),
    136  1.1  christos     ALG(PROV_NAMES_DES_ECB, ossl_des_ecb_functions),
    137  1.1  christos     ALG(PROV_NAMES_DES_CBC, ossl_des_cbc_functions),
    138  1.1  christos     ALG(PROV_NAMES_DES_OFB, ossl_des_ofb64_functions),
    139  1.1  christos     ALG(PROV_NAMES_DES_CFB, ossl_des_cfb64_functions),
    140  1.1  christos     ALG(PROV_NAMES_DES_CFB1, ossl_des_cfb1_functions),
    141  1.1  christos     ALG(PROV_NAMES_DES_CFB8, ossl_des_cfb8_functions),
    142  1.1  christos #endif /* OPENSSL_NO_DES */
    143  1.1  christos     { NULL, NULL, NULL }
    144  1.1  christos };
    145  1.1  christos 
    146  1.1  christos static const OSSL_ALGORITHM legacy_kdfs[] = {
    147  1.1  christos     ALG(PROV_NAMES_PBKDF1, ossl_kdf_pbkdf1_functions),
    148  1.1  christos     { NULL, NULL, NULL }
    149  1.1  christos };
    150  1.1  christos 
    151  1.1  christos static const OSSL_ALGORITHM *legacy_query(void *provctx, int operation_id,
    152  1.1  christos                                           int *no_cache)
    153  1.1  christos {
    154  1.1  christos     *no_cache = 0;
    155  1.1  christos     switch (operation_id) {
    156  1.1  christos     case OSSL_OP_DIGEST:
    157  1.1  christos         return legacy_digests;
    158  1.1  christos     case OSSL_OP_CIPHER:
    159  1.1  christos         return legacy_ciphers;
    160  1.1  christos     case OSSL_OP_KDF:
    161  1.1  christos         return legacy_kdfs;
    162  1.1  christos     }
    163  1.1  christos     return NULL;
    164  1.1  christos }
    165  1.1  christos 
    166  1.1  christos static void legacy_teardown(void *provctx)
    167  1.1  christos {
    168  1.1  christos     OSSL_LIB_CTX_free(PROV_LIBCTX_OF(provctx));
    169  1.1  christos     ossl_prov_ctx_free(provctx);
    170  1.1  christos }
    171  1.1  christos 
    172  1.1  christos /* Functions we provide to the core */
    173  1.1  christos static const OSSL_DISPATCH legacy_dispatch_table[] = {
    174  1.1  christos     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))legacy_teardown },
    175  1.1  christos     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params },
    176  1.1  christos     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params },
    177  1.1  christos     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query },
    178  1.1  christos     { 0, NULL }
    179  1.1  christos };
    180  1.1  christos 
    181  1.1  christos int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
    182  1.1  christos                        const OSSL_DISPATCH *in,
    183  1.1  christos                        const OSSL_DISPATCH **out,
    184  1.1  christos                        void **provctx)
    185  1.1  christos {
    186  1.1  christos     OSSL_LIB_CTX *libctx = NULL;
    187  1.1  christos 
    188  1.1  christos     if ((*provctx = ossl_prov_ctx_new()) == NULL
    189  1.1  christos         || (libctx = OSSL_LIB_CTX_new_child(handle, in)) == NULL) {
    190  1.1  christos         OSSL_LIB_CTX_free(libctx);
    191  1.1  christos         legacy_teardown(*provctx);
    192  1.1  christos         *provctx = NULL;
    193  1.1  christos         return 0;
    194  1.1  christos     }
    195  1.1  christos     ossl_prov_ctx_set0_libctx(*provctx, libctx);
    196  1.1  christos     ossl_prov_ctx_set0_handle(*provctx, handle);
    197  1.1  christos 
    198  1.1  christos     *out = legacy_dispatch_table;
    199  1.1  christos 
    200  1.1  christos     return 1;
    201  1.1  christos }
    202