openssl-0.9.9-session-ticket.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. This patch adds support for TLS SessionTicket extension (RFC 4507) 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-20071101.orig/ssl/s3_clnt.c openssl-SNAP-20071101/ssl/s3_clnt.c
  6. --- openssl-SNAP-20071101.orig/ssl/s3_clnt.c 2007-10-26 06:00:28.000000000 -0700
  7. +++ openssl-SNAP-20071101/ssl/s3_clnt.c 2007-11-01 19:19:43.000000000 -0700
  8. @@ -715,7 +715,7 @@ int ssl3_get_server_hello(SSL *s)
  9. STACK_OF(SSL_CIPHER) *sk;
  10. SSL_CIPHER *c;
  11. unsigned char *p,*d;
  12. - int i,al,ok;
  13. + int i,al,ok,pre_shared;
  14. unsigned int j;
  15. long n;
  16. #ifndef OPENSSL_NO_COMP
  17. @@ -782,7 +782,26 @@ int ssl3_get_server_hello(SSL *s)
  18. goto f_err;
  19. }
  20. - if (j != 0 && j == s->session->session_id_length
  21. + /* check if we want to resume the session based on external pre-shared secret */
  22. + pre_shared = 0;
  23. +#ifndef OPENSSL_NO_TLSEXT
  24. + if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  25. + {
  26. + SSL_CIPHER *pref_cipher=NULL;
  27. + s->session->master_key_length=sizeof(s->session->master_key);
  28. + if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
  29. + NULL, &pref_cipher, s->tls_session_secret_cb_arg))
  30. + {
  31. + s->hit=1;
  32. + s->session->cipher=pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s,p+j);
  33. + s->session->session_id_length = j;
  34. + memcpy(s->session->session_id, p, j);
  35. + pre_shared = 1;
  36. + }
  37. + }
  38. +#endif /* OPENSSL_NO_TLSEXT */
  39. +
  40. + if ((pre_shared || j != 0) && j == s->session->session_id_length
  41. && memcmp(p,s->session->session_id,j) == 0)
  42. {
  43. if(s->sid_ctx_length != s->session->sid_ctx_length
  44. diff -upr openssl-SNAP-20071101.orig/ssl/s3_srvr.c openssl-SNAP-20071101/ssl/s3_srvr.c
  45. --- openssl-SNAP-20071101.orig/ssl/s3_srvr.c 2007-10-26 06:00:29.000000000 -0700
  46. +++ openssl-SNAP-20071101/ssl/s3_srvr.c 2007-11-01 19:19:43.000000000 -0700
  47. @@ -992,6 +992,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. @@ -1118,16 +1171,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. diff -upr openssl-SNAP-20071101.orig/ssl/ssl.h openssl-SNAP-20071101/ssl/ssl.h
  129. --- openssl-SNAP-20071101.orig/ssl/ssl.h 2007-10-26 17:01:28.000000000 -0700
  130. +++ openssl-SNAP-20071101/ssl/ssl.h 2007-11-01 19:20:47.000000000 -0700
  131. @@ -353,6 +353,7 @@ extern "C" {
  132. * 'struct ssl_st *' function parameters used to prototype callbacks
  133. * in SSL_CTX. */
  134. typedef struct ssl_st *ssl_crock_st;
  135. +typedef struct tls_extension_st TLS_EXTENSION;
  136. /* used to hold info on the particular ciphers used */
  137. typedef struct ssl_cipher_st
  138. @@ -379,6 +380,8 @@ DECLARE_STACK_OF(SSL_CIPHER)
  139. typedef struct ssl_st SSL;
  140. typedef struct ssl_ctx_st SSL_CTX;
  141. +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);
  142. +
  143. /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
  144. typedef struct ssl_method_st
  145. {
  146. @@ -1121,6 +1124,13 @@ struct ssl_st
  147. void *tlsext_opaque_prf_input;
  148. size_t tlsext_opaque_prf_input_len;
  149. + /* TLS extensions */
  150. + TLS_EXTENSION *tls_extension;
  151. +
  152. + /* TLS pre-shared secret session resumption */
  153. + tls_session_secret_cb_fn tls_session_secret_cb;
  154. + void *tls_session_secret_cb_arg;
  155. +
  156. SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
  157. #define session_ctx initial_ctx
  158. #else
  159. @@ -1721,6 +1731,12 @@ void *SSL_COMP_get_compression_methods(v
  160. int SSL_COMP_add_compression_method(int id,void *cm);
  161. #endif
  162. +/* TLS extensions functions */
  163. +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
  164. +
  165. +/* Pre-shared secret session resumption functions */
  166. +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
  167. +
  168. /* BEGIN ERROR CODES */
  169. /* The following lines are auto generated by the script mkerr.pl. Any changes
  170. * made after this point may be overwritten when the script is next run.
  171. @@ -1920,6 +1936,7 @@ void ERR_load_SSL_strings(void);
  172. #define SSL_F_TLS1_PRF 284
  173. #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
  174. #define SSL_F_WRITE_PENDING 212
  175. +#define SSL_F_SSL_SET_HELLO_EXTENSION 213
  176. /* Reason codes. */
  177. #define SSL_R_APP_DATA_IN_HANDSHAKE 100
  178. diff -upr openssl-SNAP-20071101.orig/ssl/ssl_err.c openssl-SNAP-20071101/ssl/ssl_err.c
  179. --- openssl-SNAP-20071101.orig/ssl/ssl_err.c 2007-10-26 17:01:29.000000000 -0700
  180. +++ openssl-SNAP-20071101/ssl/ssl_err.c 2007-11-01 19:19:43.000000000 -0700
  181. @@ -260,6 +260,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
  182. {ERR_FUNC(SSL_F_TLS1_PRF), "tls1_prf"},
  183. {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
  184. {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
  185. +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
  186. {0,NULL}
  187. };
  188. diff -upr openssl-SNAP-20071101.orig/ssl/ssl_sess.c openssl-SNAP-20071101/ssl/ssl_sess.c
  189. --- openssl-SNAP-20071101.orig/ssl/ssl_sess.c 2007-10-17 11:00:45.000000000 -0700
  190. +++ openssl-SNAP-20071101/ssl/ssl_sess.c 2007-11-01 19:19:43.000000000 -0700
  191. @@ -831,6 +831,52 @@ long SSL_CTX_get_timeout(const SSL_CTX *
  192. return(s->session_timeout);
  193. }
  194. +#ifndef OPENSSL_NO_TLSEXT
  195. +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
  196. + STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
  197. +{
  198. + if (s == NULL) return(0);
  199. + s->tls_session_secret_cb = tls_session_secret_cb;
  200. + s->tls_session_secret_cb_arg = arg;
  201. + return(1);
  202. +}
  203. +
  204. +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
  205. +{
  206. + if(s->version >= TLS1_VERSION)
  207. + {
  208. + if(s->tls_extension)
  209. + {
  210. + OPENSSL_free(s->tls_extension);
  211. + s->tls_extension = NULL;
  212. + }
  213. +
  214. + s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
  215. + if(!s->tls_extension)
  216. + {
  217. + SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
  218. + return 0;
  219. + }
  220. +
  221. + s->tls_extension->type = ext_type;
  222. +
  223. + if(ext_data)
  224. + {
  225. + s->tls_extension->length = ext_len;
  226. + s->tls_extension->data = s->tls_extension + 1;
  227. + memcpy(s->tls_extension->data, ext_data, ext_len);
  228. + } else {
  229. + s->tls_extension->length = 0;
  230. + s->tls_extension->data = NULL;
  231. + }
  232. +
  233. + return 1;
  234. + }
  235. +
  236. + return 0;
  237. +}
  238. +#endif /* OPENSSL_NO_TLSEXT */
  239. +
  240. typedef struct timeout_param_st
  241. {
  242. SSL_CTX *ctx;
  243. diff -upr openssl-SNAP-20071101.orig/ssl/t1_lib.c openssl-SNAP-20071101/ssl/t1_lib.c
  244. --- openssl-SNAP-20071101.orig/ssl/t1_lib.c 2007-10-26 06:00:29.000000000 -0700
  245. +++ openssl-SNAP-20071101/ssl/t1_lib.c 2007-11-01 19:19:43.000000000 -0700
  246. @@ -154,6 +154,12 @@ int tls1_new(SSL *s)
  247. void tls1_free(SSL *s)
  248. {
  249. +#ifndef OPENSSL_NO_TLSEXT
  250. + if(s->tls_extension)
  251. + {
  252. + OPENSSL_free(s->tls_extension);
  253. + }
  254. +#endif
  255. ssl3_free(s);
  256. }
  257. @@ -355,8 +361,24 @@ unsigned char *ssl_add_clienthello_tlsex
  258. int ticklen;
  259. if (s->session && s->session->tlsext_tick)
  260. ticklen = s->session->tlsext_ticklen;
  261. + else if (s->session && s->tls_extension &&
  262. + s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
  263. + s->tls_extension->data)
  264. + {
  265. + ticklen = s->tls_extension->length;
  266. + s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  267. + if (!s->session->tlsext_tick)
  268. + return NULL;
  269. + memcpy(s->session->tlsext_tick, s->tls_extension->data,
  270. + ticklen);
  271. + s->session->tlsext_ticklen = ticklen;
  272. + }
  273. else
  274. ticklen = 0;
  275. + if (ticklen == 0 && s->tls_extension &&
  276. + s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
  277. + s->tls_extension->data == NULL)
  278. + goto skip_ext;
  279. /* Check for enough room 2 for extension type, 2 for len
  280. * rest for ticket
  281. */
  282. @@ -369,6 +391,7 @@ unsigned char *ssl_add_clienthello_tlsex
  283. ret += ticklen;
  284. }
  285. }
  286. + skip_ext:
  287. #ifdef TLSEXT_TYPE_opaque_prf_input
  288. if (s->s3->client_opaque_prf_input != NULL)
  289. @@ -1422,6 +1445,8 @@ int tls1_process_ticket(SSL *s, unsigned
  290. s->tlsext_ticket_expected = 1;
  291. return 0; /* Cache miss */
  292. }
  293. + if (s->tls_session_secret_cb)
  294. + return 0;
  295. return tls_decrypt_ticket(s, p, size, session_id, len,
  296. ret);
  297. }
  298. diff -upr openssl-SNAP-20071101.orig/ssl/tls1.h openssl-SNAP-20071101/ssl/tls1.h
  299. --- openssl-SNAP-20071101.orig/ssl/tls1.h 2007-09-26 15:01:39.000000000 -0700
  300. +++ openssl-SNAP-20071101/ssl/tls1.h 2007-11-01 19:19:43.000000000 -0700
  301. @@ -509,6 +509,14 @@ SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TLSEXT_OPA
  302. #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
  303. #endif
  304. +/* TLS extension struct */
  305. +struct tls_extension_st
  306. +{
  307. + unsigned short type;
  308. + unsigned short length;
  309. + void *data;
  310. +};
  311. +
  312. #ifdef __cplusplus
  313. }
  314. #endif
  315. diff -upr openssl-SNAP-20071101.orig/util/ssleay.num openssl-SNAP-20071101/util/ssleay.num
  316. --- openssl-SNAP-20071101.orig/util/ssleay.num 2007-08-31 06:03:14.000000000 -0700
  317. +++ openssl-SNAP-20071101/util/ssleay.num 2007-11-01 19:19:43.000000000 -0700
  318. @@ -253,3 +253,5 @@ PEM_write_bio_SSL_SESSION
  319. PEM_read_SSL_SESSION 302 EXIST:!WIN16:FUNCTION:
  320. PEM_read_bio_SSL_SESSION 303 EXIST::FUNCTION:
  321. PEM_write_SSL_SESSION 304 EXIST:!WIN16:FUNCTION:
  322. +SSL_set_hello_extension 305 EXIST::FUNCTION:TLSEXT
  323. +SSL_set_session_secret_cb 306 EXIST::FUNCTION:TLSEXT