X509_CHECK_HOST(3SSL)

πŸ’– X509_CHECK_HOST β€” the OpenSSL girlies' guide to cert matching πŸ’–

okay bestie, so this is the 3SSL manual page for the OpenSSL library, and it's literally all about checking whether a certificate matches a hostname, an email address, or an IP address. no cap. let's get into it πŸ¦‹


✨ NAME ✨

X509_check_host, X509_check_email, X509_check_ip, X509_check_ip_asc β€” X.509 certificate matching, and yes she's serving πŸ’…


✨ SYNOPSIS ✨

#include <openssl/x509v3.h>

int X509_check_host(const X509 *, const char *name, size_t namelen,
                    unsigned int flags, char **peername);
int X509_check_email(const X509 *, const char *address, size_t addresslen,
                     unsigned int flags);
int X509_check_ip(const X509 *, const unsigned char *address, size_t addresslen,
                  unsigned int flags);
int X509_check_ip_asc(const X509 *, const char *address, unsigned int flags);

look at all those signatures, they're giving very much "I know what I'm doing" energy πŸ‘‘


✨ DESCRIPTION ✨

so the certificate matching functions do exactly one job and they ate: they check whether a certificate matches a given hostname, an email address, or an IP address. that's it, that's the whole vibe. πŸŽ€

⚠️ but heads up bestie β€” the validity of the certificate and its trust level have to be checked by other means. these functions are NOT doing that for you. don't get caught out there.

🌸 X509_check_host()

this one checks if the certificate's Subject Alternative Name (SAN) or Subject CommonName (CN) matches the hostname you passed in. and that hostname has to be encoded in the preferred name syntax described in section 3.5 of RFC 1034. fr fr.

by default, wildcards are supported ✨ and they match only in the left-most label β€” but (plot twist) they can also match part of that label if there's an explicit prefix or suffix. for example, by default, the host name "www.example.com" would match a certificate with a SAN or CN value of "*.example.com", "w*.example.com" or "*w.example.com". slay πŸ’…

per section 6.4.2 of RFC 6125, name values representing international domain names must be given in A-label form. no cheating.

the namelen argument must be the number of characters in the name string β€” or zero, in which case the length gets calculated with strlen(name). convenient, lowkey.

when name starts with a dot (like ".example.com"), it'll be matched by a certificate valid for any sub-domain of name β€” (see also X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS below, iykyk πŸ‘€).

and when the certificate is matched, and peername is not NULL, a pointer to a copy of the matching SAN or CN from the peer certificate gets stored at the address passed in peername. 🫢

🚨 the application is responsible for freeing the peername via OPENSSL_free() when it's no longer needed. don't leak, bestie, it's not cute.

🌸 X509_check_email()

this one checks if the certificate matches the specified email address. the mailbox syntax of RFC 822 is supported β€” but comments are not allowed, and no attempt is made to normalize quoted characters. πŸ™ˆ

the mailbox syntax of RFC 6531 is supported for SmtpUTF8Mailbox address in subjectAltName according to RFC 8398, with similar limitations as for RFC 822 syntax, and no attempt is made to convert from A-label to U-label before comparison.

the addresslen argument must be the number of characters in the address string β€” or zero, in which case the length is calculated with strlen(address). same energy as before πŸ’œ


🌸 X509_check_ip()

this one checks if the certificate matches a specified IPv4 or IPv6 address.

  • the address array is in binary format, in network byte order
  • the length is either 4 (IPv4) or 16 (IPv6)

only explicitly marked addresses in the certificates are considered β€” IP addresses stored in DNS names and Common Names are ignored. periodt.

there are currently no flags that would affect the behavior of this call. iconic, honestly.


🌸 X509_check_ip_asc()

similar to the one above, except that the NUL-terminated string address is first converted to the internal representation. ✨


πŸŽ€ the flags argument

the flags argument is usually 0. it can be the bitwise OR of the flags:

  • X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT
  • X509_CHECK_FLAG_NEVER_CHECK_SUBJECT
  • X509_CHECK_FLAG_NO_WILDCARDS
  • X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
  • X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS
  • X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS

let's break each one down, bestie, one at a time πŸ’–


⭐ X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT

this flag causes the function to consider the subject DN even if the certificate contains at least one subject alternative name of the right type (DNS name or email address as appropriate).

the default is to ignore the subject DN when at least one corresponding subject alternative names is present. 🌈


⭐ X509_CHECK_FLAG_NEVER_CHECK_SUBJECT

this flag causes the function to never consider the subject DN β€” even if the certificate contains no subject alternative names of the right type (DNS name or email address as appropriate).

the default is to use the subject DN when no corresponding subject alternative names are present. πŸ’œ

🚨 and if you specify both X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT and X509_CHECK_FLAG_NEVER_CHECK_SUBJECT, the latter takes precedence and the subject DN is not checked for matching names. no cap.

⭐ X509_CHECK_FLAG_NO_WILDCARDS

if set, this disables wildcard expansion. wildcards said "not today" πŸ’€

this only applies to X509_check_host.


⭐ X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS

if set, this suppresses support for "*" as a wildcard pattern in labels that have a prefix or suffix, such as: "www*" or "*www". πŸ“

this only applies to X509_check_host.


⭐ X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS

if set, this allows a "*" that constitutes the complete label of a DNS name (e.g. "*.example.com") to match more than one label in name. 😭

this flag only applies to X509_check_host.


⭐ X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS

if set, this restricts name values which start with "." β€” the ones that would otherwise match any sub-domain in the peer certificate β€” to only match direct child sub-domains. πŸ¦‹

so, for instance, with this flag set a name of ".example.com" would match a peer certificate with a DNS name of "www.example.com", but would not match a peer certificate with a DNS name of "www.sub.example.com". πŸ’…

this flag only applies to X509_check_host.


✨ RETURN VALUES ✨

here's the scoreboard, bestie πŸ“‹

  • 1 β€” a successful match πŸŽ‰
  • 0 β€” a failed match 😭
  • -1 β€” an internal error: typically a memory allocation failure or an ASN.1 decoding error πŸ’€

all functions can also return -2 if the input is malformed. for example, X509_check_host() returns -2 if the provided name contains embedded NULs. πŸ™ˆ


✨ NOTES ✨

applications are encouraged to use X509_VERIFY_PARAM_set1_host() rather than explicitly calling X509_check_host(3). like, pls do it this way, it's the move πŸ‘‘

hostname checks may be out of scope with the DANE-EE(3) certificate usage, and the internal checks will be suppressed as appropriate when DANE support is enabled. ✨


✨ SEE ALSO ✨

go read those too, bestie, they're all connected 🫢


✨ HISTORY ✨

these functions were added in OpenSSL 1.0.2. throwback πŸŽ€


Copyright 2012-2026 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.


and that's the whole page, bestie!! πŸ’– this one's from OpenSSL 4.0.3, dated 2026-09-29, and it's part of the 3SSL manual set β€” so if you ever forget which version you're reading, now you know. mwah πŸ’…βœ¨πŸŒŸ

πŸ“¦ where this page comes from