Blame SOURCES/nss-3.67-fix-private-key-mac.patch

c2a015
diff -up ./lib/softoken/sftkpwd.c.orig ./lib/softoken/sftkpwd.c
c2a015
--- ./lib/softoken/sftkpwd.c.orig	2021-06-10 05:33:12.000000000 -0700
c2a015
+++ ./lib/softoken/sftkpwd.c	2021-07-01 14:04:34.068596942 -0700
c2a015
@@ -287,9 +287,12 @@ sftkdb_DecryptAttribute(SFTKDBHandle *ha
a809bb
     }
a809bb
 
a809bb
     /* If we are using aes 256, we need to check authentication as well.*/
a809bb
-    if ((type != CKT_INVALID_TYPE) && (cipherValue.alg == SEC_OID_AES_256_CBC)) {
a809bb
+    if ((type != CKT_INVALID_TYPE) && 
a809bb
+	(cipherValue.alg == SEC_OID_PKCS5_PBES2) &&
a809bb
+        (cipherValue.param->encAlg == SEC_OID_AES_256_CBC)) {
a809bb
         SECItem signature;
a809bb
         unsigned char signData[SDB_MAX_META_DATA_LEN];
c2a015
+        CK_RV crv;
a809bb
 
a809bb
         /* if we get here from the old legacy db, there is clearly an
a809bb
          * error, don't return the plaintext */
c2a015
@@ -301,15 +304,28 @@ sftkdb_DecryptAttribute(SFTKDBHandle *ha
c2a015
 
c2a015
         signature.data = signData;
c2a015
         signature.len = sizeof(signData);
c2a015
-        rv = sftkdb_GetAttributeSignature(handle, handle, id, type,
c2a015
+        rv = SECFailure;
c2a015
+        /* sign sftkdb_GetAttriibuteSignature returns a crv, not an rv */
c2a015
+        crv = sftkdb_GetAttributeSignature(handle, handle, id, type,
a809bb
                                           &signature);
c2a015
-        if (rv != SECSuccess) {
c2a015
-            goto loser;
c2a015
+        if (crv == CKR_OK) {
c2a015
+            rv = sftkdb_VerifyAttribute(handle, passKey, CK_INVALID_HANDLE,
c2a015
+                                        type, *plain, &signature);
a809bb
         }
c2a015
-        rv = sftkdb_VerifyAttribute(handle, passKey, CK_INVALID_HANDLE, type,
c2a015
-                                    *plain, &signature);
a809bb
         if (rv != SECSuccess) {
a809bb
-            goto loser;
a809bb
+            /*  handle a bug where old versions of NSS misfiled the signature
a809bb
+             *  attribute on password update */
a809bb
+            id |= SFTK_KEYDB_TYPE|SFTK_TOKEN_TYPE;
a809bb
+            signature.len = sizeof(signData);
c2a015
+            crv = sftkdb_GetAttributeSignature(handle, handle, id, type,
a809bb
+                                              &signature);
c2a015
+            if (crv != CKR_OK) {
c2a015
+                rv = SECFailure;
c2a015
+                PORT_SetError(SEC_ERROR_BAD_SIGNATURE);
a809bb
+                goto loser;
a809bb
+            }
a809bb
+            rv = sftkdb_VerifyAttribute(handle, passKey, CK_INVALID_HANDLE,
a809bb
+                                        type, *plain, &signature);
a809bb
         }
a809bb
     }
a809bb
 
c2a015
@@ -1198,6 +1214,7 @@ sftk_updateEncrypted(PLArenaPool *arena,
a809bb
     unsigned int i;
a809bb
     for (i = 0; i < privAttrCount; i++) {
a809bb
         // Read the old attribute in the clear.
a809bb
+        CK_OBJECT_HANDLE sdbId = id & SFTK_OBJ_ID_MASK;
a809bb
         CK_ATTRIBUTE privAttr = { privAttrTypes[i], NULL, 0 };
a809bb
         CK_RV crv = sftkdb_GetAttributeValue(keydb, id, &privAttr, 1);
a809bb
         if (crv != CKR_OK) {
c2a015
@@ -1222,7 +1239,7 @@ sftk_updateEncrypted(PLArenaPool *arena,
a809bb
         plainText.data = privAttr.pValue;
a809bb
         plainText.len = privAttr.ulValueLen;
a809bb
         if (sftkdb_EncryptAttribute(arena, keydb, keydb->db, newKey,
a809bb
-                                    iterationCount, id, privAttr.type,
a809bb
+                                    iterationCount, sdbId, privAttr.type,
a809bb
                                     &plainText, &result) != SECSuccess) {
a809bb
             return CKR_GENERAL_ERROR;
a809bb
         }
c2a015
@@ -1232,10 +1249,9 @@ sftk_updateEncrypted(PLArenaPool *arena,
a809bb
         PORT_Memset(plainText.data, 0, plainText.len);
a809bb
 
a809bb
         // Write the newly encrypted attributes out directly.
a809bb
-        CK_OBJECT_HANDLE newId = id & SFTK_OBJ_ID_MASK;
a809bb
         keydb->newKey = newKey;
a809bb
         keydb->newDefaultIterationCount = iterationCount;
a809bb
-        crv = (*keydb->db->sdb_SetAttributeValue)(keydb->db, newId, &privAttr, 1);
a809bb
+        crv = (*keydb->db->sdb_SetAttributeValue)(keydb->db, sdbId, &privAttr, 1);
a809bb
         keydb->newKey = NULL;
a809bb
         if (crv != CKR_OK) {
a809bb
             return crv;