Blame SOURCES/openssl-1.1.1-fips-curves.patch

9b88be
diff -up openssl-1.1.1g/crypto/ec/ec_curve.c.fips-curves openssl-1.1.1g/crypto/ec/ec_curve.c
9b88be
--- openssl-1.1.1g/crypto/ec/ec_curve.c.fips-curves	2020-05-18 12:59:54.839643980 +0200
9b88be
+++ openssl-1.1.1g/crypto/ec/ec_curve.c	2020-05-18 12:59:54.852644093 +0200
9b88be
@@ -13,6 +13,7 @@
9b88be
 #include <openssl/err.h>
9b88be
 #include <openssl/obj_mac.h>
9b88be
 #include <openssl/opensslconf.h>
9b88be
+#include <openssl/crypto.h>
9b88be
 #include "internal/nelem.h"
9b88be
 
9b88be
 typedef struct {
9b88be
@@ -237,6 +238,7 @@ static const struct {
9b88be
 
9b88be
 typedef struct _ec_list_element_st {
9b88be
     int nid;
9b88be
+    int fips_allowed;
9b88be
     const EC_CURVE_DATA *data;
9b88be
     const EC_METHOD *(*meth) (void);
9b88be
     const char *comment;
9b88be
@@ -246,23 +248,23 @@ static const ec_list_element curve_list[
9b88be
     /* prime field curves */
9b88be
     /* secg curves */
9b88be
 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
9b88be
-    {NID_secp224r1, &_EC_NIST_PRIME_224.h, EC_GFp_nistp224_method,
9b88be
+    {NID_secp224r1, 1, &_EC_NIST_PRIME_224.h, EC_GFp_nistp224_method,
9b88be
      "NIST/SECG curve over a 224 bit prime field"},
9b88be
 #else
9b88be
-    {NID_secp224r1, &_EC_NIST_PRIME_224.h, 0,
9b88be
+    {NID_secp224r1, 1, &_EC_NIST_PRIME_224.h, 0,
9b88be
      "NIST/SECG curve over a 224 bit prime field"},
9b88be
 #endif
9b88be
-    {NID_secp256k1, &_EC_SECG_PRIME_256K1.h, 0,
9b88be
+    {NID_secp256k1, 0, &_EC_SECG_PRIME_256K1.h, 0,
9b88be
      "SECG curve over a 256 bit prime field"},
9b88be
     /* SECG secp256r1 is the same as X9.62 prime256v1 and hence omitted */
9b88be
-    {NID_secp384r1, &_EC_NIST_PRIME_384.h,
9b88be
+    {NID_secp384r1, 1, &_EC_NIST_PRIME_384.h,
9b88be
 # if defined(S390X_EC_ASM)
9b88be
      EC_GFp_s390x_nistp384_method,
9b88be
 # else
9b88be
      0,
9b88be
 # endif
9b88be
      "NIST/SECG curve over a 384 bit prime field"},
9b88be
-    {NID_secp521r1, &_EC_NIST_PRIME_521.h,
9b88be
+    {NID_secp521r1, 1, &_EC_NIST_PRIME_521.h,
9b88be
 # if defined(S390X_EC_ASM)
9b88be
      EC_GFp_s390x_nistp521_method,
9b88be
 # elif !defined(OPENSSL_NO_EC_NISTP_64_GCC_128)
9b88be
@@ -272,7 +274,7 @@ static const ec_list_element curve_list[
9b88be
 # endif
9b88be
      "NIST/SECG curve over a 521 bit prime field"},
9b88be
     /* X9.62 curves */
9b88be
-    {NID_X9_62_prime256v1, &_EC_X9_62_PRIME_256V1.h,
9b88be
+    {NID_X9_62_prime256v1, 1, &_EC_X9_62_PRIME_256V1.h,
9b88be
 #if defined(ECP_NISTZ256_ASM)
9b88be
      EC_GFp_nistz256_method,
9b88be
 # elif defined(S390X_EC_ASM)
9b88be
@@ -404,6 +406,10 @@ EC_GROUP *EC_GROUP_new_by_curve_name(int
9b88be
 
9b88be
     for (i = 0; i < curve_list_length; i++)
9b88be
         if (curve_list[i].nid == nid) {
9b88be
+            if (!curve_list[i].fips_allowed && FIPS_mode()) {
9b88be
+                ECerr(EC_F_EC_GROUP_NEW_BY_CURVE_NAME, EC_R_NOT_A_NIST_PRIME);
9b88be
+                return NULL;
9b88be
+            }
9b88be
             ret = ec_group_new_from_data(curve_list[i]);
9b88be
             break;
9b88be
         }
9b88be
@@ -418,19 +424,31 @@ EC_GROUP *EC_GROUP_new_by_curve_name(int
9b88be
 
9b88be
 size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems)
9b88be
 {
9b88be
-    size_t i, min;
9b88be
+    size_t i, j, num;
9b88be
+    int fips_mode = FIPS_mode();
9b88be
 
9b88be
-    if (r == NULL || nitems == 0)
9b88be
-        return curve_list_length;
9b88be
+    num = curve_list_length;
9b88be
+    if (fips_mode)
9b88be
+        for (i = 0; i < curve_list_length; i++) {
9b88be
+            if (!curve_list[i].fips_allowed)
9b88be
+                --num;
9b88be
+        }
9b88be
 
9b88be
-    min = nitems < curve_list_length ? nitems : curve_list_length;
9b88be
+    if (r == NULL || nitems == 0) {
9b88be
+        return num;
9b88be
+    }
9b88be
 
9b88be
-    for (i = 0; i < min; i++) {
9b88be
-        r[i].nid = curve_list[i].nid;
9b88be
-        r[i].comment = curve_list[i].comment;
9b88be
+    for (i = 0, j = 0; i < curve_list_length; i++) {
9b88be
+        if (j >= nitems)
9b88be
+            break;
9b88be
+        if (!fips_mode || curve_list[i].fips_allowed) {
9b88be
+            r[j].nid = curve_list[i].nid;
9b88be
+            r[j].comment = curve_list[i].comment;
9b88be
+            ++j;
9b88be
+        }
9b88be
     }
9b88be
 
9b88be
-    return curve_list_length;
9b88be
+    return num;
9b88be
 }
9b88be
 
9b88be
 /* Functions to translate between common NIST curve names and NIDs */
9b88be
diff -up openssl-1.1.1g/ssl/t1_lib.c.fips-curves openssl-1.1.1g/ssl/t1_lib.c
9b88be
--- openssl-1.1.1g/ssl/t1_lib.c.fips-curves	2020-05-18 12:59:54.797643616 +0200
9b88be
+++ openssl-1.1.1g/ssl/t1_lib.c	2020-05-18 13:03:54.748725463 +0200
9b88be
@@ -678,6 +678,36 @@ static const uint16_t tls12_sigalgs[] =
9b88be
 #endif
9b88be
 };
9b88be
 
9b88be
+static const uint16_t tls12_fips_sigalgs[] = {
9b88be
+#ifndef OPENSSL_NO_EC
9b88be
+    TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
9b88be
+    TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
9b88be
+    TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
9b88be
+#endif
9b88be
+
9b88be
+    TLSEXT_SIGALG_rsa_pss_pss_sha256,
9b88be
+    TLSEXT_SIGALG_rsa_pss_pss_sha384,
9b88be
+    TLSEXT_SIGALG_rsa_pss_pss_sha512,
9b88be
+    TLSEXT_SIGALG_rsa_pss_rsae_sha256,
9b88be
+    TLSEXT_SIGALG_rsa_pss_rsae_sha384,
9b88be
+    TLSEXT_SIGALG_rsa_pss_rsae_sha512,
9b88be
+
9b88be
+    TLSEXT_SIGALG_rsa_pkcs1_sha256,
9b88be
+    TLSEXT_SIGALG_rsa_pkcs1_sha384,
9b88be
+    TLSEXT_SIGALG_rsa_pkcs1_sha512,
9b88be
+
9b88be
+#ifndef OPENSSL_NO_EC
9b88be
+    TLSEXT_SIGALG_ecdsa_sha224,
9b88be
+#endif
9b88be
+    TLSEXT_SIGALG_rsa_pkcs1_sha224,
9b88be
+#ifndef OPENSSL_NO_DSA
9b88be
+    TLSEXT_SIGALG_dsa_sha224,
9b88be
+    TLSEXT_SIGALG_dsa_sha256,
9b88be
+    TLSEXT_SIGALG_dsa_sha384,
9b88be
+    TLSEXT_SIGALG_dsa_sha512,
9b88be
+#endif
9b88be
+};
9b88be
+
9b88be
 #ifndef OPENSSL_NO_EC
9b88be
 static const uint16_t suiteb_sigalgs[] = {
9b88be
     TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
9b88be
@@ -894,6 +924,8 @@ static const SIGALG_LOOKUP *tls1_get_leg
9b88be
     }
9b88be
     if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg))
9b88be
         return NULL;
9b88be
+    if (FIPS_mode()) /* We do not allow legacy SHA1 signatures in FIPS mode */
9b88be
+        return NULL;
9b88be
     if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) {
9b88be
         const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]);
9b88be
 
9b88be
@@ -954,6 +986,9 @@ size_t tls12_get_psigalgs(SSL *s, int se
9b88be
     } else if (s->cert->conf_sigalgs) {
9b88be
         *psigs = s->cert->conf_sigalgs;
9b88be
         return s->cert->conf_sigalgslen;
9b88be
+    } else if (FIPS_mode()) {
9b88be
+        *psigs = tls12_fips_sigalgs;
9b88be
+        return OSSL_NELEM(tls12_fips_sigalgs);
9b88be
     } else {
9b88be
         *psigs = tls12_sigalgs;
9b88be
         return OSSL_NELEM(tls12_sigalgs);
9b88be
@@ -973,6 +1008,9 @@ int tls_check_sigalg_curve(const SSL *s,
9b88be
     if (s->cert->conf_sigalgs) {
9b88be
         sigs = s->cert->conf_sigalgs;
9b88be
         siglen = s->cert->conf_sigalgslen;
9b88be
+    } else if (FIPS_mode()) {
9b88be
+        sigs = tls12_fips_sigalgs;
9b88be
+        siglen = OSSL_NELEM(tls12_fips_sigalgs);
9b88be
     } else {
9b88be
         sigs = tls12_sigalgs;
9b88be
         siglen = OSSL_NELEM(tls12_sigalgs);
9b88be
@@ -1617,6 +1655,8 @@ static int tls12_sigalg_allowed(const SS
9b88be
     if (lu->sig == NID_id_GostR3410_2012_256
9b88be
             || lu->sig == NID_id_GostR3410_2012_512
9b88be
             || lu->sig == NID_id_GostR3410_2001) {
9b88be
+        if (FIPS_mode())
9b88be
+            return 0;
9b88be
         /* We never allow GOST sig algs on the server with TLSv1.3 */
9b88be
         if (s->server && SSL_IS_TLS13(s))
9b88be
             return 0;
9b88be
@@ -2842,6 +2882,13 @@ int tls_choose_sigalg(SSL *s, int fatale
9b88be
                 const uint16_t *sent_sigs;
9b88be
                 size_t sent_sigslen;
9b88be
 
9b88be
+                if (fatalerrs && FIPS_mode()) {
9b88be
+                    /* There are no suitable legacy algorithms in FIPS mode */
9b88be
+                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
9b88be
+                             SSL_F_TLS_CHOOSE_SIGALG,
9b88be
+                             SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
9b88be
+                    return 0;
9b88be
+                }
9b88be
                 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
9b88be
                     if (!fatalerrs)
9b88be
                         return 1;