SSL_CTX_set_tlsext_ticket_key_cb.pod 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. =pod
  2. =head1 NAME
  3. SSL_CTX_set_tlsext_ticket_key_evp_cb,
  4. SSL_CTX_set_tlsext_ticket_key_cb
  5. - set a callback for session ticket processing
  6. =head1 SYNOPSIS
  7. #include <openssl/tls1.h>
  8. int SSL_CTX_set_tlsext_ticket_key_evp_cb(SSL_CTX sslctx,
  9. int (*cb)(SSL *s, unsigned char key_name[16],
  10. unsigned char iv[EVP_MAX_IV_LENGTH],
  11. EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc));
  12. The following function has been deprecated since OpenSSL 3.0, and can be
  13. hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
  14. see L<openssl_user_macros(7)>:
  15. int SSL_CTX_set_tlsext_ticket_key_cb(SSL_CTX sslctx,
  16. int (*cb)(SSL *s, unsigned char key_name[16],
  17. unsigned char iv[EVP_MAX_IV_LENGTH],
  18. EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc));
  19. =head1 DESCRIPTION
  20. SSL_CTX_set_tlsext_ticket_key_evp_cb() sets a callback function I<cb> for handling
  21. session tickets for the ssl context I<sslctx>. Session tickets, defined in
  22. RFC5077 provide an enhanced session resumption capability where the server
  23. implementation is not required to maintain per session state. It only applies
  24. to TLS and there is no SSLv3 implementation.
  25. The callback function I<cb> will be called for every client instigated TLS
  26. session when session ticket extension is presented in the TLS hello
  27. message. It is the responsibility of this function to create or retrieve the
  28. cryptographic parameters and to maintain their state.
  29. The OpenSSL library uses your callback function to help implement a common TLS
  30. ticket construction state according to RFC5077 Section 4 such that per session
  31. state is unnecessary and a small set of cryptographic variables needs to be
  32. maintained by the callback function implementation.
  33. In order to reuse a session, a TLS client must send the a session ticket
  34. extension to the server. The client can only send exactly one session ticket.
  35. The server, through the callback function, either agrees to reuse the session
  36. ticket information or it starts a full TLS handshake to create a new session
  37. ticket.
  38. Before the callback function is started I<ctx> and I<hctx> have been
  39. initialised with L<EVP_CIPHER_CTX_reset(3)> and L<EVP_MAC_CTX_new(3)>
  40. respectively.
  41. For new sessions tickets, when the client doesn't present a session ticket, or
  42. an attempted retrieval of the ticket failed, or a renew option was indicated,
  43. the callback function will be called with I<enc> equal to 1. The OpenSSL
  44. library expects that the function will set an arbitrary I<name>, initialize
  45. I<iv>, and set the cipher context I<ctx> and the hash context I<hctx>.
  46. The I<name> is 16 characters long and is used as a key identifier.
  47. The I<iv> length is the length of the IV of the corresponding cipher. The
  48. maximum IV length is B<EVP_MAX_IV_LENGTH> bytes defined in F<< <openssl/evp.h> >>.
  49. The initialization vector I<iv> should be a random value. The cipher context
  50. I<ctx> should use the initialisation vector I<iv>. The cipher context can be
  51. set using L<EVP_EncryptInit_ex(3)>. The hmac context and digest can be set using
  52. L<EVP_MAC_CTX_set_params(3)> with the B<OSSL_MAC_PARAM_KEY> and
  53. B<OSSL_MAC_PARAM_DIGEST> parameters respectively.
  54. When the client presents a session ticket, the callback function with be called
  55. with I<enc> set to 0 indicating that the I<cb> function should retrieve a set
  56. of parameters. In this case I<name> and I<iv> have already been parsed out of
  57. the session ticket. The OpenSSL library expects that the I<name> will be used
  58. to retrieve a cryptographic parameters and that the cryptographic context
  59. I<ctx> will be set with the retrieved parameters and the initialization vector
  60. I<iv>. using a function like L<EVP_DecryptInit_ex(3)>. The key material and
  61. digest for I<hctx> need to be set using L<EVP_MAC_CTX_set_params(3)> with the
  62. B<OSSL_MAC_PARAM_KEY> and B<OSSL_MAC_PARAM_DIGEST> parameters respectively.
  63. If the I<name> is still valid but a renewal of the ticket is required the
  64. callback function should return 2. The library will call the callback again
  65. with an argument of enc equal to 1 to set the new ticket.
  66. The return value of the I<cb> function is used by OpenSSL to determine what
  67. further processing will occur. The following return values have meaning:
  68. =over 4
  69. =item Z<>2
  70. This indicates that the I<ctx> and I<hctx> have been set and the session can
  71. continue on those parameters. Additionally it indicates that the session
  72. ticket is in a renewal period and should be replaced. The OpenSSL library will
  73. call I<cb> again with an enc argument of 1 to set the new ticket (see RFC5077
  74. 3.3 paragraph 2).
  75. =item Z<>1
  76. This indicates that the I<ctx> and I<hctx> have been set and the session can
  77. continue on those parameters.
  78. =item Z<>0
  79. This indicates that it was not possible to set/retrieve a session ticket and
  80. the SSL/TLS session will continue by negotiating a set of cryptographic
  81. parameters or using the alternate SSL/TLS resumption mechanism, session ids.
  82. If called with enc equal to 0 the library will call the I<cb> again to get
  83. a new set of parameters.
  84. =item less than 0
  85. This indicates an error.
  86. =back
  87. The SSL_CTX_set_tlsext_ticket_key_cb() function is identical to
  88. SSL_CTX_set_tlsext_ticket_key_evp_cb() except that it takes a deprecated
  89. HMAC_CTX pointer instead of an EVP_MAC_CTX one.
  90. Before this callback function is started I<hctx> will have been
  91. initialised with L<EVP_MAC_CTX_new(3)> and the digest set with
  92. L<EVP_MAC_CTX_set_params(3)>.
  93. The I<hctx> key material can be set using L<HMAC_Init_ex(3)>.
  94. =head1 NOTES
  95. Session resumption shortcuts the TLS so that the client certificate
  96. negotiation don't occur. It makes up for this by storing client certificate
  97. an all other negotiated state information encrypted within the ticket. In a
  98. resumed session the applications will have all this state information available
  99. exactly as if a full negotiation had occurred.
  100. If an attacker can obtain the key used to encrypt a session ticket, they can
  101. obtain the master secret for any ticket using that key and decrypt any traffic
  102. using that session: even if the cipher suite supports forward secrecy. As
  103. a result applications may wish to use multiple keys and avoid using long term
  104. keys stored in files.
  105. Applications can use longer keys to maintain a consistent level of security.
  106. For example if a cipher suite uses 256 bit ciphers but only a 128 bit ticket key
  107. the overall security is only 128 bits because breaking the ticket key will
  108. enable an attacker to obtain the session keys.
  109. =head1 RETURN VALUES
  110. Returns 1 to indicate the callback function was set and 0 otherwise.
  111. =head1 EXAMPLES
  112. Reference Implementation:
  113. SSL_CTX_set_tlsext_ticket_key_evp_cb(SSL, ssl_tlsext_ticket_key_cb);
  114. ...
  115. static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16],
  116. unsigned char *iv, EVP_CIPHER_CTX *ctx,
  117. EVP_MAC_CTX *hctx, int enc)
  118. {
  119. OSSL_PARAM params[3];
  120. your_type_t *key; /* something that you need to implement */
  121. if (enc) { /* create new session */
  122. if (RAND_bytes(iv, EVP_MAX_IV_LENGTH) <= 0)
  123. return -1; /* insufficient random */
  124. key = currentkey(); /* something that you need to implement */
  125. if (key == NULL) {
  126. /* current key doesn't exist or isn't valid */
  127. key = createkey(); /*
  128. * Something that you need to implement.
  129. * createkey needs to initialise a name,
  130. * an aes_key, a hmac_key and optionally
  131. * an expire time.
  132. */
  133. if (key == NULL) /* key couldn't be created */
  134. return 0;
  135. }
  136. memcpy(key_name, key->name, 16);
  137. if (EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key->aes_key,
  138. iv) == 0)
  139. return -1; /* error in cipher initialisation */
  140. params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  141. key->hmac_key, 32);
  142. params[1] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  143. "sha256", 0);
  144. params[2] = OSSL_PARAM_construct_end();
  145. if (EVP_MAC_CTX_set_params(hctx, params) == 0)
  146. return -1; /* error in mac initialisation */
  147. return 1;
  148. } else { /* retrieve session */
  149. time_t t = time(NULL);
  150. key = findkey(key_name); /* something that you need to implement */
  151. if (key == NULL || key->expire < t)
  152. return 0;
  153. params[0] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
  154. key->hmac_key, 32);
  155. params[1] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  156. "sha256", 0);
  157. params[2] = OSSL_PARAM_construct_end();
  158. if (EVP_MAC_CTX_set_params(hctx, params) == 0)
  159. return -1; /* error in mac initialisation */
  160. if (EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key->aes_key,
  161. iv) == 0)
  162. return -1; /* error in cipher initialisation */
  163. if (key->expire < t - RENEW_TIME) { /* RENEW_TIME: implement */
  164. /*
  165. * return 2 - This session will get a new ticket even though the
  166. * current one is still valid.
  167. */
  168. return 2;
  169. }
  170. return 1;
  171. }
  172. }
  173. =head1 SEE ALSO
  174. L<ssl(7)>, L<SSL_set_session(3)>,
  175. L<SSL_session_reused(3)>,
  176. L<SSL_CTX_add_session(3)>,
  177. L<SSL_CTX_sess_number(3)>,
  178. L<SSL_CTX_sess_set_get_cb(3)>,
  179. L<SSL_CTX_set_session_id_context(3)>,
  180. =head1 HISTORY
  181. The SSL_CTX_set_tlsext_ticket_key_cb() function was deprecated in OpenSSL 3.0.
  182. The SSL_CTX_set_tlsext_ticket_key_evp_cb() function was introduced in
  183. OpenSSL 3.0.
  184. =head1 COPYRIGHT
  185. Copyright 2014-2021 The OpenSSL Project Authors. All Rights Reserved.
  186. Licensed under the Apache License 2.0 (the "License"). You may not use
  187. this file except in compliance with the License. You can obtain a copy
  188. in the file LICENSE in the source distribution or at
  189. L<https://www.openssl.org/source/license.html>.
  190. =cut