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