openssl-0.9.8i-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.8i 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.8i.orig/ssl/s3_clnt.c openssl-0.9.8i/ssl/s3_clnt.c
  9. --- openssl-0.9.8i.orig/ssl/s3_clnt.c 2008-06-16 19:56:41.000000000 +0300
  10. +++ openssl-0.9.8i/ssl/s3_clnt.c 2008-09-28 16:50:18.000000000 +0300
  11. @@ -759,6 +759,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. @@ -2701,11 +2715,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.8i.orig/ssl/s3_srvr.c openssl-0.9.8i/ssl/s3_srvr.c
  46. --- openssl-0.9.8i.orig/ssl/s3_srvr.c 2008-09-14 21:16:09.000000000 +0300
  47. +++ openssl-0.9.8i/ssl/s3_srvr.c 2008-09-28 16:50:18.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.8i.orig/ssl/ssl_err.c openssl-0.9.8i/ssl/ssl_err.c
  131. --- openssl-0.9.8i.orig/ssl/ssl_err.c 2008-08-13 22:44:44.000000000 +0300
  132. +++ openssl-0.9.8i/ssl/ssl_err.c 2008-09-28 16:50:18.000000000 +0300
  133. @@ -253,6 +253,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
  134. {ERR_FUNC(SSL_F_TLS1_ENC), "TLS1_ENC"},
  135. {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
  136. {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
  137. +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
  138. {0,NULL}
  139. };
  140. diff -upr openssl-0.9.8i.orig/ssl/ssl.h openssl-0.9.8i/ssl/ssl.h
  141. --- openssl-0.9.8i.orig/ssl/ssl.h 2008-08-13 22:44:44.000000000 +0300
  142. +++ openssl-0.9.8i/ssl/ssl.h 2008-09-28 16:50:18.000000000 +0300
  143. @@ -344,6 +344,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. @@ -362,6 +363,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. @@ -1034,6 +1037,14 @@ struct ssl_st
  158. /* RFC4507 session ticket expected to be received or sent */
  159. int tlsext_ticket_expected;
  160. +
  161. + /* TLS extensions */
  162. + TLS_EXTENSION *tls_extension;
  163. +
  164. + /* TLS pre-shared secret session resumption */
  165. + tls_session_secret_cb_fn tls_session_secret_cb;
  166. + void *tls_session_secret_cb_arg;
  167. +
  168. SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
  169. #define session_ctx initial_ctx
  170. #else
  171. @@ -1632,6 +1643,12 @@ void *SSL_COMP_get_compression_methods(v
  172. int SSL_COMP_add_compression_method(int id,void *cm);
  173. #endif
  174. +/* TLS extensions functions */
  175. +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
  176. +
  177. +/* Pre-shared secret session resumption functions */
  178. +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
  179. +
  180. /* BEGIN ERROR CODES */
  181. /* The following lines are auto generated by the script mkerr.pl. Any changes
  182. * made after this point may be overwritten when the script is next run.
  183. @@ -1824,6 +1841,7 @@ void ERR_load_SSL_strings(void);
  184. #define SSL_F_TLS1_ENC 210
  185. #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
  186. #define SSL_F_WRITE_PENDING 212
  187. +#define SSL_F_SSL_SET_HELLO_EXTENSION 213
  188. /* Reason codes. */
  189. #define SSL_R_APP_DATA_IN_HANDSHAKE 100
  190. diff -upr openssl-0.9.8i.orig/ssl/ssl_sess.c openssl-0.9.8i/ssl/ssl_sess.c
  191. --- openssl-0.9.8i.orig/ssl/ssl_sess.c 2008-06-04 21:35:27.000000000 +0300
  192. +++ openssl-0.9.8i/ssl/ssl_sess.c 2008-09-28 16:50:18.000000000 +0300
  193. @@ -707,6 +707,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. diff -upr openssl-0.9.8i.orig/ssl/t1_lib.c openssl-0.9.8i/ssl/t1_lib.c
  246. --- openssl-0.9.8i.orig/ssl/t1_lib.c 2008-09-04 01:13:04.000000000 +0300
  247. +++ openssl-0.9.8i/ssl/t1_lib.c 2008-09-28 16:50:18.000000000 +0300
  248. @@ -106,6 +106,12 @@ int tls1_new(SSL *s)
  249. void tls1_free(SSL *s)
  250. {
  251. +#ifndef OPENSSL_NO_TLSEXT
  252. + if(s->tls_extension)
  253. + {
  254. + OPENSSL_free(s->tls_extension);
  255. + }
  256. +#endif
  257. ssl3_free(s);
  258. }
  259. @@ -175,8 +181,24 @@ unsigned char *ssl_add_clienthello_tlsex
  260. int ticklen;
  261. if (s->session && s->session->tlsext_tick)
  262. ticklen = s->session->tlsext_ticklen;
  263. + else if (s->session && s->tls_extension &&
  264. + s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
  265. + s->tls_extension->data)
  266. + {
  267. + ticklen = s->tls_extension->length;
  268. + s->session->tlsext_tick = OPENSSL_malloc(ticklen);
  269. + if (!s->session->tlsext_tick)
  270. + return NULL;
  271. + memcpy(s->session->tlsext_tick, s->tls_extension->data,
  272. + ticklen);
  273. + s->session->tlsext_ticklen = ticklen;
  274. + }
  275. else
  276. ticklen = 0;
  277. + if (ticklen == 0 && s->tls_extension &&
  278. + s->tls_extension->type == TLSEXT_TYPE_session_ticket &&
  279. + s->tls_extension->data == NULL)
  280. + goto skip_ext;
  281. /* Check for enough room 2 for extension type, 2 for len
  282. * rest for ticket
  283. */
  284. @@ -190,6 +212,7 @@ unsigned char *ssl_add_clienthello_tlsex
  285. ret += ticklen;
  286. }
  287. }
  288. + skip_ext:
  289. if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
  290. {
  291. @@ -776,6 +799,8 @@ int tls1_process_ticket(SSL *s, unsigned
  292. s->tlsext_ticket_expected = 1;
  293. return 0; /* Cache miss */
  294. }
  295. + if (s->tls_session_secret_cb)
  296. + return 0;
  297. return tls_decrypt_ticket(s, p, size, session_id, len,
  298. ret);
  299. }
  300. diff -upr openssl-0.9.8i.orig/ssl/tls1.h openssl-0.9.8i/ssl/tls1.h
  301. --- openssl-0.9.8i.orig/ssl/tls1.h 2008-04-30 19:11:33.000000000 +0300
  302. +++ openssl-0.9.8i/ssl/tls1.h 2008-09-28 16:50:18.000000000 +0300
  303. @@ -398,6 +398,14 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_T
  304. #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
  305. #endif
  306. +/* TLS extension struct */
  307. +struct tls_extension_st
  308. +{
  309. + unsigned short type;
  310. + unsigned short length;
  311. + void *data;
  312. +};
  313. +
  314. #ifdef __cplusplus
  315. }
  316. #endif
  317. diff -upr openssl-0.9.8i.orig/util/ssleay.num openssl-0.9.8i/util/ssleay.num
  318. --- openssl-0.9.8i.orig/util/ssleay.num 2008-06-05 13:57:21.000000000 +0300
  319. +++ openssl-0.9.8i/util/ssleay.num 2008-09-28 16:50:57.000000000 +0300
  320. @@ -242,3 +242,5 @@ SSL_set_SSL_CTX
  321. SSL_get_servername 291 EXIST::FUNCTION:TLSEXT
  322. SSL_get_servername_type 292 EXIST::FUNCTION:TLSEXT
  323. SSL_CTX_set_client_cert_engine 293 EXIST::FUNCTION:ENGINE
  324. +SSL_set_hello_extension 305 EXIST::FUNCTION:TLSEXT
  325. +SSL_set_session_secret_cb 306 EXIST::FUNCTION:TLSEXT