openssl-0.9.8e-tls-extensions.patch 12 KB

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