<commit_before>From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: <NAME> <<EMAIL>>
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 8e53d65ddca7b54a6effd1767257a4d8239251d8..8a00bf59c728217069000b1f1ece72e1540baf85 100644
--- a/services/network/network_context.cc
+++ b/services/network/network_context.cc
@@ -116,6 +116,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"
@@ -374,6 +379,79 @@ bool SCTAuditingDelegate::IsSCTAuditingEnabled() {
 
 }  // 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;
@@ -573,6 +651,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) {
@@ -1930,6 +2015,9 @@ URLRequestContextOwner NetworkContext::MakeURLRequestContext(
         std::move(cert_verifier));
     cert_verifier = base::WrapUnique(cert_verifier_with_trust_anchors_);
 #endif  // BUILDFLAG(IS_CHROMEOS_ASH)
+    auto remote_cert_verifier = std::make_unique<RemoteCertVerifier>(std::move(cert_verifier));
+    remote_cert_verifier_ = remote_cert_verifier.get();
+    cert_verifier = std::move(remote_cert_verifier);
 
     // Whether the cert verifier is remote or in-process, we should wrap it in
     // caching and coalescing layers to avoid extra verifications and IPCs.
diff --git a/services/network/network_context.h b/services/network/network_context.h
index 72885bc1d20a4da5ad4df3fb8185f05bcf6fbfba..06b1a0d550de946aa41efca2be4efde694cc24c7 100644
--- a/services/network/network_context.h
+++ b/services/network/network_context.h
@@ -90,6 +90,7 @@ class DomainReliabilityMonitor;
 
 namespace network {
 class CertVerifierWithTrustAnchors;
+class RemoteCertVerifier;
 class CookieManager;
 class ExpectCTReporter;
 class HostResolver;
@@ -194,6 +195,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;
@@ -696,6 +699,8 @@ class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkContext
   CertVerifierWithTrustAnchors* cert_verifier_with_trust_anchors_ = nullptr;<commit_msg>fix patches<commit_after>From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: <NAME> <<EMAIL>>
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 8e53d65ddca7b54a6effd1767257a4d8239251d8..8a00bf59c728217069000b1f1ece72e1540baf85 100644
--- a/services/network/network_context.cc
+++ b/services/network/network_context.cc
@@ -116,6 +116,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"
@@ -374,6 +379,79 @@ bool SCTAuditingDelegate::IsSCTAuditingEnabled() {
 
 }  // 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;
@@ -573,6 +651,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) {
@@ -1930,6 +2015,9 @@ URLRequestContextOwner NetworkContext::MakeURLRequestContext(
         std::move(cert_verifier));
     cert_verifier = base::WrapUnique(cert_verifier_with_trust_anchors_);
 #endif  // BUILDFLAG(IS_CHROMEOS_ASH)
+    auto remote_cert_verifier = std::make_unique<RemoteCertVerifier>(std::move(cert_verifier));
+    remote_cert_verifier_ = remote_cert_verifier.get();
+    cert_verifier = std::make_unique<net::CachingCertVerifier>(std::move(remote_cert_verifier));
   }
 
   builder.SetCertVerifier(IgnoreErrorsCertVerifier::MaybeWrapCertVerifier(
diff --git a/services/network/network_context.h b/services/network/network_context.h
index 72885bc1d20a4da5ad4df3fb8185f05bcf6fbfba..06b1a0d550de946aa41efca2be4efde694cc24c7 100644
--- a/services/network/network_context.h
+++ b/services/network/network_context.h
@@ -90,6 +90,7 @@ class DomainReliabilityMonitor;
 
 namespace network {
 class CertVerifierWithTrustAnchors;
+class RemoteCertVerifier;
 class CookieManager;
 class ExpectCTReporter;
 class HostResolver;
@@ -194,6 +195,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;
@@ -696,6 +699,8 @@ class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkContext
   CertVerifierWithTrustAnchors* cert_verifier_with_trust_anchors_ = nullptr;
{'fraction_non_alphanumeric': 0.123645903859174, 'fraction_numerical': 0.04028436018957346, 'mean_word_length': 5.010681586978637, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7253424', 'n_tokens_mistral': 4134, 'n_tokens_neox': 3845, 'n_words': 600}