vishalmishra434 / rpms / openssh

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