Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/providers/baseprov.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2021 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
/* Parameters we provide to the core */
33
static const OSSL_PARAM base_param_types[] = {
34
    OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
35
    OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
36
    OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
37
    OSSL_PARAM_DEFN(OSSL_PROV_PARAM_STATUS, OSSL_PARAM_INTEGER, NULL, 0),
38
    OSSL_PARAM_END
39
};
40
41
static const OSSL_PARAM *base_gettable_params(void *provctx)
42
0
{
43
0
    return base_param_types;
44
0
}
45
46
static int base_get_params(void *provctx, OSSL_PARAM params[])
47
0
{
48
0
    OSSL_PARAM *p;
49
50
0
    p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
51
0
    if (p != NULL
52
0
        && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL Base Provider"))
53
0
        return 0;
54
0
    p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
55
0
    if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
56
0
        return 0;
57
0
    p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
58
0
    if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
59
0
        return 0;
60
0
    p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
61
0
    if (p != NULL && !OSSL_PARAM_set_int(p, ossl_prov_is_running()))
62
0
        return 0;
63
64
0
    return 1;
65
0
}
66
67
static const OSSL_ALGORITHM base_encoder[] = {
68
#define ENCODER_PROVIDER "base"
69
#include "encoders.inc"
70
    { NULL, NULL, NULL }
71
#undef ENCODER_PROVIDER
72
};
73
74
static const OSSL_ALGORITHM base_decoder[] = {
75
#define DECODER_PROVIDER "base"
76
#include "decoders.inc"
77
    { NULL, NULL, NULL }
78
#undef DECODER_PROVIDER
79
};
80
81
static const OSSL_ALGORITHM base_store[] = {
82
#define STORE(name, _fips, func_table) \
83
    { name, "provider=base,fips=" _fips, (func_table) },
84
85
#include "stores.inc"
86
    { NULL, NULL, NULL }
87
#undef STORE
88
};
89
90
static const OSSL_ALGORITHM *base_query(void *provctx, int operation_id,
91
    int *no_cache)
92
0
{
93
0
    *no_cache = 0;
94
0
    switch (operation_id) {
95
0
    case OSSL_OP_ENCODER:
96
0
        return base_encoder;
97
0
    case OSSL_OP_DECODER:
98
0
        return base_decoder;
99
0
    case OSSL_OP_STORE:
100
0
        return base_store;
101
0
    }
102
0
    return NULL;
103
0
}
104
105
static void base_teardown(void *provctx)
106
0
{
107
0
    BIO_meth_free(ossl_prov_ctx_get0_core_bio_method(provctx));
108
0
    ossl_prov_ctx_free(provctx);
109
0
}
110
111
/* Functions we provide to the core */
112
static const OSSL_DISPATCH base_dispatch_table[] = {
113
    { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))base_teardown },
114
    { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS,
115
        (void (*)(void))base_gettable_params },
116
    { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))base_get_params },
117
    { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))base_query },
118
    { 0, NULL }
119
};
120
121
OSSL_provider_init_fn ossl_base_provider_init;
122
123
int ossl_base_provider_init(const OSSL_CORE_HANDLE *handle,
124
    const OSSL_DISPATCH *in, const OSSL_DISPATCH **out,
125
    void **provctx)
126
0
{
127
0
    OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
128
0
    BIO_METHOD *corebiometh;
129
130
0
    if (!ossl_prov_bio_from_dispatch(in))
131
0
        return 0;
132
0
    for (; in->function_id != 0; in++) {
133
0
        switch (in->function_id) {
134
0
        case OSSL_FUNC_CORE_GET_LIBCTX:
135
0
            c_get_libctx = OSSL_FUNC_core_get_libctx(in);
136
0
            break;
137
0
        default:
138
            /* Just ignore anything we don't understand */
139
0
            break;
140
0
        }
141
0
    }
142
143
0
    if (c_get_libctx == NULL)
144
0
        return 0;
145
146
    /*
147
     * We want to make sure that all calls from this provider that requires
148
     * a library context use the same context as the one used to call our
149
     * functions.  We do that by passing it along in the provider context.
150
     *
151
     * This only works for built-in providers.  Most providers should
152
     * create their own library context.
153
     */
154
0
    if ((*provctx = ossl_prov_ctx_new()) == NULL
155
0
        || (corebiometh = ossl_bio_prov_init_bio_method()) == NULL) {
156
0
        ossl_prov_ctx_free(*provctx);
157
0
        *provctx = NULL;
158
0
        return 0;
159
0
    }
160
0
    ossl_prov_ctx_set0_libctx(*provctx,
161
0
        (OSSL_LIB_CTX *)c_get_libctx(handle));
162
0
    ossl_prov_ctx_set0_handle(*provctx, handle);
163
0
    ossl_prov_ctx_set0_core_bio_method(*provctx, corebiometh);
164
165
0
    *out = base_dispatch_table;
166
167
0
    return 1;
168
0
}