SSL_CTX_set_generate_session_id.pod 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. =pod
  2. =head1 NAME
  3. SSL_CTX_set_generate_session_id, SSL_set_generate_session_id, SSL_has_matching_session_id - manipulate generation of SSL session IDs (server only)
  4. =head1 SYNOPSIS
  5. #include <openssl/ssl.h>
  6. typedef int (*GEN_SESSION_CB)(const SSL *ssl, unsigned char *id,
  7. unsigned int *id_len);
  8. int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb);
  9. int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB, cb);
  10. int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
  11. unsigned int id_len);
  12. =head1 DESCRIPTION
  13. SSL_CTX_set_generate_session_id() sets the callback function for generating
  14. new session ids for SSL/TLS sessions for B<ctx> to be B<cb>.
  15. SSL_set_generate_session_id() sets the callback function for generating
  16. new session ids for SSL/TLS sessions for B<ssl> to be B<cb>.
  17. SSL_has_matching_session_id() checks, whether a session with id B<id>
  18. (of length B<id_len>) is already contained in the internal session cache
  19. of the parent context of B<ssl>.
  20. =head1 NOTES
  21. When a new session is established between client and server, the server
  22. generates a session id. The session id is an arbitrary sequence of bytes.
  23. The length of the session id is 16 bytes for SSLv2 sessions and between
  24. 1 and 32 bytes for SSLv3/TLSv1. The session id is not security critical
  25. but must be unique for the server. Additionally, the session id is
  26. transmitted in the clear when reusing the session so it must not contain
  27. sensitive information.
  28. Without a callback being set, an OpenSSL server will generate a unique
  29. session id from pseudo random numbers of the maximum possible length.
  30. Using the callback function, the session id can be changed to contain
  31. additional information like e.g. a host id in order to improve load balancing
  32. or external caching techniques.
  33. The callback function receives a pointer to the memory location to put
  34. B<id> into and a pointer to the maximum allowed length B<id_len>. The
  35. buffer at location B<id> is only guaranteed to have the size B<id_len>.
  36. The callback is only allowed to generate a shorter id and reduce B<id_len>;
  37. the callback B<must never> increase B<id_len> or write to the location
  38. B<id> exceeding the given limit.
  39. If a SSLv2 session id is generated and B<id_len> is reduced, it will be
  40. restored after the callback has finished and the session id will be padded
  41. with 0x00. It is not recommended to change the B<id_len> for SSLv2 sessions.
  42. The callback can use the L<SSL_get_version(3)|SSL_get_version(3)> function
  43. to check, whether the session is of type SSLv2.
  44. The location B<id> is filled with 0x00 before the callback is called, so the
  45. callback may only fill part of the possible length and leave B<id_len>
  46. untouched while maintaining reproducibility.
  47. Since the sessions must be distinguished, session ids must be unique.
  48. Without the callback a random number is used, so that the probability
  49. of generating the same session id is extremely small (2^128 possible ids
  50. for an SSLv2 session, 2^256 for SSLv3/TLSv1). In order to assure the
  51. uniqueness of the generated session id, the callback must call
  52. SSL_has_matching_session_id() and generate another id if a conflict occurs.
  53. If an id conflict is not resolved, the handshake will fail.
  54. If the application codes e.g. a unique host id, a unique process number, and
  55. a unique sequence number into the session id, uniqueness could easily be
  56. achieved without randomness added (it should however be taken care that
  57. no confidential information is leaked this way). If the application can not
  58. guarantee uniqueness, it is recommended to use the maximum B<id_len> and
  59. fill in the bytes not used to code special information with random data
  60. to avoid collisions.
  61. SSL_has_matching_session_id() will only query the internal session cache,
  62. not the external one. Since the session id is generated before the
  63. handshake is completed, it is not immediately added to the cache. If
  64. another thread is using the same internal session cache, a race condition
  65. can occur in that another thread generates the same session id.
  66. Collisions can also occur when using an external session cache, since
  67. the external cache is not tested with SSL_has_matching_session_id()
  68. and the same race condition applies.
  69. When calling SSL_has_matching_session_id() for an SSLv2 session with
  70. reduced B<id_len>, the match operation will be performed using the
  71. fixed length required and with a 0x00 padded id.
  72. The callback must return 0 if it cannot generate a session id for whatever
  73. reason and return 1 on success.
  74. =head1 EXAMPLES
  75. The callback function listed will generate a session id with the
  76. server id given, and will fill the rest with pseudo random bytes:
  77. const char session_id_prefix = "www-18";
  78. #define MAX_SESSION_ID_ATTEMPTS 10
  79. static int generate_session_id(const SSL *ssl, unsigned char *id,
  80. unsigned int *id_len)
  81. {
  82. unsigned int count = 0;
  83. const char *version;
  84. version = SSL_get_version(ssl);
  85. if (!strcmp(version, "SSLv2"))
  86. /* we must not change id_len */;
  87. do {
  88. RAND_pseudo_bytes(id, *id_len);
  89. /* Prefix the session_id with the required prefix. NB: If our
  90. * prefix is too long, clip it - but there will be worse effects
  91. * anyway, eg. the server could only possibly create 1 session
  92. * ID (ie. the prefix!) so all future session negotiations will
  93. * fail due to conflicts. */
  94. memcpy(id, session_id_prefix,
  95. (strlen(session_id_prefix) < *id_len) ?
  96. strlen(session_id_prefix) : *id_len);
  97. }
  98. while(SSL_has_matching_session_id(ssl, id, *id_len) &&
  99. (++count < MAX_SESSION_ID_ATTEMPTS));
  100. if(count >= MAX_SESSION_ID_ATTEMPTS)
  101. return 0;
  102. return 1;
  103. }
  104. =head1 RETURN VALUES
  105. SSL_CTX_set_generate_session_id() and SSL_set_generate_session_id()
  106. always return 1.
  107. SSL_has_matching_session_id() returns 1 if another session with the
  108. same id is already in the cache.
  109. =head1 SEE ALSO
  110. L<ssl(3)|ssl(3)>, L<SSL_get_version(3)|SSL_get_version(3)>
  111. =head1 HISTORY
  112. SSL_CTX_set_generate_session_id(), SSL_set_generate_session_id()
  113. and SSL_has_matching_session_id() have been introduced in
  114. OpenSSL 0.9.7.
  115. =cut