Prune low-level ASN.1 parse errors from error queue in decoder_process()
[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_dispatch.h>
14 #include <openssl/core_names.h>
15 #include <openssl/params.h>
16 #include "prov/provider_ctx.h"
17 #include "prov/implementations.h"
18 #include "prov/providercommon.h"
19
20 /*
21  * Forward declarations to ensure that interface functions are correctly
22  * defined.
23  */
24 static OSSL_FUNC_provider_gettable_params_fn legacy_gettable_params;
25 static OSSL_FUNC_provider_get_params_fn legacy_get_params;
26 static OSSL_FUNC_provider_query_operation_fn legacy_query;
27
28 #define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC }
29
30 #ifdef STATIC_LEGACY
31 OSSL_provider_init_fn ossl_legacy_provider_init;
32 # define OSSL_provider_init ossl_legacy_provider_init
33 #endif
34
35 /* Functions provided by the core */
36 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
37 static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
38
39 /* Parameters we provide to the core */
40 static const OSSL_PARAM legacy_param_types[] = {
41     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
42     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
43     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
44     OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
45     OSSL_PARAM_END
46 };
47
48 static const OSSL_PARAM *legacy_gettable_params(void *provctx)
49 {
50     return legacy_param_types;
51 }
52
53 static int legacy_get_params(void *provctx, OSSL_PARAM params[])
54 {
55     OSSL_PARAM *p;
56
57     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
58     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider"))
59         return 0;
60     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
61     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
62         return 0;
63     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
64     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
65         return 0;
66     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
67     if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
68         return 0;
69     return 1;
70 }
71
72 static const OSSL_ALGORITHM legacy_digests[] = {
73 #ifndef OPENSSL_NO_MD2
74     ALG("MD2", md2_functions),
75 #endif
76 #ifndef OPENSSL_NO_MD4
77     ALG("MD4", md4_functions),
78 #endif
79 #ifndef OPENSSL_NO_MDC2
80     ALG("MDC2", mdc2_functions),
81 #endif /* OPENSSL_NO_MDC2 */
82 #ifndef OPENSSL_NO_WHIRLPOOL
83     ALG("WHIRLPOOL", wp_functions),
84 #endif /* OPENSSL_NO_WHIRLPOOL */
85 #ifndef OPENSSL_NO_RMD160
86     ALG("RIPEMD-160:RIPEMD160:RIPEMD:RMD160", ripemd160_functions),
87 #endif /* OPENSSL_NO_RMD160 */
88     { NULL, NULL, NULL }
89 };
90
91 static const OSSL_ALGORITHM legacy_ciphers[] = {
92 #ifndef OPENSSL_NO_CAST
93     ALG("CAST5-ECB", cast5128ecb_functions),
94     ALG("CAST5-CBC:CAST-CBC:CAST", cast5128cbc_functions),
95     ALG("CAST5-OFB", cast5128ofb64_functions),
96     ALG("CAST5-CFB", cast5128cfb64_functions),
97 #endif /* OPENSSL_NO_CAST */
98 #ifndef OPENSSL_NO_BF
99     ALG("BF-ECB", blowfish128ecb_functions),
100     ALG("BF-CBC:BF:BLOWFISH", blowfish128cbc_functions),
101     ALG("BF-OFB", blowfish64ofb64_functions),
102     ALG("BF-CFB", blowfish64cfb64_functions),
103 #endif /* OPENSSL_NO_BF */
104 #ifndef OPENSSL_NO_IDEA
105     ALG("IDEA-ECB", idea128ecb_functions),
106     ALG("IDEA-CBC:IDEA", idea128cbc_functions),
107     ALG("IDEA-OFB:IDEA-OFB64", idea128ofb64_functions),
108     ALG("IDEA-CFB:IDEA-CFB64", idea128cfb64_functions),
109 #endif /* OPENSSL_NO_IDEA */
110 #ifndef OPENSSL_NO_SEED
111     ALG("SEED-ECB", seed128ecb_functions),
112     ALG("SEED-CBC:SEED", seed128cbc_functions),
113     ALG("SEED-OFB:SEED-OFB128", seed128ofb128_functions),
114     ALG("SEED-CFB:SEED-CFB128", seed128cfb128_functions),
115 #endif /* OPENSSL_NO_SEED */
116 #ifndef OPENSSL_NO_RC2
117     ALG("RC2-ECB", rc2128ecb_functions),
118     ALG("RC2-CBC:RC2:RC2-128", rc2128cbc_functions),
119     ALG("RC2-40-CBC:RC2-40", rc240cbc_functions),
120     ALG("RC2-64-CBC:RC2-64", rc264cbc_functions),
121     ALG("RC2-CFB", rc2128cfb128_functions),
122     ALG("RC2-OFB", rc2128ofb128_functions),
123 #endif /* OPENSSL_NO_RC2 */
124 #ifndef OPENSSL_NO_RC4
125     ALG("RC4", rc4128_functions),
126     ALG("RC4-40", rc440_functions),
127 # ifndef OPENSSL_NO_MD5
128     ALG("RC4-HMAC-MD5", rc4_hmac_md5_functions),
129 # endif /* OPENSSL_NO_MD5 */
130 #endif /* OPENSSL_NO_RC4 */
131 #ifndef OPENSSL_NO_RC5
132     ALG("RC5-ECB", rc5128ecb_functions),
133     ALG("RC5-CBC:RC5", rc5128cbc_functions),
134     ALG("RC5-OFB", rc5128ofb64_functions),
135     ALG("RC5-CFB", rc5128cfb64_functions),
136 #endif /* OPENSSL_NO_RC5 */
137 #ifndef OPENSSL_NO_DES
138     ALG("DESX-CBC:DESX", tdes_desx_cbc_functions),
139     ALG("DES-ECB", des_ecb_functions),
140     ALG("DES-CBC:DES", des_cbc_functions),
141     ALG("DES-OFB", des_ofb64_functions),
142     ALG("DES-CFB", des_cfb64_functions),
143     ALG("DES-CFB1", des_cfb1_functions),
144     ALG("DES-CFB8", des_cfb8_functions),
145 #endif /* OPENSSL_NO_DES */
146     { NULL, NULL, NULL }
147 };
148
149 static const OSSL_ALGORITHM *legacy_query(void *provctx, int operation_id,
150                                           int *no_cache)
151 {
152     *no_cache = 0;
153     switch (operation_id) {
154     case OSSL_OP_DIGEST:
155         return legacy_digests;
156     case OSSL_OP_CIPHER:
157         return legacy_ciphers;
158     }
159     return NULL;
160 }
161
162 static void legacy_teardown(void *provctx)
163 {
164     OPENSSL_CTX_free(PROV_LIBRARY_CONTEXT_OF(provctx));
165     PROV_CTX_free(provctx);
166 }
167
168 /* Functions we provide to the core */
169 static const OSSL_DISPATCH legacy_dispatch_table[] = {
170     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))legacy_teardown },
171     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params },
172     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params },
173     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query },
174     { 0, NULL }
175 };
176
177 int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
178                        const OSSL_DISPATCH *in,
179                        const OSSL_DISPATCH **out,
180                        void **provctx)
181 {
182     OSSL_FUNC_core_get_library_context_fn *c_get_libctx = NULL;
183     OPENSSL_CTX *libctx = NULL;
184
185     for (; in->function_id != 0; in++) {
186         switch (in->function_id) {
187         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
188             c_gettable_params = OSSL_FUNC_core_gettable_params(in);
189             break;
190         case OSSL_FUNC_CORE_GET_PARAMS:
191             c_get_params = OSSL_FUNC_core_get_params(in);
192             break;
193         case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
194             c_get_libctx = OSSL_FUNC_core_get_library_context(in);
195             break;
196         /* Just ignore anything we don't understand */
197         default:
198             break;
199         }
200     }
201
202     if (c_get_libctx == NULL)
203         return 0;
204
205     if ((*provctx = PROV_CTX_new()) == NULL
206         || (libctx = OPENSSL_CTX_new()) == NULL) {
207         OPENSSL_CTX_free(libctx);
208         legacy_teardown(*provctx);
209         *provctx = NULL;
210         return 0;
211     }
212     PROV_CTX_set0_library_context(*provctx, libctx);
213     PROV_CTX_set0_handle(*provctx, handle);
214
215     *out = legacy_dispatch_table;
216
217     return 1;
218 }