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