vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Petr Lautrbach 802815
diff --git a/Makefile.in b/Makefile.in
Petr Lautrbach 802815
index 3bb7f00..294bef5 100644
Petr Lautrbach 802815
--- a/Makefile.in
Petr Lautrbach 802815
+++ b/Makefile.in
Petr Lautrbach 802815
@@ -154,25 +154,25 @@ libssh.a: $(LIBSSH_OBJS)
Petr Lautrbach 802815
 	$(RANLIB) $@
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
Petr Lautrbach 802815
-	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS) $(GSSLIBS)
Petr Lautrbach 802815
+	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHLIBS) $(LIBS) $(GSSLIBS)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 sshd$(EXEEXT): libssh.a	$(LIBCOMPAT) $(SSHDOBJS)
Petr Lautrbach 802815
-	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
Petr Lautrbach 802815
+	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 scp$(EXEEXT): $(LIBCOMPAT) libssh.a scp.o progressmeter.o
Petr Lautrbach 802815
 	$(LD) -o $@ scp.o progressmeter.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o
Petr Lautrbach 802815
-	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Petr Lautrbach 802815
+	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
Petr Lautrbach 802815
-	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Petr Lautrbach 802815
+	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
Petr Lautrbach 802815
-	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Petr Lautrbach 802815
+	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o roaming_dummy.o readconf.o
Petr Lautrbach 802815
-	$(LD) -o $@ ssh-keysign.o readconf.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Petr Lautrbach 802815
+	$(LD) -o $@ ssh-keysign.o readconf.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
Petr Lautrbach 802815
 	$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
Petr Lautrbach 802815
@@ -187,7 +187,7 @@ ctr-cavstest$(EXEEXT): $(LIBCOMPAT) libssh.a ctr-cavstest.o
Petr Lautrbach 802815
 	$(LD) -o $@ ctr-cavstest.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o roaming_dummy.o
Petr Lautrbach 802815
-	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
Petr Lautrbach 802815
+	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o
Petr Lautrbach 802815
 	$(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Petr Lautrbach 802815
diff --git a/auth-rsa.c b/auth-rsa.c
Petr Lautrbach 802815
index f225b0b..8bafcd6 100644
Petr Lautrbach 802815
--- a/auth-rsa.c
Petr Lautrbach 802815
+++ b/auth-rsa.c
Petr Lautrbach 802815
@@ -244,7 +244,7 @@ rsa_key_allowed_in_file(struct passwd *pw, char *file,
Petr Lautrbach 802815
 			    "actual %d vs. announced %d.",
Petr Lautrbach 802815
 			    file, linenum, BN_num_bits(key->rsa->n), bits);
Petr Lautrbach 802815
 
Petr Lautrbach 802815
-		fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
Petr Lautrbach 802815
+		fp = key_selected_fingerprint(key, SSH_FP_HEX);
Petr Lautrbach 802815
 		debug("matching key found: file %s, line %lu %s %s",
Petr Lautrbach 802815
 		    file, linenum, key_type(key), fp);
Petr Lautrbach 802815
 		free(fp);
Petr Lautrbach 802815
diff --git a/auth2-pubkey.c b/auth2-pubkey.c
Petr Lautrbach 802815
index 6d1c872..3808ec8 100644
Petr Lautrbach 802815
--- a/auth2-pubkey.c
Petr Lautrbach 802815
+++ b/auth2-pubkey.c
Petr Lautrbach 802815
@@ -214,8 +214,7 @@ pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...)
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	if (key_is_cert(key)) {
Petr Lautrbach 802815
-		fp = key_fingerprint(key->cert->signature_key,
Petr Lautrbach 802815
-		    SSH_FP_MD5, SSH_FP_HEX);
Petr Lautrbach 802815
+		fp = key_selected_fingerprint(key->cert->signature_key, SSH_FP_HEX);
Petr Lautrbach 802815
 		auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s", 
Petr Lautrbach 802815
 		    key_type(key), key->cert->key_id,
Petr Lautrbach 802815
 		    (unsigned long long)key->cert->serial,
Petr Lautrbach 802815
@@ -223,7 +222,7 @@ pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...)
Petr Lautrbach 802815
 		    extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
Petr Lautrbach 802815
 		free(fp);
Petr Lautrbach 802815
 	} else {
Petr Lautrbach 802815
-		fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
Petr Lautrbach 802815
+		fp = key_selected_fingerprint(key, SSH_FP_HEX);
Petr Lautrbach 802815
 		auth_info(authctxt, "%s %s%s%s", key_type(key), fp,
Petr Lautrbach 802815
 		    extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
Petr Lautrbach 802815
 		free(fp);
Petr Lautrbach 802815
diff --git a/authfile.c b/authfile.c
Petr Lautrbach 802815
index ec4f4ff..2b3d650 100644
Petr Lautrbach 802815
--- a/authfile.c
Petr Lautrbach 802815
+++ b/authfile.c
Petr Lautrbach 802815
@@ -46,6 +46,7 @@
Petr Lautrbach 802815
 #include <openssl/err.h>
Petr Lautrbach 802815
 #include <openssl/evp.h>
Petr Lautrbach 802815
 #include <openssl/pem.h>
Petr Lautrbach 802815
+#include <openssl/fips.h>
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 /* compatibility with old or broken OpenSSL versions */
Petr Lautrbach 802815
 #include "openbsd-compat/openssl-compat.h"
Petr Lautrbach 802815
@@ -1068,7 +1069,7 @@ Key *
Petr Lautrbach 802815
 key_parse_private(Buffer *buffer, const char *filename,
Petr Lautrbach 802815
     const char *passphrase, char **commentp)
Petr Lautrbach 802815
 {
Petr Lautrbach 802815
-	Key *pub, *prv;
Petr Lautrbach 802815
+	Key *pub, *prv = NULL;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	/* it's a SSH v1 key if the public key part is readable */
Petr Lautrbach 802815
 	pub = key_parse_public_rsa1(buffer, commentp);
Petr Lautrbach 802815
@@ -1080,9 +1081,10 @@ key_parse_private(Buffer *buffer, const char *filename,
Petr Lautrbach 802815
 			*commentp = xstrdup(filename);
Petr Lautrbach 802815
 	} else {
Petr Lautrbach 802815
 		key_free(pub);
Petr Lautrbach 802815
-		/* key_parse_public_rsa1() has already loaded the comment */
Petr Lautrbach 802815
-		prv = key_parse_private_type(buffer, KEY_RSA1, passphrase,
Petr Lautrbach 802815
-		    NULL);
Petr Lautrbach 802815
+		if (! FIPS_mode())
Petr Lautrbach 802815
+			/* key_parse_public_rsa1() has already loaded the comment */
Petr Lautrbach 802815
+			prv = key_parse_private_type(buffer, KEY_RSA1, passphrase,
Petr Lautrbach 802815
+			    NULL);
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
 	return prv;
Petr Lautrbach 802815
 }
Petr Lautrbach 802815
diff --git a/cipher-ctr.c b/cipher-ctr.c
Petr Lautrbach 802815
index 73e9c7c..40ee395 100644
Petr Lautrbach 802815
--- a/cipher-ctr.c
Petr Lautrbach 802815
+++ b/cipher-ctr.c
Petr Lautrbach 802815
@@ -179,7 +179,8 @@ evp_aes_128_ctr(void)
Petr Lautrbach 802815
 	aes_ctr.do_cipher = ssh_aes_ctr;
Petr Lautrbach 802815
 #ifndef SSH_OLD_EVP
Petr Lautrbach 802815
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
Petr Lautrbach 802815
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
Petr Lautrbach 802815
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
Petr Lautrbach 802815
+	    EVP_CIPH_FLAG_FIPS;
Petr Lautrbach 802815
 #endif
Petr Lautrbach 802815
 	return (&aes_ctr);
Petr Lautrbach 802815
 }
Petr Lautrbach 802815
diff --git a/cipher.c b/cipher.c
Petr Lautrbach 802815
index 226e56d..b19443c 100644
Petr Lautrbach 802815
--- a/cipher.c
Petr Lautrbach 802815
+++ b/cipher.c
Petr Lautrbach 802815
@@ -39,6 +39,8 @@
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #include <sys/types.h>
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+#include <openssl/fips.h>
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 #include <string.h>
Petr Lautrbach 802815
 #include <stdarg.h>
Petr Lautrbach 802815
 #include <stdio.h>
Petr Lautrbach 802815
@@ -90,6 +92,25 @@ static const struct Cipher ciphers[] = {
Petr Lautrbach 802815
 	{ NULL,		SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
Petr Lautrbach 802815
 };
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+static const struct Cipher fips_ciphers[] = {
Petr Lautrbach 802815
+	{ "none",	SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
Petr Lautrbach 802815
+	{ "3des-cbc",	SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
Petr Lautrbach 802815
+	{ "aes128-cbc",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
Petr Lautrbach 802815
+	{ "aes192-cbc",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
Petr Lautrbach 802815
+	{ "aes256-cbc",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
Petr Lautrbach 802815
+	{ "rijndael-cbc@lysator.liu.se",
Petr Lautrbach 802815
+			SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
Petr Lautrbach 802815
+	{ "aes128-ctr",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
Petr Lautrbach 802815
+	{ "aes192-ctr",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
Petr Lautrbach 802815
+	{ "aes256-ctr",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
Petr Lautrbach 802815
+#ifdef OPENSSL_HAVE_EVPGCM
Petr Lautrbach 802815
+	{ "aes128-gcm@openssh.com",
Petr Lautrbach 802815
+			SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
Petr Lautrbach 802815
+	{ "aes256-gcm@openssh.com",
Petr Lautrbach 802815
+			SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
Petr Lautrbach 802815
+#endif
Petr Lautrbach 802815
+	{ NULL,		SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
Petr Lautrbach 802815
+};
Petr Lautrbach 802815
 /*--*/
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 /* Returns a list of supported ciphers separated by the specified char. */
Petr Lautrbach 802815
@@ -100,7 +121,7 @@ cipher_alg_list(char sep, int auth_only)
Petr Lautrbach 802815
 	size_t nlen, rlen = 0;
Petr Lautrbach 802815
 	const Cipher *c;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
-	for (c = ciphers; c->name != NULL; c++) {
Petr Lautrbach 802815
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++) {
Petr Lautrbach 802815
 		if (c->number != SSH_CIPHER_SSH2)
Petr Lautrbach 802815
 			continue;
Petr Lautrbach 802815
 		if (auth_only && c->auth_len == 0)
Petr Lautrbach 802815
@@ -180,7 +201,7 @@ const Cipher *
Petr Lautrbach 802815
 cipher_by_name(const char *name)
Petr Lautrbach 802815
 {
Petr Lautrbach 802815
 	const Cipher *c;
Petr Lautrbach 802815
-	for (c = ciphers; c->name != NULL; c++)
Petr Lautrbach 802815
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
Petr Lautrbach 802815
 		if (strcmp(c->name, name) == 0)
Petr Lautrbach 802815
 			return c;
Petr Lautrbach 802815
 	return NULL;
Petr Lautrbach 802815
@@ -190,7 +211,7 @@ const Cipher *
Petr Lautrbach 802815
 cipher_by_number(int id)
Petr Lautrbach 802815
 {
Petr Lautrbach 802815
 	const Cipher *c;
Petr Lautrbach 802815
-	for (c = ciphers; c->name != NULL; c++)
Petr Lautrbach 802815
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
Petr Lautrbach 802815
 		if (c->number == id)
Petr Lautrbach 802815
 			return c;
Petr Lautrbach 802815
 	return NULL;
Petr Lautrbach 802815
@@ -232,7 +253,7 @@ cipher_number(const char *name)
Petr Lautrbach 802815
 	const Cipher *c;
Petr Lautrbach 802815
 	if (name == NULL)
Petr Lautrbach 802815
 		return -1;
Petr Lautrbach 802815
-	for (c = ciphers; c->name != NULL; c++)
Petr Lautrbach 802815
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
Petr Lautrbach 802815
 		if (strcasecmp(c->name, name) == 0)
Petr Lautrbach 802815
 			return c->number;
Petr Lautrbach 802815
 	return -1;
Petr Lautrbach 802815
diff --git a/dh.h b/dh.h
Petr Lautrbach 802815
index 48f7b68..9ff39f4 100644
Petr Lautrbach 802815
--- a/dh.h
Petr Lautrbach 802815
+++ b/dh.h
Petr Lautrbach 802815
@@ -45,6 +45,7 @@ int	 dh_estimate(int);
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 /* Min and max values from RFC4419. */
Petr Lautrbach 802815
 #define DH_GRP_MIN	1024
Petr Lautrbach 802815
+#define DH_GRP_MIN_FIPS	2048
Petr Lautrbach 802815
 #define DH_GRP_MAX	8192
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 /*
Petr Lautrbach 802815
diff --git a/entropy.c b/entropy.c
Petr Lautrbach 802815
index b361a04..5616643 100644
Petr Lautrbach 802815
--- a/entropy.c
Petr Lautrbach 802815
+++ b/entropy.c
Petr Lautrbach 802815
@@ -222,6 +222,9 @@ seed_rng(void)
Petr Lautrbach 802815
 		fatal("OpenSSL version mismatch. Built against %lx, you "
Petr Lautrbach 802815
 		    "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+	/* clean the PRNG status when exiting the program */
Petr Lautrbach 802815
+	atexit(RAND_cleanup);
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 #ifndef OPENSSL_PRNG_ONLY
Petr Lautrbach 802815
 	if (RAND_status() == 1) {
Petr Lautrbach 802815
 		debug3("RNG is ready, skipping seeding");
Petr Lautrbach 802815
diff --git a/kex.c b/kex.c
Petr Lautrbach 802815
index bc3e53e..ede7b67 100644
Petr Lautrbach 802815
--- a/kex.c
Petr Lautrbach 802815
+++ b/kex.c
Petr Lautrbach 802815
@@ -34,6 +34,7 @@
Petr Lautrbach 802815
 #include <string.h>
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #include <openssl/crypto.h>
Petr Lautrbach 802815
+#include <openssl/fips.h>
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #include "xmalloc.h"
Petr Lautrbach 802815
 #include "ssh2.h"
Petr Lautrbach 802815
@@ -103,6 +104,25 @@ static const struct kexalg kexalgs[] = {
Petr Lautrbach 802815
 	{ NULL, -1, -1, -1},
Petr Lautrbach 802815
 };
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+static const struct kexalg kexalgs_fips[] = {
Petr Lautrbach 802815
+	{ KEX_DH14, KEX_DH_GRP14_SHA1, 0, SSH_DIGEST_SHA1 },
Petr Lautrbach 802815
+	{ KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, SSH_DIGEST_SHA1 },
Petr Lautrbach 802815
+#ifdef HAVE_EVP_SHA256
Petr Lautrbach 802815
+	{ KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, SSH_DIGEST_SHA256 },
Petr Lautrbach 802815
+#endif
Petr Lautrbach 802815
+#ifdef OPENSSL_HAS_ECC
Petr Lautrbach 802815
+	{ KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2,
Petr Lautrbach 802815
+	    NID_X9_62_prime256v1, SSH_DIGEST_SHA256 },
Petr Lautrbach 802815
+	{ KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1,
Petr Lautrbach 802815
+	    SSH_DIGEST_SHA384 },
Petr Lautrbach 802815
+# ifdef OPENSSL_HAS_NISTP521
Petr Lautrbach 802815
+	{ KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1,
Petr Lautrbach 802815
+	    SSH_DIGEST_SHA512 },
Petr Lautrbach 802815
+# endif
Petr Lautrbach 802815
+#endif
Petr Lautrbach 802815
+	{ NULL, -1, -1, NULL},
Petr Lautrbach 802815
+};
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 char *
Petr Lautrbach 802815
 kex_alg_list(char sep)
Petr Lautrbach 802815
 {
Petr Lautrbach 802815
@@ -126,7 +146,7 @@ kex_alg_by_name(const char *name)
Petr Lautrbach 802815
 {
Petr Lautrbach 802815
 	const struct kexalg *k;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
-	for (k = kexalgs; k->name != NULL; k++) {
Petr Lautrbach 802815
+	for (k = (FIPS_mode() ? kexalgs_fips : kexalgs); k->name != NULL; k++) {
Petr Lautrbach 802815
 		if (strcmp(k->name, name) == 0)
Petr Lautrbach 802815
 			return k;
Petr Lautrbach 802815
 #ifdef GSSAPI
Petr Lautrbach 802815
@@ -151,7 +171,10 @@ kex_names_valid(const char *names)
Petr Lautrbach 802815
 	for ((p = strsep(&cp, ",")); p && *p != '\0';
Petr Lautrbach 802815
 	    (p = strsep(&cp, ","))) {
Petr Lautrbach 802815
 		if (kex_alg_by_name(p) == NULL) {
Petr Lautrbach 802815
-			error("Unsupported KEX algorithm \"%.100s\"", p);
Petr Lautrbach 802815
+			if (FIPS_mode())
Petr Lautrbach 802815
+				error("\"%.100s\" is not allowed in FIPS mode", p);
Petr Lautrbach 802815
+			else
Petr Lautrbach 802815
+				error("Unsupported KEX algorithm \"%.100s\"", p);
Petr Lautrbach 802815
 			free(s);
Petr Lautrbach 802815
 			return 0;
Petr Lautrbach 802815
 		}
Petr Lautrbach 802815
diff --git a/kexecdhc.c b/kexecdhc.c
Petr Lautrbach 802815
index 2f7629c..20c9946 100644
Petr Lautrbach 802815
--- a/kexecdhc.c
Petr Lautrbach 802815
+++ b/kexecdhc.c
Petr Lautrbach 802815
@@ -154,6 +154,7 @@ kexecdh_client(Kex *kex)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
Petr Lautrbach 802815
 	BN_clear_free(shared_secret);
Petr Lautrbach 802815
+	memset(hash, 0, hashlen);
Petr Lautrbach 802815
 	kex_finish(kex);
Petr Lautrbach 802815
 }
Petr Lautrbach 802815
 #else /* OPENSSL_HAS_ECC */
Petr Lautrbach 802815
diff --git a/kexecdhs.c b/kexecdhs.c
Petr Lautrbach 802815
index 2700b72..0820894 100644
Petr Lautrbach 802815
--- a/kexecdhs.c
Petr Lautrbach 802815
+++ b/kexecdhs.c
Petr Lautrbach 802815
@@ -150,6 +150,7 @@ kexecdh_server(Kex *kex)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
Petr Lautrbach 802815
 	BN_clear_free(shared_secret);
Petr Lautrbach 802815
+	memset(hash, 0, hashlen);
Petr Lautrbach 802815
 	kex_finish(kex);
Petr Lautrbach 802815
 }
Petr Lautrbach 802815
 #else /* OPENSSL_HAS_ECC */
Petr Lautrbach 802815
diff --git a/kexgexc.c b/kexgexc.c
Petr Lautrbach 802815
index 355b7ba..427e11f 100644
Petr Lautrbach 802815
--- a/kexgexc.c
Petr Lautrbach 802815
+++ b/kexgexc.c
Petr Lautrbach 802815
@@ -26,6 +26,8 @@
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #include "includes.h"
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+#include <openssl/fips.h>
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 #include <sys/types.h>
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #include <openssl/dh.h>
Petr Lautrbach 802815
@@ -64,13 +66,13 @@ kexgex_client(Kex *kex)
Petr Lautrbach 802815
 		/* Old GEX request */
Petr Lautrbach 802815
 		packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
Petr Lautrbach 802815
 		packet_put_int(nbits);
Petr Lautrbach 802815
-		min = DH_GRP_MIN;
Petr Lautrbach 802815
+		min = FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN;
Petr Lautrbach 802815
 		max = DH_GRP_MAX;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 		debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD(%u) sent", nbits);
Petr Lautrbach 802815
 	} else {
Petr Lautrbach 802815
 		/* New GEX request */
Petr Lautrbach 802815
-		min = DH_GRP_MIN;
Petr Lautrbach 802815
+		min = FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN;
Petr Lautrbach 802815
 		max = DH_GRP_MAX;
Petr Lautrbach 802815
 		packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
Petr Lautrbach 802815
 		packet_put_int(min);
Petr Lautrbach 802815
diff --git a/kexgexs.c b/kexgexs.c
Petr Lautrbach 802815
index 770ad28..9d4fc6d 100644
Petr Lautrbach 802815
--- a/kexgexs.c
Petr Lautrbach 802815
+++ b/kexgexs.c
Petr Lautrbach 802815
@@ -76,16 +76,16 @@ kexgex_server(Kex *kex)
Petr Lautrbach 802815
 		omin = min = packet_get_int();
Petr Lautrbach 802815
 		onbits = nbits = packet_get_int();
Petr Lautrbach 802815
 		omax = max = packet_get_int();
Petr Lautrbach 802815
-		min = MAX(DH_GRP_MIN, min);
Petr Lautrbach 802815
+		min = MAX(FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN, min);
Petr Lautrbach 802815
 		max = MIN(DH_GRP_MAX, max);
Petr Lautrbach 802815
-		nbits = MAX(DH_GRP_MIN, nbits);
Petr Lautrbach 802815
+		nbits = MAX(FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN, nbits);
Petr Lautrbach 802815
 		nbits = MIN(DH_GRP_MAX, nbits);
Petr Lautrbach 802815
 		break;
Petr Lautrbach 802815
 	case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
Petr Lautrbach 802815
 		debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
Petr Lautrbach 802815
 		onbits = nbits = packet_get_int();
Petr Lautrbach 802815
 		/* unused for old GEX */
Petr Lautrbach 802815
-		omin = min = DH_GRP_MIN;
Petr Lautrbach 802815
+		omin = min = FIPS_mode() ? DH_GRP_MIN_FIPS : DH_GRP_MIN;
Petr Lautrbach 802815
 		omax = max = DH_GRP_MAX;
Petr Lautrbach 802815
 		break;
Petr Lautrbach 802815
 	default:
Petr Lautrbach 802815
diff --git a/key.c b/key.c
Petr Lautrbach 802815
index 62f3edb..c13b644 100644
Petr Lautrbach 802815
--- a/key.c
Petr Lautrbach 802815
+++ b/key.c
Petr Lautrbach 802815
@@ -42,6 +42,7 @@
Petr Lautrbach 802815
 #include "crypto_api.h"
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #include <openssl/evp.h>
Petr Lautrbach 802815
+#include <openssl/fips.h>
Petr Lautrbach 802815
 #include <openbsd-compat/openssl-compat.h>
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #include <stdarg.h>
Petr Lautrbach 802815
@@ -636,9 +637,13 @@ key_fingerprint_selection(void)
Petr Lautrbach 802815
 	char *env;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	if (!rv_defined) {
Petr Lautrbach 802815
-		env = getenv("SSH_FINGERPRINT_TYPE");
Petr Lautrbach 802815
-		rv = (env && !strcmp (env, "sha")) ?
Petr Lautrbach 802815
-			SSH_FP_SHA1 : SSH_FP_MD5;
Petr Lautrbach 802815
+		if (FIPS_mode())
Petr Lautrbach 802815
+			rv = SSH_FP_SHA1;
Petr Lautrbach 802815
+		else {
Petr Lautrbach 802815
+			env = getenv("SSH_FINGERPRINT_TYPE");
Petr Lautrbach 802815
+			rv = (env && !strcmp (env, "sha")) ?
Petr Lautrbach 802815
+				SSH_FP_SHA1 : SSH_FP_MD5;
Petr Lautrbach 802815
+		}
Petr Lautrbach 802815
 		rv_defined = 1;
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
 	return rv;
Petr Lautrbach 802815
diff --git a/mac.c b/mac.c
Petr Lautrbach 802815
index 9388af4..cd7b034 100644
Petr Lautrbach 802815
--- a/mac.c
Petr Lautrbach 802815
+++ b/mac.c
Petr Lautrbach 802815
@@ -27,6 +27,8 @@
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #include <sys/types.h>
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+#include <openssl/fips.h>
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 #include <stdarg.h>
Petr Lautrbach 802815
 #include <string.h>
Petr Lautrbach 802815
 #include <signal.h>
Petr Lautrbach 802815
@@ -60,7 +62,7 @@ struct macalg {
Petr Lautrbach 802815
 	int		etm;		/* Encrypt-then-MAC */
Petr Lautrbach 802815
 };
Petr Lautrbach 802815
 
Petr Lautrbach 802815
-static const struct macalg macs[] = {
Petr Lautrbach 802815
+static const struct macalg all_macs[] = {
Petr Lautrbach 802815
 	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
Petr Lautrbach 802815
 	{ "hmac-sha1",				SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
Petr Lautrbach 802815
 	{ "hmac-sha1-96",			SSH_DIGEST, SSH_DIGEST_SHA1, 96, 0, 0, 0 },
Petr Lautrbach 802815
@@ -91,6 +93,24 @@ static const struct macalg macs[] = {
Petr Lautrbach 802815
 	{ NULL,					0, 0, 0, 0, 0, 0 }
Petr Lautrbach 802815
 };
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+static const struct macalg fips_macs[] = {
Petr Lautrbach 802815
+	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
Petr Lautrbach 802815
+	{ "hmac-sha1",				SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 0 },
Petr Lautrbach 802815
+#ifdef HAVE_EVP_SHA256
Petr Lautrbach 802815
+	{ "hmac-sha2-256",			SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 0 },
Petr Lautrbach 802815
+	{ "hmac-sha2-512",			SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 0 },
Petr Lautrbach 802815
+#endif
Petr Lautrbach 802815
+
Petr Lautrbach 802815
+	/* Encrypt-then-MAC variants */
Petr Lautrbach 802815
+	{ "hmac-sha1-etm@openssh.com",		SSH_DIGEST, SSH_DIGEST_SHA1, 0, 0, 0, 1 },
Petr Lautrbach 802815
+#ifdef HAVE_EVP_SHA256
Petr Lautrbach 802815
+	{ "hmac-sha2-256-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA256, 0, 0, 0, 1 },
Petr Lautrbach 802815
+	{ "hmac-sha2-512-etm@openssh.com",	SSH_DIGEST, SSH_DIGEST_SHA512, 0, 0, 0, 1 },
Petr Lautrbach 802815
+#endif
Petr Lautrbach 802815
+
Petr Lautrbach 802815
+	{ NULL,					0, 0, 0, 0, 0, 0 }
Petr Lautrbach 802815
+};
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 /* Returns a list of supported MACs separated by the specified char. */
Petr Lautrbach 802815
 char *
Petr Lautrbach 802815
 mac_alg_list(char sep)
Petr Lautrbach 802815
@@ -99,7 +119,7 @@ mac_alg_list(char sep)
Petr Lautrbach 802815
 	size_t nlen, rlen = 0;
Petr Lautrbach 802815
 	const struct macalg *m;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
-	for (m = macs; m->name != NULL; m++) {
Petr Lautrbach 802815
+	for (m = FIPS_mode() ? fips_macs : all_macs; m->name != NULL; m++) {
Petr Lautrbach 802815
 		if (ret != NULL)
Petr Lautrbach 802815
 			ret[rlen++] = sep;
Petr Lautrbach 802815
 		nlen = strlen(m->name);
Petr Lautrbach 802815
@@ -133,7 +153,7 @@ mac_setup(Mac *mac, char *name)
Petr Lautrbach 802815
 {
Petr Lautrbach 802815
 	const struct macalg *m;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
-	for (m = macs; m->name != NULL; m++) {
Petr Lautrbach 802815
+	for (m = FIPS_mode() ? fips_macs : all_macs; m->name != NULL; m++) {
Petr Lautrbach 802815
 		if (strcmp(name, m->name) != 0)
Petr Lautrbach 802815
 			continue;
Petr Lautrbach 802815
 		if (mac != NULL) {
Petr Lautrbach 802815
diff --git a/myproposal.h b/myproposal.h
Petr Lautrbach 802815
index 3a0f5ae..4f35a44 100644
Petr Lautrbach 802815
--- a/myproposal.h
Petr Lautrbach 802815
+++ b/myproposal.h
Petr Lautrbach 802815
@@ -88,6 +88,12 @@
Petr Lautrbach 802815
 	"diffie-hellman-group14-sha1," \
Petr Lautrbach 802815
 	"diffie-hellman-group1-sha1"
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+#define KEX_DEFAULT_KEX_FIPS		\
Petr Lautrbach 802815
+	KEX_ECDH_METHODS \
Petr Lautrbach 802815
+	KEX_SHA256_METHODS \
Petr Lautrbach 802815
+	"diffie-hellman-group-exchange-sha1," \
Petr Lautrbach 802815
+	"diffie-hellman-group14-sha1"
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 #define	KEX_DEFAULT_PK_ALG	\
Petr Lautrbach 802815
 	HOSTKEY_ECDSA_CERT_METHODS \
Petr Lautrbach 802815
 	"ssh-ed25519-cert-v01@openssh.com," \
Petr Lautrbach 802815
@@ -133,6 +139,22 @@
Petr Lautrbach 802815
 #define	KEX_DEFAULT_COMP	"none,zlib@openssh.com,zlib"
Petr Lautrbach 802815
 #define	KEX_DEFAULT_LANG	""
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+#define	KEX_FIPS_ENCRYPT \
Petr Lautrbach 802815
+	"aes128-ctr,aes192-ctr,aes256-ctr," \
Petr Lautrbach 802815
+	"aes128-cbc,3des-cbc," \
Petr Lautrbach 802815
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se"
Petr Lautrbach 802815
+#ifdef HAVE_EVP_SHA256
Petr Lautrbach 802815
+#define	KEX_FIPS_MAC \
Petr Lautrbach 802815
+	"hmac-sha1," \
Petr Lautrbach 802815
+	"hmac-sha2-256," \
Petr Lautrbach 802815
+	"hmac-sha2-512," \
Petr Lautrbach 802815
+	"hmac-sha1-etm@openssh.com," \
Petr Lautrbach 802815
+	"hmac-sha2-256-etm@openssh.com," \
Petr Lautrbach 802815
+	"hmac-sha2-512-etm@openssh.com"
Petr Lautrbach 802815
+#else
Petr Lautrbach 802815
+#define        KEX_FIPS_MAC \
Petr Lautrbach 802815
+       "hmac-sha1"
Petr Lautrbach 802815
+#endif
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 static char *myproposal[PROPOSAL_MAX] = {
Petr Lautrbach 802815
 	KEX_DEFAULT_KEX,
Petr Lautrbach 802815
diff --git a/ssh-keygen.c b/ssh-keygen.c
Petr Lautrbach 802815
index 482dc1c..fd2eb94 100644
Petr Lautrbach 802815
--- a/ssh-keygen.c
Petr Lautrbach 802815
+++ b/ssh-keygen.c
Petr Lautrbach 802815
@@ -195,6 +195,14 @@ type_bits_valid(int type, u_int32_t *bitsp)
Petr Lautrbach 802815
 		fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
Petr Lautrbach 802815
 		exit(1);
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
+	if (FIPS_mode()) {
Petr Lautrbach 802815
+		if (type == KEY_DSA)
Petr Lautrbach 802815
+			fatal("DSA keys are not allowed in FIPS mode");
Petr Lautrbach 802815
+		if (type == KEY_RSA && bits != 2048 && bits != 3072)
Petr Lautrbach 802815
+			fatal("RSA keys must be either 2048 bits or 3072 bits in FIPS mode");
Petr Lautrbach 802815
+		if (type == KEY_ED25519)
Petr Lautrbach 802815
+			fatal("ED25519 keys are not allowed in FIPS mode");
Petr Lautrbach 802815
+	}
Petr Lautrbach 802815
 	if (type == KEY_DSA && *bitsp != 1024)
Petr Lautrbach 802815
 		fatal("DSA keys must be 1024 bits");
Petr Lautrbach 802815
 	else if (type != KEY_ECDSA && type != KEY_ED25519 && *bitsp < 768)
Petr Lautrbach 802815
@@ -746,7 +754,7 @@ do_download(struct passwd *pw)
Petr Lautrbach 802815
 	enum fp_type fptype;
Petr Lautrbach 802815
 	char *fp, *ra;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
-	fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
Petr Lautrbach 802815
+	fptype = print_bubblebabble ? SSH_FP_SHA1 : key_fingerprint_selection();
Petr Lautrbach 802815
 	rep =    print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	pkcs11_init(0);
Petr Lautrbach 802815
@@ -756,8 +764,7 @@ do_download(struct passwd *pw)
Petr Lautrbach 802815
 	for (i = 0; i < nkeys; i++) {
Petr Lautrbach 802815
 		if (print_fingerprint) {
Petr Lautrbach 802815
 			fp = key_fingerprint(keys[i], fptype, rep);
Petr Lautrbach 802815
-			ra = key_fingerprint(keys[i], SSH_FP_MD5,
Petr Lautrbach 802815
-			    SSH_FP_RANDOMART);
Petr Lautrbach 802815
+			ra = key_selected_fingerprint(keys[i], SSH_FP_RANDOMART);
Petr Lautrbach 802815
 			printf("%u %s %s (PKCS11 key)\n", key_size(keys[i]),
Petr Lautrbach 802815
 			    fp, key_type(keys[i]));
Petr Lautrbach 802815
 			if (log_level >= SYSLOG_LEVEL_VERBOSE)
Petr Lautrbach 802815
diff --git a/ssh.c b/ssh.c
Petr Lautrbach 802815
index 1e6cb90..ea9193f 100644
Petr Lautrbach 802815
--- a/ssh.c
Petr Lautrbach 802815
+++ b/ssh.c
Petr Lautrbach 802815
@@ -73,6 +73,8 @@
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #include <openssl/evp.h>
Petr Lautrbach 802815
 #include <openssl/err.h>
Petr Lautrbach 802815
+#include <openssl/fips.h>
Petr Lautrbach 802815
+#include <fipscheck.h>
Petr Lautrbach 802815
 #include "openbsd-compat/openssl-compat.h"
Petr Lautrbach 802815
 #include "openbsd-compat/sys-queue.h"
Petr Lautrbach 802815
 
Petr Lautrbach 802815
@@ -427,6 +429,13 @@ main(int ac, char **av)
Petr Lautrbach 802815
 	sanitise_stdfd();
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	__progname = ssh_get_progname(av[0]);
Petr Lautrbach 802815
+        SSLeay_add_all_algorithms();
Petr Lautrbach 802815
+	if (access("/etc/system-fips", F_OK) == 0)
Petr Lautrbach 802815
+		if (! FIPSCHECK_verify(NULL, NULL))
Petr Lautrbach 802815
+			if (FIPS_mode())
Petr Lautrbach 802815
+				fatal("FIPS integrity verification test failed.");
Petr Lautrbach 802815
+			else
Petr Lautrbach 802815
+				logit("FIPS integrity verification test failed.");
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #ifndef HAVE_SETPROCTITLE
Petr Lautrbach 802815
 	/* Prepare for later setproctitle emulation */
Petr Lautrbach 802815
@@ -504,6 +513,9 @@ main(int ac, char **av)
Petr Lautrbach 802815
 	    "ACD:E:F:I:KL:MNO:PQ:R:S:TVw:W:XYy")) != -1) {
Petr Lautrbach 802815
 		switch (opt) {
Petr Lautrbach 802815
 		case '1':
Petr Lautrbach 802815
+			if (FIPS_mode()) {
Petr Lautrbach 802815
+				fatal("Protocol 1 not allowed in the FIPS mode.");
Petr Lautrbach 802815
+			}
Petr Lautrbach 802815
 			options.protocol = SSH_PROTO_1;
Petr Lautrbach 802815
 			break;
Petr Lautrbach 802815
 		case '2':
Petr Lautrbach 802815
@@ -828,7 +840,6 @@ main(int ac, char **av)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	host_arg = xstrdup(host);
Petr Lautrbach 802815
 
Petr Lautrbach 802815
-	OpenSSL_add_all_algorithms();
Petr Lautrbach 802815
 	ERR_load_crypto_strings();
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	/* Initialize the command to execute on remote host. */
Petr Lautrbach 802815
@@ -973,6 +984,10 @@ main(int ac, char **av)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	seed_rng();
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+	if (FIPS_mode()) {
Petr Lautrbach 802815
+		logit("FIPS mode initialized");
Petr Lautrbach 802815
+	}
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 	if (options.user == NULL)
Petr Lautrbach 802815
 		options.user = xstrdup(pw->pw_name);
Petr Lautrbach 802815
 
Petr Lautrbach 802815
@@ -1020,6 +1035,12 @@ main(int ac, char **av)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	timeout_ms = options.connection_timeout * 1000;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+	if (FIPS_mode()) {
Petr Lautrbach 802815
+		options.protocol &= SSH_PROTO_2;
Petr Lautrbach 802815
+		if (options.protocol == 0)
Petr Lautrbach 802815
+			fatal("Protocol 2 disabled by configuration but required in the FIPS mode.");
Petr Lautrbach 802815
+	}
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 	/* Open a connection to the remote host. */
Petr Lautrbach 802815
 	if (ssh_connect(host, addrs, &hostaddr, options.port,
Petr Lautrbach 802815
 	    options.address_family, options.connection_attempts,
Petr Lautrbach 802815
diff --git a/sshconnect2.c b/sshconnect2.c
Petr Lautrbach 802815
index b00658b..6a1562c 100644
Petr Lautrbach 802815
--- a/sshconnect2.c
Petr Lautrbach 802815
+++ b/sshconnect2.c
Petr Lautrbach 802815
@@ -44,6 +44,8 @@
Petr Lautrbach 802815
 #include <vis.h>
Petr Lautrbach 802815
 #endif
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+#include <openssl/fips.h>
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 #include "openbsd-compat/sys-queue.h"
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #include "xmalloc.h"
Petr Lautrbach 802815
@@ -168,20 +170,25 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #ifdef GSSAPI
Petr Lautrbach 802815
 	if (options.gss_keyex) {
Petr Lautrbach 802815
-		/* Add the GSSAPI mechanisms currently supported on this 
Petr Lautrbach 802815
-		 * client to the key exchange algorithm proposal */
Petr Lautrbach 802815
-		orig = myproposal[PROPOSAL_KEX_ALGS];
Petr Lautrbach 802815
-
Petr Lautrbach 802815
-		if (options.gss_trust_dns)
Petr Lautrbach 802815
-			gss_host = (char *)get_canonical_hostname(1);
Petr Lautrbach 802815
-		else
Petr Lautrbach 802815
-			gss_host = host;
Petr Lautrbach 802815
-
Petr Lautrbach 802815
-		gss = ssh_gssapi_client_mechanisms(gss_host, options.gss_client_identity);
Petr Lautrbach 802815
-		if (gss) {
Petr Lautrbach 802815
-			debug("Offering GSSAPI proposal: %s", gss);
Petr Lautrbach 802815
-			xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
Petr Lautrbach 802815
-			    "%s,%s", gss, orig);
Petr Lautrbach 802815
+		if (FIPS_mode()) {
Petr Lautrbach 802815
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
Petr Lautrbach 802815
+			options.gss_keyex = 0;
Petr Lautrbach 802815
+		} else {
Petr Lautrbach 802815
+			/* Add the GSSAPI mechanisms currently supported on this
Petr Lautrbach 802815
+			 * client to the key exchange algorithm proposal */
Petr Lautrbach 802815
+			orig = myproposal[PROPOSAL_KEX_ALGS];
Petr Lautrbach 802815
+
Petr Lautrbach 802815
+			if (options.gss_trust_dns)
Petr Lautrbach 802815
+				gss_host = (char *)get_canonical_hostname(1);
Petr Lautrbach 802815
+			else
Petr Lautrbach 802815
+				gss_host = host;
Petr Lautrbach 802815
+
Petr Lautrbach 802815
+			gss = ssh_gssapi_client_mechanisms(gss_host, options.gss_client_identity);
Petr Lautrbach 802815
+			if (gss) {
Petr Lautrbach 802815
+				debug("Offering GSSAPI proposal: %s", gss);
Petr Lautrbach 802815
+				xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
Petr Lautrbach 802815
+				    "%s,%s", gss, orig);
Petr Lautrbach 802815
+			}
Petr Lautrbach 802815
 		}
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
 #endif
Petr Lautrbach 802815
@@ -193,6 +200,10 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Petr Lautrbach 802815
 	if (options.ciphers != NULL) {
Petr Lautrbach 802815
 		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Petr Lautrbach 802815
 		myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
Petr Lautrbach 802815
+	} else if (FIPS_mode()) {
Petr Lautrbach 802815
+		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Petr Lautrbach 802815
+		myproposal[PROPOSAL_ENC_ALGS_STOC] = KEX_FIPS_ENCRYPT;
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
 	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Petr Lautrbach 802815
 	    compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
Petr Lautrbach 802815
@@ -208,7 +219,11 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Petr Lautrbach 802815
 	if (options.macs != NULL) {
Petr Lautrbach 802815
 		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
Petr Lautrbach 802815
 		myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
Petr Lautrbach 802815
+	} else if (FIPS_mode()) {
Petr Lautrbach 802815
+		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
Petr Lautrbach 802815
+		myproposal[PROPOSAL_MAC_ALGS_STOC] = KEX_FIPS_MAC;
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 	if (options.hostkeyalgorithms != NULL)
Petr Lautrbach 802815
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Petr Lautrbach 802815
 		    compat_pkalg_proposal(options.hostkeyalgorithms);
Petr Lautrbach 802815
@@ -220,9 +235,11 @@ ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
 	if (options.kex_algorithms != NULL)
Petr Lautrbach 802815
 		myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
Petr Lautrbach 802815
+	else if (FIPS_mode())
Petr Lautrbach 802815
+		myproposal[PROPOSAL_KEX_ALGS] = KEX_DEFAULT_KEX_FIPS;
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 	myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
Petr Lautrbach 802815
 	    myproposal[PROPOSAL_KEX_ALGS]);
Petr Lautrbach 802815
-
Petr Lautrbach 802815
 #ifdef GSSAPI
Petr Lautrbach 802815
 	/* If we've got GSSAPI algorithms, then we also support the
Petr Lautrbach 802815
 	 * 'null' hostkey, as a last resort */
Petr Lautrbach 802815
diff --git a/sshd.c b/sshd.c
Petr Lautrbach 802815
index b561ec8..e977de3 100644
Petr Lautrbach 802815
--- a/sshd.c
Petr Lautrbach 802815
+++ b/sshd.c
Petr Lautrbach 802815
@@ -75,6 +75,8 @@
Petr Lautrbach 802815
 #include <openssl/dh.h>
Petr Lautrbach 802815
 #include <openssl/bn.h>
Petr Lautrbach 802815
 #include <openssl/rand.h>
Petr Lautrbach 802815
+#include <openssl/fips.h>
Petr Lautrbach 802815
+#include <fipscheck.h>
Petr Lautrbach 802815
 #include "openbsd-compat/openssl-compat.h"
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #ifdef HAVE_SECUREWARE
Petr Lautrbach 802815
@@ -1468,6 +1470,18 @@ main(int ac, char **av)
Petr Lautrbach 802815
 #endif
Petr Lautrbach 802815
 	__progname = ssh_get_progname(av[0]);
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+        SSLeay_add_all_algorithms();
Petr Lautrbach 802815
+	if (access("/etc/system-fips", F_OK) == 0)
Petr Lautrbach 802815
+		if (! FIPSCHECK_verify(NULL, NULL)) {
Petr Lautrbach 802815
+			openlog(__progname, LOG_PID, LOG_AUTHPRIV);
Petr Lautrbach 802815
+			if (FIPS_mode()) {
Petr Lautrbach 802815
+				syslog(LOG_CRIT, "FIPS integrity verification test failed.");
Petr Lautrbach 802815
+				cleanup_exit(255);
Petr Lautrbach 802815
+			}
Petr Lautrbach 802815
+			else
Petr Lautrbach 802815
+				syslog(LOG_INFO, "FIPS integrity verification test failed.");
Petr Lautrbach 802815
+			closelog();
Petr Lautrbach 802815
+		}
Petr Lautrbach 802815
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Petr Lautrbach 802815
 	saved_argc = ac;
Petr Lautrbach 802815
 	rexec_argc = ac;
Petr Lautrbach 802815
@@ -1619,8 +1633,6 @@ main(int ac, char **av)
Petr Lautrbach 802815
 	else
Petr Lautrbach 802815
 		closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
Petr Lautrbach 802815
 
Petr Lautrbach 802815
-	OpenSSL_add_all_algorithms();
Petr Lautrbach 802815
-
Petr Lautrbach 802815
 	/* If requested, redirect the logs to the specified logfile. */
Petr Lautrbach 802815
 	if (logfile != NULL) {
Petr Lautrbach 802815
 		log_redirect_stderr_to(logfile);
Petr Lautrbach 802815
@@ -1798,6 +1810,10 @@ main(int ac, char **av)
Petr Lautrbach 802815
 		debug("private host key: #%d type %d %s", i, keytype,
Petr Lautrbach 802815
 		    key_type(key ? key : pubkey));
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
+	if ((options.protocol & SSH_PROTO_1) && FIPS_mode()) {
Petr Lautrbach 802815
+		logit("Disabling protocol version 1. Not allowed in the FIPS mode.");
Petr Lautrbach 802815
+		options.protocol &= ~SSH_PROTO_1;
Petr Lautrbach 802815
+	}
Petr Lautrbach 802815
 	if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
Petr Lautrbach 802815
 		logit("Disabling protocol version 1. Could not load host key");
Petr Lautrbach 802815
 		options.protocol &= ~SSH_PROTO_1;
Petr Lautrbach 802815
@@ -1961,6 +1977,10 @@ main(int ac, char **av)
Petr Lautrbach 802815
 	/* Reinitialize the log (because of the fork above). */
Petr Lautrbach 802815
 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
Petr Lautrbach 802815
 
Petr Lautrbach 802815
+	if (FIPS_mode()) {
Petr Lautrbach 802815
+		logit("FIPS mode initialized");
Petr Lautrbach 802815
+	}
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 	/* Chdir to the root directory so that the current disk can be
Petr Lautrbach 802815
 	   unmounted if desired. */
Petr Lautrbach 802815
 	if (chdir("/") == -1)
Petr Lautrbach 802815
@@ -2530,6 +2550,9 @@ do_ssh2_kex(void)
Petr Lautrbach 802815
 	if (options.ciphers != NULL) {
Petr Lautrbach 802815
 		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Petr Lautrbach 802815
 		myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
Petr Lautrbach 802815
+	} else if (FIPS_mode()) {
Petr Lautrbach 802815
+		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Petr Lautrbach 802815
+		myproposal[PROPOSAL_ENC_ALGS_STOC] = KEX_FIPS_ENCRYPT;
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
 	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Petr Lautrbach 802815
 	    compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
Petr Lautrbach 802815
@@ -2539,6 +2562,9 @@ do_ssh2_kex(void)
Petr Lautrbach 802815
 	if (options.macs != NULL) {
Petr Lautrbach 802815
 		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
Petr Lautrbach 802815
 		myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
Petr Lautrbach 802815
+	} else if (FIPS_mode()) {
Petr Lautrbach 802815
+		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
Petr Lautrbach 802815
+		myproposal[PROPOSAL_MAC_ALGS_STOC] = KEX_FIPS_MAC;
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
 	if (options.compression == COMP_NONE) {
Petr Lautrbach 802815
 		myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Petr Lautrbach 802815
@@ -2549,6 +2575,8 @@ do_ssh2_kex(void)
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
 	if (options.kex_algorithms != NULL)
Petr Lautrbach 802815
 		myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
Petr Lautrbach 802815
+	else if (FIPS_mode())
Petr Lautrbach 802815
+		myproposal[PROPOSAL_KEX_ALGS] = KEX_DEFAULT_KEX_FIPS;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
Petr Lautrbach 802815
 	    myproposal[PROPOSAL_KEX_ALGS]);
Petr Lautrbach 802815
@@ -2575,10 +2603,14 @@ do_ssh2_kex(void)
Petr Lautrbach 802815
 	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
Petr Lautrbach 802815
 		orig = NULL;
Petr Lautrbach 802815
 
Petr Lautrbach 802815
-	if (options.gss_keyex)
Petr Lautrbach 802815
-		gss = ssh_gssapi_server_mechanisms();
Petr Lautrbach 802815
-	else
Petr Lautrbach 802815
-		gss = NULL;
Petr Lautrbach 802815
+	if (options.gss_keyex) {
Petr Lautrbach 802815
+		if (FIPS_mode()) {
Petr Lautrbach 802815
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
Petr Lautrbach 802815
+			options.gss_keyex = 0;
Petr Lautrbach 802815
+		} else {
Petr Lautrbach 802815
+			gss = ssh_gssapi_server_mechanisms();
Petr Lautrbach 802815
+		}
Petr Lautrbach 802815
+	}
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	if (gss && orig)
Petr Lautrbach 802815
 		xasprintf(&newstr, "%s,%s", gss, orig);