1 /**********************************************************************
2 * gost_prov.c - The provider itself *
4 * Copyright (c) 2021 Richard Levitte <richard@levitte.org> *
5 * This file is distributed under the same license as OpenSSL *
7 * Requires OpenSSL 3.0 for compilation *
8 **********************************************************************/
10 #include <openssl/core_dispatch.h>
11 #include <openssl/core_names.h>
12 #include "gost_prov.h"
14 #include "prov/err.h" /* libprov err functions */
16 /*********************************************************************
23 * Ugly hack, to get the errors generated by mkerr.pl. This should ideally
24 * be replaced with a local OSSL_ITEM list of < number, string > pairs as
25 * reason strings, but for now, we will simply use GOST_str_reasons.
26 * Fortunately, the ERR_STRING_DATA structure is compatible with OSSL_ITEM,
27 * so we can return it directly.
29 static struct proverr_functions_st *err_handle;
31 #include "e_gost_err.c"
32 void ERR_GOST_error(int function, int reason, char *file, int line)
34 proverr_new_error(err_handle);
35 proverr_set_error_debug(err_handle, file, line, NULL);
36 proverr_set_error(err_handle, reason, NULL);
39 /*********************************************************************
45 static void provider_ctx_free(PROV_CTX *ctx)
49 proverr_free_handle(ctx->proverr_handle);
50 OSSL_LIB_CTX_free(ctx->libctx);
55 extern int populate_gost_engine(ENGINE *e);
56 static PROV_CTX *provider_ctx_new(const OSSL_CORE_HANDLE *core,
57 const OSSL_DISPATCH *in)
61 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL
62 && (ctx->proverr_handle = proverr_new_handle(core, in)) != NULL
63 && (ctx->libctx = OSSL_LIB_CTX_new()) != NULL
64 && (ctx->e = ENGINE_new()) != NULL
65 && populate_gost_engine(ctx->e)) {
66 ctx->core_handle = core;
69 err_handle = ctx->proverr_handle;
71 provider_ctx_free(ctx);
77 /*********************************************************************
83 typedef void (*fptr_t)(void);
85 /* The function that returns the appropriate algorithm table per operation */
86 static const OSSL_ALGORITHM *gost_operation(void *vprovctx,
90 switch (operation_id) {
92 return GOST_prov_ciphers;
94 return GOST_prov_digests;
96 return GOST_prov_macs;
101 static int gost_get_params(void *provctx, OSSL_PARAM *params)
105 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
106 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL GOST Provider"))
108 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
109 if (p != NULL && !OSSL_PARAM_set_int(p, 1)) /* We never fail. */
115 static const OSSL_ITEM *gost_get_reason_strings(void *provctx)
118 return reason_strings;
120 return (OSSL_ITEM *)GOST_str_reasons;
123 /* The function that tears down this provider */
124 static void gost_teardown(void *vprovctx)
126 GOST_prov_deinit_ciphers();
127 GOST_prov_deinit_digests();
128 GOST_prov_deinit_mac_digests();
129 provider_ctx_free(vprovctx);
132 /* The base dispatch table */
133 static const OSSL_DISPATCH provider_functions[] = {
134 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (fptr_t)gost_operation },
135 { OSSL_FUNC_PROVIDER_GET_REASON_STRINGS, (fptr_t)gost_get_reason_strings },
136 { OSSL_FUNC_PROVIDER_GET_PARAMS, (fptr_t)gost_get_params },
137 { OSSL_FUNC_PROVIDER_TEARDOWN, (fptr_t)gost_teardown },
143 struct proverr_functions_st *err_handle;
146 #ifdef BUILDING_PROVIDER_AS_LIBRARY
148 * This allows the provider to be built in library form. In this case, the
149 * application must add it explicitly like this:
151 * OSSL_PROVIDER_add_builtin(NULL, "gost", GOST_provider_init);
153 # define OSSL_provider_init GOST_provider_init
157 int OSSL_provider_init(const OSSL_CORE_HANDLE *core,
158 const OSSL_DISPATCH *in,
159 const OSSL_DISPATCH **out,
162 if ((*vprovctx = provider_ctx_new(core, in)) == NULL)
164 *out = provider_functions;