Match state machine functions with new calling convention.

This cuts down on a lot of unchecked ssl->s3->hs accesses. Next is
probably the mass of extensions callbacks, and then we can play
whack-a-mole with git grep.

Change-Id: I81c506ea25c2569a51ceda903853465b8b567b0f
Reviewed-on: https://boringssl-review.googlesource.com/12237
Reviewed-by: Steven Valdez <svaldez@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
diff --git a/ssl/handshake_client.c b/ssl/handshake_client.c
index 8d503a5..c981de0 100644
--- a/ssl/handshake_client.c
+++ b/ssl/handshake_client.c
@@ -170,21 +170,21 @@
 #include "internal.h"
 
 
-static int ssl3_send_client_hello(SSL *ssl);
-static int dtls1_get_hello_verify(SSL *ssl);
-static int ssl3_get_server_hello(SSL *ssl);
-static int ssl3_get_server_certificate(SSL *ssl);
-static int ssl3_get_cert_status(SSL *ssl);
-static int ssl3_verify_server_cert(SSL *ssl);
-static int ssl3_get_server_key_exchange(SSL *ssl);
-static int ssl3_get_certificate_request(SSL *ssl);
-static int ssl3_get_server_hello_done(SSL *ssl);
-static int ssl3_send_client_certificate(SSL *ssl);
-static int ssl3_send_client_key_exchange(SSL *ssl);
-static int ssl3_send_cert_verify(SSL *ssl);
-static int ssl3_send_next_proto(SSL *ssl);
-static int ssl3_send_channel_id(SSL *ssl);
-static int ssl3_get_new_session_ticket(SSL *ssl);
+static int ssl3_send_client_hello(SSL_HANDSHAKE *hs);
+static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs);
+static int ssl3_get_server_hello(SSL_HANDSHAKE *hs);
+static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs);
+static int ssl3_get_cert_status(SSL_HANDSHAKE *hs);
+static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs);
+static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs);
+static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs);
+static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs);
+static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs);
+static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs);
+static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs);
+static int ssl3_send_next_proto(SSL_HANDSHAKE *hs);
+static int ssl3_send_channel_id(SSL_HANDSHAKE *hs);
+static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs);
 
 int ssl3_connect(SSL_HANDSHAKE *hs) {
   SSL *const ssl = hs->ssl;
@@ -216,7 +216,7 @@
 
       case SSL3_ST_CW_CLNT_HELLO_A:
       case SSL3_ST_CW_CLNT_HELLO_B:
-        ret = ssl3_send_client_hello(ssl);
+        ret = ssl3_send_client_hello(hs);
         if (ret <= 0) {
           goto end;
         }
@@ -231,7 +231,7 @@
 
       case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
         assert(SSL_is_dtls(ssl));
-        ret = dtls1_get_hello_verify(ssl);
+        ret = dtls1_get_hello_verify(hs);
         if (ret <= 0) {
           goto end;
         }
@@ -244,7 +244,7 @@
         break;
 
       case SSL3_ST_CR_SRVR_HELLO_A:
-        ret = ssl3_get_server_hello(ssl);
+        ret = ssl3_get_server_hello(hs);
         if (ssl->state == SSL_ST_TLS13) {
           break;
         }
@@ -261,7 +261,7 @@
 
       case SSL3_ST_CR_CERT_A:
         if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
-          ret = ssl3_get_server_certificate(ssl);
+          ret = ssl3_get_server_certificate(hs);
           if (ret <= 0) {
             goto end;
           }
@@ -273,7 +273,7 @@
 
       case SSL3_ST_CR_CERT_STATUS_A:
         if (hs->certificate_status_expected) {
-          ret = ssl3_get_cert_status(ssl);
+          ret = ssl3_get_cert_status(hs);
           if (ret <= 0) {
             goto end;
           }
@@ -285,7 +285,7 @@
 
       case SSL3_ST_VERIFY_SERVER_CERT:
         if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
-          ret = ssl3_verify_server_cert(ssl);
+          ret = ssl3_verify_server_cert(hs);
           if (ret <= 0) {
             goto end;
           }
@@ -296,7 +296,7 @@
         break;
 
       case SSL3_ST_CR_KEY_EXCH_A:
-        ret = ssl3_get_server_key_exchange(ssl);
+        ret = ssl3_get_server_key_exchange(hs);
         if (ret <= 0) {
           goto end;
         }
@@ -305,7 +305,7 @@
 
       case SSL3_ST_CR_CERT_REQ_A:
         if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
-          ret = ssl3_get_certificate_request(ssl);
+          ret = ssl3_get_certificate_request(hs);
           if (ret <= 0) {
             goto end;
           }
@@ -316,7 +316,7 @@
         break;
 
       case SSL3_ST_CR_SRVR_DONE_A:
-        ret = ssl3_get_server_hello_done(ssl);
+        ret = ssl3_get_server_hello_done(hs);
         if (ret <= 0) {
           goto end;
         }
@@ -328,7 +328,7 @@
       case SSL3_ST_CW_CERT_B:
       case SSL3_ST_CW_CERT_C:
         if (hs->cert_request) {
-          ret = ssl3_send_client_certificate(ssl);
+          ret = ssl3_send_client_certificate(hs);
           if (ret <= 0) {
             goto end;
           }
@@ -340,7 +340,7 @@
 
       case SSL3_ST_CW_KEY_EXCH_A:
       case SSL3_ST_CW_KEY_EXCH_B:
-        ret = ssl3_send_client_key_exchange(ssl);
+        ret = ssl3_send_client_key_exchange(hs);
         if (ret <= 0) {
           goto end;
         }
@@ -351,7 +351,7 @@
       case SSL3_ST_CW_CERT_VRFY_B:
       case SSL3_ST_CW_CERT_VRFY_C:
         if (hs->cert_request) {
-          ret = ssl3_send_cert_verify(ssl);
+          ret = ssl3_send_cert_verify(hs);
           if (ret <= 0) {
             goto end;
           }
@@ -379,7 +379,7 @@
       case SSL3_ST_CW_NEXT_PROTO_A:
       case SSL3_ST_CW_NEXT_PROTO_B:
         if (hs->next_proto_neg_seen) {
-          ret = ssl3_send_next_proto(ssl);
+          ret = ssl3_send_next_proto(hs);
           if (ret <= 0) {
             goto end;
           }
@@ -392,7 +392,7 @@
       case SSL3_ST_CW_CHANNEL_ID_A:
       case SSL3_ST_CW_CHANNEL_ID_B:
         if (ssl->s3->tlsext_channel_id_valid) {
-          ret = ssl3_send_channel_id(ssl);
+          ret = ssl3_send_channel_id(hs);
           if (ret <= 0) {
             goto end;
           }
@@ -404,7 +404,7 @@
 
       case SSL3_ST_CW_FINISHED_A:
       case SSL3_ST_CW_FINISHED_B:
-        ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A,
+        ret = ssl3_send_finished(hs, SSL3_ST_CW_FINISHED_A,
                                  SSL3_ST_CW_FINISHED_B);
         if (ret <= 0) {
           goto end;
@@ -444,7 +444,7 @@
 
       case SSL3_ST_CR_SESSION_TICKET_A:
         if (hs->ticket_expected) {
-          ret = ssl3_get_new_session_ticket(ssl);
+          ret = ssl3_get_new_session_ticket(hs);
           if (ret <= 0) {
             goto end;
           }
@@ -468,7 +468,7 @@
         break;
 
       case SSL3_ST_CR_FINISHED_A:
-        ret = ssl3_get_finished(ssl);
+        ret = ssl3_get_finished(hs);
         if (ret <= 0) {
           goto end;
         }
@@ -494,7 +494,7 @@
         break;
 
       case SSL_ST_TLS13:
-        ret = tls13_handshake(ssl);
+        ret = tls13_handshake(hs);
         if (ret <= 0) {
           goto end;
         }
@@ -720,7 +720,8 @@
   return 0;
 }
 
-static int ssl3_send_client_hello(SSL *ssl) {
+static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) {
     return ssl->method->write_message(ssl);
   }
@@ -781,7 +782,8 @@
   return ssl->method->write_message(ssl);
 }
 
-static int dtls1_get_hello_verify(SSL *ssl) {
+static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   int al;
   CBS hello_verify_request, cookie;
   uint16_t server_version;
@@ -823,7 +825,8 @@
   return -1;
 }
 
-static int ssl3_get_server_hello(SSL *ssl) {
+static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   CERT *ct = ssl->cert;
   int al = SSL_AD_INTERNAL_ERROR;
   CBS server_hello, server_random, session_id;
@@ -886,7 +889,7 @@
 
   if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
     ssl->state = SSL_ST_TLS13;
-    ssl->s3->hs->do_tls13_handshake = tls13_client_handshake;
+    hs->do_tls13_handshake = tls13_client_handshake;
     return 1;
   }
 
@@ -1027,7 +1030,8 @@
   return -1;
 }
 
-static int ssl3_get_server_certificate(SSL *ssl) {
+static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   int ret =
       ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message);
   if (ret <= 0) {
@@ -1071,7 +1075,8 @@
   return -1;
 }
 
-static int ssl3_get_cert_status(SSL *ssl) {
+static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   int al;
   CBS certificate_status, ocsp_response;
   uint8_t status_type;
@@ -1112,7 +1117,8 @@
   return -1;
 }
 
-static int ssl3_verify_server_cert(SSL *ssl) {
+static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
                              ssl->s3->new_session->x509_chain)) {
     return -1;
@@ -1121,7 +1127,8 @@
   return 1;
 }
 
-static int ssl3_get_server_key_exchange(SSL *ssl) {
+static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   int al;
   EVP_PKEY *pkey = NULL;
   DH *dh = NULL;
@@ -1184,7 +1191,7 @@
      * empty hint. Having different capabilities is odd, so we interpret empty
      * and missing as identical. */
     if (CBS_len(&psk_identity_hint) != 0 &&
-        !CBS_strdup(&psk_identity_hint, &ssl->s3->hs->peer_psk_identity_hint)) {
+        !CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) {
       al = SSL_AD_INTERNAL_ERROR;
       OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
       goto f_err;
@@ -1227,11 +1234,11 @@
       goto err;
     }
 
-    SSL_ECDH_CTX_init_for_dhe(&ssl->s3->hs->ecdh_ctx, dh);
+    SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
     dh = NULL;
 
     /* Save the peer public key for later. */
-    if (!CBS_stow(&dh_Ys, &ssl->s3->hs->peer_key, &ssl->s3->hs->peer_key_len)) {
+    if (!CBS_stow(&dh_Ys, &hs->peer_key, &hs->peer_key_len)) {
       goto err;
     }
   } else if (alg_k & SSL_kECDHE) {
@@ -1257,12 +1264,12 @@
     }
 
     /* Initialize ECDH and save the peer public key for later. */
-    if (!SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) ||
-        !CBS_stow(&point, &ssl->s3->hs->peer_key, &ssl->s3->hs->peer_key_len)) {
+    if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
+        !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
       goto err;
     }
   } else if (alg_k & SSL_kCECPQ1) {
-    SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->hs->ecdh_ctx);
+    SSL_ECDH_CTX_init_for_cecpq1(&hs->ecdh_ctx);
     CBS key;
     if (!CBS_get_u16_length_prefixed(&server_key_exchange, &key)) {
       al = SSL_AD_DECODE_ERROR;
@@ -1270,7 +1277,7 @@
       goto f_err;
     }
 
-    if (!CBS_stow(&key, &ssl->s3->hs->peer_key, &ssl->s3->hs->peer_key_len)) {
+    if (!CBS_stow(&key, &hs->peer_key, &hs->peer_key_len)) {
       goto err;
     }
   } else if (!(alg_k & SSL_kPSK)) {
@@ -1375,7 +1382,8 @@
   return -1;
 }
 
-static int ssl3_get_certificate_request(SSL *ssl) {
+static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
   if (msg_ret <= 0) {
     return msg_ret;
@@ -1406,8 +1414,8 @@
     return -1;
   }
 
-  if (!CBS_stow(&certificate_types, &ssl->s3->hs->certificate_types,
-                &ssl->s3->hs->num_certificate_types)) {
+  if (!CBS_stow(&certificate_types, &hs->certificate_types,
+                &hs->num_certificate_types)) {
     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
     return -1;
   }
@@ -1436,13 +1444,14 @@
     return -1;
   }
 
-  ssl->s3->hs->cert_request = 1;
-  sk_X509_NAME_pop_free(ssl->s3->hs->ca_names, X509_NAME_free);
-  ssl->s3->hs->ca_names = ca_sk;
+  hs->cert_request = 1;
+  sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free);
+  hs->ca_names = ca_sk;
   return 1;
 }
 
-static int ssl3_get_server_hello_done(SSL *ssl) {
+static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO_DONE,
                                          ssl_hash_message);
   if (ret <= 0) {
@@ -1459,7 +1468,8 @@
   return 1;
 }
 
-static int ssl3_send_client_certificate(SSL *ssl) {
+static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   if (ssl->state == SSL3_ST_CW_CERT_A) {
     /* Call cert_cb to update the certificate. */
     if (ssl->cert->cert_cb) {
@@ -1489,7 +1499,7 @@
     }
 
     if (!ssl_has_certificate(ssl)) {
-      ssl->s3->hs->cert_request = 0;
+      hs->cert_request = 0;
       /* Without a client certificate, the handshake buffer may be released. */
       ssl3_free_handshake_buffer(ssl);
 
@@ -1513,7 +1523,8 @@
 OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
                        SIZE_T_IS_SMALLER_THAN_UNSIGNED);
 
-static int ssl3_send_client_key_exchange(SSL *ssl) {
+static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) {
     return ssl->method->write_message(ssl);
   }
@@ -1541,9 +1552,9 @@
 
     char identity[PSK_MAX_IDENTITY_LEN + 1];
     memset(identity, 0, sizeof(identity));
-    psk_len = ssl->psk_client_callback(
-        ssl, ssl->s3->hs->peer_psk_identity_hint, identity, sizeof(identity),
-        psk, sizeof(psk));
+    psk_len =
+        ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
+                                 sizeof(identity), psk, sizeof(psk));
     if (psk_len == 0) {
       OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
       ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
@@ -1620,15 +1631,14 @@
   } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
     /* Generate a keypair and serialize the public half. */
     CBB child;
-    if (!SSL_ECDH_CTX_add_key(&ssl->s3->hs->ecdh_ctx, &body, &child)) {
+    if (!SSL_ECDH_CTX_add_key(&hs->ecdh_ctx, &body, &child)) {
       goto err;
     }
 
     /* Compute the premaster. */
     uint8_t alert;
-    if (!SSL_ECDH_CTX_accept(&ssl->s3->hs->ecdh_ctx, &child, &pms, &pms_len,
-                             &alert, ssl->s3->hs->peer_key,
-                             ssl->s3->hs->peer_key_len)) {
+    if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
+                             hs->peer_key, hs->peer_key_len)) {
       ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
       goto err;
     }
@@ -1637,10 +1647,10 @@
     }
 
     /* The key exchange state may now be discarded. */
-    SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx);
-    OPENSSL_free(ssl->s3->hs->peer_key);
-    ssl->s3->hs->peer_key = NULL;
-    ssl->s3->hs->peer_key_len = 0;
+    SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
+    OPENSSL_free(hs->peer_key);
+    hs->peer_key = NULL;
+    hs->peer_key_len = 0;
   } else if (alg_k & SSL_kPSK) {
     /* For plain PSK, other_secret is a block of 0s with the same length as
      * the pre-shared key. */
@@ -1710,7 +1720,8 @@
   return -1;
 }
 
-static int ssl3_send_cert_verify(SSL *ssl) {
+static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) {
     return ssl->method->write_message(ssl);
   }
@@ -1812,7 +1823,8 @@
   return -1;
 }
 
-static int ssl3_send_next_proto(SSL *ssl) {
+static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) {
     return ssl->method->write_message(ssl);
   }
@@ -1839,7 +1851,8 @@
   return ssl->method->write_message(ssl);
 }
 
-static int ssl3_send_channel_id(SSL *ssl) {
+static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) {
     return ssl->method->write_message(ssl);
   }
@@ -1868,7 +1881,8 @@
   return ssl->method->write_message(ssl);
 }
 
-static int ssl3_get_new_session_ticket(SSL *ssl) {
+static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
+  SSL *const ssl = hs->ssl;
   int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_NEW_SESSION_TICKET,
                                          ssl_hash_message);
   if (ret <= 0) {
@@ -1890,7 +1904,7 @@
     /* RFC 5077 allows a server to change its mind and send no ticket after
      * negotiating the extension. The value of |ticket_expected| is checked in
      * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
-    ssl->s3->hs->ticket_expected = 0;
+    hs->ticket_expected = 0;
     return 1;
   }