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