openssl-0.9.9-session-ticket.patch 11 KB

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