Commit 0a5b232307 for openssl.org

commit 0a5b2323077a27ec8abad97062008af4ffa089b5
Author: Nihal Kotte <lahinettok@gmail.com>
Date:   Wed Jun 17 18:56:05 2026 +0530

    Add X509v3_addr_get_safi()

    Adds a new function, X509v3_addr_get_safi(), which allows the SAFI
    from the IPAddressFamily to be obtained.

    Resolves: https://github.com/openssl/openssl/issues/18529
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    Reviewed-by: Matt Caswell <matt@openssl.foundation>
    Merge-date: Mon Nov  2 11:38:54 2026
    Merged-from: https://github.com/openssl/openssl/pull/31564

diff --git a/CHANGES.md b/CHANGES.md
index f6cb395529..92c2c4ebdd 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -113,6 +113,11 @@ OpenSSL 4.2

    *John Claus*

+ * Added `X509v3_addr_get_safi`, which gets the SAFI from the IPAddressFamily
+   structure.
+
+   *Nihal Kotte*
+
  * Added AVX-512 and VAES optimizations for AES-CTR mode. Performance for
    large inputs (1024 bytes or more) improved by 2.9x to 3.9x.
    <!-- https://github.com/openssl/openssl/pull/30755 -->
diff --git a/crypto/x509/v3_addr.c b/crypto/x509/v3_addr.c
index 8bf6fed69e..7c4081b268 100644
--- a/crypto/x509/v3_addr.c
+++ b/crypto/x509/v3_addr.c
@@ -97,6 +97,21 @@ unsigned int X509v3_addr_get_afi(const IPAddressFamily *f)
     return (f->addressFamily->data[0] << 8) | f->addressFamily->data[1];
 }

+/*
+ * Get SAFI from an IPAdressFamily.
+ */
+unsigned int X509v3_addr_get_safi(const IPAddressFamily *f)
+{
+    if (f == NULL
+        || f->addressFamily == NULL
+        || f->addressFamily->data == NULL
+        || f->addressFamily->length < 3) {
+        return 0;
+    }
+
+    return f->addressFamily->data[2];
+}
+
 /*
  * Expand the bitstring form of an address into a raw byte array.
  * At the moment this is coded for simplicity, not speed.
diff --git a/doc/build.info b/doc/build.info
index 3ee1e0d8ee..0db3744027 100644
--- a/doc/build.info
+++ b/doc/build.info
@@ -3155,6 +3155,10 @@ DEPEND[html/man3/X509_verify_cert.html]=man3/X509_verify_cert.pod
 GENERATE[html/man3/X509_verify_cert.html]=man3/X509_verify_cert.pod
 DEPEND[man/man3/X509_verify_cert.3]=man3/X509_verify_cert.pod
 GENERATE[man/man3/X509_verify_cert.3]=man3/X509_verify_cert.pod
+DEPEND[html/man3/X509v3_addr_get_safi.html]=man3/X509v3_addr_get_safi.pod
+GENERATE[html/man3/X509v3_addr_get_safi.html]=man3/X509v3_addr_get_safi.pod
+DEPEND[man/man3/X509v3_addr_get_safi.3]=man3/X509v3_addr_get_safi.pod
+GENERATE[man/man3/X509v3_addr_get_safi.3]=man3/X509v3_addr_get_safi.pod
 DEPEND[html/man3/X509v3_get_ext_by_NID.html]=man3/X509v3_get_ext_by_NID.pod
 GENERATE[html/man3/X509v3_get_ext_by_NID.html]=man3/X509v3_get_ext_by_NID.pod
 DEPEND[man/man3/X509v3_get_ext_by_NID.3]=man3/X509v3_get_ext_by_NID.pod
@@ -3872,6 +3876,7 @@ html/man3/X509_new.html \
 html/man3/X509_sign.html \
 html/man3/X509_verify.html \
 html/man3/X509_verify_cert.html \
+html/man3/X509v3_addr_get_safi.html \
 html/man3/X509v3_get_ext_by_NID.html \
 html/man3/b2i_PVK_bio_ex.html \
 html/man3/d2i_PKCS8PrivateKey_bio.html \
@@ -4555,6 +4560,7 @@ man/man3/X509_new.3 \
 man/man3/X509_sign.3 \
 man/man3/X509_verify.3 \
 man/man3/X509_verify_cert.3 \
+man/man3/X509v3_addr_get_safi.3 \
 man/man3/X509v3_get_ext_by_NID.3 \
 man/man3/b2i_PVK_bio_ex.3 \
 man/man3/d2i_PKCS8PrivateKey_bio.3 \
diff --git a/doc/man3/X509v3_addr_get_safi.pod b/doc/man3/X509v3_addr_get_safi.pod
new file mode 100644
index 0000000000..cd8b46c05e
--- /dev/null
+++ b/doc/man3/X509v3_addr_get_safi.pod
@@ -0,0 +1,35 @@
+=pod
+
+=head1 NAME
+
+X509v3_addr_get_safi - X509 SAFI finder
+
+=head1 SYNOPSIS
+
+ #include <openssl/x509v3.h>
+
+ unsigned int X509v3_addr_get_safi(const IPAddressFamily *f);
+
+=head1 DESCRIPTION
+
+X509v3_addr_get_safi() retrieves the SAFI from the IPAddressFamily data structure.
+
+=head1 RETURN VALUES
+
+X509v3_addr_get_safi() returns the SAFI if it can find it. In the case that
+IPAddressFamily does not have it, it returns a value of 0.
+
+=head1 HISTORY
+
+X509v3_addr_get_safi() was added in OpenSSL 4.2.0.
+
+=head1 COPYRIGHT
+
+Copyright 2015-2026 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
+in the file LICENSE in the source distribution or at
+L<https://www.openssl.org/source/license.html>.
+
+=cut
diff --git a/include/openssl/x509v3.h.in b/include/openssl/x509v3.h.in
index 34f1c6e69b..28bd80bc30 100644
--- a/include/openssl/x509v3.h.in
+++ b/include/openssl/x509v3.h.in
@@ -965,6 +965,7 @@ int X509v3_addr_add_range(IPAddrBlocks *addr,
     unsigned afi, const unsigned *safi,
     unsigned char *min, unsigned char *max);
 unsigned X509v3_addr_get_afi(const IPAddressFamily *f);
+unsigned X509v3_addr_get_safi(const IPAddressFamily *f);
 int X509v3_addr_get_range(IPAddressOrRange *aor, unsigned afi,
     unsigned char *min, unsigned char *max,
     int length);
diff --git a/test/v3nametest.c b/test/v3nametest.c
index 20029aa163..77f9a4890d 100644
--- a/test/v3nametest.c
+++ b/test/v3nametest.c
@@ -771,6 +771,38 @@ end:
     return testresult;
 }

+#if !defined(OPENSSL_NO_RFC3779)
+static int test_x509v3_addr_get_safi(void)
+{
+
+    IPAddressFamily *f = NULL;
+    int ret = 0;
+    unsigned char testdat[3] = { 0x00, 0x01, 0x07 };
+    unsigned char testdat1[2] = { 0x00, 0x01 };
+
+    if (!TEST_ptr(f = IPAddressFamily_new()))
+        goto err;
+
+    if (!TEST_true(ASN1_OCTET_STRING_set(f->addressFamily, testdat, sizeof(testdat))))
+        goto err;
+
+    if (!TEST_int_eq(X509v3_addr_get_safi(f), 7))
+        goto err;
+
+    if (!TEST_true(ASN1_OCTET_STRING_set(f->addressFamily, testdat1, sizeof(testdat1))))
+        goto err;
+
+    if (!TEST_int_eq(X509v3_addr_get_safi(f), 0))
+        goto err;
+
+    ret = 1;
+
+err:
+    IPAddressFamily_free(f);
+    return ret;
+}
+#endif
+
 int setup_tests(void)
 {
 #if !defined(OPENSSL_NO_DEPRECATED_4_1)
@@ -779,5 +811,9 @@ int setup_tests(void)
     ADD_TEST(test_check_subject_flags);
 #endif /* !defined(OPENSSL_NO_DEPRECATED_4_1) */
     ADD_TEST(test_GENERAL_NAME_cmp);
+
+#if !defined(OPENSSL_NO_RFC3779)
+    ADD_TEST(test_x509v3_addr_get_safi);
+#endif
     return 1;
 }
diff --git a/util/libcrypto.num b/util/libcrypto.num
index c9809d8ef2..c20bf4717d 100644
--- a/util/libcrypto.num
+++ b/util/libcrypto.num
@@ -5738,6 +5738,7 @@ i2d_IPAddrBlocks                        ?	4_2_0	EXIST::FUNCTION:RFC3779
 IPAddrBlocks_free                       ?	4_2_0	EXIST::FUNCTION:RFC3779
 IPAddrBlocks_new                        ?	4_2_0	EXIST::FUNCTION:RFC3779
 IPAddrBlocks_it                         ?	4_2_0	EXIST::FUNCTION:RFC3779
+X509v3_addr_get_safi                    ?	4_2_0	EXIST::FUNCTION:RFC3779
 PKCS8_PRIV_KEY_INFO_get1_skey           ?	4_2_0	EXIST::FUNCTION:
 PKCS12_decrypt_secretbag                ?	4_2_0	EXIST::FUNCTION:
 PKCS12_PARSE_CTX_new                    ?	4_2_0	EXIST::FUNCTION: