Re-enable SSL's cut-through feature in Master over openSSL 1.0.0.
It was pulled because of b/2586347 but it turns out to be a problem in
the tests. The tests were fixed in Change Id8472487, and the feature
is re-enabled here.
Bug id: 2614118

Change-Id: I0bf365dbacd8e962e1156fb1f94a684c60802f4d
diff --git a/android.testssl/testssl b/android.testssl/testssl
index 46f4576..3f24b1d 100755
--- a/android.testssl/testssl
+++ b/android.testssl/testssl
@@ -79,6 +79,8 @@
 echo test sslv2/sslv3 with both client and server authentication and small client and server buffers
 $ssltest -server_auth -client_auth -c_small_records -s_small_records $CA $extra || exit 1
 
+echo test sslv2/sslv3 with both client and server authentication and handshake cutthrough
+$ssltest -server_auth -client_auth -cutthrough $CA $extra || exit 1
 
 echo test sslv2 via BIO pair
 $ssltest -bio_pair -ssl2 $extra || exit 1
diff --git a/apps/s_client.c b/apps/s_client.c
index fd5f35e..f2b8b43 100644
--- a/apps/s_client.c
+++ b/apps/s_client.c
@@ -345,6 +345,7 @@
 	BIO_printf(bio_err," -tlsextdebug      - hex dump of all TLS extensions received\n");
 	BIO_printf(bio_err," -status           - request certificate status from server\n");
 	BIO_printf(bio_err," -no_ticket        - disable use of RFC4507bis session tickets\n");
+	BIO_printf(bio_err," -cutthrough       - enable 1-RTT full-handshake for strong ciphers\n");
 #endif
 	BIO_printf(bio_err," -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n");
 	}
@@ -402,6 +403,7 @@
 	EVP_PKEY *key = NULL;
 	char *CApath=NULL,*CAfile=NULL,*cipher=NULL;
 	int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0;
+	int cutthrough=0;
 	int crlf=0;
 	int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending;
 	SSL_CTX *ctx=NULL;
@@ -660,6 +662,8 @@
 		else if	(strcmp(*argv,"-no_ticket") == 0)
 			{ off|=SSL_OP_NO_TICKET; }
 #endif
+		else if (strcmp(*argv,"-cutthrough") == 0)
+			cutthrough=1;
 		else if (strcmp(*argv,"-serverpref") == 0)
 			off|=SSL_OP_CIPHER_SERVER_PREFERENCE;
 		else if (strcmp(*argv,"-legacy_renegotiation") == 0)
@@ -886,6 +890,15 @@
 	 */
 	if (socket_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1);
 
+	/* Enable handshake cutthrough for client connections using
+	 * strong ciphers. */
+	if (cutthrough)
+		{
+		int ssl_mode = SSL_CTX_get_mode(ctx);
+		ssl_mode |= SSL_MODE_HANDSHAKE_CUTTHROUGH;
+		SSL_CTX_set_mode(ctx, ssl_mode);
+		}
+
 	if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
 	if (cipher != NULL)
 		if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
diff --git a/include/openssl/ssl.h b/include/openssl/ssl.h
index 7858169..a05c90f 100644
--- a/include/openssl/ssl.h
+++ b/include/openssl/ssl.h
@@ -605,6 +605,10 @@
 /* Use small read and write buffers: (a) lazy allocate read buffers for
  * large incoming records, and (b) limit the size of outgoing records. */
 #define SSL_MODE_SMALL_BUFFERS 0x00000020L
+/* When set, clients may send application data before receipt of CCS
+ * and Finished.  This mode enables full-handshakes to 'complete' in
+ * one RTT. */
+#define SSL_MODE_HANDSHAKE_CUTTHROUGH 0x00000040L
 
 /* Note: SSL[_CTX]_set_{options,mode} use |= op on the previous value,
  * they cannot be used to clear bits. */
@@ -1240,10 +1244,12 @@
 /* Is the SSL_connection established? */
 #define SSL_get_state(a)		SSL_state(a)
 #define SSL_is_init_finished(a)		(SSL_state(a) == SSL_ST_OK)
-#define SSL_in_init(a)			(SSL_state(a)&SSL_ST_INIT)
+#define SSL_in_init(a)			((SSL_state(a)&SSL_ST_INIT) && \
+                                  !SSL_cutthrough_complete(a))
 #define SSL_in_before(a)		(SSL_state(a)&SSL_ST_BEFORE)
 #define SSL_in_connect_init(a)		(SSL_state(a)&SSL_ST_CONNECT)
 #define SSL_in_accept_init(a)		(SSL_state(a)&SSL_ST_ACCEPT)
+int SSL_cutthrough_complete(const SSL *s);
 
 /* The following 2 states are kept in ssl->rstate when reads fail,
  * you should not need these */
diff --git a/include/openssl/ssl3.h b/include/openssl/ssl3.h
index fb41ca5..0633950 100644
--- a/include/openssl/ssl3.h
+++ b/include/openssl/ssl3.h
@@ -529,6 +529,7 @@
 /*client */
 /* extra state */
 #define SSL3_ST_CW_FLUSH		(0x100|SSL_ST_CONNECT)
+#define SSL3_ST_CUTTHROUGH_COMPLETE	(0x101|SSL_ST_CONNECT)
 /* write to server */
 #define SSL3_ST_CW_CLNT_HELLO_A		(0x110|SSL_ST_CONNECT)
 #define SSL3_ST_CW_CLNT_HELLO_B		(0x111|SSL_ST_CONNECT)
diff --git a/openssl.config b/openssl.config
index df1f625..6c737d5 100644
--- a/openssl.config
+++ b/openssl.config
@@ -152,9 +152,8 @@
 
 OPENSSL_PATCHES="\
         progs.patch                 \
-        small_records.patch"
-# Disabled for compatibility b/2586347
-#       handshake_cutthrough.patch
+        small_records.patch         \
+        handshake_cutthrough.patch"
 
 OPENSSL_PATCHES_progs_SOURCES="\
 	apps/progs.h \
diff --git a/patches/handshake_cutthrough.patch b/patches/handshake_cutthrough.patch
index 130afa3..1a06dbd 100644
--- a/patches/handshake_cutthrough.patch
+++ b/patches/handshake_cutthrough.patch
@@ -1,6 +1,6 @@
-diff -uarp openssl-0.9.8k.orig/apps/s_client.c openssl-0.9.8k/apps/s_client.c
---- openssl-0.9.8k.orig/apps/s_client.c	2008-12-20 09:04:08.000000000 -0800
-+++ openssl-0.9.8k/apps/s_client.c	2009-10-05 17:00:24.000000000 -0700
+diff -uarp openssl-1.0.0.orig/apps/s_client.c openssl-1.0.0/apps/s_client.c
+--- openssl-1.0.0.orig/apps/s_client.c	2009-12-16 15:28:28.000000000 -0500
++++ openssl-1.0.0/apps/s_client.c	2010-04-21 14:39:49.000000000 -0400
 @@ -248,6 +248,7 @@ static void sc_usage(void)
  	BIO_printf(bio_err," -tlsextdebug      - hex dump of all TLS extensions received\n");
  	BIO_printf(bio_err," -status           - request certificate status from server\n");
@@ -42,9 +42,9 @@
  	if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback);
  	if (cipher != NULL)
  		if(!SSL_CTX_set_cipher_list(ctx,cipher)) {
-diff -uarp openssl-0.9.8k.orig/ssl/s3_clnt.c openssl-0.9.8k/ssl/s3_clnt.c
---- openssl-0.9.8k.orig/ssl/s3_clnt.c	2009-02-14 13:50:14.000000000 -0800
-+++ openssl-0.9.8k/ssl/s3_clnt.c	2009-10-05 17:00:24.000000000 -0700
+diff -uarp openssl-1.0.0.orig/ssl/s3_clnt.c openssl-1.0.0/ssl/s3_clnt.c
+--- openssl-1.0.0.orig/ssl/s3_clnt.c	2010-02-27 19:24:24.000000000 -0500
++++ openssl-1.0.0/ssl/s3_clnt.c	2010-04-21 14:39:49.000000000 -0400
 @@ -186,6 +186,20 @@ int ssl3_connect(SSL *s)
  	
  	s->in_handshake++;
@@ -127,9 +127,9 @@
  		case SSL_ST_OK:
  			/* clean a few things up */
  			ssl3_cleanup_key_block(s);
-diff -uarp openssl-0.9.8k.orig/ssl/s3_lib.c openssl-0.9.8k/ssl/s3_lib.c
---- openssl-0.9.8k.orig/ssl/s3_lib.c	2008-06-16 09:56:41.000000000 -0700
-+++ openssl-0.9.8k/ssl/s3_lib.c	2009-10-05 17:00:24.000000000 -0700
+diff -uarp openssl-1.0.0.orig/ssl/s3_lib.c openssl-1.0.0/ssl/s3_lib.c
+-- openssl-1.0.0.orig/ssl/s3_lib.c     2009-10-16 11:24:19.000000000 -0400
++++ openssl-1.0.0/ssl/s3_lib.c  2010-04-21 14:39:49.000000000 -0400
 @@ -2551,9 +2551,22 @@ int ssl3_write(SSL *s, const void *buf, 
  
  static int ssl3_read_internal(SSL *s, void *buf, int len, int peek)
@@ -154,18 +154,18 @@
  	if (s->s3->renegotiate) ssl3_renegotiate_check(s);
  	s->s3->in_read_app_data=1;
  	ret=s->method->ssl_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek);
-diff -uarp openssl-0.9.8k.orig/ssl/ssl.h openssl-0.9.8k/ssl/ssl.h
---- openssl-0.9.8k.orig/ssl/ssl.h	2009-10-05 17:00:39.000000000 -0700
-+++ openssl-0.9.8k/ssl/ssl.h	2009-10-05 17:00:24.000000000 -0700
-@@ -557,6 +557,10 @@ typedef struct ssl_session_st
+diff -uarp openssl-1.0.0.orig/ssl/ssl.h openssl-1.0.0/ssl/ssl.h
+--- openssl-1.0.0.orig/ssl/ssl.h	2010-01-06 12:37:38.000000000 -0500
++++ openssl-1.0.0/ssl/ssl.h	2010-04-21 16:57:49.000000000 -0400
+@@ -605,6 +605,10 @@ typedef struct ssl_session_st
  /* Use small read and write buffers: (a) lazy allocate read buffers for
   * large incoming records, and (b) limit the size of outgoing records. */
- #define SSL_MODE_SMALL_BUFFERS 0x00000010L
+ #define SSL_MODE_SMALL_BUFFERS 0x00000020L
 +/* When set, clients may send application data before receipt of CCS
 + * and Finished.  This mode enables full-handshakes to 'complete' in
 + * one RTT. */
-+#define SSL_MODE_HANDSHAKE_CUTTHROUGH 0x00000020L
- 
++#define SSL_MODE_HANDSHAKE_CUTTHROUGH 0x00000040L
+
  /* Note: SSL[_CTX]_set_{options,mode} use |= op on the previous value,
   * they cannot be used to clear bits. */
 @@ -1097,10 +1101,12 @@ extern "C" {
@@ -182,10 +182,10 @@
  
  /* The following 2 states are kept in ssl->rstate when reads fail,
   * you should not need these */
-Only in openssl-0.9.8k/ssl: ssl.h.orig
-diff -uarp openssl-0.9.8k.orig/ssl/ssl3.h openssl-0.9.8k/ssl/ssl3.h
---- openssl-0.9.8k.orig/ssl/ssl3.h	2009-10-05 17:00:39.000000000 -0700
-+++ openssl-0.9.8k/ssl/ssl3.h	2009-10-05 17:00:24.000000000 -0700
+Only in openssl-1.0.0/ssl: ssl.h.orig
+diff -uarp openssl-1.0.0.orig/ssl/ssl3.h openssl-1.0.0/ssl/ssl3.h
+-- openssl-1.0.0.orig/ssl/ssl3.h	2010-01-06 12:37:38.000000000 -0500
++++ openssl-1.0.0/ssl/ssl3.h	2010-04-21 14:39:49.000000000 -0400
 @@ -456,6 +456,7 @@ typedef struct ssl3_state_st
  /*client */
  /* extra state */
@@ -194,14 +194,13 @@
  /* write to server */
  #define SSL3_ST_CW_CLNT_HELLO_A		(0x110|SSL_ST_CONNECT)
  #define SSL3_ST_CW_CLNT_HELLO_B		(0x111|SSL_ST_CONNECT)
-diff -uarp openssl-0.9.8k.orig/ssl/ssl_lib.c openssl-0.9.8k/ssl/ssl_lib.c
---- openssl-0.9.8k.orig/ssl/ssl_lib.c	2009-02-23 08:02:47.000000000 -0800
-+++ openssl-0.9.8k/ssl/ssl_lib.c	2009-10-05 17:00:24.000000000 -0700
-@@ -2696,7 +2696,17 @@ void SSL_set_msg_callback(SSL *ssl, void
+diff -uarp openssl-1.0.0.orig/ssl/ssl_lib.c openssl-1.0.0/ssl/ssl_lib.c
+--- openssl-1.0.0.orig/ssl/ssl_lib.c	2010-02-17 14:43:46.000000000 -0500
++++ openssl-1.0.0/ssl/ssl_lib.c	2010-04-21 17:02:45.000000000 -0400
+@@ -3031,6 +3031,18 @@ void SSL_set_msg_callback(SSL *ssl, void
  	SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  	}
- 
--
+
 +int SSL_cutthrough_complete(const SSL *s)
 +	{
 +	return (!s->server &&                 /* cutthrough only applies to clients */
@@ -209,17 +208,17 @@
 +		s->version >= SSL3_VERSION &&
 +		s->s3->in_read_app_data == 0 &&   /* cutthrough only applies to write() */
 +		(SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) &&  /* cutthrough enabled */
-+		SSL_get_cipher_bits(s, NULL) >= 128 &&                      /* strong cipher choosen */
-+		(s->state == SSL3_ST_CR_SESSION_TICKET_A ||                 /* ready to write app-data*/
-+			s->state == SSL3_ST_CR_FINISHED_A));
++		SSL_get_cipher_bits(s, NULL) >= 128 &&       /* strong cipher choosen */
++		(s->state == SSL3_ST_CR_SESSION_TICKET_A ||  /* ready to write app-data*/
++		s->state == SSL3_ST_CR_FINISHED_A));
 +	}
- 
- #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
- #include "../crypto/bio/bss_file.c"
-Only in openssl-0.9.8k/ssl: ssl_lib.c.orig
-diff -uarp openssl-0.9.8k.orig/ssl/ssltest.c openssl-0.9.8k/ssl/ssltest.c
---- openssl-0.9.8k.orig/ssl/ssltest.c	2009-10-05 17:00:39.000000000 -0700
-+++ openssl-0.9.8k/ssl/ssltest.c	2009-10-05 17:00:24.000000000 -0700
++
+ /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
+  * vairable, freeing  EVP_MD_CTX previously stored in that variable, if
+  * any. If EVP_MD pointer is passed, initializes ctx with this md
+diff -uarp openssl-1.0.0.orig/ssl/ssltest.c openssl-1.0.0/ssl/ssltest.c
+--- openssl-1.0.0.orig/ssl/ssltest.c	2010-01-24 11:57:38.000000000 -0500
++++ openssl-1.0.0/ssl/ssltest.c	2010-04-21 17:06:35.000000000 -0400
 @@ -279,6 +279,7 @@ static void sv_usage(void)
  	fprintf(stderr," -test_cipherlist - verifies the order of the ssl cipher lists\n");
  	fprintf(stderr," -c_small_records - enable client side use of small SSL record buffers\n");
@@ -261,9 +260,9 @@
  
  #ifndef OPENSSL_NO_DH
  	if (!no_dhe)
-diff -uarp openssl-0.9.8k.orig/test/testssl openssl-0.9.8k/test/testssl
---- openssl-0.9.8k.orig/test/testssl	2009-10-05 17:00:39.000000000 -0700
-+++ openssl-0.9.8k/test/testssl	2009-10-05 17:00:24.000000000 -0700
+diff -uarp openssl-1.0.0.orig/test/testssl openssl-1.0.0/test/testssl
+--- openssl-1.0.0.orig/test/testssl	2006-03-10 18:06:27.000000000 -0500
++++ openssl-1.0.0/test/testssl	2010-04-21 16:50:13.000000000 -0400
 @@ -79,6 +79,8 @@ $ssltest -server_auth -client_auth -s_sm
  echo test sslv2/sslv3 with both client and server authentication and small client and server buffers
  $ssltest -server_auth -client_auth -c_small_records -s_small_records $CA $extra || exit 1
diff --git a/ssl/s3_clnt.c b/ssl/s3_clnt.c
index 41769fe..6bff3cc 100644
--- a/ssl/s3_clnt.c
+++ b/ssl/s3_clnt.c
@@ -199,6 +199,20 @@
 	
 	s->in_handshake++;
 	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 
+	if (SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
+		{
+		/* Renegotiation complicates the state machine */
+		s->s3->flags |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
+#if 0	/* Send app data in separate packet, otherwise, some particular site
+	 * (only one site so far) closes the socket.
+	 * Note: there is a very small chance that two TCP packets
+	 * could be arriving at server combined into a single TCP packet,
+	 * then trigger that site to break. We haven't encounter that though.
+	 */
+		/* Send app data along with CCS/Finished */
+		s->s3->flags |= SSL3_FLAGS_DELAY_CLIENT_FINISHED;
+#endif
+		}
 
 	for (;;)
 		{
@@ -474,14 +488,29 @@
 				}
 			else
 				{
-#ifndef OPENSSL_NO_TLSEXT
-				/* Allow NewSessionTicket if ticket expected */
-				if (s->tlsext_ticket_expected)
-					s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
+				if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && SSL_get_cipher_bits(s, NULL) >= 128)
+					{
+					if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
+						{
+						s->state=SSL3_ST_CUTTHROUGH_COMPLETE;
+						s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
+						s->s3->delay_buf_pop_ret=0;
+						}
+					else
+						{
+						s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE;
+						}
+					}
 				else
+					{
+#ifndef OPENSSL_NO_TLSEXT
+					/* Allow NewSessionTicket if ticket expected */
+					if (s->tlsext_ticket_expected)
+						s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
+					else
 #endif
-				
-				s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
+						s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
+					}
 				}
 			s->init_num=0;
 			break;
@@ -529,6 +558,24 @@
 			s->state=s->s3->tmp.next_state;
 			break;
 
+		case SSL3_ST_CUTTHROUGH_COMPLETE:
+#ifndef OPENSSL_NO_TLSEXT
+			/* Allow NewSessionTicket if ticket expected */
+			if (s->tlsext_ticket_expected)
+				s->state=SSL3_ST_CR_SESSION_TICKET_A;
+			else
+#endif
+				s->state=SSL3_ST_CR_FINISHED_A;
+
+			/* SSL_write() will take care of flushing buffered data if
+			 * DELAY_CLIENT_FINISHED is set.
+			 */
+			if (!(s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED))
+				ssl_free_wbio_buffer(s);
+			ret = 1;
+			goto end;
+			/* break; */
+
 		case SSL_ST_OK:
 			/* clean a few things up */
 			ssl3_cleanup_key_block(s);
diff --git a/ssl/s3_lib.c b/ssl/s3_lib.c
index d6b047c..e3edba8 100644
--- a/ssl/s3_lib.c
+++ b/ssl/s3_lib.c
@@ -3257,9 +3257,22 @@
 
 static int ssl3_read_internal(SSL *s, void *buf, int len, int peek)
 	{
-	int ret;
+	int n,ret;
 	
 	clear_sys_error();
+	if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio))
+		{
+		/* Deal with an application that calls SSL_read() when handshake data
+		 * is yet to be written.
+		 */
+		if (BIO_wpending(s->wbio) > 0)
+			{
+			s->rwstate=SSL_WRITING;
+			n=BIO_flush(s->wbio);
+			if (n <= 0) return(n);
+			s->rwstate=SSL_NOTHING;
+			}
+		}
 	if (s->s3->renegotiate) ssl3_renegotiate_check(s);
 	s->s3->in_read_app_data=1;
 	ret=s->method->ssl_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek);
diff --git a/ssl/ssl.h b/ssl/ssl.h
index 7858169..a05c90f 100644
--- a/ssl/ssl.h
+++ b/ssl/ssl.h
@@ -605,6 +605,10 @@
 /* Use small read and write buffers: (a) lazy allocate read buffers for
  * large incoming records, and (b) limit the size of outgoing records. */
 #define SSL_MODE_SMALL_BUFFERS 0x00000020L
+/* When set, clients may send application data before receipt of CCS
+ * and Finished.  This mode enables full-handshakes to 'complete' in
+ * one RTT. */
+#define SSL_MODE_HANDSHAKE_CUTTHROUGH 0x00000040L
 
 /* Note: SSL[_CTX]_set_{options,mode} use |= op on the previous value,
  * they cannot be used to clear bits. */
@@ -1240,10 +1244,12 @@
 /* Is the SSL_connection established? */
 #define SSL_get_state(a)		SSL_state(a)
 #define SSL_is_init_finished(a)		(SSL_state(a) == SSL_ST_OK)
-#define SSL_in_init(a)			(SSL_state(a)&SSL_ST_INIT)
+#define SSL_in_init(a)			((SSL_state(a)&SSL_ST_INIT) && \
+                                  !SSL_cutthrough_complete(a))
 #define SSL_in_before(a)		(SSL_state(a)&SSL_ST_BEFORE)
 #define SSL_in_connect_init(a)		(SSL_state(a)&SSL_ST_CONNECT)
 #define SSL_in_accept_init(a)		(SSL_state(a)&SSL_ST_ACCEPT)
+int SSL_cutthrough_complete(const SSL *s);
 
 /* The following 2 states are kept in ssl->rstate when reads fail,
  * you should not need these */
diff --git a/ssl/ssl3.h b/ssl/ssl3.h
index fb41ca5..0633950 100644
--- a/ssl/ssl3.h
+++ b/ssl/ssl3.h
@@ -529,6 +529,7 @@
 /*client */
 /* extra state */
 #define SSL3_ST_CW_FLUSH		(0x100|SSL_ST_CONNECT)
+#define SSL3_ST_CUTTHROUGH_COMPLETE	(0x101|SSL_ST_CONNECT)
 /* write to server */
 #define SSL3_ST_CW_CLNT_HELLO_A		(0x110|SSL_ST_CONNECT)
 #define SSL3_ST_CW_CLNT_HELLO_B		(0x111|SSL_ST_CONNECT)
diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c
index 3157f20..fc2a227 100644
--- a/ssl/ssl_lib.c
+++ b/ssl/ssl_lib.c
@@ -3031,6 +3031,18 @@
 	SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
 	}
 
+int SSL_cutthrough_complete(const SSL *s)
+	{
+	return (!s->server &&                 /* cutthrough only applies to clients */
+		!s->hit &&                        /* full-handshake */
+		s->version >= SSL3_VERSION &&
+		s->s3->in_read_app_data == 0 &&   /* cutthrough only applies to write() */
+		(SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) &&  /* cutthrough enabled */
+		SSL_get_cipher_bits(s, NULL) >= 128 &&       /* strong cipher choosen */
+		(s->state == SSL3_ST_CR_SESSION_TICKET_A ||  /* ready to write app-data*/
+		s->state == SSL3_ST_CR_FINISHED_A));
+	}
+
 /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
  * vairable, freeing  EVP_MD_CTX previously stored in that variable, if
  * any. If EVP_MD pointer is passed, initializes ctx with this md
diff --git a/ssl/ssltest.c b/ssl/ssltest.c
index 39cb541..e21ebda 100644
--- a/ssl/ssltest.c
+++ b/ssl/ssltest.c
@@ -318,6 +318,7 @@
 	fprintf(stderr," -test_cipherlist - verifies the order of the ssl cipher lists\n");
 	fprintf(stderr," -c_small_records - enable client side use of small SSL record buffers\n");
 	fprintf(stderr," -s_small_records - enable server side use of small SSL record buffers\n");
+	fprintf(stderr," -cutthrough      - enable 1-RTT full-handshake for strong ciphers\n");
 	}
 
 static void print_details(SSL *c_ssl, const char *prefix)
@@ -449,6 +450,7 @@
 	int ssl_mode = 0;
 	int c_small_records=0;
 	int s_small_records=0;
+	int cutthrough = 0;
 
 int main(int argc, char *argv[])
 	{
@@ -693,6 +695,10 @@
 			{
 			s_small_records = 1;
 			}
+		else if (strcmp(*argv, "-cutthrough") == 0)
+			{
+			cutthrough = 1;
+			}
 		else
 			{
 			fprintf(stderr,"unknown option %s\n",*argv);
@@ -829,6 +835,13 @@
 		ssl_mode |= SSL_MODE_SMALL_BUFFERS;
 		SSL_CTX_set_mode(s_ctx, ssl_mode);
 		}
+	ssl_mode = 0;
+	if (cutthrough)
+		{
+		ssl_mode = SSL_CTX_get_mode(c_ctx);
+		ssl_mode = SSL_MODE_HANDSHAKE_CUTTHROUGH;
+		SSL_CTX_set_mode(c_ctx, ssl_mode);
+		}
 
 #ifndef OPENSSL_NO_DH
 	if (!no_dhe)