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