1 commit 15a86b921a7eaa190a22a0a369e4e99beb91f5ad 2 Author: Adam Langley <agl (a] chromium.org> 3 Date: Mon Mar 26 17:43:29 2012 -0400 4 5 handshake_cutthrough 6 7 diff --git a/apps/s_client.c b/apps/s_client.c 8 index 098cce2..0e10766 100644 9 --- a/apps/s_client.c 10 +++ b/apps/s_client.c 11 @@ -361,6 +361,7 @@ static void sc_usage(void) 12 BIO_printf(bio_err," -nextprotoneg arg - enable NPN extension, considering named protocols supported (comma-separated list)\n"); 13 # endif 14 #endif 15 + BIO_printf(bio_err," -cutthrough - enable 1-RTT full-handshake for strong ciphers\n"); 16 BIO_printf(bio_err," -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n"); 17 BIO_printf(bio_err," -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n"); 18 BIO_printf(bio_err," -keymatexport label - Export keying material using label\n"); 19 @@ -573,6 +574,7 @@ int MAIN(int argc, char **argv) 20 EVP_PKEY *key = NULL; 21 char *CApath=NULL,*CAfile=NULL,*cipher=NULL; 22 int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0; 23 + int cutthrough=0; 24 int crlf=0; 25 int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending; 26 SSL_CTX *ctx=NULL; 27 @@ -885,6 +887,8 @@ int MAIN(int argc, char **argv) 28 } 29 # endif 30 #endif 31 + else if (strcmp(*argv,"-cutthrough") == 0) 32 + cutthrough=1; 33 else if (strcmp(*argv,"-serverpref") == 0) 34 off|=SSL_OP_CIPHER_SERVER_PREFERENCE; 35 else if (strcmp(*argv,"-legacy_renegotiation") == 0) 36 @@ -1156,6 +1160,15 @@ bad: 37 SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto); 38 #endif 39 40 + /* Enable handshake cutthrough for client connections using 41 + * strong ciphers. */ 42 + if (cutthrough) 43 + { 44 + int ssl_mode = SSL_CTX_get_mode(ctx); 45 + ssl_mode |= SSL_MODE_HANDSHAKE_CUTTHROUGH; 46 + SSL_CTX_set_mode(ctx, ssl_mode); 47 + } 48 + 49 if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback); 50 if (cipher != NULL) 51 if(!SSL_CTX_set_cipher_list(ctx,cipher)) { 52 diff --git a/ssl/s3_clnt.c b/ssl/s3_clnt.c 53 index 4511a91..d2ecc3e 100644 54 --- a/ssl/s3_clnt.c 55 +++ b/ssl/s3_clnt.c 56 @@ -215,6 +215,24 @@ int ssl3_connect(SSL *s) 57 } 58 #endif 59 60 +// BEGIN android-added 61 +#if 0 62 +/* Send app data in separate packet, otherwise, some particular site 63 + * (only one site so far) closes the socket. http://b/2511073 64 + * Note: there is a very small chance that two TCP packets 65 + * could be arriving at server combined into a single TCP packet, 66 + * then trigger that site to break. We haven't encounter that though. 67 + */ 68 +// END android-added 69 + if (SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) 70 + { 71 + /* Send app data along with CCS/Finished */ 72 + s->s3->flags |= SSL3_FLAGS_DELAY_CLIENT_FINISHED; 73 + } 74 + 75 +// BEGIN android-added 76 +#endif 77 +// END android-added 78 for (;;) 79 { 80 state=s->state; 81 @@ -527,14 +533,31 @@ int ssl3_connect(SSL *s) 82 } 83 else 84 { 85 -#ifndef OPENSSL_NO_TLSEXT 86 - /* Allow NewSessionTicket if ticket expected */ 87 - if (s->tlsext_ticket_expected) 88 - s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A; 89 + if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && SSL_get_cipher_bits(s, NULL) >= 128 90 + && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */ 91 + ) 92 + { 93 + if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) 94 + { 95 + s->state=SSL3_ST_CUTTHROUGH_COMPLETE; 96 + s->s3->flags|=SSL3_FLAGS_POP_BUFFER; 97 + s->s3->delay_buf_pop_ret=0; 98 + } 99 + else 100 + { 101 + s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE; 102 + } 103 + } 104 else 105 + { 106 +#ifndef OPENSSL_NO_TLSEXT 107 + /* Allow NewSessionTicket if ticket expected */ 108 + if (s->tlsext_ticket_expected) 109 + s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A; 110 + else 111 #endif 112 - 113 - s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; 114 + s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; 115 + } 116 } 117 s->init_num=0; 118 break; 119 @@ -582,6 +605,24 @@ int ssl3_connect(SSL *s) 120 s->state=s->s3->tmp.next_state; 121 break; 122 123 + case SSL3_ST_CUTTHROUGH_COMPLETE: 124 +#ifndef OPENSSL_NO_TLSEXT 125 + /* Allow NewSessionTicket if ticket expected */ 126 + if (s->tlsext_ticket_expected) 127 + s->state=SSL3_ST_CR_SESSION_TICKET_A; 128 + else 129 +#endif 130 + s->state=SSL3_ST_CR_FINISHED_A; 131 + 132 + /* SSL_write() will take care of flushing buffered data if 133 + * DELAY_CLIENT_FINISHED is set. 134 + */ 135 + if (!(s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)) 136 + ssl_free_wbio_buffer(s); 137 + ret = 1; 138 + goto end; 139 + /* break; */ 140 + 141 case SSL_ST_OK: 142 /* clean a few things up */ 143 ssl3_cleanup_key_block(s); 144 diff --git a/ssl/s3_lib.c b/ssl/s3_lib.c 145 index db75479..a2ea25a 100644 146 --- a/ssl/s3_lib.c 147 +++ b/ssl/s3_lib.c 148 @@ -4199,9 +4199,22 @@ int ssl3_write(SSL *s, const void *buf, int len) 149 150 static int ssl3_read_internal(SSL *s, void *buf, int len, int peek) 151 { 152 - int ret; 153 + int n,ret; 154 155 clear_sys_error(); 156 + if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio)) 157 + { 158 + /* Deal with an application that calls SSL_read() when handshake data 159 + * is yet to be written. 160 + */ 161 + if (BIO_wpending(s->wbio) > 0) 162 + { 163 + s->rwstate=SSL_WRITING; 164 + n=BIO_flush(s->wbio); 165 + if (n <= 0) return(n); 166 + s->rwstate=SSL_NOTHING; 167 + } 168 + } 169 if (s->s3->renegotiate) ssl3_renegotiate_check(s); 170 s->s3->in_read_app_data=1; 171 ret=s->method->ssl_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek); 172 diff --git a/ssl/ssl.h b/ssl/ssl.h 173 index 72ed766..ceaf647 100644 174 --- a/ssl/ssl.h 175 +++ b/ssl/ssl.h 176 @@ -638,6 +638,10 @@ struct ssl_session_st 177 * TLS only.) "Released" buffers are put onto a free-list in the context 178 * or just freed (depending on the context's setting for freelist_max_len). */ 179 #define SSL_MODE_RELEASE_BUFFERS 0x00000010L 180 +/* When set, clients may send application data before receipt of CCS 181 + * and Finished. This mode enables full-handshakes to 'complete' in 182 + * one RTT. */ 183 +#define SSL_MODE_HANDSHAKE_CUTTHROUGH 0x00000020L 184 185 /* Note: SSL[_CTX]_set_{options,mode} use |= op on the previous value, 186 * they cannot be used to clear bits. */ 187 @@ -1410,10 +1414,12 @@ extern "C" { 188 /* Is the SSL_connection established? */ 189 #define SSL_get_state(a) SSL_state(a) 190 #define SSL_is_init_finished(a) (SSL_state(a) == SSL_ST_OK) 191 -#define SSL_in_init(a) (SSL_state(a)&SSL_ST_INIT) 192 +#define SSL_in_init(a) ((SSL_state(a)&SSL_ST_INIT) && \ 193 + !SSL_cutthrough_complete(a)) 194 #define SSL_in_before(a) (SSL_state(a)&SSL_ST_BEFORE) 195 #define SSL_in_connect_init(a) (SSL_state(a)&SSL_ST_CONNECT) 196 #define SSL_in_accept_init(a) (SSL_state(a)&SSL_ST_ACCEPT) 197 +int SSL_cutthrough_complete(const SSL *s); 198 199 /* The following 2 states are kept in ssl->rstate when reads fail, 200 * you should not need these */ 201 diff --git a/ssl/ssl3.h b/ssl/ssl3.h 202 index 112e627..556ffc1 100644 203 --- a/ssl/ssl3.h 204 +++ b/ssl/ssl3.h 205 @@ -547,6 +547,7 @@ typedef struct ssl3_state_st 206 /*client */ 207 /* extra state */ 208 #define SSL3_ST_CW_FLUSH (0x100|SSL_ST_CONNECT) 209 +#define SSL3_ST_CUTTHROUGH_COMPLETE (0x101|SSL_ST_CONNECT) 210 #ifndef OPENSSL_NO_SCTP 211 #define DTLS1_SCTP_ST_CW_WRITE_SOCK (0x310|SSL_ST_CONNECT) 212 #define DTLS1_SCTP_ST_CR_READ_SOCK (0x320|SSL_ST_CONNECT) 213 diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c 214 index f82d071..518f152 100644 215 --- a/ssl/ssl_lib.c 216 +++ b/ssl/ssl_lib.c 217 @@ -3211,6 +3211,19 @@ void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int con 218 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 219 } 220 221 +int SSL_cutthrough_complete(const SSL *s) 222 + { 223 + return (!s->server && /* cutthrough only applies to clients */ 224 + !s->hit && /* full-handshake */ 225 + s->version >= SSL3_VERSION && 226 + s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */ 227 + (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */ 228 + SSL_get_cipher_bits(s, NULL) >= 128 && /* strong cipher choosen */ 229 + s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */ 230 + (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/ 231 + s->state == SSL3_ST_CR_FINISHED_A)); 232 + } 233 + 234 /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer 235 * vairable, freeing EVP_MD_CTX previously stored in that variable, if 236 * any. If EVP_MD pointer is passed, initializes ctx with this md 237 diff --git a/ssl/ssltest.c b/ssl/ssltest.c 238 index 0f8fd39..02ce4ec 100644 239 --- a/ssl/ssltest.c 240 +++ b/ssl/ssltest.c 241 @@ -369,6 +369,7 @@ static void sv_usage(void) 242 " (default is sect163r2).\n"); 243 #endif 244 fprintf(stderr," -test_cipherlist - verifies the order of the ssl cipher lists\n"); 245 + fprintf(stderr," -cutthrough - enable 1-RTT full-handshake for strong ciphers\n"); 246 } 247 248 static void print_details(SSL *c_ssl, const char *prefix) 249 @@ -549,6 +550,7 @@ int main(int argc, char *argv[]) 250 #ifdef OPENSSL_FIPS 251 int fips_mode=0; 252 #endif 253 + int cutthrough = 0; 254 255 verbose = 0; 256 debug = 0; 257 @@ -765,6 +767,10 @@ int main(int argc, char *argv[]) 258 { 259 test_cipherlist = 1; 260 } 261 + else if (strcmp(*argv, "-cutthrough") == 0) 262 + { 263 + cutthrough = 1; 264 + } 265 else 266 { 267 fprintf(stderr,"unknown option %s\n",*argv); 268 @@ -900,6 +906,12 @@ bad: 269 SSL_CTX_set_cipher_list(c_ctx,cipher); 270 SSL_CTX_set_cipher_list(s_ctx,cipher); 271 } 272 + if (cutthrough) 273 + { 274 + int ssl_mode = SSL_CTX_get_mode(c_ctx); 275 + ssl_mode |= SSL_MODE_HANDSHAKE_CUTTHROUGH; 276 + SSL_CTX_set_mode(c_ctx, ssl_mode); 277 + } 278 279 #ifndef OPENSSL_NO_DH 280 if (!no_dhe) 281 diff --git a/test/testssl b/test/testssl 282 index 5ae4dc8..5dfeeeb 100644 283 --- a/test/testssl 284 +++ b/test/testssl 285 @@ -70,6 +70,9 @@ $ssltest -client_auth $CA $extra || exit 1 286 echo test sslv2/sslv3 with both client and server authentication 287 $ssltest -server_auth -client_auth $CA $extra || exit 1 288 289 +echo test sslv2/sslv3 with both client and server authentication and handshake cutthrough 290 +$ssltest -server_auth -client_auth -cutthrough $CA $extra || exit 1 291 + 292 echo test sslv2 via BIO pair 293 $ssltest -bio_pair -ssl2 $extra || exit 1 294 295