Jan F 003cb0b
diff -up openssh-5.8p1/authfile.c.fips openssh-5.8p1/authfile.c
Jan F 003cb0b
--- openssh-5.8p1/authfile.c.fips	2010-12-01 02:03:39.000000000 +0100
Jan F 48446f1
+++ openssh-5.8p1/authfile.c	2011-02-25 09:23:19.000000000 +0100
Jan F 003cb0b
@@ -145,8 +145,14 @@ key_private_rsa1_to_blob(Key *key, Buffe
76f329e
 	/* Allocate space for the private part of the key in the buffer. */
76f329e
 	cp = buffer_append_space(&encrypted, buffer_len(&buffer));
76f329e
 
76f329e
-	cipher_set_key_string(&ciphercontext, cipher, passphrase,
76f329e
-	    CIPHER_ENCRYPT);
76f329e
+	if (cipher_set_key_string(&ciphercontext, cipher, passphrase,
76f329e
+	    CIPHER_ENCRYPT) < 0) {
76f329e
+	    error("cipher_set_key_string failed.");
76f329e
+	    buffer_free(&encrypted);
76f329e
+	    buffer_free(&buffer);
76f329e
+	    return 0;
76f329e
+	}
76f329e
+
76f329e
 	cipher_crypt(&ciphercontext, cp,
76f329e
 	    buffer_ptr(&buffer), buffer_len(&buffer));
76f329e
 	cipher_cleanup(&ciphercontext);
Jan F 003cb0b
@@ -447,8 +453,13 @@ key_parse_private_rsa1(Buffer *blob, con
Jan F 003cb0b
 	cp = buffer_append_space(&decrypted, buffer_len(blob));
76f329e
 
76f329e
 	/* Rest of the buffer is encrypted.  Decrypt it using the passphrase. */
76f329e
-	cipher_set_key_string(&ciphercontext, cipher, passphrase,
76f329e
-	    CIPHER_DECRYPT);
76f329e
+	if (cipher_set_key_string(&ciphercontext, cipher, passphrase,
76f329e
+	    CIPHER_DECRYPT) < 0) {
76f329e
+	    error("cipher_set_key_string failed.");
76f329e
+	    buffer_free(&decrypted);
76f329e
+	    goto fail;
76f329e
+	}
76f329e
+
76f329e
 	cipher_crypt(&ciphercontext, cp,
Jan F 003cb0b
 	    buffer_ptr(blob), buffer_len(blob));
76f329e
 	cipher_cleanup(&ciphercontext);
Jan F 003cb0b
diff -up openssh-5.8p1/cipher.c.fips openssh-5.8p1/cipher.c
Jan F 48446f1
--- openssh-5.8p1/cipher.c.fips	2011-02-25 09:23:18.000000000 +0100
Jan F 48446f1
+++ openssh-5.8p1/cipher.c	2011-02-25 09:23:19.000000000 +0100
0a4fa5d
@@ -40,6 +40,7 @@
0a4fa5d
 #include <sys/types.h>
d93958d
 
0a4fa5d
 #include <openssl/md5.h>
d93958d
+#include <openssl/fips.h>
adad2a8
 
0a4fa5d
 #include <string.h>
0a4fa5d
 #include <stdarg.h>
Jan F af87384
@@ -85,6 +86,22 @@ struct Cipher ciphers[] = {
0a4fa5d
 	{ NULL,			SSH_CIPHER_INVALID, 0, 0, 0, 0, NULL }
0a4fa5d
 };
adad2a8
 
0a4fa5d
+struct Cipher fips_ciphers[] = {
0a4fa5d
+	{ "none",		SSH_CIPHER_NONE, 8, 0, 0, 0, EVP_enc_null },
0a4fa5d
+	{ "3des",		SSH_CIPHER_3DES, 8, 16, 0, 1, evp_ssh1_3des },
d93958d
+
0a4fa5d
+	{ "3des-cbc",		SSH_CIPHER_SSH2, 8, 24, 0, 1, EVP_des_ede3_cbc },
0a4fa5d
+	{ "aes128-cbc",		SSH_CIPHER_SSH2, 16, 16, 0, 1, EVP_aes_128_cbc },
0a4fa5d
+	{ "aes192-cbc",		SSH_CIPHER_SSH2, 16, 24, 0, 1, EVP_aes_192_cbc },
0a4fa5d
+	{ "aes256-cbc",		SSH_CIPHER_SSH2, 16, 32, 0, 1, EVP_aes_256_cbc },
0a4fa5d
+	{ "rijndael-cbc@lysator.liu.se",
0a4fa5d
+				SSH_CIPHER_SSH2, 16, 32, 0, 1, EVP_aes_256_cbc },
0a4fa5d
+	{ "aes128-ctr",		SSH_CIPHER_SSH2, 16, 16, 0, 0, evp_aes_128_ctr },
0a4fa5d
+	{ "aes192-ctr",		SSH_CIPHER_SSH2, 16, 24, 0, 0, evp_aes_128_ctr },
0a4fa5d
+	{ "aes256-ctr",		SSH_CIPHER_SSH2, 16, 32, 0, 0, evp_aes_128_ctr },
0a4fa5d
+	{ NULL,			SSH_CIPHER_INVALID, 0, 0, 0, 0, NULL }
0a4fa5d
+};
adad2a8
+
0a4fa5d
 /*--*/
adad2a8
 
0a4fa5d
 u_int
Jan F af87384
@@ -127,7 +144,7 @@ Cipher *
0a4fa5d
 cipher_by_name(const char *name)
0a4fa5d
 {
0a4fa5d
 	Cipher *c;
0a4fa5d
-	for (c = ciphers; c->name != NULL; c++)
0a4fa5d
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
0a4fa5d
 		if (strcmp(c->name, name) == 0)
0a4fa5d
 			return c;
0a4fa5d
 	return NULL;
Jan F af87384
@@ -137,7 +154,7 @@ Cipher *
0a4fa5d
 cipher_by_number(int id)
0a4fa5d
 {
0a4fa5d
 	Cipher *c;
0a4fa5d
-	for (c = ciphers; c->name != NULL; c++)
0a4fa5d
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
0a4fa5d
 		if (c->number == id)
0a4fa5d
 			return c;
0a4fa5d
 	return NULL;
Jan F af87384
@@ -181,7 +198,7 @@ cipher_number(const char *name)
0a4fa5d
 	Cipher *c;
0a4fa5d
 	if (name == NULL)
0a4fa5d
 		return -1;
0a4fa5d
-	for (c = ciphers; c->name != NULL; c++)
0a4fa5d
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
0a4fa5d
 		if (strcasecmp(c->name, name) == 0)
0a4fa5d
 			return c->number;
0a4fa5d
 	return -1;
Jan F af87384
@@ -288,14 +305,15 @@ cipher_cleanup(CipherContext *cc)
76f329e
  * passphrase and using the resulting 16 bytes as the key.
76f329e
  */
76f329e
 
76f329e
-void
76f329e
+int
76f329e
 cipher_set_key_string(CipherContext *cc, Cipher *cipher,
76f329e
     const char *passphrase, int do_encrypt)
76f329e
 {
76f329e
 	MD5_CTX md;
76f329e
 	u_char digest[16];
76f329e
 
76f329e
-	MD5_Init(&md);
76f329e
+	if (MD5_Init(&md) <= 0)
76f329e
+		return -1;
76f329e
 	MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
76f329e
 	MD5_Final(digest, &md);
76f329e
 
Jan F af87384
@@ -303,6 +321,7 @@ cipher_set_key_string(CipherContext *cc,
76f329e
 
76f329e
 	memset(digest, 0, sizeof(digest));
76f329e
 	memset(&md, 0, sizeof(md));
76f329e
+	return 0;
76f329e
 }
76f329e
 
76f329e
 /*
Jan F 003cb0b
diff -up openssh-5.8p1/cipher-ctr.c.fips openssh-5.8p1/cipher-ctr.c
Jan F 003cb0b
--- openssh-5.8p1/cipher-ctr.c.fips	2010-10-07 13:06:42.000000000 +0200
Jan F 48446f1
+++ openssh-5.8p1/cipher-ctr.c	2011-02-25 09:23:19.000000000 +0100
0a4fa5d
@@ -140,7 +140,8 @@ evp_aes_128_ctr(void)
0a4fa5d
 	aes_ctr.do_cipher = ssh_aes_ctr;
0a4fa5d
 #ifndef SSH_OLD_EVP
0a4fa5d
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
0a4fa5d
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
0a4fa5d
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
0a4fa5d
+	    EVP_CIPH_FLAG_FIPS;
adad2a8
 #endif
0a4fa5d
 	return (&aes_ctr);
0a4fa5d
 }
Jan F 003cb0b
diff -up openssh-5.8p1/cipher.h.fips openssh-5.8p1/cipher.h
Jan F 48446f1
--- openssh-5.8p1/cipher.h.fips	2011-02-25 09:23:18.000000000 +0100
Jan F 48446f1
+++ openssh-5.8p1/cipher.h	2011-02-25 09:23:19.000000000 +0100
Jan F af87384
@@ -87,7 +87,7 @@ void	 cipher_init(CipherContext *, Ciphe
76f329e
     const u_char *, u_int, int);
76f329e
 void	 cipher_crypt(CipherContext *, u_char *, const u_char *, u_int);
76f329e
 void	 cipher_cleanup(CipherContext *);
76f329e
-void	 cipher_set_key_string(CipherContext *, Cipher *, const char *, int);
76f329e
+int	 cipher_set_key_string(CipherContext *, Cipher *, const char *, int);
76f329e
 u_int	 cipher_blocksize(const Cipher *);
76f329e
 u_int	 cipher_keylen(const Cipher *);
76f329e
 u_int	 cipher_is_cbc(const Cipher *);
Jan F f9ff105
diff -up openssh-5.8p1/key.c.fips openssh-5.8p1/key.c
Jan F 48446f1
--- openssh-5.8p1/key.c.fips	2011-02-25 09:23:19.000000000 +0100
Jan F 48446f1
+++ openssh-5.8p1/key.c	2011-02-25 09:24:35.000000000 +0100
Jan F f9ff105
@@ -40,6 +40,7 @@
Jan F f9ff105
 #include <sys/types.h>
Jan F f9ff105
 
Jan F f9ff105
 #include <openssl/evp.h>
Jan F f9ff105
+#include <openssl/fips.h>
Jan F f9ff105
 #include <openbsd-compat/openssl-compat.h>
Jan F f9ff105
 
Jan F f9ff105
 #include <stdarg.h>
Jan F 48446f1
@@ -602,9 +603,13 @@ key_fingerprint_selection(void)
Jan F 48446f1
 	char *env;
Jan F f9ff105
 
Jan F 48446f1
 	if (!rv_defined) {
Jan F 48446f1
-		env = getenv("SSH_FINGERPRINT_TYPE");
Jan F 48446f1
-		rv = (env && !strcmp (env, "sha")) ?
Jan F 48446f1
-			SSH_FP_SHA1 : SSH_FP_MD5;
Jan F f9ff105
+		if (FIPS_mode())
Jan F 48446f1
+			rv = SSH_FP_SHA1;
Jan F 48446f1
+		else {
Jan F 48446f1
+			env = getenv("SSH_FINGERPRINT_TYPE");
Jan F 48446f1
+			rv = (env && !strcmp (env, "sha")) ?
Jan F 48446f1
+				SSH_FP_SHA1 : SSH_FP_MD5;
Jan F 48446f1
+		}
Jan F 48446f1
 		rv_defined = 1;
Jan F f9ff105
 	}
Jan F 48446f1
 	return rv;
Jan F 003cb0b
diff -up openssh-5.8p1/mac.c.fips openssh-5.8p1/mac.c
Jan F 48446f1
--- openssh-5.8p1/mac.c.fips	2011-02-25 09:23:18.000000000 +0100
Jan F 48446f1
+++ openssh-5.8p1/mac.c	2011-02-25 09:23:19.000000000 +0100
d93958d
@@ -28,6 +28,7 @@
d93958d
 #include <sys/types.h>
d93958d
 
d93958d
 #include <openssl/hmac.h>
d93958d
+#include <openssl/fips.h>
d93958d
 
d93958d
 #include <stdarg.h>
d93958d
 #include <string.h>
d93958d
@@ -47,14 +48,14 @@
d93958d
 #define SSH_EVP		1	/* OpenSSL EVP-based MAC */
d93958d
 #define SSH_UMAC	2	/* UMAC (not integrated with OpenSSL) */
d93958d
 
d93958d
-struct {
d93958d
+struct Macs {
d93958d
 	char		*name;
d93958d
 	int		type;
d93958d
 	const EVP_MD *	(*mdfunc)(void);
d93958d
 	int		truncatebits;	/* truncate digest if != 0 */
d93958d
 	int		key_len;	/* just for UMAC */
d93958d
 	int		len;		/* just for UMAC */
d93958d
-} macs[] = {
d93958d
+} all_macs[] = {
d93958d
 	{ "hmac-sha1",			SSH_EVP, EVP_sha1, 0, -1, -1 },
d93958d
 	{ "hmac-sha1-96",		SSH_EVP, EVP_sha1, 96, -1, -1 },
d93958d
 	{ "hmac-md5",			SSH_EVP, EVP_md5, 0, -1, -1 },
d93958d
@@ -65,9 +66,15 @@ struct {
d93958d
 	{ NULL,				0, NULL, 0, -1, -1 }
d93958d
 };
d93958d
 
d93958d
+struct Macs fips_macs[] = {
d93958d
+	{ "hmac-sha1",			SSH_EVP, EVP_sha1, 0, -1, -1 },
d93958d
+	{ NULL,				0, NULL, 0, -1, -1 }
d93958d
+};
d93958d
+
d93958d
 static void
d93958d
 mac_setup_by_id(Mac *mac, int which)
d93958d
 {
d93958d
+	struct Macs *macs = FIPS_mode() ? fips_macs : all_macs;
d93958d
 	int evp_len;
d93958d
 	mac->type = macs[which].type;
d93958d
 	if (mac->type == SSH_EVP) {
d93958d
@@ -88,6 +95,7 @@ int
d93958d
 mac_setup(Mac *mac, char *name)
d93958d
 {
d93958d
 	int i;
d93958d
+	struct Macs *macs = FIPS_mode() ? fips_macs : all_macs;
d93958d
 
d93958d
 	for (i = 0; macs[i].name; i++) {
d93958d
 		if (strcmp(name, macs[i].name) == 0) {
Jan F 003cb0b
diff -up openssh-5.8p1/Makefile.in.fips openssh-5.8p1/Makefile.in
Jan F 48446f1
--- openssh-5.8p1/Makefile.in.fips	2011-02-25 09:23:19.000000000 +0100
Jan F 48446f1
+++ openssh-5.8p1/Makefile.in	2011-02-25 09:23:19.000000000 +0100
Jan F 003cb0b
@@ -145,25 +145,25 @@ libssh.a: $(LIBSSH_OBJS)
0a4fa5d
 	$(RANLIB) $@
d93958d
 
0a4fa5d
 ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
Jan F 003cb0b
-	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS)
Jan F 003cb0b
+	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHLIBS) $(LIBS)
d93958d
 
0a4fa5d
 sshd$(EXEEXT): libssh.a	$(LIBCOMPAT) $(SSHDOBJS)
0a4fa5d
-	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS)
c54a8b0
+	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(LIBS)
d93958d
 
0a4fa5d
 scp$(EXEEXT): $(LIBCOMPAT) libssh.a scp.o progressmeter.o
0a4fa5d
 	$(LD) -o $@ scp.o progressmeter.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
d93958d
 
0a4fa5d
 ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o
0a4fa5d
-	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
0a4fa5d
+	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
a3ba41c
 
974c89c
 ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
974c89c
-	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
82bc825
+	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
a3ba41c
 
0a4fa5d
 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
0a4fa5d
-	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
0a4fa5d
+	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
a3ba41c
 
974c89c
 ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o roaming_dummy.o readconf.o
c54a8b0
-	$(LD) -o $@ ssh-keysign.o readconf.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
c54a8b0
+	$(LD) -o $@ ssh-keysign.o readconf.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
0a4fa5d
 
974c89c
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
Jan F. Chadima 1b8a267
 	$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
Jan F 003cb0b
@@ -172,7 +172,7 @@ ssh-ldap-helper$(EXEEXT): $(LIBCOMPAT) l
3fdf10c
 	$(LD) -o $@ ldapconf.o ldapbody.o ldapmisc.o ldap-helper.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
974c89c
 
c54a8b0
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o roaming_dummy.o
c54a8b0
-	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
c54a8b0
+	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
0a4fa5d
 
0a4fa5d
 sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o
0a4fa5d
 	$(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jan F 003cb0b
diff -up openssh-5.8p1/myproposal.h.fips openssh-5.8p1/myproposal.h
Jan F 003cb0b
--- openssh-5.8p1/myproposal.h.fips	2011-01-13 12:00:22.000000000 +0100
Jan F 48446f1
+++ openssh-5.8p1/myproposal.h	2011-02-25 09:23:19.000000000 +0100
Jan F 003cb0b
@@ -81,7 +81,12 @@
0a4fa5d
 	"hmac-sha1-96,hmac-md5-96"
0a4fa5d
 #define	KEX_DEFAULT_COMP	"none,zlib@openssh.com,zlib"
0a4fa5d
 #define	KEX_DEFAULT_LANG	""
0a4fa5d
-
0a4fa5d
+#define	KEX_FIPS_ENCRYPT \
0a4fa5d
+	"aes128-ctr,aes192-ctr,aes256-ctr," \
0a4fa5d
+	"aes128-cbc,3des-cbc," \
0a4fa5d
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se"
0a4fa5d
+#define	KEX_FIPS_MAC \
0a4fa5d
+	"hmac-sha1"
0a4fa5d
 
0a4fa5d
 static char *myproposal[PROPOSAL_MAX] = {
0a4fa5d
 	KEX_DEFAULT_KEX,
Jan F 003cb0b
diff -up openssh-5.8p1/openbsd-compat/bsd-arc4random.c.fips openssh-5.8p1/openbsd-compat/bsd-arc4random.c
Jan F 003cb0b
--- openssh-5.8p1/openbsd-compat/bsd-arc4random.c.fips	2010-03-25 22:52:02.000000000 +0100
Jan F 48446f1
+++ openssh-5.8p1/openbsd-compat/bsd-arc4random.c	2011-02-25 09:23:19.000000000 +0100
d93958d
@@ -39,6 +39,7 @@
d93958d
 static int rc4_ready = 0;
d93958d
 static RC4_KEY rc4;
d93958d
 
d93958d
+#if 0
d93958d
 unsigned int
d93958d
 arc4random(void)
d93958d
 {
d93958d
@@ -82,6 +83,32 @@ arc4random_stir(void)
d93958d
 
d93958d
 	rc4_ready = REKEY_BYTES;
d93958d
 }
d93958d
+#else
d93958d
+unsigned int
d93958d
+arc4random(void)
d93958d
+{
d93958d
+	unsigned int r = 0;
d93958d
+	void *rp = &r;
d93958d
+
d93958d
+	if (!rc4_ready) {
d93958d
+		arc4random_stir();
d93958d
+	}
d93958d
+	RAND_bytes(rp, sizeof(r));
d93958d
+
d93958d
+	return(r);
d93958d
+}
d93958d
+
d93958d
+void
d93958d
+arc4random_stir(void)
d93958d
+{
d93958d
+	unsigned char rand_buf[SEED_SIZE];
d93958d
+
d93958d
+	if (RAND_bytes(rand_buf, sizeof(rand_buf)) <= 0)
d93958d
+		fatal("Couldn't obtain random bytes (error %ld)",
d93958d
+		    ERR_get_error());
d93958d
+	rc4_ready = 1;
d93958d
+}
d93958d
+#endif
d93958d
 #endif /* !HAVE_ARC4RANDOM */
d93958d
 
82bc825
 #ifndef HAVE_ARC4RANDOM_BUF
Jan F 003cb0b
diff -up openssh-5.8p1/ssh.c.fips openssh-5.8p1/ssh.c
Jan F 003cb0b
--- openssh-5.8p1/ssh.c.fips	2011-02-04 01:42:15.000000000 +0100
Jan F 48446f1
+++ openssh-5.8p1/ssh.c	2011-02-25 09:23:19.000000000 +0100
Jan F 003cb0b
@@ -73,6 +73,8 @@
0a4fa5d
 
0a4fa5d
 #include <openssl/evp.h>
0a4fa5d
 #include <openssl/err.h>
d93958d
+#include <openssl/fips.h>
d93958d
+#include <fipscheck.h>
0a4fa5d
 #include "openbsd-compat/openssl-compat.h"
0a4fa5d
 #include "openbsd-compat/sys-queue.h"
d93958d
 
Jan F 003cb0b
@@ -234,6 +236,10 @@ main(int ac, char **av)
0a4fa5d
 	sanitise_stdfd();
a3ba41c
 
0a4fa5d
 	__progname = ssh_get_progname(av[0]);
adad2a8
+        SSLeay_add_all_algorithms();
d93958d
+        if (FIPS_mode() && !FIPSCHECK_verify(NULL, NULL)) {
0a4fa5d
+                fatal("FIPS integrity verification test failed.");
d93958d
+        }
adad2a8
 	init_rng();
0a4fa5d
 
0a4fa5d
 	/*
Jan F 003cb0b
@@ -300,6 +306,9 @@ main(int ac, char **av)
974c89c
 	    "ACD:F:I:KL:MNO:PR:S:TVw:W:XYy")) != -1) {
76f329e
 		switch (opt) {
76f329e
 		case '1':
76f329e
+			if (FIPS_mode()) {
76f329e
+				fatal("Protocol 1 not allowed in the FIPS mode.");
76f329e
+			}
76f329e
 			options.protocol = SSH_PROTO_1;
76f329e
 			break;
76f329e
 		case '2':
Jan F 003cb0b
@@ -598,7 +607,6 @@ main(int ac, char **av)
0a4fa5d
 	if (!host)
0a4fa5d
 		usage();
0a4fa5d
 
Jan F 003cb0b
-	OpenSSL_add_all_algorithms();
0a4fa5d
 	ERR_load_crypto_strings();
0a4fa5d
 
0a4fa5d
 	/* Initialize the command to execute on remote host. */
Jan F 003cb0b
@@ -684,6 +692,10 @@ main(int ac, char **av)
0a4fa5d
 
adad2a8
 	seed_rng();
0a4fa5d
 
0a4fa5d
+	if (FIPS_mode()) {
0a4fa5d
+		logit("FIPS mode initialized");
0a4fa5d
+	}
0a4fa5d
+
0a4fa5d
 	if (options.user == NULL)
0a4fa5d
 		options.user = xstrdup(pw->pw_name);
0a4fa5d
 
Jan F 003cb0b
@@ -753,6 +765,12 @@ main(int ac, char **av)
76f329e
 
76f329e
 	timeout_ms = options.connection_timeout * 1000;
76f329e
 
76f329e
+	if (FIPS_mode()) {
76f329e
+		options.protocol &= SSH_PROTO_2;
76f329e
+		if (options.protocol == 0)
76f329e
+			fatal("Protocol 2 disabled by configuration but required in the FIPS mode.");
76f329e
+	}
76f329e
+
76f329e
 	/* Open a connection to the remote host. */
76f329e
 	if (ssh_connect(host, &hostaddr, options.port,
76f329e
 	    options.address_family, options.connection_attempts, &timeout_ms,
Jan F 003cb0b
diff -up openssh-5.8p1/sshconnect2.c.fips openssh-5.8p1/sshconnect2.c
Jan F 48446f1
--- openssh-5.8p1/sshconnect2.c.fips	2011-02-25 09:23:18.000000000 +0100
Jan F 48446f1
+++ openssh-5.8p1/sshconnect2.c	2011-02-25 09:23:19.000000000 +0100
0a4fa5d
@@ -44,6 +44,8 @@
0a4fa5d
 #include <vis.h>
0a4fa5d
 #endif
0a4fa5d
 
0a4fa5d
+#include <openssl/fips.h>
0a4fa5d
+
0a4fa5d
 #include "openbsd-compat/sys-queue.h"
0a4fa5d
 
0a4fa5d
 #include "xmalloc.h"
Jan F 003cb0b
@@ -169,6 +171,10 @@ ssh_kex2(char *host, struct sockaddr *ho
0a4fa5d
 	if (options.ciphers != NULL) {
0a4fa5d
 		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
0a4fa5d
 		myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
0a4fa5d
+	} else if (FIPS_mode()) {
0a4fa5d
+		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
0a4fa5d
+		myproposal[PROPOSAL_ENC_ALGS_STOC] = KEX_FIPS_ENCRYPT;
0a4fa5d
+
0a4fa5d
 	}
0a4fa5d
 	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
0a4fa5d
 	    compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
Jan F 003cb0b
@@ -184,7 +190,11 @@ ssh_kex2(char *host, struct sockaddr *ho
0a4fa5d
 	if (options.macs != NULL) {
0a4fa5d
 		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
0a4fa5d
 		myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
0a4fa5d
+	} else if (FIPS_mode()) {
0a4fa5d
+		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
0a4fa5d
+		myproposal[PROPOSAL_MAC_ALGS_STOC] = KEX_FIPS_MAC;
0a4fa5d
 	}
0a4fa5d
+
0a4fa5d
 	if (options.hostkeyalgorithms != NULL)
0a4fa5d
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
0a4fa5d
 		    options.hostkeyalgorithms;
Jan F 003cb0b
diff -up openssh-5.8p1/sshd.c.fips openssh-5.8p1/sshd.c
Jan F 48446f1
--- openssh-5.8p1/sshd.c.fips	2011-02-25 09:23:19.000000000 +0100
Jan F 48446f1
+++ openssh-5.8p1/sshd.c	2011-02-25 09:23:19.000000000 +0100
0a4fa5d
@@ -76,6 +76,8 @@
0a4fa5d
 #include <openssl/bn.h>
0a4fa5d
 #include <openssl/md5.h>
0a4fa5d
 #include <openssl/rand.h>
0a4fa5d
+#include <openssl/fips.h>
0a4fa5d
+#include <fipscheck.h>
0a4fa5d
 #include "openbsd-compat/openssl-compat.h"
0a4fa5d
 
0a4fa5d
 #ifdef HAVE_SECUREWARE
Jan F 48446f1
@@ -1364,6 +1366,12 @@ main(int ac, char **av)
0a4fa5d
 	(void)set_auth_parameters(ac, av);
0a4fa5d
 #endif
0a4fa5d
 	__progname = ssh_get_progname(av[0]);
0a4fa5d
+
0a4fa5d
+        SSLeay_add_all_algorithms();
0a4fa5d
+        if (FIPS_mode() && !FIPSCHECK_verify(NULL, NULL)) {
0a4fa5d
+                fatal("FIPS integrity verification test failed.");
0a4fa5d
+        }
0a4fa5d
+
0a4fa5d
 	init_rng();
0a4fa5d
 
0a4fa5d
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Jan F 48446f1
@@ -1525,8 +1533,6 @@ main(int ac, char **av)
0a4fa5d
 	else
0a4fa5d
 		closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
0a4fa5d
 
Jan F 003cb0b
-	OpenSSL_add_all_algorithms();
0a4fa5d
-
0a4fa5d
 	/*
0a4fa5d
 	 * Force logging to stderr until we have loaded the private host
0a4fa5d
 	 * key (unless started from inetd)
Jan F 48446f1
@@ -1645,6 +1651,10 @@ main(int ac, char **av)
76f329e
 		debug("private host key: #%d type %d %s", i, key->type,
76f329e
 		    key_type(key));
76f329e
 	}
76f329e
+	if ((options.protocol & SSH_PROTO_1) && FIPS_mode()) {
76f329e
+		logit("Disabling protocol version 1. Not allowed in the FIPS mode.");
76f329e
+		options.protocol &= ~SSH_PROTO_1;
76f329e
+	}
76f329e
 	if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
76f329e
 		logit("Disabling protocol version 1. Could not load host key");
76f329e
 		options.protocol &= ~SSH_PROTO_1;
Jan F 48446f1
@@ -1809,6 +1819,10 @@ main(int ac, char **av)
0a4fa5d
 	/* Initialize the random number generator. */
0a4fa5d
 	arc4random_stir();
0a4fa5d
 
0a4fa5d
+	if (FIPS_mode()) {
0a4fa5d
+		logit("FIPS mode initialized");
0a4fa5d
+	}
0a4fa5d
+
0a4fa5d
 	/* Chdir to the root directory so that the current disk can be
0a4fa5d
 	   unmounted if desired. */
0a4fa5d
 	chdir("/");
Jan F 48446f1
@@ -2350,6 +2364,9 @@ do_ssh2_kex(void)
0a4fa5d
 	if (options.ciphers != NULL) {
0a4fa5d
 		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
0a4fa5d
 		myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
0a4fa5d
+	} else if (FIPS_mode()) {
0a4fa5d
+		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
0a4fa5d
+		myproposal[PROPOSAL_ENC_ALGS_STOC] = KEX_FIPS_ENCRYPT;
0a4fa5d
 	}
0a4fa5d
 	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
0a4fa5d
 	    compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
Jan F 48446f1
@@ -2359,6 +2376,9 @@ do_ssh2_kex(void)
0a4fa5d
 	if (options.macs != NULL) {
0a4fa5d
 		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
0a4fa5d
 		myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
0a4fa5d
+	} else if (FIPS_mode()) {
0a4fa5d
+		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
0a4fa5d
+		myproposal[PROPOSAL_MAC_ALGS_STOC] = KEX_FIPS_MAC;
0a4fa5d
 	}
0a4fa5d
 	if (options.compression == COMP_NONE) {
0a4fa5d
 		myproposal[PROPOSAL_COMP_ALGS_CTOS] =