Check for NULL before use (Coverity ID 203).
[openssl.git] / engines / ccgost / gost_ameth.c
1 /**********************************************************************
2  *                          gost_ameth.c                              *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *       Implementation of RFC 4490/4491 ASN1 method                  *
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/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/asn1.h>
16 #include "gost_params.h"
17 #include "gost_lcl.h"
18 #include "e_gost_err.h"
19
20 int gost94_nid_by_params(DSA *p) 
21         {
22         R3410_params *gost_params;
23         BIGNUM *q=BN_new();
24         for (gost_params = R3410_paramset;gost_params->q!=NULL; gost_params++) 
25                 {
26                 BN_dec2bn(&q,gost_params->q);
27                 if (!BN_cmp(q,p->q)) 
28                         {
29                         BN_free(q);
30                         return gost_params->nid;
31                         }
32                 }       
33         BN_free(q);
34         return NID_undef;
35         }
36
37 static ASN1_STRING  *encode_gost_algor_params(const EVP_PKEY *key)
38         {
39         ASN1_STRING *params = ASN1_STRING_new();
40         GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
41         int pkey_param_nid = NID_undef;
42         int cipher_param_nid = NID_undef;
43         if (!params || !gkp) 
44                 {
45                 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
46                         ERR_R_MALLOC_FAILURE);
47                 ASN1_STRING_free(params);
48                 params = NULL;
49                 goto err;
50                 }       
51         switch (EVP_PKEY_base_id(key)) 
52                 {
53                 case NID_id_GostR3410_2001:
54                         pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)key)));
55                         cipher_param_nid = get_encryption_params(NULL)->nid;
56                         break;
57                 case NID_id_GostR3410_94:
58                         pkey_param_nid = (int) gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)key));
59                         if (pkey_param_nid == NID_undef) 
60                                 {
61                                 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
62                                         GOST_R_INVALID_GOST94_PARMSET);
63                                 ASN1_STRING_free(params);
64                                 params=NULL;
65                                 goto err;
66                                 }       
67                         cipher_param_nid = get_encryption_params(NULL)->nid;
68                         break;
69                 }       
70         gkp->key_params = OBJ_nid2obj(pkey_param_nid);
71         gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
72         /*gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);*/
73         params->length = i2d_GOST_KEY_PARAMS(gkp, &params->data);
74         if (params->length <=0 ) 
75                 {
76                 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
77                         ERR_R_MALLOC_FAILURE);
78                 ASN1_STRING_free(params);
79                 params = NULL;
80                 goto err;
81                 }
82         params ->type = V_ASN1_SEQUENCE;
83         err:
84         GOST_KEY_PARAMS_free(gkp);
85         return params;
86         }
87
88 /* Parses GOST algorithm parameters from X509_ALGOR and
89  * modifies pkey setting NID and parameters
90  */
91 static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg) 
92         {
93         ASN1_OBJECT *palg_obj =NULL;
94         int ptype = V_ASN1_UNDEF;
95         int pkey_nid = NID_undef,param_nid = NID_undef;
96         void *_pval;
97         ASN1_STRING *pval = NULL;
98         const unsigned char  *p;
99         GOST_KEY_PARAMS *gkp = NULL;
100
101         X509_ALGOR_get0(&palg_obj, &ptype, &_pval, palg);
102         pval = _pval;
103         if (ptype != V_ASN1_SEQUENCE) 
104                 {
105                 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
106                         GOST_R_BAD_KEY_PARAMETERS_FORMAT);
107                 return 0;
108                 }       
109         p=pval->data;
110         pkey_nid = OBJ_obj2nid(palg_obj);
111
112         gkp = d2i_GOST_KEY_PARAMS(NULL,&p,pval->length);
113         if (!gkp) 
114                 {
115                 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
116                         GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
117                 return 0;
118                 }       
119         param_nid = OBJ_obj2nid(gkp->key_params);
120         GOST_KEY_PARAMS_free(gkp);
121         EVP_PKEY_set_type(pkey,pkey_nid);
122         switch (pkey_nid) 
123                 {
124                 case NID_id_GostR3410_94:
125                 {
126                 DSA *dsa= EVP_PKEY_get0(pkey);
127                 if (!dsa) 
128                         {
129                         dsa = DSA_new();
130                         if (!EVP_PKEY_assign(pkey,pkey_nid,dsa)) return 0;
131                         }
132                 if (!fill_GOST94_params(dsa,param_nid)) return 0;
133                 break;
134                 }
135                 case NID_id_GostR3410_2001:
136                 {
137                 EC_KEY *ec = EVP_PKEY_get0(pkey);
138                 if (!ec) 
139                         {
140                         ec = EC_KEY_new();
141                         if (!EVP_PKEY_assign(pkey,pkey_nid,ec)) return 0;
142                         }
143                 if (!fill_GOST2001_params(ec,param_nid)) return 0;
144                 }
145                 }
146
147         return 1;
148         }
149
150 static int gost_set_priv_key(EVP_PKEY *pkey,BIGNUM *priv) 
151         {
152         switch (EVP_PKEY_base_id(pkey)) 
153                 {
154                 case NID_id_GostR3410_94:
155                 {
156                 DSA *dsa = EVP_PKEY_get0(pkey);
157                 if (!dsa) 
158                         {
159                         dsa = DSA_new();
160                         EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),dsa);
161                         }       
162                 dsa->priv_key = BN_dup(priv);
163                 if (!EVP_PKEY_missing_parameters(pkey)) 
164                         gost94_compute_public(dsa);
165                 break;
166                 }       
167                 case NID_id_GostR3410_2001:
168                 {
169                 EC_KEY *ec = EVP_PKEY_get0(pkey);
170                 if (!ec) 
171                         {
172                         ec = EC_KEY_new();
173                         EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),ec);
174                         }       
175                 if (!EC_KEY_set_private_key(ec,priv)) return 0;
176                 if (!EVP_PKEY_missing_parameters(pkey)) 
177                         gost2001_compute_public(ec);
178                 break;
179                 }
180                 }
181         return 1;               
182         }
183 BIGNUM* gost_get0_priv_key(const EVP_PKEY *pkey) 
184         {
185         switch (EVP_PKEY_base_id(pkey)) 
186                 {
187                 case NID_id_GostR3410_94:
188                 {
189                 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pkey);
190                 if (!dsa) 
191                         {
192                         return NULL;
193                         }       
194                 if (!dsa->priv_key) return NULL;
195                 return dsa->priv_key;
196                 break;
197                 }       
198                 case NID_id_GostR3410_2001:
199                 {
200                 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
201                 const BIGNUM* priv;
202                 if (!ec) 
203                         {
204                         return NULL;
205                         }       
206                 if (!(priv=EC_KEY_get0_private_key(ec))) return NULL;
207                 return (BIGNUM *)priv;
208                 break;
209                 }
210                 }
211         return NULL;            
212         }
213
214 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op,
215         long arg1, void *arg2)
216         {
217         switch (op)
218                 {
219                 case ASN1_PKEY_CTRL_PKCS7_SIGN:
220                         if (arg1 == 0) 
221                                 {
222                                 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
223                                 int nid = EVP_PKEY_base_id(pkey);
224                                 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO*)arg2, 
225                                         NULL, &alg1, &alg2);
226                                 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
227                                         V_ASN1_NULL, 0);
228                                 if (nid == NID_undef) 
229                                         {
230                                         return (-1);
231                                         }
232                                 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
233                                 }
234                         return 1;
235                 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
236                         if (arg1 == 0)
237                                 {
238                                 X509_ALGOR *alg;
239                                 ASN1_STRING * params = encode_gost_algor_params(pkey);
240                                 if (!params) 
241                                         {
242                                         return -1;
243                                         }
244                                 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO*)arg2, &alg);
245                                 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type),
246                                         V_ASN1_SEQUENCE, params);
247                                 }
248                         return 1;
249                 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
250                         *(int *)arg2 = NID_id_GostR3411_94;
251                         return 2;
252                 }
253         
254         return -2;
255         }
256 /*----------------------- free functions * ------------------------------*/
257 static void pkey_free_gost94(EVP_PKEY *key) 
258         {
259         if (key->pkey.dsa) 
260                 {
261                 DSA_free(key->pkey.dsa);
262                 }
263         }
264
265 static void pkey_free_gost01(EVP_PKEY *key) 
266         {
267         if (key->pkey.ec) 
268                 {
269                 EC_KEY_free(key->pkey.ec);
270                 }
271         }       
272
273 /* ------------------ private key functions  -----------------------------*/
274 static int priv_decode_gost( EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf) 
275         {
276         const unsigned char *pkey_buf = NULL,*p=NULL;
277         int priv_len = 0;
278         BIGNUM *pk_num=NULL;
279         int ret =0;
280         X509_ALGOR *palg =NULL;
281         ASN1_OBJECT *palg_obj = NULL;
282         ASN1_INTEGER *priv_key=NULL;
283
284         if (!PKCS8_pkey_get0(&palg_obj,&pkey_buf,&priv_len,&palg,p8inf)) 
285                 return 0;
286         p = pkey_buf;
287         if (!decode_gost_algor_params(pk,palg)) 
288                 {
289                 return 0;
290                 }
291         if (V_ASN1_OCTET_STRING == *p) 
292                 {
293                 /* New format - Little endian octet string */
294                 unsigned char rev_buf[32];
295                 int i;
296                 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL,&p,priv_len);
297                 if (!s||s->length !=32) 
298                         {
299                         GOSTerr(GOST_F_PRIV_DECODE_GOST,
300                                 EVP_R_DECODE_ERROR);
301                         return 0;       
302                         }
303                 for (i=0;i<32;i++)
304                         {
305                         rev_buf[31-i]=s->data[i];
306                         }
307                 ASN1_STRING_free(s);
308                 pk_num = getbnfrombuf(rev_buf,32);
309                 } 
310         else
311                 {
312                 priv_key=d2i_ASN1_INTEGER(NULL,&p,priv_len);
313                 if (!priv_key) return 0;
314                 ret= ((pk_num =  ASN1_INTEGER_to_BN(priv_key, NULL))!=NULL) ;
315                 ASN1_INTEGER_free(priv_key);
316                 if (!ret)
317                         {
318                         GOSTerr(GOST_F_PRIV_DECODE_GOST,
319                                 EVP_R_DECODE_ERROR);
320                         return 0;       
321                         }
322                 }
323
324         ret= gost_set_priv_key(pk,pk_num);
325         BN_free(pk_num);
326         return ret;
327         }
328
329 /* ----------------------------------------------------------------------*/
330 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
331         {
332         ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
333         ASN1_STRING *params = encode_gost_algor_params(pk);
334         unsigned char *priv_buf = NULL;
335         int priv_len;
336
337         ASN1_INTEGER *asn1key=NULL;
338         if (!params) 
339                 {
340                 return 0;
341                 }
342         asn1key = BN_to_ASN1_INTEGER(gost_get0_priv_key(pk),NULL);
343         priv_len = i2d_ASN1_INTEGER(asn1key,&priv_buf);
344         ASN1_INTEGER_free(asn1key);
345         return PKCS8_pkey_set0(p8,algobj,0,V_ASN1_SEQUENCE,params,
346                 priv_buf,priv_len);
347         }
348 /* --------- printing keys --------------------------------*/
349 static int print_gost_94(BIO *out, const EVP_PKEY *pkey, int indent,
350         ASN1_PCTX *pctx, int type) 
351         {
352         int param_nid = NID_undef;
353
354         if (type == 2) 
355                 {
356                 BIGNUM *key;
357
358                 if (!BIO_indent(out,indent,128)) return 0;
359                 BIO_printf(out,"Private key: ");
360                 key = gost_get0_priv_key(pkey);
361                 if (!key) 
362                         BIO_printf(out,"<undefined>");
363                 else 
364                         BN_print(out,key);
365                 BIO_printf(out,"\n");
366                 }
367         if (type >= 1)
368                 {
369                 BIGNUM *pubkey;
370                 
371                 pubkey = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
372                 BIO_indent(out,indent,128);
373                 BIO_printf(out,"Public key: ");
374                 BN_print(out,pubkey);
375                 BIO_printf(out,"\n");
376         }       
377
378         param_nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
379         BIO_indent(out,indent,128);
380         BIO_printf(out, "Parameter set: %s\n",OBJ_nid2ln(param_nid));
381         return 1;
382 }
383
384 static int param_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
385         ASN1_PCTX *pctx) 
386         {
387         return print_gost_94(out, pkey, indent, pctx,0);
388         }
389
390 static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
391         ASN1_PCTX *pctx)
392         {
393         return print_gost_94(out,pkey, indent, pctx,1);
394         }
395 static int priv_print_gost94(BIO *out,const EVP_PKEY *pkey, int indent,
396         ASN1_PCTX *pctx) 
397         {
398         return print_gost_94(out,pkey,indent,pctx,2);
399         }
400
401 static int print_gost_01(BIO *out, const EVP_PKEY *pkey, int indent,
402         ASN1_PCTX *pctx, int type)
403         {
404         int param_nid = NID_undef;
405         if (type == 2) 
406                 {
407                 BIGNUM *key;
408
409                 if (!BIO_indent(out,indent,128)) return 0;
410                 BIO_printf(out,"Private key: ");
411                 key = gost_get0_priv_key(pkey);
412                 if (!key) 
413                         BIO_printf(out,"<undefined)");
414                 else 
415                         BN_print(out,key);
416                 BIO_printf(out,"\n");
417                 }
418         if (type >= 1) 
419                 {
420                 BN_CTX *ctx = BN_CTX_new();
421                 BIGNUM *X,*Y;
422                 const EC_POINT *pubkey;
423                 const EC_GROUP *group;
424
425                 if (!ctx) 
426                         {
427                         GOSTerr(GOST_F_PRINT_GOST_01,ERR_R_MALLOC_FAILURE);
428                         return 0;
429                         }
430                 BN_CTX_start(ctx);
431                 X = BN_CTX_get(ctx);
432                 Y = BN_CTX_get(ctx);
433                 pubkey = EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
434                 group = EC_KEY_get0_group((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
435                 if (!EC_POINT_get_affine_coordinates_GFp(group,pubkey,X,Y,ctx)) 
436                         {
437                         GOSTerr(GOST_F_PRINT_GOST_01,ERR_R_EC_LIB);
438                         BN_CTX_free(ctx);
439                         return 0;
440                         }
441                 if (!BIO_indent(out,indent,128)) return 0;
442                 BIO_printf(out,"Public key:\n");
443                 if (!BIO_indent(out,indent+3,128)) return 0;
444                 BIO_printf(out,"X:");
445                 BN_print(out,X);
446                 BIO_printf(out,"\n");
447                 BIO_indent(out,indent+3,128);
448                 BIO_printf(out,"Y:");
449                 BN_print(out,Y);
450                 BIO_printf(out,"\n");
451                 BN_CTX_end(ctx);
452                 BN_CTX_free(ctx);
453                 }
454
455         param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey)));
456         if (!BIO_indent(out,indent,128)) return 0;
457         BIO_printf(out,"Parameter set: %s\n",OBJ_nid2ln(param_nid));
458         return 1;
459 }
460 static int param_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
461         ASN1_PCTX *pctx) 
462         {       
463         return print_gost_01(out,pkey,indent,pctx,0);
464         }
465 static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
466         ASN1_PCTX *pctx)
467         {
468         return print_gost_01(out,pkey, indent, pctx,1);
469         }
470 static int priv_print_gost01(BIO *out,const EVP_PKEY *pkey, int indent,
471         ASN1_PCTX *pctx) 
472         {
473         return print_gost_01(out,pkey,indent,pctx,2);
474         }
475 /* ---------------------------------------------------------------------*/
476 static int param_missing_gost94(const EVP_PKEY *pk) 
477         {
478         const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
479         if (!dsa) return 1;
480         if (!dsa->q) return 1;
481         return 0;
482         }
483
484 static int param_missing_gost01(const EVP_PKEY *pk) 
485         {
486         const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
487         if (!ec) return 1;
488         if (!EC_KEY_get0_group(ec)) return 1;
489         return 0;
490         }
491
492 static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from) 
493         {
494         const DSA *dfrom = EVP_PKEY_get0((EVP_PKEY *)from);
495         DSA *dto = EVP_PKEY_get0(to);
496         if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) 
497                 {
498                 GOSTerr(GOST_F_PARAM_COPY_GOST94,
499                         GOST_R_INCOMPATIBLE_ALGORITHMS);
500                 return 0;
501                 }       
502         if (!dfrom) 
503                 {
504                 GOSTerr(GOST_F_PARAM_COPY_GOST94,
505                         GOST_R_KEY_PARAMETERS_MISSING);
506                 return 0;
507                 }       
508         if (!dto) 
509                 {
510                 dto = DSA_new();
511                 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),dto);
512                 }       
513 #define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x);   
514         COPYBIGNUM(dto,dfrom,p)
515                 COPYBIGNUM(dto,dfrom,q)
516                 COPYBIGNUM(dto,dfrom,g)
517
518                 if (dto->priv_key) 
519                         gost94_compute_public(dto);
520         return 1;       
521         }
522 static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from) 
523         {
524         EC_KEY *eto = EVP_PKEY_get0(to);
525         const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
526         if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) 
527                 {
528                 GOSTerr(GOST_F_PARAM_COPY_GOST01,
529                         GOST_R_INCOMPATIBLE_ALGORITHMS);
530                 return 0;
531                 }       
532         if (!efrom) 
533                 {
534                 GOSTerr(GOST_F_PARAM_COPY_GOST01,
535                         GOST_R_KEY_PARAMETERS_MISSING);
536                 return 0;
537                 }       
538         if (!eto) 
539                 {
540                 eto = EC_KEY_new();
541                 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),eto);
542                 }       
543         EC_KEY_set_group(eto,EC_KEY_get0_group(efrom));
544         if (EC_KEY_get0_private_key(eto)) 
545                 {
546                 gost2001_compute_public(eto);
547                 }
548         return 1;
549         }
550
551 static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b) 
552         {
553         const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
554         const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
555         if (!BN_cmp(da->q,db->q)) return 1;
556         return 0;
557         }
558
559 static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b) 
560         {
561         if (EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)a)))==
562                 EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)b)))) 
563                 {
564                 return 1;
565                 }
566         return 0;
567
568         }
569
570 /* ---------- Public key functions * --------------------------------------*/
571 static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub)
572         {
573         X509_ALGOR *palg = NULL;
574         const unsigned char *pubkey_buf = NULL;
575         unsigned char *databuf;
576         ASN1_OBJECT *palgobj = NULL;
577         int pub_len,i,j;
578         DSA *dsa;
579         ASN1_OCTET_STRING *octet= NULL;
580
581         if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
582                         &palg, pub)) return 0;
583         EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);  
584         if (!decode_gost_algor_params(pk,palg)) return 0;
585         octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
586         if (!octet) 
587                 {
588                 GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE);
589                 return 0;
590                 }       
591         databuf = OPENSSL_malloc(octet->length);
592         for (i=0,j=octet->length-1;i<octet->length;i++,j--)
593                 {
594                 databuf[j]=octet->data[i];
595                 }       
596         dsa = EVP_PKEY_get0(pk);
597         dsa->pub_key=BN_bin2bn(databuf,octet->length,NULL);
598         ASN1_OCTET_STRING_free(octet);
599         OPENSSL_free(databuf);
600         return 1;
601
602         }
603
604 static int pub_encode_gost94(X509_PUBKEY *pub,const EVP_PKEY *pk)
605         {
606         ASN1_OBJECT *algobj = NULL;
607         ASN1_OCTET_STRING *octet = NULL;
608         void *pval = NULL;
609         unsigned char *buf=NULL,*databuf,*sptr;
610         int i,j,data_len,ret=0;
611
612         int ptype = V_ASN1_UNDEF;
613         DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
614         algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
615         if (pk->save_parameters) 
616                 {
617                 ASN1_STRING *params = encode_gost_algor_params(pk);
618                 pval = params;
619                 ptype = V_ASN1_SEQUENCE;
620                 }       
621         data_len = BN_num_bytes(dsa->pub_key);
622         databuf = OPENSSL_malloc(data_len);
623         BN_bn2bin(dsa->pub_key,databuf);
624         octet = ASN1_OCTET_STRING_new();
625         ASN1_STRING_set(octet,NULL,data_len);
626         sptr = ASN1_STRING_data(octet);
627         for (i=0,j=data_len-1; i< data_len;i++,j--)
628                 {
629                 sptr[i]=databuf[j];
630                 }
631         OPENSSL_free(databuf);
632         ret = i2d_ASN1_OCTET_STRING(octet,&buf);
633         ASN1_BIT_STRING_free(octet);
634         if (ret <0)  return 0;
635         return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
636         }
637
638 static int pub_decode_gost01(EVP_PKEY *pk,X509_PUBKEY *pub)
639         {
640         X509_ALGOR *palg = NULL;
641         const unsigned char *pubkey_buf = NULL;
642         unsigned char *databuf;
643         ASN1_OBJECT *palgobj = NULL;
644         int pub_len,i,j;
645         EC_POINT *pub_key;
646         BIGNUM *X,*Y;
647         ASN1_OCTET_STRING *octet= NULL;
648         int len;
649         const EC_GROUP *group;
650
651         if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
652                         &palg, pub)) return 0;
653         EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);  
654         if (!decode_gost_algor_params(pk,palg)) return 0;
655         group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
656         octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
657         if (!octet) 
658                 {
659                 GOSTerr(GOST_F_PUB_DECODE_GOST01,ERR_R_MALLOC_FAILURE);
660                 return 0;
661                 }       
662         databuf = OPENSSL_malloc(octet->length);
663         for (i=0,j=octet->length-1;i<octet->length;i++,j--)
664                 {
665                 databuf[j]=octet->data[i];
666                 }
667         len=octet->length/2;
668         ASN1_OCTET_STRING_free(octet);  
669         
670         Y= getbnfrombuf(databuf,len);
671         X= getbnfrombuf(databuf+len,len);
672         OPENSSL_free(databuf);
673         pub_key = EC_POINT_new(group);
674         if (!EC_POINT_set_affine_coordinates_GFp(group
675                         ,pub_key,X,Y,NULL))
676                 {
677                 GOSTerr(GOST_F_PUB_DECODE_GOST01,
678                         ERR_R_EC_LIB);
679                 EC_POINT_free(pub_key);
680                 BN_free(X);
681                 BN_free(Y);
682                 return 0;
683                 }       
684         BN_free(X);
685         BN_free(Y);
686         if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk),pub_key))
687                 {
688                 GOSTerr(GOST_F_PUB_DECODE_GOST01,
689                         ERR_R_EC_LIB);
690                 EC_POINT_free(pub_key);
691                 return 0;
692                 }       
693         EC_POINT_free(pub_key);
694         return 1;
695
696         }
697
698 static int pub_encode_gost01(X509_PUBKEY *pub,const EVP_PKEY *pk)
699         {
700         ASN1_OBJECT *algobj = NULL;
701         ASN1_OCTET_STRING *octet = NULL;
702         void *pval = NULL;
703         unsigned char *buf=NULL,*databuf,*sptr;
704         int i,j,data_len,ret=0;
705         const EC_POINT *pub_key;
706         BIGNUM *X,*Y,*order;
707         const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
708         int ptype = V_ASN1_UNDEF;
709
710         algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
711         if (pk->save_parameters) 
712                 {
713                 ASN1_STRING *params = encode_gost_algor_params(pk);
714                 pval = params;
715                 ptype = V_ASN1_SEQUENCE;
716                 }
717         order = BN_new();
718         EC_GROUP_get_order(EC_KEY_get0_group(ec),order,NULL);
719         pub_key=EC_KEY_get0_public_key(ec);
720         if (!pub_key) 
721                 {
722                 GOSTerr(GOST_F_PUB_ENCODE_GOST01,
723                         GOST_R_PUBLIC_KEY_UNDEFINED);
724                 return 0;
725                 }       
726         X=BN_new();
727         Y=BN_new();
728         EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
729                 pub_key,X,Y,NULL);
730         data_len = 2*BN_num_bytes(order);
731         BN_free(order);
732         databuf = OPENSSL_malloc(data_len);
733         memset(databuf,0,data_len);
734         
735         store_bignum(X,databuf+data_len/2,data_len/2);
736         store_bignum(Y,databuf,data_len/2);
737
738         BN_free(X);
739         BN_free(Y);
740         octet = ASN1_OCTET_STRING_new();
741         ASN1_STRING_set(octet,NULL,data_len);
742         sptr=ASN1_STRING_data(octet);
743     for (i=0,j=data_len-1;i<data_len;i++,j--) 
744                 {
745         sptr[i]=databuf[j];
746                 }
747     OPENSSL_free(databuf);
748         ret = i2d_ASN1_OCTET_STRING(octet,&buf);
749         ASN1_BIT_STRING_free(octet);
750         if (ret <0)  return 0;
751         return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
752         }
753
754 static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
755         {
756         const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
757         const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
758         if (da && db && da->pub_key && db->pub_key
759                 && !BN_cmp(da->pub_key,db->pub_key)) 
760                 {
761                 return 1;
762                 }               
763         return 0;
764         }
765
766 static int pub_cmp_gost01(const EVP_PKEY *a,const EVP_PKEY *b)
767         {
768         const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
769         const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
770         const EC_POINT *ka,*kb;
771         int ret=0;
772         if (!ea || !eb) return 0;
773         ka = EC_KEY_get0_public_key(ea);
774         kb = EC_KEY_get0_public_key(eb);
775         if (!ka || !kb) return 0;
776         ret = (0==EC_POINT_cmp(EC_KEY_get0_group(ea),ka,kb,NULL)) ;
777         return ret;
778         }
779
780
781
782
783 static int pkey_size_gost(const EVP_PKEY *pk)
784         {
785         return 64;
786         }
787
788 static int pkey_bits_gost(const EVP_PKEY *pk)
789         {
790         return 256;
791         }
792 /*------------------------ ASN1 METHOD for GOST MAC  -------------------*/
793 static void  mackey_free_gost(EVP_PKEY *pk)
794         {
795                 if (pk->pkey.ptr) {
796                         OPENSSL_free(pk->pkey.ptr);
797                 }       
798         }
799 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
800 {
801         switch (op)
802                 {
803                 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
804                         *(int *)arg2 = NID_undef;
805                         return 2;
806                 }
807         return -2;
808 }       
809 /* ----------------------------------------------------------------------*/
810 int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info) 
811         {
812         *ameth =        EVP_PKEY_asn1_new(nid, 
813                 ASN1_PKEY_SIGPARAM_NULL, pemstr, info); 
814         if (!*ameth) return 0;
815         switch (nid) 
816                 {
817                 case NID_id_GostR3410_94:
818                         EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost94);
819                         EVP_PKEY_asn1_set_private (*ameth, 
820                                 priv_decode_gost, priv_encode_gost, 
821                                 priv_print_gost94);
822
823                         EVP_PKEY_asn1_set_param (*ameth, 0, 0,
824                                 param_missing_gost94, param_copy_gost94, 
825                                 param_cmp_gost94,param_print_gost94 );
826                         EVP_PKEY_asn1_set_public (*ameth,
827                                 pub_decode_gost94, pub_encode_gost94,
828                                 pub_cmp_gost94, pub_print_gost94,
829                                 pkey_size_gost, pkey_bits_gost);
830         
831                         EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
832                         break;
833                 case NID_id_GostR3410_2001:
834                         EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost01);
835                         EVP_PKEY_asn1_set_private (*ameth, 
836                                 priv_decode_gost, priv_encode_gost, 
837                                 priv_print_gost01);
838
839                         EVP_PKEY_asn1_set_param (*ameth, 0, 0,
840                                 param_missing_gost01, param_copy_gost01, 
841                                 param_cmp_gost01, param_print_gost01);
842                         EVP_PKEY_asn1_set_public (*ameth,
843                                 pub_decode_gost01, pub_encode_gost01,
844                                 pub_cmp_gost01, pub_print_gost01,
845                                 pkey_size_gost, pkey_bits_gost);
846         
847                         EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
848                         break;
849                 case NID_id_Gost28147_89_MAC:
850                         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
851                         EVP_PKEY_asn1_set_ctrl(*ameth,mac_ctrl_gost);   
852                         break;
853                 }               
854         return 1;
855         }