285d04
diff --git a/lib/softoken/legacydb/pcertdb.c b/lib/softoken/legacydb/pcertdb.c
285d04
--- a/lib/softoken/legacydb/pcertdb.c
285d04
+++ b/lib/softoken/legacydb/pcertdb.c
285d04
@@ -4272,16 +4272,17 @@ CreateTrust(void)
285d04
 {
285d04
     NSSLOWCERTTrust *trust = NULL;
285d04
 
285d04
     nsslowcert_LockFreeList();
285d04
     trust = trustListHead;
285d04
     if (trust) {
285d04
         trustListCount--;
285d04
         trustListHead = trust->next;
285d04
+        trust->next = NULL;
285d04
     }
285d04
     PORT_Assert(trustListCount >= 0);
285d04
     nsslowcert_UnlockFreeList();
285d04
     if (trust) {
285d04
         return trust;
285d04
     }
285d04
 
285d04
     return PORT_ZNew(NSSLOWCERTTrust);
285d04
@@ -5155,19 +5156,21 @@ done:
285d04
 }
285d04
 
285d04
 PRBool
285d04
 nsslowcert_hasTrust(NSSLOWCERTCertTrust *trust)
285d04
 {
285d04
     if (trust == NULL) {
285d04
         return PR_FALSE;
285d04
     }
285d04
-    return !((trust->sslFlags & CERTDB_TRUSTED_UNKNOWN) &&
285d04
-             (trust->emailFlags & CERTDB_TRUSTED_UNKNOWN) &&
285d04
-             (trust->objectSigningFlags & CERTDB_TRUSTED_UNKNOWN));
285d04
+    /* if we only have CERTDB__USER and CERTDB_TRUSTED_UNKNOWN bits, then
285d04
+     * we don't have a trust record. */
285d04
+    return !(((trust->sslFlags & ~(CERTDB_USER|CERTDB_TRUSTED_UNKNOWN)) == 0) &&
285d04
+        ((trust->emailFlags & ~(CERTDB_USER|CERTDB_TRUSTED_UNKNOWN)) == 0) &&
285d04
+        ((trust->objectSigningFlags & ~(CERTDB_USER|CERTDB_TRUSTED_UNKNOWN)) == 0));
285d04
 }
285d04
 
285d04
 /*
285d04
  * This function has the logic that decides if another person's cert and
285d04
  * email profile from an S/MIME message should be saved.  It can deal with
285d04
  * the case when there is no profile.
285d04
  */
285d04
 static SECStatus
285d04
diff --git a/lib/softoken/sftkdb.c b/lib/softoken/sftkdb.c
285d04
--- a/lib/softoken/sftkdb.c
285d04
+++ b/lib/softoken/sftkdb.c
285d04
@@ -119,47 +119,79 @@ sftkdb_isAuthenticatedAttribute(CK_ATTRI
285d04
         case CKA_TRUST_STEP_UP_APPROVED:
285d04
         case CKA_NSS_OVERRIDE_EXTENSIONS:
285d04
             return PR_TRUE;
285d04
         default:
285d04
             break;
285d04
     }
285d04
     return PR_FALSE;
285d04
 }
285d04
-
285d04
 /*
285d04
  * convert a native ULONG to a database ulong. Database ulong's
285d04
  * are all 4 byte big endian values.
285d04
  */
285d04
 void
285d04
 sftk_ULong2SDBULong(unsigned char *data, CK_ULONG value)
285d04
 {
285d04
     int i;
285d04
 
285d04
     for (i = 0; i < SDB_ULONG_SIZE; i++) {
285d04
         data[i] = (value >> (SDB_ULONG_SIZE - 1 - i) * BBP) & 0xff;
285d04
     }
285d04
 }
285d04
 
285d04
 /*
285d04
  * convert a database ulong back to a native ULONG. (reverse of the above
285d04
- * function.
285d04
+ * function).
285d04
  */
285d04
 static CK_ULONG
285d04
 sftk_SDBULong2ULong(unsigned char *data)
285d04
 {
285d04
     int i;
285d04
     CK_ULONG value = 0;
285d04
 
285d04
     for (i = 0; i < SDB_ULONG_SIZE; i++) {
285d04
         value |= (((CK_ULONG)data[i]) << (SDB_ULONG_SIZE - 1 - i) * BBP);
285d04
     }
285d04
     return value;
285d04
 }
285d04
 
285d04
+/* certain trust records are default values, which are the values
285d04
+ * returned if the signature check fails anyway.
285d04
+ * In those cases, we can skip the signature check. */
285d04
+PRBool
285d04
+sftkdb_isNullTrust(const CK_ATTRIBUTE *template)
285d04
+{
285d04
+    switch (template->type) {
285d04
+        case CKA_TRUST_SERVER_AUTH:
285d04
+        case CKA_TRUST_CLIENT_AUTH:
285d04
+        case CKA_TRUST_EMAIL_PROTECTION:
285d04
+        case CKA_TRUST_CODE_SIGNING:
285d04
+            if (template->ulValueLen != SDB_ULONG_SIZE) {
285d04
+                break;
285d04
+            }
285d04
+            if (sftk_SDBULong2ULong(template->pValue) == 
285d04
+                CKT_NSS_TRUST_UNKNOWN) {
285d04
+                return PR_TRUE;
285d04
+            }
285d04
+            break;
285d04
+        case CKA_TRUST_STEP_UP_APPROVED:
285d04
+            if (template->ulValueLen != 1) {
285d04
+                break;
285d04
+            }
285d04
+            if (*((unsigned char *)(template->pValue)) == 0) {
285d04
+                return PR_TRUE;
285d04
+            }
285d04
+            break;
285d04
+        default:
285d04
+            break;
285d04
+    }
285d04
+    return PR_FALSE;
285d04
+}
285d04
+
285d04
 /*
285d04
  * fix up the input templates. Our fixed up ints are stored in data and must
285d04
  * be freed by the caller. The new template must also be freed. If there are no
285d04
  * CK_ULONG attributes, the orignal template is passed in as is.
285d04
  */
285d04
 static CK_ATTRIBUTE *
285d04
 sftkdb_fixupTemplateIn(const CK_ATTRIBUTE *template, int count,
285d04
                        unsigned char **dataOut, int *dataOutSize)
285d04
@@ -410,17 +442,18 @@ sftkdb_fixupTemplateOut(CK_ATTRIBUTE *te
285d04
             }
285d04
 
285d04
             /* copy the plain text back into the template */
285d04
             PORT_Memcpy(template[i].pValue, plainText->data, plainText->len);
285d04
             template[i].ulValueLen = plainText->len;
285d04
             SECITEM_ZfreeItem(plainText, PR_TRUE);
285d04
         }
285d04
         /* make sure signed attributes are valid */
285d04
-        if (checkSig && sftkdb_isAuthenticatedAttribute(ntemplate[i].type)) {
285d04
+        if (checkSig && sftkdb_isAuthenticatedAttribute(ntemplate[i].type)
285d04
+            && !sftkdb_isNullTrust(&ntemplate[i])) {
285d04
             SECStatus rv;
285d04
             CK_RV local_crv;
285d04
             SECItem signText;
285d04
             SECItem plainText;
285d04
             unsigned char signData[SDB_MAX_META_DATA_LEN];
285d04
 
285d04
             signText.data = signData;
285d04
             signText.len = sizeof(signData);
285d04
@@ -2387,16 +2420,18 @@ sftkdb_mergeObject(SFTKDBHandle *handle,
285d04
     crv = (*source->sdb_GetAttributeValue)(source, id,
285d04
                                            ptemplate, max_attributes);
285d04
     if (crv != CKR_OK) {
285d04
         goto loser;
285d04
     }
285d04
 
285d04
     objectType = sftkdb_getULongFromTemplate(CKA_CLASS, ptemplate,
285d04
                                              max_attributes);
285d04
+/*printf(" - merging object Type 0x%08lx id=0x%08lx updateID=%s\n", objectType, id,
285d04
+       handle->updateID?handle->updateID: "<NULL>");*/
285d04
 
285d04
     /*
285d04
      * Update Object updates the object template if necessary then returns
285d04
      * whether or not we need to actually write the object out to our target
285d04
      * database.
285d04
      */
285d04
     if (!handle->updateID) {
285d04
         crv = sftkdb_CreateObject(arena, handle, target, &newID,