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