vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/authfile.c.fips openssh-6.2p1/authfile.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/authfile.c.fips	2013-03-27 13:14:49.164683482 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/authfile.c	2013-03-27 13:14:49.177683431 +0100
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) {
Petr Lautrbach 8a29de
+		error("cipher_set_key_string failed.");
Petr Lautrbach 8a29de
+		buffer_free(&encrypted);
Petr Lautrbach 8a29de
+		buffer_free(&buffer);
Petr Lautrbach 8a29de
+		return 0;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	cipher_crypt(&ciphercontext, cp,
Petr Lautrbach 8a29de
 	    buffer_ptr(&buffer), buffer_len(&buffer), 0, 0);
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) {
Petr Lautrbach 8a29de
+		error("cipher_set_key_string failed.");
Petr Lautrbach 8a29de
+		buffer_free(&decrypted);
Petr Lautrbach 8a29de
+		goto fail;
Jan F. Chadima 69dd72
+	}
Jan F. Chadima 69dd72
+
Jan F. Chadima 69dd72
 	cipher_crypt(&ciphercontext, cp,
Petr Lautrbach 8a29de
 	    buffer_ptr(&copy), buffer_len(&copy), 0, 0);
Jan F. Chadima 69dd72
 	cipher_cleanup(&ciphercontext);
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/cipher.c.fips openssh-6.2p1/cipher.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/cipher.c.fips	2013-03-27 13:14:49.087683788 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/cipher.c	2013-03-27 13:14:49.177683431 +0100
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>
Petr Lautrbach 8a29de
@@ -89,6 +90,27 @@ struct Cipher ciphers[] = {
Petr Lautrbach 8a29de
 	{ NULL,		SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
Jan F. Chadima 69dd72
 };
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+struct Cipher fips_ciphers[] = {
Petr Lautrbach 8a29de
+	{ "none",		SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
Petr Lautrbach 8a29de
+	{ "3des",		SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
Jan F. Chadima 69dd72
+
Petr Lautrbach 8a29de
+	{ "3des-cbc",		SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
Petr Lautrbach 8a29de
+	{ "aes128-cbc",		SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
Petr Lautrbach 8a29de
+	{ "aes192-cbc",		SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
Petr Lautrbach 8a29de
+	{ "aes256-cbc",		SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
Jan F. Chadima 69dd72
+	{ "rijndael-cbc@lysator.liu.se",
Petr Lautrbach 8a29de
+				SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
Petr Lautrbach 8a29de
+	{ "aes128-ctr",		SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
Petr Lautrbach 8a29de
+	{ "aes192-ctr",		SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_128_ctr },
Petr Lautrbach 8a29de
+	{ "aes256-ctr",		SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_128_ctr },
Petr Lautrbach 8a29de
+#ifdef OPENSSL_HAVE_EVPGCM
Petr Lautrbach 8a29de
+	{ "aes128-gcm@openssh.com",
Petr Lautrbach 8a29de
+			SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
Petr Lautrbach 8a29de
+	{ "aes256-gcm@openssh.com",
Petr Lautrbach 8a29de
+			SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
Petr Lautrbach 8a29de
+#endif
Petr Lautrbach 8a29de
+	{ NULL,			SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, NULL }
Jan F. Chadima 69dd72
+};
Jan F. Chadima 69dd72
 /*--*/
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 u_int
Petr Lautrbach 8a29de
@@ -143,7 +165,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;
Petr Lautrbach 8a29de
@@ -153,7 +175,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;
Petr Lautrbach 8a29de
@@ -197,7 +219,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;
Petr Lautrbach 8a29de
@@ -356,14 +378,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
 
Petr Lautrbach 8a29de
@@ -371,6 +394,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
 /*
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/cipher-ctr.c.fips openssh-6.2p1/cipher-ctr.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/cipher-ctr.c.fips	2013-01-20 12:31:30.000000000 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/cipher-ctr.c	2013-03-27 13:14:49.177683431 +0100
Petr Lautrbach 8a29de
@@ -138,7 +138,8 @@ evp_aes_128_ctr(void)
Tomas Mraz e96203
 	aes_ctr.do_cipher = ssh_aes_ctr;
Tomas Mraz e96203
 #ifndef SSH_OLD_EVP
Tomas Mraz e96203
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
Tomas Mraz e96203
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
Tomas Mraz e96203
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
Tomas Mraz e96203
+	    EVP_CIPH_FLAG_FIPS;
Tomas Mraz e96203
 #endif
Tomas Mraz e96203
 	return (&aes_ctr);
Tomas Mraz e96203
 }
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/cipher.h.fips openssh-6.2p1/cipher.h
Petr Lautrbach 8a29de
--- openssh-6.2p1/cipher.h.fips	2013-03-27 13:14:49.088683784 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/cipher.h	2013-03-27 13:14:49.177683431 +0100
Petr Lautrbach 8a29de
@@ -91,7 +91,7 @@ void	 cipher_init(CipherContext *, Ciphe
Petr Lautrbach 8a29de
 void	 cipher_crypt(CipherContext *, u_char *, const u_char *,
Petr Lautrbach 8a29de
     u_int, u_int, 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 *);
Petr Lautrbach 8a29de
 u_int	 cipher_authlen(const Cipher *);
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/key.c.fips openssh-6.2p1/key.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/key.c.fips	2013-03-27 13:14:49.100683736 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/key.c	2013-03-27 13:14:49.178683427 +0100
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>
Petr Lautrbach 8a29de
@@ -607,9 +608,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;
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/mac.c.fips openssh-6.2p1/mac.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/mac.c.fips	2013-03-27 13:14:49.093683764 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/mac.c	2013-03-27 13:16:33.524266158 +0100
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>
Petr Lautrbach 8a29de
@@ -50,7 +51,7 @@
Jan F. Chadima 69dd72
 #define SSH_UMAC	2	/* UMAC (not integrated with OpenSSL) */
Petr Lautrbach 8a29de
 #define SSH_UMAC128	3
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);
Petr Lautrbach 8a29de
@@ -58,7 +59,9 @@ struct {
Jan F. Chadima 69dd72
 	int		key_len;	/* just for UMAC */
Jan F. Chadima 69dd72
 	int		len;		/* just for UMAC */
Petr Lautrbach 8a29de
 	int		etm;		/* Encrypt-then-MAC */
Jan F. Chadima 69dd72
-} macs[] = {
Petr Lautrbach 8a29de
+};
Petr Lautrbach 8a29de
+
Petr Lautrbach 8a29de
+struct Macs all_macs[] = {
Petr Lautrbach 8a29de
 	/* Encrypt-and-MAC (encrypt-and-authenticate) variants */
Petr Lautrbach 8a29de
 	{ "hmac-sha1",				SSH_EVP, EVP_sha1, 0, 0, 0, 0 },
Petr Lautrbach 8a29de
 	{ "hmac-sha1-96",			SSH_EVP, EVP_sha1, 96, 0, 0, 0 },
Petr Lautrbach 8a29de
@@ -89,9 +92,19 @@ struct {
Petr Lautrbach 8a29de
 	{ NULL,					0, NULL, 0, 0, 0, 0 }
Jan F. Chadima 69dd72
 };
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
+struct Macs fips_macs[] = {
Petr Lautrbach 8a29de
+	{ "hmac-sha1",			SSH_EVP, EVP_sha1, 0, 0, 0, 0 },
Tomas Mraz e96203
+#ifdef HAVE_EVP_SHA256
Petr Lautrbach 8a29de
+	{ "hmac-sha2-256",		SSH_EVP, EVP_sha256, 0, 0, 0, 0 },
Petr Lautrbach 8a29de
+	{ "hmac-sha2-512",		SSH_EVP, EVP_sha512, 0, 0, 0, 0 },
Tomas Mraz e96203
+#endif
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) {
Petr Lautrbach 8a29de
@@ -113,6 +126,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) {
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/Makefile.in.fips openssh-6.2p1/Makefile.in
Petr Lautrbach 8a29de
--- openssh-6.2p1/Makefile.in.fips	2013-03-27 13:14:49.155683518 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/Makefile.in	2013-03-27 13:14:49.178683427 +0100
Petr Lautrbach 8a29de
@@ -145,25 +145,25 @@ libssh.a: $(LIBSSH_OBJS)
Tomas Mraz e96203
 	$(RANLIB) $@
Tomas Mraz e96203
 
Tomas Mraz e96203
 ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
Petr Lautrbach 8a29de
-	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS) $(GSSLIBS)
Petr Lautrbach 8a29de
+	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHLIBS) $(LIBS) $(GSSLIBS)
Tomas Mraz e96203
 
Tomas Mraz e96203
 sshd$(EXEEXT): libssh.a	$(LIBCOMPAT) $(SSHDOBJS)
Petr Lautrbach 8a29de
-	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
Petr Lautrbach 8a29de
+	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
Tomas Mraz e96203
 
Tomas Mraz e96203
 scp$(EXEEXT): $(LIBCOMPAT) libssh.a scp.o progressmeter.o
Tomas Mraz e96203
 	$(LD) -o $@ scp.o progressmeter.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Tomas Mraz e96203
 
Tomas Mraz e96203
 ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o
Tomas Mraz e96203
-	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Tomas Mraz e96203
+	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Tomas Mraz e96203
 
Tomas Mraz e96203
 ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
Tomas Mraz e96203
-	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Tomas Mraz e96203
+	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Tomas Mraz e96203
 
Tomas Mraz e96203
 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
Tomas Mraz e96203
-	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Tomas Mraz e96203
+	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Tomas Mraz e96203
 
Tomas Mraz e96203
 ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o roaming_dummy.o readconf.o
Tomas Mraz e96203
-	$(LD) -o $@ ssh-keysign.o readconf.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Tomas Mraz e96203
+	$(LD) -o $@ ssh-keysign.o readconf.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Tomas Mraz e96203
 
Tomas Mraz e96203
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
Tomas Mraz e96203
 	$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
Petr Lautrbach 8a29de
@@ -175,7 +175,7 @@ ssh-keycat$(EXEEXT): $(LIBCOMPAT) libssh
Tomas Mraz e96203
 	$(LD) -o $@ ssh-keycat.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(SSHDLIBS)
Tomas Mraz e96203
 
Tomas Mraz e96203
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o roaming_dummy.o
Tomas Mraz e96203
-	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
Tomas Mraz e96203
+	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
Tomas Mraz e96203
 
Tomas Mraz e96203
 sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o
Tomas Mraz e96203
 	$(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/myproposal.h.fips openssh-6.2p1/myproposal.h
Petr Lautrbach 8a29de
--- openssh-6.2p1/myproposal.h.fips	2013-01-09 06:12:19.000000000 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/myproposal.h	2013-03-27 13:14:49.178683427 +0100
Petr Lautrbach 8a29de
@@ -106,6 +106,19 @@
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"
Tomas Mraz e96203
+#ifdef HAVE_EVP_SHA256
Tomas Mraz e96203
+#define	KEX_FIPS_MAC \
Tomas Mraz e96203
+	"hmac-sha1," \
Tomas Mraz e96203
+	"hmac-sha2-256," \
Tomas Mraz e96203
+	"hmac-sha2-512"
Tomas Mraz e96203
+#else
Jan F. Chadima 69dd72
+#define	KEX_FIPS_MAC \
Jan F. Chadima 69dd72
+	"hmac-sha1"
Tomas Mraz e96203
+#endif
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 static char *myproposal[PROPOSAL_MAX] = {
Jan F. Chadima 69dd72
 	KEX_DEFAULT_KEX,
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/openbsd-compat/bsd-arc4random.c.fips openssh-6.2p1/openbsd-compat/bsd-arc4random.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/openbsd-compat/bsd-arc4random.c.fips	2010-03-25 22:52:02.000000000 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/openbsd-compat/bsd-arc4random.c	2013-03-27 13:14:49.179683423 +0100
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
 
Petr Lautrbach f12afd
diff -up openssh-6.2p2/ssh.c.fips openssh-6.2p2/ssh.c
Petr Lautrbach f12afd
--- openssh-6.2p2/ssh.c.fips	2013-04-05 02:22:36.000000000 +0200
Petr Lautrbach f12afd
+++ openssh-6.2p2/ssh.c	2013-10-08 17:21:26.894761211 +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
 
Petr Lautrbach f12afd
@@ -253,6 +255,13 @@ 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();
Petr Lautrbach f12afd
+	if (access("/etc/system-fips", F_OK) == 0)
Petr Lautrbach f12afd
+		if (! FIPSCHECK_verify(NULL, NULL))
Petr Lautrbach f12afd
+			if (FIPS_mode())
Petr Lautrbach f12afd
+				fatal("FIPS integrity verification test failed.");
Petr Lautrbach f12afd
+			else
Petr Lautrbach f12afd
+				logit("FIPS integrity verification test failed.");
Jan F. Chadima 69dd72
 
Jan F. Chadima 69dd72
 #ifndef HAVE_SETPROCTITLE
Jan F. Chadima 69dd72
 	/* Prepare for later setproctitle emulation */
Petr Lautrbach f12afd
@@ -329,6 +338,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':
Petr Lautrbach f12afd
@@ -628,7 +640,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. */
Petr Lautrbach f12afd
@@ -719,6 +730,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
 
Petr Lautrbach f12afd
@@ -787,6 +802,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,
Petr Lautrbach 8a29de
diff -up openssh-6.2p1/sshconnect2.c.fips openssh-6.2p1/sshconnect2.c
Petr Lautrbach 8a29de
--- openssh-6.2p1/sshconnect2.c.fips	2013-03-27 13:14:49.066683871 +0100
Petr Lautrbach 8a29de
+++ openssh-6.2p1/sshconnect2.c	2013-03-27 13:14:49.179683423 +0100
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;
Petr Lautrbach f12afd
diff -up openssh-6.2p2/sshd.c.fips openssh-6.2p2/sshd.c
Petr Lautrbach f12afd
--- openssh-6.2p2/sshd.c.fips	2013-10-08 17:14:05.455864248 +0200
Petr Lautrbach f12afd
+++ openssh-6.2p2/sshd.c	2013-10-08 17:22:15.897527827 +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
Petr Lautrbach f12afd
@@ -1423,6 +1425,14 @@ 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();
Petr Lautrbach f12afd
+	if (access("/etc/system-fips", F_OK) == 0)
Petr Lautrbach f12afd
+		if (! FIPSCHECK_verify(NULL, NULL))
Petr Lautrbach f12afd
+			if (FIPS_mode())
Petr Lautrbach f12afd
+				fatal("FIPS integrity verification test failed.");
Petr Lautrbach f12afd
+			else
Petr Lautrbach f12afd
+				logit("FIPS integrity verification test failed.");
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;
Petr Lautrbach f12afd
@@ -1571,8 +1581,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)
Petr Lautrbach f12afd
@@ -1715,6 +1723,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;
Petr Lautrbach f12afd
@@ -1878,6 +1890,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("/");
Petr Lautrbach f12afd
@@ -2420,6 +2436,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]);
Petr Lautrbach f12afd
@@ -2429,6 +2448,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] =