oops, revert wrong patch
[openssl.git] / ssl / s3_srvr.c
index 4eec497bef7a4070018075a8cdeb0011948f5ceb..9e73d629217be160c65eec4dffb6df092570f2c6 100644 (file)
@@ -181,24 +181,25 @@ static const SSL_METHOD *ssl3_get_server_method(int ver)
        }
 
 #ifndef OPENSSL_NO_SRP
-static int SSL_check_srp_ext_ClientHello(SSL *s,int *ad)
+static int ssl_check_srp_ext_ClientHello(SSL *s,int *al)
        {
        int ret = SSL_ERROR_NONE;
 
-       *ad = SSL_AD_UNRECOGNIZED_NAME;
+       *al = SSL_AD_UNRECOGNIZED_NAME;
 
        if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
            (s->srp_ctx.TLS_ext_srp_username_callback != NULL))
                {
                if(s->srp_ctx.login == NULL)
                        {
-                       /* There isn't any srp login extension !!! */
-                       ret = SSL3_AL_WARNING;
-                       *ad = SSL_AD_MISSING_SRP_USERNAME;
+                       /* RFC 5054 says SHOULD reject, 
+                          we do so if There is no srp login name */
+                       ret = SSL3_AL_FATAL;
+                       *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
                        }
                else
                        {
-                       ret = SSL_srp_server_param_with_username(s,ad);
+                       ret = SSL_srp_server_param_with_username(s,al);
                        }
                }
        return ret;
@@ -217,9 +218,6 @@ int ssl3_accept(SSL *s)
        void (*cb)(const SSL *ssl,int type,int val)=NULL;
        int ret= -1;
        int new_state,state,skip=0;
-#ifndef OPENSSL_NO_SRP
-       int srp_no_username =0;
-#endif
 
        RAND_add(&Time,sizeof(Time),0);
        ERR_clear_error();
@@ -240,6 +238,18 @@ int ssl3_accept(SSL *s)
                return(-1);
                }
 
+#ifndef OPENSSL_NO_HEARTBEATS
+       /* If we're awaiting a HeartbeatResponse, pretend we
+        * already got and don't await it anymore, because
+        * Heartbeats don't make sense during handshakes anyway.
+        */
+       if (s->tlsext_hb_pending)
+               {
+               s->tlsext_hb_pending = 0;
+               s->tlsext_hb_seq++;
+               }
+#endif
+
        for (;;)
                {
                state=s->state;
@@ -340,37 +350,34 @@ int ssl3_accept(SSL *s)
                case SSL3_ST_SR_CLNT_HELLO_A:
                case SSL3_ST_SR_CLNT_HELLO_B:
                case SSL3_ST_SR_CLNT_HELLO_C:
-#ifndef OPENSSL_NO_SRP
-               case SSL3_ST_SR_CLNT_HELLO_SRP_USERNAME:
-#endif
 
-                       s->shutdown=0;
-                       ret=ssl3_get_client_hello(s);
-                       if (ret <= 0) goto end;
+                       if (s->rwstate != SSL_X509_LOOKUP)
+                       {
+                               ret=ssl3_get_client_hello(s);
+                               if (ret <= 0) goto end;
+                       }
 #ifndef OPENSSL_NO_SRP
                        {
-                       int extension_error = 0,al;
-
-                       if ((al = SSL_check_srp_ext_ClientHello(s,&extension_error)) != SSL_ERROR_NONE)
-                               {
-                               ssl3_send_alert(s,al,extension_error);
-                               if (extension_error == SSL_AD_MISSING_SRP_USERNAME)
+                       int al;
+                       if ((ret = ssl_check_srp_ext_ClientHello(s,&al))  < 0)
                                        {
-                                       if (srp_no_username) goto end;
-                                       ERR_clear_error();
-                                       srp_no_username = 1;
-                                       s->state=SSL3_ST_SR_CLNT_HELLO_SRP_USERNAME;
-                                       if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
-                                       if ((ret=BIO_flush(s->wbio)) <= 0) goto end;
-                                       s->init_num=0;
-                                       break;
+                                       /* callback indicates firther work to be done */
+                                       s->rwstate=SSL_X509_LOOKUP;
+                                       goto end;
                                        }
-                               ret = -1;
-                               SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_CLIENTHELLO_TLSEXT);
-                               goto end;
+                       if (ret != SSL_ERROR_NONE)
+                               {
+                               ssl3_send_alert(s,SSL3_AL_FATAL,al);    
+                               /* This is not really an error but the only means to
+                                   for a client to detect whether srp is supported. */
+                                  if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)      
+                                       SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_CLIENTHELLO_TLSEXT);                     
+                               ret = SSL_TLSEXT_ERR_ALERT_FATAL;                       
+                               ret= -1;
+                               goto end;       
                                }
                        }
-#endif
+#endif         
                        
                        s->renegotiate = 2;
                        s->state=SSL3_ST_SW_SRVR_HELLO_A;
@@ -516,6 +523,9 @@ int ssl3_accept(SSL *s)
                                skip=1;
                                s->s3->tmp.cert_request=0;
                                s->state=SSL3_ST_SW_SRVR_DONE_A;
+                               if (s->s3->handshake_buffer)
+                                       if (!ssl3_digest_cached_records(s))
+                                               return -1;
                                }
                        else
                                {
@@ -608,6 +618,24 @@ int ssl3_accept(SSL *s)
 #endif
                                s->init_num = 0;
                                }
+                       else if (TLS1_get_version(s) >= TLS1_2_VERSION)
+                               {
+                               s->state=SSL3_ST_SR_CERT_VRFY_A;
+                               s->init_num=0;
+                               if (!s->session->peer)
+                                       break;
+                               /* For TLS v1.2 freeze the handshake buffer
+                                * at this point and digest cached records.
+                                */
+                               if (!s->s3->handshake_buffer)
+                                       {
+                                       SSLerr(SSL_F_SSL3_ACCEPT,ERR_R_INTERNAL_ERROR);
+                                       return -1;
+                                       }
+                               s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
+                               if (!ssl3_digest_cached_records(s))
+                                       return -1;
+                               }
                        else
                                {
                                int offset=0;
@@ -674,14 +702,11 @@ int ssl3_accept(SSL *s)
                        ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
                                SSL3_ST_SR_FINISHED_B);
                        if (ret <= 0) goto end;
-#ifndef OPENSSL_NO_TLSEXT
-                       if (s->tlsext_ticket_expected)
-                               s->state=SSL3_ST_SW_SESSION_TICKET_A;
-                       else if (s->hit)
-                               s->state=SSL_ST_OK;
-#else
                        if (s->hit)
                                s->state=SSL_ST_OK;
+#ifndef OPENSSL_NO_TLSEXT
+                       else if (s->tlsext_ticket_expected)
+                               s->state=SSL3_ST_SW_SESSION_TICKET_A;
 #endif
                        else
                                s->state=SSL3_ST_SW_CHANGE_A;
@@ -768,9 +793,6 @@ int ssl3_accept(SSL *s)
 
                        if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */
                                {
-                               /* actually not necessarily a 'new' session unless
-                                * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
-                               
                                s->renegotiate=0;
                                s->new_session=0;
                                
@@ -862,15 +884,20 @@ int ssl3_check_client_hello(SSL *s)
        if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
                {
                /* Throw away what we have done so far in the current handshake,
-                * which will now be aborted. (A full SSL_clear would be too much.)
-                * I hope that tmp.dh is the only thing that may need to be cleared
-                * when a handshake is not completed ... */
+                * which will now be aborted. (A full SSL_clear would be too much.) */
 #ifndef OPENSSL_NO_DH
                if (s->s3->tmp.dh != NULL)
                        {
                        DH_free(s->s3->tmp.dh);
                        s->s3->tmp.dh = NULL;
                        }
+#endif
+#ifndef OPENSSL_NO_ECDH
+               if (s->s3->tmp.ecdh != NULL)
+                       {
+                       EC_KEY_free(s->s3->tmp.ecdh);
+                       s->s3->tmp.ecdh = NULL;
+                       }
 #endif
                return 2;
                }
@@ -897,9 +924,6 @@ int ssl3_get_client_hello(SSL *s)
         * TLSv1.
         */
        if (s->state == SSL3_ST_SR_CLNT_HELLO_A
-#ifndef OPENSSL_NO_SRP
-               || (s->state == SSL3_ST_SR_CLNT_HELLO_SRP_USERNAME)
-#endif
                )
                {
                s->state=SSL3_ST_SR_CLNT_HELLO_B;
@@ -957,13 +981,16 @@ int ssl3_get_client_hello(SSL *s)
        j= *(p++);
 
        s->hit=0;
-       /* Versions before 0.9.7 always allow session reuse during renegotiation
-        * (i.e. when s->new_session is true), option
-        * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
-        * Maybe this optional behaviour should always have been the default,
-        * but we cannot safely change the default behaviour (or new applications
-        * might be written that become totally unsecure when compiled with
-        * an earlier library version)
+       /* Versions before 0.9.7 always allow clients to resume sessions in renegotiation.
+        * 0.9.7 and later allow this by default, but optionally ignore resumption requests
+        * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
+        * than a change to default behavior so that applications relying on this for security
+        * won't even compile against older library versions).
+        *
+        * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to request
+        * renegotiation but not a new session (s->new_session remains unset): for servers,
+        * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
+        * setting will be ignored.
         */
        if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
                {
@@ -1359,8 +1386,11 @@ int ssl3_get_client_hello(SSL *s)
                s->s3->tmp.new_cipher=s->session->cipher;
                }
 
-       if (!ssl3_digest_cached_records(s))
-               goto f_err;
+       if (TLS1_get_version(s) < TLS1_2_VERSION || !(s->verify_mode & SSL_VERIFY_PEER))
+               {
+               if (!ssl3_digest_cached_records(s))
+                       goto f_err;
+               }
        
        /* we now have the following setup. 
         * client_random
@@ -1415,20 +1445,20 @@ int ssl3_send_server_hello(SSL *s)
                memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
                p+=SSL3_RANDOM_SIZE;
 
-               /* now in theory we have 3 options to sending back the
-                * session id.  If it is a re-use, we send back the
-                * old session-id, if it is a new session, we send
-                * back the new session-id or we send back a 0 length
-                * session-id if we want it to be single use.
-                * Currently I will not implement the '0' length session-id
-                * 12-Jan-98 - I'll now support the '0' length stuff.
-                *
-                * We also have an additional case where stateless session
-                * resumption is successful: we always send back the old
-                * session id. In this case s->hit is non zero: this can
-                * only happen if stateless session resumption is succesful
-                * if session caching is disabled so existing functionality
-                * is unaffected.
+               /* There are several cases for the session ID to send
+                * back in the server hello:
+                * - For session reuse from the session cache,
+                *   we send back the old session ID.
+                * - If stateless session reuse (using a session ticket)
+                *   is successful, we send back the client's "session ID"
+                *   (which doesn't actually identify the session).
+                * - If it is a new session, we send back the new
+                *   session ID.
+                * - However, if we want the new session to be single-use,
+                *   we send back a 0-length session ID.
+                * s->hit is non-zero in either case of session reuse,
+                * so the following won't overwrite an ID that we're supposed
+                * to send back.
                 */
                if (s->session->not_resumable ||
                        (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
@@ -1598,7 +1628,6 @@ int ssl3_send_server_key_exchange(SSL *s)
 
                        if (s->s3->tmp.dh != NULL)
                                {
-                               DH_free(dh);
                                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
                                goto err;
                                }
@@ -1659,7 +1688,6 @@ int ssl3_send_server_key_exchange(SSL *s)
 
                        if (s->s3->tmp.ecdh != NULL)
                                {
-                               EC_KEY_free(s->s3->tmp.ecdh); 
                                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
                                goto err;
                                }
@@ -1670,12 +1698,11 @@ int ssl3_send_server_key_exchange(SSL *s)
                                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
                                goto err;
                                }
-                       if (!EC_KEY_up_ref(ecdhp))
+                       if ((ecdh = EC_KEY_dup(ecdhp)) == NULL)
                                {
                                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
                                goto err;
                                }
-                       ecdh = ecdhp;
 
                        s->s3->tmp.ecdh=ecdh;
                        if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
@@ -1869,6 +1896,7 @@ int ssl3_send_server_key_exchange(SSL *s)
                            (unsigned char *)encodedPoint, 
                            encodedlen);
                        OPENSSL_free(encodedPoint);
+                       encodedPoint = NULL;
                        p += encodedlen;
                        }
 #endif
@@ -1890,12 +1918,14 @@ int ssl3_send_server_key_exchange(SSL *s)
                         * and p points to the space at the end. */
 #ifndef OPENSSL_NO_RSA
                        if (pkey->type == EVP_PKEY_RSA
-                                       && s->version < TLS1_2_VERSION)
+                                       && TLS1_get_version(s) < TLS1_2_VERSION)
                                {
                                q=md_buf;
                                j=0;
                                for (num=2; num > 0; num--)
                                        {
+                                       EVP_MD_CTX_set_flags(&md_ctx,
+                                               EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
                                        EVP_DigestInit_ex(&md_ctx,(num == 2)
                                                ?s->ctx->md5:s->ctx->sha1, NULL);
                                        EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
@@ -1921,7 +1951,7 @@ int ssl3_send_server_key_exchange(SSL *s)
                                {
                                /* For TLS1.2 and later send signature
                                 * algorithm */
-                               if (s->version >= TLS1_2_VERSION)
+                               if (TLS1_get_version(s) >= TLS1_2_VERSION)
                                        {
                                        if (!tls12_get_sigandhash(p, pkey, md))
                                                {
@@ -1932,6 +1962,10 @@ int ssl3_send_server_key_exchange(SSL *s)
                                                }
                                        p+=2;
                                        }
+#ifdef SSL_DEBUG
+                               fprintf(stderr, "Using hash %s\n",
+                                                       EVP_MD_name(md));
+#endif
                                EVP_SignInit_ex(&md_ctx, md, NULL);
                                EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
                                EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
@@ -1944,7 +1978,7 @@ int ssl3_send_server_key_exchange(SSL *s)
                                        }
                                s2n(i,p);
                                n+=i+2;
-                               if (s->version >= TLS1_2_VERSION)
+                               if (TLS1_get_version(s) >= TLS1_2_VERSION)
                                        n+= 2;
                                }
                        else
@@ -2000,6 +2034,14 @@ int ssl3_send_certificate_request(SSL *s)
                p+=n;
                n++;
 
+               if (TLS1_get_version(s) >= TLS1_2_VERSION)
+                       {
+                       nl = tls12_get_req_sig_algs(s, p + 2);
+                       s2n(nl, p);
+                       p += nl + 2;
+                       n += nl + 2;
+                       }
+
                off=n;
                p+=2;
                n+=2;
@@ -2572,6 +2614,12 @@ int ssl3_get_client_key_exchange(SSL *s)
                        /* Get encoded point length */
                        i = *p; 
                        p += 1;
+                       if (n != 1 + i)
+                               {
+                               SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
+                                   ERR_R_EC_LIB);
+                               goto err;
+                               }
                        if (EC_POINT_oct2point(group, 
                            clnt_ecpoint, p, i, bn_ctx) == 0)
                                {
@@ -2854,6 +2902,9 @@ int ssl3_get_cert_verify(SSL *s)
        long n;
        int type=0,i,j;
        X509 *peer;
+       const EVP_MD *md = NULL;
+       EVP_MD_CTX mctx;
+       EVP_MD_CTX_init(&mctx);
 
        n=s->method->ssl_get_message(s,
                SSL3_ST_SR_CERT_VRFY_A,
@@ -2922,6 +2973,36 @@ int ssl3_get_cert_verify(SSL *s)
                } 
        else 
                {       
+               if (TLS1_get_version(s) >= TLS1_2_VERSION)
+                       {
+                       int sigalg = tls12_get_sigid(pkey);
+                       /* Should never happen */
+                       if (sigalg == -1)
+                               {
+                               SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
+                               al=SSL_AD_INTERNAL_ERROR;
+                               goto f_err;
+                               }
+                       /* Check key type is consistent with signature */
+                       if (sigalg != (int)p[1])
+                               {
+                               SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_TYPE);
+                               al=SSL_AD_DECODE_ERROR;
+                               goto f_err;
+                               }
+                       md = tls12_get_hash(p[0]);
+                       if (md == NULL)
+                               {
+                               SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_UNKNOWN_DIGEST);
+                               al=SSL_AD_DECODE_ERROR;
+                               goto f_err;
+                               }
+#ifdef SSL_DEBUG
+fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
+#endif
+                       p += 2;
+                       n -= 2;
+                       }
                n2s(p,i);
                n-=2;
                if (i > n)
@@ -2939,6 +3020,37 @@ int ssl3_get_cert_verify(SSL *s)
                goto f_err;
                }
 
+       if (TLS1_get_version(s) >= TLS1_2_VERSION)
+               {
+               long hdatalen = 0;
+               void *hdata;
+               hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
+               if (hdatalen <= 0)
+                       {
+                       SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
+                       al=SSL_AD_INTERNAL_ERROR;
+                       goto f_err;
+                       }
+#ifdef SSL_DEBUG
+               fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
+                                                       EVP_MD_name(md));
+#endif
+               if (!EVP_VerifyInit_ex(&mctx, md, NULL)
+                       || !EVP_VerifyUpdate(&mctx, hdata, hdatalen))
+                       {
+                       SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
+                       al=SSL_AD_INTERNAL_ERROR;
+                       goto f_err;
+                       }
+
+               if (EVP_VerifyFinal(&mctx, p , i, pkey) <= 0)
+                       {
+                       al=SSL_AD_DECRYPT_ERROR;
+                       SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_SIGNATURE);
+                       goto f_err;
+                       }
+               }
+       else
 #ifndef OPENSSL_NO_RSA 
        if (pkey->type == EVP_PKEY_RSA)
                {
@@ -3029,6 +3141,13 @@ f_err:
                ssl3_send_alert(s,SSL3_AL_FATAL,al);
                }
 end:
+       if (s->s3->handshake_buffer)
+               {
+               BIO_free(s->s3->handshake_buffer);
+               s->s3->handshake_buffer = NULL;
+               s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
+               }
+       EVP_MD_CTX_cleanup(&mctx);
        EVP_PKEY_free(pkey);
        return(ret);
        }
@@ -3141,6 +3260,12 @@ int ssl3_get_client_certificate(SSL *s)
                        al=SSL_AD_HANDSHAKE_FAILURE;
                        goto f_err;
                        }
+               /* No client certificate so digest cached records */
+               if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s))
+                       {
+                       al=SSL_AD_INTERNAL_ERROR;
+                       goto f_err;
+                       }
                }
        else
                {
@@ -3217,13 +3342,17 @@ int ssl3_send_server_certificate(SSL *s)
        /* SSL3_ST_SW_CERT_B */
        return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
        }
+
 #ifndef OPENSSL_NO_TLSEXT
+/* send a new session ticket (not necessarily for a new session) */
 int ssl3_send_newsession_ticket(SSL *s)
        {
        if (s->state == SSL3_ST_SW_SESSION_TICKET_A)
                {
                unsigned char *p, *senc, *macstart;
-               int len, slen;
+               const unsigned char *const_p;
+               int len, slen_full, slen;
+               SSL_SESSION *sess;
                unsigned int hlen;
                EVP_CIPHER_CTX ctx;
                HMAC_CTX hctx;
@@ -3232,12 +3361,38 @@ int ssl3_send_newsession_ticket(SSL *s)
                unsigned char key_name[16];
 
                /* get session encoding length */
-               slen = i2d_SSL_SESSION(s->session, NULL);
+               slen_full = i2d_SSL_SESSION(s->session, NULL);
                /* Some length values are 16 bits, so forget it if session is
                 * too long
                 */
-               if (slen > 0xFF00)
+               if (slen_full > 0xFF00)
+                       return -1;
+               senc = OPENSSL_malloc(slen_full);
+               if (!senc)
                        return -1;
+               p = senc;
+               i2d_SSL_SESSION(s->session, &p);
+
+               /* create a fresh copy (not shared with other threads) to clean up */
+               const_p = senc;
+               sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
+               if (sess == NULL)
+                       {
+                       OPENSSL_free(senc);
+                       return -1;
+                       }
+               sess->session_id_length = 0; /* ID is irrelevant for the ticket */
+
+               slen = i2d_SSL_SESSION(sess, NULL);
+               if (slen > slen_full) /* shouldn't ever happen */
+                       {
+                       OPENSSL_free(senc);
+                       return -1;
+                       }
+               p = senc;
+               i2d_SSL_SESSION(sess, &p);
+               SSL_SESSION_free(sess);
+
                /* Grow buffer if need be: the length calculation is as
                 * follows 1 (size of message name) + 3 (message length
                 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
@@ -3249,11 +3404,6 @@ int ssl3_send_newsession_ticket(SSL *s)
                        26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
                        EVP_MAX_MD_SIZE + slen))
                        return -1;
-               senc = OPENSSL_malloc(slen);
-               if (!senc)
-                       return -1;
-               p = senc;
-               i2d_SSL_SESSION(s->session, &p);
 
                p=(unsigned char *)s->init_buf->data;
                /* do the header */
@@ -3284,7 +3434,13 @@ int ssl3_send_newsession_ticket(SSL *s)
                                        tlsext_tick_md(), NULL);
                        memcpy(key_name, tctx->tlsext_tick_key_name, 16);
                        }
-               l2n(s->session->tlsext_tick_lifetime_hint, p);
+
+               /* Ticket lifetime hint (advisory only):
+                * We leave this unspecified for resumed session (for simplicity),
+                * and guess that tickets for new sessions will live as long
+                * as their sessions. */
+               l2n(s->hit ? 0 : s->session->timeout, p);
+
                /* Skip ticket length for now */
                p += 2;
                /* Output key name */