Use the BoringSSL callback for certificate verification.

This moves certificate verification to within the handshake, instead of
a separate step afterwards, which allows us to verify the certificate
before prompting for client certificates.

It also means that certificate errors result in incomplete handshakes,
so this also changes SSLClientSocket unit tests not to expect connected
sockets after certificate errors.

Bug: 347402
Change-Id: I0a93da1dee5be697fa7d5c74aae206d370f97d5b
Reviewed-on: https://chromium-review.googlesource.com/c/1259123
Commit-Queue: Jesse Selover <[email protected]>
Reviewed-by: Joe Downing <[email protected]>
Reviewed-by: Ryan Sleevi <[email protected]>
Reviewed-by: David Benjamin <[email protected]>
Cr-Commit-Position: refs/heads/master@{#622963}
diff --git a/net/socket/ssl_client_socket_impl.h b/net/socket/ssl_client_socket_impl.h
index 4e5a7c17..1e8d7d00 100644
--- a/net/socket/ssl_client_socket_impl.h
+++ b/net/socket/ssl_client_socket_impl.h
@@ -139,10 +139,9 @@
 
   int DoHandshake();
   int DoHandshakeComplete(int result);
-  int DoVerifyCert(int result);
-  int DoVerifyCertComplete(int result);
   void DoConnectCallback(int result);
 
+  void OnVerifyComplete(int result);
   void OnHandshakeIOComplete(int result);
 
   int DoHandshakeLoop(int last_io_result);
@@ -154,19 +153,18 @@
   // and, if complete, runs the respective callbacks.
   void RetryAllOperations();
 
+  // Callback from the SSL layer when a certificate needs to be verified. This
+  // is called when establishing new (fresh) connections and when evaluating
+  // whether an existing session can be resumed.
+  static ssl_verify_result_t VerifyCertCallback(SSL* ssl, uint8_t* out_alert);
+  ssl_verify_result_t VerifyCert();
+  ssl_verify_result_t HandleVerifyResult();
   int VerifyCT();
 
   // Callback from the SSL layer that indicates the remote server is requesting
   // a certificate for this client.
   int ClientCertRequestCallback(SSL* ssl);
 
-  // Called after the initial handshake completes and after the server
-  // certificate has been verified. The order of handshake completion and
-  // certificate verification depends on whether the connection was false
-  // started. After both have happened (thus calling this twice), the session is
-  // safe to cache and will be cached.
-  void MaybeCacheSession();
-
   // Called from the SSL layer whenever a new session is established.
   int NewSessionCallback(SSL_SESSION* session);
 
@@ -259,6 +257,9 @@
   std::unique_ptr<CertVerifier::Request> cert_verifier_request_;
   base::TimeTicks start_cert_verification_time_;
 
+  // Result from Cert Verifier.
+  int cert_verification_result_;
+
   // Certificate Transparency: Verifier and result holder.
   ct::CTVerifyResult ct_verify_result_;
   CTVerifier* cert_transparency_verifier_;
@@ -279,8 +280,6 @@
     STATE_NONE,
     STATE_HANDSHAKE,
     STATE_HANDSHAKE_COMPLETE,
-    STATE_VERIFY_CERT,
-    STATE_VERIFY_CERT_COMPLETE,
   };
   State next_handshake_state_;
 
@@ -291,11 +290,6 @@
   bool disconnected_;
 
   NextProto negotiated_protocol_;
-  // If non-null, the newly-established to be inserted into the session cache
-  // once certificate verification is done.
-  bssl::UniquePtr<SSL_SESSION> pending_session_;
-  // True if the initial handshake's certificate has been verified.
-  bool certificate_verified_;
   // Set to true if a CertificateRequest was received.
   bool certificate_requested_;