Botan 3.14.1
Crypto and TLS for C&
name_constraint.cpp
Go to the documentation of this file.
1/*
2* X.509 Name Constraint
3* (C) 2015 Kai Michaelis
4* 2024,2026 Jack Lloyd
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/pkix_types.h>
10
11#include <botan/ber_dec.h>
12#include <botan/der_enc.h>
13#include <botan/uri.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>
20#include <span>
21
22namespace Botan {
23
24namespace {
25
26enum class RequireFQDN : bool { Yes = true, No = false };
27
28/*
29* Validate a host constraint - either a DNS name or a subtree of the
30* form of "." followed by a DNS name. RFC 5280 4.2.1.10 defines this
31* style for URI and email constraints. For DNS it is silent, but it
32* seems in practice implementations accept subtrees for DNS
33* constraints as well.
34*/
35std::optional<std::string> validate_subtree_constraint_host(std::string_view input, RequireFQDN require_fqdn) {
36 if(input.empty()) {
37 return std::nullopt;
38 }
39 const bool subtree = input.starts_with('.');
40 const std::string_view body = subtree ? input.substr(1) : input;
41 auto dns = DNSName::from_string(body);
42 if(!dns.has_value()) {
43 return std::nullopt;
44 }
45 if(require_fqdn == RequireFQDN::Yes && dns->to_string().find('.') == std::string::npos) {
46 return std::nullopt;
47 }
48
49 if(subtree) {
50 return std::string(".") + dns->to_string();
51 } else {
52 return dns->to_string();
53 }
54}
55
56} // namespace
57
58std::optional<GeneralName::DNSConstraint> GeneralName::DNSConstraint::from_string(std::string_view input) {
59 // TODO(C++23): validate_subtree_constraint_host(input, RequireFQDN::No)
60 // .transform([](std::string s) { return DNSConstraint(std::move(s)); });
61 if(auto canonical = validate_subtree_constraint_host(input, RequireFQDN::No)) {
62 return DNSConstraint(std::move(*canonical));
63 } else {
64 return std::nullopt;
65 }
66}
67
68std::optional<GeneralName::DNSConstraint> GeneralName::DNSConstraint::from_san_value(std::string_view input) {
69 if(auto parsed = DNSName::from_san_string(input)) {
70 return DNSConstraint(parsed->to_string());
71 } else {
72 return std::nullopt;
73 }
74}
75
76std::optional<GeneralName::URIConstraint> GeneralName::URIConstraint::from_string(std::string_view input) {
77 /*
78 RFC 5280 4.2.1.10:
79 The constraint MUST be specified as a fully qualified domain
80 name and MAY specify a host or a domain. Examples would be
81 "host.example.com" and ".example.com".
82 */
83 if(auto canonical = validate_subtree_constraint_host(input, RequireFQDN::Yes)) {
84 return URIConstraint(std::move(*canonical));
85 } else {
86 return std::nullopt;
87 }
88}
89
90std::optional<GeneralName::URIConstraint> GeneralName::URIConstraint::from_san_value(std::string_view full_uri) {
91 if(URI::from_string(full_uri).has_value()) {
92 return URIConstraint(std::string(full_uri));
93 } else {
94 return std::nullopt;
95 }
96}
97
98std::optional<GeneralName::EmailConstraint> GeneralName::EmailConstraint::from_string(std::string_view input) {
99 if(input.empty()) {
100 return std::nullopt;
101 }
102 if(input.find('@') != std::string_view::npos) {
103 // Mailbox form:
104 auto email = EmailAddress::from_string(input);
105 if(!email.has_value()) {
106 return std::nullopt;
107 }
108 return EmailConstraint(email->to_string());
109 }
110 if(auto canonical = validate_subtree_constraint_host(input, RequireFQDN::No)) {
111 // Host form
112 return EmailConstraint(std::move(*canonical));
113 }
114 return std::nullopt;
115}
116
117namespace {
118
119/*
120* Match a single DNS label against an RFC 6125 6.4.3 wildcard pattern
121* label (containing exactly one '*'). The candidate must have no dots
122* (it is a single label).
123*/
124bool wildcard_label_matches(std::string_view pattern_label, std::string_view candidate) {
125 if(candidate.find('.') != std::string_view::npos) {
126 return false;
127 }
128 const auto star = pattern_label.find('*');
129 if(star == std::string_view::npos) {
130 return pattern_label == candidate;
131 }
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()) {
135 return false;
136 }
137 return candidate.starts_with(prefix) && candidate.ends_with(suffix);
138}
139
140} // namespace
141
142/*
143* Does the wildcard SAN @p pattern have some expansion that falls inside the
144* excluded DNS subtree @p constraint?
145*
146* This function is similar to but subtly different from host_wildcard_match,
147* which is trying to answer a different question, namely "is `host` a name that
148* a client should trust this wildcard cert for", including various checks such
149* as the maximum length of labels. In contrast here we want to check for any
150* possible overlap - could this wildcard expand to any name inside the excluded
151* subtree.
152*/
153bool wildcard_intersects_excluded_dns_subtree(std::string_view pattern, std::string_view constraint) {
154 if(pattern.empty() || constraint.empty()) {
155 return false;
156 }
157 const bool subtree_form = (constraint.front() == '.');
158 const std::string_view c_base = subtree_form ? constraint.substr(1) : constraint;
159 if(c_base.empty()) {
160 return false;
161 }
162
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);
167
168 if(p_tail.empty()) {
169 // Single-label wildcard. Matches single-label names only, so it
170 // can only land inside a bare-host subtree whose base is also a
171 // single label.
172 if(subtree_form || c_base.find('.') != std::string_view::npos) {
173 return false;
174 }
175 return wildcard_label_matches(p_left, c_base);
176 }
177
178 // p_tail starts with ".". If it ends (label-aligned) with "." + c_base,
179 // then every wildcard expansion produces a name ending with that
180 // suffix, which is inside c_base's subtree (both bare-host and
181 // leading-dot forms accept proper-subdomain entries).
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) {
186 return true;
187 }
188 }
189 }
190
191 // Bare-host subtrees also contain c_base itself. The wildcard can
192 // produce c_base directly iff c_base = (single label) + p_tail and
193 // the prefix label fits p_left.
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);
197 }
198
199 return false;
200}
201
202namespace {
203
204/*
205* RFC 5280 subtree matching for DNS-form names: a bare-host constraint
206* matches the host itself or any name with extra leading labels (so
207* "host.example.com" matches "host.example.com" and "www.host.example.com"
208* but not "host1.example.com"). A constraint with a leading dot matches
209* proper subdomains only.
210*
211* Used as-is for DNS name constraints. URI / RFC822 host-form constraints
212* differ from this -- they're exact-match only on the bare-host form, and
213* only the leading-dot ".host" form here is shared with them. Callers
214* dispatch the leading-dot case to this helper.
215*
216* Both inputs are assumed to already be lowercased.
217*/
218bool dns_subtree_match(std::string_view name, std::string_view constraint) {
219 // Embedded nulls should have been rejected during decoding before this point
220 BOTAN_DEBUG_ASSERT(name.find('\0') == std::string_view::npos);
221
222 if(name.size() == constraint.size()) {
223 return name == constraint;
224 } else if(constraint.size() > name.size()) {
225 // The constraint is longer than the issued name: not possibly a match
226 return false;
227 }
228
229 if(constraint.empty()) {
230 return true;
231 }
232
233 BOTAN_ASSERT_NOMSG(name.size() > constraint.size());
234
235 const std::string_view substr = name.substr(name.size() - constraint.size());
236
237 if(constraint.front() == '.') {
238 return substr == constraint;
239 } else {
240 return substr == constraint && name[name.size() - constraint.size() - 1] == '.';
241 }
242}
243
244/*
245* RFC 5280 4.2.1.10 RFC822 name constraint matching.
246*
247* The constraint @p c is one of:
248* - "local@host" - matches exactly one mailbox (case-insensitive)
249* - "host" - matches addresses whose domain is exactly host
250* - ".host" - matches addresses in any subdomain of host
251* (but NOT the base host itself)
252*
253* @p c is assumed to be already lowercased and validated at decode time.
254*/
255bool email_subtree_match(const EmailAddress& candidate, std::string_view c) {
256 /*
257 RFC 5280 7.5:
258 Two email addresses are considered to match if:
259 1) the local-part of each name is an exact match, AND
260 2) the host-part of each name matches using a case-insensitive
261 ASCII comparison.
262
263 The candidate's domain comes through DNSName as canonical-lowercase, and the
264 constraint string was lowercased only on its host portion at decode, so a
265 plain string compare on each side produces the correct result.
266 */
267 const std::string& candidate_domain = candidate.domain().to_string();
268 const auto at = c.find('@');
269 if(at != std::string_view::npos) {
270 // Mailbox form: exact-match against candidate
271 return (candidate.local_part() == c.substr(0, at)) && (candidate_domain == c.substr(at + 1));
272 }
273 if(!c.empty() && c.front() == '.') {
274 // Subtree form: any subdomain, but not the base host.
275 return dns_subtree_match(candidate_domain, c);
276 }
277 /*
278 RFC 5280 4.2.1.10:
279 To indicate all Internet mail addresses on a particular host, the
280 constraint is specified as the host name. For example, the
281 constraint "example.com" is satisfied by any mail address at the
282 host "example.com".
283 */
284 return candidate_domain == c;
285}
286
287} // namespace
288
289std::string GeneralName::type() const {
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}
311
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}
319
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}
327
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}
335
336GeneralName GeneralName::_uri_san_value(std::string_view full_uri) {
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}
343
344GeneralName GeneralName::_dns_san_value(std::string_view dns_name) {
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}
351
355
359
360GeneralName GeneralName::ipv4_address(uint32_t ipv4, uint32_t mask) {
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}
367
371
373 return {NameType::IPv4, subnet};
374}
375
379
381 return {NameType::IPv6, subnet};
382}
383
384std::string GeneralName::name() const {
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}
396
397std::vector<uint8_t> GeneralName::binary_name() const {
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}
408
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);
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}
466
468 const BER_Object obj = ber.get_next_object();
469
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);
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}
541
542bool GeneralName::matches_dns(const std::string& dns_name) const {
543 if(m_type == NameType::DNS) {
544 return dns_subtree_match(dns_name, std::get<DNSConstraint>(m_name).value());
545 }
546 return false;
547}
548
549bool GeneralName::matches_dns(const DNSName& dns_name) const {
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}
555
556bool GeneralName::matches_ipv4(uint32_t ip) const {
557 if(m_type == NameType::IPv4) {
558 return std::get<IPv4Subnet>(m_name).contains(IPv4Address(ip));
559 }
560 return false;
561}
562
564 if(m_type == NameType::IPv6) {
565 return std::get<IPv6Subnet>(m_name).contains(ip);
566 }
567 return false;
568}
569
570bool GeneralName::matches_dn(const X509_DN& dn) const {
571 if(m_type == NameType::DN) {
572 return matches_dn(dn, std::get<X509_DN>(m_name));
573 }
574 return false;
575}
576
577bool GeneralName::matches_uri(const URI& uri) const {
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}
606
608 if(m_type != NameType::RFC822) {
609 return false;
610 }
611 return email_subtree_match(addr, std::get<EmailConstraint>(m_name).value());
612}
613
614bool GeneralName::matches_email(const SmtpUtf8Mailbox& mailbox) const {
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}
670
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) {
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}
770
771//static
772bool GeneralName::matches_dn(const X509_DN& name, const X509_DN& constraint) {
773 /*
774 RFC 5280 7.1:
775 Two RelativeDistinguishedNames RDN1 and RDN2 match if they have
776 the same number of naming attributes and for each naming attribute
777 in RDN1 there is a matching naming attribute in RDN2.
778
779 This is implementing directoryName subtree match, so the constraint's RDN
780 sequence must be a prefix of the name's RDN sequence.
781 */
782 return x509_dn_subtree_match(name, constraint);
783}
784
785std::ostream& operator<<(std::ostream& os, const GeneralName& gn) {
786 os << gn.type() << ":" << gn.name();
787 return os;
788}
789
791
793 /*
794 * RFC 5280 Section 4.2.1.10:
795 * Within this profile, the minimum and maximum fields are not used with any
796 * name forms, thus, the minimum MUST be zero, and maximum MUST be absent.
797 *
798 * minimum is DEFAULT 0 so it is not encoded.
799 */
800 to.start_sequence().encode(m_base).end_cons();
801}
802
804 /*
805 * RFC 5280 Section 4.2.1.10:
806 * Within this profile, the minimum and maximum fields are not used with any
807 * name forms, thus, the minimum MUST be zero, and maximum MUST be absent.
808 */
809 size_t minimum = 0;
810 std::optional<size_t> maximum;
811
812 ber.start_sequence()
813 .decode(m_base)
816 .end_cons();
817
818 if(minimum != 0) {
819 throw Decoding_Error("GeneralSubtree minimum must be 0");
820 }
821 if(maximum.has_value()) {
822 throw Decoding_Error("GeneralSubtree maximum must be absent");
823 }
824}
825
826std::ostream& operator<<(std::ostream& os, const GeneralSubtree& gs) {
827 os << gs.base();
828 return os;
829}
830
831NameConstraints::NameConstraints(std::vector<GeneralSubtree>&& permitted_subtrees,
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());
836 }
837 for(const auto& c : m_excluded_subtrees) {
838 m_excluded_name_types.insert(c.base().type_code());
839 }
840}
841
842namespace {
843
844bool exceeds_limit(size_t dn_count, size_t alt_count, size_t constraint_count) {
845 /**
846 * OpenSSL uses a similar limit, but applies it to the total number of
847 * constraints, while we apply it to permitted and excluded independently.
848 */
849 constexpr size_t MAX_NC_CHECKS = (1 << 16);
850
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) {
854 return false;
855 }
856 }
857 }
858 return true;
859}
860
861} // namespace
862
863bool NameConstraints::is_permitted(const X509_Certificate& cert, bool reject_unknown) const {
864 if(permitted().empty()) {
865 return true;
866 }
867
868 const auto& alt_name = cert.subject_alt_name();
869
870 if(exceeds_limit(cert.subject_dn().count(), alt_name.count(), permitted().size())) {
871 return false;
872 }
873
874 if(reject_unknown) {
875 /* A critical NC restricting an unrecognized GeneralName form (e.g. x400Address)
876 * causes immediate rejection.
877 *
878 * RFC 5280 4.2.1.10 leaves this both unspecified
879 * The syntax and semantics for name constraints for otherName, ediPartyName, and
880 * registeredID are not defined by this specification
881 * and discouraged
882 * Conforming CAs [...] SHOULD NOT impose name constraints on the x400Address,
883 * ediPartyName, or registeredID name forms.
884 *
885 * In principle we should only reject when the constrained form appears in the
886 * certificate. But this situation in general seems to be a minefield, with no help
887 * from specs, test suites, etc. Lacking any obvious use case, just fail closed.
888 *
889 * If you happen to hit this with a real chain, open an issue.
890 */
891 if(m_permitted_name_types.contains(GeneralName::NameType::Unknown)) {
892 return false;
893 }
894 if(m_permitted_name_types.contains(GeneralName::NameType::Other) && !alt_name.other_name_values().empty()) {
895 return false;
896 }
897 }
898
899 const auto is_permitted_dn = [&](const X509_DN& dn) {
900 // If no restrictions, then immediate accept
901 if(!m_permitted_name_types.contains(GeneralName::NameType::DN)) {
902 return true;
903 }
904
905 for(const auto& c : m_permitted_subtrees) {
906 if(c.base().matches_dn(dn)) {
907 return true;
908 }
909 }
910
911 // There is at least one permitted name and we didn't match
912 return false;
913 };
914
915 const auto is_permitted_dns_name = [&](const DNSName& name) {
916 // If no restrictions, then immediate accept
917 if(!m_permitted_name_types.contains(GeneralName::NameType::DNS)) {
918 return true;
919 }
920
921 for(const auto& c : m_permitted_subtrees) {
922 if(c.base().matches_dns(name)) {
923 return true;
924 }
925 }
926
927 // There is at least one permitted name and we didn't match
928 return false;
929 };
930
931 /*
932 RFC 5280 4.2.1.10: iPAddress is a single GeneralName element where
933 IPv4 and IPv6 are distinguished only by the length.
934
935 An iPAddress subtree of either version therefore restricts the iPAddress name
936 form for both versions.
937 */
938 const bool ip_form_restricted = m_permitted_name_types.contains(GeneralName::NameType::IPv4) ||
939 m_permitted_name_types.contains(GeneralName::NameType::IPv6);
940
941 const auto is_permitted_ipv4 = [&](const IPv4Address& ipv4) {
942 if(!ip_form_restricted) {
943 return true;
944 }
945
946 for(const auto& c : m_permitted_subtrees) {
947 if(c.base().matches_ipv4(ipv4)) {
948 return true;
949 }
950 }
951
952 // We might here check if there are any IPv6 permitted names which are
953 // mapped IPv4 addresses, and if so check if any of those apply. It's not
954 // clear if this is desirable, and RFC 5280 is completely silent on the issue.
955
956 // There is at least one permitted iPAddress name and we didn't match
957 return false;
958 };
959
960 const auto is_permitted_ipv6 = [&](const IPv6Address& ipv6) {
961 if(!ip_form_restricted) {
962 return true;
963 }
964
965 for(const auto& c : m_permitted_subtrees) {
966 if(c.base().matches_ipv6(ipv6)) {
967 return true;
968 }
969 }
970
971 // There is at least one permitted iPAddress name and we didn't match
972 return false;
973 };
974
975 const auto is_permitted_uri = [&](const URI& uri) {
976 // If no URI restrictions, accept.
977 if(!m_permitted_name_types.contains(GeneralName::NameType::URI)) {
978 return true;
979 }
980 /*
981 RFC 5280 4.2.1.10:
982 If a constraint is applied to the uniformResourceIdentifier
983 name form and a subsequent certificate includes a
984 subjectAltName extension with a uniformResourceIdentifier that
985 does not include an authority component with a host name
986 specified as a fully qualified domain name (e.g., if the URI
987 either does not include an authority component or includes an
988 authority component in which the host name is specified as an
989 IP address), then the application MUST reject the certificate.
990 */
991 const auto host = uri.host();
992 if(!host.has_value() || !std::holds_alternative<DNSName>(host->get())) {
993 return false;
994 }
995 if(std::get<DNSName>(host->get()).to_string().find('.') == std::string::npos) {
996 return false;
997 }
998 for(const auto& c : m_permitted_subtrees) {
999 if(c.base().matches_uri(uri)) {
1000 return true;
1001 }
1002 }
1003 return false;
1004 };
1005
1006 const auto is_permitted_email = [&](const EmailAddress& addr) {
1007 // If no email restrictions, accept.
1008 if(!m_permitted_name_types.contains(GeneralName::NameType::RFC822)) {
1009 return true;
1010 }
1011 for(const auto& c : m_permitted_subtrees) {
1012 if(c.base().matches_email(addr)) {
1013 return true;
1014 }
1015 }
1016 return false;
1017 };
1018
1019 // RFC 9598 Section 6 extends rfc822Name name constraints to SmtpUTF8Mailbox
1020 // SAN entries (id-on-SmtpUTF8Mailbox otherNames). When rfc822Name
1021 // constraints are in effect, every SmtpUTF8Mailbox SAN must match
1022 // at least one permitted entry.
1023 const auto is_permitted_smtp_utf8 = [&](const SmtpUtf8Mailbox& mailbox) {
1024 if(!m_permitted_name_types.contains(GeneralName::NameType::RFC822)) {
1025 return true;
1026 }
1027 for(const auto& c : m_permitted_subtrees) {
1028 if(c.base().matches_email(mailbox)) {
1029 return true;
1030 }
1031 }
1032 return false;
1033 };
1034
1035 /*
1036 RFC 5280 4.1.2.6:
1037 If subject naming information is present only in the
1038 subjectAltName extension (e.g., a key bound only to an email
1039 address or URI), then the subject name MUST be an empty
1040 sequence and the subjectAltName extension MUST be critical.
1041
1042 RFC 5280 4.2.1.10:
1043 Restrictions of the form directoryName MUST be applied to the subject
1044 field in the certificate (when the certificate includes a non-empty
1045 subject field) and to any names of type directoryName in the
1046 subjectAltName extension.
1047 */
1048 if(!cert.subject_dn().empty() && !is_permitted_dn(cert.subject_dn())) {
1049 return false;
1050 }
1051
1052 for(const auto& alt_dn : alt_name.directory_names()) {
1053 if(!is_permitted_dn(alt_dn)) {
1054 return false;
1055 }
1056 }
1057
1058 for(const auto& alt_dns : alt_name.dns_names()) {
1059 if(!is_permitted_dns_name(alt_dns)) {
1060 return false;
1061 }
1062 }
1063
1064 for(const auto& alt_ipv4 : alt_name.ipv4_addresses()) {
1065 if(!is_permitted_ipv4(alt_ipv4)) {
1066 return false;
1067 }
1068 }
1069
1070 for(const auto& alt_ipv6 : alt_name.ipv6_addresses()) {
1071 if(!is_permitted_ipv6(alt_ipv6)) {
1072 return false;
1073 }
1074 }
1075
1076 for(const auto& uri : alt_name.uri_names()) {
1077 if(!is_permitted_uri(uri)) {
1078 return false;
1079 }
1080 }
1081
1082 for(const auto& addr : alt_name.email_addresses()) {
1083 if(!is_permitted_email(addr)) {
1084 return false;
1085 }
1086 }
1087
1088 for(const auto& mailbox : alt_name.smtp_utf8_mailboxes()) {
1089 if(!is_permitted_smtp_utf8(mailbox)) {
1090 return false;
1091 }
1092 }
1093
1094 // TODO(Botan4): CN fallback is deprecated for removal in Botan4.
1095 if(alt_name.is_empty()) {
1096 for(const auto& cn : cert.subject_info("CN")) {
1097 if(auto ipv4 = IPv4Address::from_string(cn)) {
1098 if(!is_permitted_ipv4(*ipv4)) {
1099 return false;
1100 }
1101 } else if(cn.find('.') != std::string::npos) {
1102 if(auto dns_form = DNSName::from_san_string(cn)) {
1103 if(!is_permitted_dns_name(*dns_form)) {
1104 return false;
1105 }
1106 }
1107 }
1108 }
1109
1110 /*
1111 RFC 5280 4.2.1.10:
1112 When constraints are imposed on the rfc822Name name form, but the
1113 certificate does not include a subject alternative name, the
1114 rfc822Name constraint MUST be applied to the attribute of type
1115 emailAddress in the subject distinguished name.
1116 */
1117 for(const auto& email_str : cert.subject_dn().get_attribute("PKCS9.EmailAddress")) {
1118 if(auto addr = EmailAddress::from_string(email_str)) {
1119 if(!is_permitted_email(*addr)) {
1120 return false;
1121 }
1122 } else if(m_permitted_name_types.contains(GeneralName::NameType::RFC822)) {
1123 // emailAddress is present but unparsable and an rfc822Name
1124 // constraint is in effect; treat as not permitted.
1125 return false;
1126 }
1127 }
1128 }
1129
1130 // We didn't encounter a name that doesn't have a matching constraint
1131 return true;
1132}
1133
1134bool NameConstraints::is_excluded(const X509_Certificate& cert, bool reject_unknown) const {
1135 if(excluded().empty()) {
1136 return false;
1137 }
1138
1139 const auto& alt_name = cert.subject_alt_name();
1140
1141 if(exceeds_limit(cert.subject_dn().count(), alt_name.count(), excluded().size())) {
1142 return true;
1143 }
1144
1145 if(reject_unknown) {
1146 // This is one is overly broad: we should just reject if there is a name constraint
1147 // with the same OID as one of the other names
1148 if(m_excluded_name_types.contains(GeneralName::NameType::Other) && !alt_name.other_name_values().empty()) {
1149 return true;
1150 }
1151 // As in is_permitted: a critical NC restricting an unrecognized
1152 // GeneralName form cannot be evaluated; reject conservatively.
1153 if(m_excluded_name_types.contains(GeneralName::NameType::Unknown)) {
1154 return true;
1155 }
1156 }
1157
1158 const auto is_excluded_dn = [&](const X509_DN& dn) {
1159 // If no restrictions, then immediate accept
1160 if(!m_excluded_name_types.contains(GeneralName::NameType::DN)) {
1161 return false;
1162 }
1163
1164 for(const auto& c : m_excluded_subtrees) {
1165 if(c.base().matches_dn(dn)) {
1166 return true;
1167 }
1168 }
1169
1170 // There is at least one excluded name and we didn't match
1171 return false;
1172 };
1173
1174 const auto is_excluded_dns_name = [&](const DNSName& name) {
1175 // If no restrictions, then immediate accept
1176 if(!m_excluded_name_types.contains(GeneralName::NameType::DNS)) {
1177 return false;
1178 }
1179
1180 for(const auto& c : m_excluded_subtrees) {
1181 if(c.base().matches_dns(name)) {
1182 return true;
1183 }
1184
1185 /*
1186 RFC 5280 4.2.1.10:
1187 Any name matching a restriction in the excludedSubtrees
1188 field is invalid regardless of information appearing in
1189 the permittedSubtrees.
1190
1191 If the cert has a wildcard SAN (*.example.com), and that wildcard
1192 could be matched against an excluded name, it must be rejected.
1193 */
1194 if(c.base().m_type == GeneralName::NameType::DNS && name.is_wildcard()) {
1195 const auto& constraint = std::get<GeneralName::DNSConstraint>(c.base().m_name).value();
1196 if(wildcard_intersects_excluded_dns_subtree(name.to_string(), constraint)) {
1197 return true;
1198 }
1199 }
1200 }
1201
1202 // There is at least one excluded name and we didn't match
1203 return false;
1204 };
1205
1206 const auto is_excluded_ipv4 = [&](const IPv4Address& ipv4) {
1207 if(m_excluded_name_types.contains(GeneralName::NameType::IPv4)) {
1208 for(const auto& c : m_excluded_subtrees) {
1209 if(c.base().matches_ipv4(ipv4)) {
1210 return true;
1211 }
1212 }
1213 }
1214
1215 // This name did not match any of the excluded names
1216 return false;
1217 };
1218
1219 const auto is_excluded_ipv6 = [&](const IPv6Address& ipv6) {
1220 if(m_excluded_name_types.contains(GeneralName::NameType::IPv6)) {
1221 for(const auto& c : m_excluded_subtrees) {
1222 if(c.base().matches_ipv6(ipv6)) {
1223 return true;
1224 }
1225 }
1226 }
1227
1228 // An IPv4-mapped IPv6 address names an IPv4 address so verify that
1229 // address is not restricted by an IPv4 excludes rule
1230 if(m_excluded_name_types.contains(GeneralName::NameType::IPv4)) {
1231 if(auto embedded_v4 = ipv6.as_ipv4()) {
1232 for(const auto& c : m_excluded_subtrees) {
1233 if(c.base().matches_ipv4(*embedded_v4)) {
1234 return true;
1235 }
1236 }
1237 }
1238 }
1239
1240 // This name did not match any of the excluded names
1241 return false;
1242 };
1243
1244 const auto is_excluded_uri = [&](const URI& uri) {
1245 if(!m_excluded_name_types.contains(GeneralName::NameType::URI)) {
1246 return false;
1247 }
1248 /*
1249 RFC 5280 4.2.1.10:
1250 If a constraint is applied to the uniformResourceIdentifier
1251 name form and a subsequent certificate includes a
1252 subjectAltName extension with a uniformResourceIdentifier that
1253 does not include an authority component with a host name
1254 specified as a fully qualified domain name (e.g., if the URI
1255 either does not include an authority component or includes an
1256 authority component in which the host name is specified as an
1257 IP address), then the application MUST reject the certificate.
1258 */
1259 const auto host = uri.host();
1260 if(!host.has_value() || !std::holds_alternative<DNSName>(host->get())) {
1261 return true;
1262 }
1263 if(std::get<DNSName>(host->get()).to_string().find('.') == std::string::npos) {
1264 return true;
1265 }
1266 for(const auto& c : m_excluded_subtrees) {
1267 if(c.base().matches_uri(uri)) {
1268 return true;
1269 }
1270 }
1271 return false;
1272 };
1273
1274 /*
1275 * The email matching logic on the exclude side is intentionally stricter
1276 * (more expansive) than the permit side logic.
1277 *
1278 * RFC 9549 updates RFC 5280 and among other things completely removes mailbox
1279 * form constraints (ones with a '@', rather than just a domain constraint)
1280 * claiming "This capability was not used".
1281 *
1282 * This prohibition is reiterated in RFC 9598 Section 6 with "rfc822Name
1283 * constraints with a Local-part SHOULD NOT be used."
1284 *
1285 * Here we lean very conservative in our interpretation: if there is a
1286 * mailbox-form exclude constraint, we reject any mailbox at that domain. That
1287 * is, if excludedSubtrees includes "user@example.com", we treat that
1288 * constraint identically to an exclusion of "example.com".
1289 *
1290 * This might be overly cautious, but generally a rejects-valid bug gets you a
1291 * prompt bug report with testcase, while an accepts-invalid eventually gets
1292 * you a surprise CVE.
1293 */
1294 auto mailbox_form_constraint_covers_domain = [](const GeneralName& gn, const DNSName& san_domain) {
1296 return false;
1297 }
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);
1301 };
1302
1303 const auto is_excluded_email = [&](const EmailAddress& addr) {
1304 if(m_excluded_name_types.contains(GeneralName::NameType::RFC822)) {
1305 for(const auto& c : m_excluded_subtrees) {
1306 if(c.base().matches_email(addr)) {
1307 return true;
1308 }
1309 /*
1310 If we were strictly following RFC 9549 we would here want to call
1311 mailbox_form_constraint_covers_domain, but this breaks chains which
1312 are in conformance to the specifications prior to 9549.
1313 */
1314 }
1315 }
1316 return false;
1317 };
1318
1319 // RFC 9598 Section 6: rfc822Name name constraints also apply to
1320 // SmtpUTF8Mailbox SAN entries. See is_permitted_smtp_utf8.
1321 const auto is_excluded_smtp_utf8 = [&](const SmtpUtf8Mailbox& mailbox) {
1322 if(m_excluded_name_types.contains(GeneralName::NameType::RFC822)) {
1323 for(const auto& c : m_excluded_subtrees) {
1324 if(c.base().matches_email(mailbox)) {
1325 return true;
1326 }
1327 if(mailbox_form_constraint_covers_domain(c.base(), mailbox.domain())) {
1328 return true;
1329 }
1330 }
1331 }
1332 return false;
1333 };
1334
1335 if(is_excluded_dn(cert.subject_dn())) {
1336 return true;
1337 }
1338
1339 for(const auto& alt_dn : alt_name.directory_names()) {
1340 if(is_excluded_dn(alt_dn)) {
1341 return true;
1342 }
1343 }
1344
1345 for(const auto& alt_dns : alt_name.dns_names()) {
1346 if(is_excluded_dns_name(alt_dns)) {
1347 return true;
1348 }
1349 }
1350
1351 for(const auto& alt_ipv4 : alt_name.ipv4_addresses()) {
1352 if(is_excluded_ipv4(alt_ipv4)) {
1353 return true;
1354 }
1355 }
1356
1357 for(const auto& alt_ipv6 : alt_name.ipv6_addresses()) {
1358 if(is_excluded_ipv6(alt_ipv6)) {
1359 return true;
1360 }
1361 }
1362
1363 for(const auto& uri : alt_name.uri_names()) {
1364 if(is_excluded_uri(uri)) {
1365 return true;
1366 }
1367 }
1368
1369 for(const auto& addr : alt_name.email_addresses()) {
1370 if(is_excluded_email(addr)) {
1371 return true;
1372 }
1373 }
1374
1375 for(const auto& mailbox : alt_name.smtp_utf8_mailboxes()) {
1376 if(is_excluded_smtp_utf8(mailbox)) {
1377 return true;
1378 }
1379 }
1380
1381 // TODO(Botan4): CN fallback is deprecated for removal in Botan4.
1382 if(alt_name.is_empty()) {
1383 for(const auto& cn : cert.subject_info("Name")) {
1384 if(auto ipv4 = IPv4Address::from_string(cn)) {
1385 if(is_excluded_ipv4(*ipv4)) {
1386 return true;
1387 }
1388 } else if(cn.find('.') != std::string::npos) {
1389 if(auto dns_form = DNSName::from_san_string(cn)) {
1390 if(is_excluded_dns_name(*dns_form)) {
1391 return true;
1392 }
1393 }
1394 }
1395 }
1396
1397 // RFC 5280 4.2.1.10 fallback to subject DN emailAddress when the cert has no SAN
1398 for(const auto& email_str : cert.subject_dn().get_attribute("PKCS9.EmailAddress")) {
1399 if(auto addr = EmailAddress::from_string(email_str)) {
1400 if(is_excluded_email(*addr)) {
1401 return true;
1402 }
1403 } else if(m_excluded_name_types.contains(GeneralName::NameType::RFC822)) {
1404 return true;
1405 }
1406 }
1407 }
1408
1409 // We didn't encounter a name that matched any prohibited name
1410 return false;
1411}
1412
1413} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:136
#define BOTAN_ASSERT_UNREACHABLE()
Definition assert.h:173
const std::string & value() const
Definition asn1_obj.h:590
const std::set< IPv6Address > & ipv6_addresses() const
Return the set of IPv6 addresses included in this alternative name.
Definition pkix_types.h:421
const std::set< DNSName > & dns_names() const
Return the set of DNS names included in this alternative name.
Definition pkix_types.h:407
const std::set< X509_DN > & directory_names() const
Return the set of directory names included in this alternative name.
Definition pkix_types.h:441
const std::set< EmailAddress > & email_addresses() const
Return the set of email addresses included in this alternative name.
Definition pkix_types.h:398
const std::set< IPv4Address > & ipv4_addresses() const
Return the set of IPv4 addresses included in this alternative name.
Definition pkix_types.h:418
const std::set< URI > & uri_names() const
Return the set of URIs included in this alternative name.
Definition pkix_types.h:389
bool is_empty() const
Return true if this alternative name is empty (zero names).
Definition alt_name.cpp:131
BER_Object get_next_object()
Definition ber_dec.cpp:521
BER_Decoder & decode(bool &out)
Definition ber_dec.h:361
Limits limits() const
Definition ber_dec.h:201
BER_Decoder & verify_end()
Definition ber_dec.cpp:471
BER_Decoder & end_cons()
Definition ber_dec.cpp:635
BER_Decoder start_sequence()
Definition ber_dec.h:279
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
Definition ber_dec.h:550
size_t length() const
Definition asn1_obj.h:303
const uint8_t * bits() const
Definition asn1_obj.h:298
bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const
Definition asn1_obj.cpp:97
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
Definition der_enc.cpp:285
DER_Encoder & start_sequence()
Definition der_enc.h:86
DER_Encoder & end_cons()
Definition der_enc.cpp:208
DER_Encoder & encode(bool b)
Definition der_enc.cpp:313
const std::string & to_string() const
Definition dns_name.h:51
static std::optional< DNSName > from_san_string(std::string_view name)
Definition dns_name.cpp:147
static std::optional< DNSName > from_string(std::string_view name)
Definition dns_name.cpp:134
static std::optional< EmailAddress > from_string(std::string_view addr)
Definition email.cpp:78
X.509 GeneralName Type.
Definition pkix_types.h:541
static GeneralName email(std::string_view email)
void decode_from(BER_Decoder &from) override
bool matches_ipv6(const IPv6Address &ip) const
GeneralName()=default
static GeneralName ipv4_address(uint32_t ipv4)
bool matches_uri(const URI &uri) const
void encode_into(DER_Encoder &to) const override
std::string type() const
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
std::string 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
Definition pkix_types.h:594
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.
Definition pkix_types.h:716
void encode_into(DER_Encoder &to) const override
const GeneralName & base() const
Definition pkix_types.h:735
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)
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
Definition pkix_types.h:766
const std::vector< GeneralSubtree > & excluded() const
Definition pkix_types.h:773
const DNSName & domain() const
The domain, as an LDH host name in A-label form (RFC 9598 Section 3).
Definition email.h:99
static std::optional< URI > from_string(std::string_view raw)
Definition uri.cpp:163
const X509_DN & subject_dn() const
Definition x509cert.cpp:458
std::vector< std::string > subject_info(std::string_view name) const
Definition x509cert.cpp:742
const AlternativeName & subject_alt_name() const
Definition x509cert.cpp:686
std::vector< std::string > get_attribute(std::string_view attr) const
Definition x509_dn.cpp:290
bool empty() const
Definition pkix_types.h:202
void decode_from(BER_Decoder &from) override
Definition x509_dn.cpp:405
size_t count() const
Definition pkix_types.h:209
const char * name
Definition ffi_tls.h:59
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition asn1_obj.cpp:208
std::string to_string(const BER_Object &obj)
Definition asn1_obj.cpp:224
bool wildcard_intersects_excluded_dns_subtree(std::string_view pattern, std::string_view constraint)
constexpr std::optional< T > checked_add(T a, T b)
Definition int_utils.h:19
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
ASN1_Type
Definition asn1_obj.h:47
std::ostream & operator<<(std::ostream &out, const OID &oid)
Definition asn1_oid.cpp:308
constexpr std::optional< T > checked_mul(T a, T b)
Definition int_utils.h:46
bool x509_dn_subtree_match(const X509_DN &name, const X509_DN &constraint)
Definition x509_dn.cpp:359
constexpr auto concat(Rs &&... ranges)
Definition concat_util.h:95