59373286384928c24c23c7c1b91c464d3657c30d
[openssl.git] / providers / legacyprov.c
1 /*
2  * Copyright 2019 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 #ifdef STATIC_LEGACY
19 OSSL_provider_init_fn ossl_legacy_provider_init;
20 # define OSSL_provider_init ossl_legacy_provider_init
21 #endif
22
23 /* Functions provided by the core */
24 static OSSL_core_gettable_params_fn *c_gettable_params = NULL;
25 static OSSL_core_get_params_fn *c_get_params = NULL;
26
27 /* Parameters we provide to the core */
28 static const OSSL_ITEM legacy_param_types[] = {
29     { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_NAME },
30     { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_VERSION },
31     { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_BUILDINFO },
32     { 0, NULL }
33 };
34
35 static const OSSL_ITEM *legacy_gettable_params(const OSSL_PROVIDER *prov)
36 {
37     return legacy_param_types;
38 }
39
40 static int legacy_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
41 {
42     OSSL_PARAM *p;
43
44     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
45     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Legacy Provider"))
46         return 0;
47     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
48     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
49         return 0;
50     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
51     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
52         return 0;
53
54     return 1;
55 }
56
57 static const OSSL_ALGORITHM legacy_digests[] = {
58 #ifndef OPENSSL_NO_MD2
59     { "MD2", "legacy=yes", md2_functions },
60 #endif
61
62 #ifndef OPENSSL_NO_MD4
63     { "MD4", "legacy=yes", md4_functions },
64 #endif
65
66 #ifndef OPENSSL_NO_MDC2
67     { "MDC2", "legacy=yes", mdc2_functions },
68 #endif /* OPENSSL_NO_MDC2 */
69
70 #ifndef OPENSSL_NO_WHIRLPOOL
71     { "WHIRLPOOL", "legacy=yes", wp_functions },
72 #endif /* OPENSSL_NO_WHIRLPOOL */
73
74 #ifndef OPENSSL_NO_RMD160
75     { "RIPEMD-160:RIPEMD160:RIPEMD:RMD160", "legacy=yes", ripemd160_functions },
76 #endif /* OPENSSL_NO_RMD160 */
77
78     { NULL, NULL, NULL }
79 };
80
81 static const OSSL_ALGORITHM *legacy_query(OSSL_PROVIDER *prov,
82                                           int operation_id,
83                                           int *no_cache)
84 {
85     *no_cache = 0;
86     switch (operation_id) {
87     case OSSL_OP_DIGEST:
88         return legacy_digests;
89     }
90     return NULL;
91 }
92
93 /* Functions we provide to the core */
94 static const OSSL_DISPATCH legacy_dispatch_table[] = {
95     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))legacy_gettable_params },
96     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))legacy_get_params },
97     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))legacy_query },
98     { 0, NULL }
99 };
100
101 int OSSL_provider_init(const OSSL_PROVIDER *provider,
102                        const OSSL_DISPATCH *in,
103                        const OSSL_DISPATCH **out,
104                        void **provctx)
105 {
106     OSSL_core_get_library_context_fn *c_get_libctx = NULL;
107
108     for (; in->function_id != 0; in++) {
109         switch (in->function_id) {
110         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
111             c_gettable_params = OSSL_get_core_gettable_params(in);
112             break;
113         case OSSL_FUNC_CORE_GET_PARAMS:
114             c_get_params = OSSL_get_core_get_params(in);
115             break;
116         case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
117             c_get_libctx = OSSL_get_core_get_library_context(in);
118             break;
119         /* Just ignore anything we don't understand */
120         default:
121             break;
122         }
123     }
124
125     if (c_get_libctx == NULL)
126         return 0;
127
128     *out = legacy_dispatch_table;
129
130     /*
131      * We want to make sure that all calls from this provider that requires
132      * a library context use the same context as the one used to call our
133      * functions.  We do that by passing it along as the provider context.
134      */
135     *provctx = c_get_libctx(provider);
136     return 1;
137 }