1d20b5f
diff -up openssl-0.9.8j/ssl/t1_lib.c.eap-fast openssl-0.9.8j/ssl/t1_lib.c
1d20b5f
--- openssl-0.9.8j/ssl/t1_lib.c.eap-fast	2009-01-14 16:39:41.000000000 +0100
1d20b5f
+++ openssl-0.9.8j/ssl/t1_lib.c	2009-01-14 21:35:38.000000000 +0100
1d20b5f
@@ -106,6 +106,12 @@ int tls1_new(SSL *s)
1d20b5f
 
1d20b5f
 void tls1_free(SSL *s)
1d20b5f
 	{
1d20b5f
+#ifndef OPENSSL_NO_TLSEXT
1d20b5f
+	if (s && s->tlsext_session_ticket)
1d20b5f
+		{
1d20b5f
+		OPENSSL_free(s->tlsext_session_ticket);
1d20b5f
+		}
1d20b5f
+#endif /* OPENSSL_NO_TLSEXT */
1d20b5f
 	ssl3_free(s);
1d20b5f
 	}
1d20b5f
 
1d20b5f
@@ -180,8 +186,23 @@ unsigned char *ssl_add_clienthello_tlsex
1d20b5f
 		int ticklen;
1d20b5f
 		if (s->session && s->session->tlsext_tick)
1d20b5f
 			ticklen = s->session->tlsext_ticklen;
1d20b5f
+		else if (s->session && s->tlsext_session_ticket &&
1d20b5f
+			 s->tlsext_session_ticket->data)
1d20b5f
+			{
1d20b5f
+			ticklen = s->tlsext_session_ticket->length;
1d20b5f
+			s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1d20b5f
+			if (!s->session->tlsext_tick)
1d20b5f
+				return NULL;
1d20b5f
+			memcpy(s->session->tlsext_tick,
1d20b5f
+			       s->tlsext_session_ticket->data,
1d20b5f
+			       ticklen);
1d20b5f
+			s->session->tlsext_ticklen = ticklen;
1d20b5f
+			}
1d20b5f
 		else
1d20b5f
 			ticklen = 0;
1d20b5f
+		if (ticklen == 0 && s->tlsext_session_ticket &&
1d20b5f
+		    s->tlsext_session_ticket->data == NULL)
1d20b5f
+			goto skip_ext;
1d20b5f
 		/* Check for enough room 2 for extension type, 2 for len
1d20b5f
  		 * rest for ticket
1d20b5f
   		 */
1d20b5f
@@ -195,6 +216,7 @@ unsigned char *ssl_add_clienthello_tlsex
1d20b5f
 			ret += ticklen;
1d20b5f
 			}
1d20b5f
 		}
1d20b5f
+		skip_ext:
1d20b5f
 
1d20b5f
 	if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1d20b5f
 		{
1d20b5f
@@ -417,6 +439,15 @@ int ssl_parse_clienthello_tlsext(SSL *s,
1d20b5f
 				}
1d20b5f
 
1d20b5f
 			}
1d20b5f
+		else if (type == TLSEXT_TYPE_session_ticket) 
1d20b5f
+			{ 
1d20b5f
+			if (s->tls_session_ticket_ext_cb && 
1d20b5f
+			    !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg)) 
1d20b5f
+				{ 
1d20b5f
+				*al = TLS1_AD_INTERNAL_ERROR; 
1d20b5f
+				return 0; 
1d20b5f
+				} 
1d20b5f
+			} 
1d20b5f
 		else if (type == TLSEXT_TYPE_status_request
1d20b5f
 						&& s->ctx->tlsext_status_cb)
1d20b5f
 			{
1d20b5f
@@ -563,6 +594,12 @@ int ssl_parse_serverhello_tlsext(SSL *s,
1d20b5f
 			}
1d20b5f
 		else if (type == TLSEXT_TYPE_session_ticket)
1d20b5f
 			{
1d20b5f
+			if (s->tls_session_ticket_ext_cb &&
1d20b5f
+			    !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
1d20b5f
+				{
1d20b5f
+				*al = TLS1_AD_INTERNAL_ERROR;
1d20b5f
+				return 0;
1d20b5f
+				}
1d20b5f
 			if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
1d20b5f
 				|| (size > 0))
1d20b5f
 				{
1d20b5f
@@ -786,6 +823,15 @@ int tls1_process_ticket(SSL *s, unsigned
1d20b5f
 				s->tlsext_ticket_expected = 1;
1d20b5f
 				return 0;	/* Cache miss */
1d20b5f
 				}
1d20b5f
+			if (s->tls_session_secret_cb)
1d20b5f
+				{
1d20b5f
+				/* Indicate cache miss here and instead of
1d20b5f
+				 * generating the session from ticket now,
1d20b5f
+				 * trigger abbreviated handshake based on
1d20b5f
+				 * external mechanism to calculate the master
1d20b5f
+				 * secret later. */
1d20b5f
+				return 0;
1d20b5f
+				}
1d20b5f
 			return tls_decrypt_ticket(s, p, size, session_id, len,
1d20b5f
 									ret);
1d20b5f
 			}
1d20b5f
diff -up openssl-0.9.8j/ssl/s3_clnt.c.eap-fast openssl-0.9.8j/ssl/s3_clnt.c
1d20b5f
--- openssl-0.9.8j/ssl/s3_clnt.c.eap-fast	2009-01-07 11:48:23.000000000 +0100
1d20b5f
+++ openssl-0.9.8j/ssl/s3_clnt.c	2009-01-14 21:13:47.000000000 +0100
1d20b5f
@@ -759,6 +759,23 @@ int ssl3_get_server_hello(SSL *s)
1d20b5f
 		goto f_err;
1d20b5f
 		}
1d20b5f
 
1d20b5f
+#ifndef OPENSSL_NO_TLSEXT
1d20b5f
+	/* check if we want to resume the session based on external pre-shared secret */
1d20b5f
+	if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
1d20b5f
+		{
1d20b5f
+		SSL_CIPHER *pref_cipher=NULL;
1d20b5f
+		s->session->master_key_length=sizeof(s->session->master_key);
1d20b5f
+		if (s->tls_session_secret_cb(s, s->session->master_key,
1d20b5f
+					     &s->session->master_key_length,
1d20b5f
+					     NULL, &pref_cipher,
1d20b5f
+					     s->tls_session_secret_cb_arg))
1d20b5f
+			{
1d20b5f
+			s->session->cipher = pref_cipher ?
1d20b5f
+				pref_cipher : ssl_get_cipher_by_char(s, p+j);
1d20b5f
+			}
1d20b5f
+		}
1d20b5f
+#endif /* OPENSSL_NO_TLSEXT */
1d20b5f
+
1d20b5f
 	if (j != 0 && j == s->session->session_id_length
1d20b5f
 	    && memcmp(p,s->session->session_id,j) == 0)
1d20b5f
 	    {
1d20b5f
@@ -2701,11 +2718,8 @@ static int ssl3_check_finished(SSL *s)
1d20b5f
 	{
1d20b5f
 	int ok;
1d20b5f
 	long n;
1d20b5f
-	/* If we have no ticket or session ID is non-zero length (a match of
1d20b5f
-	 * a non-zero session length would never reach here) it cannot be a
1d20b5f
-	 * resumed session.
1d20b5f
-	 */
1d20b5f
-	if (!s->session->tlsext_tick || s->session->session_id_length)
1d20b5f
+	/* If we have no ticket it cannot be a resumed session. */
1d20b5f
+	if (!s->session->tlsext_tick)
1d20b5f
 		return 1;
1d20b5f
 	/* this function is called when we really expect a Certificate
1d20b5f
 	 * message, so permit appropriate message length */
1d20b5f
diff -up openssl-0.9.8j/ssl/ssl_sess.c.eap-fast openssl-0.9.8j/ssl/ssl_sess.c
1d20b5f
--- openssl-0.9.8j/ssl/ssl_sess.c.eap-fast	2008-06-04 20:35:27.000000000 +0200
1d20b5f
+++ openssl-0.9.8j/ssl/ssl_sess.c	2009-01-14 21:13:47.000000000 +0100
1d20b5f
@@ -707,6 +707,61 @@ long SSL_CTX_get_timeout(const SSL_CTX *
1d20b5f
 	return(s->session_timeout);
1d20b5f
 	}
1d20b5f
 
1d20b5f
+#ifndef OPENSSL_NO_TLSEXT
1d20b5f
+int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
1d20b5f
+	STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
1d20b5f
+	{
1d20b5f
+	if (s == NULL) return(0);
1d20b5f
+	s->tls_session_secret_cb = tls_session_secret_cb;
1d20b5f
+	s->tls_session_secret_cb_arg = arg;
1d20b5f
+	return(1);
1d20b5f
+	}
1d20b5f
+
1d20b5f
+int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1d20b5f
+				  void *arg)
1d20b5f
+	{
1d20b5f
+	if (s == NULL) return(0);
1d20b5f
+	s->tls_session_ticket_ext_cb = cb;
1d20b5f
+	s->tls_session_ticket_ext_cb_arg = arg;
1d20b5f
+	return(1);
1d20b5f
+	}
1d20b5f
+
1d20b5f
+int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
1d20b5f
+	{
1d20b5f
+	if (s->version >= TLS1_VERSION)
1d20b5f
+		{
1d20b5f
+		if (s->tlsext_session_ticket)
1d20b5f
+			{
1d20b5f
+			OPENSSL_free(s->tlsext_session_ticket);
1d20b5f
+			s->tlsext_session_ticket = NULL;
1d20b5f
+			}
1d20b5f
+
1d20b5f
+		s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
1d20b5f
+		if (!s->tlsext_session_ticket)
1d20b5f
+			{
1d20b5f
+			SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
1d20b5f
+			return 0;
1d20b5f
+			}
1d20b5f
+
1d20b5f
+		if (ext_data)
1d20b5f
+			{
1d20b5f
+			s->tlsext_session_ticket->length = ext_len;
1d20b5f
+			s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
1d20b5f
+			memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
1d20b5f
+			}
1d20b5f
+		else
1d20b5f
+			{
1d20b5f
+			s->tlsext_session_ticket->length = 0;
1d20b5f
+			s->tlsext_session_ticket->data = NULL;
1d20b5f
+			}
1d20b5f
+
1d20b5f
+		return 1;
1d20b5f
+		}
1d20b5f
+
1d20b5f
+	return 0;
1d20b5f
+	}
1d20b5f
+#endif /* OPENSSL_NO_TLSEXT */
1d20b5f
+
1d20b5f
 typedef struct timeout_param_st
1d20b5f
 	{
1d20b5f
 	SSL_CTX *ctx;
1d20b5f
diff -up openssl-0.9.8j/ssl/s3_srvr.c.eap-fast openssl-0.9.8j/ssl/s3_srvr.c
1d20b5f
--- openssl-0.9.8j/ssl/s3_srvr.c.eap-fast	2009-01-07 11:48:23.000000000 +0100
1d20b5f
+++ openssl-0.9.8j/ssl/s3_srvr.c	2009-01-14 21:22:37.000000000 +0100
1d20b5f
@@ -965,6 +965,59 @@ int ssl3_get_client_hello(SSL *s)
1d20b5f
 			SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
1d20b5f
 			goto err;
1d20b5f
 		}
1d20b5f
+
1d20b5f
+	/* Check if we want to use external pre-shared secret for this
1d20b5f
+	 * handshake for not reused session only. We need to generate
1d20b5f
+	 * server_random before calling tls_session_secret_cb in order to allow
1d20b5f
+	 * SessionTicket processing to use it in key derivation. */
1d20b5f
+	{
1d20b5f
+		unsigned long Time;
1d20b5f
+		unsigned char *pos;
1d20b5f
+		Time=(unsigned long)time(NULL);			/* Time */
1d20b5f
+		pos=s->s3->server_random;
1d20b5f
+		l2n(Time,pos);
1d20b5f
+		if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
1d20b5f
+			{
1d20b5f
+			al=SSL_AD_INTERNAL_ERROR;
1d20b5f
+			goto f_err;
1d20b5f
+			}
1d20b5f
+	}
1d20b5f
+
1d20b5f
+	if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
1d20b5f
+		{
1d20b5f
+		SSL_CIPHER *pref_cipher=NULL;
1d20b5f
+
1d20b5f
+		s->session->master_key_length=sizeof(s->session->master_key);
1d20b5f
+		if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
1d20b5f
+			ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
1d20b5f
+			{
1d20b5f
+			s->hit=1;
1d20b5f
+			s->session->ciphers=ciphers;
1d20b5f
+			s->session->verify_result=X509_V_OK;
1d20b5f
+
1d20b5f
+			ciphers=NULL;
1d20b5f
+
1d20b5f
+			/* check if some cipher was preferred by call back */
1d20b5f
+			pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1d20b5f
+			if (pref_cipher == NULL)
1d20b5f
+				{
1d20b5f
+				al=SSL_AD_HANDSHAKE_FAILURE;
1d20b5f
+				SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
1d20b5f
+				goto f_err;
1d20b5f
+				}
1d20b5f
+
1d20b5f
+			s->session->cipher=pref_cipher;
1d20b5f
+
1d20b5f
+			if (s->cipher_list)
1d20b5f
+				sk_SSL_CIPHER_free(s->cipher_list);
1d20b5f
+
1d20b5f
+			if (s->cipher_list_by_id)
1d20b5f
+				sk_SSL_CIPHER_free(s->cipher_list_by_id);
1d20b5f
+
1d20b5f
+			s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1d20b5f
+			s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1d20b5f
+			}
1d20b5f
+		}
1d20b5f
 #endif
1d20b5f
 	/* Worst case, we will use the NULL compression, but if we have other
1d20b5f
 	 * options, we will now look for them.  We have i-1 compression
1d20b5f
@@ -1103,16 +1156,22 @@ int ssl3_send_server_hello(SSL *s)
1d20b5f
 	unsigned char *buf;
1d20b5f
 	unsigned char *p,*d;
1d20b5f
 	int i,sl;
1d20b5f
-	unsigned long l,Time;
1d20b5f
+	unsigned long l;
1d20b5f
+#ifdef OPENSSL_NO_TLSEXT
1d20b5f
+	unsigned long Time;
1d20b5f
+#endif
1d20b5f
 
1d20b5f
 	if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
1d20b5f
 		{
1d20b5f
 		buf=(unsigned char *)s->init_buf->data;
1d20b5f
+#ifdef OPENSSL_NO_TLSEXT
1d20b5f
 		p=s->s3->server_random;
1d20b5f
+		/* Generate server_random if it was not needed previously */
1d20b5f
 		Time=(unsigned long)time(NULL);			/* Time */
1d20b5f
 		l2n(Time,p);
1d20b5f
 		if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
1d20b5f
 			return -1;
1d20b5f
+#endif
1d20b5f
 		/* Do the message type and length last */
1d20b5f
 		d=p= &(buf[4]);
1d20b5f
 
1d20b5f
diff -up openssl-0.9.8j/ssl/tls1.h.eap-fast openssl-0.9.8j/ssl/tls1.h
1d20b5f
--- openssl-0.9.8j/ssl/tls1.h.eap-fast	2009-01-14 16:39:41.000000000 +0100
1d20b5f
+++ openssl-0.9.8j/ssl/tls1.h	2009-01-14 21:13:47.000000000 +0100
1d20b5f
@@ -398,6 +398,13 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_T
1d20b5f
 #define TLS_MD_MASTER_SECRET_CONST    "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"  /*master secret*/
1d20b5f
 #endif
1d20b5f
 
1d20b5f
+/* TLS Session Ticket extension struct */
1d20b5f
+struct tls_session_ticket_ext_st
1d20b5f
+	{
1d20b5f
+	unsigned short length;
1d20b5f
+	void *data;
1d20b5f
+	};
1d20b5f
+
1d20b5f
 #ifdef  __cplusplus
1d20b5f
 }
1d20b5f
 #endif
1d20b5f
diff -up openssl-0.9.8j/ssl/ssl_err.c.eap-fast openssl-0.9.8j/ssl/ssl_err.c
1d20b5f
--- openssl-0.9.8j/ssl/ssl_err.c.eap-fast	2008-08-13 21:44:44.000000000 +0200
1d20b5f
+++ openssl-0.9.8j/ssl/ssl_err.c	2009-01-14 21:13:47.000000000 +0100
1d20b5f
@@ -253,6 +253,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
1d20b5f
 {ERR_FUNC(SSL_F_TLS1_ENC),	"TLS1_ENC"},
1d20b5f
 {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK),	"TLS1_SETUP_KEY_BLOCK"},
1d20b5f
 {ERR_FUNC(SSL_F_WRITE_PENDING),	"WRITE_PENDING"},
1d20b5f
+{ERR_FUNC(SSL_F_SSL_SET_SESSION_TICKET_EXT), "SSL_set_session_ticket_ext"},
1d20b5f
 {0,NULL}
1d20b5f
 	};
1d20b5f
 
1d20b5f
diff -up openssl-0.9.8j/ssl/ssl.h.eap-fast openssl-0.9.8j/ssl/ssl.h
1d20b5f
--- openssl-0.9.8j/ssl/ssl.h.eap-fast	2009-01-14 16:39:41.000000000 +0100
1d20b5f
+++ openssl-0.9.8j/ssl/ssl.h	2009-01-14 21:26:45.000000000 +0100
1d20b5f
@@ -344,6 +344,7 @@ extern "C" {
1d20b5f
  * 'struct ssl_st *' function parameters used to prototype callbacks
1d20b5f
  * in SSL_CTX. */
1d20b5f
 typedef struct ssl_st *ssl_crock_st;
1d20b5f
+typedef struct tls_session_ticket_ext_st TLS_SESSION_TICKET_EXT;
1d20b5f
 
1d20b5f
 /* used to hold info on the particular ciphers used */
1d20b5f
 typedef struct ssl_cipher_st
1d20b5f
@@ -362,6 +363,9 @@ typedef struct ssl_cipher_st
1d20b5f
 
1d20b5f
 DECLARE_STACK_OF(SSL_CIPHER)
1d20b5f
 
1d20b5f
+typedef int (*tls_session_ticket_ext_cb_fn)(SSL *s, const unsigned char *data, int len, void *arg);
1d20b5f
+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);
1d20b5f
+
1d20b5f
 /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
1d20b5f
 typedef struct ssl_method_st
1d20b5f
 	{
1d20b5f
@@ -1034,6 +1038,18 @@ struct ssl_st
1d20b5f
 
1d20b5f
 	/* RFC4507 session ticket expected to be received or sent */
1d20b5f
 	int tlsext_ticket_expected;
1d20b5f
+
1d20b5f
+	/* TLS Session Ticket extension override */ 
1d20b5f
+	TLS_SESSION_TICKET_EXT *tlsext_session_ticket; 
1d20b5f
+
1d20b5f
+	/* TLS Session Ticket extension callback */ 
1d20b5f
+	tls_session_ticket_ext_cb_fn tls_session_ticket_ext_cb; 
1d20b5f
+	void *tls_session_ticket_ext_cb_arg; 
1d20b5f
+
1d20b5f
+	/* TLS pre-shared secret session resumption */ 
1d20b5f
+	tls_session_secret_cb_fn tls_session_secret_cb; 
1d20b5f
+	void *tls_session_secret_cb_arg; 
1d20b5f
+
1d20b5f
 	SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
1d20b5f
 #define session_ctx initial_ctx
1d20b5f
 #else
1d20b5f
@@ -1624,6 +1640,15 @@ void *SSL_COMP_get_compression_methods(v
1d20b5f
 int SSL_COMP_add_compression_method(int id,void *cm);
1d20b5f
 #endif
1d20b5f
 
1d20b5f
+/* TLS extensions functions */
1d20b5f
+int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len);
1d20b5f
+
1d20b5f
+int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
1d20b5f
+				  void *arg);
1d20b5f
+
1d20b5f
+/* Pre-shared secret session resumption functions */
1d20b5f
+int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
1d20b5f
+
1d20b5f
 /* BEGIN ERROR CODES */
1d20b5f
 /* The following lines are auto generated by the script mkerr.pl. Any changes
1d20b5f
  * made after this point may be overwritten when the script is next run.
1d20b5f
@@ -1816,6 +1841,7 @@ void ERR_load_SSL_strings(void);
1d20b5f
 #define SSL_F_TLS1_ENC					 210
1d20b5f
 #define SSL_F_TLS1_SETUP_KEY_BLOCK			 211
1d20b5f
 #define SSL_F_WRITE_PENDING				 212
1d20b5f
+#define SSL_F_SSL_SET_SESSION_TICKET_EXT		 213
1d20b5f
 
1d20b5f
 /* Reason codes. */
1d20b5f
 #define SSL_R_APP_DATA_IN_HANDSHAKE			 100