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

Name Constraints. More...

#include <pkix_types.h>

Public Member Functions

const std::vector< GeneralSubtree > & excluded () const
bool is_excluded (const X509_Certificate &cert, bool reject_unknown) const
bool is_permitted (const X509_Certificate &cert, bool reject_unknown) const
 NameConstraints ()=default
 NameConstraints (std::vector< GeneralSubtree > &&permitted_subtrees, std::vector< GeneralSubtree > &&excluded_subtrees)
const std::vector< GeneralSubtree > & permitted () const

Detailed Description

Name Constraints.

Wraps the Name Constraints associated with a certificate.

Definition at line 748 of file pkix_types.h.

Constructor & Destructor Documentation

◆ NameConstraints() [1/2]

Botan::NameConstraints::NameConstraints ( )
default

Creates an empty name NameConstraints.

References NameConstraints().

Referenced by NameConstraints().

◆ NameConstraints() [2/2]

Botan::NameConstraints::NameConstraints ( std::vector< GeneralSubtree > && permitted_subtrees,
std::vector< GeneralSubtree > && excluded_subtrees )

Creates NameConstraints from a list of permitted and excluded subtrees.

Parameters
permitted_subtreesnames for which the certificate is permitted
excluded_subtreesnames for which the certificate is not permitted

Definition at line 831 of file name_constraint.cpp.

832 :
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}

Member Function Documentation

◆ excluded()

const std::vector< GeneralSubtree > & Botan::NameConstraints::excluded ( ) const
inline
Returns
excluded names

Definition at line 773 of file pkix_types.h.

773 {
774 return m_excluded_subtrees;
775 }

References excluded().

Referenced by botan_x509_cert_excluded_name_constraints(), excluded(), and is_excluded().

◆ is_excluded()

bool Botan::NameConstraints::is_excluded ( const X509_Certificate & cert,
bool reject_unknown ) const

Return true if any of the names in the certificate are excluded

Definition at line 1134 of file name_constraint.cpp.

1134 {
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) {
1295 if(gn.type_code() != GeneralName::NameType::RFC822) {
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}
static std::optional< DNSName > from_san_string(std::string_view name)
Definition dns_name.cpp:147
static std::optional< EmailAddress > from_string(std::string_view addr)
Definition email.cpp:78
static std::optional< IPv4Address > from_string(std::string_view str)
const std::vector< GeneralSubtree > & excluded() const
Definition pkix_types.h:773
const char * name
Definition ffi_tls.h:59
bool wildcard_intersects_excluded_dns_subtree(std::string_view pattern, std::string_view constraint)

References Botan::X509_DN::count(), Botan::GeneralName::DN, Botan::GeneralName::DNS, excluded(), Botan::DNSName::from_san_string(), Botan::EmailAddress::from_string(), Botan::IPv4Address::from_string(), Botan::X509_DN::get_attribute(), Botan::GeneralName::IPv4, Botan::GeneralName::IPv6, name, Botan::GeneralName::Other, Botan::GeneralName::RFC822, Botan::X509_Certificate::subject_alt_name(), Botan::X509_Certificate::subject_dn(), Botan::X509_Certificate::subject_info(), Botan::GeneralName::type_code(), Botan::GeneralName::Unknown, Botan::GeneralName::URI, and Botan::wildcard_intersects_excluded_dns_subtree().

◆ is_permitted()

bool Botan::NameConstraints::is_permitted ( const X509_Certificate & cert,
bool reject_unknown ) const

Return true if all of the names in the certificate are permitted

Definition at line 863 of file name_constraint.cpp.

863 {
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}
const std::vector< GeneralSubtree > & permitted() const
Definition pkix_types.h:766

References Botan::X509_DN::count(), Botan::GeneralName::DN, Botan::GeneralName::DNS, Botan::X509_DN::empty(), Botan::DNSName::from_san_string(), Botan::EmailAddress::from_string(), Botan::IPv4Address::from_string(), Botan::X509_DN::get_attribute(), Botan::GeneralName::IPv4, Botan::GeneralName::IPv6, name, Botan::GeneralName::Other, permitted(), Botan::GeneralName::RFC822, Botan::X509_Certificate::subject_alt_name(), Botan::X509_Certificate::subject_dn(), Botan::X509_Certificate::subject_info(), Botan::GeneralName::Unknown, and Botan::GeneralName::URI.

◆ permitted()

const std::vector< GeneralSubtree > & Botan::NameConstraints::permitted ( ) const
inline
Returns
permitted names

Definition at line 766 of file pkix_types.h.

766 {
767 return m_permitted_subtrees;
768 }

References permitted().

Referenced by botan_x509_cert_permitted_name_constraints(), is_permitted(), permitted(), and Botan::X509_Certificate::to_string().


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