Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
64369cd
Show a certificate trust panel
joshaber Mar 30, 2017
653b2d1
Expose the certificate trust panel as part of app
joshaber Mar 30, 2017
7b044ff
Compile these lovely files
joshaber Mar 30, 2017
edf61d4
Link against the Security frameworks
joshaber Mar 30, 2017
deae70d
Dummy out the certificate FromV8 converter
joshaber Mar 31, 2017
16cc793
Errrrybody's a scoped_refptr now
joshaber Mar 31, 2017
141a5ad
Maybe this is a valid conversion?
joshaber Mar 31, 2017
302ca86
Slightly better conversion
joshaber Mar 31, 2017
69defc5
Encode all the intermediates
joshaber Apr 1, 2017
bde2a59
Decode all the intermediates
joshaber Apr 1, 2017
4f3d355
Notify that the cert changed
joshaber Apr 1, 2017
ee7389b
Rename to make VS happy
joshaber Apr 1, 2017
1ed443e
Do the callback & release dance
joshaber Apr 3, 2017
4bbbcd0
Handle the callback and cleanup properly
joshaber Apr 3, 2017
6e89cb9
Move it into `dialog`
joshaber Apr 3, 2017
4ffaf2c
Remove old unnecssary change on `app`
joshaber Apr 3, 2017
bcecba2
Fix indentation
joshaber Apr 3, 2017
0b7ffd0
Expose through the actual JS API too
joshaber Apr 3, 2017
11f5c94
Conditionalize
joshaber Apr 3, 2017
06643e5
Const ref that message
joshaber Apr 3, 2017
b0ef7dd
Use better iteration
joshaber Apr 3, 2017
da1b0aa
Flesh out some docs
joshaber Apr 3, 2017
b8ff466
Remove stale includes
joshaber Apr 3, 2017
74c0cbd
Linebreak to keep the linter happy
joshaber Apr 3, 2017
370cf81
Get rid of `intermediates` and rehydrate from `issuerCert`
joshaber Apr 3, 2017
398132c
Fix file sorting
joshaber Apr 4, 2017
b3e865a
Fix some style things
joshaber Apr 4, 2017
2badfbe
Remove the intermediate function
joshaber Apr 4, 2017
e2bda3c
Use an options object for most of the params
joshaber Apr 4, 2017
8d46523
Appease our linty overlords
joshaber Apr 4, 2017
0cab8a3
Put CertFromData in an anon namespace
joshaber Apr 4, 2017
146e1ed
Don't pass the result through
joshaber Apr 4, 2017
736d6af
As you wish linter
joshaber Apr 4, 2017
2749ded
Fix c&p error
joshaber Apr 4, 2017
2e32525
Make browser window optional
kevinsawicki Apr 4, 2017
2bd47eb
Add specs for showCertificateTrustDialog option errors
kevinsawicki Apr 4, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions atom/browser/api/atom_api_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

#include "atom/browser/api/atom_api_window.h"
#include "atom/browser/native_window.h"
#include "atom/browser/ui/certificate_trust.h"
#include "atom/browser/ui/file_dialog.h"
#include "atom/browser/ui/message_box.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/native_mate_converters/net_converter.h"
#include "native_mate/dictionary.h"

#include "atom/common/node_includes.h"
Expand Down Expand Up @@ -126,6 +128,10 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
dict.SetMethod("showErrorBox", &atom::ShowErrorBox);
dict.SetMethod("showOpenDialog", &ShowOpenDialog);
dict.SetMethod("showSaveDialog", &ShowSaveDialog);
#if defined(OS_MACOSX)
dict.SetMethod("showCertificateTrustDialog",
&certificate_trust::ShowCertificateTrust);
#endif
}

} // namespace
Expand Down
29 changes: 29 additions & 0 deletions atom/browser/ui/certificate_trust.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#ifndef ATOM_BROWSER_UI_CERTIFICATE_TRUST_H_
#define ATOM_BROWSER_UI_CERTIFICATE_TRUST_H_

#include <string>

#include "base/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "net/cert/x509_certificate.h"

namespace atom {
class NativeWindow;
} // namespace atom

namespace certificate_trust {

typedef base::Callback<void(void)> ShowTrustCallback;

void ShowCertificateTrust(atom::NativeWindow* parent_window,
const scoped_refptr<net::X509Certificate>& cert,
const std::string& message,
const ShowTrustCallback& callback);

} // namespace certificate_trust

#endif // ATOM_BROWSER_UI_CERTIFICATE_TRUST_H_
112 changes: 112 additions & 0 deletions atom/browser/ui/certificate_trust_mac.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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 "atom/browser/ui/certificate_trust.h"

#import <Cocoa/Cocoa.h>
#import <SecurityInterface/SFCertificateTrustPanel.h>

#include "atom/browser/native_window.h"
#include "base/strings/sys_string_conversions.h"
#include "net/cert/cert_database.h"

@interface TrustDelegate : NSObject {
@private
certificate_trust::ShowTrustCallback callback_;
SFCertificateTrustPanel* panel_;
scoped_refptr<net::X509Certificate> cert_;
SecTrustRef trust_;
CFArrayRef cert_chain_;
SecPolicyRef sec_policy_;
}

- (id)initWithCallback:(const certificate_trust::ShowTrustCallback&)callback
panel:(SFCertificateTrustPanel*)panel
cert:(const scoped_refptr<net::X509Certificate>&)cert
trust:(SecTrustRef)trust
certChain:(CFArrayRef)certChain
secPolicy:(SecPolicyRef)secPolicy;

- (void)panelDidEnd:(NSWindow*)sheet
returnCode:(int)returnCode
contextInfo:(void*)contextInfo;

@end

@implementation TrustDelegate

- (void)dealloc {
[panel_ release];
CFRelease(trust_);
CFRelease(cert_chain_);
CFRelease(sec_policy_);

[super dealloc];
}

- (id)initWithCallback:(const certificate_trust::ShowTrustCallback&)callback
panel:(SFCertificateTrustPanel*)panel
cert:(const scoped_refptr<net::X509Certificate>&)cert
trust:(SecTrustRef)trust
certChain:(CFArrayRef)certChain
secPolicy:(SecPolicyRef)secPolicy {
if ((self = [super init])) {
callback_ = callback;
panel_ = panel;
cert_ = cert;
trust_ = trust;
cert_chain_ = certChain;
sec_policy_ = secPolicy;
}

return self;
}

- (void)panelDidEnd:(NSWindow*)sheet
returnCode:(int)returnCode
contextInfo:(void*)contextInfo {
auto cert_db = net::CertDatabase::GetInstance();
// This forces Chromium to reload the certificate since it might be trusted
// now.
cert_db->NotifyObserversCertDBChanged(cert_.get());

callback_.Run();

[self autorelease];
}

@end

namespace certificate_trust {

void ShowCertificateTrust(atom::NativeWindow* parent_window,
const scoped_refptr<net::X509Certificate>& cert,
const std::string& message,
const ShowTrustCallback& callback) {
auto sec_policy = SecPolicyCreateBasicX509();
auto cert_chain = cert->CreateOSCertChainForCert();
SecTrustRef trust = nullptr;
SecTrustCreateWithCertificates(cert_chain, sec_policy, &trust);

NSWindow* window = parent_window ?
parent_window->GetNativeWindow() :
nil;
auto msg = base::SysUTF8ToNSString(message);

auto panel = [[SFCertificateTrustPanel alloc] init];
auto delegate = [[TrustDelegate alloc] initWithCallback:callback
panel:panel
cert:cert
trust:trust
certChain:cert_chain
secPolicy:sec_policy];
[panel beginSheetForWindow:window
modalDelegate:delegate
didEndSelector:@selector(panelDidEnd:returnCode:contextInfo:)
contextInfo:nil
trust:trust
message:msg];
}

} // namespace certificate_trust
52 changes: 52 additions & 0 deletions atom/common/native_mate_converters/net_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@

namespace mate {

namespace {

bool CertFromData(const std::string& data,
scoped_refptr<net::X509Certificate>* out) {
auto cert_list = net::X509Certificate::CreateCertificateListFromBytes(
data.c_str(), data.length(),
net::X509Certificate::FORMAT_SINGLE_CERTIFICATE);
if (cert_list.empty())
return false;

auto leaf_cert = cert_list.front();
if (!leaf_cert)
return false;

*out = leaf_cert;

return true;
}

} // namespace

// static
v8::Local<v8::Value> Converter<const net::AuthChallengeInfo*>::ToV8(
v8::Isolate* isolate, const net::AuthChallengeInfo* val) {
Expand Down Expand Up @@ -73,6 +94,37 @@ v8::Local<v8::Value> Converter<scoped_refptr<net::X509Certificate>>::ToV8(
return dict.GetHandle();
}

bool Converter<scoped_refptr<net::X509Certificate>>::FromV8(
v8::Isolate* isolate, v8::Local<v8::Value> val,
scoped_refptr<net::X509Certificate>* out) {
mate::Dictionary dict;
if (!ConvertFromV8(isolate, val, &dict))
return false;

std::string data;
dict.Get("data", &data);
scoped_refptr<net::X509Certificate> leaf_cert;
if (!CertFromData(data, &leaf_cert))
return false;

scoped_refptr<net::X509Certificate> parent;
if (dict.Get("issuerCert", &parent)) {
auto parents = std::vector<net::X509Certificate::OSCertHandle>(
parent->GetIntermediateCertificates());
parents.insert(parents.begin(), parent->os_cert_handle());
auto cert = net::X509Certificate::CreateFromHandle(
leaf_cert->os_cert_handle(), parents);
if (!cert)
return false;

*out = cert;
} else {
*out = leaf_cert;
}

return true;
}

// static
v8::Local<v8::Value> Converter<net::CertPrincipal>::ToV8(
v8::Isolate* isolate, const net::CertPrincipal& val) {
Expand Down
4 changes: 4 additions & 0 deletions atom/common/native_mate_converters/net_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ template<>
struct Converter<scoped_refptr<net::X509Certificate>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const scoped_refptr<net::X509Certificate>& val);

static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
scoped_refptr<net::X509Certificate>* out);
};

template<>
Expand Down
14 changes: 14 additions & 0 deletions docs/api/dialog.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ it is usually used to report errors in early stage of startup. If called
before the app `ready`event on Linux, the message will be emitted to stderr,
and no GUI dialog will appear.

### `dialog.showCertificateTrustDialog([browserWindow, ]options, callback)` _macOS_

* `browserWindow` BrowserWindow (optional)
* `options` Object
* `certificate` [Certificate](structures/certificate.md) - The certificate to trust/import.
* `message` String - The message to display to the user.
* `callback` Function

Displays a modal dialog that shows a message and certificate information, and
gives the user the option of trusting/importing the certificate.

The `browserWindow` argument allows the dialog to attach itself to a parent
window, making it modal.

## Sheets

On macOS, dialogs are presented as sheets attached to a window if you provide
Expand Down
2 changes: 2 additions & 0 deletions electron.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@
'$(SDKROOT)/System/Library/Frameworks/Carbon.framework',
'$(SDKROOT)/System/Library/Frameworks/QuartzCore.framework',
'$(SDKROOT)/System/Library/Frameworks/Quartz.framework',
'$(SDKROOT)/System/Library/Frameworks/Security.framework',
'$(SDKROOT)/System/Library/Frameworks/SecurityInterface.framework',
],
},
'mac_bundle': 1,
Expand Down
2 changes: 2 additions & 0 deletions filenames.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@
'atom/browser/ui/accelerator_util_views.cc',
'atom/browser/ui/atom_menu_model.cc',
'atom/browser/ui/atom_menu_model.h',
'atom/browser/ui/certificate_trust.h',
'atom/browser/ui/certificate_trust_mac.mm',
'atom/browser/ui/cocoa/atom_menu_controller.h',
'atom/browser/ui/cocoa/atom_menu_controller.mm',
'atom/browser/ui/cocoa/atom_touch_bar.h',
Expand Down
21 changes: 21 additions & 0 deletions lib/browser/api/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,27 @@ module.exports = {

showErrorBox: function (...args) {
return binding.showErrorBox(...args)
},

showCertificateTrustDialog: function (...args) {
let [window, options, callback] = parseArgs(...args)

if (options == null || typeof options !== 'object') {
throw new TypeError('options must be an object')
}

let {certificate, message} = options
if (certificate == null || typeof certificate !== 'object') {
throw new TypeError('certificate must be an object')
}

if (message == null) {
message = ''
} else if (typeof message !== 'string') {
throw new TypeError('message must be a string')
}

return binding.showCertificateTrustDialog(window, certificate, message, callback)
}
}

Expand Down
16 changes: 16 additions & 0 deletions spec/api-dialog-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,20 @@ describe('dialog module', () => {
}, /Error processing argument at index 1/)
})
})

describe('showCertificateTrustDialog', () => {
it('throws errors when the options are invalid', () => {
assert.throws(() => {
dialog.showCertificateTrustDialog()
}, /options must be an object/)

assert.throws(() => {
dialog.showCertificateTrustDialog({})
}, /certificate must be an object/)

assert.throws(() => {
dialog.showCertificateTrustDialog({certificate: {}, message: false})
}, /message must be a string/)
})
})
})