1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5e71b7053SJung-uk KimX509_check_host, X509_check_email, X509_check_ip, X509_check_ip_asc - X.509 certificate matching 6e71b7053SJung-uk Kim 7e71b7053SJung-uk Kim=head1 SYNOPSIS 8e71b7053SJung-uk Kim 9e71b7053SJung-uk Kim #include <openssl/x509v3.h> 10e71b7053SJung-uk Kim 11e71b7053SJung-uk Kim int X509_check_host(X509 *, const char *name, size_t namelen, 12e71b7053SJung-uk Kim unsigned int flags, char **peername); 13e71b7053SJung-uk Kim int X509_check_email(X509 *, const char *address, size_t addresslen, 14e71b7053SJung-uk Kim unsigned int flags); 15e71b7053SJung-uk Kim int X509_check_ip(X509 *, const unsigned char *address, size_t addresslen, 16e71b7053SJung-uk Kim unsigned int flags); 17e71b7053SJung-uk Kim int X509_check_ip_asc(X509 *, const char *address, unsigned int flags); 18e71b7053SJung-uk Kim 19e71b7053SJung-uk Kim=head1 DESCRIPTION 20e71b7053SJung-uk Kim 21e71b7053SJung-uk KimThe certificate matching functions are used to check whether a 22e71b7053SJung-uk Kimcertificate matches a given hostname, email address, or IP address. 23e71b7053SJung-uk KimThe validity of the certificate and its trust level has to be checked by 24e71b7053SJung-uk Kimother means. 25e71b7053SJung-uk Kim 26e71b7053SJung-uk KimX509_check_host() checks if the certificate Subject Alternative 2758f35182SJung-uk KimName (SAN) or Subject CommonName (CN) matches the specified hostname, 2858f35182SJung-uk Kimwhich must be encoded in the preferred name syntax described 29e71b7053SJung-uk Kimin section 3.5 of RFC 1034. By default, wildcards are supported 30e71b7053SJung-uk Kimand they match only in the left-most label; but they may match 31e71b7053SJung-uk Kimpart of that label with an explicit prefix or suffix. For example, 32e71b7053SJung-uk Kimby default, the host B<name> "www.example.com" would match a 33e71b7053SJung-uk Kimcertificate with a SAN or CN value of "*.example.com", "w*.example.com" 34e71b7053SJung-uk Kimor "*w.example.com". 35e71b7053SJung-uk Kim 36e71b7053SJung-uk KimPer section 6.4.2 of RFC 6125, B<name> values representing international 37e71b7053SJung-uk Kimdomain names must be given in A-label form. The B<namelen> argument 38e71b7053SJung-uk Kimmust be the number of characters in the name string or zero in which 39e71b7053SJung-uk Kimcase the length is calculated with strlen(B<name>). When B<name> starts 4058f35182SJung-uk Kimwith a dot (e.g. ".example.com"), it will be matched by a certificate 41e71b7053SJung-uk Kimvalid for any sub-domain of B<name>, (see also 42e71b7053SJung-uk KimB<X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS> below). 43e71b7053SJung-uk Kim 44e71b7053SJung-uk KimWhen the certificate is matched, and B<peername> is not NULL, a 45e71b7053SJung-uk Kimpointer to a copy of the matching SAN or CN from the peer certificate 46e71b7053SJung-uk Kimis stored at the address passed in B<peername>. The application 47e71b7053SJung-uk Kimis responsible for freeing the peername via OPENSSL_free() when it 48e71b7053SJung-uk Kimis no longer needed. 49e71b7053SJung-uk Kim 50e71b7053SJung-uk KimX509_check_email() checks if the certificate matches the specified 51*b077aed3SPierre Proncheryemail B<address>. The mailbox syntax of RFC 822 is supported, 52e71b7053SJung-uk Kimcomments are not allowed, and no attempt is made to normalize quoted 53*b077aed3SPierre Proncherycharacters. The mailbox syntax of RFC 6531 is supported for 54*b077aed3SPierre ProncherySmtpUTF8Mailbox address in subjectAltName according to RFC 8398, 55*b077aed3SPierre Proncherywith similar limitations as for RFC 822 syntax, and no attempt 56*b077aed3SPierre Proncheryis made to convert from A-label to U-label before comparison. 57*b077aed3SPierre ProncheryThe B<addresslen> argument must be the number of 58e71b7053SJung-uk Kimcharacters in the address string or zero in which case the length 59e71b7053SJung-uk Kimis calculated with strlen(B<address>). 60e71b7053SJung-uk Kim 61e71b7053SJung-uk KimX509_check_ip() checks if the certificate matches a specified IPv4 or 62e71b7053SJung-uk KimIPv6 address. The B<address> array is in binary format, in network 63e71b7053SJung-uk Kimbyte order. The length is either 4 (IPv4) or 16 (IPv6). Only 64e71b7053SJung-uk Kimexplicitly marked addresses in the certificates are considered; IP 65*b077aed3SPierre Proncheryaddresses stored in DNS names and Common Names are ignored. There are 66*b077aed3SPierre Proncherycurrently no B<flags> that would affect the behavior of this call. 67e71b7053SJung-uk Kim 68e71b7053SJung-uk KimX509_check_ip_asc() is similar, except that the NUL-terminated 69e71b7053SJung-uk Kimstring B<address> is first converted to the internal representation. 70e71b7053SJung-uk Kim 71e71b7053SJung-uk KimThe B<flags> argument is usually 0. It can be the bitwise OR of the 72e71b7053SJung-uk Kimflags: 73e71b7053SJung-uk Kim 74e71b7053SJung-uk Kim=over 4 75e71b7053SJung-uk Kim 76e71b7053SJung-uk Kim=item B<X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT>, 77e71b7053SJung-uk Kim 78e71b7053SJung-uk Kim=item B<X509_CHECK_FLAG_NEVER_CHECK_SUBJECT>, 79e71b7053SJung-uk Kim 80e71b7053SJung-uk Kim=item B<X509_CHECK_FLAG_NO_WILDCARDS>, 81e71b7053SJung-uk Kim 82e71b7053SJung-uk Kim=item B<X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS>, 83e71b7053SJung-uk Kim 84e71b7053SJung-uk Kim=item B<X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS>. 85e71b7053SJung-uk Kim 86e71b7053SJung-uk Kim=item B<X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS>. 87e71b7053SJung-uk Kim 88e71b7053SJung-uk Kim=back 89e71b7053SJung-uk Kim 90e71b7053SJung-uk KimThe B<X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT> flag causes the function 91e71b7053SJung-uk Kimto consider the subject DN even if the certificate contains at least 92e71b7053SJung-uk Kimone subject alternative name of the right type (DNS name or email 93e71b7053SJung-uk Kimaddress as appropriate); the default is to ignore the subject DN 94e71b7053SJung-uk Kimwhen at least one corresponding subject alternative names is present. 95e71b7053SJung-uk Kim 96e71b7053SJung-uk KimThe B<X509_CHECK_FLAG_NEVER_CHECK_SUBJECT> flag causes the function to never 97e71b7053SJung-uk Kimconsider the subject DN even if the certificate contains no subject alternative 98e71b7053SJung-uk Kimnames of the right type (DNS name or email address as appropriate); the default 99e71b7053SJung-uk Kimis to use the subject DN when no corresponding subject alternative names are 100e71b7053SJung-uk Kimpresent. 101e71b7053SJung-uk KimIf both B<X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT> and 102e71b7053SJung-uk KimB<X509_CHECK_FLAG_NEVER_CHECK_SUBJECT> are specified, the latter takes 103e71b7053SJung-uk Kimprecedence and the subject DN is not checked for matching names. 104e71b7053SJung-uk Kim 105e71b7053SJung-uk KimIf set, B<X509_CHECK_FLAG_NO_WILDCARDS> disables wildcard 106e71b7053SJung-uk Kimexpansion; this only applies to B<X509_check_host>. 107e71b7053SJung-uk Kim 108e71b7053SJung-uk KimIf set, B<X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS> suppresses support 109e71b7053SJung-uk Kimfor "*" as wildcard pattern in labels that have a prefix or suffix, 110e71b7053SJung-uk Kimsuch as: "www*" or "*www"; this only applies to B<X509_check_host>. 111e71b7053SJung-uk Kim 112e71b7053SJung-uk KimIf set, B<X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS> allows a "*" that 113e71b7053SJung-uk Kimconstitutes the complete label of a DNS name (e.g. "*.example.com") 114e71b7053SJung-uk Kimto match more than one label in B<name>; this flag only applies 115e71b7053SJung-uk Kimto B<X509_check_host>. 116e71b7053SJung-uk Kim 117e71b7053SJung-uk KimIf set, B<X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS> restricts B<name> 118e71b7053SJung-uk Kimvalues which start with ".", that would otherwise match any sub-domain 119e71b7053SJung-uk Kimin the peer certificate, to only match direct child sub-domains. 120e71b7053SJung-uk KimThus, for instance, with this flag set a B<name> of ".example.com" 121e71b7053SJung-uk Kimwould match a peer certificate with a DNS name of "www.example.com", 122e71b7053SJung-uk Kimbut would not match a peer certificate with a DNS name of 123e71b7053SJung-uk Kim"www.sub.example.com"; this flag only applies to B<X509_check_host>. 124e71b7053SJung-uk Kim 125e71b7053SJung-uk Kim=head1 RETURN VALUES 126e71b7053SJung-uk Kim 127e71b7053SJung-uk KimThe functions return 1 for a successful match, 0 for a failed match 128e71b7053SJung-uk Kimand -1 for an internal error: typically a memory allocation failure 129e71b7053SJung-uk Kimor an ASN.1 decoding error. 130e71b7053SJung-uk Kim 131e71b7053SJung-uk KimAll functions can also return -2 if the input is malformed. For example, 132e71b7053SJung-uk KimX509_check_host() returns -2 if the provided B<name> contains embedded 133e71b7053SJung-uk KimNULs. 134e71b7053SJung-uk Kim 135e71b7053SJung-uk Kim=head1 NOTES 136e71b7053SJung-uk Kim 137e71b7053SJung-uk KimApplications are encouraged to use X509_VERIFY_PARAM_set1_host() 138e71b7053SJung-uk Kimrather than explicitly calling L<X509_check_host(3)>. Hostname 139e71b7053SJung-uk Kimchecks may be out of scope with the DANE-EE(3) certificate usage, 140e71b7053SJung-uk Kimand the internal checks will be suppressed as appropriate when 141e71b7053SJung-uk KimDANE support is enabled. 142e71b7053SJung-uk Kim 143e71b7053SJung-uk Kim=head1 SEE ALSO 144e71b7053SJung-uk Kim 145e71b7053SJung-uk KimL<SSL_get_verify_result(3)>, 146e71b7053SJung-uk KimL<X509_VERIFY_PARAM_set1_host(3)>, 147e71b7053SJung-uk KimL<X509_VERIFY_PARAM_add1_host(3)>, 148e71b7053SJung-uk KimL<X509_VERIFY_PARAM_set1_email(3)>, 149*b077aed3SPierre ProncheryL<X509_VERIFY_PARAM_set1_ip(3)> 150e71b7053SJung-uk Kim 151e71b7053SJung-uk Kim=head1 HISTORY 152e71b7053SJung-uk Kim 153e71b7053SJung-uk KimThese functions were added in OpenSSL 1.0.2. 154e71b7053SJung-uk Kim 155e71b7053SJung-uk Kim=head1 COPYRIGHT 156e71b7053SJung-uk Kim 157*b077aed3SPierre ProncheryCopyright 2012-2022 The OpenSSL Project Authors. All Rights Reserved. 158e71b7053SJung-uk Kim 159*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 160e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 161e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 162e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 163e71b7053SJung-uk Kim 164e71b7053SJung-uk Kim=cut 165