d19ba9
diff -up openssh-8.0p1/cipher-ctr.c.fips openssh-8.0p1/cipher-ctr.c
d19ba9
--- openssh-8.0p1/cipher-ctr.c.fips	2019-07-23 14:55:45.326525641 +0200
d19ba9
+++ openssh-8.0p1/cipher-ctr.c	2019-07-23 14:55:45.401526401 +0200
535d34
@@ -179,7 +179,8 @@ evp_aes_128_ctr(void)
535d34
 	aes_ctr.do_cipher = ssh_aes_ctr;
535d34
 #ifndef SSH_OLD_EVP
535d34
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
535d34
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
535d34
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
535d34
+	    EVP_CIPH_FLAG_FIPS;
535d34
 #endif
535d34
 	return (&aes_ctr);
535d34
 }
d19ba9
diff -up openssh-8.0p1/dh.c.fips openssh-8.0p1/dh.c
d19ba9
--- openssh-8.0p1/dh.c.fips	2019-04-18 00:52:57.000000000 +0200
d19ba9
+++ openssh-8.0p1/dh.c	2019-07-23 14:55:45.401526401 +0200
81a703
@@ -152,6 +152,12 @@ choose_dh(int min, int wantbits, int max
81a703
 	int best, bestcount, which, linenum;
81a703
 	struct dhgroup dhg;
81a703
 
81a703
+	if (FIPS_mode()) {
81a703
+		logit("Using arbitrary primes is not allowed in FIPS mode."
81a703
+		    " Falling back to known groups.");
81a703
+		return (dh_new_group_fallback(max));
81a703
+	}
81a703
+
81a703
 	if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL) {
81a703
 		logit("WARNING: could not open %s (%s), using fixed modulus",
81a703
 		    _PATH_DH_MODULI, strerror(errno));
81a703
@@ -489,4 +495,38 @@ dh_estimate(int bits)
81a703
 	return 8192;
81a703
 }
81a703
 
81a703
+/*
81a703
+ * Compares the received DH parameters with known-good groups,
81a703
+ * which might be either from group14, group16 or group18.
81a703
+ */
81a703
+int
81a703
+dh_is_known_group(const DH *dh)
81a703
+{
81a703
+	const BIGNUM *p, *g;
81a703
+	const BIGNUM *known_p, *known_g;
81a703
+	DH *known = NULL;
81a703
+	int bits = 0, rv = 0;
81a703
+
81a703
+	DH_get0_pqg(dh, &p, NULL, &g);
81a703
+	bits = BN_num_bits(p);
81a703
+
81a703
+	if (bits <= 3072) {
81a703
+		known = dh_new_group14();
81a703
+	} else if (bits <= 6144) {
81a703
+		known = dh_new_group16();
81a703
+	} else {
81a703
+		known = dh_new_group18();
81a703
+	}
81a703
+
81a703
+	DH_get0_pqg(known, &known_p, NULL, &known_g);
81a703
+
81a703
+	if (BN_cmp(g, known_g) == 0 &&
81a703
+	    BN_cmp(p, known_p) == 0) {
81a703
+		rv = 1;
81a703
+	}
81a703
+
81a703
+	DH_free(known);
81a703
+	return rv;
81a703
+}
81a703
+
81a703
 #endif /* WITH_OPENSSL */
d19ba9
diff -up openssh-8.0p1/dh.h.fips openssh-8.0p1/dh.h
d19ba9
--- openssh-8.0p1/dh.h.fips	2019-04-18 00:52:57.000000000 +0200
d19ba9
+++ openssh-8.0p1/dh.h	2019-07-23 14:55:45.401526401 +0200
81a703
@@ -43,6 +43,7 @@ DH	*dh_new_group_fallback(int);
81a703
 
81a703
 int	 dh_gen_key(DH *, int);
81a703
 int	 dh_pub_is_valid(const DH *, const BIGNUM *);
81a703
+int	 dh_is_known_group(const DH *);
81a703
 
81a703
 u_int	 dh_estimate(int);
81a703
 
d19ba9
diff -up openssh-8.0p1/kex.c.fips openssh-8.0p1/kex.c
d19ba9
--- openssh-8.0p1/kex.c.fips	2019-07-23 14:55:45.395526340 +0200
d19ba9
+++ openssh-8.0p1/kex.c	2019-07-23 14:55:45.402526411 +0200
d19ba9
@@ -199,7 +199,10 @@ kex_names_valid(const char *names)
802815
 	for ((p = strsep(&cp, ",")); p && *p != '\0';
802815
 	    (p = strsep(&cp, ","))) {
802815
 		if (kex_alg_by_name(p) == NULL) {
802815
-			error("Unsupported KEX algorithm \"%.100s\"", p);
802815
+			if (FIPS_mode())
802815
+				error("\"%.100s\" is not allowed in FIPS mode", p);
802815
+			else
802815
+				error("Unsupported KEX algorithm \"%.100s\"", p);
802815
 			free(s);
802815
 			return 0;
802815
 		}
d19ba9
diff -up openssh-8.0p1/kexgexc.c.fips openssh-8.0p1/kexgexc.c
d19ba9
--- openssh-8.0p1/kexgexc.c.fips	2019-04-18 00:52:57.000000000 +0200
d19ba9
+++ openssh-8.0p1/kexgexc.c	2019-07-23 14:55:45.402526411 +0200
535d34
@@ -28,6 +28,7 @@
802815
 
132f8f
 #ifdef WITH_OPENSSL
802815
 
cb3595
+#include <openssl/crypto.h>
802815
 #include <sys/types.h>
802815
 
6cf9b8
 #include <openssl/dh.h>
d19ba9
@@ -113,6 +114,10 @@ input_kex_dh_gex_group(int type, u_int32
81a703
 		r = SSH_ERR_ALLOC_FAIL;
81a703
 		goto out;
81a703
 	}
81a703
+	if (FIPS_mode() && dh_is_known_group(kex->dh) == 0) {
81a703
+		r = SSH_ERR_INVALID_ARGUMENT;
81a703
+		goto out;
81a703
+	}
81a703
 	p = g = NULL; /* belong to kex->dh now */
81a703
 
81a703
 	/* generate and send 'e', client DH public key */
d19ba9
diff -up openssh-8.0p1/Makefile.in.fips openssh-8.0p1/Makefile.in
d19ba9
--- openssh-8.0p1/Makefile.in.fips	2019-07-23 14:55:45.396526350 +0200
d19ba9
+++ openssh-8.0p1/Makefile.in	2019-07-23 14:55:45.402526411 +0200
d19ba9
@@ -180,25 +180,25 @@ libssh.a: $(LIBSSH_OBJS)
535d34
 	$(RANLIB) $@
535d34
 
535d34
 ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
535d34
-	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS) $(GSSLIBS)
535d34
+	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHLIBS) $(LIBS) $(GSSLIBS)
535d34
 
535d34
 sshd$(EXEEXT): libssh.a	$(LIBCOMPAT) $(SSHDOBJS)
535d34
-	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
535d34
+	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
535d34
 
51f5c1
 scp$(EXEEXT): $(LIBCOMPAT) libssh.a $(SCP_OBJS)
51f5c1
 	$(LD) -o $@ $(SCP_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d34
 
51f5c1
 ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHADD_OBJS)
51f5c1
-	$(LD) -o $@ $(SSHADD_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
51f5c1
+	$(LD) -o $@ $(SSHADD_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d34
 
51f5c1
 ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHAGENT_OBJS)
51f5c1
-	$(LD) -o $@ $(SSHAGENT_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
51f5c1
+	$(LD) -o $@ $(SSHAGENT_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d34
 
51f5c1
 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHKEYGEN_OBJS)
51f5c1
-	$(LD) -o $@ $(SSHKEYGEN_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
51f5c1
+	$(LD) -o $@ $(SSHKEYGEN_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d34
 
51f5c1
 ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHKEYSIGN_OBJS)
51f5c1
-	$(LD) -o $@ $(SSHKEYSIGN_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
51f5c1
+	$(LD) -o $@ $(SSHKEYSIGN_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d34
 
51f5c1
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a $(P11HELPER_OBJS)
51f5c1
 	$(LD) -o $@ $(P11HELPER_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
d19ba9
@@ -216,7 +216,7 @@ ssh-cavs$(EXEEXT): $(LIBCOMPAT) libssh.a
51f5c1
 	$(LD) -o $@ ssh-cavs.o $(SKOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d34
 
51f5c1
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHKEYSCAN_OBJS)
51f5c1
-	$(LD) -o $@ $(SSHKEYSCAN_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
51f5c1
+	$(LD) -o $@ $(SSHKEYSCAN_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
535d34
 
51f5c1
 sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a $(SFTPSERVER_OBJS)
51f5c1
 	$(LD) -o $@ $(SFTPSERVER_OBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
d19ba9
diff -up openssh-8.0p1/myproposal.h.fips openssh-8.0p1/myproposal.h
d19ba9
--- openssh-8.0p1/myproposal.h.fips	2019-04-18 00:52:57.000000000 +0200
d19ba9
+++ openssh-8.0p1/myproposal.h	2019-07-23 14:55:45.402526411 +0200
51f5c1
@@ -111,6 +111,20 @@
9dbec7
 	"rsa-sha2-256," \
9dbec7
 	"ssh-rsa"
9dbec7
 
9dbec7
+#define	KEX_FIPS_PK_ALG	\
51f5c1
+	"ecdsa-sha2-nistp256-cert-v01@openssh.com," \
51f5c1
+	"ecdsa-sha2-nistp384-cert-v01@openssh.com," \
51f5c1
+	"ecdsa-sha2-nistp521-cert-v01@openssh.com," \
5eb2d5
+	"rsa-sha2-512-cert-v01@openssh.com," \
5eb2d5
+	"rsa-sha2-256-cert-v01@openssh.com," \
9dbec7
+	"ssh-rsa-cert-v01@openssh.com," \
51f5c1
+	"ecdsa-sha2-nistp256," \
51f5c1
+	"ecdsa-sha2-nistp384," \
51f5c1
+	"ecdsa-sha2-nistp521," \
9dbec7
+	"rsa-sha2-512," \
9dbec7
+	"rsa-sha2-256," \
9dbec7
+	"ssh-rsa"
9dbec7
+
51f5c1
 #define	KEX_SERVER_ENCRYPT \
51f5c1
 	"chacha20-poly1305@openssh.com," \
51f5c1
 	"aes128-ctr,aes192-ctr,aes256-ctr," \
51f5c1
@@ -134,6 +142,27 @@
13bf5b
 
13bf5b
 #define KEX_CLIENT_MAC KEX_SERVER_MAC
802815
 
802815
+#define	KEX_FIPS_ENCRYPT \
802815
+	"aes128-ctr,aes192-ctr,aes256-ctr," \
802815
+	"aes128-cbc,3des-cbc," \
51f5c1
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se," \
51f5c1
+	"aes128-gcm@openssh.com,aes256-gcm@openssh.com"
51f5c1
+#define KEX_DEFAULT_KEX_FIPS		\
51f5c1
+	"ecdh-sha2-nistp256," \
51f5c1
+	"ecdh-sha2-nistp384," \
51f5c1
+	"ecdh-sha2-nistp521," \
51f5c1
+	"diffie-hellman-group-exchange-sha256," \
51f5c1
+	"diffie-hellman-group16-sha512," \
51f5c1
+	"diffie-hellman-group18-sha512," \
eb751f
+	"diffie-hellman-group14-sha256"
51f5c1
+#define KEX_FIPS_MAC \
802815
+	"hmac-sha1," \
802815
+	"hmac-sha2-256," \
802815
+	"hmac-sha2-512," \
802815
+	"hmac-sha1-etm@openssh.com," \
802815
+	"hmac-sha2-256-etm@openssh.com," \
802815
+	"hmac-sha2-512-etm@openssh.com"
190035
+
eaa7af
 /* Not a KEX value, but here so all the algorithm defaults are together */
eaa7af
 #define	SSH_ALLOWED_CA_SIGALGS	\
51f5c1
 	"ecdsa-sha2-nistp256," \
d19ba9
diff -up openssh-8.0p1/readconf.c.fips openssh-8.0p1/readconf.c
d19ba9
--- openssh-8.0p1/readconf.c.fips	2019-07-23 14:55:45.334525723 +0200
d19ba9
+++ openssh-8.0p1/readconf.c	2019-07-23 14:55:45.402526411 +0200
51f5c1
@@ -2179,11 +2179,16 @@ fill_default_options(Options * options)
bbf61d
 	all_key = sshkey_alg_list(0, 0, 1, ',');
eaa7af
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
51f5c1
 	/* remove unsupported algos from default lists */
51f5c1
-	def_cipher = match_filter_whitelist(KEX_CLIENT_ENCRYPT, all_cipher);
51f5c1
-	def_mac = match_filter_whitelist(KEX_CLIENT_MAC, all_mac);
51f5c1
-	def_kex = match_filter_whitelist(KEX_CLIENT_KEX, all_kex);
51f5c1
-	def_key = match_filter_whitelist(KEX_DEFAULT_PK_ALG, all_key);
51f5c1
-	def_sig = match_filter_whitelist(SSH_ALLOWED_CA_SIGALGS, all_sig);
51f5c1
+	def_cipher = match_filter_whitelist((FIPS_mode() ?
51f5c1
+	    KEX_FIPS_ENCRYPT : KEX_CLIENT_ENCRYPT), all_cipher);
51f5c1
+	def_mac = match_filter_whitelist((FIPS_mode() ?
51f5c1
+	    KEX_FIPS_MAC : KEX_CLIENT_MAC), all_mac);
51f5c1
+	def_kex = match_filter_whitelist((FIPS_mode() ?
51f5c1
+	    KEX_DEFAULT_KEX_FIPS : KEX_CLIENT_KEX), all_kex);
51f5c1
+	def_key = match_filter_whitelist((FIPS_mode() ?
51f5c1
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG), all_key);
51f5c1
+	def_sig = match_filter_whitelist((FIPS_mode() ?
51f5c1
+	    KEX_FIPS_PK_ALG : SSH_ALLOWED_CA_SIGALGS), all_sig);
51f5c1
 #define ASSEMBLE(what, defaults, all) \
bbf61d
 	do { \
bbf61d
 		if ((r = kex_assemble_names(&options->what, \
d19ba9
diff -up openssh-8.0p1/sandbox-seccomp-filter.c.fips openssh-8.0p1/sandbox-seccomp-filter.c
d19ba9
--- openssh-8.0p1/sandbox-seccomp-filter.c.fips	2019-07-23 14:55:45.373526117 +0200
d19ba9
+++ openssh-8.0p1/sandbox-seccomp-filter.c	2019-07-23 14:55:45.402526411 +0200
9dbec7
@@ -137,6 +137,9 @@ static const struct sock_filter preauth_
6cf9b8
 #ifdef __NR_open
17b491
 	SC_DENY(__NR_open, EACCES),
6cf9b8
 #endif
6cf9b8
+#ifdef __NR_socket
17b491
+	SC_DENY(__NR_socket, EACCES),
6cf9b8
+#endif
6cf9b8
 #ifdef __NR_openat
17b491
 	SC_DENY(__NR_openat, EACCES),
6cf9b8
 #endif
d19ba9
diff -up openssh-8.0p1/servconf.c.fips openssh-8.0p1/servconf.c
d19ba9
--- openssh-8.0p1/servconf.c.fips	2019-07-23 14:55:45.361525996 +0200
d19ba9
+++ openssh-8.0p1/servconf.c	2019-07-23 14:55:45.403526421 +0200
51f5c1
@@ -208,11 +208,16 @@ assemble_algorithms(ServerOptions *o)
bbf61d
 	all_key = sshkey_alg_list(0, 0, 1, ',');
eaa7af
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
51f5c1
 	/* remove unsupported algos from default lists */
51f5c1
-	def_cipher = match_filter_whitelist(KEX_SERVER_ENCRYPT, all_cipher);
51f5c1
-	def_mac = match_filter_whitelist(KEX_SERVER_MAC, all_mac);
51f5c1
-	def_kex = match_filter_whitelist(KEX_SERVER_KEX, all_kex);
51f5c1
-	def_key = match_filter_whitelist(KEX_DEFAULT_PK_ALG, all_key);
51f5c1
-	def_sig = match_filter_whitelist(SSH_ALLOWED_CA_SIGALGS, all_sig);
51f5c1
+	def_cipher = match_filter_whitelist((FIPS_mode() ?
51f5c1
+	    KEX_FIPS_ENCRYPT : KEX_SERVER_ENCRYPT), all_cipher);
51f5c1
+	def_mac = match_filter_whitelist((FIPS_mode() ?
51f5c1
+	    KEX_FIPS_MAC : KEX_SERVER_MAC), all_mac);
51f5c1
+	def_kex = match_filter_whitelist((FIPS_mode() ?
51f5c1
+	    KEX_DEFAULT_KEX_FIPS : KEX_SERVER_KEX), all_kex);
51f5c1
+	def_key = match_filter_whitelist((FIPS_mode() ?
51f5c1
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG), all_key);
51f5c1
+	def_sig = match_filter_whitelist((FIPS_mode() ?
51f5c1
+	    KEX_FIPS_PK_ALG : SSH_ALLOWED_CA_SIGALGS), all_sig);
51f5c1
 #define ASSEMBLE(what, defaults, all) \
bbf61d
 	do { \
51f5c1
 		if ((r = kex_assemble_names(&o->what, defaults, all)) != 0) \
d19ba9
diff -up openssh-8.0p1/ssh.c.fips openssh-8.0p1/ssh.c
d19ba9
--- openssh-8.0p1/ssh.c.fips	2019-07-23 14:55:45.378526168 +0200
d19ba9
+++ openssh-8.0p1/ssh.c	2019-07-23 14:55:45.403526421 +0200
6cf9b8
@@ -76,6 +76,8 @@
802815
 #include <openssl/evp.h>
802815
 #include <openssl/err.h>
190035
 #endif
cb3595
+#include <openssl/crypto.h>
802815
+#include <fipscheck.h>
802815
 #include "openbsd-compat/openssl-compat.h"
802815
 #include "openbsd-compat/sys-queue.h"
802815
 
81a703
@@ -600,6 +602,16 @@ main(int ac, char **av)
802815
 	sanitise_stdfd();
802815
 
802815
 	__progname = ssh_get_progname(av[0]);
eaa7af
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
eaa7af
+	SSLeay_add_all_algorithms();
eaa7af
+#endif
802815
+	if (access("/etc/system-fips", F_OK) == 0)
580f98
+		if (! FIPSCHECK_verify(NULL, NULL)){
802815
+			if (FIPS_mode())
802815
+				fatal("FIPS integrity verification test failed.");
802815
+			else
802815
+				logit("FIPS integrity verification test failed.");
580f98
+	}
802815
 
802815
 #ifndef HAVE_SETPROCTITLE
802815
 	/* Prepare for later setproctitle emulation */
d19ba9
@@ -614,6 +626,10 @@ main(int ac, char **av)
802815
 
802815
 	seed_rng();
802815
 
802815
+	if (FIPS_mode()) {
ee9cb0
+		debug("FIPS mode initialized");
802815
+	}
802815
+
def1de
 	/*
def1de
 	 * Discard other fds that are hanging around. These can cause problem
def1de
 	 * with backgrounded ssh processes started by ControlPersist.
d19ba9
diff -up openssh-8.0p1/sshconnect2.c.fips openssh-8.0p1/sshconnect2.c
d19ba9
--- openssh-8.0p1/sshconnect2.c.fips	2019-07-23 14:55:45.336525743 +0200
d19ba9
+++ openssh-8.0p1/sshconnect2.c	2019-07-23 14:55:45.403526421 +0200
3f5513
@@ -44,6 +44,8 @@
802815
 #include <vis.h>
802815
 #endif
802815
 
cb3595
+#include <openssl/crypto.h>
802815
+
802815
 #include "openbsd-compat/sys-queue.h"
802815
 
802815
 #include "xmalloc.h"
d19ba9
@@ -198,29 +203,34 @@ ssh_kex2(struct ssh *ssh, char *host, st
802815
 
def1de
 #if defined(GSSAPI) && defined(WITH_OPENSSL)
802815
 	if (options.gss_keyex) {
def1de
-		/* Add the GSSAPI mechanisms currently supported on this
802815
-		 * client to the key exchange algorithm proposal */
def1de
-		orig = myproposal[PROPOSAL_KEX_ALGS];
132f8f
-
e8876f
-		if (options.gss_server_identity)
def1de
-			gss_host = xstrdup(options.gss_server_identity);
e8876f
-		else if (options.gss_trust_dns)
def1de
-			gss_host = remote_hostname(ssh);
132f8f
-		else
def1de
-			gss_host = xstrdup(host);
132f8f
-
bc4ef0
-		gss = ssh_gssapi_client_mechanisms(gss_host,
bc4ef0
-		    options.gss_client_identity, options.gss_kex_algorithms);
132f8f
-		if (gss) {
132f8f
-			debug("Offering GSSAPI proposal: %s", gss);
def1de
-			xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
132f8f
-			    "%s,%s", gss, orig);
def1de
-
def1de
-			/* If we've got GSSAPI algorithms, then we also support the
def1de
-			 * 'null' hostkey, as a last resort */
def1de
-			orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
def1de
-			xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
def1de
-			    "%s,null", orig);
802815
+		if (FIPS_mode()) {
802815
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
802815
+			options.gss_keyex = 0;
802815
+		} else {
802815
+			/* Add the GSSAPI mechanisms currently supported on this
802815
+			 * client to the key exchange algorithm proposal */
def1de
+			orig = myproposal[PROPOSAL_KEX_ALGS];
132f8f
+
e8876f
+			if (options.gss_server_identity)
def1de
+				gss_host = xstrdup(options.gss_server_identity);
e8876f
+			else if (options.gss_trust_dns)
def1de
+				gss_host = remote_hostname(ssh);
802815
+			else
def1de
+				gss_host = xstrdup(host);
132f8f
+
bc4ef0
+			gss = ssh_gssapi_client_mechanisms(gss_host,
bc4ef0
+			    options.gss_client_identity, options.gss_kex_algorithms);
802815
+			if (gss) {
802815
+				debug("Offering GSSAPI proposal: %s", gss);
def1de
+				xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
802815
+				    "%s,%s", gss, orig);
def1de
+
def1de
+				/* If we've got GSSAPI algorithms, then we also support the
def1de
+				 * 'null' hostkey, as a last resort */
def1de
+				orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
def1de
+				xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
def1de
+				    "%s,null", orig);
802815
+			}
802815
 		}
802815
 	}
802815
 #endif
d19ba9
diff -up openssh-8.0p1/sshd.c.fips openssh-8.0p1/sshd.c
d19ba9
--- openssh-8.0p1/sshd.c.fips	2019-07-23 14:55:45.398526371 +0200
d19ba9
+++ openssh-8.0p1/sshd.c	2019-07-23 14:55:45.403526421 +0200
190035
@@ -66,6 +66,7 @@
190035
 #include <grp.h>
190035
 #include <pwd.h>
190035
 #include <signal.h>
190035
+#include <syslog.h>
190035
 #include <stdarg.h>
190035
 #include <stdio.h>
190035
 #include <stdlib.h>
132f8f
@@ -77,6 +78,8 @@
802815
 #include <openssl/dh.h>
802815
 #include <openssl/bn.h>
802815
 #include <openssl/rand.h>
cb3595
+#include <openssl/crypto.h>
802815
+#include <fipscheck.h>
802815
 #include "openbsd-compat/openssl-compat.h"
190035
 #endif
802815
 
d19ba9
@@ -1529,6 +1532,18 @@ main(int ac, char **av)
802815
 #endif
802815
 	__progname = ssh_get_progname(av[0]);
802815
 
eaa7af
+	OpenSSL_add_all_algorithms();
802815
+	if (access("/etc/system-fips", F_OK) == 0)
802815
+		if (! FIPSCHECK_verify(NULL, NULL)) {
802815
+			openlog(__progname, LOG_PID, LOG_AUTHPRIV);
802815
+			if (FIPS_mode()) {
802815
+				syslog(LOG_CRIT, "FIPS integrity verification test failed.");
802815
+				cleanup_exit(255);
802815
+			}
802815
+			else
802815
+				syslog(LOG_INFO, "FIPS integrity verification test failed.");
802815
+			closelog();
802815
+		}
802815
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
802815
 	saved_argc = ac;
802815
 	rexec_argc = ac;
d19ba9
@@ -1992,6 +2007,10 @@ main(int ac, char **av)
802815
 	/* Reinitialize the log (because of the fork above). */
802815
 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
802815
 
802815
+	if (FIPS_mode()) {
ee9cb0
+		debug("FIPS mode initialized");
802815
+	}
802815
+
802815
 	/* Chdir to the root directory so that the current disk can be
802815
 	   unmounted if desired. */
802815
 	if (chdir("/") == -1)
d19ba9
@@ -2382,10 +2401,14 @@ do_ssh2_kex(struct ssh *ssh)
802815
 	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
802815
 		orig = NULL;
802815
 
802815
-	if (options.gss_keyex)
802815
-		gss = ssh_gssapi_server_mechanisms();
802815
-	else
802815
-		gss = NULL;
802815
+	if (options.gss_keyex) {
802815
+		if (FIPS_mode()) {
802815
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
802815
+			options.gss_keyex = 0;
802815
+		} else {
802815
+			gss = ssh_gssapi_server_mechanisms();
802815
+		}
802815
+	}
802815
 
802815
 	if (gss && orig)
802815
 		xasprintf(&newstr, "%s,%s", gss, orig);
d19ba9
diff -up openssh-8.0p1/sshkey.c.fips openssh-8.0p1/sshkey.c
d19ba9
--- openssh-8.0p1/sshkey.c.fips	2019-07-23 14:55:45.398526371 +0200
d19ba9
+++ openssh-8.0p1/sshkey.c	2019-07-23 14:55:45.404526431 +0200
6cf9b8
@@ -34,6 +34,7 @@
190035
 #include <openssl/evp.h>
190035
 #include <openssl/err.h>
190035
 #include <openssl/pem.h>
cb3595
+#include <openssl/crypto.h>
132f8f
 #endif
190035
 
190035
 #include "crypto_api.h"
44e203
@@ -57,6 +58,7 @@
36fef5
 #define SSHKEY_INTERNAL
84d398
 #include "sshkey.h"
84d398
 #include "match.h"
9dbec7
+#include "log.h"
51f5c1
 #include "ssh-sk.h"
84d398
 
36fef5
 #ifdef WITH_XMSS
d19ba9
@@ -1591,6 +1593,8 @@ rsa_generate_private_key(u_int bits, RSA
190035
 	}
190035
 	if (!BN_set_word(f4, RSA_F4) ||
190035
 	    !RSA_generate_key_ex(private, bits, f4, NULL)) {
190035
+			if (FIPS_mode())
190035
+				logit("%s: the key length might be unsupported by FIPS mode approved key generation method", __func__);
190035
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
190035
 		goto out;
190035
 	}
d19ba9
diff -up openssh-8.0p1/ssh-keygen.c.fips openssh-8.0p1/ssh-keygen.c
d19ba9
--- openssh-8.0p1/ssh-keygen.c.fips	2019-07-23 14:55:45.391526300 +0200
d19ba9
+++ openssh-8.0p1/ssh-keygen.c	2019-07-23 14:57:54.118830056 +0200
d19ba9
@@ -199,6 +199,12 @@ type_bits_valid(int type, const char *na
36fef5
 #endif
36fef5
 	}
36fef5
 #ifdef WITH_OPENSSL
9dbec7
+	if (FIPS_mode()) {
9dbec7
+		if (type == KEY_DSA)
9dbec7
+			fatal("DSA keys are not allowed in FIPS mode");
9dbec7
+		if (type == KEY_ED25519)
9dbec7
+			fatal("ED25519 keys are not allowed in FIPS mode");
9dbec7
+	}
5b55d0
 	switch (type) {
5b55d0
 	case KEY_DSA:
5b55d0
 		if (*bitsp != 1024)
d19ba9
@@ -1029,9 +1035,17 @@ do_gen_all_hostkeys(struct passwd *pw)
d19ba9
 			first = 1;
d19ba9
 			printf("%s: generating new host keys: ", __progname);
d19ba9
 		}
d19ba9
+		type = sshkey_type_from_name(key_types[i].key_type);
d19ba9
+
d19ba9
+		/* Skip the keys that are not supported in FIPS mode */
d19ba9
+		if (FIPS_mode() && (type == KEY_DSA || type == KEY_ED25519)) {
d19ba9
+			logit("Skipping %s key in FIPS mode",
d19ba9
+			    key_types[i].key_type_display);
d19ba9
+			goto next;
d19ba9
+		}
d19ba9
+
d19ba9
 		printf("%s ", key_types[i].key_type_display);
d19ba9
 		fflush(stdout);
d19ba9
-		type = sshkey_type_from_name(key_types[i].key_type);
d19ba9
 		if ((fd = mkstemp(prv_tmp)) == -1) {
d19ba9
 			error("Could not save your public key in %s: %s",
d19ba9
 			    prv_tmp, strerror(errno));