Do CRL method init after other operations.
[openssl.git] / engines / ccgost / pmeth.c
1 /**********************************************************************
2  *                          pmeth.c                                   *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *   Implementation of RFC 4357 (GOST R 34.10) Publick key method     *
7  *       for OpenSSL                                                  *
8  *          Requires OpenSSL 0.9.9 for compilation                    *
9  **********************************************************************/
10 #include <openssl/evp.h>
11 #include <openssl/objects.h>
12 #include <openssl/ec.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include "meth.h"
17 #include "pmeth.h"
18 #include "sign.h"
19 #include "gostkeyx.h"
20 #include "paramset.h"
21 #include "tools.h"
22 #include "e_gost_err.h"
23 /*-------init, cleanup, copy - uniform for all algs  ---------------*/
24 /* Allocates new gost_pmeth_data structure and assigns it as data */
25 static int pkey_gost_init(EVP_PKEY_CTX *ctx) {
26         struct gost_pmeth_data *data;
27         data = OPENSSL_malloc(sizeof(struct gost_pmeth_data));
28         if (!data) return 0;
29         memset(data,0,sizeof(struct gost_pmeth_data));
30         EVP_PKEY_CTX_set_data(ctx,data);
31         return 1;
32 }
33 /* Copies contents of gost_pmeth_data structure */
34 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
35 {
36         struct gost_pmeth_data *dst_data,*src_data;
37         if (!pkey_gost_init(dst)) {
38                 return 0;
39         }
40         src_data = EVP_PKEY_CTX_get_data(src);
41         dst_data = EVP_PKEY_CTX_get_data(dst);
42         *dst_data = *src_data;
43         if (src_data -> eph_seckey) {
44                 dst_data ->eph_seckey = NULL;
45         }       
46         return 1;
47 }
48 /* Frees up gost_pmeth_data structure */
49 static void pkey_gost_cleanup (EVP_PKEY_CTX *ctx) {
50         struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
51         if (data->eph_seckey) EVP_PKEY_free(data->eph_seckey);
52         OPENSSL_free(data);
53 }       
54 /* --------------------- control functions  ------------------------------*/
55 static int pkey_gost_ctrl (EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
56 {
57         struct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
58         switch (type)
59         {
60                 case EVP_PKEY_CTRL_MD:
61                         {
62                                 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94) {
63                                         GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
64                                         return 0;
65                                 }
66                                 pctx->md = (EVP_MD *)p2;
67                                 return 1;
68                         }
69                         break;
70
71                 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
72                 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
73                 case EVP_PKEY_CTRL_PKCS7_SIGN:
74                 return 1; 
75
76                 case EVP_PKEY_CTRL_GOST_PARAMSET:
77                         pctx->sign_param_nid = (int)p1;
78                         pctx->crypt_param_nid= (int)p2;
79                         return 1;
80                         
81         }
82         return -2;
83 }
84
85 static int pkey_gost_ctrl94cc_str(EVP_PKEY_CTX *ctx, 
86                 const char *type, const char *value)
87 {
88         if(!strcmp(type, param_ctrl_string)) {
89                 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
90                         NID_id_GostR3410_94_CryptoPro_A_ParamSet, 
91                         (void *)NID_id_Gost28147_89_cc);
92         }
93         return -2;
94 }
95
96 static int pkey_gost_ctrl01cc_str(EVP_PKEY_CTX *ctx, 
97                 const char *type, const char *value)
98 {
99         if(!strcmp(type, param_ctrl_string)) {
100                 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
101                         NID_id_GostR3410_2001_ParamSet_cc, 
102                         (void *)
103                         NID_id_Gost28147_89_cc);
104         }
105         return -2;
106 }
107 static int pkey_gost_ctrl94_str(EVP_PKEY_CTX *ctx, 
108                 const char *type, const char *value)
109 {
110         int param_nid=0;
111         if(!strcmp(type, param_ctrl_string)) {
112                 if (!value) {
113                         return 0;
114                 }
115                 if (strlen(value) == 1) {
116                         switch(toupper(value[0])) {
117                                 case 'A':
118                                         param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
119                                         break;
120                                 case 'B':
121                                         param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet;
122                                         break;
123                                 case 'C':
124                                         param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet;
125                                         break;
126                                 case 'D':
127                                         param_nid = NID_id_GostR3410_94_CryptoPro_D_ParamSet;
128                                         break;
129                                 default:
130                                         return 0;
131                                         break;
132                         }
133                 } else if ((strlen(value) == 2) && (toupper(value[0]) == 'X')) {
134                         switch (toupper(value[1])) {
135                                 case 'A':
136                                         param_nid = NID_id_GostR3410_94_CryptoPro_XchA_ParamSet;
137                                         break;
138                                 case 'B':
139                                         param_nid = NID_id_GostR3410_94_CryptoPro_XchB_ParamSet;
140                                         break;
141                                 case 'C':
142                                         param_nid = NID_id_GostR3410_94_CryptoPro_XchC_ParamSet;
143                                         break;
144                                 default:
145                                         return 0;
146                                         break;
147                         }
148                 } else {
149                         R3410_params *p = R3410_paramset;
150                         param_nid = OBJ_txt2nid(value);
151                         if (param_nid == NID_undef) {
152                                 return 0;
153                         }
154                         for (;p->nid != NID_undef;p++) {
155                                 if (p->nid == param_nid) break;
156                         }
157                         if (p->nid == NID_undef) {
158                                 GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR,
159                                 GOST_R_INVALID_PARAMSET);
160                                 return 0;
161                         }
162                 }
163
164                 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, 
165                                 param_nid, (void *)NID_id_Gost28147_89_CryptoPro_A_ParamSet);
166         }
167         return -2;
168 }
169
170 static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx, 
171                 const char *type, const char *value)
172 {
173         int param_nid=0;
174         if(!strcmp(type, param_ctrl_string)) {
175                 if (!value) {
176                         return 0;
177                 }
178                 if (strlen(value) == 1) {
179                         switch(toupper(value[0])) {
180                                 case 'A':
181                                         param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
182                                         break;  
183                                 case 'B':
184                                         param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
185                                         break;
186                                 case 'C':
187                                         param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
188                                         break;
189                                 case '0':
190                                         param_nid = NID_id_GostR3410_2001_TestParamSet;
191                                         break;
192                                 default:
193                                         return 0;
194                                         break;
195                         }
196                 } else if ((strlen(value) == 2) && (toupper(value[0]) == 'X')) {
197                         switch (toupper(value[1])) {
198                                 case 'A':
199                                         param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
200                                         break;
201                                 case 'B':
202                                         param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
203                                         break;
204                                 default:
205                                         return 0;
206                                         break;
207                         }
208                 } else {
209                         R3410_2001_params *p = R3410_2001_paramset;
210                         param_nid = OBJ_txt2nid(value);
211                         if (param_nid == NID_undef) {
212                                 return 0;
213                         }
214                         for (;p->nid != NID_undef;p++) {
215                                 if (p->nid == param_nid) break;
216                         }
217                         if (p->nid == NID_undef) {
218                                 GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR,
219                                 GOST_R_INVALID_PARAMSET);
220                                 return 0;
221                         }
222                 }
223
224                 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, 
225                                 param_nid, (void *)NID_id_Gost28147_89_CryptoPro_A_ParamSet);
226         }
227         return -2;
228 }
229 /* --------------------- key generation  --------------------------------*/
230 /* Generates GOST 94 key and assigns it setting specified type */
231 static int pkey_gost94_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey,int type)
232 {
233         struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
234         DSA *dsa=NULL;
235         if (data->sign_param_nid == NID_undef) {
236                 if (type== NID_id_GostR3410_94_cc) {
237                         data->sign_param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
238                 } else {
239                         GOSTerr(GOST_F_PKEY_GOST94_KEYGEN,
240                         GOST_R_NO_PARAMETERS_SET);
241                         return 0;
242                 }       
243         }
244         dsa = DSA_new();
245         if (!fill_GOST94_params(dsa,data->sign_param_nid)) {
246                 DSA_free(dsa);
247                 return 0;
248         }
249         gost_sign_keygen(dsa);
250         EVP_PKEY_assign(pkey,type,dsa);
251         return 1;
252 }
253 /* Generates Gost_R3410_94_cc key */
254 static int pkey_gost94cc_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
255         return pkey_gost94_keygen(ctx,pkey,NID_id_GostR3410_94_cc);
256 }
257
258 /* Generates Gost_R3410_94_cp key */
259 static int pkey_gost94cp_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
260         return pkey_gost94_keygen(ctx,pkey,NID_id_GostR3410_94);
261 }
262 /* Generates GOST_R3410 2001 key and assigns it using specified type */
263 static int pkey_gost01_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey,int type)
264 {
265         struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
266         EC_KEY *ec=NULL;
267         if (data->sign_param_nid == NID_undef) {
268                 if (type == NID_id_GostR3410_2001_cc) {
269                         data->sign_param_nid = NID_id_GostR3410_2001_ParamSet_cc;
270                 } else {        
271                         GOSTerr(GOST_F_PKEY_GOST01_KEYGEN,
272                         GOST_R_NO_PARAMETERS_SET);
273                         return 0;
274                 }
275         }
276         ec = EC_KEY_new();
277         if (!fill_GOST2001_params(ec,data->sign_param_nid)) {
278                 EC_KEY_free(ec);
279                 return 0;
280         }
281         gost2001_keygen(ec);
282
283         EVP_PKEY_assign(pkey,type,ec);
284         return 1;
285 }
286 /* Generates GOST R3410 2001_cc key */
287 static int pkey_gost01cc_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
288         return pkey_gost01_keygen(ctx,pkey,NID_id_GostR3410_2001_cc);
289 }
290
291 /* Generates GOST R3410 2001_cp key */
292 static int pkey_gost01cp_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
293         return pkey_gost01_keygen(ctx,pkey,NID_id_GostR3410_2001);
294 }
295 /* ----------- sign callbacks --------------------------------------*/
296 static int pkey_gost94_cc_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
297         const unsigned char *tbs, size_t tbs_len)
298 {
299         DSA_SIG *unpacked_sig=NULL;
300         EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
301         if (!siglen) return 0;
302         if (!sig) 
303         {
304                 *siglen= 64; /* better to check size of pkey->pkey.dsa-q */
305                 return 1;
306         }       
307         unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
308         if (!unpacked_sig) {
309                 return 0;
310         }
311
312         return pack_sign_cc(unpacked_sig,32,sig,siglen);
313         
314         
315 }
316 static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
317         const unsigned char *tbs, size_t tbs_len)
318 {
319         DSA_SIG *unpacked_sig=NULL;
320         EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
321         if (!siglen) return 0;
322         if (!sig) 
323         {
324                 *siglen= 64; /* better to check size of pkey->pkey.dsa-q */
325                 return 1;
326         }       
327         unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
328         if (!unpacked_sig) {
329                 return 0;
330         }
331         return pack_sign_cp(unpacked_sig,32,sig,siglen);
332         
333         
334 }
335 static int pkey_gost01_cc_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
336         const unsigned char *tbs, size_t tbs_len)
337 {
338         DSA_SIG *unpacked_sig=NULL;
339         EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
340         if (!siglen) return 0;
341         if (!sig) 
342         {
343                 *siglen= 64; /* better to check size of curve order*/
344                 return 1;
345         }       
346         unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
347         if (!unpacked_sig) {
348                 return 0;
349         }
350         return pack_sign_cc(unpacked_sig,32,sig,siglen);
351 }
352 static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
353         const unsigned char *tbs, size_t tbs_len)
354 {
355         DSA_SIG *unpacked_sig=NULL;
356         EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
357         if (!siglen) return 0;
358         if (!sig) 
359         {
360                 *siglen= 64; /* better to check size of curve order*/
361                 return 1;
362         }       
363         unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
364         if (!unpacked_sig) {
365                 return 0;
366         }
367         return pack_sign_cp(unpacked_sig,32,sig,siglen);
368 }
369 /* ------------------- verify callbacks ---------------------------*/
370 static int pkey_gost94_cc_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
371         size_t siglen, const unsigned char *tbs, size_t tbs_len)
372 {
373         int ok = 0;
374         EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
375         DSA_SIG *s=unpack_cc_signature(sig,siglen);
376         if (!s) return 0;
377         if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
378         DSA_SIG_free(s);
379         return ok;
380 }
381
382 static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
383         size_t siglen, const unsigned char *tbs, size_t tbs_len)
384 {
385         int ok = 0;
386         EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
387         DSA_SIG *s=unpack_cp_signature(sig,siglen);
388         if (!s) return 0;
389         if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
390         DSA_SIG_free(s);
391         return ok;
392 }
393 static int pkey_gost01_cc_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
394         size_t siglen, const unsigned char *tbs, size_t tbs_len)
395 {
396         int ok = 0;
397         EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
398         DSA_SIG *s=unpack_cc_signature(sig,siglen);
399         fprintf(stderr,"R=");
400         BN_print_fp(stderr,s->r);
401         fprintf(stderr,"\nS=");
402         BN_print_fp(stderr,s->s);
403         fprintf(stderr,"\n");
404         if (!s) return 0;
405         if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
406         DSA_SIG_free(s);
407         return ok;
408 }
409
410 static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
411         size_t siglen, const unsigned char *tbs, size_t tbs_len)
412 {
413         int ok = 0;
414         EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
415         DSA_SIG *s=unpack_cp_signature(sig,siglen);
416         if (!s) return 0;
417         fprintf(stderr,"R=");
418         BN_print_fp(stderr,s->r);
419         fprintf(stderr,"\nS=");
420         BN_print_fp(stderr,s->s);
421         fprintf(stderr,"\n");
422         if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
423         DSA_SIG_free(s);
424         return ok;
425 }
426 /* ------------- encrypt init -------------------------------------*/
427 /* Generates ephermeral key */
428 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx) 
429 {
430         struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
431         EVP_PKEY *eph_key = EVP_PKEY_new();
432         EVP_PKEY *old_key =EVP_PKEY_CTX_get0_pkey(ctx);
433
434         if (data->eph_seckey) EVP_PKEY_free(data->eph_seckey);
435         EVP_PKEY_assign(eph_key,EVP_PKEY_base_id(old_key),NULL);
436         if (!EVP_PKEY_copy_parameters(eph_key,old_key)) return 0;
437         switch (EVP_PKEY_base_id(old_key)) {
438         case NID_id_GostR3410_2001:
439         case NID_id_GostR3410_2001_cc:
440                 gost2001_keygen(EVP_PKEY_get0(eph_key));
441                 break;
442         case NID_id_GostR3410_94:
443         case NID_id_GostR3410_94_cc:
444                 gost_sign_keygen(EVP_PKEY_get0(eph_key));
445                 break;
446         
447                         
448         }
449
450         
451         data->eph_seckey=eph_key;
452         return 1;
453 }
454 /* ----------------------------------------------------------------*/
455 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth,int flags) {
456         *pmeth = EVP_PKEY_meth_new(id, flags);
457         if (!*pmeth) return 0;
458
459         switch (id) {
460                 case NID_id_GostR3410_94_cc:
461                         EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94cc_str); 
462                         EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cc_keygen);
463                         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cc_sign);
464                         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cc_verify);
465                         EVP_PKEY_meth_set_encrypt(*pmeth, 
466                                 pkey_gost_encrypt_init, pkey_GOST94cc_encrypt);
467                         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cc_decrypt); 
468                         break;
469                 case NID_id_GostR3410_94:
470                         EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94_str);
471                         EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cp_keygen);
472                         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign);
473                         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify);
474                         EVP_PKEY_meth_set_encrypt(*pmeth, 
475                                 pkey_gost_encrypt_init, pkey_GOST94cp_encrypt);
476                         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt); 
477
478         
479                         break;
480                 case NID_id_GostR3410_2001_cc:
481                         EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01cc_str);
482                         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cc_sign);
483                         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cc_verify);
484
485                         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cc_keygen);
486
487                         EVP_PKEY_meth_set_encrypt(*pmeth, 
488                                 pkey_gost_encrypt_init, pkey_GOST01cc_encrypt);
489                         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cc_decrypt); 
490                         break;
491                         /* There is intentionally no break here */
492                 case NID_id_GostR3410_2001:
493                         EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01_str);
494                         EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign);
495                         EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify);
496
497                         EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen);
498
499                         EVP_PKEY_meth_set_encrypt(*pmeth, 
500                                 pkey_gost_encrypt_init, pkey_GOST01cp_encrypt);
501                         EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt); 
502                         break;
503                 default: //Unsupported method
504                         return 0;
505         }
506         EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
507         EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
508
509         EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
510         //FIXME derive etc...
511         
512         return 1;
513 }
514