Commit 6f81892e authored by Dr. Stephen Henson's avatar Dr. Stephen Henson
Browse files

Transfer parameter handling and key comparison to algorithm methods.

parent 448be743
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@

extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[];
extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[];
extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;

/* Keep this sorted in type order !! */
@@ -71,6 +72,7 @@ const EVP_PKEY_ASN1_METHOD *standard_methods[] =
	{
	&rsa_asn1_meths[0],
	&rsa_asn1_meths[1],
	&dh_asn1_meth,
	&dsa_asn1_meths[0],
	&dsa_asn1_meths[1],
	&dsa_asn1_meths[2],
@@ -86,14 +88,16 @@ void main()
	for (i = 0;
		i < sizeof(standard_methods)/sizeof(EVP_PKEY_ASN1_METHOD *);
		i++)
		fprintf(stderr, "Number %d id=%d\n", i,
			standard_methods[i]->pkey_id);
		fprintf(stderr, "Number %d id=%d (%s)\n", i,
			standard_methods[i]->pkey_id,
			OBJ_nid2sn(standard_methods[i]->pkey_id));
	}
#endif

static int ameth_cmp(const EVP_PKEY_ASN1_METHOD * const *a,
                const EVP_PKEY_ASN1_METHOD * const *b)
	{
/*fprintf(stderr, "Comparing %d with %d\n", (*a)->pkey_id, (*b)->pkey_id);*/
        return ((*a)->pkey_id - (*b)->pkey_id);
	}

@@ -106,6 +110,8 @@ const EVP_PKEY_ASN1_METHOD *EVP_PKEY_ASN1_find(int type)
			sizeof(standard_methods)/sizeof(EVP_PKEY_ASN1_METHOD *),
        		sizeof(EVP_PKEY_ASN1_METHOD *),
			(int (*)(const void *, const void *))ameth_cmp);
	if (!ret || !*ret)
		return NULL;
	if ((*ret)->pkey_flags & ASN1_PKEY_ALIAS)
		return EVP_PKEY_ASN1_find((*ret)->pkey_base_id);
	return *ret;
+19 −3
Original line number Diff line number Diff line
@@ -291,13 +291,29 @@ struct evp_pkey_asn1_method_st
	int pkey_id;
	int pkey_base_id;
	unsigned long pkey_flags;

	int (*pub_decode)(EVP_PKEY *pk, X509_PUBKEY *pub);
	int (*pub_encode)(X509_PUBKEY *pub, EVP_PKEY *pk);
	int (*pub_print)(BIO *out, EVP_PKEY *pkey, int indent, ASN1_PCTX *pctx);
	int (*pub_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk);
	int (*pub_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
	int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent,
							ASN1_PCTX *pctx);

	int (*priv_decode)(EVP_PKEY *pk, PKCS8_PRIV_KEY_INFO *p8inf);
	int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pk);
	int (*priv_encode)(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pk);
	int (*priv_print)(BIO *out, EVP_PKEY *pkey, int indent,
							ASN1_PCTX *pctx);

	int (*pkey_size)(const EVP_PKEY *pk);
	int (*pkey_bits)(const EVP_PKEY *pk);

	int (*param_decode)(const EVP_PKEY *pk, X509_PUBKEY *pub);
	int (*param_encode)(X509_PUBKEY *pub, const EVP_PKEY *pk);
	int (*param_missing)(const EVP_PKEY *pk);
	int (*param_copy)(EVP_PKEY *to, const EVP_PKEY *from);
	int (*param_cmp)(const EVP_PKEY *a, const EVP_PKEY *b);
	int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent,
							ASN1_PCTX *pctx);

	void (*pkey_free)(EVP_PKEY *pkey);
	void (*pkey_ctrl)(EVP_PKEY *pkey, int op, long arg1, void *arg2);
	} /* EVP_PKEY_ASN1_METHOD */;
+1 −0
Original line number Diff line number Diff line
@@ -89,6 +89,7 @@ EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,

	ret->save_type=type;
	ret->type=EVP_PKEY_type(type);
	ret->ameth = EVP_PKEY_ASN1_find(type);
	switch (ret->type)
		{
#ifndef OPENSSL_NO_RSA
+4 −2
Original line number Diff line number Diff line
@@ -17,8 +17,10 @@ TEST= dhtest.c
APPS=

LIB=$(TOP)/libcrypto.a
LIBSRC= dh_asn1.c dh_gen.c dh_key.c dh_lib.c dh_check.c dh_err.c dh_depr.c
LIBOBJ= dh_asn1.o dh_gen.o dh_key.o dh_lib.o dh_check.o dh_err.o dh_depr.o
LIBSRC= dh_asn1.c dh_gen.c dh_key.c dh_lib.c dh_check.c dh_err.c dh_depr.c \
	dh_ameth.c
LIBOBJ= dh_asn1.o dh_gen.o dh_key.o dh_lib.o dh_check.o dh_err.o dh_depr.o \
	dh_ameth.o

SRC= $(LIBSRC)

+81 −2
Original line number Diff line number Diff line
@@ -119,7 +119,7 @@ static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)

	}

static int dsa_pub_encode(X509_PUBKEY *pk, EVP_PKEY *pkey)
static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
	{
	DSA *dsa;
	void *pval;
@@ -168,6 +168,14 @@ static int dsa_pub_encode(X509_PUBKEY *pk, EVP_PKEY *pkey)
	return 0;
	}

static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
	{
	if (BN_cmp(b->pkey.dsa->pub_key,a->pkey.dsa->pub_key) != 0)
		return 0;
	else
		return 1;
	}

/* In PKCS#8 DSA: you just get a private key integer and parameters in the
 * AlgorithmIdentifier the pubkey must be recalculated.
 */
@@ -278,7 +286,7 @@ static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
	return 0;
	}

static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
{
	ASN1_STRING *params = NULL;
	ASN1_INTEGER *prkey = NULL;
@@ -330,6 +338,64 @@ err:
	return 0;
}

static int int_dsa_size(const EVP_PKEY *pkey)
	{
	return(DSA_size(pkey->pkey.dsa));
	}

static int dsa_bits(const EVP_PKEY *pkey)
	{
	return BN_num_bits(pkey->pkey.dsa->p);
	}

static int dsa_missing_parameters(const EVP_PKEY *pkey)
	{
	DSA *dsa;
	dsa=pkey->pkey.dsa;
	if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))
			return 1;
	return 0;
	}

static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
	{
	BIGNUM *a;

	if ((a=BN_dup(from->pkey.dsa->p)) == NULL)
		return 0;
	if (to->pkey.dsa->p != NULL)
		BN_free(to->pkey.dsa->p);
	to->pkey.dsa->p=a;

	if ((a=BN_dup(from->pkey.dsa->q)) == NULL)
		return 0;
	if (to->pkey.dsa->q != NULL)
		BN_free(to->pkey.dsa->q);
	to->pkey.dsa->q=a;

	if ((a=BN_dup(from->pkey.dsa->g)) == NULL)
		return 0;
	if (to->pkey.dsa->g != NULL)
		BN_free(to->pkey.dsa->g);
	to->pkey.dsa->g=a;
	return 1;
	}

static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
	{
	if (	BN_cmp(a->pkey.dsa->p,b->pkey.dsa->p) ||
		BN_cmp(a->pkey.dsa->q,b->pkey.dsa->q) ||
		BN_cmp(a->pkey.dsa->g,b->pkey.dsa->g))
		return 0;
	else
		return 1;
	}

static void int_dsa_free(EVP_PKEY *pkey)
	{
	DSA_free(pkey->pkey.dsa);
	}

/* NB these are sorted in pkey_id order, lowest first */

const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] = 
@@ -363,13 +429,26 @@ const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] =
		EVP_PKEY_DSA,
		EVP_PKEY_DSA,
		0,

		dsa_pub_decode,
		dsa_pub_encode,
		dsa_pub_cmp,
		0,

		dsa_priv_decode,
		dsa_priv_encode,
		0,

		int_dsa_size,
		dsa_bits,

		0,0,
		dsa_missing_parameters,
		dsa_copy_parameters,
		dsa_cmp_parameters,
		0,

		int_dsa_free,
		0
		}
	};
Loading