Simplify DSA public key handling.
authorDr. Stephen Henson <steve@openssl.org>
Thu, 26 Mar 2015 14:35:49 +0000 (14:35 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Thu, 26 Mar 2015 22:04:15 +0000 (22:04 +0000)
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 <levitte@openssl.org>
crypto/dsa/dsa.h
crypto/dsa/dsa_ameth.c
crypto/dsa/dsa_asn1.c
crypto/dsa/dsa_lib.c

index 949360f..8fd5596 100644 (file)
@@ -160,7 +160,6 @@ struct dsa_st {
      */
     int pad;
     long version;
-    int write_params;
     BIGNUM *p;
     BIGNUM *q;                  /* == 20 */
     BIGNUM *g;
index 96d5c5a..65e07fd 100644 (file)
@@ -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);
index 08ed52b..e7f80a8 100644 (file)
@@ -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)
 
index bfd9106..c94be9d 100644 (file)
@@ -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;