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