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 5ec13e8ff139ead592541f5a3eff7c59ed62274a
Author: makejian <[email protected]>
AuthorDate: Tue Nov 24 17:35:02 2026 +0800

    crypto/openssl_mbedtls_wrapper: add PKCS12 parsing support
    
    Implement the standard OpenSSL PKCS12 API on top of mbed TLS
    primitives, compiled only when MBEDTLS_PKCS12_C is enabled:
     - d2i_PKCS12, d2i_PKCS12_fp, d2i_PKCS12_bio
     - i2d_PKCS12, i2d_PKCS12_bio
     - PKCS12_parse, PKCS12_free
     - PKCS12_create, PKCS12_newpass
    
    Supported: BMPString password conversion, MAC verification via the
    PKCS#12 key derivation and HMAC, PKCS#12 PBE decryption (3DES-CBC
    with the PKCS#12 KDF), PKCS#5 PBES2 decryption (PBKDF2 + AES-CBC),
    certificate chain extraction (leaf certificate + CA stack),
    shroudedKeyBag private key extraction and BIO-based serialization.
    All dynamic memory comes from mbedtls_calloc()/mbedtls_free().
    
    Supporting interface changes: X509_cmp(), the BIO_free()
    declaration, the STACK_OF() macro, element-count tracking in
    OPENSSL_STACK (num vs num_alloc) and the NID set needed for the
    PKCS12 bag and PBE algorithm names.
    
    Declare the mbed TLS modules the wrapper requires in Kconfig so the
    option is only selectable when the needed features are built in.
    
    testing/crypto: add the PKCS12 API test ported from OpenSSL's
    test/pkcs12_api_test.c (cmocka, embedded DER blob, changepass
    round-trip).  Give testing/crypto a Makefile so its Kconfig files
    are picked up in Make builds; this also makes the existing passwd
    test reachable there.  The CMake side already discovers the new
    subdirectory through nuttx_add_subdirectory(), so no CMakeLists.txt
    change is needed.
    
    [skip-cmake-parity]
    
    Signed-off-by: makejian <[email protected]>
---
 crypto/openssl_mbedtls_wrapper/Kconfig             |   16 +
 .../openssl_mbedtls_wrapper/include/openssl/bio.h  |    2 +
 .../openssl_mbedtls_wrapper/include/openssl/nid.h  |   37 +-
 .../include/openssl/{nid.h => pkcs12.h}            |   58 +-
 .../include/openssl/stack.h                        |    1 +
 .../include/openssl/types.h                        |    2 +
 .../openssl_mbedtls_wrapper/include/openssl/x509.h |    2 +
 crypto/openssl_mbedtls_wrapper/mbedtls/pkcs12.c    | 2491 ++++++++++++++++++++
 crypto/openssl_mbedtls_wrapper/mbedtls/ssl_lib.c   |    4 +-
 crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c    |    2 +-
 crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.h    |    4 +-
 crypto/openssl_mbedtls_wrapper/mbedtls/x509.c      |   28 +
 testing/crypto/.gitignore                          |    1 +
 testing/crypto/Makefile                            |   25 +
 .../crypto/openssl_mbedtls_wrapper/CMakeLists.txt  |   40 +
 testing/crypto/openssl_mbedtls_wrapper/Kconfig     |   31 +
 testing/crypto/openssl_mbedtls_wrapper/Make.defs   |   25 +
 testing/crypto/openssl_mbedtls_wrapper/Makefile    |   34 +
 .../openssl_mbedtls_wrapper/pkcs12_api_test.c      |  427 ++++
 19 files changed, 3194 insertions(+), 36 deletions(-)

diff --git a/crypto/openssl_mbedtls_wrapper/Kconfig 
b/crypto/openssl_mbedtls_wrapper/Kconfig
index 3a4ee11a2..396907f5a 100644
--- a/crypto/openssl_mbedtls_wrapper/Kconfig
+++ b/crypto/openssl_mbedtls_wrapper/Kconfig
@@ -5,6 +5,22 @@
 
 config OPENSSL_MBEDTLS_WRAPPER
        depends on CRYPTO_MBEDTLS
+       depends on MBEDTLS_ECP_C
+       depends on MBEDTLS_PK_WRITE_C
+       depends on MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C
+       depends on MBEDTLS_PKCS5_C
+       depends on MBEDTLS_CIPHER_PADDING_PKCS7
+       depends on MBEDTLS_SSL_ALPN
+       depends on MBEDTLS_SSL_SERVER_NAME_INDICATION
+       depends on MBEDTLS_SSL_TLS_C
+       depends on MBEDTLS_SSL_CLI_C
+       depends on MBEDTLS_SSL_PROTO_TLS1_2
+       depends on MBEDTLS_X509_CRT_PARSE_C
+       depends on MBEDTLS_PK_PARSE_C
+       depends on MBEDTLS_RSA_C
+       depends on MBEDTLS_MD5_C
+       depends on MBEDTLS_SHA1_C
+       depends on MBEDTLS_FS_IO
        bool "openssl mbedtls wrapper"
        default n
 
diff --git a/crypto/openssl_mbedtls_wrapper/include/openssl/bio.h 
b/crypto/openssl_mbedtls_wrapper/include/openssl/bio.h
index 7bfab1eb9..565d125fd 100644
--- a/crypto/openssl_mbedtls_wrapper/include/openssl/bio.h
+++ b/crypto/openssl_mbedtls_wrapper/include/openssl/bio.h
@@ -101,6 +101,8 @@ int BIO_write(BIO *b, const void *data, int dlen);
 
 int BIO_read(BIO *b, void *data, int dlen);
 
+int BIO_free(BIO *a);
+
 void BIO_free_all(BIO *bio);
 
 BIO *BIO_next(BIO *b);
diff --git a/crypto/openssl_mbedtls_wrapper/include/openssl/nid.h 
b/crypto/openssl_mbedtls_wrapper/include/openssl/nid.h
index 975af6f7a..8345d967c 100644
--- a/crypto/openssl_mbedtls_wrapper/include/openssl/nid.h
+++ b/crypto/openssl_mbedtls_wrapper/include/openssl/nid.h
@@ -32,17 +32,32 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define NID_rsaEncryption 6
-#define NID_commonName 13
-#define NID_key_usage 83
-#define NID_X9_62_id_ecPublicKey 408
-#define NID_X9_62_prime256v1 415
-#define NID_sha256WithRSAEncryption 668
-#define NID_secp224r1 713
-#define NID_secp384r1 715
-#define NID_secp521r1 716
-#define NID_X25519 948
-#define NID_ED25519 949
+#define NID_undef                              0
+#define NID_rsaEncryption                      6
+#define NID_commonName                         13
+#define NID_pkcs7_data                         21
+#define NID_pkcs7_signed                       22
+#define NID_pkcs7_enveloped                    23
+#define NID_pkcs7_encrypted                    26
+#define NID_pbkdf2                             69
+#define NID_key_usage                          83
+#define NID_keyBag                             150
+#define NID_pkcs8ShroudedKeyBag                151
+#define NID_certBag                            152
+#define NID_crlBag                             153
+#define NID_secretBag                          154
+#define NID_safeContentsBag                    155
+#define NID_x509Certificate                    158
+#define NID_pbes2                              161
+#define NID_pbe_WithSHA1And3_Key_TripleDES_CBC 146
+#define NID_X9_62_id_ecPublicKey               408
+#define NID_X9_62_prime256v1                   415
+#define NID_sha256WithRSAEncryption            668
+#define NID_secp224r1                          713
+#define NID_secp384r1                          715
+#define NID_secp521r1                          716
+#define NID_X25519                             1034
+#define NID_ED25519                            1087
 
 /****************************************************************************
  * Public Function Prototypes
diff --git a/crypto/openssl_mbedtls_wrapper/include/openssl/nid.h 
b/crypto/openssl_mbedtls_wrapper/include/openssl/pkcs12.h
similarity index 59%
copy from crypto/openssl_mbedtls_wrapper/include/openssl/nid.h
copy to crypto/openssl_mbedtls_wrapper/include/openssl/pkcs12.h
index 975af6f7a..4aca0282b 100644
--- a/crypto/openssl_mbedtls_wrapper/include/openssl/nid.h
+++ b/crypto/openssl_mbedtls_wrapper/include/openssl/pkcs12.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * apps/crypto/openssl_mbedtls_wrapper/include/openssl/nid.h
+ * apps/crypto/openssl_mbedtls_wrapper/include/openssl/pkcs12.h
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -19,42 +19,60 @@
  * under the License.
  ****************************************************************************/
 
-#ifndef OPENSSL_MBEDTLS_WRAPPER_NID_H
-#define OPENSSL_MBEDTLS_WRAPPER_NID_H
+#ifndef OPENSSL_MBEDTLS_WRAPPER_PKCS12_H
+#define OPENSSL_MBEDTLS_WRAPPER_PKCS12_H
 
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <openssl/base.h>
+#include <openssl/bio.h>
+#include <openssl/x509.h>
+#include <openssl/evp.h>
+#include <openssl/stack.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
 
 /****************************************************************************
- * Pre-processor Definitions
+ * Public Types
  ****************************************************************************/
 
-#define NID_rsaEncryption 6
-#define NID_commonName 13
-#define NID_key_usage 83
-#define NID_X9_62_id_ecPublicKey 408
-#define NID_X9_62_prime256v1 415
-#define NID_sha256WithRSAEncryption 668
-#define NID_secp224r1 713
-#define NID_secp384r1 715
-#define NID_secp521r1 716
-#define NID_X25519 948
-#define NID_ED25519 949
+typedef struct PKCS12_st PKCS12;
 
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
 
-#ifdef __cplusplus
-extern "C"
-{
-#endif
+PKCS12 *d2i_PKCS12(PKCS12 **a, const unsigned char **in, long len);
+
+PKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **a);
+
+int PKCS12_parse(PKCS12 *p12, const char *pass,
+                 EVP_PKEY **pkey, X509 **cert,
+                 STACK_OF(X509) **ca);
+
+void PKCS12_free(PKCS12 *p12);
+
+int i2d_PKCS12(const PKCS12 *p12, unsigned char **out);
+
+PKCS12 *PKCS12_create(const char *pass, const char *name,
+                      EVP_PKEY *pkey, X509 *cert,
+                      STACK_OF(X509) *ca,
+                      int nid_key, int nid_cert,
+                      int iter, int mac_iter, int keytype);
+
+int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass);
+
+PKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12);
+
+int i2d_PKCS12_bio(BIO *bp, const PKCS12 *p12);
 
 #ifdef __cplusplus
 }
 #endif
 
-#endif /* OPENSSL_MBEDTLS_WRAPPER_NID_H */
+#endif /* OPENSSL_MBEDTLS_WRAPPER_PKCS12_H */
diff --git a/crypto/openssl_mbedtls_wrapper/include/openssl/stack.h 
b/crypto/openssl_mbedtls_wrapper/include/openssl/stack.h
index b92138ee0..290643bf2 100644
--- a/crypto/openssl_mbedtls_wrapper/include/openssl/stack.h
+++ b/crypto/openssl_mbedtls_wrapper/include/openssl/stack.h
@@ -39,6 +39,7 @@ extern "C"
 
 struct stack_st
 {
+    int num;
     char **data;
     int num_alloc;
     OPENSSL_sk_compfunc c;
diff --git a/crypto/openssl_mbedtls_wrapper/include/openssl/types.h 
b/crypto/openssl_mbedtls_wrapper/include/openssl/types.h
index 294ef20f6..c378b8e9b 100644
--- a/crypto/openssl_mbedtls_wrapper/include/openssl/types.h
+++ b/crypto/openssl_mbedtls_wrapper/include/openssl/types.h
@@ -52,6 +52,8 @@ typedef struct evp_pkey_st EVP_PKEY;
 typedef struct x509_method_st X509_METHOD;
 typedef struct pkey_method_st PKEY_METHOD;
 
+#define STACK_OF(type) OPENSSL_STACK
+
 #define ossl_inline inline
 #define OPENSSL_NPN_NEGOTIATED 1
 
diff --git a/crypto/openssl_mbedtls_wrapper/include/openssl/x509.h 
b/crypto/openssl_mbedtls_wrapper/include/openssl/x509.h
index cda221276..7d6771a54 100644
--- a/crypto/openssl_mbedtls_wrapper/include/openssl/x509.h
+++ b/crypto/openssl_mbedtls_wrapper/include/openssl/x509.h
@@ -149,6 +149,8 @@ const char *X509_verify_cert_error_string(long n);
 
 X509 *__X509_new(X509 *ix);
 
+int X509_cmp(const X509 *a, const X509 *b);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/pkcs12.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/pkcs12.c
new file mode 100644
index 000000000..fbacfcdee
--- /dev/null
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/pkcs12.c
@@ -0,0 +1,2491 @@
+/****************************************************************************
+ * apps/crypto/openssl_mbedtls_wrapper/mbedtls/pkcs12.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <openssl/pkcs12.h>
+#include <openssl/obj.h>
+#include <openssl/ssl_dbg.h>
+
+#include <mbedtls/asn1.h>
+#include <mbedtls/asn1write.h>
+#include <mbedtls/cipher.h>
+#include <mbedtls/constant_time.h>
+#include <mbedtls/ctr_drbg.h>
+#include <mbedtls/entropy.h>
+#include <mbedtls/md.h>
+#include <mbedtls/oid.h>
+#include <mbedtls/pk.h>
+#include <mbedtls/pkcs12.h>
+#include <mbedtls/pkcs5.h>
+#include <mbedtls/platform.h>
+#include <mbedtls/x509_crt.h>
+
+#include "ssl_port.h"
+#include "ssl_pm.h"
+
+#if defined(MBEDTLS_PKCS12_C)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SSL_PKCS12_ERROR_LEVEL       1
+#define PKCS12_MAX_PWDLEN            128
+
+/* PKCS#12 BagTypes: {pkcs-12 10 1 x} */
+
+#define MBEDTLS_OID_PKCS12_BAG_TYPE  MBEDTLS_OID_PKCS12 "\x0a\x01"
+
+/* PKCS#9 certTypes: {pkcs-9 22 x} */
+
+#define MBEDTLS_OID_PKCS9_CERT_TYPE  MBEDTLS_OID_PKCS9 "\x16"
+
+#define OID_ENTRY(oid_macro, nid_val) \
+  { oid_macro, sizeof(oid_macro) - 1, nid_val }
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct PKCS12_st
+{
+  unsigned char *data;
+  size_t len;
+};
+
+struct oid_nid_entry
+{
+  const char *oid;
+  size_t      oid_len;
+  int         nid;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct oid_nid_entry g_oid_table[] =
+{
+  /* PKCS#7 ContentInfo types */
+
+  OID_ENTRY(MBEDTLS_OID_PKCS7_DATA,           NID_pkcs7_data),
+  OID_ENTRY(MBEDTLS_OID_PKCS7_SIGNED_DATA,    NID_pkcs7_signed),
+  OID_ENTRY(MBEDTLS_OID_PKCS7_ENVELOPED_DATA, NID_pkcs7_enveloped),
+  OID_ENTRY(MBEDTLS_OID_PKCS7_ENCRYPTED_DATA, NID_pkcs7_encrypted),
+
+  /* PKCS#12 BagTypes */
+
+  OID_ENTRY(MBEDTLS_OID_PKCS12_BAG_TYPE "\x01", NID_keyBag),
+  OID_ENTRY(MBEDTLS_OID_PKCS12_BAG_TYPE "\x02", NID_pkcs8ShroudedKeyBag),
+  OID_ENTRY(MBEDTLS_OID_PKCS12_BAG_TYPE "\x03", NID_certBag),
+  OID_ENTRY(MBEDTLS_OID_PKCS12_BAG_TYPE "\x04", NID_crlBag),
+  OID_ENTRY(MBEDTLS_OID_PKCS12_BAG_TYPE "\x05", NID_secretBag),
+  OID_ENTRY(MBEDTLS_OID_PKCS12_BAG_TYPE "\x06", NID_safeContentsBag),
+
+  /* PKCS#9 certTypes */
+
+  OID_ENTRY(MBEDTLS_OID_PKCS9_CERT_TYPE "\x01", NID_x509Certificate),
+
+  /* PKCS#5 */
+
+  OID_ENTRY(MBEDTLS_OID_PKCS5 "\x0c", NID_pbkdf2),
+  OID_ENTRY(MBEDTLS_OID_PKCS5 "\x0d", NID_pbes2),
+
+  /* PKCS#12 PBE */
+
+  OID_ENTRY(MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC,
+            NID_pbe_WithSHA1And3_Key_TripleDES_CBC),
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int pkcs12_oid2nid(const mbedtls_asn1_buf *oid)
+{
+  size_t i;
+
+  if (oid->p == NULL || oid->len == 0)
+    {
+      return NID_undef;
+    }
+
+  for (i = 0; i < sizeof(g_oid_table) / sizeof(g_oid_table[0]); i++)
+    {
+      if (g_oid_table[i].oid_len == oid->len &&
+          memcmp(g_oid_table[i].oid, oid->p, oid->len) == 0)
+        {
+          return g_oid_table[i].nid;
+        }
+    }
+
+  return NID_undef;
+}
+
+/****************************************************************************
+ * Name: pkcs12_asc2uni
+ *
+ * Description:
+ *   Convert UTF-8 password to BMPString (UTF-16BE with null terminator)
+ *   as required by PKCS#12 specification (RFC 7292, B.1).
+ *
+ * Returns:
+ *   0 on success, -1 on failure. bmp_len includes the 2-byte null term.
+ ****************************************************************************/
+
+static int pkcs12_asc2uni(const char *pass, size_t pass_len,
+                          unsigned char *bmp, size_t *bmp_len)
+{
+  size_t out_len = (pass_len + 1) * 2;
+  size_t i;
+
+  if (out_len > PKCS12_MAX_PWDLEN * 2 + 2)
+    {
+      return -1;
+    }
+
+  for (i = 0; i < pass_len; i++)
+    {
+      bmp[i * 2]     = 0x00;
+      bmp[i * 2 + 1] = (unsigned char)pass[i];
+    }
+
+  /* Null terminator in UTF-16BE */
+
+  bmp[pass_len * 2]     = 0x00;
+  bmp[pass_len * 2 + 1] = 0x00;
+
+  *bmp_len = out_len;
+  return 0;
+}
+
+/****************************************************************************
+ * Name: pkcs12_verify_mac
+ *
+ * Description:
+ *   Verify the MacData integrity check on the authSafe content.
+ *   MacData ::= SEQUENCE {
+ *     mac        DigestInfo,
+ *     macSalt    OCTET STRING,
+ *     iterations INTEGER DEFAULT 1
+ *   }
+ ****************************************************************************/
+
+static int pkcs12_verify_mac(unsigned char *mac_data,
+                             unsigned char *mac_end,
+                             const unsigned char *authsafe,
+                             size_t authsafe_len,
+                             const unsigned char *bmp_pwd,
+                             size_t bmp_pwd_len)
+{
+  unsigned char computed_mac[MBEDTLS_MD_MAX_SIZE];
+  unsigned char key[MBEDTLS_MD_MAX_SIZE];
+  const mbedtls_md_info_t *md_info;
+  mbedtls_asn1_buf alg_params;
+  mbedtls_asn1_buf mac_digest;
+  mbedtls_asn1_buf mac_salt;
+  mbedtls_asn1_buf alg_oid;
+  mbedtls_md_type_t md_type;
+  unsigned char *p = mac_data;
+  size_t key_len;
+  size_t mac_len;
+  size_t len;
+  int iterations = 1;
+  int ret;
+
+  /* MacData ::= SEQUENCE */
+
+  ret = mbedtls_asn1_get_tag(&p, mac_end, &len,
+                             MBEDTLS_ASN1_CONSTRUCTED |
+                             MBEDTLS_ASN1_SEQUENCE);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  mac_end = p + len;
+
+  /* DigestInfo ::= SEQUENCE { algorithm, digest } */
+
+  ret = mbedtls_asn1_get_tag(&p, mac_end, &len,
+                             MBEDTLS_ASN1_CONSTRUCTED |
+                             MBEDTLS_ASN1_SEQUENCE);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  /* AlgorithmIdentifier */
+
+  ret = mbedtls_asn1_get_alg(&p, p + len, &alg_oid, &alg_params);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  ret = mbedtls_oid_get_md_alg(&alg_oid, &md_type);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  md_info = mbedtls_md_info_from_type(md_type);
+  if (md_info == NULL)
+    {
+      return -1;
+    }
+
+  mac_len = mbedtls_md_get_size(md_info);
+  key_len = mac_len;
+
+  /* Guard the fixed-size key/computed_mac buffers: the MAC digest algorithm
+   * comes from the (untrusted) MacData AlgorithmIdentifier, so reject any
+   * digest whose output would overflow the local buffers.
+   */
+
+  if (mac_len == 0 || mac_len > sizeof(key))
+    {
+      return -1;
+    }
+
+  /* digest OCTET STRING */
+
+  ret = mbedtls_asn1_get_tag(&p, mac_end, &mac_digest.len,
+                             MBEDTLS_ASN1_OCTET_STRING);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  mac_digest.p = p;
+  p += mac_digest.len;
+
+  /* macSalt OCTET STRING */
+
+  ret = mbedtls_asn1_get_tag(&p, mac_end, &mac_salt.len,
+                             MBEDTLS_ASN1_OCTET_STRING);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  mac_salt.p = p;
+  p += mac_salt.len;
+
+  /* iterations INTEGER (optional, default 1) */
+
+  if (p < mac_end)
+    {
+      ret = mbedtls_asn1_get_int(&p, mac_end, &iterations);
+      if (ret != 0)
+        {
+          return ret;
+        }
+    }
+
+  /* Derive MAC key using PKCS#12 key derivation (id=3 for MAC key) */
+
+  ret = mbedtls_pkcs12_derivation(key, key_len,
+                                  bmp_pwd, bmp_pwd_len,
+                                  mac_salt.p, mac_salt.len,
+                                  md_type,
+                                  MBEDTLS_PKCS12_DERIVE_MAC_KEY,
+                                  iterations);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  /* Compute HMAC over authSafe content */
+
+  ret = mbedtls_md_hmac(md_info, key, key_len,
+                        authsafe, authsafe_len,
+                        computed_mac);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  /* Compare */
+
+  if (mac_digest.len != mac_len ||
+      mbedtls_ct_memcmp(computed_mac, mac_digest.p, mac_len) != 0)
+    {
+      return -1;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: pkcs12_pbe_crypt
+ *
+ * Description:
+ *   Decrypt encrypted content using either PKCS#12 PBE or PKCS#5 PBES2.
+ *   Handles AlgorithmIdentifier dispatch internally.
+ ****************************************************************************/
+
+static int pkcs12_pbe_crypt(const unsigned char *data, size_t data_len,
+                            const unsigned char *pwd, size_t pwd_len,
+                            const unsigned char *bmp_pwd,
+                            size_t bmp_pwd_len,
+                            unsigned char **out, size_t *out_len)
+{
+  mbedtls_cipher_type_t cipher_alg;
+  mbedtls_md_type_t md_alg;
+  mbedtls_asn1_buf alg_params;
+  mbedtls_asn1_buf alg_oid;
+  unsigned char *dec_buf = NULL;
+  unsigned char *end = (unsigned char *)data + data_len;
+  unsigned char *p = (unsigned char *)data;
+  size_t dec_len = 0;
+  size_t enc_len;
+  int ret;
+
+  /* Parse AlgorithmIdentifier */
+
+  ret = mbedtls_asn1_get_alg(&p, end, &alg_oid, &alg_params);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  /* encryptedContent [0] IMPLICIT OCTET STRING — strip the tag+length */
+
+  ret = mbedtls_asn1_get_tag(&p, end, &enc_len,
+                             MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  dec_buf = mbedtls_calloc(1, enc_len);
+  if (!dec_buf)
+    {
+      return -1;
+    }
+
+  /* Try PKCS#12 PBE first */
+
+  ret = mbedtls_oid_get_pkcs12_pbe_alg(&alg_oid, &md_alg, &cipher_alg);
+  if (ret == 0)
+    {
+      ret = mbedtls_pkcs12_pbe_ext(&alg_params,
+                                   MBEDTLS_PKCS12_PBE_DECRYPT,
+                                   cipher_alg, md_alg,
+                                   pwd, pwd_len,
+                                   p, enc_len,
+                                   dec_buf, enc_len, &dec_len);
+      if (ret != 0)
+        {
+          mbedtls_free(dec_buf);
+          return ret;
+        }
+
+      *out = dec_buf;
+      *out_len = dec_len;
+      return 0;
+    }
+
+  /* Try PKCS#5 PBES2 */
+
+  if (pkcs12_oid2nid(&alg_oid) == NID_pbes2)
+    {
+      ret = mbedtls_pkcs5_pbes2_ext(&alg_params, MBEDTLS_PKCS5_DECRYPT,
+                                    pwd, pwd_len,
+                                    p, enc_len,
+                                    dec_buf, enc_len, &dec_len);
+      if (ret != 0)
+        {
+          mbedtls_free(dec_buf);
+          return ret;
+        }
+
+      *out = dec_buf;
+      *out_len = dec_len;
+      return 0;
+    }
+
+  mbedtls_free(dec_buf);
+  return -1;
+}
+
+/****************************************************************************
+ * Name: parse_bags
+ *
+ * Description:
+ *   Parse SafeContents (SEQUENCE OF SafeBag) and extract certs/keys.
+ ****************************************************************************/
+
+static int parse_bags(const unsigned char *data, size_t data_len,
+                      const unsigned char *pwd, size_t pwd_len,
+                      const unsigned char *bmp_pwd,
+                      size_t bmp_pwd_len,
+                      EVP_PKEY **pkey, X509 **cert,
+                      OPENSSL_STACK **ca)
+{
+  mbedtls_ctr_drbg_context ctr_drbg;
+  mbedtls_asn1_buf bag_oid;
+  mbedtls_asn1_buf cert_oid;
+  struct pkey_pm *pm;
+  unsigned char *end = (unsigned char *)data + data_len;
+  unsigned char *p = (unsigned char *)data;
+  unsigned char *bag_end;
+  unsigned char *cb_end;
+  unsigned char *cb_p;
+  OPENSSL_STACK *sk;
+  EVP_PKEY *k;
+  X509 *ca_cert;
+  X509 *c;
+  char **new_data;
+  size_t bag_len;
+  size_t seq_len;
+  size_t len;
+  int new_alloc;
+  int ret;
+
+  /* SafeContents ::= SEQUENCE OF SafeBag */
+
+  ret = mbedtls_asn1_get_tag(&p, end, &len,
+                             MBEDTLS_ASN1_CONSTRUCTED |
+                             MBEDTLS_ASN1_SEQUENCE);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  end = p + len;
+
+  while (p < end)
+    {
+      /* SafeBag ::= SEQUENCE { bagId, bagValue [0] EXPLICIT, bagAttr } */
+
+      ret = mbedtls_asn1_get_tag(&p, end, &bag_len,
+                                 MBEDTLS_ASN1_CONSTRUCTED |
+                                 MBEDTLS_ASN1_SEQUENCE);
+      if (ret != 0)
+        {
+          return ret;
+        }
+
+      bag_end = p + bag_len;
+
+      /* bagId OID */
+
+      ret = mbedtls_asn1_get_tag(&p, bag_end, &bag_oid.len,
+                                 MBEDTLS_ASN1_OID);
+      if (ret != 0)
+        {
+          return ret;
+        }
+
+      bag_oid.p = p;
+      p += bag_oid.len;
+
+      /* bagValue [0] EXPLICIT */
+
+      ret = mbedtls_asn1_get_tag(&p, bag_end, &len,
+                                 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
+                                 MBEDTLS_ASN1_CONSTRUCTED | 0);
+      if (ret != 0)
+        {
+          return ret;
+        }
+
+      if (pkcs12_oid2nid(&bag_oid) == NID_certBag)
+        {
+          /* CertBag ::= SEQUENCE { certId, certValue [0] EXPLICIT } */
+
+          cb_end = p + len;
+          cb_p = p;
+
+          ret = mbedtls_asn1_get_tag(&cb_p, cb_end, &seq_len,
+                                     MBEDTLS_ASN1_CONSTRUCTED |
+                                     MBEDTLS_ASN1_SEQUENCE);
+          if (ret != 0)
+            {
+              goto skip_bag;
+            }
+
+          /* certId OID */
+
+          ret = mbedtls_asn1_get_tag(&cb_p, cb_end, &cert_oid.len,
+                                     MBEDTLS_ASN1_OID);
+          if (ret != 0)
+            {
+              goto skip_bag;
+            }
+
+          cert_oid.p = cb_p;
+          cb_p += cert_oid.len;
+
+          if (pkcs12_oid2nid(&cert_oid) != NID_x509Certificate)
+            {
+              goto skip_bag;
+            }
+
+          /* certValue [0] EXPLICIT -> OCTET STRING containing DER cert */
+
+          ret = mbedtls_asn1_get_tag(&cb_p, cb_end, &len,
+                                     MBEDTLS_ASN1_CONTEXT_SPECIFIC |
+                                     MBEDTLS_ASN1_CONSTRUCTED | 0);
+          if (ret != 0)
+            {
+              goto skip_bag;
+            }
+
+          ret = mbedtls_asn1_get_tag(&cb_p, cb_end, &len,
+                                     MBEDTLS_ASN1_OCTET_STRING);
+          if (ret != 0)
+            {
+              goto skip_bag;
+            }
+
+          if (cert != NULL && *cert == NULL)
+            {
+              c = X509_new();
+              if (c == NULL)
+                {
+                  return -1;
+                }
+
+              ret = X509_METHOD_CALL(load, c, cb_p, (int)len);
+              if (ret != 0)
+                {
+                  X509_free(c);
+                  goto skip_bag;
+                }
+
+              *cert = c;
+            }
+          else if (ca != NULL)
+            {
+              ca_cert = X509_new();
+              if (ca_cert == NULL)
+                {
+                  return -1;
+                }
+
+              ret = X509_METHOD_CALL(load, ca_cert, cb_p, (int)len);
+              if (ret != 0)
+                {
+                  X509_free(ca_cert);
+                  goto skip_bag;
+                }
+
+              if (*ca == NULL)
+                {
+                  *ca = OPENSSL_sk_new_null();
+                  if (*ca == NULL)
+                    {
+                      X509_free(ca_cert);
+                      return -1;
+                    }
+                }
+
+              sk = *ca;
+
+              /* Grow the backing array when the preallocated slots
+               * (num_alloc) are exhausted.  Without this bounds check a
+               * PKCS#12 file carrying more CA certificates than the initial
+               * capacity would write past sk->data[] (heap overflow).
+               */
+
+              if (sk->num >= sk->num_alloc)
+                {
+                  new_alloc = sk->num_alloc * 2;
+                  new_data = ssl_mem_zalloc(new_alloc * sizeof(*new_data));
+                  if (new_data == NULL)
+                    {
+                      X509_free(ca_cert);
+                      return -1;
+                    }
+
+                  memcpy(new_data, sk->data,
+                         sk->num * sizeof(*new_data));
+                  ssl_mem_free(sk->data);
+                  sk->data = new_data;
+                  sk->num_alloc = new_alloc;
+                }
+
+              sk->data[sk->num] = (char *)ca_cert;
+              sk->num++;
+            }
+        }
+      else if (pkcs12_oid2nid(&bag_oid) == NID_pkcs8ShroudedKeyBag)
+        {
+          /* PKCS8ShroudedKeyBag: EncryptedPrivateKeyInfo */
+
+          if (pkey != NULL && *pkey == NULL)
+            {
+              *pkey = EVP_PKEY_new();
+              if (*pkey == NULL)
+                {
+                  return -1;
+                }
+
+              pm = (struct pkey_pm *)(*pkey)->pkey_pm;
+              if (!pm->pkey)
+                {
+                  pm->pkey = mbedtls_calloc(1, sizeof(mbedtls_pk_context));
+                  if (!pm->pkey)
+                    {
+                      EVP_PKEY_free(*pkey);
+                      *pkey = NULL;
+                      return -1;
+                    }
+                }
+
+              mbedtls_pk_init(pm->pkey);
+              mbedtls_ctr_drbg_init(&ctr_drbg);
+
+              ret = mbedtls_pk_parse_key(pm->pkey, p, len,
+                                         pwd, pwd_len,
+                                         mbedtls_ctr_drbg_random,
+                                         &ctr_drbg);
+              mbedtls_ctr_drbg_free(&ctr_drbg);
+              if (ret != 0)
+                {
+                  SSL_DEBUG(SSL_PKCS12_ERROR_LEVEL,
+                            "pk_parse_key failed: -0x%x", -ret);
+                  mbedtls_pk_free(pm->pkey);
+                  mbedtls_free(pm->pkey);
+                  pm->pkey = NULL;
+                  EVP_PKEY_free(*pkey);
+                  *pkey = NULL;
+                }
+            }
+        }
+      else if (pkcs12_oid2nid(&bag_oid) == NID_keyBag)
+        {
+          /* KeyBag: unencrypted PrivateKeyInfo */
+
+          if (pkey != NULL && *pkey == NULL)
+            {
+              k = EVP_PKEY_new();
+              if (k == NULL)
+                {
+                  return -1;
+                }
+
+              EVP_PKEY_METHOD_CALL(load, k, p, (int)len);
+              *pkey = k;
+            }
+        }
+
+skip_bag:
+      p = bag_end;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: parse_pk12
+ *
+ * Description:
+ *   Parse a single ContentInfo from AuthenticatedSafe.
+ *   Handles both plain data and encryptedData content types.
+ ****************************************************************************/
+
+static int parse_pk12(const unsigned char *data, size_t len,
+                      const unsigned char *pwd, size_t pwd_len,
+                      const unsigned char *bmp_pwd,
+                      size_t bmp_pwd_len,
+                      EVP_PKEY **pkey, X509 **cert,
+                      OPENSSL_STACK **ca)
+{
+  mbedtls_asn1_buf ci_oid;
+  mbedtls_asn1_buf ct_oid;
+  unsigned char *dec_data = NULL;
+  unsigned char *end = (unsigned char *)data + len;
+  unsigned char *p = (unsigned char *)data;
+  unsigned char *ci_end;
+  unsigned char *eci_end;
+  unsigned char *ed_end;
+  size_t dec_len = 0;
+  size_t oct_len;
+  size_t seq_len;
+  size_t eci_len;
+  size_t ci_len;
+  int version;
+  int ret;
+
+  /* ContentInfo ::= SEQUENCE { contentType, content [0] EXPLICIT } */
+
+  ret = mbedtls_asn1_get_tag(&p, end, &ci_len,
+                             MBEDTLS_ASN1_CONSTRUCTED |
+                             MBEDTLS_ASN1_SEQUENCE);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  ci_end = p + ci_len;
+
+  /* contentType OID */
+
+  ret = mbedtls_asn1_get_tag(&p, ci_end, &ci_oid.len, MBEDTLS_ASN1_OID);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  ci_oid.p = p;
+  p += ci_oid.len;
+
+  /* content [0] EXPLICIT */
+
+  ret = mbedtls_asn1_get_tag(&p, ci_end, &ci_len,
+                             MBEDTLS_ASN1_CONTEXT_SPECIFIC |
+                             MBEDTLS_ASN1_CONSTRUCTED | 0);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  if (pkcs12_oid2nid(&ci_oid) == NID_pkcs7_data)
+    {
+      /* data content: OCTET STRING containing SafeContents */
+
+      ret = mbedtls_asn1_get_tag(&p, ci_end, &oct_len,
+                                 MBEDTLS_ASN1_OCTET_STRING);
+      if (ret != 0)
+        {
+          return ret;
+        }
+
+      ret = parse_bags(p, oct_len, pwd, pwd_len,
+                       bmp_pwd, bmp_pwd_len,
+                       pkey, cert, ca);
+    }
+  else if (pkcs12_oid2nid(&ci_oid) == NID_pkcs7_encrypted)
+    {
+      /* encryptedData: decrypt then parse SafeContents */
+
+      ret = mbedtls_asn1_get_tag(&p, ci_end, &seq_len,
+                                 MBEDTLS_ASN1_CONSTRUCTED |
+                                 MBEDTLS_ASN1_SEQUENCE);
+      if (ret != 0)
+        {
+          return ret;
+        }
+
+      ed_end = p + seq_len;
+
+      /* version INTEGER */
+
+      ret = mbedtls_asn1_get_int(&p, ed_end, &version);
+      if (ret != 0)
+        {
+          return ret;
+        }
+
+      /* EncryptedContentInfo ::= SEQUENCE */
+
+      ret = mbedtls_asn1_get_tag(&p, ed_end, &eci_len,
+                                 MBEDTLS_ASN1_CONSTRUCTED |
+                                 MBEDTLS_ASN1_SEQUENCE);
+      if (ret != 0)
+        {
+          return ret;
+        }
+
+      eci_end = p + eci_len;
+
+      /* contentType OID (skip) */
+
+      ret = mbedtls_asn1_get_tag(&p, eci_end, &ct_oid.len,
+                                 MBEDTLS_ASN1_OID);
+      if (ret != 0)
+        {
+          return ret;
+        }
+
+      p += ct_oid.len;
+
+      /* contentEncryptionAlgorithm + [0] encryptedContent */
+
+      ret = pkcs12_pbe_crypt(p, (size_t)(eci_end - p),
+                             pwd, pwd_len,
+                             bmp_pwd, bmp_pwd_len,
+                             &dec_data, &dec_len);
+      if (ret != 0)
+        {
+          return ret;
+        }
+
+      ret = parse_bags(dec_data, dec_len, pwd, pwd_len,
+                       bmp_pwd, bmp_pwd_len,
+                       pkey, cert, ca);
+      mbedtls_free(dec_data);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: pkcs12_select_leaf_cert
+ *
+ * Description:
+ *   OpenSSL's PKCS12_parse designates the certificate matching the private
+ *   key as the leaf (*cert) and puts the rest into the CA stack, regardless
+ *   of the order the bags appear in the file.  parse_bags fills *cert with
+ *   the first certificate it encounters, which is wrong when a CA
+ *   certificate precedes the end-entity certificate in the PKCS#12 file.
+ *
+ *   This helper corrects that once both the key and all certificates have
+ *   been parsed: if *cert does not match the private key, scan the CA stack
+ *   for a certificate that does and swap it into *cert.  When there is no
+ *   private key the first-seen certificate is kept (same as OpenSSL).
+ ****************************************************************************/
+
+static void pkcs12_select_leaf_cert(EVP_PKEY *pkey, X509 **cert,
+                                    OPENSSL_STACK *ca)
+{
+  mbedtls_entropy_context entropy;
+  mbedtls_ctr_drbg_context ctr_drbg;
+  mbedtls_pk_context *prv;
+  struct x509_pm *cert_pm;
+  struct x509_pm *ca_pm;
+  struct pkey_pm *pm;
+  X509 *cur;
+  int i;
+
+  if (pkey == NULL || cert == NULL || *cert == NULL || ca == NULL)
+    {
+      return;
+    }
+
+  pm = (struct pkey_pm *)pkey->pkey_pm;
+  if (pm == NULL || pm->pkey == NULL)
+    {
+      return;
+    }
+
+  prv = pm->pkey;
+
+  mbedtls_entropy_init(&entropy);
+  mbedtls_ctr_drbg_init(&ctr_drbg);
+  if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
+                            &entropy, NULL, 0) != 0)
+    {
+      mbedtls_ctr_drbg_free(&ctr_drbg);
+      mbedtls_entropy_free(&entropy);
+      return;
+    }
+
+  /* If the current leaf already matches the private key, nothing to do. */
+
+  cert_pm = (struct x509_pm *)(*cert)->x509_pm;
+  if (cert_pm != NULL &&
+      mbedtls_pk_check_pair(&cert_pm->x509_crt.pk, prv,
+                            mbedtls_ctr_drbg_random, &ctr_drbg) == 0)
+    {
+      goto done;
+    }
+
+  /* Otherwise scan the CA stack for the certificate that matches. */
+
+  for (i = 0; i < ca->num; i++)
+    {
+      cur = (X509 *)ca->data[i];
+      if (cur == NULL)
+        {
+          continue;
+        }
+
+      ca_pm = (struct x509_pm *)cur->x509_pm;
+      if (ca_pm == NULL)
+        {
+          continue;
+        }
+
+      if (mbedtls_pk_check_pair(&ca_pm->x509_crt.pk, prv,
+                                mbedtls_ctr_drbg_random, &ctr_drbg) == 0)
+        {
+          /* Swap: matching CA entry becomes the leaf, old leaf joins CA. */
+
+          ca->data[i] = (char *)(*cert);
+          *cert = cur;
+          break;
+        }
+    }
+
+done:
+  mbedtls_ctr_drbg_free(&ctr_drbg);
+  mbedtls_entropy_free(&entropy);
+}
+
+/****************************************************************************
+ * Name: pkcs12_create_pbe_params
+ *
+ * Description:
+ *   Build DER-encoded pkcs-12PbeParams: SEQUENCE { salt, iterations }
+ *   Written backwards into buf. Returns total length written.
+ ****************************************************************************/
+
+static int pkcs12_create_pbe_params(unsigned char *buf, size_t buflen,
+                                    const unsigned char *salt,
+                                    size_t salt_len,
+                                    int iterations)
+{
+  unsigned char *p = buf + buflen;
+  const unsigned char *start = buf;
+  int len = 0;
+  int ret;
+
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&p, start, iterations));
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_octet_string(&p, start,
+                                                            salt, salt_len));
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, start,
+                        MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  memmove(buf, p, (size_t)len);
+  return len;
+}
+
+/****************************************************************************
+ * Name: pkcs12_pbe_crypt_enc
+ *
+ * Description:
+ *   Encrypt data using pbeWithSHA1And3-KeyTripleDES-CBC.
+ *   Generates random salt, builds PBE params, encrypts.
+ *   Returns encrypted data (caller frees). pbe_params_out receives the
+ *   DER-encoded AlgorithmIdentifier parameters (SEQUENCE {salt, iter}).
+ ****************************************************************************/
+
+static int pkcs12_pbe_crypt_enc(const unsigned char *data, size_t data_len,
+                                const unsigned char *pwd, size_t pwd_len,
+                                int iterations,
+                                unsigned char **enc_out, size_t *enc_out_len,
+                                unsigned char *pbe_params_buf,
+                                size_t *pbe_params_len)
+{
+  mbedtls_entropy_context *entropy;
+  mbedtls_ctr_drbg_context *ctr_drbg;
+  mbedtls_asn1_buf params;
+  unsigned char *pp = pbe_params_buf;
+  unsigned char salt[8];
+  size_t inner_len;
+  size_t out_len = 0;
+  size_t out_size;
+  int ret;
+
+  entropy = mbedtls_calloc(1, sizeof(*entropy));
+  ctr_drbg = mbedtls_calloc(1, sizeof(*ctr_drbg));
+  if (entropy == NULL || ctr_drbg == NULL)
+    {
+      mbedtls_free(entropy);
+      mbedtls_free(ctr_drbg);
+      return -1;
+    }
+
+  mbedtls_entropy_init(entropy);
+  mbedtls_ctr_drbg_init(ctr_drbg);
+  ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func,
+                              entropy, NULL, 0);
+  if (ret != 0)
+    {
+      mbedtls_ctr_drbg_free(ctr_drbg);
+      mbedtls_entropy_free(entropy);
+      mbedtls_free(ctr_drbg);
+      mbedtls_free(entropy);
+      return ret;
+    }
+
+  ret = mbedtls_ctr_drbg_random(ctr_drbg, salt, sizeof(salt));
+  mbedtls_ctr_drbg_free(ctr_drbg);
+  mbedtls_entropy_free(entropy);
+  mbedtls_free(ctr_drbg);
+  mbedtls_free(entropy);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  /* Build PBE params DER */
+
+  ret = pkcs12_create_pbe_params(pbe_params_buf, 64, salt,
+                                 sizeof(salt), iterations);
+  if (ret < 0)
+    {
+      goto cleanup;
+    }
+
+  *pbe_params_len = (size_t)ret;
+
+  /* Re-parse the SEQUENCE header to point params at the inner content */
+
+  ret = mbedtls_asn1_get_tag(&pp, pbe_params_buf + *pbe_params_len,
+                             &inner_len,
+                             MBEDTLS_ASN1_CONSTRUCTED |
+                             MBEDTLS_ASN1_SEQUENCE);
+  if (ret != 0)
+    {
+      goto cleanup;
+    }
+
+  params.tag = MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
+  params.p = pp;
+  params.len = inner_len;
+
+  /* Allocate output: input + up to one block of padding (8 for 3DES) */
+
+  out_size = data_len + 8;
+  *enc_out = mbedtls_calloc(1, out_size);
+  if (*enc_out == NULL)
+    {
+      ret = -1;
+      goto cleanup;
+    }
+
+  ret = mbedtls_pkcs12_pbe_ext(&params, MBEDTLS_PKCS12_PBE_ENCRYPT,
+                               MBEDTLS_CIPHER_DES_EDE3_CBC,
+                               MBEDTLS_MD_SHA1,
+                               pwd, pwd_len,
+                               data, data_len,
+                               *enc_out, out_size, &out_len);
+  if (ret != 0)
+    {
+      mbedtls_free(*enc_out);
+      *enc_out = NULL;
+      goto cleanup;
+    }
+
+  *enc_out_len = out_len;
+
+cleanup:
+  return ret;
+}
+
+/****************************************************************************
+ * Name: pkcs12_write_key_ci
+ *
+ * Description:
+ *   Write the "data" ContentInfo holding a pkcs8ShroudedKeyBag (DER,
+ *   backwards): ContentInfo { pkcs7-data, [0] OCTET STRING { SafeContents
+ *   { SafeBag { pkcs8ShroudedKeyBag, [0] EncryptedPrivateKeyInfo } } } }.
+ *   enc_key is the PBE-encrypted PKCS8 key, pbe_params the DER-encoded PBE
+ *   AlgorithmIdentifier parameters.  Returns bytes written or negative.
+ ****************************************************************************/
+
+static int pkcs12_write_key_ci(unsigned char **p, const unsigned char *start,
+                               const unsigned char *enc_key,
+                               size_t enc_key_len,
+                               const unsigned char *pbe_params,
+                               size_t pbe_params_len)
+{
+  int alg_len = 0;
+  int len = 0;
+  int ret;
+
+  /* encryptedData OCTET STRING */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_raw_buffer(p, start, enc_key, enc_key_len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, enc_key_len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
+
+  /* AlgorithmIdentifier SEQUENCE { OID, params } */
+
+  MBEDTLS_ASN1_CHK_ADD(alg_len,
+    mbedtls_asn1_write_raw_buffer(p, start, pbe_params, pbe_params_len));
+  MBEDTLS_ASN1_CHK_ADD(alg_len,
+    mbedtls_asn1_write_oid(p, start,
+      MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC,
+      sizeof(MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC) - 1));
+  MBEDTLS_ASN1_CHK_ADD(alg_len,
+    mbedtls_asn1_write_len(p, start, (size_t)alg_len));
+  MBEDTLS_ASN1_CHK_ADD(alg_len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  len += alg_len;
+
+  /* EncryptedPrivateKeyInfo SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  /* bagValue [0] EXPLICIT */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0));
+
+  /* bagId: pkcs8ShroudedKeyBag */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_oid(p, start,
+      MBEDTLS_OID_PKCS12 "\x0a\x01\x02",
+      sizeof(MBEDTLS_OID_PKCS12 "\x0a\x01\x02") - 1));
+
+  /* SafeBag SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  /* SafeContents SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  /* OCTET STRING wrapping SafeContents */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
+
+  /* content [0] EXPLICIT */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0));
+
+  /* contentType: pkcs7-data */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_oid(p, start,
+      MBEDTLS_OID_PKCS7_DATA, sizeof(MBEDTLS_OID_PKCS7_DATA) - 1));
+
+  /* ContentInfo SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  return len;
+}
+
+/****************************************************************************
+ * Name: pkcs12_write_certs_ci
+ *
+ * Description:
+ *   Write the "encryptedData" ContentInfo holding the encrypted cert
+ *   SafeContents (DER, backwards): ContentInfo { pkcs7-encryptedData, [0]
+ *   EncryptedData { version, EncryptedContentInfo { pkcs7-data, AlgId,
+ *   [0] encryptedContent } } }.  Returns bytes written or negative.
+ ****************************************************************************/
+
+static int pkcs12_write_certs_ci(unsigned char **p,
+                                 const unsigned char *start,
+                                 const unsigned char *enc_certs,
+                                 size_t enc_certs_len,
+                                 const unsigned char *pbe_params,
+                                 size_t pbe_params_len)
+{
+  int alg_len = 0;
+  int len = 0;
+  int ret;
+
+  /* encryptedContent [0] IMPLICIT OCTET STRING */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_raw_buffer(p, start, enc_certs, enc_certs_len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, enc_certs_len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0));
+
+  /* contentEncryptionAlgorithm AlgorithmIdentifier */
+
+  MBEDTLS_ASN1_CHK_ADD(alg_len,
+    mbedtls_asn1_write_raw_buffer(p, start, pbe_params, pbe_params_len));
+  MBEDTLS_ASN1_CHK_ADD(alg_len,
+    mbedtls_asn1_write_oid(p, start,
+      MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC,
+      sizeof(MBEDTLS_OID_PKCS12_PBE_SHA1_DES3_EDE_CBC) - 1));
+  MBEDTLS_ASN1_CHK_ADD(alg_len,
+    mbedtls_asn1_write_len(p, start, (size_t)alg_len));
+  MBEDTLS_ASN1_CHK_ADD(alg_len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  len += alg_len;
+
+  /* contentType: pkcs7-data */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_oid(p, start,
+      MBEDTLS_OID_PKCS7_DATA, sizeof(MBEDTLS_OID_PKCS7_DATA) - 1));
+
+  /* EncryptedContentInfo SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  /* version INTEGER 0 */
+
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
+
+  /* EncryptedData SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  /* content [0] EXPLICIT */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0));
+
+  /* contentType: pkcs7-encryptedData */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_oid(p, start,
+      MBEDTLS_OID_PKCS7_ENCRYPTED_DATA,
+      sizeof(MBEDTLS_OID_PKCS7_ENCRYPTED_DATA) - 1));
+
+  /* ContentInfo SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  return len;
+}
+
+/****************************************************************************
+ * Name: pkcs12_write_pkcs8
+ *
+ * Description:
+ *   Wrap a raw private key DER in a PKCS8 PrivateKeyInfo (DER, backwards):
+ *   SEQUENCE { version INTEGER(0), algorithm AlgId,
+ *               privateKey OCTET STRING }
+ *   Writes into the caller-provided buffer; returns bytes written or a
+ *   negative error code.
+ ****************************************************************************/
+
+static int pkcs12_write_pkcs8(unsigned char **p, const unsigned char *start,
+                              const unsigned char *raw_key, size_t raw_len)
+{
+  int len = 0;
+  int ret;
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_octet_string(p, start, raw_key, raw_len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_algorithm_identifier(p, start,
+      MBEDTLS_OID_PKCS1_RSA, sizeof(MBEDTLS_OID_PKCS1_RSA) - 1, 0));
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 0));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  return len;
+}
+
+/****************************************************************************
+ * Name: pkcs12_write_cert_bag
+ *
+ * Description:
+ *   Write a single certBag SafeBag (DER, backwards) into the buffer:
+ *   SafeBag { bagId=certBag, bagValue [0] { CertBag { certType, [0] cert }}}
+ *   Returns the number of bytes written, or a negative error code.
+ ****************************************************************************/
+
+static int pkcs12_write_cert_bag(unsigned char **p,
+                                 const unsigned char *start,
+                                 const mbedtls_x509_crt *crt)
+{
+  int len = 0;
+  int ret;
+
+  /* certValue [0] EXPLICIT OCTET STRING containing DER cert */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_raw_buffer(p, start, crt->raw.p, crt->raw.len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, crt->raw.len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0));
+
+  /* certType OID: x509Certificate */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_oid(p, start,
+      MBEDTLS_OID_PKCS9 "\x16\x01",
+      sizeof(MBEDTLS_OID_PKCS9 "\x16\x01") - 1));
+
+  /* CertBag SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  /* bagValue [0] EXPLICIT */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0));
+
+  /* bagId OID: certBag */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_oid(p, start,
+      MBEDTLS_OID_PKCS12 "\x0a\x01\x03",
+      sizeof(MBEDTLS_OID_PKCS12 "\x0a\x01\x03") - 1));
+
+  /* SafeBag SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len,
+    mbedtls_asn1_write_tag(p, start,
+      MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  return len;
+}
+
+/****************************************************************************
+ * Name: pkcs12_write_seq_hdr
+ *
+ * Description:
+ *   Prepend a SEQUENCE tag+length header over already-written content
+ *   (reverse DER).  Returns the number of header bytes written.
+ ****************************************************************************/
+
+static int pkcs12_write_seq_hdr(unsigned char **p,
+                                const unsigned char *start,
+                                size_t content_len)
+{
+  int len = 0;
+  int ret;
+
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, content_len));
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
+                        MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  return len;
+}
+
+/****************************************************************************
+ * Name: pkcs12_write_authsafe
+ *
+ * Description:
+ *   Write the authSafe portion of a PFX (reverse DER): the two ContentInfos
+ *   (shrouded key bag + encrypted certs), the AuthenticatedSafe SEQUENCE,
+ *   and the enclosing pkcs7-data ContentInfo plus version INTEGER(3).
+ *   Reports the AuthenticatedSafe content span so the caller can MAC it.
+ *   Returns the total number of bytes written.
+ ****************************************************************************/
+
+static int pkcs12_write_authsafe(unsigned char **p,
+                                 const unsigned char *start,
+                                 const unsigned char *enc_key,
+                                 size_t enc_key_len,
+                                 const unsigned char *pbe_params_key,
+                                 size_t pbe_params_key_len,
+                                 const unsigned char *enc_certs,
+                                 size_t enc_certs_len,
+                                 const unsigned char *pbe_params_cert,
+                                 size_t pbe_params_cert_len,
+                                 unsigned char **authsafe_content,
+                                 size_t *authsafe_content_len)
+{
+  int len = 0;
+  int ret;
+
+  /* ContentInfo for key (data type, contains shrouded key bag) */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    pkcs12_write_key_ci(p, start, enc_key, enc_key_len,
+                        pbe_params_key, pbe_params_key_len));
+
+  /* ContentInfo for certs (encryptedData type) */
+
+  MBEDTLS_ASN1_CHK_ADD(len,
+    pkcs12_write_certs_ci(p, start, enc_certs, enc_certs_len,
+                          pbe_params_cert, pbe_params_cert_len));
+
+  /* AuthenticatedSafe SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
+                        MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  /* Save authSafe content span for MAC computation */
+
+  *authsafe_content = *p;
+  *authsafe_content_len = (size_t)len;
+
+  /* Wrap in OCTET STRING */
+
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
+                            MBEDTLS_ASN1_OCTET_STRING));
+
+  /* content [0] EXPLICIT */
+
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
+                            MBEDTLS_ASN1_CONTEXT_SPECIFIC |
+                            MBEDTLS_ASN1_CONSTRUCTED | 0));
+
+  /* contentType: pkcs7-data */
+
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start,
+                            MBEDTLS_OID_PKCS7_DATA,
+                            sizeof(MBEDTLS_OID_PKCS7_DATA) - 1));
+
+  /* authSafe ContentInfo SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
+                        MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  /* version INTEGER 3 */
+
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, 3));
+
+  return len;
+}
+
+/****************************************************************************
+ * Name: pkcs12_write_macdata
+ *
+ * Description:
+ *   Write a MacData structure (reverse DER): SEQUENCE { DigestInfo,
+ *   macSalt OCTET STRING, iterations INTEGER (omitted when 1) }.
+ *   Returns the number of bytes written.
+ ****************************************************************************/
+
+static int pkcs12_write_macdata(unsigned char **p,
+                                const unsigned char *start,
+                                const unsigned char *mac_digest,
+                                size_t mac_digest_len,
+                                const unsigned char *mac_salt,
+                                size_t mac_salt_len,
+                                int mac_iter)
+{
+  int di_len = 0;
+  int len = 0;
+  int ret;
+
+  if (mac_iter != 1)
+    {
+      MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(p, start, mac_iter));
+    }
+
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_octet_string(p, start,
+                            mac_salt, mac_salt_len));
+
+  /* DigestInfo SEQUENCE { AlgorithmIdentifier(SHA1), digest
+   * OCTET STRING
+   */
+
+  MBEDTLS_ASN1_CHK_ADD(di_len, mbedtls_asn1_write_octet_string(p, start,
+                               mac_digest, mac_digest_len));
+  MBEDTLS_ASN1_CHK_ADD(di_len,
+                       mbedtls_asn1_write_algorithm_identifier(p, start,
+                               MBEDTLS_OID_DIGEST_ALG_SHA1,
+                               sizeof(MBEDTLS_OID_DIGEST_ALG_SHA1) - 1, 0));
+  MBEDTLS_ASN1_CHK_ADD(di_len, mbedtls_asn1_write_len(p, start,
+                               (size_t)di_len));
+  MBEDTLS_ASN1_CHK_ADD(di_len, mbedtls_asn1_write_tag(p, start,
+                               MBEDTLS_ASN1_CONSTRUCTED |
+                               MBEDTLS_ASN1_SEQUENCE));
+
+  len += di_len;
+
+  /* MacData SEQUENCE */
+
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, (size_t)len));
+  MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
+                        MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
+
+  return len;
+}
+
+/****************************************************************************
+ * Name: pkcs12_compute_mac
+ *
+ * Description:
+ *   Generate a random MAC salt, derive the PKCS#12 MAC key from the BMP
+ *   password, and compute HMAC-SHA1 over the AuthenticatedSafe content.
+ *   Fills mac_salt and mac_digest.  Returns 0 on success.
+ ****************************************************************************/
+
+static int pkcs12_compute_mac(const unsigned char *authsafe,
+                              size_t authsafe_len,
+                              const unsigned char *bmp_pwd,
+                              size_t bmp_pwd_len,
+                              int mac_iter,
+                              unsigned char *mac_salt, size_t mac_salt_len,
+                              unsigned char *mac_digest)
+{
+  mbedtls_entropy_context *entropy;
+  mbedtls_ctr_drbg_context *ctr_drbg;
+  unsigned char mac_key[20];
+  int ret;
+
+  entropy = mbedtls_calloc(1, sizeof(*entropy));
+  ctr_drbg = mbedtls_calloc(1, sizeof(*ctr_drbg));
+  if (entropy == NULL || ctr_drbg == NULL)
+    {
+      mbedtls_free(entropy);
+      mbedtls_free(ctr_drbg);
+      return -1;
+    }
+
+  mbedtls_entropy_init(entropy);
+  mbedtls_ctr_drbg_init(ctr_drbg);
+
+  ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func,
+                              entropy, NULL, 0);
+  if (ret == 0)
+    {
+      ret = mbedtls_ctr_drbg_random(ctr_drbg, mac_salt, mac_salt_len);
+    }
+
+  mbedtls_ctr_drbg_free(ctr_drbg);
+  mbedtls_entropy_free(entropy);
+  mbedtls_free(ctr_drbg);
+  mbedtls_free(entropy);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  /* Derive MAC key */
+
+  ret = mbedtls_pkcs12_derivation(mac_key, sizeof(mac_key),
+                                  bmp_pwd, bmp_pwd_len,
+                                  mac_salt, mac_salt_len,
+                                  MBEDTLS_MD_SHA1,
+                                  MBEDTLS_PKCS12_DERIVE_MAC_KEY,
+                                  mac_iter);
+  if (ret != 0)
+    {
+      return ret;
+    }
+
+  /* Compute HMAC-SHA1 over authSafe content */
+
+  ret = mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
+                        mac_key, sizeof(mac_key),
+                        authsafe, authsafe_len,
+                        mac_digest);
+  mbedtls_platform_zeroize(mac_key, sizeof(mac_key));
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+PKCS12 *d2i_PKCS12(PKCS12 **a, const unsigned char **in, long len)
+{
+  PKCS12 *p12;
+
+  SSL_ASSERT2(in);
+  SSL_ASSERT2(*in);
+  SSL_ASSERT2(len > 0);
+
+  p12 = mbedtls_calloc(1, sizeof(PKCS12));
+  if (!p12)
+    {
+      SSL_DEBUG(SSL_PKCS12_ERROR_LEVEL, "no enough memory > (p12)");
+      return NULL;
+    }
+
+  p12->data = mbedtls_calloc(1, (size_t)len);
+  if (!p12->data)
+    {
+      SSL_DEBUG(SSL_PKCS12_ERROR_LEVEL, "no enough memory > (p12->data)");
+      mbedtls_free(p12);
+      return NULL;
+    }
+
+  memcpy(p12->data, *in, (size_t)len);
+  p12->len = (size_t)len;
+
+  *in += len;
+
+  if (a != NULL)
+    {
+      *a = p12;
+    }
+
+  return p12;
+}
+
+PKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **a)
+{
+  const unsigned char *p;
+  unsigned char *buf = NULL;
+  PKCS12 *p12;
+  long len;
+
+  SSL_ASSERT2(fp);
+
+  fseek(fp, 0, SEEK_END);
+  len = ftell(fp);
+  fseek(fp, 0, SEEK_SET);
+
+  if (len <= 0)
+    {
+      return NULL;
+    }
+
+  buf = mbedtls_calloc(1, (size_t)len);
+  if (!buf)
+    {
+      return NULL;
+    }
+
+  if (fread(buf, 1, (size_t)len, fp) != (size_t)len)
+    {
+      mbedtls_free(buf);
+      return NULL;
+    }
+
+  p = buf;
+  p12 = d2i_PKCS12(a, &p, len);
+  mbedtls_free(buf);
+  return p12;
+}
+
+int PKCS12_parse(PKCS12 *p12, const char *pass,
+                 EVP_PKEY **pkey, X509 **cert,
+                 STACK_OF(X509) **ca)
+{
+  unsigned char bmp_pwd[PKCS12_MAX_PWDLEN * 2 + 2];
+  mbedtls_asn1_buf ci_oid;
+  const unsigned char *pwd = NULL;
+  unsigned char *authsafe_content;
+  unsigned char *elem_start;
+  unsigned char *ci_end;
+  unsigned char *as_end;
+  unsigned char *end;
+  unsigned char *p;
+  size_t authsafe_content_len;
+  size_t bmp_pwd_len = 0;
+  size_t pwd_len = 0;
+  size_t elem_len;
+  size_t as_len;
+  size_t ci_len;
+  size_t len;
+  int version;
+  int ret;
+  int i;
+
+  if (p12 == NULL || p12->data == NULL)
+    {
+      return 0;
+    }
+
+  if (pkey)
+    {
+      *pkey = NULL;
+    }
+
+  if (cert)
+    {
+      *cert = NULL;
+    }
+
+  if (ca)
+    {
+      *ca = NULL;
+    }
+
+  /* Prepare password in both raw and BMPString forms */
+
+  if (pass != NULL && pass[0] != '\0')
+    {
+      pwd_len = strlen(pass);
+      pwd = (const unsigned char *)pass;
+
+      if (pkcs12_asc2uni(pass, pwd_len,
+                         bmp_pwd, &bmp_pwd_len) != 0)
+        {
+          SSL_DEBUG(SSL_PKCS12_ERROR_LEVEL, "password too long");
+          return 0;
+        }
+    }
+  else
+    {
+      /* Empty password: BMPString is just null terminator (2 bytes) */
+
+      bmp_pwd[0] = 0x00;
+      bmp_pwd[1] = 0x00;
+      bmp_pwd_len = 2;
+    }
+
+  p = p12->data;
+  end = p + p12->len;
+
+  /* PFX ::= SEQUENCE { version, authSafe, macData } */
+
+  ret = mbedtls_asn1_get_tag(&p, end, &len,
+                             MBEDTLS_ASN1_CONSTRUCTED |
+                             MBEDTLS_ASN1_SEQUENCE);
+  if (ret != 0)
+    {
+      SSL_DEBUG(SSL_PKCS12_ERROR_LEVEL, "PFX SEQUENCE parse failed");
+      return 0;
+    }
+
+  end = p + len;
+
+  /* version INTEGER (must be 3) */
+
+  ret = mbedtls_asn1_get_int(&p, end, &version);
+  if (ret != 0 || version != 3)
+    {
+      SSL_DEBUG(SSL_PKCS12_ERROR_LEVEL, "PFX version != 3");
+      return 0;
+    }
+
+  /* authSafe: ContentInfo */
+
+  ret = mbedtls_asn1_get_tag(&p, end, &ci_len,
+                             MBEDTLS_ASN1_CONSTRUCTED |
+                             MBEDTLS_ASN1_SEQUENCE);
+  if (ret != 0)
+    {
+      return 0;
+    }
+
+  ci_end = p + ci_len;
+
+  /* contentType OID */
+
+  ret = mbedtls_asn1_get_tag(&p, ci_end, &ci_oid.len, MBEDTLS_ASN1_OID);
+  if (ret != 0)
+    {
+      return 0;
+    }
+
+  ci_oid.p = p;
+  p += ci_oid.len;
+
+  /* content [0] EXPLICIT -> OCTET STRING -> AuthenticatedSafe */
+
+  ret = mbedtls_asn1_get_tag(&p, ci_end, &len,
+                             MBEDTLS_ASN1_CONTEXT_SPECIFIC |
+                             MBEDTLS_ASN1_CONSTRUCTED | 0);
+  if (ret != 0)
+    {
+      return 0;
+    }
+
+  ret = mbedtls_asn1_get_tag(&p, ci_end, &len,
+                             MBEDTLS_ASN1_OCTET_STRING);
+  if (ret != 0)
+    {
+      return 0;
+    }
+
+  /* Save authSafe content range for MAC verification */
+
+  authsafe_content = p;
+  authsafe_content_len = len;
+
+  /* AuthenticatedSafe ::= SEQUENCE OF ContentInfo */
+
+  ret = mbedtls_asn1_get_tag(&p, p + len, &as_len,
+                             MBEDTLS_ASN1_CONSTRUCTED |
+                             MBEDTLS_ASN1_SEQUENCE);
+  if (ret != 0)
+    {
+      return 0;
+    }
+
+  as_end = p + as_len;
+
+  while (p < as_end)
+    {
+      elem_start = p;
+
+      ret = mbedtls_asn1_get_tag(&p, as_end, &elem_len,
+                                 MBEDTLS_ASN1_CONSTRUCTED |
+                                 MBEDTLS_ASN1_SEQUENCE);
+      if (ret != 0)
+        {
+          return 0;
+        }
+
+      ret = parse_pk12(elem_start,
+                       (size_t)(p + elem_len - elem_start),
+                       pwd, pwd_len,
+                       bmp_pwd, bmp_pwd_len,
+                       pkey, cert, ca);
+      if (ret != 0)
+        {
+          SSL_DEBUG(SSL_PKCS12_ERROR_LEVEL,
+                    "ContentInfo parse error: -0x%x", -ret);
+        }
+
+      p += elem_len;
+    }
+
+  /* Verify MAC if macData is present */
+
+  p = ci_end;
+  if (p < end)
+    {
+      ret = pkcs12_verify_mac(p, end,
+                              authsafe_content, authsafe_content_len,
+                              bmp_pwd, bmp_pwd_len);
+      if (ret != 0)
+        {
+          SSL_DEBUG(SSL_PKCS12_ERROR_LEVEL, "MAC verification failed");
+
+          /* Clean up on MAC failure */
+
+          if (pkey && *pkey)
+            {
+              EVP_PKEY_free(*pkey);
+              *pkey = NULL;
+            }
+
+          if (cert && *cert)
+            {
+              X509_free(*cert);
+              *cert = NULL;
+            }
+
+          if (ca && *ca)
+            {
+              for (i = 0; i < (*ca)->num; i++)
+                {
+                  X509_free((X509 *)(*ca)->data[i]);
+                }
+
+              OPENSSL_sk_free(*ca);
+              *ca = NULL;
+            }
+
+          return 0;
+        }
+    }
+
+  /* Align with OpenSSL: the certificate matching the private key is the
+   * leaf; correct *cert if a CA-stack entry is the real match.
+   */
+
+  if (pkey != NULL && cert != NULL && ca != NULL)
+    {
+      pkcs12_select_leaf_cert(*pkey, cert, *ca);
+    }
+
+  mbedtls_platform_zeroize(bmp_pwd, sizeof(bmp_pwd));
+  return 1;
+}
+
+int i2d_PKCS12(const PKCS12 *p12, unsigned char **out)
+{
+  if (p12 == NULL || p12->data == NULL)
+    {
+      return 0;
+    }
+
+  if (out != NULL)
+    {
+      if (*out == NULL)
+        {
+          *out = mbedtls_calloc(1, p12->len);
+          if (*out == NULL)
+            {
+              return -1;
+            }
+
+          memcpy(*out, p12->data, p12->len);
+        }
+      else
+        {
+          memcpy(*out, p12->data, p12->len);
+          *out += p12->len;
+        }
+    }
+
+  return (int)p12->len;
+}
+
+void PKCS12_free(PKCS12 *p12)
+{
+  SSL_ASSERT3(p12);
+
+  if (p12->data)
+    {
+      mbedtls_platform_zeroize(p12->data, p12->len);
+      mbedtls_free(p12->data);
+    }
+
+  mbedtls_free(p12);
+}
+
+PKCS12 *PKCS12_create(const char *pass, const char *name,
+                      EVP_PKEY *pkey, X509 *cert,
+                      STACK_OF(X509) *ca,
+                      int nid_key, int nid_cert,
+                      int iter, int mac_iter, int keytype)
+{
+  struct x509_pm *cert_pm;
+  struct x509_pm *ca_pm;
+  struct pkey_pm *pm;
+  unsigned char bmp_pwd[PKCS12_MAX_PWDLEN * 2 + 2];
+  unsigned char pbe_params_key[64];
+  unsigned char pbe_params_cert[64];
+  unsigned char mac_digest[20];
+  unsigned char mac_salt[8];
+  unsigned char mac_der[128];
+  unsigned char seq_hdr[8];
+  const unsigned char *start;
+  const unsigned char *ms = mac_der;
+  const unsigned char *ss = seq_hdr;
+  const char *pwd = pass ? pass : "";
+  unsigned char *authsafe_content;
+  unsigned char *cert_bags = NULL;
+  unsigned char *enc_certs = NULL;
+  unsigned char *enc_key = NULL;
+  unsigned char *final_buf;
+  unsigned char *key_der = NULL;
+  unsigned char *pkcs8_buf;
+  unsigned char *buf = NULL;
+  unsigned char *mp = mac_der + sizeof(mac_der);
+  unsigned char *sp = seq_hdr + sizeof(seq_hdr);
+  unsigned char *pk8p;
+  unsigned char *cp;
+  unsigned char *p;
+  const unsigned char *cs;
+  X509 *ca_cert;
+  PKCS12 *p12;
+  size_t authsafe_content_len;
+  size_t cert_bags_size = 4096;
+  size_t pbe_params_cert_len = 0;
+  size_t pbe_params_key_len = 0;
+  size_t key_der_size = 2048;
+  size_t enc_certs_len = 0;
+  size_t enc_key_len = 0;
+  size_t bmp_pwd_len = 0;
+  size_t pwd_len = strlen(pwd);
+  size_t pkcs8_size;
+  size_t buflen = 8192;
+  size_t cur_len;
+  size_t total;
+  int key_der_len;
+  int hdr_len = 0;
+  int mac_len = 0;
+  int cont_len = 0;
+  int ret;
+  int i;
+
+  /* Iteration defaults */
+
+  if (iter <= 0)
+    {
+      iter = 2048;
+    }
+
+  if (mac_iter <= 0)
+    {
+      mac_iter = 1;
+    }
+
+  if (pkey == NULL || cert == NULL)
+    {
+      return NULL;
+    }
+
+  /* Convert password to BMP */
+
+  ret = pkcs12_asc2uni(pwd, pwd_len, bmp_pwd, &bmp_pwd_len);
+  if (ret != 0)
+    {
+      return NULL;
+    }
+
+  /* Serialize the private key to PKCS8 DER */
+
+  pm = (struct pkey_pm *)pkey->pkey_pm;
+  if (pm == NULL || pm->pkey == NULL)
+    {
+      return NULL;
+    }
+
+  key_der = mbedtls_calloc(1, key_der_size);
+  if (key_der == NULL)
+    {
+      return NULL;
+    }
+
+  key_der_len = mbedtls_pk_write_key_der(pm->pkey, key_der, key_der_size);
+  if (key_der_len < 0)
+    {
+      ret = key_der_len;
+      goto fail;
+    }
+
+  /* Wrap raw key in PKCS8 PrivateKeyInfo:
+   * SEQUENCE { version INTEGER(0), algorithm AlgId,
+   *            privateKey OCTET STRING }
+   */
+
+  pkcs8_size = (size_t)key_der_len + 64;
+  pkcs8_buf = mbedtls_calloc(1, pkcs8_size);
+  if (pkcs8_buf == NULL)
+    {
+      ret = -1;
+      goto fail;
+    }
+
+  pk8p = pkcs8_buf + pkcs8_size;
+  ret = pkcs12_write_pkcs8(&pk8p, pkcs8_buf,
+                           key_der + key_der_size - key_der_len,
+                           (size_t)key_der_len);
+  if (ret < 0)
+    {
+      mbedtls_platform_zeroize(key_der, key_der_size);
+      mbedtls_free(pkcs8_buf);
+      goto fail;
+    }
+
+  mbedtls_platform_zeroize(key_der, key_der_size);
+  mbedtls_free(key_der);
+  key_der = pkcs8_buf;
+  key_der_size = pkcs8_size;
+  key_der_len = ret;
+
+  /* Encrypt the private key */
+
+  ret = pkcs12_pbe_crypt_enc(key_der + key_der_size - key_der_len,
+                             (size_t)key_der_len,
+                             (const unsigned char *)pwd, pwd_len, iter,
+                             &enc_key, &enc_key_len,
+                             pbe_params_key, &pbe_params_key_len);
+  mbedtls_platform_zeroize(key_der, key_der_size);
+  if (ret != 0)
+    {
+      goto fail;
+    }
+
+  /* Build cert SafeContents DER (unencrypted), then encrypt it */
+
+  cert_pm = (struct x509_pm *)cert->x509_pm;
+  if (cert_pm == NULL)
+    {
+      ret = -1;
+      goto fail;
+    }
+
+  cert_bags = mbedtls_calloc(1, cert_bags_size);
+  if (cert_bags == NULL)
+    {
+      ret = -1;
+      goto fail;
+    }
+
+  cp = cert_bags + cert_bags_size;
+  cs = cert_bags;
+
+  /* Write cert bags backwards: CA certs first (they end up last) */
+
+  if (ca != NULL)
+    {
+      for (i = ca->num - 1; i >= 0; i--)
+        {
+          ca_cert = (X509 *)ca->data[i];
+          if (ca_cert == NULL)
+            {
+              continue;
+            }
+
+          ca_pm = (struct x509_pm *)ca_cert->x509_pm;
+          if (ca_pm == NULL)
+            {
+              continue;
+            }
+
+          ret = pkcs12_write_cert_bag(&cp, cs, &ca_pm->x509_crt);
+          if (ret < 0)
+            {
+              goto fail;
+            }
+
+          cont_len += ret;
+        }
+    }
+
+  /* Write the main cert (end entity) */
+
+  ret = pkcs12_write_cert_bag(&cp, cs, &cert_pm->x509_crt);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  cont_len += ret;
+
+  /* Wrap in SafeContents SEQUENCE */
+
+  ret = pkcs12_write_seq_hdr(&cp, cs, (size_t)cont_len);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  cont_len += ret;
+
+  /* Encrypt the cert SafeContents */
+
+  ret = pkcs12_pbe_crypt_enc(cp, (size_t)cont_len,
+                             (const unsigned char *)pwd, pwd_len, iter,
+                             &enc_certs, &enc_certs_len,
+                             pbe_params_cert, &pbe_params_cert_len);
+  mbedtls_free(cert_bags);
+  cert_bags = NULL;
+  if (ret != 0)
+    {
+      goto fail;
+    }
+
+  /* Now build the final PFX DER (backwards) */
+
+  buf = mbedtls_calloc(1, buflen);
+  if (buf == NULL)
+    {
+      goto fail;
+    }
+
+  p = buf + buflen;
+  start = buf;
+
+  /* We build: PFX ::= SEQUENCE { version, authSafe, macData }.
+   * Build authSafe first (backwards), then prepend MacData at the end.
+   * AuthenticatedSafe ::= SEQUENCE OF ContentInfo
+   *   ContentInfo 1: encryptedData (certs)
+   *   ContentInfo 2: data (shrouded key bag)
+   */
+
+  ret = pkcs12_write_authsafe(&p, start,
+                              enc_key, enc_key_len,
+                              pbe_params_key, pbe_params_key_len,
+                              enc_certs, enc_certs_len,
+                              pbe_params_cert, pbe_params_cert_len,
+                              &authsafe_content, &authsafe_content_len);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  /* Compute the MAC (random salt + HMAC-SHA1) over the saved
+   * AuthenticatedSafe content.  Since ASN.1 is written backwards, the
+   * MacData is built into a separate buffer and concatenated afterwards.
+   */
+
+  ret = pkcs12_compute_mac(authsafe_content, authsafe_content_len,
+                           bmp_pwd, bmp_pwd_len, mac_iter,
+                           mac_salt, sizeof(mac_salt),
+                           mac_digest);
+  if (ret != 0)
+    {
+      goto fail;
+    }
+
+  /* Build MacData DER backwards:
+   * MacData ::= SEQUENCE {
+   *   mac DigestInfo ::= SEQUENCE { algorithm, digest },
+   *   macSalt OCTET STRING,
+   *   iterations INTEGER
+   * }
+   */
+
+  ret = pkcs12_write_macdata(&mp, ms, mac_digest, 20,
+                             mac_salt, sizeof(mac_salt), mac_iter);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  mac_len = ret;
+
+  /* Combine: PFX SEQUENCE = current content (version + authSafe) + MacData.
+   * Current content is at p..buf+buflen; MacData at mp..mac_der+sizeof.
+   */
+
+  cur_len = (size_t)(buf + buflen - p);
+  final_buf = mbedtls_calloc(1, cur_len + (size_t)mac_len + 16);
+  if (final_buf == NULL)
+    {
+      goto fail;
+    }
+
+  /* Content: version + authSafe + macData */
+
+  memcpy(final_buf, p, cur_len);
+  memcpy(final_buf + cur_len, mp, (size_t)mac_len);
+
+  /* Wrap in PFX outer SEQUENCE */
+
+  total = cur_len + (size_t)mac_len;
+
+  ret = pkcs12_write_seq_hdr(&sp, ss, total);
+  if (ret < 0)
+    {
+      goto fail;
+    }
+
+  hdr_len = ret;
+
+  /* Build final PKCS12 */
+
+  p12 = mbedtls_calloc(1, sizeof(PKCS12));
+  if (p12 == NULL)
+    {
+      mbedtls_free(final_buf);
+      goto fail;
+    }
+
+  p12->len = (size_t)hdr_len + total;
+  p12->data = mbedtls_calloc(1, p12->len);
+  if (p12->data == NULL)
+    {
+      mbedtls_free(p12);
+      mbedtls_free(final_buf);
+      goto fail;
+    }
+
+  memcpy(p12->data, sp, (size_t)hdr_len);
+  memcpy(p12->data + hdr_len, final_buf, total);
+  mbedtls_free(final_buf);
+  mbedtls_free(buf);
+  mbedtls_free(key_der);
+  mbedtls_free(enc_key);
+  mbedtls_free(enc_certs);
+  mbedtls_platform_zeroize(bmp_pwd, sizeof(bmp_pwd));
+  return p12;
+
+fail:
+  mbedtls_free(buf);
+  mbedtls_free(key_der);
+  mbedtls_free(cert_bags);
+  mbedtls_free(enc_key);
+  mbedtls_free(enc_certs);
+  mbedtls_platform_zeroize(bmp_pwd, sizeof(bmp_pwd));
+  return NULL;
+}
+
+int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass)
+{
+  STACK_OF(X509) *ca = NULL;
+  EVP_PKEY *pkey = NULL;
+  X509 *cert = NULL;
+  PKCS12 *new_p12;
+  int ret;
+  int i;
+
+  if (p12 == NULL)
+    {
+      return 0;
+    }
+
+  ret = PKCS12_parse(p12, oldpass, &pkey, &cert, &ca);
+  if (ret != 1)
+    {
+      return 0;
+    }
+
+  new_p12 = PKCS12_create(newpass, NULL, pkey, cert, ca,
+                          0, 0, 0, 0, 0);
+
+  EVP_PKEY_free(pkey);
+  X509_free(cert);
+  if (ca)
+    {
+      for (i = 0; i < ca->num; i++)
+        {
+          X509_free((X509 *)ca->data[i]);
+        }
+
+      OPENSSL_sk_free(ca);
+    }
+
+  if (new_p12 == NULL)
+    {
+      return 0;
+    }
+
+  /* Swap the DER data */
+
+  if (p12->data)
+    {
+      mbedtls_platform_zeroize(p12->data, p12->len);
+      mbedtls_free(p12->data);
+    }
+
+  p12->data = new_p12->data;
+  p12->len = new_p12->len;
+  new_p12->data = NULL;
+  mbedtls_free(new_p12);
+
+  return 1;
+}
+
+int i2d_PKCS12_bio(BIO *bp, const PKCS12 *p12)
+{
+  unsigned char *b = NULL;
+  int ret = 1;
+  int j = 0;
+  int n;
+  int i;
+
+  if (bp == NULL || p12 == NULL)
+    {
+      return 0;
+    }
+
+  n = i2d_PKCS12(p12, &b);
+  if (n < 0 || b == NULL)
+    {
+      return 0;
+    }
+
+  while (1)
+    {
+      i = BIO_write(bp, &b[j], n);
+      if (i == n)
+        {
+          break;
+        }
+
+      if (i <= 0)
+        {
+          ret = 0;
+          break;
+        }
+
+      j += i;
+      n -= i;
+    }
+
+  mbedtls_free(b);
+  return ret;
+}
+
+PKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12)
+{
+  const unsigned char *p;
+  unsigned char *buf = NULL;
+  unsigned char *tmp;
+  size_t total = 0;
+  size_t cap = 4096;
+  PKCS12 *ret;
+  int n;
+
+  SSL_ASSERT2(bp);
+
+  buf = mbedtls_calloc(1, cap);
+  if (buf == NULL)
+    {
+      return NULL;
+    }
+
+  while ((n = BIO_read(bp, buf + total, (int)(cap - total))) > 0)
+    {
+      total += (size_t)n;
+      if (total == cap)
+        {
+          if (cap > SIZE_MAX / 2)
+            {
+              mbedtls_free(buf);
+              return NULL;
+            }
+
+          cap *= 2;
+          tmp = mbedtls_calloc(1, cap);
+          if (tmp == NULL)
+            {
+              mbedtls_free(buf);
+              return NULL;
+            }
+
+          memcpy(tmp, buf, total);
+          mbedtls_free(buf);
+          buf = tmp;
+        }
+    }
+
+  if (total == 0)
+    {
+      mbedtls_free(buf);
+      return NULL;
+    }
+
+  p = buf;
+  ret = d2i_PKCS12(p12, &p, (long)total);
+  mbedtls_free(buf);
+  return ret;
+}
+
+#endif /* MBEDTLS_PKCS12_C */
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_lib.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_lib.c
index 94e20e2e2..841f457eb 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_lib.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_lib.c
@@ -1327,9 +1327,9 @@ int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
   return 0;
 }
 
-long SSL_CTX_set_mode(SSL_CTX *ctx, long larg)
+long SSL_CTX_set_mode(SSL_CTX *ctx, long mode)
 {
-  return ctx->mode |= larg;
+  return ctx->mode |= mode;
 }
 
 void SSL_CTX_set_info_callback(SSL_CTX *ctx,
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c
index 9d7f5b5cb..04599d2bd 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.c
@@ -495,7 +495,7 @@ int ssl_pm_send(SSL *ssl, const void *buffer, int len)
    * If this function returns something other than a positive value or
    * MBEDTLS_ERR_SSL_WANT_READ/WRITE, the ssl context becomes unusable, and
    * you should either free it or call mbedtls_ssl_session_reset() on it
-   * before re-using it for a new connection; the current connection must
+   * before reusing it for a new connection; the current connection must
    * be closed.
    *
    * When this function returns MBEDTLS_ERR_SSL_WANT_WRITE/READ, it must be
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.h 
b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.h
index 09833760d..60e5bdd06 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.h
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/ssl_pm.h
@@ -42,7 +42,7 @@ extern "C"
 
 /* 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.
+ * (evp.c, pkcs12.c) reference the same layout instead of redeclaring it.
  */
 
 struct pkey_pm
@@ -53,7 +53,7 @@ struct pkey_pm
 
 /* 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.
+ * (x509.c, pkcs12.c) reference the same layout instead of redeclaring it.
  */
 
 struct x509_pm
diff --git a/crypto/openssl_mbedtls_wrapper/mbedtls/x509.c 
b/crypto/openssl_mbedtls_wrapper/mbedtls/x509.c
index 54e5c7d73..c9752f759 100644
--- a/crypto/openssl_mbedtls_wrapper/mbedtls/x509.c
+++ b/crypto/openssl_mbedtls_wrapper/mbedtls/x509.c
@@ -301,3 +301,31 @@ failed:
 no_mem:
   return NULL;
 }
+
+int X509_cmp(const X509 *a, const X509 *b)
+{
+  const struct x509_pm *pm_a;
+  const struct x509_pm *pm_b;
+
+  if (a == NULL || b == NULL)
+    {
+      return a == b ? 0 : (a < b ? -1 : 1);
+    }
+
+  pm_a = (const struct x509_pm *)a->x509_pm;
+  pm_b = (const struct x509_pm *)b->x509_pm;
+
+  if (pm_a == NULL || pm_b == NULL)
+    {
+      return pm_a == pm_b ? 0 : (pm_a < pm_b ? -1 : 1);
+    }
+
+  if (pm_a->x509_crt.raw.len != pm_b->x509_crt.raw.len)
+    {
+      return pm_a->x509_crt.raw.len < pm_b->x509_crt.raw.len ? -1 : 1;
+    }
+
+  return memcmp(pm_a->x509_crt.raw.p,
+                pm_b->x509_crt.raw.p,
+                pm_a->x509_crt.raw.len);
+}
diff --git a/testing/crypto/.gitignore b/testing/crypto/.gitignore
new file mode 100644
index 000000000..9e1d2593e
--- /dev/null
+++ b/testing/crypto/.gitignore
@@ -0,0 +1 @@
+/Kconfig
diff --git a/testing/crypto/Makefile b/testing/crypto/Makefile
new file mode 100644
index 000000000..43b447a7a
--- /dev/null
+++ b/testing/crypto/Makefile
@@ -0,0 +1,25 @@
+############################################################################
+# apps/testing/crypto/Makefile
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+MENUDESC = "crypto"
+
+include $(APPDIR)/Directory.mk
diff --git a/testing/crypto/openssl_mbedtls_wrapper/CMakeLists.txt 
b/testing/crypto/openssl_mbedtls_wrapper/CMakeLists.txt
new file mode 100644
index 000000000..fe7ade8e5
--- /dev/null
+++ b/testing/crypto/openssl_mbedtls_wrapper/CMakeLists.txt
@@ -0,0 +1,40 @@
+# 
##############################################################################
+# apps/testing/crypto/openssl_mbedtls_wrapper/CMakeLists.txt
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more 
contributor
+# license agreements.  See the NOTICE file distributed with this work for
+# additional information regarding copyright ownership.  The ASF licenses this
+# file to you under the Apache License, Version 2.0 (the "License"); you may 
not
+# use this file except in compliance with the License.  You may obtain a copy 
of
+# the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
+#
+# 
##############################################################################
+
+if(CONFIG_TESTING_OPENSSL_WRAPPER_MBEDTLS)
+  if(CONFIG_TESTING_OPENSSL_WRAPPER_MBEDTLS_PKCS12)
+    nuttx_add_application(
+      NAME
+      cmocka_pkcs12
+      PRIORITY
+      ${CONFIG_TESTING_OPENSSL_WRAPPER_MBEDTLS_PRIORITY}
+      STACKSIZE
+      ${CONFIG_TESTING_OPENSSL_WRAPPER_MBEDTLS_STACKSIZE}
+      MODULE
+      ${CONFIG_TESTING_OPENSSL_WRAPPER_MBEDTLS}
+      DEPENDS
+      ${CMOCKA_LIB}
+      SRCS
+      pkcs12_api_test.c
+      ${CMOCKA_KERNEL})
+  endif()
+endif()
diff --git a/testing/crypto/openssl_mbedtls_wrapper/Kconfig 
b/testing/crypto/openssl_mbedtls_wrapper/Kconfig
new file mode 100644
index 000000000..ef5c7204d
--- /dev/null
+++ b/testing/crypto/openssl_mbedtls_wrapper/Kconfig
@@ -0,0 +1,31 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config TESTING_OPENSSL_WRAPPER_MBEDTLS
+       tristate "openssl_mbedtls_wrapper cmocka tests"
+       default n
+       depends on TESTING_CMOCKA
+       depends on OPENSSL_MBEDTLS_WRAPPER
+       ---help---
+               Enable cmocka tests for openssl_mbedtls_wrapper
+
+if TESTING_OPENSSL_WRAPPER_MBEDTLS
+
+config TESTING_OPENSSL_WRAPPER_MBEDTLS_PRIORITY
+       int "Task priority"
+       default 100
+
+config TESTING_OPENSSL_WRAPPER_MBEDTLS_STACKSIZE
+       int "Stack size"
+       default 32768
+
+config TESTING_OPENSSL_WRAPPER_MBEDTLS_PKCS12
+       bool "Enable PKCS12 parse test"
+       depends on MBEDTLS_PKCS12_C
+       default y
+       ---help---
+               Test d2i_PKCS12, PKCS12_parse, PKCS12_free
+
+endif
diff --git a/testing/crypto/openssl_mbedtls_wrapper/Make.defs 
b/testing/crypto/openssl_mbedtls_wrapper/Make.defs
new file mode 100644
index 000000000..d1519a6dd
--- /dev/null
+++ b/testing/crypto/openssl_mbedtls_wrapper/Make.defs
@@ -0,0 +1,25 @@
+############################################################################
+# apps/testing/crypto/openssl_mbedtls_wrapper/Make.defs
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+ifneq ($(CONFIG_TESTING_OPENSSL_WRAPPER_MBEDTLS),)
+CONFIGURED_APPS += $(APPDIR)/testing/crypto/openssl_mbedtls_wrapper
+endif
diff --git a/testing/crypto/openssl_mbedtls_wrapper/Makefile 
b/testing/crypto/openssl_mbedtls_wrapper/Makefile
new file mode 100644
index 000000000..c7f584d0b
--- /dev/null
+++ b/testing/crypto/openssl_mbedtls_wrapper/Makefile
@@ -0,0 +1,34 @@
+############################################################################
+# apps/testing/crypto/openssl_mbedtls_wrapper/Makefile
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+include $(APPDIR)/Make.defs
+
+PRIORITY  = $(CONFIG_TESTING_OPENSSL_WRAPPER_MBEDTLS_PRIORITY)
+STACKSIZE = $(CONFIG_TESTING_OPENSSL_WRAPPER_MBEDTLS_STACKSIZE)
+MODULE    = $(CONFIG_TESTING_OPENSSL_WRAPPER_MBEDTLS)
+
+ifneq ($(CONFIG_TESTING_OPENSSL_WRAPPER_MBEDTLS_PKCS12),)
+MAINSRC  += pkcs12_api_test.c
+PROGNAME += cmocka_pkcs12
+endif
+
+include $(APPDIR)/Application.mk
diff --git a/testing/crypto/openssl_mbedtls_wrapper/pkcs12_api_test.c 
b/testing/crypto/openssl_mbedtls_wrapper/pkcs12_api_test.c
new file mode 100644
index 000000000..0729ce34b
--- /dev/null
+++ b/testing/crypto/openssl_mbedtls_wrapper/pkcs12_api_test.c
@@ -0,0 +1,427 @@
+/****************************************************************************
+ * apps/testing/crypto/openssl_mbedtls_wrapper/pkcs12_api_test.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* Ported from OpenSSL test/pkcs12_api_test.c — PKCS12_parse minimal set.
+ * Original: Copyright 2022-2023 The OpenSSL Project Authors.
+ * Licensed under the Apache License 2.0.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <cmocka.h>
+
+#include <openssl/pkcs12.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/stack.h>
+#include <openssl/x509.h>
+
+/****************************************************************************
+ * Test Data
+ *
+ * OpenSSL native test (test/recipes/80-test_pkcs12.t) generates P12 files
+ * at runtime from PEM certs in test/certs/ via the openssl CLI, then passes
+ * them to pkcs12_api_test via "-in <file>". Since we run on NuttX without
+ * filesystem or CLI tools, the DER is pre-generated and embedded here.
+ *
+ * Generation commands:
+ *
+ *   openssl req -x509 -newkey rsa:1024 -keyout key.pem -out cert.pem \
+ *       -days 3650 -nodes -subj "/CN=test"
+ *
+ *   openssl pkcs12 -export -in cert.pem -inkey key.pem -certfile cert.pem \
+ *       -out test.p12 -password pass:test123 \
+ *       -keypbe pbeWithSHA1And3-KeyTripleDES-CBC \
+ *       -certpbe pbeWithSHA1And3-KeyTripleDES-CBC
+ *
+ *   xxd -i test.p12
+ *
+ * Contents: RSA-1024 key + self-signed X509 cert + CA chain (same cert),
+ *           password "test123", PBE: pbeWithSHA1And3-KeyTripleDES-CBC.
+ ****************************************************************************/
+
+static const unsigned char g_test_p12_data[] =
+{
+  0x30, 0x82, 0x07, 0xc1, 0x02, 0x01, 0x03, 0x30, 0x82, 0x07, 0x87, 0x06,
+  0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x82,
+  0x07, 0x78, 0x04, 0x82, 0x07, 0x74, 0x30, 0x82, 0x07, 0x70, 0x30, 0x82,
+  0x04, 0x6f, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07,
+  0x06, 0xa0, 0x82, 0x04, 0x60, 0x30, 0x82, 0x04, 0x5c, 0x02, 0x01, 0x00,
+  0x30, 0x82, 0x04, 0x55, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
+  0x01, 0x07, 0x01, 0x30, 0x1c, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7,
+  0x0d, 0x01, 0x0c, 0x01, 0x03, 0x30, 0x0e, 0x04, 0x08, 0x60, 0x7f, 0xb2,
+  0x96, 0xa3, 0x10, 0x4d, 0xee, 0x02, 0x02, 0x08, 0x00, 0x80, 0x82, 0x04,
+  0x28, 0x4e, 0x34, 0x58, 0x0e, 0xe6, 0x29, 0xa5, 0x9c, 0xc1, 0x2b, 0xd1,
+  0x22, 0x05, 0x48, 0x1e, 0xf3, 0xde, 0x36, 0x07, 0x1e, 0x4b, 0x68, 0x5d,
+  0x2b, 0x26, 0xf8, 0xe6, 0xe1, 0x5a, 0x42, 0x35, 0xd6, 0xb7, 0x59, 0x01,
+  0x4a, 0x41, 0xb1, 0x3b, 0xbc, 0xe0, 0xbc, 0x5c, 0x5e, 0x15, 0x41, 0xfb,
+  0xa3, 0xf3, 0x19, 0xbe, 0xcd, 0x59, 0x79, 0x63, 0x28, 0x2d, 0xd3, 0xa2,
+  0x65, 0xfa, 0x0f, 0x1f, 0x54, 0x5b, 0x95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 0x87, 0x22, 0xd7, 0xe4, 0xf1, 0xb8, 0xe2, 0xf6,
+  0x89, 0xad, 0xa7, 0x69, 0x03, 0x00, 0x4e, 0x93, 0x00, 0x30, 0x03, 0x1f,
+  0xe5, 0x12, 0x37, 0xbe, 0x7d, 0xaf, 0x28, 0x68, 0x1c, 0x33, 0xb8, 0xc5,
+  0xbb, 0x34, 0x3f, 0x2a, 0xe7, 0xe2, 0x90, 0xfb, 0x22, 0xfe, 0xd5, 0xc4,
+  0x37, 0xe7, 0x37, 0x52, 0x65, 0x56, 0x27, 0x1b, 0x48, 0x10, 0xf3, 0xef,
+  0x48, 0xe2, 0x24, 0x29, 0x08, 0x39, 0x6a, 0xc5, 0xc3, 0xd3, 0xec, 0x16,
+  0x70, 0x3c, 0xa9, 0x47, 0xd3, 0x1c, 0x01, 0x81, 0x54, 0xc3, 0xad, 0x0c,
+  0xde, 0x8f, 0xb1, 0x8f, 0x3c, 0x09, 0xa8, 0xc2, 0x19, 0x2a, 0x1f, 0x0b,
+  0x99, 0x6f, 0xed, 0xc3, 0x3d, 0x31, 0x74, 0xb9, 0x66, 0x71, 0x29, 0xf4,
+  0xa4, 0x0e, 0x17, 0x66, 0x7a, 0x04, 0x95, 0xd3, 0xec, 0x55, 0x77, 0xeb,
+  0x7c, 0xcd, 0x9c, 0x9d, 0xdc, 0xaa, 0x34, 0x0a, 0xaf, 0xd4, 0x35, 0x6f,
+  0x37, 0xf2, 0x40, 0x7d, 0xe3, 0x62, 0x33, 0x8c, 0x44, 0x86, 0x36, 0x49,
+  0x35, 0xc3, 0x9d, 0xb6, 0xfc, 0x04, 0x99, 0x3d, 0xcf, 0x31, 0x99, 0xe5,
+  0x3f, 0x0d, 0x4e, 0x81, 0x3b, 0x57, 0x7f, 0x51, 0x48, 0x5f, 0x60, 0x53,
+  0x99, 0xd1, 0xee, 0x7f, 0xb7, 0x8c, 0x2d, 0xef, 0x7d, 0x58, 0x99, 0x32,
+  0x48, 0x33, 0x27, 0x84, 0x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ac, 0x65, 0x48, 0x8d, 0x7c, 0xa0, 0xb3, 0xed, 0xca, 0x56,
+  0x07, 0x2c, 0x72, 0x7e, 0xc9, 0x5d, 0x8a, 0xc3, 0x06, 0xd5, 0xb0, 0xdd,
+  0xab, 0xb9, 0x6f, 0x98, 0x5c, 0xa5, 0x47, 0x72, 0xa7, 0xb7, 0x7f, 0xf3,
+  0xbf, 0x56, 0x4c, 0xb7, 0x80, 0x2c, 0x92, 0x55, 0x0a, 0x1d, 0x65, 0x8c,
+  0x44, 0xc9, 0xc9, 0xa0, 0xb7, 0x43, 0xb5, 0x6d, 0x44, 0xc3, 0x61, 0x94,
+  0xc4, 0xd8, 0xfd, 0x5b, 0x11, 0x53, 0x3a, 0x24, 0xf3, 0xdb, 0xa6, 0xf1,
+  0x44, 0xa2, 0x72, 0xa3, 0x37, 0xef, 0xca, 0x2c, 0xf6, 0x74, 0x68, 0xd6,
+  0x04, 0x56, 0x7c, 0x19, 0xb5, 0x4b, 0x47, 0x50, 0x35, 0xf3, 0xa4, 0x55,
+  0x5c, 0xcf, 0xc7, 0x46, 0x14, 0x0e, 0x40, 0x00, 0xdc, 0x30, 0x82, 0x02,
+  0xf9, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01,
+  0xa0, 0x82, 0x02, 0xea, 0x04, 0x82, 0x02, 0xe6, 0x30, 0x82, 0x02, 0xe2,
+  0x30, 0x82, 0x02, 0xde, 0x06, 0x0b, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
+  0x01, 0x0c, 0x0a, 0x01, 0x02, 0xa0, 0x82, 0x02, 0xa6, 0x30, 0x82, 0x02,
+  0xa2, 0x30, 0x1c, 0x06, 0x0a, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
+  0x0c, 0x01, 0x03, 0x30, 0x0e, 0x04, 0x08, 0x65, 0xa9, 0x0c, 0xdd, 0x2a,
+  0xef, 0xf1, 0x1e, 0x02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, 0x7f,
+  0xf3, 0xb0, 0xbf, 0x8d, 0x5e, 0x34, 0xa2, 0x12, 0x64, 0x5a, 0xdd, 0x61,
+  0x66, 0x7d, 0x24, 0x22, 0x27, 0x74, 0xcc, 0x72, 0x84, 0x56, 0xc9, 0x88,
+  0x9a, 0x05, 0x40, 0xc6, 0x0d, 0xdf, 0x85, 0x0c, 0x7d, 0xab, 0x5e, 0x00,
+  0x8a, 0xe1, 0x04, 0x42, 0x48, 0xa6, 0x59, 0x52, 0x1d, 0x2c, 0x6d, 0xdb,
+  0x7b, 0x5c, 0x39, 0x29, 0xb3, 0x53, 0x08, 0x40, 0x49, 0xf0, 0xb7, 0x81,
+  0xc2, 0x6c, 0x8f, 0x18, 0xd5, 0xf9, 0x4e, 0x89, 0xd6, 0x13, 0x9e, 0xbd,
+  0xf5, 0x23, 0x43, 0x76, 0x16, 0xad, 0xb3, 0x03, 0x6a, 0xa6, 0xb7, 0x73,
+  0x59, 0xd2, 0x84, 0x18, 0xa3, 0x3c, 0x1d, 0xaf, 0x9e, 0x84, 0x80, 0xfc,
+  0x68, 0x96, 0x87, 0x4c, 0xc6, 0x82, 0xde, 0xa9, 0xbe, 0x34, 0x55, 0x99,
+  0x44, 0x66, 0x76, 0x31, 0x25, 0x30, 0x23, 0x06, 0x09, 0x2a, 0x86, 0x48,
+  0x86, 0xf7, 0x0d, 0x01, 0x09, 0x15, 0x31, 0x16, 0x04, 0x14, 0x73, 0x01,
+  0x2c, 0x2c, 0x43, 0x1c, 0x8c, 0x29, 0x1e, 0x3e, 0xf0, 0x96, 0x7a, 0x08,
+  0x5b, 0x55, 0x05, 0xa5, 0x52, 0x77, 0x30, 0x31, 0x30, 0x21, 0x30, 0x09,
+  0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0xc1,
+  0xac, 0xf8, 0x8f, 0x38, 0xb6, 0x96, 0xa4, 0xfd, 0xd9, 0x88, 0xdc, 0x1b,
+  0xa9, 0xea, 0x27, 0x7a, 0xb0, 0xd7, 0xc7, 0x04, 0x08, 0xc2, 0xe6, 0x68,
+  0x7e, 0x40, 0x0a, 0x5f, 0x02, 0x02, 0x02, 0x08, 0x00
+};
+
+static const unsigned int g_test_p12_data_len = 1989;
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define TEST_P12_PASSWORD "test123"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Corresponds to PKCS12_load() in pkcs12_api_test.c.
+ * We load from embedded DER data instead of file.
+ */
+
+static PKCS12 *pkcs12_load(void)
+{
+  const unsigned char *p = g_test_p12_data;
+
+  return d2i_PKCS12(NULL, &p, (long)g_test_p12_data_len);
+}
+
+/* OPENSSL_sk_free() only drops the stack container; this wrapper has no
+ * pop_free, so release each X509 entry first, as PKCS12_newpass() does.
+ */
+
+static void free_x509_sk(STACK_OF(X509) *sk)
+{
+  int i;
+
+  if (sk == NULL)
+    {
+      return;
+    }
+
+  for (i = 0; i < sk->num; i++)
+    {
+      X509_free((X509 *)sk->data[i]);
+    }
+
+  OPENSSL_sk_free(sk);
+}
+
+/* Corresponds to changepass() in pkcs12_api_test.c.
+ * Uses BIO roundtrip matching OpenSSL original flow:
+ * PKCS12_newpass → i2d_PKCS12_bio(mem) → d2i_PKCS12_bio(mem) →
+ * parse with new password → compare key and cert.
+ */
+
+static int changepass(PKCS12 *p12, EVP_PKEY *key, X509 *cert,
+                      STACK_OF(X509) *ca)
+{
+  STACK_OF(X509) *ca2 = NULL;
+  EVP_PKEY *key2 = NULL;
+  PKCS12 *p12new = NULL;
+  X509 *cert2 = NULL;
+  BIO *bio = NULL;
+  int ret = 0;
+
+  if (PKCS12_newpass(p12, TEST_P12_PASSWORD, "NEWPASS") != 1)
+    {
+      printf("[CP] FAIL: newpass\n");
+      goto err;
+    }
+
+  bio = BIO_new(BIO_s_mem());
+  if (bio == NULL)
+    {
+      printf("[CP] FAIL: BIO_new\n");
+      goto err;
+    }
+
+  if (i2d_PKCS12_bio(bio, p12) != 1)
+    {
+      printf("[CP] FAIL: i2d_PKCS12_bio\n");
+      goto err;
+    }
+
+  p12new = d2i_PKCS12_bio(bio, NULL);
+  if (p12new == NULL)
+    {
+      printf("[CP] FAIL: d2i_PKCS12_bio\n");
+      goto err;
+    }
+
+  if (PKCS12_parse(p12new, "NEWPASS", &key2, &cert2, &ca2) != 1)
+    {
+      printf("[CP] FAIL: re-parse\n");
+      goto err;
+    }
+
+  if (key != NULL)
+    {
+      if (key2 == NULL || EVP_PKEY_eq(key, key2) != 1)
+        {
+          printf("[CP] FAIL: key_eq (key2=%p)\n", key2);
+          goto err;
+        }
+    }
+
+  if (cert != NULL)
+    {
+      if (cert2 == NULL || X509_cmp(cert, cert2) != 0)
+        {
+          printf("[CP] FAIL: cert_cmp (cert2=%p)\n", cert2);
+          goto err;
+        }
+    }
+
+  ret = 1;
+
+err:
+  BIO_free(bio);
+  PKCS12_free(p12new);
+  EVP_PKEY_free(key2);
+  X509_free(cert2);
+  free_x509_sk(ca2);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: test_null_args
+ *
+ * Description:
+ *   Ported from pkcs12_api_test.c test_null_args().
+ *   PKCS12_parse(NULL, NULL, NULL, NULL, NULL) must return 0 (failure).
+ ****************************************************************************/
+
+static void test_null_args(void **state)
+{
+  assert_int_equal(PKCS12_parse(NULL, NULL, NULL, NULL, NULL), 0);
+}
+
+/****************************************************************************
+ * Name: test_pkcs12_parse
+ *
+ * Description:
+ *   Ported from pkcs12_api_test.c pkcs12_parse_test().
+ *   Load a valid P12 → parse → verify key, cert, ca are present →
+ *   then run changepass round-trip.
+ ****************************************************************************/
+
+static void test_pkcs12_parse(void **state)
+{
+  STACK_OF(X509) *ca = NULL;
+  PKCS12 *p12 = NULL;
+  EVP_PKEY *key = NULL;
+  X509 *cert = NULL;
+
+  p12 = pkcs12_load();
+  assert_non_null(p12);
+
+  assert_int_equal(PKCS12_parse(p12, TEST_P12_PASSWORD,
+                                &key, &cert, &ca), 1);
+
+  /* Our test data has key, cert, and ca */
+
+  assert_non_null(key);
+  assert_non_null(cert);
+  assert_non_null(ca);
+
+  /* changepass round-trip: newpass → serialize → parse → compare */
+
+  assert_int_equal(changepass(p12, key, cert, ca), 1);
+
+  PKCS12_free(p12);
+  EVP_PKEY_free(key);
+  X509_free(cert);
+  free_x509_sk(ca);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  const struct CMUnitTest tests[] =
+    {
+      cmocka_unit_test(test_null_args),
+      cmocka_unit_test(test_pkcs12_parse),
+    };
+
+  return cmocka_run_group_tests(tests, NULL, NULL);
+}

Reply via email to