openssl-0.9.9-session-ticket.patch 11 KB

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