1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Implementation of RFC 4490/4491 ASN1 method *
8 * Requires OpenSSL 0.9.9 for compilation *
9 **********************************************************************/
10 #include <openssl/engine.h>
11 #include <openssl/evp.h>
13 #include "gost_params.h"
15 #include "e_gost_err.h"
17 int gost94_nid_by_params(DSA *p)
19 R3410_params *gost_params;
21 for (gost_params = R3410_paramset;gost_params->q!=NULL; gost_params++)
23 BN_dec2bn(&q,gost_params->q);
27 return gost_params->nid;
34 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
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;
42 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
43 ERR_R_MALLOC_FAILURE);
44 ASN1_STRING_free(params);
48 switch (EVP_PKEY_base_id(key))
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;
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;
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;
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)
66 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
67 GOST_R_INVALID_GOST94_PARMSET);
68 ASN1_STRING_free(params);
72 cipher_param_nid = get_encryption_params(NULL)->nid;
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, ¶ms->data);
79 if (params->length <=0 )
81 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
82 ERR_R_MALLOC_FAILURE);
83 ASN1_STRING_free(params);
87 params ->type = V_ASN1_SEQUENCE;
89 GOST_KEY_PARAMS_free(gkp);
93 /* Parses GOST algorithm parameters from X509_ALGOR and
94 * modifies pkey setting NID and parameters
96 static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg)
98 ASN1_OBJECT *palg_obj =NULL;
99 int ptype = V_ASN1_UNDEF;
100 int pkey_nid = NID_undef,param_nid = NID_undef;
102 ASN1_STRING *pval = NULL;
103 const unsigned char *p;
104 GOST_KEY_PARAMS *gkp = NULL;
106 X509_ALGOR_get0(&palg_obj, &ptype, &_pval, palg);
108 if (ptype != V_ASN1_SEQUENCE)
110 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
111 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
115 pkey_nid = OBJ_obj2nid(palg_obj);
117 gkp = d2i_GOST_KEY_PARAMS(NULL,&p,pval->length);
120 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
121 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
124 param_nid = OBJ_obj2nid(gkp->key_params);
125 GOST_KEY_PARAMS_free(gkp);
126 EVP_PKEY_set_type(pkey,pkey_nid);
129 case NID_id_GostR3410_94:
130 case NID_id_GostR3410_94_cc:
132 DSA *dsa= EVP_PKEY_get0(pkey);
136 if (!EVP_PKEY_assign(pkey,pkey_nid,dsa)) return 0;
138 if (!fill_GOST94_params(dsa,param_nid)) return 0;
141 case NID_id_GostR3410_2001:
142 case NID_id_GostR3410_2001_cc:
144 EC_KEY *ec = EVP_PKEY_get0(pkey);
148 if (!EVP_PKEY_assign(pkey,pkey_nid,ec)) return 0;
150 if (!fill_GOST2001_params(ec,param_nid)) return 0;
157 static int gost_set_priv_key(EVP_PKEY *pkey,BIGNUM *priv)
159 switch (EVP_PKEY_base_id(pkey))
161 case NID_id_GostR3410_94:
162 case NID_id_GostR3410_94_cc:
164 DSA *dsa = EVP_PKEY_get0(pkey);
168 EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),dsa);
170 dsa->priv_key = BN_dup(priv);
171 if (!EVP_PKEY_missing_parameters(pkey))
172 gost94_compute_public(dsa);
175 case NID_id_GostR3410_2001:
176 case NID_id_GostR3410_2001_cc:
178 EC_KEY *ec = EVP_PKEY_get0(pkey);
182 EVP_PKEY_assign(pkey,EVP_PKEY_base_id(pkey),ec);
184 if (!EC_KEY_set_private_key(ec,priv)) return 0;
185 if (!EVP_PKEY_missing_parameters(pkey))
186 gost2001_compute_public(ec);
192 BIGNUM* gost_get_priv_key(const EVP_PKEY *pkey)
194 switch (EVP_PKEY_base_id(pkey))
196 case NID_id_GostR3410_94:
197 case NID_id_GostR3410_94_cc:
199 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pkey);
204 if (!dsa->priv_key) return NULL;
205 return BN_dup(dsa->priv_key);
208 case NID_id_GostR3410_2001:
209 case NID_id_GostR3410_2001_cc:
211 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
217 if (!(priv=EC_KEY_get0_private_key(ec))) return NULL;
225 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op,
226 long arg1, void *arg2)
230 case ASN1_PKEY_CTRL_PKCS7_SIGN:
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,
237 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
239 if (nid == NID_undef)
243 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
246 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
250 ASN1_STRING * params = encode_gost_algor_params(pkey);
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);
260 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
261 *(int *)arg2 = NID_id_GostR3411_94;
267 /*----------------------- free functions * ------------------------------*/
268 static void pkey_free_gost94(EVP_PKEY *key)
272 DSA_free(key->pkey.dsa);
276 static void pkey_free_gost01(EVP_PKEY *key)
280 EC_KEY_free(key->pkey.ec);
284 /* ------------------ private key functions -----------------------------*/
285 static int priv_decode_gost( EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf)
287 const unsigned char *pkey_buf = NULL,*p=NULL;
291 X509_ALGOR *palg =NULL;
292 ASN1_OBJECT *palg_obj = NULL;
293 ASN1_INTEGER *priv_key=NULL;
295 if (!PKCS8_pkey_get0(&palg_obj,&pkey_buf,&priv_len,&palg,p8inf))
298 if (!decode_gost_algor_params(pk,palg))
302 if (V_ASN1_OCTET_STRING == *p)
304 /* New format - Little endian octet string */
305 unsigned char rev_buf[32];
307 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL,&p,priv_len);
308 if (!s||s->length !=32)
310 GOSTerr(GOST_F_PRIV_DECODE_GOST_94,
316 rev_buf[31-i]=s->data[i];
319 pk_num = getbnfrombuf(rev_buf,32);
323 priv_key=d2i_ASN1_INTEGER(NULL,&p,priv_len);
324 if (!priv_key || !(pk_num = ASN1_INTEGER_to_BN(priv_key, NULL)))
326 GOSTerr(GOST_F_PRIV_DECODE_GOST_94,
332 ret= gost_set_priv_key(pk,pk_num);
337 /* ----------------------------------------------------------------------*/
338 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
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;
346 ASN1_INTEGER *asn1key=NULL;
351 key = gost_get_priv_key(pk);
352 asn1key = BN_to_ASN1_INTEGER(key,NULL);
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,
360 static int priv_print_gost (BIO *out,const EVP_PKEY *pkey, int indent,
364 if (!BIO_indent(out,indent,128)) return 0;
365 key = gost_get_priv_key(pkey);
372 /* ---------------------------------------------------------------------*/
373 static int param_missing_gost94(const EVP_PKEY *pk)
375 const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
377 if (!dsa->q) return 1;
381 static int param_missing_gost01(const EVP_PKEY *pk)
383 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
385 if (!EC_KEY_get0_group(ec)) return 1;
389 static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from)
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))
395 GOSTerr(GOST_F_PARAM_COPY_GOST94,
396 GOST_R_INCOMPATIBLE_ALGORITHMS);
401 GOSTerr(GOST_F_PARAM_COPY_GOST94,
402 GOST_R_KEY_PARAMETERS_MISSING);
408 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),dto);
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)
416 gost94_compute_public(dto);
419 static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from)
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))
425 GOSTerr(GOST_F_PARAM_COPY_GOST01,
426 GOST_R_INCOMPATIBLE_ALGORITHMS);
431 GOSTerr(GOST_F_PARAM_COPY_GOST94,
432 GOST_R_KEY_PARAMETERS_MISSING);
438 EVP_PKEY_assign(to,EVP_PKEY_base_id(from),eto);
440 EC_KEY_set_group(eto,EC_GROUP_dup(EC_KEY_get0_group(efrom)));
441 if (EC_KEY_get0_private_key(eto))
443 gost2001_compute_public(eto);
448 static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
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;
456 static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
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))))
467 /* ---------- Public key functions * --------------------------------------*/
468 static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub)
470 X509_ALGOR *palg = NULL;
471 const unsigned char *pubkey_buf = NULL;
472 unsigned char *databuf;
473 ASN1_OBJECT *palgobj = NULL;
476 ASN1_OCTET_STRING *octet= NULL;
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);
485 GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE);
488 databuf = OPENSSL_malloc(octet->length);
489 for (i=0,j=octet->length-1;i<octet->length;i++,j--)
491 databuf[j]=octet->data[i];
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);
501 static int pub_encode_gost94(X509_PUBKEY *pub,const EVP_PKEY *pk)
503 ASN1_OBJECT *algobj = NULL;
504 ASN1_OCTET_STRING *octet = NULL;
506 unsigned char *buf=NULL,*databuf,*sptr;
507 int i,j,data_len,ret=0;
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)
514 ASN1_STRING *params = encode_gost_algor_params(pk);
516 ptype = V_ASN1_SEQUENCE;
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--)
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);
535 static int pub_decode_gost01(EVP_PKEY *pk,X509_PUBKEY *pub)
537 X509_ALGOR *palg = NULL;
538 const unsigned char *pubkey_buf = NULL;
539 unsigned char *databuf;
540 ASN1_OBJECT *palgobj = NULL;
544 ASN1_OCTET_STRING *octet= NULL;
545 const EC_GROUP *group;
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);
555 GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE);
558 databuf = OPENSSL_malloc(octet->length);
559 for (i=0,j=octet->length-1;i<octet->length;i++,j--)
561 databuf[j]=octet->data[i];
563 if (EVP_PKEY_base_id(pk) == NID_id_GostR3410_2001_cc)
565 X= getbnfrombuf(databuf,octet->length/2);
566 Y= getbnfrombuf(databuf+(octet->length/2),octet->length/2);
570 Y= getbnfrombuf(databuf,octet->length/2);
571 X= getbnfrombuf(databuf+(octet->length/2),octet->length/2);
573 OPENSSL_free(databuf);
574 pub_key = EC_POINT_new(group);
575 if (!EC_POINT_set_affine_coordinates_GFp(group
578 GOSTerr(GOST_F_PUB_DECODE_GOST01,
584 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk),pub_key))
586 GOSTerr(GOST_F_PUB_DECODE_GOST01,
590 /*EC_POINT_free(pub_key);*/
595 static int pub_encode_gost01(X509_PUBKEY *pub,const EVP_PKEY *pk)
597 ASN1_OBJECT *algobj = NULL;
598 ASN1_OCTET_STRING *octet = NULL;
600 unsigned char *buf=NULL,*databuf,*sptr;
601 int i,j,data_len,ret=0;
602 const EC_POINT *pub_key;
604 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
605 int ptype = V_ASN1_UNDEF;
607 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
608 if (pk->save_parameters)
610 ASN1_STRING *params = encode_gost_algor_params(pk);
612 ptype = V_ASN1_SEQUENCE;
615 EC_GROUP_get_order(EC_KEY_get0_group(ec),order,NULL);
616 pub_key=EC_KEY_get0_public_key(ec);
619 GOSTerr(GOST_F_PUB_ENCODE_GOST01,
620 GOST_R_PUBLIC_KEY_UNDEFINED);
625 EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
627 data_len = 2*BN_num_bytes(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)
633 store_bignum(X,databuf,data_len/2);
634 store_bignum(Y,databuf+data_len/2,data_len/2);
638 store_bignum(X,databuf+data_len/2,data_len/2);
639 store_bignum(Y,databuf,data_len/2);
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--)
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);
657 static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
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))
669 static int pub_cmp_gost01(const EVP_PKEY *a,const EVP_PKEY *b)
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;
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)) ;
683 static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
687 if (!BIO_indent(out,indent,128)) return 0;
688 key = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
694 static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
700 static int pkey_size_gost(const EVP_PKEY *pk)
705 static int pkey_bits_gost(const EVP_PKEY *pk)
710 /* ----------------------------------------------------------------------*/
711 int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info)
713 *ameth = EVP_PKEY_asn1_new(nid,
714 ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
715 if (!*ameth) return 0;
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,
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);
733 EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);
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,
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);
750 EVP_PKEY_asn1_set_ctrl (*ameth, pkey_ctrl_gost);