From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jeremy Apthorp <nornagon@nornagon.net>
Date: Wed, 8 May 2019 17:25:55 -0700
Subject: network_service_allow_remote_certificate_verification_logic.patch

This adds a callback from the network service that's used to implement
session.setCertificateVerifyCallback.

diff --git a/services/network/network_context.cc b/services/network/network_context.cc
index e12f76b1ca50efee574a7bbf852d2f6923d528cd..8b51dbe8f2859de3be2e6dc918f4eb535a8a5622 100644
--- a/services/network/network_context.cc
+++ b/services/network/network_context.cc
@@ -93,6 +93,11 @@
 #include "services/network/url_loader.h"
 #include "services/network/url_request_context_builder_mojo.h"
 
+// Electron
+#include "net/cert/caching_cert_verifier.h"
+#include "net/cert/cert_verify_proc.h"
+#include "net/cert/multi_threaded_cert_verifier.h"
+
 #if BUILDFLAG(IS_CT_SUPPORTED)
 #include "components/certificate_transparency/chrome_ct_policy_enforcer.h"
 #include "components/certificate_transparency/chrome_require_ct_delegate.h"
@@ -327,6 +332,79 @@ std::string HashesToBase64String(const net::HashValueVector& hashes) {
 
 }  // namespace
 
+class RemoteCertVerifier : public net::CertVerifier {
+ public:
+  RemoteCertVerifier(std::unique_ptr<net::CertVerifier> upstream): upstream_(std::move(upstream)) {
+  }
+  ~RemoteCertVerifier() override = default;
+
+  void Bind(
+      mojo::PendingRemote<mojom::CertVerifierClient> client_info) {
+    client_.reset();
+    if (client_info.is_valid()) {
+      client_.Bind(std::move(client_info));
+    }
+  }
+
+  // CertVerifier implementation
+  int Verify(const RequestParams& params,
+             net::CertVerifyResult* verify_result,
+             net::CompletionOnceCallback callback,
+             std::unique_ptr<Request>* out_req,
+             const net::NetLogWithSource& net_log) override {
+    out_req->reset();
+
+    net::CompletionOnceCallback callback2 = base::BindOnce(
+        &RemoteCertVerifier::OnRequestFinished, base::Unretained(this),
+        params, std::move(callback), verify_result);
+    int result = upstream_->Verify(params, verify_result,
+                                   std::move(callback2), out_req, net_log);
+    if (result != net::ERR_IO_PENDING) {
+      // Synchronous completion
+    }
+
+    return result;
+  }
+
+
+  void SetConfig(const Config& config) override {
+    upstream_->SetConfig(config);
+  }
+
+  void OnRequestFinished(const RequestParams& params, net::CompletionOnceCallback callback, net::CertVerifyResult* verify_result, int error) {
+    if (client_.is_bound()) {
+      client_->Verify(error, *verify_result, params.certificate(),
+          params.hostname(), params.flags(), params.ocsp_response(),
+          base::BindOnce(&RemoteCertVerifier::OnRemoteResponse,
+            base::Unretained(this), params, verify_result, error,
+            std::move(callback)));
+    } else {
+      std::move(callback).Run(error);
+    }
+  }
+
+  void OnRemoteResponse(
+      const RequestParams& params,
+      net::CertVerifyResult* verify_result,
+      int error,
+      net::CompletionOnceCallback callback,
+      int error2,
+      const net::CertVerifyResult& verify_result2) {
+    if (error2 == net::ERR_ABORTED) {
+      // use the default
+      std::move(callback).Run(error);
+    } else {
+      // use the override
+      verify_result->Reset();
+      verify_result->verified_cert = verify_result2.verified_cert;
+      std::move(callback).Run(error2);
+    }
+  }
+ private:
+  std::unique_ptr<net::CertVerifier> upstream_;
+  mojo::Remote<mojom::CertVerifierClient> client_;
+};
+
 constexpr uint32_t NetworkContext::kMaxOutstandingRequestsPerProcess;
 
 NetworkContext::PendingCertVerify::PendingCertVerify() = default;
@@ -513,6 +591,13 @@ void NetworkContext::SetClient(
   client_.Bind(std::move(client));
 }
 
+void NetworkContext::SetCertVerifierClient(
+    mojo::PendingRemote<mojom::CertVerifierClient> client) {
+  if (remote_cert_verifier_) {
+    remote_cert_verifier_->Bind(std::move(client));
+  }
+}
+
 void NetworkContext::CreateURLLoaderFactory(
     mojo::PendingReceiver<mojom::URLLoaderFactory> receiver,
     mojom::URLLoaderFactoryParamsPtr params) {
@@ -1643,6 +1728,7 @@ URLRequestContextOwner NetworkContext::MakeURLRequestContext(
       base::CommandLine::ForCurrentProcess();
 
   std::unique_ptr<net::CertVerifier> cert_verifier;
+  std::unique_ptr<net::CertVerifier> temp_verifier;
   if (g_cert_verifier_for_testing) {
     cert_verifier = std::make_unique<WrappedTestingCertVerifier>();
   } else {
@@ -1695,8 +1781,8 @@ URLRequestContextOwner NetworkContext::MakeURLRequestContext(
     }
 #endif
 #if BUILDFLAG(BUILTIN_CERT_VERIFIER_FEATURE_SUPPORTED)
-    if (!cert_verifier) {
-      cert_verifier = std::make_unique<net::CachingCertVerifier>(
+    if (!temp_verifier) {
+      temp_verifier = std::make_unique<net::CachingCertVerifier>(
           std::make_unique<net::CoalescingCertVerifier>(
               std::make_unique<net::MultiThreadedCertVerifier>(
                   params_->use_builtin_cert_verifier
@@ -1706,12 +1792,19 @@ URLRequestContextOwner NetworkContext::MakeURLRequestContext(
                             cert_net_fetcher_))));
     }
 #endif
-    if (!cert_verifier)
-      cert_verifier = net::CertVerifier::CreateDefault(cert_net_fetcher_);
+    if (!temp_verifier) {
+      temp_verifier = std::make_unique<net::MultiThreadedCertVerifier>(
+          net::CertVerifyProc::CreateSystemVerifyProc(std::move(cert_net_fetcher_)));
+    }
+    auto remote_cert_verifier = std::make_unique<RemoteCertVerifier>(std::move(temp_verifier));
+    remote_cert_verifier_ = remote_cert_verifier.get();
+    cert_verifier = std::make_unique<net::CachingCertVerifier>(std::move(remote_cert_verifier));
   }
 
-  builder.SetCertVerifier(IgnoreErrorsCertVerifier::MaybeWrapCertVerifier(
-      *command_line, nullptr, std::move(cert_verifier)));
+  cert_verifier = IgnoreErrorsCertVerifier::MaybeWrapCertVerifier(
+      *command_line, nullptr, std::move(cert_verifier));
+
+  builder.SetCertVerifier(std::move(cert_verifier));
 
   std::unique_ptr<NetworkServiceNetworkDelegate> network_delegate =
       std::make_unique<NetworkServiceNetworkDelegate>(
diff --git a/services/network/network_context.h b/services/network/network_context.h
index 55d2de8f1e5d538f3168808c0b3c2a19520cf824..829a26f486e1e7c7a24f1d273e9af48df5fbd9b1 100644
--- a/services/network/network_context.h
+++ b/services/network/network_context.h
@@ -87,6 +87,7 @@ class DomainReliabilityMonitor;
 
 namespace network {
 class CertVerifierWithTrustAnchors;
+class RemoteCertVerifier;
 class CookieManager;
 class ExpectCTReporter;
 class HostResolver;
@@ -183,6 +184,8 @@ class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkContext
   void CreateURLLoaderFactory(
       mojo::PendingReceiver<mojom::URLLoaderFactory> receiver,
       mojom::URLLoaderFactoryParamsPtr params) override;
+  void SetCertVerifierClient(
+      mojo::PendingRemote<mojom::CertVerifierClient> client) override;
   void ResetURLLoaderFactories() override;
   void GetCookieManager(
       mojo::PendingReceiver<mojom::CookieManager> receiver) override;
@@ -612,6 +615,8 @@ class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkContext
   std::unique_ptr<network::NSSTempCertsCacheChromeOS> nss_temp_certs_cache_;
 #endif
 
+  RemoteCertVerifier* remote_cert_verifier_ = nullptr;
+
   // CertNetFetcher used by the context's CertVerifier. May be nullptr if
   // CertNetFetcher is not used by the current platform.
   scoped_refptr<cert_verifier::CertNetFetcherURLLoader> cert_net_fetcher_;
diff --git a/services/network/public/mojom/network_context.mojom b/services/network/public/mojom/network_context.mojom
index ee5d5864eaf9fcb4bf5f13fc5e3a6ae70a6d7a86..f45ea3ba79d68d6d58aedba54c5bc9d7fc4935e9 100644
--- a/services/network/public/mojom/network_context.mojom
+++ b/services/network/public/mojom/network_context.mojom
@@ -190,6 +190,17 @@ struct HttpAuthStaticNetworkContextParams {
      = DefaultCredentials.ALLOW_DEFAULT_CREDENTIALS;
 };
 
+interface CertVerifierClient {
+  Verify(
+    int32 default_error,
+    CertVerifyResult default_result,
+    X509Certificate certificate,
+    string hostname,
+    int32 flags,
+    string? ocsp_response
+  ) => (int32 error_code, CertVerifyResult result);
+};
+
 // Parameters for constructing a network context.
 struct NetworkContextParams {
   // Name used by memory tools to identify the context.
@@ -798,6 +809,9 @@ interface NetworkContext {
   // Sets a client for this network context.
   SetClient(pending_remote<NetworkContextClient> client);
 
+  // Sets a certificate verifier client for this network context.
+  SetCertVerifierClient(pending_remote<CertVerifierClient>? client);
+
   // Creates a new URLLoaderFactory with the given |params|.
   CreateURLLoaderFactory(pending_receiver<URLLoaderFactory> url_loader_factory,
                          URLLoaderFactoryParams params);
