
#use wml::openssl-macros area=docs page=SSL_CTX_set_generate_session_id

<title>Documents, SSL_CTX_set_generate_session_id(3)</title>

<h1>SSL_CTX_set_generate_session_id(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#NOTES">NOTES</A>
	<LI><A HREF="#EXAMPLES">EXAMPLES</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
	<LI><A HREF="#HISTORY">HISTORY</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
SSL_CTX_set_generate_session_id, SSL_set_generate_session_id,
SSL_has_matching_session_id - manipulate generation of SSL session IDs
(server only)

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/ssl.h&gt;
</PRE>
<PRE> typedef int (*GEN_SESSION_CB)(const SSL *ssl, unsigned char *id,
                               unsigned int *id_len);
</PRE>
<PRE> int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb);
 int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB, cb);
 int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
                                 unsigned int id_len);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>SSL_CTX_set_generate_session_id()</CODE> sets the callback function
for generating new session ids for SSL/TLS sessions for <STRONG>ctx</STRONG> to be <STRONG>cb</STRONG>.

</P>
<P>
<CODE>SSL_set_generate_session_id()</CODE> sets the callback function for
generating new session ids for SSL/TLS sessions for <STRONG>ssl</STRONG> to be <STRONG>cb</STRONG>.

</P>
<P>
<CODE>SSL_has_matching_session_id()</CODE> checks, whether a session with
id <STRONG>id</STRONG>
(of length <STRONG>id_len</STRONG>) is already contained in the internal session cache of the parent context
of <STRONG>ssl</STRONG>.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
When a new session is established between client and server, the server
generates a session id. The session id is an arbitrary sequence of bytes.
The length of the session id is 16 bytes for SSLv2 sessions and between 1
and 32 bytes for SSLv3/TLSv1. The session id is not security critical but
must be unique for the server. Additionally, the session id is transmitted
in the clear when reusing the session so it must not contain sensitive
information.

</P>
<P>
Without a callback being set, an OpenSSL server will generate a unique
session id from pseudo random numbers of the maximum possible length. Using
the callback function, the session id can be changed to contain additional
information like e.g. a host id in order to improve load balancing or
external caching techniques.

</P>
<P>
The callback function receives a pointer to the memory location to put
<STRONG>id</STRONG> into and a pointer to the maximum allowed length <STRONG>id_len</STRONG>. The buffer at location <STRONG>id</STRONG> is only guaranteed to have the size <STRONG>id_len</STRONG>. The callback is only allowed to generate a shorter id and reduce <STRONG>id_len</STRONG>; the callback <STRONG>must never</STRONG> increase <STRONG>id_len</STRONG> or write to the location
<STRONG>id</STRONG> exceeding the given limit.

</P>
<P>
If a SSLv2 session id is generated and <STRONG>id_len</STRONG> is reduced, it will be restored after the callback has finished and the
session id will be padded with 0x00. It is not recommended to change the <STRONG>id_len</STRONG> for SSLv2 sessions. The callback can use the <A HREF="../ssl/SSL_get_version.html#">SSL_get_version(3)</A> function to check, whether the session is of type SSLv2.

</P>
<P>
The location <STRONG>id</STRONG> is filled with 0x00 before the callback is called, so the callback may only
fill part of the possible length and leave <STRONG>id_len</STRONG>
untouched while maintaining reproducibility.

</P>
<P>
Since the sessions must be distinguished, session ids must be unique.
Without the callback a random number is used, so that the probability of
generating the same session id is extremely small (2^128 possible ids for
an SSLv2 session, 2^256 for SSLv3/TLSv1). In order to assure the uniqueness
of the generated session id, the callback must call
<CODE>SSL_has_matching_session_id()</CODE> and generate another id if a
conflict occurs. If an id conflict is not resolved, the handshake will
fail. If the application codes e.g. a unique host id, a unique process
number, and a unique sequence number into the session id, uniqueness could
easily be achieved without randomness added (it should however be taken
care that no confidential information is leaked this way). If the
application can not guarantee uniqueness, it is recommended to use the
maximum <STRONG>id_len</STRONG> and fill in the bytes not used to code special information with random data
to avoid collisions.

</P>
<P>
<CODE>SSL_has_matching_session_id()</CODE> will only query the internal
session cache, not the external one. Since the session id is generated
before the handshake is completed, it is not immediately added to the
cache. If another thread is using the same internal session cache, a race
condition can occur in that another thread generates the same session id.
Collisions can also occur when using an external session cache, since the
external cache is not tested with
<CODE>SSL_has_matching_session_id()</CODE> and the same race condition
applies.

</P>
<P>
When calling <CODE>SSL_has_matching_session_id()</CODE> for an SSLv2
session with reduced <STRONG>id_len</STRONG>, the match operation will be performed using the fixed length required and
with a 0x00 padded id.

</P>
<P>
The callback must return 0 if it cannot generate a session id for whatever
reason and return 1 on success.

</P>
<P>
<HR>
<H1><A NAME="EXAMPLES">EXAMPLES</A></H1>
<P>
The callback function listed will generate a session id with the server id
given, and will fill the rest with pseudo random bytes:

</P>
<PRE> const char session_id_prefix = &quot;www-18&quot;;
</PRE>
<PRE> #define MAX_SESSION_ID_ATTEMPTS 10
 static int generate_session_id(const SSL *ssl, unsigned char *id,
                              unsigned int *id_len)
      {
      unsigned int count = 0;
      const char *version;
</PRE>
<PRE>      version = SSL_get_version(ssl);
      if (!strcmp(version, &quot;SSLv2&quot;))
          /* we must not change id_len */;
</PRE>
<PRE>      do      {
              RAND_pseudo_bytes(id, *id_len);
              /* Prefix the session_id with the required prefix. NB: If our
               * prefix is too long, clip it - but there will be worse effects
               * anyway, eg. the server could only possibly create 1 session
               * ID (ie. the prefix!) so all future session negotiations will
               * fail due to conflicts. */
              memcpy(id, session_id_prefix,
                      (strlen(session_id_prefix) &lt; *id_len) ?
                      strlen(session_id_prefix) : *id_len);
              }
      while(SSL_has_matching_session_id(ssl, id, *id_len) &amp;&amp;
              (++count &lt; MAX_SESSION_ID_ATTEMPTS));
      if(count &gt;= MAX_SESSION_ID_ATTEMPTS)
              return 0;
      return 1;
      }
</PRE>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>SSL_CTX_set_generate_session_id()</CODE> and
<CODE>SSL_set_generate_session_id()</CODE> always return 1.

</P>
<P>
<CODE>SSL_has_matching_session_id()</CODE> returns 1 if another session
with the same id is already in the cache.

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../ssl/ssl.html#">ssl(3)</A>, <A HREF="../ssl/SSL_get_version.html#">SSL_get_version(3)</A>



</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
<CODE>SSL_CTX_set_generate_session_id(),</CODE>
<CODE>SSL_set_generate_session_id()</CODE> and
<CODE>SSL_has_matching_session_id()</CODE> have been introduced in OpenSSL
0.9.7.

</P>
:}

