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