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