openssl-0.9.9-session-ticket.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. This patch adds support for TLS SessionTicket extension (RFC 5077) for
  2. the parts used by EAP-FAST (RFC 4851).
  3. This is based on the patch from Alexey Kobozev <akobozev@cisco.com>
  4. (sent to openssl-dev mailing list on Tue, 07 Jun 2005 15:40:58 +0300).
  5. Index: openssl-SNAP-20081111/ssl/s3_clnt.c
  6. ===================================================================
  7. --- openssl-SNAP-20081111.orig/ssl/s3_clnt.c
  8. +++ openssl-SNAP-20081111/ssl/s3_clnt.c
  9. @@ -788,6 +788,23 @@ int ssl3_get_server_hello(SSL *s)
  10. goto f_err;
  11. }
  12. +#ifndef OPENSSL_NO_TLSEXT
  13. + /* check if we want to resume the session based on external pre-shared secret */
  14. + if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  15. + {
  16. + SSL_CIPHER *pref_cipher=NULL;
  17. + s->session->master_key_length=sizeof(s->session->master_key);
  18. + if (s->tls_session_secret_cb(s, s->session->master_key,
  19. + &s->session->master_key_length,
  20. + NULL, &pref_cipher,
  21. + s->tls_session_secret_cb_arg))
  22. + {
  23. + s->session->cipher = pref_cipher ?
  24. + pref_cipher : ssl_get_cipher_by_char(s, p+j);
  25. + }
  26. + }
  27. +#endif /* OPENSSL_NO_TLSEXT */
  28. +
  29. if (j != 0 && j == s->session->session_id_length
  30. && memcmp(p,s->session->session_id,j) == 0)
  31. {
  32. @@ -2927,11 +2944,8 @@ static int ssl3_check_finished(SSL *s)
  33. {
  34. int ok;
  35. long n;
  36. - /* If we have no ticket or session ID is non-zero length (a match of
  37. - * a non-zero session length would never reach here) it cannot be a
  38. - * resumed session.
  39. - */
  40. - if (!s->session->tlsext_tick || s->session->session_id_length)
  41. + /* If we have no ticket it cannot be a resumed session. */
  42. + if (!s->session->tlsext_tick)
  43. return 1;
  44. /* this function is called when we really expect a Certificate
  45. * message, so permit appropriate message length */
  46. Index: openssl-SNAP-20081111/ssl/s3_srvr.c
  47. ===================================================================
  48. --- openssl-SNAP-20081111.orig/ssl/s3_srvr.c
  49. +++ openssl-SNAP-20081111/ssl/s3_srvr.c
  50. @@ -1010,6 +1010,59 @@ int ssl3_get_client_hello(SSL *s)
  51. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
  52. goto err;
  53. }
  54. +
  55. + /* Check if we want to use external pre-shared secret for this
  56. + * handshake for not reused session only. We need to generate
  57. + * server_random before calling tls_session_secret_cb in order to allow
  58. + * SessionTicket processing to use it in key derivation. */
  59. + {
  60. + unsigned long Time;
  61. + unsigned char *pos;
  62. + Time=(unsigned long)time(NULL); /* Time */
  63. + pos=s->s3->server_random;
  64. + l2n(Time,pos);
  65. + if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
  66. + {
  67. + al=SSL_AD_INTERNAL_ERROR;
  68. + goto f_err;
  69. + }
  70. + }
  71. +
  72. + if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  73. + {
  74. + SSL_CIPHER *pref_cipher=NULL;
  75. +
  76. + s->session->master_key_length=sizeof(s->session->master_key);
  77. + if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
  78. + ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
  79. + {
  80. + s->hit=1;
  81. + s->session->ciphers=ciphers;
  82. + s->session->verify_result=X509_V_OK;
  83. +
  84. + ciphers=NULL;
  85. +
  86. + /* check if some cipher was preferred by call back */
  87. + pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
  88. + if (pref_cipher == NULL)
  89. + {
  90. + al=SSL_AD_HANDSHAKE_FAILURE;
  91. + SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
  92. + goto f_err;
  93. + }
  94. +
  95. + s->session->cipher=pref_cipher;
  96. +
  97. + if (s->cipher_list)
  98. + sk_SSL_CIPHER_free(s->cipher_list);
  99. +
  100. + if (s->cipher_list_by_id)
  101. + sk_SSL_CIPHER_free(s->cipher_list_by_id);
  102. +
  103. + s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
  104. + s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
  105. + }
  106. + }
  107. #endif
  108. /* Worst case, we will use the NULL compression, but if we have other
  109. @@ -1134,16 +1187,22 @@ int ssl3_send_server_hello(SSL *s)
  110. unsigned char *buf;
  111. unsigned char *p,*d;
  112. int i,sl;
  113. - unsigned long l,Time;
  114. + unsigned long l;
  115. +#ifdef OPENSSL_NO_TLSEXT
  116. + unsigned long Time;
  117. +#endif
  118. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  119. {
  120. buf=(unsigned char *)s->init_buf->data;
  121. +#ifdef OPENSSL_NO_TLSEXT
  122. p=s->s3->server_random;
  123. + /* Generate server_random if it was not needed previously */
  124. Time=(unsigned long)time(NULL); /* Time */
  125. l2n(Time,p);
  126. if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
  127. return -1;
  128. +#endif
  129. /* Do the message type and length last */
  130. d=p= &(buf[4]);
  131. Index: openssl-SNAP-20081111/ssl/ssl_err.c
  132. ===================================================================
  133. --- openssl-SNAP-20081111.orig/ssl/ssl_err.c
  134. +++ openssl-SNAP-20081111/ssl/ssl_err.c
  135. @@ -263,6 +263,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
  136. {ERR_FUNC(SSL_F_TLS1_PRF), "tls1_prf"},
  137. {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
  138. {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
  139. +{ERR_FUNC(SSL_F_SSL_SET_SESSION_TICKET_EXT), "SSL_set_session_ticket_ext"},
  140. {0,NULL}
  141. };
  142. Index: openssl-SNAP-20081111/ssl/ssl.h
  143. ===================================================================
  144. --- openssl-SNAP-20081111.orig/ssl/ssl.h
  145. +++ openssl-SNAP-20081111/ssl/ssl.h
  146. @@ -355,6 +355,7 @@ extern "C" {
  147. * 'struct ssl_st *' function parameters used to prototype callbacks
  148. * in SSL_CTX. */
  149. typedef struct ssl_st *ssl_crock_st;
  150. +typedef struct tls_session_ticket_ext_st TLS_SESSION_TICKET_EXT;
  151. /* used to hold info on the particular ciphers used */
  152. typedef struct ssl_cipher_st
  153. @@ -378,6 +379,8 @@ typedef struct ssl_cipher_st
  154. DECLARE_STACK_OF(SSL_CIPHER)
  155. +typedef int (*tls_session_secret_cb_fn)(SSL *s, void *secret, int *secret_len, STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg);
  156. +
  157. /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
  158. typedef struct ssl_method_st
  159. {
  160. @@ -1145,6 +1148,13 @@ struct ssl_st
  161. void *tlsext_opaque_prf_input;
  162. size_t tlsext_opaque_prf_input_len;
  163. + /* TLS Session Ticket extension override */
  164. + TLS_SESSION_TICKET_EXT *tlsext_session_ticket;
  165. +
  166. + /* TLS pre-shared secret session resumption */
  167. + tls_session_secret_cb_fn tls_session_secret_cb;
  168. + void *tls_session_secret_cb_arg;
  169. +
  170. SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
  171. #define session_ctx initial_ctx
  172. #else
  173. @@ -1746,6 +1756,16 @@ void *SSL_COMP_get_compression_methods(v
  174. int SSL_COMP_add_compression_method(int id,void *cm);
  175. #endif
  176. +/* NOTE: This function will be removed; it is only here for backwards
  177. + * compatibility for the API during testing. */
  178. +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
  179. +
  180. +/* TLS extensions functions */
  181. +int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len);
  182. +
  183. +/* Pre-shared secret session resumption functions */
  184. +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
  185. +
  186. /* BEGIN ERROR CODES */
  187. /* The following lines are auto generated by the script mkerr.pl. Any changes
  188. * made after this point may be overwritten when the script is next run.
  189. @@ -1948,6 +1968,7 @@ void ERR_load_SSL_strings(void);
  190. #define SSL_F_TLS1_PRF 284
  191. #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
  192. #define SSL_F_WRITE_PENDING 212
  193. +#define SSL_F_SSL_SET_SESSION_TICKET_EXT 213
  194. /* Reason codes. */
  195. #define SSL_R_APP_DATA_IN_HANDSHAKE 100
  196. Index: openssl-SNAP-20081111/ssl/ssl_sess.c
  197. ===================================================================
  198. --- openssl-SNAP-20081111.orig/ssl/ssl_sess.c
  199. +++ openssl-SNAP-20081111/ssl/ssl_sess.c
  200. @@ -834,6 +834,62 @@ long SSL_CTX_get_timeout(const SSL_CTX *
  201. return(s->session_timeout);
  202. }
  203. +#ifndef OPENSSL_NO_TLSEXT
  204. +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
  205. + STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
  206. + {
  207. + if (s == NULL) return(0);
  208. + s->tls_session_secret_cb = tls_session_secret_cb;
  209. + s->tls_session_secret_cb_arg = arg;
  210. + return(1);
  211. + }
  212. +
  213. +int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
  214. + {
  215. + if (s->version >= TLS1_VERSION)
  216. + {
  217. + if (s->tlsext_session_ticket)
  218. + {
  219. + OPENSSL_free(s->tlsext_session_ticket);
  220. + s->tlsext_session_ticket = NULL;
  221. + }
  222. +
  223. + s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
  224. + if (!s->tlsext_session_ticket)
  225. + {
  226. + SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
  227. + return 0;
  228. + }
  229. +
  230. + if (ext_data)
  231. + {
  232. + s->tlsext_session_ticket->length = ext_len;
  233. + s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
  234. + memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
  235. + }
  236. + else
  237. + {
  238. + s->tlsext_session_ticket->length = 0;
  239. + s->tlsext_session_ticket->data = NULL;
  240. + }
  241. +
  242. + return 1;
  243. + }
  244. +
  245. + return 0;
  246. + }
  247. +
  248. +/* NOTE: This function will be removed; it is only here for backwards
  249. + * compatibility for the API during testing. */
  250. +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
  251. + {
  252. + if (ext_type != TLSEXT_TYPE_session_ticket)
  253. + return 0;
  254. +
  255. + return SSL_set_session_ticket_ext(s, ext_data, ext_len);
  256. + }
  257. +#endif /* OPENSSL_NO_TLSEXT */
  258. +
  259. typedef struct timeout_param_st
  260. {
  261. SSL_CTX *ctx;
  262. Index: openssl-SNAP-20081111/ssl/t1_lib.c
  263. ===================================================================
  264. --- openssl-SNAP-20081111.orig/ssl/t1_lib.c
  265. +++ openssl-SNAP-20081111/ssl/t1_lib.c
  266. @@ -154,6 +154,12 @@ int tls1_new(SSL *s)
  267. void tls1_free(SSL *s)
  268. {
  269. +#ifndef OPENSSL_NO_TLSEXT
  270. + if (s->tlsext_session_ticket)
  271. + {
  272. + OPENSSL_free(s->tlsext_session_ticket);
  273. + }
  274. +#endif /* OPENSSL_NO_TLSEXT */
  275. ssl3_free(s);
  276. }
  277. @@ -357,8 +363,23 @@ unsigned char *ssl_add_clienthello_tlsex
  278. int ticklen;
  279. if (s->session && s->session->tlsext_tick)
  280. ticklen = s->session->tlsext_ticklen;
  281. + else if (s->session && s->tlsext_session_ticket &&
  282. + s->tlsext_session_ticket->data)
  283. + {
  284. + ticklen = s->tlsext_session_ticket->length;
  285. + s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  286. + if (!s->session->tlsext_tick)
  287. + return NULL;
  288. + memcpy(s->session->tlsext_tick,
  289. + s->tlsext_session_ticket->data,
  290. + ticklen);
  291. + s->session->tlsext_ticklen = ticklen;
  292. + }
  293. else
  294. ticklen = 0;
  295. + if (ticklen == 0 && s->tlsext_session_ticket &&
  296. + s->tlsext_session_ticket->data == NULL)
  297. + goto skip_ext;
  298. /* Check for enough room 2 for extension type, 2 for len
  299. * rest for ticket
  300. */
  301. @@ -371,6 +392,7 @@ unsigned char *ssl_add_clienthello_tlsex
  302. ret += ticklen;
  303. }
  304. }
  305. + skip_ext:
  306. #ifdef TLSEXT_TYPE_opaque_prf_input
  307. if (s->s3->client_opaque_prf_input != NULL)
  308. @@ -1435,6 +1457,15 @@ int tls1_process_ticket(SSL *s, unsigned
  309. s->tlsext_ticket_expected = 1;
  310. return 0; /* Cache miss */
  311. }
  312. + if (s->tls_session_secret_cb)
  313. + {
  314. + /* Indicate cache miss here and instead of
  315. + * generating the session from ticket now,
  316. + * trigger abbreviated handshake based on
  317. + * external mechanism to calculate the master
  318. + * secret later. */
  319. + return 0;
  320. + }
  321. return tls_decrypt_ticket(s, p, size, session_id, len,
  322. ret);
  323. }
  324. Index: openssl-SNAP-20081111/ssl/tls1.h
  325. ===================================================================
  326. --- openssl-SNAP-20081111.orig/ssl/tls1.h
  327. +++ openssl-SNAP-20081111/ssl/tls1.h
  328. @@ -512,6 +512,13 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_T
  329. #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
  330. #endif
  331. +/* TLS Session Ticket extension struct */
  332. +struct tls_session_ticket_ext_st
  333. + {
  334. + unsigned short length;
  335. + void *data;
  336. + };
  337. +
  338. #ifdef __cplusplus
  339. }
  340. #endif
  341. Index: openssl-SNAP-20081111/util/ssleay.num
  342. ===================================================================
  343. --- openssl-SNAP-20081111.orig/util/ssleay.num
  344. +++ openssl-SNAP-20081111/util/ssleay.num
  345. @@ -254,3 +254,5 @@ PEM_read_bio_SSL_SESSION
  346. SSL_CTX_set_psk_server_callback 303 EXIST::FUNCTION:PSK
  347. SSL_get_psk_identity 304 EXIST::FUNCTION:PSK
  348. PEM_write_SSL_SESSION 305 EXIST:!WIN16:FUNCTION:
  349. +SSL_set_session_ticket_ext 306 EXIST::FUNCTION:TLSEXT
  350. +SSL_set_session_secret_cb 307 EXIST::FUNCTION:TLSEXT