rcolebaugh / rpms / openssh

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