bmh10 / rpms / openssh

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