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