Replace s->first_packet with a s->s3->have_version bit.

first_packet is a temporary connection-global flag set for the duration of some
call and then queried from other code. This kind of logic is too difficult to
reason through. It also incorrectly treats renegotiate ClientHellos as
pre-version-negotiation records. This eliminates the need to query
enc_write_ctx (which wasn't EVP_AEAD-aware anyway).

Instead, take a leaf from Go TLS's book and add a have_version bit. This is
placed on s->s3 as it is connection state; s->s3 automatically gets reset on
SSL_clear while s doesn't.

This new flag will also be used to determine whether to do the V2ClientHello
sniff when the version-locked methods merge into SSLv23_method. It will also
replace needing to condition s->method against a dummy DTLS_ANY_VERSION value
to determine whether DTLS version negotiation has happened yet.

Change-Id: I5c8bc6258b182ba4ab175a48a84eab6d3a001333
Reviewed-by: Adam Langley <>
diff --git a/ssl/s3_clnt.c b/ssl/s3_clnt.c
index 2b39ad1..635606e 100644
--- a/ssl/s3_clnt.c
+++ b/ssl/s3_clnt.c
@@ -777,11 +777,6 @@
 	uint8_t compression_method;
 	unsigned long mask_ssl;
-	/* DTLS_ANY_VERSION does not sniff the version ahead of time,
-	 * so disable the version check. */
-	if (SSL_IS_DTLS(s))
-		s->first_packet = 1;
@@ -790,9 +785,6 @@
-	if (SSL_IS_DTLS(s))
-		s->first_packet = 0;
 	if (!ok) return((int)n);
 	CBS_init(&server_hello, s->init_msg, n);
@@ -837,6 +829,12 @@
 		goto f_err;
+	/* At this point, the connection's version is known and s->version is
+	 * fixed. Begin enforcing the record-layer version. Note: SSLv23_method
+	 * currently determines its version sooner, but it will later be moved
+	 * to this point. */
+	s->s3->have_version = 1;
 	/* Copy over the server random. */
 	memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);