1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/ssl_client_certificate_selector_cocoa.h"
#import <SecurityInterface/SFChooseIdentityPanel.h>
#include <stddef.h>
#include <utility>
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/ssl/ssl_client_auth_observer.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
#include "chrome/grit/generated_resources.h"
#include "components/guest_view/browser/guest_view_base.h"
#include "components/strings/grit/components_strings.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/client_certificate_delegate.h"
#include "content/public/browser/web_contents.h"
#include "net/cert/x509_certificate.h"
#include "net/cert/x509_util_mac.h"
#include "net/ssl/ssl_cert_request_info.h"
#include "ui/base/cocoa/window_size_constants.h"
#include "ui/base/l10n/l10n_util_mac.h"
using content::BrowserThread;
@interface SFChooseIdentityPanel (SystemPrivate)
// A system-private interface that dismisses a panel whose sheet was started by
// -beginSheetForWindow:modalDelegate:didEndSelector:contextInfo:identities:message:
// as though the user clicked the button identified by returnCode. Verified
// present in 10.5 through 10.8.
- (void)_dismissWithCode:(NSInteger)code;
@end
@interface SSLClientCertificateSelectorCocoa ()
- (void)onConstrainedWindowClosed;
@end
class SSLClientAuthObserverCocoaBridge : public SSLClientAuthObserver,
public ConstrainedWindowMacDelegate {
public:
SSLClientAuthObserverCocoaBridge(
const content::BrowserContext* browser_context,
net::SSLCertRequestInfo* cert_request_info,
std::unique_ptr<content::ClientCertificateDelegate> delegate,
SSLClientCertificateSelectorCocoa* controller)
: SSLClientAuthObserver(browser_context,
cert_request_info,
std::move(delegate)),
controller_(controller) {}
// SSLClientAuthObserver implementation:
void OnCertSelectedByNotification() override {
[controller_ closeWebContentsModalDialog];
}
// ConstrainedWindowMacDelegate implementation:
void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override {
// |onConstrainedWindowClosed| will delete the sheet which might be still
// in use higher up the call stack. Wait for the next cycle of the event
// loop to call this function.
[controller_ performSelector:@selector(onConstrainedWindowClosed)
withObject:nil
afterDelay:0];
}
private:
SSLClientCertificateSelectorCocoa* controller_; // weak
};
namespace chrome {
void ShowSSLClientCertificateSelector(
content::WebContents* contents,
net::SSLCertRequestInfo* cert_request_info,
std::unique_ptr<content::ClientCertificateDelegate> delegate) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Not all WebContentses can show modal dialogs.
//
// Use the top-level embedder if |contents| is a guest.
// GetTopLevelWebContents() will return |contents| otherwise.
// TODO(davidben): Move this hook to the WebContentsDelegate and only try to
// show a dialog in Browser's implementation. https://crbug.com/456255
if (web_modal::WebContentsModalDialogManager::FromWebContents(
guest_view::GuestViewBase::GetTopLevelWebContents(contents)) ==
nullptr)
return;
// The dialog manages its own lifetime.
SSLClientCertificateSelectorCocoa* selector =
[[SSLClientCertificateSelectorCocoa alloc]
initWithBrowserContext:contents->GetBrowserContext()
certRequestInfo:cert_request_info
delegate:std::move(delegate)];
[selector displayForWebContents:contents];
}
} // namespace chrome
namespace {
// These ClearTableViewDataSources... functions help work around a bug in macOS
// 10.12 where SFChooseIdentityPanel leaks a window and some views, including
// an NSTableView. Future events may make cause the table view to query its
// dataSource, which will have been deallocated.
//
// NSTableView.dataSource becomes a zeroing weak reference starting in 10.11,
// so this workaround can be removed once we're on the 10.11 SDK.
//
// See https://crbug.com/653093 and rdar://29409207 for more information.
#if !defined(MAC_OS_X_VERSION_10_11) || \
MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_11
void ClearTableViewDataSources(NSView* view) {
if (auto table_view = base::mac::ObjCCast<NSTableView>(view)) {
table_view.dataSource = nil;
} else {
for (NSView* subview in view.subviews) {
ClearTableViewDataSources(subview);
}
}
}
void ClearTableViewDataSourcesIfWindowStillExists(NSWindow* leaked_window) {
for (NSWindow* window in [NSApp windows]) {
// If the window is still in the window list...
if (window == leaked_window) {
// ...search it for table views.
ClearTableViewDataSources(window.contentView);
break;
}
}
}
void ClearTableViewDataSourcesIfNeeded(NSWindow* leaked_window) {
// Let the autorelease pool drain before deciding if the window was leaked.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(ClearTableViewDataSourcesIfWindowStillExists,
base::Unretained(leaked_window)));
}
#else
void ClearTableViewDataSourcesIfNeeded(NSWindow*) {}
#endif // MAC_OS_X_VERSION_10_11
} // namespace
@implementation SSLClientCertificateSelectorCocoa
- (id)initWithBrowserContext:(const content::BrowserContext*)browserContext
certRequestInfo:(net::SSLCertRequestInfo*)certRequestInfo
delegate:
(std::unique_ptr<content::ClientCertificateDelegate>)
delegate {
DCHECK(browserContext);
DCHECK(certRequestInfo);
if ((self = [super init])) {
observer_.reset(new SSLClientAuthObserverCocoaBridge(
browserContext, certRequestInfo, std::move(delegate), self));
}
return self;
}
- (void)sheetDidEnd:(NSWindow*)sheet
returnCode:(NSInteger)returnCode
context:(void*)context {
net::X509Certificate* cert = NULL;
if (returnCode == NSFileHandlingPanelOKButton) {
CFRange range = CFRangeMake(0, CFArrayGetCount(identities_));
CFIndex index =
CFArrayGetFirstIndexOfValue(identities_, range, [panel_ identity]);
if (index != -1)
cert = certificates_[index].get();
else
NOTREACHED();
}
// See comment at definition; this works around a 10.12 bug.
ClearTableViewDataSourcesIfNeeded(sheet);
if (!closePending_) {
// If |closePending_| is already set, |closeSheetWithAnimation:| was called
// already to cancel the selection rather than continue with no
// certificate. Otherwise, tell the backend which identity (or none) the
// user selected.
userResponded_ = YES;
observer_->CertificateSelected(cert);
constrainedWindow_->CloseWebContentsModalDialog();
}
}
- (void)displayForWebContents:(content::WebContents*)webContents {
// Create an array of CFIdentityRefs for the certificates:
size_t numCerts = observer_->cert_request_info()->client_certs.size();
identities_.reset(CFArrayCreateMutable(
kCFAllocatorDefault, numCerts, &kCFTypeArrayCallBacks));
for (size_t i = 0; i < numCerts; ++i) {
SecCertificateRef cert =
observer_->cert_request_info()->client_certs[i]->os_cert_handle();
SecIdentityRef identity;
if (SecIdentityCreateWithCertificate(NULL, cert, &identity) == noErr) {
CFArrayAppendValue(identities_, identity);
CFRelease(identity);
certificates_.push_back(observer_->cert_request_info()->client_certs[i]);
}
}
// Get the message to display:
NSString* message = l10n_util::GetNSStringF(
IDS_CLIENT_CERT_DIALOG_TEXT,
base::ASCIIToUTF16(
observer_->cert_request_info()->host_and_port.ToString()));
// Create and set up a system choose-identity panel.
panel_.reset([[SFChooseIdentityPanel alloc] init]);
[panel_ setInformativeText:message];
[panel_ setDefaultButtonTitle:l10n_util::GetNSString(IDS_OK)];
[panel_ setAlternateButtonTitle:l10n_util::GetNSString(IDS_CANCEL)];
SecPolicyRef sslPolicy;
if (net::x509_util::CreateSSLClientPolicy(&sslPolicy) == noErr) {
[panel_ setPolicies:(id)sslPolicy];
CFRelease(sslPolicy);
}
constrainedWindow_ =
CreateAndShowWebModalDialogMac(observer_.get(), webContents, self);
observer_->StartObserving();
}
- (void)closeWebContentsModalDialog {
DCHECK(constrainedWindow_);
constrainedWindow_->CloseWebContentsModalDialog();
}
- (NSWindow*)overlayWindow {
return overlayWindow_;
}
- (SFChooseIdentityPanel*)panel {
return panel_;
}
- (void)showSheetForWindow:(NSWindow*)window {
NSString* title = l10n_util::GetNSString(IDS_CLIENT_CERT_DIALOG_TITLE);
overlayWindow_.reset([window retain]);
[panel_ beginSheetForWindow:window
modalDelegate:self
didEndSelector:@selector(sheetDidEnd:returnCode:context:)
contextInfo:NULL
identities:base::mac::CFToNSCast(identities_)
message:title];
}
- (void)closeSheetWithAnimation:(BOOL)withAnimation {
if (!userResponded_) {
// If the sheet is closed by closing the tab rather than the user explicitly
// hitting Cancel, |closeSheetWithAnimation:| gets called before
// |sheetDidEnd:|. In this case, the selection should be canceled rather
// than continue with no certificate. The |returnCode| parameter to
// |sheetDidEnd:| is the same in both cases.
observer_->CancelCertificateSelection();
}
closePending_ = YES;
overlayWindow_.reset();
// Closing the sheet using -[NSApp endSheet:] doesn't work so use the private
// method.
[panel_ _dismissWithCode:NSFileHandlingPanelCancelButton];
}
- (void)hideSheet {
NSWindow* sheetWindow = [overlayWindow_ attachedSheet];
[sheetWindow setAlphaValue:0.0];
[sheetWindow setIgnoresMouseEvents:YES];
oldResizesSubviews_ = [[sheetWindow contentView] autoresizesSubviews];
[[sheetWindow contentView] setAutoresizesSubviews:NO];
}
- (void)unhideSheet {
NSWindow* sheetWindow = [overlayWindow_ attachedSheet];
[[sheetWindow contentView] setAutoresizesSubviews:oldResizesSubviews_];
[sheetWindow setAlphaValue:1.0];
[sheetWindow setIgnoresMouseEvents:NO];
}
- (void)pulseSheet {
// NOOP
}
- (void)makeSheetKeyAndOrderFront {
[[overlayWindow_ attachedSheet] makeKeyAndOrderFront:nil];
}
- (void)updateSheetPosition {
// NOOP
}
- (void)resizeWithNewSize:(NSSize)size {
// NOOP
}
- (NSWindow*)sheetWindow {
return panel_;
}
- (void)onConstrainedWindowClosed {
observer_->StopObserving();
panel_.reset();
constrainedWindow_.reset();
[self release];
}
@end
|