VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.7/providers/baseprov.c@ 107935

Last change on this file since 107935 was 104078, checked in by vboxsync, 11 months ago

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette