openssl-0.9.8h-tls-extensions.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. OpenSSL 0.9.8h does not enable TLS extension support by default, so it
  6. will need to be enabled by adding enable-tlsext to config script
  7. command line.
  8. diff -upr openssl-0.9.8h.orig/ssl/s3_clnt.c openssl-0.9.8h/ssl/s3_clnt.c
  9. --- openssl-0.9.8h.orig/ssl/s3_clnt.c 2008-05-28 10:29:27.000000000 +0300
  10. +++ openssl-0.9.8h/ssl/s3_clnt.c 2008-05-29 10:44:25.000000000 +0300
  11. @@ -752,6 +752,20 @@ int ssl3_get_server_hello(SSL *s)
  12. goto f_err;
  13. }
  14. +#ifndef OPENSSL_NO_TLSEXT
  15. + /* check if we want to resume the session based on external pre-shared secret */
  16. + if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  17. + {
  18. + SSL_CIPHER *pref_cipher=NULL;
  19. + s->session->master_key_length=sizeof(s->session->master_key);
  20. + if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
  21. + NULL, &pref_cipher, s->tls_session_secret_cb_arg))
  22. + {
  23. + s->session->cipher=pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s,p+j);
  24. + }
  25. + }
  26. +#endif /* OPENSSL_NO_TLSEXT */
  27. +
  28. if (j != 0 && j == s->session->session_id_length
  29. && memcmp(p,s->session->session_id,j) == 0)
  30. {
  31. @@ -2693,11 +2707,8 @@ static int ssl3_check_finished(SSL *s)
  32. {
  33. int ok;
  34. long n;
  35. - /* If we have no ticket or session ID is non-zero length (a match of
  36. - * a non-zero session length would never reach here) it cannot be a
  37. - * resumed session.
  38. - */
  39. - if (!s->session->tlsext_tick || s->session->session_id_length)
  40. + /* If we have no ticket it cannot be a resumed session. */
  41. + if (!s->session->tlsext_tick)
  42. return 1;
  43. /* this function is called when we really expect a Certificate
  44. * message, so permit appropriate message length */
  45. diff -upr openssl-0.9.8h.orig/ssl/s3_srvr.c openssl-0.9.8h/ssl/s3_srvr.c
  46. --- openssl-0.9.8h.orig/ssl/s3_srvr.c 2008-04-30 19:11:32.000000000 +0300
  47. +++ openssl-0.9.8h/ssl/s3_srvr.c 2008-05-28 18:49:34.000000000 +0300
  48. @@ -959,6 +959,59 @@ int ssl3_get_client_hello(SSL *s)
  49. SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
  50. goto err;
  51. }
  52. +
  53. + /* Check if we want to use external pre-shared secret for this
  54. + * handshake for not reused session only. We need to generate
  55. + * server_random before calling tls_session_secret_cb in order to allow
  56. + * SessionTicket processing to use it in key derivation. */
  57. + {
  58. + unsigned long Time;
  59. + unsigned char *pos;
  60. + Time=(unsigned long)time(NULL); /* Time */
  61. + pos=s->s3->server_random;
  62. + l2n(Time,pos);
  63. + if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
  64. + {
  65. + al=SSL_AD_INTERNAL_ERROR;
  66. + goto f_err;
  67. + }
  68. + }
  69. +
  70. + if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  71. + {
  72. + SSL_CIPHER *pref_cipher=NULL;
  73. +
  74. + s->session->master_key_length=sizeof(s->session->master_key);
  75. + if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
  76. + ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
  77. + {
  78. + s->hit=1;
  79. + s->session->ciphers=ciphers;
  80. + s->session->verify_result=X509_V_OK;
  81. +
  82. + ciphers=NULL;
  83. +
  84. + /* check if some cipher was preferred by call back */
  85. + pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
  86. + if (pref_cipher == NULL)
  87. + {
  88. + al=SSL_AD_HANDSHAKE_FAILURE;
  89. + SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
  90. + goto f_err;
  91. + }
  92. +
  93. + s->session->cipher=pref_cipher;
  94. +
  95. + if (s->cipher_list)
  96. + sk_SSL_CIPHER_free(s->cipher_list);
  97. +
  98. + if (s->cipher_list_by_id)
  99. + sk_SSL_CIPHER_free(s->cipher_list_by_id);
  100. +
  101. + s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
  102. + s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
  103. + }
  104. + }
  105. #endif
  106. /* Worst case, we will use the NULL compression, but if we have other
  107. * options, we will now look for them. We have i-1 compression
  108. @@ -1097,16 +1150,22 @@ int ssl3_send_server_hello(SSL *s)
  109. unsigned char *buf;
  110. unsigned char *p,*d;
  111. int i,sl;
  112. - unsigned long l,Time;
  113. + unsigned long l;
  114. +#ifdef OPENSSL_NO_TLSEXT
  115. + unsigned long Time;
  116. +#endif
  117. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  118. {
  119. buf=(unsigned char *)s->init_buf->data;
  120. +#ifdef OPENSSL_NO_TLSEXT
  121. p=s->s3->server_random;
  122. + /* Generate server_random if it was not needed previously */
  123. Time=(unsigned long)time(NULL); /* Time */
  124. l2n(Time,p);
  125. if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
  126. return -1;
  127. +#endif
  128. /* Do the message type and length last */
  129. d=p= &(buf[4]);
  130. diff -upr openssl-0.9.8h.orig/ssl/ssl.h openssl-0.9.8h/ssl/ssl.h
  131. --- openssl-0.9.8h.orig/ssl/ssl.h 2008-04-30 19:11:32.000000000 +0300
  132. +++ openssl-0.9.8h/ssl/ssl.h 2008-05-28 18:49:34.000000000 +0300
  133. @@ -343,6 +343,7 @@ extern "C" {
  134. * 'struct ssl_st *' function parameters used to prototype callbacks
  135. * in SSL_CTX. */
  136. typedef struct ssl_st *ssl_crock_st;
  137. +typedef struct tls_extension_st TLS_EXTENSION;
  138. /* used to hold info on the particular ciphers used */
  139. typedef struct ssl_cipher_st
  140. @@ -364,6 +365,8 @@ DECLARE_STACK_OF(SSL_CIPHER)
  141. typedef struct ssl_st SSL;
  142. typedef struct ssl_ctx_st SSL_CTX;
  143. +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);
  144. +
  145. /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
  146. typedef struct ssl_method_st
  147. {
  148. @@ -1027,6 +1030,14 @@ struct ssl_st
  149. /* RFC4507 session ticket expected to be received or sent */
  150. int tlsext_ticket_expected;
  151. +
  152. + /* TLS extensions */
  153. + TLS_EXTENSION *tls_extension;
  154. +
  155. + /* TLS pre-shared secret session resumption */
  156. + tls_session_secret_cb_fn tls_session_secret_cb;
  157. + void *tls_session_secret_cb_arg;
  158. +
  159. SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
  160. #define session_ctx initial_ctx
  161. #else
  162. @@ -1625,6 +1636,12 @@ void *SSL_COMP_get_compression_methods(v
  163. int SSL_COMP_add_compression_method(int id,void *cm);
  164. #endif
  165. +/* TLS extensions functions */
  166. +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
  167. +
  168. +/* Pre-shared secret session resumption functions */
  169. +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
  170. +
  171. /* BEGIN ERROR CODES */
  172. /* The following lines are auto generated by the script mkerr.pl. Any changes
  173. * made after this point may be overwritten when the script is next run.
  174. @@ -1815,6 +1832,7 @@ void ERR_load_SSL_strings(void);
  175. #define SSL_F_TLS1_ENC 210
  176. #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
  177. #define SSL_F_WRITE_PENDING 212
  178. +#define SSL_F_SSL_SET_HELLO_EXTENSION 213
  179. /* Reason codes. */
  180. #define SSL_R_APP_DATA_IN_HANDSHAKE 100
  181. diff -upr openssl-0.9.8h.orig/ssl/ssl_err.c openssl-0.9.8h/ssl/ssl_err.c
  182. --- openssl-0.9.8h.orig/ssl/ssl_err.c 2007-10-12 03:00:30.000000000 +0300
  183. +++ openssl-0.9.8h/ssl/ssl_err.c 2008-05-28 18:49:34.000000000 +0300
  184. @@ -251,6 +251,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
  185. {ERR_FUNC(SSL_F_TLS1_ENC), "TLS1_ENC"},
  186. {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
  187. {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
  188. +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
  189. {0,NULL}
  190. };
  191. diff -upr openssl-0.9.8h.orig/ssl/ssl_sess.c openssl-0.9.8h/ssl/ssl_sess.c
  192. --- openssl-0.9.8h.orig/ssl/ssl_sess.c 2007-10-17 20:30:15.000000000 +0300
  193. +++ openssl-0.9.8h/ssl/ssl_sess.c 2008-05-28 18:49:34.000000000 +0300
  194. @@ -704,6 +704,52 @@ long SSL_CTX_get_timeout(const SSL_CTX *
  195. return(s->session_timeout);
  196. }
  197. +#ifndef OPENSSL_NO_TLSEXT
  198. +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
  199. + STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
  200. +{
  201. + if (s == NULL) return(0);
  202. + s->tls_session_secret_cb = tls_session_secret_cb;
  203. + s->tls_session_secret_cb_arg = arg;
  204. + return(1);
  205. +}
  206. +
  207. +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
  208. +{
  209. + if(s->version >= TLS1_VERSION)
  210. + {
  211. + if(s->tls_extension)
  212. + {
  213. + OPENSSL_free(s->tls_extension);
  214. + s->tls_extension = NULL;
  215. + }
  216. +
  217. + s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
  218. + if(!s->tls_extension)
  219. + {
  220. + SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
  221. + return 0;
  222. + }
  223. +
  224. + s->tls_extension->type = ext_type;
  225. +
  226. + if(ext_data)
  227. + {
  228. + s->tls_extension->length = ext_len;
  229. + s->tls_extension->data = s->tls_extension + 1;
  230. + memcpy(s->tls_extension->data, ext_data, ext_len);
  231. + } else {
  232. + s->tls_extension->length = 0;
  233. + s->tls_extension->data = NULL;
  234. + }
  235. +
  236. + return 1;
  237. + }
  238. +
  239. + return 0;
  240. +}
  241. +#endif /* OPENSSL_NO_TLSEXT */
  242. +
  243. typedef struct timeout_param_st
  244. {
  245. SSL_CTX *ctx;
  246. diff -upr openssl-0.9.8h.orig/ssl/t1_lib.c openssl-0.9.8h/ssl/t1_lib.c
  247. --- openssl-0.9.8h.orig/ssl/t1_lib.c 2008-05-28 10:26:33.000000000 +0300
  248. +++ openssl-0.9.8h/ssl/t1_lib.c 2008-05-28 18:49:34.000000000 +0300
  249. @@ -106,6 +106,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. @@ -175,8 +181,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. @@ -190,6 +212,7 @@ unsigned char *ssl_add_clienthello_tlsex
  286. ret += ticklen;
  287. }
  288. }
  289. + skip_ext:
  290. if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
  291. {
  292. @@ -774,6 +797,8 @@ 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. + return 0;
  298. return tls_decrypt_ticket(s, p, size, session_id, len,
  299. ret);
  300. }
  301. diff -upr openssl-0.9.8h.orig/ssl/tls1.h openssl-0.9.8h/ssl/tls1.h
  302. --- openssl-0.9.8h.orig/ssl/tls1.h 2008-04-30 19:11:33.000000000 +0300
  303. +++ openssl-0.9.8h/ssl/tls1.h 2008-05-28 18:49:34.000000000 +0300
  304. @@ -398,6 +398,14 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_T
  305. #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
  306. #endif
  307. +/* TLS extension struct */
  308. +struct tls_extension_st
  309. +{
  310. + unsigned short type;
  311. + unsigned short length;
  312. + void *data;
  313. +};
  314. +
  315. #ifdef __cplusplus
  316. }
  317. #endif
  318. diff -upr openssl-0.9.8h.orig/util/ssleay.num openssl-0.9.8h/util/ssleay.num
  319. --- openssl-0.9.8h.orig/util/ssleay.num 2007-08-13 01:31:16.000000000 +0300
  320. +++ openssl-0.9.8h/util/ssleay.num 2008-05-28 18:49:34.000000000 +0300
  321. @@ -241,3 +241,5 @@ SSL_CTX_sess_get_remove_cb
  322. SSL_set_SSL_CTX 290 EXIST::FUNCTION:
  323. SSL_get_servername 291 EXIST::FUNCTION:TLSEXT
  324. SSL_get_servername_type 292 EXIST::FUNCTION:TLSEXT
  325. +SSL_set_hello_extension 305 EXIST::FUNCTION:TLSEXT
  326. +SSL_set_session_secret_cb 306 EXIST::FUNCTION:TLSEXT