GOST public key algorithm ENGINE donated to the OpenSSL by Cryptocom.
[openssl.git] / engines / ccgost / ameth.c
1 /**********************************************************************
2  *                          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 <openssl/engine.h>
11 #include <openssl/evp.h>
12 #include <string.h>
13 #include "meth.h"
14 #include "pmeth.h"
15 #include "paramset.h"
16 #include "gost_asn1.h"
17 #include "crypt.h"
18 #include "sign.h"
19 #include "tools.h"
20 #include "e_gost_err.h"
21
22 int gost94_nid_by_params(DSA *p) 
23 {
24         R3410_params *gost_params;
25         BIGNUM *q=BN_new();
26         for (gost_params = R3410_paramset;gost_params->q!=NULL; gost_params++) {
27                 BN_dec2bn(&q,gost_params->q);
28                 if (!BN_cmp(q,p->q)) 
29                 {
30                         BN_free(q);
31                         return gost_params->nid;
32                 }
33         }       
34         BN_free(q);
35         return NID_undef;
36 }
37
38 static ASN1_STRING  *encode_gost_algor_params(const EVP_PKEY *key)
39 {
40         ASN1_STRING *params = ASN1_STRING_new();
41         GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
42         int pkey_param_nid = NID_undef;
43         int cipher_param_nid = NID_undef;
44         if (!params || !gkp) {
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         case NID_id_GostR3410_2001_cc:
53                 pkey_param_nid = NID_id_GostR3410_2001_ParamSet_cc;
54                 cipher_param_nid = NID_id_Gost28147_89_cc;
55                 break;
56         case NID_id_GostR3410_94_cc:
57                 pkey_param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
58                 cipher_param_nid = NID_id_Gost28147_89_cc;
59                 break;
60         case NID_id_GostR3410_2001:
61                 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)key)));
62                 cipher_param_nid = get_encryption_params(NULL)->nid;
63                 break;
64         case NID_id_GostR3410_94:
65                 pkey_param_nid = (int) gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)key));
66                 if (pkey_param_nid == NID_undef) {
67                         GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
68                         GOST_R_INVALID_GOST94_PARMSET);
69                         ASN1_STRING_free(params);
70                         params=NULL;
71                         goto err;
72                 }       
73                 cipher_param_nid = get_encryption_params(NULL)->nid;
74                 break;
75         }       
76         gkp->key_params = OBJ_nid2obj(pkey_param_nid);
77         gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
78         /*gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);*/
79         params->length = i2d_GOST_KEY_PARAMS(gkp, &params->data);
80         if (params->length <=0 ) 
81         {
82                 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
83                         ERR_R_MALLOC_FAILURE);
84                 ASN1_STRING_free(params);
85                 params = NULL;
86                 goto err;
87         }
88         params ->type = V_ASN1_SEQUENCE;
89 err:
90         GOST_KEY_PARAMS_free(gkp);
91         return params;
92 }
93 /* Parses GOST algorithm parameters from X509_ALGOR and
94  * modifies pkey setting NID and parameters
95  */
96 static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg) 
97 {
98         ASN1_OBJECT *palg_obj =NULL;
99         int ptype = V_ASN1_UNDEF;
100         int pkey_nid = NID_undef,param_nid = NID_undef;
101         ASN1_STRING *pval = NULL;
102         const unsigned char  *p;
103         GOST_KEY_PARAMS *gkp = NULL;
104
105         X509_ALGOR_get0(&palg_obj, &ptype, (void **) (&pval), palg);
106         if (ptype != V_ASN1_SEQUENCE) {
107                 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
108                         GOST_R_BAD_KEY_PARAMETERS_FORMAT);
109                 return 0;
110         }       
111         p=pval->data;
112         pkey_nid = OBJ_obj2nid(palg_obj);
113
114         gkp = d2i_GOST_KEY_PARAMS(NULL,&p,pval->length);
115         if (!gkp) {
116                 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
117                 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
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         case NID_id_GostR3410_94:
124         case NID_id_GostR3410_94_cc:
125                 {       DSA *dsa= EVP_PKEY_get0(pkey);
126                         if (!dsa) {
127                                 dsa = DSA_new();
128                                 if (!EVP_PKEY_assign(pkey,pkey_nid,dsa)) return 0;
129                         }
130                         if (!fill_GOST94_params(dsa,param_nid)) return 0;
131                         break;
132                 }
133         case NID_id_GostR3410_2001:
134         case NID_id_GostR3410_2001_cc:
135                 { EC_KEY *ec = EVP_PKEY_get0(pkey);
136                         if (!ec) {
137                                 ec = EC_KEY_new();
138                                 if (!EVP_PKEY_assign(pkey,pkey_nid,ec)) return 0;
139                         }
140                         if (!fill_GOST2001_params(ec,param_nid)) return 0;
141
142                 }       
143
144         }                       
145
146         return 1;
147 }
148
149 static int gost_set_priv_key(EVP_PKEY *pkey,BIGNUM *priv) 
150 {
151         switch (EVP_PKEY_base_id(pkey)) {
152         case NID_id_GostR3410_94:
153         case NID_id_GostR3410_94_cc:
154                 {       DSA *dsa = EVP_PKEY_get0(pkey);
155                         if (!dsa) {
156                                 dsa = DSA_new();
157                                 EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),dsa);
158                         }       
159                         dsa->priv_key = BN_dup(priv);
160                         if (!EVP_PKEY_missing_parameters(pkey)) 
161                                 gost94_compute_public(dsa);
162                         break;
163                 }       
164         case NID_id_GostR3410_2001:
165         case NID_id_GostR3410_2001_cc:
166                 {       EC_KEY *ec = EVP_PKEY_get0(pkey);
167                         if (!ec) {
168                                 ec = EC_KEY_new();
169                                 EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),ec);
170                         }       
171                         if (!EC_KEY_set_private_key(ec,priv)) return 0;
172                         if (!EVP_PKEY_missing_parameters(pkey)) 
173                                 gost2001_compute_public(ec);
174                         break;
175                 }
176
177         }
178         return 1;               
179 }
180 BIGNUM* gost_get_priv_key(const EVP_PKEY *pkey) 
181 {
182         switch (EVP_PKEY_base_id(pkey)) {
183         case NID_id_GostR3410_94:
184         case NID_id_GostR3410_94_cc:
185                 {       DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pkey);
186                         if (!dsa) {
187                                 return NULL;
188                         }       
189                         if (!dsa->priv_key) return NULL;
190                         return BN_dup(dsa->priv_key);
191                         break;
192                 }       
193         case NID_id_GostR3410_2001:
194         case NID_id_GostR3410_2001_cc:
195                 {       EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
196                         const BIGNUM* priv;
197                         if (!ec) {
198                                 return NULL;
199                         }       
200                         if (!(priv=EC_KEY_get0_private_key(ec))) return NULL;
201                         return BN_dup(priv);
202                         break;
203                 }
204
205         }
206         return NULL;            
207 }
208 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op,
209                               long arg1, void *arg2)
210 {
211         switch (op)
212         {
213                 case ASN1_PKEY_CTRL_PKCS7_SIGN:
214                         if (arg1 == 0) {
215                                 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
216                                 int nid = EVP_PKEY_base_id(pkey);
217                                 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO*)arg2, 
218                                                 NULL, &alg1, &alg2);
219                                 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
220                                                 V_ASN1_NULL, 0);
221                                 if (nid == NID_undef) {
222                                         return (-1);
223                                 }
224                                 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
225                         }
226                         return 1;
227                 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
228                         if (arg1 == 0)
229                                 {
230                                 X509_ALGOR *alg;
231                                 ASN1_STRING * params = encode_gost_algor_params(pkey);
232                                 if (!params) {
233                                         return -1;
234                                 }
235                                 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO*)arg2, &alg);
236                                 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type),
237                                                                 V_ASN1_SEQUENCE, params);
238                                 }
239                         return 1;
240                 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
241                 *(int *)arg2 = NID_id_GostR3411_94;
242                 return 2;
243         }
244         
245         return -2;
246 }
247 /*----------------------- free functions * ------------------------------*/
248 static void pkey_free_gost94(EVP_PKEY *key) {
249         if (key->pkey.dsa) {
250                 DSA_free(key->pkey.dsa);
251         }
252 }
253 static void pkey_free_gost01(EVP_PKEY *key) {
254         if (key->pkey.ec) {
255                 EC_KEY_free(key->pkey.ec);
256         }
257 }       
258 /* ------------------ private key functions  -----------------------------*/
259 static int priv_decode_gost( EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf) 
260 {
261         const unsigned char *pkey_buf = NULL,*p=NULL;
262         int priv_len = 0;
263         BIGNUM *pk_num=NULL;
264         int ret =0;
265         X509_ALGOR *palg =NULL;
266         ASN1_OBJECT *palg_obj = NULL;
267         ASN1_INTEGER *priv_key=NULL;
268
269         if (!PKCS8_pkey_get0(&palg_obj,&pkey_buf,&priv_len,&palg,p8inf)) 
270                 return 0;
271         p = pkey_buf;
272         if (!decode_gost_algor_params(pk,palg)) {
273                 return 0;
274         }       
275         priv_key=d2i_ASN1_INTEGER(NULL,&p,priv_len);
276         if (!priv_key) {
277         }
278
279         if (!(pk_num =  ASN1_INTEGER_to_BN(priv_key, NULL))) {
280                 GOSTerr(GOST_F_PRIV_DECODE_GOST_94,
281                         EVP_R_DECODE_ERROR);
282         }
283
284         ret= gost_set_priv_key(pk,pk_num);
285         BN_free(pk_num);
286         return ret;
287 }
288 /* ----------------------------------------------------------------------*/
289 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
290 {
291         ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
292         ASN1_STRING *params = encode_gost_algor_params(pk);
293         unsigned char *priv_buf = NULL;
294         int priv_len;
295         BIGNUM *key;
296
297         ASN1_INTEGER *asn1key=NULL;
298         if (!params) {
299                 return 0;
300         }
301         key = gost_get_priv_key(pk);
302         asn1key = BN_to_ASN1_INTEGER(key,NULL);
303         BN_free(key);
304         priv_len = i2d_ASN1_INTEGER(asn1key,&priv_buf);
305         ASN1_INTEGER_free(asn1key);
306         return PKCS8_pkey_set0(p8,algobj,0,V_ASN1_SEQUENCE,params,
307                 priv_buf,priv_len);
308 }
309
310 static int priv_print_gost (BIO *out,const EVP_PKEY *pkey, int indent,
311  ASN1_PCTX *pctx) 
312 {
313         BIGNUM *key;
314         if (!BIO_indent(out,indent,128)) return 0;
315         key = gost_get_priv_key(pkey);
316         if (!key) return 0;
317         BN_print(out,key);
318         BN_free(key);
319         return 1;
320 }
321 /* ---------------------------------------------------------------------*/
322 static int param_missing_gost94(const EVP_PKEY *pk) 
323 {
324         const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
325         if (!dsa) return 1;
326         if (!dsa->q) return 1;
327         return 0;
328 }
329 static int param_missing_gost01(const EVP_PKEY *pk) 
330 {
331         const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
332         if (!ec) return 1;
333         if (!EC_KEY_get0_group(ec)) return 1;
334         return 0;
335 }
336
337 static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from) 
338 {
339         const DSA *dfrom = EVP_PKEY_get0((EVP_PKEY *)from);
340         DSA *dto = EVP_PKEY_get0(to);
341         if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) 
342         {
343                 GOSTerr(GOST_F_PARAM_COPY_GOST94,
344                 GOST_R_INCOMPATIBLE_ALGORITHMS);
345                 return 0;
346         }       
347         if (!dfrom) 
348         {
349                 GOSTerr(GOST_F_PARAM_COPY_GOST94,
350                 GOST_R_KEY_PARAMETERS_MISSING);
351                 return 0;
352         }       
353         if (!dto) 
354         {
355                 dto = DSA_new();
356                 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),dto);
357         }       
358 #define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x);   
359         COPYBIGNUM(dto,dfrom,p)
360         COPYBIGNUM(dto,dfrom,q)
361         COPYBIGNUM(dto,dfrom,g)
362
363         if (dto->priv_key) 
364                 gost94_compute_public(dto);
365         return 1;       
366 }
367 static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from) {
368         EC_KEY *eto = EVP_PKEY_get0(to);
369         const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
370         if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
371                 GOSTerr(GOST_F_PARAM_COPY_GOST01,
372                 GOST_R_INCOMPATIBLE_ALGORITHMS);
373                 return 0;
374         }       
375         if (!efrom) {
376                 GOSTerr(GOST_F_PARAM_COPY_GOST94,
377                 GOST_R_KEY_PARAMETERS_MISSING);
378                 return 0;
379         }       
380         if (!eto) {
381                 eto = EC_KEY_new();
382                 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),eto);
383         }       
384         EC_KEY_set_group(eto,EC_GROUP_dup(EC_KEY_get0_group(efrom)));
385         if (EC_KEY_get0_private_key(eto)) {
386                 gost2001_compute_public(eto);
387         }
388         return 1;
389         
390
391
392 }
393 static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b) {
394         const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
395         const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
396         if (!BN_cmp(da->q,db->q)) return 1;
397         return 0;
398 }
399 static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b) {
400         if (EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)a)))==
401                 EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)b)))) {
402                         return 1;
403         }
404         return 0;
405
406 }
407 /* ---------- Public key functions * --------------------------------------*/
408 static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub)
409 {
410         X509_ALGOR *palg = NULL;
411         const unsigned char *pubkey_buf = NULL;
412         unsigned char *databuf;
413         ASN1_OBJECT *palgobj = NULL;
414         int pub_len,i,j;
415         DSA *dsa;
416         ASN1_OCTET_STRING *octet= NULL;
417
418         if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
419                 &palg, pub)) return 0;
420         EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);  
421         if (!decode_gost_algor_params(pk,palg)) return 0;
422         octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
423         if (!octet) 
424         {
425                 GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE);
426                 return 0;
427         }       
428         databuf = OPENSSL_malloc(octet->length);
429         for (i=0,j=octet->length-1;i<octet->length;i++,j--)
430         {
431                 databuf[j]=octet->data[i];
432         }       
433         dsa = EVP_PKEY_get0(pk);
434         dsa->pub_key=BN_bin2bn(databuf,octet->length,NULL);
435         ASN1_OCTET_STRING_free(octet);
436         OPENSSL_free(databuf);
437         return 1;
438
439 }
440 static int pub_encode_gost94(X509_PUBKEY *pub,const EVP_PKEY *pk)
441 {
442         ASN1_OBJECT *algobj = NULL;
443         ASN1_OCTET_STRING *octet = NULL;
444         void *pval = NULL;
445         unsigned char *buf=NULL,*databuf,*sptr;
446         int i,j,data_len,ret=0;
447
448         int ptype;
449         DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
450         algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
451         if (pk->save_parameters) {
452                 ASN1_STRING *params = encode_gost_algor_params(pk);
453                 pval = params;
454                 ptype = V_ASN1_SEQUENCE;
455         }       
456         data_len = BN_num_bytes(dsa->pub_key);
457         databuf = OPENSSL_malloc(data_len);
458         BN_bn2bin(dsa->pub_key,databuf);
459         octet = ASN1_OCTET_STRING_new();
460         ASN1_STRING_set(octet,NULL,data_len);
461         sptr = ASN1_STRING_data(octet);
462         for (i=0,j=data_len-1; i< data_len;i++,j--)
463         {
464                 sptr[i]=databuf[j];
465         }
466         OPENSSL_free(databuf);
467         ret = i2d_ASN1_OCTET_STRING(octet,&buf);
468         ASN1_BIT_STRING_free(octet);
469         if (ret <0)  return 0;
470         return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
471 }
472 static int pub_decode_gost01(EVP_PKEY *pk,X509_PUBKEY *pub)
473 {
474         X509_ALGOR *palg = NULL;
475         const unsigned char *pubkey_buf = NULL;
476         unsigned char *databuf;
477         ASN1_OBJECT *palgobj = NULL;
478         int pub_len,i,j;
479         EC_POINT *pub_key;
480         BIGNUM *X,*Y;
481         ASN1_OCTET_STRING *octet= NULL;
482         const EC_GROUP *group;
483
484         if (!X509_PUBKEY_get0_param(&palgobj,&pubkey_buf,&pub_len,
485                 &palg, pub)) return 0;
486         EVP_PKEY_assign(pk,OBJ_obj2nid(palgobj),NULL);  
487         if (!decode_gost_algor_params(pk,palg)) return 0;
488         group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
489         octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
490         if (!octet) 
491         {
492                 GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE);
493                 return 0;
494         }       
495         databuf = OPENSSL_malloc(octet->length);
496         for (i=0,j=octet->length-1;i<octet->length;i++,j--)
497         {
498                 databuf[j]=octet->data[i];
499         }
500         if (EVP_PKEY_base_id(pk) == NID_id_GostR3410_2001_cc) {
501                 X= getbnfrombuf(databuf,octet->length/2);
502                 Y= getbnfrombuf(databuf+(octet->length/2),octet->length/2);
503         } else {
504                 Y= getbnfrombuf(databuf,octet->length/2);
505                 X= getbnfrombuf(databuf+(octet->length/2),octet->length/2);
506         }
507         OPENSSL_free(databuf);
508         pub_key = EC_POINT_new(group);
509         if (!EC_POINT_set_affine_coordinates_GFp(group
510                 ,pub_key,X,Y,NULL))
511         {
512                 GOSTerr(GOST_F_PUB_DECODE_GOST01,
513                 ERR_R_EC_LIB);
514                 return 0;
515         }       
516         BN_free(X);
517         BN_free(Y);
518         if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk),pub_key))
519         {
520                 GOSTerr(GOST_F_PUB_DECODE_GOST01,
521                 ERR_R_EC_LIB);
522                 return 0;
523         }       
524         /*EC_POINT_free(pub_key);*/
525         return 1;
526
527 }
528 static int pub_encode_gost01(X509_PUBKEY *pub,const EVP_PKEY *pk)
529 {
530         ASN1_OBJECT *algobj = NULL;
531         ASN1_OCTET_STRING *octet = NULL;
532         void *pval = NULL;
533         unsigned char *buf=NULL,*databuf,*sptr;
534         int i,j,data_len,ret=0;
535         const EC_POINT *pub_key;
536         BIGNUM *X,*Y,*order;
537         const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
538         int ptype;
539
540         algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
541         if (pk->save_parameters) {
542                 ASN1_STRING *params = encode_gost_algor_params(pk);
543                 pval = params;
544                 ptype = V_ASN1_SEQUENCE;
545         }
546         order = BN_new();
547         EC_GROUP_get_order(EC_KEY_get0_group(ec),order,NULL);
548         pub_key=EC_KEY_get0_public_key(ec);
549         if (!pub_key) {
550                 GOSTerr(GOST_F_PUB_ENCODE_GOST01,
551                 GOST_R_PUBLIC_KEY_UNDEFINED);
552                 return 0;
553         }       
554         X=BN_new();
555         Y=BN_new();
556         EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
557                 pub_key,X,Y,NULL);
558         data_len = 2*BN_num_bytes(order);
559         BN_free(order);
560         databuf = OPENSSL_malloc(data_len);
561         memset(databuf,0,data_len);
562         if (EVP_PKEY_base_id(pk) == NID_id_GostR3410_2001_cc) {
563                 store_bignum(X,databuf,data_len/2);
564                 store_bignum(Y,databuf+data_len/2,data_len/2);
565         } else {
566                 store_bignum(X,databuf+data_len/2,data_len/2);
567                 store_bignum(Y,databuf,data_len/2);
568         }
569         BN_free(X);
570         BN_free(Y);
571         octet = ASN1_OCTET_STRING_new();
572         ASN1_STRING_set(octet,NULL,data_len);
573         sptr=ASN1_STRING_data(octet);
574     for (i=0,j=data_len-1;i<data_len;i++,j--) {
575         sptr[i]=databuf[j];
576     }
577     OPENSSL_free(databuf);
578         ret = i2d_ASN1_OCTET_STRING(octet,&buf);
579         ASN1_BIT_STRING_free(octet);
580         if (ret <0)  return 0;
581         return X509_PUBKEY_set0_param(pub,algobj,ptype,pval,buf,ret);
582 }
583 static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
584 {
585         const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
586         const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
587         if (da && db && da->pub_key && db->pub_key
588                 && !BN_cmp(da->pub_key,db->pub_key)) {
589                         return 1;
590         }               
591         return 0;
592 }
593 static int pub_cmp_gost01(const EVP_PKEY *a,const EVP_PKEY *b)
594 {
595         const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
596         const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
597         const EC_POINT *ka,*kb;
598         int ret=0;
599         if (!ea || !eb) return 0;
600         ka = EC_KEY_get0_public_key(ea);
601         kb = EC_KEY_get0_public_key(eb);
602         if (!ka || !kb) return 0;
603         ret = (0==EC_POINT_cmp(EC_KEY_get0_group(ea),ka,kb,NULL)) ;
604         return ret;
605 }
606 static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
607         ASN1_PCTX *pctx)
608 {
609         const BIGNUM *key;
610         if (!BIO_indent(out,indent,128)) return 0;
611         key = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
612         if (!key) return 0;
613         BN_print(out,key);
614         return 1;
615 }
616 static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
617         ASN1_PCTX *pctx)
618 {
619         return 0;
620 }
621 static int pkey_size_gost(const EVP_PKEY *pk)
622 {
623         return 64;
624 }
625 static int pkey_bits_gost(const EVP_PKEY *pk)
626 {
627         return 256;
628 }
629 /* ----------------------------------------------------------------------*/
630 int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info) {
631         *ameth =        EVP_PKEY_asn1_new(nid, 
632                         ASN1_PKEY_SIGPARAM_NULL, pemstr, info); 
633         if (!*ameth) return 0;
634         switch (nid) {
635                 case NID_id_GostR3410_94_cc:
636                 case NID_id_GostR3410_94:
637                         EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost94);
638                         EVP_PKEY_asn1_set_private (*ameth, 
639                                 priv_decode_gost, priv_encode_gost, 
640                                 priv_print_gost);
641
642                         EVP_PKEY_asn1_set_param (*ameth, 0, 0,
643                                 param_missing_gost94, param_copy_gost94, 
644                                 param_cmp_gost94,0 );
645                         EVP_PKEY_asn1_set_public (*ameth,
646                                 pub_decode_gost94, pub_encode_gost94,
647                                 pub_cmp_gost94, pub_print_gost94,
648                                 pkey_size_gost, pkey_bits_gost);
649         
650                         EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
651                         break;
652                 case NID_id_GostR3410_2001_cc:
653                 case NID_id_GostR3410_2001:
654                         EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost01);
655                         EVP_PKEY_asn1_set_private (*ameth, 
656                                 priv_decode_gost, priv_encode_gost, 
657                                 priv_print_gost);
658
659                         EVP_PKEY_asn1_set_param (*ameth, 0, 0,
660                                 param_missing_gost01, param_copy_gost01, 
661                                 param_cmp_gost01, 0);
662                         EVP_PKEY_asn1_set_public (*ameth,
663                                 pub_decode_gost01, pub_encode_gost01,
664                                 pub_cmp_gost01, pub_print_gost01,
665                                 pkey_size_gost, pkey_bits_gost);
666         
667                         EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
668                         break;
669         }               
670         return 1;
671 }