07b505a8ac5c3a4a8d08cc0d24409110b0451bb8
[openssl.git] / providers / legacyprov.c
1 /*
2  * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11 #include <stdio.h>
12 #include <openssl/core.h>
13 #include <openssl/core_numbers.h>
14 #include <openssl/core_names.h>
15 #include <openssl/params.h>
16 #include "prov/provider_ctx.h"
17 #include "prov/implementations.h"
18
19 /*
20  * Forward declarations to ensure that interface functions are correctly
21  * defined.
22  */
23 static OSSL_provider_gettable_params_fn legacy_gettable_params;
24 static OSSL_provider_get_params_fn legacy_get_params;
25 static OSSL_provider_query_operation_fn legacy_query;
26
27 #define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC }
28
29 #ifdef STATIC_LEGACY
30 OSSL_provider_init_fn ossl_legacy_provider_init;
31 # define OSSL_provider_init ossl_legacy_provider_init
32 #endif
33
34 /* Functions provided by the core */
35 static OSSL_core_gettable_params_fn *c_gettable_params = NULL;
36 static OSSL_core_get_params_fn *c_get_params = NULL;
37
38 /* Parameters we provide to the core */
39 static const OSSL_PARAM legacy_param_types[] = {
40     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
41     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
42     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
43     OSSL_PARAM_END
44 };
45
46 static const OSSL_PARAM *legacy_gettable_params(void *provctx)
47 {
48     return legacy_param_types;
49 }
50
51 static int legacy_get_params(void *provctx, OSSL_PARAM params[])
52 {
53     OSSL_PARAM *p;
54
55     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
56     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider"))
57         return 0;
58     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
59     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
60         return 0;
61     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
62     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
63         return 0;
64
65     return 1;
66 }
67
68 static const OSSL_ALGORITHM legacy_digests[] = {
69 #ifndef OPENSSL_NO_MD2
70     ALG("MD2", md2_functions),
71 #endif
72 #ifndef OPENSSL_NO_MD4
73     ALG("MD4", md4_functions),
74 #endif
75 #ifndef OPENSSL_NO_MDC2
76     ALG("MDC2", mdc2_functions),
77 #endif /* OPENSSL_NO_MDC2 */
78 #ifndef OPENSSL_NO_WHIRLPOOL
79     ALG("WHIRLPOOL", wp_functions),
80 #endif /* OPENSSL_NO_WHIRLPOOL */
81 #ifndef OPENSSL_NO_RMD160
82     ALG("RIPEMD-160:RIPEMD160:RIPEMD:RMD160", ripemd160_functions),
83 #endif /* OPENSSL_NO_RMD160 */
84     { NULL, NULL, NULL }
85 };
86
87 static const OSSL_ALGORITHM legacy_ciphers[] = {
88 #ifndef OPENSSL_NO_CAST
89     ALG("CAST5-ECB", cast5128ecb_functions),
90     ALG("CAST5-CBC:CAST-CBC:CAST", cast5128cbc_functions),
91     ALG("CAST5-OFB", cast5128ofb64_functions),
92     ALG("CAST5-CFB", cast5128cfb64_functions),
93 #endif /* OPENSSL_NO_CAST */
94 #ifndef OPENSSL_NO_BF
95     ALG("BF-ECB", blowfish128ecb_functions),
96     ALG("BF-CBC:BF:BLOWFISH", blowfish128cbc_functions),
97     ALG("BF-OFB", blowfish64ofb64_functions),
98     ALG("BF-CFB", blowfish64cfb64_functions),
99 #endif /* OPENSSL_NO_BF */
100 #ifndef OPENSSL_NO_IDEA
101     ALG("IDEA-ECB", idea128ecb_functions),
102     ALG("IDEA-CBC:IDEA", idea128cbc_functions),
103     ALG("IDEA-OFB:IDEA-OFB64", idea128ofb64_functions),
104     ALG("IDEA-CFB:IDEA-CFB64", idea128cfb64_functions),
105 #endif /* OPENSSL_NO_IDEA */
106 #ifndef OPENSSL_NO_SEED
107     ALG("SEED-ECB", seed128ecb_functions),
108     ALG("SEED-CBC:SEED", seed128cbc_functions),
109     ALG("SEED-OFB:SEED-OFB128", seed128ofb128_functions),
110     ALG("SEED-CFB:SEED-CFB128", seed128cfb128_functions),
111 #endif /* OPENSSL_NO_SEED */
112 #ifndef OPENSSL_NO_RC2
113     ALG("RC2-ECB", rc2128ecb_functions),
114     ALG("RC2-CBC", rc2128cbc_functions),
115     ALG("RC2-40-CBC", rc240cbc_functions),
116     ALG("RC2-64-CBC", rc264cbc_functions),
117     ALG("RC2-CFB", rc2128cfb128_functions),
118     ALG("RC2-OFB", rc2128ofb128_functions),
119 #endif /* OPENSSL_NO_RC2 */
120 #ifndef OPENSSL_NO_RC4
121     ALG("RC4", rc4128_functions),
122     ALG("RC4-40", rc440_functions),
123 # ifndef OPENSSL_NO_MD5
124     ALG("RC4-HMAC-MD5", rc4_hmac_md5_functions),
125 # endif /* OPENSSL_NO_MD5 */
126 #endif /* OPENSSL_NO_RC4 */
127 #ifndef OPENSSL_NO_RC5
128     ALG("RC5-ECB", rc5128ecb_functions),
129     ALG("RC5-CBC", rc5128cbc_functions),
130     ALG("RC5-OFB", rc5128ofb64_functions),
131     ALG("RC5-CFB", rc5128cfb64_functions),
132 #endif /* OPENSSL_NO_RC5 */
133 #ifndef OPENSSL_NO_DES
134     ALG("DESX-CBC:DESX", tdes_desx_cbc_functions),
135     ALG("DES-ECB", des_ecb_functions),
136     ALG("DES-CBC:DES", des_cbc_functions),
137     ALG("DES-OFB", des_ofb64_functions),
138     ALG("DES-CFB", des_cfb64_functions),
139     ALG("DES-CFB1", des_cfb1_functions),
140     ALG("DES-CFB8", des_cfb8_functions),
141 #endif /* OPENSSL_NO_DES */
142     { NULL, NULL, NULL }
143 };
144
145 static const OSSL_ALGORITHM *legacy_query(void *provctx, int operation_id,
146                                           int *no_cache)
147 {
148     *no_cache = 0;
149     switch (operation_id) {
150     case OSSL_OP_DIGEST:
151         return legacy_digests;
152     case OSSL_OP_CIPHER:
153         return legacy_ciphers;
154     }
155     return NULL;
156 }
157
158 /* Functions we provide to the core */
159 static const OSSL_DISPATCH legacy_dispatch_table[] = {
160     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params },
161     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params },
162     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query },
163     { 0, NULL }
164 };
165
166 int OSSL_provider_init(const OSSL_PROVIDER *provider,
167                        const OSSL_DISPATCH *in,
168                        const OSSL_DISPATCH **out,
169                        void **provctx)
170 {
171     OSSL_core_get_library_context_fn *c_get_libctx = NULL;
172
173     for (; in->function_id != 0; in++) {
174         switch (in->function_id) {
175         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
176             c_gettable_params = OSSL_get_core_gettable_params(in);
177             break;
178         case OSSL_FUNC_CORE_GET_PARAMS:
179             c_get_params = OSSL_get_core_get_params(in);
180             break;
181         case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
182             c_get_libctx = OSSL_get_core_get_library_context(in);
183             break;
184         /* Just ignore anything we don't understand */
185         default:
186             break;
187         }
188     }
189
190     if (c_get_libctx == NULL)
191         return 0;
192
193     *out = legacy_dispatch_table;
194
195     /*
196      * We want to make sure that all calls from this provider that requires
197      * a library context use the same context as the one used to call our
198      * functions.  We do that by passing it along as the provider context.
199      */
200     *provctx = c_get_libctx(provider);
201     return 1;
202 }