Botan 3.14.1
Crypto and TLS for C&
Botan::GeneralName Class Referencefinal

X.509 GeneralName Type. More...

#include <pkix_types.h>

Inheritance diagram for Botan::GeneralName:
Botan::ASN1_Object

Public Types

enum  MatchResult : uint8_t {
  All , Some , None , NotFound ,
  UnknownType
}
enum class  NameType : uint8_t {
  Unknown = 0 , RFC822 = 1 , DNS = 2 , URI = 3 ,
  DN = 4 , IPv4 = 5 , IPv6 = 6 , Other = 7
}

Public Member Functions

std::vector< uint8_t > BER_encode () const
std::vector< uint8_t > binary_name () const
void decode_from (BER_Decoder &from) override
void encode_into (DER_Encoder &to) const override
 GeneralName ()=default
MatchResult matches (const X509_Certificate &cert) const
bool matches_dn (const X509_DN &dn) const
bool matches_dns (const DNSName &dns_name) const
bool matches_dns (const std::string &dns_name) const
bool matches_email (const EmailAddress &addr) const
bool matches_email (const SmtpUtf8Mailbox &mailbox) const
bool matches_ipv4 (const IPv4Address &ip) const
bool matches_ipv4 (uint32_t ip) const
bool matches_ipv6 (const IPv6Address &ip) const
bool matches_uri (const URI &uri) const
std::string name () const
std::string type () const
NameType type_code () const

Static Public Member Functions

static GeneralName _dns_san_value (std::string_view dns)
static GeneralName _uri_san_value (std::string_view full_uri)
static GeneralName directory_name (Botan::X509_DN dn)
static GeneralName dns (std::string_view dns)
static GeneralName email (std::string_view email)
static GeneralName ipv4_address (const IPv4Subnet &subnet)
static GeneralName ipv4_address (IPv4Address ipv4)
static GeneralName ipv4_address (uint32_t ipv4)
static GeneralName ipv4_address (uint32_t ipv4, uint32_t mask)
static GeneralName ipv6_address (const IPv6Address &ipv6)
static GeneralName ipv6_address (const IPv6Subnet &subnet)
static GeneralName uri (std::string_view uri)

Friends

class NameConstraints

Detailed Description

X.509 GeneralName Type.

Handles parsing GeneralName types in their BER and canonical string encoding. Allows matching GeneralNames against each other using the rules laid out in the RFC 5280, sec. 4.2.1.10 (Name Constraints).

This entire class is deprecated and will be removed in a future major release

Definition at line 541 of file pkix_types.h.

Member Enumeration Documentation

◆ MatchResult

Enumerator
All 
Some 
None 
NotFound 
UnknownType 

Definition at line 543 of file pkix_types.h.

543 : uint8_t /* NOLINT(*-use-enum-class) */ {
544 All,
545 Some,
546 None,
547 NotFound,
549 };

◆ NameType

enum class Botan::GeneralName::NameType : uint8_t
strong
Enumerator
Unknown 
RFC822 
DNS 
URI 
DN 
IPv4 
IPv6 
Other 

Definition at line 551 of file pkix_types.h.

551 : uint8_t {
552 Unknown = 0,
553 RFC822 = 1,
554 DNS = 2,
555 URI = 3,
556 DN = 4,
557 IPv4 = 5,
558 IPv6 = 6,
559 Other = 7,
560 };

Constructor & Destructor Documentation

◆ GeneralName()

Member Function Documentation

◆ _dns_san_value()

GeneralName Botan::GeneralName::_dns_san_value ( std::string_view dns)
static

Wrap a DNS SAN in a GeneralName, this is used for ffi

Warning
internal function that may be removed at any time

Definition at line 344 of file name_constraint.cpp.

344 {
345 if(auto dns = DNSConstraint::from_san_value(dns_name)) {
346 return {NameType::DNS, std::move(*dns)};
347 } else {
348 throw Invalid_Argument(fmt("Invalid DNS SAN value '{}'", dns_name));
349 }
350}
static GeneralName dns(std::string_view dns)
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References DNS, dns(), Botan::fmt(), and GeneralName().

Referenced by GeneralName().

◆ _uri_san_value()

GeneralName Botan::GeneralName::_uri_san_value ( std::string_view full_uri)
static

Wrap a URI SAN in a GeneralName, this is used for ffi

Warning
internal function that may be removed at any time

Definition at line 336 of file name_constraint.cpp.

336 {
337 if(auto uri = URIConstraint::from_san_value(full_uri)) {
338 return {NameType::URI, std::move(*uri)};
339 } else {
340 throw Invalid_Argument(fmt("Invalid URI SAN value '{}'", full_uri));
341 }
342}
static GeneralName uri(std::string_view uri)

References Botan::fmt(), GeneralName(), URI, and uri().

Referenced by GeneralName().

◆ 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().

◆ binary_name()

std::vector< uint8_t > Botan::GeneralName::binary_name ( ) const
Returns
The name as binary string. Format depends on type.

Definition at line 397 of file name_constraint.cpp.

397 {
398 return std::visit(Botan::overloaded{
399 [](const Botan::X509_DN& dn) { return Botan::ASN1::put_in_sequence(dn.get_bits()); },
400 [](const IPv4Subnet& subnet) { return subnet.serialize(); },
401 [](const IPv6Subnet& subnet) { return subnet.serialize(); },
402 [](const auto&) -> std::vector<uint8_t> {
403 throw Invalid_State("Cannot convert GeneralName to binary string");
404 },
405 },
406 m_name);
407}
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition asn1_obj.cpp:208
overloaded(Ts...) -> overloaded< Ts... >

References Botan::ASN1::put_in_sequence().

Referenced by botan_x509_general_name_view_binary_value().

◆ decode_from()

void Botan::GeneralName::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 467 of file name_constraint.cpp.

467 {
468 const BER_Object obj = ber.get_next_object();
469
470 if(obj.is_a(0, ASN1_Class::ExplicitContextSpecific)) {
471 m_type = NameType::Other;
472 } else if(obj.is_a(1, ASN1_Class::ContextSpecific)) {
473 /*
474 RFC 5280 4.2.1.10:
475 A name constraint for Internet mail addresses MAY specify a
476 particular mailbox, all addresses at a particular host, or all
477 mailboxes in a domain.
478 EmailConstraint::from_string validates and canonicalizes per the
479 Section 7.5 matching rules.
480 */
481 auto constraint = EmailConstraint::from_string(ASN1::to_string(obj));
482 if(!constraint.has_value()) {
483 throw Decoding_Error("Malformed RFC822 name in GeneralName");
484 }
485 m_type = NameType::RFC822;
486 m_name = std::move(*constraint);
487 } else if(obj.is_a(2, ASN1_Class::ContextSpecific)) {
488 auto constraint = DNSConstraint::from_string(ASN1::to_string(obj));
489 if(!constraint.has_value()) {
490 throw Decoding_Error("Malformed DNS name in GeneralName");
491 }
492 m_type = NameType::DNS;
493 m_name = std::move(*constraint);
494 } else if(obj.is_a(6, ASN1_Class::ContextSpecific)) {
495 /*
496 RFC 5280 4.2.1.10:
497 For URIs, the constraint applies to the host part of the name.
498 The constraint MUST be specified as a fully qualified domain
499 name and MAY specify a host or a domain. Examples would be
500 "host.example.com" and ".example.com".
501 */
502 auto constraint = URIConstraint::from_string(ASN1::to_string(obj));
503 if(!constraint.has_value()) {
504 throw Decoding_Error("Malformed URI name in GeneralName");
505 }
506 m_type = NameType::URI;
507 m_name = std::move(*constraint);
508 } else if(obj.is_a(4, ASN1_Class::ContextSpecific | ASN1_Class::Constructed)) {
509 X509_DN dn;
510 BER_Decoder dec(obj, ber.limits());
511 dn.decode_from(dec);
512 dec.verify_end();
513 m_type = NameType::DN;
514 m_name.emplace<X509_DN>(dn);
515 } else if(obj.is_a(7, ASN1_Class::ContextSpecific)) {
516 if(obj.length() == 8) {
517 const auto addr_and_mask = std::span<const uint8_t, 8>{obj.bits(), 8};
518 auto subnet = IPv4Subnet::from_address_and_mask(addr_and_mask);
519 if(!subnet.has_value()) {
520 throw Decoding_Error("IPv4 name constraint mask is not a contiguous CIDR prefix");
521 }
522
523 m_type = NameType::IPv4;
524 m_name.emplace<IPv4Subnet>(*subnet);
525 } else if(obj.length() == 32) {
526 const auto addr_and_mask = std::span<const uint8_t, 32>{obj.bits(), 32};
527 auto subnet = IPv6Subnet::from_address_and_mask(addr_and_mask);
528 if(!subnet.has_value()) {
529 throw Decoding_Error("IPv6 name constraint mask is not a contiguous CIDR prefix");
530 }
531
532 m_type = NameType::IPv6;
533 m_name.emplace<IPv6Subnet>(*subnet);
534 } else {
535 throw Decoding_Error("Invalid IP name constraint size " + std::to_string(obj.length()));
536 }
537 } else {
538 m_type = NameType::Unknown;
539 }
540}
static std::optional< IPv4Subnet > from_address_and_mask(std::span< const uint8_t, 8 > addr_and_mask)
static std::optional< IPv6Subnet > from_address_and_mask(std::span< const uint8_t, 32 > addr_and_mask)
std::string to_string(const BER_Object &obj)
Definition asn1_obj.cpp:224

References Botan::BER_Object::bits(), Botan::Constructed, Botan::ContextSpecific, Botan::X509_DN::decode_from(), DN, DNS, Botan::ExplicitContextSpecific, Botan::IPv4Subnet::from_address_and_mask(), Botan::IPv6Subnet::from_address_and_mask(), Botan::BER_Decoder::get_next_object(), IPv4, IPv6, Botan::BER_Object::is_a(), Botan::BER_Object::length(), Botan::BER_Decoder::limits(), Other, RFC822, Botan::ASN1::to_string(), Unknown, URI, and Botan::BER_Decoder::verify_end().

Referenced by GeneralName().

◆ directory_name()

GeneralName Botan::GeneralName::directory_name ( Botan::X509_DN dn)
static

Definition at line 352 of file name_constraint.cpp.

352 {
353 return {NameType::DN, std::move(dn)};
354}

References DN, and GeneralName().

Referenced by GeneralName().

◆ dns()

GeneralName Botan::GeneralName::dns ( std::string_view dns)
static

Definition at line 320 of file name_constraint.cpp.

320 {
321 if(auto constraint = DNSConstraint::from_string(dns)) {
322 return {NameType::DNS, std::move(*constraint)};
323 } else {
324 throw Invalid_Argument(fmt("Invalid DNS name constraint '{}'", dns));
325 }
326}

References DNS, dns(), Botan::fmt(), and GeneralName().

Referenced by _dns_san_value(), dns(), GeneralName(), and matches().

◆ email()

GeneralName Botan::GeneralName::email ( std::string_view email)
static

Definition at line 312 of file name_constraint.cpp.

312 {
313 if(auto constraint = EmailConstraint::from_string(email)) {
314 return {NameType::RFC822, std::move(*constraint)};
315 } else {
316 throw Invalid_Argument(fmt("Invalid RFC822 name constraint '{}'", email));
317 }
318}
static GeneralName email(std::string_view email)

References email(), Botan::fmt(), GeneralName(), and RFC822.

Referenced by email(), and GeneralName().

◆ encode_into()

void Botan::GeneralName::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 409 of file name_constraint.cpp.

409 {
410 /*
411 GeneralName ::= CHOICE {
412 otherName [0] OtherName,
413 rfc822Name [1] IA5String,
414 dNSName [2] IA5String,
415 x400Address [3] ORAddress,
416 directoryName [4] Name,
417 ediPartyName [5] EDIPartyName,
418 uniformResourceIdentifier [6] IA5String,
419 iPAddress [7] OCTET STRING,
420 registeredID [8] OBJECT IDENTIFIER }
421 */
422 const auto emit_ia5_implicit = [&](uint32_t tag, std::string_view value) {
423 const ASN1_String str(value, ASN1_Type::Ia5String);
424 to.add_object(ASN1_Type(tag), ASN1_Class::ContextSpecific, str.value());
425 };
426
427 switch(m_type) {
428 case NameType::RFC822:
429 emit_ia5_implicit(1, std::get<EmailConstraint>(m_name).value());
430 return;
431 case NameType::DNS:
432 emit_ia5_implicit(2, std::get<DNSConstraint>(m_name).value());
433 return;
434 case NameType::URI:
435 emit_ia5_implicit(6, std::get<URIConstraint>(m_name).value());
436 return;
437 case NameType::DN:
438 to.add_object(ASN1_Type(4), ASN1_Class::ExplicitContextSpecific, std::get<X509_DN>(m_name).DER_encode());
439 return;
440 case NameType::IPv4: {
441 // In a name constraint the iPAddress is always address followed by mask,
442 // even for a single host (unlike the SAN form)
443 const auto& subnet = std::get<IPv4Subnet>(m_name);
444 const auto addr_and_mask =
445 concat(subnet.address().to_bytes(), IPv4Address::netmask(subnet.prefix_length()).to_bytes());
446 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
447 to.add_object(ASN1_Type(7), ASN1_Class::ContextSpecific, addr_and_mask);
448 return;
449 }
450 case NameType::IPv6: {
451 const auto& subnet = std::get<IPv6Subnet>(m_name);
452 const auto addr_and_mask =
453 concat(subnet.address().address(), IPv6Address::netmask(subnet.prefix_length()).address());
454 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
455 to.add_object(ASN1_Type(7), ASN1_Class::ContextSpecific, addr_and_mask);
456 return;
457 }
458 case NameType::Other:
460 // Decoding retains only the type tag for these forms, not the value
461 break;
462 }
463
464 throw Encoding_Error("Cannot encode GeneralName of Other or Unknown type");
465}
std::array< uint8_t, 4 > to_bytes() const
The address as four bytes, network-byte-order.
static IPv4Address netmask(size_t bits)
std::array< uint8_t, 16 > address() const
static IPv6Address netmask(size_t bits)
ASN1_Type
Definition asn1_obj.h:47
constexpr auto concat(Rs &&... ranges)
Definition concat_util.h:95

References Botan::DER_Encoder::add_object(), Botan::concat(), Botan::ContextSpecific, DN, DNS, Botan::ExplicitContextSpecific, Botan::Ia5String, IPv4, IPv6, Botan::IPv4Address::netmask(), Botan::IPv6Address::netmask(), Other, RFC822, Unknown, URI, and Botan::ASN1_String::value().

Referenced by GeneralName().

◆ ipv4_address() [1/4]

GeneralName Botan::GeneralName::ipv4_address ( const IPv4Subnet & subnet)
static

Definition at line 372 of file name_constraint.cpp.

372 {
373 return {NameType::IPv4, subnet};
374}

References GeneralName(), and IPv4.

◆ ipv4_address() [2/4]

GeneralName Botan::GeneralName::ipv4_address ( IPv4Address ipv4)
static

Definition at line 368 of file name_constraint.cpp.

368 {
369 return {NameType::IPv4, IPv4Subnet::host(ipv4)};
370}
static IPv4Subnet host(IPv4Address address)

References GeneralName(), Botan::IPv4Subnet::host(), and IPv4.

◆ ipv4_address() [3/4]

GeneralName Botan::GeneralName::ipv4_address ( uint32_t ipv4)
static

Definition at line 356 of file name_constraint.cpp.

356 {
357 return GeneralName::ipv4_address(IPv4Address(ipv4));
358}
static GeneralName ipv4_address(uint32_t ipv4)

References GeneralName(), and ipv4_address().

Referenced by GeneralName(), and ipv4_address().

◆ ipv4_address() [4/4]

GeneralName Botan::GeneralName::ipv4_address ( uint32_t ipv4,
uint32_t mask )
static

Definition at line 360 of file name_constraint.cpp.

360 {
361 if(auto subnet = IPv4Subnet::from_address_and_mask(ipv4, mask)) {
362 return {NameType::IPv4, *subnet};
363 } else {
364 throw Invalid_Argument("IPv4 subnet mask is not a contiguous CIDR prefix");
365 }
366}

References Botan::IPv4Subnet::from_address_and_mask(), GeneralName(), and IPv4.

◆ ipv6_address() [1/2]

GeneralName Botan::GeneralName::ipv6_address ( const IPv6Address & ipv6)
static

Definition at line 376 of file name_constraint.cpp.

376 {
377 return {NameType::IPv6, IPv6Subnet::host(ipv6)};
378}
static IPv6Subnet host(IPv6Address address)

References GeneralName(), Botan::IPv6Subnet::host(), and IPv6.

Referenced by GeneralName().

◆ ipv6_address() [2/2]

GeneralName Botan::GeneralName::ipv6_address ( const IPv6Subnet & subnet)
static

Definition at line 380 of file name_constraint.cpp.

380 {
381 return {NameType::IPv6, subnet};
382}

References GeneralName(), and IPv6.

◆ matches()

GeneralName::MatchResult Botan::GeneralName::matches ( const X509_Certificate & cert) const

Checks whether a given certificate (partially) matches this name.

Parameters
certcertificate to be matched
Returns
the match result

Definition at line 671 of file name_constraint.cpp.

671 {
672 class MatchScore final {
673 public:
674 MatchScore() : m_any(false), m_some(false), m_all(true) {}
675
676 void add(bool m) {
677 m_any = true;
678 m_some |= m;
679 m_all &= m;
680 }
681
682 MatchResult result() const {
683 if(!m_any) {
684 return MatchResult::NotFound;
685 } else if(m_all) {
686 return MatchResult::All;
687 } else if(m_some) {
688 return MatchResult::Some;
689 } else {
690 return MatchResult::None;
691 }
692 }
693
694 private:
695 bool m_any;
696 bool m_some;
697 bool m_all;
698 };
699
700 const X509_DN& dn = cert.subject_dn();
701 const AlternativeName& alt_name = cert.subject_alt_name();
702
703 MatchScore score;
704
705 if(m_type == NameType::DNS) {
706 const auto& constraint = std::get<DNSConstraint>(m_name).value();
707
708 for(const auto& dns : alt_name.dns_names()) {
709 score.add(dns_subtree_match(dns.to_string(), constraint));
710 }
711
712 if(alt_name.is_empty()) {
713 // TODO(Botan4): CN fallback is deprecated for removal in Botan4.
714 // Check CN instead...
715 for(const std::string& cn : dn.get_attribute("CN")) {
716 if(cn.find('.') == std::string::npos) {
717 continue;
718 }
719 if(IPv4Address::from_string(cn).has_value()) {
720 continue;
721 }
722 if(auto dns_form = DNSName::from_san_string(cn)) {
723 score.add(dns_subtree_match(dns_form->to_string(), constraint));
724 }
725 }
726 }
727 } else if(m_type == NameType::DN) {
728 const X509_DN& constraint = std::get<X509_DN>(m_name);
729 score.add(matches_dn(dn, constraint));
730
731 for(const auto& alt_dn : alt_name.directory_names()) {
732 score.add(matches_dn(alt_dn, constraint));
733 }
734 } else if(m_type == NameType::IPv4) {
735 const auto& subnet = std::get<IPv4Subnet>(m_name);
736
737 if(alt_name.is_empty()) {
738 // TODO(Botan4): CN fallback is deprecated for removal in Botan4.
739 // Check CN instead...
740 for(const std::string& cn : dn.get_attribute("CN")) {
741 if(auto ipv4 = IPv4Address::from_string(cn)) {
742 score.add(subnet.contains(*ipv4));
743 }
744 }
745 } else {
746 for(const auto& ipv4 : alt_name.ipv4_addresses()) {
747 score.add(subnet.contains(ipv4));
748 }
749 }
750 } else if(m_type == NameType::IPv6) {
751 for(const auto& ipv6 : alt_name.ipv6_addresses()) {
752 score.add(matches_ipv6(ipv6));
753 }
754 } else if(m_type == NameType::URI) {
755 for(const auto& uri : alt_name.uri_names()) {
756 score.add(matches_uri(uri));
757 }
758 } else if(m_type == NameType::RFC822) {
759 for(const auto& addr : alt_name.email_addresses()) {
760 score.add(matches_email(addr));
761 }
762 } else {
763 // Only NameType::Other (and the sentinel Unknown) remain; those
764 // cannot be matched without per-OID semantics.
766 }
767
768 return score.result();
769}
static std::optional< DNSName > from_san_string(std::string_view name)
Definition dns_name.cpp:147
bool matches_ipv6(const IPv6Address &ip) const
bool matches_uri(const URI &uri) const
bool matches_dn(const X509_DN &dn) const
bool matches_email(const EmailAddress &addr) const
static std::optional< IPv4Address > from_string(std::string_view str)

References All, Botan::AlternativeName::directory_names(), DN, DNS, dns(), Botan::AlternativeName::dns_names(), Botan::AlternativeName::email_addresses(), Botan::DNSName::from_san_string(), Botan::IPv4Address::from_string(), Botan::X509_DN::get_attribute(), IPv4, Botan::AlternativeName::ipv4_addresses(), IPv6, Botan::AlternativeName::ipv6_addresses(), Botan::AlternativeName::is_empty(), matches_dn(), matches_email(), matches_ipv6(), matches_uri(), None, NotFound, RFC822, Some, Botan::X509_Certificate::subject_alt_name(), Botan::X509_Certificate::subject_dn(), UnknownType, URI, uri(), and Botan::AlternativeName::uri_names().

◆ matches_dn()

bool Botan::GeneralName::matches_dn ( const X509_DN & dn) const

Definition at line 570 of file name_constraint.cpp.

570 {
571 if(m_type == NameType::DN) {
572 return matches_dn(dn, std::get<X509_DN>(m_name));
573 }
574 return false;
575}

References DN, and matches_dn().

Referenced by matches(), and matches_dn().

◆ matches_dns() [1/2]

bool Botan::GeneralName::matches_dns ( const DNSName & dns_name) const

Definition at line 549 of file name_constraint.cpp.

549 {
550 if(m_type == NameType::DNS) {
551 return dns_subtree_match(dns_name.to_string(), std::get<DNSConstraint>(m_name).value());
552 }
553 return false;
554}

References DNS, and Botan::DNSName::to_string().

◆ matches_dns() [2/2]

bool Botan::GeneralName::matches_dns ( const std::string & dns_name) const

Definition at line 542 of file name_constraint.cpp.

542 {
543 if(m_type == NameType::DNS) {
544 return dns_subtree_match(dns_name, std::get<DNSConstraint>(m_name).value());
545 }
546 return false;
547}

References DNS.

◆ matches_email() [1/2]

bool Botan::GeneralName::matches_email ( const EmailAddress & addr) const

Definition at line 607 of file name_constraint.cpp.

607 {
608 if(m_type != NameType::RFC822) {
609 return false;
610 }
611 return email_subtree_match(addr, std::get<EmailConstraint>(m_name).value());
612}

References RFC822.

Referenced by matches().

◆ matches_email() [2/2]

bool Botan::GeneralName::matches_email ( const SmtpUtf8Mailbox & mailbox) const

Definition at line 614 of file name_constraint.cpp.

614 {
615 if(m_type != NameType::RFC822) {
616 return false;
617 }
618 /*
619 RFC 9598 Section 6:
620 Setup converts the inputs of the comparison ... to constraint
621 comparison form. For both the name constraint and the subject,
622 this will convert all A-labels and NR-LDH labels to lowercase.
623 Strip the Local-part and "@" separator from each rfc822Name and
624 SmtpUTF8Mailbox, which leaves just the domain part. After setup,
625 follow the comparison steps defined in Section 4.2.1.10 of
626 [RFC5280] as follows. If the resulting name constraint domain
627 starts with a "." character, then for the name constraint to
628 match, a suffix of the resulting subject alternative name domain
629 MUST match the name constraint (including the leading ".") octet
630 for octet. If the resulting name constraint domain does not
631 start with a "." character, then for the name constraint to
632 match, the entire resulting subject alternative name domain MUST
633 match the name constraint octet for octet.
634
635 Per RFC 9598 Section 3 the SmtpUTF8Mailbox domain is already A-label /
636 NR-LDH and lowercase by construction (DNSName::from_string enforces
637 LDH + lowercase). The rfc822Name constraint flows through the same
638 DNSName validation. So octet-for-octet comparison is the correct
639 algorithm with no IDNA conversion required.
640 */
641 const std::string& candidate_domain = mailbox.domain().to_string();
642 const std::string& constraint = std::get<EmailConstraint>(m_name).value();
643 if(constraint.find('@') != std::string::npos) {
644 /*
645 * The situation with SmtpUTF8Mailbox mailbox constraints (with '@') is a bit confused.
646 *
647 * RFC 9549 updates RFC 5280 to completely drop support for mailbox constraints.
648 * Then RFC 9598 Section 6 (relevant section quoted above) defines a mechanism to
649 * apply rfc822 mailbox name constraints to SmtpUTF8Mailbox, but it does so in a
650 * completely insecure way, namely by stripping off the local-part and comparing just
651 * the domains. Under these rules, if an intermediate certificate had a permittedSubtrees
652 * containing alice@example.com then a leaf certificate could have a SmtpUTF8Mailbox
653 * containing bob@example.com, and per RFC 9598 that's fine because we are supposed
654 * to just check the domains.
655 *
656 * This is obviously nonsense. Here we return false, which ensures that
657 * is_permitted_smtp_utf8 never accepts on a mailbox constraint. In is_excluded_smtp_utf8
658 * we first call matches_email then additionally (for mailbox constraints) reject any
659 * matching domain using the additional check in mailbox_form_constraint_covers_domain.
660 */
661 return false;
662 }
663 if(!constraint.empty() && constraint.front() == '.') {
664 // Leading-dot subtree form: suffix match including the dot.
665 return candidate_domain.ends_with(constraint);
666 }
667 // Host form: exact match on the domain.
668 return candidate_domain == constraint;
669}

References Botan::SmtpUtf8Mailbox::domain(), RFC822, and Botan::DNSName::to_string().

◆ matches_ipv4() [1/2]

bool Botan::GeneralName::matches_ipv4 ( const IPv4Address & ip) const
inline

Definition at line 623 of file pkix_types.h.

623{ return matches_ipv4(ip.address()); }
bool matches_ipv4(uint32_t ip) const

References matches_ipv4(), and matches_ipv4().

Referenced by matches_ipv4().

◆ matches_ipv4() [2/2]

bool Botan::GeneralName::matches_ipv4 ( uint32_t ip) const

Definition at line 556 of file name_constraint.cpp.

556 {
557 if(m_type == NameType::IPv4) {
558 return std::get<IPv4Subnet>(m_name).contains(IPv4Address(ip));
559 }
560 return false;
561}

References IPv4.

Referenced by matches_ipv4().

◆ matches_ipv6()

bool Botan::GeneralName::matches_ipv6 ( const IPv6Address & ip) const

Definition at line 563 of file name_constraint.cpp.

563 {
564 if(m_type == NameType::IPv6) {
565 return std::get<IPv6Subnet>(m_name).contains(ip);
566 }
567 return false;
568}

References IPv6.

Referenced by matches().

◆ matches_uri()

bool Botan::GeneralName::matches_uri ( const URI & uri) const

Definition at line 577 of file name_constraint.cpp.

577 {
578 if(m_type != NameType::URI) {
579 return false;
580 }
581 // RFC 5280 4.2.1.10 does not provide for applying a DNS-form URI
582 // constraint to an IP-literal host.
583 const auto host = uri.host();
584 if(!host.has_value() || !std::holds_alternative<DNSName>(host->get())) {
585 return false;
586 }
587 const std::string& dns_host = std::get<DNSName>(host->get()).to_string();
588 const std::string& constraint = std::get<URIConstraint>(m_name).value();
589 /*
590 RFC 5280 4.2.1.10:
591 When the constraint begins with a period, it MAY be expanded with
592 one or more labels. That is, the constraint ".example.com" is
593 satisfied by both host.example.com and my.host.example.com.
594 However, the constraint ".example.com" is not satisfied by
595 "example.com". When the constraint does not begin with a period,
596 it specifies a host.
597
598 So a bare-host URI constraint is exact-match only; subdomains don't
599 satisfy it. dns_subtree_match handles the leading-dot form correctly.
600 */
601 if(!constraint.empty() && constraint.front() == '.') {
602 return dns_subtree_match(dns_host, constraint);
603 }
604 return dns_host == constraint;
605}

References URI, and uri().

Referenced by matches().

◆ name()

std::string Botan::GeneralName::name ( ) const
Returns
The name as string. Format depends on type.

Definition at line 384 of file name_constraint.cpp.

384 {
385 return std::visit(
387 [](const EmailConstraint& c) -> std::string { return c.value(); },
388 [](const DNSConstraint& c) -> std::string { return c.value(); },
389 [](const URIConstraint& c) -> std::string { return c.value(); },
390 [](const X509_DN& dn) -> std::string { return dn.to_string(); },
391 [](const IPv4Subnet& s) -> std::string { return s.is_host() ? s.address().to_string() : s.to_string(); },
392 [](const IPv6Subnet& s) -> std::string { return s.is_host() ? s.address().to_string() : s.to_string(); },
393 },
394 m_name);
395}

Referenced by botan_x509_general_name_view_string_value(), and Botan::operator<<().

◆ type()

std::string Botan::GeneralName::type ( ) const
Returns
Type of the name. Can be DN, DNS, IP, RFC822 or URI.

Definition at line 289 of file name_constraint.cpp.

289 {
290 switch(m_type) {
292 throw Encoding_Error("Could not convert unknown NameType to string");
293 case NameType::RFC822:
294 return "RFC822";
295 case NameType::DNS:
296 return "DNS";
297 case NameType::URI:
298 return "URI";
299 case NameType::DN:
300 return "DN";
301 case NameType::IPv4:
302 return "IP";
303 case NameType::IPv6:
304 return "IPv6";
305 case NameType::Other:
306 return "Other";
307 }
308
310}
#define BOTAN_ASSERT_UNREACHABLE()
Definition assert.h:173

References BOTAN_ASSERT_UNREACHABLE, DN, DNS, IPv4, IPv6, Other, RFC822, Unknown, and URI.

Referenced by Botan::operator<<().

◆ type_code()

NameType Botan::GeneralName::type_code ( ) const
inline
Returns
Type of the name expressed in this restriction

Definition at line 594 of file pkix_types.h.

594{ return m_type; }

References type_code().

Referenced by botan_x509_general_name_get_type(), botan_x509_general_name_view_binary_value(), botan_x509_general_name_view_string_value(), Botan::NameConstraints::is_excluded(), and type_code().

◆ uri()

GeneralName Botan::GeneralName::uri ( std::string_view uri)
static

Definition at line 328 of file name_constraint.cpp.

328 {
329 if(auto constraint = URIConstraint::from_string(uri)) {
330 return {NameType::URI, std::move(*constraint)};
331 } else {
332 throw Invalid_Argument(fmt("Invalid URI name constraint '{}'", uri));
333 }
334}

References Botan::fmt(), GeneralName(), URI, and uri().

Referenced by _uri_san_value(), GeneralName(), matches(), matches_uri(), and uri().

◆ NameConstraints

friend class NameConstraints
friend

Definition at line 632 of file pkix_types.h.

References NameConstraints.

Referenced by NameConstraints.


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