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"
22 #define PK_WRAP_PARAM "LEGACY_PK_WRAP"
25 * Pack bignum into byte buffer of given size, filling all leading bytes by
28 int store_bignum(const BIGNUM *bn, unsigned char *buf, int len)
30 int bytes = BN_num_bytes(bn);
35 BN_bn2bin(bn, buf + len - bytes);
39 static int pkey_bits_gost(const EVP_PKEY *pk)
44 switch (EVP_PKEY_base_id(pk)) {
45 case NID_id_GostR3410_2001:
46 case NID_id_GostR3410_2001DH:
47 case NID_id_GostR3410_2012_256:
49 case NID_id_GostR3410_2012_512:
56 static ASN1_STRING *encode_gost_algor_params(const EVP_PKEY *key)
58 ASN1_STRING *params = ASN1_STRING_new();
59 GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
60 int pkey_param_nid = NID_undef;
61 void *key_ptr = EVP_PKEY_get0((EVP_PKEY *)key);
64 if (!params || !gkp) {
65 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
68 switch (EVP_PKEY_base_id(key)) {
69 case NID_id_GostR3410_2012_256:
70 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
71 switch (pkey_param_nid) {
72 case NID_id_GostR3410_2001_TestParamSet:
73 case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
74 case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
75 case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
76 case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
77 case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
78 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_256);
81 case NID_id_GostR3410_2012_512:
82 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
83 switch (pkey_param_nid) {
84 case NID_id_tc26_gost_3410_2012_512_paramSetTest:
85 case NID_id_tc26_gost_3410_2012_512_paramSetA:
86 case NID_id_tc26_gost_3410_2012_512_paramSetB:
87 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_2012_512);
90 case NID_id_GostR3410_2001:
91 case NID_id_GostR3410_2001DH:
92 pkey_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(key_ptr));
93 gkp->hash_params = OBJ_nid2obj(NID_id_GostR3411_94_CryptoProParamSet);
97 if (pkey_param_nid == NID_undef) {
98 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, GOST_R_INVALID_PARAMSET);
102 gkp->key_params = OBJ_nid2obj(pkey_param_nid);
104 * gkp->cipher_params = OBJ_nid2obj(cipher_param_nid);
106 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
107 if (params->length <= 0) {
108 GOSTerr(GOST_F_ENCODE_GOST_ALGOR_PARAMS, ERR_R_MALLOC_FAILURE);
111 params->type = V_ASN1_SEQUENCE;
115 GOST_KEY_PARAMS_free(gkp);
116 if (result == 0) { /* if error */
118 ASN1_STRING_free(params);
124 static int gost_decode_nid_params(EVP_PKEY *pkey, int pkey_nid, int param_nid)
126 void *key_ptr = EVP_PKEY_get0(pkey);
129 case NID_id_GostR3410_2012_256:
130 case NID_id_GostR3410_2012_512:
131 case NID_id_GostR3410_2001:
132 case NID_id_GostR3410_2001DH:
134 key_ptr = EC_KEY_new();
135 if (!EVP_PKEY_assign(pkey, pkey_nid, key_ptr)) {
136 EC_KEY_free(key_ptr);
140 return fill_GOST_EC_params(key_ptr, param_nid);
147 * Parses GOST algorithm parameters from X509_ALGOR and modifies pkey setting
150 static int decode_gost_algor_params(EVP_PKEY *pkey,
151 const X509_ALGOR *palg)
153 const ASN1_OBJECT *palg_obj = NULL;
154 int ptype = V_ASN1_UNDEF;
155 int pkey_nid = NID_undef, param_nid = NID_undef;
156 ASN1_STRING *pval = NULL;
157 const unsigned char *p;
158 GOST_KEY_PARAMS *gkp = NULL;
162 X509_ALGOR_get0(&palg_obj, &ptype, (const void **)&pval, palg);
163 if (ptype != V_ASN1_SEQUENCE) {
164 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
165 GOST_R_BAD_KEY_PARAMETERS_FORMAT);
169 pkey_nid = OBJ_obj2nid(palg_obj);
171 gkp = d2i_GOST_KEY_PARAMS(NULL, &p, pval->length);
173 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS,
174 GOST_R_BAD_PKEY_PARAMETERS_FORMAT);
177 param_nid = OBJ_obj2nid(gkp->key_params);
178 GOST_KEY_PARAMS_free(gkp);
179 if (!EVP_PKEY_set_type(pkey, pkey_nid)) {
180 GOSTerr(GOST_F_DECODE_GOST_ALGOR_PARAMS, ERR_R_INTERNAL_ERROR);
183 return gost_decode_nid_params(pkey, pkey_nid, param_nid);
186 static int gost_set_priv_key(EVP_PKEY *pkey, BIGNUM *priv)
188 switch (EVP_PKEY_base_id(pkey)) {
189 case NID_id_GostR3410_2012_512:
190 case NID_id_GostR3410_2012_256:
191 case NID_id_GostR3410_2001:
192 case NID_id_GostR3410_2001DH:
194 EC_KEY *ec = EVP_PKEY_get0(pkey);
197 EVP_PKEY_assign(pkey, EVP_PKEY_base_id(pkey), ec);
199 if (!EC_KEY_set_private_key(ec, priv))
201 if (!EVP_PKEY_missing_parameters(pkey))
202 return gost_ec_compute_public(ec);
211 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey)
213 switch (EVP_PKEY_base_id(pkey)) {
214 case NID_id_GostR3410_2012_512:
215 case NID_id_GostR3410_2012_256:
216 case NID_id_GostR3410_2001:
217 case NID_id_GostR3410_2001DH:
219 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
221 return (BIGNUM *)EC_KEY_get0_private_key(ec);
229 * GOST CMS processing functions
231 /* FIXME reaarange declarations */
232 static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub);
234 static int gost_cms_set_kari_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
238 ASN1_OCTET_STRING *ukm;
240 /* Deal with originator */
241 X509_ALGOR *pubalg = NULL;
242 ASN1_BIT_STRING *pubkey = NULL;
244 EVP_PKEY *peer_key = NULL;
245 X509_PUBKEY *tmp = NULL;
248 unsigned char shared_key[64];
249 size_t shared_key_size = 64;
250 const EVP_CIPHER *cipher = NULL;
252 if (CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm) == 0)
255 if (CMS_RecipientInfo_kari_get0_orig_id(ri, &pubalg, &pubkey, NULL, NULL, NULL) == 0)
258 nid = OBJ_obj2nid(alg->algorithm);
259 if (alg->parameter->type != V_ASN1_SEQUENCE)
263 case NID_kuznyechik_kexp15:
264 case NID_magma_kexp15:
265 cipher = EVP_get_cipherbynid(nid);
269 if (cipher == NULL) {
270 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_CIPHER_NOT_FOUND);
274 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_SET_IV,
275 ASN1_STRING_length(ukm), (void *)ASN1_STRING_get0_data(ukm)) <= 0)
278 if (pubkey != NULL && pubalg != NULL) {
279 const ASN1_OBJECT *paobj = NULL;
281 const void *param = NULL;
283 peer_key = EVP_PKEY_new();
284 tmp = X509_PUBKEY_new();
286 if ((peer_key == NULL) || (tmp == NULL)) {
287 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, ERR_R_MALLOC_FAILURE);
291 X509_ALGOR_get0(&paobj, &ptype, ¶m, pubalg);
293 if (X509_PUBKEY_set0_param(tmp, (ASN1_OBJECT *)paobj,
294 ptype, (void *)param,
295 (unsigned char *)ASN1_STRING_get0_data(pubkey),
296 ASN1_STRING_length(pubkey) ) == 0) {
297 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_PUBLIC_KEY_UNDEFINED);
301 if (pub_decode_gost_ec(peer_key, tmp) <= 0) {
302 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_DECODING_PUBLIC_KEY);
306 if (EVP_PKEY_derive_set_peer(pctx, peer_key) <= 0) {
307 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_SETTING_PEER_KEY);
312 if (EVP_PKEY_derive(pctx, shared_key, &shared_key_size) <= 0) {
313 GOSTerr(GOST_F_GOST_CMS_SET_KARI_SHARED_INFO, GOST_R_ERROR_COMPUTING_SHARED_KEY);
317 EVP_CIPHER_CTX_set_flags(CMS_RecipientInfo_kari_get0_ctx(ri), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
318 if (EVP_DecryptInit_ex(CMS_RecipientInfo_kari_get0_ctx(ri), cipher, NULL,
319 shared_key, ukm->data+24) == 0)
324 EVP_PKEY_free(peer_key);
326 X509_PUBKEY_free(tmp);
332 static int gost_cms_set_ktri_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
335 struct gost_pmeth_data *gctx = EVP_PKEY_CTX_get_data(pctx);
337 CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg);
339 switch (OBJ_obj2nid(alg->algorithm)) {
340 case NID_kuznyechik_kexp15:
341 gctx->cipher_nid = NID_kuznyechik_ctr;
344 case NID_magma_kexp15:
345 gctx->cipher_nid = NID_magma_ctr;
348 case NID_id_GostR3410_2001:
349 case NID_id_GostR3410_2001DH:
350 case NID_id_GostR3410_2012_256:
351 case NID_id_GostR3410_2012_512:
352 gctx->cipher_nid = NID_id_Gost28147_89;
356 GOSTerr(GOST_F_GOST_CMS_SET_KTRI_SHARED_INFO, GOST_R_UNSUPPORTED_RECIPIENT_INFO);
363 static int gost_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
365 switch(CMS_RecipientInfo_type(ri)) {
366 case CMS_RECIPINFO_AGREE:
367 return gost_cms_set_kari_shared_info(pctx, ri);
369 case CMS_RECIPINFO_TRANS:
370 return gost_cms_set_ktri_shared_info(pctx, ri);
374 GOSTerr(GOST_F_GOST_CMS_SET_SHARED_INFO, GOST_R_UNSUPPORTED_RECIPIENT_INFO);
378 static ASN1_STRING *gost_encode_cms_params(int ka_nid)
380 ASN1_STRING *ret = NULL;
381 ASN1_STRING *params = ASN1_STRING_new();
383 /* It's a hack. We have only one OID here, so we can use
384 * GOST_KEY_PARAMS which is a sequence of 3 OIDs,
385 * the 1st one is mandatory and the rest are optional */
386 GOST_KEY_PARAMS *gkp = GOST_KEY_PARAMS_new();
388 if (params == NULL || gkp == NULL) {
389 GOSTerr(GOST_F_GOST_ENCODE_CMS_PARAMS, ERR_R_MALLOC_FAILURE);
393 gkp->key_params = OBJ_nid2obj(ka_nid);
394 params->length = i2d_GOST_KEY_PARAMS(gkp, ¶ms->data);
396 if (params->length < 0) {
397 GOSTerr(GOST_F_GOST_ENCODE_CMS_PARAMS, ERR_R_MALLOC_FAILURE);
401 params->type = V_ASN1_SEQUENCE;
405 GOST_KEY_PARAMS_free(gkp);
408 ASN1_STRING_free(params);
416 static int pkey_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
418 int nid = EVP_PKEY_base_id(pkey), md_nid = NID_undef;
419 X509_ALGOR *alg1 = NULL, *alg2 = NULL;
422 case NID_id_GostR3410_2012_512:
423 md_nid = NID_id_GostR3411_2012_512;
425 case NID_id_GostR3410_2012_256:
426 md_nid = NID_id_GostR3411_2012_256;
428 case NID_id_GostR3410_2001:
429 case NID_id_GostR3410_2001DH:
430 case NID_id_GostR3410_94:
431 md_nid = NID_id_GostR3411_94;
438 case ASN1_PKEY_CTRL_PKCS7_SIGN:
440 PKCS7_SIGNER_INFO_get0_algs((PKCS7_SIGNER_INFO *)arg2, NULL,
442 X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
443 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
446 #ifndef OPENSSL_NO_CMS
447 case ASN1_PKEY_CTRL_CMS_SIGN:
449 CMS_SignerInfo_get0_algs((CMS_SignerInfo *)arg2, NULL, NULL,
451 X509_ALGOR_set0(alg1, OBJ_nid2obj(md_nid), V_ASN1_NULL, 0);
452 X509_ALGOR_set0(alg2, OBJ_nid2obj(nid), V_ASN1_NULL, 0);
456 case ASN1_PKEY_CTRL_PKCS7_ENCRYPT:
457 if (arg1 == 0) { /* Encryption */
458 ASN1_STRING *params = encode_gost_algor_params(pkey);
462 PKCS7_RECIP_INFO_get0_alg((PKCS7_RECIP_INFO *)arg2, &alg1);
463 X509_ALGOR_set0(alg1, OBJ_nid2obj(EVP_PKEY_id(pkey)),
464 V_ASN1_SEQUENCE, params);
467 #ifndef OPENSSL_NO_CMS
468 case ASN1_PKEY_CTRL_CMS_ENVELOPE:
471 CMS_RecipientInfo *ri = arg2;
473 struct gost_pmeth_data *gctx = NULL;
474 ASN1_STRING *params = NULL;
476 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
480 gctx = EVP_PKEY_CTX_get_data(pctx);
482 switch (gctx->cipher_nid) {
484 case NID_kuznyechik_ctr:
488 nid = (gctx->cipher_nid == NID_magma_ctr) ? NID_magma_kexp15 :
489 NID_kuznyechik_kexp15;
491 ka_nid = (EVP_PKEY_base_id(pkey) == NID_id_GostR3410_2012_256) ?
492 NID_id_tc26_agreement_gost_3410_2012_256 : NID_id_tc26_agreement_gost_3410_2012_512;
494 params = gost_encode_cms_params(ka_nid);
498 params = encode_gost_algor_params(pkey);
505 CMS_RecipientInfo_ktri_get0_algs((CMS_RecipientInfo *)arg2, NULL,
507 X509_ALGOR_set0(alg1, OBJ_nid2obj(nid), V_ASN1_SEQUENCE, params);
510 CMS_RecipientInfo *ri = arg2;
511 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
514 return gost_cms_set_shared_info(pctx, ri);
517 #ifdef ASN1_PKEY_CTRL_CMS_RI_TYPE
518 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
519 *(int *)arg2 = CMS_RECIPINFO_TRANS;
521 case ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED:
522 if (arg1 == CMS_RECIPINFO_AGREE || arg1 == CMS_RECIPINFO_TRANS) {
531 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
532 *(int *)arg2 = md_nid;
535 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
536 return gost_ec_oct2key((EC_KEY *)EVP_PKEY_get0(pkey), arg2, arg1);
538 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
539 return gost_ec_key2buf((EC_KEY *)EVP_PKEY_get0(pkey), arg2);
545 /* --------------------- free functions * ------------------------------*/
546 static void pkey_free_gost_ec(EVP_PKEY *key)
548 EC_KEY_free((EC_KEY *)EVP_PKEY_get0(key));
551 /* ------------------ private key functions -----------------------------*/
553 static BIGNUM *unmask_priv_key(EVP_PKEY *pk,
554 const unsigned char *buf, int len, int num_masks)
556 BIGNUM *pknum_masked = NULL, *q = NULL;
557 const EC_KEY *key_ptr = (pk) ? EVP_PKEY_get0(pk) : NULL;
558 const EC_GROUP *group = (key_ptr) ? EC_KEY_get0_group(key_ptr) : NULL;
560 pknum_masked = BN_lebin2bn(buf, len, BN_secure_new());
566 * XXX Remove sign by gost94
568 const unsigned char *p = buf + num_masks * len;
571 if (!q || !group || EC_GROUP_get_order(group, q, NULL) <= 0) {
572 BN_free(pknum_masked);
577 for (; p != buf; p -= len) {
578 BIGNUM *mask = BN_lebin2bn(p, len, BN_secure_new());
579 BN_CTX *ctx = BN_CTX_secure_new();
581 BN_mod_mul(pknum_masked, pknum_masked, mask, q, ctx);
594 static int priv_decode_gost(EVP_PKEY *pk,
595 const PKCS8_PRIV_KEY_INFO *p8inf)
597 const unsigned char *pkey_buf = NULL, *p = NULL;
599 BIGNUM *pk_num = NULL;
601 const X509_ALGOR *palg = NULL;
602 const ASN1_OBJECT *palg_obj = NULL;
603 ASN1_INTEGER *priv_key = NULL;
604 int expected_key_len;
606 if (!PKCS8_pkey_get0(&palg_obj, &pkey_buf, &priv_len, &palg, p8inf))
609 if (!decode_gost_algor_params(pk, palg)) {
613 expected_key_len = pkey_bits_gost(pk) > 0 ? pkey_bits_gost(pk) / 8 : 0;
614 if (expected_key_len == 0) {
615 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
619 if (priv_len % expected_key_len == 0) {
620 /* Key is not wrapped but masked */
621 pk_num = unmask_priv_key(pk, pkey_buf, expected_key_len,
622 priv_len / expected_key_len - 1);
623 } else if (V_ASN1_OCTET_STRING == *p) {
624 /* New format - Little endian octet string */
625 ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL, &p, priv_len);
626 if (!s || ((s->length != 32) && (s->length != 64))) {
628 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
631 pk_num = BN_lebin2bn(s->data, s->length, BN_secure_new());
633 } else if (V_ASN1_INTEGER == *p) {
634 priv_key = d2i_ASN1_INTEGER(NULL, &p, priv_len);
636 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
639 pk_num = ASN1_INTEGER_to_BN(priv_key, BN_secure_new());
640 ASN1_INTEGER_free(priv_key);
641 } else if ((V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) == *p) {
642 MASKED_GOST_KEY *mgk = d2i_MASKED_GOST_KEY(NULL, &p, priv_len);
645 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
649 priv_len = mgk->masked_priv_key->length;
650 if (priv_len % expected_key_len) {
651 MASKED_GOST_KEY_free(mgk);
652 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
656 pk_num = unmask_priv_key(pk, mgk->masked_priv_key->data,
658 priv_len / expected_key_len - 1);
659 MASKED_GOST_KEY_free(mgk);
661 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
665 if (pk_num == NULL) {
666 GOSTerr(GOST_F_PRIV_DECODE_GOST, EVP_R_DECODE_ERROR);
670 ret = gost_set_priv_key(pk, pk_num);
675 /* ----------------------------------------------------------------------*/
676 static int priv_encode_gost(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk)
678 ASN1_OBJECT *algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
679 ASN1_STRING *params = NULL;
680 unsigned char *buf = NULL;
681 int key_len = pkey_bits_gost(pk), i = 0;
682 /* unmasked private key */
683 const char *pk_format = get_gost_engine_param(GOST_PARAM_PK_FORMAT);
685 key_len = (key_len < 0) ? 0 : key_len / 8;
686 if (key_len == 0 || !(buf = OPENSSL_secure_malloc(key_len))) {
690 if (!store_bignum(gost_get0_priv_key(pk), buf, key_len)) {
691 OPENSSL_secure_free(buf);
695 params = encode_gost_algor_params(pk);
697 OPENSSL_secure_free(buf);
701 /* Convert buf to Little-endian */
702 for (i = 0; i < key_len / 2; i++) {
703 unsigned char tmp = buf[i];
704 buf[i] = buf[key_len - 1 - i];
705 buf[key_len - 1 - i] = tmp;
708 if (pk_format != NULL && strcmp(pk_format, PK_WRAP_PARAM) == 0) {
709 ASN1_STRING *octet = ASN1_STRING_new();
711 unsigned char *priv_buf = NULL;
712 if (!octet || !ASN1_OCTET_STRING_set(octet, buf, key_len)) {
713 ASN1_STRING_free(octet);
714 ASN1_STRING_free(params);
715 OPENSSL_secure_free(buf);
718 priv_len = i2d_ASN1_OCTET_STRING(octet, &priv_buf);
719 ASN1_STRING_free(octet);
720 OPENSSL_secure_free(buf);
722 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
726 return PKCS8_pkey_set0(p8, algobj, 0, V_ASN1_SEQUENCE, params,
730 /* --------- printing keys --------------------------------*/
731 static int print_gost_priv(BIO *out, const EVP_PKEY *pkey, int indent)
735 if (!BIO_indent(out, indent, 128))
737 BIO_printf(out, "Private key: ");
738 key = gost_get0_priv_key(pkey);
740 BIO_printf(out, "<undefined>");
743 BIO_printf(out, "\n");
748 static int print_gost_ec_pub(BIO *out, const EVP_PKEY *pkey, int indent)
752 const EC_POINT *pubkey;
753 const EC_GROUP *group;
754 EC_KEY *key = (EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey);
759 GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_MALLOC_FAILURE);
766 pubkey = (key) ? EC_KEY_get0_public_key(key) : NULL;
767 group = (key) ? EC_KEY_get0_group(key) : NULL;
768 if (!pubkey || !group)
771 if (!EC_POINT_get_affine_coordinates(group, pubkey, X, Y, ctx)) {
772 GOSTerr(GOST_F_PRINT_GOST_EC_PUB, ERR_R_EC_LIB);
775 if (!BIO_indent(out, indent, 128))
777 BIO_printf(out, "Public key:\n");
778 if (!BIO_indent(out, indent + 3, 128))
780 BIO_printf(out, "X:");
782 BIO_printf(out, "\n");
783 if (!BIO_indent(out, indent + 3, 128))
785 BIO_printf(out, "Y:");
787 BIO_printf(out, "\n");
796 static int print_gost_ec_param(BIO *out, const EVP_PKEY *pkey, int indent)
798 EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pkey);
799 const EC_GROUP *group = (ec) ? EC_KEY_get0_group(ec) : NULL;
805 param_nid = EC_GROUP_get_curve_name(group);
806 if (!BIO_indent(out, indent, 128))
808 BIO_printf(out, "Parameter set: %s\n", OBJ_nid2ln(param_nid));
813 static int print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
814 ASN1_PCTX *pctx, int type)
817 if (print_gost_priv(out, pkey, indent) == 0)
821 if (print_gost_ec_pub(out, pkey, indent) == 0)
825 return print_gost_ec_param(out, pkey, indent);
828 static int param_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
831 return print_gost_ec(out, pkey, indent, pctx, 0);
834 static int pub_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
837 return print_gost_ec(out, pkey, indent, pctx, 1);
840 static int priv_print_gost_ec(BIO *out, const EVP_PKEY *pkey, int indent,
843 return print_gost_ec(out, pkey, indent, pctx, 2);
846 /* ---------------------------------------------------------------------*/
847 static int param_missing_gost_ec(const EVP_PKEY *pk)
849 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
852 if (!EC_KEY_get0_group(ec))
857 static int param_copy_gost_ec(EVP_PKEY *to, const EVP_PKEY *from)
859 EC_KEY *eto = EVP_PKEY_get0(to);
860 const EC_KEY *efrom = EVP_PKEY_get0((EVP_PKEY *)from);
861 if (EVP_PKEY_base_id(from) != EVP_PKEY_base_id(to)) {
862 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_INCOMPATIBLE_ALGORITHMS);
866 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, GOST_R_KEY_PARAMETERS_MISSING);
872 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_MALLOC_FAILURE);
875 if (!EVP_PKEY_assign(to, EVP_PKEY_base_id(from), eto)) {
876 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
881 if (!EC_KEY_set_group(eto, EC_KEY_get0_group(efrom))) {
882 GOSTerr(GOST_F_PARAM_COPY_GOST_EC, ERR_R_INTERNAL_ERROR);
885 if (EC_KEY_get0_private_key(eto)) {
886 return gost_ec_compute_public(eto);
891 static int param_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
893 const EC_GROUP *group_a, *group_b;
894 EC_KEY *ec_a = EVP_PKEY_get0((EVP_PKEY *)a);
895 EC_KEY *ec_b = EVP_PKEY_get0((EVP_PKEY *)b);
899 group_a = EC_KEY_get0_group(ec_a);
900 group_b = EC_KEY_get0_group(ec_b);
901 if (!group_a || !group_b)
904 if (EC_GROUP_get_curve_name(group_a) == EC_GROUP_get_curve_name(group_b)) {
910 /* ---------- Public key functions * --------------------------------------*/
911 static int pub_decode_gost_ec(EVP_PKEY *pk, X509_PUBKEY *pub)
913 X509_ALGOR *palg = NULL;
914 const unsigned char *pubkey_buf = NULL;
915 unsigned char *databuf;
916 ASN1_OBJECT *palgobj = NULL;
920 ASN1_OCTET_STRING *octet = NULL;
922 const EC_GROUP *group;
924 if (!X509_PUBKEY_get0_param(&palgobj, &pubkey_buf, &pub_len, &palg, pub))
926 EVP_PKEY_assign(pk, OBJ_obj2nid(palgobj), NULL);
927 if (!decode_gost_algor_params(pk, palg))
929 group = EC_KEY_get0_group(EVP_PKEY_get0(pk));
930 octet = d2i_ASN1_OCTET_STRING(NULL, &pubkey_buf, pub_len);
932 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
935 databuf = OPENSSL_malloc(octet->length);
936 if (databuf == NULL) {
937 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_MALLOC_FAILURE);
938 ASN1_OCTET_STRING_free(octet);
942 BUF_reverse(databuf, octet->data, octet->length);
943 len = octet->length / 2;
944 ASN1_OCTET_STRING_free(octet);
946 Y = BN_bin2bn(databuf, len, NULL);
947 X = BN_bin2bn(databuf + len, len, NULL);
948 OPENSSL_free(databuf);
949 pub_key = EC_POINT_new(group);
950 if (!EC_POINT_set_affine_coordinates(group, pub_key, X, Y, NULL)) {
951 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
952 EC_POINT_free(pub_key);
959 if (!EC_KEY_set_public_key(EVP_PKEY_get0(pk), pub_key)) {
960 GOSTerr(GOST_F_PUB_DECODE_GOST_EC, ERR_R_EC_LIB);
961 EC_POINT_free(pub_key);
964 EC_POINT_free(pub_key);
969 static int pub_encode_gost_ec(X509_PUBKEY *pub, const EVP_PKEY *pk)
972 ASN1_OCTET_STRING *octet = NULL;
974 unsigned char *buf = NULL, *databuf = NULL;
975 int data_len, ret = -1;
976 const EC_POINT *pub_key;
977 BIGNUM *X = NULL, *Y = NULL, *order;
978 const EC_KEY *ec = EVP_PKEY_get0((EVP_PKEY *)pk);
979 int ptype = V_ASN1_SEQUENCE;
982 algobj = OBJ_nid2obj(EVP_PKEY_base_id(pk));
984 params = encode_gost_algor_params(pk);
988 if (order == NULL || EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL) == 0) {
989 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
992 EC_GROUP_get_order(EC_KEY_get0_group(ec), order, NULL);
993 pub_key = EC_KEY_get0_public_key(ec);
995 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, GOST_R_PUBLIC_KEY_UNDEFINED);
1001 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1004 if (!EC_POINT_get_affine_coordinates(EC_KEY_get0_group(ec),
1005 pub_key, X, Y, NULL)) {
1006 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_INTERNAL_ERROR);
1009 data_len = 2 * BN_num_bytes(order);
1010 databuf = OPENSSL_zalloc(data_len);
1011 if (databuf == NULL) {
1012 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1016 store_bignum(X, databuf + data_len / 2, data_len / 2);
1017 store_bignum(Y, databuf, data_len / 2);
1019 BUF_reverse(databuf, NULL, data_len);
1021 octet = ASN1_OCTET_STRING_new();
1022 if (octet == NULL) {
1023 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1027 if (0 == ASN1_STRING_set(octet, databuf, data_len)) {
1028 GOSTerr(GOST_F_PUB_ENCODE_GOST_EC, ERR_R_MALLOC_FAILURE);
1032 ret = i2d_ASN1_OCTET_STRING(octet, &buf);
1034 ASN1_BIT_STRING_free(octet);
1042 OPENSSL_free(databuf);
1046 return X509_PUBKEY_set0_param(pub, algobj, ptype, pval, buf, ret);
1049 static int pub_cmp_gost_ec(const EVP_PKEY *a, const EVP_PKEY *b)
1051 const EC_KEY *ea = EVP_PKEY_get0((EVP_PKEY *)a);
1052 const EC_KEY *eb = EVP_PKEY_get0((EVP_PKEY *)b);
1053 const EC_POINT *ka, *kb;
1056 ka = EC_KEY_get0_public_key(ea);
1057 kb = EC_KEY_get0_public_key(eb);
1060 return (0 == EC_POINT_cmp(EC_KEY_get0_group(ea), ka, kb, NULL));
1063 static int pkey_size_gost(const EVP_PKEY *pk)
1068 switch (EVP_PKEY_base_id(pk)) {
1069 case NID_id_GostR3410_94:
1070 case NID_id_GostR3410_2001:
1071 case NID_id_GostR3410_2001DH:
1072 case NID_id_GostR3410_2012_256:
1074 case NID_id_GostR3410_2012_512:
1081 /* ---------------------- ASN1 METHOD for GOST MAC -------------------*/
1082 static void mackey_free_gost(EVP_PKEY *pk)
1084 OPENSSL_free(EVP_PKEY_get0(pk));
1087 static int mac_ctrl_gost(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1090 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1092 *(int *)arg2 = NID_id_Gost28147_89_MAC;
1099 static int mac_ctrl_gost_12(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1102 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1104 *(int *)arg2 = NID_gost_mac_12;
1111 static int mac_ctrl_magma(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1114 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1116 *(int *)arg2 = NID_magma_mac;
1123 static int mac_ctrl_grasshopper(EVP_PKEY *pkey, int op, long arg1, void *arg2)
1126 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1128 *(int *)arg2 = NID_grasshopper_mac;
1135 static int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
1138 EC_GROUP_get_curve_name(EC_KEY_get0_group
1139 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
1140 return i2d_ASN1_OBJECT(OBJ_nid2obj(nid), pder);
1143 static int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder,
1146 ASN1_OBJECT *obj = NULL;
1148 if (d2i_ASN1_OBJECT(&obj, pder, derlen) == NULL) {
1151 nid = OBJ_obj2nid(obj);
1152 ASN1_OBJECT_free(obj);
1154 return gost_decode_nid_params(pkey, NID_id_GostR3410_2001, nid);
1157 /* ----------------------------------------------------------------------*/
1158 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
1159 const char *pemstr, const char *info)
1161 *ameth = EVP_PKEY_asn1_new(nid, ASN1_PKEY_SIGPARAM_NULL, pemstr, info);
1165 case NID_id_GostR3410_2001:
1166 case NID_id_GostR3410_2001DH:
1167 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
1168 EVP_PKEY_asn1_set_private(*ameth,
1169 priv_decode_gost, priv_encode_gost,
1170 priv_print_gost_ec);
1172 EVP_PKEY_asn1_set_param(*ameth,
1173 gost2001_param_decode, gost2001_param_encode,
1174 param_missing_gost_ec, param_copy_gost_ec,
1175 param_cmp_gost_ec, param_print_gost_ec);
1176 EVP_PKEY_asn1_set_public(*ameth,
1177 pub_decode_gost_ec, pub_encode_gost_ec,
1178 pub_cmp_gost_ec, pub_print_gost_ec,
1179 pkey_size_gost, pkey_bits_gost);
1181 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
1182 EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
1184 case NID_id_GostR3410_2012_256:
1185 case NID_id_GostR3410_2012_512:
1186 EVP_PKEY_asn1_set_free(*ameth, pkey_free_gost_ec);
1187 EVP_PKEY_asn1_set_private(*ameth,
1188 priv_decode_gost, priv_encode_gost,
1189 priv_print_gost_ec);
1191 EVP_PKEY_asn1_set_param(*ameth,
1193 param_missing_gost_ec, param_copy_gost_ec,
1194 param_cmp_gost_ec, NULL);
1196 EVP_PKEY_asn1_set_public(*ameth,
1197 pub_decode_gost_ec, pub_encode_gost_ec,
1198 pub_cmp_gost_ec, pub_print_gost_ec,
1199 pkey_size_gost, pkey_bits_gost);
1201 EVP_PKEY_asn1_set_ctrl(*ameth, pkey_ctrl_gost);
1202 EVP_PKEY_asn1_set_security_bits(*ameth, pkey_bits_gost);
1204 case NID_id_Gost28147_89_MAC:
1205 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1206 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost);
1208 case NID_gost_mac_12:
1209 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1210 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_gost_12);
1213 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1214 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_magma);
1216 case NID_grasshopper_mac:
1217 EVP_PKEY_asn1_set_free(*ameth, mackey_free_gost);
1218 EVP_PKEY_asn1_set_ctrl(*ameth, mac_ctrl_grasshopper);