From ea6b07b54c1f8fc2275a121cdda071e2df7bd6c1 Mon Sep 17 00:00:00 2001 From: "Dr. Stephen Henson" Date: Thu, 26 Mar 2015 14:35:49 +0000 Subject: [PATCH] Simplify DSA public key handling. DSA public keys could exist in two forms: a single Integer type or a SEQUENCE containing the parameters and public key with a field called "write_params" deciding which form to use. These forms are non standard and were only used by functions containing "DSAPublicKey" in the name. Simplify code to only use the parameter form and encode the public key component directly in the DSA public key method. Reviewed-by: Richard Levitte --- crypto/dsa/dsa.h | 1 - crypto/dsa/dsa_ameth.c | 11 +++++++++-- crypto/dsa/dsa_asn1.c | 9 ++------- crypto/dsa/dsa_lib.c | 1 - 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/crypto/dsa/dsa.h b/crypto/dsa/dsa.h index 949360faab..8fd55961cb 100644 --- a/crypto/dsa/dsa.h +++ b/crypto/dsa/dsa.h @@ -160,7 +160,6 @@ struct dsa_st { */ int pad; long version; - int write_params; BIGNUM *p; BIGNUM *q; /* == 20 */ BIGNUM *g; diff --git a/crypto/dsa/dsa_ameth.c b/crypto/dsa/dsa_ameth.c index 96d5c5ae79..65e07fd329 100644 --- a/crypto/dsa/dsa_ameth.c +++ b/crypto/dsa/dsa_ameth.c @@ -132,6 +132,7 @@ static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) unsigned char *penc = NULL; int penclen; ASN1_STRING *str = NULL; + ASN1_INTEGER *pubint = NULL; dsa = pkey->pkey.dsa; if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) { @@ -149,9 +150,15 @@ static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) } else ptype = V_ASN1_UNDEF; - dsa->write_params = 0; + pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL); - penclen = i2d_DSAPublicKey(dsa, &penc); + if (pubint == NULL) { + DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); + goto err; + } + + penclen = i2d_ASN1_INTEGER(pubint, &penc); + ASN1_INTEGER_free(pubint); if (penclen <= 0) { DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE); diff --git a/crypto/dsa/dsa_asn1.c b/crypto/dsa/dsa_asn1.c index 08ed52ba5d..e7f80a8d62 100644 --- a/crypto/dsa/dsa_asn1.c +++ b/crypto/dsa/dsa_asn1.c @@ -132,17 +132,12 @@ IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams) * key as an INTEGER or the parameters and public key in a SEQUENCE */ -ASN1_SEQUENCE(dsa_pub_internal) = { +ASN1_SEQUENCE(DSAPublicKey) = { ASN1_SIMPLE(DSA, pub_key, BIGNUM), ASN1_SIMPLE(DSA, p, BIGNUM), ASN1_SIMPLE(DSA, q, BIGNUM), ASN1_SIMPLE(DSA, g, BIGNUM) -} ASN1_SEQUENCE_END_name(DSA, dsa_pub_internal) - -ASN1_CHOICE_cb(DSAPublicKey, dsa_cb) = { - ASN1_SIMPLE(DSA, pub_key, BIGNUM), - ASN1_EX_COMBINE(0, 0, dsa_pub_internal) -} ASN1_CHOICE_END_cb(DSA, DSAPublicKey, write_params) +} ASN1_SEQUENCE_END_name(DSA, DSAPublicKey) IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey) diff --git a/crypto/dsa/dsa_lib.c b/crypto/dsa/dsa_lib.c index bfd91062f5..c94be9d5df 100644 --- a/crypto/dsa/dsa_lib.c +++ b/crypto/dsa/dsa_lib.c @@ -146,7 +146,6 @@ DSA *DSA_new_method(ENGINE *engine) ret->pad = 0; ret->version = 0; - ret->write_params = 1; ret->p = NULL; ret->q = NULL; ret->g = NULL; -- 2.34.1