* for OpenSSL *
* Requires OpenSSL 0.9.9 for compilation *
**********************************************************************/
+#include <string.h>
+#include <openssl/crypto.h>
+#include <openssl/err.h>
#include <openssl/engine.h>
#include <openssl/evp.h>
#include <openssl/asn1.h>
-#include <string.h>
#include "gost_params.h"
#include "gost_lcl.h"
#include "e_gost_err.h"
ASN1_OCTET_STRING *s = d2i_ASN1_OCTET_STRING(NULL,&p,priv_len);
if (!s||s->length !=32)
{
- GOSTerr(GOST_F_PRIV_DECODE_GOST_94,
+ GOSTerr(GOST_F_PRIV_DECODE_GOST,
EVP_R_DECODE_ERROR);
return 0;
}
ASN1_INTEGER_free(priv_key);
if (!ret)
{
- GOSTerr(GOST_F_PRIV_DECODE_GOST_94,
+ GOSTerr(GOST_F_PRIV_DECODE_GOST,
EVP_R_DECODE_ERROR);
return 0;
}
return PKCS8_pkey_set0(p8,algobj,0,V_ASN1_SEQUENCE,params,
priv_buf,priv_len);
}
+/* --------- printing keys --------------------------------*/
+static int print_gost_94(BIO *out, const EVP_PKEY *pkey, int indent,
+ ASN1_PCTX *pctx, int type)
+ {
+ int param_nid = NID_undef;
+
+ if (type == 2)
+ {
+ BIGNUM *key;
+
+ if (!BIO_indent(out,indent,128)) return 0;
+ BIO_printf(out,"Private key: ");
+ key = gost_get0_priv_key(pkey);
+ if (!key)
+ BIO_printf(out,"<undefined>");
+ else
+ BN_print(out,key);
+ BIO_printf(out,"\n");
+ }
+ if (type >= 1)
+ {
+ BIGNUM *pubkey;
+
+ pubkey = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
+ BIO_indent(out,indent,128);
+ BIO_printf(out,"Public key: ");
+ BN_print(out,pubkey);
+ BIO_printf(out,"\n");
+ }
+
+ param_nid = gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
+ BIO_indent(out,indent,128);
+ BIO_printf(out, "Parameter set: %s\n",OBJ_nid2ln(param_nid));
+ return 1;
+}
+
+static int param_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
+ ASN1_PCTX *pctx)
+ {
+ return print_gost_94(out, pkey, indent, pctx,0);
+ }
-static int priv_print_gost (BIO *out,const EVP_PKEY *pkey, int indent,
+static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
+ ASN1_PCTX *pctx)
+ {
+ return print_gost_94(out,pkey, indent, pctx,1);
+ }
+static int priv_print_gost94(BIO *out,const EVP_PKEY *pkey, int indent,
ASN1_PCTX *pctx)
{
- BIGNUM *key;
+ return print_gost_94(out,pkey,indent,pctx,2);
+ }
+
+static int print_gost_01(BIO *out, const EVP_PKEY *pkey, int indent,
+ ASN1_PCTX *pctx, int type)
+ {
+ int param_nid = NID_undef;
+ if (type == 2)
+ {
+ BIGNUM *key;
+
+ if (!BIO_indent(out,indent,128)) return 0;
+ BIO_printf(out,"Private key: ");
+ key = gost_get0_priv_key(pkey);
+ if (!key)
+ BIO_printf(out,"<undefined)");
+ else
+ BN_print(out,key);
+ BIO_printf(out,"\n");
+ }
+ if (type >= 1)
+ {
+ BN_CTX *ctx = BN_CTX_new();
+ BIGNUM *X,*Y;
+ const EC_POINT *pubkey;
+ const EC_GROUP *group;
+
+ if (!ctx)
+ {
+ GOSTerr(GOST_F_PRINT_GOST_01,ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+ BN_CTX_start(ctx);
+ X = BN_CTX_get(ctx);
+ Y = BN_CTX_get(ctx);
+ pubkey = EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
+ group = EC_KEY_get0_group((EC_KEY *)EVP_PKEY_get0((EVP_PKEY *)pkey));
+ if (!EC_POINT_get_affine_coordinates_GFp(group,pubkey,X,Y,ctx))
+ {
+ GOSTerr(GOST_F_PRINT_GOST_01,ERR_R_EC_LIB);
+ BN_CTX_free(ctx);
+ return 0;
+ }
+ if (!BIO_indent(out,indent,128)) return 0;
+ BIO_printf(out,"Public key:\n");
+ if (!BIO_indent(out,indent+3,128)) return 0;
+ BIO_printf(out,"X:");
+ BN_print(out,X);
+ BIO_printf(out,"\n");
+ BIO_indent(out,indent+3,128);
+ BIO_printf(out,"Y:");
+ BN_print(out,Y);
+ BIO_printf(out,"\n");
+ BN_CTX_end(ctx);
+ BN_CTX_free(ctx);
+ }
+
+ param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey)));
if (!BIO_indent(out,indent,128)) return 0;
- key = gost_get0_priv_key(pkey);
- if (!key) return 0;
- BN_print(out,key);
+ BIO_printf(out,"Parameter set: %s\n",OBJ_nid2ln(param_nid));
return 1;
+}
+static int param_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
+ ASN1_PCTX *pctx)
+ {
+ return print_gost_01(out,pkey,indent,pctx,0);
+ }
+static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
+ ASN1_PCTX *pctx)
+ {
+ return print_gost_01(out,pkey, indent, pctx,1);
+ }
+static int priv_print_gost01(BIO *out,const EVP_PKEY *pkey, int indent,
+ ASN1_PCTX *pctx)
+ {
+ return print_gost_01(out,pkey,indent,pctx,2);
}
-
/* ---------------------------------------------------------------------*/
static int param_missing_gost94(const EVP_PKEY *pk)
{
}
if (!efrom)
{
- GOSTerr(GOST_F_PARAM_COPY_GOST94,
+ GOSTerr(GOST_F_PARAM_COPY_GOST01,
GOST_R_KEY_PARAMETERS_MISSING);
return 0;
}
octet = d2i_ASN1_OCTET_STRING(NULL,&pubkey_buf,pub_len);
if (!octet)
{
- GOSTerr(GOST_F_PUB_DECODE_GOST94,ERR_R_MALLOC_FAILURE);
+ GOSTerr(GOST_F_PUB_DECODE_GOST01,ERR_R_MALLOC_FAILURE);
return 0;
}
databuf = OPENSSL_malloc(octet->length);
return ret;
}
-static int pub_print_gost94(BIO *out, const EVP_PKEY *pkey, int indent,
- ASN1_PCTX *pctx)
- {
- const BIGNUM *key;
- if (!BIO_indent(out,indent,128)) return 0;
- key = ((DSA *)EVP_PKEY_get0((EVP_PKEY *)pkey))->pub_key;
- if (!key) return 0;
- BN_print(out,key);
- return 1;
- }
-static int pub_print_gost01(BIO *out, const EVP_PKEY *pkey, int indent,
- ASN1_PCTX *pctx)
- {
- return 0;
- }
+
static int pkey_size_gost(const EVP_PKEY *pk)
{
}
return -2;
}
+
+int gost94_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
+{
+ int nid=gost94_nid_by_params(EVP_PKEY_get0((EVP_PKEY *)pkey));
+ return i2d_ASN1_OBJECT(OBJ_nid2obj(nid),pder);
+}
+int gost2001_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
+{
+ int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey)));
+ return i2d_ASN1_OBJECT(OBJ_nid2obj(nid),pder);
+}
+
+int gost94_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen)
+{
+ ASN1_OBJECT *obj=NULL;
+ DSA *dsa = EVP_PKEY_get0(pkey);
+ int nid;
+ if (d2i_ASN1_OBJECT(&obj,pder,derlen)==NULL) {
+ return 0;
+ }
+ nid = OBJ_obj2nid(obj);
+ ASN1_OBJECT_free(obj);
+ if (!dsa)
+ {
+ dsa=DSA_new();
+ if (!EVP_PKEY_assign(pkey,NID_id_GostR3410_94,dsa)) return 0;
+ }
+ if (!fill_GOST94_params(dsa,nid)) return 0;
+ return 1;
+}
+
+int gost2001_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) {
+ ASN1_OBJECT *obj=NULL;
+ int nid;
+ EC_KEY *ec = EVP_PKEY_get0(pkey);
+ if (d2i_ASN1_OBJECT(&obj,pder,derlen)==NULL) {
+ return 0;
+ }
+ nid = OBJ_obj2nid(obj);
+ ASN1_OBJECT_free(obj);
+ if (!ec)
+ {
+ ec = EC_KEY_new();
+ if (!EVP_PKEY_assign(pkey,NID_id_GostR3410_2001,ec)) return 0;
+ }
+ if (!fill_GOST2001_params(ec, nid)) return 0;
+ return 1;
+}
+
+
+
+
+
/* ----------------------------------------------------------------------*/
int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info)
{
EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost94);
EVP_PKEY_asn1_set_private (*ameth,
priv_decode_gost, priv_encode_gost,
- priv_print_gost);
+ priv_print_gost94);
- EVP_PKEY_asn1_set_param (*ameth, 0, 0,
+ EVP_PKEY_asn1_set_param (*ameth,
+ gost94_param_decode, gost94_param_encode,
param_missing_gost94, param_copy_gost94,
- param_cmp_gost94,0 );
+ param_cmp_gost94,param_print_gost94 );
EVP_PKEY_asn1_set_public (*ameth,
pub_decode_gost94, pub_encode_gost94,
pub_cmp_gost94, pub_print_gost94,
EVP_PKEY_asn1_set_free (*ameth, pkey_free_gost01);
EVP_PKEY_asn1_set_private (*ameth,
priv_decode_gost, priv_encode_gost,
- priv_print_gost);
+ priv_print_gost01);
- EVP_PKEY_asn1_set_param (*ameth, 0, 0,
+ EVP_PKEY_asn1_set_param (*ameth,
+ gost2001_param_decode, gost2001_param_encode,
param_missing_gost01, param_copy_gost01,
- param_cmp_gost01, 0);
+ param_cmp_gost01, param_print_gost01);
EVP_PKEY_asn1_set_public (*ameth,
pub_decode_gost01, pub_encode_gost01,
pub_cmp_gost01, pub_print_gost01,