]>
Commit | Line | Data |
---|---|---|
dfc0857d | 1 | /* |
da1c088f | 2 | * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved. |
dfc0857d P |
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/opensslconf.h> | |
13 | #include <openssl/core.h> | |
14 | #include <openssl/core_dispatch.h> | |
15 | #include <openssl/core_names.h> | |
16 | #include <openssl/params.h> | |
17 | #include "prov/bio.h" | |
18 | #include "prov/provider_ctx.h" | |
19 | #include "prov/providercommon.h" | |
20 | #include "prov/implementations.h" | |
21 | #include "prov/provider_util.h" | |
dfc0857d P |
22 | |
23 | /* | |
24 | * Forward declarations to ensure that interface functions are correctly | |
25 | * defined. | |
26 | */ | |
27 | static OSSL_FUNC_provider_gettable_params_fn base_gettable_params; | |
28 | static OSSL_FUNC_provider_get_params_fn base_get_params; | |
29 | static OSSL_FUNC_provider_query_operation_fn base_query; | |
30 | ||
31 | /* Functions provided by the core */ | |
32 | static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL; | |
33 | static OSSL_FUNC_core_get_params_fn *c_get_params = NULL; | |
34 | ||
35 | /* Parameters we provide to the core */ | |
36 | static const OSSL_PARAM base_param_types[] = { | |
37 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0), | |
38 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0), | |
39 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0), | |
eab7b424 | 40 | OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0), |
dfc0857d P |
41 | OSSL_PARAM_END |
42 | }; | |
43 | ||
44 | static const OSSL_PARAM *base_gettable_params(void *provctx) | |
45 | { | |
46 | return base_param_types; | |
47 | } | |
48 | ||
49 | static int base_get_params(void *provctx, OSSL_PARAM params[]) | |
50 | { | |
51 | OSSL_PARAM *p; | |
52 | ||
53 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME); | |
54 | if (p != NULL | |
55 | && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Base Provider")) | |
56 | return 0; | |
57 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION); | |
58 | if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR)) | |
59 | return 0; | |
60 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO); | |
61 | if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR)) | |
62 | return 0; | |
eab7b424 P |
63 | p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS); |
64 | if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running())) | |
65 | return 0; | |
dfc0857d P |
66 | |
67 | return 1; | |
68 | } | |
69 | ||
ece9304c | 70 | static const OSSL_ALGORITHM base_encoder[] = { |
c319b627 | 71 | #define ENCODER_PROVIDER "base" |
ece9304c | 72 | #include "encoders.inc" |
dfc0857d | 73 | { NULL, NULL, NULL } |
c319b627 | 74 | #undef ENCODER_PROVIDER |
dfc0857d | 75 | }; |
dfc0857d | 76 | |
ece9304c | 77 | static const OSSL_ALGORITHM base_decoder[] = { |
2c090c1d | 78 | #define DECODER_PROVIDER "base" |
ece9304c | 79 | #include "decoders.inc" |
a3f15e23 | 80 | { NULL, NULL, NULL } |
2c090c1d | 81 | #undef DECODER_PROVIDER |
a3f15e23 | 82 | }; |
a3f15e23 | 83 | |
63f187cf | 84 | static const OSSL_ALGORITHM base_store[] = { |
ce43db7a JS |
85 | #define STORE(name, _fips, func_table) \ |
86 | { name, "provider=base,fips=" _fips, (func_table) }, | |
63f187cf RL |
87 | |
88 | #include "stores.inc" | |
89 | { NULL, NULL, NULL } | |
90 | #undef STORE | |
91 | }; | |
92 | ||
dfc0857d P |
93 | static const OSSL_ALGORITHM *base_query(void *provctx, int operation_id, |
94 | int *no_cache) | |
95 | { | |
96 | *no_cache = 0; | |
a3f15e23 | 97 | switch (operation_id) { |
ece9304c RL |
98 | case OSSL_OP_ENCODER: |
99 | return base_encoder; | |
100 | case OSSL_OP_DECODER: | |
101 | return base_decoder; | |
63f187cf RL |
102 | case OSSL_OP_STORE: |
103 | return base_store; | |
a3f15e23 P |
104 | } |
105 | return NULL; | |
dfc0857d P |
106 | } |
107 | ||
108 | static void base_teardown(void *provctx) | |
109 | { | |
7d6766cb P |
110 | BIO_meth_free(ossl_prov_ctx_get0_core_bio_method(provctx)); |
111 | ossl_prov_ctx_free(provctx); | |
dfc0857d P |
112 | } |
113 | ||
114 | /* Functions we provide to the core */ | |
115 | static const OSSL_DISPATCH base_dispatch_table[] = { | |
116 | { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))base_teardown }, | |
117 | { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, | |
118 | (void (*)(void))base_gettable_params }, | |
119 | { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))base_get_params }, | |
120 | { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))base_query }, | |
1e6bd31e | 121 | OSSL_DISPATCH_END |
dfc0857d P |
122 | }; |
123 | ||
124 | OSSL_provider_init_fn ossl_base_provider_init; | |
125 | ||
126 | int ossl_base_provider_init(const OSSL_CORE_HANDLE *handle, | |
127 | const OSSL_DISPATCH *in, const OSSL_DISPATCH **out, | |
128 | void **provctx) | |
129 | { | |
a829b735 | 130 | OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL; |
dfc0857d P |
131 | BIO_METHOD *corebiometh; |
132 | ||
133 | if (!ossl_prov_bio_from_dispatch(in)) | |
134 | return 0; | |
135 | for (; in->function_id != 0; in++) { | |
136 | switch (in->function_id) { | |
137 | case OSSL_FUNC_CORE_GETTABLE_PARAMS: | |
138 | c_gettable_params = OSSL_FUNC_core_gettable_params(in); | |
139 | break; | |
140 | case OSSL_FUNC_CORE_GET_PARAMS: | |
141 | c_get_params = OSSL_FUNC_core_get_params(in); | |
142 | break; | |
a829b735 DMSP |
143 | case OSSL_FUNC_CORE_GET_LIBCTX: |
144 | c_get_libctx = OSSL_FUNC_core_get_libctx(in); | |
dfc0857d P |
145 | break; |
146 | default: | |
147 | /* Just ignore anything we don't understand */ | |
148 | break; | |
149 | } | |
150 | } | |
151 | ||
152 | if (c_get_libctx == NULL) | |
153 | return 0; | |
154 | ||
155 | /* | |
156 | * We want to make sure that all calls from this provider that requires | |
157 | * a library context use the same context as the one used to call our | |
158 | * functions. We do that by passing it along in the provider context. | |
159 | * | |
160 | * This only works for built-in providers. Most providers should | |
161 | * create their own library context. | |
162 | */ | |
7d6766cb | 163 | if ((*provctx = ossl_prov_ctx_new()) == NULL |
9500c823 | 164 | || (corebiometh = ossl_bio_prov_init_bio_method()) == NULL) { |
7d6766cb | 165 | ossl_prov_ctx_free(*provctx); |
dfc0857d P |
166 | *provctx = NULL; |
167 | return 0; | |
168 | } | |
a829b735 | 169 | ossl_prov_ctx_set0_libctx(*provctx, |
b4250010 | 170 | (OSSL_LIB_CTX *)c_get_libctx(handle)); |
7d6766cb P |
171 | ossl_prov_ctx_set0_handle(*provctx, handle); |
172 | ossl_prov_ctx_set0_core_bio_method(*provctx, corebiometh); | |
dfc0857d P |
173 | |
174 | *out = base_dispatch_table; | |
175 | ||
176 | return 1; | |
177 | } |