1 diff -uarp openssl-1.0.0.orig/apps/s_client.c openssl-1.0.0/apps/s_client.c 2 --- openssl-1.0.0.orig/apps/s_client.c 2009-12-16 15:28:28.000000000 -0500 3 +++ openssl-1.0.0/apps/s_client.c 2010-04-21 14:39:49.000000000 -0400 4 @@ -248,6 +248,7 @@ static void sc_usage(void) 5 BIO_printf(bio_err," -tlsextdebug - hex dump of all TLS extensions received\n"); 6 BIO_printf(bio_err," -status - request certificate status from server\n"); 7 BIO_printf(bio_err," -no_ticket - disable use of RFC4507bis session tickets\n"); 8 + BIO_printf(bio_err," -cutthrough - enable 1-RTT full-handshake for strong ciphers\n"); 9 #endif 10 } 11 12 @@ -304,6 +305,7 @@ int MAIN(int argc, char **argv) 13 EVP_PKEY *key = NULL; 14 char *CApath=NULL,*CAfile=NULL,*cipher=NULL; 15 int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0; 16 + int cutthrough=0; 17 int crlf=0; 18 int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending; 19 SSL_CTX *ctx=NULL; 20 @@ -533,6 +535,8 @@ int MAIN(int argc, char **argv) 21 else if (strcmp(*argv,"-no_ticket") == 0) 22 { off|=SSL_OP_NO_TICKET; } 23 #endif 24 + else if (strcmp(*argv,"-cutthrough") == 0) 25 + cutthrough=1; 26 else if (strcmp(*argv,"-serverpref") == 0) 27 off|=SSL_OP_CIPHER_SERVER_PREFERENCE; 28 else if (strcmp(*argv,"-cipher") == 0) 29 @@ -714,6 +718,15 @@ bad: 30 */ 31 if (sock_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1); 32 33 + /* Enable handshake cutthrough for client connections using 34 + * strong ciphers. */ 35 + if (cutthrough) 36 + { 37 + int ssl_mode = SSL_CTX_get_mode(ctx); 38 + ssl_mode |= SSL_MODE_HANDSHAKE_CUTTHROUGH; 39 + SSL_CTX_set_mode(ctx, ssl_mode); 40 + } 41 + 42 if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback); 43 if (cipher != NULL) 44 if(!SSL_CTX_set_cipher_list(ctx,cipher)) { 45 diff -uarp openssl-1.0.0.orig/ssl/s3_clnt.c openssl-1.0.0/ssl/s3_clnt.c 46 --- openssl-1.0.0.orig/ssl/s3_clnt.c 2010-02-27 19:24:24.000000000 -0500 47 +++ openssl-1.0.0/ssl/s3_clnt.c 2010-04-21 14:39:49.000000000 -0400 48 @@ -186,6 +186,18 @@ int ssl3_connect(SSL *s) 49 50 s->in_handshake++; 51 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 52 +#if 0 /* Send app data in separate packet, otherwise, some particular site 53 + * (only one site so far) closes the socket. 54 + * Note: there is a very small chance that two TCP packets 55 + * could be arriving at server combined into a single TCP packet, 56 + * then trigger that site to break. We haven't encounter that though. 57 + */ 58 + if (SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) 59 + { 60 + /* Send app data along with CCS/Finished */ 61 + s->s3->flags |= SSL3_FLAGS_DELAY_CLIENT_FINISHED; 62 + } 63 +#endif 64 65 for (;;) 66 { 67 @@ -454,14 +468,31 @@ int ssl3_connect(SSL *s) 68 } 69 else 70 { 71 -#ifndef OPENSSL_NO_TLSEXT 72 - /* Allow NewSessionTicket if ticket expected */ 73 - if (s->tlsext_ticket_expected) 74 - s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A; 75 + if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && SSL_get_cipher_bits(s, NULL) >= 128 76 + && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */ 77 + ) 78 + { 79 + if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) 80 + { 81 + s->state=SSL3_ST_CUTTHROUGH_COMPLETE; 82 + s->s3->flags|=SSL3_FLAGS_POP_BUFFER; 83 + s->s3->delay_buf_pop_ret=0; 84 + } 85 + else 86 + { 87 + s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE; 88 + } 89 + } 90 else 91 + { 92 +#ifndef OPENSSL_NO_TLSEXT 93 + /* Allow NewSessionTicket if ticket expected */ 94 + if (s->tlsext_ticket_expected) 95 + s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A; 96 + else 97 #endif 98 - 99 - s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; 100 + s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; 101 + } 102 } 103 s->init_num=0; 104 break; 105 @@ -512,6 +541,24 @@ int ssl3_connect(SSL *s) 106 s->state=s->s3->tmp.next_state; 107 break; 108 109 + case SSL3_ST_CUTTHROUGH_COMPLETE: 110 +#ifndef OPENSSL_NO_TLSEXT 111 + /* Allow NewSessionTicket if ticket expected */ 112 + if (s->tlsext_ticket_expected) 113 + s->state=SSL3_ST_CR_SESSION_TICKET_A; 114 + else 115 +#endif 116 + s->state=SSL3_ST_CR_FINISHED_A; 117 + 118 + /* SSL_write() will take care of flushing buffered data if 119 + * DELAY_CLIENT_FINISHED is set. 120 + */ 121 + if (!(s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)) 122 + ssl_free_wbio_buffer(s); 123 + ret = 1; 124 + goto end; 125 + /* break; */ 126 + 127 case SSL_ST_OK: 128 /* clean a few things up */ 129 ssl3_cleanup_key_block(s); 130 diff -uarp openssl-1.0.0.orig/ssl/s3_lib.c openssl-1.0.0/ssl/s3_lib.c 131 -- openssl-1.0.0.orig/ssl/s3_lib.c 2009-10-16 11:24:19.000000000 -0400 132 +++ openssl-1.0.0/ssl/s3_lib.c 2010-04-21 14:39:49.000000000 -0400 133 @@ -2551,9 +2551,22 @@ int ssl3_write(SSL *s, const void *buf, 134 135 static int ssl3_read_internal(SSL *s, void *buf, int len, int peek) 136 { 137 - int ret; 138 + int n,ret; 139 140 clear_sys_error(); 141 + if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio)) 142 + { 143 + /* Deal with an application that calls SSL_read() when handshake data 144 + * is yet to be written. 145 + */ 146 + if (BIO_wpending(s->wbio) > 0) 147 + { 148 + s->rwstate=SSL_WRITING; 149 + n=BIO_flush(s->wbio); 150 + if (n <= 0) return(n); 151 + s->rwstate=SSL_NOTHING; 152 + } 153 + } 154 if (s->s3->renegotiate) ssl3_renegotiate_check(s); 155 s->s3->in_read_app_data=1; 156 ret=s->method->ssl_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek); 157 diff -uarp openssl-1.0.0.orig/ssl/ssl.h openssl-1.0.0/ssl/ssl.h 158 --- openssl-1.0.0.orig/ssl/ssl.h 2010-01-06 12:37:38.000000000 -0500 159 +++ openssl-1.0.0/ssl/ssl.h 2010-04-21 16:57:49.000000000 -0400 160 @@ -605,6 +605,10 @@ typedef struct ssl_session_st 161 /* Use small read and write buffers: (a) lazy allocate read buffers for 162 * large incoming records, and (b) limit the size of outgoing records. */ 163 #define SSL_MODE_SMALL_BUFFERS 0x00000020L 164 +/* When set, clients may send application data before receipt of CCS 165 + * and Finished. This mode enables full-handshakes to 'complete' in 166 + * one RTT. */ 167 +#define SSL_MODE_HANDSHAKE_CUTTHROUGH 0x00000040L 168 169 /* Note: SSL[_CTX]_set_{options,mode} use |= op on the previous value, 170 * they cannot be used to clear bits. */ 171 @@ -1097,10 +1101,12 @@ extern "C" { 172 /* Is the SSL_connection established? */ 173 #define SSL_get_state(a) SSL_state(a) 174 #define SSL_is_init_finished(a) (SSL_state(a) == SSL_ST_OK) 175 -#define SSL_in_init(a) (SSL_state(a)&SSL_ST_INIT) 176 +#define SSL_in_init(a) ((SSL_state(a)&SSL_ST_INIT) && \ 177 + !SSL_cutthrough_complete(a)) 178 #define SSL_in_before(a) (SSL_state(a)&SSL_ST_BEFORE) 179 #define SSL_in_connect_init(a) (SSL_state(a)&SSL_ST_CONNECT) 180 #define SSL_in_accept_init(a) (SSL_state(a)&SSL_ST_ACCEPT) 181 +int SSL_cutthrough_complete(const SSL *s); 182 183 /* The following 2 states are kept in ssl->rstate when reads fail, 184 * you should not need these */ 185 Only in openssl-1.0.0/ssl: ssl.h.orig 186 diff -uarp openssl-1.0.0.orig/ssl/ssl3.h openssl-1.0.0/ssl/ssl3.h 187 -- openssl-1.0.0.orig/ssl/ssl3.h 2010-01-06 12:37:38.000000000 -0500 188 +++ openssl-1.0.0/ssl/ssl3.h 2010-04-21 14:39:49.000000000 -0400 189 @@ -456,6 +456,7 @@ typedef struct ssl3_state_st 190 /*client */ 191 /* extra state */ 192 #define SSL3_ST_CW_FLUSH (0x100|SSL_ST_CONNECT) 193 +#define SSL3_ST_CUTTHROUGH_COMPLETE (0x101|SSL_ST_CONNECT) 194 /* write to server */ 195 #define SSL3_ST_CW_CLNT_HELLO_A (0x110|SSL_ST_CONNECT) 196 #define SSL3_ST_CW_CLNT_HELLO_B (0x111|SSL_ST_CONNECT) 197 diff -uarp openssl-1.0.0.orig/ssl/ssl_lib.c openssl-1.0.0/ssl/ssl_lib.c 198 --- openssl-1.0.0.orig/ssl/ssl_lib.c 2010-02-17 14:43:46.000000000 -0500 199 +++ openssl-1.0.0/ssl/ssl_lib.c 2010-04-21 17:02:45.000000000 -0400 200 @@ -3031,6 +3031,19 @@ void SSL_set_msg_callback(SSL *ssl, void 201 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 202 } 203 204 +int SSL_cutthrough_complete(const SSL *s) 205 + { 206 + return (!s->server && /* cutthrough only applies to clients */ 207 + !s->hit && /* full-handshake */ 208 + s->version >= SSL3_VERSION && 209 + s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */ 210 + (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */ 211 + SSL_get_cipher_bits(s, NULL) >= 128 && /* strong cipher choosen */ 212 + s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */ 213 + (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/ 214 + s->state == SSL3_ST_CR_FINISHED_A)); 215 + } 216 + 217 /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer 218 * vairable, freeing EVP_MD_CTX previously stored in that variable, if 219 * any. If EVP_MD pointer is passed, initializes ctx with this md 220 diff -uarp openssl-1.0.0.orig/ssl/ssltest.c openssl-1.0.0/ssl/ssltest.c 221 --- openssl-1.0.0.orig/ssl/ssltest.c 2010-01-24 11:57:38.000000000 -0500 222 +++ openssl-1.0.0/ssl/ssltest.c 2010-04-21 17:06:35.000000000 -0400 223 @@ -279,6 +279,7 @@ static void sv_usage(void) 224 fprintf(stderr," -test_cipherlist - verifies the order of the ssl cipher lists\n"); 225 fprintf(stderr," -c_small_records - enable client side use of small SSL record buffers\n"); 226 fprintf(stderr," -s_small_records - enable server side use of small SSL record buffers\n"); 227 + fprintf(stderr," -cutthrough - enable 1-RTT full-handshake for strong ciphers\n"); 228 } 229 230 static void print_details(SSL *c_ssl, const char *prefix) 231 @@ -436,6 +437,7 @@ int main(int argc, char *argv[]) 232 int ssl_mode = 0; 233 int c_small_records=0; 234 int s_small_records=0; 235 + int cutthrough = 0; 236 237 verbose = 0; 238 debug = 0; 239 @@ -632,6 +634,10 @@ int main(int argc, char *argv[]) 240 { 241 s_small_records = 1; 242 } 243 + else if (strcmp(*argv, "-cutthrough") == 0) 244 + { 245 + cutthrough = 1; 246 + } 247 else 248 { 249 fprintf(stderr,"unknown option %s\n",*argv); 250 @@ -782,6 +788,13 @@ bad: 251 ssl_mode |= SSL_MODE_SMALL_BUFFERS; 252 SSL_CTX_set_mode(s_ctx, ssl_mode); 253 } 254 + ssl_mode = 0; 255 + if (cutthrough) 256 + { 257 + ssl_mode = SSL_CTX_get_mode(c_ctx); 258 + ssl_mode = SSL_MODE_HANDSHAKE_CUTTHROUGH; 259 + SSL_CTX_set_mode(c_ctx, ssl_mode); 260 + } 261 262 #ifndef OPENSSL_NO_DH 263 if (!no_dhe) 264 diff -uarp openssl-1.0.0.orig/test/testssl openssl-1.0.0/test/testssl 265 --- openssl-1.0.0.orig/test/testssl 2006-03-10 18:06:27.000000000 -0500 266 +++ openssl-1.0.0/test/testssl 2010-04-21 16:50:13.000000000 -0400 267 @@ -79,6 +79,8 @@ $ssltest -server_auth -client_auth -s_sm 268 echo test sslv2/sslv3 with both client and server authentication and small client and server buffers 269 $ssltest -server_auth -client_auth -c_small_records -s_small_records $CA $extra || exit 1 270 271 +echo test sslv2/sslv3 with both client and server authentication and handshake cutthrough 272 +$ssltest -server_auth -client_auth -cutthrough $CA $extra || exit 1 273 274 echo test sslv2 via BIO pair 275 $ssltest -bio_pair -ssl2 $extra || exit 1 276