Blame SOURCES/nss-3.79-fips-review.patches

18cffa
diff -up ./lib/freebl/dh.c.fips-review ./lib/freebl/dh.c
18cffa
--- ./lib/freebl/dh.c.fips-review	2022-05-26 02:54:33.000000000 -0700
18cffa
+++ ./lib/freebl/dh.c	2023-03-16 11:54:37.839935303 -0700
18cffa
@@ -445,7 +445,7 @@ cleanup:
18cffa
 PRBool
18cffa
 KEA_Verify(SECItem *Y, SECItem *prime, SECItem *subPrime)
18cffa
 {
18cffa
-    mp_int p, q, y, r;
18cffa
+    mp_int p, q, y, r, psub1;
18cffa
     mp_err err;
18cffa
     int cmp = 1; /* default is false */
18cffa
     if (!Y || !prime || !subPrime) {
18cffa
@@ -456,13 +456,30 @@ KEA_Verify(SECItem *Y, SECItem *prime, S
18cffa
     MP_DIGITS(&q) = 0;
18cffa
     MP_DIGITS(&y) = 0;
18cffa
     MP_DIGITS(&r) = 0;
18cffa
+    MP_DIGITS(&psub1) = 0;
18cffa
     CHECK_MPI_OK(mp_init(&p);;
18cffa
     CHECK_MPI_OK(mp_init(&q);;
18cffa
     CHECK_MPI_OK(mp_init(&y);;
18cffa
     CHECK_MPI_OK(mp_init(&r);;
18cffa
+    CHECK_MPI_OK(mp_init(&psub1));
18cffa
     SECITEM_TO_MPINT(*prime, &p);
18cffa
     SECITEM_TO_MPINT(*subPrime, &q);
18cffa
     SECITEM_TO_MPINT(*Y, &y);
18cffa
+    CHECK_MPI_OK(mp_sub_d(&p, 1, &psub1));
18cffa
+    /*
18cffa
+     * We check that the public value isn't zero (which isn't in the
18cffa
+     * group), one (subgroup of order one) or p-1 (subgroup of order 2). We
18cffa
+     * also check that the public value is less than p, to avoid being fooled
18cffa
+     * by values like p+1 or 2*p-1.
18cffa
+     * This check is required by SP-800-56Ar3. It's also done in derive,
18cffa
+     * but this is only called in various FIPS cases, so put it here to help
18cffa
+     * reviewers find it.
18cffa
+     */
18cffa
+    if (mp_cmp_d(&y, 1) <= 0 ||
18cffa
+        mp_cmp(&y, &psub1) >= 0) {
18cffa
+        err = MP_BADARG;
18cffa
+        goto cleanup;
18cffa
+    }
18cffa
     /* compute r = y**q mod p */
18cffa
     CHECK_MPI_OK(mp_exptmod(&y, &q, &p, &r);;
18cffa
     /* compare to 1 */
18cffa
@@ -472,6 +489,7 @@ cleanup:
18cffa
     mp_clear(&q);
18cffa
     mp_clear(&y);
18cffa
     mp_clear(&r);
18cffa
+    mp_clear(&psub1);
18cffa
     if (err) {
18cffa
         MP_TO_SEC_ERROR(err);
18cffa
         return PR_FALSE;
18cffa
diff -up ./lib/softoken/pkcs11c.c.fips-review ./lib/softoken/pkcs11c.c
18cffa
--- ./lib/softoken/pkcs11c.c.fips-review	2023-03-16 11:53:04.703068972 -0700
18cffa
+++ ./lib/softoken/pkcs11c.c	2023-03-16 11:55:23.498360007 -0700
18cffa
@@ -4780,6 +4780,10 @@ NSC_GenerateKey(CK_SESSION_HANDLE hSessi
18cffa
      * handle the base object stuff
18cffa
      */
18cffa
     crv = sftk_handleObject(key, session);
18cffa
+    /* we need to do this check at the end, so we can check the generated 
18cffa
+     * key length against fips requirements */
18cffa
+    key->isFIPS = sftk_operationIsFIPS(slot, pMechanism, CKA_NSS_GENERATE, key);
18cffa
+    session->lastOpWasFIPS = key->isFIPS;
18cffa
     sftk_FreeSession(session);
18cffa
     if (crv == CKR_OK && sftk_isTrue(key, CKA_SENSITIVE)) {
18cffa
         crv = sftk_forceAttribute(key, CKA_ALWAYS_SENSITIVE, &cktrue, sizeof(CK_BBOOL));
18cffa
@@ -4787,9 +4791,6 @@ NSC_GenerateKey(CK_SESSION_HANDLE hSessi
18cffa
     if (crv == CKR_OK && !sftk_isTrue(key, CKA_EXTRACTABLE)) {
18cffa
         crv = sftk_forceAttribute(key, CKA_NEVER_EXTRACTABLE, &cktrue, sizeof(CK_BBOOL));
18cffa
     }
18cffa
-    /* we need to do this check at the end, so we can check the generated key length against
18cffa
-     * fips requirements */
18cffa
-    key->isFIPS = sftk_operationIsFIPS(slot, pMechanism, CKA_NSS_GENERATE, key);
18cffa
     if (crv == CKR_OK) {
18cffa
         *phKey = key->handle;
18cffa
     }
18cffa
@@ -5093,60 +5094,67 @@ sftk_PairwiseConsistencyCheck(CK_SESSION
18cffa
 
18cffa
     if (isDerivable) {
18cffa
         SFTKAttribute *pubAttribute = NULL;
18cffa
-        CK_OBJECT_HANDLE newKey;
18cffa
         PRBool isFIPS = sftk_isFIPS(slot->slotID);
18cffa
-        CK_RV crv2;
18cffa
-        CK_OBJECT_CLASS secret = CKO_SECRET_KEY;
18cffa
-        CK_KEY_TYPE generic = CKK_GENERIC_SECRET;
18cffa
-        CK_ULONG keyLen = 128;
18cffa
-        CK_BBOOL ckTrue = CK_TRUE;
18cffa
-        CK_ATTRIBUTE template[] = {
18cffa
-            { CKA_CLASS, &secret, sizeof(secret) },
18cffa
-            { CKA_KEY_TYPE, &generic, sizeof(generic) },
18cffa
-            { CKA_VALUE_LEN, &keyLen, sizeof(keyLen) },
18cffa
-            { CKA_DERIVE, &ckTrue, sizeof(ckTrue) }
18cffa
-        };
18cffa
-        CK_ULONG templateCount = PR_ARRAY_SIZE(template);
18cffa
-        CK_ECDH1_DERIVE_PARAMS ecParams;
18cffa
+        NSSLOWKEYPrivateKey *lowPrivKey = NULL;
18cffa
+        ECPrivateKey *ecPriv;
18cffa
+        SECItem *lowPubValue = NULL;
18cffa
+        SECItem item;
18cffa
+        SECStatus rv;
18cffa
 
18cffa
         crv = CKR_OK; /*paranoia, already get's set before we drop to the end */
18cffa
-        /* FIPS 140-2 requires we verify that the resulting key is a valid key.
18cffa
-         * The easiest way to do this is to do a derive operation, which checks
18cffa
-         * the validity of the key */
18cffa
-
18cffa
+        /* FIPS 140-3 requires we verify that the resulting key is a valid key
18cffa
+         * by recalculating the public can an compare it to our own public
18cffa
+         * key. */
18cffa
+        lowPrivKey = sftk_GetPrivKey(privateKey, keyType, &crv;;
18cffa
+        if (lowPrivKey == NULL) {
18cffa
+            return sftk_MapCryptError(PORT_GetError());
18cffa
+        }
18cffa
+        /* recalculate the public key from the private key */
18cffa
         switch (keyType) {
18cffa
-            case CKK_DH:
18cffa
-                mech.mechanism = CKM_DH_PKCS_DERIVE;
18cffa
-                pubAttribute = sftk_FindAttribute(publicKey, CKA_VALUE);
18cffa
-                if (pubAttribute == NULL) {
18cffa
-                    return CKR_DEVICE_ERROR;
18cffa
-                }
18cffa
-                mech.pParameter = pubAttribute->attrib.pValue;
18cffa
-                mech.ulParameterLen = pubAttribute->attrib.ulValueLen;
18cffa
-                break;
18cffa
-            case CKK_EC:
18cffa
-                mech.mechanism = CKM_ECDH1_DERIVE;
18cffa
-                pubAttribute = sftk_FindAttribute(publicKey, CKA_EC_POINT);
18cffa
-                if (pubAttribute == NULL) {
18cffa
-                    return CKR_DEVICE_ERROR;
18cffa
-                }
18cffa
-                ecParams.kdf = CKD_NULL;
18cffa
-                ecParams.ulSharedDataLen = 0;
18cffa
-                ecParams.pSharedData = NULL;
18cffa
-                ecParams.ulPublicDataLen = pubAttribute->attrib.ulValueLen;
18cffa
-                ecParams.pPublicData = pubAttribute->attrib.pValue;
18cffa
-                mech.pParameter = &ecParams;
18cffa
-                mech.ulParameterLen = sizeof(ecParams);
18cffa
-                break;
18cffa
-            default:
18cffa
-                return CKR_DEVICE_ERROR;
18cffa
+        case CKK_DH:
18cffa
+            rv = DH_Derive(&lowPrivKey->u.dh.base, &lowPrivKey->u.dh.prime,
18cffa
+                           &lowPrivKey->u.dh.privateValue, &item, 0);
18cffa
+            if (rv != SECSuccess) {
18cffa
+                return CKR_GENERAL_ERROR;
18cffa
+            }
18cffa
+            lowPubValue = SECITEM_DupItem(&item);
18cffa
+            SECITEM_ZfreeItem(&item, PR_FALSE);
18cffa
+            pubAttribute = sftk_FindAttribute(publicKey, CKA_VALUE);
18cffa
+            break;
18cffa
+        case CKK_EC:
18cffa
+            rv = EC_NewKeyFromSeed(&lowPrivKey->u.ec.ecParams, &ecPriv,
18cffa
+                                   lowPrivKey->u.ec.privateValue.data,
18cffa
+                                   lowPrivKey->u.ec.privateValue.len);
18cffa
+            if (rv != SECSuccess) {
18cffa
+                return CKR_GENERAL_ERROR;
18cffa
+            }
18cffa
+            /* make sure it has the same encoding */
18cffa
+            if (PR_GetEnvSecure("NSS_USE_DECODED_CKA_EC_POINT") ||
18cffa
+                lowPrivKey->u.ec.ecParams.fieldID.type == ec_field_plain) {
18cffa
+              lowPubValue = SECITEM_DupItem(&ecPriv->publicValue);
18cffa
+            } else {
18cffa
+              lowPubValue = SEC_ASN1EncodeItem(NULL, NULL, &ecPriv->publicValue,
18cffa
+                                               SEC_ASN1_GET(SEC_OctetStringTemplate));;
18cffa
+            }
18cffa
+            pubAttribute = sftk_FindAttribute(publicKey, CKA_EC_POINT);
18cffa
+            /* clear out our generated private key */
18cffa
+            PORT_FreeArena(ecPriv->ecParams.arena, PR_TRUE);
18cffa
+            break;
18cffa
+        default:
18cffa
+            return CKR_DEVICE_ERROR;
18cffa
         }
18cffa
-
18cffa
-        crv = NSC_DeriveKey(hSession, &mech, privateKey->handle, template, templateCount, &newKey);
18cffa
-        if (crv != CKR_OK) {
18cffa
-            sftk_FreeAttribute(pubAttribute);
18cffa
-            return crv;
18cffa
+        /* now compare new public key with our already generated key */
18cffa
+        if ((pubAttribute == NULL) || (lowPubValue == NULL) ||
18cffa
+            (pubAttribute->attrib.ulValueLen != lowPubValue->len) ||
18cffa
+            (PORT_Memcmp(pubAttribute->attrib.pValue, lowPubValue->data,
18cffa
+                        lowPubValue->len) != 0)) {
18cffa
+            if (pubAttribute) sftk_FreeAttribute(pubAttribute);
18cffa
+            if (lowPubValue) SECITEM_ZfreeItem(lowPubValue, PR_TRUE);
18cffa
+            PORT_SetError(SEC_ERROR_BAD_KEY);
18cffa
+            return CKR_GENERAL_ERROR;
18cffa
         }
18cffa
+        SECITEM_ZfreeItem(lowPubValue, PR_TRUE);
18cffa
+
18cffa
         /* FIPS requires full validation, but in fipx mode NSC_Derive
18cffa
          * only does partial validation with approved primes, now handle
18cffa
          * full validation */
18cffa
@@ -5166,18 +5174,41 @@ sftk_PairwiseConsistencyCheck(CK_SESSION
18cffa
             }
18cffa
             crv = sftk_Attribute2SecItem(NULL, &prime, privateKey, CKA_PRIME);
18cffa
             /* we ignore the return code an only look at the length */
18cffa
-            if (subPrime.len == 0) {
18cffa
-                /* subprime not supplied, In this case look it up.
18cffa
-                 * This only works with approved primes, but in FIPS mode
18cffa
-                 * that's the only kine of prime that will get here */
18cffa
-                subPrimePtr = sftk_VerifyDH_Prime(&prime, isFIPS);
18cffa
-                if (subPrimePtr == NULL) {
18cffa
-                    crv = CKR_GENERAL_ERROR;
18cffa
+            /* do we have a known prime ? */
18cffa
+            subPrimePtr = sftk_VerifyDH_Prime(&prime, isFIPS);
18cffa
+            if (subPrimePtr == NULL) {
18cffa
+                if (subPrime.len == 0) {
18cffa
+                    /* if not a known prime, subprime must be supplied */
18cffa
+                    crv = CKR_ATTRIBUTE_VALUE_INVALID;
18cffa
                     goto done;
18cffa
+                } else {
18cffa
+                    /* not a known prime, check for primality of prime
18cffa
+                     * and subPrime */
18cffa
+                    if (!KEA_PrimeCheck(&prime)) {
18cffa
+                        crv = CKR_ATTRIBUTE_VALUE_INVALID;
18cffa
+                        goto done;
18cffa
+                    }
18cffa
+                    if (!KEA_PrimeCheck(&subPrime)) {
18cffa
+                        crv = CKR_ATTRIBUTE_VALUE_INVALID;
18cffa
+                        goto done;
18cffa
+                    }
18cffa
                 }
18cffa
+                subPrimePtr = &subPrime;
18cffa
+            } else {
18cffa
+                if (subPrime.len != 0) {
18cffa
+                    /* we have a known prime and a supplied subPrime,
18cffa
+                     * make sure the subPrime matches the subPrime for
18cffa
+                     * the known Prime */
18cffa
+                     if ((subPrimePtr->len != subPrimeLen) ||
18cffa
+                        (PORT_Memcmp(subPrimePtr->data, subPrime.data,
18cffa
+                                     subPrime.len) != 0)) {
18cffa
+                        crv = CKR_ATTRIBUTE_VALUE_INVALID;
18cffa
+                        goto done;
18cffa
+                     }
18cffa
+                 }
18cffa
             }
18cffa
             if (!KEA_Verify(&pubKey, &prime, (SECItem *)subPrimePtr)) {
18cffa
-                crv = CKR_GENERAL_ERROR;
18cffa
+                crv = CKR_ATTRIBUTE_VALUE_INVALID;
18cffa
             }
18cffa
         done:
18cffa
             SECITEM_ZfreeItem(&subPrime, PR_FALSE);
18cffa
@@ -5185,13 +5216,9 @@ sftk_PairwiseConsistencyCheck(CK_SESSION
18cffa
         }
18cffa
         /* clean up before we return */
18cffa
         sftk_FreeAttribute(pubAttribute);
18cffa
-        crv2 = NSC_DestroyObject(hSession, newKey);
18cffa
         if (crv != CKR_OK) {
18cffa
             return crv;
18cffa
         }
18cffa
-        if (crv2 != CKR_OK) {
18cffa
-            return crv2;
18cffa
-        }
18cffa
     }
18cffa
 
18cffa
     return CKR_OK;
18cffa
@@ -5709,8 +5736,8 @@ NSC_GenerateKeyPair(CK_SESSION_HANDLE hS
18cffa
      * created and linked.
18cffa
      */
18cffa
     crv = sftk_handleObject(publicKey, session);
18cffa
-    sftk_FreeSession(session);
18cffa
     if (crv != CKR_OK) {
18cffa
+        sftk_FreeSession(session);
18cffa
         sftk_FreeObject(publicKey);
18cffa
         NSC_DestroyObject(hSession, privateKey->handle);
18cffa
         sftk_FreeObject(privateKey);
18cffa
@@ -5752,6 +5779,7 @@ NSC_GenerateKeyPair(CK_SESSION_HANDLE hS
18cffa
     }
18cffa
 
18cffa
     if (crv != CKR_OK) {
18cffa
+        sftk_FreeSession(session);
18cffa
         NSC_DestroyObject(hSession, publicKey->handle);
18cffa
         sftk_FreeObject(publicKey);
18cffa
         NSC_DestroyObject(hSession, privateKey->handle);
18cffa
@@ -5761,6 +5789,8 @@ NSC_GenerateKeyPair(CK_SESSION_HANDLE hS
18cffa
     /* we need to do this check at the end to make sure the generated key meets the key length requirements */
18cffa
     privateKey->isFIPS = sftk_operationIsFIPS(slot, pMechanism, CKA_NSS_GENERATE_KEY_PAIR, privateKey);
18cffa
     publicKey->isFIPS = privateKey->isFIPS;
18cffa
+    session->lastOpWasFIPS = privateKey->isFIPS;
18cffa
+    sftk_FreeSession(session);
18cffa
 
18cffa
     *phPrivateKey = privateKey->handle;
18cffa
     *phPublicKey = publicKey->handle;
18cffa
@@ -8563,6 +8593,7 @@ NSC_DeriveKey(CK_SESSION_HANDLE hSession
18cffa
                 secretlen = tmp.len;
18cffa
             } else {
18cffa
                 secretlen = keySize;
18cffa
+                key->isFIPS = PR_FALSE;
18cffa
                 crv = sftk_ANSI_X9_63_kdf(&secret, keySize,
18cffa
                                           &tmp, mechParams->pSharedData,
18cffa
                                           mechParams->ulSharedDataLen, mechParams->kdf);
18cffa
diff -up ./lib/softoken/pkcs11.c.fips-review ./lib/softoken/pkcs11.c
18cffa
--- ./lib/softoken/pkcs11.c.fips-review	2022-05-26 02:54:33.000000000 -0700
18cffa
+++ ./lib/softoken/pkcs11.c	2023-03-16 11:54:37.840935312 -0700
18cffa
@@ -4599,7 +4599,10 @@ NSC_CreateObject(CK_SESSION_HANDLE hSess
18cffa
     if (object == NULL) {
18cffa
         return CKR_HOST_MEMORY;
18cffa
     }
18cffa
-    object->isFIPS = PR_FALSE; /* if we created the object on the fly,
18cffa
+    /* object types that we aren't allowed to create in FIPS mode are
18cffa
+     * already rejected explicitly. If we get here, then the object is
18cffa
+     * FIPS OK (most notably public key objects )*/
18cffa
+    /* object->isFIPS = PR_FALSE;  if we created the object on the fly,
18cffa
                                 * it's not a FIPS object */
18cffa
 
18cffa
     /*
18cffa
diff -up ./lib/softoken/sftkike.c.fips-review ./lib/softoken/sftkike.c
18cffa
--- ./lib/softoken/sftkike.c.fips-review	2022-05-26 02:54:33.000000000 -0700
18cffa
+++ ./lib/softoken/sftkike.c	2023-03-16 11:54:37.840935312 -0700
18cffa
@@ -516,6 +516,11 @@ sftk_ike_prf(CK_SESSION_HANDLE hSession,
18cffa
             goto fail;
18cffa
         }
18cffa
     } else {
18cffa
+        /* ikev1 isn't validated, if we use this function in ikev1 mode,
18cffa
+         * mark the resulting key as not FIPS */
18cffa
+        if (!params->bRekey) {
18cffa
+            outKey->isFIPS = PR_FALSE;
18cffa
+        }
18cffa
         crv = prf_init(&context, inKey->attrib.pValue,
18cffa
                        inKey->attrib.ulValueLen);
18cffa
         if (crv != CKR_OK) {