1*28b5fa2cSschwarze.\"	$OpenBSD: SSL_CTX_set_generate_session_id.3,v 1.5 2018/03/22 21:09:18 schwarze Exp $
23eca2c13Sschwarze.\"	OpenSSL b97fdb57 Nov 11 09:33:09 2016 +0100
3f1a3c524Sschwarze.\"
43eca2c13Sschwarze.\" This file was written by Lutz Jaenicke <jaenicke@openssl.org>.
53eca2c13Sschwarze.\" Copyright (c) 2001, 2014 The OpenSSL Project.  All rights reserved.
6f1a3c524Sschwarze.\"
73eca2c13Sschwarze.\" Redistribution and use in source and binary forms, with or without
83eca2c13Sschwarze.\" modification, are permitted provided that the following conditions
93eca2c13Sschwarze.\" are met:
103eca2c13Sschwarze.\"
113eca2c13Sschwarze.\" 1. Redistributions of source code must retain the above copyright
123eca2c13Sschwarze.\"    notice, this list of conditions and the following disclaimer.
133eca2c13Sschwarze.\"
143eca2c13Sschwarze.\" 2. Redistributions in binary form must reproduce the above copyright
153eca2c13Sschwarze.\"    notice, this list of conditions and the following disclaimer in
163eca2c13Sschwarze.\"    the documentation and/or other materials provided with the
173eca2c13Sschwarze.\"    distribution.
183eca2c13Sschwarze.\"
193eca2c13Sschwarze.\" 3. All advertising materials mentioning features or use of this
203eca2c13Sschwarze.\"    software must display the following acknowledgment:
213eca2c13Sschwarze.\"    "This product includes software developed by the OpenSSL Project
223eca2c13Sschwarze.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
233eca2c13Sschwarze.\"
243eca2c13Sschwarze.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
253eca2c13Sschwarze.\"    endorse or promote products derived from this software without
263eca2c13Sschwarze.\"    prior written permission. For written permission, please contact
273eca2c13Sschwarze.\"    openssl-core@openssl.org.
283eca2c13Sschwarze.\"
293eca2c13Sschwarze.\" 5. Products derived from this software may not be called "OpenSSL"
303eca2c13Sschwarze.\"    nor may "OpenSSL" appear in their names without prior written
313eca2c13Sschwarze.\"    permission of the OpenSSL Project.
323eca2c13Sschwarze.\"
333eca2c13Sschwarze.\" 6. Redistributions of any form whatsoever must retain the following
343eca2c13Sschwarze.\"    acknowledgment:
353eca2c13Sschwarze.\"    "This product includes software developed by the OpenSSL Project
363eca2c13Sschwarze.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
373eca2c13Sschwarze.\"
383eca2c13Sschwarze.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
393eca2c13Sschwarze.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
403eca2c13Sschwarze.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
413eca2c13Sschwarze.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
423eca2c13Sschwarze.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
433eca2c13Sschwarze.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
443eca2c13Sschwarze.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
453eca2c13Sschwarze.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
463eca2c13Sschwarze.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
473eca2c13Sschwarze.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
483eca2c13Sschwarze.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
493eca2c13Sschwarze.\" OF THE POSSIBILITY OF SUCH DAMAGE.
503eca2c13Sschwarze.\"
51*28b5fa2cSschwarze.Dd $Mdocdate: March 22 2018 $
52f1a3c524Sschwarze.Dt SSL_CTX_SET_GENERATE_SESSION_ID 3
53f1a3c524Sschwarze.Os
54f1a3c524Sschwarze.Sh NAME
55f1a3c524Sschwarze.Nm SSL_CTX_set_generate_session_id ,
56f1a3c524Sschwarze.Nm SSL_set_generate_session_id ,
5730dcf770Sschwarze.Nm SSL_has_matching_session_id ,
5830dcf770Sschwarze.Nm GEN_SESSION_CB
59f1a3c524Sschwarze.Nd manipulate generation of SSL session IDs (server only)
60f1a3c524Sschwarze.Sh SYNOPSIS
61f1a3c524Sschwarze.In openssl/ssl.h
623eca2c13Sschwarze.Ft typedef int
633eca2c13Sschwarze.Fo (*GEN_SESSION_CB)
643eca2c13Sschwarze.Fa "const SSL *ssl"
653eca2c13Sschwarze.Fa "unsigned char *id"
663eca2c13Sschwarze.Fa "unsigned int *id_len"
673eca2c13Sschwarze.Fc
68f1a3c524Sschwarze.Ft int
69f1a3c524Sschwarze.Fn SSL_CTX_set_generate_session_id "SSL_CTX *ctx" "GEN_SESSION_CB cb"
70f1a3c524Sschwarze.Ft int
71b41b3495Sschwarze.Fn SSL_set_generate_session_id "SSL *ssl" "GEN_SESSION_CB cb"
72f1a3c524Sschwarze.Ft int
73f1a3c524Sschwarze.Fo SSL_has_matching_session_id
74f1a3c524Sschwarze.Fa "const SSL *ssl" "const unsigned char *id" "unsigned int id_len"
75f1a3c524Sschwarze.Fc
76f1a3c524Sschwarze.Sh DESCRIPTION
77f1a3c524Sschwarze.Fn SSL_CTX_set_generate_session_id
78f1a3c524Sschwarzesets the callback function for generating new session ids for SSL/TLS sessions
79f1a3c524Sschwarzefor
80f1a3c524Sschwarze.Fa ctx
81f1a3c524Sschwarzeto be
82f1a3c524Sschwarze.Fa cb .
83f1a3c524Sschwarze.Pp
84f1a3c524Sschwarze.Fn SSL_set_generate_session_id
85f1a3c524Sschwarzesets the callback function for generating new session ids for SSL/TLS sessions
86f1a3c524Sschwarzefor
87f1a3c524Sschwarze.Fa ssl
88f1a3c524Sschwarzeto be
89f1a3c524Sschwarze.Fa cb .
90f1a3c524Sschwarze.Pp
91f1a3c524Sschwarze.Fn SSL_has_matching_session_id
92f1a3c524Sschwarzechecks, whether a session with id
93f1a3c524Sschwarze.Fa id
94f1a3c524Sschwarze(of length
95f1a3c524Sschwarze.Fa id_len )
96f1a3c524Sschwarzeis already contained in the internal session cache
97f1a3c524Sschwarzeof the parent context of
98f1a3c524Sschwarze.Fa ssl .
993eca2c13Sschwarze.Pp
100f1a3c524SschwarzeWhen a new session is established between client and server,
101f1a3c524Sschwarzethe server generates a session id.
102f1a3c524SschwarzeThe session id is an arbitrary sequence of bytes.
1033eca2c13SschwarzeThe length of the session id is between 1 and 32 bytes.
104f1a3c524SschwarzeThe session id is not security critical but must be unique for the server.
105f1a3c524SschwarzeAdditionally, the session id is transmitted in the clear when reusing the
106f1a3c524Sschwarzesession so it must not contain sensitive information.
107f1a3c524Sschwarze.Pp
108f1a3c524SschwarzeWithout a callback being set, an OpenSSL server will generate a unique session
109f1a3c524Sschwarzeid from pseudo random numbers of the maximum possible length.
110f1a3c524SschwarzeUsing the callback function, the session id can be changed to contain
111f1a3c524Sschwarzeadditional information like, e.g., a host id in order to improve load balancing
112f1a3c524Sschwarzeor external caching techniques.
113f1a3c524Sschwarze.Pp
114f1a3c524SschwarzeThe callback function receives a pointer to the memory location to put
115f1a3c524Sschwarze.Fa id
116f1a3c524Sschwarzeinto and a pointer to the maximum allowed length
117f1a3c524Sschwarze.Fa id_len .
118f1a3c524SschwarzeThe buffer at location
119f1a3c524Sschwarze.Fa id
120f1a3c524Sschwarzeis only guaranteed to have the size
121f1a3c524Sschwarze.Fa id_len .
122f1a3c524SschwarzeThe callback is only allowed to generate a shorter id and reduce
123f1a3c524Sschwarze.Fa id_len ;
124f1a3c524Sschwarzethe callback
125f1a3c524Sschwarze.Em must never
126f1a3c524Sschwarzeincrease
127f1a3c524Sschwarze.Fa id_len
128f1a3c524Sschwarzeor write to the location
129f1a3c524Sschwarze.Fa id
130f1a3c524Sschwarzeexceeding the given limit.
131f1a3c524Sschwarze.Pp
132f1a3c524SschwarzeThe location
133f1a3c524Sschwarze.Fa id
134f1a3c524Sschwarzeis filled with 0x00 before the callback is called,
135f1a3c524Sschwarzeso the callback may only fill part of the possible length and leave
136f1a3c524Sschwarze.Fa id_len
137f1a3c524Sschwarzeuntouched while maintaining reproducibility.
138f1a3c524Sschwarze.Pp
139f1a3c524SschwarzeSince the sessions must be distinguished, session ids must be unique.
140f1a3c524SschwarzeWithout the callback a random number is used,
141f1a3c524Sschwarzeso that the probability of generating the same session id is extremely small
1423eca2c13Sschwarze(2^256 for TLSv1).
143f1a3c524SschwarzeIn order to ensure the uniqueness of the generated session id,
144f1a3c524Sschwarzethe callback must call
145f1a3c524Sschwarze.Fn SSL_has_matching_session_id
146f1a3c524Sschwarzeand generate another id if a conflict occurs.
147f1a3c524SschwarzeIf an id conflict is not resolved, the handshake will fail.
148f1a3c524SschwarzeIf the application codes, e.g., a unique host id, a unique process number, and
149f1a3c524Sschwarzea unique sequence number into the session id, uniqueness could easily be
150f1a3c524Sschwarzeachieved without randomness added (it should however be taken care that
151f1a3c524Sschwarzeno confidential information is leaked this way).
152f1a3c524SschwarzeIf the application cannot guarantee uniqueness,
153f1a3c524Sschwarzeit is recommended to use the maximum
154f1a3c524Sschwarze.Fa id_len
155f1a3c524Sschwarzeand fill in the bytes not used to code special information with random data to
156f1a3c524Sschwarzeavoid collisions.
157f1a3c524Sschwarze.Pp
158f1a3c524Sschwarze.Fn SSL_has_matching_session_id
159f1a3c524Sschwarzewill only query the internal session cache, not the external one.
160f1a3c524SschwarzeSince the session id is generated before the handshake is completed,
161f1a3c524Sschwarzeit is not immediately added to the cache.
162f1a3c524SschwarzeIf another thread is using the same internal session cache,
163f1a3c524Sschwarzea race condition can occur in that another thread generates the same session id.
164f1a3c524SschwarzeCollisions can also occur when using an external session cache,
165f1a3c524Sschwarzesince the external cache is not tested with
166f1a3c524Sschwarze.Fn SSL_has_matching_session_id
167f1a3c524Sschwarzeand the same race condition applies.
168f1a3c524Sschwarze.Pp
169f1a3c524SschwarzeThe callback must return 0 if it cannot generate a session id for whatever
170f1a3c524Sschwarzereason and return 1 on success.
171f1a3c524Sschwarze.Sh RETURN VALUES
172f1a3c524Sschwarze.Fn SSL_CTX_set_generate_session_id
173f1a3c524Sschwarzeand
174f1a3c524Sschwarze.Fn SSL_set_generate_session_id
175f1a3c524Sschwarzealways return 1.
176f1a3c524Sschwarze.Pp
177f1a3c524Sschwarze.Fn SSL_has_matching_session_id
178f1a3c524Sschwarzereturns 1 if another session with the same id is already in the cache.
179f1a3c524Sschwarze.Sh EXAMPLES
180f1a3c524SschwarzeThe callback function listed will generate a session id with the server id
181f1a3c524Sschwarzegiven, and will fill the rest with pseudo random bytes:
182f1a3c524Sschwarze.Bd -literal
183f1a3c524Sschwarzeconst char session_id_prefix = "www-18";
184f1a3c524Sschwarze
185f1a3c524Sschwarze#define MAX_SESSION_ID_ATTEMPTS 10
186f1a3c524Sschwarzestatic int
187f1a3c524Sschwarzegenerate_session_id(const SSL *ssl, unsigned char *id,
188f1a3c524Sschwarze    unsigned int *id_len)
189f1a3c524Sschwarze{
190f1a3c524Sschwarze	unsigned int count = 0;
191f1a3c524Sschwarze
192f1a3c524Sschwarze	do {
193f1a3c524Sschwarze		RAND_pseudo_bytes(id, *id_len);
194f1a3c524Sschwarze		/*
195f1a3c524Sschwarze		 * Prefix the session_id with the required prefix. NB: If
196f1a3c524Sschwarze		 * our prefix is too long, clip it \(en but there will be
197f1a3c524Sschwarze		 * worse effects anyway, e.g., the server could only
198f1a3c524Sschwarze		 * possibly create one session ID (the prefix!) so all
199f1a3c524Sschwarze		 * future session negotiations will fail due to conflicts.
200f1a3c524Sschwarze		 */
201f1a3c524Sschwarze		memcpy(id, session_id_prefix,
202f1a3c524Sschwarze		    (strlen(session_id_prefix) < *id_len) ?
203f1a3c524Sschwarze		    strlen(session_id_prefix) : *id_len);
204f1a3c524Sschwarze	} while (SSL_has_matching_session_id(ssl, id, *id_len) &&
205f1a3c524Sschwarze	    (++count < MAX_SESSION_ID_ATTEMPTS));
206f1a3c524Sschwarze
207f1a3c524Sschwarze	if (count >= MAX_SESSION_ID_ATTEMPTS)
208f1a3c524Sschwarze		return 0;
209f1a3c524Sschwarze	return 1;
210f1a3c524Sschwarze}
211f1a3c524Sschwarze.Ed
212f1a3c524Sschwarze.Sh SEE ALSO
213f1a3c524Sschwarze.Xr ssl 3 ,
214f1a3c524Sschwarze.Xr SSL_get_version 3
215f1a3c524Sschwarze.Sh HISTORY
216f1a3c524Sschwarze.Fn SSL_CTX_set_generate_session_id ,
217f1a3c524Sschwarze.Fn SSL_set_generate_session_id
218f1a3c524Sschwarzeand
219f1a3c524Sschwarze.Fn SSL_has_matching_session_id
220*28b5fa2cSschwarzefirst appeared in OpenSSL 0.9.7 and have been available since
221*28b5fa2cSschwarze.Ox 3.2 .
222