vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/Makefile.in.fips openssh-5.9p1/Makefile.in
Jan F. Chadima 3b545b
--- openssh-5.9p1/Makefile.in.fips	2011-09-08 14:51:55.788515769 +0200
Jan F. Chadima 3b545b
+++ openssh-5.9p1/Makefile.in	2011-09-08 14:51:58.390439672 +0200
Jan F. Chadima 69dd72
@@ -142,25 +142,25 @@ libssh.a: $(LIBSSH_OBJS)
Jan F. Chadima 69dd72
 	$(RANLIB) $@
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
Jan F. Chadima 69dd72
-	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS)
Jan F. Chadima 69dd72
+	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHLIBS) $(LIBS)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 sshd$(EXEEXT): libssh.a	$(LIBCOMPAT) $(SSHDOBJS)
Jan F. Chadima 69dd72
-	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS)
Jan F. Chadima 69dd72
+	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(LIBS)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 scp$(EXEEXT): $(LIBCOMPAT) libssh.a scp.o progressmeter.o
Jan F. Chadima 69dd72
 	$(LD) -o $@ scp.o progressmeter.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o
Jan F. Chadima 69dd72
-	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jan F. Chadima 69dd72
+	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
Jan F. Chadima 69dd72
-	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jan F. Chadima 69dd72
+	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
Jan F. Chadima 69dd72
-	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jan F. Chadima 69dd72
+	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o roaming_dummy.o readconf.o
Jan F. Chadima 69dd72
-	$(LD) -o $@ ssh-keysign.o readconf.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jan F. Chadima 69dd72
+	$(LD) -o $@ ssh-keysign.o readconf.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
Jan F. Chadima 69dd72
 	$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
Jan F. Chadima 69dd72
@@ -172,7 +172,7 @@ ssh-keycat$(EXEEXT): $(LIBCOMPAT) libssh
Jan F. Chadima 69dd72
 	$(LD) -o $@ ssh-keycat.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(SSHDLIBS)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o roaming_dummy.o
Jan F. Chadima 69dd72
-	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
Jan F. Chadima 69dd72
+	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o
Jan F. Chadima 69dd72
 	$(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/authfile.c.fips openssh-5.9p1/authfile.c
Jan F. Chadima 3b545b
--- openssh-5.9p1/authfile.c.fips	2011-09-08 14:51:57.076455523 +0200
Jan F. Chadima 3b545b
+++ openssh-5.9p1/authfile.c	2011-09-08 14:51:58.485565698 +0200
Jan F. Chadima 69dd72
@@ -148,8 +148,14 @@ key_private_rsa1_to_blob(Key *key, Buffe
Jan F. Chadima 69dd72
 	/* Allocate space for the private part of the key in the buffer. */
Jan F. Chadima 69dd72
 	cp = buffer_append_space(&encrypted, buffer_len(&buffer));
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-	cipher_set_key_string(&ciphercontext, cipher, passphrase,
Jan F. Chadima 69dd72
-	    CIPHER_ENCRYPT);
Jan F. Chadima 69dd72
+	if (cipher_set_key_string(&ciphercontext, cipher, passphrase,
Jan F. Chadima 69dd72
+	    CIPHER_ENCRYPT) < 0) {
Jan F. Chadima 69dd72
+	    error("cipher_set_key_string failed.");
Jan F. Chadima 69dd72
+	    buffer_free(&encrypted);
Jan F. Chadima 69dd72
+	    buffer_free(&buffer);
Jan F. Chadima 69dd72
+	    return 0;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	cipher_crypt(&ciphercontext, cp,
Jan F. Chadima 69dd72
 	    buffer_ptr(&buffer), buffer_len(&buffer));
Jan F. Chadima 69dd72
 	cipher_cleanup(&ciphercontext);
Jan F. Chadima 69dd72
@@ -472,8 +478,13 @@ key_parse_private_rsa1(Buffer *blob, con
Jan F. Chadima 69dd72
 	cp = buffer_append_space(&decrypted, buffer_len(©);;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	/* Rest of the buffer is encrypted.  Decrypt it using the passphrase. */
Jan F. Chadima 69dd72
-	cipher_set_key_string(&ciphercontext, cipher, passphrase,
Jan F. Chadima 69dd72
-	    CIPHER_DECRYPT);
Jan F. Chadima 69dd72
+	if (cipher_set_key_string(&ciphercontext, cipher, passphrase,
Jan F. Chadima 69dd72
+	    CIPHER_DECRYPT) < 0) {
Jan F. Chadima 69dd72
+	    error("cipher_set_key_string failed.");
Jan F. Chadima 69dd72
+	    buffer_free(&decrypted);
Jan F. Chadima 69dd72
+	    goto fail;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	cipher_crypt(&ciphercontext, cp,
Jan F. Chadima 69dd72
 	    buffer_ptr(&copy), buffer_len(©);;
Jan F. Chadima 69dd72
 	cipher_cleanup(&ciphercontext);
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/cipher-ctr.c.fips openssh-5.9p1/cipher-ctr.c
Jan F. Chadima 3b545b
--- openssh-5.9p1/cipher-ctr.c.fips	2010-10-07 13:06:42.000000000 +0200
Jan F. Chadima 3b545b
+++ openssh-5.9p1/cipher-ctr.c	2011-09-08 14:51:58.593563819 +0200
Jan F. Chadima 69dd72
@@ -140,7 +140,8 @@ evp_aes_128_ctr(void)
Jan F. Chadima 69dd72
 	aes_ctr.do_cipher = ssh_aes_ctr;
Jan F. Chadima 69dd72
 #ifndef SSH_OLD_EVP
Jan F. Chadima 69dd72
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
Jan F. Chadima 69dd72
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
Jan F. Chadima 69dd72
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
Jan F. Chadima 69dd72
+	    EVP_CIPH_FLAG_FIPS;
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
 	return (&aes_ctr);
Jan F. Chadima 69dd72
 }
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/cipher.c.fips openssh-5.9p1/cipher.c
Jan F. Chadima 3b545b
--- openssh-5.9p1/cipher.c.fips	2011-09-08 14:51:44.592501867 +0200
Jan F. Chadima 3b545b
+++ openssh-5.9p1/cipher.c	2011-09-08 14:51:58.700440064 +0200
Jan F. Chadima 69dd72
@@ -40,6 +40,7 @@
Jan F. Chadima 69dd72
 #include <sys/types.h>
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #include <openssl/md5.h>
Jan F. Chadima 69dd72
+#include <openssl/fips.h>
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #include <string.h>
Jan F. Chadima 69dd72
 #include <stdarg.h>
Jan F. Chadima 69dd72
@@ -86,6 +87,22 @@ struct Cipher ciphers[] = {
Jan F. Chadima 69dd72
 	{ NULL,			SSH_CIPHER_INVALID, 0, 0, 0, 0, NULL }
Jan F. Chadima 69dd72
 };
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+struct Cipher fips_ciphers[] = {
Jan F. Chadima 69dd72
+	{ "none",		SSH_CIPHER_NONE, 8, 0, 0, 0, EVP_enc_null },
Jan F. Chadima 69dd72
+	{ "3des",		SSH_CIPHER_3DES, 8, 16, 0, 1, evp_ssh1_3des },
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
+	{ "3des-cbc",		SSH_CIPHER_SSH2, 8, 24, 0, 1, EVP_des_ede3_cbc },
Jan F. Chadima 69dd72
+	{ "aes128-cbc",		SSH_CIPHER_SSH2, 16, 16, 0, 1, EVP_aes_128_cbc },
Jan F. Chadima 69dd72
+	{ "aes192-cbc",		SSH_CIPHER_SSH2, 16, 24, 0, 1, EVP_aes_192_cbc },
Jan F. Chadima 69dd72
+	{ "aes256-cbc",		SSH_CIPHER_SSH2, 16, 32, 0, 1, EVP_aes_256_cbc },
Jan F. Chadima 69dd72
+	{ "rijndael-cbc@lysator.liu.se",
Jan F. Chadima 69dd72
+				SSH_CIPHER_SSH2, 16, 32, 0, 1, EVP_aes_256_cbc },
Jan F. Chadima 69dd72
+	{ "aes128-ctr",		SSH_CIPHER_SSH2, 16, 16, 0, 0, evp_aes_128_ctr },
Jan F. Chadima 69dd72
+	{ "aes192-ctr",		SSH_CIPHER_SSH2, 16, 24, 0, 0, evp_aes_128_ctr },
Jan F. Chadima 69dd72
+	{ "aes256-ctr",		SSH_CIPHER_SSH2, 16, 32, 0, 0, evp_aes_128_ctr },
Jan F. Chadima 69dd72
+	{ NULL,			SSH_CIPHER_INVALID, 0, 0, 0, 0, NULL }
Jan F. Chadima 69dd72
+};
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 /*--*/
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 u_int
Jan F. Chadima 69dd72
@@ -128,7 +145,7 @@ Cipher *
Jan F. Chadima 69dd72
 cipher_by_name(const char *name)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 	Cipher *c;
Jan F. Chadima 69dd72
-	for (c = ciphers; c->name != NULL; c++)
Jan F. Chadima 69dd72
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
Jan F. Chadima 69dd72
 		if (strcmp(c->name, name) == 0)
Jan F. Chadima 69dd72
 			return c;
Jan F. Chadima 69dd72
 	return NULL;
Jan F. Chadima 69dd72
@@ -138,7 +155,7 @@ Cipher *
Jan F. Chadima 69dd72
 cipher_by_number(int id)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 	Cipher *c;
Jan F. Chadima 69dd72
-	for (c = ciphers; c->name != NULL; c++)
Jan F. Chadima 69dd72
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
Jan F. Chadima 69dd72
 		if (c->number == id)
Jan F. Chadima 69dd72
 			return c;
Jan F. Chadima 69dd72
 	return NULL;
Jan F. Chadima 69dd72
@@ -182,7 +199,7 @@ cipher_number(const char *name)
Jan F. Chadima 69dd72
 	Cipher *c;
Jan F. Chadima 69dd72
 	if (name == NULL)
Jan F. Chadima 69dd72
 		return -1;
Jan F. Chadima 69dd72
-	for (c = ciphers; c->name != NULL; c++)
Jan F. Chadima 69dd72
+	for (c = FIPS_mode() ? fips_ciphers : ciphers; c->name != NULL; c++)
Jan F. Chadima 69dd72
 		if (strcasecmp(c->name, name) == 0)
Jan F. Chadima 69dd72
 			return c->number;
Jan F. Chadima 69dd72
 	return -1;
Jan F. Chadima 69dd72
@@ -289,14 +306,15 @@ cipher_cleanup(CipherContext *cc)
Jan F. Chadima 69dd72
  * passphrase and using the resulting 16 bytes as the key.
Jan F. Chadima 69dd72
  */
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-void
Jan F. Chadima 69dd72
+int
Jan F. Chadima 69dd72
 cipher_set_key_string(CipherContext *cc, Cipher *cipher,
Jan F. Chadima 69dd72
     const char *passphrase, int do_encrypt)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 	MD5_CTX md;
Jan F. Chadima 69dd72
 	u_char digest[16];
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-	MD5_Init(&md);
Jan F. Chadima 69dd72
+	if (MD5_Init(&md) <= 0)
Jan F. Chadima 69dd72
+		return -1;
Jan F. Chadima 69dd72
 	MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
Jan F. Chadima 69dd72
 	MD5_Final(digest, &md);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
@@ -304,6 +322,7 @@ cipher_set_key_string(CipherContext *cc,
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	memset(digest, 0, sizeof(digest));
Jan F. Chadima 69dd72
 	memset(&md, 0, sizeof(md));
Jan F. Chadima 69dd72
+	return 0;
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 /*
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/cipher.h.fips openssh-5.9p1/cipher.h
Jan F. Chadima 3b545b
--- openssh-5.9p1/cipher.h.fips	2011-09-08 14:51:44.697501719 +0200
Jan F. Chadima 3b545b
+++ openssh-5.9p1/cipher.h	2011-09-08 14:51:58.800502283 +0200
Jan F. Chadima 69dd72
@@ -87,7 +87,7 @@ void	 cipher_init(CipherContext *, Ciphe
Jan F. Chadima 69dd72
     const u_char *, u_int, int);
Jan F. Chadima 69dd72
 void	 cipher_crypt(CipherContext *, u_char *, const u_char *, u_int);
Jan F. Chadima 69dd72
 void	 cipher_cleanup(CipherContext *);
Jan F. Chadima 69dd72
-void	 cipher_set_key_string(CipherContext *, Cipher *, const char *, int);
Jan F. Chadima 69dd72
+int	 cipher_set_key_string(CipherContext *, Cipher *, const char *, int);
Jan F. Chadima 69dd72
 u_int	 cipher_blocksize(const Cipher *);
Jan F. Chadima 69dd72
 u_int	 cipher_keylen(const Cipher *);
Jan F. Chadima 69dd72
 u_int	 cipher_is_cbc(const Cipher *);
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/key.c.fips openssh-5.9p1/key.c
Jan F. Chadima 3b545b
--- openssh-5.9p1/key.c.fips	2011-09-08 14:51:49.002451595 +0200
Jan F. Chadima 3b545b
+++ openssh-5.9p1/key.c	2011-09-08 14:51:58.908501542 +0200
Jan F. Chadima 69dd72
@@ -40,6 +40,7 @@
Jan F. Chadima 69dd72
 #include <sys/types.h>
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #include <openssl/evp.h>
Jan F. Chadima 69dd72
+#include <openssl/fips.h>
Jan F. Chadima 69dd72
 #include <openbsd-compat/openssl-compat.h>
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #include <stdarg.h>
Jan F. Chadima 69dd72
@@ -602,9 +603,13 @@ key_fingerprint_selection(void)
Jan F. Chadima 69dd72
 	char *env;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	if (!rv_defined) {
Jan F. Chadima 69dd72
-		env = getenv("SSH_FINGERPRINT_TYPE");
Jan F. Chadima 69dd72
-		rv = (env && !strcmp (env, "sha")) ?
Jan F. Chadima 69dd72
-			SSH_FP_SHA1 : SSH_FP_MD5;
Jan F. Chadima 69dd72
+		if (FIPS_mode())
Jan F. Chadima 69dd72
+			rv = SSH_FP_SHA1;
Jan F. Chadima 69dd72
+		else {
Jan F. Chadima 69dd72
+			env = getenv("SSH_FINGERPRINT_TYPE");
Jan F. Chadima 69dd72
+			rv = (env && !strcmp (env, "sha")) ?
Jan F. Chadima 69dd72
+				SSH_FP_SHA1 : SSH_FP_MD5;
Jan F. Chadima 69dd72
+		}
Jan F. Chadima 69dd72
 		rv_defined = 1;
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 	return rv;
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/mac.c.fips openssh-5.9p1/mac.c
Jan F. Chadima 3b545b
--- openssh-5.9p1/mac.c.fips	2011-09-08 14:51:46.755466816 +0200
Jan F. Chadima 3b545b
+++ openssh-5.9p1/mac.c	2011-09-08 14:51:59.029600712 +0200
Jan F. Chadima 69dd72
@@ -28,6 +28,7 @@
Jan F. Chadima 69dd72
 #include <sys/types.h>
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #include <openssl/hmac.h>
Jan F. Chadima 69dd72
+#include <openssl/fips.h>
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #include <stdarg.h>
Jan F. Chadima 69dd72
 #include <string.h>
Jan F. Chadima 69dd72
@@ -47,14 +48,14 @@
Jan F. Chadima 69dd72
 #define SSH_EVP		1	/* OpenSSL EVP-based MAC */
Jan F. Chadima 69dd72
 #define SSH_UMAC	2	/* UMAC (not integrated with OpenSSL) */
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-struct {
Jan F. Chadima 69dd72
+struct Macs {
Jan F. Chadima 69dd72
 	char		*name;
Jan F. Chadima 69dd72
 	int		type;
Jan F. Chadima 69dd72
 	const EVP_MD *	(*mdfunc)(void);
Jan F. Chadima 69dd72
 	int		truncatebits;	/* truncate digest if != 0 */
Jan F. Chadima 69dd72
 	int		key_len;	/* just for UMAC */
Jan F. Chadima 69dd72
 	int		len;		/* just for UMAC */
Jan F. Chadima 69dd72
-} macs[] = {
Jan F. Chadima 69dd72
+} all_macs[] = {
Jan F. Chadima 69dd72
 	{ "hmac-sha1",			SSH_EVP, EVP_sha1, 0, -1, -1 },
Jan F. Chadima 69dd72
 	{ "hmac-sha1-96",		SSH_EVP, EVP_sha1, 96, -1, -1 },
Jan F. Chadima 69dd72
 #ifdef HAVE_EVP_SHA256
Jan F. Chadima 69dd72
@@ -71,9 +72,15 @@ struct {
Jan F. Chadima 69dd72
 	{ NULL,				0, NULL, 0, -1, -1 }
Jan F. Chadima 69dd72
 };
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+struct Macs fips_macs[] = {
Jan F. Chadima 69dd72
+	{ "hmac-sha1",			SSH_EVP, EVP_sha1, 0, -1, -1 },
Jan F. Chadima 69dd72
+	{ NULL,				0, NULL, 0, -1, -1 }
Jan F. Chadima 69dd72
+};
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 static void
Jan F. Chadima 69dd72
 mac_setup_by_id(Mac *mac, int which)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
+	struct Macs *macs = FIPS_mode() ? fips_macs : all_macs;
Jan F. Chadima 69dd72
 	int evp_len;
Jan F. Chadima 69dd72
 	mac->type = macs[which].type;
Jan F. Chadima 69dd72
 	if (mac->type == SSH_EVP) {
Jan F. Chadima 69dd72
@@ -94,6 +101,7 @@ int
Jan F. Chadima 69dd72
 mac_setup(Mac *mac, char *name)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 	int i;
Jan F. Chadima 69dd72
+	struct Macs *macs = FIPS_mode() ? fips_macs : all_macs;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	for (i = 0; macs[i].name; i++) {
Jan F. Chadima 69dd72
 		if (strcmp(name, macs[i].name) == 0) {
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/myproposal.h.fips openssh-5.9p1/myproposal.h
Jan F. Chadima 3b545b
--- openssh-5.9p1/myproposal.h.fips	2011-08-17 02:29:03.000000000 +0200
Jan F. Chadima 3b545b
+++ openssh-5.9p1/myproposal.h	2011-09-08 14:51:59.150503573 +0200
Jan F. Chadima 69dd72
@@ -97,6 +97,12 @@
Jan F. Chadima 69dd72
 #define	KEX_DEFAULT_COMP	"none,zlib@openssh.com,zlib"
Jan F. Chadima 69dd72
 #define	KEX_DEFAULT_LANG	""
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+#define	KEX_FIPS_ENCRYPT \
Jan F. Chadima 69dd72
+	"aes128-ctr,aes192-ctr,aes256-ctr," \
Jan F. Chadima 69dd72
+	"aes128-cbc,3des-cbc," \
Jan F. Chadima 69dd72
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se"
Jan F. Chadima 69dd72
+#define	KEX_FIPS_MAC \
Jan F. Chadima 69dd72
+	"hmac-sha1"
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 static char *myproposal[PROPOSAL_MAX] = {
Jan F. Chadima 69dd72
 	KEX_DEFAULT_KEX,
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/openbsd-compat/bsd-arc4random.c.fips openssh-5.9p1/openbsd-compat/bsd-arc4random.c
Jan F. Chadima 3b545b
--- openssh-5.9p1/openbsd-compat/bsd-arc4random.c.fips	2010-03-25 22:52:02.000000000 +0100
Jan F. Chadima 3b545b
+++ openssh-5.9p1/openbsd-compat/bsd-arc4random.c	2011-09-08 14:51:59.262439903 +0200
Jan F. Chadima 69dd72
@@ -37,25 +37,18 @@
Jan F. Chadima 69dd72
 #define REKEY_BYTES	(1 << 24)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 static int rc4_ready = 0;
Jan F. Chadima 69dd72
-static RC4_KEY rc4;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 unsigned int
Jan F. Chadima 69dd72
 arc4random(void)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 	unsigned int r = 0;
Jan F. Chadima 69dd72
-	static int first_time = 1;
Jan F. Chadima 69dd72
+	void *rp = &r;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-	if (rc4_ready <= 0) {
Jan F. Chadima 69dd72
-		if (first_time)
Jan F. Chadima 69dd72
-			seed_rng();
Jan F. Chadima 69dd72
-		first_time = 0;
Jan F. Chadima 69dd72
+	if (!rc4_ready) {
Jan F. Chadima 69dd72
 		arc4random_stir();
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
+	RAND_bytes(rp, sizeof(r));
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-	RC4(&rc4, sizeof(r), (unsigned char *)&r, (unsigned char *)&r);
Jan F. Chadima 69dd72
-
Jan F. Chadima 69dd72
-	rc4_ready -= sizeof(r);
Jan F. Chadima 69dd72
-	
Jan F. Chadima 69dd72
 	return(r);
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
@@ -63,24 +56,11 @@ void
Jan F. Chadima 69dd72
 arc4random_stir(void)
Jan F. Chadima 69dd72
 {
Jan F. Chadima 69dd72
 	unsigned char rand_buf[SEED_SIZE];
Jan F. Chadima 69dd72
-	int i;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-	memset(&rc4, 0, sizeof(rc4));
Jan F. Chadima 69dd72
 	if (RAND_bytes(rand_buf, sizeof(rand_buf)) <= 0)
Jan F. Chadima 69dd72
 		fatal("Couldn't obtain random bytes (error %ld)",
Jan F. Chadima 69dd72
 		    ERR_get_error());
Jan F. Chadima 69dd72
-	RC4_set_key(&rc4, sizeof(rand_buf), rand_buf);
Jan F. Chadima 69dd72
-
Jan F. Chadima 69dd72
-	/*
Jan F. Chadima 69dd72
-	 * Discard early keystream, as per recommendations in:
Jan F. Chadima 69dd72
-	 * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
Jan F. Chadima 69dd72
-	 */
Jan F. Chadima 69dd72
-	for(i = 0; i <= 256; i += sizeof(rand_buf))
Jan F. Chadima 69dd72
-		RC4(&rc4, sizeof(rand_buf), rand_buf, rand_buf);
Jan F. Chadima 69dd72
-
Jan F. Chadima 69dd72
-	memset(rand_buf, 0, sizeof(rand_buf));
Jan F. Chadima 69dd72
-
Jan F. Chadima 69dd72
-	rc4_ready = REKEY_BYTES;
Jan F. Chadima 69dd72
+	rc4_ready = 1;
Jan F. Chadima 69dd72
 }
Jan F. Chadima 69dd72
 #endif /* !HAVE_ARC4RANDOM */
Jan F. Chadima 69dd72
 
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/ssh.c.fips openssh-5.9p1/ssh.c
Jan F. Chadima 3b545b
--- openssh-5.9p1/ssh.c.fips	2011-08-05 22:18:16.000000000 +0200
Jan F. Chadima 3b545b
+++ openssh-5.9p1/ssh.c	2011-09-08 14:51:59.369485419 +0200
Jan F. Chadima 69dd72
@@ -73,6 +73,8 @@
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #include <openssl/evp.h>
Jan F. Chadima 69dd72
 #include <openssl/err.h>
Jan F. Chadima 69dd72
+#include <openssl/fips.h>
Jan F. Chadima 69dd72
+#include <fipscheck.h>
Jan F. Chadima 69dd72
 #include "openbsd-compat/openssl-compat.h"
Jan F. Chadima 69dd72
 #include "openbsd-compat/sys-queue.h"
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
@@ -253,6 +255,10 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 	sanitise_stdfd();
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	__progname = ssh_get_progname(av[0]);
Jan F. Chadima 69dd72
+        SSLeay_add_all_algorithms();
Jan F. Chadima 69dd72
+        if (FIPS_mode() && !FIPSCHECK_verify(NULL, NULL)) {
Jan F. Chadima 69dd72
+                fatal("FIPS integrity verification test failed.");
Jan F. Chadima 69dd72
+        }
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #ifndef HAVE_SETPROCTITLE
Jan F. Chadima 69dd72
 	/* Prepare for later setproctitle emulation */
Jan F. Chadima 69dd72
@@ -329,6 +335,9 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 	    "ACD:F:I:KL:MNO:PR:S:TVw:W:XYy")) != -1) {
Jan F. Chadima 69dd72
 		switch (opt) {
Jan F. Chadima 69dd72
 		case '1':
Jan F. Chadima 69dd72
+			if (FIPS_mode()) {
Jan F. Chadima 69dd72
+				fatal("Protocol 1 not allowed in the FIPS mode.");
Jan F. Chadima 69dd72
+			}
Jan F. Chadima 69dd72
 			options.protocol = SSH_PROTO_1;
Jan F. Chadima 69dd72
 			break;
Jan F. Chadima 69dd72
 		case '2':
Jan F. Chadima 69dd72
@@ -630,7 +639,6 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 	if (!host)
Jan F. Chadima 69dd72
 		usage();
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-	OpenSSL_add_all_algorithms();
Jan F. Chadima 69dd72
 	ERR_load_crypto_strings();
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	/* Initialize the command to execute on remote host. */
Jan F. Chadima 69dd72
@@ -721,6 +729,10 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	seed_rng();
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+	if (FIPS_mode()) {
Jan F. Chadima 69dd72
+		logit("FIPS mode initialized");
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	if (options.user == NULL)
Jan F. Chadima 69dd72
 		options.user = xstrdup(pw->pw_name);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
@@ -789,6 +801,12 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 	timeout_ms = options.connection_timeout * 1000;
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+	if (FIPS_mode()) {
Jan F. Chadima 69dd72
+		options.protocol &= SSH_PROTO_2;
Jan F. Chadima 69dd72
+		if (options.protocol == 0)
Jan F. Chadima 69dd72
+			fatal("Protocol 2 disabled by configuration but required in the FIPS mode.");
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	/* Open a connection to the remote host. */
Jan F. Chadima 69dd72
 	if (ssh_connect(host, &hostaddr, options.port,
Jan F. Chadima 69dd72
 	    options.address_family, options.connection_attempts, &timeout_ms,
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/sshconnect2.c.fips openssh-5.9p1/sshconnect2.c
Jan F. Chadima 3b545b
--- openssh-5.9p1/sshconnect2.c.fips	2011-09-08 14:51:38.333470704 +0200
Jan F. Chadima 3b545b
+++ openssh-5.9p1/sshconnect2.c	2011-09-08 14:51:59.474500288 +0200
Jan F. Chadima 69dd72
@@ -44,6 +44,8 @@
Jan F. Chadima 69dd72
 #include <vis.h>
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+#include <openssl/fips.h>
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 #include "openbsd-compat/sys-queue.h"
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #include "xmalloc.h"
Jan F. Chadima 69dd72
@@ -170,6 +172,10 @@ ssh_kex2(char *host, struct sockaddr *ho
Jan F. Chadima 69dd72
 	if (options.ciphers != NULL) {
Jan F. Chadima 69dd72
 		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Jan F. Chadima 69dd72
 		myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
Jan F. Chadima 69dd72
+	} else if (FIPS_mode()) {
Jan F. Chadima 69dd72
+		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Jan F. Chadima 69dd72
+		myproposal[PROPOSAL_ENC_ALGS_STOC] = KEX_FIPS_ENCRYPT;
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Jan F. Chadima 69dd72
 	    compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
Jan F. Chadima 69dd72
@@ -185,7 +191,11 @@ ssh_kex2(char *host, struct sockaddr *ho
Jan F. Chadima 69dd72
 	if (options.macs != NULL) {
Jan F. Chadima 69dd72
 		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
Jan F. Chadima 69dd72
 		myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
Jan F. Chadima 69dd72
+	} else if (FIPS_mode()) {
Jan F. Chadima 69dd72
+		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
Jan F. Chadima 69dd72
+		myproposal[PROPOSAL_MAC_ALGS_STOC] = KEX_FIPS_MAC;
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	if (options.hostkeyalgorithms != NULL)
Jan F. Chadima 69dd72
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Jan F. Chadima 69dd72
 		    options.hostkeyalgorithms;
Jan F. Chadima 3b545b
diff -up openssh-5.9p1/sshd.c.fips openssh-5.9p1/sshd.c
Jan F. Chadima 3b545b
--- openssh-5.9p1/sshd.c.fips	2011-09-08 14:51:52.866451334 +0200
Jan F. Chadima 3b545b
+++ openssh-5.9p1/sshd.c	2011-09-08 14:57:01.982447369 +0200
Jan F. Chadima 69dd72
@@ -76,6 +76,8 @@
Jan F. Chadima 69dd72
 #include <openssl/bn.h>
Jan F. Chadima 69dd72
 #include <openssl/md5.h>
Jan F. Chadima 69dd72
 #include <openssl/rand.h>
Jan F. Chadima 69dd72
+#include <openssl/fips.h>
Jan F. Chadima 69dd72
+#include <fipscheck.h>
Jan F. Chadima 69dd72
 #include "openbsd-compat/openssl-compat.h"
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #ifdef HAVE_SECUREWARE
Jan F. Chadima 3b545b
@@ -1391,6 +1393,11 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 #endif
Jan F. Chadima 69dd72
 	__progname = ssh_get_progname(av[0]);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+        SSLeay_add_all_algorithms();
Jan F. Chadima 69dd72
+        if (FIPS_mode() && !FIPSCHECK_verify(NULL, NULL)) {
Jan F. Chadima 69dd72
+                fatal("FIPS integrity verification test failed.");
Jan F. Chadima 69dd72
+        }
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Jan F. Chadima 69dd72
 	saved_argc = ac;
Jan F. Chadima 69dd72
 	rexec_argc = ac;
Jan F. Chadima 3b545b
@@ -1550,8 +1557,6 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 	else
Jan F. Chadima 69dd72
 		closefrom(REEXEC_DEVCRYPTO_RESERVED_FD);
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
-	OpenSSL_add_all_algorithms();
Jan F. Chadima 69dd72
-
Jan F. Chadima 69dd72
 	/*
Jan F. Chadima 69dd72
 	 * Force logging to stderr until we have loaded the private host
Jan F. Chadima 69dd72
 	 * key (unless started from inetd)
Jan F. Chadima 3b545b
@@ -1669,6 +1674,10 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 		debug("private host key: #%d type %d %s", i, key->type,
Jan F. Chadima 69dd72
 		    key_type(key));
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
+	if ((options.protocol & SSH_PROTO_1) && FIPS_mode()) {
Jan F. Chadima 69dd72
+		logit("Disabling protocol version 1. Not allowed in the FIPS mode.");
Jan F. Chadima 69dd72
+		options.protocol &= ~SSH_PROTO_1;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
 	if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
Jan F. Chadima 69dd72
 		logit("Disabling protocol version 1. Could not load host key");
Jan F. Chadima 69dd72
 		options.protocol &= ~SSH_PROTO_1;
Jan F. Chadima 3b545b
@@ -1833,6 +1842,10 @@ main(int ac, char **av)
Jan F. Chadima 69dd72
 	/* Initialize the random number generator. */
Jan F. Chadima 69dd72
 	arc4random_stir();
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+	if (FIPS_mode()) {
Jan F. Chadima 69dd72
+		logit("FIPS mode initialized");
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	/* Chdir to the root directory so that the current disk can be
Jan F. Chadima 69dd72
 	   unmounted if desired. */
Jan F. Chadima 3b545b
 	(void) chdir("/");
Jan F. Chadima 3b545b
@@ -2375,6 +2388,9 @@ do_ssh2_kex(void)
Jan F. Chadima 69dd72
 	if (options.ciphers != NULL) {
Jan F. Chadima 69dd72
 		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Jan F. Chadima 69dd72
 		myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
Jan F. Chadima 69dd72
+	} else if (FIPS_mode()) {
Jan F. Chadima 69dd72
+		myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Jan F. Chadima 69dd72
+		myproposal[PROPOSAL_ENC_ALGS_STOC] = KEX_FIPS_ENCRYPT;
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Jan F. Chadima 69dd72
 	    compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
Jan F. Chadima 3b545b
@@ -2384,6 +2400,9 @@ do_ssh2_kex(void)
Jan F. Chadima 69dd72
 	if (options.macs != NULL) {
Jan F. Chadima 69dd72
 		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
Jan F. Chadima 69dd72
 		myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
Jan F. Chadima 69dd72
+	} else if (FIPS_mode()) {
Jan F. Chadima 69dd72
+		myproposal[PROPOSAL_MAC_ALGS_CTOS] =
Jan F. Chadima 69dd72
+		myproposal[PROPOSAL_MAC_ALGS_STOC] = KEX_FIPS_MAC;
Jan F. Chadima 69dd72
 	}
Jan F. Chadima 69dd72
 	if (options.compression == COMP_NONE) {
Jan F. Chadima 69dd72
 		myproposal[PROPOSAL_COMP_ALGS_CTOS] =