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