]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_eng.c
Fix missing cleanup of GOST specific error messages
[openssl-gost/engine.git] / gost_eng.c
1 /**********************************************************************
2  *                          gost_eng.c                                *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *              Main file of GOST engine                              *
7  *       for OpenSSL                                                  *
8  *          Requires OpenSSL 0.9.9 for compilation                    *
9  **********************************************************************/
10 #include <string.h>
11 #include <openssl/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/evp.h>
14 #include <openssl/engine.h>
15 #include <openssl/obj_mac.h>
16 #include "e_gost_err.h"
17 #include "gost_lcl.h"
18 static const char *engine_gost_id = "gost";
19 static const char *engine_gost_name =
20     "Reference implementation of GOST engine";
21
22 /* Symmetric cipher and digest function registrar */
23
24 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
25                         const int **nids, int nid);
26
27 static int gost_digests(ENGINE *e, const EVP_MD **digest,
28                         const int **nids, int ind);
29
30 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
31                            const int **nids, int nid);
32
33 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
34                                 const int **nids, int nid);
35
36 static int gost_cipher_nids[] = {
37     NID_id_Gost28147_89,
38     NID_gost89_cnt,
39     NID_gost89_cnt_12,
40     NID_gost89_cbc,
41     0
42 };
43
44 static int gost_digest_nids[] = {
45     NID_id_GostR3411_94,
46     NID_id_Gost28147_89_MAC,
47     NID_id_GostR3411_2012_256,
48     NID_id_GostR3411_2012_512,
49     NID_gost_mac_12,
50     0
51 };
52
53 static int gost_pkey_meth_nids[] = {
54     NID_id_GostR3410_2001,
55     NID_id_Gost28147_89_MAC,
56     NID_id_GostR3410_2012_256,
57     NID_id_GostR3410_2012_512,
58     NID_gost_mac_12,
59     0
60 };
61
62 static EVP_PKEY_METHOD *pmeth_GostR3410_2001 = NULL,
63     *pmeth_GostR3410_2012_256 = NULL,
64     *pmeth_GostR3410_2012_512 = NULL,
65     *pmeth_Gost28147_MAC = NULL, *pmeth_Gost28147_MAC_12 = NULL;
66
67 static EVP_PKEY_ASN1_METHOD *ameth_GostR3410_2001 = NULL,
68     *ameth_GostR3410_2012_256 = NULL,
69     *ameth_GostR3410_2012_512 = NULL,
70     *ameth_Gost28147_MAC = NULL, *ameth_Gost28147_MAC_12 = NULL;
71
72 static int gost_engine_init(ENGINE *e)
73 {
74     return 1;
75 }
76
77 static int gost_engine_finish(ENGINE *e)
78 {
79     return 1;
80 }
81
82 static int gost_engine_destroy(ENGINE *e)
83 {
84     gost_param_free();
85
86     pmeth_GostR3410_2001 = NULL;
87     pmeth_Gost28147_MAC = NULL;
88     pmeth_GostR3410_2012_256 = NULL;
89     pmeth_GostR3410_2012_512 = NULL;
90     pmeth_Gost28147_MAC_12 = NULL;
91
92     ameth_GostR3410_2001 = NULL;
93     ameth_Gost28147_MAC = NULL;
94     ameth_GostR3410_2012_256 = NULL;
95     ameth_GostR3410_2012_512 = NULL;
96     ameth_Gost28147_MAC_12 = NULL;
97
98         ERR_unload_GOST_strings();
99         
100     return 1;
101 }
102
103 static int bind_gost(ENGINE *e, const char *id)
104 {
105     int ret = 0;
106     if (id && strcmp(id, engine_gost_id))
107         return 0;
108     if (ameth_GostR3410_2001) {
109         printf("GOST engine already loaded\n");
110         goto end;
111     }
112     if (!ENGINE_set_id(e, engine_gost_id)) {
113         printf("ENGINE_set_id failed\n");
114         goto end;
115     }
116     if (!ENGINE_set_name(e, engine_gost_name)) {
117         printf("ENGINE_set_name failed\n");
118         goto end;
119     }
120     if (!ENGINE_set_digests(e, gost_digests)) {
121         printf("ENGINE_set_digests failed\n");
122         goto end;
123     }
124     if (!ENGINE_set_ciphers(e, gost_ciphers)) {
125         printf("ENGINE_set_ciphers failed\n");
126         goto end;
127     }
128     if (!ENGINE_set_pkey_meths(e, gost_pkey_meths)) {
129         printf("ENGINE_set_pkey_meths failed\n");
130         goto end;
131     }
132     if (!ENGINE_set_pkey_asn1_meths(e, gost_pkey_asn1_meths)) {
133         printf("ENGINE_set_pkey_asn1_meths failed\n");
134         goto end;
135     }
136     /* Control function and commands */
137     if (!ENGINE_set_cmd_defns(e, gost_cmds)) {
138         fprintf(stderr, "ENGINE_set_cmd_defns failed\n");
139         goto end;
140     }
141     if (!ENGINE_set_ctrl_function(e, gost_control_func)) {
142         fprintf(stderr, "ENGINE_set_ctrl_func failed\n");
143         goto end;
144     }
145     if (!ENGINE_set_destroy_function(e, gost_engine_destroy)
146         || !ENGINE_set_init_function(e, gost_engine_init)
147         || !ENGINE_set_finish_function(e, gost_engine_finish)) {
148         goto end;
149     }
150
151     if (!register_ameth_gost
152         (NID_id_GostR3410_2001, &ameth_GostR3410_2001, "GOST2001",
153          "GOST R 34.10-2001"))
154         goto end;
155     if (!register_ameth_gost
156         (NID_id_GostR3410_2012_256, &ameth_GostR3410_2012_256, "GOST2012_256",
157          "GOST R 34.10-2012 with 256 bit key"))
158         goto end;
159     if (!register_ameth_gost
160         (NID_id_GostR3410_2012_512, &ameth_GostR3410_2012_512, "GOST2012_512",
161          "GOST R 34.10-2012 with 512 bit key"))
162         goto end;
163     if (!register_ameth_gost(NID_id_Gost28147_89_MAC, &ameth_Gost28147_MAC,
164                              "GOST-MAC", "GOST 28147-89 MAC"))
165         goto end;
166     if (!register_ameth_gost(NID_gost_mac_12, &ameth_Gost28147_MAC_12,
167                              "GOST-MAC-12",
168                              "GOST 28147-89 MAC with 2012 params"))
169         goto end;
170
171     if (!register_pmeth_gost(NID_id_GostR3410_2001, &pmeth_GostR3410_2001, 0))
172         goto end;
173
174     if (!register_pmeth_gost
175         (NID_id_GostR3410_2012_256, &pmeth_GostR3410_2012_256, 0))
176         goto end;
177     if (!register_pmeth_gost
178         (NID_id_GostR3410_2012_512, &pmeth_GostR3410_2012_512, 0))
179         goto end;
180     if (!register_pmeth_gost
181         (NID_id_Gost28147_89_MAC, &pmeth_Gost28147_MAC, 0))
182         goto end;
183     if (!register_pmeth_gost(NID_gost_mac_12, &pmeth_Gost28147_MAC_12, 0))
184         goto end;
185     if (!ENGINE_register_ciphers(e)
186         || !ENGINE_register_digests(e)
187         || !ENGINE_register_pkey_meths(e)
188         /* These two actually should go in LIST_ADD command */
189         || !EVP_add_cipher(&cipher_gost)
190         || !EVP_add_cipher(&cipher_gost_cbc)
191         || !EVP_add_cipher(&cipher_gost_cpacnt)
192         || !EVP_add_cipher(&cipher_gost_cpcnt_12)
193         || !EVP_add_digest(&digest_gost)
194         || !EVP_add_digest(&digest_gost2012_512)
195         || !EVP_add_digest(&digest_gost2012_256)
196         || !EVP_add_digest(&imit_gost_cpa)
197         || !EVP_add_digest(&imit_gost_cp_12)
198         ) {
199         goto end;
200     }
201
202     ERR_load_GOST_strings();
203     ret = 1;
204  end:
205     return ret;
206 }
207
208 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
209 IMPLEMENT_DYNAMIC_BIND_FN(bind_gost)
210     IMPLEMENT_DYNAMIC_CHECK_FN()
211 #endif                          /* ndef OPENSSL_NO_DYNAMIC_ENGINE */
212 static int gost_digests(ENGINE *e, const EVP_MD **digest,
213                         const int **nids, int nid)
214 {
215     int ok = 1;
216     if (!digest) {
217         *nids = gost_digest_nids;
218         return sizeof(gost_digest_nids) / sizeof(gost_digest_nids[0]) - 1;
219     }
220     if (nid == NID_id_GostR3411_94) {
221         *digest = &digest_gost;
222     } else if (nid == NID_id_GostR3411_2012_256) {
223         *digest = &digest_gost2012_256;
224     } else if (nid == NID_id_GostR3411_2012_512) {
225         *digest = &digest_gost2012_512;
226     } else if (nid == NID_id_Gost28147_89_MAC) {
227         *digest = &imit_gost_cpa;
228     } else if (nid == NID_gost_mac_12) {
229         *digest = &imit_gost_cp_12;
230     } else {
231         ok = 0;
232         *digest = NULL;
233     }
234     return ok;
235 }
236
237 static int gost_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
238                         const int **nids, int nid)
239 {
240     int ok = 1;
241     if (!cipher) {
242         *nids = gost_cipher_nids;
243         return sizeof(gost_cipher_nids) / sizeof(gost_cipher_nids[0]) - 1;
244     }
245
246     if (nid == NID_id_Gost28147_89) {
247         *cipher = &cipher_gost;
248     } else if (nid == NID_gost89_cnt) {
249         *cipher = &cipher_gost_cpacnt;
250     } else if (nid == NID_gost89_cnt_12) {
251         *cipher = &cipher_gost_cpcnt_12;
252     } else if (nid == NID_gost89_cbc) {
253         *cipher = &cipher_gost_cbc;
254     } else {
255         ok = 0;
256         *cipher = NULL;
257     }
258     return ok;
259 }
260
261 static int gost_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
262                            const int **nids, int nid)
263 {
264     if (!pmeth) {
265         *nids = gost_pkey_meth_nids;
266         return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) -
267             1;
268     }
269
270     switch (nid) {
271     case NID_id_GostR3410_2001:
272         *pmeth = pmeth_GostR3410_2001;
273         return 1;
274     case NID_id_GostR3410_2012_256:
275         *pmeth = pmeth_GostR3410_2012_256;
276         return 1;
277     case NID_id_GostR3410_2012_512:
278         *pmeth = pmeth_GostR3410_2012_512;
279         return 1;
280     case NID_id_Gost28147_89_MAC:
281         *pmeth = pmeth_Gost28147_MAC;
282         return 1;
283     case NID_gost_mac_12:
284         *pmeth = pmeth_Gost28147_MAC_12;
285         return 1;
286
287     default:;
288     }
289
290     *pmeth = NULL;
291     return 0;
292 }
293
294 static int gost_pkey_asn1_meths(ENGINE *e, EVP_PKEY_ASN1_METHOD **ameth,
295                                 const int **nids, int nid)
296 {
297     if (!ameth) {
298         *nids = gost_pkey_meth_nids;
299         return sizeof(gost_pkey_meth_nids) / sizeof(gost_pkey_meth_nids[0]) -
300             1;
301     }
302     switch (nid) {
303     case NID_id_GostR3410_2001:
304         *ameth = ameth_GostR3410_2001;
305         return 1;
306     case NID_id_GostR3410_2012_256:
307         *ameth = ameth_GostR3410_2012_256;
308         return 1;
309     case NID_id_GostR3410_2012_512:
310         *ameth = ameth_GostR3410_2012_512;
311         return 1;
312     case NID_id_Gost28147_89_MAC:
313         *ameth = ameth_Gost28147_MAC;
314         return 1;
315     case NID_gost_mac_12:
316         *ameth = ameth_Gost28147_MAC_12;
317         return 1;
318
319     default:;
320     }
321
322     *ameth = NULL;
323     return 0;
324 }
325
326 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
327 static ENGINE *engine_gost(void)
328 {
329     ENGINE *ret = ENGINE_new();
330     if (!ret)
331         return NULL;
332     if (!bind_gost(ret, engine_gost_id)) {
333         ENGINE_free(ret);
334         return NULL;
335     }
336     return ret;
337 }
338
339 void ENGINE_load_gost(void)
340 {
341     ENGINE *toadd;
342     if (pmeth_GostR3410_2001)
343         return;
344     toadd = engine_gost();
345     if (!toadd)
346         return;
347     ENGINE_add(toadd);
348     ENGINE_free(toadd);
349     ERR_clear_error();
350 }
351 #endif