diff -urp zj.orig/CHANGES zj/CHANGES
--- zj.orig/CHANGES	2023-11-06 11:20:03.000000000 +0100
+++ zj/CHANGES	2025-08-06 14:02:19.352887943 +0200
@@ -7,9 +7,39 @@
  https://github.com/openssl/openssl/commits/ and pick the appropriate
  release branch.
 
- Changes between 1.0.2zi and 1.0.2zj [xx XXX xxxx]
+ Changes between 1.0.2zi and 1.0.2zj [25 Jan 2024]
 
- *)
+ *) A file in PKCS12 format can contain certificates and keys and may come from
+    an untrusted source. The PKCS12 specification allows certain fields to be
+    NULL, but OpenSSL did not correctly check for this case. A fix has been
+    applied to prevent a NULL pointer dereference that results in OpenSSL
+    crashing. If an application processes PKCS12 files from an untrusted source
+    using the OpenSSL APIs then that application will be vulnerable to this
+    issue prior to this fix.
+
+    OpenSSL APIs that were vulnerable to this are: PKCS12_parse(),
+    PKCS12_unpack_p7data(), PKCS12_unpack_p7encdata(), PKCS12_unpack_authsafes()
+    and PKCS12_newpass().
+
+    We have also fixed a similar issue in SMIME_write_PKCS7(). However since this
+    function is related to writing data we do not consider it security
+    significant.
+
+    (CVE-2024-0727)
+    [Matt Caswell]
+
+ *) Fix excessive time spent in DH check / generation with large Q parameter
+    value.
+
+    Applications that use the functions DH_generate_key() to generate an
+    X9.42 DH key may experience long delays. Likewise, applications that use
+    DH_check_pub_key(), DH_check_pub_key_ex() or EVP_PKEY_public_check()
+    to check an X9.42 DH key or X9.42 DH parameters may experience long delays.
+    Where the key or parameters that are being checked have been obtained from
+    an untrusted source this may lead to a Denial of Service.
+
+    (CVE-2023-5678)
+    [Richard Levitte]
 
  Changes between 1.0.2zh and 1.0.2zi [1 Aug 2023]
 
diff -urp zj.orig/crypto/dh/dh.h zj/crypto/dh/dh.h
--- zj.orig/crypto/dh/dh.h	2023-11-06 11:20:03.000000000 +0100
+++ zj/crypto/dh/dh.h	2025-08-06 13:54:32.791906000 +0200
@@ -165,14 +165,16 @@ struct dh_st {
 /* #define DH_GENERATOR_3       3 */
 # define DH_GENERATOR_5          5
 
-/* DH_check error codes */
+/* DH_check error codes, some of them shared with DH_check_pub_key */
 # define DH_CHECK_P_NOT_PRIME            0x01
 # define DH_CHECK_P_NOT_SAFE_PRIME       0x02
 # define DH_UNABLE_TO_CHECK_GENERATOR    0x04
 # define DH_NOT_SUITABLE_GENERATOR       0x08
 # define DH_CHECK_Q_NOT_PRIME            0x10
-# define DH_CHECK_INVALID_Q_VALUE        0x20
+# define DH_CHECK_INVALID_Q_VALUE        0x20 /* +DH_check_pub_key */
 # define DH_CHECK_INVALID_J_VALUE        0x40
+# define DH_MODULUS_TOO_SMALL            0x80
+# define DH_MODULUS_TOO_LARGE            0x100 /* +DH_check_pub_key */
 
 /* DH_check_pub_key error codes */
 # define DH_CHECK_PUBKEY_TOO_SMALL       0x01
diff -urp zj.orig/crypto/opensslv.h zj/crypto/opensslv.h
--- zj.orig/crypto/opensslv.h	2023-11-06 11:20:03.000000000 +0100
+++ zj/crypto/opensslv.h	2025-08-06 14:37:45.666073753 +0200
@@ -30,11 +30,11 @@ extern "C" {
  * (Prior to 0.9.5a beta1, a different scheme was used: MMNNFFRBB for
  *  major minor fix final patch/beta)
  */
-# define OPENSSL_VERSION_NUMBER  0x10002230L
+# define OPENSSL_VERSION_NUMBER  0x1000223fL
 # ifdef OPENSSL_FIPS
-#  define OPENSSL_VERSION_TEXT    "OpenSSL 1.0.2zj-fips-dev  xx XXX xxxx"
+#  define OPENSSL_VERSION_TEXT    "OpenSSL 1.0.2zj-fips-sec  25 Jan 2024"
 # else
-#  define OPENSSL_VERSION_TEXT    "OpenSSL 1.0.2zj-dev  xx XXX xxxx"
+#  define OPENSSL_VERSION_TEXT    "OpenSSL 1.0.2zj-sec  25 Jan 2024"
 # endif
 # define OPENSSL_VERSION_PTEXT   " part of " OPENSSL_VERSION_TEXT
 
diff -urp zj.orig/crypto/pkcs12/p12_add.c zj/crypto/pkcs12/p12_add.c
--- zj.orig/crypto/pkcs12/p12_add.c	2023-11-06 11:20:03.000000000 +0100
+++ zj/crypto/pkcs12/p12_add.c	2025-08-06 13:50:22.504642489 +0200
@@ -171,6 +171,12 @@ STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_
                   PKCS12_R_CONTENT_TYPE_NOT_DATA);
         return NULL;
     }
+
+    if (p7->d.data == NULL) {
+        PKCS12err(PKCS12_F_PKCS12_UNPACK_P7DATA, PKCS12_R_DECODE_ERROR);
+        return NULL;
+    }
+
     return ASN1_item_unpack(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS));
 }
 
@@ -226,6 +232,12 @@ STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_
 {
     if (!PKCS7_type_is_encrypted(p7))
         return NULL;
+
+    if (p7->d.encrypted == NULL) {
+        PKCS12err(PKCS12_F_PKCS12_UNPACK_P7ENCDATA, PKCS12_R_DECODE_ERROR);
+        return NULL;
+    }
+
     return PKCS12_item_decrypt_d2i(p7->d.encrypted->enc_data->algorithm,
                                    ASN1_ITEM_rptr(PKCS12_SAFEBAGS),
                                    pass, passlen,
@@ -253,6 +265,12 @@ STACK_OF(PKCS7) *PKCS12_unpack_authsafes
                   PKCS12_R_CONTENT_TYPE_NOT_DATA);
         return NULL;
     }
+
+    if (p12->authsafes->d.data == NULL) {
+        PKCS12err(PKCS12_F_PKCS12_UNPACK_AUTHSAFES, PKCS12_R_DECODE_ERROR);
+        return NULL;
+    }
+
     return ASN1_item_unpack(p12->authsafes->d.data,
                             ASN1_ITEM_rptr(PKCS12_AUTHSAFES));
 }
diff -urp zj.orig/crypto/pkcs12/p12_mutl.c zj/crypto/pkcs12/p12_mutl.c
--- zj.orig/crypto/pkcs12/p12_mutl.c	2023-11-06 11:20:03.000000000 +0100
+++ zj/crypto/pkcs12/p12_mutl.c	2025-08-06 13:50:58.190697870 +0200
@@ -80,6 +80,11 @@ int PKCS12_gen_mac(PKCS12 *p12, const ch
         return 0;
     }
 
+    if (p12->authsafes->d.data == NULL) {
+        PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_DECODE_ERROR);
+        return 0;
+    }
+
     salt = p12->mac->salt->data;
     saltlen = p12->mac->salt->length;
     if (!p12->mac->iter)
diff -urp zj.orig/crypto/pkcs12/p12_npas.c zj/crypto/pkcs12/p12_npas.c
--- zj.orig/crypto/pkcs12/p12_npas.c	2023-11-06 11:20:03.000000000 +0100
+++ zj/crypto/pkcs12/p12_npas.c	2025-08-06 13:51:48.852346805 +0200
@@ -126,8 +126,9 @@ static int newpass_p12(PKCS12 *p12, cons
             bags = PKCS12_unpack_p7data(p7);
         } else if (bagnid == NID_pkcs7_encrypted) {
             bags = PKCS12_unpack_p7encdata(p7, oldpass, -1);
-            if (!alg_get(p7->d.encrypted->enc_data->algorithm,
-                         &pbe_nid, &pbe_iter, &pbe_saltlen))
+            if (p7->d.encrypted == NULL
+                    || !alg_get(p7->d.encrypted->enc_data->algorithm,
+                                &pbe_nid, &pbe_iter, &pbe_saltlen))
                 goto err;
         } else {
             continue;
diff -urp zj.orig/crypto/pkcs12/pk12err.c zj/crypto/pkcs12/pk12err.c
--- zj.orig/crypto/pkcs12/pk12err.c	2023-11-06 11:20:03.000000000 +0100
+++ zj/crypto/pkcs12/pk12err.c	2025-08-06 13:52:19.269530384 +0200
@@ -98,6 +98,7 @@ static ERR_STRING_DATA PKCS12_str_functs
     {ERR_FUNC(PKCS12_F_PKCS12_SET_MAC), "PKCS12_set_mac"},
     {ERR_FUNC(PKCS12_F_PKCS12_UNPACK_AUTHSAFES), "PKCS12_unpack_authsafes"},
     {ERR_FUNC(PKCS12_F_PKCS12_UNPACK_P7DATA), "PKCS12_unpack_p7data"},
+    {ERR_FUNC(PKCS12_F_PKCS12_UNPACK_P7ENCDATA), "PKCS12_unpack_p7encdata"},
     {ERR_FUNC(PKCS12_F_PKCS12_VERIFY_MAC), "PKCS12_verify_mac"},
     {ERR_FUNC(PKCS12_F_PKCS8_ADD_KEYUSAGE), "PKCS8_add_keyusage"},
     {ERR_FUNC(PKCS12_F_PKCS8_ENCRYPT), "PKCS8_encrypt"},
diff -urp zj.orig/crypto/pkcs12/pkcs12.h zj/crypto/pkcs12/pkcs12.h
--- zj.orig/crypto/pkcs12/pkcs12.h	2023-11-06 11:20:03.000000000 +0100
+++ zj/crypto/pkcs12/pkcs12.h	2025-08-06 13:56:23.221866626 +0200
@@ -308,6 +308,7 @@ void ERR_load_PKCS12_strings(void);
 # define PKCS12_F_PKCS12_SET_MAC                          123
 # define PKCS12_F_PKCS12_UNPACK_AUTHSAFES                 130
 # define PKCS12_F_PKCS12_UNPACK_P7DATA                    131
+# define PKCS12_F_PKCS12_UNPACK_P7ENCDATA                 134
 # define PKCS12_F_PKCS12_VERIFY_MAC                       126
 # define PKCS12_F_PKCS8_ADD_KEYUSAGE                      124
 # define PKCS12_F_PKCS8_ENCRYPT                           125
diff -urp zj.orig/crypto/pkcs7/pk7_mime.c zj/crypto/pkcs7/pk7_mime.c
--- zj.orig/crypto/pkcs7/pk7_mime.c	2023-11-06 11:20:03.000000000 +0100
+++ zj/crypto/pkcs7/pk7_mime.c	2025-08-06 13:53:02.505363660 +0200
@@ -78,10 +78,15 @@ int SMIME_write_PKCS7(BIO *bio, PKCS7 *p
 {
     STACK_OF(X509_ALGOR) *mdalgs;
     int ctype_nid = OBJ_obj2nid(p7->type);
-    if (ctype_nid == NID_pkcs7_signed)
+    if (ctype_nid == NID_pkcs7_signed) {
+        if (p7->d.sign == NULL) {
+            return 0;
+        }
         mdalgs = p7->d.sign->md_algs;
-    else
+    }
+    else {
         mdalgs = NULL;
+    }
 
     flags ^= SMIME_OLDMIME;
 
