]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_prov.c
tcl_tests: ca.try: Ignore openssl crl exit status for 'corrupted CRL' test
[openssl-gost/engine.git] / gost_prov.c
1 /**********************************************************************
2  *                 gost_prov.c - The provider itself                  *
3  *                                                                    *
4  *      Copyright (c) 2021 Richard Levitte <richard@levitte.org>      *
5  *     This file is distributed under the same license as OpenSSL     *
6  *                                                                    *
7  *                Requires OpenSSL 3.0 for compilation                *
8  **********************************************************************/
9
10 #include <openssl/core_dispatch.h>
11 #include <openssl/core_names.h>
12 #include "gost_prov.h"
13 #include "gost_lcl.h"
14 #include "prov/err.h"           /* libprov err functions */
15
16 /*********************************************************************
17  *
18  *  Errors
19  *
20  *****/
21
22 /*
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.
28  */
29 static struct proverr_functions_st *err_handle;
30 #define GOST_PROV
31 #include "e_gost_err.c"
32 void ERR_GOST_error(int function, int reason, char *file, int line)
33 {
34     proverr_new_error(err_handle);
35     proverr_set_error_debug(err_handle, file, line, NULL);
36     proverr_set_error(err_handle, reason, NULL);
37 }
38
39 /*********************************************************************
40  *
41  *  Provider context
42  *
43  *****/
44
45 static void provider_ctx_free(PROV_CTX *ctx)
46 {
47     if (ctx != NULL) {
48         ENGINE_free(ctx->e);
49         proverr_free_handle(ctx->proverr_handle);
50         OSSL_LIB_CTX_free(ctx->libctx);
51     }
52     OPENSSL_free(ctx);
53 }
54
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)
58 {
59     PROV_CTX *ctx;
60
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;
67
68         /* Ugly hack */
69         err_handle = ctx->proverr_handle;
70     } else {
71         provider_ctx_free(ctx);
72         ctx = NULL;
73     }
74     return ctx;
75 }
76
77 /*********************************************************************
78  *
79  *  Setup
80  *
81  *****/
82
83 typedef void (*fptr_t)(void);
84
85 /* The function that returns the appropriate algorithm table per operation */
86 static const OSSL_ALGORITHM *gost_operation(void *vprovctx,
87                                                 int operation_id,
88                                                 const int *no_cache)
89 {
90     switch (operation_id) {
91     case OSSL_OP_CIPHER:
92         return GOST_prov_ciphers;
93     case OSSL_OP_DIGEST:
94         return GOST_prov_digests;
95     case OSSL_OP_MAC:
96         return GOST_prov_macs;
97     }
98     return NULL;
99 }
100
101 static int gost_get_params(void *provctx, OSSL_PARAM *params)
102 {
103     OSSL_PARAM *p;
104
105     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
106     if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL GOST Provider"))
107         return 0;
108     p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_STATUS);
109     if (p != NULL && !OSSL_PARAM_set_int(p, 1)) /* We never fail. */
110         return 0;
111
112     return 1;
113 }
114
115 static const OSSL_ITEM *gost_get_reason_strings(void *provctx)
116 {
117 #if 0
118     return reason_strings;
119 #endif
120     return (OSSL_ITEM *)GOST_str_reasons;
121 }
122
123 /* The function that tears down this provider */
124 static void gost_teardown(void *vprovctx)
125 {
126     GOST_prov_deinit_ciphers();
127     GOST_prov_deinit_digests();
128     GOST_prov_deinit_mac_digests();
129     provider_ctx_free(vprovctx);
130 }
131
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 },
138     { 0, NULL }
139 };
140
141 struct prov_ctx_st {
142     void *core_handle;
143     struct proverr_functions_st *err_handle;
144 };
145
146 #ifdef BUILDING_PROVIDER_AS_LIBRARY
147 /*
148  * This allows the provider to be built in library form.  In this case, the
149  * application must add it explicitly like this:
150  *
151  * OSSL_PROVIDER_add_builtin(NULL, "gost", GOST_provider_init);
152  */
153 # define OSSL_provider_init GOST_provider_init
154 #endif
155
156 OPENSSL_EXPORT
157 int OSSL_provider_init(const OSSL_CORE_HANDLE *core,
158                        const OSSL_DISPATCH *in,
159                        const OSSL_DISPATCH **out,
160                        void **vprovctx)
161 {
162     if ((*vprovctx = provider_ctx_new(core, in)) == NULL)
163         return 0;
164     *out = provider_functions;
165     return 1;
166 }