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