Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/dh.c.fips openssh-8.6p1/dh.c
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/dh.c.fips	2021-04-16 05:55:25.000000000 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/dh.c	2021-05-06 12:12:10.107634472 +0200
Dmitry Belyavskiy 9dff9c
@@ -164,6 +164,12 @@ choose_dh(int min, int wantbits, int max
Petr Šabata 81d24c
 	int best, bestcount, which, linenum;
Petr Šabata 81d24c
 	struct dhgroup dhg;
Petr Šabata 81d24c
 
Petr Šabata 81d24c
+	if (FIPS_mode()) {
Zoltan Fridrich e11cd7
+		verbose("Using arbitrary primes is not allowed in FIPS mode."
Petr Šabata 81d24c
+		    " Falling back to known groups.");
Petr Šabata 81d24c
+		return (dh_new_group_fallback(max));
Petr Šabata 81d24c
+	}
Petr Šabata 81d24c
+
Dmitry Belyavskiy 9dff9c
 	if ((f = fopen(get_moduli_filename(), "r")) == NULL) {
Petr Šabata 81d24c
 		logit("WARNING: could not open %s (%s), using fixed modulus",
Dmitry Belyavskiy 9dff9c
 		    get_moduli_filename(), strerror(errno));
Dmitry Belyavskiy 9dff9c
@@ -502,4 +508,38 @@ dh_estimate(int bits)
Petr Šabata 81d24c
 	return 8192;
Petr Šabata 81d24c
 }
Petr Šabata 81d24c
 
Petr Šabata 81d24c
+/*
Petr Šabata 81d24c
+ * Compares the received DH parameters with known-good groups,
Petr Šabata 81d24c
+ * which might be either from group14, group16 or group18.
Petr Šabata 81d24c
+ */
Petr Šabata 81d24c
+int
Petr Šabata 81d24c
+dh_is_known_group(const DH *dh)
Petr Šabata 81d24c
+{
Petr Šabata 81d24c
+	const BIGNUM *p, *g;
Petr Šabata 81d24c
+	const BIGNUM *known_p, *known_g;
Petr Šabata 81d24c
+	DH *known = NULL;
Petr Šabata 81d24c
+	int bits = 0, rv = 0;
Petr Šabata 81d24c
+
Petr Šabata 81d24c
+	DH_get0_pqg(dh, &p, NULL, &g);
Petr Šabata 81d24c
+	bits = BN_num_bits(p);
Petr Šabata 81d24c
+
Petr Šabata 81d24c
+	if (bits <= 3072) {
Petr Šabata 81d24c
+		known = dh_new_group14();
Petr Šabata 81d24c
+	} else if (bits <= 6144) {
Petr Šabata 81d24c
+		known = dh_new_group16();
Petr Šabata 81d24c
+	} else {
Petr Šabata 81d24c
+		known = dh_new_group18();
Petr Šabata 81d24c
+	}
Petr Šabata 81d24c
+
Petr Šabata 81d24c
+	DH_get0_pqg(known, &known_p, NULL, &known_g);
Petr Šabata 81d24c
+
Petr Šabata 81d24c
+	if (BN_cmp(g, known_g) == 0 &&
Petr Šabata 81d24c
+	    BN_cmp(p, known_p) == 0) {
Petr Šabata 81d24c
+		rv = 1;
Petr Šabata 81d24c
+	}
Petr Šabata 81d24c
+
Petr Šabata 81d24c
+	DH_free(known);
Petr Šabata 81d24c
+	return rv;
Petr Šabata 81d24c
+}
Petr Šabata 81d24c
+
Petr Šabata 81d24c
 #endif /* WITH_OPENSSL */
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/dh.h.fips openssh-8.6p1/dh.h
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/dh.h.fips	2021-05-06 12:08:36.498926877 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/dh.h	2021-05-06 12:11:28.393298005 +0200
Dmitry Belyavskiy 9dff9c
@@ -45,6 +45,7 @@ DH	*dh_new_group_fallback(int);
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 int	 dh_gen_key(DH *, int);
Petr Šabata 81d24c
 int	 dh_pub_is_valid(const DH *, const BIGNUM *);
Petr Šabata 81d24c
+int	 dh_is_known_group(const DH *);
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 u_int	 dh_estimate(int);
Dmitry Belyavskiy 9dff9c
 void	 dh_set_moduli_file(const char *);
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/kex.c.fips openssh-8.6p1/kex.c
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/kex.c.fips	2021-05-06 12:08:36.489926807 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/kex.c	2021-05-06 12:08:36.498926877 +0200
Dmitry Belyavskiy 9dff9c
@@ -203,7 +203,10 @@ kex_names_valid(const char *names)
Petr Šabata 81d24c
 	for ((p = strsep(&cp, ",")); p && *p != '\0';
Petr Šabata 81d24c
 	    (p = strsep(&cp, ","))) {
Petr Šabata 81d24c
 		if (kex_alg_by_name(p) == NULL) {
Petr Šabata 81d24c
-			error("Unsupported KEX algorithm \"%.100s\"", p);
Petr Šabata 81d24c
+			if (FIPS_mode())
Petr Šabata 81d24c
+				error("\"%.100s\" is not allowed in FIPS mode", p);
Petr Šabata 81d24c
+			else
Petr Šabata 81d24c
+				error("Unsupported KEX algorithm \"%.100s\"", p);
Petr Šabata 81d24c
 			free(s);
Petr Šabata 81d24c
 			return 0;
Petr Šabata 81d24c
 		}
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/kexgexc.c.fips openssh-8.6p1/kexgexc.c
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/kexgexc.c.fips	2021-04-16 05:55:25.000000000 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/kexgexc.c	2021-05-06 12:08:36.498926877 +0200
Petr Šabata 81d24c
@@ -28,6 +28,7 @@
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 #ifdef WITH_OPENSSL
Petr Šabata 81d24c
 
Petr Šabata 81d24c
+#include <openssl/crypto.h>
Petr Šabata 81d24c
 #include <sys/types.h>
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 #include <openssl/dh.h>
Dmitry Belyavskiy 9dff9c
@@ -115,6 +116,10 @@ input_kex_dh_gex_group(int type, u_int32
Petr Šabata 81d24c
 		r = SSH_ERR_ALLOC_FAIL;
Petr Šabata 81d24c
 		goto out;
Petr Šabata 81d24c
 	}
Petr Šabata 81d24c
+	if (FIPS_mode() && dh_is_known_group(kex->dh) == 0) {
Petr Šabata 81d24c
+		r = SSH_ERR_INVALID_ARGUMENT;
Petr Šabata 81d24c
+		goto out;
Petr Šabata 81d24c
+	}
Petr Šabata 81d24c
 	p = g = NULL; /* belong to kex->dh now */
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 	/* generate and send 'e', client DH public key */
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/myproposal.h.fips openssh-8.6p1/myproposal.h
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/myproposal.h.fips	2021-04-16 05:55:25.000000000 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/myproposal.h	2021-05-06 12:08:36.498926877 +0200
Dmitry Belyavskiy 32a826
@@ -57,6 +57,18 @@
Petr Šabata 81d24c
 	"rsa-sha2-256," \
Petr Šabata 81d24c
 	"ssh-rsa"
Petr Šabata 81d24c
 
Petr Šabata 81d24c
+#define	KEX_FIPS_PK_ALG	\
Petr Šabata 81d24c
+	"ecdsa-sha2-nistp256-cert-v01@openssh.com," \
Petr Šabata 81d24c
+	"ecdsa-sha2-nistp384-cert-v01@openssh.com," \
Petr Šabata 81d24c
+	"ecdsa-sha2-nistp521-cert-v01@openssh.com," \
Petr Šabata 81d24c
+	"rsa-sha2-512-cert-v01@openssh.com," \
Petr Šabata 81d24c
+	"rsa-sha2-256-cert-v01@openssh.com," \
Petr Šabata 81d24c
+	"ecdsa-sha2-nistp256," \
Petr Šabata 81d24c
+	"ecdsa-sha2-nistp384," \
Petr Šabata 81d24c
+	"ecdsa-sha2-nistp521," \
Petr Šabata 81d24c
+	"rsa-sha2-512," \
Dmitry Belyavskiy 32a826
+	"rsa-sha2-256"
Petr Šabata 81d24c
+
Petr Šabata 81d24c
 #define	KEX_SERVER_ENCRYPT \
Petr Šabata 81d24c
 	"chacha20-poly1305@openssh.com," \
Petr Šabata 81d24c
 	"aes128-ctr,aes192-ctr,aes256-ctr," \
Dmitry Belyavskiy 9dff9c
@@ -78,6 +92,27 @@
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 #define KEX_CLIENT_MAC KEX_SERVER_MAC
Petr Šabata 81d24c
 
Petr Šabata 81d24c
+#define	KEX_FIPS_ENCRYPT \
Petr Šabata 81d24c
+	"aes128-ctr,aes192-ctr,aes256-ctr," \
Petr Šabata 81d24c
+	"aes128-cbc,3des-cbc," \
Petr Šabata 81d24c
+	"aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se," \
Petr Šabata 81d24c
+	"aes128-gcm@openssh.com,aes256-gcm@openssh.com"
Petr Šabata 81d24c
+#define KEX_DEFAULT_KEX_FIPS		\
Petr Šabata 81d24c
+	"ecdh-sha2-nistp256," \
Petr Šabata 81d24c
+	"ecdh-sha2-nistp384," \
Petr Šabata 81d24c
+	"ecdh-sha2-nistp521," \
Petr Šabata 81d24c
+	"diffie-hellman-group-exchange-sha256," \
Petr Šabata 81d24c
+	"diffie-hellman-group16-sha512," \
Petr Šabata 81d24c
+	"diffie-hellman-group18-sha512," \
Petr Šabata 81d24c
+	"diffie-hellman-group14-sha256"
Petr Šabata 81d24c
+#define KEX_FIPS_MAC \
Petr Šabata 81d24c
+	"hmac-sha1," \
Petr Šabata 81d24c
+	"hmac-sha2-256," \
Petr Šabata 81d24c
+	"hmac-sha2-512," \
Petr Šabata 81d24c
+	"hmac-sha1-etm@openssh.com," \
Petr Šabata 81d24c
+	"hmac-sha2-256-etm@openssh.com," \
Petr Šabata 81d24c
+	"hmac-sha2-512-etm@openssh.com"
Petr Šabata 81d24c
+
Petr Šabata 81d24c
 /* Not a KEX value, but here so all the algorithm defaults are together */
Petr Šabata 81d24c
 #define	SSH_ALLOWED_CA_SIGALGS	\
DistroBaker d029bb
 	"ssh-ed25519," \
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/readconf.c.fips openssh-8.6p1/readconf.c
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/readconf.c.fips	2021-05-06 12:08:36.428926336 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/readconf.c	2021-05-06 12:08:36.499926885 +0200
Dmitry Belyavskiy 9dff9c
@@ -2538,11 +2538,16 @@ fill_default_options(Options * options)
Petr Šabata 81d24c
 	all_key = sshkey_alg_list(0, 0, 1, ',');
Petr Šabata 81d24c
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
Petr Šabata 81d24c
 	/* remove unsupported algos from default lists */
Petr Šabata 81d24c
-	def_cipher = match_filter_allowlist(KEX_CLIENT_ENCRYPT, all_cipher);
Petr Šabata 81d24c
-	def_mac = match_filter_allowlist(KEX_CLIENT_MAC, all_mac);
Petr Šabata 81d24c
-	def_kex = match_filter_allowlist(KEX_CLIENT_KEX, all_kex);
Petr Šabata 81d24c
-	def_key = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
Petr Šabata 81d24c
-	def_sig = match_filter_allowlist(SSH_ALLOWED_CA_SIGALGS, all_sig);
Petr Šabata 81d24c
+	def_cipher = match_filter_allowlist((FIPS_mode() ?
Petr Šabata 81d24c
+	    KEX_FIPS_ENCRYPT : KEX_CLIENT_ENCRYPT), all_cipher);
Petr Šabata 81d24c
+	def_mac = match_filter_allowlist((FIPS_mode() ?
Petr Šabata 81d24c
+	    KEX_FIPS_MAC : KEX_CLIENT_MAC), all_mac);
Petr Šabata 81d24c
+	def_kex = match_filter_allowlist((FIPS_mode() ?
Petr Šabata 81d24c
+	    KEX_DEFAULT_KEX_FIPS : KEX_CLIENT_KEX), all_kex);
Petr Šabata 81d24c
+	def_key = match_filter_allowlist((FIPS_mode() ?
Petr Šabata 81d24c
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG), all_key);
Petr Šabata 81d24c
+	def_sig = match_filter_allowlist((FIPS_mode() ?
Petr Šabata 81d24c
+	    KEX_FIPS_PK_ALG : SSH_ALLOWED_CA_SIGALGS), all_sig);
Petr Šabata 81d24c
 #define ASSEMBLE(what, defaults, all) \
Petr Šabata 81d24c
 	do { \
Petr Šabata 81d24c
 		if ((r = kex_assemble_names(&options->what, \
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/sandbox-seccomp-filter.c.fips openssh-8.6p1/sandbox-seccomp-filter.c
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/sandbox-seccomp-filter.c.fips	2021-05-06 12:08:36.463926606 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/sandbox-seccomp-filter.c	2021-05-06 12:08:36.499926885 +0200
Dmitry Belyavskiy 9dff9c
@@ -160,6 +160,9 @@ static const struct sock_filter preauth_
Petr Šabata 81d24c
 #ifdef __NR_open
Petr Šabata 81d24c
 	SC_DENY(__NR_open, EACCES),
Petr Šabata 81d24c
 #endif
Petr Šabata 81d24c
+#ifdef __NR_socket
Petr Šabata 81d24c
+	SC_DENY(__NR_socket, EACCES),
Petr Šabata 81d24c
+#endif
Petr Šabata 81d24c
 #ifdef __NR_openat
Petr Šabata 81d24c
 	SC_DENY(__NR_openat, EACCES),
Petr Šabata 81d24c
 #endif
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/servconf.c.fips openssh-8.6p1/servconf.c
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/servconf.c.fips	2021-05-06 12:08:36.455926545 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/servconf.c	2021-05-06 12:08:36.500926893 +0200
Dmitry Belyavskiy 9dff9c
@@ -226,11 +226,16 @@ assemble_algorithms(ServerOptions *o)
Petr Šabata 81d24c
 	all_key = sshkey_alg_list(0, 0, 1, ',');
Petr Šabata 81d24c
 	all_sig = sshkey_alg_list(0, 1, 1, ',');
Petr Šabata 81d24c
 	/* remove unsupported algos from default lists */
Petr Šabata 81d24c
-	def_cipher = match_filter_allowlist(KEX_SERVER_ENCRYPT, all_cipher);
Petr Šabata 81d24c
-	def_mac = match_filter_allowlist(KEX_SERVER_MAC, all_mac);
Petr Šabata 81d24c
-	def_kex = match_filter_allowlist(KEX_SERVER_KEX, all_kex);
Petr Šabata 81d24c
-	def_key = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key);
Petr Šabata 81d24c
-	def_sig = match_filter_allowlist(SSH_ALLOWED_CA_SIGALGS, all_sig);
Petr Šabata 81d24c
+	def_cipher = match_filter_allowlist((FIPS_mode() ?
Petr Šabata 81d24c
+	    KEX_FIPS_ENCRYPT : KEX_SERVER_ENCRYPT), all_cipher);
Petr Šabata 81d24c
+	def_mac = match_filter_allowlist((FIPS_mode() ?
Petr Šabata 81d24c
+	    KEX_FIPS_MAC : KEX_SERVER_MAC), all_mac);
Petr Šabata 81d24c
+	def_kex = match_filter_allowlist((FIPS_mode() ?
Petr Šabata 81d24c
+	    KEX_DEFAULT_KEX_FIPS : KEX_SERVER_KEX), all_kex);
Petr Šabata 81d24c
+	def_key = match_filter_allowlist((FIPS_mode() ?
Petr Šabata 81d24c
+	    KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG), all_key);
Petr Šabata 81d24c
+	def_sig = match_filter_allowlist((FIPS_mode() ?
Petr Šabata 81d24c
+	    KEX_FIPS_PK_ALG : SSH_ALLOWED_CA_SIGALGS), all_sig);
Petr Šabata 81d24c
 #define ASSEMBLE(what, defaults, all) \
Petr Šabata 81d24c
 	do { \
Petr Šabata 81d24c
 		if ((r = kex_assemble_names(&o->what, defaults, all)) != 0) \
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/ssh.c.fips openssh-8.6p1/ssh.c
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/ssh.c.fips	2021-05-06 12:08:36.467926637 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/ssh.c	2021-05-06 12:08:36.500926893 +0200
Dmitry Belyavskiy 9dff9c
@@ -77,6 +77,7 @@
Petr Šabata 81d24c
 #include <openssl/evp.h>
Petr Šabata 81d24c
 #include <openssl/err.h>
Petr Šabata 81d24c
 #endif
Petr Šabata 81d24c
+#include <openssl/crypto.h>
Petr Šabata 81d24c
 #include "openbsd-compat/openssl-compat.h"
Petr Šabata 81d24c
 #include "openbsd-compat/sys-queue.h"
Petr Šabata 81d24c
 
Dmitry Belyavskiy 9dff9c
@@ -1516,6 +1517,10 @@ main(int ac, char **av)
Petr Šabata 81d24c
 		exit(0);
Petr Šabata 81d24c
 	}
Dmitry Belyavskiy 9dff9c
 
Petr Šabata 81d24c
+	if (FIPS_mode()) {
Petr Šabata 81d24c
+		debug("FIPS mode initialized");
Petr Šabata 81d24c
+	}
Dmitry Belyavskiy 9dff9c
+
Petr Šabata 81d24c
 	/* Expand SecurityKeyProvider if it refers to an environment variable */
Petr Šabata 81d24c
 	if (options.sk_provider != NULL && *options.sk_provider == '$' &&
Dmitry Belyavskiy 9dff9c
 	    strlen(options.sk_provider) > 1) {
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/sshconnect2.c.fips openssh-8.6p1/sshconnect2.c
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/sshconnect2.c.fips	2021-05-06 12:08:36.485926777 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/sshconnect2.c	2021-05-06 12:08:36.501926900 +0200
Dmitry Belyavskiy 9dff9c
@@ -45,6 +45,8 @@
Petr Šabata 81d24c
 #include <vis.h>
Petr Šabata 81d24c
 #endif
Petr Šabata 81d24c
 
Petr Šabata 81d24c
+#include <openssl/crypto.h>
Petr Šabata 81d24c
+
Petr Šabata 81d24c
 #include "openbsd-compat/sys-queue.h"
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 #include "xmalloc.h"
Dmitry Belyavskiy 9dff9c
@@ -269,36 +271,41 @@ ssh_kex2(struct ssh *ssh, char *host, st
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 #if defined(GSSAPI) && defined(WITH_OPENSSL)
Petr Šabata 81d24c
 	if (options.gss_keyex) {
Petr Šabata 81d24c
-		/* Add the GSSAPI mechanisms currently supported on this
Petr Šabata 81d24c
-		 * client to the key exchange algorithm proposal */
Petr Šabata 81d24c
-		orig = myproposal[PROPOSAL_KEX_ALGS];
Petr Šabata 81d24c
-
Petr Šabata 81d24c
-		if (options.gss_server_identity) {
Petr Šabata 81d24c
-			gss_host = xstrdup(options.gss_server_identity);
Petr Šabata 81d24c
-		} else if (options.gss_trust_dns) {
Petr Šabata 81d24c
-			gss_host = remote_hostname(ssh);
Petr Šabata 81d24c
-			/* Fall back to specified host if we are using proxy command
Petr Šabata 81d24c
-			 * and can not use DNS on that socket */
Petr Šabata 81d24c
-			if (strcmp(gss_host, "UNKNOWN") == 0) {
Petr Šabata 81d24c
-				free(gss_host);
Petr Šabata 81d24c
+		if (FIPS_mode()) {
Petr Šabata 81d24c
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
Petr Šabata 81d24c
+			options.gss_keyex = 0;
Petr Šabata 81d24c
+		} else {
Petr Šabata 81d24c
+			/* Add the GSSAPI mechanisms currently supported on this
Petr Šabata 81d24c
+			 * client to the key exchange algorithm proposal */
Petr Šabata 81d24c
+			orig = myproposal[PROPOSAL_KEX_ALGS];
Petr Šabata 81d24c
+
Petr Šabata 81d24c
+			if (options.gss_server_identity) {
Petr Šabata 81d24c
+				gss_host = xstrdup(options.gss_server_identity);
Petr Šabata 81d24c
+			} else if (options.gss_trust_dns) {
Petr Šabata 81d24c
+				gss_host = remote_hostname(ssh);
Petr Šabata 81d24c
+				/* Fall back to specified host if we are using proxy command
Petr Šabata 81d24c
+				 * and can not use DNS on that socket */
Petr Šabata 81d24c
+				if (strcmp(gss_host, "UNKNOWN") == 0) {
Petr Šabata 81d24c
+					free(gss_host);
Petr Šabata 81d24c
+					gss_host = xstrdup(host);
Petr Šabata 81d24c
+				}
Petr Šabata 81d24c
+			} else {
Dmitry Belyavskiy 9dff9c
 				gss_host = xstrdup(host);
Dmitry Belyavskiy 9dff9c
 			}
Dmitry Belyavskiy 9dff9c
-		} else {
Dmitry Belyavskiy 9dff9c
-			gss_host = xstrdup(host);
Dmitry Belyavskiy 9dff9c
-		}
Dmitry Belyavskiy 9dff9c
 
Dmitry Belyavskiy 9dff9c
-		gss = ssh_gssapi_client_mechanisms(gss_host,
Dmitry Belyavskiy 9dff9c
-		    options.gss_client_identity, options.gss_kex_algorithms);
Dmitry Belyavskiy 9dff9c
-		if (gss) {
Dmitry Belyavskiy 9dff9c
-			debug("Offering GSSAPI proposal: %s", gss);
Dmitry Belyavskiy 9dff9c
-			xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
Dmitry Belyavskiy 9dff9c
-			    "%s,%s", gss, orig);
Dmitry Belyavskiy 9dff9c
-
Dmitry Belyavskiy 9dff9c
-			/* If we've got GSSAPI algorithms, then we also support the
Dmitry Belyavskiy 9dff9c
-			 * 'null' hostkey, as a last resort */
Dmitry Belyavskiy 9dff9c
-			orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
Dmitry Belyavskiy 9dff9c
-			xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
Dmitry Belyavskiy 9dff9c
-			    "%s,null", orig);
Petr Šabata 81d24c
+			gss = ssh_gssapi_client_mechanisms(gss_host,
Petr Šabata 81d24c
+			    options.gss_client_identity, options.gss_kex_algorithms);
Petr Šabata 81d24c
+			if (gss) {
Petr Šabata 81d24c
+				debug("Offering GSSAPI proposal: %s", gss);
Petr Šabata 81d24c
+				xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
Petr Šabata 81d24c
+				    "%s,%s", gss, orig);
Petr Šabata 81d24c
+
Petr Šabata 81d24c
+				/* If we've got GSSAPI algorithms, then we also support the
Petr Šabata 81d24c
+				 * 'null' hostkey, as a last resort */
Petr Šabata 81d24c
+				orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
Petr Šabata 81d24c
+				xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
Petr Šabata 81d24c
+				    "%s,null", orig);
Petr Šabata 81d24c
+			}
Petr Šabata 81d24c
 		}
Petr Šabata 81d24c
 	}
Petr Šabata 81d24c
 #endif
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/sshd.c.fips openssh-8.6p1/sshd.c
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/sshd.c.fips	2021-05-06 12:08:36.493926838 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/sshd.c	2021-05-06 12:13:56.501492639 +0200
Petr Šabata 81d24c
@@ -66,6 +66,7 @@
Petr Šabata 81d24c
 #include <grp.h>
Petr Šabata 81d24c
 #include <pwd.h>
Petr Šabata 81d24c
 #include <signal.h>
Petr Šabata 81d24c
+#include <syslog.h>
Petr Šabata 81d24c
 #include <stdarg.h>
Petr Šabata 81d24c
 #include <stdio.h>
Petr Šabata 81d24c
 #include <stdlib.h>
Petr Šabata 81d24c
@@ -77,6 +78,7 @@
Petr Šabata 81d24c
 #include <openssl/dh.h>
Petr Šabata 81d24c
 #include <openssl/bn.h>
Petr Šabata 81d24c
 #include <openssl/rand.h>
Petr Šabata 81d24c
+#include <openssl/crypto.h>
Petr Šabata 81d24c
 #include "openbsd-compat/openssl-compat.h"
Petr Šabata 81d24c
 #endif
Petr Šabata 81d24c
 
Dmitry Belyavskiy 9dff9c
@@ -1619,6 +1621,7 @@ main(int ac, char **av)
Petr Šabata 81d24c
 #endif
Petr Šabata 81d24c
 	__progname = ssh_get_progname(av[0]);
Petr Šabata 81d24c
 
Petr Šabata 81d24c
+	OpenSSL_add_all_algorithms();
Petr Šabata 81d24c
 	/* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
Petr Šabata 81d24c
 	saved_argc = ac;
Petr Šabata 81d24c
 	rexec_argc = ac;
Dmitry Belyavskiy 9dff9c
@@ -2110,6 +2113,10 @@ main(int ac, char **av)
Petr Šabata 81d24c
 	/* Reinitialize the log (because of the fork above). */
Petr Šabata 81d24c
 	log_init(__progname, options.log_level, options.log_facility, log_stderr);
Petr Šabata 81d24c
 
Petr Šabata 81d24c
+	if (FIPS_mode()) {
Petr Šabata 81d24c
+		debug("FIPS mode initialized");
Petr Šabata 81d24c
+	}
Petr Šabata 81d24c
+
Dmitry Belyavskiy 9dff9c
 	/*
Dmitry Belyavskiy 9dff9c
 	 * Chdir to the root directory so that the current disk can be
Dmitry Belyavskiy 9dff9c
 	 * unmounted if desired.
Dmitry Belyavskiy 9dff9c
@@ -2494,10 +2501,14 @@ do_ssh2_kex(struct ssh *ssh)
Petr Šabata 81d24c
 	if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0)
Petr Šabata 81d24c
 		orig = NULL;
Petr Šabata 81d24c
 
Petr Šabata 81d24c
-	if (options.gss_keyex)
Petr Šabata 81d24c
-		gss = ssh_gssapi_server_mechanisms();
Petr Šabata 81d24c
-	else
Petr Šabata 81d24c
-		gss = NULL;
Petr Šabata 81d24c
+	if (options.gss_keyex) {
Petr Šabata 81d24c
+		if (FIPS_mode()) {
Petr Šabata 81d24c
+			logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode");
Petr Šabata 81d24c
+			options.gss_keyex = 0;
Petr Šabata 81d24c
+		} else {
Petr Šabata 81d24c
+			gss = ssh_gssapi_server_mechanisms();
Petr Šabata 81d24c
+		}
Petr Šabata 81d24c
+	}
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 	if (gss && orig)
Petr Šabata 81d24c
 		xasprintf(&newstr, "%s,%s", gss, orig);
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/sshkey.c.fips openssh-8.6p1/sshkey.c
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/sshkey.c.fips	2021-05-06 12:08:36.493926838 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/sshkey.c	2021-05-06 12:08:36.502926908 +0200
Petr Šabata 81d24c
@@ -34,6 +34,7 @@
Petr Šabata 81d24c
 #include <openssl/evp.h>
Petr Šabata 81d24c
 #include <openssl/err.h>
Petr Šabata 81d24c
 #include <openssl/pem.h>
Petr Šabata 81d24c
+#include <openssl/crypto.h>
Petr Šabata 81d24c
 #endif
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 #include "crypto_api.h"
Petr Šabata 81d24c
@@ -57,6 +58,7 @@
Petr Šabata 81d24c
 #define SSHKEY_INTERNAL
Petr Šabata 81d24c
 #include "sshkey.h"
Petr Šabata 81d24c
 #include "match.h"
Petr Šabata 81d24c
+#include "log.h"
Petr Šabata 81d24c
 #include "ssh-sk.h"
Petr Šabata 81d24c
 
Petr Šabata 81d24c
 #ifdef WITH_XMSS
Dmitry Belyavskiy 9dff9c
@@ -1705,6 +1707,8 @@ rsa_generate_private_key(u_int bits, RSA
Dmitry Belyavskiy d0bf0e
		goto out;
Dmitry Belyavskiy d0bf0e
Dmitry Belyavskiy d0bf0e
	if (EVP_PKEY_keygen(ctx, &res) <= 0) {
Dmitry Belyavskiy 399096
+		if (FIPS_mode())
Dmitry Belyavskiy 399096
+			logit_f("the key length might be unsupported by FIPS mode approved key generation method");
Petr Šabata 81d24c
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
Petr Šabata 81d24c
 		goto out;
Petr Šabata 81d24c
 	}
Dmitry Belyavskiy 399096
@@ -2916,6 +2916,11 @@ sshkey_sign(struct sshkey *key,
Dmitry Belyavskiy 399096
 		break;
Dmitry Belyavskiy 399096
 	case KEY_ED25519_SK:
Dmitry Belyavskiy 399096
 	case KEY_ED25519_SK_CERT:
Dmitry Belyavskiy 399096
+		if (FIPS_mode()) {
Dmitry Belyavskiy 399096
+		    logit("Ed25519 keys are not alowed in FIPS mode");
Dmitry Belyavskiy 399096
+		    return SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 399096
+		}
Dmitry Belyavskiy 399096
+		/* Fallthrough */
Dmitry Belyavskiy 399096
 	case KEY_ECDSA_SK_CERT:
Dmitry Belyavskiy 399096
 	case KEY_ECDSA_SK:
Dmitry Belyavskiy 399096
 		r = sshsk_sign(sk_provider, key, sigp, lenp, data,
Dmitry Belyavskiy 399096
@@ -2973,6 +2978,10 @@ sshkey_verify(const struct sshkey *key,
Dmitry Belyavskiy 399096
 		return ssh_ed25519_verify(key, sig, siglen, data, dlen, compat);
Dmitry Belyavskiy 399096
 	case KEY_ED25519_SK:
Dmitry Belyavskiy 399096
 	case KEY_ED25519_SK_CERT:
Dmitry Belyavskiy 399096
+		if (FIPS_mode()) {
Dmitry Belyavskiy 399096
+		    logit("Ed25519 keys are not alowed in FIPS mode");
Dmitry Belyavskiy 399096
+		    return SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 399096
+		}
Dmitry Belyavskiy 399096
 		return ssh_ed25519_sk_verify(key, sig, siglen, data, dlen,
Dmitry Belyavskiy 399096
 		    compat, detailsp);
Dmitry Belyavskiy 399096
 #ifdef WITH_XMSS
Dmitry Belyavskiy 9dff9c
diff -up openssh-8.6p1/ssh-keygen.c.fips openssh-8.6p1/ssh-keygen.c
Dmitry Belyavskiy 9dff9c
--- openssh-8.6p1/ssh-keygen.c.fips	2021-05-06 12:08:36.467926637 +0200
Dmitry Belyavskiy 9dff9c
+++ openssh-8.6p1/ssh-keygen.c	2021-05-06 12:08:36.503926916 +0200
Dmitry Belyavskiy 9dff9c
@@ -205,6 +205,12 @@ type_bits_valid(int type, const char *na
Petr Šabata 81d24c
 #endif
Petr Šabata 81d24c
 	}
Petr Šabata 81d24c
 #ifdef WITH_OPENSSL
Petr Šabata 81d24c
+	if (FIPS_mode()) {
Petr Šabata 81d24c
+		if (type == KEY_DSA)
Petr Šabata 81d24c
+			fatal("DSA keys are not allowed in FIPS mode");
Dmitry Belyavskiy 399096
+		if (type == KEY_ED25519 || type == KEY_ED25519_SK)
Petr Šabata 81d24c
+			fatal("ED25519 keys are not allowed in FIPS mode");
Petr Šabata 81d24c
+	}
Petr Šabata 81d24c
 	switch (type) {
Petr Šabata 81d24c
 	case KEY_DSA:
Petr Šabata 81d24c
 		if (*bitsp != 1024)
Dmitry Belyavskiy 9dff9c
@@ -1098,9 +1104,17 @@ do_gen_all_hostkeys(struct passwd *pw)
Petr Šabata 81d24c
 			first = 1;
Petr Šabata 81d24c
 			printf("%s: generating new host keys: ", __progname);
Petr Šabata 81d24c
 		}
Petr Šabata 81d24c
+		type = sshkey_type_from_name(key_types[i].key_type);
Petr Šabata 81d24c
+
Petr Šabata 81d24c
+		/* Skip the keys that are not supported in FIPS mode */
Petr Šabata 81d24c
+		if (FIPS_mode() && (type == KEY_DSA || type == KEY_ED25519)) {
Petr Šabata 81d24c
+			logit("Skipping %s key in FIPS mode",
Petr Šabata 81d24c
+			    key_types[i].key_type_display);
Petr Šabata 81d24c
+			goto next;
Petr Šabata 81d24c
+		}
Petr Šabata 81d24c
+
Petr Šabata 81d24c
 		printf("%s ", key_types[i].key_type_display);
Petr Šabata 81d24c
 		fflush(stdout);
Petr Šabata 81d24c
-		type = sshkey_type_from_name(key_types[i].key_type);
Petr Šabata 81d24c
 		if ((fd = mkstemp(prv_tmp)) == -1) {
Petr Šabata 81d24c
 			error("Could not save your private key in %s: %s",
Dmitry Belyavskiy 9dff9c
 			    prv_tmp, strerror(errno));
Dmitry Belyavskiy 32a826
diff -up openssh-8.7p1/kexgen.c.fips3 openssh-8.7p1/kexgen.c
Dmitry Belyavskiy 32a826
--- openssh-8.7p1/kexgen.c.fips3	2022-07-11 16:11:21.973519913 +0200
Dmitry Belyavskiy 32a826
+++ openssh-8.7p1/kexgen.c	2022-07-11 16:25:31.172187365 +0200
Dmitry Belyavskiy 32a826
@@ -31,6 +31,7 @@
Dmitry Belyavskiy 32a826
 #include <stdio.h>
Dmitry Belyavskiy 32a826
 #include <string.h>
Dmitry Belyavskiy 32a826
 #include <signal.h>
Dmitry Belyavskiy 32a826
+#include <openssl/crypto.h>
Dmitry Belyavskiy 32a826
 
Dmitry Belyavskiy 32a826
 #include "sshkey.h"
Dmitry Belyavskiy 32a826
 #include "kex.h"
Dmitry Belyavskiy 32a826
@@ -115,10 +116,20 @@ kex_gen_client(struct ssh *ssh)
Dmitry Belyavskiy 32a826
 		break;
Dmitry Belyavskiy 32a826
 #endif
Dmitry Belyavskiy 32a826
 	case KEX_C25519_SHA256:
Dmitry Belyavskiy 32a826
-		r = kex_c25519_keypair(kex);
Dmitry Belyavskiy 32a826
+		if (FIPS_mode()) {
Dmitry Belyavskiy 32a826
+		    logit("Key exchange type c25519 is not alowed in FIPS mode");
Dmitry Belyavskiy 32a826
+		    r = SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 32a826
+		} else {
Dmitry Belyavskiy 32a826
+		    r = kex_c25519_keypair(kex);
Dmitry Belyavskiy 32a826
+		}
Dmitry Belyavskiy 32a826
 		break;
Dmitry Belyavskiy 32a826
 	case KEX_KEM_SNTRUP761X25519_SHA512:
Dmitry Belyavskiy 32a826
-		r = kex_kem_sntrup761x25519_keypair(kex);
Dmitry Belyavskiy 32a826
+		if (FIPS_mode()) {
Dmitry Belyavskiy 32a826
+		    logit("Key exchange type sntrup761 is not alowed in FIPS mode");
Dmitry Belyavskiy 32a826
+		    r = SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 32a826
+		} else {
Dmitry Belyavskiy 32a826
+		    r = kex_kem_sntrup761x25519_keypair(kex);
Dmitry Belyavskiy 32a826
+		}
Dmitry Belyavskiy 32a826
 		break;
Dmitry Belyavskiy 32a826
 	default:
Dmitry Belyavskiy 32a826
 		r = SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 32a826
@@ -186,11 +197,21 @@ input_kex_gen_reply(int type, u_int32_t
Dmitry Belyavskiy 32a826
 		break;
Dmitry Belyavskiy 32a826
 #endif
Dmitry Belyavskiy 32a826
 	case KEX_C25519_SHA256:
Dmitry Belyavskiy 32a826
-		r = kex_c25519_dec(kex, server_blob, &shared_secret);
Dmitry Belyavskiy 32a826
+		if (FIPS_mode()) {
Dmitry Belyavskiy 32a826
+		    logit("Key exchange type c25519 is not alowed in FIPS mode");
Dmitry Belyavskiy 32a826
+		    r = SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 32a826
+		} else {
Dmitry Belyavskiy 32a826
+		    r = kex_c25519_dec(kex, server_blob, &shared_secret);
Dmitry Belyavskiy 32a826
+		}
Dmitry Belyavskiy 32a826
 		break;
Dmitry Belyavskiy 32a826
 	case KEX_KEM_SNTRUP761X25519_SHA512:
Dmitry Belyavskiy 32a826
-		r = kex_kem_sntrup761x25519_dec(kex, server_blob,
Dmitry Belyavskiy 32a826
-		    &shared_secret);
Dmitry Belyavskiy 32a826
+		if (FIPS_mode()) {
Dmitry Belyavskiy 32a826
+		    logit("Key exchange type sntrup761 is not alowed in FIPS mode");
Dmitry Belyavskiy 32a826
+		    r = SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 32a826
+		} else {
Dmitry Belyavskiy 32a826
+		    r = kex_kem_sntrup761x25519_dec(kex, server_blob,
Dmitry Belyavskiy 32a826
+		        &shared_secret);
Dmitry Belyavskiy 32a826
+		}
Dmitry Belyavskiy 32a826
 		break;
Dmitry Belyavskiy 32a826
 	default:
Dmitry Belyavskiy 32a826
 		r = SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 32a826
@@ -285,12 +306,22 @@ input_kex_gen_init(int type, u_int32_t s
Dmitry Belyavskiy 32a826
 		break;
Dmitry Belyavskiy 32a826
 #endif
Dmitry Belyavskiy 32a826
 	case KEX_C25519_SHA256:
Dmitry Belyavskiy 32a826
-		r = kex_c25519_enc(kex, client_pubkey, &server_pubkey,
Dmitry Belyavskiy 32a826
-		    &shared_secret);
Dmitry Belyavskiy 32a826
+		if (FIPS_mode()) {
Dmitry Belyavskiy 32a826
+		    logit("Key exchange type c25519 is not alowed in FIPS mode");
Dmitry Belyavskiy 32a826
+		    r = SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 32a826
+		} else {
Dmitry Belyavskiy 32a826
+		    r = kex_c25519_enc(kex, client_pubkey, &server_pubkey,
Dmitry Belyavskiy 32a826
+		        &shared_secret);
Dmitry Belyavskiy 32a826
+		}
Dmitry Belyavskiy 32a826
 		break;
Dmitry Belyavskiy 32a826
 	case KEX_KEM_SNTRUP761X25519_SHA512:
Dmitry Belyavskiy 32a826
-		r = kex_kem_sntrup761x25519_enc(kex, client_pubkey,
Dmitry Belyavskiy 32a826
-		    &server_pubkey, &shared_secret);
Dmitry Belyavskiy 32a826
+		if (FIPS_mode()) {
Dmitry Belyavskiy 32a826
+		    logit("Key exchange type sntrup761 is not alowed in FIPS mode");
Dmitry Belyavskiy 32a826
+		    r = SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 32a826
+		} else {
Dmitry Belyavskiy 32a826
+		    r = kex_kem_sntrup761x25519_enc(kex, client_pubkey,
Dmitry Belyavskiy 32a826
+		        &server_pubkey, &shared_secret);
Dmitry Belyavskiy 32a826
+		}
Dmitry Belyavskiy 32a826
 		break;
Dmitry Belyavskiy 32a826
 	default:
Dmitry Belyavskiy 32a826
 		r = SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 399096
diff -up openssh-8.7p1/ssh-ed25519.c.fips3 openssh-8.7p1/ssh-ed25519.c
Dmitry Belyavskiy 399096
--- openssh-8.7p1/ssh-ed25519.c.fips3	2022-07-11 16:53:41.428343304 +0200
Dmitry Belyavskiy 399096
+++ openssh-8.7p1/ssh-ed25519.c	2022-07-11 16:56:09.284663661 +0200
Dmitry Belyavskiy 399096
@@ -24,6 +24,7 @@
Dmitry Belyavskiy 399096
 
Dmitry Belyavskiy 399096
 #include <string.h>
Dmitry Belyavskiy 399096
 #include <stdarg.h>
Dmitry Belyavskiy 399096
+#include <openssl/crypto.h>
Dmitry Belyavskiy 399096
 
Dmitry Belyavskiy 399096
 #include "log.h"
Dmitry Belyavskiy 399096
 #include "sshbuf.h"
Dmitry Belyavskiy 399096
@@ -52,6 +53,10 @@ ssh_ed25519_sign(const struct sshkey *ke
Dmitry Belyavskiy 399096
 	    key->ed25519_sk == NULL ||
Dmitry Belyavskiy 399096
 	    datalen >= INT_MAX - crypto_sign_ed25519_BYTES)
Dmitry Belyavskiy 399096
 		return SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 399096
+	if (FIPS_mode()) {
Dmitry Belyavskiy 399096
+	    logit("Ed25519 keys are not alowed in FIPS mode");
Dmitry Belyavskiy 399096
+	    return SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 399096
+	}
Dmitry Belyavskiy 399096
 	smlen = slen = datalen + crypto_sign_ed25519_BYTES;
Dmitry Belyavskiy 399096
 	if ((sig = malloc(slen)) == NULL)
Dmitry Belyavskiy 399096
 		return SSH_ERR_ALLOC_FAIL;
Dmitry Belyavskiy 399096
@@ -108,6 +113,10 @@ ssh_ed25519_verify(const struct sshkey *
Dmitry Belyavskiy 399096
 	    datalen >= INT_MAX - crypto_sign_ed25519_BYTES ||
Dmitry Belyavskiy 399096
 	    signature == NULL || signaturelen == 0)
Dmitry Belyavskiy 399096
 		return SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 399096
+	if (FIPS_mode()) {
Dmitry Belyavskiy 399096
+	    logit("Ed25519 keys are not alowed in FIPS mode");
Dmitry Belyavskiy 399096
+	    return SSH_ERR_INVALID_ARGUMENT;
Dmitry Belyavskiy 399096
+	}
Dmitry Belyavskiy 399096
 
Dmitry Belyavskiy 399096
 	if ((b = sshbuf_from(signature, signaturelen)) == NULL)
Dmitry Belyavskiy 399096
 		return SSH_ERR_ALLOC_FAIL;