openssl-0.9.9-session-ticket.patch 12 KB

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