From c200e5f13afe3d84e11b9e70000121dafc8040d6 Mon Sep 17 00:00:00 2001
From: tb <>
Date: Wed, 30 Oct 2024 06:10:35 +0000
Subject: Add a convenience wrapper for EC_POINT_point2oct()

EC_POING_point2oct() is annoying to use since its invocation involves
two calls: one to determine the space to allocate and one to pass the
buffer and perform the actual conversion. Wrap this dance in a helper
with the correct signature.

ok jsing
---
 src/lib/libcrypto/ec/ec_local.h |  8 +++++++-
 src/lib/libcrypto/ec/ec_oct.c   | 42 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/src/lib/libcrypto/ec/ec_local.h b/src/lib/libcrypto/ec/ec_local.h
index 1a49067cd8..7aa1c3f64e 100644
--- a/src/lib/libcrypto/ec/ec_local.h
+++ b/src/lib/libcrypto/ec/ec_local.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: ec_local.h,v 1.32 2024/10/28 18:01:26 tb Exp $ */
+/* $OpenBSD: ec_local.h,v 1.33 2024/10/30 06:10:35 tb Exp $ */
 /*
  * Originally written by Bodo Moeller for the OpenSSL project.
  */
@@ -359,6 +359,12 @@ int EC_POINT_get_Jprojective_coordinates(const EC_GROUP *group,
 int ec_group_is_builtin_curve(const EC_GROUP *group);
 int ec_group_get_field_type(const EC_GROUP *group);
 
+/*
+ * Wrapper around the unergonomic EC_POINT_point2oct().
+ */
+int ec_point_to_octets(const EC_GROUP *group, const EC_POINT *point, int form,
+    unsigned char **out_buf, size_t *len, BN_CTX *ctx_in);
+
 /* Public API in OpenSSL */
 const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group);
 const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group);
diff --git a/src/lib/libcrypto/ec/ec_oct.c b/src/lib/libcrypto/ec/ec_oct.c
index 8249866502..6fcda17403 100644
--- a/src/lib/libcrypto/ec/ec_oct.c
+++ b/src/lib/libcrypto/ec/ec_oct.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ec_oct.c,v 1.17 2024/04/10 15:01:31 beck Exp $ */
+/* $OpenBSD: ec_oct.c,v 1.18 2024/10/30 06:10:35 tb Exp $ */
 /*
  * Originally written by Bodo Moeller for the OpenSSL project.
  */
@@ -65,9 +65,11 @@
 
 #include <openssl/opensslconf.h>
 
+#include <openssl/asn1.h>
 #include <openssl/err.h>
 #include <openssl/opensslv.h>
 
+#include "asn1_local.h"
 #include "ec_local.h"
 
 int
@@ -109,6 +111,44 @@ EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
 }
 LCRYPTO_ALIAS(EC_POINT_set_compressed_coordinates_GFp);
 
+int
+ec_point_to_octets(const EC_GROUP *group, const EC_POINT *point, int form,
+    unsigned char **out_buf, size_t *out_len, BN_CTX *ctx)
+{
+	unsigned char *buf = NULL;
+	size_t len = 0;
+	int ret = 0;
+
+	if (out_buf != NULL && *out_buf != NULL)
+		goto err;
+
+	*out_len = 0;
+
+	if ((len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx)) == 0)
+		goto err;
+
+	if (out_buf == NULL)
+		goto done;
+
+	if ((buf = calloc(1, len)) == NULL)
+		goto err;
+	if (EC_POINT_point2oct(group, point, form, buf, len, ctx) != len)
+		goto err;
+
+	*out_buf = buf;
+	buf = NULL;
+
+ done:
+	*out_len = len;
+
+	ret = 1;
+
+ err:
+	freezero(buf, len);
+
+	return ret;
+}
+
 size_t
 EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
     point_conversion_form_t form, unsigned char *buf, size_t len,
-- 
cgit v1.2.3-55-g6feb