jonathancammack / rpms / openssh

Forked from rpms/openssh 9 months ago
Clone
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()) {
3e8b5b
+		logit("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/Makefile.in.fips openssh-7.9p1/Makefile.in
3e8b5b
--- openssh-7.9p1/Makefile.in.fips	2019-03-11 17:06:37.615877984 +0100
3e8b5b
+++ openssh-7.9p1/Makefile.in	2019-03-11 17:06:37.621878041 +0100
3e8b5b
@@ -179,25 +179,25 @@ libssh.a: $(LIBSSH_OBJS)
3e8b5b
 	$(RANLIB) $@
3e8b5b
 
3e8b5b
 ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
3e8b5b
-	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS) $(GSSLIBS)
3e8b5b
+	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHLIBS) $(LIBS) $(GSSLIBS)
3e8b5b
 
3e8b5b
 sshd$(EXEEXT): libssh.a	$(LIBCOMPAT) $(SSHDOBJS)
3e8b5b
-	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
3e8b5b
+	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
3e8b5b
 
3e8b5b
 scp$(EXEEXT): $(LIBCOMPAT) libssh.a scp.o progressmeter.o
3e8b5b
 	$(LD) -o $@ scp.o progressmeter.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
3e8b5b
 
3e8b5b
 ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o
3e8b5b
-	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
3e8b5b
+	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
3e8b5b
 
3e8b5b
 ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
3e8b5b
-	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
3e8b5b
+	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
3e8b5b
 
3e8b5b
 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
3e8b5b
-	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
3e8b5b
+	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
3e8b5b
 
3e8b5b
 ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o readconf.o uidswap.o compat.o
3e8b5b
-	$(LD) -o $@ ssh-keysign.o readconf.o uidswap.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
3e8b5b
+	$(LD) -o $@ ssh-keysign.o readconf.o uidswap.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
3e8b5b
 
3e8b5b
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
3e8b5b
 	$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
3e8b5b
@@ -215,7 +215,7 @@ ssh-cavs$(EXEEXT): $(LIBCOMPAT) libssh.a
3e8b5b
 	$(LD) -o $@ ssh-cavs.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
3e8b5b
 
3e8b5b
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
3e8b5b
-	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
3e8b5b
+	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
3e8b5b
 
3e8b5b
 sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o
3e8b5b
 	$(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
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
3e8b5b
@@ -76,6 +76,8 @@
3e8b5b
 #include <openssl/evp.h>
3e8b5b
 #include <openssl/err.h>
3e8b5b
 #endif
3e8b5b
+#include <openssl/crypto.h>
3e8b5b
+#include <fipscheck.h>
3e8b5b
 #include "openbsd-compat/openssl-compat.h"
3e8b5b
 #include "openbsd-compat/sys-queue.h"
3e8b5b
 
3e8b5b
@@ -600,6 +602,16 @@ main(int ac, char **av)
3e8b5b
 	sanitise_stdfd();
3e8b5b
 
3e8b5b
 	__progname = ssh_get_progname(av[0]);
3e8b5b
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
3e8b5b
+	SSLeay_add_all_algorithms();
3e8b5b
+#endif
3e8b5b
+	if (access("/etc/system-fips", F_OK) == 0)
3e8b5b
+		if (! FIPSCHECK_verify(NULL, NULL)){
3e8b5b
+			if (FIPS_mode())
3e8b5b
+				fatal("FIPS integrity verification test failed.");
3e8b5b
+			else
3e8b5b
+				logit("FIPS integrity verification test failed.");
3e8b5b
+	}
3e8b5b
 
3e8b5b
 #ifndef HAVE_SETPROCTITLE
3e8b5b
 	/* Prepare for later setproctitle emulation */
3e8b5b
@@ -1283,6 +1294,10 @@ main(int ac, char **av)
3e8b5b
 
3e8b5b
 	seed_rng();
3e8b5b
 
3e8b5b
+	if (FIPS_mode()) {
3e8b5b
+		logit("FIPS mode initialized");
3e8b5b
+	}
3e8b5b
+
3e8b5b
 	/*
3e8b5b
 	 * Discard other fds that are hanging around. These can cause problem
3e8b5b
 	 * with backgrounded ssh processes started by ControlPersist.
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"
3e8b5b
@@ -117,7 +117,8 @@ order_hostkeyalgs(char *host, struct soc
3e8b5b
 	for (i = 0; i < options.num_system_hostfiles; i++)
3e8b5b
 		load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
3e8b5b
 
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);
3e8b5b
@@ -185,14 +185,16 @@ ssh_kex2(char *host, struct sockaddr *ho
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(
3e8b5b
@@ -201,29 +201,34 @@ 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
-
3e8b5b
-		if (options.gss_server_identity)
3e8b5b
-			gss_host = xstrdup(options.gss_server_identity);
3e8b5b
-		else if (options.gss_trust_dns)
3e8b5b
-			gss_host = remote_hostname(ssh);
3e8b5b
-		else
3e8b5b
-			gss_host = xstrdup(host);
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
+
3e8b5b
+			if (options.gss_server_identity)
3e8b5b
+				gss_host = xstrdup(options.gss_server_identity);
3e8b5b
+			else if (options.gss_trust_dns)
3e8b5b
+				gss_host = remote_hostname(ssh);
3e8b5b
+			else
3e8b5b
+				gss_host = xstrdup(host);
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>
3e8b5b
@@ -77,6 +78,8 @@
3e8b5b
 #include <openssl/dh.h>
3e8b5b
 #include <openssl/bn.h>
3e8b5b
 #include <openssl/rand.h>
3e8b5b
+#include <openssl/crypto.h>
3e8b5b
+#include <fipscheck.h>
3e8b5b
 #include "openbsd-compat/openssl-compat.h"
3e8b5b
 #endif
3e8b5b
 
3e8b5b
@@ -1581,6 +1584,18 @@ main(int ac, char **av)
3e8b5b
 #endif
3e8b5b
 	__progname = ssh_get_progname(av[0]);
3e8b5b
 
3e8b5b
+	OpenSSL_add_all_algorithms();
3e8b5b
+	if (access("/etc/system-fips", F_OK) == 0)
3e8b5b
+		if (! FIPSCHECK_verify(NULL, NULL)) {
3e8b5b
+			openlog(__progname, LOG_PID, LOG_AUTHPRIV);
3e8b5b
+			if (FIPS_mode()) {
3e8b5b
+				syslog(LOG_CRIT, "FIPS integrity verification test failed.");
3e8b5b
+				cleanup_exit(255);
3e8b5b
+			}
3e8b5b
+			else
3e8b5b
+				syslog(LOG_INFO, "FIPS integrity verification test failed.");
3e8b5b
+			closelog();
3e8b5b
+		}
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()) {
3e8b5b
+		logit("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));