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 EVP_PKEY_set_type(pkey, pkey_nid);
120 case NID_id_GostR3410_94:
122 DSA *dsa = EVP_PKEY_get0(pkey);
125 if (!EVP_PKEY_assign(pkey, pkey_nid, dsa))
128 if (!fill_GOST94_params(dsa, param_nid))
132 case NID_id_GostR3410_2001:
134 EC_KEY *ec = EVP_PKEY_get0(pkey);
137 if (!EVP_PKEY_assign(pkey, pkey_nid, ec))
140 if (!fill_GOST2001_params(ec, param_nid))
148 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
150 switch (EVP_PKEY_base_id(pkey)) {
151 case NID_id_GostR3410_94:
153 DSA *dsa = EVP_PKEY_get0(pkey);
156 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), dsa);
158 dsa->priv_key = BN_dup(priv);
159 if (!EVP_PKEY_missing_parameters(pkey))
160 gost94_compute_public(dsa);
163 case NID_id_GostR3410_2001:
165 EC_KEY *ec = EVP_PKEY_get0(pkey);
168 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
170 if (!EC_KEY_set_private_key(ec, priv))
172 if (!EVP_PKEY_missing_parameters(pkey))
173 gost2001_compute_public(ec);
180 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
182 switch (EVP_PKEY_base_id(pkey)) {
183 case NID_id_GostR3410_94:
185 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pkey);
191 return dsa->priv_key;
193 case NID_id_GostR3410_2001:
195 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
200 if (!(priv = EC_KEY_get0_private_key(ec)))
202 return (BIGNUM *)priv;
208 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
211 case ASN1_PKEY_CTRL_PKCS7_SIGN:
213 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
214 int nid = EVP_PKEY_base_id(pkey);
215 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2,
217 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
219 if (nid == NID_undef) {
222 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
225 #ifndef OPENSSL_NO_CMS
226 case ASN1_PKEY_CTRL_CMS_SIGN:
228 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
229 int nid = EVP_PKEY_base_id(pkey);
230 CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2,
231 NULL, NULL, &alg1, &alg2);
232 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
234 if (nid == NID_undef) {
237 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
241 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
244 ASN1_STRING *params = encode_gost_algor_params(pkey);
248 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg);
249 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type),
250 V_ASN1_SEQUENCE, params);
253 #ifndef OPENSSL_NO_CMS
254 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
256 X509_ALGOR *alg = NULL;
257 ASN1_STRING *params = encode_gost_algor_params(pkey);
261 CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
263 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type), V_ASN1_SEQUENCE,
268 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
269 *(int *)arg2 = NID_id_GostR3411_94;
276 /* --------------------- free functions * ------------------------------*/
277 static void pkey_free_gost94(EVP_PKEY *key)
280 DSA_free(key->pkey.dsa);
284 static void pkey_free_gost01(EVP_PKEY *key)
287 EC_KEY_free(key->pkey.ec);
291 /* ------------------ private key functions -----------------------------*/
292 static int priv_decode_gost(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf)
294 const unsigned char *pkey_buf = NULL, *p = NULL;
296 BIGNUM *pk_num = NULL;
298 X509_ALGOR *palg = NULL;
299 ASN1_OBJECT *palg_obj = NULL;
300 ASN1_INTEGER *priv_key = NULL;
302 if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
305 if (!decode_gost_algor_params(pk, palg)) {
308 if (V_ASN1_OCTET_STRING == *p) {
309 /* New format - Little endian octet string */
310 unsigned char rev_buf[32];
312 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
313 if (!s || s->length != 32) {
314 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
317 for (i = 0; i < 32; i++) {
318 rev_buf[31 - i] = s->data[i];
321 pk_num = getbnfrombuf(rev_buf, 32);
323 priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
326 ret = ((pk_num = ASN1_INTEGER_to_BN(priv_key, NULL)) != NULL);
327 ASN1_INTEGER_free(priv_key);
329 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
334 ret = gost_set_priv_key(pk, pk_num);
339 /* ----------------------------------------------------------------------*/
340 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
342 ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
343 ASN1_STRING *params = encode_gost_algor_params(pk);
344 unsigned char *priv_buf = NULL;
347 ASN1_INTEGER *asn1key = NULL;
351 asn1key = BN_to_ASN1_INTEGER(gost_get0_priv_key(pk), NULL);
352 priv_len = i2d_ASN1_INTEGER(asn1key, &priv_buf);
353 ASN1_INTEGER_free(asn1key);
354 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
358 /* --------- printing keys --------------------------------*/
359 static int print_gost_94(BIO *out, const EVP_PKEY *pkey, int indent,
360 ASN1_PCTX *pctx, int type)
362 int param_nid = NID_undef;
367 if (!BIO_indent(out, indent, 128))
369 BIO_printf(out, "Private key: ");
370 key = gost_get0_priv_key(pkey);
372 BIO_printf(out, "<undefined>");
375 BIO_printf(out, "\n");
380 pubkey = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
381 BIO_indent(out, indent, 128);
382 BIO_printf(out, "Public key: ");
383 BN_print(out, pubkey);
384 BIO_printf(out, "\n");
387 param_nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
388 BIO_indent(out, indent, 128);
389 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
393 static int param_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
396 return print_gost_94(out, pkey, indent, pctx, 0);
399 static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
402 return print_gost_94(out, pkey, indent, pctx, 1);
405 static int priv_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
408 return print_gost_94(out, pkey, indent, pctx, 2);
411 static int print_gost_01(BIO *out, const EVP_PKEY *pkey, int indent,
412 ASN1_PCTX *pctx, int type)
414 int param_nid = NID_undef;
418 if (!BIO_indent(out, indent, 128))
420 BIO_printf(out, "Private key: ");
421 key = gost_get0_priv_key(pkey);
423 BIO_printf(out, "<undefined)");
426 BIO_printf(out, "\n");
429 BN_CTX *ctx = BN_CTX_new();
431 const EC_POINT *pubkey;
432 const EC_GROUP *group;
435 GOSTerr(GOST_F_PRINT_GOST_01, ERR_R_MALLOC_FAILURE);
442 EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
443 group = EC_KEY_get0_group((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
444 if (!EC_POINT_get_affine_coordinates_GFp(group, pubkey, X, Y, ctx)) {
445 GOSTerr(GOST_F_PRINT_GOST_01, ERR_R_EC_LIB);
449 if (!BIO_indent(out, indent, 128))
451 BIO_printf(out, "Public key:\n");
452 if (!BIO_indent(out, indent + 3, 128))
454 BIO_printf(out, "X:");
456 BIO_printf(out, "\n");
457 BIO_indent(out, indent + 3, 128);
458 BIO_printf(out, "Y:");
460 BIO_printf(out, "\n");
466 EC_GROUP_get_curve_name(EC_KEY_get0_group
467 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
468 if (!BIO_indent(out, indent, 128))
470 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
474 static int param_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
477 return print_gost_01(out, pkey, indent, pctx, 0);
480 static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
483 return print_gost_01(out, pkey, indent, pctx, 1);
486 static int priv_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
489 return print_gost_01(out, pkey, indent, pctx, 2);
492 /* ---------------------------------------------------------------------*/
493 static int param_missing_gost94(const EVP_PKEY *pk)
495 const DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
503 static int param_missing_gost01(const EVP_PKEY *pk)
505 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
508 if (!EC_KEY_get0_group(ec))
513 static int param_copy_gost94(EVP_PKEY *to, const EVP_PKEY *from)
515 const DSA *dfrom = EVP_PKEY_get0((EVP_PKEY *)from);
516 DSA *dto = EVP_PKEY_get0(to);
517 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
518 GOSTerr(GOST_F_PARAM_COPY_GOST94, GOST_R_INCOMPATIBLE_ALGORITHMS);
522 GOSTerr(GOST_F_PARAM_COPY_GOST94, GOST_R_KEY_PARAMETERS_MISSING);
527 EVP_PKEY_assign(to, EVP_PKEY_base_id(from), dto);
529 #define COPYBIGNUM(a,b,x) if (a->x) BN_free(a->x); a->x=BN_dup(b->x);
530 COPYBIGNUM(dto, dfrom, p)
531 COPYBIGNUM(dto, dfrom, q)
532 COPYBIGNUM(dto, dfrom, g)
535 gost94_compute_public(dto);
539 static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from)
541 EC_KEY *eto = EVP_PKEY_get0(to);
542 const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
543 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
544 GOSTerr(GOST_F_PARAM_COPY_GOST01, GOST_R_INCOMPATIBLE_ALGORITHMS);
548 GOSTerr(GOST_F_PARAM_COPY_GOST01, GOST_R_KEY_PARAMETERS_MISSING);
553 EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto);
555 EC_KEY_set_group(eto, EC_KEY_get0_group(efrom));
556 if (EC_KEY_get0_private_key(eto)) {
557 gost2001_compute_public(eto);
562 static int param_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
564 const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
565 const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
566 if (!BN_cmp(da->q, db->q))
571 static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
573 if (EC_GROUP_get_curve_name
574 (EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)a))) ==
575 EC_GROUP_get_curve_name(EC_KEY_get0_group
576 (EVP_PKEY_get0((EVP_PKEY *)b)))) {
583 /* ---------- Public key functions * --------------------------------------*/
584 static int pub_decode_gost94(EVP_PKEY *pk, X509_PUBKEY *pub)
586 X509_ALGOR *palg = NULL;
587 const unsigned char *pubkey_buf = NULL;
588 unsigned char *databuf;
589 ASN1_OBJECT *palgobj = NULL;
592 ASN1_OCTET_STRING *octet = NULL;
594 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
596 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
597 if (!decode_gost_algor_params(pk, palg))
599 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
601 GOSTerr(GOST_F_PUB_DECODE_GOST94, ERR_R_MALLOC_FAILURE);
604 databuf = OPENSSL_malloc(octet->length);
605 if (databuf == NULL) {
606 GOSTerr(GOST_F_PUB_DECODE_GOST94, ERR_R_MALLOC_FAILURE);
607 ASN1_OCTET_STRING_free(octet);
610 for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) {
611 databuf[j] = octet->data[i];
613 dsa = EVP_PKEY_get0(pk);
614 dsa->pub_key = BN_bin2bn(databuf, octet->length, NULL);
615 ASN1_OCTET_STRING_free(octet);
616 OPENSSL_free(databuf);
621 static int pub_encode_gost94(X509_PUBKEY *pub, const EVP_PKEY *pk)
623 ASN1_OBJECT *algobj = NULL;
624 ASN1_OCTET_STRING *octet = NULL;
626 unsigned char *buf = NULL, *databuf, *sptr;
627 int i, j, data_len, ret = 0;
629 int ptype = V_ASN1_UNDEF;
630 DSA *dsa = EVP_PKEY_get0((EVP_PKEY *)pk);
631 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
632 if (pk->save_parameters) {
633 ASN1_STRING *params = encode_gost_algor_params(pk);
635 ptype = V_ASN1_SEQUENCE;
637 data_len = BN_num_bytes(dsa->pub_key);
638 databuf = OPENSSL_malloc(data_len);
639 if (databuf == NULL) {
640 GOSTerr(GOST_F_PUB_ENCODE_GOST94, ERR_R_MALLOC_FAILURE);
643 BN_bn2bin(dsa->pub_key, databuf);
644 octet = ASN1_OCTET_STRING_new();
646 GOSTerr(GOST_F_PUB_ENCODE_GOST94, ERR_R_MALLOC_FAILURE);
647 OPENSSL_free(databuf);
650 ASN1_STRING_set(octet, NULL, data_len);
651 sptr = ASN1_STRING_data(octet);
652 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
653 sptr[i] = databuf[j];
655 OPENSSL_free(databuf);
656 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
657 ASN1_BIT_STRING_free(octet);
660 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
663 static int pub_decode_gost01(EVP_PKEY *pk, X509_PUBKEY *pub)
665 X509_ALGOR *palg = NULL;
666 const unsigned char *pubkey_buf = NULL;
667 unsigned char *databuf;
668 ASN1_OBJECT *palgobj = NULL;
672 ASN1_OCTET_STRING *octet = NULL;
674 const EC_GROUP *group;
676 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
678 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
679 if (!decode_gost_algor_params(pk, palg))
681 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
682 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
684 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_MALLOC_FAILURE);
687 databuf = OPENSSL_malloc(octet->length);
688 if (databuf == NULL) {
689 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_MALLOC_FAILURE);
690 ASN1_OCTET_STRING_free(octet);
693 for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) {
694 databuf[j] = octet->data[i];
696 len = octet->length / 2;
697 ASN1_OCTET_STRING_free(octet);
699 Y = getbnfrombuf(databuf, len);
700 X = getbnfrombuf(databuf + len, len);
701 OPENSSL_free(databuf);
702 pub_key = EC_POINT_new(group);
703 if (!EC_POINT_set_affine_coordinates_GFp(group, pub_key, X, Y, NULL)) {
704 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_EC_LIB);
705 EC_POINT_free(pub_key);
712 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
713 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_EC_LIB);
714 EC_POINT_free(pub_key);
717 EC_POINT_free(pub_key);
722 static int pub_encode_gost01(X509_PUBKEY *pub, const EVP_PKEY *pk)
724 ASN1_OBJECT *algobj = NULL;
725 ASN1_OCTET_STRING *octet = NULL;
727 unsigned char *buf = NULL, *databuf, *sptr;
728 int i, j, data_len, ret = 0;
729 const EC_POINT *pub_key;
730 BIGNUM *X, *Y, *order;
731 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
732 int ptype = V_ASN1_UNDEF;
734 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
735 if (pk->save_parameters) {
736 ASN1_STRING *params = encode_gost_algor_params(pk);
738 ptype = V_ASN1_SEQUENCE;
741 EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
742 pub_key = EC_KEY_get0_public_key(ec);
744 GOSTerr(GOST_F_PUB_ENCODE_GOST01, GOST_R_PUBLIC_KEY_UNDEFINED);
750 EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
751 pub_key, X, Y, NULL);
752 data_len = 2 * BN_num_bytes(order);
754 databuf = OPENSSL_malloc(data_len);
755 if (databuf == NULL) {
756 GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
761 memset(databuf, 0, data_len);
763 store_bignum(X, databuf + data_len / 2, data_len / 2);
764 store_bignum(Y, databuf, data_len / 2);
768 octet = ASN1_OCTET_STRING_new();
770 GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
771 OPENSSL_free(databuf);
774 ASN1_STRING_set(octet, NULL, data_len);
775 sptr = ASN1_STRING_data(octet);
776 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
777 sptr[i] = databuf[j];
779 OPENSSL_free(databuf);
780 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
781 ASN1_BIT_STRING_free(octet);
784 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
787 static int pub_cmp_gost94(const EVP_PKEY *a, const EVP_PKEY *b)
789 const DSA *da = EVP_PKEY_get0((EVP_PKEY *)a);
790 const DSA *db = EVP_PKEY_get0((EVP_PKEY *)b);
791 if (da && db && da->pub_key && db->pub_key
792 && !BN_cmp(da->pub_key, db->pub_key)) {
798 static int pub_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
800 const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
801 const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
802 const EC_POINT *ka, *kb;
806 ka = EC_KEY_get0_public_key(ea);
807 kb = EC_KEY_get0_public_key(eb);
810 ret = (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
814 static int pkey_size_gost(const EVP_PKEY *pk)
819 static int pkey_bits_gost(const EVP_PKEY *pk)
824 /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/
825 static void mackey_free_gost(EVP_PKEY *pk)
828 OPENSSL_free(pk->pkey.ptr);
832 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
835 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
836 *(int *)arg2 = NID_id_Gost28147_89_MAC;
842 static int gost94_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
844 int nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
845 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
848 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
851 EC_GROUP_get_curve_name(EC_KEY_get0_group
852 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
853 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
856 static int gost94_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
859 ASN1_OBJECT *obj = NULL;
860 DSA *dsa = EVP_PKEY_get0(pkey);
862 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
865 nid = OBJ_obj2nid(obj);
866 ASN1_OBJECT_free(obj);
869 if (!EVP_PKEY_assign(pkey, NID_id_GostR3410_94, dsa))
872 if (!fill_GOST94_params(dsa, nid))
877 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
880 ASN1_OBJECT *obj = NULL;
882 EC_KEY *ec = EVP_PKEY_get0(pkey);
883 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
886 nid = OBJ_obj2nid(obj);
887 ASN1_OBJECT_free(obj);
890 if (!EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec))
893 if (!fill_GOST2001_params(ec, nid))
898 /* ----------------------------------------------------------------------*/
899 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
900 const char *pemstr, const char *info)
902 *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
906 case NID_id_GostR3410_94:
907 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost94);
908 EVP_PKEY_asn1_set_private(*ameth,
909 priv_decode_gost, priv_encode_gost,
912 EVP_PKEY_asn1_set_param(*ameth,
913 gost94_param_decode, gost94_param_encode,
914 param_missing_gost94, param_copy_gost94,
915 param_cmp_gost94, param_print_gost94);
916 EVP_PKEY_asn1_set_public(*ameth,
917 pub_decode_gost94, pub_encode_gost94,
918 pub_cmp_gost94, pub_print_gost94,
919 pkey_size_gost, pkey_bits_gost);
921 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
923 case NID_id_GostR3410_2001:
924 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost01);
925 EVP_PKEY_asn1_set_private(*ameth,
926 priv_decode_gost, priv_encode_gost,
929 EVP_PKEY_asn1_set_param(*ameth,
930 gost2001_param_decode, gost2001_param_encode,
931 param_missing_gost01, param_copy_gost01,
932 param_cmp_gost01, param_print_gost01);
933 EVP_PKEY_asn1_set_public(*ameth,
934 pub_decode_gost01, pub_encode_gost01,
935 pub_cmp_gost01, pub_print_gost01,
936 pkey_size_gost, pkey_bits_gost);
938 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
940 case NID_id_Gost28147_89_MAC:
941 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
942 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);