vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jakub Jelen 81a703
diff -up openssh-7.9p1/cipher-ctr.c.fips openssh-7.9p1/cipher-ctr.c
Jakub Jelen 81a703
--- openssh-7.9p1/cipher-ctr.c.fips	2019-03-11 17:06:37.519877082 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/cipher-ctr.c	2019-03-11 17:06:37.620878031 +0100
Jakub Jelen 535d34
@@ -179,7 +179,8 @@ evp_aes_128_ctr(void)
Jakub Jelen 535d34
 	aes_ctr.do_cipher = ssh_aes_ctr;
Jakub Jelen 535d34
 #ifndef SSH_OLD_EVP
Jakub Jelen 535d34
 	aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
Jakub Jelen 535d34
-	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
Jakub Jelen 535d34
+	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV |
Jakub Jelen 535d34
+	    EVP_CIPH_FLAG_FIPS;
Jakub Jelen 535d34
 #endif
Jakub Jelen 535d34
 	return (&aes_ctr);
Jakub Jelen 535d34
 }
Jakub Jelen 81a703
diff -up openssh-7.9p1/clientloop.c.fips openssh-7.9p1/clientloop.c
Jakub Jelen 81a703
--- openssh-7.9p1/clientloop.c.fips	2019-03-11 17:06:37.523877120 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/clientloop.c	2019-03-11 17:06:37.620878031 +0100
Jakub Jelen 81a703
@@ -2014,7 +2014,8 @@ key_accepted_by_hostkeyalgs(const struct
Jakub Jelen 9dbec7
 {
Jakub Jelen 9dbec7
 	const char *ktype = sshkey_ssh_name(key);
Jakub Jelen 9dbec7
 	const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
Jakub Jelen 9dbec7
-	    options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
Jakub Jelen 9dbec7
+	    options.hostkeyalgorithms : (FIPS_mode() ?
Jakub Jelen 9dbec7
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG);
Jakub Jelen 9dbec7
 
Jakub Jelen 9dbec7
 	if (key == NULL || key->type == KEY_UNSPEC)
Jakub Jelen 9dbec7
 		return 0;
Jakub Jelen 81a703
diff -up openssh-7.9p1/dh.c.fips openssh-7.9p1/dh.c
Jakub Jelen 81a703
--- openssh-7.9p1/dh.c.fips	2018-10-17 02:01:20.000000000 +0200
Jakub Jelen 81a703
+++ openssh-7.9p1/dh.c	2019-03-11 17:08:11.769763057 +0100
Jakub Jelen 81a703
@@ -152,6 +152,12 @@ choose_dh(int min, int wantbits, int max
Jakub Jelen 81a703
 	int best, bestcount, which, linenum;
Jakub Jelen 81a703
 	struct dhgroup dhg;
Jakub Jelen 81a703
 
Jakub Jelen 81a703
+	if (FIPS_mode()) {
Jakub Jelen 81a703
+		logit("Using arbitrary primes is not allowed in FIPS mode."
Jakub Jelen 81a703
+		    " Falling back to known groups.");
Jakub Jelen 81a703
+		return (dh_new_group_fallback(max));
Jakub Jelen 81a703
+	}
Jakub Jelen 81a703
+
Jakub Jelen 81a703
 	if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL) {
Jakub Jelen 81a703
 		logit("WARNING: could not open %s (%s), using fixed modulus",
Jakub Jelen 81a703
 		    _PATH_DH_MODULI, strerror(errno));
Jakub Jelen 81a703
@@ -489,4 +495,38 @@ dh_estimate(int bits)
Jakub Jelen 81a703
 	return 8192;
Jakub Jelen 81a703
 }
Jakub Jelen 81a703
 
Jakub Jelen 81a703
+/*
Jakub Jelen 81a703
+ * Compares the received DH parameters with known-good groups,
Jakub Jelen 81a703
+ * which might be either from group14, group16 or group18.
Jakub Jelen 81a703
+ */
Jakub Jelen 81a703
+int
Jakub Jelen 81a703
+dh_is_known_group(const DH *dh)
Jakub Jelen 81a703
+{
Jakub Jelen 81a703
+	const BIGNUM *p, *g;
Jakub Jelen 81a703
+	const BIGNUM *known_p, *known_g;
Jakub Jelen 81a703
+	DH *known = NULL;
Jakub Jelen 81a703
+	int bits = 0, rv = 0;
Jakub Jelen 81a703
+
Jakub Jelen 81a703
+	DH_get0_pqg(dh, &p, NULL, &g);
Jakub Jelen 81a703
+	bits = BN_num_bits(p);
Jakub Jelen 81a703
+
Jakub Jelen 81a703
+	if (bits <= 3072) {
Jakub Jelen 81a703
+		known = dh_new_group14();
Jakub Jelen 81a703
+	} else if (bits <= 6144) {
Jakub Jelen 81a703
+		known = dh_new_group16();
Jakub Jelen 81a703
+	} else {
Jakub Jelen 81a703
+		known = dh_new_group18();
Jakub Jelen 81a703
+	}
Jakub Jelen 81a703
+
Jakub Jelen 81a703
+	DH_get0_pqg(known, &known_p, NULL, &known_g);
Jakub Jelen 81a703
+
Jakub Jelen 81a703
+	if (BN_cmp(g, known_g) == 0 &&
Jakub Jelen 81a703
+	    BN_cmp(p, known_p) == 0) {
Jakub Jelen 81a703
+		rv = 1;
Jakub Jelen 81a703
+	}
Jakub Jelen 81a703
+
Jakub Jelen 81a703
+	DH_free(known);
Jakub Jelen 81a703
+	return rv;
Jakub Jelen 81a703
+}
Jakub Jelen 81a703
+
Jakub Jelen 81a703
 #endif /* WITH_OPENSSL */
Jakub Jelen 81a703
diff -up openssh-7.9p1/dh.h.fips openssh-7.9p1/dh.h
Jakub Jelen 81a703
--- openssh-7.9p1/dh.h.fips	2018-10-17 02:01:20.000000000 +0200
Jakub Jelen 81a703
+++ openssh-7.9p1/dh.h	2019-03-11 17:08:18.718828381 +0100
Jakub Jelen 81a703
@@ -43,6 +43,7 @@ DH	*dh_new_group_fallback(int);
Jakub Jelen 81a703
 
Jakub Jelen 81a703
 int	 dh_gen_key(DH *, int);
Jakub Jelen 81a703
 int	 dh_pub_is_valid(const DH *, const BIGNUM *);
Jakub Jelen 81a703
+int	 dh_is_known_group(const DH *);
Jakub Jelen 81a703
 
Jakub Jelen 81a703
 u_int	 dh_estimate(int);
Jakub Jelen 81a703
 
Jakub Jelen 81a703
diff -up openssh-7.9p1/kex.c.fips openssh-7.9p1/kex.c
Jakub Jelen 81a703
--- openssh-7.9p1/kex.c.fips	2019-03-11 17:06:37.614877975 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/kex.c	2019-03-11 17:06:37.621878041 +0100
Jakub Jelen 44e203
@@ -175,7 +196,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
 		}
Jakub Jelen 81a703
diff -up openssh-7.9p1/kexgexc.c.fips openssh-7.9p1/kexgexc.c
Jakub Jelen 81a703
--- openssh-7.9p1/kexgexc.c.fips	2018-10-17 02:01:20.000000000 +0200
Jakub Jelen 81a703
+++ openssh-7.9p1/kexgexc.c	2019-03-11 17:06:37.621878041 +0100
Jakub Jelen 535d34
@@ -28,6 +28,7 @@
Petr Lautrbach 802815
 
Jakub Jelen 132f8f
 #ifdef WITH_OPENSSL
Petr Lautrbach 802815
 
Jakub Jelen cb3595
+#include <openssl/crypto.h>
Petr Lautrbach 802815
 #include <sys/types.h>
Petr Lautrbach 802815
 
Jakub Jelen 6cf9b8
 #include <openssl/dh.h>
Jakub Jelen 81a703
@@ -118,6 +119,10 @@ input_kex_dh_gex_group(int type, u_int32
Jakub Jelen 81a703
 		r = SSH_ERR_ALLOC_FAIL;
Jakub Jelen 81a703
 		goto out;
Jakub Jelen 81a703
 	}
Jakub Jelen 81a703
+	if (FIPS_mode() && dh_is_known_group(kex->dh) == 0) {
Jakub Jelen 81a703
+		r = SSH_ERR_INVALID_ARGUMENT;
Jakub Jelen 81a703
+		goto out;
Jakub Jelen 81a703
+	}
Jakub Jelen 81a703
 	p = g = NULL; /* belong to kex->dh now */
Jakub Jelen 81a703
 
Jakub Jelen 81a703
 	/* generate and send 'e', client DH public key */
Jakub Jelen 81a703
diff -up openssh-7.9p1/Makefile.in.fips openssh-7.9p1/Makefile.in
Jakub Jelen 81a703
--- openssh-7.9p1/Makefile.in.fips	2019-03-11 17:06:37.615877984 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/Makefile.in	2019-03-11 17:06:37.621878041 +0100
Jakub Jelen 44e203
@@ -179,25 +179,25 @@ libssh.a: $(LIBSSH_OBJS)
Jakub Jelen 535d34
 	$(RANLIB) $@
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 ssh$(EXEEXT): $(LIBCOMPAT) libssh.a $(SSHOBJS)
Jakub Jelen 535d34
-	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHLIBS) $(LIBS) $(GSSLIBS)
Jakub Jelen 535d34
+	$(LD) -o $@ $(SSHOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHLIBS) $(LIBS) $(GSSLIBS)
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 sshd$(EXEEXT): libssh.a	$(LIBCOMPAT) $(SSHDOBJS)
Jakub Jelen 535d34
-	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
Jakub Jelen 535d34
+	$(LD) -o $@ $(SSHDOBJS) $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(LIBS) $(GSSLIBS) $(K5LIBS)
Jakub Jelen 535d34
 
Jakub Jelen 5878eb
 scp$(EXEEXT): $(LIBCOMPAT) libssh.a scp.o progressmeter.o
Jakub Jelen bbf61d
 	$(LD) -o $@ scp.o progressmeter.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 ssh-add$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-add.o
Jakub Jelen 535d34
-	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jakub Jelen 535d34
+	$(LD) -o $@ ssh-add.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 ssh-agent$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-agent.o ssh-pkcs11-client.o
Jakub Jelen 535d34
-	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jakub Jelen 535d34
+	$(LD) -o $@ ssh-agent.o ssh-pkcs11-client.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 ssh-keygen$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keygen.o
Jakub Jelen 535d34
-	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jakub Jelen 535d34
+	$(LD) -o $@ ssh-keygen.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Jakub Jelen 535d34
 
Jakub Jelen def1de
 ssh-keysign$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keysign.o readconf.o uidswap.o compat.o
Jakub Jelen bbf61d
-	$(LD) -o $@ ssh-keysign.o readconf.o uidswap.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jakub Jelen bbf61d
+	$(LD) -o $@ ssh-keysign.o readconf.o uidswap.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(LIBS)
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 ssh-pkcs11-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-pkcs11-helper.o ssh-pkcs11.o
Jakub Jelen 535d34
 	$(LD) -o $@ ssh-pkcs11-helper.o ssh-pkcs11.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lopenbsd-compat $(LIBS)
Jakub Jelen 44e203
@@ -215,7 +215,7 @@ ssh-cavs$(EXEEXT): $(LIBCOMPAT) libssh.a
Jakub Jelen 13073f
 	$(LD) -o $@ ssh-cavs.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jakub Jelen 535d34
 
Jakub Jelen 13073f
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
Jakub Jelen 13073f
-	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
Jakub Jelen 13073f
+	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
Jakub Jelen 535d34
 
Jakub Jelen 535d34
 sftp-server$(EXEEXT): $(LIBCOMPAT) libssh.a sftp.o sftp-common.o sftp-server.o sftp-server-main.o
Jakub Jelen 535d34
 	$(LD) -o $@ sftp-server.o sftp-common.o sftp-server-main.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
Jakub Jelen 81a703
diff -up openssh-7.9p1/myproposal.h.fips openssh-7.9p1/myproposal.h
Jakub Jelen 81a703
--- openssh-7.9p1/myproposal.h.fips	2018-10-17 02:01:20.000000000 +0200
Jakub Jelen 81a703
+++ openssh-7.9p1/myproposal.h	2019-03-11 17:06:37.621878041 +0100
Jakub Jelen 81a703
@@ -116,6 +116,14 @@
Jakub Jelen 9dbec7
 	"rsa-sha2-256," \
Jakub Jelen 9dbec7
 	"ssh-rsa"
Jakub Jelen 9dbec7
 
Jakub Jelen 9dbec7
+#define	KEX_FIPS_PK_ALG	\
Jakub Jelen 9dbec7
+	HOSTKEY_ECDSA_CERT_METHODS \
Jakub Jelen 9dbec7
+	"ssh-rsa-cert-v01@openssh.com," \
Jakub Jelen 9dbec7
+	HOSTKEY_ECDSA_METHODS \
Jakub Jelen 9dbec7
+	"rsa-sha2-512," \
Jakub Jelen 9dbec7
+	"rsa-sha2-256," \
Jakub Jelen 9dbec7
+	"ssh-rsa"
Jakub Jelen 9dbec7
+
Jakub Jelen 9dbec7
 /* the actual algorithms */
Jakub Jelen 9dbec7
 
Jakub Jelen 9dbec7
 #define KEX_SERVER_ENCRYPT \
Jakub Jelen 81a703
@@ -139,6 +147,38 @@
Jakub Jelen 13bf5b
 
Jakub Jelen 13bf5b
 #define KEX_CLIENT_MAC KEX_SERVER_MAC
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," \
Jakub Jelen 951e3c
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se" \
Jakub Jelen 951e3c
+	AESGCM_CIPHER_MODES
Petr Lautrbach 802815
+#ifdef HAVE_EVP_SHA256
Jakub Jelen eb751f
+# define KEX_DEFAULT_KEX_FIPS		\
Jakub Jelen eb751f
+	KEX_ECDH_METHODS \
Jakub Jelen eb751f
+	KEX_SHA2_METHODS \
Jakub Jelen eb751f
+	"diffie-hellman-group14-sha256"
Jakub Jelen eb751f
+# 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
Jakub Jelen eb751f
+# ifdef OPENSSL_HAS_NISTP521
Jakub Jelen eb751f
+#  define KEX_DEFAULT_KEX_FIPS		\
Jakub Jelen eb751f
+	"ecdh-sha2-nistp256," \
Jakub Jelen eb751f
+	"ecdh-sha2-nistp384," \
Jakub Jelen eb751f
+	"ecdh-sha2-nistp521"
Jakub Jelen eb751f
+# else
Jakub Jelen eb751f
+#  define KEX_DEFAULT_KEX_FIPS		\
Jakub Jelen eb751f
+	"ecdh-sha2-nistp256," \
Jakub Jelen eb751f
+	"ecdh-sha2-nistp384"
Jakub Jelen eb751f
+# endif
Petr Lautrbach 802815
+#define        KEX_FIPS_MAC \
Petr Lautrbach 802815
+       "hmac-sha1"
Petr Lautrbach 802815
+#endif
Petr Lautrbach 190035
+
Jakub Jelen eaa7af
 /* Not a KEX value, but here so all the algorithm defaults are together */
Jakub Jelen eaa7af
 #define	SSH_ALLOWED_CA_SIGALGS	\
Jakub Jelen eaa7af
 	"ecdsa-sha2-nistp256," \
Jakub Jelen 81a703
diff -up openssh-7.9p1/readconf.c.fips openssh-7.9p1/readconf.c
Jakub Jelen 81a703
--- openssh-7.9p1/readconf.c.fips	2019-03-11 17:06:37.601877853 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/readconf.c	2019-03-11 17:06:37.622878050 +0100
Jakub Jelen 81a703
@@ -2178,18 +2178,19 @@ fill_default_options(Options * options)
Jakub Jelen bbf61d
 	all_kex = kex_alg_list(',');
Jakub Jelen bbf61d
 	all_key = sshkey_alg_list(0, 0, 1, ',');
Jakub Jelen eaa7af
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
Jakub Jelen bbf61d
-#define ASSEMBLE(what, defaults, all) \
Jakub Jelen bbf61d
+#define ASSEMBLE(what, defaults, fips_defaults, all) \
Jakub Jelen bbf61d
 	do { \
Jakub Jelen bbf61d
 		if ((r = kex_assemble_names(&options->what, \
Jakub Jelen bbf61d
-		    defaults, all)) != 0) \
Jakub Jelen bbf61d
+		    (FIPS_mode() ? fips_defaults : defaults), \
Jakub Jelen bbf61d
+		    all)) != 0) \
Jakub Jelen bbf61d
 			fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
Jakub Jelen bbf61d
 	} while (0)
Jakub Jelen def1de
-	ASSEMBLE(ciphers, KEX_CLIENT_ENCRYPT, all_cipher);
Jakub Jelen def1de
-	ASSEMBLE(macs, KEX_CLIENT_MAC, all_mac);
Jakub Jelen def1de
-	ASSEMBLE(kex_algorithms, KEX_CLIENT_KEX, all_kex);
Jakub Jelen bbf61d
-	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
Jakub Jelen bbf61d
-	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
Jakub Jelen eaa7af
-	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
Jakub Jelen def1de
+	ASSEMBLE(ciphers, KEX_CLIENT_ENCRYPT, KEX_FIPS_ENCRYPT, all_cipher);
Jakub Jelen def1de
+	ASSEMBLE(macs, KEX_CLIENT_MAC, KEX_FIPS_MAC, all_mac);
Jakub Jelen def1de
+	ASSEMBLE(kex_algorithms, KEX_CLIENT_KEX, KEX_DEFAULT_KEX_FIPS, all_kex);
Jakub Jelen bbf61d
+	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
Jakub Jelen bbf61d
+	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
Jakub Jelen eaa7af
+	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, KEX_FIPS_PK_ALG, all_sig);
Jakub Jelen bbf61d
 #undef ASSEMBLE
Jakub Jelen bbf61d
 	free(all_cipher);
Jakub Jelen bbf61d
 	free(all_mac);
Jakub Jelen 81a703
diff -up openssh-7.9p1/sandbox-seccomp-filter.c.fips openssh-7.9p1/sandbox-seccomp-filter.c
Jakub Jelen 81a703
--- openssh-7.9p1/sandbox-seccomp-filter.c.fips	2019-03-11 17:06:37.586877712 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/sandbox-seccomp-filter.c	2019-03-11 17:06:37.622878050 +0100
Jakub Jelen 9dbec7
@@ -137,6 +137,9 @@ static const struct sock_filter preauth_
Jakub Jelen 6cf9b8
 #ifdef __NR_open
Jakub Jelen 17b491
 	SC_DENY(__NR_open, EACCES),
Jakub Jelen 6cf9b8
 #endif
Jakub Jelen 6cf9b8
+#ifdef __NR_socket
Jakub Jelen 17b491
+	SC_DENY(__NR_socket, EACCES),
Jakub Jelen 6cf9b8
+#endif
Jakub Jelen 6cf9b8
 #ifdef __NR_openat
Jakub Jelen 17b491
 	SC_DENY(__NR_openat, EACCES),
Jakub Jelen 6cf9b8
 #endif
Jakub Jelen 81a703
diff -up openssh-7.9p1/servconf.c.fips openssh-7.9p1/servconf.c
Jakub Jelen 81a703
--- openssh-7.9p1/servconf.c.fips	2019-03-11 17:06:37.568877543 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/servconf.c	2019-03-11 17:06:37.622878050 +0100
Jakub Jelen 81a703
@@ -209,18 +209,19 @@ assemble_algorithms(ServerOptions *o)
Jakub Jelen bbf61d
 	all_kex = kex_alg_list(',');
Jakub Jelen bbf61d
 	all_key = sshkey_alg_list(0, 0, 1, ',');
Jakub Jelen eaa7af
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
Jakub Jelen bbf61d
-#define ASSEMBLE(what, defaults, all) \
Jakub Jelen bbf61d
+#define ASSEMBLE(what, defaults, fips_defaults, all) \
Jakub Jelen bbf61d
 	do { \
Jakub Jelen bbf61d
-		if ((r = kex_assemble_names(&o->what, defaults, all)) != 0) \
Jakub Jelen bbf61d
+		if ((r = kex_assemble_names(&o->what, (FIPS_mode() \
Jakub Jelen bbf61d
+		    ? fips_defaults : defaults), all)) != 0) \
Jakub Jelen bbf61d
 			fatal("%s: %s: %s", __func__, #what, ssh_err(r)); \
Jakub Jelen bbf61d
 	} while (0)
Jakub Jelen bbf61d
-	ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, all_cipher);
Jakub Jelen bbf61d
-	ASSEMBLE(macs, KEX_SERVER_MAC, all_mac);
Jakub Jelen bbf61d
-	ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, all_kex);
Jakub Jelen bbf61d
-	ASSEMBLE(hostkeyalgorithms, KEX_DEFAULT_PK_ALG, all_key);
Jakub Jelen bbf61d
-	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, all_key);
Jakub Jelen bbf61d
-	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, all_key);
Jakub Jelen eaa7af
-	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, all_sig);
Jakub Jelen bbf61d
+	ASSEMBLE(ciphers, KEX_SERVER_ENCRYPT, KEX_FIPS_ENCRYPT, all_cipher);
Jakub Jelen bbf61d
+	ASSEMBLE(macs, KEX_SERVER_MAC, KEX_FIPS_MAC, all_mac);
Jakub Jelen bbf61d
+	ASSEMBLE(kex_algorithms, KEX_SERVER_KEX, KEX_DEFAULT_KEX_FIPS, all_kex);
Jakub Jelen bbf61d
+	ASSEMBLE(hostkeyalgorithms, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
Jakub Jelen bbf61d
+	ASSEMBLE(hostbased_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
Jakub Jelen bbf61d
+	ASSEMBLE(pubkey_key_types, KEX_DEFAULT_PK_ALG, KEX_FIPS_PK_ALG, all_key);
Jakub Jelen eaa7af
+	ASSEMBLE(ca_sign_algorithms, SSH_ALLOWED_CA_SIGALGS, KEX_FIPS_PK_ALG, all_sig);
Jakub Jelen bbf61d
 #undef ASSEMBLE
Jakub Jelen bbf61d
 	free(all_cipher);
Jakub Jelen bbf61d
 	free(all_mac);
Jakub Jelen 81a703
diff -up openssh-7.9p1/ssh.c.fips openssh-7.9p1/ssh.c
Jakub Jelen 81a703
--- openssh-7.9p1/ssh.c.fips	2019-03-11 17:06:37.602877862 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/ssh.c	2019-03-11 17:06:37.623878060 +0100
Jakub Jelen 6cf9b8
@@ -76,6 +76,8 @@
Petr Lautrbach 802815
 #include <openssl/evp.h>
Petr Lautrbach 802815
 #include <openssl/err.h>
Petr Lautrbach 190035
 #endif
Jakub Jelen cb3595
+#include <openssl/crypto.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
 
Jakub Jelen 81a703
@@ -600,6 +602,16 @@ main(int ac, char **av)
Petr Lautrbach 802815
 	sanitise_stdfd();
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 	__progname = ssh_get_progname(av[0]);
Jakub Jelen eaa7af
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
Jakub Jelen eaa7af
+	SSLeay_add_all_algorithms();
Jakub Jelen eaa7af
+#endif
Petr Lautrbach 802815
+	if (access("/etc/system-fips", F_OK) == 0)
Jakub Jelen 580f98
+		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.");
Jakub Jelen 580f98
+	}
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #ifndef HAVE_SETPROCTITLE
Petr Lautrbach 802815
 	/* Prepare for later setproctitle emulation */
Jakub Jelen 81a703
@@ -1283,6 +1294,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
+
Jakub Jelen def1de
 	/*
Jakub Jelen def1de
 	 * Discard other fds that are hanging around. These can cause problem
Jakub Jelen def1de
 	 * with backgrounded ssh processes started by ControlPersist.
Jakub Jelen 81a703
diff -up openssh-7.9p1/sshconnect2.c.fips openssh-7.9p1/sshconnect2.c
Jakub Jelen 81a703
--- openssh-7.9p1/sshconnect2.c.fips	2019-03-11 17:06:37.580877655 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/sshconnect2.c	2019-03-11 17:06:37.623878060 +0100
Jakub Jelen 3f5513
@@ -44,6 +44,8 @@
Petr Lautrbach 802815
 #include <vis.h>
Petr Lautrbach 802815
 #endif
Petr Lautrbach 802815
 
Jakub Jelen cb3595
+#include <openssl/crypto.h>
Petr Lautrbach 802815
+
Petr Lautrbach 802815
 #include "openbsd-compat/sys-queue.h"
Petr Lautrbach 802815
 
Petr Lautrbach 802815
 #include "xmalloc.h"
Jakub Jelen def1de
@@ -117,7 +117,8 @@ order_hostkeyalgs(char *host, struct soc
Jakub Jelen 9dbec7
 	for (i = 0; i < options.num_system_hostfiles; i++)
Jakub Jelen 9dbec7
 		load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Jakub Jelen 9dbec7
 
Jakub Jelen 9dbec7
-	oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
Jakub Jelen 9dbec7
+	oavail = avail = xstrdup((FIPS_mode()
Jakub Jelen 9dbec7
+	    ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG));
Jakub Jelen 9dbec7
 	maxlen = strlen(avail) + 1;
Jakub Jelen 9dbec7
 	first = xmalloc(maxlen);
Jakub Jelen 9dbec7
 	last = xmalloc(maxlen);
Jakub Jelen def1de
@@ -185,14 +185,16 @@ ssh_kex2(char *host, struct sockaddr *ho
Jakub Jelen def1de
 	if (options.hostkeyalgorithms != NULL) {
Jakub Jelen def1de
 		all_key = sshkey_alg_list(0, 0, 1, ',');
Jakub Jelen def1de
 		if (kex_assemble_names(&options.hostkeyalgorithms,
Jakub Jelen def1de
-		    KEX_DEFAULT_PK_ALG, all_key) != 0)
Jakub Jelen def1de
+		    (FIPS_mode() ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG),
Jakub Jelen def1de
+		    all_key) != 0)
Jakub Jelen def1de
 			fatal("%s: kex_assemble_namelist", __func__);
Jakub Jelen def1de
 		free(all_key);
Jakub Jelen def1de
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Jakub Jelen def1de
 		    compat_pkalg_proposal(options.hostkeyalgorithms);
Jakub Jelen def1de
 	} else {
Jakub Jelen def1de
 		/* Enforce default */
Jakub Jelen def1de
-		options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
Jakub Jelen def1de
+		options.hostkeyalgorithms = xstrdup((FIPS_mode()
Jakub Jelen def1de
+		    ? KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG));
Jakub Jelen def1de
 		/* Prefer algorithms that we already have keys for */
Jakub Jelen def1de
 		myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Jakub Jelen def1de
 		    compat_pkalg_proposal(
Jakub Jelen def1de
@@ -201,29 +201,34 @@ ssh_kex2(char *host, struct sockaddr *ho
Petr Lautrbach 802815
 
Jakub Jelen def1de
 #if defined(GSSAPI) && defined(WITH_OPENSSL)
Petr Lautrbach 802815
 	if (options.gss_keyex) {
Jakub Jelen def1de
-		/* Add the GSSAPI mechanisms currently supported on this
Petr Lautrbach 802815
-		 * client to the key exchange algorithm proposal */
Jakub Jelen def1de
-		orig = myproposal[PROPOSAL_KEX_ALGS];
Jakub Jelen 132f8f
-
Jakub Jelen e8876f
-		if (options.gss_server_identity)
Jakub Jelen def1de
-			gss_host = xstrdup(options.gss_server_identity);
Jakub Jelen e8876f
-		else if (options.gss_trust_dns)
Jakub Jelen def1de
-			gss_host = remote_hostname(ssh);
Jakub Jelen 132f8f
-		else
Jakub Jelen def1de
-			gss_host = xstrdup(host);
Jakub Jelen 132f8f
-
Jakub Jelen bc4ef0
-		gss = ssh_gssapi_client_mechanisms(gss_host,
Jakub Jelen bc4ef0
-		    options.gss_client_identity, options.gss_kex_algorithms);
Jakub Jelen 132f8f
-		if (gss) {
Jakub Jelen 132f8f
-			debug("Offering GSSAPI proposal: %s", gss);
Jakub Jelen def1de
-			xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
Jakub Jelen 132f8f
-			    "%s,%s", gss, orig);
Jakub Jelen def1de
-
Jakub Jelen def1de
-			/* If we've got GSSAPI algorithms, then we also support the
Jakub Jelen def1de
-			 * 'null' hostkey, as a last resort */
Jakub Jelen def1de
-			orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
Jakub Jelen def1de
-			xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
Jakub Jelen def1de
-			    "%s,null", 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 */
Jakub Jelen def1de
+			orig = myproposal[PROPOSAL_KEX_ALGS];
Jakub Jelen 132f8f
+
Jakub Jelen e8876f
+			if (options.gss_server_identity)
Jakub Jelen def1de
+				gss_host = xstrdup(options.gss_server_identity);
Jakub Jelen e8876f
+			else if (options.gss_trust_dns)
Jakub Jelen def1de
+				gss_host = remote_hostname(ssh);
Petr Lautrbach 802815
+			else
Jakub Jelen def1de
+				gss_host = xstrdup(host);
Jakub Jelen 132f8f
+
Jakub Jelen bc4ef0
+			gss = ssh_gssapi_client_mechanisms(gss_host,
Jakub Jelen bc4ef0
+			    options.gss_client_identity, options.gss_kex_algorithms);
Petr Lautrbach 802815
+			if (gss) {
Petr Lautrbach 802815
+				debug("Offering GSSAPI proposal: %s", gss);
Jakub Jelen def1de
+				xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
Petr Lautrbach 802815
+				    "%s,%s", gss, orig);
Jakub Jelen def1de
+
Jakub Jelen def1de
+				/* If we've got GSSAPI algorithms, then we also support the
Jakub Jelen def1de
+				 * 'null' hostkey, as a last resort */
Jakub Jelen def1de
+				orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
Jakub Jelen def1de
+				xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
Jakub Jelen def1de
+				    "%s,null", orig);
Petr Lautrbach 802815
+			}
Petr Lautrbach 802815
 		}
Petr Lautrbach 802815
 	}
Petr Lautrbach 802815
 #endif
Jakub Jelen 81a703
diff -up openssh-7.9p1/sshd.c.fips openssh-7.9p1/sshd.c
Jakub Jelen 81a703
--- openssh-7.9p1/sshd.c.fips	2019-03-11 17:06:37.617878003 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/sshd.c	2019-03-11 17:06:37.624878069 +0100
Petr Lautrbach 190035
@@ -66,6 +66,7 @@
Petr Lautrbach 190035
 #include <grp.h>
Petr Lautrbach 190035
 #include <pwd.h>
Petr Lautrbach 190035
 #include <signal.h>
Petr Lautrbach 190035
+#include <syslog.h>
Petr Lautrbach 190035
 #include <stdarg.h>
Petr Lautrbach 190035
 #include <stdio.h>
Petr Lautrbach 190035
 #include <stdlib.h>
Jakub Jelen 132f8f
@@ -77,6 +78,8 @@
Petr Lautrbach 802815
 #include <openssl/dh.h>
Petr Lautrbach 802815
 #include <openssl/bn.h>
Petr Lautrbach 802815
 #include <openssl/rand.h>
Jakub Jelen cb3595
+#include <openssl/crypto.h>
Petr Lautrbach 802815
+#include <fipscheck.h>
Petr Lautrbach 802815
 #include "openbsd-compat/openssl-compat.h"
Petr Lautrbach 190035
 #endif
Petr Lautrbach 802815
 
Jakub Jelen 81a703
@@ -1581,6 +1584,18 @@ main(int ac, char **av)
Petr Lautrbach 802815
 #endif
Petr Lautrbach 802815
 	__progname = ssh_get_progname(av[0]);
Petr Lautrbach 802815
 
Jakub Jelen eaa7af
+	OpenSSL_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;
Jakub Jelen 81a703
@@ -2036,6 +2051,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)
Jakub Jelen 81a703
@@ -2412,10 +2431,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);
Jakub Jelen 81a703
diff -up openssh-7.9p1/sshkey.c.fips openssh-7.9p1/sshkey.c
Jakub Jelen 81a703
--- openssh-7.9p1/sshkey.c.fips	2019-03-11 17:06:37.617878003 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/sshkey.c	2019-03-11 17:06:37.624878069 +0100
Jakub Jelen 6cf9b8
@@ -34,6 +34,7 @@
Petr Lautrbach 190035
 #include <openssl/evp.h>
Petr Lautrbach 190035
 #include <openssl/err.h>
Petr Lautrbach 190035
 #include <openssl/pem.h>
Jakub Jelen cb3595
+#include <openssl/crypto.h>
Jakub Jelen 132f8f
 #endif
Petr Lautrbach 190035
 
Petr Lautrbach 190035
 #include "crypto_api.h"
Jakub Jelen 44e203
@@ -57,6 +58,7 @@
Jakub Jelen 84d398
 #include "sshkey.h"
Jakub Jelen 3cd489
 #include "sshkey-xmss.h"
Jakub Jelen 84d398
 #include "match.h"
Jakub Jelen 9dbec7
+#include "log.h"
Jakub Jelen 84d398
 
Jakub Jelen 3cd489
 #include "xmss_fast.h"
Jakub Jelen 3cd489
 
Jakub Jelen 81a703
@@ -1514,6 +1516,8 @@ rsa_generate_private_key(u_int bits, RSA
Petr Lautrbach 190035
 	}
Petr Lautrbach 190035
 	if (!BN_set_word(f4, RSA_F4) ||
Petr Lautrbach 190035
 	    !RSA_generate_key_ex(private, bits, f4, NULL)) {
Petr Lautrbach 190035
+			if (FIPS_mode())
Petr Lautrbach 190035
+				logit("%s: the key length might be unsupported by FIPS mode approved key generation method", __func__);
Petr Lautrbach 190035
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
Petr Lautrbach 190035
 		goto out;
Petr Lautrbach 190035
 	}
Jakub Jelen 81a703
diff -up openssh-7.9p1/ssh-keygen.c.fips openssh-7.9p1/ssh-keygen.c
Jakub Jelen 81a703
--- openssh-7.9p1/ssh-keygen.c.fips	2019-03-11 17:06:37.590877750 +0100
Jakub Jelen 81a703
+++ openssh-7.9p1/ssh-keygen.c	2019-03-11 17:06:37.625878079 +0100
Jakub Jelen 81a703
@@ -230,6 +230,12 @@ type_bits_valid(int type, const char *na
Jakub Jelen 9dbec7
 	    OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
Jakub Jelen 9dbec7
 	if (*bitsp > maxbits)
Jakub Jelen 9dbec7
 		fatal("key bits exceeds maximum %d", maxbits);
Jakub Jelen 9dbec7
+	if (FIPS_mode()) {
Jakub Jelen 9dbec7
+		if (type == KEY_DSA)
Jakub Jelen 9dbec7
+			fatal("DSA keys are not allowed in FIPS mode");
Jakub Jelen 9dbec7
+		if (type == KEY_ED25519)
Jakub Jelen 9dbec7
+			fatal("ED25519 keys are not allowed in FIPS mode");
Jakub Jelen 9dbec7
+	}
Jakub Jelen 5b55d0
 	switch (type) {
Jakub Jelen 5b55d0
 	case KEY_DSA:
Jakub Jelen 5b55d0
 		if (*bitsp != 1024)