Norbert Pocs 1490ff
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-8.7p1/dh.c openssh-8.7p1-patched/dh.c
Norbert Pocs 48718a
--- openssh-8.7p1/dh.c	2023-05-25 09:01:23.295627077 +0200
Norbert Pocs 48718a
+++ openssh-8.7p1-patched/dh.c	2023-05-25 09:00:56.519332820 +0200
Norbert Pocs 1490ff
@@ -37,6 +37,9 @@
Norbert Pocs 587d7b
 #include <openssl/bn.h>
Norbert Pocs 587d7b
 #include <openssl/dh.h>
Norbert Pocs 587d7b
 #include <openssl/fips.h>
Norbert Pocs 587d7b
+#include <openssl/evp.h>
Norbert Pocs 587d7b
+#include <openssl/core_names.h>
Norbert Pocs 587d7b
+#include <openssl/param_build.h>
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 #include "dh.h"
Norbert Pocs 587d7b
 #include "pathnames.h"
Norbert Pocs 1490ff
@@ -290,10 +293,15 @@
Norbert Pocs 587d7b
 int
Norbert Pocs 587d7b
 dh_gen_key(DH *dh, int need)
Norbert Pocs 587d7b
 {
Norbert Pocs 587d7b
-	int pbits;
Norbert Pocs 587d7b
-	const BIGNUM *dh_p, *pub_key;
Norbert Pocs 587d7b
+	const BIGNUM *dh_p, *dh_g;
Norbert Pocs 587d7b
+	BIGNUM *pub_key = NULL, *priv_key = NULL;
Norbert Pocs 587d7b
+	EVP_PKEY *pkey = NULL;
Norbert Pocs 587d7b
+  	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs 587d7b
+  	OSSL_PARAM_BLD *param_bld = NULL;
Norbert Pocs 587d7b
+  	OSSL_PARAM *params = NULL;
Norbert Pocs 587d7b
+	int pbits, r = 0;
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
-	DH_get0_pqg(dh, &dh_p, NULL, NULL);
Norbert Pocs 587d7b
+	DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 	if (need < 0 || dh_p == NULL ||
Norbert Pocs 587d7b
 	    (pbits = BN_num_bits(dh_p)) <= 0 ||
Norbert Pocs 1490ff
@@ -301,19 +309,85 @@
Norbert Pocs 587d7b
 		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs 587d7b
 	if (need < 256)
Norbert Pocs 587d7b
 		need = 256;
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+	if ((param_bld = OSSL_PARAM_BLD_new()) == NULL ||
Norbert Pocs 587d7b
+	    (ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)) == NULL) {
Norbert Pocs 587d7b
+		OSSL_PARAM_BLD_free(param_bld);
Norbert Pocs 587d7b
+		return SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+	if (OSSL_PARAM_BLD_push_BN(param_bld,
Norbert Pocs 587d7b
+	        OSSL_PKEY_PARAM_FFC_P, dh_p) != 1 ||
Norbert Pocs 587d7b
+	    OSSL_PARAM_BLD_push_BN(param_bld,
Norbert Pocs 587d7b
+	        OSSL_PKEY_PARAM_FFC_G, dh_g) != 1) {
Norbert Pocs 587d7b
+		error_f("Could not set p,q,g parameters");
Norbert Pocs 587d7b
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
 	/*
Norbert Pocs 587d7b
 	 * Pollard Rho, Big step/Little Step attacks are O(sqrt(n)),
Norbert Pocs 587d7b
 	 * so double requested need here.
Norbert Pocs 587d7b
 	 */
Norbert Pocs 587d7b
-	if (!DH_set_length(dh, MINIMUM(need * 2, pbits - 1)))
Norbert Pocs 587d7b
-		return SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
-
Norbert Pocs 587d7b
-	if (DH_generate_key(dh) == 0)
Norbert Pocs 587d7b
-		return SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
-	DH_get0_key(dh, &pub_key, NULL);
Norbert Pocs 587d7b
-	if (!dh_pub_is_valid(dh, pub_key))
Norbert Pocs 587d7b
-		return SSH_ERR_INVALID_FORMAT;
Norbert Pocs 587d7b
-	return 0;
Norbert Pocs 587d7b
+	if (OSSL_PARAM_BLD_push_int(param_bld,
Norbert Pocs 587d7b
+	        OSSL_PKEY_PARAM_DH_PRIV_LEN,
Norbert Pocs 587d7b
+		MINIMUM(need * 2, pbits - 1)) != 1 ||
Norbert Pocs 587d7b
+	    (params = OSSL_PARAM_BLD_to_param(param_bld)) == NULL) {
Norbert Pocs 587d7b
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+	if (EVP_PKEY_fromdata_init(ctx) != 1) {
Norbert Pocs 587d7b
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+	if (EVP_PKEY_fromdata(ctx, &pkey,
Norbert Pocs 587d7b
+	        EVP_PKEY_KEY_PARAMETERS, params) != 1) {
Norbert Pocs 587d7b
+		error_f("Failed key generation");
Norbert Pocs 587d7b
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+	/* reuse context for key generation */
Norbert Pocs 587d7b
+	EVP_PKEY_CTX_free(ctx);
Norbert Pocs 587d7b
+	ctx = NULL;
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+	if ((ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL)) == NULL ||
Norbert Pocs 587d7b
+	    EVP_PKEY_keygen_init(ctx) != 1) {
Norbert Pocs 587d7b
+		error_f("Could not create or init context");
Norbert Pocs 587d7b
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+	if (EVP_PKEY_generate(ctx, &pkey) != 1) {
Norbert Pocs 587d7b
+		error_f("Could not generate keys");
Norbert Pocs 587d7b
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+	if (EVP_PKEY_public_check(ctx) != 1) {
Norbert Pocs 587d7b
+		error_f("The public key is incorrect");
Norbert Pocs 587d7b
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+	if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PUB_KEY,
Norbert Pocs 587d7b
+	    &pub_key) != 1 ||
Norbert Pocs 587d7b
+	    EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
Norbert Pocs 587d7b
+	    &priv_key) != 1 ||
Norbert Pocs 587d7b
+	    DH_set0_key(dh, pub_key, priv_key) != 1) {
Norbert Pocs 587d7b
+		error_f("Could not set pub/priv keys to DH struct");
Norbert Pocs 587d7b
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+	/* transferred */
Norbert Pocs 587d7b
+	pub_key = NULL;
Norbert Pocs 587d7b
+	priv_key = NULL;
Norbert Pocs 587d7b
+out:
Norbert Pocs 587d7b
+	OSSL_PARAM_free(params);
Norbert Pocs 587d7b
+	OSSL_PARAM_BLD_free(param_bld);
Norbert Pocs 587d7b
+	EVP_PKEY_CTX_free(ctx);
Norbert Pocs 587d7b
+	EVP_PKEY_free(pkey);
Norbert Pocs 587d7b
+	BN_clear_free(pub_key);
Norbert Pocs 587d7b
+	BN_clear_free(priv_key);
Norbert Pocs 587d7b
+	return r;
Norbert Pocs 587d7b
 }
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 DH *
Norbert Pocs 1490ff
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-8.7p1/kex.c openssh-8.7p1-patched/kex.c
Norbert Pocs 48718a
--- openssh-8.7p1/kex.c	2023-05-25 09:01:23.299627122 +0200
Norbert Pocs 48718a
+++ openssh-8.7p1-patched/kex.c	2023-05-25 09:00:56.519332820 +0200
Norbert Pocs 1490ff
@@ -1603,3 +1603,47 @@
Norbert Pocs 587d7b
 	return r;
Norbert Pocs 587d7b
 }
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
+#ifdef WITH_OPENSSL
Norbert Pocs 587d7b
+/* 
Norbert Pocs 587d7b
+ * Creates an EVP_PKEY from the given parameters and keys.
Norbert Pocs 587d7b
+ * The private key can be omitted.
Norbert Pocs 587d7b
+ */
Norbert Pocs 587d7b
+int
Norbert Pocs 587d7b
+kex_create_evp_dh(EVP_PKEY **pkey, const BIGNUM *p, const BIGNUM *q,
Norbert Pocs 587d7b
+    const BIGNUM *g, const BIGNUM *pub, const BIGNUM *priv)
Norbert Pocs 587d7b
+{
Norbert Pocs 587d7b
+	OSSL_PARAM_BLD *param_bld = NULL;
Norbert Pocs 587d7b
+	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs 587d7b
+	int r = 0;
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+	/* create EVP_PKEY-DH key */
Norbert Pocs 587d7b
+	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL)) == NULL ||
Norbert Pocs 587d7b
+	    (param_bld = OSSL_PARAM_BLD_new()) == NULL) {
Norbert Pocs 587d7b
+		error_f("EVP_PKEY_CTX or PARAM_BLD init failed");
Norbert Pocs 587d7b
+		r = SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+	if (OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_P, p) != 1 ||
Norbert Pocs 587d7b
+	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_Q, q) != 1 ||
Norbert Pocs 587d7b
+	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_G, g) != 1 ||
Norbert Pocs 587d7b
+	    OSSL_PARAM_BLD_push_BN(param_bld,
Norbert Pocs 587d7b
+	        OSSL_PKEY_PARAM_PUB_KEY, pub) != 1) {
Norbert Pocs 587d7b
+		error_f("Failed pushing params to OSSL_PARAM_BLD");
Norbert Pocs 587d7b
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+	if (priv != NULL &&
Norbert Pocs 587d7b
+	    OSSL_PARAM_BLD_push_BN(param_bld,
Norbert Pocs 587d7b
+	        OSSL_PKEY_PARAM_PRIV_KEY, priv) != 1) {
Norbert Pocs 587d7b
+		error_f("Failed pushing private key to OSSL_PARAM_BLD");
Norbert Pocs 587d7b
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+	if ((*pkey = sshkey_create_evp(param_bld, ctx)) == NULL)
Norbert Pocs 587d7b
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+out:
Norbert Pocs 587d7b
+	OSSL_PARAM_BLD_free(param_bld);
Norbert Pocs 587d7b
+	EVP_PKEY_CTX_free(ctx);
Norbert Pocs 587d7b
+	return r;
Norbert Pocs 587d7b
+}
Norbert Pocs 587d7b
+#endif /* WITH_OPENSSL */
Norbert Pocs 1490ff
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-8.7p1/kexdh.c openssh-8.7p1-patched/kexdh.c
Norbert Pocs 48718a
--- openssh-8.7p1/kexdh.c	2023-05-25 09:01:23.237626425 +0200
Norbert Pocs 48718a
+++ openssh-8.7p1-patched/kexdh.c	2023-05-25 09:03:21.817957988 +0200
Norbert Pocs 1490ff
@@ -35,6 +35,10 @@
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 #include "openbsd-compat/openssl-compat.h"
Norbert Pocs 587d7b
 #include <openssl/dh.h>
Norbert Pocs 1490ff
+#include <openssl/err.h>
Norbert Pocs 587d7b
+#include <openssl/evp.h>
Norbert Pocs 587d7b
+#include <openssl/core_names.h>
Norbert Pocs 587d7b
+#include <openssl/param_build.h>
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 #include "sshkey.h"
Norbert Pocs 587d7b
 #include "kex.h"
Norbert Pocs 1490ff
@@ -83,9 +87,12 @@
Norbert Pocs 587d7b
 kex_dh_compute_key(struct kex *kex, BIGNUM *dh_pub, struct sshbuf *out)
Norbert Pocs 587d7b
 {
Norbert Pocs 587d7b
 	BIGNUM *shared_secret = NULL;
Norbert Pocs 587d7b
+	const BIGNUM *pub, *priv, *p, *q, *g;
Norbert Pocs 587d7b
+	EVP_PKEY *pkey = NULL, *dh_pkey = NULL;
Norbert Pocs 587d7b
+	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs 587d7b
 	u_char *kbuf = NULL;
Norbert Pocs 587d7b
 	size_t klen = 0;
Norbert Pocs 1490ff
-	int kout, r;
Norbert Pocs 1490ff
+	int kout, r = 0;
Norbert Pocs 1490ff
 
Norbert Pocs 1490ff
 #ifdef DEBUG_KEXDH
Norbert Pocs 1490ff
 	fprintf(stderr, "dh_pub= ");
Norbert Pocs 48718a
@@ -100,24 +107,59 @@
Norbert Pocs 1490ff
 		r = SSH_ERR_MESSAGE_INCOMPLETE;
Norbert Pocs 587d7b
 		goto out;
Norbert Pocs 587d7b
 	}
Norbert Pocs 1490ff
-	klen = DH_size(kex->dh);
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+	DH_get0_key(kex->dh, &pub, &priv;;
Norbert Pocs 587d7b
+	DH_get0_pqg(kex->dh, &p, &q, &g);
Norbert Pocs 587d7b
+	/* import key */
Norbert Pocs 1490ff
+	r = kex_create_evp_dh(&pkey, p, q, g, pub, priv);
Norbert Pocs 1490ff
+	if (r != 0) {
Norbert Pocs 1490ff
+		error_f("Could not create EVP_PKEY for dh");
Norbert Pocs 1490ff
+		ERR_print_errors_fp(stderr);
Norbert Pocs 1490ff
+		goto out;
Norbert Pocs 1490ff
+	}
Norbert Pocs 587d7b
+	/* import peer key 
Norbert Pocs 587d7b
+	 * the parameters should be the same as with pkey
Norbert Pocs 587d7b
+	 */
Norbert Pocs 1490ff
+	r = kex_create_evp_dh(&dh_pkey, p, q, g, dh_pub, NULL);
Norbert Pocs 1490ff
+	if (r != 0) {
Norbert Pocs 1490ff
+		error_f("Could not import peer key for dh");
Norbert Pocs 1490ff
+		ERR_print_errors_fp(stderr);
Norbert Pocs 1490ff
+		goto out;
Norbert Pocs 1490ff
+	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+	if ((ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL)) == NULL) {
Norbert Pocs 587d7b
+		error_f("Could not init EVP_PKEY_CTX for dh");
Norbert Pocs 587d7b
+		r = SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+	if (EVP_PKEY_derive_init(ctx) != 1 ||
Norbert Pocs 587d7b
+	    EVP_PKEY_derive_set_peer(ctx, dh_pkey) != 1 ||
Norbert Pocs 1490ff
+	    EVP_PKEY_derive(ctx, NULL, &klen) != 1) {
Norbert Pocs 1490ff
+		error_f("Could not get key size");
Norbert Pocs 1490ff
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 1490ff
+		goto out;
Norbert Pocs 1490ff
+	}
Norbert Pocs 1490ff
 	if ((kbuf = malloc(klen)) == NULL ||
Norbert Pocs 1490ff
 	    (shared_secret = BN_new()) == NULL) {
Norbert Pocs 1490ff
 		r = SSH_ERR_ALLOC_FAIL;
Norbert Pocs 1490ff
 		goto out;
Norbert Pocs 1490ff
 	}
Norbert Pocs 1490ff
-	if ((kout = DH_compute_key(kbuf, dh_pub, kex->dh)) < 0 ||
Norbert Pocs 1490ff
-	    BN_bin2bn(kbuf, kout, shared_secret) == NULL) {
Norbert Pocs 1490ff
+	if (EVP_PKEY_derive(ctx, kbuf, &klen) != 1 ||
Norbert Pocs 587d7b
+	    BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
Norbert Pocs 587d7b
+		error_f("Could not derive key");
Norbert Pocs 587d7b
 		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
 		goto out;
Norbert Pocs 587d7b
 	}
Norbert Pocs 587d7b
 #ifdef DEBUG_KEXDH
Norbert Pocs 587d7b
-	dump_digest("shared secret", kbuf, kout);
Norbert Pocs 587d7b
+	dump_digest("shared secret", kbuf, klen);
Norbert Pocs 587d7b
 #endif
Norbert Pocs 587d7b
 	r = sshbuf_put_bignum2(out, shared_secret);
Norbert Pocs 587d7b
  out:
Norbert Pocs 587d7b
 	freezero(kbuf, klen);
Norbert Pocs 587d7b
 	BN_clear_free(shared_secret);
Norbert Pocs 587d7b
+	EVP_PKEY_free(pkey);
Norbert Pocs 587d7b
+	EVP_PKEY_free(dh_pkey);
Norbert Pocs 587d7b
+	EVP_PKEY_CTX_free(ctx);
Norbert Pocs 587d7b
 	return r;
Norbert Pocs 587d7b
 }
Norbert Pocs 587d7b
 
Norbert Pocs 1490ff
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-8.7p1/kex.h openssh-8.7p1-patched/kex.h
Norbert Pocs 48718a
--- openssh-8.7p1/kex.h	2023-05-25 09:01:23.299627122 +0200
Norbert Pocs 48718a
+++ openssh-8.7p1-patched/kex.h	2023-05-25 09:00:56.519332820 +0200
Norbert Pocs 587d7b
@@ -33,6 +33,9 @@
Norbert Pocs 587d7b
 # include <openssl/bn.h>
Norbert Pocs 587d7b
 # include <openssl/dh.h>
Norbert Pocs 587d7b
 # include <openssl/ecdsa.h>
Norbert Pocs 587d7b
+# include <openssl/evp.h>
Norbert Pocs 587d7b
+# include <openssl/core_names.h>
Norbert Pocs 587d7b
+# include <openssl/param_build.h>
Norbert Pocs 587d7b
 # ifdef OPENSSL_HAS_ECC
Norbert Pocs 587d7b
 #  include <openssl/ec.h>
Norbert Pocs 587d7b
 # else /* OPENSSL_HAS_ECC */
Norbert Pocs 587d7b
@@ -278,6 +281,8 @@
Norbert Pocs 587d7b
     const u_char pub[CURVE25519_SIZE], struct sshbuf *out, int)
Norbert Pocs 587d7b
 	__attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
Norbert Pocs 587d7b
 	__attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)));
Norbert Pocs 587d7b
+int	kex_create_evp_dh(EVP_PKEY **, const BIGNUM *, const BIGNUM *,
Norbert Pocs 587d7b
+    const BIGNUM *, const BIGNUM *, const BIGNUM *);
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
Norbert Pocs 587d7b
 void	dump_digest(const char *, const u_char *, int);