9#include <botan/pkix_types.h>
11#include <botan/ber_dec.h>
12#include <botan/der_enc.h>
14#include <botan/x509cert.h>
15#include <botan/internal/concat_util.h>
16#include <botan/internal/fmt.h>
17#include <botan/internal/int_utils.h>
18#include <botan/internal/stl_util.h>
19#include <botan/internal/x509_utils.h>
26enum class RequireFQDN :
bool { Yes =
true, No =
false };
35std::optional<std::string> validate_subtree_constraint_host(std::string_view input, RequireFQDN require_fqdn) {
39 const bool subtree = input.starts_with(
'.');
40 const std::string_view body = subtree ? input.substr(1) : input;
42 if(!dns.has_value()) {
45 if(require_fqdn == RequireFQDN::Yes && dns->to_string().find(
'.') == std::string::npos) {
50 return std::string(
".") + dns->to_string();
52 return dns->to_string();
58std::optional<GeneralName::DNSConstraint> GeneralName::DNSConstraint::from_string(std::string_view input) {
61 if(
auto canonical = validate_subtree_constraint_host(input, RequireFQDN::No)) {
62 return DNSConstraint(std::move(*canonical));
68std::optional<GeneralName::DNSConstraint> GeneralName::DNSConstraint::from_san_value(std::string_view input) {
70 return DNSConstraint(parsed->to_string());
76std::optional<GeneralName::URIConstraint> GeneralName::URIConstraint::from_string(std::string_view input) {
83 if(
auto canonical = validate_subtree_constraint_host(input, RequireFQDN::Yes)) {
84 return URIConstraint(std::move(*canonical));
90std::optional<GeneralName::URIConstraint> GeneralName::URIConstraint::from_san_value(std::string_view full_uri) {
92 return URIConstraint(std::string(full_uri));
98std::optional<GeneralName::EmailConstraint> GeneralName::EmailConstraint::from_string(std::string_view input) {
102 if(input.find(
'@') != std::string_view::npos) {
105 if(!
email.has_value()) {
108 return EmailConstraint(
email->to_string());
110 if(
auto canonical = validate_subtree_constraint_host(input, RequireFQDN::No)) {
112 return EmailConstraint(std::move(*canonical));
124bool wildcard_label_matches(std::string_view pattern_label, std::string_view candidate) {
125 if(candidate.find(
'.') != std::string_view::npos) {
128 const auto star = pattern_label.find(
'*');
129 if(star == std::string_view::npos) {
130 return pattern_label == candidate;
132 const auto prefix = pattern_label.substr(0, star);
133 const auto suffix = pattern_label.substr(star + 1);
134 if(candidate.size() < prefix.size() + suffix.size()) {
137 return candidate.starts_with(prefix) && candidate.ends_with(suffix);
154 if(pattern.empty() || constraint.empty()) {
157 const bool subtree_form = (constraint.front() ==
'.');
158 const std::string_view c_base = subtree_form ? constraint.substr(1) : constraint;
163 const auto first_dot = pattern.find(
'.');
164 const std::string_view p_left = (first_dot == std::string_view::npos) ? pattern : pattern.substr(0, first_dot);
165 const std::string_view p_tail =
166 (first_dot == std::string_view::npos) ? std::string_view{} : pattern.substr(first_dot);
172 if(subtree_form || c_base.find(
'.') != std::string_view::npos) {
175 return wildcard_label_matches(p_left, c_base);
182 if(
auto suffix_len =
checked_add(c_base.size(),
size_t{1})) {
183 if(p_tail.size() >= *suffix_len) {
184 const auto tail_suffix = p_tail.substr(p_tail.size() - *suffix_len);
185 if(tail_suffix.front() ==
'.' && tail_suffix.substr(1) == c_base) {
194 if(!subtree_form && c_base.size() > p_tail.size() && c_base.substr(c_base.size() - p_tail.size()) == p_tail) {
195 const auto x_view = c_base.substr(0, c_base.size() - p_tail.size());
196 return wildcard_label_matches(p_left, x_view);
218bool dns_subtree_match(std::string_view
name, std::string_view constraint) {
222 if(
name.size() == constraint.size()) {
223 return name == constraint;
224 }
else if(constraint.size() >
name.size()) {
229 if(constraint.empty()) {
235 const std::string_view substr =
name.substr(
name.size() - constraint.size());
237 if(constraint.front() ==
'.') {
238 return substr == constraint;
240 return substr == constraint &&
name[
name.size() - constraint.size() - 1] ==
'.';
255bool email_subtree_match(
const EmailAddress& candidate, std::string_view c) {
267 const std::string& candidate_domain = candidate.domain().to_string();
268 const auto at = c.find(
'@');
269 if(at != std::string_view::npos) {
271 return (candidate.local_part() == c.substr(0, at)) && (candidate_domain == c.substr(at + 1));
273 if(!c.empty() && c.front() ==
'.') {
275 return dns_subtree_match(candidate_domain, c);
284 return candidate_domain == c;
292 throw Encoding_Error(
"Could not convert unknown NameType to string");
313 if(
auto constraint = EmailConstraint::from_string(
email)) {
321 if(
auto constraint = DNSConstraint::from_string(
dns)) {
329 if(
auto constraint = URIConstraint::from_string(
uri)) {
337 if(
auto uri = URIConstraint::from_san_value(full_uri)) {
345 if(
auto dns = DNSConstraint::from_san_value(dns_name)) {
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(); },
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");
422 const auto emit_ia5_implicit = [&](uint32_t tag, std::string_view value) {
429 emit_ia5_implicit(1, std::get<EmailConstraint>(m_name).value());
432 emit_ia5_implicit(2, std::get<DNSConstraint>(m_name).value());
435 emit_ia5_implicit(6, std::get<URIConstraint>(m_name).value());
443 const auto& subnet = std::get<IPv4Subnet>(m_name);
444 const auto addr_and_mask =
451 const auto& subnet = std::get<IPv6Subnet>(m_name);
452 const auto addr_and_mask =
464 throw Encoding_Error(
"Cannot encode GeneralName of Other or Unknown type");
482 if(!constraint.has_value()) {
486 m_name = std::move(*constraint);
489 if(!constraint.has_value()) {
493 m_name = std::move(*constraint);
503 if(!constraint.has_value()) {
507 m_name = std::move(*constraint);
517 const auto addr_and_mask = std::span<const uint8_t, 8>{obj.
bits(), 8};
519 if(!subnet.has_value()) {
520 throw Decoding_Error(
"IPv4 name constraint mask is not a contiguous CIDR prefix");
525 }
else if(obj.
length() == 32) {
526 const auto addr_and_mask = std::span<const uint8_t, 32>{obj.
bits(), 32};
528 if(!subnet.has_value()) {
529 throw Decoding_Error(
"IPv6 name constraint mask is not a contiguous CIDR prefix");
544 return dns_subtree_match(dns_name, std::get<DNSConstraint>(m_name).value());
551 return dns_subtree_match(dns_name.
to_string(), std::get<DNSConstraint>(m_name).value());
558 return std::get<IPv4Subnet>(m_name).contains(
IPv4Address(ip));
565 return std::get<IPv6Subnet>(m_name).contains(ip);
572 return matches_dn(dn, std::get<X509_DN>(m_name));
583 const auto host =
uri.host();
584 if(!host.has_value() || !std::holds_alternative<DNSName>(host->get())) {
587 const std::string& dns_host = std::get<DNSName>(host->get()).to_string();
588 const std::string& constraint = std::get<URIConstraint>(m_name).value();
601 if(!constraint.empty() && constraint.front() ==
'.') {
602 return dns_subtree_match(dns_host, constraint);
604 return dns_host == constraint;
611 return email_subtree_match(addr, std::get<EmailConstraint>(m_name).value());
642 const std::string& constraint = std::get<EmailConstraint>(m_name).value();
643 if(constraint.find(
'@') != std::string::npos) {
663 if(!constraint.empty() && constraint.front() ==
'.') {
665 return candidate_domain.ends_with(constraint);
668 return candidate_domain == constraint;
672 class MatchScore final {
674 MatchScore() : m_any(
false), m_some(
false), m_all(
true) {}
706 const auto& constraint = std::get<DNSConstraint>(m_name).value();
709 score.add(dns_subtree_match(
dns.to_string(), constraint));
716 if(cn.find(
'.') == std::string::npos) {
723 score.add(dns_subtree_match(dns_form->to_string(), constraint));
728 const X509_DN& constraint = std::get<X509_DN>(m_name);
735 const auto& subnet = std::get<IPv4Subnet>(m_name);
742 score.add(subnet.contains(*ipv4));
747 score.add(subnet.contains(ipv4));
768 return score.result();
786 os << gn.
type() <<
":" << gn.
name();
810 std::optional<size_t> maximum;
821 if(maximum.has_value()) {
832 std::vector<GeneralSubtree>&& excluded_subtrees) :
833 m_permitted_subtrees(std::move(permitted_subtrees)), m_excluded_subtrees(std::move(excluded_subtrees)) {
834 for(
const auto& c : m_permitted_subtrees) {
835 m_permitted_name_types.insert(c.base().type_code());
837 for(
const auto& c : m_excluded_subtrees) {
838 m_excluded_name_types.insert(c.base().type_code());
844bool exceeds_limit(
size_t dn_count,
size_t alt_count,
size_t constraint_count) {
849 constexpr size_t MAX_NC_CHECKS = (1 << 16);
851 if(
auto names =
checked_add(dn_count, alt_count)) {
852 if(
auto product =
checked_mul(*names, constraint_count)) {
853 if(*product < MAX_NC_CHECKS) {
899 const auto is_permitted_dn = [&](
const X509_DN& dn) {
905 for(
const auto& c : m_permitted_subtrees) {
906 if(c.base().matches_dn(dn)) {
915 const auto is_permitted_dns_name = [&](
const DNSName&
name) {
921 for(
const auto& c : m_permitted_subtrees) {
922 if(c.base().matches_dns(
name)) {
941 const auto is_permitted_ipv4 = [&](
const IPv4Address& ipv4) {
942 if(!ip_form_restricted) {
946 for(
const auto& c : m_permitted_subtrees) {
947 if(c.base().matches_ipv4(ipv4)) {
960 const auto is_permitted_ipv6 = [&](
const IPv6Address& ipv6) {
961 if(!ip_form_restricted) {
965 for(
const auto& c : m_permitted_subtrees) {
966 if(c.base().matches_ipv6(ipv6)) {
975 const auto is_permitted_uri = [&](
const URI& uri) {
991 const auto host = uri.host();
992 if(!host.has_value() || !std::holds_alternative<DNSName>(host->get())) {
995 if(std::get<DNSName>(host->get()).to_string().find(
'.') == std::string::npos) {
998 for(
const auto& c : m_permitted_subtrees) {
999 if(c.base().matches_uri(uri)) {
1006 const auto is_permitted_email = [&](
const EmailAddress& addr) {
1011 for(
const auto& c : m_permitted_subtrees) {
1012 if(c.base().matches_email(addr)) {
1023 const auto is_permitted_smtp_utf8 = [&](
const SmtpUtf8Mailbox& mailbox) {
1027 for(
const auto& c : m_permitted_subtrees) {
1028 if(c.base().matches_email(mailbox)) {
1052 for(
const auto& alt_dn : alt_name.directory_names()) {
1053 if(!is_permitted_dn(alt_dn)) {
1058 for(
const auto& alt_dns : alt_name.dns_names()) {
1059 if(!is_permitted_dns_name(alt_dns)) {
1064 for(
const auto& alt_ipv4 : alt_name.ipv4_addresses()) {
1065 if(!is_permitted_ipv4(alt_ipv4)) {
1070 for(
const auto& alt_ipv6 : alt_name.ipv6_addresses()) {
1071 if(!is_permitted_ipv6(alt_ipv6)) {
1076 for(
const auto& uri : alt_name.uri_names()) {
1077 if(!is_permitted_uri(uri)) {
1082 for(
const auto& addr : alt_name.email_addresses()) {
1083 if(!is_permitted_email(addr)) {
1088 for(
const auto& mailbox : alt_name.smtp_utf8_mailboxes()) {
1089 if(!is_permitted_smtp_utf8(mailbox)) {
1095 if(alt_name.is_empty()) {
1098 if(!is_permitted_ipv4(*ipv4)) {
1101 }
else if(cn.find(
'.') != std::string::npos) {
1103 if(!is_permitted_dns_name(*dns_form)) {
1119 if(!is_permitted_email(*addr)) {
1145 if(reject_unknown) {
1158 const auto is_excluded_dn = [&](
const X509_DN& dn) {
1164 for(
const auto& c : m_excluded_subtrees) {
1165 if(c.base().matches_dn(dn)) {
1174 const auto is_excluded_dns_name = [&](
const DNSName&
name) {
1180 for(
const auto& c : m_excluded_subtrees) {
1181 if(c.base().matches_dns(
name)) {
1195 const auto& constraint = std::get<GeneralName::DNSConstraint>(c.base().m_name).value();
1206 const auto is_excluded_ipv4 = [&](
const IPv4Address& ipv4) {
1208 for(
const auto& c : m_excluded_subtrees) {
1209 if(c.base().matches_ipv4(ipv4)) {
1219 const auto is_excluded_ipv6 = [&](
const IPv6Address& ipv6) {
1221 for(
const auto& c : m_excluded_subtrees) {
1222 if(c.base().matches_ipv6(ipv6)) {
1231 if(
auto embedded_v4 = ipv6.as_ipv4()) {
1232 for(
const auto& c : m_excluded_subtrees) {
1233 if(c.base().matches_ipv4(*embedded_v4)) {
1244 const auto is_excluded_uri = [&](
const URI& uri) {
1259 const auto host = uri.host();
1260 if(!host.has_value() || !std::holds_alternative<DNSName>(host->get())) {
1263 if(std::get<DNSName>(host->get()).to_string().find(
'.') == std::string::npos) {
1266 for(
const auto& c : m_excluded_subtrees) {
1267 if(c.base().matches_uri(uri)) {
1294 auto mailbox_form_constraint_covers_domain = [](
const GeneralName& gn,
const DNSName& san_domain) {
1298 const auto& constraint = std::get<GeneralName::EmailConstraint>(gn.m_name).value();
1299 const auto at = constraint.find(
'@');
1300 return at != std::string::npos && san_domain.to_string() == constraint.substr(at + 1);
1303 const auto is_excluded_email = [&](
const EmailAddress& addr) {
1305 for(
const auto& c : m_excluded_subtrees) {
1306 if(c.base().matches_email(addr)) {
1321 const auto is_excluded_smtp_utf8 = [&](
const SmtpUtf8Mailbox& mailbox) {
1323 for(
const auto& c : m_excluded_subtrees) {
1324 if(c.base().matches_email(mailbox)) {
1327 if(mailbox_form_constraint_covers_domain(c.base(), mailbox.domain())) {
1339 for(
const auto& alt_dn : alt_name.directory_names()) {
1340 if(is_excluded_dn(alt_dn)) {
1345 for(
const auto& alt_dns : alt_name.dns_names()) {
1346 if(is_excluded_dns_name(alt_dns)) {
1351 for(
const auto& alt_ipv4 : alt_name.ipv4_addresses()) {
1352 if(is_excluded_ipv4(alt_ipv4)) {
1357 for(
const auto& alt_ipv6 : alt_name.ipv6_addresses()) {
1358 if(is_excluded_ipv6(alt_ipv6)) {
1363 for(
const auto& uri : alt_name.uri_names()) {
1364 if(is_excluded_uri(uri)) {
1369 for(
const auto& addr : alt_name.email_addresses()) {
1370 if(is_excluded_email(addr)) {
1375 for(
const auto& mailbox : alt_name.smtp_utf8_mailboxes()) {
1376 if(is_excluded_smtp_utf8(mailbox)) {
1382 if(alt_name.is_empty()) {
1385 if(is_excluded_ipv4(*ipv4)) {
1388 }
else if(cn.find(
'.') != std::string::npos) {
1390 if(is_excluded_dns_name(*dns_form)) {
1400 if(is_excluded_email(*addr)) {
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ASSERT_UNREACHABLE()
const std::string & value() const
const std::set< IPv6Address > & ipv6_addresses() const
Return the set of IPv6 addresses included in this alternative name.
const std::set< DNSName > & dns_names() const
Return the set of DNS names included in this alternative name.
const std::set< X509_DN > & directory_names() const
Return the set of directory names included in this alternative name.
const std::set< EmailAddress > & email_addresses() const
Return the set of email addresses included in this alternative name.
const std::set< IPv4Address > & ipv4_addresses() const
Return the set of IPv4 addresses included in this alternative name.
const std::set< URI > & uri_names() const
Return the set of URIs included in this alternative name.
bool is_empty() const
Return true if this alternative name is empty (zero names).
BER_Object get_next_object()
BER_Decoder & decode(bool &out)
BER_Decoder & verify_end()
BER_Decoder start_sequence()
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
const uint8_t * bits() const
bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
DER_Encoder & start_sequence()
DER_Encoder & encode(bool b)
const std::string & to_string() const
static std::optional< DNSName > from_san_string(std::string_view name)
static std::optional< DNSName > from_string(std::string_view name)
static std::optional< EmailAddress > from_string(std::string_view addr)
static GeneralName email(std::string_view email)
void decode_from(BER_Decoder &from) override
bool matches_ipv6(const IPv6Address &ip) const
static GeneralName ipv4_address(uint32_t ipv4)
bool matches_uri(const URI &uri) const
void encode_into(DER_Encoder &to) const override
static GeneralName uri(std::string_view uri)
static GeneralName _dns_san_value(std::string_view dns)
MatchResult matches(const X509_Certificate &cert) const
bool matches_dn(const X509_DN &dn) const
std::vector< uint8_t > binary_name() const
bool matches_email(const EmailAddress &addr) const
static GeneralName ipv6_address(const IPv6Address &ipv6)
bool matches_dns(const std::string &dns_name) const
NameType type_code() const
bool matches_ipv4(uint32_t ip) const
static GeneralName dns(std::string_view dns)
static GeneralName _uri_san_value(std::string_view full_uri)
static GeneralName directory_name(Botan::X509_DN dn)
A single Name Constraint.
void encode_into(DER_Encoder &to) const override
const GeneralName & base() const
void decode_from(BER_Decoder &from) override
static std::optional< IPv4Address > from_string(std::string_view str)
static IPv4Address netmask(size_t bits)
static std::optional< IPv4Subnet > from_address_and_mask(std::span< const uint8_t, 8 > addr_and_mask)
static IPv4Subnet host(IPv4Address address)
static IPv6Address netmask(size_t bits)
static std::optional< IPv6Subnet > from_address_and_mask(std::span< const uint8_t, 32 > addr_and_mask)
static IPv6Subnet host(IPv6Address address)
NameConstraints()=default
bool is_permitted(const X509_Certificate &cert, bool reject_unknown) const
bool is_excluded(const X509_Certificate &cert, bool reject_unknown) const
const std::vector< GeneralSubtree > & permitted() const
const std::vector< GeneralSubtree > & excluded() const
const DNSName & domain() const
The domain, as an LDH host name in A-label form (RFC 9598 Section 3).
static std::optional< URI > from_string(std::string_view raw)
const X509_DN & subject_dn() const
std::vector< std::string > subject_info(std::string_view name) const
const AlternativeName & subject_alt_name() const
std::vector< std::string > get_attribute(std::string_view attr) const
void decode_from(BER_Decoder &from) override
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
std::string to_string(const BER_Object &obj)
bool wildcard_intersects_excluded_dns_subtree(std::string_view pattern, std::string_view constraint)
constexpr std::optional< T > checked_add(T a, T b)
@ ExplicitContextSpecific
std::string fmt(std::string_view format, const T &... args)
std::ostream & operator<<(std::ostream &out, const OID &oid)
constexpr std::optional< T > checked_mul(T a, T b)
bool x509_dn_subtree_match(const X509_DN &name, const X509_DN &constraint)
constexpr auto concat(Rs &&... ranges)