electron/shell/browser/ui/certificate_trust_win.cc
Charles Kerr e70ce89235
chore: more iwyu (#43063)
* chore: iwyu shell/browser/electron_pdf_document_helper_client.h

* chore: iwyu shell/browser/hid/electron_hid_delegate.h

* chore: iwyu content/public/browser/web_contents.h

* chore: iwyu shell/browser/usb/electron_usb_delegate.h

* chore: iwyu shell/browser/browser_observer.h

* chore: iwyu shell/browser/bluetooth/electron_bluetooth_delegate.h

* chore: iwyu shell/browser/serial/electron_serial_delegate.h

* chore: iwyu shell/browser/api/frame_subscriber.h

* chore: iwyu mojo/public/cpp/bindings/

* chore: iwyu components/

* chore: iwyu extensions/

* chore: iwyu shell/common/gin_helper/

* chore: iwyu v8/

* chore: iwyu base/containers/linked_list.h

* chore: iwyu shell/browser/native_window.h

* chore: iwyu shell/browser/api/electron_api_base_window.h

* chore: iwyu shell/common/node_includes.h

* chore: iwyu gin/handle.h

* chore: iwyu base/functional/callback.h

* chore: iwyu ui/gfx/

* chore: iwyu content/public/browser/render_frame_host.h

* fix: mac

* fix: mac

* fix: win

* chore: iwyu base/files/file_path.h

* chore: iwyu base/unguessable_token.h

* chore: iwyu ui/display/screen.h

* chore: iwyu chrome/browser/predictors/preconnect_manager.h

* chore: iwyu base/observer_list_types.h

* chore: iwyu content/public/browser/web_contents.h

* chore: iwyu chrome/browser/devtools/devtools_eye_dropper.h

* chore: iwyu shell/browser/ui/inspectable_web_contents.h

* chore: iwyu content/public/browser/keyboard_event_processing_result.h

* chore: iwyu net/cookies/canonical_cookie.h

* chore: iwyu net/base/address_list.h

* chore: iwyu net/cert/x509_certificate.h

* chore: iwyu net/cookies/cookie_change_dispatcher.h

* chore: iwyu net/dns/public/host_resolver_results.h

* fix: mac

* Revert "chore: iwyu net/cert/x509_certificate.h"

This reverts commit 002896f71146e90f1e29e090a1d6eede48cee11e.
2024-07-29 12:42:57 -05:00

89 lines
2.8 KiB
C++

// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/ui/certificate_trust.h"
#include <windows.h> // windows.h must be included first
#include <wincrypt.h>
#include "net/cert/cert_database.h"
#include "net/cert/x509_util_win.h"
#include "shell/browser/javascript_environment.h"
#include "shell/common/gin_helper/promise.h"
namespace certificate_trust {
// Add the provided certificate to the Trusted Root Certificate Authorities
// store for the current user.
//
// This requires prompting the user to confirm they trust the certificate.
BOOL AddToTrustedRootStore(const PCCERT_CONTEXT cert_context,
const scoped_refptr<net::X509Certificate>& cert) {
auto* root_cert_store = CertOpenStore(
CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root");
if (root_cert_store == nullptr) {
return false;
}
auto result = CertAddCertificateContextToStore(
root_cert_store, cert_context, CERT_STORE_ADD_REPLACE_EXISTING, nullptr);
if (result) {
// force Chromium to reload it's database for this certificate
auto* cert_db = net::CertDatabase::GetInstance();
cert_db->NotifyObserversTrustStoreChanged();
}
CertCloseStore(root_cert_store, CERT_CLOSE_STORE_FORCE_FLAG);
return result;
}
CERT_CHAIN_PARA GetCertificateChainParameters() {
CERT_ENHKEY_USAGE enhkey_usage;
enhkey_usage.cUsageIdentifier = 0;
enhkey_usage.rgpszUsageIdentifier = nullptr;
CERT_USAGE_MATCH cert_usage;
// ensure the rules are applied to the entire chain
cert_usage.dwType = USAGE_MATCH_TYPE_AND;
cert_usage.Usage = enhkey_usage;
CERT_CHAIN_PARA params = {sizeof(CERT_CHAIN_PARA)};
params.RequestedUsage = cert_usage;
return params;
}
v8::Local<v8::Promise> ShowCertificateTrust(
electron::NativeWindow* parent_window,
const scoped_refptr<net::X509Certificate>& cert,
const std::string& message) {
v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
gin_helper::Promise<void> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();
PCCERT_CHAIN_CONTEXT chain_context;
auto cert_context = net::x509_util::CreateCertContextWithChain(cert.get());
auto params = GetCertificateChainParameters();
if (CertGetCertificateChain(nullptr, cert_context.get(), nullptr, nullptr,
&params, 0, nullptr, &chain_context)) {
auto error_status = chain_context->TrustStatus.dwErrorStatus;
if (error_status == CERT_TRUST_IS_SELF_SIGNED ||
error_status == CERT_TRUST_IS_UNTRUSTED_ROOT) {
// these are the only scenarios we're interested in supporting
AddToTrustedRootStore(cert_context.get(), cert);
}
CertFreeCertificateChain(chain_context);
}
promise.Resolve();
return handle;
}
} // namespace certificate_trust