PR: 1574
authorDr. Stephen Henson <steve@openssl.org>
Sat, 15 Nov 2008 17:18:12 +0000 (17:18 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Sat, 15 Nov 2008 17:18:12 +0000 (17:18 +0000)
Submitted by: Jouni Malinen <j@w1.fi>
Approved by: steve@openssl.org

Ticket override support for EAP-FAST.

CHANGES
ssl/s3_clnt.c
ssl/s3_srvr.c
ssl/ssl.h
ssl/ssl_err.c
ssl/ssl_sess.c
ssl/t1_lib.c
ssl/tls1.h
util/ssleay.num

diff --git a/CHANGES b/CHANGES
index dc95e026efd25b26c47a967be25f6310e6b49903..74da83b5326bca18378298fe6576b0fe36aecfbb 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,9 @@
 
  Changes between 0.9.8j and 0.9.9  [xx XXX xxxx]
 
+  *) Add session ticket override functionality for use by EAP-FAST.
+     [Jouni Malinen <j@w1.fi>]
+
   *) Modify HMAC functions to return a value. Since these can be implemented
      in an ENGINE errors can occur.
      [Steve Henson]
index aae133429cba14997c7e5f57e7c827008d48d7b3..5cea73ca1abcbedaf7c06d5482d1e7dd66af734c 100644 (file)
@@ -788,6 +788,23 @@ int ssl3_get_server_hello(SSL *s)
                goto f_err;
                }
 
+#ifndef OPENSSL_NO_TLSEXT
+       /* check if we want to resume the session based on external pre-shared secret */
+       if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
+               {
+               SSL_CIPHER *pref_cipher=NULL;
+               s->session->master_key_length=sizeof(s->session->master_key);
+               if (s->tls_session_secret_cb(s, s->session->master_key,
+                                            &s->session->master_key_length,
+                                            NULL, &pref_cipher,
+                                            s->tls_session_secret_cb_arg))
+                       {
+                       s->session->cipher = pref_cipher ?
+                               pref_cipher : ssl_get_cipher_by_char(s, p+j);
+                       }
+               }
+#endif /* OPENSSL_NO_TLSEXT */
+
        if (j != 0 && j == s->session->session_id_length
            && memcmp(p,s->session->session_id,j) == 0)
            {
@@ -2927,11 +2944,8 @@ static int ssl3_check_finished(SSL *s)
        {
        int ok;
        long n;
-       /* If we have no ticket or session ID is non-zero length (a match of
-        * a non-zero session length would never reach here) it cannot be a
-        * resumed session.
-        */
-       if (!s->session->tlsext_tick || s->session->session_id_length)
+       /* If we have no ticket it cannot be a resumed session. */
+       if (!s->session->tlsext_tick)
                return 1;
        /* this function is called when we really expect a Certificate
         * message, so permit appropriate message length */
index b124a8559c5e2217a28c2bf58e77855aa248253e..876d0caf38afd52f8275f310ef1ec3e415999274 100644 (file)
@@ -1010,6 +1010,59 @@ int ssl3_get_client_hello(SSL *s)
                        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT);
                        goto err;
                }
+
+       /* Check if we want to use external pre-shared secret for this
+        * handshake for not reused session only. We need to generate
+        * server_random before calling tls_session_secret_cb in order to allow
+        * SessionTicket processing to use it in key derivation. */
+       {
+               unsigned long Time;
+               unsigned char *pos;
+               Time=(unsigned long)time(NULL);                 /* Time */
+               pos=s->s3->server_random;
+               l2n(Time,pos);
+               if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0)
+                       {
+                       al=SSL_AD_INTERNAL_ERROR;
+                       goto f_err;
+                       }
+       }
+
+       if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
+               {
+               SSL_CIPHER *pref_cipher=NULL;
+
+               s->session->master_key_length=sizeof(s->session->master_key);
+               if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
+                       ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
+                       {
+                       s->hit=1;
+                       s->session->ciphers=ciphers;
+                       s->session->verify_result=X509_V_OK;
+
+                       ciphers=NULL;
+
+                       /* check if some cipher was preferred by call back */
+                       pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
+                       if (pref_cipher == NULL)
+                               {
+                               al=SSL_AD_HANDSHAKE_FAILURE;
+                               SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
+                               goto f_err;
+                               }
+
+                       s->session->cipher=pref_cipher;
+
+                       if (s->cipher_list)
+                               sk_SSL_CIPHER_free(s->cipher_list);
+
+                       if (s->cipher_list_by_id)
+                               sk_SSL_CIPHER_free(s->cipher_list_by_id);
+
+                       s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
+                       s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
+                       }
+               }
 #endif
 
        /* Worst case, we will use the NULL compression, but if we have other
@@ -1134,16 +1187,22 @@ int ssl3_send_server_hello(SSL *s)
        unsigned char *buf;
        unsigned char *p,*d;
        int i,sl;
-       unsigned long l,Time;
+       unsigned long l;
+#ifdef OPENSSL_NO_TLSEXT
+       unsigned long Time;
+#endif
 
        if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
                {
                buf=(unsigned char *)s->init_buf->data;
+#ifdef OPENSSL_NO_TLSEXT
                p=s->s3->server_random;
+               /* Generate server_random if it was not needed previously */
                Time=(unsigned long)time(NULL);                 /* Time */
                l2n(Time,p);
                if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
                        return -1;
+#endif
                /* Do the message type and length last */
                d=p= &(buf[4]);
 
index f23f24b73706a37af7c462749032754006d69db6..1029e823aace69fba8a6a0a2c3d2a4cce909180e 100644 (file)
--- a/ssl/ssl.h
+++ b/ssl/ssl.h
@@ -355,6 +355,7 @@ extern "C" {
  * 'struct ssl_st *' function parameters used to prototype callbacks
  * in SSL_CTX. */
 typedef struct ssl_st *ssl_crock_st;
+typedef struct tls_session_ticket_ext_st TLS_SESSION_TICKET_EXT;
 
 /* used to hold info on the particular ciphers used */
 typedef struct ssl_cipher_st
@@ -378,6 +379,9 @@ typedef struct ssl_cipher_st
 
 DECLARE_STACK_OF(SSL_CIPHER)
 
+typedef int (*tls_session_ticket_ext_cb_fn)(SSL *s, const unsigned char *data, int len, void *arg);
+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);
+
 /* Used to hold functions for SSLv2 or SSLv3/TLSv1 functions */
 typedef struct ssl_method_st
        {
@@ -1145,6 +1149,17 @@ struct ssl_st
        void *tlsext_opaque_prf_input;
        size_t tlsext_opaque_prf_input_len;
 
+       /* TLS Session Ticket extension override */
+       TLS_SESSION_TICKET_EXT *tlsext_session_ticket;
+
+       /* TLS Session Ticket extension callback */
+       tls_session_ticket_ext_cb_fn tls_session_ticket_ext_cb;
+       void *tls_session_ticket_ext_cb_arg;
+
+       /* TLS pre-shared secret session resumption */
+       tls_session_secret_cb_fn tls_session_secret_cb;
+       void *tls_session_secret_cb_arg;
+
        SSL_CTX * initial_ctx; /* initial ctx, used to store sessions */
 #define session_ctx initial_ctx
 #else
@@ -1746,6 +1761,15 @@ void *SSL_COMP_get_compression_methods(void);
 int SSL_COMP_add_compression_method(int id,void *cm);
 #endif
 
+/* TLS extensions functions */
+int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len);
+
+int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
+                                 void *arg);
+
+/* Pre-shared secret session resumption functions */
+int SSL_set_session_secret_cb(SSL *s, tls_session_secret_cb_fn tls_session_secret_cb, void *arg);
+
 /* BEGIN ERROR CODES */
 /* The following lines are auto generated by the script mkerr.pl. Any changes
  * made after this point may be overwritten when the script is next run.
@@ -1948,6 +1972,7 @@ void ERR_load_SSL_strings(void);
 #define SSL_F_TLS1_PRF                                  284
 #define SSL_F_TLS1_SETUP_KEY_BLOCK                      211
 #define SSL_F_WRITE_PENDING                             212
+#define SSL_F_SSL_SET_SESSION_TICKET_EXT                213
 
 /* Reason codes. */
 #define SSL_R_APP_DATA_IN_HANDSHAKE                     100
index e8bfd830f94322f2e23bd837a6e5afcb5e6d29c8..817b67e2d298be8dbd42f5ca97a874567b987b41 100644 (file)
@@ -263,6 +263,7 @@ static ERR_STRING_DATA SSL_str_functs[]=
 {ERR_FUNC(SSL_F_TLS1_PRF),     "tls1_prf"},
 {ERR_FUNC(SSL_F_TLS1_SETUP_KEY_BLOCK), "TLS1_SETUP_KEY_BLOCK"},
 {ERR_FUNC(SSL_F_WRITE_PENDING),        "WRITE_PENDING"},
+{ERR_FUNC(SSL_F_SSL_SET_SESSION_TICKET_EXT), "SSL_set_session_ticket_ext"},
 {0,NULL}
        };
 
index 1378f7600e8b6d31b8c54442bf82ee2e0381e1d4..4c72f18a7538b1c17e13c829bbb8c271eec3cee1 100644 (file)
@@ -834,6 +834,61 @@ long SSL_CTX_get_timeout(const SSL_CTX *s)
        return(s->session_timeout);
        }
 
+#ifndef OPENSSL_NO_TLSEXT
+int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
+       STACK_OF(SSL_CIPHER) *peer_ciphers, SSL_CIPHER **cipher, void *arg), void *arg)
+       {
+       if (s == NULL) return(0);
+       s->tls_session_secret_cb = tls_session_secret_cb;
+       s->tls_session_secret_cb_arg = arg;
+       return(1);
+       }
+
+int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
+                                 void *arg)
+       {
+       if (s == NULL) return(0);
+       s->tls_session_ticket_ext_cb = cb;
+       s->tls_session_ticket_ext_cb_arg = arg;
+       return(1);
+       }
+
+int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
+       {
+       if (s->version >= TLS1_VERSION)
+               {
+               if (s->tlsext_session_ticket)
+                       {
+                       OPENSSL_free(s->tlsext_session_ticket);
+                       s->tlsext_session_ticket = NULL;
+                       }
+
+               s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
+               if (!s->tlsext_session_ticket)
+                       {
+                       SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
+                       return 0;
+                       }
+
+               if (ext_data)
+                       {
+                       s->tlsext_session_ticket->length = ext_len;
+                       s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
+                       memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
+                       }
+               else
+                       {
+                       s->tlsext_session_ticket->length = 0;
+                       s->tlsext_session_ticket->data = NULL;
+                       }
+
+               return 1;
+               }
+
+       return 0;
+       }
+#endif /* OPENSSL_NO_TLSEXT */
+
 typedef struct timeout_param_st
        {
        SSL_CTX *ctx;
index dc0396cfaab26fe317f73b36799ae8f1add9181f..e6ba33d85b1b847a6ba90670e8865b0fadc3c7e5 100644 (file)
@@ -154,6 +154,12 @@ int tls1_new(SSL *s)
 
 void tls1_free(SSL *s)
        {
+#ifndef OPENSSL_NO_TLSEXT
+       if (s->tlsext_session_ticket)
+               {
+               OPENSSL_free(s->tlsext_session_ticket);
+               }
+#endif /* OPENSSL_NO_TLSEXT */
        ssl3_free(s);
        }
 
@@ -357,8 +363,23 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned cha
                int ticklen;
                if (s->session && s->session->tlsext_tick)
                        ticklen = s->session->tlsext_ticklen;
+               else if (s->session && s->tlsext_session_ticket &&
+                        s->tlsext_session_ticket->data)
+                       {
+                       ticklen = s->tlsext_session_ticket->length;
+                       s->session->tlsext_tick = OPENSSL_malloc(ticklen);
+                       if (!s->session->tlsext_tick)
+                               return NULL;
+                       memcpy(s->session->tlsext_tick,
+                              s->tlsext_session_ticket->data,
+                              ticklen);
+                       s->session->tlsext_ticklen = ticklen;
+                       }
                else
                        ticklen = 0;
+               if (ticklen == 0 && s->tlsext_session_ticket &&
+                   s->tlsext_session_ticket->data == NULL)
+                       goto skip_ext;
                /* Check for enough room 2 for extension type, 2 for len
                 * rest for ticket
                 */
@@ -371,6 +392,7 @@ unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned cha
                        ret += ticklen;
                        }
                }
+               skip_ext:
 
 #ifdef TLSEXT_TYPE_opaque_prf_input
        if (s->s3->client_opaque_prf_input != NULL)
@@ -751,6 +773,15 @@ int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, in
                                }
                        }
 #endif
+               else if (type == TLSEXT_TYPE_session_ticket)
+                       {
+                       if (s->tls_session_ticket_ext_cb &&
+                           !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
+                               {
+                               *al = TLS1_AD_INTERNAL_ERROR;
+                               return 0;
+                               }
+                       }
                else if (type == TLSEXT_TYPE_status_request
                                                && s->ctx->tlsext_status_cb)
                        {
@@ -928,6 +959,12 @@ int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, in
 
                else if (type == TLSEXT_TYPE_session_ticket)
                        {
+                       if (s->tls_session_ticket_ext_cb &&
+                           !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
+                               {
+                               *al = TLS1_AD_INTERNAL_ERROR;
+                               return 0;
+                               }
                        if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
                                || (size > 0))
                                {
@@ -1435,6 +1472,15 @@ int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
                                s->tlsext_ticket_expected = 1;
                                return 0;       /* Cache miss */
                                }
+                       if (s->tls_session_secret_cb)
+                               {
+                               /* Indicate cache miss here and instead of
+                                * generating the session from ticket now,
+                                * trigger abbreviated handshake based on
+                                * external mechanism to calculate the master
+                                * secret later. */
+                               return 0;
+                               }
                        return tls_decrypt_ticket(s, p, size, session_id, len,
                                                                        ret);
                        }
index 22399b9e33f0579f92115991d2ad09e1a7187409..0ecbc6d1052e4d5959a9b2460b44dfa21fe3f18b 100644 (file)
@@ -512,6 +512,13 @@ SSL_CTX_callback_ctrl(ssl,SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB,(void (*)(void))cb)
 #define TLS_MD_MASTER_SECRET_CONST    "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"  /*master secret*/
 #endif
 
+/* TLS Session Ticket extension struct */
+struct tls_session_ticket_ext_st
+       {
+       unsigned short length;
+       void *data;
+       };
+
 #ifdef  __cplusplus
 }
 #endif
index e48403d88d5609dfce71663922841763f7f639f3..e11abfa52f9bfb0d1aad9de7c4f933ed3f29ecb0 100755 (executable)
@@ -254,3 +254,5 @@ PEM_read_bio_SSL_SESSION                302 EXIST::FUNCTION:
 SSL_CTX_set_psk_server_callback         303    EXIST::FUNCTION:PSK
 SSL_get_psk_identity                    304    EXIST::FUNCTION:PSK
 PEM_write_SSL_SESSION                   305    EXIST:!WIN16:FUNCTION:
+SSL_set_session_ticket_ext             306     EXIST::FUNCTION:TLSEXT
+SSL_set_session_secret_cb              307     EXIST::FUNCTION:TLSEXT