1*78e936b2SEnji Cooper=pod 2*78e936b2SEnji Cooper 3*78e936b2SEnji Cooper=head1 NAME 4*78e936b2SEnji Cooper 5*78e936b2SEnji CooperNAME_CONSTRAINTS_check, 6*78e936b2SEnji CooperNAME_CONSTRAINTS_check_CN - check a certificate's names against a name 7*78e936b2SEnji Cooperconstraints extension 8*78e936b2SEnji Cooper 9*78e936b2SEnji Cooper=head1 SYNOPSIS 10*78e936b2SEnji Cooper 11*78e936b2SEnji Cooper #include <openssl/x509v3.h> 12*78e936b2SEnji Cooper 13*78e936b2SEnji Cooper int NAME_CONSTRAINTS_check(const X509 *x, NAME_CONSTRAINTS *nc); 14*78e936b2SEnji Cooper int NAME_CONSTRAINTS_check_CN(const X509 *x, NAME_CONSTRAINTS *nc); 15*78e936b2SEnji Cooper 16*78e936b2SEnji Cooper=head1 DESCRIPTION 17*78e936b2SEnji Cooper 18*78e936b2SEnji CooperNAME_CONSTRAINTS_check() tests whether the names asserted by certificate 19*78e936b2SEnji CooperI<x> satisfy the name constraints I<nc>. It implements the matching 20*78e936b2SEnji Cooperprimitive of RFC 5280 section 4.2.1.10: given a constraint set (a 21*78e936b2SEnji CooperB<NAME_CONSTRAINTS> structure containing zero or more B<permittedSubtrees> 22*78e936b2SEnji Cooperand B<excludedSubtrees>) and a candidate certificate, decide whether the 23*78e936b2SEnji Coopercertificate's names fall within the permitted subtrees and outside the 24*78e936b2SEnji Cooperexcluded subtrees. 25*78e936b2SEnji Cooper 26*78e936b2SEnji CooperThe names considered by NAME_CONSTRAINTS_check() are: 27*78e936b2SEnji Cooper 28*78e936b2SEnji Cooper=over 4 29*78e936b2SEnji Cooper 30*78e936b2SEnji Cooper=item * 31*78e936b2SEnji Cooper 32*78e936b2SEnji CooperThe certificate's subject distinguished name, matched as a B<directoryName> 33*78e936b2SEnji Coopergeneral-name type. The subject is considered only when it is nonempty. 34*78e936b2SEnji Cooper 35*78e936b2SEnji Cooper=item * 36*78e936b2SEnji Cooper 37*78e936b2SEnji CooperEach B<emailAddress> attribute appearing within the subject distinguished 38*78e936b2SEnji Coopername, matched as an B<rfc822Name> general-name type. These attributes are 39*78e936b2SEnji Cooperthe historical, pre-SAN way of expressing an email address in a 40*78e936b2SEnji Coopercertificate's subject, and RFC 5280 requires that they be subjected to 41*78e936b2SEnji Coopername-constraint checking. 42*78e936b2SEnji Cooper 43*78e936b2SEnji Cooper=item * 44*78e936b2SEnji Cooper 45*78e936b2SEnji CooperEach entry in the certificate's subject alternative name extension, matched 46*78e936b2SEnji Cooperaccording to its declared general-name type. 47*78e936b2SEnji Cooper 48*78e936b2SEnji Cooper=back 49*78e936b2SEnji Cooper 50*78e936b2SEnji CooperNAME_CONSTRAINTS_check() implements matching for the following 51*78e936b2SEnji Coopergeneral-name types: B<directoryName>, B<dNSName>, B<rfc822Name>, 52*78e936b2SEnji CooperB<uniformResourceIdentifier>, and B<iPAddress>. The B<otherName> form 53*78e936b2SEnji CooperB<id-on-SmtpUTF8Mailbox> (RFC 8398) is additionally matched against 54*78e936b2SEnji CooperB<rfc822Name> subtrees. Any other general-name type, including 55*78e936b2SEnji CooperB<x400Address>, B<ediPartyName>, B<registeredID>, and other B<otherName> 56*78e936b2SEnji Cooperforms, yields B<X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE>. 57*78e936b2SEnji Cooper 58*78e936b2SEnji CooperFor each name considered, the function evaluates two conditions: 59*78e936b2SEnji Cooper 60*78e936b2SEnji Cooper=over 4 61*78e936b2SEnji Cooper 62*78e936b2SEnji Cooper=item * 63*78e936b2SEnji Cooper 64*78e936b2SEnji CooperIf I<nc> contains at least one B<permittedSubtree> of the same general-name 65*78e936b2SEnji Coopertype as the name, the name must match at least one of those permitted 66*78e936b2SEnji Coopersubtrees. If I<nc> contains no permitted subtrees of that type, no 67*78e936b2SEnji Cooperpermitted-subtrees test is imposed on names of that type. 68*78e936b2SEnji Cooper 69*78e936b2SEnji Cooper=item * 70*78e936b2SEnji Cooper 71*78e936b2SEnji CooperThe name must not match any B<excludedSubtree> of the same general-name 72*78e936b2SEnji Coopertype in I<nc>. 73*78e936b2SEnji Cooper 74*78e936b2SEnji Cooper=back 75*78e936b2SEnji Cooper 76*78e936b2SEnji CooperThe function returns at the first violation encountered; it does not 77*78e936b2SEnji Coopercollect or report multiple failures. 78*78e936b2SEnji Cooper 79*78e936b2SEnji CooperFor B<dNSName> entries, matching follows the byte/label algorithm of 80*78e936b2SEnji CooperRFC 5280 section 4.2.1.10, which RFC 5280 mandates when no 81*78e936b2SEnji Cooperprotocol-specific matching rules apply. Because this match is performed 82*78e936b2SEnji Cooperwithout awareness of any specific higher-level protocol, additional 83*78e936b2SEnji Coopermatching rules defined by later or more specific protocols must be 84*78e936b2SEnji Cooperapplied independently of this function to the certificate chain. 85*78e936b2SEnji Cooper 86*78e936b2SEnji CooperNAME_CONSTRAINTS_check() performs only the constraint match for a single 87*78e936b2SEnji Coopercertificate against a single constraint set. It does B<not> perform the 88*78e936b2SEnji Cooperchain-wide enforcement of RFC 5280 section 6.1.4(g)-(j): callers wishing to 89*78e936b2SEnji Cooperenforce name constraints across an entire certification path must walk the 90*78e936b2SEnji Cooperchain themselves and apply each ancestor's constraint set to certificates 91*78e936b2SEnji Cooperlower in the chain, observing the usual exceptions (for example, 92*78e936b2SEnji Cooperself-issued intermediate certificates are exempt from constraints imposed 93*78e936b2SEnji Cooperby certificates above them, except when they are the leaf of the chain). 94*78e936b2SEnji CooperFor full RFC 5280 name-constraint enforcement integrated with chain 95*78e936b2SEnji Coopervalidation, applications should use L<X509_verify_cert(3)>, which performs 96*78e936b2SEnji Cooperthis internally. 97*78e936b2SEnji Cooper 98*78e936b2SEnji CooperNAME_CONSTRAINTS_check() enforces an implementation limit on the product 99*78e936b2SEnji Cooperof the certificate's name count and the constraint set's subtree count, to 100*78e936b2SEnji Cooperprevent computationally expensive matching on pathological input. If that 101*78e936b2SEnji Cooperlimit is exceeded the function returns B<X509_V_ERR_UNSPECIFIED> without 102*78e936b2SEnji Cooperperforming any matching. The current limit is 2**20 (1,048,576) on the 103*78e936b2SEnji Cooperproduct of the name count (subject DN entries plus B<subjectAltName> 104*78e936b2SEnji Cooperentries) and the subtree count (B<permittedSubtrees> plus 105*78e936b2SEnji CooperB<excludedSubtrees>). 106*78e936b2SEnji Cooper 107*78e936b2SEnji Cooper=head1 RETURN VALUES 108*78e936b2SEnji Cooper 109*78e936b2SEnji CooperNAME_CONSTRAINTS_check() returns B<X509_V_OK> if every name considered 110*78e936b2SEnji Coopersatisfies the constraints. Otherwise it returns one of the following 111*78e936b2SEnji CooperB<X509_V_ERR_*> codes: 112*78e936b2SEnji Cooper 113*78e936b2SEnji Cooper=over 4 114*78e936b2SEnji Cooper 115*78e936b2SEnji Cooper=item B<X509_V_ERR_PERMITTED_VIOLATION> 116*78e936b2SEnji Cooper 117*78e936b2SEnji CooperA name of a type for which I<nc> contains at least one permitted subtree 118*78e936b2SEnji Cooperfailed to match any of those subtrees. 119*78e936b2SEnji Cooper 120*78e936b2SEnji Cooper=item B<X509_V_ERR_EXCLUDED_VIOLATION> 121*78e936b2SEnji Cooper 122*78e936b2SEnji CooperA name matched an excluded subtree. 123*78e936b2SEnji Cooper 124*78e936b2SEnji Cooper=item B<X509_V_ERR_SUBTREE_MINMAX> 125*78e936b2SEnji Cooper 126*78e936b2SEnji CooperA subtree in I<nc> specified a B<minimum> other than 0 or a B<maximum> at 127*78e936b2SEnji Cooperall. RFC 5280 requires that these B<GeneralSubtree> fields not be used, 128*78e936b2SEnji Cooperand a constraint set that uses them cannot be processed. 129*78e936b2SEnji Cooper 130*78e936b2SEnji Cooper=item B<X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE> 131*78e936b2SEnji Cooper 132*78e936b2SEnji CooperA general-name type for which matching is not implemented was encountered. 133*78e936b2SEnji CooperThe list of supported types is given in the DESCRIPTION above. 134*78e936b2SEnji Cooper 135*78e936b2SEnji Cooper=item B<X509_V_ERR_UNSUPPORTED_NAME_SYNTAX> 136*78e936b2SEnji Cooper 137*78e936b2SEnji CooperA name in the certificate is encoded in a way that cannot be matched (for 138*78e936b2SEnji Cooperexample, an B<emailAddress> attribute in the subject that is not encoded 139*78e936b2SEnji Cooperas an B<IA5String>). 140*78e936b2SEnji Cooper 141*78e936b2SEnji Cooper=item B<X509_V_ERR_UNSPECIFIED> 142*78e936b2SEnji Cooper 143*78e936b2SEnji CooperThe product of the certificate's name count and the constraint set's 144*78e936b2SEnji Coopersubtree count exceeded the implementation limit; no matching was 145*78e936b2SEnji Cooperperformed. 146*78e936b2SEnji Cooper 147*78e936b2SEnji Cooper=back 148*78e936b2SEnji Cooper 149*78e936b2SEnji CooperOther B<X509_V_ERR_*> codes may be returned by deeper name-matching 150*78e936b2SEnji Cooperhelpers (for example, codes arising from individual general-name type 151*78e936b2SEnji Coopercomparisons). Callers should treat the return value as the authoritative 152*78e936b2SEnji Coopersuccess/failure signal and treat any value other than B<X509_V_OK> as a 153*78e936b2SEnji Cooperfailure, rather than enumerating the specific codes above. 154*78e936b2SEnji Cooper 155*78e936b2SEnji Cooper=head1 NOTES 156*78e936b2SEnji Cooper 157*78e936b2SEnji CooperNAME_CONSTRAINTS_check() does not match the certificate's commonName 158*78e936b2SEnji Cooperagainst B<dNSName> name constraints; that check is provided by a separate 159*78e936b2SEnji Cooperfunction, B<NAME_CONSTRAINTS_check_CN>(). The commonName-as-DNS-identity 160*78e936b2SEnji Cooperpractice is a legacy concern: modern certificates assert DNS identities 161*78e936b2SEnji Cooperthrough B<dNSName> entries in the subject alternative name extension, 162*78e936b2SEnji Cooperwhich NAME_CONSTRAINTS_check() already covers. NAME_CONSTRAINTS_check_CN() 163*78e936b2SEnji Cooperis required only for older certificates that express a DNS identity 164*78e936b2SEnji Cooperthrough their commonName instead of, or in addition to, the SAN; for 165*78e936b2SEnji Coopercertificates conforming to modern profiles a call to NAME_CONSTRAINTS_check() 166*78e936b2SEnji Cooperalone is generally sufficient. 167*78e936b2SEnji Cooper 168*78e936b2SEnji Cooper=head1 BUGS 169*78e936b2SEnji Cooper 170*78e936b2SEnji CooperRFC 9525's wildcard semantics apply only to presented-identifier 171*78e936b2SEnji Coopermatching for TLS service identity, and explicitly call out they are not 172*78e936b2SEnji Coopervalid for any other purpose; they do not define wildcard handling 173*78e936b2SEnji Cooperfor name-constraint matching. NAME_CONSTRAINTS_check() therefore 174*78e936b2SEnji Cooperfollows RFC 5280's requirements for when this is undefined, and treats 175*78e936b2SEnji Cooperthe B<*> character in a B<dNSName> as a literal label component, per 176*78e936b2SEnji Cooperthe RFC 5280 algorithm, which is often contrary to caller expectation. 177*78e936b2SEnji Cooper 178*78e936b2SEnji CooperEven if specified in the future, due to the "fallback implementation" 179*78e936b2SEnji Coopernature of matching wildcards in SAN B<dNSName> entries specified by 180*78e936b2SEnji CooperRFC 5280, name constraint behaviour in the presence of wildcards 181*78e936b2SEnji Coopershould not be strictly relied upon across implementations and 182*78e936b2SEnji Cooperprotocols. This matters most for the use of B<excluded names> 183*78e936b2SEnji Cooperconstraints, which should not be relied upon to reliably constrain 184*78e936b2SEnji Coopersigning certificates for a PKI in a security dependent manner unless 185*78e936b2SEnji Cooperthe consumers of these certificates are themselves known to be 186*78e936b2SEnji Cooperconstrained by other means to only use implementations that provide 187*78e936b2SEnji Cooperdifferent semantics, or the PKI can be constrained by other means to 188*78e936b2SEnji Cooperensure that wildcards are never issued from such signing 189*78e936b2SEnji Coopercertificates. 190*78e936b2SEnji Cooper 191*78e936b2SEnji Cooper=head1 SEE ALSO 192*78e936b2SEnji Cooper 193*78e936b2SEnji CooperL<X509_verify_cert(3)>, 194*78e936b2SEnji CooperL<X509_VERIFY_PARAM_set_flags(3)> 195*78e936b2SEnji Cooper 196*78e936b2SEnji Cooper=head1 HISTORY 197*78e936b2SEnji Cooper 198*78e936b2SEnji CooperNAME_CONSTRAINTS_check() was added in OpenSSL 1.0.0. 199*78e936b2SEnji Cooper 200*78e936b2SEnji CooperNAME_CONSTRAINTS_check_CN() was added in OpenSSL 1.1.0. 201*78e936b2SEnji Cooper 202*78e936b2SEnji Cooper=head1 COPYRIGHT 203*78e936b2SEnji Cooper 204*78e936b2SEnji CooperCopyright 2026 The OpenSSL Project Authors. All Rights Reserved. 205*78e936b2SEnji Cooper 206*78e936b2SEnji CooperLicensed under the Apache License 2.0 (the "License"). You may not use 207*78e936b2SEnji Cooperthis file except in compliance with the License. You can obtain a copy 208*78e936b2SEnji Cooperin the file LICENSE in the source distribution or at 209*78e936b2SEnji CooperL<https://www.openssl.org/source/license.html>. 210*78e936b2SEnji Cooper 211*78e936b2SEnji Cooper=cut 212