/src/openssl/providers/common/provider_ctx.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2020-2025 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 <stdlib.h> |
11 | | #include <string.h> |
12 | | #include "prov/provider_ctx.h" |
13 | | #include "prov/bio.h" |
14 | | |
15 | | PROV_CTX *ossl_prov_ctx_new(void) |
16 | 1 | { |
17 | 1 | return OPENSSL_zalloc(sizeof(PROV_CTX)); |
18 | 1 | } |
19 | | |
20 | | void ossl_prov_ctx_free(PROV_CTX *ctx) |
21 | 1 | { |
22 | 1 | OPENSSL_free(ctx); |
23 | 1 | } |
24 | | |
25 | | void ossl_prov_ctx_set0_libctx(PROV_CTX *ctx, OSSL_LIB_CTX *libctx) |
26 | 1 | { |
27 | 1 | if (ctx != NULL) |
28 | 1 | ctx->libctx = libctx; |
29 | 1 | } |
30 | | |
31 | | void ossl_prov_ctx_set0_handle(PROV_CTX *ctx, const OSSL_CORE_HANDLE *handle) |
32 | 1 | { |
33 | 1 | if (ctx != NULL) |
34 | 1 | ctx->handle = handle; |
35 | 1 | } |
36 | | |
37 | | void ossl_prov_ctx_set0_core_bio_method(PROV_CTX *ctx, BIO_METHOD *corebiometh) |
38 | 1 | { |
39 | 1 | if (ctx != NULL) |
40 | 1 | ctx->corebiometh = corebiometh; |
41 | 1 | } |
42 | | |
43 | | void |
44 | | ossl_prov_ctx_set0_core_get_params(PROV_CTX *ctx, |
45 | | OSSL_FUNC_core_get_params_fn *c_get_params) |
46 | 1 | { |
47 | 1 | if (ctx != NULL) |
48 | 1 | ctx->core_get_params = c_get_params; |
49 | 1 | } |
50 | | |
51 | | OSSL_LIB_CTX *ossl_prov_ctx_get0_libctx(PROV_CTX *ctx) |
52 | 1.15k | { |
53 | 1.15k | if (ctx == NULL) |
54 | 0 | return NULL; |
55 | 1.15k | return ctx->libctx; |
56 | 1.15k | } |
57 | | |
58 | | const OSSL_CORE_HANDLE *ossl_prov_ctx_get0_handle(PROV_CTX *ctx) |
59 | 0 | { |
60 | 0 | if (ctx == NULL) |
61 | 0 | return NULL; |
62 | 0 | return ctx->handle; |
63 | 0 | } |
64 | | |
65 | | BIO_METHOD *ossl_prov_ctx_get0_core_bio_method(PROV_CTX *ctx) |
66 | 1 | { |
67 | 1 | if (ctx == NULL) |
68 | 0 | return NULL; |
69 | 1 | return ctx->corebiometh; |
70 | 1 | } |
71 | | |
72 | | OSSL_FUNC_core_get_params_fn *ossl_prov_ctx_get0_core_get_params(PROV_CTX *ctx) |
73 | 0 | { |
74 | 0 | if (ctx == NULL) |
75 | 0 | return NULL; |
76 | 0 | return ctx->core_get_params; |
77 | 0 | } |
78 | | |
79 | | const char * |
80 | | ossl_prov_ctx_get_param(PROV_CTX *ctx, const char *name, const char *defval) |
81 | 0 | { |
82 | 0 | char *val = NULL; |
83 | 0 | OSSL_PARAM param[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
84 | |
|
85 | 0 | if (ctx == NULL |
86 | 0 | || ctx->handle == NULL |
87 | 0 | || ctx->core_get_params == NULL) |
88 | 0 | return defval; |
89 | | |
90 | 0 | param[0].key = (char *) name; |
91 | 0 | param[0].data_type = OSSL_PARAM_UTF8_PTR; |
92 | 0 | param[0].data = (void *) &val; |
93 | 0 | param[0].data_size = sizeof(val); |
94 | 0 | param[0].return_size = OSSL_PARAM_UNMODIFIED; |
95 | | |
96 | | /* Errors are ignored, returning the default value */ |
97 | 0 | if (ctx->core_get_params(ctx->handle, param) |
98 | 0 | && OSSL_PARAM_modified(param) |
99 | 0 | && val != NULL) |
100 | 0 | return val; |
101 | 0 | return defval; |
102 | 0 | } |
103 | | |
104 | | int ossl_prov_ctx_get_bool_param(PROV_CTX *ctx, const char *name, int defval) |
105 | 0 | { |
106 | 0 | const char *val = ossl_prov_ctx_get_param(ctx, name, NULL); |
107 | |
|
108 | 0 | if (val != NULL) { |
109 | 0 | if ((strcmp(val, "1") == 0) |
110 | 0 | || (OPENSSL_strcasecmp(val, "yes") == 0) |
111 | 0 | || (OPENSSL_strcasecmp(val, "true") == 0) |
112 | 0 | || (OPENSSL_strcasecmp(val, "on") == 0)) |
113 | 0 | return 1; |
114 | 0 | else if ((strcmp(val, "0") == 0) |
115 | 0 | || (OPENSSL_strcasecmp(val, "no") == 0) |
116 | 0 | || (OPENSSL_strcasecmp(val, "false") == 0) |
117 | 0 | || (OPENSSL_strcasecmp(val, "off") == 0)) |
118 | 0 | return 0; |
119 | 0 | } |
120 | 0 | return defval; |
121 | 0 | } |