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 #ifndef OPENSSL_NO_CMS
17 # include <openssl/cms.h>
19 #include "gost_params.h"
21 #include "e_gost_err.h"
23 int gost94_nid_by_params(DSA *p)
25 R3410_params *gost_params;
27 for (gost_params = R3410_paramset; gost_params->q != NULL; gost_params++) {
28 BN_dec2bn(&q, gost_params->q);
29 if (!BN_cmp(q, p->q)) {
31 return gost_params->nid;
38 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
40 ASN1_STRING *params = ASN1_STRING_new();
41 GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
42 int pkey_param_nid = NID_undef;
44 if (!params || !gkp) {
45 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
46 ASN1_STRING_free(params);
50 switch (EVP_PKEY_base_id(key)) {
51 case NID_id_GostR3410_2001:
53 EC_GROUP_get_curve_name(EC_KEY_get0_group
54 (EVP_PKEY_get0((EVP_PKEY *)key)));
56 case NID_id_GostR3410_94:
58 (int)gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)key));
59 if (pkey_param_nid == NID_undef) {
60 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS,
61 GOST_R_INVALID_GOST94_PARMSET);
62 ASN1_STRING_free(params);
68 gkp->key_params = OBJ_nid2obj(pkey_param_nid);
69 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
71 * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
73 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
74 if (params->length <= 0) {
75 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
76 ASN1_STRING_free(params);
80 params->type = V_ASN1_SEQUENCE;
82 GOST_KEY_PARAMS_free(gkp);
87 * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
90 static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg)
92 ASN1_OBJECT *palg_obj = NULL;
93 int ptype = V_ASN1_UNDEF;
94 int pkey_nid = NID_undef, param_nid = NID_undef;
96 ASN1_STRING *pval = NULL;
97 const unsigned char *p;
98 GOST_KEY_PARAMS *gkp = NULL;
100 X509_ALGOR_get0(&palg_obj, &ptype, &_pval, palg);
102 if (ptype != V_ASN1_SEQUENCE) {
103 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
104 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
108 pkey_nid = OBJ_obj2nid(palg_obj);
110 gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
112 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
113 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
116 param_nid = OBJ_obj2nid(gkp->key_params);
117 GOST_KEY_PARAMS_free(gkp);
118 if(!EVP_PKEY_set_type(pkey, pkey_nid)) {
119 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, ERR_R_INTERNAL_ERROR);
123 case NID_id_GostR3410_94:
125 DSA *dsa = EVP_PKEY_get0(pkey);
128 if (!EVP_PKEY_assign(pkey, pkey_nid, dsa))
131 if (!fill_GOST94_params(dsa, param_nid))
135 case NID_id_GostR3410_2001:
137 EC_KEY *ec = EVP_PKEY_get0(pkey);
140 if (!EVP_PKEY_assign(pkey, pkey_nid, ec))
143 if (!fill_GOST2001_params(ec, param_nid))
151 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
153 switch (EVP_PKEY_base_id(pkey)) {
154 case NID_id_GostR3410_94:
156 DSA *dsa = EVP_PKEY_get0(pkey);
159 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), dsa);
161 dsa->priv_key = BN_dup(priv);
162 if (!EVP_PKEY_missing_parameters(pkey))
163 gost94_compute_public(dsa);
166 case NID_id_GostR3410_2001:
168 EC_KEY *ec = EVP_PKEY_get0(pkey);
171 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
173 if (!EC_KEY_set_private_key(ec, priv))
175 if (!EVP_PKEY_missing_parameters(pkey))
176 gost2001_compute_public(ec);
183 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
185 switch (EVP_PKEY_base_id(pkey)) {
186 case NID_id_GostR3410_94:
188 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pkey);
194 return dsa->priv_key;
197 case NID_id_GostR3410_2001:
199 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
204 if (!(priv = EC_KEY_get0_private_key(ec)))
206 return (BIGNUM *)priv;
213 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
216 case ASN1_PKEY_CTRL_PKCS7_SIGN:
218 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
219 int nid = EVP_PKEY_base_id(pkey);
220 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2,
222 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
224 if (nid == NID_undef) {
227 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
230 #ifndef OPENSSL_NO_CMS
231 case ASN1_PKEY_CTRL_CMS_SIGN:
233 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
234 int nid = EVP_PKEY_base_id(pkey);
235 CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2,
236 NULL, NULL, &alg1, &alg2);
237 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
239 if (nid == NID_undef) {
242 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
246 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
249 ASN1_STRING *params = encode_gost_algor_params(pkey);
253 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg);
254 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type),
255 V_ASN1_SEQUENCE, params);
258 #ifndef OPENSSL_NO_CMS
259 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
261 X509_ALGOR *alg = NULL;
262 ASN1_STRING *params = encode_gost_algor_params(pkey);
266 CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
268 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type), V_ASN1_SEQUENCE,
273 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
274 *(int *)arg2 = NID_id_GostR3411_94;
281 /* --------------------- free functions * ------------------------------*/
282 static void pkey_free_gost94(EVP_PKEY *key)
285 DSA_free(key->pkey.dsa);
289 static void pkey_free_gost01(EVP_PKEY *key)
292 EC_KEY_free(key->pkey.ec);
296 /* ------------------ private key functions -----------------------------*/
297 static int priv_decode_gost(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf)
299 const unsigned char *pkey_buf = NULL, *p = NULL;
301 BIGNUM *pk_num = NULL;
303 X509_ALGOR *palg = NULL;
304 ASN1_OBJECT *palg_obj = NULL;
305 ASN1_INTEGER *priv_key = NULL;
307 if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
310 if (!decode_gost_algor_params(pk, palg)) {
313 if (V_ASN1_OCTET_STRING == *p) {
314 /* New format - Little endian octet string */
315 unsigned char rev_buf[32];
317 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
318 if (!s || s->length != 32) {
319 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
322 for (i = 0; i < 32; i++) {
323 rev_buf[31 - i] = s->data[i];
326 pk_num = getbnfrombuf(rev_buf, 32);
328 priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
331 ret = ((pk_num = ASN1_INTEGER_to_BN(priv_key, NULL)) != NULL);
332 ASN1_INTEGER_free(priv_key);
334 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
339 ret = gost_set_priv_key(pk, pk_num);
344 /* ----------------------------------------------------------------------*/
345 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
347 ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
348 ASN1_STRING *params = encode_gost_algor_params(pk);
349 unsigned char *priv_buf = NULL;
352 ASN1_INTEGER *asn1key = NULL;
356 asn1key = BN_to_ASN1_INTEGER(gost_get0_priv_key(pk), NULL);
357 priv_len = i2d_ASN1_INTEGER(asn1key, &priv_buf);
358 ASN1_INTEGER_free(asn1key);
359 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
363 /* --------- printing keys --------------------------------*/
364 static int print_gost_94(BIO *out, const EVP_PKEY *pkey, int indent,
365 ASN1_PCTX *pctx, int type)
367 int param_nid = NID_undef;
372 if (!BIO_indent(out, indent, 128))
374 BIO_printf(out, "Private key: ");
375 key = gost_get0_priv_key(pkey);
377 BIO_printf(out, "<undefined>");
380 BIO_printf(out, "\n");
385 pubkey = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
386 BIO_indent(out, indent, 128);
387 BIO_printf(out, "Public key: ");
388 BN_print(out, pubkey);
389 BIO_printf(out, "\n");
392 param_nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
393 BIO_indent(out, indent, 128);
394 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
398 static int param_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
401 return print_gost_94(out, pkey, indent, pctx, 0);
404 static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
407 return print_gost_94(out, pkey, indent, pctx, 1);
410 static int priv_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
413 return print_gost_94(out, pkey, indent, pctx, 2);
416 static int print_gost_01(BIO *out, const EVP_PKEY *pkey, int indent,
417 ASN1_PCTX *pctx, int type)
419 int param_nid = NID_undef;
423 if (!BIO_indent(out, indent, 128))
425 BIO_printf(out, "Private key: ");
426 key = gost_get0_priv_key(pkey);
428 BIO_printf(out, "<undefined)");
431 BIO_printf(out, "\n");
434 BN_CTX *ctx = BN_CTX_new();
436 const EC_POINT *pubkey;
437 const EC_GROUP *group;
440 GOSTerr(GOST_F_PRINT_GOST_01, ERR_R_MALLOC_FAILURE);
447 EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
448 group = EC_KEY_get0_group((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
449 if (!EC_POINT_get_affine_coordinates_GFp(group, pubkey, X, Y, ctx)) {
450 GOSTerr(GOST_F_PRINT_GOST_01, ERR_R_EC_LIB);
454 if (!BIO_indent(out, indent, 128))
456 BIO_printf(out, "Public key:\n");
457 if (!BIO_indent(out, indent + 3, 128))
459 BIO_printf(out, "X:");
461 BIO_printf(out, "\n");
462 BIO_indent(out, indent + 3, 128);
463 BIO_printf(out, "Y:");
465 BIO_printf(out, "\n");
471 EC_GROUP_get_curve_name(EC_KEY_get0_group
472 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
473 if (!BIO_indent(out, indent, 128))
475 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
479 static int param_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
482 return print_gost_01(out, pkey, indent, pctx, 0);
485 static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
488 return print_gost_01(out, pkey, indent, pctx, 1);
491 static int priv_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
494 return print_gost_01(out, pkey, indent, pctx, 2);
497 /* ---------------------------------------------------------------------*/
498 static int param_missing_gost94(const EVP_PKEY *pk)
500 const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
508 static int param_missing_gost01(const EVP_PKEY *pk)
510 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
513 if (!EC_KEY_get0_group(ec))
518 static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from)
520 const DSA *dfrom = EVP_PKEY_get0((EVP_PKEY *)from);
521 DSA *dto = EVP_PKEY_get0(to);
522 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
523 GOSTerr(GOST_F_PARAM_COPY_GOST94, GOST_R_INCOMPATIBLE_ALGORITHMS);
527 GOSTerr(GOST_F_PARAM_COPY_GOST94, GOST_R_KEY_PARAMETERS_MISSING);
532 EVP_PKEY_assign(to, EVP_PKEY_base_id(from), dto);
534 #define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x);
535 COPYBIGNUM(dto, dfrom, p)
536 COPYBIGNUM(dto, dfrom, q)
537 COPYBIGNUM(dto, dfrom, g)
540 gost94_compute_public(dto);
544 static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from)
546 EC_KEY *eto = EVP_PKEY_get0(to);
547 const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
548 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
549 GOSTerr(GOST_F_PARAM_COPY_GOST01, GOST_R_INCOMPATIBLE_ALGORITHMS);
553 GOSTerr(GOST_F_PARAM_COPY_GOST01, GOST_R_KEY_PARAMETERS_MISSING);
559 GOSTerr(GOST_F_PARAM_COPY_GOST01, ERR_R_MALLOC_FAILURE);
562 if(!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
563 GOSTerr(GOST_F_PARAM_COPY_GOST01, ERR_R_INTERNAL_ERROR);
567 if(!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
568 GOSTerr(GOST_F_PARAM_COPY_GOST01, ERR_R_INTERNAL_ERROR);
571 if (EC_KEY_get0_private_key(eto)) {
572 gost2001_compute_public(eto);
577 static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
579 const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
580 const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
581 if (!BN_cmp(da->q, db->q))
586 static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
588 if (EC_GROUP_get_curve_name
589 (EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)a))) ==
590 EC_GROUP_get_curve_name(EC_KEY_get0_group
591 (EVP_PKEY_get0((EVP_PKEY *)b)))) {
598 /* ---------- Public key functions * --------------------------------------*/
599 static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub)
601 X509_ALGOR *palg = NULL;
602 const unsigned char *pubkey_buf = NULL;
603 unsigned char *databuf;
604 ASN1_OBJECT *palgobj = NULL;
607 ASN1_OCTET_STRING *octet = NULL;
609 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
611 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
612 if (!decode_gost_algor_params(pk, palg))
614 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
616 GOSTerr(GOST_F_PUB_DECODE_GOST94, ERR_R_MALLOC_FAILURE);
619 databuf = OPENSSL_malloc(octet->length);
620 if (databuf == NULL) {
621 GOSTerr(GOST_F_PUB_DECODE_GOST94, ERR_R_MALLOC_FAILURE);
624 for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) {
625 databuf[j] = octet->data[i];
627 dsa = EVP_PKEY_get0(pk);
628 dsa->pub_key = BN_bin2bn(databuf, octet->length, NULL);
629 ASN1_OCTET_STRING_free(octet);
630 OPENSSL_free(databuf);
635 static int pub_encode_gost94(X509_PUBKEY *pub, const EVP_PKEY *pk)
637 ASN1_OBJECT *algobj = NULL;
638 ASN1_OCTET_STRING *octet = NULL;
640 unsigned char *buf = NULL, *databuf, *sptr;
641 int i, j, data_len, ret = 0;
643 int ptype = V_ASN1_UNDEF;
644 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
645 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
646 if (pk->save_parameters) {
647 ASN1_STRING *params = encode_gost_algor_params(pk);
649 ptype = V_ASN1_SEQUENCE;
651 data_len = BN_num_bytes(dsa->pub_key);
652 databuf = OPENSSL_malloc(data_len);
655 BN_bn2bin(dsa->pub_key, databuf);
656 octet = ASN1_OCTET_STRING_new();
657 ASN1_STRING_set(octet, NULL, data_len);
658 sptr = ASN1_STRING_data(octet);
659 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
660 sptr[i] = databuf[j];
662 OPENSSL_free(databuf);
663 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
664 ASN1_BIT_STRING_free(octet);
667 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
670 static int pub_decode_gost01(EVP_PKEY *pk, X509_PUBKEY *pub)
672 X509_ALGOR *palg = NULL;
673 const unsigned char *pubkey_buf = NULL;
674 unsigned char *databuf;
675 ASN1_OBJECT *palgobj = NULL;
679 ASN1_OCTET_STRING *octet = NULL;
681 const EC_GROUP *group;
683 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
685 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
686 if (!decode_gost_algor_params(pk, palg))
688 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
689 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
691 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_MALLOC_FAILURE);
694 databuf = OPENSSL_malloc(octet->length);
695 if (databuf == NULL) {
696 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_MALLOC_FAILURE);
699 for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) {
700 databuf[j] = octet->data[i];
702 len = octet->length / 2;
703 ASN1_OCTET_STRING_free(octet);
705 Y = getbnfrombuf(databuf, len);
706 X = getbnfrombuf(databuf + len, len);
707 OPENSSL_free(databuf);
708 pub_key = EC_POINT_new(group);
709 if (!EC_POINT_set_affine_coordinates_GFp(group, pub_key, X, Y, NULL)) {
710 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_EC_LIB);
711 EC_POINT_free(pub_key);
718 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
719 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_EC_LIB);
720 EC_POINT_free(pub_key);
723 EC_POINT_free(pub_key);
728 static int pub_encode_gost01(X509_PUBKEY *pub, const EVP_PKEY *pk)
730 ASN1_OBJECT *algobj = NULL;
731 ASN1_OCTET_STRING *octet = NULL;
733 unsigned char *buf = NULL, *databuf, *sptr;
734 int i, j, data_len, ret = 0;
735 const EC_POINT *pub_key;
736 BIGNUM *X, *Y, *order;
737 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
738 int ptype = V_ASN1_UNDEF;
740 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
741 if (pk->save_parameters) {
742 ASN1_STRING *params = encode_gost_algor_params(pk);
744 ptype = V_ASN1_SEQUENCE;
747 EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
748 pub_key = EC_KEY_get0_public_key(ec);
750 GOSTerr(GOST_F_PUB_ENCODE_GOST01, GOST_R_PUBLIC_KEY_UNDEFINED);
756 GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
762 if(!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
763 pub_key, X, Y, NULL)) {
764 GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_INTERNAL_ERROR);
770 data_len = 2 * BN_num_bytes(order);
772 databuf = OPENSSL_malloc(data_len);
773 if (databuf == NULL) {
774 GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
777 memset(databuf, 0, data_len);
779 store_bignum(X, databuf + data_len / 2, data_len / 2);
780 store_bignum(Y, databuf, data_len / 2);
784 octet = ASN1_OCTET_STRING_new();
785 ASN1_STRING_set(octet, NULL, data_len);
786 sptr = ASN1_STRING_data(octet);
787 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
788 sptr[i] = databuf[j];
790 OPENSSL_free(databuf);
791 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
792 ASN1_BIT_STRING_free(octet);
795 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
798 static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
800 const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
801 const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
802 if (da && db && da->pub_key && db->pub_key
803 && !BN_cmp(da->pub_key, db->pub_key)) {
809 static int pub_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
811 const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
812 const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
813 const EC_POINT *ka, *kb;
817 ka = EC_KEY_get0_public_key(ea);
818 kb = EC_KEY_get0_public_key(eb);
821 ret = (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
825 static int pkey_size_gost(const EVP_PKEY *pk)
830 static int pkey_bits_gost(const EVP_PKEY *pk)
835 /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/
836 static void mackey_free_gost(EVP_PKEY *pk)
839 OPENSSL_free(pk->pkey.ptr);
843 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
846 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
847 *(int *)arg2 = NID_id_Gost28147_89_MAC;
853 static int gost94_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
855 int nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
856 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
859 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
862 EC_GROUP_get_curve_name(EC_KEY_get0_group
863 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
864 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
867 static int gost94_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
870 ASN1_OBJECT *obj = NULL;
871 DSA *dsa = EVP_PKEY_get0(pkey);
873 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
876 nid = OBJ_obj2nid(obj);
877 ASN1_OBJECT_free(obj);
880 if (!EVP_PKEY_assign(pkey, NID_id_GostR3410_94, dsa))
883 if (!fill_GOST94_params(dsa, nid))
888 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
891 ASN1_OBJECT *obj = NULL;
893 EC_KEY *ec = EVP_PKEY_get0(pkey);
894 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
897 nid = OBJ_obj2nid(obj);
898 ASN1_OBJECT_free(obj);
901 if (!EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec))
904 if (!fill_GOST2001_params(ec, nid))
909 /* ----------------------------------------------------------------------*/
910 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
911 const char *pemstr, const char *info)
913 *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
917 case NID_id_GostR3410_94:
918 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost94);
919 EVP_PKEY_asn1_set_private(*ameth,
920 priv_decode_gost, priv_encode_gost,
923 EVP_PKEY_asn1_set_param(*ameth,
924 gost94_param_decode, gost94_param_encode,
925 param_missing_gost94, param_copy_gost94,
926 param_cmp_gost94, param_print_gost94);
927 EVP_PKEY_asn1_set_public(*ameth,
928 pub_decode_gost94, pub_encode_gost94,
929 pub_cmp_gost94, pub_print_gost94,
930 pkey_size_gost, pkey_bits_gost);
932 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
934 case NID_id_GostR3410_2001:
935 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost01);
936 EVP_PKEY_asn1_set_private(*ameth,
937 priv_decode_gost, priv_encode_gost,
940 EVP_PKEY_asn1_set_param(*ameth,
941 gost2001_param_decode, gost2001_param_encode,
942 param_missing_gost01, param_copy_gost01,
943 param_cmp_gost01, param_print_gost01);
944 EVP_PKEY_asn1_set_public(*ameth,
945 pub_decode_gost01, pub_encode_gost01,
946 pub_cmp_gost01, pub_print_gost01,
947 pkey_size_gost, pkey_bits_gost);
949 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
951 case NID_id_Gost28147_89_MAC:
952 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
953 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);