1 From 4c654523c703645f8b517389b6da537c5a9e5168 Mon Sep 17 00:00:00 2001 2 From: Adam Langley <agl (a] chromium.org> 3 Date: Thu, 24 Jan 2013 16:22:07 -0500 4 Subject: [PATCH] handshake_cutthrough 5 6 Enables SSL3+ clients to send application data immediately following the 7 Finished message even when negotiating full-handshakes. With this 8 patch, clients can negotiate SSL connections in 1-RTT even when 9 performing full-handshakes. 10 --- 11 apps/s_client.c | 13 +++++++++++++ 12 ssl/s3_clnt.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++------ 13 ssl/s3_lib.c | 15 ++++++++++++++- 14 ssl/ssl.h | 10 +++++++++- 15 ssl/ssl3.h | 1 + 16 ssl/ssl_lib.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 17 ssl/ssl_locl.h | 2 ++ 18 ssl/ssltest.c | 12 ++++++++++++ 19 test/testssl | 3 +++ 20 9 files changed, 144 insertions(+), 8 deletions(-) 21 22 diff --git a/apps/s_client.c b/apps/s_client.c 23 index 3ba6605..791e277 100644 24 --- a/apps/s_client.c 25 +++ b/apps/s_client.c 26 @@ -361,6 +361,7 @@ static void sc_usage(void) 27 BIO_printf(bio_err," -nextprotoneg arg - enable NPN extension, considering named protocols supported (comma-separated list)\n"); 28 # endif 29 #endif 30 + BIO_printf(bio_err," -cutthrough - enable 1-RTT full-handshake for strong ciphers\n"); 31 BIO_printf(bio_err," -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n"); 32 #ifndef OPENSSL_NO_SRTP 33 BIO_printf(bio_err," -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n"); 34 @@ -577,6 +578,7 @@ int MAIN(int argc, char **argv) 35 EVP_PKEY *key = NULL; 36 char *CApath=NULL,*CAfile=NULL,*cipher=NULL; 37 int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0; 38 + int cutthrough=0; 39 int crlf=0; 40 int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending; 41 SSL_CTX *ctx=NULL; 42 @@ -883,6 +885,8 @@ int MAIN(int argc, char **argv) 43 } 44 # endif 45 #endif 46 + else if (strcmp(*argv,"-cutthrough") == 0) 47 + cutthrough=1; 48 else if (strcmp(*argv,"-serverpref") == 0) 49 off|=SSL_OP_CIPHER_SERVER_PREFERENCE; 50 else if (strcmp(*argv,"-legacy_renegotiation") == 0) 51 @@ -1158,6 +1162,15 @@ bad: 52 SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto); 53 #endif 54 55 + /* Enable handshake cutthrough for client connections using 56 + * strong ciphers. */ 57 + if (cutthrough) 58 + { 59 + int ssl_mode = SSL_CTX_get_mode(ctx); 60 + ssl_mode |= SSL_MODE_HANDSHAKE_CUTTHROUGH; 61 + SSL_CTX_set_mode(ctx, ssl_mode); 62 + } 63 + 64 if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback); 65 if (cipher != NULL) 66 if(!SSL_CTX_set_cipher_list(ctx,cipher)) { 67 diff --git a/ssl/s3_clnt.c b/ssl/s3_clnt.c 68 index a6b3c01..3d3fd64 100644 69 --- a/ssl/s3_clnt.c 70 +++ b/ssl/s3_clnt.c 71 @@ -215,6 +215,12 @@ int ssl3_connect(SSL *s) 72 } 73 #endif 74 75 + if (SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) 76 + { 77 + /* Send app data along with CCS/Finished */ 78 + s->s3->flags |= SSL3_FLAGS_DELAY_CLIENT_FINISHED; 79 + } 80 + 81 for (;;) 82 { 83 state=s->state; 84 @@ -526,14 +532,32 @@ int ssl3_connect(SSL *s) 85 } 86 else 87 { 88 -#ifndef OPENSSL_NO_TLSEXT 89 - /* Allow NewSessionTicket if ticket expected */ 90 - if (s->tlsext_ticket_expected) 91 - s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A; 92 + if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) 93 + && ssl3_can_cutthrough(s) 94 + && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */ 95 + ) 96 + { 97 + if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) 98 + { 99 + s->state=SSL3_ST_CUTTHROUGH_COMPLETE; 100 + s->s3->flags|=SSL3_FLAGS_POP_BUFFER; 101 + s->s3->delay_buf_pop_ret=0; 102 + } 103 + else 104 + { 105 + s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE; 106 + } 107 + } 108 else 109 + { 110 +#ifndef OPENSSL_NO_TLSEXT 111 + /* Allow NewSessionTicket if ticket expected */ 112 + if (s->tlsext_ticket_expected) 113 + s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A; 114 + else 115 #endif 116 - 117 - s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; 118 + s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; 119 + } 120 } 121 s->init_num=0; 122 break; 123 @@ -581,6 +605,24 @@ int ssl3_connect(SSL *s) 124 s->state=s->s3->tmp.next_state; 125 break; 126 127 + case SSL3_ST_CUTTHROUGH_COMPLETE: 128 +#ifndef OPENSSL_NO_TLSEXT 129 + /* Allow NewSessionTicket if ticket expected */ 130 + if (s->tlsext_ticket_expected) 131 + s->state=SSL3_ST_CR_SESSION_TICKET_A; 132 + else 133 +#endif 134 + s->state=SSL3_ST_CR_FINISHED_A; 135 + 136 + /* SSL_write() will take care of flushing buffered data if 137 + * DELAY_CLIENT_FINISHED is set. 138 + */ 139 + if (!(s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)) 140 + ssl_free_wbio_buffer(s); 141 + ret = 1; 142 + goto end; 143 + /* break; */ 144 + 145 case SSL_ST_OK: 146 /* clean a few things up */ 147 ssl3_cleanup_key_block(s); 148 diff --git a/ssl/s3_lib.c b/ssl/s3_lib.c 149 index c4ef273..1865c70 100644 150 --- a/ssl/s3_lib.c 151 +++ b/ssl/s3_lib.c 152 @@ -4211,9 +4211,22 @@ int ssl3_write(SSL *s, const void *buf, int len) 153 154 static int ssl3_read_internal(SSL *s, void *buf, int len, int peek) 155 { 156 - int ret; 157 + int n,ret; 158 159 clear_sys_error(); 160 + if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio)) 161 + { 162 + /* Deal with an application that calls SSL_read() when handshake data 163 + * is yet to be written. 164 + */ 165 + if (BIO_wpending(s->wbio) > 0) 166 + { 167 + s->rwstate=SSL_WRITING; 168 + n=BIO_flush(s->wbio); 169 + if (n <= 0) return(n); 170 + s->rwstate=SSL_NOTHING; 171 + } 172 + } 173 if (s->s3->renegotiate) ssl3_renegotiate_check(s); 174 s->s3->in_read_app_data=1; 175 ret=s->method->ssl_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len,peek); 176 diff --git a/ssl/ssl.h b/ssl/ssl.h 177 index 1f255c3..3e31fb5 100644 178 --- a/ssl/ssl.h 179 +++ b/ssl/ssl.h 180 @@ -644,6 +644,7 @@ struct ssl_session_st 181 * TLS only.) "Released" buffers are put onto a free-list in the context 182 * or just freed (depending on the context's setting for freelist_max_len). */ 183 #define SSL_MODE_RELEASE_BUFFERS 0x00000010L 184 + 185 /* Send the current time in the Random fields of the ClientHello and 186 * ServerHello records for compatibility with hypothetical implementations 187 * that require it. 188 @@ -651,6 +652,11 @@ struct ssl_session_st 189 #define SSL_MODE_SEND_CLIENTHELLO_TIME 0x00000020L 190 #define SSL_MODE_SEND_SERVERHELLO_TIME 0x00000040L 191 192 +/* When set, clients may send application data before receipt of CCS 193 + * and Finished. This mode enables full-handshakes to 'complete' in 194 + * one RTT. */ 195 +#define SSL_MODE_HANDSHAKE_CUTTHROUGH 0x00000200L 196 + 197 /* Note: SSL[_CTX]_set_{options,mode} use |= op on the previous value, 198 * they cannot be used to clear bits. */ 199 200 @@ -1424,10 +1430,12 @@ extern "C" { 201 /* Is the SSL_connection established? */ 202 #define SSL_get_state(a) SSL_state(a) 203 #define SSL_is_init_finished(a) (SSL_state(a) == SSL_ST_OK) 204 -#define SSL_in_init(a) (SSL_state(a)&SSL_ST_INIT) 205 +#define SSL_in_init(a) ((SSL_state(a)&SSL_ST_INIT) && \ 206 + !SSL_cutthrough_complete(a)) 207 #define SSL_in_before(a) (SSL_state(a)&SSL_ST_BEFORE) 208 #define SSL_in_connect_init(a) (SSL_state(a)&SSL_ST_CONNECT) 209 #define SSL_in_accept_init(a) (SSL_state(a)&SSL_ST_ACCEPT) 210 +int SSL_cutthrough_complete(const SSL *s); 211 212 /* The following 2 states are kept in ssl->rstate when reads fail, 213 * you should not need these */ 214 diff --git a/ssl/ssl3.h b/ssl/ssl3.h 215 index cb8b249..9a61b71 100644 216 --- a/ssl/ssl3.h 217 +++ b/ssl/ssl3.h 218 @@ -556,6 +556,7 @@ typedef struct ssl3_state_st 219 /*client */ 220 /* extra state */ 221 #define SSL3_ST_CW_FLUSH (0x100|SSL_ST_CONNECT) 222 +#define SSL3_ST_CUTTHROUGH_COMPLETE (0x101|SSL_ST_CONNECT) 223 #ifndef OPENSSL_NO_SCTP 224 #define DTLS1_SCTP_ST_CW_WRITE_SOCK (0x310|SSL_ST_CONNECT) 225 #define DTLS1_SCTP_ST_CR_READ_SOCK (0x320|SSL_ST_CONNECT) 226 diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c 227 index 6dbc3c1..7892928 100644 228 --- a/ssl/ssl_lib.c 229 +++ b/ssl/ssl_lib.c 230 @@ -3225,6 +3225,48 @@ void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int con 231 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); 232 } 233 234 +int SSL_cutthrough_complete(const SSL *s) 235 + { 236 + return (!s->server && /* cutthrough only applies to clients */ 237 + !s->hit && /* full-handshake */ 238 + s->version >= SSL3_VERSION && 239 + s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */ 240 + (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */ 241 + ssl3_can_cutthrough(s) && /* cutthrough allowed */ 242 + s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */ 243 + (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/ 244 + s->state == SSL3_ST_CR_FINISHED_A)); 245 + } 246 + 247 +int ssl3_can_cutthrough(const SSL *s) 248 + { 249 + const SSL_CIPHER *c; 250 + 251 + /* require a strong enough cipher */ 252 + if (SSL_get_cipher_bits(s, NULL) < 128) 253 + return 0; 254 + 255 + /* require ALPN or NPN extension */ 256 + if (!s->s3->alpn_selected 257 +#ifndef OPENSSL_NO_NEXTPROTONEG 258 + && !s->s3->next_proto_neg_seen 259 +#endif 260 + ) 261 + { 262 + return 0; 263 + } 264 + 265 + /* require a forward-secret cipher */ 266 + c = SSL_get_current_cipher(s); 267 + if (!c || (c->algorithm_mkey != SSL_kEDH && 268 + c->algorithm_mkey != SSL_kEECDH)) 269 + { 270 + return 0; 271 + } 272 + 273 + return 1; 274 + } 275 + 276 /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer 277 * vairable, freeing EVP_MD_CTX previously stored in that variable, if 278 * any. If EVP_MD pointer is passed, initializes ctx with this md 279 diff --git a/ssl/ssl_locl.h b/ssl/ssl_locl.h 280 index e485907..3b1d644 100644 281 --- a/ssl/ssl_locl.h 282 +++ b/ssl/ssl_locl.h 283 @@ -1126,6 +1126,8 @@ int tls12_get_sigid(const EVP_PKEY *pk); 284 const EVP_MD *tls12_get_hash(unsigned char hash_alg); 285 286 #endif 287 + 288 +int ssl3_can_cutthrough(const SSL *s); 289 EVP_MD_CTX* ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md) ; 290 void ssl_clear_hash_ctx(EVP_MD_CTX **hash); 291 int ssl_add_serverhello_renegotiate_ext(SSL *s, unsigned char *p, int *len, 292 diff --git a/ssl/ssltest.c b/ssl/ssltest.c 293 index 4f80be8..28fa223 100644 294 --- a/ssl/ssltest.c 295 +++ b/ssl/ssltest.c 296 @@ -369,6 +369,7 @@ static void sv_usage(void) 297 " (default is sect163r2).\n"); 298 #endif 299 fprintf(stderr," -test_cipherlist - verifies the order of the ssl cipher lists\n"); 300 + fprintf(stderr," -cutthrough - enable 1-RTT full-handshake for strong ciphers\n"); 301 } 302 303 static void print_details(SSL *c_ssl, const char *prefix) 304 @@ -549,6 +550,7 @@ int main(int argc, char *argv[]) 305 #ifdef OPENSSL_FIPS 306 int fips_mode=0; 307 #endif 308 + int cutthrough = 0; 309 310 verbose = 0; 311 debug = 0; 312 @@ -765,6 +767,10 @@ int main(int argc, char *argv[]) 313 { 314 test_cipherlist = 1; 315 } 316 + else if (strcmp(*argv, "-cutthrough") == 0) 317 + { 318 + cutthrough = 1; 319 + } 320 else 321 { 322 fprintf(stderr,"unknown option %s\n",*argv); 323 @@ -906,6 +912,12 @@ bad: 324 SSL_CTX_set_cipher_list(c_ctx,cipher); 325 SSL_CTX_set_cipher_list(s_ctx,cipher); 326 } 327 + if (cutthrough) 328 + { 329 + int ssl_mode = SSL_CTX_get_mode(c_ctx); 330 + ssl_mode |= SSL_MODE_HANDSHAKE_CUTTHROUGH; 331 + SSL_CTX_set_mode(c_ctx, ssl_mode); 332 + } 333 334 #ifndef OPENSSL_NO_DH 335 if (!no_dhe) 336 diff --git a/test/testssl b/test/testssl 337 index 4e8542b..b5f90ba 100644 338 --- a/test/testssl 339 +++ b/test/testssl 340 @@ -70,6 +70,9 @@ $ssltest -client_auth $CA $extra || exit 1 341 echo test sslv2/sslv3 with both client and server authentication 342 $ssltest -server_auth -client_auth $CA $extra || exit 1 343 344 +echo test sslv2/sslv3 with both client and server authentication and handshake cutthrough 345 +$ssltest -server_auth -client_auth -cutthrough $CA $extra || exit 1 346 + 347 echo test sslv2 via BIO pair 348 $ssltest -bio_pair -ssl2 $extra || exit 1 349 350 -- 351 1.9.1.423.g4596e3a 352 353