ddf7d0
# HG changeset patch
ddf7d0
# User Kevin Jacobs <kjacobs@mozilla.com>
ddf7d0
# Date 1560499074 0
ddf7d0
#      Fri Jun 14 07:57:54 2019 +0000
ddf7d0
# Branch NSS_3_44_BRANCH
ddf7d0
# Node ID 876bca2723a1f969422edc93e7504420d8331d3c
ddf7d0
# Parent  20671f860c2619dc27208d64a84a759fdffc5ed1
ddf7d0
Bug 1540541 - Don't unnecessarily strip leading 0's from key material during PKCS11 import. r=jcj,mt
ddf7d0
ddf7d0
Differential Revision: https://phabricator.services.mozilla.com/D31671
ddf7d0
ddf7d0
diff --git a/lib/freebl/ecl/ecp_25519.c b/lib/freebl/ecl/ecp_25519.c
ddf7d0
--- a/lib/freebl/ecl/ecp_25519.c
ddf7d0
+++ b/lib/freebl/ecl/ecp_25519.c
ddf7d0
@@ -114,6 +114,9 @@ ec_Curve25519_pt_mul(SECItem *X, SECItem
ddf7d0
         }
ddf7d0
         px = P->data;
ddf7d0
     }
ddf7d0
+    if (k->len != 32) {
ddf7d0
+        return SECFailure;
ddf7d0
+    }
ddf7d0
 
ddf7d0
     SECStatus rv = ec_Curve25519_mul(X->data, k->data, px);
ddf7d0
     if (NSS_SecureMemcmpZero(X->data, X->len) == 0) {
ddf7d0
diff --git a/lib/pk11wrap/pk11akey.c b/lib/pk11wrap/pk11akey.c
ddf7d0
--- a/lib/pk11wrap/pk11akey.c
ddf7d0
+++ b/lib/pk11wrap/pk11akey.c
ddf7d0
@@ -190,7 +190,6 @@ PK11_ImportPublicKey(PK11SlotInfo *slot,
ddf7d0
                 attrs++;
ddf7d0
                 PK11_SETATTRS(attrs, CKA_DERIVE, &cktrue, sizeof(CK_BBOOL));
ddf7d0
                 attrs++;
ddf7d0
-                signedattr = attrs;
ddf7d0
                 PK11_SETATTRS(attrs, CKA_EC_PARAMS,
ddf7d0
                               pubKey->u.ec.DEREncodedParams.data,
ddf7d0
                               pubKey->u.ec.DEREncodedParams.len);
ddf7d0
@@ -222,12 +221,14 @@ PK11_ImportPublicKey(PK11SlotInfo *slot,
ddf7d0
                 PORT_SetError(SEC_ERROR_BAD_KEY);
ddf7d0
                 return CK_INVALID_HANDLE;
ddf7d0
         }
ddf7d0
-
ddf7d0
         templateCount = attrs - theTemplate;
ddf7d0
-        signedcount = attrs - signedattr;
ddf7d0
         PORT_Assert(templateCount <= (sizeof(theTemplate) / sizeof(CK_ATTRIBUTE)));
ddf7d0
-        for (attrs = signedattr; signedcount; attrs++, signedcount--) {
ddf7d0
-            pk11_SignedToUnsigned(attrs);
ddf7d0
+        if (pubKey->keyType != ecKey) {
ddf7d0
+            PORT_Assert(signedattr);
ddf7d0
+            signedcount = attrs - signedattr;
ddf7d0
+            for (attrs = signedattr; signedcount; attrs++, signedcount--) {
ddf7d0
+                pk11_SignedToUnsigned(attrs);
ddf7d0
+            }
ddf7d0
         }
ddf7d0
         rv = PK11_CreateNewObject(slot, CK_INVALID_SESSION, theTemplate,
ddf7d0
                                   templateCount, isToken, &objectID);
ddf7d0
@@ -1074,9 +1075,13 @@ pk11_loadPrivKeyWithFlags(PK11SlotInfo *
ddf7d0
                                         &cktrue, &ckfalse);
ddf7d0
 
ddf7d0
     /* Not everyone can handle zero padded key values, give
ddf7d0
-      * them the raw data as unsigned */
ddf7d0
-    for (ap = attrs; extra_count; ap++, extra_count--) {
ddf7d0
-        pk11_SignedToUnsigned(ap);
ddf7d0
+     * them the raw data as unsigned. The exception is EC,
ddf7d0
+     * where the values are encoded or zero-preserving
ddf7d0
+     * per-RFC5915 */
ddf7d0
+    if (privKey->keyType != ecKey) {
ddf7d0
+        for (ap = attrs; extra_count; ap++, extra_count--) {
ddf7d0
+            pk11_SignedToUnsigned(ap);
ddf7d0
+        }
ddf7d0
     }
ddf7d0
 
ddf7d0
     /* now Store the puppies */
ddf7d0
diff --git a/lib/pk11wrap/pk11cert.c b/lib/pk11wrap/pk11cert.c
ddf7d0
--- a/lib/pk11wrap/pk11cert.c
ddf7d0
+++ b/lib/pk11wrap/pk11cert.c
ddf7d0
@@ -184,7 +184,9 @@ PK11_IsUserCert(PK11SlotInfo *slot, CERT
ddf7d0
             SECKEY_DestroyPublicKey(pubKey);
ddf7d0
             return PR_FALSE;
ddf7d0
         }
ddf7d0
-        pk11_SignedToUnsigned(&theTemplate);
ddf7d0
+        if (pubKey->keyType != ecKey) {
ddf7d0
+            pk11_SignedToUnsigned(&theTemplate);
ddf7d0
+        }
ddf7d0
         if (pk11_FindObjectByTemplate(slot, &theTemplate, 1) != CK_INVALID_HANDLE) {
ddf7d0
             SECKEY_DestroyPublicKey(pubKey);
ddf7d0
             return PR_TRUE;
ddf7d0
diff --git a/lib/pk11wrap/pk11pk12.c b/lib/pk11wrap/pk11pk12.c
ddf7d0
--- a/lib/pk11wrap/pk11pk12.c
ddf7d0
+++ b/lib/pk11wrap/pk11pk12.c
ddf7d0
@@ -505,7 +505,7 @@ PK11_ImportAndReturnPrivateKey(PK11SlotI
ddf7d0
             }
ddf7d0
             PK11_SETATTRS(attrs, CKA_ID, ck_id->data, ck_id->len);
ddf7d0
             attrs++;
ddf7d0
-            signedattr = attrs;
ddf7d0
+            /* No signed attrs for EC */
ddf7d0
             /* curveOID always is a copy of AlgorithmID.parameters. */
ddf7d0
             PK11_SETATTRS(attrs, CKA_EC_PARAMS, lpk->u.ec.curveOID.data,
ddf7d0
                           lpk->u.ec.curveOID.len);
ddf7d0
@@ -523,11 +523,12 @@ PK11_ImportAndReturnPrivateKey(PK11SlotI
ddf7d0
     }
ddf7d0
     templateCount = attrs - theTemplate;
ddf7d0
     PORT_Assert(templateCount <= sizeof(theTemplate) / sizeof(CK_ATTRIBUTE));
ddf7d0
-    PORT_Assert(signedattr != NULL);
ddf7d0
-    signedcount = attrs - signedattr;
ddf7d0
-
ddf7d0
-    for (ap = signedattr; signedcount; ap++, signedcount--) {
ddf7d0
-        pk11_SignedToUnsigned(ap);
ddf7d0
+    if (lpk->keyType != ecKey) {
ddf7d0
+        PORT_Assert(signedattr);
ddf7d0
+        signedcount = attrs - signedattr;
ddf7d0
+        for (ap = signedattr; signedcount; ap++, signedcount--) {
ddf7d0
+            pk11_SignedToUnsigned(ap);
ddf7d0
+        }
ddf7d0
     }
ddf7d0
 
ddf7d0
     rv = PK11_CreateNewObject(slot, CK_INVALID_SESSION,
ddf7d0
diff --git a/lib/softoken/legacydb/lgattr.c b/lib/softoken/legacydb/lgattr.c
ddf7d0
--- a/lib/softoken/legacydb/lgattr.c
ddf7d0
+++ b/lib/softoken/legacydb/lgattr.c
ddf7d0
@@ -950,9 +950,9 @@ lg_FindECPrivateKeyAttribute(NSSLOWKEYPr
ddf7d0
         case CKA_UNWRAP:
ddf7d0
             return LG_CLONE_ATTR(attribute, type, lg_StaticFalseAttr);
ddf7d0
         case CKA_VALUE:
ddf7d0
-            return lg_CopyPrivAttrSigned(attribute, type,
ddf7d0
-                                         key->u.ec.privateValue.data,
ddf7d0
-                                         key->u.ec.privateValue.len, sdbpw);
ddf7d0
+            return lg_CopyPrivAttribute(attribute, type,
ddf7d0
+                                        key->u.ec.privateValue.data,
ddf7d0
+                                        key->u.ec.privateValue.len, sdbpw);
ddf7d0
         case CKA_EC_PARAMS:
ddf7d0
             return lg_CopyAttributeSigned(attribute, type,
ddf7d0
                                           key->u.ec.ecParams.DEREncoding.data,
ddf7d0
diff --git a/lib/softoken/pkcs11c.c b/lib/softoken/pkcs11c.c
ddf7d0
--- a/lib/softoken/pkcs11c.c
ddf7d0
+++ b/lib/softoken/pkcs11c.c
ddf7d0
@@ -7747,7 +7747,7 @@ NSC_DeriveKey(CK_SESSION_HANDLE hSession
ddf7d0
 
ddf7d0
             rv = ECDH_Derive(&ecPoint, &privKey->u.ec.ecParams, &ecScalar,
ddf7d0
                              withCofactor, &tmp);
ddf7d0
-            PORT_Free(ecScalar.data);
ddf7d0
+            PORT_ZFree(ecScalar.data, ecScalar.len);
ddf7d0
             ecScalar.data = NULL;
ddf7d0
             if (privKey != sourceKey->objectInfo) {
ddf7d0
                 nsslowkey_DestroyPrivateKey(privKey);