aboutsummaryrefslogtreecommitdiff
path: root/crypto/openssl/test/x509_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/openssl/test/x509_test.c')
-rw-r--r--crypto/openssl/test/x509_test.c111
1 files changed, 110 insertions, 1 deletions
diff --git a/crypto/openssl/test/x509_test.c b/crypto/openssl/test/x509_test.c
index 1c6e569a4c44..a9023a809471 100644
--- a/crypto/openssl/test/x509_test.c
+++ b/crypto/openssl/test/x509_test.c
@@ -1,5 +1,5 @@
/*
- * Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
@@ -176,6 +176,112 @@ static int test_asn1_item_verify(void)
return ret;
}
+static int test_x509_delete_last_extension(void)
+{
+ int ret = 0;
+ X509 *x509 = NULL;
+ X509_EXTENSION *ext = NULL;
+ ASN1_OBJECT *obj = NULL;
+
+ if (!TEST_ptr((x509 = X509_new()))
+ /* Initially, there are no extensions and thus no extension list. */
+ || !TEST_ptr_null(X509_get0_extensions(x509))
+ /* Add an extension. */
+ || !TEST_ptr((ext = X509_EXTENSION_new()))
+ || !TEST_ptr((obj = OBJ_nid2obj(NID_subject_key_identifier)))
+ || !TEST_int_eq(X509_EXTENSION_set_object(ext, obj), 1)
+ || !TEST_int_eq(X509_add_ext(x509, ext, -1), 1)
+ /* There should now be an extension list. */
+ || !TEST_ptr(X509_get0_extensions(x509))
+ || !TEST_int_eq(sk_X509_EXTENSION_num(X509_get0_extensions(x509)), 1))
+ goto err;
+
+ /* Delete the extension. */
+ X509_EXTENSION_free(X509_delete_ext(x509, 0));
+
+ /* The extension list should be NULL again. */
+ if (!TEST_ptr_null(X509_get0_extensions(x509)))
+ goto err;
+
+ ret = 1;
+
+err:
+ X509_free(x509);
+ X509_EXTENSION_free(ext);
+ return ret;
+}
+
+static int test_x509_crl_delete_last_extension(void)
+{
+ int ret = 0;
+ X509_CRL *crl = NULL;
+ X509_EXTENSION *ext = NULL;
+ ASN1_OBJECT *obj = NULL;
+
+ if (!TEST_ptr((crl = X509_CRL_new()))
+ /* Initially, there are no extensions and thus no extension list. */
+ || !TEST_ptr_null(X509_CRL_get0_extensions(crl))
+ /* Add an extension. */
+ || !TEST_ptr((ext = X509_EXTENSION_new()))
+ || !TEST_ptr((obj = OBJ_nid2obj(NID_subject_key_identifier)))
+ || !TEST_int_eq(X509_EXTENSION_set_object(ext, obj), 1)
+ || !TEST_int_eq(X509_CRL_add_ext(crl, ext, -1), 1)
+ /* There should now be an extension list. */
+ || !TEST_ptr(X509_CRL_get0_extensions(crl))
+ || !TEST_int_eq(sk_X509_EXTENSION_num(X509_CRL_get0_extensions(crl)),
+ 1))
+ goto err;
+
+ /* Delete the extension. */
+ X509_EXTENSION_free(X509_CRL_delete_ext(crl, 0));
+
+ /* The extension list should be NULL again. */
+ if (!TEST_ptr_null(X509_CRL_get0_extensions(crl)))
+ goto err;
+
+ ret = 1;
+
+err:
+ X509_CRL_free(crl);
+ X509_EXTENSION_free(ext);
+ return ret;
+}
+
+static int test_x509_revoked_delete_last_extension(void)
+{
+ int ret = 0;
+ X509_REVOKED *rev = NULL;
+ X509_EXTENSION *ext = NULL;
+ ASN1_OBJECT *obj = NULL;
+
+ if (!TEST_ptr((rev = X509_REVOKED_new()))
+ /* Initially, there are no extensions and thus no extension list. */
+ || !TEST_ptr_null(X509_REVOKED_get0_extensions(rev))
+ /* Add an extension. */
+ || !TEST_ptr((ext = X509_EXTENSION_new()))
+ || !TEST_ptr((obj = OBJ_nid2obj(NID_subject_key_identifier)))
+ || !TEST_int_eq(X509_EXTENSION_set_object(ext, obj), 1)
+ || !TEST_int_eq(X509_REVOKED_add_ext(rev, ext, -1), 1)
+ /* There should now be an extension list. */
+ || !TEST_ptr(X509_REVOKED_get0_extensions(rev))
+ || !TEST_int_eq(sk_X509_EXTENSION_num(X509_REVOKED_get0_extensions(rev)), 1))
+ goto err;
+
+ /* Delete the extension. */
+ X509_EXTENSION_free(X509_REVOKED_delete_ext(rev, 0));
+
+ /* The extension list should be NULL again. */
+ if (!TEST_ptr_null(X509_REVOKED_get0_extensions(rev)))
+ goto err;
+
+ ret = 1;
+
+err:
+ X509_REVOKED_free(rev);
+ X509_EXTENSION_free(ext);
+ return ret;
+}
+
OPT_TEST_DECLARE_USAGE("<pss-self-signed-cert.pem>\n")
int setup_tests(void)
@@ -210,6 +316,9 @@ int setup_tests(void)
ADD_TEST(test_x509_tbs_cache);
ADD_TEST(test_x509_crl_tbs_cache);
ADD_TEST(test_asn1_item_verify);
+ ADD_TEST(test_x509_delete_last_extension);
+ ADD_TEST(test_x509_crl_delete_last_extension);
+ ADD_TEST(test_x509_revoked_delete_last_extension);
return 1;
}