openssl-0.9.8i-tls-extensions.patch revision 214501
1296465SdelphijThis patch adds support for TLS SessionTicket extension (RFC 5077) for 2110010Smarkmthe parts used by EAP-FAST (RFC 4851). 3110010Smarkm 4142429SnectarThis is based on the patch from Alexey Kobozev <akobozev@cisco.com> 5110010Smarkm(sent to openssl-dev mailing list on Tue, 07 Jun 2005 15:40:58 +0300). 6110010Smarkm 7110010SmarkmOpenSSL 0.9.8i does not enable TLS extension support by default, so it 8110010Smarkmwill need to be enabled by adding enable-tlsext to config script 9110010Smarkmcommand line. 10110010Smarkm 11110010Smarkm 12110010SmarkmIndex: openssl-0.9.8i/ssl/s3_clnt.c 13110010Smarkm=================================================================== 14110010Smarkm--- openssl-0.9.8i.orig/ssl/s3_clnt.c 2008-06-16 19:56:41.000000000 +0300 15110010Smarkm+++ openssl-0.9.8i/ssl/s3_clnt.c 2008-11-23 20:39:40.000000000 +0200 16110010Smarkm@@ -759,6 +759,21 @@ 17110010Smarkm goto f_err; 18110010Smarkm } 19110010Smarkm 20215698Ssimon+#ifndef OPENSSL_NO_TLSEXT 21215698Ssimon+ /* check if we want to resume the session based on external pre-shared secret */ 22215698Ssimon+ if (s->version >= TLS1_VERSION && s->tls_session_secret_cb) 23215698Ssimon+ { 24215698Ssimon+ SSL_CIPHER *pref_cipher=NULL; 25110010Smarkm+ s->session->master_key_length=sizeof(s->session->master_key); 26110010Smarkm+ if (s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length, 27110010Smarkm+ NULL, &pref_cipher, s->tls_session_secret_cb_arg)) 28110010Smarkm+ { 29110010Smarkm+ s->session->cipher=pref_cipher ? 30110010Smarkm+ pref_cipher : ssl_get_cipher_by_char(s,p+j); 31110010Smarkm+ } 32110010Smarkm+ } 33110010Smarkm+#endif /* OPENSSL_NO_TLSEXT */ 34110010Smarkm+ 35110010Smarkm if (j != 0 && j == s->session->session_id_length 36110010Smarkm && memcmp(p,s->session->session_id,j) == 0) 37110010Smarkm { 38110010Smarkm@@ -2701,11 +2716,8 @@ 39110010Smarkm { 40110010Smarkm int ok; 41279265Sdelphij long n; 42279265Sdelphij- /* If we have no ticket or session ID is non-zero length (a match of 43110010Smarkm- * a non-zero session length would never reach here) it cannot be a 44110010Smarkm- * resumed session. 45215698Ssimon- */ 46215698Ssimon- if (!s->session->tlsext_tick || s->session->session_id_length) 47215698Ssimon+ /* If we have no ticket it cannot be a resumed session. */ 48215698Ssimon+ if (!s->session->tlsext_tick) 49142429Snectar return 1; 50215698Ssimon /* this function is called when we really expect a Certificate 51142429Snectar * message, so permit appropriate message length */ 52142429SnectarIndex: openssl-0.9.8i/ssl/s3_srvr.c 53279265Sdelphij=================================================================== 54279265Sdelphij--- openssl-0.9.8i.orig/ssl/s3_srvr.c 2008-09-14 21:16:09.000000000 +0300 55279265Sdelphij+++ openssl-0.9.8i/ssl/s3_srvr.c 2008-11-23 20:37:40.000000000 +0200 56110010Smarkm@@ -959,6 +959,59 @@ 57279265Sdelphij SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT); 58279265Sdelphij goto err; 59279265Sdelphij } 60279265Sdelphij+ 61279265Sdelphij+ /* Check if we want to use external pre-shared secret for this 62279265Sdelphij+ * handshake for not reused session only. We need to generate 63215698Ssimon+ * server_random before calling tls_session_secret_cb in order to allow 64279265Sdelphij+ * SessionTicket processing to use it in key derivation. */ 65279265Sdelphij+ { 66279265Sdelphij+ unsigned long Time; 67279265Sdelphij+ unsigned char *pos; 68279265Sdelphij+ Time=(unsigned long)time(NULL); /* Time */ 69215698Ssimon+ pos=s->s3->server_random; 70279265Sdelphij+ l2n(Time,pos); 71110010Smarkm+ if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0) 72110010Smarkm+ { 73110010Smarkm+ al=SSL_AD_INTERNAL_ERROR; 74110010Smarkm+ goto f_err; 75110010Smarkm+ } 76110010Smarkm+ } 77110010Smarkm+ 78110010Smarkm+ if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) 79110010Smarkm+ { 80110010Smarkm+ SSL_CIPHER *pref_cipher=NULL; 81110010Smarkm+ 82110010Smarkm+ s->session->master_key_length=sizeof(s->session->master_key); 83110010Smarkm+ if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length, 84110010Smarkm+ ciphers, &pref_cipher, s->tls_session_secret_cb_arg)) 85110010Smarkm+ { 86110010Smarkm+ s->hit=1; 87110010Smarkm+ s->session->ciphers=ciphers; 88110010Smarkm+ s->session->verify_result=X509_V_OK; 89110010Smarkm+ 90110010Smarkm+ ciphers=NULL; 91110010Smarkm+ 92110010Smarkm+ /* check if some cipher was preferred by call back */ 93110010Smarkm+ pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s)); 94110010Smarkm+ if (pref_cipher == NULL) 95110010Smarkm+ { 96110010Smarkm+ al=SSL_AD_HANDSHAKE_FAILURE; 97110010Smarkm+ SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER); 98110010Smarkm+ goto f_err; 99110010Smarkm+ } 100110010Smarkm+ 101110010Smarkm+ s->session->cipher=pref_cipher; 102110010Smarkm+ 103110010Smarkm+ if (s->cipher_list) 104110010Smarkm+ sk_SSL_CIPHER_free(s->cipher_list); 105110010Smarkm+ 106110010Smarkm+ if (s->cipher_list_by_id) 107110010Smarkm+ sk_SSL_CIPHER_free(s->cipher_list_by_id); 108110010Smarkm+ 109110010Smarkm+ s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers); 110110010Smarkm+ s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers); 111110010Smarkm+ } 112110010Smarkm+ } 113110010Smarkm #endif 114110010Smarkm /* Worst case, we will use the NULL compression, but if we have other 115110010Smarkm * options, we will now look for them. We have i-1 compression 116110010Smarkm@@ -1097,16 +1150,22 @@ 117110010Smarkm unsigned char *buf; 118110010Smarkm unsigned char *p,*d; 119110010Smarkm int i,sl; 120110010Smarkm- unsigned long l,Time; 121110010Smarkm+ unsigned long l; 122110010Smarkm+#ifdef OPENSSL_NO_TLSEXT 123110010Smarkm+ unsigned long Time; 124110010Smarkm+#endif 125110010Smarkm 126110010Smarkm if (s->state == SSL3_ST_SW_SRVR_HELLO_A) 127110010Smarkm { 128110010Smarkm buf=(unsigned char *)s->init_buf->data; 129110010Smarkm+#ifdef OPENSSL_NO_TLSEXT 130110010Smarkm p=s->s3->server_random; 131110010Smarkm+ /* Generate server_random if it was not needed previously */ 132110010Smarkm Time=(unsigned long)time(NULL); /* Time */ 133142429Snectar l2n(Time,p); 134110010Smarkm if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0) 135110655Snectar return -1; 136296465Sdelphij+#endif 137215698Ssimon /* Do the message type and length last */ 138215698Ssimon d=p= &(buf[4]); 139215698Ssimon 140215698SsimonIndex: openssl-0.9.8i/ssl/ssl_err.c 141110010Smarkm=================================================================== 142110010Smarkm--- openssl-0.9.8i.orig/ssl/ssl_err.c 2008-08-13 22:44:44.000000000 +0300 143110010Smarkm+++ openssl-0.9.8i/ssl/ssl_err.c 2008-11-23 20:33:43.000000000 +0200 144110010Smarkm@@ -253,6 +253,7 @@ 145110010Smarkm {ERR_FUNC(SSL_F_TLS1_ENC), "TLS1_ENC"}, 146110010Smarkm {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"}, 147110010Smarkm {ERR_FUNC(SSL_F_WRITE_PENDING), "WRITE_PENDING"}, 148110010Smarkm+{ERR_FUNC(SSL_F_SSL_SET_SESSION_TICKET_EXT), "SSL_set_session_ticket_ext"}, 149110010Smarkm {0,NULL} 150110010Smarkm }; 151110010Smarkm 152110010SmarkmIndex: openssl-0.9.8i/ssl/ssl.h 153110010Smarkm=================================================================== 154110010Smarkm--- openssl-0.9.8i.orig/ssl/ssl.h 2008-08-13 22:44:44.000000000 +0300 155160819Ssimon+++ openssl-0.9.8i/ssl/ssl.h 2008-11-23 20:35:41.000000000 +0200 156160819Ssimon@@ -344,6 +344,7 @@ 157279265Sdelphij * 'struct ssl_st *' function parameters used to prototype callbacks 158110010Smarkm * in SSL_CTX. */ 159110010Smarkm typedef struct ssl_st *ssl_crock_st; 160110010Smarkm+typedef struct tls_session_ticket_ext_st TLS_SESSION_TICKET_EXT; 161110010Smarkm 162110010Smarkm /* used to hold info on the particular ciphers used */ 163110010Smarkm typedef struct ssl_cipher_st 164110010Smarkm@@ -362,6 +363,9 @@ 165110010Smarkm 166279265Sdelphij DECLARE_STACK_OF(SSL_CIPHER) 167110010Smarkm 168110010Smarkm+typedef int (*tls_session_ticket_ext_cb_fn)(SSL *s, const unsigned char *data, int len, void *arg); 169110010Smarkm+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); 170110010Smarkm+ 171110010Smarkm /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */ 172110010Smarkm typedef struct ssl_method_st 173110010Smarkm { 174110010Smarkm@@ -1034,6 +1038,18 @@ 175110010Smarkm 176110010Smarkm /* RFC4507 session ticket expected to be received or sent */ 177110010Smarkm int tlsext_ticket_expected; 178110010Smarkm+ 179110010Smarkm+ /* TLS Session Ticket extension override */ 180279265Sdelphij+ TLS_SESSION_TICKET_EXT *tlsext_session_ticket; 181110010Smarkm+ 182110010Smarkm+ /* TLS Session Ticket extension callback */ 183110010Smarkm+ tls_session_ticket_ext_cb_fn tls_session_ticket_ext_cb; 184110010Smarkm+ void *tls_session_ticket_ext_cb_arg; 185110010Smarkm+ 186110010Smarkm+ /* TLS pre-shared secret session resumption */ 187110010Smarkm+ tls_session_secret_cb_fn tls_session_secret_cb; 188279265Sdelphij+ void *tls_session_secret_cb_arg; 189110010Smarkm+ 190110010Smarkm SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */ 191110010Smarkm #define session_ctx initial_ctx 192110010Smarkm #else 193110010Smarkm@@ -1632,6 +1648,15 @@ 194111151Snectar int SSL_COMP_add_compression_method(int id,void *cm); 195110010Smarkm #endif 196110010Smarkm 197110010Smarkm+/* TLS extensions functions */ 198110010Smarkm+int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len); 199279265Sdelphij+ 200110010Smarkm+int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb, 201110010Smarkm+ void *arg); 202110010Smarkm+ 203110010Smarkm+/* Pre-shared secret session resumption functions */ 204110010Smarkm+int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg); 205110010Smarkm+ 206279265Sdelphij /* BEGIN ERROR CODES */ 207110010Smarkm /* The following lines are auto generated by the script mkerr.pl. Any changes 208142429Snectar * made after this point may be overwritten when the script is next run. 209110010Smarkm@@ -1824,6 +1849,7 @@ 210110010Smarkm #define SSL_F_TLS1_ENC 210 211110010Smarkm #define SSL_F_TLS1_SETUP_KEY_BLOCK 211 212110010Smarkm #define SSL_F_WRITE_PENDING 212 213110010Smarkm+#define SSL_F_SSL_SET_SESSION_TICKET_EXT 213 214110010Smarkm 215110010Smarkm /* Reason codes. */ 216142429Snectar #define SSL_R_APP_DATA_IN_HANDSHAKE 100 217110010SmarkmIndex: openssl-0.9.8i/ssl/ssl_sess.c 218110010Smarkm=================================================================== 219110010Smarkm--- openssl-0.9.8i.orig/ssl/ssl_sess.c 2008-06-04 21:35:27.000000000 +0300 220142429Snectar+++ openssl-0.9.8i/ssl/ssl_sess.c 2008-11-23 20:32:24.000000000 +0200 221110010Smarkm@@ -707,6 +707,61 @@ 222110010Smarkm return(s->session_timeout); 223110010Smarkm } 224142429Snectar 225110010Smarkm+#ifndef OPENSSL_NO_TLSEXT 226110010Smarkm+int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len, 227110010Smarkm+ STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg) 228142429Snectar+ { 229110010Smarkm+ if (s == NULL) return(0); 230110010Smarkm+ s->tls_session_secret_cb = tls_session_secret_cb; 231110010Smarkm+ s->tls_session_secret_cb_arg = arg; 232160819Ssimon+ return(1); 233160819Ssimon+ } 234142429Snectar+ 235111151Snectar+int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb, 236111151Snectar+ void *arg) 237111151Snectar+ { 238111151Snectar+ if (s == NULL) return(0); 239111151Snectar+ s->tls_session_ticket_ext_cb = cb; 240279265Sdelphij+ s->tls_session_ticket_ext_cb_arg = arg; 241110010Smarkm+ return(1); 242110010Smarkm+ } 243279265Sdelphij+ 244142429Snectar+int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len) 245110010Smarkm+ { 246110010Smarkm+ if (s->version >= TLS1_VERSION) 247110010Smarkm+ { 248110010Smarkm+ if (s->tlsext_session_ticket) 249142429Snectar+ { 250110010Smarkm+ OPENSSL_free(s->tlsext_session_ticket); 251110010Smarkm+ s->tlsext_session_ticket = NULL; 252110010Smarkm+ } 253279265Sdelphij+ 254110010Smarkm+ s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len); 255142429Snectar+ if (!s->tlsext_session_ticket) 256110010Smarkm+ { 257110010Smarkm+ SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE); 258142429Snectar+ return 0; 259110010Smarkm+ } 260110010Smarkm+ 261110010Smarkm+ if (ext_data) 262142429Snectar+ { 263110010Smarkm+ s->tlsext_session_ticket->length = ext_len; 264110010Smarkm+ s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1; 265160819Ssimon+ memcpy(s->tlsext_session_ticket->data, ext_data, ext_len); 266160819Ssimon+ } 267110010Smarkm+ else 268110010Smarkm+ { 269110010Smarkm+ s->tlsext_session_ticket->length = 0; 270160819Ssimon+ s->tlsext_session_ticket->data = NULL; 271160819Ssimon+ } 272160819Ssimon+ 273279265Sdelphij+ return 1; 274279265Sdelphij+ } 275279265Sdelphij+ 276160819Ssimon+ return 0; 277160819Ssimon+ } 278194208Ssimon+#endif /* OPENSSL_NO_TLSEXT */ 279142429Snectar+ 280110010Smarkm typedef struct timeout_param_st 281110010Smarkm { 282142429Snectar SSL_CTX *ctx; 283110010SmarkmIndex: openssl-0.9.8i/ssl/t1_lib.c 284110010Smarkm=================================================================== 285142429Snectar--- openssl-0.9.8i.orig/ssl/t1_lib.c 2008-09-04 01:13:04.000000000 +0300 286110010Smarkm+++ openssl-0.9.8i/ssl/t1_lib.c 2008-11-23 20:31:20.000000000 +0200 287110010Smarkm@@ -106,6 +106,12 @@ 288110010Smarkm 289110010Smarkm void tls1_free(SSL *s) 290279265Sdelphij { 291142429Snectar+#ifndef OPENSSL_NO_TLSEXT 292110010Smarkm+ if (s->tlsext_session_ticket) 293110010Smarkm+ { 294142429Snectar+ OPENSSL_free(s->tlsext_session_ticket); 295110010Smarkm+ } 296110010Smarkm+#endif 297142429Snectar ssl3_free(s); 298110010Smarkm } 299110010Smarkm 300142429Snectar@@ -175,8 +181,23 @@ 301110010Smarkm int ticklen; 302110010Smarkm if (s->session && s->session->tlsext_tick) 303279265Sdelphij ticklen = s->session->tlsext_ticklen; 304279265Sdelphij+ else if (s->session && s->tlsext_session_ticket && 305279265Sdelphij+ s->tlsext_session_ticket->data) 306279265Sdelphij+ { 307142429Snectar+ ticklen = s->tlsext_session_ticket->length; 308110010Smarkm+ s->session->tlsext_tick = OPENSSL_malloc(ticklen); 309110010Smarkm+ if (!s->session->tlsext_tick) 310110010Smarkm+ return NULL; 311142429Snectar+ memcpy(s->session->tlsext_tick, 312110010Smarkm+ s->tlsext_session_ticket->data, 313110010Smarkm+ ticklen); 314279265Sdelphij+ s->session->tlsext_ticklen = ticklen; 315110010Smarkm+ } 316110010Smarkm else 317110010Smarkm ticklen = 0; 318110010Smarkm+ if (ticklen == 0 && s->tlsext_session_ticket && 319110010Smarkm+ s->tlsext_session_ticket->data == NULL) 320110010Smarkm+ goto skip_ext; 321110010Smarkm /* Check for enough room 2 for extension type, 2 for len 322110010Smarkm * rest for ticket 323110010Smarkm */ 324110010Smarkm@@ -190,6 +211,7 @@ 325110010Smarkm ret += ticklen; 326110010Smarkm } 327279265Sdelphij } 328110010Smarkm+ skip_ext: 329110010Smarkm 330110010Smarkm if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) 331110010Smarkm { 332110010Smarkm@@ -407,6 +429,15 @@ 333110010Smarkm } 334110010Smarkm 335110010Smarkm } 336142429Snectar+ else if (type == TLSEXT_TYPE_session_ticket) 337110010Smarkm+ { 338110010Smarkm+ if (s->tls_session_ticket_ext_cb && 339110010Smarkm+ !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg)) 340110010Smarkm+ { 341110010Smarkm+ *al = TLS1_AD_INTERNAL_ERROR; 342110010Smarkm+ return 0; 343142429Snectar+ } 344110010Smarkm+ } 345110010Smarkm else if (type == TLSEXT_TYPE_status_request 346110010Smarkm && s->ctx->tlsext_status_cb) 347142429Snectar { 348110010Smarkm@@ -553,6 +584,12 @@ 349110010Smarkm } 350142429Snectar else if (type == TLSEXT_TYPE_session_ticket) 351110010Smarkm { 352110010Smarkm+ if (s->tls_session_ticket_ext_cb && 353142429Snectar+ !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg)) 354110010Smarkm+ { 355110010Smarkm+ *al = TLS1_AD_INTERNAL_ERROR; 356142429Snectar+ return 0; 357110010Smarkm+ } 358110010Smarkm if ((SSL_get_options(s) & SSL_OP_NO_TICKET) 359110010Smarkm || (size > 0)) 360110010Smarkm { 361110010Smarkm@@ -776,6 +813,15 @@ 362142429Snectar s->tlsext_ticket_expected = 1; 363110010Smarkm return 0; /* Cache miss */ 364110010Smarkm } 365110010Smarkm+ if (s->tls_session_secret_cb) 366142429Snectar+ { 367110010Smarkm+ /* Indicate cache miss here and instead of 368110010Smarkm+ * generating the session from ticket now, 369279265Sdelphij+ * trigger abbreviated handshake based on 370279265Sdelphij+ * external mechanism to calculate the master 371279265Sdelphij+ * secret later. */ 372110010Smarkm+ return 0; 373110010Smarkm+ } 374279265Sdelphij return tls_decrypt_ticket(s, p, size, session_id, len, 375142429Snectar ret); 376110010Smarkm } 377110010SmarkmIndex: openssl-0.9.8i/ssl/tls1.h 378215698Ssimon=================================================================== 379110010Smarkm--- openssl-0.9.8i.orig/ssl/tls1.h 2008-04-30 19:11:33.000000000 +0300 380110010Smarkm+++ openssl-0.9.8i/ssl/tls1.h 2008-11-23 20:22:38.000000000 +0200 381110010Smarkm@@ -398,6 +398,13 @@ 382110010Smarkm #define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74" /*master secret*/ 383110010Smarkm #endif 384110010Smarkm 385110010Smarkm+/* TLS extension struct */ 386110010Smarkm+struct tls_session_ticket_ext_st 387110010Smarkm+ { 388110010Smarkm+ unsigned short length; 389110010Smarkm+ void *data; 390279265Sdelphij+ }; 391279265Sdelphij+ 392279265Sdelphij #ifdef __cplusplus 393279265Sdelphij } 394142429Snectar #endif 395110010SmarkmIndex: openssl-0.9.8i/util/ssleay.num 396110010Smarkm=================================================================== 397110010Smarkm--- openssl-0.9.8i.orig/util/ssleay.num 2008-06-05 13:57:21.000000000 +0300 398110010Smarkm+++ openssl-0.9.8i/util/ssleay.num 2008-11-23 20:22:05.000000000 +0200 399110010Smarkm@@ -242,3 +242,5 @@ 400142429Snectar SSL_get_servername 291 EXIST::FUNCTION:TLSEXT 401110010Smarkm SSL_get_servername_type 292 EXIST::FUNCTION:TLSEXT 402110010Smarkm SSL_CTX_set_client_cert_engine 293 EXIST::FUNCTION:ENGINE 403110010Smarkm+SSL_set_session_ticket_ext 306 EXIST::FUNCTION:TLSEXT 404142429Snectar+SSL_set_session_secret_cb 307 EXIST::FUNCTION:TLSEXT 405110010Smarkm