163057e
From 136988155862ce2b45683ef8045e7a8cdd11e215 Mon Sep 17 00:00:00 2001
163057e
From: Dmitry Belyavskiy <dbelyavs@redhat.com>
163057e
Date: Mon, 21 Aug 2023 16:13:46 +0200
163057e
Subject: [PATCH 47/48] 0113-asymciphers-kem-Add-explicit-FIPS-indicator.patch
163057e
163057e
Patch-name: 0113-asymciphers-kem-Add-explicit-FIPS-indicator.patch
163057e
Patch-id: 113
163057e
---
163057e
 include/openssl/core_names.h                  |  2 ++
163057e
 include/openssl/evp.h                         |  4 +++
163057e
 .../implementations/asymciphers/rsa_enc.c     | 22 ++++++++++++++
163057e
 providers/implementations/kem/rsa_kem.c       | 30 ++++++++++++++++++-
163057e
 4 files changed, 57 insertions(+), 1 deletion(-)
163057e
163057e
diff --git a/include/openssl/core_names.h b/include/openssl/core_names.h
163057e
index 29459049ad..9af0b1847d 100644
163057e
--- a/include/openssl/core_names.h
163057e
+++ b/include/openssl/core_names.h
163057e
@@ -480,6 +480,7 @@ extern "C" {
163057e
 #ifdef FIPS_MODULE
163057e
 #define OSSL_ASYM_CIPHER_PARAM_REDHAT_KAT_OEAP_SEED     "redhat-kat-oaep-seed"
163057e
 #endif
163057e
+#define OSSL_ASYM_CIPHER_PARAM_REDHAT_FIPS_INDICATOR    "redhat-fips-indicator"
163057e
 
163057e
 /*
163057e
  * Encoder / decoder parameters
163057e
@@ -514,6 +515,7 @@ extern "C" {
163057e
 
163057e
 /* KEM parameters */
163057e
 #define OSSL_KEM_PARAM_OPERATION            "operation"
163057e
+#define OSSL_KEM_PARAM_REDHAT_FIPS_INDICATOR "redhat-fips-indicator" /* int */
163057e
 
163057e
 /* OSSL_KEM_PARAM_OPERATION values */
163057e
 #define OSSL_KEM_PARAM_OPERATION_RSASVE     "RSASVE"
163057e
diff --git a/include/openssl/evp.h b/include/openssl/evp.h
163057e
index f1a33ff6f2..dadbf46a5a 100644
163057e
--- a/include/openssl/evp.h
163057e
+++ b/include/openssl/evp.h
163057e
@@ -1767,6 +1767,10 @@ OSSL_DEPRECATEDIN_3_0 size_t EVP_PKEY_meth_get_count(void);
163057e
 OSSL_DEPRECATEDIN_3_0 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx);
163057e
 # endif
163057e
 
163057e
+# define EVP_PKEY_REDHAT_FIPS_INDICATOR_UNDETERMINED 0
163057e
+# define EVP_PKEY_REDHAT_FIPS_INDICATOR_APPROVED     1
163057e
+# define EVP_PKEY_REDHAT_FIPS_INDICATOR_NOT_APPROVED 2
163057e
+
163057e
 EVP_KEYMGMT *EVP_KEYMGMT_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
163057e
                                const char *properties);
163057e
 int EVP_KEYMGMT_up_ref(EVP_KEYMGMT *keymgmt);
163057e
diff --git a/providers/implementations/asymciphers/rsa_enc.c b/providers/implementations/asymciphers/rsa_enc.c
163057e
index d169bfd396..bd4dcb4e27 100644
163057e
--- a/providers/implementations/asymciphers/rsa_enc.c
163057e
+++ b/providers/implementations/asymciphers/rsa_enc.c
163057e
@@ -466,6 +466,27 @@ static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
163057e
     if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->implicit_rejection))
163057e
         return 0;
163057e
 
163057e
+#ifdef FIPS_MODULE
163057e
+    p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_REDHAT_FIPS_INDICATOR);
163057e
+    if (p != NULL) {
163057e
+        int fips_indicator = EVP_PKEY_REDHAT_FIPS_INDICATOR_APPROVED;
163057e
+
163057e
+        /* NIST SP 800-56Br2 section 6.4.2.1 requires either explicit key
163057e
+         * confirmation (section 6.4.2.3.2), or assurance from a trusted third
163057e
+         * party (section 6.4.2.3.1) for the KTS-OAEP key transport scheme, but
163057e
+         * explicit key confirmation is not implemented here and cannot be
163057e
+         * implemented without protocol changes, and the FIPS provider does not
163057e
+         * implement trusted third party validation, since it relies on its
163057e
+         * callers to do that. We must thus mark RSA-OAEP as unapproved until
163057e
+         * we have received clarification from NIST on how library modules such
163057e
+         * as OpenSSL should implement TTP validation. */
163057e
+        fips_indicator = EVP_PKEY_REDHAT_FIPS_INDICATOR_NOT_APPROVED;
163057e
+
163057e
+        if (!OSSL_PARAM_set_int(p, fips_indicator))
163057e
+            return 0;
163057e
+    }
163057e
+#endif /* defined(FIPS_MODULE) */
163057e
+
163057e
     return 1;
163057e
 }
163057e
 
163057e
@@ -480,6 +501,7 @@ static const OSSL_PARAM known_gettable_ctx_params[] = {
163057e
     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, NULL),
163057e
 #ifdef FIPS_MODULE
163057e
     OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_REDHAT_KAT_OEAP_SEED, NULL, 0),
163057e
+    OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_REDHAT_FIPS_INDICATOR, NULL),
163057e
 #endif /* FIPS_MODULE */
163057e
     OSSL_PARAM_END
163057e
 };
163057e
diff --git a/providers/implementations/kem/rsa_kem.c b/providers/implementations/kem/rsa_kem.c
163057e
index 8a6f585d0b..f4b7415074 100644
163057e
--- a/providers/implementations/kem/rsa_kem.c
163057e
+++ b/providers/implementations/kem/rsa_kem.c
163057e
@@ -152,11 +152,39 @@ static int rsakem_decapsulate_init(void *vprsactx, void *vrsa,
163057e
 static int rsakem_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
163057e
 {
163057e
     PROV_RSA_CTX *ctx = (PROV_RSA_CTX *)vprsactx;
163057e
+#ifdef FIPS_MODULE
163057e
+    OSSL_PARAM *p;
163057e
+#endif /* defined(FIPS_MODULE) */
163057e
+
163057e
+    if (ctx == NULL)
163057e
+        return 0;
163057e
+
163057e
+#ifdef FIPS_MODULE
163057e
+    p = OSSL_PARAM_locate(params, OSSL_KEM_PARAM_REDHAT_FIPS_INDICATOR);
163057e
+    if (p != NULL) {
163057e
+        /* NIST SP 800-56Br2 section 6.4.2.1 requires either explicit key
163057e
+         * confirmation (section 6.4.2.3.2), or assurance from a trusted third
163057e
+         * party (section 6.4.2.3.1) for key agreement or key transport, but
163057e
+         * explicit key confirmation is not implemented here and cannot be
163057e
+         * implemented without protocol changes, and the FIPS provider does not
163057e
+         * implement trusted third party validation, since it relies on its
163057e
+         * callers to do that. We must thus mark RSASVE unapproved until we
163057e
+         * have received clarification from NIST on how library modules such as
163057e
+         * OpenSSL should implement TTP validation. */
163057e
+        int fips_indicator = EVP_PKEY_REDHAT_FIPS_INDICATOR_NOT_APPROVED;
163057e
+
163057e
+        if (!OSSL_PARAM_set_int(p, fips_indicator))
163057e
+            return 0;
163057e
+    }
163057e
+#endif /* defined(FIPS_MODULE) */
163057e
 
163057e
-    return ctx != NULL;
163057e
+    return 1;
163057e
 }
163057e
 
163057e
 static const OSSL_PARAM known_gettable_rsakem_ctx_params[] = {
163057e
+#ifdef FIPS_MODULE
163057e
+    OSSL_PARAM_int(OSSL_KEM_PARAM_REDHAT_FIPS_INDICATOR, NULL),
163057e
+#endif /* defined(FIPS_MODULE) */
163057e
     OSSL_PARAM_END
163057e
 };
163057e
 
163057e
-- 
163057e
2.41.0
163057e