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 **********************************************************************/
11 #include <openssl/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/asn1.h>
16 #include "gost_params.h"
18 #include "e_gost_err.h"
20 int gost94_nid_by_params(DSA
*p
)
22 R3410_params
*gost_params
;
24 for (gost_params
= R3410_paramset
;gost_params
->q
!=NULL
; gost_params
++)
26 BN_dec2bn(&q
,gost_params
->q
);
30 return gost_params
->nid
;
37 static ASN1_STRING
*encode_gost_algor_params(const EVP_PKEY
*key
)
39 ASN1_STRING
*params
= ASN1_STRING_new();
40 GOST_KEY_PARAMS
*gkp
= GOST_KEY_PARAMS_new();
41 int pkey_param_nid
= NID_undef
;
42 int cipher_param_nid
= NID_undef
;
45 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS
,
46 ERR_R_MALLOC_FAILURE
);
47 ASN1_STRING_free(params
);
51 switch (EVP_PKEY_base_id(key
))
53 case NID_id_GostR3410_2001
:
54 pkey_param_nid
= EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY
*)key
)));
55 cipher_param_nid
= get_encryption_params(NULL
)->nid
;
57 case NID_id_GostR3410_94
:
58 pkey_param_nid
= (int) gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY
*)key
));
59 if (pkey_param_nid
== NID_undef
)
61 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS
,
62 GOST_R_INVALID_GOST94_PARMSET
);
63 ASN1_STRING_free(params
);
67 cipher_param_nid
= get_encryption_params(NULL
)->nid
;
70 gkp
->key_params
= OBJ_nid2obj(pkey_param_nid
);
71 gkp
->hash_params
= OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet
);
72 /*gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);*/
73 params
->length
= i2d_GOST_KEY_PARAMS(gkp
, ¶ms
->data
);
74 if (params
->length
<=0 )
76 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS
,
77 ERR_R_MALLOC_FAILURE
);
78 ASN1_STRING_free(params
);
82 params
->type
= V_ASN1_SEQUENCE
;
84 GOST_KEY_PARAMS_free(gkp
);
88 /* Parses GOST algorithm parameters from X509_ALGOR and
89 * modifies pkey setting NID and parameters
91 static int decode_gost_algor_params(EVP_PKEY
*pkey
, X509_ALGOR
*palg
)
93 ASN1_OBJECT
*palg_obj
=NULL
;
94 int ptype
= V_ASN1_UNDEF
;
95 int pkey_nid
= NID_undef
,param_nid
= NID_undef
;
97 ASN1_STRING
*pval
= NULL
;
98 const unsigned char *p
;
99 GOST_KEY_PARAMS
*gkp
= NULL
;
101 X509_ALGOR_get0(&palg_obj
, &ptype
, &_pval
, palg
);
103 if (ptype
!= V_ASN1_SEQUENCE
)
105 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS
,
106 GOST_R_BAD_KEY_PARAMETERS_FORMAT
);
110 pkey_nid
= OBJ_obj2nid(palg_obj
);
112 gkp
= d2i_GOST_KEY_PARAMS(NULL
,&p
,pval
->length
);
115 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS
,
116 GOST_R_BAD_PKEY_PARAMETERS_FORMAT
);
119 param_nid
= OBJ_obj2nid(gkp
->key_params
);
120 GOST_KEY_PARAMS_free(gkp
);
121 EVP_PKEY_set_type(pkey
,pkey_nid
);
124 case NID_id_GostR3410_94
:
126 DSA
*dsa
= EVP_PKEY_get0(pkey
);
130 if (!EVP_PKEY_assign(pkey
,pkey_nid
,dsa
)) return 0;
132 if (!fill_GOST94_params(dsa
,param_nid
)) return 0;
135 case NID_id_GostR3410_2001
:
137 EC_KEY
*ec
= EVP_PKEY_get0(pkey
);
141 if (!EVP_PKEY_assign(pkey
,pkey_nid
,ec
)) return 0;
143 if (!fill_GOST2001_params(ec
,param_nid
)) return 0;
150 static int gost_set_priv_key(EVP_PKEY
*pkey
,BIGNUM
*priv
)
152 switch (EVP_PKEY_base_id(pkey
))
154 case NID_id_GostR3410_94
:
156 DSA
*dsa
= EVP_PKEY_get0(pkey
);
160 EVP_PKEY_assign(pkey
,EVP_PKEY_base_id(pkey
),dsa
);
162 dsa
->priv_key
= BN_dup(priv
);
163 if (!EVP_PKEY_missing_parameters(pkey
))
164 gost94_compute_public(dsa
);
167 case NID_id_GostR3410_2001
:
169 EC_KEY
*ec
= EVP_PKEY_get0(pkey
);
173 EVP_PKEY_assign(pkey
,EVP_PKEY_base_id(pkey
),ec
);
175 if (!EC_KEY_set_private_key(ec
,priv
)) return 0;
176 if (!EVP_PKEY_missing_parameters(pkey
))
177 gost2001_compute_public(ec
);
183 BIGNUM
* gost_get0_priv_key(const EVP_PKEY
*pkey
)
185 switch (EVP_PKEY_base_id(pkey
))
187 case NID_id_GostR3410_94
:
189 DSA
*dsa
= EVP_PKEY_get0((EVP_PKEY
*)pkey
);
194 if (!dsa
->priv_key
) return NULL
;
195 return dsa
->priv_key
;
198 case NID_id_GostR3410_2001
:
200 EC_KEY
*ec
= EVP_PKEY_get0((EVP_PKEY
*)pkey
);
206 if (!(priv
=EC_KEY_get0_private_key(ec
))) return NULL
;
207 return (BIGNUM
*)priv
;
214 static int pkey_ctrl_gost(EVP_PKEY
*pkey
, int op
,
215 long arg1
, void *arg2
)
219 case ASN1_PKEY_CTRL_PKCS7_SIGN
:
222 X509_ALGOR
*alg1
= NULL
, *alg2
= NULL
;
223 int nid
= EVP_PKEY_base_id(pkey
);
224 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO
*)arg2
,
226 X509_ALGOR_set0(alg1
, OBJ_nid2obj(NID_id_GostR3411_94
),
228 if (nid
== NID_undef
)
232 X509_ALGOR_set0(alg2
, OBJ_nid2obj(nid
), V_ASN1_NULL
, 0);
235 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT
:
239 ASN1_STRING
* params
= encode_gost_algor_params(pkey
);
244 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO
*)arg2
, &alg
);
245 X509_ALGOR_set0(alg
, OBJ_nid2obj(pkey
->type
),
246 V_ASN1_SEQUENCE
, params
);
249 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
250 *(int *)arg2
= NID_id_GostR3411_94
;
256 /*----------------------- free functions * ------------------------------*/
257 static void pkey_free_gost94(EVP_PKEY
*key
)
261 DSA_free(key
->pkey
.dsa
);
265 static void pkey_free_gost01(EVP_PKEY
*key
)
269 EC_KEY_free(key
->pkey
.ec
);
273 /* ------------------ private key functions -----------------------------*/
274 static int priv_decode_gost( EVP_PKEY
*pk
, PKCS8_PRIV_KEY_INFO
*p8inf
)
276 const unsigned char *pkey_buf
= NULL
,*p
=NULL
;
280 X509_ALGOR
*palg
=NULL
;
281 ASN1_OBJECT
*palg_obj
= NULL
;
282 ASN1_INTEGER
*priv_key
=NULL
;
284 if (!PKCS8_pkey_get0(&palg_obj
,&pkey_buf
,&priv_len
,&palg
,p8inf
))
287 if (!decode_gost_algor_params(pk
,palg
))
291 if (V_ASN1_OCTET_STRING
== *p
)
293 /* New format - Little endian octet string */
294 unsigned char rev_buf
[32];
296 ASN1_OCTET_STRING
*s
= d2i_ASN1_OCTET_STRING(NULL
,&p
,priv_len
);
297 if (!s
||s
->length
!=32)
299 GOSTerr(GOST_F_PRIV_DECODE_GOST
,
305 rev_buf
[31-i
]=s
->data
[i
];
308 pk_num
= getbnfrombuf(rev_buf
,32);
312 priv_key
=d2i_ASN1_INTEGER(NULL
,&p
,priv_len
);
313 if (!priv_key
) return 0;
314 ret
= ((pk_num
= ASN1_INTEGER_to_BN(priv_key
, NULL
))!=NULL
) ;
315 ASN1_INTEGER_free(priv_key
);
318 GOSTerr(GOST_F_PRIV_DECODE_GOST
,
324 ret
= gost_set_priv_key(pk
,pk_num
);
329 /* ----------------------------------------------------------------------*/
330 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO
*p8
, const EVP_PKEY
*pk
)
332 ASN1_OBJECT
*algobj
= OBJ_nid2obj(EVP_PKEY_base_id(pk
));
333 ASN1_STRING
*params
= encode_gost_algor_params(pk
);
334 unsigned char *priv_buf
= NULL
;
337 ASN1_INTEGER
*asn1key
=NULL
;
342 asn1key
= BN_to_ASN1_INTEGER(gost_get0_priv_key(pk
),NULL
);
343 priv_len
= i2d_ASN1_INTEGER(asn1key
,&priv_buf
);
344 ASN1_INTEGER_free(asn1key
);
345 return PKCS8_pkey_set0(p8
,algobj
,0,V_ASN1_SEQUENCE
,params
,
348 /* --------- printing keys --------------------------------*/
349 static int print_gost_94(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
350 ASN1_PCTX
*pctx
, int type
)
352 int param_nid
= NID_undef
;
358 if (!BIO_indent(out
,indent
,128)) return 0;
359 BIO_printf(out
,"Private key: ");
360 key
= gost_get0_priv_key(pkey
);
362 BIO_printf(out
,"<undefined>");
365 BIO_printf(out
,"\n");
371 pubkey
= ((DSA
*)EVP_PKEY_get0((EVP_PKEY
*)pkey
))->pub_key
;
372 BIO_indent(out
,indent
,128);
373 BIO_printf(out
,"Public key: ");
374 BN_print(out
,pubkey
);
375 BIO_printf(out
,"\n");
378 param_nid
= gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY
*)pkey
));
379 BIO_indent(out
,indent
,128);
380 BIO_printf(out
, "Parameter set: %s\n",OBJ_nid2ln(param_nid
));
384 static int param_print_gost94(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
387 return print_gost_94(out
, pkey
, indent
, pctx
,0);
390 static int pub_print_gost94(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
393 return print_gost_94(out
,pkey
, indent
, pctx
,1);
395 static int priv_print_gost94(BIO
*out
,const EVP_PKEY
*pkey
, int indent
,
398 return print_gost_94(out
,pkey
,indent
,pctx
,2);
401 static int print_gost_01(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
402 ASN1_PCTX
*pctx
, int type
)
404 int param_nid
= NID_undef
;
409 if (!BIO_indent(out
,indent
,128)) return 0;
410 BIO_printf(out
,"Private key: ");
411 key
= gost_get0_priv_key(pkey
);
413 BIO_printf(out
,"<undefined)");
416 BIO_printf(out
,"\n");
420 BN_CTX
*ctx
= BN_CTX_new();
422 const EC_POINT
*pubkey
;
423 const EC_GROUP
*group
;
427 GOSTerr(GOST_F_PRINT_GOST_01
,ERR_R_MALLOC_FAILURE
);
433 pubkey
= EC_KEY_get0_public_key((EC_KEY
*)EVP_PKEY_get0((EVP_PKEY
*)pkey
));
434 group
= EC_KEY_get0_group((EC_KEY
*)EVP_PKEY_get0((EVP_PKEY
*)pkey
));
435 if (!EC_POINT_get_affine_coordinates_GFp(group
,pubkey
,X
,Y
,ctx
))
437 GOSTerr(GOST_F_PRINT_GOST_01
,ERR_R_EC_LIB
);
441 if (!BIO_indent(out
,indent
,128)) return 0;
442 BIO_printf(out
,"Public key:\n");
443 if (!BIO_indent(out
,indent
+3,128)) return 0;
444 BIO_printf(out
,"X:");
446 BIO_printf(out
,"\n");
447 BIO_indent(out
,indent
+3,128);
448 BIO_printf(out
,"Y:");
450 BIO_printf(out
,"\n");
455 param_nid
= EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY
*)pkey
)));
456 if (!BIO_indent(out
,indent
,128)) return 0;
457 BIO_printf(out
,"Parameter set: %s\n",OBJ_nid2ln(param_nid
));
460 static int param_print_gost01(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
463 return print_gost_01(out
,pkey
,indent
,pctx
,0);
465 static int pub_print_gost01(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
468 return print_gost_01(out
,pkey
, indent
, pctx
,1);
470 static int priv_print_gost01(BIO
*out
,const EVP_PKEY
*pkey
, int indent
,
473 return print_gost_01(out
,pkey
,indent
,pctx
,2);
475 /* ---------------------------------------------------------------------*/
476 static int param_missing_gost94(const EVP_PKEY
*pk
)
478 const DSA
*dsa
= EVP_PKEY_get0((EVP_PKEY
*)pk
);
480 if (!dsa
->q
) return 1;
484 static int param_missing_gost01(const EVP_PKEY
*pk
)
486 const EC_KEY
*ec
= EVP_PKEY_get0((EVP_PKEY
*)pk
);
488 if (!EC_KEY_get0_group(ec
)) return 1;
492 static int param_copy_gost94(EVP_PKEY
*to
, const EVP_PKEY
*from
)
494 const DSA
*dfrom
= EVP_PKEY_get0((EVP_PKEY
*)from
);
495 DSA
*dto
= EVP_PKEY_get0(to
);
496 if (EVP_PKEY_base_id(from
) != EVP_PKEY_base_id(to
))
498 GOSTerr(GOST_F_PARAM_COPY_GOST94
,
499 GOST_R_INCOMPATIBLE_ALGORITHMS
);
504 GOSTerr(GOST_F_PARAM_COPY_GOST94
,
505 GOST_R_KEY_PARAMETERS_MISSING
);
511 EVP_PKEY_assign(to
,EVP_PKEY_base_id(from
),dto
);
513 #define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x);
514 COPYBIGNUM(dto
,dfrom
,p
)
515 COPYBIGNUM(dto
,dfrom
,q
)
516 COPYBIGNUM(dto
,dfrom
,g
)
519 gost94_compute_public(dto
);
522 static int param_copy_gost01(EVP_PKEY
*to
, const EVP_PKEY
*from
)
524 EC_KEY
*eto
= EVP_PKEY_get0(to
);
525 const EC_KEY
*efrom
= EVP_PKEY_get0((EVP_PKEY
*)from
);
526 if (EVP_PKEY_base_id(from
) != EVP_PKEY_base_id(to
))
528 GOSTerr(GOST_F_PARAM_COPY_GOST01
,
529 GOST_R_INCOMPATIBLE_ALGORITHMS
);
534 GOSTerr(GOST_F_PARAM_COPY_GOST01
,
535 GOST_R_KEY_PARAMETERS_MISSING
);
541 EVP_PKEY_assign(to
,EVP_PKEY_base_id(from
),eto
);
543 EC_KEY_set_group(eto
,EC_KEY_get0_group(efrom
));
544 if (EC_KEY_get0_private_key(eto
))
546 gost2001_compute_public(eto
);
551 static int param_cmp_gost94(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
553 const DSA
*da
= EVP_PKEY_get0((EVP_PKEY
*)a
);
554 const DSA
*db
= EVP_PKEY_get0((EVP_PKEY
*)b
);
555 if (!BN_cmp(da
->q
,db
->q
)) return 1;
559 static int param_cmp_gost01(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
561 if (EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY
*)a
)))==
562 EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY
*)b
))))
570 /* ---------- Public key functions * --------------------------------------*/
571 static int pub_decode_gost94(EVP_PKEY
*pk
, X509_PUBKEY
*pub
)
573 X509_ALGOR
*palg
= NULL
;
574 const unsigned char *pubkey_buf
= NULL
;
575 unsigned char *databuf
;
576 ASN1_OBJECT
*palgobj
= NULL
;
579 ASN1_OCTET_STRING
*octet
= NULL
;
581 if (!X509_PUBKEY_get0_param(&palgobj
,&pubkey_buf
,&pub_len
,
582 &palg
, pub
)) return 0;
583 EVP_PKEY_assign(pk
,OBJ_obj2nid(palgobj
),NULL
);
584 if (!decode_gost_algor_params(pk
,palg
)) return 0;
585 octet
= d2i_ASN1_OCTET_STRING(NULL
,&pubkey_buf
,pub_len
);
588 GOSTerr(GOST_F_PUB_DECODE_GOST94
,ERR_R_MALLOC_FAILURE
);
591 databuf
= OPENSSL_malloc(octet
->length
);
592 for (i
=0,j
=octet
->length
-1;i
<octet
->length
;i
++,j
--)
594 databuf
[j
]=octet
->data
[i
];
596 dsa
= EVP_PKEY_get0(pk
);
597 dsa
->pub_key
=BN_bin2bn(databuf
,octet
->length
,NULL
);
598 ASN1_OCTET_STRING_free(octet
);
599 OPENSSL_free(databuf
);
604 static int pub_encode_gost94(X509_PUBKEY
*pub
,const EVP_PKEY
*pk
)
606 ASN1_OBJECT
*algobj
= NULL
;
607 ASN1_OCTET_STRING
*octet
= NULL
;
609 unsigned char *buf
=NULL
,*databuf
,*sptr
;
610 int i
,j
,data_len
,ret
=0;
612 int ptype
= V_ASN1_UNDEF
;
613 DSA
*dsa
= EVP_PKEY_get0((EVP_PKEY
*)pk
);
614 algobj
= OBJ_nid2obj(EVP_PKEY_base_id(pk
));
615 if (pk
->save_parameters
)
617 ASN1_STRING
*params
= encode_gost_algor_params(pk
);
619 ptype
= V_ASN1_SEQUENCE
;
621 data_len
= BN_num_bytes(dsa
->pub_key
);
622 databuf
= OPENSSL_malloc(data_len
);
623 BN_bn2bin(dsa
->pub_key
,databuf
);
624 octet
= ASN1_OCTET_STRING_new();
625 ASN1_STRING_set(octet
,NULL
,data_len
);
626 sptr
= ASN1_STRING_data(octet
);
627 for (i
=0,j
=data_len
-1; i
< data_len
;i
++,j
--)
631 OPENSSL_free(databuf
);
632 ret
= i2d_ASN1_OCTET_STRING(octet
,&buf
);
633 ASN1_BIT_STRING_free(octet
);
634 if (ret
<0) return 0;
635 return X509_PUBKEY_set0_param(pub
,algobj
,ptype
,pval
,buf
,ret
);
638 static int pub_decode_gost01(EVP_PKEY
*pk
,X509_PUBKEY
*pub
)
640 X509_ALGOR
*palg
= NULL
;
641 const unsigned char *pubkey_buf
= NULL
;
642 unsigned char *databuf
;
643 ASN1_OBJECT
*palgobj
= NULL
;
647 ASN1_OCTET_STRING
*octet
= NULL
;
649 const EC_GROUP
*group
;
651 if (!X509_PUBKEY_get0_param(&palgobj
,&pubkey_buf
,&pub_len
,
652 &palg
, pub
)) return 0;
653 EVP_PKEY_assign(pk
,OBJ_obj2nid(palgobj
),NULL
);
654 if (!decode_gost_algor_params(pk
,palg
)) return 0;
655 group
= EC_KEY_get0_group(EVP_PKEY_get0(pk
));
656 octet
= d2i_ASN1_OCTET_STRING(NULL
,&pubkey_buf
,pub_len
);
659 GOSTerr(GOST_F_PUB_DECODE_GOST01
,ERR_R_MALLOC_FAILURE
);
662 databuf
= OPENSSL_malloc(octet
->length
);
663 for (i
=0,j
=octet
->length
-1;i
<octet
->length
;i
++,j
--)
665 databuf
[j
]=octet
->data
[i
];
668 ASN1_OCTET_STRING_free(octet
);
670 Y
= getbnfrombuf(databuf
,len
);
671 X
= getbnfrombuf(databuf
+len
,len
);
672 OPENSSL_free(databuf
);
673 pub_key
= EC_POINT_new(group
);
674 if (!EC_POINT_set_affine_coordinates_GFp(group
677 GOSTerr(GOST_F_PUB_DECODE_GOST01
,
679 EC_POINT_free(pub_key
);
686 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk
),pub_key
))
688 GOSTerr(GOST_F_PUB_DECODE_GOST01
,
690 EC_POINT_free(pub_key
);
693 EC_POINT_free(pub_key
);
698 static int pub_encode_gost01(X509_PUBKEY
*pub
,const EVP_PKEY
*pk
)
700 ASN1_OBJECT
*algobj
= NULL
;
701 ASN1_OCTET_STRING
*octet
= NULL
;
703 unsigned char *buf
=NULL
,*databuf
,*sptr
;
704 int i
,j
,data_len
,ret
=0;
705 const EC_POINT
*pub_key
;
707 const EC_KEY
*ec
= EVP_PKEY_get0((EVP_PKEY
*)pk
);
708 int ptype
= V_ASN1_UNDEF
;
710 algobj
= OBJ_nid2obj(EVP_PKEY_base_id(pk
));
711 if (pk
->save_parameters
)
713 ASN1_STRING
*params
= encode_gost_algor_params(pk
);
715 ptype
= V_ASN1_SEQUENCE
;
718 EC_GROUP_get_order(EC_KEY_get0_group(ec
),order
,NULL
);
719 pub_key
=EC_KEY_get0_public_key(ec
);
722 GOSTerr(GOST_F_PUB_ENCODE_GOST01
,
723 GOST_R_PUBLIC_KEY_UNDEFINED
);
728 EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec
),
730 data_len
= 2*BN_num_bytes(order
);
732 databuf
= OPENSSL_malloc(data_len
);
733 memset(databuf
,0,data_len
);
735 store_bignum(X
,databuf
+data_len
/2,data_len
/2);
736 store_bignum(Y
,databuf
,data_len
/2);
740 octet
= ASN1_OCTET_STRING_new();
741 ASN1_STRING_set(octet
,NULL
,data_len
);
742 sptr
=ASN1_STRING_data(octet
);
743 for (i
=0,j
=data_len
-1;i
<data_len
;i
++,j
--)
747 OPENSSL_free(databuf
);
748 ret
= i2d_ASN1_OCTET_STRING(octet
,&buf
);
749 ASN1_BIT_STRING_free(octet
);
750 if (ret
<0) return 0;
751 return X509_PUBKEY_set0_param(pub
,algobj
,ptype
,pval
,buf
,ret
);
754 static int pub_cmp_gost94(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
756 const DSA
*da
= EVP_PKEY_get0((EVP_PKEY
*)a
);
757 const DSA
*db
= EVP_PKEY_get0((EVP_PKEY
*)b
);
758 if (da
&& db
&& da
->pub_key
&& db
->pub_key
759 && !BN_cmp(da
->pub_key
,db
->pub_key
))
766 static int pub_cmp_gost01(const EVP_PKEY
*a
,const EVP_PKEY
*b
)
768 const EC_KEY
*ea
= EVP_PKEY_get0((EVP_PKEY
*)a
);
769 const EC_KEY
*eb
= EVP_PKEY_get0((EVP_PKEY
*)b
);
770 const EC_POINT
*ka
,*kb
;
772 if (!ea
|| !eb
) return 0;
773 ka
= EC_KEY_get0_public_key(ea
);
774 kb
= EC_KEY_get0_public_key(eb
);
775 if (!ka
|| !kb
) return 0;
776 ret
= (0==EC_POINT_cmp(EC_KEY_get0_group(ea
),ka
,kb
,NULL
)) ;
783 static int pkey_size_gost(const EVP_PKEY
*pk
)
788 static int pkey_bits_gost(const EVP_PKEY
*pk
)
792 /*------------------------ ASN1 METHOD for GOST MAC -------------------*/
793 static void mackey_free_gost(EVP_PKEY
*pk
)
796 OPENSSL_free(pk
->pkey
.ptr
);
799 static int mac_ctrl_gost(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
803 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
804 *(int *)arg2
= NID_undef
;
810 static int gost94_param_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
812 int nid
=gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY
*)pkey
));
813 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid
),pder
);
815 static int gost2001_param_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
817 int nid
= EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY
*)pkey
)));
818 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid
),pder
);
821 static int gost94_param_decode(EVP_PKEY
*pkey
, const unsigned char **pder
, int derlen
)
823 ASN1_OBJECT
*obj
=NULL
;
824 DSA
*dsa
= EVP_PKEY_get0(pkey
);
826 if (d2i_ASN1_OBJECT(&obj
,pder
,derlen
)==NULL
) {
829 nid
= OBJ_obj2nid(obj
);
830 ASN1_OBJECT_free(obj
);
834 if (!EVP_PKEY_assign(pkey
,NID_id_GostR3410_94
,dsa
)) return 0;
836 if (!fill_GOST94_params(dsa
,nid
)) return 0;
840 static int gost2001_param_decode(EVP_PKEY
*pkey
, const unsigned char **pder
, int derlen
) {
841 ASN1_OBJECT
*obj
=NULL
;
843 EC_KEY
*ec
= EVP_PKEY_get0(pkey
);
844 if (d2i_ASN1_OBJECT(&obj
,pder
,derlen
)==NULL
) {
847 nid
= OBJ_obj2nid(obj
);
848 ASN1_OBJECT_free(obj
);
852 if (!EVP_PKEY_assign(pkey
,NID_id_GostR3410_2001
,ec
)) return 0;
854 if (!fill_GOST2001_params(ec
, nid
)) return 0;
862 /* ----------------------------------------------------------------------*/
863 int register_ameth_gost (int nid
, EVP_PKEY_ASN1_METHOD
**ameth
, const char* pemstr
, const char* info
)
865 *ameth
= EVP_PKEY_asn1_new(nid
,
866 ASN1_PKEY_SIGPARAM_NULL
, pemstr
, info
);
867 if (!*ameth
) return 0;
870 case NID_id_GostR3410_94
:
871 EVP_PKEY_asn1_set_free (*ameth
, pkey_free_gost94
);
872 EVP_PKEY_asn1_set_private (*ameth
,
873 priv_decode_gost
, priv_encode_gost
,
876 EVP_PKEY_asn1_set_param (*ameth
,
877 gost94_param_decode
, gost94_param_encode
,
878 param_missing_gost94
, param_copy_gost94
,
879 param_cmp_gost94
,param_print_gost94
);
880 EVP_PKEY_asn1_set_public (*ameth
,
881 pub_decode_gost94
, pub_encode_gost94
,
882 pub_cmp_gost94
, pub_print_gost94
,
883 pkey_size_gost
, pkey_bits_gost
);
885 EVP_PKEY_asn1_set_ctrl (*ameth
, pkey_ctrl_gost
);
887 case NID_id_GostR3410_2001
:
888 EVP_PKEY_asn1_set_free (*ameth
, pkey_free_gost01
);
889 EVP_PKEY_asn1_set_private (*ameth
,
890 priv_decode_gost
, priv_encode_gost
,
893 EVP_PKEY_asn1_set_param (*ameth
,
894 gost2001_param_decode
, gost2001_param_encode
,
895 param_missing_gost01
, param_copy_gost01
,
896 param_cmp_gost01
, param_print_gost01
);
897 EVP_PKEY_asn1_set_public (*ameth
,
898 pub_decode_gost01
, pub_encode_gost01
,
899 pub_cmp_gost01
, pub_print_gost01
,
900 pkey_size_gost
, pkey_bits_gost
);
902 EVP_PKEY_asn1_set_ctrl (*ameth
, pkey_ctrl_gost
);
904 case NID_id_Gost28147_89_MAC
:
905 EVP_PKEY_asn1_set_free(*ameth
, mackey_free_gost
);
906 EVP_PKEY_asn1_set_ctrl(*ameth
,mac_ctrl_gost
);