Blame SOURCES/nss-3.53-fix-private_key_mac.patch

83c154
diff --git a/lib/softoken/sftkpwd.c b/lib/softoken/sftkpwd.c
83c154
--- a/lib/softoken/sftkpwd.c
83c154
+++ b/lib/softoken/sftkpwd.c
83c154
@@ -277,17 +277,19 @@ sftkdb_DecryptAttribute(SFTKDBHandle *ha
83c154
     *plain = nsspkcs5_CipherData(cipherValue.param, passKey, &cipherValue.value,
83c154
                                  PR_FALSE, NULL);
83c154
     if (*plain == NULL) {
83c154
         rv = SECFailure;
83c154
         goto loser;
83c154
     }
83c154
 
83c154
     /* If we are using aes 256, we need to check authentication as well.*/
83c154
-    if ((type != CKT_INVALID_TYPE) && (cipherValue.alg == SEC_OID_AES_256_CBC)) {
83c154
+    if ((type != CKT_INVALID_TYPE) && 
83c154
+	(cipherValue.alg == SEC_OID_PKCS5_PBES2) &&
83c154
+        (cipherValue.param->encAlg == SEC_OID_AES_256_CBC)) {
83c154
         SECItem signature;
83c154
         unsigned char signData[SDB_MAX_META_DATA_LEN];
83c154
 
83c154
         /* if we get here from the old legacy db, there is clearly an
83c154
          * error, don't return the plaintext */
83c154
         if (handle == NULL) {
83c154
             rv = SECFailure;
83c154
             PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
83c154
@@ -299,17 +301,27 @@ sftkdb_DecryptAttribute(SFTKDBHandle *ha
83c154
         rv = sftkdb_GetAttributeSignature(handle, handle, id, type,
83c154
                                           &signature);
83c154
         if (rv != SECSuccess) {
83c154
             goto loser;
83c154
         }
83c154
         rv = sftkdb_VerifyAttribute(handle, passKey, CK_INVALID_HANDLE, type,
83c154
                                     *plain, &signature);
83c154
         if (rv != SECSuccess) {
83c154
-            goto loser;
83c154
+            /*  handle a bug where old versions of NSS misfiled the signature
83c154
+             *  attribute on password update */
83c154
+            id |= SFTK_KEYDB_TYPE|SFTK_TOKEN_TYPE;
83c154
+            signature.len = sizeof(signData);
83c154
+            rv = sftkdb_GetAttributeSignature(handle, handle, id, type,
83c154
+                                              &signature);
83c154
+            if (rv != SECSuccess) {
83c154
+                goto loser;
83c154
+            }
83c154
+            rv = sftkdb_VerifyAttribute(handle, passKey, CK_INVALID_HANDLE,
83c154
+                                        type, *plain, &signature);
83c154
         }
83c154
     }
83c154
 
83c154
 loser:
83c154
     if (cipherValue.param) {
83c154
         nsspkcs5_DestroyPBEParameter(cipherValue.param);
83c154
     }
83c154
     if (cipherValue.arena) {
83c154
@@ -1186,16 +1198,17 @@ sftk_updateEncrypted(PLArenaPool *arena,
83c154
     };
83c154
     const CK_ULONG privAttrCount = sizeof(privAttrTypes) / sizeof(privAttrTypes[0]);
83c154
 
83c154
     // We don't know what attributes this object has, so we update them one at a
83c154
     // time.
83c154
     unsigned int i;
83c154
     for (i = 0; i < privAttrCount; i++) {
83c154
         // Read the old attribute in the clear.
83c154
+        CK_OBJECT_HANDLE sdbId = id & SFTK_OBJ_ID_MASK;
83c154
         CK_ATTRIBUTE privAttr = { privAttrTypes[i], NULL, 0 };
83c154
         CK_RV crv = sftkdb_GetAttributeValue(keydb, id, &privAttr, 1);
83c154
         if (crv != CKR_OK) {
83c154
             continue;
83c154
         }
83c154
         if ((privAttr.ulValueLen == -1) || (privAttr.ulValueLen == 0)) {
83c154
             continue;
83c154
         }
83c154
@@ -1210,30 +1223,29 @@ sftk_updateEncrypted(PLArenaPool *arena,
83c154
         if ((privAttr.ulValueLen == -1) || (privAttr.ulValueLen == 0)) {
83c154
             return CKR_GENERAL_ERROR;
83c154
         }
83c154
         SECItem plainText;
83c154
         SECItem *result;
83c154
         plainText.data = privAttr.pValue;
83c154
         plainText.len = privAttr.ulValueLen;
83c154
         if (sftkdb_EncryptAttribute(arena, keydb, keydb->db, newKey,
83c154
-                                    iterationCount, id, privAttr.type,
83c154
+                                    iterationCount, sdbId, privAttr.type,
83c154
                                     &plainText, &result) != SECSuccess) {
83c154
             return CKR_GENERAL_ERROR;
83c154
         }
83c154
         privAttr.pValue = result->data;
83c154
         privAttr.ulValueLen = result->len;
83c154
         // Clear sensitive data.
83c154
         PORT_Memset(plainText.data, 0, plainText.len);
83c154
 
83c154
         // Write the newly encrypted attributes out directly.
83c154
-        CK_OBJECT_HANDLE newId = id & SFTK_OBJ_ID_MASK;
83c154
         keydb->newKey = newKey;
83c154
         keydb->newDefaultIterationCount = iterationCount;
83c154
-        crv = (*keydb->db->sdb_SetAttributeValue)(keydb->db, newId, &privAttr, 1);
83c154
+        crv = (*keydb->db->sdb_SetAttributeValue)(keydb->db, sdbId, &privAttr, 1);
83c154
         keydb->newKey = NULL;
83c154
         if (crv != CKR_OK) {
83c154
             return crv;
83c154
         }
83c154
     }
83c154
 
83c154
     return CKR_OK;
83c154
 }