]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/baseprov.c
Rename OSSL_SERIALIZER / OSSL_DESERIALIZER to OSSL_ENCODE / OSSL_DECODE
[thirdparty/openssl.git] / providers / baseprov.c
CommitLineData
dfc0857d
P
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 */
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_END
42};
43
44static const OSSL_PARAM *base_gettable_params(void *provctx)
45{
46 return base_param_types;
47}
48
49static 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;
63
64 return 1;
65}
66
ece9304c
RL
67static const OSSL_ALGORITHM base_encoder[] = {
68#define ENCODER(name, fips, format, type, func_table) \
dfc0857d
P
69 { name, \
70 "provider=base,fips=" fips ",format=" format ",type=" type, \
71 (func_table) }
72
ece9304c 73#include "encoders.inc"
dfc0857d
P
74 { NULL, NULL, NULL }
75};
ece9304c 76#undef ENCODER
dfc0857d 77
ece9304c
RL
78static const OSSL_ALGORITHM base_decoder[] = {
79#define DECODER(name, fips, input, func_table) \
a3f15e23
P
80 { name, \
81 "provider=base,fips=" fips ",input=" input, \
82 (func_table) }
83
ece9304c 84#include "decoders.inc"
a3f15e23
P
85 { NULL, NULL, NULL }
86};
ece9304c 87#undef DECODER
a3f15e23 88
dfc0857d
P
89static const OSSL_ALGORITHM *base_query(void *provctx, int operation_id,
90 int *no_cache)
91{
92 *no_cache = 0;
a3f15e23 93 switch (operation_id) {
ece9304c
RL
94 case OSSL_OP_ENCODER:
95 return base_encoder;
96 case OSSL_OP_DECODER:
97 return base_decoder;
a3f15e23
P
98 }
99 return NULL;
dfc0857d
P
100}
101
102static void base_teardown(void *provctx)
103{
104 BIO_meth_free(PROV_CTX_get0_core_bio_method(provctx));
105 PROV_CTX_free(provctx);
106}
107
108/* Functions we provide to the core */
109static const OSSL_DISPATCH base_dispatch_table[] = {
110 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))base_teardown },
111 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS,
112 (void (*)(void))base_gettable_params },
113 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))base_get_params },
114 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))base_query },
115 { 0, NULL }
116};
117
118OSSL_provider_init_fn ossl_base_provider_init;
119
120int ossl_base_provider_init(const OSSL_CORE_HANDLE *handle,
121 const OSSL_DISPATCH *in, const OSSL_DISPATCH **out,
122 void **provctx)
123{
124 OSSL_FUNC_core_get_library_context_fn *c_get_libctx = NULL;
125 BIO_METHOD *corebiometh;
126
127 if (!ossl_prov_bio_from_dispatch(in))
128 return 0;
129 for (; in->function_id != 0; in++) {
130 switch (in->function_id) {
131 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
132 c_gettable_params = OSSL_FUNC_core_gettable_params(in);
133 break;
134 case OSSL_FUNC_CORE_GET_PARAMS:
135 c_get_params = OSSL_FUNC_core_get_params(in);
136 break;
137 case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
138 c_get_libctx = OSSL_FUNC_core_get_library_context(in);
139 break;
140 default:
141 /* Just ignore anything we don't understand */
142 break;
143 }
144 }
145
146 if (c_get_libctx == NULL)
147 return 0;
148
149 /*
150 * We want to make sure that all calls from this provider that requires
151 * a library context use the same context as the one used to call our
152 * functions. We do that by passing it along in the provider context.
153 *
154 * This only works for built-in providers. Most providers should
155 * create their own library context.
156 */
157 if ((*provctx = PROV_CTX_new()) == NULL
158 || (corebiometh = bio_prov_init_bio_method()) == NULL) {
159 PROV_CTX_free(*provctx);
160 *provctx = NULL;
161 return 0;
162 }
163 PROV_CTX_set0_library_context(*provctx, (OPENSSL_CTX *)c_get_libctx(handle));
164 PROV_CTX_set0_handle(*provctx, handle);
165 PROV_CTX_set0_core_bio_method(*provctx, corebiometh);
166
167 *out = base_dispatch_table;
168
169 return 1;
170}