Update copyright year
[openssl.git] / crypto / pkcs12 / p12_mutl.c
index 325da0c207808b257432c03fce4898707b230b59..88d1d66324e3ad561ef9e993df1d11b66ec26372 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the OpenSSL license (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -66,32 +66,6 @@ static int pkcs12_gen_gost_mac_key(const char *pass, int passlen,
     return 1;
 }
 
-#undef PKCS12_key_gen
-/*
- * |PKCS12_key_gen| is used to convey information about old-style broken
- * password being used to PKCS12_PBE_keyivgen in decrypt cases. Workflow
- * is if PKCS12_verify_mac notes that password encoded with compliant
- * PKCS12_key_gen_utf8 conversion subroutine isn't right, while encoded
- * with legacy non-compliant one is, then it sets |PKCS12_key_gen| to
- * legacy PKCS12_key_gen_asc conversion subroutine, which is then picked
- * by PKCS12_PBE_keyivgen. This applies to reading data. Written data
- * on the other hand is protected with standard-compliant encoding, i.e.
- * in backward-incompatible manner. Note that formally the approach is
- * not MT-safe. Rationale is that in order to access PKCS#12 files from
- * MT or even production application, you would be required to convert
- * data to correct interoperable format. In which case this variable
- * won't have to change. Conversion would have to be done with pkcs12
- * utility, which is not MT, and hence can tolerate it. In other words
- * goal is not to make this heuristic approach work in general case,
- * but in one specific one, apps/pkcs12.c.
- */
-int (*PKCS12_key_gen)(const char *pass, int passlen,
-                      unsigned char *salt, int slen,
-                      int id, int iter, int n,
-                      unsigned char *out,
-                      const EVP_MD *md_type) = NULL;
-
-
 /* Generate a MAC */
 static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
                           unsigned char *mac, unsigned int *maclen,
@@ -101,6 +75,7 @@ static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
                                                 unsigned char *out,
                                                 const EVP_MD *md_type))
 {
+    int ret = 0;
     const EVP_MD *md_type;
     HMAC_CTX *hmac = NULL;
     unsigned char key[EVP_MAX_MD_SIZE], *salt;
@@ -110,8 +85,6 @@ static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
     const X509_ALGOR *macalg;
     const ASN1_OBJECT *macoid;
 
-    if (pkcs12_key_gen == NULL)
-        pkcs12_key_gen = PKCS12_key_gen;
     if (pkcs12_key_gen == NULL)
         pkcs12_key_gen = PKCS12_key_gen_utf8;
 
@@ -144,24 +117,27 @@ static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
         if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter,
                                      md_size, key, md_type)) {
             PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
-            return 0;
+            goto err;
         }
     } else
         if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
                                iter, md_size, key, md_type)) {
         PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_KEY_GEN_ERROR);
-        return 0;
+        goto err;
     }
-    hmac = HMAC_CTX_new();
-    if (!HMAC_Init_ex(hmac, key, md_size, md_type, NULL)
+    if ((hmac = HMAC_CTX_new()) == NULL
+        || !HMAC_Init_ex(hmac, key, md_size, md_type, NULL)
         || !HMAC_Update(hmac, p12->authsafes->d.data->data,
                         p12->authsafes->d.data->length)
         || !HMAC_Final(hmac, mac, maclen)) {
-        HMAC_CTX_free(hmac);
-        return 0;
+        goto err;
     }
+    ret = 1;
+
+err:
+    OPENSSL_cleanse(key, sizeof(key));
     HMAC_CTX_free(hmac);
-    return 1;
+    return ret;
 }
 
 int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
@@ -187,30 +163,10 @@ int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
         return 0;
     }
     X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
-    if (maclen != (unsigned int)ASN1_STRING_length(macoct))
+    if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
+        || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
         return 0;
 
-    if (CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0) {
-        if (pass == NULL)
-            return 0;
-        /*
-         * In order to facilitate accessing old data retry with
-         * old-style broken password ...
-         */
-        if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
-                            PKCS12_key_gen_asc)) {
-            PKCS12err(PKCS12_F_PKCS12_VERIFY_MAC, PKCS12_R_MAC_GENERATION_ERROR);
-            return 0;
-        }
-        if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
-            || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
-            return 0;
-        else
-            PKCS12_key_gen = PKCS12_key_gen_asc;
-        /*
-         * ... and if suceeded, pass it on to PKCS12_PBE_keyivgen.
-         */
-    }
     return 1;
 }
 
@@ -252,6 +208,9 @@ int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
 {
     X509_ALGOR *macalg;
 
+    PKCS12_MAC_DATA_free(p12->mac);
+    p12->mac = NULL;
+
     if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
         return PKCS12_ERROR;
     if (iter > 1) {