vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Norbert Pocs b63272
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/kexecdh.c ./kexecdh.c
Norbert Pocs b63272
--- ../openssh-8.7p1/kexecdh.c	2021-08-20 06:03:49.000000000 +0200
Norbert Pocs b63272
+++ ./kexecdh.c	2023-04-13 14:30:14.882449593 +0200
Norbert Pocs b63272
@@ -35,17 +35,57 @@
Norbert Pocs b63272
 #include <signal.h>
Norbert Pocs b63272
 
Norbert Pocs b63272
 #include <openssl/ecdh.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
+#include <openssl/err.h>
Norbert Pocs b63272
 
Norbert Pocs b63272
 #include "sshkey.h"
Norbert Pocs b63272
 #include "kex.h"
Norbert Pocs b63272
 #include "sshbuf.h"
Norbert Pocs b63272
 #include "digest.h"
Norbert Pocs b63272
 #include "ssherr.h"
Norbert Pocs b63272
+#include "log.h"
Norbert Pocs b63272
 
Norbert Pocs b63272
 static int
Norbert Pocs b63272
 kex_ecdh_dec_key_group(struct kex *, const struct sshbuf *, EC_KEY *key,
Norbert Pocs b63272
     const EC_GROUP *, struct sshbuf **);
Norbert Pocs b63272
 
Norbert Pocs b63272
+static EC_KEY *
Norbert Pocs b63272
+generate_ec_keys(int ec_nid)
Norbert Pocs b63272
+{
Norbert Pocs b63272
+	EC_KEY *client_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
+	const char *group_name;
Norbert Pocs b63272
+
Norbert Pocs b63272
+	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) == NULL ||
Norbert Pocs b63272
+	    (param_bld = OSSL_PARAM_BLD_new()) == NULL)
Norbert Pocs b63272
+		goto out;
Norbert Pocs b63272
+	if ((group_name = OSSL_EC_curve_nid2name(ec_nid)) == NULL ||
Norbert Pocs b63272
+	    OSSL_PARAM_BLD_push_utf8_string(param_bld,
Norbert Pocs b63272
+	        OSSL_PKEY_PARAM_GROUP_NAME, group_name, 0) != 1 ||
Norbert Pocs b63272
+	    (params = OSSL_PARAM_BLD_to_param(param_bld)) == NULL) {
Norbert Pocs b63272
+		error_f("Could not create OSSL_PARAM");
Norbert Pocs b63272
+		goto out;
Norbert Pocs b63272
+	}
Norbert Pocs b63272
+	if (EVP_PKEY_keygen_init(ctx) != 1 ||
Norbert Pocs b63272
+	    EVP_PKEY_CTX_set_params(ctx, params) != 1 ||
Norbert Pocs b63272
+	    EVP_PKEY_generate(ctx, &pkey) != 1 ||
Norbert Pocs b63272
+	    (client_key = EVP_PKEY_get1_EC_KEY(pkey)) == NULL) {
Norbert Pocs b63272
+		error_f("Could not generate ec keys");
Norbert Pocs b63272
+		goto out;
Norbert Pocs b63272
+	}
Norbert Pocs b63272
+out:
Norbert Pocs b63272
+	EVP_PKEY_free(pkey);
Norbert Pocs b63272
+	EVP_PKEY_CTX_free(ctx);
Norbert Pocs b63272
+	OSSL_PARAM_BLD_free(param_bld);
Norbert Pocs b63272
+	OSSL_PARAM_free(params);
Norbert Pocs b63272
+	return client_key;
Norbert Pocs b63272
+}
Norbert Pocs b63272
+
Norbert Pocs b63272
 int
Norbert Pocs b63272
 kex_ecdh_keypair(struct kex *kex)
Norbert Pocs b63272
 {
Norbert Pocs b63272
@@ -55,11 +95,7 @@
Norbert Pocs b63272
 	struct sshbuf *buf = NULL;
Norbert Pocs b63272
 	int r;
Norbert Pocs b63272
 
Norbert Pocs b63272
-	if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
Norbert Pocs b63272
-		r = SSH_ERR_ALLOC_FAIL;
Norbert Pocs b63272
-		goto out;
Norbert Pocs b63272
-	}
Norbert Pocs b63272
-	if (EC_KEY_generate_key(client_key) != 1) {
Norbert Pocs b63272
+	if ((client_key = generate_ec_keys(kex->ec_nid)) == NULL) {
Norbert Pocs b63272
 		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs b63272
 		goto out;
Norbert Pocs b63272
 	}
Norbert Pocs b63272
@@ -101,11 +137,7 @@
Norbert Pocs b63272
 	*server_blobp = NULL;
Norbert Pocs b63272
 	*shared_secretp = NULL;
Norbert Pocs b63272
 
Norbert Pocs b63272
-	if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
Norbert Pocs b63272
-		r = SSH_ERR_ALLOC_FAIL;
Norbert Pocs b63272
-		goto out;
Norbert Pocs b63272
-	}
Norbert Pocs b63272
-	if (EC_KEY_generate_key(server_key) != 1) {
Norbert Pocs b63272
+	if ((server_key = generate_ec_keys(kex->ec_nid)) == NULL) {
Norbert Pocs b63272
 		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs b63272
 		goto out;
Norbert Pocs b63272
 	}
Norbert Pocs b63272
@@ -140,11 +172,21 @@
Norbert Pocs b63272
 {
Norbert Pocs b63272
 	struct sshbuf *buf = NULL;
Norbert Pocs b63272
 	BIGNUM *shared_secret = NULL;
Norbert Pocs b63272
-	EC_POINT *dh_pub = NULL;
Norbert Pocs b63272
-	u_char *kbuf = NULL;
Norbert Pocs b63272
-	size_t klen = 0;
Norbert Pocs b63272
+	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs b63272
+	EVP_PKEY *pkey = NULL, *dh_pkey = NULL;
Norbert Pocs b63272
+	OSSL_PARAM_BLD *param_bld = NULL;
Norbert Pocs b63272
+	OSSL_PARAM *params = NULL;
Norbert Pocs b63272
+	u_char *kbuf = NULL, *pub = NULL;
Norbert Pocs b63272
+	size_t klen = 0, publen;
Norbert Pocs b63272
+	const char *group_name;
Norbert Pocs b63272
 	int r;
Norbert Pocs b63272
 
Norbert Pocs b63272
+	/* import EC_KEY to EVP_PKEY */
Norbert Pocs b63272
+	if ((r = ssh_create_evp_ec(key, kex->ec_nid, &pkey)) != 0) {
Norbert Pocs b63272
+		error_f("Could not create EVP_PKEY");
Norbert Pocs b63272
+		goto out;
Norbert Pocs b63272
+	}
Norbert Pocs b63272
+
Norbert Pocs b63272
 	*shared_secretp = NULL;
Norbert Pocs b63272
 
Norbert Pocs b63272
 	if ((buf = sshbuf_new()) == NULL) {
Norbert Pocs b63272
@@ -153,45 +195,82 @@
Norbert Pocs b63272
 	}
Norbert Pocs b63272
 	if ((r = sshbuf_put_stringb(buf, ec_blob)) != 0)
Norbert Pocs b63272
 		goto out;
Norbert Pocs b63272
-	if ((dh_pub = EC_POINT_new(group)) == NULL) {
Norbert Pocs b63272
+
Norbert Pocs b63272
+	/* the public key is in the buffer in octet string UNCOMPRESSED
Norbert Pocs b63272
+	 * format. See sshbuf_put_ec */
Norbert Pocs b63272
+	if ((r = sshbuf_get_string(buf, &pub, &publen)) != 0)
Norbert Pocs b63272
+		goto out;
Norbert Pocs b63272
+	sshbuf_reset(buf);
Norbert Pocs b63272
+	if ((ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL)) == NULL ||
Norbert Pocs b63272
+	    (param_bld = OSSL_PARAM_BLD_new()) == NULL) {
Norbert Pocs b63272
 		r = SSH_ERR_ALLOC_FAIL;
Norbert Pocs b63272
 		goto out;
Norbert Pocs b63272
 	}
Norbert Pocs b63272
-	if ((r = sshbuf_get_ec(buf, dh_pub, group)) != 0) {
Norbert Pocs b63272
+	if ((group_name = OSSL_EC_curve_nid2name(kex->ec_nid)) == NULL) {
Norbert Pocs b63272
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs b63272
+		goto out;
Norbert Pocs b63272
+	}
Norbert Pocs b63272
+	if (OSSL_PARAM_BLD_push_octet_string(param_bld,
Norbert Pocs b63272
+	        OSSL_PKEY_PARAM_PUB_KEY, pub, publen) != 1 ||
Norbert Pocs b63272
+	    OSSL_PARAM_BLD_push_utf8_string(param_bld,
Norbert Pocs b63272
+	        OSSL_PKEY_PARAM_GROUP_NAME, group_name, 0) != 1 ||
Norbert Pocs b63272
+	    (params = OSSL_PARAM_BLD_to_param(param_bld)) == NULL) {
Norbert Pocs b63272
+		error_f("Failed to set params for dh_pkey");
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
+	    EVP_PKEY_fromdata(ctx, &dh_pkey,
Norbert Pocs b63272
+	        EVP_PKEY_PUBLIC_KEY, params) != 1 ||
Norbert Pocs b63272
+	    EVP_PKEY_public_check(ctx) != 1) {
Norbert Pocs b63272
+		error_f("Peer public key import failed");
Norbert Pocs b63272
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs b63272
 		goto out;
Norbert Pocs b63272
 	}
Norbert Pocs b63272
-	sshbuf_reset(buf);
Norbert Pocs b63272
 
Norbert Pocs b63272
 #ifdef DEBUG_KEXECDH
Norbert Pocs b63272
 	fputs("public key:\n", stderr);
Norbert Pocs b63272
-	sshkey_dump_ec_point(group, dh_pub);
Norbert Pocs b63272
+	EVP_PKEY_print_public_fp(stderr, dh_pkey, 0, NULL);
Norbert Pocs b63272
 #endif
Norbert Pocs b63272
-	if (sshkey_ec_validate_public(group, dh_pub) != 0) {
Norbert Pocs b63272
-		r = SSH_ERR_MESSAGE_INCOMPLETE;
Norbert Pocs b63272
+	EVP_PKEY_CTX_free(ctx);
Norbert Pocs b63272
+	ctx = NULL;
Norbert Pocs b63272
+	if ((ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL)) == NULL ||
Norbert Pocs b63272
+	    EVP_PKEY_derive_init(ctx) != 1 ||
Norbert Pocs b63272
+	    EVP_PKEY_derive_set_peer(ctx, dh_pkey) != 1 ||
Norbert Pocs b63272
+	    EVP_PKEY_derive(ctx, NULL, &klen) != 1) {
Norbert Pocs b63272
+		error_f("Failed to get derive information");
Norbert Pocs b63272
+		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs b63272
 		goto out;
Norbert Pocs b63272
 	}
Norbert Pocs b63272
-	klen = (EC_GROUP_get_degree(group) + 7) / 8;
Norbert Pocs b63272
-	if ((kbuf = malloc(klen)) == NULL ||
Norbert Pocs b63272
-	    (shared_secret = BN_new()) == NULL) {
Norbert Pocs b63272
+	if ((kbuf = malloc(klen)) == NULL) {
Norbert Pocs b63272
 		r = SSH_ERR_ALLOC_FAIL;
Norbert Pocs b63272
 		goto out;
Norbert Pocs b63272
 	}
Norbert Pocs b63272
-	if (ECDH_compute_key(kbuf, klen, dh_pub, key, NULL) != (int)klen ||
Norbert Pocs b63272
-	    BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
Norbert Pocs b63272
+	if (EVP_PKEY_derive(ctx, kbuf, &klen) != 1) {
Norbert Pocs b63272
 		r = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs b63272
 		goto out;
Norbert Pocs b63272
 	}
Norbert Pocs b63272
 #ifdef DEBUG_KEXECDH
Norbert Pocs b63272
 	dump_digest("shared secret", kbuf, klen);
Norbert Pocs b63272
 #endif
Norbert Pocs b63272
+	if ((shared_secret = BN_new()) == NULL ||
Norbert Pocs b63272
+	    (BN_bin2bn(kbuf, klen, shared_secret) == NULL)) {
Norbert Pocs b63272
+		r = SSH_ERR_ALLOC_FAIL;
Norbert Pocs b63272
+		goto out;
Norbert Pocs b63272
+	}
Norbert Pocs b63272
 	if ((r = sshbuf_put_bignum2(buf, shared_secret)) != 0)
Norbert Pocs b63272
 		goto out;
Norbert Pocs b63272
 	*shared_secretp = buf;
Norbert Pocs b63272
 	buf = NULL;
Norbert Pocs b63272
  out:
Norbert Pocs b63272
-	EC_POINT_clear_free(dh_pub);
Norbert Pocs b63272
+	EVP_PKEY_CTX_free(ctx);
Norbert Pocs b63272
+	EVP_PKEY_free(pkey);
Norbert Pocs b63272
+	EVP_PKEY_free(dh_pkey);
Norbert Pocs b63272
+	OSSL_PARAM_BLD_free(param_bld);
Norbert Pocs b63272
+	OSSL_PARAM_free(params);
Norbert Pocs b63272
 	BN_clear_free(shared_secret);
Norbert Pocs b63272
 	freezero(kbuf, klen);
Norbert Pocs b63272
+	freezero(pub, publen);
Norbert Pocs b63272
 	sshbuf_free(buf);
Norbert Pocs b63272
 	return r;
Norbert Pocs b63272
 }