Botan 3.14.1
Crypto and TLS for C&
Botan::Cert_Extension::IPAddressBlocks Class Referencefinal

#include <x509_ext.h>

Inheritance diagram for Botan::Cert_Extension::IPAddressBlocks:
Botan::Certificate_Extension

Classes

class  IPAddress
class  IPAddressChoice
class  IPAddressFamily
class  IPAddressOrRange

Public Types

enum class  Version : uint8_t { IPv4 = 4 , IPv6 = 16 }

Public Member Functions

void add_address (const IPv4Address &address, std::optional< uint8_t > safi=std::nullopt)
 Add a single IPv4 address to this extension (for the specified SAFI, if any).
void add_address (const IPv4Address &min, const IPv4Address &max, std::optional< uint8_t > safi=std::nullopt)
 Add an IPv4 address range to this extension (for the specified SAFI, if any).
void add_address (const IPv4Subnet &subnet, std::optional< uint8_t > safi=std::nullopt)
 Add an IPv4 subnet to this extension (for the specified SAFI, if any).
void add_address (const IPv6Address &address, std::optional< uint8_t > safi=std::nullopt)
 Add a single IPv6 address to this extension (for the specified SAFI, if any).
void add_address (const IPv6Address &min, const IPv6Address &max, std::optional< uint8_t > safi=std::nullopt)
 Add an IPv6 address range to this extension (for the specified SAFI, if any).
void add_address (const IPv6Subnet &subnet, std::optional< uint8_t > safi=std::nullopt)
 Add an IPv6 subnet to this extension (for the specified SAFI, if any).
template<Version V>
void add_address (const std::array< uint8_t, static_cast< size_t >(V)> &address, std::optional< uint8_t > safi=std::nullopt)
 Add a single IP address to this extension (for the specified SAFI, if any).
template<Version V>
void add_address (const std::array< uint8_t, static_cast< std::size_t >(V)> &min, const std::array< uint8_t, static_cast< std::size_t >(V)> &max, std::optional< uint8_t > safi=std::nullopt)
 Add an IP address range to this extension (for the specified SAFI, if any).
const std::vector< IPAddressFamily > & addr_blocks () const
std::unique_ptr< Certificate_Extension > copy () const override
template<Version V>
void inherit (std::optional< uint8_t > safi=std::nullopt)
 Mark the specified IP version as 'inherit' (for the specified SAFI, if any).
 IPAddressBlocks ()=default
 IPAddressBlocks (const std::vector< IPAddressFamily > &blocks)
OID oid_of () const override
template<Version V>
void restrict (std::optional< uint8_t > safi=std::nullopt)
 Make the extension contain no allowed IP addresses for the specified IP version (and SAFI, if any).
size_t v4_count () const
 The number of IPv4 families contained in the extension.
size_t v6_count () const
 The number of IPv6 families contained in the extension.
void validate (const X509_Certificate &subject, const std::optional< X509_Certificate > &issuer, const std::vector< X509_Certificate > &cert_path, std::vector< std::set< Certificate_Status_Code > > &cert_status, size_t pos) const override

Static Public Member Functions

static OID static_oid ()

Detailed Description

IP Address Blocks Extension

RFC 3779 X.509 Extensions for IP Addr

Definition at line 1021 of file x509_ext.h.

Member Enumeration Documentation

◆ Version

Enumerator
IPv4 
IPv6 

Definition at line 1023 of file x509_ext.h.

1023 : uint8_t {
1024 IPv4 = 4,
1025 IPv6 = 16,
1026 };

Constructor & Destructor Documentation

◆ IPAddressBlocks() [1/2]

Botan::Cert_Extension::IPAddressBlocks::IPAddressBlocks ( )
default

Referenced by validate().

◆ IPAddressBlocks() [2/2]

Botan::Cert_Extension::IPAddressBlocks::IPAddressBlocks ( const std::vector< IPAddressFamily > & blocks)
inlineexplicit

Definition at line 1208 of file x509_ext.h.

1208 : m_ip_addr_blocks(blocks) {
1209 this->sort_and_merge();
1210 }

Member Function Documentation

◆ add_address() [1/8]

void Botan::Cert_Extension::IPAddressBlocks::add_address ( const IPv4Address & address,
std::optional< uint8_t > safi = std::nullopt )
inline

Add a single IPv4 address to this extension (for the specified SAFI, if any).

Definition at line 1225 of file x509_ext.h.

1225 {
1226 add_address(address, address, safi);
1227 }
void add_address(const IPv4Address &address, std::optional< uint8_t > safi=std::nullopt)
Add a single IPv4 address to this extension (for the specified SAFI, if any).
Definition x509_ext.h:1225

References add_address().

Referenced by add_address(), add_address(), add_address(), and add_address().

◆ add_address() [2/8]

void Botan::Cert_Extension::IPAddressBlocks::add_address ( const IPv4Address & min,
const IPv4Address & max,
std::optional< uint8_t > safi = std::nullopt )
inline

Add an IPv4 address range to this extension (for the specified SAFI, if any).

Definition at line 1235 of file x509_ext.h.

◆ add_address() [3/8]

void Botan::Cert_Extension::IPAddressBlocks::add_address ( const IPv4Subnet & subnet,
std::optional< uint8_t > safi = std::nullopt )
inline

Add an IPv4 subnet to this extension (for the specified SAFI, if any).

Definition at line 1245 of file x509_ext.h.

1245 {
1246 add_range(IPAddressOrRange<Version::IPv4>(subnet), safi);
1247 }

◆ add_address() [4/8]

void Botan::Cert_Extension::IPAddressBlocks::add_address ( const IPv6Address & address,
std::optional< uint8_t > safi = std::nullopt )
inline

Add a single IPv6 address to this extension (for the specified SAFI, if any).

Definition at line 1230 of file x509_ext.h.

1230 {
1231 add_address(address, address, safi);
1232 }

References add_address().

◆ add_address() [5/8]

void Botan::Cert_Extension::IPAddressBlocks::add_address ( const IPv6Address & min,
const IPv6Address & max,
std::optional< uint8_t > safi = std::nullopt )
inline

Add an IPv6 address range to this extension (for the specified SAFI, if any).

Definition at line 1240 of file x509_ext.h.

1240 {
1241 add_range(IPAddressOrRange<Version::IPv6>(min, max), safi);
1242 }

◆ add_address() [6/8]

void Botan::Cert_Extension::IPAddressBlocks::add_address ( const IPv6Subnet & subnet,
std::optional< uint8_t > safi = std::nullopt )
inline

Add an IPv6 subnet to this extension (for the specified SAFI, if any).

Definition at line 1250 of file x509_ext.h.

1250 {
1251 add_range(IPAddressOrRange<Version::IPv6>(subnet), safi);
1252 }

◆ add_address() [7/8]

template<Version V>
void Botan::Cert_Extension::IPAddressBlocks::add_address ( const std::array< uint8_t, static_cast< size_t >(V)> & address,
std::optional< uint8_t > safi = std::nullopt )
inline

Add a single IP address to this extension (for the specified SAFI, if any).

Definition at line 1257 of file x509_ext.h.

1258 {
1259 add_range(IPAddressOrRange<V>(IPAddress<V>::from_bytes(address)), safi);
1260 }

References add_address().

◆ add_address() [8/8]

template<Version V>
void Botan::Cert_Extension::IPAddressBlocks::add_address ( const std::array< uint8_t, static_cast< std::size_t >(V)> & min,
const std::array< uint8_t, static_cast< std::size_t >(V)> & max,
std::optional< uint8_t > safi = std::nullopt )
inline

Add an IP address range to this extension (for the specified SAFI, if any).

Definition at line 1265 of file x509_ext.h.

1267 {
1268 add_range(IPAddressOrRange<V>(IPAddress<V>::from_bytes(min), IPAddress<V>::from_bytes(max)), safi);
1269 }

References add_address().

◆ addr_blocks()

const std::vector< IPAddressFamily > & Botan::Cert_Extension::IPAddressBlocks::addr_blocks ( ) const
inline

Definition at line 1286 of file x509_ext.h.

1286{ return m_ip_addr_blocks; }

Referenced by validate().

◆ copy()

std::unique_ptr< Certificate_Extension > Botan::Cert_Extension::IPAddressBlocks::copy ( ) const
inlineoverridevirtual

Make a copy of this extension

Returns
copy of this

Implements Botan::Certificate_Extension.

Definition at line 1212 of file x509_ext.h.

1212{ return std::make_unique<IPAddressBlocks>(*this); }

◆ inherit()

template<Version V>
void Botan::Cert_Extension::IPAddressBlocks::inherit ( std::optional< uint8_t > safi = std::nullopt)
inline

Mark the specified IP version as 'inherit' (for the specified SAFI, if any).

Definition at line 1281 of file x509_ext.h.

◆ oid_of()

OID Botan::Cert_Extension::IPAddressBlocks::oid_of ( ) const
inlineoverridevirtual

Return object identifier for this extension

Returns
OID representing this extension

Implements Botan::Certificate_Extension.

Definition at line 1216 of file x509_ext.h.

1216{ return static_oid(); }

References static_oid().

◆ restrict()

template<Version V>
void Botan::Cert_Extension::IPAddressBlocks::restrict ( std::optional< uint8_t > safi = std::nullopt)
inline

Make the extension contain no allowed IP addresses for the specified IP version (and SAFI, if any).

Definition at line 1273 of file x509_ext.h.

1273 {
1274 std::vector<IPAddressOrRange<V>> addresses = {};
1275 m_ip_addr_blocks.push_back(IPAddressFamily(IPAddressChoice<V>(addresses), safi));
1276 sort_and_merge();
1277 }

◆ static_oid()

OID Botan::Cert_Extension::IPAddressBlocks::static_oid ( )
inlinestatic

Definition at line 1214 of file x509_ext.h.

1214{ return OID({1, 3, 6, 1, 5, 5, 7, 1, 7}); }

Referenced by oid_of().

◆ v4_count()

size_t Botan::Cert_Extension::IPAddressBlocks::v4_count ( ) const
inline

The number of IPv4 families contained in the extension.

Definition at line 1289 of file x509_ext.h.

1289{ return m_v4_count; }

Referenced by botan_x509_ext_ip_addr_blocks_get_address(), botan_x509_ext_ip_addr_blocks_get_counts(), and botan_x509_ext_ip_addr_blocks_get_family().

◆ v6_count()

size_t Botan::Cert_Extension::IPAddressBlocks::v6_count ( ) const
inline

The number of IPv6 families contained in the extension.

Definition at line 1292 of file x509_ext.h.

1292{ return m_v6_count; }

◆ validate()

void Botan::Cert_Extension::IPAddressBlocks::validate ( const X509_Certificate & subject,
const std::optional< X509_Certificate > & issuer,
const std::vector< X509_Certificate > & cert_path,
std::vector< std::set< Certificate_Status_Code > > & cert_status,
size_t pos ) const
overridevirtual

Callback visited during path validation.

An extension can implement this callback to inspect the path during path validation.

If an error occurs during validation of this extension, an appropriate status code shall be added to cert_status.

Parameters
subjectSubject certificate that contains this extension
issuerIssuer certificate. nullopt for certificates with no available issuer (e.g. non self-signed trust anchors).
cert_pathCertificate path which is currently validated
cert_statusCertificate validation status codes for subject certificate
posPosition of subject certificate in cert_path

Reimplemented from Botan::Certificate_Extension.

Definition at line 2581 of file x509_ext.cpp.

2585 {
2586 // maps in the form of (s)afi -> (needs_checking, ranges)
2587 auto [v4_needs_check, v6_needs_check] = create_validation_map(m_ip_addr_blocks);
2588
2589 if(pos == cert_path.size() - 1) {
2590 // checks if any range / family has 'inherit' as a value somewhere, not allowed for the root cert
2591 const auto validate_root_cert_ext = [&](const auto& map) {
2592 // check if any range has a value of 'false', indicating 'inherit'
2593 return std::any_of(map.begin(), map.end(), [&](const auto& it) {
2594 const auto& [_1, validation_info] = it;
2595 const auto& [needs_checking, _2] = validation_info;
2596 return !needs_checking;
2597 });
2598 };
2599 if(validate_root_cert_ext(v4_needs_check) || validate_root_cert_ext(v6_needs_check)) {
2600 cert_status.at(pos).insert(Certificate_Status_Code::IPADDR_BLOCKS_ERROR);
2601 }
2602 return;
2603 }
2604
2605 // traverse the chain until we find a cert with concrete values for the extension (so not 'inherit')
2606 for(auto cert_path_it = cert_path.begin() + pos + 1; cert_path_it != cert_path.end(); cert_path_it++) {
2607 const IPAddressBlocks* const parent_ip = cert_path_it->v3_extensions().get_extension_object_as<IPAddressBlocks>();
2608 // extension not present for parent
2609 if(parent_ip == nullptr) {
2610 cert_status.at(pos).insert(Certificate_Status_Code::IPADDR_BLOCKS_ERROR);
2611 return;
2612 }
2613 auto [issuer_v4, issuer_v6] = create_validation_map(parent_ip->addr_blocks());
2614
2615 const auto validate_against_issuer = [&](auto& subject_map, const auto& issuer_map) {
2616 for(auto map_it = subject_map.begin(); map_it != subject_map.end(); map_it++) {
2617 auto& [afam, validation_info] = *map_it;
2618
2619 // the issuer does not have this combination of afi/safi
2620 if(issuer_map.count(afam) == 0) {
2621 cert_status.at(pos).insert(Certificate_Status_Code::IPADDR_BLOCKS_ERROR);
2622 return false;
2623 }
2624
2625 auto& [needs_check, subject_value] = validation_info;
2626 const auto& [issuer_has_value, issuer_value] = issuer_map.at(afam);
2627 BOTAN_ASSERT_NOMSG(!needs_check || subject_value != nullptr);
2628 BOTAN_ASSERT_NOMSG(!issuer_has_value || issuer_value != nullptr);
2629
2630 // we still need to check this range and the issuer has an actual value for it (so not 'inherit')
2631 if(needs_check && issuer_has_value) {
2632 if(!validate_subject_in_issuer(std::span(*subject_value), std::span(*issuer_value))) {
2633 cert_status.at(pos).insert(Certificate_Status_Code::IPADDR_BLOCKS_ERROR);
2634 return false;
2635 }
2636 needs_check = false;
2637 }
2638 }
2639 return true;
2640 };
2641
2642 if(!validate_against_issuer(v4_needs_check, issuer_v4) || !validate_against_issuer(v6_needs_check, issuer_v6)) {
2643 return;
2644 }
2645
2646 const auto validate_no_checks_left = [&](const auto& map) {
2647 // check if all ranges have been checked, either by comparing their ranges if they have any,
2648 // or if they are inherit, their parent(s) will be validated later
2649 return std::all_of(map.begin(), map.end(), [&](const auto& it) {
2650 const auto& [_1, validation_info] = it;
2651 const auto& [needs_checking, _2] = validation_info;
2652 return !needs_checking;
2653 });
2654 };
2655
2656 if(validate_no_checks_left(v4_needs_check) && validate_no_checks_left(v6_needs_check)) {
2657 // we've validated what we need to and can stop traversing the cert chain
2658 return;
2659 }
2660 }
2661}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82

References addr_blocks(), BOTAN_ASSERT_NOMSG, Botan::IPADDR_BLOCKS_ERROR, and IPAddressBlocks().


The documentation for this class was generated from the following files: