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