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