1.\" $OpenBSD: SSL_CTX_set_tlsext_servername_callback.3,v 1.6 2021/09/01 13:56:03 schwarze Exp $
2.\" full merge up to: OpenSSL 190b9a03 Jun 28 15:46:13 2017 +0800
3.\" selective merge up to: OpenSSL 6328d367 Jul 4 21:58:30 2020 +0200
4.\"
5.\" This file was written by Jon Spillett <jon.spillett@oracle.com>,
6.\" Paul Yang <yang dot yang at baishancloud dot com>, and
7.\" Matt Caswell <matt@openssl.org>.
8.\" Copyright (c) 2017, 2019 The OpenSSL Project.  All rights reserved.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\"
14.\" 1. Redistributions of source code must retain the above copyright
15.\"    notice, this list of conditions and the following disclaimer.
16.\"
17.\" 2. Redistributions in binary form must reproduce the above copyright
18.\"    notice, this list of conditions and the following disclaimer in
19.\"    the documentation and/or other materials provided with the
20.\"    distribution.
21.\"
22.\" 3. All advertising materials mentioning features or use of this
23.\"    software must display the following acknowledgment:
24.\"    "This product includes software developed by the OpenSSL Project
25.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
26.\"
27.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28.\"    endorse or promote products derived from this software without
29.\"    prior written permission. For written permission, please contact
30.\"    openssl-core@openssl.org.
31.\"
32.\" 5. Products derived from this software may not be called "OpenSSL"
33.\"    nor may "OpenSSL" appear in their names without prior written
34.\"    permission of the OpenSSL Project.
35.\"
36.\" 6. Redistributions of any form whatsoever must retain the following
37.\"    acknowledgment:
38.\"    "This product includes software developed by the OpenSSL Project
39.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
40.\"
41.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
45.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52.\" OF THE POSSIBILITY OF SUCH DAMAGE.
53.\"
54.Dd $Mdocdate: September 1 2021 $
55.Dt SSL_CTX_SET_TLSEXT_SERVERNAME_CALLBACK 3
56.Os
57.Sh NAME
58.Nm SSL_CTX_set_tlsext_servername_callback ,
59.Nm SSL_CTX_set_tlsext_servername_arg ,
60.Nm SSL_get_servername_type ,
61.Nm SSL_get_servername ,
62.Nm SSL_set_tlsext_host_name
63.Nd handle server name indication (SNI)
64.Sh SYNOPSIS
65.In openssl/ssl.h
66.Ft long
67.Fo SSL_CTX_set_tlsext_servername_callback
68.Fa "SSL_CTX *ctx"
69.Fa "int (*cb)(SSL *ssl, int *alert, void *arg)"
70.Fc
71.Ft long
72.Fo SSL_CTX_set_tlsext_servername_arg
73.Fa "SSL_CTX *ctx"
74.Fa "void *arg"
75.Fc
76.Ft const char *
77.Fo SSL_get_servername
78.Fa "const SSL *ssl"
79.Fa "const int type"
80.Fc
81.Ft int
82.Fo SSL_get_servername_type
83.Fa "const SSL *ssl"
84.Fc
85.Ft int
86.Fo SSL_set_tlsext_host_name
87.Fa "const SSL *ssl"
88.Fa "const char *name"
89.Fc
90.Sh DESCRIPTION
91.Fn SSL_CTX_set_tlsext_servername_callback
92sets the application callback
93.Fa cb
94used by a server to perform any actions or configuration required based
95on the servername extension received in the incoming connection.
96Like the ALPN callback, it is executed during Client Hello processing.
97When
98.Fa cb
99is
100.Dv NULL ,
101SNI is not used.
102.Pp
103The servername callback should return one of the following values:
104.Bl -tag -width Ds
105.It Dv SSL_TLSEXT_ERR_OK
106This is used to indicate that the servername requested by the client
107has been accepted.
108Typically a server will call
109.Xr SSL_set_SSL_CTX 3
110in the callback to set up a different configuration
111for the selected servername in this case.
112.It Dv SSL_TLSEXT_ERR_ALERT_FATAL
113In this case the servername requested by the client is not accepted
114and the handshake will be aborted.
115The value of the alert to be used should be stored in the location
116pointed to by the
117.Fa alert
118parameter to the callback.
119By default this value is initialised to
120.Dv SSL_AD_UNRECOGNIZED_NAME .
121.It Dv SSL_TLSEXT_ERR_ALERT_WARNING
122If this value is returned, then the servername is not accepted by the server.
123However, the handshake will continue and send a warning alert instead.
124The value of the alert should be stored in the location pointed to by the
125.Fa alert
126parameter as for
127.Dv SSL_TLSEXT_ERR_ALERT_FATAL
128above.
129Note that TLSv1.3 does not support warning alerts, so if TLSv1.3 has
130been negotiated then this return value is treated the same way as
131.Dv SSL_TLSEXT_ERR_NOACK .
132.It Dv SSL_TLSEXT_ERR_NOACK
133This return value indicates
134that the servername is not accepted by the server.
135No alerts are sent
136and the server will not acknowledge the requested servername.
137.El
138.Pp
139.Fn SSL_CTX_set_tlsext_servername_arg
140sets a context-specific argument to be passed into the callback via the
141.Fa arg
142parameter for
143.Fa ctx .
144.ig end_of_get_servername_details
145.\" I would suggest to comment out that second wall text of dubious
146.\" usefulness and see if we can meet all these documented API
147.\" requirements in the future or decide that it's not worth the
148.\" effort.  -- tb@ Aug 30, 2021
149.Pp
150The behaviour of
151.Fn SSL_get_servername
152depends on a number of different factors.
153In particular note that in TLSv1.3,
154the servername is negotiated in every handshake.
155In TLSv1.2 the servername is only negotiated on initial handshakes
156and not on resumption handshakes.
157.Bl -tag -width Ds
158.It On the client, before the handshake:
159If a servername has been set via a call to
160.Fn SSL_set_tlsext_host_name ,
161then it will return that servername.
162If one has not been set, but a TLSv1.2 resumption is being attempted
163and the session from the original handshake had a servername
164accepted by the server, then it will return that servername.
165Otherwise it returns
166.Dv NULL .
167.It On the client, during or after the handshake,\
168 if a TLSv1.2 (or below) resumption occurred:
169If the session from the original handshake had a servername accepted by the
170server, then it will return that servername.
171Otherwise it returns the servername set via
172.Fn SSL_set_tlsext_host_name
173or
174.Dv NULL
175if it was not called.
176.It On the client, during or after the handshake,\
177 if a TLSv1.2 (or below) resumption did not occur:
178It will return the servername set via
179.Fn SSL_set_tlsext_host_name
180or
181.Dv NULL
182if it was not called.
183.It On the server, before the handshake:
184The function will always return
185.Dv NULL
186before the handshake.
187.It On the server, after the servername extension has been processed,\
188 if a TLSv1.2 (or below) resumption occurred:
189If a servername was accepted by the server in the original handshake,
190then it will return that servername, or
191.Dv NULL
192otherwise.
193.It On the server, after the servername extension has been processed,\
194 if a TLSv1.2 (or below) resumption did not occur:
195The function will return the servername
196requested by the client in this handshake or
197.Dv NULL
198if none was requested.
199.El
200.Pp
201Note that the early callback occurs before a servername extension
202from the client is processed.
203The servername, certificate and ALPN callbacks occur
204after a servername extension from the client is processed.
205.end_of_get_servername_details
206.Pp
207.Fn SSL_set_tlsext_host_name
208sets the server name indication ClientHello extension
209to contain the value
210.Fa name ,
211or clears it if
212.Fa name
213is
214.Dv NULL .
215The type of server name indication
216extension is set to
217.Dv TLSEXT_NAMETYPE_host_name
218as defined in RFC 3546.
219.Pp
220All three functions are implemented as macros.
221.Sh RETURN VALUES
222.Fn SSL_CTX_set_tlsext_servername_callback
223and
224.Fn SSL_CTX_set_tlsext_servername_arg
225always return 1 indicating success.
226.Pp
227.Fn SSL_get_servername
228returns a servername extension value of the specified type if provided
229in the Client Hello, or
230.Dv NULL
231otherwise.
232.Pp
233.Fn SSL_get_servername_type
234returns the servername type or -1 if no servername is present.
235Currently the only supported type (defined in RFC 3546) is
236.Dv TLSEXT_NAMETYPE_host_name .
237.Pp
238.Fn SSL_set_tlsext_host_name
239returns 1 on success or 0 in case of an error.
240.Sh SEE ALSO
241.Xr ssl 3 ,
242.Xr SSL_CTX_callback_ctrl 3 ,
243.Xr SSL_CTX_set_alpn_select_cb 3
244.Sh HISTORY
245These functions first appeared in OpenSSL 0.9.8f
246and have been available since
247.Ox 4.5 .
248