4369a3
diff -up openssh-7.9p1/cipher-ctr.c.fips openssh-7.9p1/cipher-ctr.c
4369a3
--- openssh-7.9p1/cipher-ctr.c.fips	2019-03-11 17:06:37.519877082 +0100
4369a3
+++ openssh-7.9p1/cipher-ctr.c	2019-03-11 17:06:37.620878031 +0100
f5835d
@@ -179,7 +179,8 @@ evp_aes_128_ctr(void)
f5835d
 	aes_ctr.do_cipher = ssh_aes_ctr;
f5835d
 #ifndef SSH_OLD_EVP
f5835d
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
f5835d
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
f5835d
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
f5835d
+	    EVP_CIPH_FLAG_FIPS;
f5835d
 #endif
f5835d
 	return (&aes_ctr);
f5835d
 }
4369a3
diff -up openssh-7.9p1/clientloop.c.fips openssh-7.9p1/clientloop.c
4369a3
--- openssh-7.9p1/clientloop.c.fips	2019-03-11 17:06:37.523877120 +0100
4369a3
+++ openssh-7.9p1/clientloop.c	2019-03-11 17:06:37.620878031 +0100
4369a3
@@ -2014,7 +2014,8 @@ key_accepted_by_hostkeyalgs(const struct
f5835d
 {
f5835d
 	const char *ktype = sshkey_ssh_name(key);
f5835d
 	const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
f5835d
-	    options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
f5835d
+	    options.hostkeyalgorithms : (FIPS_mode() ?
f5835d
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG);
f5835d
 
f5835d
 	if (key == NULL || key->type == KEY_UNSPEC)
f5835d
 		return 0;
4369a3
diff -up openssh-7.9p1/dh.c.fips openssh-7.9p1/dh.c
4369a3
--- openssh-7.9p1/dh.c.fips	2018-10-17 02:01:20.000000000 +0200
4369a3
+++ openssh-7.9p1/dh.c	2019-03-11 17:08:11.769763057 +0100
4369a3
@@ -152,6 +152,12 @@ choose_dh(int min, int wantbits, int max
4369a3
 	int best, bestcount, which, linenum;
4369a3
 	struct dhgroup dhg;
f5835d
 
4369a3
+	if (FIPS_mode()) {
3a2fd7
+		verbose("Using arbitrary primes is not allowed in FIPS mode."
4369a3
+		    " Falling back to known groups.");
4369a3
+		return (dh_new_group_fallback(max));
4369a3
+	}
4369a3
+
4369a3
 	if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL) {
4369a3
 		logit("WARNING: could not open %s (%s), using fixed modulus",
4369a3
 		    _PATH_DH_MODULI, strerror(errno));
4369a3
@@ -489,4 +495,38 @@ dh_estimate(int bits)
4369a3
 	return 8192;
4369a3
 }
f5835d
 
4369a3
+/*
4369a3
+ * Compares the received DH parameters with known-good groups,
4369a3
+ * which might be either from group14, group16 or group18.
4369a3
+ */
4369a3
+int
4369a3
+dh_is_known_group(const DH *dh)
4369a3
+{
4369a3
+	const BIGNUM *p, *g;
4369a3
+	const BIGNUM *known_p, *known_g;
4369a3
+	DH *known = NULL;
4369a3
+	int bits = 0, rv = 0;
f5835d
+
4369a3
+	DH_get0_pqg(dh, &p, NULL, &g);
4369a3
+	bits = BN_num_bits(p);
4369a3
+
4369a3
+	if (bits <= 3072) {
4369a3
+		known = dh_new_group14();
4369a3
+	} else if (bits <= 6144) {
4369a3
+		known = dh_new_group16();
4369a3
+	} else {
4369a3
+		known = dh_new_group18();
4369a3
+	}
4369a3
+
4369a3
+	DH_get0_pqg(known, &known_p, NULL, &known_g);
4369a3
+
4369a3
+	if (BN_cmp(g, known_g) == 0 &&
4369a3
+	    BN_cmp(p, known_p) == 0) {
4369a3
+		rv = 1;
4369a3
+	}
4369a3
+
4369a3
+	DH_free(known);
4369a3
+	return rv;
4369a3
+}
4369a3
+
4369a3
 #endif /* WITH_OPENSSL */
4369a3
diff -up openssh-7.9p1/dh.h.fips openssh-7.9p1/dh.h
4369a3
--- openssh-7.9p1/dh.h.fips	2018-10-17 02:01:20.000000000 +0200
4369a3
+++ openssh-7.9p1/dh.h	2019-03-11 17:08:18.718828381 +0100
4369a3
@@ -43,6 +43,7 @@ DH	*dh_new_group_fallback(int);
4369a3
 
4369a3
 int	 dh_gen_key(DH *, int);
4369a3
 int	 dh_pub_is_valid(const DH *, const BIGNUM *);
4369a3
+int	 dh_is_known_group(const DH *);
f5835d
 
4369a3
 u_int	 dh_estimate(int);
4369a3
 
4369a3
diff -up openssh-7.9p1/kex.c.fips openssh-7.9p1/kex.c
4369a3
--- openssh-7.9p1/kex.c.fips	2019-03-11 17:06:37.614877975 +0100
4369a3
+++ openssh-7.9p1/kex.c	2019-03-11 17:06:37.621878041 +0100
f5835d
@@ -175,7 +196,10 @@ kex_names_valid(const char *names)
f5835d
 	for ((p = strsep(&cp, ",")); p && *p != '\0';
f5835d
 	    (p = strsep(&cp, ","))) {
f5835d
 		if (kex_alg_by_name(p) == NULL) {
f5835d
-			error("Unsupported KEX algorithm \"%.100s\"", p);
f5835d
+			if (FIPS_mode())
f5835d
+				error("\"%.100s\" is not allowed in FIPS mode", p);
f5835d
+			else
f5835d
+				error("Unsupported KEX algorithm \"%.100s\"", p);
f5835d
 			free(s);
f5835d
 			return 0;
f5835d
 		}
4369a3
diff -up openssh-7.9p1/kexgexc.c.fips openssh-7.9p1/kexgexc.c
4369a3
--- openssh-7.9p1/kexgexc.c.fips	2018-10-17 02:01:20.000000000 +0200
4369a3
+++ openssh-7.9p1/kexgexc.c	2019-03-11 17:06:37.621878041 +0100
f5835d
@@ -28,6 +28,7 @@
f5835d
 
f5835d
 #ifdef WITH_OPENSSL
f5835d
 
4369a3
+#include <openssl/crypto.h>
f5835d
 #include <sys/types.h>
f5835d
 
f5835d
 #include <openssl/dh.h>
4369a3
@@ -118,6 +119,10 @@ input_kex_dh_gex_group(int type, u_int32
4369a3
 		r = SSH_ERR_ALLOC_FAIL;
4369a3
 		goto out;
4369a3
 	}
4369a3
+	if (FIPS_mode() && dh_is_known_group(kex->dh) == 0) {
4369a3
+		r = SSH_ERR_INVALID_ARGUMENT;
4369a3
+		goto out;
4369a3
+	}
4369a3
 	p = g = NULL; /* belong to kex->dh now */
f5835d
 
4369a3
 	/* generate and send 'e', client DH public key */
4369a3
diff -up openssh-7.9p1/myproposal.h.fips openssh-7.9p1/myproposal.h
4369a3
--- openssh-7.9p1/myproposal.h.fips	2018-10-17 02:01:20.000000000 +0200
4369a3
+++ openssh-7.9p1/myproposal.h	2019-03-11 17:06:37.621878041 +0100
4369a3
@@ -116,6 +116,16 @@
f5835d
 	"rsa-sha2-256," \
f5835d
 	"ssh-rsa"
f5835d
 
f5835d
+#define	KEX_FIPS_PK_ALG	\
f5835d
+	HOSTKEY_ECDSA_CERT_METHODS \
4369a3
+	"rsa-sha2-512-cert-v01@openssh.com," \
4369a3
+	"rsa-sha2-256-cert-v01@openssh.com," \
f5835d
+	"ssh-rsa-cert-v01@openssh.com," \
f5835d
+	HOSTKEY_ECDSA_METHODS \
f5835d
+	"rsa-sha2-512," \
f5835d
+	"rsa-sha2-256," \
f5835d
+	"ssh-rsa"
f5835d
+
f5835d
 /* the actual algorithms */
f5835d
 
f5835d
 #define KEX_SERVER_ENCRYPT \
4369a3
@@ -139,6 +147,38 @@
f5835d
 
f5835d
 #define KEX_CLIENT_MAC KEX_SERVER_MAC
f5835d
 
f5835d
+#define	KEX_FIPS_ENCRYPT \
f5835d
+	"aes128-ctr,aes192-ctr,aes256-ctr," \
f5835d
+	"aes128-cbc,3des-cbc," \
f5835d
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se" \
f5835d
+	AESGCM_CIPHER_MODES
f5835d
+#ifdef HAVE_EVP_SHA256
f5835d
+# define KEX_DEFAULT_KEX_FIPS		\
f5835d
+	KEX_ECDH_METHODS \
f5835d
+	KEX_SHA2_METHODS \
f5835d
+	"diffie-hellman-group14-sha256"
f5835d
+# define KEX_FIPS_MAC \
f5835d
+	"hmac-sha1," \
f5835d
+	"hmac-sha2-256," \
f5835d
+	"hmac-sha2-512," \
f5835d
+	"hmac-sha1-etm@openssh.com," \
f5835d
+	"hmac-sha2-256-etm@openssh.com," \
f5835d
+	"hmac-sha2-512-etm@openssh.com"
f5835d
+#else
f5835d
+# ifdef OPENSSL_HAS_NISTP521
f5835d
+#  define KEX_DEFAULT_KEX_FIPS		\
f5835d
+	"ecdh-sha2-nistp256," \
f5835d
+	"ecdh-sha2-nistp384," \
f5835d
+	"ecdh-sha2-nistp521"
f5835d
+# else
f5835d
+#  define KEX_DEFAULT_KEX_FIPS		\
f5835d
+	"ecdh-sha2-nistp256," \
f5835d
+	"ecdh-sha2-nistp384"
f5835d
+# endif
f5835d
+#define        KEX_FIPS_MAC \
f5835d
+       "hmac-sha1"
f5835d
+#endif
f5835d
+
4369a3
 /* Not a KEX value, but here so all the algorithm defaults are together */
4369a3
 #define	SSH_ALLOWED_CA_SIGALGS	\
4369a3
 	"ecdsa-sha2-nistp256," \
4369a3
diff -up openssh-7.9p1/readconf.c.fips openssh-7.9p1/readconf.c
4369a3
--- openssh-7.9p1/readconf.c.fips	2019-03-11 17:06:37.601877853 +0100
4369a3
+++ openssh-7.9p1/readconf.c	2019-03-11 17:06:37.622878050 +0100
4369a3
@@ -2178,18 +2178,19 @@ fill_default_options(Options * options)
f5835d
 	all_kex = kex_alg_list(',');
f5835d
 	all_key = sshkey_alg_list(0, 0, 1, ',');
4369a3
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
f5835d
-#define ASSEMBLE(what, defaults, all) \
f5835d
+#define ASSEMBLE(what, defaults, fips_defaults, all) \
f5835d
 	do { \
f5835d
 		if ((r = kex_assemble_names(&options->what, \
f5835d
-		    defaults, all)) != 0) \
f5835d
+		    (FIPS_mode() ? fips_defaults : defaults), \
f5835d
+		    all)) != 0) \
f5835d
 			fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
f5835d
 	} while (0)
4369a3
-	ASSEMBLE(ciphers, KEX_CLIENT_ENCRYPT, all_cipher);
4369a3
-	ASSEMBLE(macs, KEX_CLIENT_MAC, all_mac);
4369a3
-	ASSEMBLE(kex_algorithms, KEX_CLIENT_KEX, all_kex);
f5835d
-	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
f5835d
-	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
4369a3
-	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
4369a3
+	ASSEMBLE(ciphers, KEX_CLIENT_ENCRYPT, KEX_FIPS_ENCRYPT, all_cipher);
4369a3
+	ASSEMBLE(macs, KEX_CLIENT_MAC, KEX_FIPS_MAC, all_mac);
4369a3
+	ASSEMBLE(kex_algorithms, KEX_CLIENT_KEX, KEX_DEFAULT_KEX_FIPS, all_kex);
f5835d
+	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
f5835d
+	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
4369a3
+	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, KEX_FIPS_PK_ALG, all_sig);
f5835d
 #undef ASSEMBLE
f5835d
 	free(all_cipher);
f5835d
 	free(all_mac);
4369a3
diff -up openssh-7.9p1/sandbox-seccomp-filter.c.fips openssh-7.9p1/sandbox-seccomp-filter.c
4369a3
--- openssh-7.9p1/sandbox-seccomp-filter.c.fips	2019-03-11 17:06:37.586877712 +0100
4369a3
+++ openssh-7.9p1/sandbox-seccomp-filter.c	2019-03-11 17:06:37.622878050 +0100
f5835d
@@ -137,6 +137,9 @@ static const struct sock_filter preauth_
f5835d
 #ifdef __NR_open
f5835d
 	SC_DENY(__NR_open, EACCES),
f5835d
 #endif
f5835d
+#ifdef __NR_socket
f5835d
+	SC_DENY(__NR_socket, EACCES),
f5835d
+#endif
f5835d
 #ifdef __NR_openat
f5835d
 	SC_DENY(__NR_openat, EACCES),
f5835d
 #endif
4369a3
diff -up openssh-7.9p1/servconf.c.fips openssh-7.9p1/servconf.c
4369a3
--- openssh-7.9p1/servconf.c.fips	2019-03-11 17:06:37.568877543 +0100
4369a3
+++ openssh-7.9p1/servconf.c	2019-03-11 17:06:37.622878050 +0100
4369a3
@@ -209,18 +209,19 @@ assemble_algorithms(ServerOptions *o)
f5835d
 	all_kex = kex_alg_list(',');
f5835d
 	all_key = sshkey_alg_list(0, 0, 1, ',');
4369a3
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
f5835d
-#define ASSEMBLE(what, defaults, all) \
f5835d
+#define ASSEMBLE(what, defaults, fips_defaults, all) \
f5835d
 	do { \
f5835d
-		if ((r = kex_assemble_names(&o->what, defaults, all)) != 0) \
f5835d
+		if ((r = kex_assemble_names(&o->what, (FIPS_mode() \
f5835d
+		    ? fips_defaults : defaults), all)) != 0) \
f5835d
 			fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
f5835d
 	} while (0)
f5835d
-	ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, all_cipher);
f5835d
-	ASSEMBLE(macs, KEX_SERVER_MAC, all_mac);
f5835d
-	ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, all_kex);
f5835d
-	ASSEMBLE(hostkeyalgorithms, KEX_DEFAULT_PK_ALG, all_key);
f5835d
-	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
f5835d
-	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
4369a3
-	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
f5835d
+	ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, KEX_FIPS_ENCRYPT, all_cipher);
f5835d
+	ASSEMBLE(macs, KEX_SERVER_MAC, KEX_FIPS_MAC, all_mac);
f5835d
+	ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, KEX_DEFAULT_KEX_FIPS, all_kex);
f5835d
+	ASSEMBLE(hostkeyalgorithms, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
f5835d
+	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
f5835d
+	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
4369a3
+	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, KEX_FIPS_PK_ALG, all_sig);
f5835d
 #undef ASSEMBLE
f5835d
 	free(all_cipher);
f5835d
 	free(all_mac);
4369a3
diff -up openssh-7.9p1/ssh.c.fips openssh-7.9p1/ssh.c
4369a3
--- openssh-7.9p1/ssh.c.fips	2019-03-11 17:06:37.602877862 +0100
4369a3
+++ openssh-7.9p1/ssh.c	2019-03-11 17:06:37.623878060 +0100
0d83e7
@@ -76,6 +76,7 @@
f5835d
 #include <openssl/evp.h>
f5835d
 #include <openssl/err.h>
f5835d
 #endif
4369a3
+#include <openssl/crypto.h>
f5835d
 #include "openbsd-compat/openssl-compat.h"
f5835d
 #include "openbsd-compat/sys-queue.h"
f5835d
 
4369a3
@@ -1283,6 +1294,10 @@ main(int ac, char **av)
0d83e7
 		dump_client_config(&options, host);
0d83e7
 		exit(0);
0d83e7
 	}
0d83e7
+
f5835d
+	if (FIPS_mode()) {
0d83e7
+		debug("FIPS mode initialized");
f5835d
+	}
0d83e7
 
0d83e7
 	if (muxclient_command != 0 && options.control_path == NULL)
0d83e7
 		fatal("No ControlPath specified for \"-O\" command");
4369a3
diff -up openssh-7.9p1/sshconnect2.c.fips openssh-7.9p1/sshconnect2.c
4369a3
--- openssh-7.9p1/sshconnect2.c.fips	2019-03-11 17:06:37.580877655 +0100
4369a3
+++ openssh-7.9p1/sshconnect2.c	2019-03-11 17:06:37.623878060 +0100
f5835d
@@ -44,6 +44,8 @@
f5835d
 #include <vis.h>
f5835d
 #endif
f5835d
 
4369a3
+#include <openssl/crypto.h>
f5835d
+
f5835d
 #include "openbsd-compat/sys-queue.h"
f5835d
 
f5835d
 #include "xmalloc.h"
152122
@@ -148,7 +150,8 @@ order_hostkeyalgs(char *host, struct soc
152122
 	 * Otherwise, prefer the host key algorithms that match known keys
152122
 	 * while keeping the ordering of HostkeyAlgorithms as much as possible.
152122
 	 */
f5835d
-	oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
f5835d
+	oavail = avail = xstrdup((FIPS_mode()
f5835d
+	    ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG));
f5835d
 	maxlen = strlen(avail) + 1;
f5835d
 	first = xmalloc(maxlen);
f5835d
 	last = xmalloc(maxlen);
152122
@@ -229,14 +232,16 @@ ssh_kex2(struct ssh *ssh, char *host, st
4369a3
 	if (options.hostkeyalgorithms != NULL) {
4369a3
 		all_key = sshkey_alg_list(0, 0, 1, ',');
4369a3
 		if (kex_assemble_names(&options.hostkeyalgorithms,
4369a3
-		    KEX_DEFAULT_PK_ALG, all_key) != 0)
4369a3
+		    (FIPS_mode() ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG),
4369a3
+		    all_key) != 0)
4369a3
 			fatal("%s: kex_assemble_namelist", __func__);
4369a3
 		free(all_key);
4369a3
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
4369a3
 		    compat_pkalg_proposal(options.hostkeyalgorithms);
4369a3
 	} else {
4369a3
 		/* Enforce default */
4369a3
-		options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
4369a3
+		options.hostkeyalgorithms = xstrdup((FIPS_mode()
4369a3
+		    ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG));
4369a3
 		/* Prefer algorithms that we already have keys for */
4369a3
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
4369a3
 		    compat_pkalg_proposal(
0d83e7
@@ -201,35 +201,40 @@ ssh_kex2(char *host, struct sockaddr *ho
f5835d
 
4369a3
 #if defined(GSSAPI) && defined(WITH_OPENSSL)
f5835d
 	if (options.gss_keyex) {
4369a3
-		/* Add the GSSAPI mechanisms currently supported on this
f5835d
-		 * client to the key exchange algorithm proposal */
4369a3
-		orig = myproposal[PROPOSAL_KEX_ALGS];
f5835d
-
0d83e7
-		if (options.gss_server_identity) {
4369a3
-			gss_host = xstrdup(options.gss_server_identity);
0d83e7
-		} else if (options.gss_trust_dns) {
4369a3
-			gss_host = remote_hostname(ssh);
0d83e7
-			/* Fall back to specified host if we are using proxy command
0d83e7
-			 * and can not use DNS on that socket */
0d83e7
-			if (strcmp(gss_host, "UNKNOWN") == 0) {
0d83e7
-				gss_host = xstrdup(host);
0d83e7
-			}
0d83e7
-		} else {
4369a3
-			gss_host = xstrdup(host);
0d83e7
-		}
f5835d
-
f5835d
-		gss = ssh_gssapi_client_mechanisms(gss_host,
f5835d
-		    options.gss_client_identity, options.gss_kex_algorithms);
f5835d
-		if (gss) {
f5835d
-			debug("Offering GSSAPI proposal: %s", gss);
4369a3
-			xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
f5835d
-			    "%s,%s", gss, orig);
4369a3
-
4369a3
-			/* If we've got GSSAPI algorithms, then we also support the
4369a3
-			 * 'null' hostkey, as a last resort */
4369a3
-			orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
4369a3
-			xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
4369a3
-			    "%s,null", orig);
f5835d
+		if (FIPS_mode()) {
f5835d
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
f5835d
+			options.gss_keyex = 0;
f5835d
+		} else {
f5835d
+			/* Add the GSSAPI mechanisms currently supported on this
f5835d
+			 * client to the key exchange algorithm proposal */
4369a3
+			orig = myproposal[PROPOSAL_KEX_ALGS];
f5835d
+
0d83e7
+			if (options.gss_server_identity) {
4369a3
+				gss_host = xstrdup(options.gss_server_identity);
0d83e7
+			} else if (options.gss_trust_dns) {
4369a3
+				gss_host = remote_hostname(ssh);
0d83e7
+				/* Fall back to specified host if we are using proxy command
0d83e7
+				 * and can not use DNS on that socket */
0d83e7
+				if (strcmp(gss_host, "UNKNOWN") == 0) {
0d83e7
+					gss_host = xstrdup(host);
0d83e7
+				}
0d83e7
+			} else {
4369a3
+				gss_host = xstrdup(host);
0d83e7
+			}
f5835d
+
f5835d
+			gss = ssh_gssapi_client_mechanisms(gss_host,
f5835d
+			    options.gss_client_identity, options.gss_kex_algorithms);
f5835d
+			if (gss) {
f5835d
+				debug("Offering GSSAPI proposal: %s", gss);
4369a3
+				xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
f5835d
+				    "%s,%s", gss, orig);
4369a3
+
4369a3
+				/* If we've got GSSAPI algorithms, then we also support the
4369a3
+				 * 'null' hostkey, as a last resort */
4369a3
+				orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
4369a3
+				xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
4369a3
+				    "%s,null", orig);
f5835d
+			}
f5835d
 		}
f5835d
 	}
f5835d
 #endif
4369a3
diff -up openssh-7.9p1/sshd.c.fips openssh-7.9p1/sshd.c
4369a3
--- openssh-7.9p1/sshd.c.fips	2019-03-11 17:06:37.617878003 +0100
4369a3
+++ openssh-7.9p1/sshd.c	2019-03-11 17:06:37.624878069 +0100
f5835d
@@ -66,6 +66,7 @@
f5835d
 #include <grp.h>
f5835d
 #include <pwd.h>
f5835d
 #include <signal.h>
f5835d
+#include <syslog.h>
f5835d
 #include <stdarg.h>
f5835d
 #include <stdio.h>
f5835d
 #include <stdlib.h>
0d83e7
@@ -77,6 +78,7 @@
f5835d
 #include <openssl/dh.h>
f5835d
 #include <openssl/bn.h>
f5835d
 #include <openssl/rand.h>
4369a3
+#include <openssl/crypto.h>
f5835d
 #include "openbsd-compat/openssl-compat.h"
f5835d
 #endif
f5835d
 
0d83e7
@@ -1581,6 +1584,7 @@ main(int ac, char **av)
f5835d
 #endif
f5835d
 	__progname = ssh_get_progname(av[0]);
f5835d
 
4369a3
+	OpenSSL_add_all_algorithms();
f5835d
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
f5835d
 	saved_argc = ac;
f5835d
 	rexec_argc = ac;
4369a3
@@ -2036,6 +2051,10 @@ main(int ac, char **av)
f5835d
 	/* Reinitialize the log (because of the fork above). */
f5835d
 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
f5835d
 
f5835d
+	if (FIPS_mode()) {
0d83e7
+		debug("FIPS mode initialized");
f5835d
+	}
f5835d
+
f5835d
 	/* Chdir to the root directory so that the current disk can be
f5835d
 	   unmounted if desired. */
f5835d
 	if (chdir("/") == -1)
4369a3
@@ -2412,10 +2431,14 @@ do_ssh2_kex(void)
f5835d
 	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
f5835d
 		orig = NULL;
f5835d
 
f5835d
-	if (options.gss_keyex)
f5835d
-		gss = ssh_gssapi_server_mechanisms();
f5835d
-	else
f5835d
-		gss = NULL;
f5835d
+	if (options.gss_keyex) {
f5835d
+		if (FIPS_mode()) {
f5835d
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
f5835d
+			options.gss_keyex = 0;
f5835d
+		} else {
f5835d
+			gss = ssh_gssapi_server_mechanisms();
f5835d
+		}
f5835d
+	}
f5835d
 
f5835d
 	if (gss && orig)
f5835d
 		xasprintf(&newstr, "%s,%s", gss, orig);
4369a3
diff -up openssh-7.9p1/sshkey.c.fips openssh-7.9p1/sshkey.c
4369a3
--- openssh-7.9p1/sshkey.c.fips	2019-03-11 17:06:37.617878003 +0100
4369a3
+++ openssh-7.9p1/sshkey.c	2019-03-11 17:06:37.624878069 +0100
f5835d
@@ -34,6 +34,7 @@
f5835d
 #include <openssl/evp.h>
f5835d
 #include <openssl/err.h>
f5835d
 #include <openssl/pem.h>
4369a3
+#include <openssl/crypto.h>
f5835d
 #endif
f5835d
 
f5835d
 #include "crypto_api.h"
f5835d
@@ -57,6 +58,7 @@
f5835d
 #include "sshkey.h"
f5835d
 #include "sshkey-xmss.h"
f5835d
 #include "match.h"
f5835d
+#include "log.h"
f5835d
 
f5835d
 #include "xmss_fast.h"
f5835d
 
4369a3
@@ -1514,6 +1516,8 @@ rsa_generate_private_key(u_int bits, RSA
f5835d
 	}
f5835d
 	if (!BN_set_word(f4, RSA_F4) ||
f5835d
 	    !RSA_generate_key_ex(private, bits, f4, NULL)) {
f5835d
+			if (FIPS_mode())
f5835d
+				logit("%s: the key length might be unsupported by FIPS mode approved key generation method", __func__);
f5835d
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
f5835d
 		goto out;
f5835d
 	}
4369a3
diff -up openssh-7.9p1/ssh-keygen.c.fips openssh-7.9p1/ssh-keygen.c
4369a3
--- openssh-7.9p1/ssh-keygen.c.fips	2019-03-11 17:06:37.590877750 +0100
4369a3
+++ openssh-7.9p1/ssh-keygen.c	2019-03-11 17:06:37.625878079 +0100
4369a3
@@ -230,6 +230,12 @@ type_bits_valid(int type, const char *na
f5835d
 	    OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
f5835d
 	if (*bitsp > maxbits)
f5835d
 		fatal("key bits exceeds maximum %d", maxbits);
f5835d
+	if (FIPS_mode()) {
f5835d
+		if (type == KEY_DSA)
f5835d
+			fatal("DSA keys are not allowed in FIPS mode");
f5835d
+		if (type == KEY_ED25519)
f5835d
+			fatal("ED25519 keys are not allowed in FIPS mode");
f5835d
+	}
f5835d
 	switch (type) {
f5835d
 	case KEY_DSA:
f5835d
 		if (*bitsp != 1024)
4369a3
@@ -1029,9 +1035,17 @@ do_gen_all_hostkeys(struct passwd *pw)
4369a3
 			first = 1;
4369a3
 			printf("%s: generating new host keys: ", __progname);
4369a3
 		}
4369a3
+		type = sshkey_type_from_name(key_types[i].key_type);
4369a3
+
4369a3
+		/* Skip the keys that are not supported in FIPS mode */
4369a3
+		if (FIPS_mode() && (type == KEY_DSA || type == KEY_ED25519)) {
4369a3
+			logit("Skipping %s key in FIPS mode",
4369a3
+			    key_types[i].key_type_display);
4369a3
+			goto next;
4369a3
+		}
4369a3
+
4369a3
 		printf("%s ", key_types[i].key_type_display);
4369a3
 		fflush(stdout);
4369a3
-		type = sshkey_type_from_name(key_types[i].key_type);
4369a3
 		if ((fd = mkstemp(prv_tmp)) == -1) {
4369a3
 			error("Could not save your public key in %s: %s",
4369a3
 			    prv_tmp, strerror(errno));