Botan 3.14.1
Crypto and TLS for C&
Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V > Class Template Referencefinal

#include <x509_ext.h>

Inheritance diagram for Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >:
Botan::ASN1_Object

Public Member Functions

std::optional< Subnet > as_subnet () const
std::vector< uint8_t > BER_encode () const
void decode_from (BER_Decoder &from) override
void encode_into (DER_Encoder &to) const override
 IPAddressOrRange ()=default
 IPAddressOrRange (const Address &addr)
 IPAddressOrRange (const Address &min, const Address &max)
 IPAddressOrRange (const IPAddress< V > &addr)
 IPAddressOrRange (const IPAddress< V > &min, const IPAddress< V > &max)
 IPAddressOrRange (const Subnet &subnet)
IPAddress< V > max () const
IPAddress< V > min () const

Detailed Description

template<Version V>
class Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >

Definition at line 1108 of file x509_ext.h.

Constructor & Destructor Documentation

◆ IPAddressOrRange() [1/6]

◆ IPAddressOrRange() [2/6]

template<Version V>
Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::IPAddressOrRange ( const IPAddress< V > & addr)
inlineexplicit

Definition at line 1118 of file x509_ext.h.

◆ IPAddressOrRange() [3/6]

template<Version V>
Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::IPAddressOrRange ( const IPAddress< V > & min,
const IPAddress< V > & max )
inline

Definition at line 1120 of file x509_ext.h.

1120 : m_min(min), m_max(max) {
1121 if(max < min) {
1122 throw Decoding_Error("IP address ranges must be sorted");
1123 }
1124 }

References max(), and min().

◆ IPAddressOrRange() [4/6]

template<Version V>
Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::IPAddressOrRange ( const Address & addr)
inlineexplicit

Create a range containing a single address (IPv4Address or IPv6Address)

Definition at line 1129 of file x509_ext.h.

References IPAddressOrRange().

◆ IPAddressOrRange() [5/6]

template<Version V>
Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::IPAddressOrRange ( const Address & min,
const Address & max )
inline

Create a range of addresses (IPv4Address or IPv6Address) from min to max (inclusive)

Definition at line 1134 of file x509_ext.h.

References IPAddressOrRange(), max(), and min().

◆ IPAddressOrRange() [6/6]

template<Version V>
Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::IPAddressOrRange ( const Subnet & subnet)
inlineexplicit

Create a range covering exactly the addresses of a subnet (IPv4Subnet or IPv6Subnet)

Definition at line 1140 of file x509_ext.h.

1140 :
1141 IPAddressOrRange(subnet.address(), subnet.last_address()) {}

References IPAddressOrRange().

Member Function Documentation

◆ as_subnet()

template<Version V>
auto Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::as_subnet ( ) const

If this range covers exactly the addresses of some subnet, return it. Otherwise return nullopt.

Definition at line 2555 of file x509_ext.cpp.

2555 {
2556 if(auto prefix_len = range_prefix_length<V>(m_min, m_max)) {
2557 return Subnet(m_min, *prefix_len);
2558 }
2559 return std::nullopt;
2560}

◆ BER_encode()

std::vector< uint8_t > Botan::ASN1_Object::BER_encode ( ) const
inherited

Return the encoding of this object. This is a convenience method when just one object needs to be serialized. Use DER_Encoder for complicated encodings.

Definition at line 21 of file asn1_obj.cpp.

21 {
22 std::vector<uint8_t> output;
23 DER_Encoder der(output);
24 this->encode_into(der);
25 return output;
26}
virtual void encode_into(DER_Encoder &to) const =0

References encode_into().

Referenced by decode_from(), Botan::PKCS12::export_to(), Botan::Certificate_Store_In_SQL::find_all_certs(), Botan::Certificate_Store_In_SQL::find_cert(), Botan::X509_Certificate::fingerprint(), Botan::Certificate_Store_In_SQL::insert_cert(), Botan::X509_Object::PEM_encode(), and Botan::PSS_Params::PSS_Params().

◆ decode_from()

template<Version V>
void Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::decode_from ( BER_Decoder & from)
overridevirtual

Decode whatever this object is from from

Parameters
fromthe BER_Decoder that will be read from

Implements Botan::ASN1_Object.

Definition at line 2465 of file x509_ext.cpp.

2465 {
2466 const ASN1_Type next_tag = from.peek_next_object().type_tag();
2467
2468 // this can either be a prefix or a single address
2470 // construct a min and a max address from the prefix
2471
2473 from.decode_bitstring(prefix);
2474
2475 // min address gets filled with 0's
2476 m_min = decode_single_address(prefix, true);
2477 // max address with 1's
2478 m_max = decode_single_address(prefix, false);
2479 } else if(next_tag == ASN1_Type::Sequence) {
2480 // this is a range
2481
2484
2485 from.start_sequence().decode_bitstring(addr_min).decode_bitstring(addr_max).end_cons();
2486
2487 /* RFC 3779 Section 2.2.3.9 -
2488 *
2489 * The BIT STRING for the minimum address results from removing all the
2490 * least-significant zero-bits from the minimum address. The BIT STRING
2491 * for the maximum address results from removing all the least-significant
2492 * one-bits from the maximum address.
2493 *
2494 * So a canonically encoded minimum ends in a one bit (or is empty) and
2495 * a canonically encoded maximum ends in a zero bit (or is empty).
2496 * Section 2.2.3.9 also requires that a maximum "MUST contain at least
2497 * one bit whose value is 1", but verified erratum 2537 removes that
2498 * sentence as it contradicts the removal rule, so it is not checked.
2499 */
2500 if(!addr_min.bytes().empty() && ((addr_min.bytes().back() >> addr_min.unused_bits()) & 1) == 0) {
2501 throw Decoding_Error("IPAddressRange minimum must have trailing zero bits removed");
2502 }
2503 if(!addr_max.bytes().empty() && ((addr_max.bytes().back() >> addr_max.unused_bits()) & 1) == 1) {
2504 throw Decoding_Error("IPAddressRange maximum must have trailing one bits removed");
2505 }
2506
2507 m_min = decode_single_address(addr_min, true);
2508 m_max = decode_single_address(addr_max, false);
2509
2510 if(m_min > m_max) {
2511 throw Decoding_Error("IP address ranges must be sorted.");
2512 }
2513
2514 /* RFC 3779 Section 2.2.3.7 -
2515 *
2516 * This specification requires that any range of addresses that can be
2517 * encoded as a prefix MUST be encoded using an IPAddress element (a BIT
2518 * STRING), and any range that cannot be encoded as a prefix MUST be
2519 * encoded using an IPAddressRange
2520 */
2521 if(range_is_expressible_as_prefix<V>(m_min, m_max)) {
2522 throw Decoding_Error("IPAddressRange expressible as a prefix must be encoded as a prefix");
2523 }
2524 } else {
2525 throw Decoding_Error(fmt("Unexpected type for IPAddressOrRange {}", static_cast<uint32_t>(next_tag)));
2526 }
2527}
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::BitString, Botan::ASN1_BitString::bytes(), Botan::BER_Decoder::decode_bitstring(), Botan::BER_Decoder::end_cons(), Botan::fmt(), Botan::BER_Decoder::peek_next_object(), Botan::Sequence, Botan::BER_Decoder::start_sequence(), Botan::BER_Object::type_tag(), and Botan::ASN1_BitString::unused_bits().

◆ encode_into()

template<Version V>
void Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::encode_into ( DER_Encoder & to) const
overridevirtual

Encode whatever this object is into to

Parameters
tothe DER_Encoder that will be written to

Implements Botan::ASN1_Object.

Definition at line 2366 of file x509_ext.cpp.

2366 {
2367 // Compress IPAddressOrRange as much as possible
2368 // cf. https://www.rfc-editor.org/rfc/rfc3779.html#section-2.2.3.7 - https://www.rfc-editor.org/rfc/rfc3779.html#section-2.2.3.9
2369 //
2370 // If possible encode as a prefix x.x.x.x/x, else encode as a range of min-max.
2371 // Single addresses are encoded as is (technically a /32 or /128 prefix).
2372 //
2373 // A range can be encoded as a prefix if the lowest n bits of the min address are 0
2374 // and the highest n bits of the max address are 1, or in other words, contiguous sequences of 0s and 1s are omitted.
2375 // To make reconstruction possible, an 'unused' octet is included at the start, since in the case of e.g. /25 only
2376 // the highest bit of the last octet is actually meaningful.
2377 //
2378 // If encoding requires a range, the individual elements can still be compressed using the above method,
2379 // but the number of used bits varies between them.
2380
2381 const size_t version_octets = static_cast<size_t>(V);
2382
2383 std::array<uint8_t, version_octets> min = m_min.to_bytes();
2384 std::array<uint8_t, version_octets> max = m_max.to_bytes();
2385
2386 uint8_t zeros = 0;
2387 uint8_t ones = 0;
2388
2389 bool zeros_done = false;
2390 bool ones_done = false;
2391
2392 // count contiguous 0s/1s from the right of the min/max addresses
2393 for(size_t i = version_octets; i > 0; i--) {
2394 if(!zeros_done) {
2395 const uint8_t local_zeros = static_cast<uint8_t>(std::countr_zero(min[i - 1]));
2396 zeros += local_zeros;
2397 zeros_done = (local_zeros != 8);
2398 }
2399
2400 if(!ones_done) {
2401 const uint8_t local_ones = static_cast<uint8_t>(std::countr_one(max[i - 1]));
2402 ones += local_ones;
2403 ones_done = (local_ones != 8);
2404 }
2405
2406 if(zeros_done && ones_done) {
2407 break;
2408 }
2409 }
2410
2411 // the part we want to compress
2412 const uint8_t host = std::min(zeros, ones);
2413
2414 // these we can outright drop
2415 const uint8_t discarded_octets = host / 8;
2416 // in a partially used octet
2417 const uint8_t unused_bits = host % 8;
2418
2419 bool octets_match = true;
2420 bool used_bits_match = true;
2421
2422 // we have octets to check
2424 // check all but the last octet
2425 for(size_t i = 0; i < static_cast<uint8_t>(version_octets - discarded_octets - 1); i++) {
2426 if(min[i] != max[i]) {
2427 octets_match = false;
2428 break;
2429 }
2430 }
2431 // check the last significant octet if we have matched so far
2432 if(octets_match) {
2436 }
2437 }
2438
2439 // both the full octets and the partially used one match
2441 // at this point the range can be encoded as a prefix
2442 into.encode_bitstring(std::span{min}.first(version_octets - discarded_octets), unused_bits);
2443 } else {
2445 const uint8_t unused_bits_min = zeros % 8;
2446
2447 const uint8_t discarded_octets_max = ones / 8;
2448 const uint8_t unused_bits_max = ones % 8;
2449
2450 // compress the max address by setting unused bits to 0, for the min address these are already 0
2451 if(unused_bits_max != 0) {
2455 }
2456
2457 into.start_sequence()
2460 .end_cons();
2461 }
2462}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82

References BOTAN_ASSERT_NOMSG, Botan::DER_Encoder::encode_bitstring(), Botan::DER_Encoder::end_cons(), max(), min(), and Botan::DER_Encoder::start_sequence().

◆ max()

template<Version V>
IPAddress< V > Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::max ( ) const
inline

Definition at line 1145 of file x509_ext.h.

1145{ return m_max; }

Referenced by encode_into(), IPAddressOrRange(), and IPAddressOrRange().

◆ min()

template<Version V>
IPAddress< V > Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::min ( ) const
inline

Definition at line 1143 of file x509_ext.h.

1143{ return m_min; }

Referenced by encode_into(), IPAddressOrRange(), and IPAddressOrRange().


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