jonathancammack / rpms / openssh

Forked from rpms/openssh 9 months ago
Clone
943807
diff -up openssh-8.6p1/dh.c.fips openssh-8.6p1/dh.c
943807
--- openssh-8.6p1/dh.c.fips	2021-04-16 05:55:25.000000000 +0200
943807
+++ openssh-8.6p1/dh.c	2021-05-06 12:12:10.107634472 +0200
2b784c
@@ -36,6 +36,7 @@
2b784c
 
2b784c
 #include <openssl/bn.h>
2b784c
 #include <openssl/dh.h>
2b784c
+#include <openssl/fips.h>
2b784c
 
2b784c
 #include "dh.h"
2b784c
 #include "pathnames.h"
943807
@@ -164,6 +164,12 @@ choose_dh(int min, int wantbits, int max
943807
 	int best, bestcount, which, linenum;
943807
 	struct dhgroup dhg;
943807
 
943807
+	if (FIPS_mode()) {
39b801
+		verbose("Using arbitrary primes is not allowed in FIPS mode."
943807
+		    " Falling back to known groups.");
943807
+		return (dh_new_group_fallback(max));
943807
+	}
943807
+
943807
 	if ((f = fopen(get_moduli_filename(), "r")) == NULL) {
943807
 		logit("WARNING: could not open %s (%s), using fixed modulus",
943807
 		    get_moduli_filename(), strerror(errno));
943807
@@ -502,4 +508,38 @@ dh_estimate(int bits)
943807
 	return 8192;
943807
 }
943807
 
943807
+/*
943807
+ * Compares the received DH parameters with known-good groups,
943807
+ * which might be either from group14, group16 or group18.
943807
+ */
943807
+int
943807
+dh_is_known_group(const DH *dh)
943807
+{
943807
+	const BIGNUM *p, *g;
943807
+	const BIGNUM *known_p, *known_g;
943807
+	DH *known = NULL;
943807
+	int bits = 0, rv = 0;
943807
+
943807
+	DH_get0_pqg(dh, &p, NULL, &g);
943807
+	bits = BN_num_bits(p);
943807
+
943807
+	if (bits <= 3072) {
943807
+		known = dh_new_group14();
943807
+	} else if (bits <= 6144) {
943807
+		known = dh_new_group16();
943807
+	} else {
943807
+		known = dh_new_group18();
943807
+	}
943807
+
943807
+	DH_get0_pqg(known, &known_p, NULL, &known_g);
943807
+
943807
+	if (BN_cmp(g, known_g) == 0 &&
943807
+	    BN_cmp(p, known_p) == 0) {
943807
+		rv = 1;
943807
+	}
943807
+
943807
+	DH_free(known);
943807
+	return rv;
943807
+}
943807
+
943807
 #endif /* WITH_OPENSSL */
943807
diff -up openssh-8.6p1/dh.h.fips openssh-8.6p1/dh.h
943807
--- openssh-8.6p1/dh.h.fips	2021-05-06 12:08:36.498926877 +0200
943807
+++ openssh-8.6p1/dh.h	2021-05-06 12:11:28.393298005 +0200
943807
@@ -45,6 +45,7 @@ DH	*dh_new_group_fallback(int);
943807
 
943807
 int	 dh_gen_key(DH *, int);
943807
 int	 dh_pub_is_valid(const DH *, const BIGNUM *);
943807
+int	 dh_is_known_group(const DH *);
943807
 
943807
 u_int	 dh_estimate(int);
943807
 void	 dh_set_moduli_file(const char *);
943807
diff -up openssh-8.6p1/kex.c.fips openssh-8.6p1/kex.c
943807
--- openssh-8.6p1/kex.c.fips	2021-05-06 12:08:36.489926807 +0200
943807
+++ openssh-8.6p1/kex.c	2021-05-06 12:08:36.498926877 +0200
2b784c
@@ -39,6 +39,7 @@
2b784c
 
2b784c
 #ifdef WITH_OPENSSL
2b784c
 #include <openssl/crypto.h>
2b784c
+#include <openssl/fips.h>
2b784c
 #include <openssl/dh.h>
2b784c
 # ifdef HAVE_EVP_KDF_CTX_NEW_ID
2b784c
 # include <openssl/kdf.h>
943807
@@ -203,7 +203,10 @@ kex_names_valid(const char *names)
943807
 	for ((p = strsep(&cp, ",")); p && *p != '\0';
943807
 	    (p = strsep(&cp, ","))) {
943807
 		if (kex_alg_by_name(p) == NULL) {
943807
-			error("Unsupported KEX algorithm \"%.100s\"", p);
943807
+			if (FIPS_mode())
943807
+				error("\"%.100s\" is not allowed in FIPS mode", p);
943807
+			else
943807
+				error("Unsupported KEX algorithm \"%.100s\"", p);
943807
 			free(s);
943807
 			return 0;
943807
 		}
943807
diff -up openssh-8.6p1/kexgexc.c.fips openssh-8.6p1/kexgexc.c
943807
--- openssh-8.6p1/kexgexc.c.fips	2021-04-16 05:55:25.000000000 +0200
943807
+++ openssh-8.6p1/kexgexc.c	2021-05-06 12:08:36.498926877 +0200
943807
@@ -28,6 +28,7 @@
943807
 
943807
 #ifdef WITH_OPENSSL
943807
 
2b784c
+#include <openssl/fips.h>
943807
 #include <sys/types.h>
943807
 
943807
 #include <openssl/dh.h>
943807
@@ -115,6 +116,10 @@ input_kex_dh_gex_group(int type, u_int32
943807
 		r = SSH_ERR_ALLOC_FAIL;
943807
 		goto out;
943807
 	}
943807
+	if (FIPS_mode() && dh_is_known_group(kex->dh) == 0) {
943807
+		r = SSH_ERR_INVALID_ARGUMENT;
943807
+		goto out;
943807
+	}
943807
 	p = g = NULL; /* belong to kex->dh now */
943807
 
943807
 	/* generate and send 'e', client DH public key */
943807
diff -up openssh-8.6p1/myproposal.h.fips openssh-8.6p1/myproposal.h
943807
--- openssh-8.6p1/myproposal.h.fips	2021-04-16 05:55:25.000000000 +0200
943807
+++ openssh-8.6p1/myproposal.h	2021-05-06 12:08:36.498926877 +0200
39b801
@@ -57,6 +57,18 @@
943807
 	"rsa-sha2-256," \
943807
 	"ssh-rsa"
943807
 
943807
+#define	KEX_FIPS_PK_ALG	\
943807
+	"ecdsa-sha2-nistp256-cert-v01@openssh.com," \
943807
+	"ecdsa-sha2-nistp384-cert-v01@openssh.com," \
943807
+	"ecdsa-sha2-nistp521-cert-v01@openssh.com," \
943807
+	"rsa-sha2-512-cert-v01@openssh.com," \
943807
+	"rsa-sha2-256-cert-v01@openssh.com," \
943807
+	"ecdsa-sha2-nistp256," \
943807
+	"ecdsa-sha2-nistp384," \
943807
+	"ecdsa-sha2-nistp521," \
943807
+	"rsa-sha2-512," \
39b801
+	"rsa-sha2-256"
943807
+
943807
 #define	KEX_SERVER_ENCRYPT \
943807
 	"chacha20-poly1305@openssh.com," \
943807
 	"aes128-ctr,aes192-ctr,aes256-ctr," \
943807
@@ -78,6 +92,27 @@
943807
 
943807
 #define KEX_CLIENT_MAC KEX_SERVER_MAC
943807
 
943807
+#define	KEX_FIPS_ENCRYPT \
943807
+	"aes128-ctr,aes192-ctr,aes256-ctr," \
943807
+	"aes128-cbc,3des-cbc," \
943807
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se," \
943807
+	"aes128-gcm@openssh.com,aes256-gcm@openssh.com"
943807
+#define KEX_DEFAULT_KEX_FIPS		\
943807
+	"ecdh-sha2-nistp256," \
943807
+	"ecdh-sha2-nistp384," \
943807
+	"ecdh-sha2-nistp521," \
943807
+	"diffie-hellman-group-exchange-sha256," \
943807
+	"diffie-hellman-group16-sha512," \
943807
+	"diffie-hellman-group18-sha512," \
943807
+	"diffie-hellman-group14-sha256"
943807
+#define KEX_FIPS_MAC \
943807
+	"hmac-sha1," \
943807
+	"hmac-sha2-256," \
943807
+	"hmac-sha2-512," \
943807
+	"hmac-sha1-etm@openssh.com," \
943807
+	"hmac-sha2-256-etm@openssh.com," \
943807
+	"hmac-sha2-512-etm@openssh.com"
943807
+
943807
 /* Not a KEX value, but here so all the algorithm defaults are together */
943807
 #define	SSH_ALLOWED_CA_SIGALGS	\
943807
 	"ssh-ed25519," \
943807
diff -up openssh-8.6p1/readconf.c.fips openssh-8.6p1/readconf.c
943807
--- openssh-8.6p1/readconf.c.fips	2021-05-06 12:08:36.428926336 +0200
943807
+++ openssh-8.6p1/readconf.c	2021-05-06 12:08:36.499926885 +0200
2b784c
@@ -39,6 +39,7 @@
2b784c
 #include <string.h>
2b784c
 #include <stdarg.h>
2b784c
 #include <unistd.h>
2b784c
+#include <openssl/fips.h>
2b784c
 #ifdef USE_SYSTEM_GLOB
2b784c
 # include <glob.h>
2b784c
 #else
943807
@@ -2538,11 +2538,16 @@ fill_default_options(Options * options)
943807
 	all_key = sshkey_alg_list(0, 0, 1, ',');
943807
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
943807
 	/* remove unsupported algos from default lists */
943807
-	def_cipher = match_filter_allowlist(KEX_CLIENT_ENCRYPT, all_cipher);
943807
-	def_mac = match_filter_allowlist(KEX_CLIENT_MAC, all_mac);
943807
-	def_kex = match_filter_allowlist(KEX_CLIENT_KEX, all_kex);
943807
-	def_key = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
943807
-	def_sig = match_filter_allowlist(SSH_ALLOWED_CA_SIGALGS, all_sig);
943807
+	def_cipher = match_filter_allowlist((FIPS_mode() ?
943807
+	    KEX_FIPS_ENCRYPT : KEX_CLIENT_ENCRYPT), all_cipher);
943807
+	def_mac = match_filter_allowlist((FIPS_mode() ?
943807
+	    KEX_FIPS_MAC : KEX_CLIENT_MAC), all_mac);
943807
+	def_kex = match_filter_allowlist((FIPS_mode() ?
943807
+	    KEX_DEFAULT_KEX_FIPS : KEX_CLIENT_KEX), all_kex);
943807
+	def_key = match_filter_allowlist((FIPS_mode() ?
943807
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG), all_key);
943807
+	def_sig = match_filter_allowlist((FIPS_mode() ?
943807
+	    KEX_FIPS_PK_ALG : SSH_ALLOWED_CA_SIGALGS), all_sig);
943807
 #define ASSEMBLE(what, defaults, all) \
943807
 	do { \
943807
 		if ((r = kex_assemble_names(&options->what, \
943807
diff -up openssh-8.6p1/sandbox-seccomp-filter.c.fips openssh-8.6p1/sandbox-seccomp-filter.c
943807
--- openssh-8.6p1/sandbox-seccomp-filter.c.fips	2021-05-06 12:08:36.463926606 +0200
943807
+++ openssh-8.6p1/sandbox-seccomp-filter.c	2021-05-06 12:08:36.499926885 +0200
943807
@@ -160,6 +160,9 @@ static const struct sock_filter preauth_
943807
 #ifdef __NR_open
943807
 	SC_DENY(__NR_open, EACCES),
943807
 #endif
943807
+#ifdef __NR_socket
943807
+	SC_DENY(__NR_socket, EACCES),
943807
+#endif
943807
 #ifdef __NR_openat
943807
 	SC_DENY(__NR_openat, EACCES),
943807
 #endif
943807
diff -up openssh-8.6p1/servconf.c.fips openssh-8.6p1/servconf.c
943807
--- openssh-8.6p1/servconf.c.fips	2021-05-06 12:08:36.455926545 +0200
943807
+++ openssh-8.6p1/servconf.c	2021-05-06 12:08:36.500926893 +0200
2b784c
@@ -38,6 +38,7 @@
2b784c
 #include <limits.h>
2b784c
 #include <stdarg.h>
2b784c
 #include <errno.h>
2b784c
+#include <openssl/fips.h>
2b784c
 #ifdef HAVE_UTIL_H
2b784c
 #include <util.h>
2b784c
 #endif
943807
@@ -226,11 +226,16 @@ assemble_algorithms(ServerOptions *o)
943807
 	all_key = sshkey_alg_list(0, 0, 1, ',');
943807
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
943807
 	/* remove unsupported algos from default lists */
943807
-	def_cipher = match_filter_allowlist(KEX_SERVER_ENCRYPT, all_cipher);
943807
-	def_mac = match_filter_allowlist(KEX_SERVER_MAC, all_mac);
943807
-	def_kex = match_filter_allowlist(KEX_SERVER_KEX, all_kex);
943807
-	def_key = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
943807
-	def_sig = match_filter_allowlist(SSH_ALLOWED_CA_SIGALGS, all_sig);
943807
+	def_cipher = match_filter_allowlist((FIPS_mode() ?
943807
+	    KEX_FIPS_ENCRYPT : KEX_SERVER_ENCRYPT), all_cipher);
943807
+	def_mac = match_filter_allowlist((FIPS_mode() ?
943807
+	    KEX_FIPS_MAC : KEX_SERVER_MAC), all_mac);
943807
+	def_kex = match_filter_allowlist((FIPS_mode() ?
943807
+	    KEX_DEFAULT_KEX_FIPS : KEX_SERVER_KEX), all_kex);
943807
+	def_key = match_filter_allowlist((FIPS_mode() ?
943807
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG), all_key);
943807
+	def_sig = match_filter_allowlist((FIPS_mode() ?
943807
+	    KEX_FIPS_PK_ALG : SSH_ALLOWED_CA_SIGALGS), all_sig);
943807
 #define ASSEMBLE(what, defaults, all) \
943807
 	do { \
943807
 		if ((r = kex_assemble_names(&o->what, defaults, all)) != 0) \
943807
diff -up openssh-8.6p1/ssh.c.fips openssh-8.6p1/ssh.c
943807
--- openssh-8.6p1/ssh.c.fips	2021-05-06 12:08:36.467926637 +0200
943807
+++ openssh-8.6p1/ssh.c	2021-05-06 12:08:36.500926893 +0200
943807
@@ -77,6 +77,7 @@
943807
 #include <openssl/evp.h>
943807
 #include <openssl/err.h>
943807
 #endif
2b784c
+#include <openssl/fips.h>
943807
 #include "openbsd-compat/openssl-compat.h"
943807
 #include "openbsd-compat/sys-queue.h"
943807
 
943807
@@ -1516,6 +1517,10 @@ main(int ac, char **av)
943807
 		exit(0);
943807
 	}
943807
 
943807
+	if (FIPS_mode()) {
943807
+		debug("FIPS mode initialized");
943807
+	}
943807
+
943807
 	/* Expand SecurityKeyProvider if it refers to an environment variable */
943807
 	if (options.sk_provider != NULL && *options.sk_provider == '$' &&
943807
 	    strlen(options.sk_provider) > 1) {
943807
diff -up openssh-8.6p1/sshconnect2.c.fips openssh-8.6p1/sshconnect2.c
943807
--- openssh-8.6p1/sshconnect2.c.fips	2021-05-06 12:08:36.485926777 +0200
943807
+++ openssh-8.6p1/sshconnect2.c	2021-05-06 12:08:36.501926900 +0200
943807
@@ -45,6 +45,8 @@
943807
 #include <vis.h>
943807
 #endif
943807
 
2b784c
+#include <openssl/fips.h>
943807
+
943807
 #include "openbsd-compat/sys-queue.h"
943807
 
943807
 #include "xmalloc.h"
943807
@@ -269,36 +271,41 @@ ssh_kex2(struct ssh *ssh, char *host, st
943807
 
943807
 #if defined(GSSAPI) && defined(WITH_OPENSSL)
943807
 	if (options.gss_keyex) {
943807
-		/* Add the GSSAPI mechanisms currently supported on this
943807
-		 * client to the key exchange algorithm proposal */
943807
-		orig = myproposal[PROPOSAL_KEX_ALGS];
943807
-
943807
-		if (options.gss_server_identity) {
943807
-			gss_host = xstrdup(options.gss_server_identity);
943807
-		} else if (options.gss_trust_dns) {
943807
-			gss_host = remote_hostname(ssh);
943807
-			/* Fall back to specified host if we are using proxy command
943807
-			 * and can not use DNS on that socket */
943807
-			if (strcmp(gss_host, "UNKNOWN") == 0) {
943807
-				free(gss_host);
943807
+		if (FIPS_mode()) {
943807
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
943807
+			options.gss_keyex = 0;
943807
+		} else {
943807
+			/* Add the GSSAPI mechanisms currently supported on this
943807
+			 * client to the key exchange algorithm proposal */
943807
+			orig = myproposal[PROPOSAL_KEX_ALGS];
943807
+
943807
+			if (options.gss_server_identity) {
943807
+				gss_host = xstrdup(options.gss_server_identity);
943807
+			} else if (options.gss_trust_dns) {
943807
+				gss_host = remote_hostname(ssh);
943807
+				/* Fall back to specified host if we are using proxy command
943807
+				 * and can not use DNS on that socket */
943807
+				if (strcmp(gss_host, "UNKNOWN") == 0) {
943807
+					free(gss_host);
943807
+					gss_host = xstrdup(host);
943807
+				}
943807
+			} else {
943807
 				gss_host = xstrdup(host);
943807
 			}
943807
-		} else {
943807
-			gss_host = xstrdup(host);
943807
-		}
943807
 
943807
-		gss = ssh_gssapi_client_mechanisms(gss_host,
943807
-		    options.gss_client_identity, options.gss_kex_algorithms);
943807
-		if (gss) {
943807
-			debug("Offering GSSAPI proposal: %s", gss);
943807
-			xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
943807
-			    "%s,%s", gss, orig);
943807
-
943807
-			/* If we've got GSSAPI algorithms, then we also support the
943807
-			 * 'null' hostkey, as a last resort */
943807
-			orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
943807
-			xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
943807
-			    "%s,null", orig);
943807
+			gss = ssh_gssapi_client_mechanisms(gss_host,
943807
+			    options.gss_client_identity, options.gss_kex_algorithms);
943807
+			if (gss) {
943807
+				debug("Offering GSSAPI proposal: %s", gss);
943807
+				xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
943807
+				    "%s,%s", gss, orig);
943807
+
943807
+				/* If we've got GSSAPI algorithms, then we also support the
943807
+				 * 'null' hostkey, as a last resort */
943807
+				orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
943807
+				xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
943807
+				    "%s,null", orig);
943807
+			}
943807
 		}
943807
 	}
943807
 #endif
943807
diff -up openssh-8.6p1/sshd.c.fips openssh-8.6p1/sshd.c
943807
--- openssh-8.6p1/sshd.c.fips	2021-05-06 12:08:36.493926838 +0200
943807
+++ openssh-8.6p1/sshd.c	2021-05-06 12:13:56.501492639 +0200
943807
@@ -66,6 +66,7 @@
943807
 #include <grp.h>
943807
 #include <pwd.h>
943807
 #include <signal.h>
943807
+#include <syslog.h>
943807
 #include <stdarg.h>
943807
 #include <stdio.h>
943807
 #include <stdlib.h>
943807
@@ -77,6 +78,7 @@
943807
 #include <openssl/dh.h>
943807
 #include <openssl/bn.h>
943807
 #include <openssl/rand.h>
2b784c
+#include <openssl/fips.h>
943807
 #include "openbsd-compat/openssl-compat.h"
943807
 #endif
943807
 
943807
@@ -1619,6 +1621,7 @@ main(int ac, char **av)
943807
 #endif
943807
 	__progname = ssh_get_progname(av[0]);
943807
 
943807
+	OpenSSL_add_all_algorithms();
943807
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
943807
 	saved_argc = ac;
943807
 	rexec_argc = ac;
39b801
@@ -1931,6 +1931,13 @@ main(int ac, char **av)
39b801
 		    &key, NULL)) != 0 && r != SSH_ERR_SYSTEM_ERROR)
39b801
 			do_log2_r(r, ll, "Unable to load host key \"%s\"",
39b801
 			    options.host_key_files[i]);
39b801
+		if (FIPS_mode() && key != NULL && (sshkey_type_plain(key->type) == KEY_ED25519_SK
39b801
+				||  sshkey_type_plain(key->type) == KEY_ED25519)) {
39b801
+		    logit_f("sshd: Ed25519 keys are not allowed in FIPS mode, skipping %s", options.host_key_files[i]);
39b801
+		    sshkey_free(key);
39b801
+		    key = NULL;
39b801
+		    continue;
39b801
+		}
39b801
 		if (sshkey_is_sk(key) &&
39b801
 		    key->sk_flags & SSH_SK_USER_PRESENCE_REQD) {
39b801
 			debug("host key %s requires user presence, ignoring",
943807
@@ -2110,6 +2113,10 @@ main(int ac, char **av)
943807
 	/* Reinitialize the log (because of the fork above). */
943807
 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
943807
 
943807
+	if (FIPS_mode()) {
943807
+		debug("FIPS mode initialized");
943807
+	}
943807
+
943807
 	/*
943807
 	 * Chdir to the root directory so that the current disk can be
943807
 	 * unmounted if desired.
943807
@@ -2494,10 +2501,14 @@ do_ssh2_kex(struct ssh *ssh)
943807
 	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
943807
 		orig = NULL;
943807
 
943807
-	if (options.gss_keyex)
943807
-		gss = ssh_gssapi_server_mechanisms();
943807
-	else
943807
-		gss = NULL;
943807
+	if (options.gss_keyex) {
943807
+		if (FIPS_mode()) {
943807
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
943807
+			options.gss_keyex = 0;
943807
+		} else {
943807
+			gss = ssh_gssapi_server_mechanisms();
943807
+		}
943807
+	}
943807
 
943807
 	if (gss && orig)
943807
 		xasprintf(&newstr, "%s,%s", gss, orig);
943807
diff -up openssh-8.6p1/sshkey.c.fips openssh-8.6p1/sshkey.c
943807
--- openssh-8.6p1/sshkey.c.fips	2021-05-06 12:08:36.493926838 +0200
943807
+++ openssh-8.6p1/sshkey.c	2021-05-06 12:08:36.502926908 +0200
943807
@@ -34,6 +34,7 @@
943807
 #include <openssl/evp.h>
943807
 #include <openssl/err.h>
943807
 #include <openssl/pem.h>
2b784c
+#include <openssl/fips.h>
943807
 #endif
943807
 
943807
 #include "crypto_api.h"
943807
@@ -57,6 +58,7 @@
943807
 #define SSHKEY_INTERNAL
943807
 #include "sshkey.h"
943807
 #include "match.h"
943807
+#include "log.h"
943807
 #include "ssh-sk.h"
943807
 
943807
 #ifdef WITH_XMSS
39b801
@@ -285,6 +285,18 @@ sshkey_alg_list(int certs_only, int plai
39b801
 	for (kt = keytypes; kt->type != -1; kt++) {
39b801
 		if (kt->name == NULL || kt->type == KEY_NULL)
39b801
 			continue;
39b801
+		if (FIPS_mode()) {
39b801
+			switch (kt->type) {
39b801
+			case KEY_ED25519:
39b801
+			case KEY_ED25519_SK:
39b801
+			case KEY_ED25519_CERT:
39b801
+			case KEY_ED25519_SK_CERT:
39b801
+			     continue;
39b801
+			     break;
39b801
+			default:
39b801
+			     break;
39b801
+			}
39b801
+		}
39b801
 		if (!include_sigonly && kt->sigonly)
39b801
 			continue;
39b801
 		if ((certs_only && !kt->cert) || (plain_only && kt->cert))
39b801
@@ -1503,6 +1503,20 @@ sshkey_read(struct sshkey *ret, char **c
39b801
 		return SSH_ERR_EC_CURVE_MISMATCH;
943807
 	}
39b801
 
39b801
+	switch (type) {
39b801
+	case KEY_ED25519:
39b801
+	case KEY_ED25519_SK:
39b801
+	case KEY_ED25519_CERT:
39b801
+	case KEY_ED25519_SK_CERT:
39b801
+		if (FIPS_mode()) {
39b801
+		    sshkey_free(k);
39b801
+		    logit_f("Ed25519 keys are not allowed in FIPS mode");
39b801
+		    return SSH_ERR_INVALID_ARGUMENT;
39b801
+		}
39b801
+		break;
39b801
+	default:
39b801
+		break;
39b801
+	}
39b801
 	/* Fill in ret from parsed key */
39b801
 	ret->type = type;
39b801
 	if (sshkey_is_cert(ret)) {
39b801
@@ -1705,6 +1707,8 @@ rsa_generate_private_key(u_int bits, RSA
39b801
		goto out;
39b801
39b801
	if (EVP_PKEY_keygen(ctx, &res) <= 0) {
39b801
+		if (FIPS_mode())
39b801
+			logit_f("the key length might be unsupported by FIPS mode approved key generation method");
943807
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
943807
 		goto out;
943807
 	}
39b801
@@ -2916,6 +2916,11 @@ sshkey_sign(struct sshkey *key,
39b801
 		break;
39b801
 	case KEY_ED25519_SK:
39b801
 	case KEY_ED25519_SK_CERT:
39b801
+		if (FIPS_mode()) {
39b801
+		    logit_f("Ed25519 keys are not allowed in FIPS mode");
39b801
+		    return SSH_ERR_INVALID_ARGUMENT;
39b801
+		}
39b801
+		/* Fallthrough */
39b801
 	case KEY_ECDSA_SK_CERT:
39b801
 	case KEY_ECDSA_SK:
39b801
 		r = sshsk_sign(sk_provider, key, sigp, lenp, data,
39b801
@@ -2973,6 +2978,10 @@ sshkey_verify(const struct sshkey *key,
39b801
 		return ssh_ed25519_verify(key, sig, siglen, data, dlen, compat);
39b801
 	case KEY_ED25519_SK:
39b801
 	case KEY_ED25519_SK_CERT:
39b801
+		if (FIPS_mode()) {
39b801
+		    logit_f("Ed25519 keys are not allowed in FIPS mode");
39b801
+		    return SSH_ERR_INVALID_ARGUMENT;
39b801
+		}
39b801
 		return ssh_ed25519_sk_verify(key, sig, siglen, data, dlen,
39b801
 		    compat, detailsp);
39b801
 #ifdef WITH_XMSS
943807
diff -up openssh-8.6p1/ssh-keygen.c.fips openssh-8.6p1/ssh-keygen.c
943807
--- openssh-8.6p1/ssh-keygen.c.fips	2021-05-06 12:08:36.467926637 +0200
943807
+++ openssh-8.6p1/ssh-keygen.c	2021-05-06 12:08:36.503926916 +0200
2b784c
@@ -20,6 +20,7 @@
2b784c
 
2b784c
 #ifdef WITH_OPENSSL
2b784c
 #include <openssl/evp.h>
2b784c
+#include <openssl/fips.h>
2b784c
 #include <openssl/pem.h>
2b784c
 #include "openbsd-compat/openssl-compat.h"
2b784c
 #endif
943807
@@ -205,6 +205,12 @@ type_bits_valid(int type, const char *na
943807
 #endif
943807
 	}
943807
 #ifdef WITH_OPENSSL
943807
+	if (FIPS_mode()) {
943807
+		if (type == KEY_DSA)
943807
+			fatal("DSA keys are not allowed in FIPS mode");
39b801
+		if (type == KEY_ED25519 || type == KEY_ED25519_SK)
943807
+			fatal("ED25519 keys are not allowed in FIPS mode");
943807
+	}
943807
 	switch (type) {
943807
 	case KEY_DSA:
943807
 		if (*bitsp != 1024)
943807
@@ -1098,9 +1104,17 @@ do_gen_all_hostkeys(struct passwd *pw)
943807
 			first = 1;
943807
 			printf("%s: generating new host keys: ", __progname);
943807
 		}
943807
+		type = sshkey_type_from_name(key_types[i].key_type);
943807
+
943807
+		/* Skip the keys that are not supported in FIPS mode */
943807
+		if (FIPS_mode() && (type == KEY_DSA || type == KEY_ED25519)) {
943807
+			logit("Skipping %s key in FIPS mode",
943807
+			    key_types[i].key_type_display);
943807
+			goto next;
943807
+		}
943807
+
943807
 		printf("%s ", key_types[i].key_type_display);
943807
 		fflush(stdout);
943807
-		type = sshkey_type_from_name(key_types[i].key_type);
943807
 		if ((fd = mkstemp(prv_tmp)) == -1) {
943807
 			error("Could not save your private key in %s: %s",
943807
 			    prv_tmp, strerror(errno));
39b801
diff -up openssh-8.7p1/kexgen.c.fips3 openssh-8.7p1/kexgen.c
39b801
--- openssh-8.7p1/kexgen.c.fips3	2022-07-11 16:11:21.973519913 +0200
39b801
+++ openssh-8.7p1/kexgen.c	2022-07-11 16:25:31.172187365 +0200
39b801
@@ -31,6 +31,7 @@
39b801
 #include <stdio.h>
39b801
 #include <string.h>
39b801
 #include <signal.h>
2b784c
+#include <openssl/fips.h>
39b801
 
39b801
 #include "sshkey.h"
39b801
 #include "kex.h"
39b801
@@ -115,10 +116,20 @@ kex_gen_client(struct ssh *ssh)
39b801
 		break;
39b801
 #endif
39b801
 	case KEX_C25519_SHA256:
39b801
-		r = kex_c25519_keypair(kex);
39b801
+		if (FIPS_mode()) {
39b801
+		    logit_f("Key exchange type c25519 is not allowed in FIPS mode");
39b801
+		    r = SSH_ERR_INVALID_ARGUMENT;
39b801
+		} else {
39b801
+		    r = kex_c25519_keypair(kex);
39b801
+		}
39b801
 		break;
39b801
 	case KEX_KEM_SNTRUP761X25519_SHA512:
39b801
-		r = kex_kem_sntrup761x25519_keypair(kex);
39b801
+		if (FIPS_mode()) {
39b801
+		    logit_f("Key exchange type sntrup761 is not allowed in FIPS mode");
39b801
+		    r = SSH_ERR_INVALID_ARGUMENT;
39b801
+		} else {
39b801
+		    r = kex_kem_sntrup761x25519_keypair(kex);
39b801
+		}
39b801
 		break;
39b801
 	default:
39b801
 		r = SSH_ERR_INVALID_ARGUMENT;
39b801
@@ -186,11 +197,21 @@ input_kex_gen_reply(int type, u_int32_t
39b801
 		break;
39b801
 #endif
39b801
 	case KEX_C25519_SHA256:
39b801
-		r = kex_c25519_dec(kex, server_blob, &shared_secret);
39b801
+		if (FIPS_mode()) {
39b801
+		    logit_f("Key exchange type c25519 is not allowed in FIPS mode");
39b801
+		    r = SSH_ERR_INVALID_ARGUMENT;
39b801
+		} else {
39b801
+		    r = kex_c25519_dec(kex, server_blob, &shared_secret);
39b801
+		}
39b801
 		break;
39b801
 	case KEX_KEM_SNTRUP761X25519_SHA512:
39b801
-		r = kex_kem_sntrup761x25519_dec(kex, server_blob,
39b801
-		    &shared_secret);
39b801
+		if (FIPS_mode()) {
39b801
+		    logit_f("Key exchange type sntrup761 is not allowed in FIPS mode");
39b801
+		    r = SSH_ERR_INVALID_ARGUMENT;
39b801
+		} else {
39b801
+		    r = kex_kem_sntrup761x25519_dec(kex, server_blob,
39b801
+		        &shared_secret);
39b801
+		}
39b801
 		break;
39b801
 	default:
39b801
 		r = SSH_ERR_INVALID_ARGUMENT;
39b801
@@ -285,12 +306,22 @@ input_kex_gen_init(int type, u_int32_t s
39b801
 		break;
39b801
 #endif
39b801
 	case KEX_C25519_SHA256:
39b801
-		r = kex_c25519_enc(kex, client_pubkey, &server_pubkey,
39b801
-		    &shared_secret);
39b801
+		if (FIPS_mode()) {
39b801
+		    logit_f("Key exchange type c25519 is not allowed in FIPS mode");
39b801
+		    r = SSH_ERR_INVALID_ARGUMENT;
39b801
+		} else {
39b801
+		    r = kex_c25519_enc(kex, client_pubkey, &server_pubkey,
39b801
+		        &shared_secret);
39b801
+		}
39b801
 		break;
39b801
 	case KEX_KEM_SNTRUP761X25519_SHA512:
39b801
-		r = kex_kem_sntrup761x25519_enc(kex, client_pubkey,
39b801
-		    &server_pubkey, &shared_secret);
39b801
+		if (FIPS_mode()) {
39b801
+		    logit_f("Key exchange type sntrup761 is not allowed in FIPS mode");
39b801
+		    r = SSH_ERR_INVALID_ARGUMENT;
39b801
+		} else {
39b801
+		    r = kex_kem_sntrup761x25519_enc(kex, client_pubkey,
39b801
+		        &server_pubkey, &shared_secret);
39b801
+		}
39b801
 		break;
39b801
 	default:
39b801
 		r = SSH_ERR_INVALID_ARGUMENT;
39b801
diff -up openssh-8.7p1/ssh-ed25519.c.fips3 openssh-8.7p1/ssh-ed25519.c
39b801
--- openssh-8.7p1/ssh-ed25519.c.fips3	2022-07-11 16:53:41.428343304 +0200
39b801
+++ openssh-8.7p1/ssh-ed25519.c	2022-07-11 16:56:09.284663661 +0200
39b801
@@ -24,6 +24,7 @@
39b801
 
39b801
 #include <string.h>
39b801
 #include <stdarg.h>
2b784c
+#include <openssl/fips.h>
39b801
 
39b801
 #include "log.h"
39b801
 #include "sshbuf.h"
39b801
@@ -52,6 +53,10 @@ ssh_ed25519_sign(const struct sshkey *ke
39b801
 	    key->ed25519_sk == NULL ||
39b801
 	    datalen >= INT_MAX - crypto_sign_ed25519_BYTES)
39b801
 		return SSH_ERR_INVALID_ARGUMENT;
39b801
+	if (FIPS_mode()) {
39b801
+	    logit_f("Ed25519 keys are not allowed in FIPS mode");
39b801
+	    return SSH_ERR_INVALID_ARGUMENT;
39b801
+	}
39b801
 	smlen = slen = datalen + crypto_sign_ed25519_BYTES;
39b801
 	if ((sig = malloc(slen)) == NULL)
39b801
 		return SSH_ERR_ALLOC_FAIL;
39b801
@@ -108,6 +113,10 @@ ssh_ed25519_verify(const struct sshkey *
39b801
 	    datalen >= INT_MAX - crypto_sign_ed25519_BYTES ||
39b801
 	    signature == NULL || signaturelen == 0)
39b801
 		return SSH_ERR_INVALID_ARGUMENT;
39b801
+	if (FIPS_mode()) {
39b801
+	    logit_f("Ed25519 keys are not allowed in FIPS mode");
39b801
+	    return SSH_ERR_INVALID_ARGUMENT;
39b801
+	}
39b801
 
39b801
 	if ((b = sshbuf_from(signature, signaturelen)) == NULL)
39b801
 		return SSH_ERR_ALLOC_FAIL;