vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
f5835d
diff -up openssh-7.7p1/cipher.c.fips openssh-7.7p1/cipher.c
f5835d
--- openssh-7.7p1/cipher.c.fips	2018-08-08 10:08:40.814719906 +0200
f5835d
+++ openssh-7.7p1/cipher.c	2018-08-08 10:08:40.821719965 +0200
f5835d
@@ -39,6 +39,8 @@
f5835d
 
f5835d
 #include <sys/types.h>
f5835d
 
f5835d
+#include <openssl/fips.h>
f5835d
+
f5835d
 #include <string.h>
f5835d
 #include <stdarg.h>
f5835d
 #include <stdio.h>
f5835d
@@ -90,6 +92,33 @@ static const struct sshcipher ciphers[]
f5835d
 	{ NULL,			0, 0, 0, 0, 0, NULL }
f5835d
 };
f5835d
 
f5835d
+static const struct sshcipher fips_ciphers[] = {
f5835d
+#ifdef WITH_OPENSSL
f5835d
+	{ "3des-cbc",		8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
f5835d
+	{ "aes128-cbc",		16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
f5835d
+	{ "aes192-cbc",		16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
f5835d
+	{ "aes256-cbc",		16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
f5835d
+	{ "rijndael-cbc@lysator.liu.se",
f5835d
+				16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
f5835d
+	{ "aes128-ctr",		16, 16, 0, 0, 0, EVP_aes_128_ctr },
f5835d
+	{ "aes192-ctr",		16, 24, 0, 0, 0, EVP_aes_192_ctr },
f5835d
+	{ "aes256-ctr",		16, 32, 0, 0, 0, EVP_aes_256_ctr },
f5835d
+# ifdef OPENSSL_HAVE_EVPGCM
f5835d
+	{ "aes128-gcm@openssh.com",
f5835d
+				16, 16, 12, 16, 0, EVP_aes_128_gcm },
f5835d
+	{ "aes256-gcm@openssh.com",
f5835d
+				16, 32, 12, 16, 0, EVP_aes_256_gcm },
f5835d
+# endif /* OPENSSL_HAVE_EVPGCM */
f5835d
+#else
f5835d
+	{ "aes128-ctr",		16, 16, 0, 0, CFLAG_AESCTR, NULL },
f5835d
+	{ "aes192-ctr",		16, 24, 0, 0, CFLAG_AESCTR, NULL },
f5835d
+	{ "aes256-ctr",		16, 32, 0, 0, CFLAG_AESCTR, NULL },
f5835d
+#endif
f5835d
+	{ "none",		8, 0, 0, 0, CFLAG_NONE, NULL },
f5835d
+
f5835d
+	{ NULL,			0, 0, 0, 0, 0, NULL }
f5835d
+};
f5835d
+
f5835d
 /*--*/
f5835d
 
f5835d
 /* Returns a comma-separated list of supported ciphers. */
f5835d
@@ -100,7 +129,7 @@ cipher_alg_list(char sep, int auth_only)
f5835d
 	size_t nlen, rlen = 0;
f5835d
 	const struct sshcipher *c;
f5835d
 
f5835d
-	for (c = ciphers; c->name != NULL; c++) {
f5835d
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++) {
f5835d
 		if ((c->flags & CFLAG_INTERNAL) != 0)
f5835d
 			continue;
f5835d
 		if (auth_only && c->auth_len == 0)
f5835d
@@ -172,7 +201,7 @@ const struct sshcipher *
f5835d
 cipher_by_name(const char *name)
f5835d
 {
f5835d
 	const struct sshcipher *c;
f5835d
-	for (c = ciphers; c->name != NULL; c++)
f5835d
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
f5835d
 		if (strcmp(c->name, name) == 0)
f5835d
 			return c;
f5835d
 	return NULL;
f5835d
diff -up openssh-7.7p1/cipher-ctr.c.fips openssh-7.7p1/cipher-ctr.c
f5835d
--- openssh-7.7p1/cipher-ctr.c.fips	2018-08-08 10:08:40.709719021 +0200
f5835d
+++ openssh-7.7p1/cipher-ctr.c	2018-08-08 10:08:40.821719965 +0200
f5835d
@@ -179,7 +179,8 @@ evp_aes_128_ctr(void)
f5835d
 	aes_ctr.do_cipher = ssh_aes_ctr;
f5835d
 #ifndef SSH_OLD_EVP
f5835d
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
f5835d
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
f5835d
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
f5835d
+	    EVP_CIPH_FLAG_FIPS;
f5835d
 #endif
f5835d
 	return (&aes_ctr);
f5835d
 }
f5835d
diff -up openssh-7.7p1/clientloop.c.fips openssh-7.7p1/clientloop.c
f5835d
--- openssh-7.7p1/clientloop.c.fips	2018-08-08 10:08:40.769719527 +0200
f5835d
+++ openssh-7.7p1/clientloop.c	2018-08-08 10:08:40.822719973 +0200
f5835d
@@ -1978,7 +1978,8 @@ key_accepted_by_hostkeyalgs(const struct
f5835d
 {
f5835d
 	const char *ktype = sshkey_ssh_name(key);
f5835d
 	const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
f5835d
-	    options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
f5835d
+	    options.hostkeyalgorithms : (FIPS_mode() ?
f5835d
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG);
f5835d
 
f5835d
 	if (key == NULL || key->type == KEY_UNSPEC)
f5835d
 		return 0;
f5835d
diff -up openssh-7.7p1/dh.h.fips openssh-7.7p1/dh.h
f5835d
--- openssh-7.7p1/dh.h.fips	2018-04-02 07:38:28.000000000 +0200
f5835d
+++ openssh-7.7p1/dh.h	2018-08-08 10:08:40.822719973 +0200
f5835d
@@ -51,6 +51,7 @@ u_int	 dh_estimate(int);
f5835d
  * Miniumum increased in light of DH precomputation attacks.
f5835d
  */
f5835d
 #define DH_GRP_MIN	2048
f5835d
+#define DH_GRP_MIN_FIPS	2048
f5835d
 #define DH_GRP_MAX	8192
f5835d
 
f5835d
 /*
f5835d
diff -up openssh-7.7p1/entropy.c.fips openssh-7.7p1/entropy.c
f5835d
--- openssh-7.7p1/entropy.c.fips	2018-08-08 10:08:40.698718928 +0200
f5835d
+++ openssh-7.7p1/entropy.c	2018-08-08 10:08:40.822719973 +0200
f5835d
@@ -217,6 +217,9 @@ seed_rng(void)
f5835d
 		fatal("OpenSSL version mismatch. Built against %lx, you "
f5835d
 		    "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
f5835d
 
f5835d
+	/* clean the PRNG status when exiting the program */
f5835d
+	atexit(RAND_cleanup);
f5835d
+
f5835d
 #ifndef OPENSSL_PRNG_ONLY
f5835d
 	if (RAND_status() == 1) {
f5835d
 		debug3("RNG is ready, skipping seeding");
f5835d
diff -up openssh-7.7p1/kex.c.fips openssh-7.7p1/kex.c
f5835d
--- openssh-7.7p1/kex.c.fips	2018-08-08 10:08:40.815719915 +0200
f5835d
+++ openssh-7.7p1/kex.c	2018-08-08 10:11:24.109081924 +0200
f5835d
@@ -35,6 +35,7 @@
f5835d
 #ifdef WITH_OPENSSL
f5835d
 #include <openssl/crypto.h>
f5835d
 #include <openssl/dh.h>
f5835d
+#include <openssl/fips.h>
f5835d
 #endif
f5835d
 
f5835d
 #include "ssh2.h"
f5835d
@@ -122,6 +123,26 @@ static const struct kexalg kexalgs[] = {
f5835d
 	{ NULL, -1, -1, -1},
f5835d
 };
f5835d
 
f5835d
+static const struct kexalg kexalgs_fips[] = {
f5835d
+	{ KEX_DH14_SHA256, KEX_DH_GRP14_SHA256, 0, SSH_DIGEST_SHA256 },
f5835d
+	{ KEX_DH16_SHA512, KEX_DH_GRP16_SHA512, 0, SSH_DIGEST_SHA512 },
f5835d
+	{ KEX_DH18_SHA512, KEX_DH_GRP18_SHA512, 0, SSH_DIGEST_SHA512 },
f5835d
+#ifdef HAVE_EVP_SHA256
f5835d
+	{ KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
f5835d
+#endif
f5835d
+#ifdef OPENSSL_HAS_ECC
f5835d
+	{ KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2,
f5835d
+	    NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
f5835d
+	{ KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1,
f5835d
+	    SSH_DIGEST_SHA384 },
f5835d
+# ifdef OPENSSL_HAS_NISTP521
f5835d
+	{ KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
f5835d
+	    SSH_DIGEST_SHA512 },
f5835d
+# endif
f5835d
+#endif
f5835d
+	{ NULL, -1, -1, -1},
f5835d
+};
f5835d
+
f5835d
 char *
f5835d
 kex_alg_list(char sep)
f5835d
 {
f5835d
@@ -129,7 +150,7 @@ kex_alg_list(char sep)
f5835d
 	size_t nlen, rlen = 0;
f5835d
 	const struct kexalg *k;
f5835d
 
f5835d
-	for (k = kexalgs; k->name != NULL; k++) {
f5835d
+	for (k = (FIPS_mode() ? kexalgs_fips : kexalgs); k->name != NULL; k++) {
f5835d
 		if (ret != NULL)
f5835d
 			ret[rlen++] = sep;
f5835d
 		nlen = strlen(k->name);
f5835d
@@ -149,7 +170,7 @@ kex_alg_by_name(const char *name)
f5835d
 {
f5835d
 	const struct kexalg *k;
f5835d
 
f5835d
-	for (k = kexalgs; k->name != NULL; k++) {
f5835d
+	for (k = (FIPS_mode() ? kexalgs_fips : kexalgs); k->name != NULL; k++) {
f5835d
 		if (strcmp(k->name, name) == 0)
f5835d
 			return k;
f5835d
 #ifdef GSSAPI
f5835d
@@ -175,7 +196,10 @@ kex_names_valid(const char *names)
f5835d
 	for ((p = strsep(&cp, ",")); p && *p != '\0';
f5835d
 	    (p = strsep(&cp, ","))) {
f5835d
 		if (kex_alg_by_name(p) == NULL) {
f5835d
-			error("Unsupported KEX algorithm \"%.100s\"", p);
f5835d
+			if (FIPS_mode())
f5835d
+				error("\"%.100s\" is not allowed in FIPS mode", p);
f5835d
+			else
f5835d
+				error("Unsupported KEX algorithm \"%.100s\"", p);
f5835d
 			free(s);
f5835d
 			return 0;
f5835d
 		}
f5835d
diff -up openssh-7.7p1/kexgexc.c.fips openssh-7.7p1/kexgexc.c
f5835d
--- openssh-7.7p1/kexgexc.c.fips	2018-04-02 07:38:28.000000000 +0200
f5835d
+++ openssh-7.7p1/kexgexc.c	2018-08-08 10:08:40.822719973 +0200
f5835d
@@ -28,6 +28,7 @@
f5835d
 
f5835d
 #ifdef WITH_OPENSSL
f5835d
 
f5835d
+#include <openssl/fips.h>
f5835d
 #include <sys/types.h>
f5835d
 
f5835d
 #include <openssl/dh.h>
f5835d
@@ -63,7 +64,7 @@ kexgex_client(struct ssh *ssh)
f5835d
 
f5835d
 	nbits = dh_estimate(kex->dh_need * 8);
f5835d
 
f5835d
-	kex->min = DH_GRP_MIN;
f5835d
+	kex->min = FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN;
f5835d
 	kex->max = DH_GRP_MAX;
f5835d
 	kex->nbits = nbits;
f5835d
 	if (datafellows & SSH_BUG_DHGEX_LARGE)
f5835d
diff -up openssh-7.7p1/kexgexs.c.fips openssh-7.7p1/kexgexs.c
f5835d
--- openssh-7.7p1/kexgexs.c.fips	2018-04-02 07:38:28.000000000 +0200
f5835d
+++ openssh-7.7p1/kexgexs.c	2018-08-08 10:08:40.823719982 +0200
f5835d
@@ -82,9 +82,9 @@ input_kex_dh_gex_request(int type, u_int
f5835d
 	kex->nbits = nbits;
f5835d
 	kex->min = min;
f5835d
 	kex->max = max;
f5835d
-	min = MAXIMUM(DH_GRP_MIN, min);
f5835d
+	min = MAXIMUM(FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN, min);
f5835d
 	max = MINIMUM(DH_GRP_MAX, max);
f5835d
-	nbits = MAXIMUM(DH_GRP_MIN, nbits);
f5835d
+	nbits = MAXIMUM(FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN, nbits);
f5835d
 	nbits = MINIMUM(DH_GRP_MAX, nbits);
f5835d
 
f5835d
 	if (kex->max < kex->min || kex->nbits < kex->min ||
f5835d
diff -up openssh-7.7p1/mac.c.fips openssh-7.7p1/mac.c
f5835d
--- openssh-7.7p1/mac.c.fips	2018-08-08 10:08:40.815719915 +0200
f5835d
+++ openssh-7.7p1/mac.c	2018-08-08 10:11:56.915352642 +0200
f5835d
@@ -27,6 +27,8 @@
f5835d
 
f5835d
 #include <sys/types.h>
f5835d
 
f5835d
+#include <openssl/fips.h>
f5835d
+
f5835d
 #include <string.h>
f5835d
 #include <stdio.h>
f5835d
 
f5835d
@@ -54,7 +56,7 @@ struct macalg {
f5835d
 	int		etm;		/* Encrypt-then-MAC */
f5835d
 };
f5835d
 
f5835d
-static const struct macalg macs[] = {
f5835d
+static const struct macalg all_macs[] = {
f5835d
 	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
f5835d
 	{ "hmac-sha1",				SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
f5835d
 	{ "hmac-sha1-96",			SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 },
f5835d
@@ -82,6 +84,24 @@ static const struct macalg macs[] = {
f5835d
 	{ NULL,					0, 0, 0, 0, 0, 0 }
f5835d
 };
f5835d
 
f5835d
+static const struct macalg fips_macs[] = {
f5835d
+	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
f5835d
+	{ "hmac-sha1",				SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
f5835d
+#ifdef HAVE_EVP_SHA256
f5835d
+	{ "hmac-sha2-256",			SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 },
f5835d
+	{ "hmac-sha2-512",			SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 },
f5835d
+#endif
f5835d
+
f5835d
+	/* Encrypt-then-MAC variants */
f5835d
+	{ "hmac-sha1-etm@openssh.com",		SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 },
f5835d
+#ifdef HAVE_EVP_SHA256
f5835d
+	{ "hmac-sha2-256-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 },
f5835d
+	{ "hmac-sha2-512-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 },
f5835d
+#endif
f5835d
+
f5835d
+	{ NULL,					0, 0, 0, 0, 0, 0 }
f5835d
+};
f5835d
+
f5835d
 /* Returns a list of supported MACs separated by the specified char. */
f5835d
 char *
f5835d
 mac_alg_list(char sep)
f5835d
@@ -90,7 +110,7 @@ mac_alg_list(char sep)
f5835d
 	size_t nlen, rlen = 0;
f5835d
 	const struct macalg *m;
f5835d
 
f5835d
-	for (m = macs; m->name != NULL; m++) {
f5835d
+	for (m = FIPS_mode() ? fips_macs : all_macs; m->name != NULL; m++) {
f5835d
 		if (ret != NULL)
f5835d
 			ret[rlen++] = sep;
f5835d
 		nlen = strlen(m->name);
f5835d
@@ -129,7 +149,7 @@ mac_setup(struct sshmac *mac, char *name
f5835d
 {
f5835d
 	const struct macalg *m;
f5835d
 
f5835d
-	for (m = macs; m->name != NULL; m++) {
f5835d
+	for (m = FIPS_mode() ? fips_macs : all_macs; m->name != NULL; m++) {
f5835d
 		if (strcmp(name, m->name) != 0)
f5835d
 			continue;
f5835d
 		if (mac != NULL)
f5835d
diff -up openssh-7.7p1/Makefile.in.fips openssh-7.7p1/Makefile.in
f5835d
--- openssh-7.7p1/Makefile.in.fips	2018-08-08 10:08:40.815719915 +0200
f5835d
+++ openssh-7.7p1/Makefile.in	2018-08-08 10:08:40.823719982 +0200
f5835d
@@ -179,25 +179,25 @@ libssh.a: $(LIBSSH_OBJS)
f5835d
 	$(RANLIB) $@
f5835d
 
f5835d
 ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
f5835d
-	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS) $(GSSLIBS)
f5835d
+	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHLIBS) $(LIBS) $(GSSLIBS)
f5835d
 
f5835d
 sshd$(EXEEXT): libssh.a	$(LIBCOMPAT) $(SSHDOBJS)
f5835d
-	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
f5835d
+	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
f5835d
 
f5835d
 scp$(EXEEXT): $(LIBCOMPAT) libssh.a scp.o progressmeter.o
f5835d
 	$(LD) -o $@ scp.o progressmeter.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
f5835d
 
f5835d
 ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o
f5835d
-	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
f5835d
+	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
f5835d
 
f5835d
 ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
f5835d
-	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
f5835d
+	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
f5835d
 
f5835d
 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
f5835d
-	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
f5835d
+	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
f5835d
 
f5835d
 ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o readconf.o uidswap.o
f5835d
-	$(LD) -o $@ ssh-keysign.o readconf.o uidswap.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
f5835d
+	$(LD) -o $@ ssh-keysign.o readconf.o uidswap.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
f5835d
 
f5835d
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
f5835d
 	$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
f5835d
@@ -215,7 +215,7 @@ ssh-cavs$(EXEEXT): $(LIBCOMPAT) libssh.a
f5835d
 	$(LD) -o $@ ssh-cavs.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
f5835d
 
f5835d
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
f5835d
-	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
f5835d
+	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
f5835d
 
f5835d
 sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o
f5835d
 	$(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
f5835d
diff -up openssh-7.7p1/myproposal.h.fips openssh-7.7p1/myproposal.h
f5835d
--- openssh-7.7p1/myproposal.h.fips	2018-04-02 07:38:28.000000000 +0200
f5835d
+++ openssh-7.7p1/myproposal.h	2018-08-08 10:08:40.823719982 +0200
f5835d
@@ -114,6 +114,14 @@
f5835d
 	"rsa-sha2-256," \
f5835d
 	"ssh-rsa"
f5835d
 
f5835d
+#define	KEX_FIPS_PK_ALG	\
f5835d
+	HOSTKEY_ECDSA_CERT_METHODS \
f5835d
+	"ssh-rsa-cert-v01@openssh.com," \
f5835d
+	HOSTKEY_ECDSA_METHODS \
f5835d
+	"rsa-sha2-512," \
f5835d
+	"rsa-sha2-256," \
f5835d
+	"ssh-rsa"
f5835d
+
f5835d
 /* the actual algorithms */
f5835d
 
f5835d
 #define KEX_SERVER_ENCRYPT \
f5835d
@@ -137,6 +145,38 @@
f5835d
 
f5835d
 #define KEX_CLIENT_MAC KEX_SERVER_MAC
f5835d
 
f5835d
+#define	KEX_FIPS_ENCRYPT \
f5835d
+	"aes128-ctr,aes192-ctr,aes256-ctr," \
f5835d
+	"aes128-cbc,3des-cbc," \
f5835d
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se" \
f5835d
+	AESGCM_CIPHER_MODES
f5835d
+#ifdef HAVE_EVP_SHA256
f5835d
+# define KEX_DEFAULT_KEX_FIPS		\
f5835d
+	KEX_ECDH_METHODS \
f5835d
+	KEX_SHA2_METHODS \
f5835d
+	"diffie-hellman-group14-sha256"
f5835d
+# define KEX_FIPS_MAC \
f5835d
+	"hmac-sha1," \
f5835d
+	"hmac-sha2-256," \
f5835d
+	"hmac-sha2-512," \
f5835d
+	"hmac-sha1-etm@openssh.com," \
f5835d
+	"hmac-sha2-256-etm@openssh.com," \
f5835d
+	"hmac-sha2-512-etm@openssh.com"
f5835d
+#else
f5835d
+# ifdef OPENSSL_HAS_NISTP521
f5835d
+#  define KEX_DEFAULT_KEX_FIPS		\
f5835d
+	"ecdh-sha2-nistp256," \
f5835d
+	"ecdh-sha2-nistp384," \
f5835d
+	"ecdh-sha2-nistp521"
f5835d
+# else
f5835d
+#  define KEX_DEFAULT_KEX_FIPS		\
f5835d
+	"ecdh-sha2-nistp256," \
f5835d
+	"ecdh-sha2-nistp384"
f5835d
+# endif
f5835d
+#define        KEX_FIPS_MAC \
f5835d
+       "hmac-sha1"
f5835d
+#endif
f5835d
+
f5835d
 #else /* WITH_OPENSSL */
f5835d
 
f5835d
 #define KEX_SERVER_KEX		\
f5835d
diff -up openssh-7.7p1/readconf.c.fips openssh-7.7p1/readconf.c
f5835d
--- openssh-7.7p1/readconf.c.fips	2018-08-08 10:08:40.769719527 +0200
f5835d
+++ openssh-7.7p1/readconf.c	2018-08-08 10:08:40.824719990 +0200
f5835d
@@ -2081,17 +2081,18 @@ fill_default_options(Options * options)
f5835d
 	all_mac = mac_alg_list(',');
f5835d
 	all_kex = kex_alg_list(',');
f5835d
 	all_key = sshkey_alg_list(0, 0, 1, ',');
f5835d
-#define ASSEMBLE(what, defaults, all) \
f5835d
+#define ASSEMBLE(what, defaults, fips_defaults, all) \
f5835d
 	do { \
f5835d
 		if ((r = kex_assemble_names(&options->what, \
f5835d
-		    defaults, all)) != 0) \
f5835d
+		    (FIPS_mode() ? fips_defaults : defaults), \
f5835d
+		    all)) != 0) \
f5835d
 			fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
f5835d
 	} while (0)
f5835d
-	ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, all_cipher);
f5835d
-	ASSEMBLE(macs, KEX_SERVER_MAC, all_mac);
f5835d
-	ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, all_kex);
f5835d
-	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
f5835d
-	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
f5835d
+	ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, KEX_FIPS_ENCRYPT, all_cipher);
f5835d
+	ASSEMBLE(macs, KEX_SERVER_MAC, KEX_FIPS_MAC, all_mac);
f5835d
+	ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, KEX_DEFAULT_KEX_FIPS, all_kex);
f5835d
+	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
f5835d
+	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
f5835d
 #undef ASSEMBLE
f5835d
 	free(all_cipher);
f5835d
 	free(all_mac);
f5835d
diff -up openssh-7.7p1/sandbox-seccomp-filter.c.fips openssh-7.7p1/sandbox-seccomp-filter.c
f5835d
--- openssh-7.7p1/sandbox-seccomp-filter.c.fips	2018-08-08 10:08:40.794719737 +0200
f5835d
+++ openssh-7.7p1/sandbox-seccomp-filter.c	2018-08-08 10:08:40.824719990 +0200
f5835d
@@ -137,6 +137,9 @@ static const struct sock_filter preauth_
f5835d
 #ifdef __NR_open
f5835d
 	SC_DENY(__NR_open, EACCES),
f5835d
 #endif
f5835d
+#ifdef __NR_socket
f5835d
+	SC_DENY(__NR_socket, EACCES),
f5835d
+#endif
f5835d
 #ifdef __NR_openat
f5835d
 	SC_DENY(__NR_openat, EACCES),
f5835d
 #endif
f5835d
diff -up openssh-7.7p1/servconf.c.fips openssh-7.7p1/servconf.c
f5835d
--- openssh-7.7p1/servconf.c.fips	2018-08-08 10:08:40.778719603 +0200
f5835d
+++ openssh-7.7p1/servconf.c	2018-08-08 10:08:40.824719990 +0200
f5835d
@@ -196,17 +196,18 @@ option_clear_or_none(const char *o)
f5835d
 	all_mac = mac_alg_list(',');
f5835d
 	all_kex = kex_alg_list(',');
f5835d
 	all_key = sshkey_alg_list(0, 0, 1, ',');
f5835d
-#define ASSEMBLE(what, defaults, all) \
f5835d
+#define ASSEMBLE(what, defaults, fips_defaults, all) \
f5835d
 	do { \
f5835d
-		if ((r = kex_assemble_names(&o->what, defaults, all)) != 0) \
f5835d
+		if ((r = kex_assemble_names(&o->what, (FIPS_mode() \
f5835d
+		    ? fips_defaults : defaults), all)) != 0) \
f5835d
 			fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
f5835d
 	} while (0)
f5835d
-	ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, all_cipher);
f5835d
-	ASSEMBLE(macs, KEX_SERVER_MAC, all_mac);
f5835d
-	ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, all_kex);
f5835d
-	ASSEMBLE(hostkeyalgorithms, KEX_DEFAULT_PK_ALG, all_key);
f5835d
-	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
f5835d
-	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
f5835d
+	ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, KEX_FIPS_ENCRYPT, all_cipher);
f5835d
+	ASSEMBLE(macs, KEX_SERVER_MAC, KEX_FIPS_MAC, all_mac);
f5835d
+	ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, KEX_DEFAULT_KEX_FIPS, all_kex);
f5835d
+	ASSEMBLE(hostkeyalgorithms, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
f5835d
+	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
f5835d
+	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
f5835d
 #undef ASSEMBLE
f5835d
 	free(all_cipher);
f5835d
 	free(all_mac);
f5835d
diff -up openssh-7.7p1/ssh.c.fips openssh-7.7p1/ssh.c
f5835d
--- openssh-7.7p1/ssh.c.fips	2018-08-08 10:08:40.811719881 +0200
f5835d
+++ openssh-7.7p1/ssh.c	2018-08-08 10:08:40.825719999 +0200
f5835d
@@ -76,6 +76,8 @@
f5835d
 #include <openssl/evp.h>
f5835d
 #include <openssl/err.h>
f5835d
 #endif
f5835d
+#include <openssl/fips.h>
f5835d
+#include <fipscheck.h>
f5835d
 #include "openbsd-compat/openssl-compat.h"
f5835d
 #include "openbsd-compat/sys-queue.h"
f5835d
 
f5835d
@@ -579,6 +581,14 @@ main(int ac, char **av)
f5835d
 	sanitise_stdfd();
f5835d
 
f5835d
 	__progname = ssh_get_progname(av[0]);
f5835d
+        SSLeay_add_all_algorithms();
f5835d
+	if (access("/etc/system-fips", F_OK) == 0)
f5835d
+		if (! FIPSCHECK_verify(NULL, NULL)){
f5835d
+			if (FIPS_mode())
f5835d
+				fatal("FIPS integrity verification test failed.");
f5835d
+			else
f5835d
+				logit("FIPS integrity verification test failed.");
f5835d
+	}
f5835d
 
f5835d
 #ifndef HAVE_SETPROCTITLE
f5835d
 	/* Prepare for later setproctitle emulation */
f5835d
@@ -1045,7 +1055,6 @@ main(int ac, char **av)
f5835d
 	host_arg = xstrdup(host);
f5835d
 
f5835d
 #ifdef WITH_OPENSSL
f5835d
-	OpenSSL_add_all_algorithms();
f5835d
 	ERR_load_crypto_strings();
f5835d
 #endif
f5835d
 
f5835d
@@ -1268,6 +1277,10 @@ main(int ac, char **av)
f5835d
 
f5835d
 	seed_rng();
f5835d
 
f5835d
+	if (FIPS_mode()) {
f5835d
+		logit("FIPS mode initialized");
f5835d
+	}
f5835d
+
f5835d
 	if (options.user == NULL)
f5835d
 		options.user = xstrdup(pw->pw_name);
f5835d
 
f5835d
diff -up openssh-7.7p1/sshconnect2.c.fips openssh-7.7p1/sshconnect2.c
f5835d
--- openssh-7.7p1/sshconnect2.c.fips	2018-08-08 10:08:40.786719670 +0200
f5835d
+++ openssh-7.7p1/sshconnect2.c	2018-08-08 10:08:40.825719999 +0200
f5835d
@@ -44,6 +44,8 @@
f5835d
 #include <vis.h>
f5835d
 #endif
f5835d
 
f5835d
+#include <openssl/fips.h>
f5835d
+
f5835d
 #include "openbsd-compat/sys-queue.h"
f5835d
 
f5835d
 #include "xmalloc.h"
f5835d
@@ -235,7 +237,8 @@ order_hostkeyalgs(char *host, struct soc
f5835d
 	for (i = 0; i < options.num_system_hostfiles; i++)
f5835d
 		load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
f5835d
 
f5835d
-	oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
f5835d
+	oavail = avail = xstrdup((FIPS_mode()
f5835d
+	    ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG));
f5835d
 	maxlen = strlen(avail) + 1;
f5835d
 	first = xmalloc(maxlen);
f5835d
 	last = xmalloc(maxlen);
f5835d
@@ -290,23 +293,28 @@ ssh_kex2(char *host, struct sockaddr *ho
f5835d
 
f5835d
 #ifdef GSSAPI
f5835d
 	if (options.gss_keyex) {
f5835d
-		/* Add the GSSAPI mechanisms currently supported on this 
f5835d
-		 * client to the key exchange algorithm proposal */
f5835d
-		orig = options.kex_algorithms;
f5835d
-
f5835d
-		if (options.gss_server_identity)
f5835d
-			gss_host = options.gss_server_identity;
f5835d
-		else if (options.gss_trust_dns)
f5835d
-			gss_host = (char *)get_canonical_hostname(active_state, 1);
f5835d
-		else
f5835d
-			gss_host = host;
f5835d
-
f5835d
-		gss = ssh_gssapi_client_mechanisms(gss_host,
f5835d
-		    options.gss_client_identity, options.gss_kex_algorithms);
f5835d
-		if (gss) {
f5835d
-			debug("Offering GSSAPI proposal: %s", gss);
f5835d
-			xasprintf(&options.kex_algorithms,
f5835d
-			    "%s,%s", gss, orig);
f5835d
+		if (FIPS_mode()) {
f5835d
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
f5835d
+			options.gss_keyex = 0;
f5835d
+		} else {
f5835d
+			/* Add the GSSAPI mechanisms currently supported on this
f5835d
+			 * client to the key exchange algorithm proposal */
f5835d
+			orig = options.kex_algorithms;
f5835d
+
f5835d
+			if (options.gss_server_identity)
f5835d
+				gss_host = options.gss_server_identity;
f5835d
+			else if (options.gss_trust_dns)
f5835d
+				gss_host = (char *)get_canonical_hostname(active_state, 1);
f5835d
+			else
f5835d
+				gss_host = host;
f5835d
+
f5835d
+			gss = ssh_gssapi_client_mechanisms(gss_host,
f5835d
+			    options.gss_client_identity, options.gss_kex_algorithms);
f5835d
+			if (gss) {
f5835d
+				debug("Offering GSSAPI proposal: %s", gss);
f5835d
+				xasprintf(&options.kex_algorithms,
f5835d
+				    "%s,%s", gss, orig);
f5835d
+			}
f5835d
 		}
f5835d
 	}
f5835d
 #endif
f5835d
@@ -322,14 +330,16 @@ ssh_kex2(char *host, struct sockaddr *ho
f5835d
 	if (options.hostkeyalgorithms != NULL) {
f5835d
 		all_key = sshkey_alg_list(0, 0, 1, ',');
f5835d
		if (kex_assemble_names(&options.hostkeyalgorithms,
f5835d
-		    KEX_DEFAULT_PK_ALG, all_key) != 0)
f5835d
+		    (FIPS_mode() ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG),
f5835d
+		    all_key) != 0)
f5835d
 			fatal("%s: kex_assemble_namelist", __func__);
f5835d
 		free(all_key);
f5835d
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
f5835d
 		    compat_pkalg_proposal(options.hostkeyalgorithms);
f5835d
 	} else {
f5835d
 		/* Enforce default */
f5835d
-		options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
f5835d
+		options.hostkeyalgorithms = xstrdup((FIPS_mode()
f5835d
+		    ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG));
f5835d
 		/* Prefer algorithms that we already have keys for */
f5835d
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
f5835d
 		    compat_pkalg_proposal(
f5835d
diff -up openssh-7.7p1/sshd.c.fips openssh-7.7p1/sshd.c
f5835d
--- openssh-7.7p1/sshd.c.fips	2018-08-08 10:08:40.818719940 +0200
f5835d
+++ openssh-7.7p1/sshd.c	2018-08-08 10:08:40.826720007 +0200
f5835d
@@ -66,6 +66,7 @@
f5835d
 #include <grp.h>
f5835d
 #include <pwd.h>
f5835d
 #include <signal.h>
f5835d
+#include <syslog.h>
f5835d
 #include <stdarg.h>
f5835d
 #include <stdio.h>
f5835d
 #include <stdlib.h>
f5835d
@@ -77,6 +78,8 @@
f5835d
 #include <openssl/dh.h>
f5835d
 #include <openssl/bn.h>
f5835d
 #include <openssl/rand.h>
f5835d
+#include <openssl/fips.h>
f5835d
+#include <fipscheck.h>
f5835d
 #include "openbsd-compat/openssl-compat.h"
f5835d
 #endif
f5835d
 
f5835d
@@ -1534,6 +1537,18 @@ main(int ac, char **av)
f5835d
 #endif
f5835d
 	__progname = ssh_get_progname(av[0]);
f5835d
 
f5835d
+        SSLeay_add_all_algorithms();
f5835d
+	if (access("/etc/system-fips", F_OK) == 0)
f5835d
+		if (! FIPSCHECK_verify(NULL, NULL)) {
f5835d
+			openlog(__progname, LOG_PID, LOG_AUTHPRIV);
f5835d
+			if (FIPS_mode()) {
f5835d
+				syslog(LOG_CRIT, "FIPS integrity verification test failed.");
f5835d
+				cleanup_exit(255);
f5835d
+			}
f5835d
+			else
f5835d
+				syslog(LOG_INFO, "FIPS integrity verification test failed.");
f5835d
+			closelog();
f5835d
+		}
f5835d
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
f5835d
 	saved_argc = ac;
f5835d
 	rexec_argc = ac;
f5835d
@@ -1675,7 +1690,7 @@ main(int ac, char **av)
f5835d
 	else
f5835d
 		closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
f5835d
 
f5835d
-#ifdef WITH_OPENSSL
f5835d
+#if 0 /* FIPS */
f5835d
 	OpenSSL_add_all_algorithms();
f5835d
 #endif
f5835d
 
f5835d
@@ -1979,6 +1994,10 @@ main(int ac, char **av)
f5835d
 	/* Reinitialize the log (because of the fork above). */
f5835d
 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
f5835d
 
f5835d
+	if (FIPS_mode()) {
f5835d
+		logit("FIPS mode initialized");
f5835d
+	}
f5835d
+
f5835d
 	/* Chdir to the root directory so that the current disk can be
f5835d
 	   unmounted if desired. */
f5835d
 	if (chdir("/") == -1)
f5835d
@@ -2359,10 +2378,14 @@ do_ssh2_kex(void)
f5835d
 	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
f5835d
 		orig = NULL;
f5835d
 
f5835d
-	if (options.gss_keyex)
f5835d
-		gss = ssh_gssapi_server_mechanisms();
f5835d
-	else
f5835d
-		gss = NULL;
f5835d
+	if (options.gss_keyex) {
f5835d
+		if (FIPS_mode()) {
f5835d
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
f5835d
+			options.gss_keyex = 0;
f5835d
+		} else {
f5835d
+			gss = ssh_gssapi_server_mechanisms();
f5835d
+		}
f5835d
+	}
f5835d
 
f5835d
 	if (gss && orig)
f5835d
 		xasprintf(&newstr, "%s,%s", gss, orig);
f5835d
diff -up openssh-7.7p1/sshkey.c.fips openssh-7.7p1/sshkey.c
f5835d
--- openssh-7.7p1/sshkey.c.fips	2018-08-08 10:08:40.818719940 +0200
f5835d
+++ openssh-7.7p1/sshkey.c	2018-08-08 10:08:40.826720007 +0200
f5835d
@@ -34,6 +34,7 @@
f5835d
 #include <openssl/evp.h>
f5835d
 #include <openssl/err.h>
f5835d
 #include <openssl/pem.h>
f5835d
+#include <openssl/fips.h>
f5835d
 #endif
f5835d
 
f5835d
 #include "crypto_api.h"
f5835d
@@ -57,6 +58,7 @@
f5835d
 #include "sshkey.h"
f5835d
 #include "sshkey-xmss.h"
f5835d
 #include "match.h"
f5835d
+#include "log.h"
f5835d
 
f5835d
 #include "xmss_fast.h"
f5835d
 
f5835d
@@ -1526,6 +1528,8 @@ rsa_generate_private_key(u_int bits, RSA
f5835d
 	}
f5835d
 	if (!BN_set_word(f4, RSA_F4) ||
f5835d
 	    !RSA_generate_key_ex(private, bits, f4, NULL)) {
f5835d
+			if (FIPS_mode())
f5835d
+				logit("%s: the key length might be unsupported by FIPS mode approved key generation method", __func__);
f5835d
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
f5835d
 		goto out;
f5835d
 	}
f5835d
diff -up openssh-7.7p1/ssh-keygen.c.fips openssh-7.7p1/ssh-keygen.c
f5835d
--- openssh-7.7p1/ssh-keygen.c.fips	2018-08-08 10:08:40.801719797 +0200
f5835d
+++ openssh-7.7p1/ssh-keygen.c	2018-08-08 10:08:40.827720016 +0200
f5835d
@@ -229,6 +229,12 @@ type_bits_valid(int type, const char *na
f5835d
 	    OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
f5835d
 	if (*bitsp > maxbits)
f5835d
 		fatal("key bits exceeds maximum %d", maxbits);
f5835d
+	if (FIPS_mode()) {
f5835d
+		if (type == KEY_DSA)
f5835d
+			fatal("DSA keys are not allowed in FIPS mode");
f5835d
+		if (type == KEY_ED25519)
f5835d
+			fatal("ED25519 keys are not allowed in FIPS mode");
f5835d
+	}
f5835d
 	switch (type) {
f5835d
 	case KEY_DSA:
f5835d
 		if (*bitsp != 1024)