isaacpittman-hitachi / rpms / openssl

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