Commit 350489e1f1 for asterisk.org

commit 350489e1f15551f1e8867632fccb41851571fcec
Author: George Joseph <gjoseph@sangoma.com>
Date:   Mon Nov 16 13:59:42 2026 -0700

    Updates for OpenSSL 4

    Most of the changes were the result of OpenSSL changing some API signatures to
    return `const` instead of non-constant object pointers.  There was also a
    deprecation of `X509_STORE_get0_objects()` because it returned internal
    pointers. It was replaced by `X509_STORE_get1_objects()` which returns a
    thread-safe pointer and must be freed with `sk_X509_OBJECT_pop_free()`.
    `X509_cmp_time()` was also deprecated in favor of `ASN1_TIME_cmp_time_t()`

    A minor update to the `stir_shaken verify certificate_file` CLI command was
    also made which now warns if the certificate tested is is either not valid
    yet or has expired.  Previously the command only returned whether the cert
    was trusted or not which only cheks the certificate chain, not the valididity
    periods.

    FYI... The changes mostly revolved around adding ...
    `#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)`
    for the new behavior.  The patch was tested locally on Rocky 8 with
    OpenSSL 1.1.1k to make sure there were no issues with backwards compatibility.

    Also be aware, some of the new functions and prototypes were actually added
    or updated in OpenSSL 3.3 and have been available for a while.  This commit
    however only uses them if the version is >= 4.0.0 to minimize changes to the
    source files.

    Resolves: #2302

diff --git a/main/tcptls.c b/main/tcptls.c
index d0710612d8..084ff1c404 100644
--- a/main/tcptls.c
+++ b/main/tcptls.c
@@ -82,7 +82,11 @@ static void session_instance_destructor(void *obj)
 }

 #ifdef DO_SSL
+#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
+static int check_tcptls_cert_name(const ASN1_STRING *cert_str, const char *hostname, const char *desc)
+#else
 static int check_tcptls_cert_name(ASN1_STRING *cert_str, const char *hostname, const char *desc)
+#endif
 {
 	unsigned char *str;
 	int ret;
@@ -174,8 +178,13 @@ struct ast_tcptls_session_instance *ast_tcptls_start_tls(struct ast_tcptls_sessi
 				return NULL;
 			}
 			if (!ast_test_flag(&tcptls_session->parent->tls_cfg->flags, AST_SSL_IGNORE_COMMON_NAME)) {
+#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
+				const ASN1_STRING *str;
+				const X509_NAME *name = X509_get_subject_name(peer);
+#else
 				ASN1_STRING *str;
 				X509_NAME *name = X509_get_subject_name(peer);
+#endif
 				STACK_OF(GENERAL_NAME) *alt_names;
 				int pos = -1;
 				int found = 0;
diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c
index 4ac6d28593..8feb822c10 100644
--- a/res/res_rtp_asterisk.c
+++ b/res/res_rtp_asterisk.c
@@ -2092,12 +2092,14 @@ static int create_ephemeral_certificate(EVP_PKEY *keypair, X509 **certificate)
 #endif

 	/* Set the name and issuer */
-	if (!(name = X509_get_subject_name(cert))
-	   || !X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC,
+	if (!(name = X509_NAME_new())
+		|| !X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_ASC,
 									  (unsigned char *) "asterisk", -1, -1, 0)
-	   || !X509_set_issuer_name(cert, name)) {
+		|| !X509_set_subject_name(cert, name)
+		|| !X509_set_issuer_name(cert, name)) {
 		goto error;
 	}
+	X509_NAME_free(name);

 	/* Sign it */
 	if (!X509_sign(cert, keypair, EVP_sha256())) {
diff --git a/res/res_stir_shaken/common_config.c b/res/res_stir_shaken/common_config.c
index 33e2d188a9..948b230b8c 100644
--- a/res/res_stir_shaken/common_config.c
+++ b/res/res_stir_shaken/common_config.c
@@ -354,6 +354,35 @@ static char *cli_verify_cert(struct ast_cli_entry *e, int cmd, struct ast_cli_ar
 		return CLI_SUCCESS;
 	}

+	if (!crypto_is_cert_time_valid(cert, time(NULL))) {
+		const ASN1_TIME *notbefore = X509_get_notBefore(cert);
+		const ASN1_TIME *notafter = X509_get_notAfter(cert);
+		const char *cert_before = (char *)ASN1_STRING_get0_data(notbefore);
+		const char *cert_after = (char *)ASN1_STRING_get0_data(notafter);
+		char *before = NULL;
+		char *after = NULL;
+		struct tm tm = { 0, };
+		char buf[255];
+
+		if (!notbefore || !notafter) {
+			ast_log(LOG_ERROR, "Either notbefore or notafter were missing in the cert\n");
+			return 0;
+		}
+
+		/*
+		 * ASN1_TIME is actually an ASN1_STRING in the format 'yymmddHHMMSS'
+		 */
+		strptime(cert_before, "%y%m%d%H%M%S", &tm);
+		strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
+		before = ast_strdupa(buf);
+		strptime(cert_after, "%y%m%d%H%M%S", &tm);
+		strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
+		after = ast_strdupa(buf);
+
+		ast_cli(a->fd, "Certificate %s is either not valid yet or has expired.  Certificate valid date range: %s -> %s\n", a->argv[3],
+			before, after);
+	}
+
 	if (crypto_is_cert_trusted(tcs, cert, cert_chain, &errmsg)) {
 		ast_cli(a->fd, "Certificate %s trusted\n", a->argv[3]);
 	} else {
diff --git a/res/res_stir_shaken/crypto_utils.c b/res/res_stir_shaken/crypto_utils.c
index ce22bf8bc9..b3332fc98a 100644
--- a/res/res_stir_shaken/crypto_utils.c
+++ b/res/res_stir_shaken/crypto_utils.c
@@ -28,6 +28,7 @@
 #include <openssl/x509.h>
 #include <openssl/x509v3.h>
 #include <openssl/x509_vfy.h>
+#include <openssl/asn1.h>

 #include "crypto_utils.h"

@@ -104,11 +105,18 @@ int crypto_register_x509_extension(const char *oid, const char *short_name,
 	return nid;
 }

-ASN1_OCTET_STRING *crypto_get_cert_extension_data(X509 *cert,
+#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
+const ASN1_OCTET_STRING *crypto_get_cert_extension_data(X509 *cert,
 	int nid, const char *short_name)
 {
+	const X509_EXTENSION *ex;
+#else
+	ASN1_OCTET_STRING *crypto_get_cert_extension_data(X509 *cert,
+		int nid, const char *short_name)
+	{
+		X509_EXTENSION *ex;
+#endif
 	int ex_idx;
-	X509_EXTENSION *ex;

 	if (nid <= 0) {
 		nid = OBJ_sn2nid(short_name);
@@ -695,7 +703,14 @@ int crypto_load_untrusted_cert_store(struct crypto_cert_store *store, const char
 	 * The store holds the references to the certs so we can't
 	 * free it.
 	 */
+#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
+	objs = X509_STORE_get1_objects(store->untrusted);
+#else
 	objs = X509_STORE_get0_objects(store->untrusted);
+#endif
+	if (!objs) {
+		return -1;
+	}
 	count = sk_X509_OBJECT_num(objs);
 	for (i = 0; i < count ; i++) {
 		X509_OBJECT *o = sk_X509_OBJECT_value(objs, i);
@@ -704,7 +719,9 @@ int crypto_load_untrusted_cert_store(struct crypto_cert_store *store, const char
 			sk_X509_push(store->untrusted_stack, c);
 		}
 	}
-
+#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
+	sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
+#endif
 	return 0;
 }

@@ -738,7 +755,14 @@ int crypto_load_crl_store(struct crypto_cert_store *store, const char *file,
 	 * The store holds the references to the CRLs so we can't
 	 * free it.
 	 */
+#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
+	objs = X509_STORE_get1_objects(store->crls);
+#else
 	objs = X509_STORE_get0_objects(store->crls);
+#endif
+	if (!objs) {
+		return -1;
+	}
 	count = sk_X509_OBJECT_num(objs);
 	for (i = 0; i < count ; i++) {
 		X509_OBJECT *o = sk_X509_OBJECT_value(objs, i);
@@ -747,6 +771,9 @@ int crypto_load_crl_store(struct crypto_cert_store *store, const char *file,
 			sk_X509_CRL_push(store->crl_stack, c);
 		}
 	}
+#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
+	sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
+#endif

 	return 0;
 }
@@ -764,7 +791,14 @@ int crypto_show_cli_store(struct crypto_cert_store *store, int fd)
 	/*
 	 * The CA certificates are stored in the certs store.
 	 */
+#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
+	objs = X509_STORE_get1_objects(store->certs);
+#else
 	objs = X509_STORE_get0_objects(store->certs);
+#endif
+	if (!objs) {
+		return 0;
+	}
 	count = sk_X509_OBJECT_num(objs);

 	for (i = 0; i < count ; i++) {
@@ -777,7 +811,9 @@ int crypto_show_cli_store(struct crypto_cert_store *store, int fd)
 			ast_log(LOG_ERROR, "CRLs are not allowed in the CA cert store\n");
 		}
 	}
-
+#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
+	sk_X509_OBJECT_pop_free(objs, X509_OBJECT_free);
+#endif
 	/*
 	 * Although the untrusted certs are stored in the untrusted store,
 	 * we already have the stack of certificates so we can just
@@ -809,8 +845,9 @@ int crypto_show_cli_store(struct crypto_cert_store *store, int fd)

 int crypto_is_cert_time_valid(X509*cert, time_t reftime)
 {
-	ASN1_STRING *notbefore;
-	ASN1_STRING *notafter;
+	const ASN1_TIME *notbefore;
+	const ASN1_TIME *notafter;
+	int valid=0;

 	if (!reftime) {
 		reftime = time(NULL);
@@ -822,8 +859,11 @@ int crypto_is_cert_time_valid(X509*cert, time_t reftime)
 		return 0;
 	}

-	return (X509_cmp_time(notbefore, &reftime) < 0 &&
-		X509_cmp_time(notafter, &reftime) > 0);
+	valid=(ASN1_TIME_cmp_time_t(notbefore, reftime) < 0 &&
+			ASN1_TIME_cmp_time_t(notafter, reftime) > 0);
+	ast_debug(4, "Certificate time valid: %s\n", AST_YESNO(valid));
+
+	return valid;
 }

 int crypto_is_cert_trusted(struct crypto_cert_store *store, X509 *cert,
@@ -939,7 +979,7 @@ char *crypto_get_cert_subject(X509 *cert, const char *short_name)
 		short_name ? XN_FLAG_FN_SN | XN_FLAG_SEP_MULTILINE : XN_FLAG_ONELINE;
 	FILE *fp = open_memstream(&buffer, &len);
 	BIO *bio = fp ? BIO_new_fp(fp, BIO_CLOSE) : NULL;
-	X509_NAME *subject = X509_get_subject_name(cert);
+	const X509_NAME *subject = X509_get_subject_name(cert);
 	int rc = 0;

 	if (!fp || !bio || !subject) {
diff --git a/res/res_stir_shaken/crypto_utils.h b/res/res_stir_shaken/crypto_utils.h
index 2780ba15c4..4fc6d99def 100644
--- a/res/res_stir_shaken/crypto_utils.h
+++ b/res/res_stir_shaken/crypto_utils.h
@@ -70,8 +70,13 @@ int crypto_register_x509_extension(const char *oid,
  *
  * \warning Do NOT attempt to free the returned buffer.
  */
-ASN1_OCTET_STRING *crypto_get_cert_extension_data(X509 *cert, int nid,
-	const char *short_name);
+#if (OPENSSL_VERSION_NUMBER >= 0x40000000L)
+const ASN1_OCTET_STRING *crypto_get_cert_extension_data(X509 *cert,
+	int nid, const char *short_name);
+#else
+ASN1_OCTET_STRING *crypto_get_cert_extension_data(X509 *cert,
+	int nid, const char *short_name);
+#endif

 /*!
  * \brief Load an X509 Cert and any chained certs from a file
diff --git a/res/res_stir_shaken/verification.c b/res/res_stir_shaken/verification.c
index 3fed81a978..43072e90ad 100644
--- a/res/res_stir_shaken/verification.c
+++ b/res/res_stir_shaken/verification.c
@@ -253,7 +253,7 @@ static int is_cert_cache_entry_expired(char *expiration)
 static enum ast_stir_shaken_vs_response_code
 	check_tn_auth_list(struct ast_stir_shaken_vs_ctx * ctx)
 {
-	ASN1_OCTET_STRING *tn_exten;
+	const ASN1_OCTET_STRING *tn_exten;
 	const unsigned char* octet_str_data = NULL;
 	long xlen;
 	int tag, xclass;