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>
20 #include "e_gost_err.h"
23 /* Convert little-endian byte array into bignum */
24 BIGNUM *hashsum2bn(const unsigned char *dgst)
26 unsigned char buf[32];
28 BUF_reverse(buf, (unsigned char*)dgst, 32);
29 return BN_bin2bn(buf, 32, NULL);
33 * Pack bignum into byte buffer of given size, filling all leading bytes by
36 int store_bignum(BIGNUM *bn, unsigned char *buf, int len)
38 int bytes = BN_num_bytes(bn);
43 BN_bn2bin(bn, buf + len - bytes);
47 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
49 ASN1_STRING *params = ASN1_STRING_new();
50 GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
51 int pkey_param_nid = NID_undef;
53 if (!params || !gkp) {
54 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
55 ASN1_STRING_free(params);
59 switch (EVP_PKEY_base_id(key)) {
60 case NID_id_GostR3410_2001:
62 EC_GROUP_get_curve_name(EC_KEY_get0_group
63 (EVP_PKEY_get0((EVP_PKEY *)key)));
66 gkp->key_params = OBJ_nid2obj(pkey_param_nid);
67 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
69 * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
71 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
72 if (params->length <= 0) {
73 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
74 ASN1_STRING_free(params);
78 params->type = V_ASN1_SEQUENCE;
80 GOST_KEY_PARAMS_free(gkp);
85 * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
88 static int decode_gost_algor_params(EVP_PKEY *pkey, X509_ALGOR *palg)
90 ASN1_OBJECT *palg_obj = NULL;
91 int ptype = V_ASN1_UNDEF;
92 int pkey_nid = NID_undef, param_nid = NID_undef;
94 ASN1_STRING *pval = NULL;
95 const unsigned char *p;
96 GOST_KEY_PARAMS *gkp = NULL;
98 X509_ALGOR_get0(&palg_obj, &ptype, &_pval, palg);
100 if (ptype != V_ASN1_SEQUENCE) {
101 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
102 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
106 pkey_nid = OBJ_obj2nid(palg_obj);
108 gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
110 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
111 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
114 param_nid = OBJ_obj2nid(gkp->key_params);
115 GOST_KEY_PARAMS_free(gkp);
116 if (!EVP_PKEY_set_type(pkey, pkey_nid)) {
117 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, ERR_R_INTERNAL_ERROR);
121 case NID_id_GostR3410_2001:
123 EC_KEY *ec = EVP_PKEY_get0(pkey);
126 if (!EVP_PKEY_assign(pkey, pkey_nid, ec))
129 if (!fill_GOST2001_params(ec, param_nid))
137 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
139 switch (EVP_PKEY_base_id(pkey)) {
140 case NID_id_GostR3410_2001:
142 EC_KEY *ec = EVP_PKEY_get0(pkey);
145 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
147 if (!EC_KEY_set_private_key(ec, priv))
149 if (!EVP_PKEY_missing_parameters(pkey))
150 gost2001_compute_public(ec);
157 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
159 switch (EVP_PKEY_base_id(pkey)) {
160 case NID_id_GostR3410_2001:
162 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
167 if ((priv = EC_KEY_get0_private_key(ec)) == NULL)
169 return (BIGNUM *)priv;
175 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
178 case ASN1_PKEY_CTRL_PKCS7_SIGN:
180 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
181 int nid = EVP_PKEY_base_id(pkey);
182 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2,
184 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
186 if (nid == NID_undef) {
189 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
192 #ifndef OPENSSL_NO_CMS
193 case ASN1_PKEY_CTRL_CMS_SIGN:
195 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
196 int nid = EVP_PKEY_base_id(pkey);
197 CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2,
198 NULL, NULL, &alg1, &alg2);
199 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_id_GostR3411_94),
201 if (nid == NID_undef) {
204 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
208 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
211 ASN1_STRING *params = encode_gost_algor_params(pkey);
215 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg);
216 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type),
217 V_ASN1_SEQUENCE, params);
220 #ifndef OPENSSL_NO_CMS
221 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
223 X509_ALGOR *alg = NULL;
224 ASN1_STRING *params = encode_gost_algor_params(pkey);
228 CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
230 X509_ALGOR_set0(alg, OBJ_nid2obj(pkey->type), V_ASN1_SEQUENCE,
235 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
236 *(int *)arg2 = NID_id_GostR3411_94;
243 /* --------------------- free functions * ------------------------------*/
244 static void pkey_free_gost01(EVP_PKEY *key)
246 EC_KEY_free(key->pkey.ec);
249 /* ------------------ private key functions -----------------------------*/
250 static int priv_decode_gost(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf)
252 const unsigned char *pkey_buf = NULL, *p = NULL;
254 BIGNUM *pk_num = NULL;
256 X509_ALGOR *palg = NULL;
257 ASN1_OBJECT *palg_obj = NULL;
258 ASN1_INTEGER *priv_key = NULL;
260 if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
263 if (!decode_gost_algor_params(pk, palg)) {
266 if (V_ASN1_OCTET_STRING == *p) {
267 /* New format - Little endian octet string */
268 unsigned char rev_buf[32];
270 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
271 if (!s || s->length != 32) {
272 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
275 for (i = 0; i < 32; i++) {
276 rev_buf[31 - i] = s->data[i];
279 pk_num = getbnfrombuf(rev_buf, 32);
281 priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
284 ret = ((pk_num = ASN1_INTEGER_to_BN(priv_key, NULL)) != NULL);
285 ASN1_INTEGER_free(priv_key);
287 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
292 ret = gost_set_priv_key(pk, pk_num);
297 /* ----------------------------------------------------------------------*/
298 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
300 ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
301 ASN1_STRING *params = encode_gost_algor_params(pk);
302 unsigned char *priv_buf = NULL;
305 ASN1_INTEGER *asn1key = NULL;
309 asn1key = BN_to_ASN1_INTEGER(gost_get0_priv_key(pk), NULL);
310 priv_len = i2d_ASN1_INTEGER(asn1key, &priv_buf);
311 ASN1_INTEGER_free(asn1key);
312 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
316 /* --------- printing keys --------------------------------*/
317 static int print_gost_01(BIO *out, const EVP_PKEY *pkey, int indent,
318 ASN1_PCTX *pctx, int type)
320 int param_nid = NID_undef;
324 if (!BIO_indent(out, indent, 128))
326 BIO_printf(out, "Private key: ");
327 key = gost_get0_priv_key(pkey);
329 BIO_printf(out, "<undefined)");
332 BIO_printf(out, "\n");
335 BN_CTX *ctx = BN_CTX_new();
337 const EC_POINT *pubkey;
338 const EC_GROUP *group;
341 GOSTerr(GOST_F_PRINT_GOST_01, ERR_R_MALLOC_FAILURE);
348 EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
349 group = EC_KEY_get0_group((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
350 if (!EC_POINT_get_affine_coordinates_GFp(group, pubkey, X, Y, ctx)) {
351 GOSTerr(GOST_F_PRINT_GOST_01, ERR_R_EC_LIB);
355 if (!BIO_indent(out, indent, 128))
357 BIO_printf(out, "Public key:\n");
358 if (!BIO_indent(out, indent + 3, 128))
360 BIO_printf(out, "X:");
362 BIO_printf(out, "\n");
363 BIO_indent(out, indent + 3, 128);
364 BIO_printf(out, "Y:");
366 BIO_printf(out, "\n");
372 EC_GROUP_get_curve_name(EC_KEY_get0_group
373 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
374 if (!BIO_indent(out, indent, 128))
376 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
380 static int param_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
383 return print_gost_01(out, pkey, indent, pctx, 0);
386 static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
389 return print_gost_01(out, pkey, indent, pctx, 1);
392 static int priv_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
395 return print_gost_01(out, pkey, indent, pctx, 2);
398 /* ---------------------------------------------------------------------*/
399 static int param_missing_gost01(const EVP_PKEY *pk)
401 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
404 if (!EC_KEY_get0_group(ec))
410 static int param_copy_gost01(EVP_PKEY *to, const EVP_PKEY *from)
412 EC_KEY *eto = EVP_PKEY_get0(to);
413 const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
414 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
415 GOSTerr(GOST_F_PARAM_COPY_GOST01, GOST_R_INCOMPATIBLE_ALGORITHMS);
419 GOSTerr(GOST_F_PARAM_COPY_GOST01, GOST_R_KEY_PARAMETERS_MISSING);
425 GOSTerr(GOST_F_PARAM_COPY_GOST01, ERR_R_MALLOC_FAILURE);
428 if (!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
429 GOSTerr(GOST_F_PARAM_COPY_GOST01, ERR_R_INTERNAL_ERROR);
433 if (!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
434 GOSTerr(GOST_F_PARAM_COPY_GOST01, ERR_R_INTERNAL_ERROR);
437 if (EC_KEY_get0_private_key(eto)) {
438 gost2001_compute_public(eto);
443 static int param_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
445 if (EC_GROUP_get_curve_name
446 (EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)a))) ==
447 EC_GROUP_get_curve_name(EC_KEY_get0_group
448 (EVP_PKEY_get0((EVP_PKEY *)b)))) {
455 /* ---------- Public key functions * --------------------------------------*/
457 static int pub_decode_gost01(EVP_PKEY *pk, X509_PUBKEY *pub)
459 X509_ALGOR *palg = NULL;
460 const unsigned char *pubkey_buf = NULL;
461 unsigned char *databuf;
462 ASN1_OBJECT *palgobj = NULL;
466 ASN1_OCTET_STRING *octet = NULL;
468 const EC_GROUP *group;
470 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
472 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
473 if (!decode_gost_algor_params(pk, palg))
475 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
476 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
478 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_MALLOC_FAILURE);
481 databuf = OPENSSL_malloc(octet->length);
482 if (databuf == NULL) {
483 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_MALLOC_FAILURE);
484 ASN1_OCTET_STRING_free(octet);
487 for (i = 0, j = octet->length - 1; i < octet->length; i++, j--) {
488 databuf[j] = octet->data[i];
490 len = octet->length / 2;
491 ASN1_OCTET_STRING_free(octet);
493 Y = getbnfrombuf(databuf, len);
494 X = getbnfrombuf(databuf + len, len);
495 OPENSSL_free(databuf);
496 pub_key = EC_POINT_new(group);
497 if (!EC_POINT_set_affine_coordinates_GFp(group, pub_key, X, Y, NULL)) {
498 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_EC_LIB);
499 EC_POINT_free(pub_key);
506 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
507 GOSTerr(GOST_F_PUB_DECODE_GOST01, ERR_R_EC_LIB);
508 EC_POINT_free(pub_key);
511 EC_POINT_free(pub_key);
516 static int pub_encode_gost01(X509_PUBKEY *pub, const EVP_PKEY *pk)
518 ASN1_OBJECT *algobj = NULL;
519 ASN1_OCTET_STRING *octet = NULL;
521 unsigned char *buf = NULL, *databuf, *sptr;
522 int i, j, data_len, ret = 0;
523 const EC_POINT *pub_key;
524 BIGNUM *X, *Y, *order;
525 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
526 int ptype = V_ASN1_UNDEF;
528 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
529 if (pk->save_parameters) {
530 ASN1_STRING *params = encode_gost_algor_params(pk);
532 ptype = V_ASN1_SEQUENCE;
535 EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
536 pub_key = EC_KEY_get0_public_key(ec);
538 GOSTerr(GOST_F_PUB_ENCODE_GOST01, GOST_R_PUBLIC_KEY_UNDEFINED);
545 GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
551 if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec),
552 pub_key, X, Y, NULL)) {
553 GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_INTERNAL_ERROR);
559 data_len = 2 * BN_num_bytes(order);
561 databuf = OPENSSL_malloc(data_len);
562 if (databuf == NULL) {
563 GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
568 memset(databuf, 0, data_len);
570 store_bignum(X, databuf + data_len / 2, data_len / 2);
571 store_bignum(Y, databuf, data_len / 2);
575 octet = ASN1_OCTET_STRING_new();
577 GOSTerr(GOST_F_PUB_ENCODE_GOST01, ERR_R_MALLOC_FAILURE);
578 OPENSSL_free(databuf);
581 ASN1_STRING_set(octet, NULL, data_len);
582 sptr = ASN1_STRING_data(octet);
583 for (i = 0, j = data_len - 1; i < data_len; i++, j--) {
584 sptr[i] = databuf[j];
586 OPENSSL_free(databuf);
587 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
588 ASN1_BIT_STRING_free(octet);
591 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
594 static int pub_cmp_gost01(const EVP_PKEY *a, const EVP_PKEY *b)
596 const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
597 const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
598 const EC_POINT *ka, *kb;
602 ka = EC_KEY_get0_public_key(ea);
603 kb = EC_KEY_get0_public_key(eb);
606 ret = (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
610 static int pkey_size_gost(const EVP_PKEY *pk)
615 static int pkey_bits_gost(const EVP_PKEY *pk)
620 /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/
621 static void mackey_free_gost(EVP_PKEY *pk)
623 OPENSSL_free(pk->pkey.ptr);
626 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
629 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
630 *(int *)arg2 = NID_id_Gost28147_89_MAC;
636 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
639 EC_GROUP_get_curve_name(EC_KEY_get0_group
640 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
641 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
644 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
647 ASN1_OBJECT *obj = NULL;
649 EC_KEY *ec = EVP_PKEY_get0(pkey);
650 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
653 nid = OBJ_obj2nid(obj);
654 ASN1_OBJECT_free(obj);
657 if (!EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec))
660 if (!fill_GOST2001_params(ec, nid))
665 /* ----------------------------------------------------------------------*/
666 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
667 const char *pemstr, const char *info)
669 *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
673 case NID_id_GostR3410_2001:
674 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost01);
675 EVP_PKEY_asn1_set_private(*ameth,
676 priv_decode_gost, priv_encode_gost,
679 EVP_PKEY_asn1_set_param(*ameth,
680 gost2001_param_decode, gost2001_param_encode,
681 param_missing_gost01, param_copy_gost01,
682 param_cmp_gost01, param_print_gost01);
683 EVP_PKEY_asn1_set_public(*ameth,
684 pub_decode_gost01, pub_encode_gost01,
685 pub_cmp_gost01, pub_print_gost01,
686 pkey_size_gost, pkey_bits_gost);
688 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
690 case NID_id_Gost28147_89_MAC:
691 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
692 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);