rutwa189 / rpms / openssh

Forked from rpms/openssh a year ago
Clone
8f2528
diff -up openssh-7.4p1/cipher.c.fips openssh-7.4p1/cipher.c
8f2528
--- openssh-7.4p1/cipher.c.fips	2017-02-09 14:53:47.174347449 +0100
8f2528
+++ openssh-7.4p1/cipher.c	2017-02-09 14:53:47.182347441 +0100
8f2528
@@ -39,6 +39,8 @@
8f2528
 
8f2528
 #include <sys/types.h>
8f2528
 
8f2528
+#include <openssl/fips.h>
8f2528
+
8f2528
 #include <string.h>
8f2528
 #include <stdarg.h>
8f2528
 #include <stdio.h>
8f2528
@@ -116,6 +118,20 @@ static const struct sshcipher ciphers[]
8f2528
 	{ NULL,		SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
8f2528
 };
8f2528
 
8f2528
+static const struct sshcipher fips_ciphers[] = {
8f2528
+	{ "none",	SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
8f2528
+	{ "3des-cbc",	SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
8f2528
+	{ "aes128-cbc",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
8f2528
+	{ "aes192-cbc",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
8f2528
+	{ "aes256-cbc",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
8f2528
+	{ "rijndael-cbc@lysator.liu.se",
8f2528
+			SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
8f2528
+	{ "aes128-ctr",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
8f2528
+	{ "aes192-ctr",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
8f2528
+	{ "aes256-ctr",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
8f2528
+	{ NULL,		SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
8f2528
+};
8f2528
+
8f2528
 /*--*/
8f2528
 
8f2528
 /* Returns a comma-separated list of supported ciphers. */
8f2528
@@ -126,7 +142,7 @@ cipher_alg_list(char sep, int auth_only)
8f2528
 	size_t nlen, rlen = 0;
8f2528
 	const struct sshcipher *c;
8f2528
 
8f2528
-	for (c = ciphers; c->name != NULL; c++) {
8f2528
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++) {
8f2528
 		if (c->number != SSH_CIPHER_SSH2)
8f2528
 			continue;
8f2528
 		if (auth_only && c->auth_len == 0)
8f2528
@@ -222,7 +238,7 @@ const struct sshcipher *
8f2528
 cipher_by_name(const char *name)
8f2528
 {
8f2528
 	const struct sshcipher *c;
8f2528
-	for (c = ciphers; c->name != NULL; c++)
8f2528
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
8f2528
 		if (strcmp(c->name, name) == 0)
8f2528
 			return c;
8f2528
 	return NULL;
8f2528
@@ -232,7 +248,7 @@ const struct sshcipher *
8f2528
 cipher_by_number(int id)
8f2528
 {
8f2528
 	const struct sshcipher *c;
8f2528
-	for (c = ciphers; c->name != NULL; c++)
8f2528
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
8f2528
 		if (c->number == id)
8f2528
 			return c;
8f2528
 	return NULL;
8f2528
@@ -273,7 +289,7 @@ cipher_number(const char *name)
8f2528
 	const struct sshcipher *c;
8f2528
 	if (name == NULL)
8f2528
 		return -1;
8f2528
-	for (c = ciphers; c->name != NULL; c++)
8f2528
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
8f2528
 		if (strcasecmp(c->name, name) == 0)
8f2528
 			return c->number;
8f2528
 	return -1;
8f2528
diff -up openssh-7.4p1/cipher-ctr.c.fips openssh-7.4p1/cipher-ctr.c
8f2528
--- openssh-7.4p1/cipher-ctr.c.fips	2017-02-09 14:53:47.125347498 +0100
8f2528
+++ openssh-7.4p1/cipher-ctr.c	2017-02-09 14:53:47.182347441 +0100
8f2528
@@ -179,7 +179,8 @@ evp_aes_128_ctr(void)
8f2528
 	aes_ctr.do_cipher = ssh_aes_ctr;
8f2528
 #ifndef SSH_OLD_EVP
8f2528
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
8f2528
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
8f2528
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
8f2528
+	    EVP_CIPH_FLAG_FIPS;
8f2528
 #endif
8f2528
 	return (&aes_ctr);
8f2528
 }
8f2528
diff -up openssh-7.4p1/clientloop.c.fips openssh-7.4p1/clientloop.c
8f2528
--- openssh-7.4p1/clientloop.c.fips	2017-05-30 19:10:26.537505598 +0200
8f2528
+++ openssh-7.4p1/clientloop.c	2017-05-30 19:10:26.571505583 +0200
8f2528
@@ -2452,7 +2452,7 @@ client_input_hostkeys(void)
8f2528
 		/* Check that the key is accepted in HostkeyAlgorithms */
8f2528
 		if (match_pattern_list(sshkey_ssh_name(key),
8f2528
 		    options.hostkeyalgorithms ? options.hostkeyalgorithms :
8f2528
-		    KEX_DEFAULT_PK_ALG, 0) != 1) {
8f2528
+		    (FIPS_mode() ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG), 0) != 1) {
8f2528
 			debug3("%s: %s key not permitted by HostkeyAlgorithms",
8f2528
 			    __func__, sshkey_ssh_name(key));
8f2528
 			continue;
8f2528
diff -up openssh-7.4p1/dh.h.fips openssh-7.4p1/dh.h
8f2528
--- openssh-7.4p1/dh.h.fips	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/dh.h	2017-02-09 14:53:47.182347441 +0100
8f2528
@@ -51,6 +51,7 @@ u_int	 dh_estimate(int);
8f2528
  * Miniumum increased in light of DH precomputation attacks.
8f2528
  */
8f2528
 #define DH_GRP_MIN	1024
8f2528
+#define DH_GRP_MIN_FIPS	2048
8f2528
 #define DH_GRP_MAX	8192
8f2528
 
8f2528
 /*
8f2528
diff -up openssh-7.4p1/entropy.c.fips openssh-7.4p1/entropy.c
8f2528
--- openssh-7.4p1/entropy.c.fips	2017-02-09 14:53:47.116347507 +0100
8f2528
+++ openssh-7.4p1/entropy.c	2017-02-09 14:53:47.182347441 +0100
8f2528
@@ -217,6 +217,9 @@ seed_rng(void)
8f2528
 		fatal("OpenSSL version mismatch. Built against %lx, you "
8f2528
 		    "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
8f2528
 
8f2528
+	/* clean the PRNG status when exiting the program */
8f2528
+	atexit(RAND_cleanup);
8f2528
+
8f2528
 #ifndef OPENSSL_PRNG_ONLY
8f2528
 	if (RAND_status() == 1) {
8f2528
 		debug3("RNG is ready, skipping seeding");
8f2528
diff -up openssh-7.4p1/kex.c.fips openssh-7.4p1/kex.c
8f2528
--- openssh-7.4p1/kex.c.fips	2017-02-09 14:53:47.174347449 +0100
8f2528
+++ openssh-7.4p1/kex.c	2017-02-09 14:53:47.183347440 +0100
8f2528
@@ -35,6 +35,7 @@
8f2528
 #ifdef WITH_OPENSSL
8f2528
 #include <openssl/crypto.h>
8f2528
 #include <openssl/dh.h>
8f2528
+#include <openssl/fips.h>
8f2528
 #endif
8f2528
 
8f2528
 #include "ssh2.h"
8f2528
@@ -124,6 +125,28 @@ static const struct kexalg kexalgs[] = {
8f2528
 	{ NULL, -1, -1, -1},
8f2528
 };
8f2528
 
8f2528
+static const struct kexalg kexalgs_fips[] = {
8f2528
+	{ KEX_DH14_SHA1, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
8f2528
+	{ KEX_DH14_SHA256, KEX_DH_GRP14_SHA256, 0, SSH_DIGEST_SHA256 },
8f2528
+	{ KEX_DH16_SHA512, KEX_DH_GRP16_SHA512, 0, SSH_DIGEST_SHA512 },
8f2528
+	{ KEX_DH18_SHA512, KEX_DH_GRP18_SHA512, 0, SSH_DIGEST_SHA512 },
8f2528
+	{ KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
8f2528
+#ifdef HAVE_EVP_SHA256
8f2528
+	{ KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
8f2528
+#endif
8f2528
+#ifdef OPENSSL_HAS_ECC
8f2528
+	{ KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2,
8f2528
+	    NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
8f2528
+	{ KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1,
8f2528
+	    SSH_DIGEST_SHA384 },
8f2528
+# ifdef OPENSSL_HAS_NISTP521
8f2528
+	{ KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
8f2528
+	    SSH_DIGEST_SHA512 },
8f2528
+# endif
8f2528
+#endif
8f2528
+	{ NULL, -1, -1, -1},
8f2528
+};
8f2528
+
8f2528
 char *
8f2528
 kex_alg_list(char sep)
8f2528
 {
8f2528
@@ -151,7 +169,7 @@ kex_alg_by_name(const char *name)
8f2528
 {
8f2528
 	const struct kexalg *k;
8f2528
 
8f2528
-	for (k = kexalgs; k->name != NULL; k++) {
8f2528
+	for (k = (FIPS_mode() ? kexalgs_fips : kexalgs); k->name != NULL; k++) {
8f2528
 		if (strcmp(k->name, name) == 0)
8f2528
 			return k;
8f2528
 #ifdef GSSAPI
8f2528
@@ -177,7 +195,10 @@ kex_names_valid(const char *names)
8f2528
 	for ((p = strsep(&cp, ",")); p && *p != '\0';
8f2528
 	    (p = strsep(&cp, ","))) {
8f2528
 		if (kex_alg_by_name(p) == NULL) {
8f2528
-			error("Unsupported KEX algorithm \"%.100s\"", p);
8f2528
+			if (FIPS_mode())
8f2528
+				error("\"%.100s\" is not allowed in FIPS mode", p);
8f2528
+			else
8f2528
+				error("Unsupported KEX algorithm \"%.100s\"", p);
8f2528
 			free(s);
8f2528
 			return 0;
8f2528
 		}
8f2528
diff -up openssh-7.4p1/kexgexc.c.fips openssh-7.4p1/kexgexc.c
8f2528
--- openssh-7.4p1/kexgexc.c.fips	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/kexgexc.c	2017-02-09 14:53:47.183347440 +0100
8f2528
@@ -28,6 +28,7 @@
8f2528
 
8f2528
 #ifdef WITH_OPENSSL
8f2528
 
8f2528
+#include <openssl/fips.h>
8f2528
 #include <sys/types.h>
8f2528
 
8f2528
 #include <openssl/dh.h>
8f2528
@@ -63,7 +64,7 @@ kexgex_client(struct ssh *ssh)
8f2528
 
8f2528
 	nbits = dh_estimate(kex->dh_need * 8);
8f2528
 
8f2528
-	kex->min = DH_GRP_MIN;
8f2528
+	kex->min = FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN;
8f2528
 	kex->max = DH_GRP_MAX;
8f2528
 	kex->nbits = nbits;
8f2528
 	if (datafellows & SSH_BUG_DHGEX_LARGE)
8f2528
diff -up openssh-7.4p1/kexgexs.c.fips openssh-7.4p1/kexgexs.c
8f2528
--- openssh-7.4p1/kexgexs.c.fips	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/kexgexs.c	2017-02-09 14:53:47.183347440 +0100
8f2528
@@ -83,9 +83,9 @@ input_kex_dh_gex_request(int type, u_int
8f2528
 	kex->nbits = nbits;
8f2528
 	kex->min = min;
8f2528
 	kex->max = max;
8f2528
-	min = MAXIMUM(DH_GRP_MIN, min);
8f2528
+	min = MAXIMUM(FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN, min);
8f2528
 	max = MINIMUM(DH_GRP_MAX, max);
8f2528
-	nbits = MAXIMUM(DH_GRP_MIN, nbits);
8f2528
+	nbits = MAXIMUM(FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN, nbits);
8f2528
 	nbits = MINIMUM(DH_GRP_MAX, nbits);
8f2528
 
8f2528
 	if (kex->max < kex->min || kex->nbits < kex->min ||
8f2528
diff -up openssh-7.4p1/mac.c.fips openssh-7.4p1/mac.c
8f2528
--- openssh-7.4p1/mac.c.fips	2017-02-09 14:53:47.175347448 +0100
8f2528
+++ openssh-7.4p1/mac.c	2017-02-09 14:53:47.183347440 +0100
8f2528
@@ -27,6 +27,8 @@
8f2528
 
8f2528
 #include <sys/types.h>
8f2528
 
8f2528
+#include <openssl/fips.h>
8f2528
+
8f2528
 #include <string.h>
8f2528
 #include <stdio.h>
8f2528
 
8f2528
@@ -54,7 +56,7 @@ struct macalg {
8f2528
 	int		etm;		/* Encrypt-then-MAC */
8f2528
 };
8f2528
 
8f2528
-static const struct macalg macs[] = {
8f2528
+static const struct macalg all_macs[] = {
8f2528
 	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
8f2528
 	{ "hmac-sha1",				SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
8f2528
 	{ "hmac-sha1-96",			SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 },
8f2528
@@ -89,6 +91,24 @@ static const struct macalg macs[] = {
8f2528
 	{ NULL,					0, 0, 0, 0, 0, 0 }
8f2528
 };
8f2528
 
8f2528
+static const struct macalg fips_macs[] = {
8f2528
+	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
8f2528
+	{ "hmac-sha1",				SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
8f2528
+#ifdef HAVE_EVP_SHA256
8f2528
+	{ "hmac-sha2-256",			SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 },
8f2528
+	{ "hmac-sha2-512",			SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 },
8f2528
+#endif
8f2528
+
8f2528
+	/* Encrypt-then-MAC variants */
8f2528
+	{ "hmac-sha1-etm@openssh.com",		SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 },
8f2528
+#ifdef HAVE_EVP_SHA256
8f2528
+	{ "hmac-sha2-256-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 },
8f2528
+	{ "hmac-sha2-512-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 },
8f2528
+#endif
8f2528
+
8f2528
+	{ NULL,					0, 0, 0, 0, 0, 0 }
8f2528
+};
8f2528
+
8f2528
 /* Returns a list of supported MACs separated by the specified char. */
8f2528
 char *
8f2528
 mac_alg_list(char sep)
8f2528
@@ -97,7 +117,7 @@ mac_alg_list(char sep)
8f2528
 	size_t nlen, rlen = 0;
8f2528
 	const struct macalg *m;
8f2528
 
8f2528
-	for (m = macs; m->name != NULL; m++) {
8f2528
+	for (m = FIPS_mode() ? fips_macs : all_macs; m->name != NULL; m++) {
8f2528
 		if (ret != NULL)
8f2528
 			ret[rlen++] = sep;
8f2528
 		nlen = strlen(m->name);
8f2528
@@ -136,7 +156,7 @@ mac_setup(struct sshmac *mac, char *name
8f2528
 {
8f2528
 	const struct macalg *m;
8f2528
 
8f2528
-	for (m = macs; m->name != NULL; m++) {
8f2528
+	for (m = FIPS_mode() ? fips_macs : all_macs; m->name != NULL; m++) {
8f2528
 		if (strcmp(name, m->name) != 0)
8f2528
 			continue;
8f2528
 		if (mac != NULL)
8f2528
diff -up openssh-7.4p1/Makefile.in.fips openssh-7.4p1/Makefile.in
8f2528
--- openssh-7.4p1/Makefile.in.fips	2017-02-09 14:53:47.175347448 +0100
8f2528
+++ openssh-7.4p1/Makefile.in	2017-02-09 14:53:47.184347440 +0100
8f2528
@@ -168,25 +168,25 @@ libssh.a: $(LIBSSH_OBJS)
8f2528
 	$(RANLIB) $@
8f2528
 
8f2528
 ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
8f2528
-	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS) $(GSSLIBS)
8f2528
+	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHLIBS) $(LIBS) $(GSSLIBS)
8f2528
 
8f2528
 sshd$(EXEEXT): libssh.a	$(LIBCOMPAT) $(SSHDOBJS)
8f2528
-	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
8f2528
+	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
8f2528
 
8f2528
 scp$(EXEEXT): $(LIBCOMPAT) libssh.a scp.o progressmeter.o
8f2528
 	$(LD) -o $@ scp.o progressmeter.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
8f2528
 
8f2528
 ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o
8f2528
-	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
8f2528
+	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
8f2528
 
8f2528
 ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
8f2528
-	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
8f2528
+	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
8f2528
 
8f2528
 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
8f2528
-	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
8f2528
+	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
8f2528
 
8f2528
 ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o readconf.o
8f2528
-	$(LD) -o $@ ssh-keysign.o readconf.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
8f2528
+	$(LD) -o $@ ssh-keysign.o readconf.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
8f2528
 
8f2528
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
8f2528
 	$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
8f2528
@@ -205,7 +205,7 @@ ssh-cavs$(EXEEXT): $(LIBCOMPAT) libssh.a
8f2528
 	$(LD) -o $@ ssh-cavs.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
8f2528
 
8f2528
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
8f2528
-	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
8f2528
+	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
8f2528
 
8f2528
 sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o
8f2528
 	$(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
8f2528
diff -up openssh-7.4p1/myproposal.h.fips openssh-7.4p1/myproposal.h
8f2528
--- openssh-7.4p1/myproposal.h.fips	2017-05-30 19:10:26.535505599 +0200
8f2528
+++ openssh-7.4p1/myproposal.h	2017-05-30 19:10:26.574505582 +0200
8f2528
@@ -119,6 +119,14 @@
8f2528
 	"ssh-rsa," \
8f2528
 	"ssh-dss"
8f2528
 
8f2528
+#define	KEX_FIPS_PK_ALG	\
8f2528
+	HOSTKEY_ECDSA_CERT_METHODS \
8f2528
+	"ssh-rsa-cert-v01@openssh.com," \
8f2528
+	HOSTKEY_ECDSA_METHODS \
8f2528
+	"rsa-sha2-512," \
8f2528
+	"rsa-sha2-256," \
8f2528
+	"ssh-rsa"
8f2528
+
8f2528
 /* the actual algorithms */
8f2528
 
8f2528
 #define KEX_CLIENT_ENCRYPT \
8f2528
@@ -144,6 +152,37 @@
8f2528
 
8f2528
 #define KEX_CLIENT_MAC KEX_SERVER_MAC
8f2528
 
8f2528
+#define	KEX_FIPS_ENCRYPT \
8f2528
+	"aes128-ctr,aes192-ctr,aes256-ctr," \
8f2528
+	"aes128-cbc,3des-cbc," \
8f2528
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se"
8f2528
+#ifdef HAVE_EVP_SHA256
8f2528
+# define KEX_DEFAULT_KEX_FIPS		\
8f2528
+	KEX_ECDH_METHODS \
8f2528
+	KEX_SHA2_METHODS \
8f2528
+	"diffie-hellman-group14-sha256"
8f2528
+# define KEX_FIPS_MAC \
8f2528
+	"hmac-sha1," \
8f2528
+	"hmac-sha2-256," \
8f2528
+	"hmac-sha2-512," \
8f2528
+	"hmac-sha1-etm@openssh.com," \
8f2528
+	"hmac-sha2-256-etm@openssh.com," \
8f2528
+	"hmac-sha2-512-etm@openssh.com"
8f2528
+#else
8f2528
+# ifdef OPENSSL_HAS_NISTP521
8f2528
+#  define KEX_DEFAULT_KEX_FIPS		\
8f2528
+	"ecdh-sha2-nistp256," \
8f2528
+	"ecdh-sha2-nistp384," \
8f2528
+	"ecdh-sha2-nistp521"
8f2528
+# else
8f2528
+#  define KEX_DEFAULT_KEX_FIPS		\
8f2528
+	"ecdh-sha2-nistp256," \
8f2528
+	"ecdh-sha2-nistp384"
8f2528
+# endif
8f2528
+#define        KEX_FIPS_MAC \
8f2528
+       "hmac-sha1"
8f2528
+#endif
8f2528
+
8f2528
 #else /* WITH_OPENSSL */
8f2528
 
8f2528
 #define KEX_SERVER_KEX		\
8f2528
diff -up openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c.fips openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c
8f2528
--- openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c.fips	2017-02-09 14:53:47.184347440 +0100
8f2528
+++ openssh-7.4p1/pam_ssh_agent_auth-0.10.3/pam_user_key_allowed2.c	2017-02-09 14:55:25.123250447 +0100
8f2528
@@ -102,7 +102,8 @@ pamsshagentauth_check_authkeys_file(FILE
8f2528
             found_key = 1;
8f2528
             logit("matching key found: file/command %s, line %lu", file,
8f2528
                                   linenum);
8f2528
-            fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
8f2528
+            fp = sshkey_fingerprint(found, FIPS_mode() ? SSH_DIGEST_SHA1 : SSH_DIGEST_MD5,
8f2528
+				SSH_FP_HEX);
8f2528
             logit("Found matching %s key: %s",
8f2528
                                   key_type(found), fp);
8f2528
             free(fp);
8f2528
diff -up openssh-7.4p1/readconf.c.fips openssh-7.4p1/readconf.c
8f2528
--- openssh-7.4p1/readconf.c.fips	2017-02-09 14:53:47.185347438 +0100
8f2528
+++ openssh-7.4p1/readconf.c	2017-02-09 14:56:24.840191308 +0100
8f2528
@@ -2104,12 +2104,17 @@ fill_default_options(Options * options)
8f2528
 	}
8f2528
 	if (options->update_hostkeys == -1)
8f2528
 		options->update_hostkeys = 0;
8f2528
-	if (kex_assemble_names(KEX_CLIENT_ENCRYPT, &options->ciphers) != 0 ||
8f2528
-	    kex_assemble_names(KEX_CLIENT_MAC, &options->macs) != 0 ||
8f2528
-	    kex_assemble_names(KEX_CLIENT_KEX, &options->kex_algorithms) != 0 ||
8f2528
-	    kex_assemble_names(KEX_DEFAULT_PK_ALG,
8f2528
+	if (kex_assemble_names((FIPS_mode() ? KEX_FIPS_ENCRYPT
8f2528
+	        : KEX_CLIENT_ENCRYPT), &options->ciphers) != 0 ||
8f2528
+	    kex_assemble_names((FIPS_mode() ? KEX_FIPS_MAC
8f2528
+	        : KEX_CLIENT_MAC), &options->macs) != 0 ||
8f2528
+	    kex_assemble_names((FIPS_mode() ? KEX_DEFAULT_KEX_FIPS
8f2528
+	        : KEX_CLIENT_KEX), &options->kex_algorithms) != 0 ||
8f2528
+	    kex_assemble_names((FIPS_mode() ? KEX_FIPS_PK_ALG
8f2528
+	        : KEX_DEFAULT_PK_ALG),
8f2528
 	    &options->hostbased_key_types) != 0 ||
8f2528
-	    kex_assemble_names(KEX_DEFAULT_PK_ALG,
8f2528
+	    kex_assemble_names((FIPS_mode() ? KEX_FIPS_PK_ALG
8f2528
+	        : KEX_DEFAULT_PK_ALG),
8f2528
 	    &options->pubkey_key_types) != 0)
8f2528
 		fatal("%s: kex_assemble_names failed", __func__);
8f2528
 
8f2528
@@ -2559,7 +2564,8 @@ dump_client_config(Options *o, const cha
8f2528
 	char buf[8];
8f2528
 
8f2528
 	/* This is normally prepared in ssh_kex2 */
8f2528
-	if (kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->hostkeyalgorithms) != 0)
8f2528
+	if (kex_assemble_names((FIPS_mode() ? KEX_FIPS_PK_ALG
8f2528
+	    : KEX_DEFAULT_PK_ALG), &o->hostkeyalgorithms) != 0)
8f2528
 		fatal("%s: kex_assemble_names failed", __func__);
8f2528
 
8f2528
 	/* Most interesting options first: user, host, port */
8f2528
diff -up openssh-7.4p1/sandbox-seccomp-filter.c.fips openssh-7.4p1/sandbox-seccomp-filter.c
8f2528
--- openssh-7.4p1/sandbox-seccomp-filter.c.fips	2017-02-09 14:53:47.177347446 +0100
8f2528
+++ openssh-7.4p1/sandbox-seccomp-filter.c	2017-02-09 14:53:47.185347438 +0100
8f2528
@@ -118,6 +118,9 @@ static const struct sock_filter preauth_
8f2528
 #ifdef __NR_open
8f2528
 	SC_DENY(open, EACCES),
8f2528
 #endif
8f2528
+#ifdef __NR_socket
8f2528
+	SC_DENY(socket, EACCES),
8f2528
+#endif
8f2528
 #ifdef __NR_openat
8f2528
 	SC_DENY(openat, EACCES),
8f2528
 #endif
8f2528
diff -up openssh-7.4p1/servconf.c.fips openssh-7.4p1/servconf.c
8f2528
--- openssh-7.4p1/servconf.c.fips	2017-06-07 13:07:28.403983349 +0200
8f2528
+++ openssh-7.4p1/servconf.c	2017-06-07 13:09:46.710997099 +0200
8f2528
@@ -185,14 +185,20 @@ option_clear_or_none(const char *o)
8f2528
 static void
8f2528
 assemble_algorithms(ServerOptions *o)
8f2528
 {
8f2528
-	if (kex_assemble_names(KEX_SERVER_ENCRYPT, &o->ciphers) != 0 ||
8f2528
-	    kex_assemble_names(KEX_SERVER_MAC, &o->macs) != 0 ||
8f2528
-	    kex_assemble_names(KEX_SERVER_KEX, &o->kex_algorithms) != 0 ||
8f2528
-	    kex_assemble_names(KEX_DEFAULT_PK_ALG,
8f2528
+	if (kex_assemble_names((FIPS_mode() ? KEX_FIPS_ENCRYPT
8f2528
+	        : KEX_SERVER_ENCRYPT), &o->ciphers) != 0 ||
8f2528
+	    kex_assemble_names((FIPS_mode() ? KEX_FIPS_MAC
8f2528
+	        : KEX_SERVER_MAC), &o->macs) != 0 ||
8f2528
+	    kex_assemble_names((FIPS_mode() ? KEX_DEFAULT_KEX_FIPS
8f2528
+	        : KEX_SERVER_KEX), &o->kex_algorithms) != 0 ||
8f2528
+	    kex_assemble_names((FIPS_mode() ? KEX_FIPS_PK_ALG
8f2528
+	        : KEX_DEFAULT_PK_ALG),
8f2528
 	    &o->hostkeyalgorithms) != 0 ||
8f2528
-	    kex_assemble_names(KEX_DEFAULT_PK_ALG,
8f2528
+	    kex_assemble_names((FIPS_mode() ? KEX_FIPS_PK_ALG
8f2528
+	        : KEX_DEFAULT_PK_ALG),
8f2528
 	    &o->hostbased_key_types) != 0 ||
8f2528
-	    kex_assemble_names(KEX_DEFAULT_PK_ALG, &o->pubkey_key_types) != 0)
8f2528
+	    kex_assemble_names((FIPS_mode() ? KEX_FIPS_PK_ALG
8f2528
+	        : KEX_DEFAULT_PK_ALG), &o->pubkey_key_types) != 0)
8f2528
 		fatal("kex_assemble_names failed");
8f2528
 }
8f2528
 
8f2528
@@ -2390,8 +2396,10 @@ dump_config(ServerOptions *o)
8f2528
 	/* string arguments */
8f2528
 	dump_cfg_string(sPidFile, o->pid_file);
8f2528
 	dump_cfg_string(sXAuthLocation, o->xauth_location);
8f2528
-	dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : KEX_SERVER_ENCRYPT);
8f2528
-	dump_cfg_string(sMacs, o->macs ? o->macs : KEX_SERVER_MAC);
8f2528
+	dump_cfg_string(sCiphers, o->ciphers ? o->ciphers : FIPS_mode()
8f2528
+		? KEX_FIPS_ENCRYPT : KEX_SERVER_ENCRYPT);
8f2528
+	dump_cfg_string(sMacs, o->macs ? o->macs : FIPS_mode()
8f2528
+		? KEX_FIPS_MAC : KEX_SERVER_MAC);
8f2528
 	dump_cfg_string(sBanner, o->banner == NULL ? "none" : o->banner);
8f2528
 	dump_cfg_string(sForceCommand, o->adm_forced_command);
8f2528
 	dump_cfg_string(sChrootDirectory, o->chroot_directory);
8f2528
@@ -2406,14 +2414,17 @@ dump_config(ServerOptions *o)
8f2528
 	dump_cfg_string(sAuthorizedPrincipalsCommand, o->authorized_principals_command);
8f2528
 	dump_cfg_string(sAuthorizedPrincipalsCommandUser, o->authorized_principals_command_user);
8f2528
 	dump_cfg_string(sHostKeyAgent, o->host_key_agent);
8f2528
-	dump_cfg_string(sKexAlgorithms,
8f2528
-	    o->kex_algorithms ? o->kex_algorithms : KEX_SERVER_KEX);
8f2528
+	dump_cfg_string(sKexAlgorithms, o->kex_algorithms ? o->kex_algorithms :
8f2528
+		FIPS_mode() ? KEX_DEFAULT_KEX_FIPS : KEX_SERVER_KEX);
8f2528
 	dump_cfg_string(sHostbasedAcceptedKeyTypes, o->hostbased_key_types ?
8f2528
-	    o->hostbased_key_types : KEX_DEFAULT_PK_ALG);
8f2528
+	    o->hostbased_key_types : (FIPS_mode() ? KEX_FIPS_PK_ALG
8f2528
+	        : KEX_DEFAULT_PK_ALG));
8f2528
 	dump_cfg_string(sHostKeyAlgorithms, o->hostkeyalgorithms ?
8f2528
-	    o->hostkeyalgorithms : KEX_DEFAULT_PK_ALG);
8f2528
+	    o->hostkeyalgorithms : (FIPS_mode() ? KEX_FIPS_PK_ALG
8f2528
+	        : KEX_DEFAULT_PK_ALG));
8f2528
 	dump_cfg_string(sPubkeyAcceptedKeyTypes, o->pubkey_key_types ?
8f2528
-	    o->pubkey_key_types : KEX_DEFAULT_PK_ALG);
8f2528
+	    o->pubkey_key_types : (FIPS_mode() ? KEX_FIPS_PK_ALG
8f2528
+	        : KEX_DEFAULT_PK_ALG));
8f2528
 
8f2528
 	/* string arguments requiring a lookup */
8f2528
 	dump_cfg_string(sLogLevel, log_level_name(o->log_level));
8f2528
diff -up openssh-7.4p1/ssh.c.fips openssh-7.4p1/ssh.c
8f2528
--- openssh-7.4p1/ssh.c.fips	2016-12-19 05:59:41.000000000 +0100
8f2528
+++ openssh-7.4p1/ssh.c	2017-02-09 14:53:47.185347438 +0100
8f2528
@@ -76,6 +76,8 @@
8f2528
 #include <openssl/evp.h>
8f2528
 #include <openssl/err.h>
8f2528
 #endif
8f2528
+#include <openssl/fips.h>
8f2528
+#include <fipscheck.h>
8f2528
 #include "openbsd-compat/openssl-compat.h"
8f2528
 #include "openbsd-compat/sys-queue.h"
8f2528
 
8f2528
@@ -530,6 +532,14 @@ main(int ac, char **av)
8f2528
 	sanitise_stdfd();
8f2528
 
8f2528
 	__progname = ssh_get_progname(av[0]);
8f2528
+        SSLeay_add_all_algorithms();
8f2528
+	if (access("/etc/system-fips", F_OK) == 0)
8f2528
+		if (! FIPSCHECK_verify(NULL, NULL)){
8f2528
+			if (FIPS_mode())
8f2528
+				fatal("FIPS integrity verification test failed.");
8f2528
+			else
8f2528
+				logit("FIPS integrity verification test failed.");
8f2528
+	}
8f2528
 
8f2528
 #ifndef HAVE_SETPROCTITLE
8f2528
 	/* Prepare for later setproctitle emulation */
8f2528
@@ -609,6 +619,9 @@ main(int ac, char **av)
8f2528
 	    "ACD:E:F:GI:J:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) {
8f2528
 		switch (opt) {
8f2528
 		case '1':
8f2528
+			if (FIPS_mode()) {
8f2528
+				fatal("Protocol 1 not allowed in the FIPS mode.");
8f2528
+			}
8f2528
 			options.protocol = SSH_PROTO_1;
8f2528
 			break;
8f2528
 		case '2':
8f2528
@@ -964,7 +977,6 @@ main(int ac, char **av)
8f2528
 	host_arg = xstrdup(host);
8f2528
 
8f2528
 #ifdef WITH_OPENSSL
8f2528
-	OpenSSL_add_all_algorithms();
8f2528
 	ERR_load_crypto_strings();
8f2528
 #endif
8f2528
 
8f2528
@@ -1175,6 +1187,10 @@ main(int ac, char **av)
8f2528
 
8f2528
 	seed_rng();
8f2528
 
8f2528
+	if (FIPS_mode()) {
8f2528
+		logit("FIPS mode initialized");
8f2528
+	}
8f2528
+
8f2528
 	if (options.user == NULL)
8f2528
 		options.user = xstrdup(pw->pw_name);
8f2528
 
8f2528
@@ -1263,6 +1279,12 @@ main(int ac, char **av)
8f2528
 
8f2528
 	timeout_ms = options.connection_timeout * 1000;
8f2528
 
8f2528
+	if (FIPS_mode()) {
8f2528
+		options.protocol &= SSH_PROTO_2;
8f2528
+		if (options.protocol == 0)
8f2528
+			fatal("Protocol 2 disabled by configuration but required in the FIPS mode.");
8f2528
+	}
8f2528
+
8f2528
 	/* Open a connection to the remote host. */
8f2528
 	if (ssh_connect(host, addrs, &hostaddr, options.port,
8f2528
 	    options.address_family, options.connection_attempts,
8f2528
diff -up openssh-7.4p1/sshconnect2.c.fips openssh-7.4p1/sshconnect2.c
8f2528
--- openssh-7.4p1/sshconnect2.c.fips	2017-02-09 14:53:47.162347461 +0100
8f2528
+++ openssh-7.4p1/sshconnect2.c	2017-02-09 14:53:47.186347437 +0100
8f2528
@@ -44,6 +44,8 @@
8f2528
 #include <vis.h>
8f2528
 #endif
8f2528
 
8f2528
+#include <openssl/fips.h>
8f2528
+
8f2528
 #include "openbsd-compat/sys-queue.h"
8f2528
 
8f2528
 #include "xmalloc.h"
8f2528
@@ -117,7 +119,8 @@ order_hostkeyalgs(char *host, struct soc
8f2528
 	for (i = 0; i < options.num_system_hostfiles; i++)
8f2528
 		load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
8f2528
 
8f2528
-	oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
8f2528
+	oavail = avail = xstrdup((FIPS_mode()
8f2528
+	    ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG));
8f2528
 	maxlen = strlen(avail) + 1;
8f2528
 	first = xmalloc(maxlen);
8f2528
 	last = xmalloc(maxlen);
8f2528
@@ -172,21 +175,26 @@ ssh_kex2(char *host, struct sockaddr *ho
8f2528
 
8f2528
 #ifdef GSSAPI
8f2528
 	if (options.gss_keyex) {
8f2528
-		/* Add the GSSAPI mechanisms currently supported on this 
8f2528
-		 * client to the key exchange algorithm proposal */
8f2528
-		orig = options.kex_algorithms;
8f2528
-
8f2528
-		if (options.gss_trust_dns)
8f2528
-			gss_host = (char *)get_canonical_hostname(active_state, 1);
8f2528
-		else
8f2528
-			gss_host = host;
8f2528
-
8f2528
-		gss = ssh_gssapi_client_mechanisms(gss_host,
8f2528
-		    options.gss_client_identity, options.gss_kex_algorithms);
8f2528
-		if (gss) {
8f2528
-			debug("Offering GSSAPI proposal: %s", gss);
8f2528
-			xasprintf(&options.kex_algorithms,
8f2528
-			    "%s,%s", gss, orig);
8f2528
+		if (FIPS_mode()) {
8f2528
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
8f2528
+			options.gss_keyex = 0;
8f2528
+		} else {
8f2528
+			/* Add the GSSAPI mechanisms currently supported on this
8f2528
+			 * client to the key exchange algorithm proposal */
8f2528
+			orig = options.kex_algorithms;
8f2528
+
8f2528
+			if (options.gss_trust_dns)
8f2528
+				gss_host = (char *)get_canonical_hostname(active_state, 1);
8f2528
+			else
8f2528
+				gss_host = host;
8f2528
+
8f2528
+			gss = ssh_gssapi_client_mechanisms(gss_host,
8f2528
+			    options.gss_client_identity, options.gss_kex_algorithms);
8f2528
+			if (gss) {
8f2528
+				debug("Offering GSSAPI proposal: %s", gss);
8f2528
+				xasprintf(&options.kex_algorithms,
8f2528
+				    "%s,%s", gss, orig);
8f2528
+			}
8f2528
 		}
8f2528
 	}
8f2528
 #endif
8f2528
@@ -204,14 +212,16 @@ ssh_kex2(char *host, struct sockaddr *ho
8f2528
 	myproposal[PROPOSAL_MAC_ALGS_CTOS] =
8f2528
 	    myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
8f2528
 	if (options.hostkeyalgorithms != NULL) {
8f2528
-		if (kex_assemble_names(KEX_DEFAULT_PK_ALG,
8f2528
+		if (kex_assemble_names((FIPS_mode() ? KEX_FIPS_PK_ALG
8f2528
+		    : KEX_DEFAULT_PK_ALG),
8f2528
 		    &options.hostkeyalgorithms) != 0)
8f2528
 			fatal("%s: kex_assemble_namelist", __func__);
8f2528
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
8f2528
 		    compat_pkalg_proposal(options.hostkeyalgorithms);
8f2528
 	} else {
8f2528
 		/* Enforce default */
8f2528
-		options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
8f2528
+		options.hostkeyalgorithms = xstrdup((FIPS_mode()
8f2528
+		    ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG));
8f2528
 		/* Prefer algorithms that we already have keys for */
8f2528
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
8f2528
 		    compat_pkalg_proposal(
8f2528
diff -up openssh-7.4p1/sshd.c.fips openssh-7.4p1/sshd.c
8f2528
--- openssh-7.4p1/sshd.c.fips	2017-02-09 14:53:47.178347445 +0100
8f2528
+++ openssh-7.4p1/sshd.c	2017-02-09 14:53:47.186347437 +0100
8f2528
@@ -66,6 +66,7 @@
8f2528
 #include <grp.h>
8f2528
 #include <pwd.h>
8f2528
 #include <signal.h>
8f2528
+#include <syslog.h>
8f2528
 #include <stdarg.h>
8f2528
 #include <stdio.h>
8f2528
 #include <stdlib.h>
8f2528
@@ -77,6 +78,8 @@
8f2528
 #include <openssl/dh.h>
8f2528
 #include <openssl/bn.h>
8f2528
 #include <openssl/rand.h>
8f2528
+#include <openssl/fips.h>
8f2528
+#include <fipscheck.h>
8f2528
 #include "openbsd-compat/openssl-compat.h"
8f2528
 #endif
8f2528
 
8f2528
@@ -1471,6 +1474,18 @@ main(int ac, char **av)
8f2528
 #endif
8f2528
 	__progname = ssh_get_progname(av[0]);
8f2528
 
8f2528
+        SSLeay_add_all_algorithms();
8f2528
+	if (access("/etc/system-fips", F_OK) == 0)
8f2528
+		if (! FIPSCHECK_verify(NULL, NULL)) {
8f2528
+			openlog(__progname, LOG_PID, LOG_AUTHPRIV);
8f2528
+			if (FIPS_mode()) {
8f2528
+				syslog(LOG_CRIT, "FIPS integrity verification test failed.");
8f2528
+				cleanup_exit(255);
8f2528
+			}
8f2528
+			else
8f2528
+				syslog(LOG_INFO, "FIPS integrity verification test failed.");
8f2528
+			closelog();
8f2528
+		}
8f2528
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
8f2528
 	saved_argc = ac;
8f2528
 	rexec_argc = ac;
8f2528
@@ -1619,7 +1634,7 @@ main(int ac, char **av)
8f2528
 	else
8f2528
 		closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
8f2528
 
8f2528
-#ifdef WITH_OPENSSL
8f2528
+#if 0 /* FIPS */
8f2528
 	OpenSSL_add_all_algorithms();
8f2528
 #endif
8f2528
 
8f2528
@@ -1928,6 +1943,10 @@ main(int ac, char **av)
8f2528
 	/* Reinitialize the log (because of the fork above). */
8f2528
 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
8f2528
 
8f2528
+	if (FIPS_mode()) {
8f2528
+		logit("FIPS mode initialized");
8f2528
+	}
8f2528
+
8f2528
 	/* Chdir to the root directory so that the current disk can be
8f2528
 	   unmounted if desired. */
8f2528
 	if (chdir("/") == -1)
8f2528
@@ -2282,10 +2301,14 @@ do_ssh2_kex(void)
8f2528
 	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
8f2528
 		orig = NULL;
8f2528
 
8f2528
-	if (options.gss_keyex)
8f2528
-		gss = ssh_gssapi_server_mechanisms();
8f2528
-	else
8f2528
-		gss = NULL;
8f2528
+	if (options.gss_keyex) {
8f2528
+		if (FIPS_mode()) {
8f2528
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
8f2528
+			options.gss_keyex = 0;
8f2528
+		} else {
8f2528
+			gss = ssh_gssapi_server_mechanisms();
8f2528
+		}
8f2528
+	}
8f2528
 
8f2528
 	if (gss && orig)
8f2528
 		xasprintf(&newstr, "%s,%s", gss, orig);
8f2528
diff -up openssh-7.4p1/sshkey.c.fips openssh-7.4p1/sshkey.c
8f2528
--- openssh-7.4p1/sshkey.c.fips	2017-02-09 14:53:47.179347444 +0100
8f2528
+++ openssh-7.4p1/sshkey.c	2017-02-09 14:58:02.117094971 +0100
8f2528
@@ -34,6 +34,7 @@
8f2528
 #include <openssl/evp.h>
8f2528
 #include <openssl/err.h>
8f2528
 #include <openssl/pem.h>
8f2528
+#include <openssl/fips.h>
8f2528
 #endif
8f2528
 
8f2528
 #include "crypto_api.h"
8f2528
@@ -57,6 +58,7 @@
8f2528
 #include "sshkey.h"
8f2528
 #include "match.h"
8f2528
 #include "xmalloc.h"
8f2528
+#include "log.h"
8f2528
 
8f2528
 /* openssh private key file format */
8f2528
 #define MARK_BEGIN		"-----BEGIN OPENSSH PRIVATE KEY-----\n"
8f2528
@@ -1555,6 +1557,8 @@ rsa_generate_private_key(u_int bits, RSA
8f2528
 	}
8f2528
 	if (!BN_set_word(f4, RSA_F4) ||
8f2528
 	    !RSA_generate_key_ex(private, bits, f4, NULL)) {
8f2528
+			if (FIPS_mode())
8f2528
+				logit("%s: the key length might be unsupported by FIPS mode approved key generation method", __func__);
8f2528
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
8f2528
 		goto out;
8f2528
 	}
8f2528
@@ -3921,8 +3925,11 @@ sshkey_parse_private_fileblob_type(struc
8f2528
 	switch (type) {
8f2528
 #ifdef WITH_SSH1
8f2528
 	case KEY_RSA1:
8f2528
-		return sshkey_parse_private_rsa1(blob, passphrase,
8f2528
-		    keyp, commentp);
8f2528
+		if (! FIPS_mode())
8f2528
+			return sshkey_parse_private_rsa1(blob, passphrase,
8f2528
+			    keyp, commentp);
8f2528
+		error("%s: cannot parse rsa1 key in FIPS mode", __func__);
8f2528
+		return SSH_ERR_KEY_TYPE_UNKNOWN;
8f2528
 #endif /* WITH_SSH1 */
8f2528
 #ifdef WITH_OPENSSL
8f2528
 	case KEY_DSA:
8f2528
@@ -3961,8 +3968,9 @@ sshkey_parse_private_fileblob(struct ssh
8f2528
 #ifdef WITH_SSH1
8f2528
 	/* it's a SSH v1 key if the public key part is readable */
8f2528
 	if (sshkey_parse_public_rsa1_fileblob(buffer, NULL, NULL) == 0) {
8f2528
-		return sshkey_parse_private_fileblob_type(buffer, KEY_RSA1,
8f2528
-		    passphrase, keyp, commentp);
8f2528
+		if (!FIPS_mode())
8f2528
+			return sshkey_parse_private_fileblob_type(buffer, KEY_RSA1,
8f2528
+			    passphrase, keyp, commentp);
8f2528
 	}
8f2528
 #endif /* WITH_SSH1 */
8f2528
 	return sshkey_parse_private_fileblob_type(buffer, KEY_UNSPEC,
8f2528
diff -up openssh-7.4p1/ssh-keygen.c.fips openssh-7.4p1/ssh-keygen.c
8f2528
--- openssh-7.4p1/ssh-keygen.c.fips	2017-05-22 13:50:06.731776762 +0200
8f2528
+++ openssh-7.4p1/ssh-keygen.c	2017-05-22 13:50:11.843773909 +0200
8f2528
@@ -215,6 +215,12 @@ type_bits_valid(int type, const char *na
8f2528
 	    OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
8f2528
 	if (*bitsp > maxbits)
8f2528
 		fatal("key bits exceeds maximum %d", maxbits);
8f2528
+	if (FIPS_mode()) {
8f2528
+		if (type == KEY_DSA)
8f2528
+			fatal("DSA keys are not allowed in FIPS mode");
8f2528
+		if (type == KEY_ED25519)
8f2528
+			fatal("ED25519 keys are not allowed in FIPS mode");
8f2528
+	}
8f2528
 	if (type == KEY_DSA && *bitsp != 1024)
8f2528
 		fatal("DSA keys must be 1024 bits");
8f2528
 	else if (type != KEY_ECDSA && type != KEY_ED25519 && *bitsp < 1024)