This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit adbc9b0113747bd3b4ea60a1aa23d7f92099b64c
Author: makejian <[email protected]>
AuthorDate: Tue Nov 24 17:33:02 2026 +0800

    crypto/openssl_mbedtls_wrapper: add raw key export and DER serialization
    
    Implement the EVP_PKEY_get_raw_public_key() and
    EVP_PKEY_get_raw_private_key() stubs for EC keys: the public key as
    the X9.62 uncompressed point, the private key as the fixed-length
    big-endian scalar.  Implement i2d_X509() to return the certificate
    DER from the underlying mbedtls_x509_crt, allocating the output
    buffer when *outp is NULL.  Add EVP_PKEY_eq(), which compares two
    keys by their serialized public key DER.
    
    Move struct x509_pm and struct pkey_pm from ssl_pm.c to ssl_pm.h so
    that evp.c and x509.c can reach the mbed TLS handles behind the
    wrapper objects.
    
    Signed-off-by: makejian <[email protected]>
---
 .../openssl_mbedtls_wrapper/include/openssl/evp.h  |   2 +
 crypto/openssl_mbedtls_wrapper/mbedtls/evp.c       | 175 ++++++++++++++++++++-
 crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c    |  12 --
 crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.h    |  29 ++++
 crypto/openssl_mbedtls_wrapper/mbedtls/x509.c      |  50 +++++-
 5 files changed, 253 insertions(+), 15 deletions(-)

diff --git a/crypto/openssl_mbedtls_wrapper/include/openssl/evp.h 
b/crypto/openssl_mbedtls_wrapper/include/openssl/evp.h
index e1696bfa6..2e0b03387 100644
--- a/crypto/openssl_mbedtls_wrapper/include/openssl/evp.h
+++ b/crypto/openssl_mbedtls_wrapper/include/openssl/evp.h
@@ -170,6 +170,8 @@ int PKCS5_PBKDF2_HMAC(const char *password, size_t 
password_len,
 
 const PKEY_METHOD *EVP_PKEY_method(void);
 
+int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b);
+
 int OpenSSL_add_all_algorithms(void);
 
 void EVP_cleanup(void);
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/evp.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/evp.c
index b1feae343..cea00829a 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/evp.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/evp.c
@@ -33,8 +33,11 @@
 
 #include <mbedtls/md.h>
 #include <mbedtls/pk.h>
+#include <mbedtls/platform.h>
 #include <mbedtls/rsa.h>
 
+#include "ssl_pm.h"
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -331,7 +334,57 @@ int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key, 
size_t *out_key_len)
 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey,
                                 uint8_t *out, size_t *out_len)
 {
-  return 0;
+  struct pkey_pm *pm;
+  mbedtls_pk_context *pk;
+  mbedtls_ecp_keypair *ecp;
+  unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
+  size_t olen;
+  int ret;
+
+  if (pkey == NULL || out_len == NULL)
+    {
+      return 0;
+    }
+
+  pm = (struct pkey_pm *)pkey->pkey_pm;
+  if (pm == NULL || pm->pkey == NULL)
+    {
+      return 0;
+    }
+
+  pk = pm->pkey;
+  if (!mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY))
+    {
+      return 0;
+    }
+
+  /* Raw public key is the X9.62 uncompressed point, not the SPKI DER. */
+
+  ecp = mbedtls_pk_ec(*pk);
+  ret = mbedtls_ecp_point_write_binary(&ecp->MBEDTLS_PRIVATE(grp),
+                                       &ecp->MBEDTLS_PRIVATE(Q),
+                                       MBEDTLS_ECP_PF_UNCOMPRESSED,
+                                       &olen, buf, sizeof(buf));
+  if (ret != 0)
+    {
+      return 0;
+    }
+
+  if (out == NULL)
+    {
+      *out_len = olen;
+      return 1;
+    }
+
+  if (*out_len < olen)
+    {
+      *out_len = olen;
+      return 0;
+    }
+
+  memcpy(out, buf, olen);
+  *out_len = olen;
+  return 1;
 }
 
 EVP_PKEY *d2i_PUBKEY(EVP_PKEY **out, const uint8_t **inp, long len)
@@ -342,7 +395,125 @@ EVP_PKEY *d2i_PUBKEY(EVP_PKEY **out, const uint8_t **inp, 
long len)
 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey,
                                  uint8_t *out, size_t *out_len)
 {
-  return 0;
+  struct pkey_pm *pm;
+  mbedtls_pk_context *pk;
+  mbedtls_ecp_keypair *ecp;
+  size_t need;
+  int ret;
+
+  if (pkey == NULL || out_len == NULL)
+    {
+      return 0;
+    }
+
+  pm = (struct pkey_pm *)pkey->pkey_pm;
+  if (pm == NULL || pm->pkey == NULL)
+    {
+      return 0;
+    }
+
+  pk = pm->pkey;
+  if (!mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY))
+    {
+      return 0;
+    }
+
+  /* Raw private key is the fixed-length big-endian scalar. */
+
+  ecp = mbedtls_pk_ec(*pk);
+  need = (ecp->MBEDTLS_PRIVATE(grp).pbits + 7) / 8;
+
+  if (out == NULL)
+    {
+      *out_len = need;
+      return 1;
+    }
+
+  if (*out_len < need)
+    {
+      *out_len = need;
+      return 0;
+    }
+
+  ret = mbedtls_mpi_write_binary(&ecp->MBEDTLS_PRIVATE(d), out, need);
+  if (ret != 0)
+    {
+      return 0;
+    }
+
+  *out_len = need;
+  return 1;
+}
+
+int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b)
+{
+  unsigned char *buf_a = NULL;
+  unsigned char *buf_b = NULL;
+  struct pkey_pm *pm_a;
+  struct pkey_pm *pm_b;
+  size_t buf_size;
+  int len_a;
+  int len_b;
+  int result;
+
+  if (a == NULL || b == NULL)
+    {
+      return -2;
+    }
+
+  pm_a = (struct pkey_pm *)a->pkey_pm;
+  pm_b = (struct pkey_pm *)b->pkey_pm;
+
+  if (pm_a == NULL || pm_a->pkey == NULL ||
+      pm_b == NULL || pm_b->pkey == NULL)
+    {
+      return -1;
+    }
+
+  /* Size the DER buffer from the key length so large keys (e.g. RSA-4096)
+   * fit while small keys do not over-allocate.  The public key DER adds
+   * only a small SubjectPublicKeyInfo overhead on top of the key material.
+   */
+
+  buf_size = mbedtls_pk_get_len(pm_a->pkey);
+  if (mbedtls_pk_get_len(pm_b->pkey) > buf_size)
+    {
+      buf_size = mbedtls_pk_get_len(pm_b->pkey);
+    }
+
+  buf_size = buf_size * 2 + 128;
+
+  buf_a = mbedtls_calloc(1, buf_size);
+  buf_b = mbedtls_calloc(1, buf_size);
+  if (buf_a == NULL || buf_b == NULL)
+    {
+      result = -1;
+      goto out;
+    }
+
+  len_a = mbedtls_pk_write_pubkey_der(pm_a->pkey, buf_a, buf_size);
+  len_b = mbedtls_pk_write_pubkey_der(pm_b->pkey, buf_b, buf_size);
+  if (len_a < 0 || len_b < 0)
+    {
+      result = -1;
+      goto out;
+    }
+
+  if (len_a != len_b)
+    {
+      result = 0;
+      goto out;
+    }
+
+  /* mbedtls writes DER from the end of the buffer */
+
+  result = memcmp(buf_a + buf_size - len_a,
+                  buf_b + buf_size - len_b, len_a) == 0 ? 1 : 0;
+
+out:
+  mbedtls_free(buf_a);
+  mbedtls_free(buf_b);
+  return result;
 }
 
 int OpenSSL_add_all_algorithms(void)
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c
index 55e5d384e..2f89e0a4b 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c
@@ -65,18 +65,6 @@ struct ssl_pm
   SSL *owner;
 };
 
-struct x509_pm
-{
-  mbedtls_x509_crt x509_crt;
-  mbedtls_x509_crt *ex_crt;
-};
-
-struct pkey_pm
-{
-  mbedtls_pk_context *pkey;
-  mbedtls_pk_context *ex_pkey;
-};
-
 /****************************************************************************
  * Public Data
  ****************************************************************************/
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.h 
b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.h
index 0d2065024..09833760d 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.h
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.h
@@ -28,11 +28,40 @@
 #include <openssl/ssl.h>
 #include "ssl_port.h"
 
+#include <mbedtls/pk.h>
+#include <mbedtls/x509_crt.h>
+
 #ifdef __cplusplus
 extern "C"
 {
 #endif
 
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/* Private mbedtls-backed representation of an EVP_PKEY.  EVP_PKEY.pkey_pm
+ * points to one of these.  Defined here so that helpers outside ssl_pm.c
+ * (evp.c, pkcs12.c) reference the same layout instead of re-declaring it.
+ */
+
+struct pkey_pm
+{
+  mbedtls_pk_context *pkey;
+  mbedtls_pk_context *ex_pkey;
+};
+
+/* Private mbedtls-backed representation of an X509.  X509.x509_pm points
+ * to one of these.  Defined here so that helpers outside ssl_pm.c
+ * (x509.c, pkcs12.c) reference the same layout instead of re-declaring it.
+ */
+
+struct x509_pm
+{
+  mbedtls_x509_crt x509_crt;
+  mbedtls_x509_crt *ex_crt;
+};
+
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/x509.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/x509.c
index 27f5abb1d..54e5c7d73 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/x509.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/x509.c
@@ -24,10 +24,16 @@
  * Included Files
  ****************************************************************************/
 
+#include <string.h>
+
 #include <openssl/ssl_dbg.h>
 #include <openssl/x509.h>
+
+#include <mbedtls/x509_crt.h>
+
 #include "ssl_port.h"
 #include "ssl_methods.h"
+#include "ssl_pm.h"
 
 /****************************************************************************
  * Public Functions
@@ -153,7 +159,49 @@ X509_ALGOR *X509_ALGOR_new(void)
 
 int i2d_X509(X509 *x509, uint8_t **outp)
 {
-  return 0;
+  const mbedtls_x509_crt *crt;
+  struct x509_pm *pm;
+  unsigned char *out;
+
+  if (x509 == NULL)
+    {
+      return 0;
+    }
+
+  pm = (struct x509_pm *)x509->x509_pm;
+  if (pm == NULL)
+    {
+      return 0;
+    }
+
+  crt = &pm->x509_crt;
+  if (crt->raw.p == NULL || crt->raw.len == 0)
+    {
+      return 0;
+    }
+
+  if (outp == NULL)
+    {
+      return (int)crt->raw.len;
+    }
+
+  out = *outp;
+  if (out == NULL)
+    {
+      out = ssl_mem_zalloc(crt->raw.len);
+      if (out == NULL)
+        {
+          return 0;
+        }
+
+      memcpy(out, crt->raw.p, crt->raw.len);
+      *outp = out;
+      return (int)crt->raw.len;
+    }
+
+  memcpy(out, crt->raw.p, crt->raw.len);
+  *outp = out + crt->raw.len;
+  return (int)crt->raw.len;
 }
 
 PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO(PKCS8_PRIV_KEY_INFO *info,

Reply via email to