xref: /freebsd/crypto/openssl/doc/man3/SSL_set1_host.pod (revision e7be843b4a162e68651d3911f0357ed464915629)
1=pod
2
3=head1 NAME
4
5SSL_set1_host, SSL_add1_host, SSL_set_hostflags, SSL_get0_peername -
6SSL server verification parameters
7
8=head1 SYNOPSIS
9
10 #include <openssl/ssl.h>
11
12 int SSL_set1_host(SSL *s, const char *host);
13 int SSL_add1_host(SSL *s, const char *host);
14 void SSL_set_hostflags(SSL *s, unsigned int flags);
15 const char *SSL_get0_peername(SSL *s);
16
17=head1 DESCRIPTION
18
19These functions configure server hostname checks in the SSL client.
20
21SSL_set1_host() sets in the verification parameters of I<s>
22the expected DNS hostname or IP address to I<host>,
23clearing any previously specified IP address and hostnames.
24If I<host> is NULL or the empty string, IP address
25and hostname checks are not performed on the peer certificate.
26When a nonempty I<host> is specified, certificate verification automatically
27checks the peer hostname via L<X509_check_host(3)> with I<flags> as specified
28via SSL_set_hostflags().  Clients that enable DANE TLSA authentication
29via L<SSL_dane_enable(3)> should leave it to that function to set
30the primary reference identifier of the peer, and should not call
31SSL_set1_host().
32
33SSL_add1_host() adds I<host> as an additional reference identifier
34that can match the peer's certificate.  Any previous hostnames
35set via SSL_set1_host() or SSL_add1_host() are retained.
36Adding an IP address is allowed only if no IP address has been set before.
37No change is made if I<host> is NULL or empty.
38When an IP address and/or multiple hostnames are configured,
39the peer is considered verified when any of these matches.
40This function is required for DANE TLSA in the presence of service name indirection
41via CNAME, MX or SRV records as specified in RFCs 7671, 7672, and 7673.
42
43TLS clients are recommended to use SSL_set1_host() or SSL_add1_host()
44for server hostname or IP address validation,
45as well as L<SSL_set_tlsext_host_name(3)> for Server Name Indication (SNI),
46which may be crucial also for correct routing of the connection request.
47
48SSL_set_hostflags() sets the I<flags> that will be passed to
49L<X509_check_host(3)> when name checks are applicable, by default
50the I<flags> value is 0.  See L<X509_check_host(3)> for the list
51of available flags and their meaning.
52
53SSL_get0_peername() returns the DNS hostname or subject CommonName
54from the peer certificate that matched one of the reference
55identifiers.  When wildcard matching is not disabled, the name
56matched in the peer certificate may be a wildcard name.  When one
57of the reference identifiers configured via SSL_set1_host() or
58SSL_add1_host() starts with ".", which indicates a parent domain prefix
59rather than a fixed name, the matched peer name may be a sub-domain
60of the reference identifier.  The returned string is allocated by
61the library and is no longer valid once the associated I<ssl> handle
62is cleared or freed, or a renegotiation takes place.  Applications
63must not free the return value.
64
65SSL clients are advised to use these functions in preference to
66explicitly calling L<X509_check_host(3)>.  Hostname checks may be out
67of scope with the RFC 7671 DANE-EE(3) certificate usage, and the
68internal check will be suppressed as appropriate when DANE is
69enabled.
70
71=head1 RETURN VALUES
72
73SSL_set1_host() and SSL_add1_host() return 1 for success and 0 for
74failure.
75
76SSL_set_hostflags() returns nothing at all.
77
78SSL_get0_peername() returns NULL if peername verification is not
79applicable (as with RFC 7671 DANE-EE(3)), or no trusted peername was
80matched.  Otherwise, it returns the matched peername.  To determine
81whether verification succeeded call L<SSL_get_verify_result(3)>.
82
83=head1 EXAMPLES
84
85Suppose "smtp.example.com" is the MX host of the domain "example.com".
86The calls below will arrange to match either the MX hostname or the
87destination domain name in the SMTP server certificate.  Wildcards
88are supported, but must match the entire label.  The actual name
89matched in the certificate (which might be a wildcard) is retrieved,
90and must be copied by the application if it is to be retained beyond
91the lifetime of the SSL connection.
92
93 SSL_set_hostflags(ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
94 if (!SSL_set1_host(ssl, "smtp.example.com"))
95     /* error */
96 if (!SSL_add1_host(ssl, "example.com"))
97     /* error */
98
99 /* XXX: Perform SSL_connect() handshake and handle errors here */
100
101 if (SSL_get_verify_result(ssl) == X509_V_OK) {
102     const char *peername = SSL_get0_peername(ssl);
103
104     if (peername != NULL)
105         /* Name checks were in scope and matched the peername */
106 }
107
108=head1 SEE ALSO
109
110L<ssl(7)>,
111L<X509_check_host(3)>, L<SSL_set_tlsext_host_name(3)>,
112L<SSL_get_verify_result(3)>, L<SSL_dane_enable(3)>
113
114=head1 HISTORY
115
116These functions were added in OpenSSL 1.1.0.
117
118=head1 COPYRIGHT
119
120Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
121
122Licensed under the Apache License 2.0 (the "License").  You may not use
123this file except in compliance with the License.  You can obtain a copy
124in the file LICENSE in the source distribution or at
125L<https://www.openssl.org/source/license.html>.
126
127=cut
128