| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/ssl_client_certificate_selector_cocoa.h" | 5 #import "chrome/browser/ui/cocoa/ssl_client_certificate_selector_cocoa.h" |
| 6 | 6 |
| 7 #import <SecurityInterface/SFChooseIdentityPanel.h> | 7 #import <SecurityInterface/SFChooseIdentityPanel.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/mac/foundation_util.h" | 13 #include "base/mac/foundation_util.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "chrome/browser/ssl/ssl_client_auth_observer.h" | 18 #include "chrome/browser/ssl/ssl_client_auth_observer.h" |
| 19 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" | 19 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" |
| 20 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
| 21 #include "components/guest_view/browser/guest_view_base.h" | 21 #include "components/guest_view/browser/guest_view_base.h" |
| 22 #include "components/strings/grit/components_strings.h" | 22 #include "components/strings/grit/components_strings.h" |
| 23 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 23 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 25 #include "content/public/browser/client_certificate_delegate.h" | 25 #include "content/public/browser/client_certificate_delegate.h" |
| 26 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 27 #include "net/cert/x509_certificate.h" | 27 #include "net/cert/x509_certificate.h" |
| 28 #include "net/cert/x509_util_mac.h" | 28 #include "net/cert/x509_util_mac.h" |
| 29 #include "net/ssl/ssl_cert_request_info.h" | 29 #include "net/ssl/ssl_cert_request_info.h" |
| 30 #include "net/ssl/ssl_platform_key_mac.h" |
| 30 #include "ui/base/cocoa/window_size_constants.h" | 31 #include "ui/base/cocoa/window_size_constants.h" |
| 31 #include "ui/base/l10n/l10n_util_mac.h" | 32 #include "ui/base/l10n/l10n_util_mac.h" |
| 32 | 33 |
| 33 using content::BrowserThread; | 34 using content::BrowserThread; |
| 34 | 35 |
| 35 @interface SFChooseIdentityPanel (SystemPrivate) | 36 @interface SFChooseIdentityPanel (SystemPrivate) |
| 36 // A system-private interface that dismisses a panel whose sheet was started by | 37 // A system-private interface that dismisses a panel whose sheet was started by |
| 37 // -beginSheetForWindow:modalDelegate:didEndSelector:contextInfo:identities:mess
age: | 38 // -beginSheetForWindow:modalDelegate:didEndSelector:contextInfo:identities:mess
age: |
| 38 // as though the user clicked the button identified by returnCode. Verified | 39 // as though the user clicked the button identified by returnCode. Verified |
| 39 // present in 10.5 through 10.8. | 40 // present in 10.5 through 10.12. |
| 40 - (void)_dismissWithCode:(NSInteger)code; | 41 - (void)_dismissWithCode:(NSInteger)code; |
| 41 @end | 42 @end |
| 42 | 43 |
| 43 @interface SSLClientCertificateSelectorCocoa () | 44 @interface SSLClientCertificateSelectorCocoa () |
| 44 - (void)onConstrainedWindowClosed; | 45 - (void)onConstrainedWindowClosed; |
| 45 @end | 46 @end |
| 46 | 47 |
| 47 class SSLClientAuthObserverCocoaBridge : public SSLClientAuthObserver, | 48 class SSLClientAuthObserverCocoaBridge : public SSLClientAuthObserver, |
| 48 public ConstrainedWindowMacDelegate { | 49 public ConstrainedWindowMacDelegate { |
| 49 public: | 50 public: |
| (...skipping 24 matching lines...) Expand all Loading... |
| 74 | 75 |
| 75 private: | 76 private: |
| 76 SSLClientCertificateSelectorCocoa* controller_; // weak | 77 SSLClientCertificateSelectorCocoa* controller_; // weak |
| 77 }; | 78 }; |
| 78 | 79 |
| 79 namespace chrome { | 80 namespace chrome { |
| 80 | 81 |
| 81 void ShowSSLClientCertificateSelector( | 82 void ShowSSLClientCertificateSelector( |
| 82 content::WebContents* contents, | 83 content::WebContents* contents, |
| 83 net::SSLCertRequestInfo* cert_request_info, | 84 net::SSLCertRequestInfo* cert_request_info, |
| 84 net::CertificateList client_certs, | 85 net::ClientCertIdentityList client_certs, |
| 85 std::unique_ptr<content::ClientCertificateDelegate> delegate) { | 86 std::unique_ptr<content::ClientCertificateDelegate> delegate) { |
| 86 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 87 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 87 | 88 |
| 88 // Not all WebContentses can show modal dialogs. | 89 // Not all WebContentses can show modal dialogs. |
| 89 // | 90 // |
| 90 // Use the top-level embedder if |contents| is a guest. | 91 // Use the top-level embedder if |contents| is a guest. |
| 91 // GetTopLevelWebContents() will return |contents| otherwise. | 92 // GetTopLevelWebContents() will return |contents| otherwise. |
| 92 // TODO(davidben): Move this hook to the WebContentsDelegate and only try to | 93 // TODO(davidben): Move this hook to the WebContentsDelegate and only try to |
| 93 // show a dialog in Browser's implementation. https://crbug.com/456255 | 94 // show a dialog in Browser's implementation. https://crbug.com/456255 |
| 94 if (web_modal::WebContentsModalDialogManager::FromWebContents( | 95 if (web_modal::WebContentsModalDialogManager::FromWebContents( |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 if ((self = [super init])) { | 171 if ((self = [super init])) { |
| 171 observer_.reset(new SSLClientAuthObserverCocoaBridge( | 172 observer_.reset(new SSLClientAuthObserverCocoaBridge( |
| 172 browserContext, certRequestInfo, std::move(delegate), self)); | 173 browserContext, certRequestInfo, std::move(delegate), self)); |
| 173 } | 174 } |
| 174 return self; | 175 return self; |
| 175 } | 176 } |
| 176 | 177 |
| 177 - (void)sheetDidEnd:(NSWindow*)sheet | 178 - (void)sheetDidEnd:(NSWindow*)sheet |
| 178 returnCode:(NSInteger)returnCode | 179 returnCode:(NSInteger)returnCode |
| 179 context:(void*)context { | 180 context:(void*)context { |
| 180 net::X509Certificate* cert = NULL; | 181 net::ClientCertIdentity* cert = nullptr; |
| 181 if (returnCode == NSFileHandlingPanelOKButton) { | 182 if (returnCode == NSFileHandlingPanelOKButton) { |
| 182 CFRange range = CFRangeMake(0, CFArrayGetCount(identities_)); | 183 CFRange range = CFRangeMake(0, CFArrayGetCount(sec_identities_)); |
| 183 CFIndex index = | 184 CFIndex index = |
| 184 CFArrayGetFirstIndexOfValue(identities_, range, [panel_ identity]); | 185 CFArrayGetFirstIndexOfValue(sec_identities_, range, [panel_ identity]); |
| 185 if (index != -1) | 186 if (index != -1) |
| 186 cert = certificates_[index].get(); | 187 cert = cert_identities_[index].get(); |
| 187 else | 188 else |
| 188 NOTREACHED(); | 189 NOTREACHED(); |
| 189 } | 190 } |
| 190 | 191 |
| 191 // See comment at definition; this works around a 10.12 bug. | 192 // See comment at definition; this works around a 10.12 bug. |
| 192 ClearTableViewDataSourcesIfNeeded(sheet); | 193 ClearTableViewDataSourcesIfNeeded(sheet); |
| 193 | 194 |
| 194 if (!closePending_) { | 195 if (!closePending_) { |
| 195 // If |closePending_| is already set, |closeSheetWithAnimation:| was called | 196 // If |closePending_| is already set, |closeSheetWithAnimation:| was called |
| 196 // already to cancel the selection rather than continue with no | 197 // already to cancel the selection rather than continue with no |
| 197 // certificate. Otherwise, tell the backend which identity (or none) the | 198 // certificate. Otherwise, tell the backend which identity (or none) the |
| 198 // user selected. | 199 // user selected. |
| 199 userResponded_ = YES; | 200 userResponded_ = YES; |
| 200 observer_->CertificateSelected(cert); | 201 |
| 202 if (cert) { |
| 203 observer_->CertificateSelected( |
| 204 cert->certificate(), |
| 205 CreateSSLPrivateKeyForSecIdentity(cert->certificate(), |
| 206 cert->sec_identity_ref()) |
| 207 .get()); |
| 208 } else { |
| 209 observer_->CertificateSelected(nullptr, nullptr); |
| 210 } |
| 201 | 211 |
| 202 constrainedWindow_->CloseWebContentsModalDialog(); | 212 constrainedWindow_->CloseWebContentsModalDialog(); |
| 203 } | 213 } |
| 204 } | 214 } |
| 205 | 215 |
| 206 - (void)displayForWebContents:(content::WebContents*)webContents | 216 - (void)displayForWebContents:(content::WebContents*)webContents |
| 207 clientCerts:(net::CertificateList)inputClientCerts { | 217 clientCerts:(net::ClientCertIdentityList)inputClientCerts { |
| 218 cert_identities_ = std::move(inputClientCerts); |
| 208 // Create an array of CFIdentityRefs for the certificates: | 219 // Create an array of CFIdentityRefs for the certificates: |
| 209 size_t numCerts = inputClientCerts.size(); | 220 size_t numCerts = cert_identities_.size(); |
| 210 identities_.reset(CFArrayCreateMutable( | 221 sec_identities_.reset(CFArrayCreateMutable(kCFAllocatorDefault, numCerts, |
| 211 kCFAllocatorDefault, numCerts, &kCFTypeArrayCallBacks)); | 222 &kCFTypeArrayCallBacks)); |
| 212 for (size_t i = 0; i < numCerts; ++i) { | 223 for (size_t i = 0; i < numCerts; ++i) { |
| 213 base::ScopedCFTypeRef<SecCertificateRef> cert( | 224 DCHECK(cert_identities_[i]->sec_identity_ref()); |
| 214 net::x509_util::CreateSecCertificateFromX509Certificate( | 225 CFArrayAppendValue(sec_identities_, |
| 215 inputClientCerts[i].get())); | 226 cert_identities_[i]->sec_identity_ref()); |
| 216 if (!cert) | |
| 217 continue; | |
| 218 SecIdentityRef identity; | |
| 219 if (SecIdentityCreateWithCertificate(NULL, cert, &identity) == noErr) { | |
| 220 CFArrayAppendValue(identities_, identity); | |
| 221 CFRelease(identity); | |
| 222 certificates_.push_back(inputClientCerts[i]); | |
| 223 } | |
| 224 } | 227 } |
| 225 | 228 |
| 226 // Get the message to display: | 229 // Get the message to display: |
| 227 NSString* message = l10n_util::GetNSStringF( | 230 NSString* message = l10n_util::GetNSStringF( |
| 228 IDS_CLIENT_CERT_DIALOG_TEXT, | 231 IDS_CLIENT_CERT_DIALOG_TEXT, |
| 229 base::ASCIIToUTF16( | 232 base::ASCIIToUTF16( |
| 230 observer_->cert_request_info()->host_and_port.ToString())); | 233 observer_->cert_request_info()->host_and_port.ToString())); |
| 231 | 234 |
| 232 // Create and set up a system choose-identity panel. | 235 // Create and set up a system choose-identity panel. |
| 233 panel_.reset([[SFChooseIdentityPanel alloc] init]); | 236 panel_.reset([[SFChooseIdentityPanel alloc] init]); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 258 return panel_; | 261 return panel_; |
| 259 } | 262 } |
| 260 | 263 |
| 261 - (void)showSheetForWindow:(NSWindow*)window { | 264 - (void)showSheetForWindow:(NSWindow*)window { |
| 262 NSString* title = l10n_util::GetNSString(IDS_CLIENT_CERT_DIALOG_TITLE); | 265 NSString* title = l10n_util::GetNSString(IDS_CLIENT_CERT_DIALOG_TITLE); |
| 263 overlayWindow_.reset([window retain]); | 266 overlayWindow_.reset([window retain]); |
| 264 [panel_ beginSheetForWindow:window | 267 [panel_ beginSheetForWindow:window |
| 265 modalDelegate:self | 268 modalDelegate:self |
| 266 didEndSelector:@selector(sheetDidEnd:returnCode:context:) | 269 didEndSelector:@selector(sheetDidEnd:returnCode:context:) |
| 267 contextInfo:NULL | 270 contextInfo:NULL |
| 268 identities:base::mac::CFToNSCast(identities_) | 271 identities:base::mac::CFToNSCast(sec_identities_) |
| 269 message:title]; | 272 message:title]; |
| 270 } | 273 } |
| 271 | 274 |
| 272 - (void)closeSheetWithAnimation:(BOOL)withAnimation { | 275 - (void)closeSheetWithAnimation:(BOOL)withAnimation { |
| 273 if (!userResponded_) { | 276 if (!userResponded_) { |
| 274 // If the sheet is closed by closing the tab rather than the user explicitly | 277 // If the sheet is closed by closing the tab rather than the user explicitly |
| 275 // hitting Cancel, |closeSheetWithAnimation:| gets called before | 278 // hitting Cancel, |closeSheetWithAnimation:| gets called before |
| 276 // |sheetDidEnd:|. In this case, the selection should be canceled rather | 279 // |sheetDidEnd:|. In this case, the selection should be canceled rather |
| 277 // than continue with no certificate. The |returnCode| parameter to | 280 // than continue with no certificate. The |returnCode| parameter to |
| 278 // |sheetDidEnd:| is the same in both cases. | 281 // |sheetDidEnd:| is the same in both cases. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 } | 325 } |
| 323 | 326 |
| 324 - (void)onConstrainedWindowClosed { | 327 - (void)onConstrainedWindowClosed { |
| 325 observer_->StopObserving(); | 328 observer_->StopObserving(); |
| 326 panel_.reset(); | 329 panel_.reset(); |
| 327 constrainedWindow_.reset(); | 330 constrainedWindow_.reset(); |
| 328 [self release]; | 331 [self release]; |
| 329 } | 332 } |
| 330 | 333 |
| 331 @end | 334 @end |
| OLD | NEW |