81a703d
diff -up openssh-7.9p1/cipher-ctr.c.fips openssh-7.9p1/cipher-ctr.c
81a703d
--- openssh-7.9p1/cipher-ctr.c.fips	2019-03-11 17:06:37.519877082 +0100
81a703d
+++ openssh-7.9p1/cipher-ctr.c	2019-03-11 17:06:37.620878031 +0100
535d341
@@ -179,7 +179,8 @@ evp_aes_128_ctr(void)
535d341
 	aes_ctr.do_cipher = ssh_aes_ctr;
535d341
 #ifndef SSH_OLD_EVP
535d341
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
535d341
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
535d341
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
535d341
+	    EVP_CIPH_FLAG_FIPS;
535d341
 #endif
535d341
 	return (&aes_ctr);
535d341
 }
81a703d
diff -up openssh-7.9p1/clientloop.c.fips openssh-7.9p1/clientloop.c
81a703d
--- openssh-7.9p1/clientloop.c.fips	2019-03-11 17:06:37.523877120 +0100
81a703d
+++ openssh-7.9p1/clientloop.c	2019-03-11 17:06:37.620878031 +0100
81a703d
@@ -2014,7 +2014,8 @@ key_accepted_by_hostkeyalgs(const struct
9dbec70
 {
9dbec70
 	const char *ktype = sshkey_ssh_name(key);
9dbec70
 	const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
9dbec70
-	    options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
9dbec70
+	    options.hostkeyalgorithms : (FIPS_mode() ?
9dbec70
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG);
9dbec70
 
9dbec70
 	if (key == NULL || key->type == KEY_UNSPEC)
9dbec70
 		return 0;
81a703d
diff -up openssh-7.9p1/dh.c.fips openssh-7.9p1/dh.c
81a703d
--- openssh-7.9p1/dh.c.fips	2018-10-17 02:01:20.000000000 +0200
81a703d
+++ openssh-7.9p1/dh.c	2019-03-11 17:08:11.769763057 +0100
81a703d
@@ -152,6 +152,12 @@ choose_dh(int min, int wantbits, int max
81a703d
 	int best, bestcount, which, linenum;
81a703d
 	struct dhgroup dhg;
81a703d
 
81a703d
+	if (FIPS_mode()) {
81a703d
+		logit("Using arbitrary primes is not allowed in FIPS mode."
81a703d
+		    " Falling back to known groups.");
81a703d
+		return (dh_new_group_fallback(max));
81a703d
+	}
81a703d
+
81a703d
 	if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL) {
81a703d
 		logit("WARNING: could not open %s (%s), using fixed modulus",
81a703d
 		    _PATH_DH_MODULI, strerror(errno));
81a703d
@@ -489,4 +495,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 */
81a703d
diff -up openssh-7.9p1/dh.h.fips openssh-7.9p1/dh.h
81a703d
--- openssh-7.9p1/dh.h.fips	2018-10-17 02:01:20.000000000 +0200
81a703d
+++ openssh-7.9p1/dh.h	2019-03-11 17:08:18.718828381 +0100
81a703d
@@ -43,6 +43,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);
81a703d
 
81a703d
diff -up openssh-7.9p1/kex.c.fips openssh-7.9p1/kex.c
81a703d
--- openssh-7.9p1/kex.c.fips	2019-03-11 17:06:37.614877975 +0100
81a703d
+++ openssh-7.9p1/kex.c	2019-03-11 17:06:37.621878041 +0100
44e2032
@@ -175,7 +196,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
 		}
81a703d
diff -up openssh-7.9p1/kexgexc.c.fips openssh-7.9p1/kexgexc.c
81a703d
--- openssh-7.9p1/kexgexc.c.fips	2018-10-17 02:01:20.000000000 +0200
81a703d
+++ openssh-7.9p1/kexgexc.c	2019-03-11 17:06:37.621878041 +0100
535d341
@@ -28,6 +28,7 @@
8028159
 
132f8f8
 #ifdef WITH_OPENSSL
8028159
 
cb35953
+#include <openssl/crypto.h>
8028159
 #include <sys/types.h>
8028159
 
6cf9b8e
 #include <openssl/dh.h>
81a703d
@@ -118,6 +119,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 */
81a703d
diff -up openssh-7.9p1/Makefile.in.fips openssh-7.9p1/Makefile.in
81a703d
--- openssh-7.9p1/Makefile.in.fips	2019-03-11 17:06:37.615877984 +0100
81a703d
+++ openssh-7.9p1/Makefile.in	2019-03-11 17:06:37.621878041 +0100
44e2032
@@ -179,25 +179,25 @@ libssh.a: $(LIBSSH_OBJS)
535d341
 	$(RANLIB) $@
535d341
 
535d341
 ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
535d341
-	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS) $(GSSLIBS)
535d341
+	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHLIBS) $(LIBS) $(GSSLIBS)
535d341
 
535d341
 sshd$(EXEEXT): libssh.a	$(LIBCOMPAT) $(SSHDOBJS)
535d341
-	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
535d341
+	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
535d341
 
5878ebb
 scp$(EXEEXT): $(LIBCOMPAT) libssh.a scp.o progressmeter.o
bbf61da
 	$(LD) -o $@ scp.o progressmeter.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d341
 
535d341
 ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o
535d341
-	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d341
+	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d341
 
535d341
 ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
535d341
-	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d341
+	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d341
 
535d341
 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
535d341
-	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d341
+	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d341
 
def1deb
 ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o readconf.o uidswap.o compat.o
bbf61da
-	$(LD) -o $@ ssh-keysign.o readconf.o uidswap.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
bbf61da
+	$(LD) -o $@ ssh-keysign.o readconf.o uidswap.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
535d341
 
535d341
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
535d341
 	$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
44e2032
@@ -215,7 +215,7 @@ ssh-cavs$(EXEEXT): $(LIBCOMPAT) libssh.a
13073f8
 	$(LD) -o $@ ssh-cavs.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
535d341
 
13073f8
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
13073f8
-	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
13073f8
+	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
535d341
 
535d341
 sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o
535d341
 	$(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
81a703d
diff -up openssh-7.9p1/myproposal.h.fips openssh-7.9p1/myproposal.h
81a703d
--- openssh-7.9p1/myproposal.h.fips	2018-10-17 02:01:20.000000000 +0200
81a703d
+++ openssh-7.9p1/myproposal.h	2019-03-11 17:06:37.621878041 +0100
81a703d
@@ -116,6 +116,14 @@
9dbec70
 	"rsa-sha2-256," \
9dbec70
 	"ssh-rsa"
9dbec70
 
9dbec70
+#define	KEX_FIPS_PK_ALG	\
9dbec70
+	HOSTKEY_ECDSA_CERT_METHODS \
9dbec70
+	"ssh-rsa-cert-v01@openssh.com," \
9dbec70
+	HOSTKEY_ECDSA_METHODS \
9dbec70
+	"rsa-sha2-512," \
9dbec70
+	"rsa-sha2-256," \
9dbec70
+	"ssh-rsa"
9dbec70
+
9dbec70
 /* the actual algorithms */
9dbec70
 
9dbec70
 #define KEX_SERVER_ENCRYPT \
81a703d
@@ -139,6 +147,38 @@
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," \
951e3ca
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se" \
951e3ca
+	AESGCM_CIPHER_MODES
8028159
+#ifdef HAVE_EVP_SHA256
eb751fd
+# define KEX_DEFAULT_KEX_FIPS		\
eb751fd
+	KEX_ECDH_METHODS \
eb751fd
+	KEX_SHA2_METHODS \
eb751fd
+	"diffie-hellman-group14-sha256"
eb751fd
+# 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"
8028159
+#else
eb751fd
+# ifdef OPENSSL_HAS_NISTP521
eb751fd
+#  define KEX_DEFAULT_KEX_FIPS		\
eb751fd
+	"ecdh-sha2-nistp256," \
eb751fd
+	"ecdh-sha2-nistp384," \
eb751fd
+	"ecdh-sha2-nistp521"
eb751fd
+# else
eb751fd
+#  define KEX_DEFAULT_KEX_FIPS		\
eb751fd
+	"ecdh-sha2-nistp256," \
eb751fd
+	"ecdh-sha2-nistp384"
eb751fd
+# endif
8028159
+#define        KEX_FIPS_MAC \
8028159
+       "hmac-sha1"
8028159
+#endif
1900351
+
eaa7af2
 /* Not a KEX value, but here so all the algorithm defaults are together */
eaa7af2
 #define	SSH_ALLOWED_CA_SIGALGS	\
eaa7af2
 	"ecdsa-sha2-nistp256," \
81a703d
diff -up openssh-7.9p1/readconf.c.fips openssh-7.9p1/readconf.c
81a703d
--- openssh-7.9p1/readconf.c.fips	2019-03-11 17:06:37.601877853 +0100
81a703d
+++ openssh-7.9p1/readconf.c	2019-03-11 17:06:37.622878050 +0100
81a703d
@@ -2178,18 +2178,19 @@ fill_default_options(Options * options)
bbf61da
 	all_kex = kex_alg_list(',');
bbf61da
 	all_key = sshkey_alg_list(0, 0, 1, ',');
eaa7af2
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
bbf61da
-#define ASSEMBLE(what, defaults, all) \
bbf61da
+#define ASSEMBLE(what, defaults, fips_defaults, all) \
bbf61da
 	do { \
bbf61da
 		if ((r = kex_assemble_names(&options->what, \
bbf61da
-		    defaults, all)) != 0) \
bbf61da
+		    (FIPS_mode() ? fips_defaults : defaults), \
bbf61da
+		    all)) != 0) \
bbf61da
 			fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
bbf61da
 	} while (0)
def1deb
-	ASSEMBLE(ciphers, KEX_CLIENT_ENCRYPT, all_cipher);
def1deb
-	ASSEMBLE(macs, KEX_CLIENT_MAC, all_mac);
def1deb
-	ASSEMBLE(kex_algorithms, KEX_CLIENT_KEX, all_kex);
bbf61da
-	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
bbf61da
-	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
eaa7af2
-	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
def1deb
+	ASSEMBLE(ciphers, KEX_CLIENT_ENCRYPT, KEX_FIPS_ENCRYPT, all_cipher);
def1deb
+	ASSEMBLE(macs, KEX_CLIENT_MAC, KEX_FIPS_MAC, all_mac);
def1deb
+	ASSEMBLE(kex_algorithms, KEX_CLIENT_KEX, KEX_DEFAULT_KEX_FIPS, all_kex);
bbf61da
+	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
bbf61da
+	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
eaa7af2
+	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, KEX_FIPS_PK_ALG, all_sig);
bbf61da
 #undef ASSEMBLE
bbf61da
 	free(all_cipher);
bbf61da
 	free(all_mac);
81a703d
diff -up openssh-7.9p1/sandbox-seccomp-filter.c.fips openssh-7.9p1/sandbox-seccomp-filter.c
81a703d
--- openssh-7.9p1/sandbox-seccomp-filter.c.fips	2019-03-11 17:06:37.586877712 +0100
81a703d
+++ openssh-7.9p1/sandbox-seccomp-filter.c	2019-03-11 17:06:37.622878050 +0100
9dbec70
@@ -137,6 +137,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
81a703d
diff -up openssh-7.9p1/servconf.c.fips openssh-7.9p1/servconf.c
81a703d
--- openssh-7.9p1/servconf.c.fips	2019-03-11 17:06:37.568877543 +0100
81a703d
+++ openssh-7.9p1/servconf.c	2019-03-11 17:06:37.622878050 +0100
81a703d
@@ -209,18 +209,19 @@ assemble_algorithms(ServerOptions *o)
bbf61da
 	all_kex = kex_alg_list(',');
bbf61da
 	all_key = sshkey_alg_list(0, 0, 1, ',');
eaa7af2
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
bbf61da
-#define ASSEMBLE(what, defaults, all) \
bbf61da
+#define ASSEMBLE(what, defaults, fips_defaults, all) \
bbf61da
 	do { \
bbf61da
-		if ((r = kex_assemble_names(&o->what, defaults, all)) != 0) \
bbf61da
+		if ((r = kex_assemble_names(&o->what, (FIPS_mode() \
bbf61da
+		    ? fips_defaults : defaults), all)) != 0) \
bbf61da
 			fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
bbf61da
 	} while (0)
bbf61da
-	ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, all_cipher);
bbf61da
-	ASSEMBLE(macs, KEX_SERVER_MAC, all_mac);
bbf61da
-	ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, all_kex);
bbf61da
-	ASSEMBLE(hostkeyalgorithms, KEX_DEFAULT_PK_ALG, all_key);
bbf61da
-	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
bbf61da
-	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
eaa7af2
-	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
bbf61da
+	ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, KEX_FIPS_ENCRYPT, all_cipher);
bbf61da
+	ASSEMBLE(macs, KEX_SERVER_MAC, KEX_FIPS_MAC, all_mac);
bbf61da
+	ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, KEX_DEFAULT_KEX_FIPS, all_kex);
bbf61da
+	ASSEMBLE(hostkeyalgorithms, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
bbf61da
+	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
bbf61da
+	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
eaa7af2
+	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, KEX_FIPS_PK_ALG, all_sig);
bbf61da
 #undef ASSEMBLE
bbf61da
 	free(all_cipher);
bbf61da
 	free(all_mac);
81a703d
diff -up openssh-7.9p1/ssh.c.fips openssh-7.9p1/ssh.c
81a703d
--- openssh-7.9p1/ssh.c.fips	2019-03-11 17:06:37.602877862 +0100
81a703d
+++ openssh-7.9p1/ssh.c	2019-03-11 17:06:37.623878060 +0100
6cf9b8e
@@ -76,6 +76,8 @@
8028159
 #include <openssl/evp.h>
8028159
 #include <openssl/err.h>
1900351
 #endif
cb35953
+#include <openssl/crypto.h>
8028159
+#include <fipscheck.h>
8028159
 #include "openbsd-compat/openssl-compat.h"
8028159
 #include "openbsd-compat/sys-queue.h"
8028159
 
81a703d
@@ -600,6 +602,16 @@ main(int ac, char **av)
8028159
 	sanitise_stdfd();
8028159
 
8028159
 	__progname = ssh_get_progname(av[0]);
eaa7af2
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
eaa7af2
+	SSLeay_add_all_algorithms();
eaa7af2
+#endif
8028159
+	if (access("/etc/system-fips", F_OK) == 0)
580f986
+		if (! FIPSCHECK_verify(NULL, NULL)){
8028159
+			if (FIPS_mode())
8028159
+				fatal("FIPS integrity verification test failed.");
8028159
+			else
8028159
+				logit("FIPS integrity verification test failed.");
580f986
+	}
8028159
 
8028159
 #ifndef HAVE_SETPROCTITLE
8028159
 	/* Prepare for later setproctitle emulation */
81a703d
@@ -1283,6 +1294,10 @@ main(int ac, char **av)
8028159
 
8028159
 	seed_rng();
8028159
 
8028159
+	if (FIPS_mode()) {
8028159
+		logit("FIPS mode initialized");
8028159
+	}
8028159
+
def1deb
 	/*
def1deb
 	 * Discard other fds that are hanging around. These can cause problem
def1deb
 	 * with backgrounded ssh processes started by ControlPersist.
81a703d
diff -up openssh-7.9p1/sshconnect2.c.fips openssh-7.9p1/sshconnect2.c
81a703d
--- openssh-7.9p1/sshconnect2.c.fips	2019-03-11 17:06:37.580877655 +0100
81a703d
+++ openssh-7.9p1/sshconnect2.c	2019-03-11 17:06:37.623878060 +0100
3f55133
@@ -44,6 +44,8 @@
8028159
 #include <vis.h>
8028159
 #endif
8028159
 
cb35953
+#include <openssl/crypto.h>
8028159
+
8028159
 #include "openbsd-compat/sys-queue.h"
8028159
 
8028159
 #include "xmalloc.h"
def1deb
@@ -117,7 +117,8 @@ order_hostkeyalgs(char *host, struct soc
9dbec70
 	for (i = 0; i < options.num_system_hostfiles; i++)
9dbec70
 		load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
9dbec70
 
9dbec70
-	oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
9dbec70
+	oavail = avail = xstrdup((FIPS_mode()
9dbec70
+	    ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG));
9dbec70
 	maxlen = strlen(avail) + 1;
9dbec70
 	first = xmalloc(maxlen);
9dbec70
 	last = xmalloc(maxlen);
def1deb
@@ -185,14 +185,16 @@ ssh_kex2(char *host, struct sockaddr *ho
def1deb
 	if (options.hostkeyalgorithms != NULL) {
def1deb
 		all_key = sshkey_alg_list(0, 0, 1, ',');
def1deb
 		if (kex_assemble_names(&options.hostkeyalgorithms,
def1deb
-		    KEX_DEFAULT_PK_ALG, all_key) != 0)
def1deb
+		    (FIPS_mode() ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG),
def1deb
+		    all_key) != 0)
def1deb
 			fatal("%s: kex_assemble_namelist", __func__);
def1deb
 		free(all_key);
def1deb
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
def1deb
 		    compat_pkalg_proposal(options.hostkeyalgorithms);
def1deb
 	} else {
def1deb
 		/* Enforce default */
def1deb
-		options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
def1deb
+		options.hostkeyalgorithms = xstrdup((FIPS_mode()
def1deb
+		    ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG));
def1deb
 		/* Prefer algorithms that we already have keys for */
def1deb
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
def1deb
 		    compat_pkalg_proposal(
def1deb
@@ -201,29 +201,34 @@ ssh_kex2(char *host, struct sockaddr *ho
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
-
e8876f1
-		if (options.gss_server_identity)
def1deb
-			gss_host = xstrdup(options.gss_server_identity);
e8876f1
-		else if (options.gss_trust_dns)
def1deb
-			gss_host = remote_hostname(ssh);
132f8f8
-		else
def1deb
-			gss_host = xstrdup(host);
132f8f8
-
bc4ef0f
-		gss = ssh_gssapi_client_mechanisms(gss_host,
bc4ef0f
-		    options.gss_client_identity, options.gss_kex_algorithms);
132f8f8
-		if (gss) {
132f8f8
-			debug("Offering GSSAPI proposal: %s", gss);
def1deb
-			xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
132f8f8
-			    "%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
+		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
+
e8876f1
+			if (options.gss_server_identity)
def1deb
+				gss_host = xstrdup(options.gss_server_identity);
e8876f1
+			else if (options.gss_trust_dns)
def1deb
+				gss_host = remote_hostname(ssh);
8028159
+			else
def1deb
+				gss_host = xstrdup(host);
132f8f8
+
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
81a703d
diff -up openssh-7.9p1/sshd.c.fips openssh-7.9p1/sshd.c
81a703d
--- openssh-7.9p1/sshd.c.fips	2019-03-11 17:06:37.617878003 +0100
81a703d
+++ openssh-7.9p1/sshd.c	2019-03-11 17:06:37.624878069 +0100
1900351
@@ -66,6 +66,7 @@
1900351
 #include <grp.h>
1900351
 #include <pwd.h>
1900351
 #include <signal.h>
1900351
+#include <syslog.h>
1900351
 #include <stdarg.h>
1900351
 #include <stdio.h>
1900351
 #include <stdlib.h>
132f8f8
@@ -77,6 +78,8 @@
8028159
 #include <openssl/dh.h>
8028159
 #include <openssl/bn.h>
8028159
 #include <openssl/rand.h>
cb35953
+#include <openssl/crypto.h>
8028159
+#include <fipscheck.h>
8028159
 #include "openbsd-compat/openssl-compat.h"
1900351
 #endif
8028159
 
81a703d
@@ -1581,6 +1584,18 @@ main(int ac, char **av)
8028159
 #endif
8028159
 	__progname = ssh_get_progname(av[0]);
8028159
 
eaa7af2
+	OpenSSL_add_all_algorithms();
8028159
+	if (access("/etc/system-fips", F_OK) == 0)
8028159
+		if (! FIPSCHECK_verify(NULL, NULL)) {
8028159
+			openlog(__progname, LOG_PID, LOG_AUTHPRIV);
8028159
+			if (FIPS_mode()) {
8028159
+				syslog(LOG_CRIT, "FIPS integrity verification test failed.");
8028159
+				cleanup_exit(255);
8028159
+			}
8028159
+			else
8028159
+				syslog(LOG_INFO, "FIPS integrity verification test failed.");
8028159
+			closelog();
8028159
+		}
8028159
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
8028159
 	saved_argc = ac;
8028159
 	rexec_argc = ac;
81a703d
@@ -2036,6 +2051,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()) {
8028159
+		logit("FIPS mode initialized");
8028159
+	}
8028159
+
8028159
 	/* Chdir to the root directory so that the current disk can be
8028159
 	   unmounted if desired. */
8028159
 	if (chdir("/") == -1)
81a703d
@@ -2412,10 +2431,14 @@ do_ssh2_kex(void)
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);
81a703d
diff -up openssh-7.9p1/sshkey.c.fips openssh-7.9p1/sshkey.c
81a703d
--- openssh-7.9p1/sshkey.c.fips	2019-03-11 17:06:37.617878003 +0100
81a703d
+++ openssh-7.9p1/sshkey.c	2019-03-11 17:06:37.624878069 +0100
6cf9b8e
@@ -34,6 +34,7 @@
1900351
 #include <openssl/evp.h>
1900351
 #include <openssl/err.h>
1900351
 #include <openssl/pem.h>
cb35953
+#include <openssl/crypto.h>
132f8f8
 #endif
1900351
 
1900351
 #include "crypto_api.h"
44e2032
@@ -57,6 +58,7 @@
84d3989
 #include "sshkey.h"
3cd4899
 #include "sshkey-xmss.h"
84d3989
 #include "match.h"
9dbec70
+#include "log.h"
84d3989
 
3cd4899
 #include "xmss_fast.h"
3cd4899
 
81a703d
@@ -1514,6 +1516,8 @@ rsa_generate_private_key(u_int bits, RSA
1900351
 	}
1900351
 	if (!BN_set_word(f4, RSA_F4) ||
1900351
 	    !RSA_generate_key_ex(private, bits, f4, NULL)) {
1900351
+			if (FIPS_mode())
1900351
+				logit("%s: the key length might be unsupported by FIPS mode approved key generation method", __func__);
1900351
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
1900351
 		goto out;
1900351
 	}
81a703d
diff -up openssh-7.9p1/ssh-keygen.c.fips openssh-7.9p1/ssh-keygen.c
81a703d
--- openssh-7.9p1/ssh-keygen.c.fips	2019-03-11 17:06:37.590877750 +0100
81a703d
+++ openssh-7.9p1/ssh-keygen.c	2019-03-11 17:06:37.625878079 +0100
81a703d
@@ -230,6 +230,12 @@ type_bits_valid(int type, const char *na
9dbec70
 	    OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
9dbec70
 	if (*bitsp > maxbits)
9dbec70
 		fatal("key bits exceeds maximum %d", maxbits);
9dbec70
+	if (FIPS_mode()) {
9dbec70
+		if (type == KEY_DSA)
9dbec70
+			fatal("DSA keys are not allowed in FIPS mode");
9dbec70
+		if (type == KEY_ED25519)
9dbec70
+			fatal("ED25519 keys are not allowed in FIPS mode");
9dbec70
+	}
5b55d09
 	switch (type) {
5b55d09
 	case KEY_DSA:
5b55d09
 		if (*bitsp != 1024)