Make X509_SIG opaque.
authorDr. Stephen Henson <steve@openssl.org>
Thu, 10 Mar 2016 15:04:46 +0000 (15:04 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Fri, 11 Mar 2016 17:40:47 +0000 (17:40 +0000)
Reviewed-by: Rich Salz <rsalz@openssl.org>
apps/pkcs12.c
crypto/asn1/x_sig.c
crypto/include/internal/x509_int.h
crypto/pkcs12/p12_mutl.c
crypto/pkcs12/p12_npas.c
crypto/pkcs12/p12_p8d.c
crypto/pkcs12/p12_p8e.c
crypto/rsa/rsa_sign.c
doc/crypto/d2i_X509_SIG.pod
include/openssl/x509.h

index 5ed2122da6aeead020b8e116d0aba49f46a6d565..1fd1fad001b657537d7b573325c29b456760c28c 100644 (file)
@@ -668,10 +668,12 @@ int dump_certs_pkeys_bag(BIO *out, PKCS12_SAFEBAG *bag, char *pass,
     case NID_pkcs8ShroudedKeyBag:
         if (options & INFO) {
             X509_SIG *tp8;
+            X509_ALGOR *tp8alg;
 
             BIO_printf(bio_err, "Shrouded Keybag: ");
             tp8 = PKCS12_SAFEBAG_get0_pkcs8(bag);
-            alg_print(tp8->algor);
+            X509_SIG_get0(&tp8alg, NULL, tp8);
+            alg_print(tp8alg);
         }
         if (options & NOKEYS)
             return 1;
index 8197d2a30a24f85446e9c8c5c51cc8cb87b35f73..b880e2420b1b215a294f845a391a8bfdcf98b476 100644 (file)
@@ -59,6 +59,7 @@
 #include "internal/cryptlib.h"
 #include <openssl/asn1t.h>
 #include <openssl/x509.h>
+#include "internal/x509_int.h"
 
 ASN1_SEQUENCE(X509_SIG) = {
         ASN1_SIMPLE(X509_SIG, algor, X509_ALGOR),
@@ -66,3 +67,12 @@ ASN1_SEQUENCE(X509_SIG) = {
 } ASN1_SEQUENCE_END(X509_SIG)
 
 IMPLEMENT_ASN1_FUNCTIONS(X509_SIG)
+
+void X509_SIG_get0(X509_ALGOR **palg, ASN1_OCTET_STRING **pdigest,
+                   X509_SIG *sig)
+{
+    if (palg)
+        *palg = sig->algor;
+    if (pdigest)
+        *pdigest = sig->digest;
+}
index eec024c95d45cbdeeae1585f11774d1f8ffcc062..fc032ae07d2ef86f51f12e9ccb6f2f9213165183 100644 (file)
@@ -225,3 +225,8 @@ struct pkcs8_priv_key_info_st {
     ASN1_OCTET_STRING *pkey;
     STACK_OF(X509_ATTRIBUTE) *attributes;
 };
+
+struct X509_sig_st {
+    X509_ALGOR *algor;
+    ASN1_OCTET_STRING *digest;
+};
index 230f3e6b302e995bf6f1713815112d0f92f26273..0395358325ab6c52a41f086c4906cd332a487aeb 100644 (file)
@@ -74,10 +74,7 @@ void PKCS12_get0_mac(ASN1_OCTET_STRING **pmac, X509_ALGOR **pmacalg,
                      PKCS12 *p12)
 {
     if (p12->mac) {
-        if (pmac)
-            *pmac = p12->mac->dinfo->digest;
-        if (pmacalg)
-            *pmacalg = p12->mac->dinfo->algor;
+        X509_SIG_get0(pmacalg, pmac, p12->mac->dinfo);
         if (psalt)
             *psalt = p12->mac->salt;
         if (piter)
@@ -126,6 +123,8 @@ int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
     int saltlen, iter;
     int md_size = 0;
     int md_type_nid;
+    X509_ALGOR *macalg;
+    ASN1_OBJECT *macoid;
 
     if (!PKCS7_type_is_data(p12->authsafes)) {
         PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_CONTENT_TYPE_NOT_DATA);
@@ -138,8 +137,9 @@ int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
         iter = 1;
     else
         iter = ASN1_INTEGER_get(p12->mac->iter);
-    if ((md_type = EVP_get_digestbyobj(p12->mac->dinfo->algor->algorithm))
-            == NULL) {
+    X509_SIG_get0(&macalg, NULL, p12->mac->dinfo);
+    X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
+    if ((md_type = EVP_get_digestbyobj(macoid)) == NULL) {
         PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
         return 0;
     }
@@ -180,6 +180,8 @@ int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
 {
     unsigned char mac[EVP_MAX_MD_SIZE];
     unsigned int maclen;
+    ASN1_OCTET_STRING *macoct;
+
     if (p12->mac == NULL) {
         PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_ABSENT);
         return 0;
@@ -188,8 +190,9 @@ int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
         PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_GENERATION_ERROR);
         return 0;
     }
-    if ((maclen != (unsigned int)p12->mac->dinfo->digest->length)
-        || CRYPTO_memcmp(mac, p12->mac->dinfo->digest->data, maclen))
+    X509_SIG_get0(NULL, &macoct, p12->mac->dinfo);
+    if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
+        || CRYPTO_memcmp(mac, ASN1_STRING_data(macoct), maclen))
         return 0;
     return 1;
 }
@@ -202,6 +205,7 @@ int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
 {
     unsigned char mac[EVP_MAX_MD_SIZE];
     unsigned int maclen;
+    ASN1_OCTET_STRING *macoct;
 
     if (!md_type)
         md_type = EVP_sha1();
@@ -213,7 +217,8 @@ int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
         PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_GENERATION_ERROR);
         return 0;
     }
-    if (!(ASN1_OCTET_STRING_set(p12->mac->dinfo->digest, mac, maclen))) {
+    X509_SIG_get0(NULL, &macoct, p12->mac->dinfo);
+    if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
         PKCS12err(PKCS12_F_PKCS12_SET_MAC, PKCS12_R_MAC_STRING_SET_ERROR);
         return 0;
     }
@@ -224,6 +229,8 @@ int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen,
 int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
                      const EVP_MD *md_type)
 {
+    X509_ALGOR *macalg;
+
     if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
         return PKCS12_ERROR;
     if (iter > 1) {
@@ -248,12 +255,12 @@ int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
             return 0;
     } else
         memcpy(p12->mac->salt->data, salt, saltlen);
-    p12->mac->dinfo->algor->algorithm = OBJ_nid2obj(EVP_MD_type(md_type));
-    if ((p12->mac->dinfo->algor->parameter = ASN1_TYPE_new()) == NULL) {
+    X509_SIG_get0(&macalg, NULL, p12->mac->dinfo);
+    if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_type(md_type)),
+                         V_ASN1_NULL, NULL)) {
         PKCS12err(PKCS12_F_PKCS12_SETUP_MAC, ERR_R_MALLOC_FAILURE);
         return 0;
     }
-    p12->mac->dinfo->algor->parameter->type = V_ASN1_NULL;
 
     return 1;
 }
index f2fc12f752d8bb4c7b95860fd4a71c57e0c79542..e23d0352c770fd38ea8bd0d2d6e9fff3610c56a2 100644 (file)
@@ -109,7 +109,7 @@ static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass)
     STACK_OF(PKCS12_SAFEBAG) *bags;
     int i, bagnid, pbe_nid = 0, pbe_iter = 0, pbe_saltlen = 0;
     PKCS7 *p7, *p7new;
-    ASN1_OCTET_STRING *p12_data_tmp = NULL, *macnew = NULL;
+    ASN1_OCTET_STRING *p12_data_tmp = NULL, *macoct = NULL;
     unsigned char mac[EVP_MAX_MD_SIZE];
     unsigned int maclen;
 
@@ -165,12 +165,9 @@ static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass)
 
     if (!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen))
         goto saferr;
-    if ((macnew = ASN1_OCTET_STRING_new()) == NULL)
+    X509_SIG_get0(NULL, &macoct, p12->mac->dinfo);
+    if (!ASN1_OCTET_STRING_set(macoct, mac, maclen))
         goto saferr;
-    if (!ASN1_OCTET_STRING_set(macnew, mac, maclen))
-        goto saferr;
-    ASN1_OCTET_STRING_free(p12->mac->dinfo->digest);
-    p12->mac->dinfo->digest = macnew;
     ASN1_OCTET_STRING_free(p12_data_tmp);
 
     return 1;
@@ -178,7 +175,6 @@ static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass)
  saferr:
     /* Restore old safe */
     ASN1_OCTET_STRING_free(p12->authsafes->d.data);
-    ASN1_OCTET_STRING_free(macnew);
     p12->authsafes->d.data = p12_data_tmp;
     return 0;
 
@@ -202,13 +198,15 @@ static int newpass_bag(PKCS12_SAFEBAG *bag, char *oldpass, char *newpass)
     PKCS8_PRIV_KEY_INFO *p8;
     X509_SIG *p8new;
     int p8_nid, p8_saltlen, p8_iter;
+    X509_ALGOR *shalg;
 
     if (PKCS12_SAFEBAG_get_nid(bag) != NID_pkcs8ShroudedKeyBag)
         return 1;
 
     if ((p8 = PKCS8_decrypt(bag->value.shkeybag, oldpass, -1)) == NULL)
         return 0;
-    if (!alg_get(bag->value.shkeybag->algor, &p8_nid, &p8_iter, &p8_saltlen))
+    X509_SIG_get0(&shalg, NULL, bag->value.shkeybag);
+    if (!alg_get(shalg, &p8_nid, &p8_iter, &p8_saltlen))
         return 0;
     if ((p8new = PKCS8_encrypt(p8_nid, NULL, newpass, -1, NULL, p8_saltlen,
                                 p8_iter, p8)) == NULL)
index 9bdfd3f77f9523da30d25705e3f6b0c7ac3537ac..8980abe9b919c93c1856f27047de781c7edf5903 100644 (file)
 PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(X509_SIG *p8, const char *pass,
                                    int passlen)
 {
-    return PKCS12_item_decrypt_d2i(p8->algor,
+    X509_ALGOR *dalg;
+    ASN1_OCTET_STRING *doct;
+    X509_SIG_get0(&dalg, &doct, p8);
+    return PKCS12_item_decrypt_d2i(dalg,
                                    ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass,
-                                   passlen, p8->digest, 1);
+                                   passlen, doct, 1);
 }
index a6255155ba83b82930a797990cb4220052a2d970..b79ca64272135245cb00f5cbfe61f13d64b94310 100644 (file)
@@ -59,6 +59,7 @@
 #include <stdio.h>
 #include "internal/cryptlib.h"
 #include <openssl/pkcs12.h>
+#include "internal/x509_int.h"
 
 X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher,
                         const char *pass, int passlen,
@@ -103,13 +104,13 @@ X509_SIG *PKCS8_set0_pbe(const char *pass, int passlen,
         return NULL;
     }
 
-    if ((p8 = X509_SIG_new()) == NULL) {
+    p8 = OPENSSL_zalloc(sizeof(*p8));
+
+    if (p8 == NULL) {
         PKCS12err(PKCS12_F_PKCS8_SET0_PBE, ERR_R_MALLOC_FAILURE);
         ASN1_OCTET_STRING_free(enckey);
         return NULL;
     }
-    X509_ALGOR_free(p8->algor);
-    ASN1_OCTET_STRING_free(p8->digest);
     p8->algor = pbe;
     p8->digest = enckey;
 
index 61f91b9856523d2b80698801b12742a7b95c6feb..439d699f789dec31ef0de7a0aae6e3465eb7473a 100644 (file)
@@ -61,6 +61,7 @@
 #include <openssl/rsa.h>
 #include <openssl/objects.h>
 #include <openssl/x509.h>
+#include "internal/x509_int.h"
 #include "rsa_locl.h"
 
 /* Size of an SSL signature: MD5+SHA1 */
index 3efb556a0608d47cc73c6b88c7b709662b64b8b8..08d08766cedad98fa4cf535c83f915684c0589f6 100644 (file)
@@ -10,15 +10,21 @@ d2i_X509_SIG, i2d_X509_SIG - DigestInfo functions.
 
  X509_SIG *d2i_X509_SIG(X509_SIG **a, unsigned char **pp, long length);
  int i2d_X509_SIG(X509_SIG *a, unsigned char **pp);
+ void X509_SIG_get0(X509_ALGOR **palg, ASN1_OCTET_STRING **pdigest,
+                    X509_SIG *sig);
 
 =head1 DESCRIPTION
 
-These functions decode and encode an X509_SIG structure which is
-equivalent to the B<DigestInfo> structure defined in PKCS#1 and PKCS#7.
+The functions d2i_X509_SIG() and i2d_X509_SIG() decode and encode an
+X509_SIG structure which is equivalent to the B<DigestInfo> structure
+defined in PKCS#1 and PKCS#7.
 
-Otherwise these behave in a similar way to d2i_X509() and i2d_X509()
+Otherwise they behave in a similar way to d2i_X509() and i2d_X509()
 described in the L<d2i_X509(3)> manual page.
 
+X509_SIG_get0() returns pointers to the algorithm identifier and digest
+value in B<sig>. These values can then be examined or initialised.
+
 =head1 SEE ALSO
 
 L<d2i_X509(3)>
index fe60dc82a276f11ecb0c90a33e7149b9249103a4..5c138ca9717090097dd88775309f2b3ac70aa738 100644 (file)
@@ -136,10 +136,7 @@ struct X509_pubkey_st {
     CRYPTO_RWLOCK *lock;
 };
 
-typedef struct X509_sig_st {
-    X509_ALGOR *algor;
-    ASN1_OCTET_STRING *digest;
-} X509_SIG;
+typedef struct X509_sig_st X509_SIG;
 
 typedef struct X509_name_entry_st X509_NAME_ENTRY;
 
@@ -586,6 +583,9 @@ EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length);
 # endif
 
 DECLARE_ASN1_FUNCTIONS(X509_SIG)
+void X509_SIG_get0(X509_ALGOR **palg, ASN1_OCTET_STRING **pdigest,
+                   X509_SIG *sig);
+
 DECLARE_ASN1_FUNCTIONS(X509_REQ_INFO)
 DECLARE_ASN1_FUNCTIONS(X509_REQ)