Enable the client to call SSL_read() without stopping the ability to call SSL_write_early()

Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/2737)
diff --git a/ssl/statem/statem.c b/ssl/statem/statem.c
index 2c202f4..a1807f2 100644
--- a/ssl/statem/statem.c
+++ b/ssl/statem/statem.c
@@ -168,6 +168,13 @@
     return 1;
 }
 
+void ossl_statem_check_finish_init(SSL *s, int send)
+{
+    if ((send && s->statem.hand_state == TLS_ST_CW_PENDING_EARLY_DATA_END)
+            || (!send && s->statem.hand_state == TLS_ST_CW_EARLY_DATA))
+        ossl_statem_set_in_init(s, 1);
+}
+
 void ossl_statem_set_hello_verify_done(SSL *s)
 {
     s->statem.state = MSG_FLOW_UNINITED;
diff --git a/ssl/statem/statem.h b/ssl/statem/statem.h
index c0c9cab..56009b0 100644
--- a/ssl/statem/statem.h
+++ b/ssl/statem/statem.h
@@ -123,6 +123,7 @@
 int ossl_statem_get_in_handshake(SSL *s);
 void ossl_statem_set_in_handshake(SSL *s, int inhand);
 __owur int ossl_statem_skip_early_data(SSL *s);
+void ossl_statem_check_finish_init(SSL *s, int send);
 void ossl_statem_set_hello_verify_done(SSL *s);
 __owur int ossl_statem_app_data_allowed(SSL *s);
 #ifndef OPENSSL_NO_SCTP
diff --git a/ssl/statem/statem_clnt.c b/ssl/statem/statem_clnt.c
index 23a4d76..6fdb37e 100644
--- a/ssl/statem/statem_clnt.c
+++ b/ssl/statem/statem_clnt.c
@@ -435,6 +435,14 @@
         return WRITE_TRAN_CONTINUE;
 
     case TLS_ST_CR_FINISHED:
+        if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY)
+            st->hand_state = TLS_ST_CW_PENDING_EARLY_DATA_END;
+        else
+            st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT
+                                                        : TLS_ST_CW_FINISHED;
+        return WRITE_TRAN_CONTINUE;
+
+    case TLS_ST_CW_PENDING_EARLY_DATA_END:
         st->hand_state = (s->s3->tmp.cert_req != 0) ? TLS_ST_CW_CERT
                                                     : TLS_ST_CW_FINISHED;
         return WRITE_TRAN_CONTINUE;
@@ -659,6 +667,7 @@
         break;
 
     case TLS_ST_CW_EARLY_DATA:
+    case TLS_ST_CW_PENDING_EARLY_DATA_END:
     case TLS_ST_OK:
         return tls_finish_handshake(s, wst, 1);
     }