Blame SOURCES/0044-FIPS-140-3-keychecks.patch

727bdf
diff -up openssl-3.0.1/crypto/dh/dh_key.c.fips3 openssl-3.0.1/crypto/dh/dh_key.c
727bdf
--- openssl-3.0.1/crypto/dh/dh_key.c.fips3	2022-07-18 16:01:41.159543735 +0200
727bdf
+++ openssl-3.0.1/crypto/dh/dh_key.c	2022-07-18 16:24:30.251388248 +0200
727bdf
@@ -43,6 +43,9 @@ int ossl_dh_compute_key(unsigned char *k
727bdf
     BN_MONT_CTX *mont = NULL;
727bdf
     BIGNUM *z = NULL, *pminus1;
727bdf
     int ret = -1;
727bdf
+#ifdef FIPS_MODULE
727bdf
+    int validate = 0;
727bdf
+#endif
727bdf
 
727bdf
     if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
727bdf
         ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
727bdf
@@ -54,6 +57,13 @@ int ossl_dh_compute_key(unsigned char *k
727bdf
         return 0;
727bdf
     }
727bdf
 
727bdf
+#ifdef FIPS_MODULE
727bdf
+    if (DH_check_pub_key(dh, pub_key, &validate) <= 0) {
727bdf
+        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
727bdf
+        return 0;
727bdf
+    }
727bdf
+#endif
727bdf
+
727bdf
     ctx = BN_CTX_new_ex(dh->libctx);
727bdf
     if (ctx == NULL)
727bdf
         goto err;
727bdf
@@ -262,6 +272,9 @@ static int generate_key(DH *dh)
727bdf
 #endif
727bdf
     BN_CTX *ctx = NULL;
727bdf
     BIGNUM *pub_key = NULL, *priv_key = NULL;
727bdf
+#ifdef FIPS_MODULE
727bdf
+    int validate = 0;
727bdf
+#endif
727bdf
 
727bdf
     if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
727bdf
         ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
727bdf
@@ -354,8 +367,23 @@ static int generate_key(DH *dh)
727bdf
     if (!ossl_dh_generate_public_key(ctx, dh, priv_key, pub_key))
727bdf
         goto err;
727bdf
 
727bdf
+#ifdef FIPS_MODULE
727bdf
+    if (DH_check_pub_key(dh, pub_key, &validate) <= 0) {
727bdf
+        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
727bdf
+        goto err;
727bdf
+    }
727bdf
+#endif
727bdf
+
727bdf
     dh->pub_key = pub_key;
727bdf
     dh->priv_key = priv_key;
727bdf
+#ifdef FIPS_MODULE
727bdf
+    if (ossl_dh_check_pairwise(dh) <= 0) {
727bdf
+        dh->pub_key = dh->priv_key = NULL;
727bdf
+        ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
727bdf
+        goto err;
727bdf
+    }
727bdf
+#endif
727bdf
+
727bdf
     dh->dirty_cnt++;
727bdf
     ok = 1;
727bdf
  err:
727bdf
diff -up openssl-3.0.1/crypto/ec/ec_key.c.fips3 openssl-3.0.1/crypto/ec/ec_key.c
727bdf
diff -up openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c.fips3 openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c
727bdf
--- openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c.fips3	2022-07-25 13:42:46.814952053 +0200
727bdf
+++ openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c	2022-07-25 13:52:12.292065706 +0200
727bdf
@@ -488,6 +488,25 @@ int ecdh_plain_derive(void *vpecdhctx, u
727bdf
     }
727bdf
 
727bdf
     ppubkey = EC_KEY_get0_public_key(pecdhctx->peerk);
727bdf
+#ifdef FIPS_MODULE
727bdf
+    {
727bdf
+        BN_CTX *bn_ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(privk));
727bdf
+        int check = 0;
727bdf
+
727bdf
+        if (bn_ctx == NULL) {
727bdf
+            ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
727bdf
+            goto end;
727bdf
+        }
727bdf
+
727bdf
+        check = ossl_ec_key_public_check(pecdhctx->peerk, bn_ctx);
727bdf
+        BN_CTX_free(bn_ctx);
727bdf
+
727bdf
+        if (check <= 0) {
727bdf
+            ERR_raise(ERR_LIB_PROV, EC_R_INVALID_PEER_KEY);
727bdf
+            goto end;
727bdf
+        }
727bdf
+    }
727bdf
+#endif
727bdf
 
727bdf
     retlen = ECDH_compute_key(secret, size, ppubkey, privk, NULL);
727bdf
 
727bdf
diff -up openssl-3.0.1/crypto/ec/ec_key.c.fips3 openssl-3.0.1/crypto/ec/ec_key.c
727bdf
--- openssl-3.0.1/crypto/ec/ec_key.c.fips3	2022-07-25 14:03:34.420222507 +0200
727bdf
+++ openssl-3.0.1/crypto/ec/ec_key.c	2022-07-25 14:09:00.728164294 +0200
727bdf
@@ -336,6 +336,11 @@ static int ec_generate_key(EC_KEY *eckey
727bdf
 
727bdf
         OSSL_SELF_TEST_get_callback(eckey->libctx, &cb, &cbarg);
727bdf
         ok = ecdsa_keygen_pairwise_test(eckey, cb, cbarg);
727bdf
+
727bdf
+#ifdef FIPS_MODULE
727bdf
+        ok &= ossl_ec_key_public_check(eckey, ctx);
727bdf
+        ok &= ossl_ec_key_pairwise_check(eckey, ctx);
727bdf
+#endif /* FIPS_MODULE */
727bdf
     }
727bdf
 err:
727bdf
     /* Step (9): If there is an error return an invalid keypair. */
727bdf
diff -up openssl-3.0.1/crypto/rsa/rsa_gen.c.fips3 openssl-3.0.1/crypto/rsa/rsa_gen.c
727bdf
--- openssl-3.0.1/crypto/rsa/rsa_gen.c.fips3	2022-07-25 17:02:17.807271297 +0200
727bdf
+++ openssl-3.0.1/crypto/rsa/rsa_gen.c	2022-07-25 17:18:24.931959649 +0200
727bdf
@@ -23,6 +23,7 @@
727bdf
 #include <time.h>
727bdf
 #include "internal/cryptlib.h"
727bdf
 #include <openssl/bn.h>
727bdf
+#include <openssl/obj_mac.h>
727bdf
 #include <openssl/self_test.h>
727bdf
 #include "prov/providercommon.h"
727bdf
 #include "rsa_local.h"
727bdf
@@ -476,52 +476,43 @@ static int rsa_keygen(OSSL_LIB_CTX *libc
727bdf
 static int rsa_keygen_pairwise_test(RSA *rsa, OSSL_CALLBACK *cb, void *cbarg)
727bdf
 {
727bdf
     int ret = 0;
727bdf
-    unsigned int ciphertxt_len;
727bdf
-    unsigned char *ciphertxt = NULL;
727bdf
-    const unsigned char plaintxt[16] = {0};
727bdf
-    unsigned char *decoded = NULL;
727bdf
-    unsigned int decoded_len;
727bdf
-    unsigned int plaintxt_len = (unsigned int)sizeof(plaintxt_len);
727bdf
-    int padding = RSA_PKCS1_PADDING;
727bdf
+    unsigned int signature_len;
727bdf
+    unsigned char *signature = NULL;
727bdf
     OSSL_SELF_TEST *st = NULL;
727bdf
+    static const unsigned char dgst[] = {
727bdf
+        0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
727bdf
+        0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
727bdf
+        0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
727bdf
+    };
727bdf
 
727bdf
     st = OSSL_SELF_TEST_new(cb, cbarg);
727bdf
     if (st == NULL)
727bdf
         goto err;
727bdf
     OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
727bdf
+                           /* No special name for RSA signature PCT*/
727bdf
                            OSSL_SELF_TEST_DESC_PCT_RSA_PKCS1);
727bdf
 
727bdf
-    ciphertxt_len = RSA_size(rsa);
727bdf
+    signature_len = RSA_size(rsa);
727bdf
-    /*
727bdf
-     * RSA_private_encrypt() and RSA_private_decrypt() requires the 'to'
727bdf
-     * parameter to be a maximum of RSA_size() - allocate space for both.
727bdf
-     */
727bdf
-    ciphertxt = OPENSSL_zalloc(ciphertxt_len * 2);
727bdf
-    if (ciphertxt == NULL)
727bdf
+    signature = OPENSSL_zalloc(signature_len);
727bdf
+    if (signature == NULL)
727bdf
         goto err;
727bdf
-    decoded = ciphertxt + ciphertxt_len;
727bdf
 
727bdf
-    ciphertxt_len = RSA_public_encrypt(plaintxt_len, plaintxt, ciphertxt, rsa,
727bdf
-                                       padding);
727bdf
-    if (ciphertxt_len <= 0)
727bdf
+    if (RSA_sign(NID_sha256, dgst, sizeof(dgst), signature, &signature_len, rsa) <= 0)
727bdf
         goto err;
727bdf
-    if (ciphertxt_len == plaintxt_len
727bdf
-        && memcmp(ciphertxt, plaintxt, plaintxt_len) == 0)
727bdf
+
727bdf
+    if (signature_len <= 0)
727bdf
         goto err;
727bdf
 
727bdf
-    OSSL_SELF_TEST_oncorrupt_byte(st, ciphertxt);
727bdf
+    OSSL_SELF_TEST_oncorrupt_byte(st, signature);
727bdf
 
727bdf
-    decoded_len = RSA_private_decrypt(ciphertxt_len, ciphertxt, decoded, rsa,
727bdf
-                                      padding);
727bdf
-    if (decoded_len != plaintxt_len
727bdf
-        || memcmp(decoded, plaintxt,  decoded_len) != 0)
727bdf
+    if (RSA_verify(NID_sha256, dgst, sizeof(dgst), signature, signature_len, rsa) <= 0)
727bdf
         goto err;
727bdf
 
727bdf
     ret = 1;
727bdf
 err:
727bdf
     OSSL_SELF_TEST_onend(st, ret);
727bdf
     OSSL_SELF_TEST_free(st);
727bdf
-    OPENSSL_free(ciphertxt);
727bdf
+    OPENSSL_free(signature);
727bdf
 
727bdf
     return ret;
727bdf
 }