Rename some occurrences of 'library_context' and 'lib_ctx' to 'libctx'
[openssl.git] / providers / baseprov.c
1 /*
2  * Copyright 2020 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 "internal/nelem.h"
23
24 /*
25  * Forward declarations to ensure that interface functions are correctly
26  * defined.
27  */
28 static OSSL_FUNC_provider_gettable_params_fn base_gettable_params;
29 static OSSL_FUNC_provider_get_params_fn base_get_params;
30 static OSSL_FUNC_provider_query_operation_fn base_query;
31
32 /* Functions provided by the core */
33 static OSSL_FUNC_core_gettable_params_fn *c_gettable_params = NULL;
34 static OSSL_FUNC_core_get_params_fn *c_get_params = NULL;
35
36 /* Parameters we provide to the core */
37 static 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
45 static const OSSL_PARAM *base_gettable_params(void *provctx)
46 {
47     return base_param_types;
48 }
49
50 static 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
71 static const OSSL_ALGORITHM base_encoder[] = {
72 #define ENCODER(name, _fips, _output, func_table)                           \
73     { name,                                                                 \
74       "provider=base,fips=" _fips ",output=" _output,                       \
75       (func_table) }
76
77 #include "encoders.inc"
78     { NULL, NULL, NULL }
79 };
80 #undef ENCODER
81
82 static const OSSL_ALGORITHM base_decoder[] = {
83 #define DECODER(name, _fips, _input, func_table)                            \
84     { name,                                                                 \
85       "provider=base,fips=" _fips ",input=" _input,                         \
86       (func_table) }
87
88 #include "decoders.inc"
89     { NULL, NULL, NULL }
90 };
91 #undef DECODER
92
93 static const OSSL_ALGORITHM base_store[] = {
94 #define STORE(name, _fips, func_table)                           \
95     { name, "provider=base,fips=" _fips, (func_table) },
96
97 #include "stores.inc"
98     { NULL, NULL, NULL }
99 #undef STORE
100 };
101
102 static const OSSL_ALGORITHM *base_query(void *provctx, int operation_id,
103                                          int *no_cache)
104 {
105     *no_cache = 0;
106     switch (operation_id) {
107     case OSSL_OP_ENCODER:
108         return base_encoder;
109     case OSSL_OP_DECODER:
110         return base_decoder;
111     case OSSL_OP_STORE:
112         return base_store;
113     }
114     return NULL;
115 }
116
117 static void base_teardown(void *provctx)
118 {
119     BIO_meth_free(ossl_prov_ctx_get0_core_bio_method(provctx));
120     ossl_prov_ctx_free(provctx);
121 }
122
123 /* Functions we provide to the core */
124 static const OSSL_DISPATCH base_dispatch_table[] = {
125     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))base_teardown },
126     { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS,
127       (void (*)(void))base_gettable_params },
128     { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))base_get_params },
129     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))base_query },
130     { 0, NULL }
131 };
132
133 OSSL_provider_init_fn ossl_base_provider_init;
134
135 int ossl_base_provider_init(const OSSL_CORE_HANDLE *handle,
136                             const OSSL_DISPATCH *in, const OSSL_DISPATCH **out,
137                             void **provctx)
138 {
139     OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
140     BIO_METHOD *corebiometh;
141
142     if (!ossl_prov_bio_from_dispatch(in))
143         return 0;
144     for (; in->function_id != 0; in++) {
145         switch (in->function_id) {
146         case OSSL_FUNC_CORE_GETTABLE_PARAMS:
147             c_gettable_params = OSSL_FUNC_core_gettable_params(in);
148             break;
149         case OSSL_FUNC_CORE_GET_PARAMS:
150             c_get_params = OSSL_FUNC_core_get_params(in);
151             break;
152         case OSSL_FUNC_CORE_GET_LIBCTX:
153             c_get_libctx = OSSL_FUNC_core_get_libctx(in);
154             break;
155         default:
156             /* Just ignore anything we don't understand */
157             break;
158         }
159     }
160
161     if (c_get_libctx == NULL)
162         return 0;
163
164     /*
165      * We want to make sure that all calls from this provider that requires
166      * a library context use the same context as the one used to call our
167      * functions.  We do that by passing it along in the provider context.
168      *
169      * This only works for built-in providers.  Most providers should
170      * create their own library context.
171      */
172     if ((*provctx = ossl_prov_ctx_new()) == NULL
173             || (corebiometh = bio_prov_init_bio_method()) == NULL) {
174         ossl_prov_ctx_free(*provctx);
175         *provctx = NULL;
176         return 0;
177     }
178     ossl_prov_ctx_set0_libctx(*provctx,
179                                        (OSSL_LIB_CTX *)c_get_libctx(handle));
180     ossl_prov_ctx_set0_handle(*provctx, handle);
181     ossl_prov_ctx_set0_core_bio_method(*provctx, corebiometh);
182
183     *out = base_dispatch_table;
184
185     return 1;
186 }