bmh10 / rpms / openssh

Forked from rpms/openssh 2 days ago
Clone
9070b3
diff -up openssh-8.6p1/cipher-ctr.c.fips openssh-8.6p1/cipher-ctr.c
9070b3
--- openssh-8.6p1/cipher-ctr.c.fips	2021-05-06 12:08:36.423926297 +0200
9070b3
+++ openssh-8.6p1/cipher-ctr.c	2021-05-06 12:08:36.497926869 +0200
9070b3
@@ -179,7 +179,8 @@ evp_aes_128_ctr(void)
9070b3
 	aes_ctr.do_cipher = ssh_aes_ctr;
9070b3
 #ifndef SSH_OLD_EVP
9070b3
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
9070b3
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
9070b3
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
9070b3
+	    EVP_CIPH_FLAG_FIPS;
9070b3
 #endif
9070b3
 	return (&aes_ctr);
9070b3
 }
9070b3
diff -up openssh-8.6p1/dh.c.fips openssh-8.6p1/dh.c
9070b3
--- openssh-8.6p1/dh.c.fips	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/dh.c	2021-05-06 12:12:10.107634472 +0200
9070b3
@@ -164,6 +164,12 @@ choose_dh(int min, int wantbits, int max
9070b3
 	int best, bestcount, which, linenum;
9070b3
 	struct dhgroup dhg;
9070b3
 
9070b3
+	if (FIPS_mode()) {
9070b3
+		logit("Using arbitrary primes is not allowed in FIPS mode."
9070b3
+		    " Falling back to known groups.");
9070b3
+		return (dh_new_group_fallback(max));
9070b3
+	}
9070b3
+
9070b3
 	if ((f = fopen(get_moduli_filename(), "r")) == NULL) {
9070b3
 		logit("WARNING: could not open %s (%s), using fixed modulus",
9070b3
 		    get_moduli_filename(), strerror(errno));
9070b3
@@ -502,4 +508,38 @@ dh_estimate(int bits)
9070b3
 	return 8192;
9070b3
 }
9070b3
 
9070b3
+/*
9070b3
+ * Compares the received DH parameters with known-good groups,
9070b3
+ * which might be either from group14, group16 or group18.
9070b3
+ */
9070b3
+int
9070b3
+dh_is_known_group(const DH *dh)
9070b3
+{
9070b3
+	const BIGNUM *p, *g;
9070b3
+	const BIGNUM *known_p, *known_g;
9070b3
+	DH *known = NULL;
9070b3
+	int bits = 0, rv = 0;
9070b3
+
9070b3
+	DH_get0_pqg(dh, &p, NULL, &g);
9070b3
+	bits = BN_num_bits(p);
9070b3
+
9070b3
+	if (bits <= 3072) {
9070b3
+		known = dh_new_group14();
9070b3
+	} else if (bits <= 6144) {
9070b3
+		known = dh_new_group16();
9070b3
+	} else {
9070b3
+		known = dh_new_group18();
9070b3
+	}
9070b3
+
9070b3
+	DH_get0_pqg(known, &known_p, NULL, &known_g);
9070b3
+
9070b3
+	if (BN_cmp(g, known_g) == 0 &&
9070b3
+	    BN_cmp(p, known_p) == 0) {
9070b3
+		rv = 1;
9070b3
+	}
9070b3
+
9070b3
+	DH_free(known);
9070b3
+	return rv;
9070b3
+}
9070b3
+
9070b3
 #endif /* WITH_OPENSSL */
9070b3
diff -up openssh-8.6p1/dh.h.fips openssh-8.6p1/dh.h
9070b3
--- openssh-8.6p1/dh.h.fips	2021-05-06 12:08:36.498926877 +0200
9070b3
+++ openssh-8.6p1/dh.h	2021-05-06 12:11:28.393298005 +0200
9070b3
@@ -45,6 +45,7 @@ DH	*dh_new_group_fallback(int);
9070b3
 
9070b3
 int	 dh_gen_key(DH *, int);
9070b3
 int	 dh_pub_is_valid(const DH *, const BIGNUM *);
9070b3
+int	 dh_is_known_group(const DH *);
9070b3
 
9070b3
 u_int	 dh_estimate(int);
9070b3
 void	 dh_set_moduli_file(const char *);
9070b3
diff -up openssh-8.6p1/kex.c.fips openssh-8.6p1/kex.c
9070b3
--- openssh-8.6p1/kex.c.fips	2021-05-06 12:08:36.489926807 +0200
9070b3
+++ openssh-8.6p1/kex.c	2021-05-06 12:08:36.498926877 +0200
9070b3
@@ -203,7 +203,10 @@ kex_names_valid(const char *names)
9070b3
 	for ((p = strsep(&cp, ",")); p && *p != '\0';
9070b3
 	    (p = strsep(&cp, ","))) {
9070b3
 		if (kex_alg_by_name(p) == NULL) {
9070b3
-			error("Unsupported KEX algorithm \"%.100s\"", p);
9070b3
+			if (FIPS_mode())
9070b3
+				error("\"%.100s\" is not allowed in FIPS mode", p);
9070b3
+			else
9070b3
+				error("Unsupported KEX algorithm \"%.100s\"", p);
9070b3
 			free(s);
9070b3
 			return 0;
9070b3
 		}
9070b3
diff -up openssh-8.6p1/kexgexc.c.fips openssh-8.6p1/kexgexc.c
9070b3
--- openssh-8.6p1/kexgexc.c.fips	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/kexgexc.c	2021-05-06 12:08:36.498926877 +0200
9070b3
@@ -28,6 +28,7 @@
9070b3
 
9070b3
 #ifdef WITH_OPENSSL
9070b3
 
9070b3
+#include <openssl/crypto.h>
9070b3
 #include <sys/types.h>
9070b3
 
9070b3
 #include <openssl/dh.h>
9070b3
@@ -115,6 +116,10 @@ input_kex_dh_gex_group(int type, u_int32
9070b3
 		r = SSH_ERR_ALLOC_FAIL;
9070b3
 		goto out;
9070b3
 	}
9070b3
+	if (FIPS_mode() && dh_is_known_group(kex->dh) == 0) {
9070b3
+		r = SSH_ERR_INVALID_ARGUMENT;
9070b3
+		goto out;
9070b3
+	}
9070b3
 	p = g = NULL; /* belong to kex->dh now */
9070b3
 
9070b3
 	/* generate and send 'e', client DH public key */
9070b3
diff -up openssh-8.6p1/myproposal.h.fips openssh-8.6p1/myproposal.h
9070b3
--- openssh-8.6p1/myproposal.h.fips	2021-04-16 05:55:25.000000000 +0200
9070b3
+++ openssh-8.6p1/myproposal.h	2021-05-06 12:08:36.498926877 +0200
9070b3
@@ -57,6 +57,20 @@
9070b3
 	"rsa-sha2-256," \
9070b3
 	"ssh-rsa"
9070b3
 
9070b3
+#define	KEX_FIPS_PK_ALG	\
9070b3
+	"ecdsa-sha2-nistp256-cert-v01@openssh.com," \
9070b3
+	"ecdsa-sha2-nistp384-cert-v01@openssh.com," \
9070b3
+	"ecdsa-sha2-nistp521-cert-v01@openssh.com," \
9070b3
+	"rsa-sha2-512-cert-v01@openssh.com," \
9070b3
+	"rsa-sha2-256-cert-v01@openssh.com," \
9070b3
+	"ssh-rsa-cert-v01@openssh.com," \
9070b3
+	"ecdsa-sha2-nistp256," \
9070b3
+	"ecdsa-sha2-nistp384," \
9070b3
+	"ecdsa-sha2-nistp521," \
9070b3
+	"rsa-sha2-512," \
9070b3
+	"rsa-sha2-256," \
9070b3
+	"ssh-rsa"
9070b3
+
9070b3
 #define	KEX_SERVER_ENCRYPT \
9070b3
 	"chacha20-poly1305@openssh.com," \
9070b3
 	"aes128-ctr,aes192-ctr,aes256-ctr," \
9070b3
@@ -78,6 +92,27 @@
9070b3
 
9070b3
 #define KEX_CLIENT_MAC KEX_SERVER_MAC
9070b3
 
9070b3
+#define	KEX_FIPS_ENCRYPT \
9070b3
+	"aes128-ctr,aes192-ctr,aes256-ctr," \
9070b3
+	"aes128-cbc,3des-cbc," \
9070b3
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se," \
9070b3
+	"aes128-gcm@openssh.com,aes256-gcm@openssh.com"
9070b3
+#define KEX_DEFAULT_KEX_FIPS		\
9070b3
+	"ecdh-sha2-nistp256," \
9070b3
+	"ecdh-sha2-nistp384," \
9070b3
+	"ecdh-sha2-nistp521," \
9070b3
+	"diffie-hellman-group-exchange-sha256," \
9070b3
+	"diffie-hellman-group16-sha512," \
9070b3
+	"diffie-hellman-group18-sha512," \
9070b3
+	"diffie-hellman-group14-sha256"
9070b3
+#define KEX_FIPS_MAC \
9070b3
+	"hmac-sha1," \
9070b3
+	"hmac-sha2-256," \
9070b3
+	"hmac-sha2-512," \
9070b3
+	"hmac-sha1-etm@openssh.com," \
9070b3
+	"hmac-sha2-256-etm@openssh.com," \
9070b3
+	"hmac-sha2-512-etm@openssh.com"
9070b3
+
9070b3
 /* Not a KEX value, but here so all the algorithm defaults are together */
9070b3
 #define	SSH_ALLOWED_CA_SIGALGS	\
9070b3
 	"ssh-ed25519," \
9070b3
diff -up openssh-8.6p1/readconf.c.fips openssh-8.6p1/readconf.c
9070b3
--- openssh-8.6p1/readconf.c.fips	2021-05-06 12:08:36.428926336 +0200
9070b3
+++ openssh-8.6p1/readconf.c	2021-05-06 12:08:36.499926885 +0200
9070b3
@@ -2538,11 +2538,16 @@ fill_default_options(Options * options)
9070b3
 	all_key = sshkey_alg_list(0, 0, 1, ',');
9070b3
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
9070b3
 	/* remove unsupported algos from default lists */
9070b3
-	def_cipher = match_filter_allowlist(KEX_CLIENT_ENCRYPT, all_cipher);
9070b3
-	def_mac = match_filter_allowlist(KEX_CLIENT_MAC, all_mac);
9070b3
-	def_kex = match_filter_allowlist(KEX_CLIENT_KEX, all_kex);
9070b3
-	def_key = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
9070b3
-	def_sig = match_filter_allowlist(SSH_ALLOWED_CA_SIGALGS, all_sig);
9070b3
+	def_cipher = match_filter_allowlist((FIPS_mode() ?
9070b3
+	    KEX_FIPS_ENCRYPT : KEX_CLIENT_ENCRYPT), all_cipher);
9070b3
+	def_mac = match_filter_allowlist((FIPS_mode() ?
9070b3
+	    KEX_FIPS_MAC : KEX_CLIENT_MAC), all_mac);
9070b3
+	def_kex = match_filter_allowlist((FIPS_mode() ?
9070b3
+	    KEX_DEFAULT_KEX_FIPS : KEX_CLIENT_KEX), all_kex);
9070b3
+	def_key = match_filter_allowlist((FIPS_mode() ?
9070b3
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG), all_key);
9070b3
+	def_sig = match_filter_allowlist((FIPS_mode() ?
9070b3
+	    KEX_FIPS_PK_ALG : SSH_ALLOWED_CA_SIGALGS), all_sig);
9070b3
 #define ASSEMBLE(what, defaults, all) \
9070b3
 	do { \
9070b3
 		if ((r = kex_assemble_names(&options->what, \
9070b3
diff -up openssh-8.6p1/sandbox-seccomp-filter.c.fips openssh-8.6p1/sandbox-seccomp-filter.c
9070b3
--- openssh-8.6p1/sandbox-seccomp-filter.c.fips	2021-05-06 12:08:36.463926606 +0200
9070b3
+++ openssh-8.6p1/sandbox-seccomp-filter.c	2021-05-06 12:08:36.499926885 +0200
9070b3
@@ -160,6 +160,9 @@ static const struct sock_filter preauth_
9070b3
 #ifdef __NR_open
9070b3
 	SC_DENY(__NR_open, EACCES),
9070b3
 #endif
9070b3
+#ifdef __NR_socket
9070b3
+	SC_DENY(__NR_socket, EACCES),
9070b3
+#endif
9070b3
 #ifdef __NR_openat
9070b3
 	SC_DENY(__NR_openat, EACCES),
9070b3
 #endif
9070b3
diff -up openssh-8.6p1/servconf.c.fips openssh-8.6p1/servconf.c
9070b3
--- openssh-8.6p1/servconf.c.fips	2021-05-06 12:08:36.455926545 +0200
9070b3
+++ openssh-8.6p1/servconf.c	2021-05-06 12:08:36.500926893 +0200
9070b3
@@ -226,11 +226,16 @@ assemble_algorithms(ServerOptions *o)
9070b3
 	all_key = sshkey_alg_list(0, 0, 1, ',');
9070b3
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
9070b3
 	/* remove unsupported algos from default lists */
9070b3
-	def_cipher = match_filter_allowlist(KEX_SERVER_ENCRYPT, all_cipher);
9070b3
-	def_mac = match_filter_allowlist(KEX_SERVER_MAC, all_mac);
9070b3
-	def_kex = match_filter_allowlist(KEX_SERVER_KEX, all_kex);
9070b3
-	def_key = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
9070b3
-	def_sig = match_filter_allowlist(SSH_ALLOWED_CA_SIGALGS, all_sig);
9070b3
+	def_cipher = match_filter_allowlist((FIPS_mode() ?
9070b3
+	    KEX_FIPS_ENCRYPT : KEX_SERVER_ENCRYPT), all_cipher);
9070b3
+	def_mac = match_filter_allowlist((FIPS_mode() ?
9070b3
+	    KEX_FIPS_MAC : KEX_SERVER_MAC), all_mac);
9070b3
+	def_kex = match_filter_allowlist((FIPS_mode() ?
9070b3
+	    KEX_DEFAULT_KEX_FIPS : KEX_SERVER_KEX), all_kex);
9070b3
+	def_key = match_filter_allowlist((FIPS_mode() ?
9070b3
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG), all_key);
9070b3
+	def_sig = match_filter_allowlist((FIPS_mode() ?
9070b3
+	    KEX_FIPS_PK_ALG : SSH_ALLOWED_CA_SIGALGS), all_sig);
9070b3
 #define ASSEMBLE(what, defaults, all) \
9070b3
 	do { \
9070b3
 		if ((r = kex_assemble_names(&o->what, defaults, all)) != 0) \
9070b3
diff -up openssh-8.6p1/ssh.c.fips openssh-8.6p1/ssh.c
9070b3
--- openssh-8.6p1/ssh.c.fips	2021-05-06 12:08:36.467926637 +0200
9070b3
+++ openssh-8.6p1/ssh.c	2021-05-06 12:08:36.500926893 +0200
9070b3
@@ -77,6 +77,7 @@
9070b3
 #include <openssl/evp.h>
9070b3
 #include <openssl/err.h>
9070b3
 #endif
9070b3
+#include <openssl/crypto.h>
9070b3
 #include "openbsd-compat/openssl-compat.h"
9070b3
 #include "openbsd-compat/sys-queue.h"
9070b3
 
9070b3
@@ -1516,6 +1517,10 @@ main(int ac, char **av)
9070b3
 		exit(0);
9070b3
 	}
9070b3
 
9070b3
+	if (FIPS_mode()) {
9070b3
+		debug("FIPS mode initialized");
9070b3
+	}
9070b3
+
9070b3
 	/* Expand SecurityKeyProvider if it refers to an environment variable */
9070b3
 	if (options.sk_provider != NULL && *options.sk_provider == '$' &&
9070b3
 	    strlen(options.sk_provider) > 1) {
9070b3
diff -up openssh-8.6p1/sshconnect2.c.fips openssh-8.6p1/sshconnect2.c
9070b3
--- openssh-8.6p1/sshconnect2.c.fips	2021-05-06 12:08:36.485926777 +0200
9070b3
+++ openssh-8.6p1/sshconnect2.c	2021-05-06 12:08:36.501926900 +0200
9070b3
@@ -45,6 +45,8 @@
9070b3
 #include <vis.h>
9070b3
 #endif
9070b3
 
9070b3
+#include <openssl/crypto.h>
9070b3
+
9070b3
 #include "openbsd-compat/sys-queue.h"
9070b3
 
9070b3
 #include "xmalloc.h"
9070b3
@@ -269,36 +271,41 @@ ssh_kex2(struct ssh *ssh, char *host, st
9070b3
 
9070b3
 #if defined(GSSAPI) && defined(WITH_OPENSSL)
9070b3
 	if (options.gss_keyex) {
9070b3
-		/* Add the GSSAPI mechanisms currently supported on this
9070b3
-		 * client to the key exchange algorithm proposal */
9070b3
-		orig = myproposal[PROPOSAL_KEX_ALGS];
9070b3
-
9070b3
-		if (options.gss_server_identity) {
9070b3
-			gss_host = xstrdup(options.gss_server_identity);
9070b3
-		} else if (options.gss_trust_dns) {
9070b3
-			gss_host = remote_hostname(ssh);
9070b3
-			/* Fall back to specified host if we are using proxy command
9070b3
-			 * and can not use DNS on that socket */
9070b3
-			if (strcmp(gss_host, "UNKNOWN") == 0) {
9070b3
-				free(gss_host);
9070b3
+		if (FIPS_mode()) {
9070b3
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
9070b3
+			options.gss_keyex = 0;
9070b3
+		} else {
9070b3
+			/* Add the GSSAPI mechanisms currently supported on this
9070b3
+			 * client to the key exchange algorithm proposal */
9070b3
+			orig = myproposal[PROPOSAL_KEX_ALGS];
9070b3
+
9070b3
+			if (options.gss_server_identity) {
9070b3
+				gss_host = xstrdup(options.gss_server_identity);
9070b3
+			} else if (options.gss_trust_dns) {
9070b3
+				gss_host = remote_hostname(ssh);
9070b3
+				/* Fall back to specified host if we are using proxy command
9070b3
+				 * and can not use DNS on that socket */
9070b3
+				if (strcmp(gss_host, "UNKNOWN") == 0) {
9070b3
+					free(gss_host);
9070b3
+					gss_host = xstrdup(host);
9070b3
+				}
9070b3
+			} else {
9070b3
 				gss_host = xstrdup(host);
9070b3
 			}
9070b3
-		} else {
9070b3
-			gss_host = xstrdup(host);
9070b3
-		}
9070b3
 
9070b3
-		gss = ssh_gssapi_client_mechanisms(gss_host,
9070b3
-		    options.gss_client_identity, options.gss_kex_algorithms);
9070b3
-		if (gss) {
9070b3
-			debug("Offering GSSAPI proposal: %s", gss);
9070b3
-			xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
9070b3
-			    "%s,%s", gss, orig);
9070b3
-
9070b3
-			/* If we've got GSSAPI algorithms, then we also support the
9070b3
-			 * 'null' hostkey, as a last resort */
9070b3
-			orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
9070b3
-			xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
9070b3
-			    "%s,null", orig);
9070b3
+			gss = ssh_gssapi_client_mechanisms(gss_host,
9070b3
+			    options.gss_client_identity, options.gss_kex_algorithms);
9070b3
+			if (gss) {
9070b3
+				debug("Offering GSSAPI proposal: %s", gss);
9070b3
+				xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
9070b3
+				    "%s,%s", gss, orig);
9070b3
+
9070b3
+				/* If we've got GSSAPI algorithms, then we also support the
9070b3
+				 * 'null' hostkey, as a last resort */
9070b3
+				orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
9070b3
+				xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
9070b3
+				    "%s,null", orig);
9070b3
+			}
9070b3
 		}
9070b3
 	}
9070b3
 #endif
9070b3
diff -up openssh-8.6p1/sshd.c.fips openssh-8.6p1/sshd.c
9070b3
--- openssh-8.6p1/sshd.c.fips	2021-05-06 12:08:36.493926838 +0200
9070b3
+++ openssh-8.6p1/sshd.c	2021-05-06 12:13:56.501492639 +0200
9070b3
@@ -66,6 +66,7 @@
9070b3
 #include <grp.h>
9070b3
 #include <pwd.h>
9070b3
 #include <signal.h>
9070b3
+#include <syslog.h>
9070b3
 #include <stdarg.h>
9070b3
 #include <stdio.h>
9070b3
 #include <stdlib.h>
9070b3
@@ -77,6 +78,7 @@
9070b3
 #include <openssl/dh.h>
9070b3
 #include <openssl/bn.h>
9070b3
 #include <openssl/rand.h>
9070b3
+#include <openssl/crypto.h>
9070b3
 #include "openbsd-compat/openssl-compat.h"
9070b3
 #endif
9070b3
 
9070b3
@@ -1619,6 +1621,7 @@ main(int ac, char **av)
9070b3
 #endif
9070b3
 	__progname = ssh_get_progname(av[0]);
9070b3
 
9070b3
+	OpenSSL_add_all_algorithms();
9070b3
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
9070b3
 	saved_argc = ac;
9070b3
 	rexec_argc = ac;
9070b3
@@ -2110,6 +2113,10 @@ main(int ac, char **av)
9070b3
 	/* Reinitialize the log (because of the fork above). */
9070b3
 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
9070b3
 
9070b3
+	if (FIPS_mode()) {
9070b3
+		debug("FIPS mode initialized");
9070b3
+	}
9070b3
+
9070b3
 	/*
9070b3
 	 * Chdir to the root directory so that the current disk can be
9070b3
 	 * unmounted if desired.
9070b3
@@ -2494,10 +2501,14 @@ do_ssh2_kex(struct ssh *ssh)
9070b3
 	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
9070b3
 		orig = NULL;
9070b3
 
9070b3
-	if (options.gss_keyex)
9070b3
-		gss = ssh_gssapi_server_mechanisms();
9070b3
-	else
9070b3
-		gss = NULL;
9070b3
+	if (options.gss_keyex) {
9070b3
+		if (FIPS_mode()) {
9070b3
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
9070b3
+			options.gss_keyex = 0;
9070b3
+		} else {
9070b3
+			gss = ssh_gssapi_server_mechanisms();
9070b3
+		}
9070b3
+	}
9070b3
 
9070b3
 	if (gss && orig)
9070b3
 		xasprintf(&newstr, "%s,%s", gss, orig);
9070b3
diff -up openssh-8.6p1/sshkey.c.fips openssh-8.6p1/sshkey.c
9070b3
--- openssh-8.6p1/sshkey.c.fips	2021-05-06 12:08:36.493926838 +0200
9070b3
+++ openssh-8.6p1/sshkey.c	2021-05-06 12:08:36.502926908 +0200
9070b3
@@ -34,6 +34,7 @@
9070b3
 #include <openssl/evp.h>
9070b3
 #include <openssl/err.h>
9070b3
 #include <openssl/pem.h>
9070b3
+#include <openssl/crypto.h>
9070b3
 #endif
9070b3
 
9070b3
 #include "crypto_api.h"
9070b3
@@ -57,6 +58,7 @@
9070b3
 #define SSHKEY_INTERNAL
9070b3
 #include "sshkey.h"
9070b3
 #include "match.h"
9070b3
+#include "log.h"
9070b3
 #include "ssh-sk.h"
9070b3
 
9070b3
 #ifdef WITH_XMSS
9070b3
@@ -1705,6 +1707,8 @@ rsa_generate_private_key(u_int bits, RSA
9070b3
 	}
9070b3
 	if (!BN_set_word(f4, RSA_F4) ||
9070b3
 	    !RSA_generate_key_ex(private, bits, f4, NULL)) {
9070b3
+			if (FIPS_mode())
9070b3
+				logit_f("the key length might be unsupported by FIPS mode approved key generation method");
9070b3
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
9070b3
 		goto out;
9070b3
 	}
9070b3
diff -up openssh-8.6p1/ssh-keygen.c.fips openssh-8.6p1/ssh-keygen.c
9070b3
--- openssh-8.6p1/ssh-keygen.c.fips	2021-05-06 12:08:36.467926637 +0200
9070b3
+++ openssh-8.6p1/ssh-keygen.c	2021-05-06 12:08:36.503926916 +0200
9070b3
@@ -205,6 +205,12 @@ type_bits_valid(int type, const char *na
9070b3
 #endif
9070b3
 	}
9070b3
 #ifdef WITH_OPENSSL
9070b3
+	if (FIPS_mode()) {
9070b3
+		if (type == KEY_DSA)
9070b3
+			fatal("DSA keys are not allowed in FIPS mode");
9070b3
+		if (type == KEY_ED25519)
9070b3
+			fatal("ED25519 keys are not allowed in FIPS mode");
9070b3
+	}
9070b3
 	switch (type) {
9070b3
 	case KEY_DSA:
9070b3
 		if (*bitsp != 1024)
9070b3
@@ -1098,9 +1104,17 @@ do_gen_all_hostkeys(struct passwd *pw)
9070b3
 			first = 1;
9070b3
 			printf("%s: generating new host keys: ", __progname);
9070b3
 		}
9070b3
+		type = sshkey_type_from_name(key_types[i].key_type);
9070b3
+
9070b3
+		/* Skip the keys that are not supported in FIPS mode */
9070b3
+		if (FIPS_mode() && (type == KEY_DSA || type == KEY_ED25519)) {
9070b3
+			logit("Skipping %s key in FIPS mode",
9070b3
+			    key_types[i].key_type_display);
9070b3
+			goto next;
9070b3
+		}
9070b3
+
9070b3
 		printf("%s ", key_types[i].key_type_display);
9070b3
 		fflush(stdout);
9070b3
-		type = sshkey_type_from_name(key_types[i].key_type);
9070b3
 		if ((fd = mkstemp(prv_tmp)) == -1) {
9070b3
 			error("Could not save your private key in %s: %s",
9070b3
 			    prv_tmp, strerror(errno));