Fix a variety of warnings generated by some elevated compiler-fascism,
[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                 BN_CTX_start(ctx);
425                 X= BN_CTX_get(ctx);
426                 Y=BN_CTX_get(ctx);
427                 if (!ctx) 
428                         {
429                         GOSTerr(GOST_F_PRINT_GOST_01,ERR_R_MALLOC_FAILURE);
430                         return 0;
431                         }
432                 pubkey = EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
433                 group = EC_KEY_get0_group((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
434                 if (!EC_POINT_get_affine_coordinates_GFp(group,pubkey,X,Y,ctx)) 
435                         {
436                         GOSTerr(GOST_F_PRINT_GOST_01,ERR_R_EC_LIB);
437                         BN_CTX_free(ctx);
438                         return 0;
439                         }
440                 if (!BIO_indent(out,indent,128)) return 0;
441                 BIO_printf(out,"Public key:\n");
442                 if (!BIO_indent(out,indent+3,128)) return 0;
443                 BIO_printf(out,"X:");
444                 BN_print(out,X);
445                 BIO_printf(out,"\n");
446                 BIO_indent(out,indent+3,128);
447                 BIO_printf(out,"Y:");
448                 BN_print(out,Y);
449                 BIO_printf(out,"\n");
450                 BN_CTX_end(ctx);
451                 BN_CTX_free(ctx);
452                 }
453
454         param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey)));
455         if (!BIO_indent(out,indent,128)) return 0;
456         BIO_printf(out,"Parameter set: %s\n",OBJ_nid2ln(param_nid));
457         return 1;
458 }
459 static int param_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
460         ASN1_PCTX *pctx) 
461         {       
462         return print_gost_01(out,pkey,indent,pctx,0);
463         }
464 static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
465         ASN1_PCTX *pctx)
466         {
467         return print_gost_01(out,pkey, indent, pctx,1);
468         }
469 static int priv_print_gost01(BIO *out,const EVP_PKEY *pkey, int indent,
470         ASN1_PCTX *pctx) 
471         {
472         return print_gost_01(out,pkey,indent,pctx,2);
473         }
474 /* ---------------------------------------------------------------------*/
475 static int param_missing_gost94(const EVP_PKEY *pk) 
476         {
477         const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
478         if (!dsa) return 1;
479         if (!dsa->q) return 1;
480         return 0;
481         }
482
483 static int param_missing_gost01(const EVP_PKEY *pk) 
484         {
485         const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
486         if (!ec) return 1;
487         if (!EC_KEY_get0_group(ec)) return 1;
488         return 0;
489         }
490
491 static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from) 
492         {
493         const DSA *dfrom = EVP_PKEY_get0((EVP_PKEY *)from);
494         DSA *dto = EVP_PKEY_get0(to);
495         if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) 
496                 {
497                 GOSTerr(GOST_F_PARAM_COPY_GOST94,
498                         GOST_R_INCOMPATIBLE_ALGORITHMS);
499                 return 0;
500                 }       
501         if (!dfrom) 
502                 {
503                 GOSTerr(GOST_F_PARAM_COPY_GOST94,
504                         GOST_R_KEY_PARAMETERS_MISSING);
505                 return 0;
506                 }       
507         if (!dto) 
508                 {
509                 dto = DSA_new();
510                 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),dto);
511                 }       
512 #define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x);   
513         COPYBIGNUM(dto,dfrom,p)
514                 COPYBIGNUM(dto,dfrom,q)
515                 COPYBIGNUM(dto,dfrom,g)
516
517                 if (dto->priv_key) 
518                         gost94_compute_public(dto);
519         return 1;       
520         }
521 static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from) 
522         {
523         EC_KEY *eto = EVP_PKEY_get0(to);
524         const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
525         if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) 
526                 {
527                 GOSTerr(GOST_F_PARAM_COPY_GOST01,
528                         GOST_R_INCOMPATIBLE_ALGORITHMS);
529                 return 0;
530                 }       
531         if (!efrom) 
532                 {
533                 GOSTerr(GOST_F_PARAM_COPY_GOST01,
534                         GOST_R_KEY_PARAMETERS_MISSING);
535                 return 0;
536                 }       
537         if (!eto) 
538                 {
539                 eto = EC_KEY_new();
540                 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),eto);
541                 }       
542         EC_KEY_set_group(eto,EC_KEY_get0_group(efrom));
543         if (EC_KEY_get0_private_key(eto)) 
544                 {
545                 gost2001_compute_public(eto);
546                 }
547         return 1;
548         }
549
550 static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b) 
551         {
552         const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
553         const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
554         if (!BN_cmp(da->q,db->q)) return 1;
555         return 0;
556         }
557
558 static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b) 
559         {
560         if (EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)a)))==
561                 EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)b)))) 
562                 {
563                 return 1;
564                 }
565         return 0;
566
567         }
568
569 /* ---------- Public key functions * --------------------------------------*/
570 static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub)
571         {
572         X509_ALGOR *palg = NULL;
573         const unsigned char *pubkey_buf = NULL;
574         unsigned char *databuf;
575         ASN1_OBJECT *palgobj = NULL;
576         int pub_len,i,j;
577         DSA *dsa;
578         ASN1_OCTET_STRING *octet= NULL;
579
580         if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
581                         &palg, pub)) return 0;
582         EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);  
583         if (!decode_gost_algor_params(pk,palg)) return 0;
584         octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
585         if (!octet) 
586                 {
587                 GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE);
588                 return 0;
589                 }       
590         databuf = OPENSSL_malloc(octet->length);
591         for (i=0,j=octet->length-1;i<octet->length;i++,j--)
592                 {
593                 databuf[j]=octet->data[i];
594                 }       
595         dsa = EVP_PKEY_get0(pk);
596         dsa->pub_key=BN_bin2bn(databuf,octet->length,NULL);
597         ASN1_OCTET_STRING_free(octet);
598         OPENSSL_free(databuf);
599         return 1;
600
601         }
602
603 static int pub_encode_gost94(X509_PUBKEY *pub,const EVP_PKEY *pk)
604         {
605         ASN1_OBJECT *algobj = NULL;
606         ASN1_OCTET_STRING *octet = NULL;
607         void *pval = NULL;
608         unsigned char *buf=NULL,*databuf,*sptr;
609         int i,j,data_len,ret=0;
610
611         int ptype = V_ASN1_UNDEF;
612         DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
613         algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
614         if (pk->save_parameters) 
615                 {
616                 ASN1_STRING *params = encode_gost_algor_params(pk);
617                 pval = params;
618                 ptype = V_ASN1_SEQUENCE;
619                 }       
620         data_len = BN_num_bytes(dsa->pub_key);
621         databuf = OPENSSL_malloc(data_len);
622         BN_bn2bin(dsa->pub_key,databuf);
623         octet = ASN1_OCTET_STRING_new();
624         ASN1_STRING_set(octet,NULL,data_len);
625         sptr = ASN1_STRING_data(octet);
626         for (i=0,j=data_len-1; i< data_len;i++,j--)
627                 {
628                 sptr[i]=databuf[j];
629                 }
630         OPENSSL_free(databuf);
631         ret = i2d_ASN1_OCTET_STRING(octet,&buf);
632         ASN1_BIT_STRING_free(octet);
633         if (ret <0)  return 0;
634         return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
635         }
636
637 static int pub_decode_gost01(EVP_PKEY *pk,X509_PUBKEY *pub)
638         {
639         X509_ALGOR *palg = NULL;
640         const unsigned char *pubkey_buf = NULL;
641         unsigned char *databuf;
642         ASN1_OBJECT *palgobj = NULL;
643         int pub_len,i,j;
644         EC_POINT *pub_key;
645         BIGNUM *X,*Y;
646         ASN1_OCTET_STRING *octet= NULL;
647         int len;
648         const EC_GROUP *group;
649
650         if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
651                         &palg, pub)) return 0;
652         EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);  
653         if (!decode_gost_algor_params(pk,palg)) return 0;
654         group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
655         octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
656         if (!octet) 
657                 {
658                 GOSTerr(GOST_F_PUB_DECODE_GOST01,ERR_R_MALLOC_FAILURE);
659                 return 0;
660                 }       
661         databuf = OPENSSL_malloc(octet->length);
662         for (i=0,j=octet->length-1;i<octet->length;i++,j--)
663                 {
664                 databuf[j]=octet->data[i];
665                 }
666         len=octet->length/2;
667         ASN1_OCTET_STRING_free(octet);  
668         
669         Y= getbnfrombuf(databuf,len);
670         X= getbnfrombuf(databuf+len,len);
671         OPENSSL_free(databuf);
672         pub_key = EC_POINT_new(group);
673         if (!EC_POINT_set_affine_coordinates_GFp(group
674                         ,pub_key,X,Y,NULL))
675                 {
676                 GOSTerr(GOST_F_PUB_DECODE_GOST01,
677                         ERR_R_EC_LIB);
678                 EC_POINT_free(pub_key);
679                 BN_free(X);
680                 BN_free(Y);
681                 return 0;
682                 }       
683         BN_free(X);
684         BN_free(Y);
685         if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk),pub_key))
686                 {
687                 GOSTerr(GOST_F_PUB_DECODE_GOST01,
688                         ERR_R_EC_LIB);
689                 EC_POINT_free(pub_key);
690                 return 0;
691                 }       
692         EC_POINT_free(pub_key);
693         return 1;
694
695         }
696
697 static int pub_encode_gost01(X509_PUBKEY *pub,const EVP_PKEY *pk)
698         {
699         ASN1_OBJECT *algobj = NULL;
700         ASN1_OCTET_STRING *octet = NULL;
701         void *pval = NULL;
702         unsigned char *buf=NULL,*databuf,*sptr;
703         int i,j,data_len,ret=0;
704         const EC_POINT *pub_key;
705         BIGNUM *X,*Y,*order;
706         const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
707         int ptype = V_ASN1_UNDEF;
708
709         algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
710         if (pk->save_parameters) 
711                 {
712                 ASN1_STRING *params = encode_gost_algor_params(pk);
713                 pval = params;
714                 ptype = V_ASN1_SEQUENCE;
715                 }
716         order = BN_new();
717         EC_GROUP_get_order(EC_KEY_get0_group(ec),order,NULL);
718         pub_key=EC_KEY_get0_public_key(ec);
719         if (!pub_key) 
720                 {
721                 GOSTerr(GOST_F_PUB_ENCODE_GOST01,
722                         GOST_R_PUBLIC_KEY_UNDEFINED);
723                 return 0;
724                 }       
725         X=BN_new();
726         Y=BN_new();
727         EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
728                 pub_key,X,Y,NULL);
729         data_len = 2*BN_num_bytes(order);
730         BN_free(order);
731         databuf = OPENSSL_malloc(data_len);
732         memset(databuf,0,data_len);
733         
734         store_bignum(X,databuf+data_len/2,data_len/2);
735         store_bignum(Y,databuf,data_len/2);
736
737         BN_free(X);
738         BN_free(Y);
739         octet = ASN1_OCTET_STRING_new();
740         ASN1_STRING_set(octet,NULL,data_len);
741         sptr=ASN1_STRING_data(octet);
742     for (i=0,j=data_len-1;i<data_len;i++,j--) 
743                 {
744         sptr[i]=databuf[j];
745                 }
746     OPENSSL_free(databuf);
747         ret = i2d_ASN1_OCTET_STRING(octet,&buf);
748         ASN1_BIT_STRING_free(octet);
749         if (ret <0)  return 0;
750         return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
751         }
752
753 static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
754         {
755         const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
756         const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
757         if (da && db && da->pub_key && db->pub_key
758                 && !BN_cmp(da->pub_key,db->pub_key)) 
759                 {
760                 return 1;
761                 }               
762         return 0;
763         }
764
765 static int pub_cmp_gost01(const EVP_PKEY *a,const EVP_PKEY *b)
766         {
767         const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
768         const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
769         const EC_POINT *ka,*kb;
770         int ret=0;
771         if (!ea || !eb) return 0;
772         ka = EC_KEY_get0_public_key(ea);
773         kb = EC_KEY_get0_public_key(eb);
774         if (!ka || !kb) return 0;
775         ret = (0==EC_POINT_cmp(EC_KEY_get0_group(ea),ka,kb,NULL)) ;
776         return ret;
777         }
778
779
780
781
782 static int pkey_size_gost(const EVP_PKEY *pk)
783         {
784         return 64;
785         }
786
787 static int pkey_bits_gost(const EVP_PKEY *pk)
788         {
789         return 256;
790         }
791 /*------------------------ ASN1 METHOD for GOST MAC  -------------------*/
792 static void  mackey_free_gost(EVP_PKEY *pk)
793         {
794                 if (pk->pkey.ptr) {
795                         OPENSSL_free(pk->pkey.ptr);
796                 }       
797         }
798 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
799 {
800         switch (op)
801                 {
802                 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
803                         *(int *)arg2 = NID_undef;
804                         return 2;
805                 }
806         return -2;
807 }       
808 /* ----------------------------------------------------------------------*/
809 int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info) 
810         {
811         *ameth =        EVP_PKEY_asn1_new(nid, 
812                 ASN1_PKEY_SIGPARAM_NULL, pemstr, info); 
813         if (!*ameth) return 0;
814         switch (nid) 
815                 {
816                 case NID_id_GostR3410_94:
817                         EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost94);
818                         EVP_PKEY_asn1_set_private (*ameth, 
819                                 priv_decode_gost, priv_encode_gost, 
820                                 priv_print_gost94);
821
822                         EVP_PKEY_asn1_set_param (*ameth, 0, 0,
823                                 param_missing_gost94, param_copy_gost94, 
824                                 param_cmp_gost94,param_print_gost94 );
825                         EVP_PKEY_asn1_set_public (*ameth,
826                                 pub_decode_gost94, pub_encode_gost94,
827                                 pub_cmp_gost94, pub_print_gost94,
828                                 pkey_size_gost, pkey_bits_gost);
829         
830                         EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
831                         break;
832                 case NID_id_GostR3410_2001:
833                         EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost01);
834                         EVP_PKEY_asn1_set_private (*ameth, 
835                                 priv_decode_gost, priv_encode_gost, 
836                                 priv_print_gost01);
837
838                         EVP_PKEY_asn1_set_param (*ameth, 0, 0,
839                                 param_missing_gost01, param_copy_gost01, 
840                                 param_cmp_gost01, param_print_gost01);
841                         EVP_PKEY_asn1_set_public (*ameth,
842                                 pub_decode_gost01, pub_encode_gost01,
843                                 pub_cmp_gost01, pub_print_gost01,
844                                 pkey_size_gost, pkey_bits_gost);
845         
846                         EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
847                         break;
848                 case NID_id_Gost28147_89_MAC:
849                         EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
850                         EVP_PKEY_asn1_set_ctrl(*ameth,mac_ctrl_gost);   
851                         break;
852                 }               
853         return 1;
854         }