5 SSL_set1_host, SSL_add1_host, SSL_set_hostflags, SSL_get0_peername -
6 SSL server verification parameters
10 #include <openssl/ssl.h>
11 #include <openssl/x509_vfy.h>
13 int SSL_set1_host(SSL *s, const char *hostname);
14 int SSL_add1_host(SSL *s, const char *hostname);
15 void SSL_set_hostflags(SSL *s, unsigned int flags);
16 const char *SSL_get0_peername(SSL *s);
20 These functions configure server hostname checks in the SSL client.
22 SSL_set1_host() sets the expected DNS hostname to B<name> clearing
23 any previously specified host name or names. If B<name> is NULL,
24 or the empty string the list of hostnames is cleared, and name
25 checks are not performed on the peer certificate. When a non-empty
26 B<name> is specified, certificate verification automatically checks
27 the peer hostname via L<X509_check_host(3)> with B<flags> as specified
28 via SSL_set_hostflags(). Clients that enable DANE TLSA authentication
29 via L<SSL_dane_enable(3)> should leave it to that function to set
30 the primary reference identifier of the peer, and should not call
33 SSL_add1_host() adds B<name> as an additional reference identifier
34 that can match the peer's certificate. Any previous names set via
35 SSL_set1_host() or SSL_add1_host() are retained, no change is made
36 if B<name> is NULL or empty. When multiple names are configured,
37 the peer is considered verified when any name matches. This function
38 is required for DANE TLSA in the presence of service name indirection
39 via CNAME, MX or SRV records as specified in RFC7671, RFC7672 or
42 SSL_set_hostflags() sets the B<flags> that will be passed to
43 L<X509_check_host(3)> when name checks are applicable, by default
44 the B<flags> value is 0. See L<X509_check_host(3)> for the list
45 of available flags and their meaning.
47 SSL_get0_peername() returns the DNS hostname or subject CommonName
48 from the peer certificate that matched one of the reference
49 identifiers. When wildcard matching is not disabled, the name
50 matched in the peer certificate may be a wildcard name. When one
51 of the reference identifiers configured via SSL_set1_host() or
52 SSL_add1_host() starts with ".", which indicates a parent domain prefix
53 rather than a fixed name, the matched peer name may be a sub-domain
54 of the reference identifier. The returned string is allocated by
55 the library and is no longer valid once the associated B<ssl> handle
56 is cleared or freed, or a renegotiation takes place. Applications
57 must not free the return value.
59 SSL clients are advised to use these functions in preference to
60 explicitly calling L<X509_check_host(3)>. Hostname checks are out
61 of scope with the RFC7671 DANE-EE(3) certificate usage, and the
62 internal check will be suppressed as appropriate when DANE is
67 SSL_set1_host() and SSL_add1_host() return 1 for success and 0 for
70 SSL_get0_peername() returns NULL if peername verification is not
71 applicable (as with RFC7671 DANE-EE(3)), or no trusted peername was
72 matched. Otherwise, it returns the matched peername. To determine
73 whether verification succeeded call L<SSL_get_verify_result(3)>.
77 Suppose "smtp.example.com" is the MX host of the domain "example.com".
78 The calls below will arrange to match either the MX hostname or the
79 destination domain name in the SMTP server certificate. Wildcards
80 are supported, but must match the entire label. The actual name
81 matched in the certificate (which might be a wildcard) is retrieved,
82 and must be copied by the application if it is to be retained beyond
83 the lifetime of the SSL connection.
85 SSL_set_hostflags(ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
86 if (!SSL_set1_host(ssl, "smtp.example.com")) {
89 if (!SSL_add1_host(ssl, "example.com")) {
93 /* XXX: Perform SSL_connect() handshake and handle errors here */
95 if (SSL_get_verify_result(ssl) == X509_V_OK) {
96 const char *peername = SSL_get0_peername(ssl);
98 if (peername != NULL) {
99 /* Name checks were in scope and matched the peername */
105 L<X509_check_host(3)>,
106 L<SSL_get_verify_result(3)>.
107 L<SSL_dane_enable(3)>.
111 These functions were first added to OpenSSL 1.1.0.
117 Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
119 Licensed under the OpenSSL license (the "License"). You may not use
120 this file except in compliance with the License. You can obtain a copy
121 in the file LICENSE in the source distribution or at
122 L<https://www.openssl.org/source/license.html>.