openssl-0.9.8d-tls-extensions.patch 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. This patch is adding support for TLS hello extensions and externally
  2. generated pre-shared key material to OpenSSL 0.9.8d. This is
  3. 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 -uprN openssl-0.9.8d.orig/include/openssl/ssl.h openssl-0.9.8d/include/openssl/ssl.h
  6. --- openssl-0.9.8d.orig/include/openssl/ssl.h 2006-06-14 06:52:49.000000000 -0700
  7. +++ openssl-0.9.8d/include/openssl/ssl.h 2006-12-10 08:20:02.000000000 -0800
  8. @@ -345,6 +345,7 @@ extern "C" {
  9. * 'struct ssl_st *' function parameters used to prototype callbacks
  10. * in SSL_CTX. */
  11. typedef struct ssl_st *ssl_crock_st;
  12. +typedef struct tls_extension_st TLS_EXTENSION;
  13. /* used to hold info on the particular ciphers used */
  14. typedef struct ssl_cipher_st
  15. @@ -366,6 +367,8 @@ DECLARE_STACK_OF(SSL_CIPHER)
  16. typedef struct ssl_st SSL;
  17. typedef struct ssl_ctx_st SSL_CTX;
  18. +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);
  19. +
  20. /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
  21. typedef struct ssl_method_st
  22. {
  23. @@ -973,6 +976,15 @@ struct ssl_st
  24. int first_packet;
  25. int client_version; /* what was passed, used for
  26. * SSLv3/TLS rollback check */
  27. +
  28. + /* TLS externsions */
  29. + TLS_EXTENSION *tls_extension;
  30. + int (*tls_extension_cb)(SSL *s, TLS_EXTENSION *tls_ext, void *arg);
  31. + void *tls_extension_cb_arg;
  32. +
  33. + /* TLS pre-shared secret session resumption */
  34. + tls_session_secret_cb_fn tls_session_secret_cb;
  35. + void *tls_session_secret_cb_arg;
  36. };
  37. #ifdef __cplusplus
  38. @@ -1538,6 +1550,13 @@ void *SSL_COMP_get_compression_methods(v
  39. int SSL_COMP_add_compression_method(int id,void *cm);
  40. #endif
  41. +/* TLS extensions functions */
  42. +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
  43. +int SSL_set_hello_extension_cb(SSL *s, int (*cb)(SSL *, TLS_EXTENSION *, void *), void *arg);
  44. +
  45. +/* Pre-shared secret session resumption functions */
  46. +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
  47. +
  48. /* BEGIN ERROR CODES */
  49. /* The following lines are auto generated by the script mkerr.pl. Any changes
  50. * made after this point may be overwritten when the script is next run.
  51. @@ -1719,6 +1738,7 @@ void ERR_load_SSL_strings(void);
  52. #define SSL_F_TLS1_ENC 210
  53. #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
  54. #define SSL_F_WRITE_PENDING 212
  55. +#define SSL_F_SSL_SET_HELLO_EXTENSION 213
  56. /* Reason codes. */
  57. #define SSL_R_APP_DATA_IN_HANDSHAKE 100
  58. diff -uprN openssl-0.9.8d.orig/include/openssl/tls1.h openssl-0.9.8d/include/openssl/tls1.h
  59. --- openssl-0.9.8d.orig/include/openssl/tls1.h 2006-06-14 10:52:01.000000000 -0700
  60. +++ openssl-0.9.8d/include/openssl/tls1.h 2006-12-10 08:20:02.000000000 -0800
  61. @@ -296,6 +296,14 @@ extern "C" {
  62. #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
  63. #endif
  64. +/* TLS extension struct */
  65. +struct tls_extension_st
  66. +{
  67. + unsigned short type;
  68. + unsigned short length;
  69. + void *data;
  70. +};
  71. +
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75. diff -uprN openssl-0.9.8d.orig/ssl/Makefile openssl-0.9.8d/ssl/Makefile
  76. --- openssl-0.9.8d.orig/ssl/Makefile 2006-02-03 17:49:35.000000000 -0800
  77. +++ openssl-0.9.8d/ssl/Makefile 2006-12-10 08:20:02.000000000 -0800
  78. @@ -24,7 +24,7 @@ LIBSRC= \
  79. s2_meth.c s2_srvr.c s2_clnt.c s2_lib.c s2_enc.c s2_pkt.c \
  80. s3_meth.c s3_srvr.c s3_clnt.c s3_lib.c s3_enc.c s3_pkt.c s3_both.c \
  81. s23_meth.c s23_srvr.c s23_clnt.c s23_lib.c s23_pkt.c \
  82. - t1_meth.c t1_srvr.c t1_clnt.c t1_lib.c t1_enc.c \
  83. + t1_meth.c t1_srvr.c t1_clnt.c t1_lib.c t1_enc.c t1_ext.c \
  84. d1_meth.c d1_srvr.c d1_clnt.c d1_lib.c d1_pkt.c \
  85. d1_both.c d1_enc.c \
  86. ssl_lib.c ssl_err2.c ssl_cert.c ssl_sess.c \
  87. @@ -35,7 +35,7 @@ LIBOBJ= \
  88. s2_meth.o s2_srvr.o s2_clnt.o s2_lib.o s2_enc.o s2_pkt.o \
  89. s3_meth.o s3_srvr.o s3_clnt.o s3_lib.o s3_enc.o s3_pkt.o s3_both.o \
  90. s23_meth.o s23_srvr.o s23_clnt.o s23_lib.o s23_pkt.o \
  91. - t1_meth.o t1_srvr.o t1_clnt.o t1_lib.o t1_enc.o \
  92. + t1_meth.o t1_srvr.o t1_clnt.o t1_lib.o t1_enc.o t1_ext.o \
  93. d1_meth.o d1_srvr.o d1_clnt.o d1_lib.o d1_pkt.o \
  94. d1_both.o d1_enc.o \
  95. ssl_lib.o ssl_err2.o ssl_cert.o ssl_sess.o \
  96. @@ -968,3 +968,4 @@ t1_srvr.o: ../include/openssl/ssl23.h ..
  97. t1_srvr.o: ../include/openssl/stack.h ../include/openssl/symhacks.h
  98. t1_srvr.o: ../include/openssl/tls1.h ../include/openssl/x509.h
  99. t1_srvr.o: ../include/openssl/x509_vfy.h ssl_locl.h t1_srvr.c
  100. +t1_ext.o: t1_ext.c ssl_locl.h
  101. diff -uprN openssl-0.9.8d.orig/ssl/s3_clnt.c openssl-0.9.8d/ssl/s3_clnt.c
  102. --- openssl-0.9.8d.orig/ssl/s3_clnt.c 2005-12-12 23:41:46.000000000 -0800
  103. +++ openssl-0.9.8d/ssl/s3_clnt.c 2006-12-10 08:20:02.000000000 -0800
  104. @@ -601,6 +601,20 @@ int ssl3_client_hello(SSL *s)
  105. #endif
  106. *(p++)=0; /* Add the NULL method */
  107. + /* send client hello extensions if any */
  108. + if (s->version >= TLS1_VERSION && s->tls_extension)
  109. + {
  110. + // set the total extensions length
  111. + s2n(s->tls_extension->length + 4, p);
  112. +
  113. + // put the extensions with type and length
  114. + s2n(s->tls_extension->type, p);
  115. + s2n(s->tls_extension->length, p);
  116. +
  117. + memcpy(p, s->tls_extension->data, s->tls_extension->length);
  118. + p+=s->tls_extension->length;
  119. + }
  120. +
  121. l=(p-d);
  122. d=buf;
  123. *(d++)=SSL3_MT_CLIENT_HELLO;
  124. @@ -623,7 +637,7 @@ int ssl3_get_server_hello(SSL *s)
  125. STACK_OF(SSL_CIPHER) *sk;
  126. SSL_CIPHER *c;
  127. unsigned char *p,*d;
  128. - int i,al,ok;
  129. + int i,al,ok,pre_shared;
  130. unsigned int j;
  131. long n;
  132. #ifndef OPENSSL_NO_COMP
  133. @@ -690,7 +704,24 @@ int ssl3_get_server_hello(SSL *s)
  134. goto f_err;
  135. }
  136. - if (j != 0 && j == s->session->session_id_length
  137. + /* check if we want to resume the session based on external pre-shared secret */
  138. + pre_shared = 0;
  139. + if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  140. + {
  141. + SSL_CIPHER *pref_cipher=NULL;
  142. + s->session->master_key_length=sizeof(s->session->master_key);
  143. + if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
  144. + NULL, &pref_cipher, s->tls_session_secret_cb_arg))
  145. + {
  146. + s->hit=1;
  147. + s->session->cipher=pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s,p+j);
  148. + s->session->session_id_length = j;
  149. + memcpy(s->session->session_id, p, j);
  150. + pre_shared = 1;
  151. + }
  152. + }
  153. +
  154. + if ((pre_shared || j != 0) && j == s->session->session_id_length
  155. && memcmp(p,s->session->session_id,j) == 0)
  156. {
  157. if(s->sid_ctx_length != s->session->sid_ctx_length
  158. diff -uprN openssl-0.9.8d.orig/ssl/s3_srvr.c openssl-0.9.8d/ssl/s3_srvr.c
  159. --- openssl-0.9.8d.orig/ssl/s3_srvr.c 2006-09-28 04:29:03.000000000 -0700
  160. +++ openssl-0.9.8d/ssl/s3_srvr.c 2006-12-10 08:20:02.000000000 -0800
  161. @@ -943,6 +943,75 @@ int ssl3_get_client_hello(SSL *s)
  162. }
  163. #endif
  164. + /* Check for TLS client hello extension here */
  165. + if (p < (d+n) && s->version >= TLS1_VERSION)
  166. + {
  167. + if (s->tls_extension_cb)
  168. + {
  169. + TLS_EXTENSION tls_ext;
  170. + unsigned short ext_total_len;
  171. +
  172. + n2s(p, ext_total_len);
  173. + n2s(p, tls_ext.type);
  174. + n2s(p, tls_ext.length);
  175. +
  176. + // sanity check in TLS extension len
  177. + if (tls_ext.length > (d+n) - p)
  178. + {
  179. + // just cut the lenth to packet border
  180. + tls_ext.length = (d+n) - p;
  181. + }
  182. +
  183. + tls_ext.data = p;
  184. +
  185. + // returns an alert code or 0
  186. + al = s->tls_extension_cb(s, &tls_ext, s->tls_extension_cb_arg);
  187. + if (al != 0)
  188. + {
  189. + SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PEER_ERROR);
  190. + goto f_err;
  191. + }
  192. + }
  193. + }
  194. +
  195. + /* Check if we want to use external pre-shared secret for this handshake */
  196. + /* for not reused session only */
  197. + if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  198. + {
  199. + SSL_CIPHER *pref_cipher=NULL;
  200. +
  201. + s->session->master_key_length=sizeof(s->session->master_key);
  202. + if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
  203. + ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
  204. + {
  205. + s->hit=1;
  206. + s->session->ciphers=ciphers;
  207. + s->session->verify_result=X509_V_OK;
  208. +
  209. + ciphers=NULL;
  210. +
  211. + /* check if some cipher was preferred by call back */
  212. + pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
  213. + if (pref_cipher == NULL)
  214. + {
  215. + al=SSL_AD_HANDSHAKE_FAILURE;
  216. + SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
  217. + goto f_err;
  218. + }
  219. +
  220. + s->session->cipher=pref_cipher;
  221. +
  222. + if (s->cipher_list)
  223. + sk_SSL_CIPHER_free(s->cipher_list);
  224. +
  225. + if (s->cipher_list_by_id)
  226. + sk_SSL_CIPHER_free(s->cipher_list_by_id);
  227. +
  228. + s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
  229. + s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
  230. + }
  231. + }
  232. +
  233. /* Given s->session->ciphers and SSL_get_ciphers, we must
  234. * pick a cipher */
  235. diff -uprN openssl-0.9.8d.orig/ssl/ssl.h openssl-0.9.8d/ssl/ssl.h
  236. --- openssl-0.9.8d.orig/ssl/ssl.h 2006-06-14 06:52:49.000000000 -0700
  237. +++ openssl-0.9.8d/ssl/ssl.h 2006-12-10 08:20:02.000000000 -0800
  238. @@ -345,6 +345,7 @@ extern "C" {
  239. * 'struct ssl_st *' function parameters used to prototype callbacks
  240. * in SSL_CTX. */
  241. typedef struct ssl_st *ssl_crock_st;
  242. +typedef struct tls_extension_st TLS_EXTENSION;
  243. /* used to hold info on the particular ciphers used */
  244. typedef struct ssl_cipher_st
  245. @@ -366,6 +367,8 @@ DECLARE_STACK_OF(SSL_CIPHER)
  246. typedef struct ssl_st SSL;
  247. typedef struct ssl_ctx_st SSL_CTX;
  248. +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);
  249. +
  250. /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
  251. typedef struct ssl_method_st
  252. {
  253. @@ -973,6 +976,15 @@ struct ssl_st
  254. int first_packet;
  255. int client_version; /* what was passed, used for
  256. * SSLv3/TLS rollback check */
  257. +
  258. + /* TLS externsions */
  259. + TLS_EXTENSION *tls_extension;
  260. + int (*tls_extension_cb)(SSL *s, TLS_EXTENSION *tls_ext, void *arg);
  261. + void *tls_extension_cb_arg;
  262. +
  263. + /* TLS pre-shared secret session resumption */
  264. + tls_session_secret_cb_fn tls_session_secret_cb;
  265. + void *tls_session_secret_cb_arg;
  266. };
  267. #ifdef __cplusplus
  268. @@ -1538,6 +1550,13 @@ void *SSL_COMP_get_compression_methods(v
  269. int SSL_COMP_add_compression_method(int id,void *cm);
  270. #endif
  271. +/* TLS extensions functions */
  272. +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len);
  273. +int SSL_set_hello_extension_cb(SSL *s, int (*cb)(SSL *, TLS_EXTENSION *, void *), void *arg);
  274. +
  275. +/* Pre-shared secret session resumption functions */
  276. +int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
  277. +
  278. /* BEGIN ERROR CODES */
  279. /* The following lines are auto generated by the script mkerr.pl. Any changes
  280. * made after this point may be overwritten when the script is next run.
  281. @@ -1719,6 +1738,7 @@ void ERR_load_SSL_strings(void);
  282. #define SSL_F_TLS1_ENC 210
  283. #define SSL_F_TLS1_SETUP_KEY_BLOCK 211
  284. #define SSL_F_WRITE_PENDING 212
  285. +#define SSL_F_SSL_SET_HELLO_EXTENSION 213
  286. /* Reason codes. */
  287. #define SSL_R_APP_DATA_IN_HANDSHAKE 100
  288. diff -uprN openssl-0.9.8d.orig/ssl/ssl_err.c openssl-0.9.8d/ssl/ssl_err.c
  289. --- openssl-0.9.8d.orig/ssl/ssl_err.c 2006-01-08 13:52:46.000000000 -0800
  290. +++ openssl-0.9.8d/ssl/ssl_err.c 2006-12-10 08:20:02.000000000 -0800
  291. @@ -242,6 +242,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
  292. {ERR_FUNC(SSL_F_TLS1_ENC), "TLS1_ENC"},
  293. {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
  294. {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"},
  295. +{ERR_FUNC(SSL_F_SSL_SET_HELLO_EXTENSION), "SSL_set_hello_extension"},
  296. {0,NULL}
  297. };
  298. diff -uprN openssl-0.9.8d.orig/ssl/ssl_sess.c openssl-0.9.8d/ssl/ssl_sess.c
  299. --- openssl-0.9.8d.orig/ssl/ssl_sess.c 2005-12-30 15:51:57.000000000 -0800
  300. +++ openssl-0.9.8d/ssl/ssl_sess.c 2006-12-10 08:20:02.000000000 -0800
  301. @@ -656,6 +656,15 @@ long SSL_CTX_get_timeout(const SSL_CTX *
  302. return(s->session_timeout);
  303. }
  304. +int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
  305. + STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
  306. +{
  307. + if (s == NULL) return(0);
  308. + s->tls_session_secret_cb = tls_session_secret_cb;
  309. + s->tls_session_secret_cb_arg = arg;
  310. + return(1);
  311. +}
  312. +
  313. typedef struct timeout_param_st
  314. {
  315. SSL_CTX *ctx;
  316. diff -uprN openssl-0.9.8d.orig/ssl/t1_ext.c openssl-0.9.8d/ssl/t1_ext.c
  317. --- openssl-0.9.8d.orig/ssl/t1_ext.c 1969-12-31 16:00:00.000000000 -0800
  318. +++ openssl-0.9.8d/ssl/t1_ext.c 2006-12-10 08:20:02.000000000 -0800
  319. @@ -0,0 +1,48 @@
  320. +
  321. +#include <stdio.h>
  322. +#include "ssl_locl.h"
  323. +
  324. +
  325. +int SSL_set_hello_extension(SSL *s, int ext_type, void *ext_data, int ext_len)
  326. +{
  327. + if(s->version >= TLS1_VERSION)
  328. + {
  329. + if(s->tls_extension)
  330. + {
  331. + OPENSSL_free(s->tls_extension);
  332. + s->tls_extension = NULL;
  333. + }
  334. +
  335. + if(ext_data)
  336. + {
  337. + s->tls_extension = OPENSSL_malloc(sizeof(TLS_EXTENSION) + ext_len);
  338. + if(!s->tls_extension)
  339. + {
  340. + SSLerr(SSL_F_SSL_SET_HELLO_EXTENSION, ERR_R_MALLOC_FAILURE);
  341. + return 0;
  342. + }
  343. +
  344. + s->tls_extension->type = ext_type;
  345. + s->tls_extension->length = ext_len;
  346. + s->tls_extension->data = s->tls_extension + 1;
  347. + memcpy(s->tls_extension->data, ext_data, ext_len);
  348. + }
  349. +
  350. + return 1;
  351. + }
  352. +
  353. + return 0;
  354. +}
  355. +
  356. +int SSL_set_hello_extension_cb(SSL *s, int (*cb)(SSL *, TLS_EXTENSION *, void *), void *arg)
  357. +{
  358. + if(s->version >= TLS1_VERSION)
  359. + {
  360. + s->tls_extension_cb = cb;
  361. + s->tls_extension_cb_arg = arg;
  362. +
  363. + return 1;
  364. + }
  365. +
  366. + return 0;
  367. +}
  368. diff -uprN openssl-0.9.8d.orig/ssl/t1_lib.c openssl-0.9.8d/ssl/t1_lib.c
  369. --- openssl-0.9.8d.orig/ssl/t1_lib.c 2005-08-05 16:52:07.000000000 -0700
  370. +++ openssl-0.9.8d/ssl/t1_lib.c 2006-12-10 08:20:02.000000000 -0800
  371. @@ -97,6 +97,10 @@ int tls1_new(SSL *s)
  372. void tls1_free(SSL *s)
  373. {
  374. + if(s->tls_extension)
  375. + {
  376. + OPENSSL_free(s->tls_extension);
  377. + }
  378. ssl3_free(s);
  379. }
  380. diff -uprN openssl-0.9.8d.orig/ssl/tls1.h openssl-0.9.8d/ssl/tls1.h
  381. --- openssl-0.9.8d.orig/ssl/tls1.h 2006-06-14 10:52:01.000000000 -0700
  382. +++ openssl-0.9.8d/ssl/tls1.h 2006-12-10 08:20:02.000000000 -0800
  383. @@ -296,6 +296,14 @@ extern "C" {
  384. #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/
  385. #endif
  386. +/* TLS extension struct */
  387. +struct tls_extension_st
  388. +{
  389. + unsigned short type;
  390. + unsigned short length;
  391. + void *data;
  392. +};
  393. +
  394. #ifdef __cplusplus
  395. }
  396. #endif
  397. diff -uprN openssl-0.9.8d.orig/util/ssleay.num openssl-0.9.8d/util/ssleay.num
  398. --- openssl-0.9.8d.orig/util/ssleay.num 2005-05-08 17:22:02.000000000 -0700
  399. +++ openssl-0.9.8d/util/ssleay.num 2006-12-10 08:20:02.000000000 -0800
  400. @@ -226,3 +226,6 @@ DTLSv1_server_method
  401. SSL_COMP_get_compression_methods 276 EXIST:!VMS:FUNCTION:COMP
  402. SSL_COMP_get_compress_methods 276 EXIST:VMS:FUNCTION:COMP
  403. SSL_SESSION_get_id 277 EXIST::FUNCTION:
  404. +SSL_set_hello_extension 278 EXIST::FUNCTION:
  405. +SSL_set_hello_extension_cb 279 EXIST::FUNCTION:
  406. +SSL_set_session_secret_cb 280 EXIST::FUNCTION: