SSL_CTX_set_tlsext_ticket_key_cb.pod 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. =pod
  2. =head1 NAME
  3. SSL_CTX_set_tlsext_ticket_key_cb - set a callback for session ticket processing
  4. =head1 SYNOPSIS
  5. #include <openssl/tls1.h>
  6. long SSL_CTX_set_tlsext_ticket_key_cb(SSL_CTX sslctx,
  7. int (*cb)(SSL *s, unsigned char key_name[16],
  8. unsigned char iv[EVP_MAX_IV_LENGTH],
  9. EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc));
  10. =head1 DESCRIPTION
  11. SSL_CTX_set_tlsext_ticket_key_cb() sets a callback fuction I<cb> for handling
  12. session tickets for the ssl context I<sslctx>. Session tickets, defined in
  13. RFC5077 provide an enhanced session resumption capability where the server
  14. implementation is not required to maintain per session state. It only applies
  15. to TLS and there is no SSLv3 implementation.
  16. The callback is available when the OpenSSL library was built without
  17. I<OPENSSL_NO_TLSEXT> being defined.
  18. The callback function I<cb> will be called for every client instigated TLS
  19. session when session ticket extension is presented in the TLS hello
  20. message. It is the responsibility of this function to create or retrieve the
  21. cryptographic parameters and to maintain their state.
  22. The OpenSSL library uses your callback function to help implement a common TLS
  23. ticket construction state according to RFC5077 Section 4 such that per session
  24. state is unnecessary and a small set of cryptographic variables needs to be
  25. maintained by the callback function implementation.
  26. In order to reuse a session, a TLS client must send the a session ticket
  27. extension to the server. The client can only send exactly one session ticket.
  28. The server, through the callback function, either agrees to reuse the session
  29. ticket information or it starts a full TLS handshake to create a new session
  30. ticket.
  31. Before the callback function is started I<ctx> and I<hctx> have been
  32. initialised with EVP_CIPHER_CTX_init and HMAC_CTX_init respectively.
  33. For new sessions tickets, when the client doesn't present a session ticket, or
  34. an attempted retreival of the ticket failed, or a renew option was indicated,
  35. the callback function will be called with I<enc> equal to 1. The OpenSSL
  36. library expects that the function will set an arbitary I<name>, initialize
  37. I<iv>, and set the cipher context I<ctx> and the hash context I<hctx>.
  38. The I<name> is 16 characters long and is used as a key identifier.
  39. The I<iv> length is the length of the IV of the corresponding cipher. The
  40. maximum IV length is L<EVP_MAX_IV_LENGTH> bytes defined in B<evp.h>.
  41. The initialization vector I<iv> should be a random value. The cipher context
  42. I<ctx> should use the initialisation vector I<iv>. The cipher context can be
  43. set using L<EVP_EncryptInit_ex>. The hmac context can be set using L<HMAC_Init_ex>.
  44. When the client presents a session ticket, the callback function with be called
  45. with I<enc> set to 0 indicating that the I<cb> function should retreive a set
  46. of parameters. In this case I<name> and I<iv> have already been parsed out of
  47. the session ticket. The OpenSSL library expects that the I<name> will be used
  48. to retrieve a cryptographic parameters and that the cryptographic context
  49. I<ctx> will be set with the retreived parameters and the initialization vector
  50. I<iv>. using a function like L<EVP_DecryptInit_ex>. The I<hctx> needs to be set
  51. using L<HMAC_Init_ex>.
  52. If the I<name> is still valid but a renewal of the ticket is required the
  53. callback function should return 2. The library will call the callback again
  54. with an arguement of enc equal to 1 to set the new ticket.
  55. The return value of the I<cb> function is used by OpenSSL to determine what
  56. further processing will occur. The following return values have meaning:
  57. =over 4
  58. =item Z<>2
  59. This indicates that the I<ctx> and I<hctx> have been set and the session can
  60. continue on those parameters. Additionally it indicates that the session
  61. ticket is in a renewal period and should be replaced. The OpenSSL library will
  62. call I<cb> again with an enc argument of 1 to set the new ticket (see RFC5077
  63. 3.3 paragraph 2).
  64. =item Z<>1
  65. This indicates that the I<ctx> and I<hctx> have been set and the session can
  66. continue on those parameters.
  67. =item Z<>0
  68. This indicates that it was not possible to set/retrieve a session ticket and
  69. the SSL/TLS session will continue by by negiotationing a set of cryptographic
  70. parameters or using the alternate SSL/TLS resumption mechanism, session ids.
  71. If called with enc equal to 0 the library will call the I<cb> again to get
  72. a new set of parameters.
  73. =item less than 0
  74. This indicates an error.
  75. =back
  76. =head1 NOTES
  77. Session resumption shortcuts the TLS so that the client certificate
  78. negiotation don't occur. It makes up for this by storing client certificate
  79. an all other negotiated state information encrypted within the ticket. In a
  80. resumed session the applications will have all this state information available
  81. exactly as if a full negiotation had occured.
  82. If an attacker can obtain the key used to encrypt a session ticket, they can
  83. obtain the master secret for any ticket using that key and decrypt any traffic
  84. using that session: even if the ciphersuite supports forward secrecy. As
  85. a result applications may wish to use multiple keys and avoid using long term
  86. keys stored in files.
  87. Applications can use longer keys to maintain a consistent level of security.
  88. For example if a ciphersuite uses 256 bit ciphers but only a 128 bit ticket key
  89. the overall security is only 128 bits because breaking the ticket key will
  90. enable an attacker to obtain the session keys.
  91. =head1 EXAMPLES
  92. Reference Implemention:
  93. SSL_CTX_set_tlsext_ticket_key_cb(SSL,ssl_tlsext_ticket_key_cb);
  94. ....
  95. static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ctx, HMAC_CTX *hctx, int enc)
  96. {
  97. if (enc) { /* create new session */
  98. if (RAND_bytes(iv, EVP_MAX_IV_LENGTH) ) {
  99. return -1; /* insufficient random */
  100. }
  101. key = currentkey(); /* something that you need to implement */
  102. if ( !key ) {
  103. /* current key doesn't exist or isn't valid */
  104. key = createkey(); /* something that you need to implement.
  105. * createkey needs to initialise, a name,
  106. * an aes_key, a hmac_key and optionally
  107. * an expire time. */
  108. if ( !key ) { /* key couldn't be created */
  109. return 0;
  110. }
  111. }
  112. memcpy(key_name, key->name, 16);
  113. EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key->aes_key, iv);
  114. HMAC_Init_ex(&hctx, key->hmac_key, 16, EVP_sha256(), NULL);
  115. return 1;
  116. } else { /* retrieve session */
  117. key = findkey(name);
  118. if (!key || key->expire < now() ) {
  119. return 0;
  120. }
  121. HMAC_Init_ex(&hctx, key->hmac_key, 16, EVP_sha256(), NULL);
  122. EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key->aes_key, iv );
  123. if (key->expire < ( now() - RENEW_TIME ) ) {
  124. /* return 2 - this session will get a new ticket even though the current is still valid */
  125. return 2;
  126. }
  127. return 1;
  128. }
  129. }
  130. =head1 RETURN VALUES
  131. returns 0 to indicate the callback function was set.
  132. =head1 SEE ALSO
  133. L<ssl(3)|ssl(3)>, L<SSL_set_session(3)|SSL_set_session(3)>,
  134. L<SSL_session_reused(3)|SSL_session_reused(3)>,
  135. L<SSL_CTX_add_session(3)|SSL_CTX_add_session(3)>,
  136. L<SSL_CTX_sess_number(3)|SSL_CTX_sess_number(3)>,
  137. L<SSL_CTX_sess_set_get_cb(3)|SSL_CTX_sess_set_get_cb(3)>,
  138. L<SSL_CTX_set_session_id_context(3)|SSL_CTX_set_session_id_context(3)>,
  139. =head1 HISTORY
  140. This function was introduced in OpenSSL 0.9.8h
  141. =cut