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

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