bmh10 / rpms / openssh

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