Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(437)

Unified Diff: ios/web/web_state/ui/crw_wk_web_view_web_controller.mm

Issue 1322193003: WKWebView(iOS9): correctly update SSL status for current navigation item (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@reland_cert_verification
Patch Set: Merged with origin/master Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/web/web_state/ui/crw_wk_web_view_web_controller.mm
diff --git a/ios/web/web_state/ui/crw_wk_web_view_web_controller.mm b/ios/web/web_state/ui/crw_wk_web_view_web_controller.mm
index e5c88664c2057d26c56a89ea4e4b249886d6e8fb..b5c930d4e547e2030eb5a5ecc6d87ddbca0cd79d 100644
--- a/ios/web/web_state/ui/crw_wk_web_view_web_controller.mm
+++ b/ios/web/web_state/ui/crw_wk_web_view_web_controller.mm
@@ -11,6 +11,7 @@
#include "base/json/json_reader.h"
#import "base/mac/scoped_nsobject.h"
#include "base/macros.h"
+#include "base/metrics/histogram_macros.h"
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
#import "ios/net/http_response_headers_util.h"
@@ -82,6 +83,14 @@ WKWebViewErrorSource WKWebViewErrorSourceFromError(NSError* error) {
} // namespace
+#if !defined(__IPHONE_9_0)
stuartmorgan 2015/10/06 22:02:36 You're missing: || __IPHONE_OS_VERSION_MAX_ALLOWE
Eugene But (OOO till 7-30) 2015/10/07 15:27:13 Done.
+// Predeclare iOS9 API so calls are compiled on iOS8 bots.
+// TODO(eugenebut): cleanup this (crbug.com/523365).
+@interface WKWebView (CRWIOS9API)
+@property(nonatomic, readonly, copy) NSArray* certificateChain;
+@end
+#endif
+
@interface CRWWKWebViewWebController () <WKNavigationDelegate,
WKScriptMessageHandler,
WKUIDelegate> {
@@ -250,12 +259,21 @@ WKWebViewErrorSource WKWebViewErrorSourceFromError(NSError* error) {
// Clears all activity indicator tasks for this web controller.
- (void)clearActivityIndicatorTasks;
+// Obtains SSL status from given |certChain| and updates navigation items with
+// given |certID| and |host|.
+- (void)updateSSLStatusForNavigationItemsWithCertID:(int)certID
+ host:(NSString*)host
+ usingCertChain:(NSArray*)certChain;
+
#if !defined(ENABLE_CHROME_NET_STACK_FOR_WKWEBVIEW)
// Updates SSL status for the current navigation item based on the information
// provided by web view.
- (void)updateSSLStatusForCurrentNavigationItem;
#endif
+// Reports "WebController.WKWebViewHasCertForSecureConnection" UMA.
+- (void)reportHasCertForSecureConnectionUMAWithValue:(bool)value;
+
// Registers load request with empty referrer and link or client redirect
// transition based on user interaction state.
- (void)registerLoadRequest:(const GURL&)url;
@@ -864,12 +882,54 @@ WKWebViewErrorSource WKWebViewErrorSourceFromError(NSError* error) {
clearNetworkTasksForGroup:[self activityIndicatorGroupID]];
}
+- (void)updateSSLStatusForNavigationItemsWithCertID:(int)certID
+ host:(NSString*)host
+ usingCertChain:(NSArray*)certChain {
+ base::WeakNSObject<CRWWKWebViewWebController> weakSelf(self);
+ void (^SSLStatusResponse)(web::SecurityStyle, net::CertStatus) = ^(
+ web::SecurityStyle style, net::CertStatus certStatus) {
+ base::scoped_nsobject<CRWWKWebViewWebController> strongSelf(
+ [weakSelf retain]);
+ if (!strongSelf || [strongSelf isBeingDestroyed]) {
+ return;
+ }
+
+ web::NavigationManager* navigationManager =
+ [strongSelf webStateImpl]->GetNavigationManager();
+ int currentItemIndex = navigationManager->GetCurrentEntryIndex();
stuartmorgan 2015/10/06 22:02:36 Are we guaranteed that the lookup can't be for a p
Eugene But (OOO till 7-30) 2015/10/07 15:27:13 There is no need to do a lookup for a pending item
+
+ bool updatedCurrentItem = false;
+ std::string GURLHost = base::SysNSStringToUTF8(host);
+ for (int i = 0; i < navigationManager->GetEntryCount(); i++) {
+ web::NavigationItem* item = navigationManager->GetItemAtIndex(i);
+ web::SSLStatus& SSLStatus = item->GetSSL();
+ if (SSLStatus.cert_id == certID && item->GetURL().host() == GURLHost) {
+ web::SSLStatus previousSSLStatus = item->GetSSL();
+ SSLStatus.cert_status = certStatus;
+ SSLStatus.security_style = style;
+ if (currentItemIndex == i && !previousSSLStatus.Equals(SSLStatus)) {
+ updatedCurrentItem = true;
+ }
+ }
+ }
+
+ if (updatedCurrentItem) {
+ [strongSelf didUpdateSSLStatusForCurrentNavigationItem];
+ }
+ };
+
+ [_certVerificationController
+ querySSLStatusForTrust:web::CreateServerTrustFromChain(certChain, host)
+ host:host
+ completionHandler:SSLStatusResponse];
+}
+
#if !defined(ENABLE_CHROME_NET_STACK_FOR_WKWEBVIEW)
+
- (void)updateSSLStatusForCurrentNavigationItem {
if ([self isBeingDestroyed])
return;
- DCHECK(self.webStateImpl);
web::NavigationItem* item =
self.webStateImpl->GetNavigationManagerImpl().GetLastCommittedItem();
if (!item)
@@ -877,34 +937,61 @@ WKWebViewErrorSource WKWebViewErrorSourceFromError(NSError* error) {
web::SSLStatus previousSSLStatus = item->GetSSL();
web::SSLStatus& SSLStatus = item->GetSSL();
- if (item->GetURL().SchemeIsCryptographic()) {
- // TODO(eugenebut): Do not set security style to authenticated once
- // proceeding with bad ssl cert is implemented.
- SSLStatus.security_style = web::SECURITY_STYLE_AUTHENTICATED;
- SSLStatus.content_status = [_wkWebView hasOnlySecureContent]
- ? web::SSLStatus::NORMAL_CONTENT
- : web::SSLStatus::DISPLAYED_INSECURE_CONTENT;
-
- if (base::ios::IsRunningOnIOS9OrLater()) {
- scoped_refptr<net::X509Certificate> cert(web::CreateCertFromChain(
- [_wkWebView performSelector:@selector(certificateChain)]));
- if (cert) {
- SSLStatus.cert_id = web::CertStore::GetInstance()->StoreCert(
- cert.get(), self.certGroupID);
- } else {
- SSLStatus.security_style = web::SECURITY_STYLE_UNAUTHENTICATED;
- SSLStatus.cert_id = 0;
+
+ // Starting from iOS9 WKWebView blocks active mixed content, so if
+ // |hasOnlySecureContent| returns NO it means passive content.
+ // On iOS8 there is no way to determine if web view has active mixed content.
+ SSLStatus.content_status = [_wkWebView hasOnlySecureContent]
+ ? web::SSLStatus::NORMAL_CONTENT
+ : web::SSLStatus::DISPLAYED_INSECURE_CONTENT;
+
+ // Retrieve top level frame certificate.
stuartmorgan 2015/10/06 22:02:36 Since the innermost code in the block is updating
Eugene But (OOO till 7-30) 2015/10/07 15:27:13 Updated comment. I can't really factor much of the
+ scoped_refptr<net::X509Certificate> cert;
+ if (base::ios::IsRunningOnIOS9OrLater() &&
+ item->GetURL().SchemeIsCryptographic()) {
+ NSArray* chain = [_wkWebView certificateChain];
+ cert = web::CreateCertFromChain(chain);
+ if (cert) {
+ [self reportHasCertForSecureConnectionUMAWithValue:true];
+ int oldCertID = SSLStatus.cert_id;
+ SSLStatus.cert_id = web::CertStore::GetInstance()->StoreCert(
+ cert.get(), self.certGroupID);
+ NSString* host = [_wkWebView URL].host;
stuartmorgan 2015/10/06 22:02:36 Are you sure you want the raw web view URL, rather
Eugene But (OOO till 7-30) 2015/10/07 15:27:13 I wanted to avoid std::string -> NSString conversi
+ if (oldCertID != SSLStatus.cert_id ||
+ item->GetURL().host() != base::SysNSStringToUTF8(host)) {
+ [self updateSSLStatusForNavigationItemsWithCertID:SSLStatus.cert_id
+ host:host
+ usingCertChain:chain];
}
}
- } else {
- SSLStatus.security_style = web::SECURITY_STYLE_UNAUTHENTICATED;
+ }
+
+ if (!cert) {
SSLStatus.cert_id = 0;
+ if (!item->GetURL().SchemeIsCryptographic()) {
+ // HTTP or other non-secure connection.
+ SSLStatus.security_style = web::SECURITY_STYLE_UNAUTHENTICATED;
+ } else if (base::ios::IsRunningOnIOS9OrLater()) {
+ // HTTPS, iOS9 and no certificate (this use-case has not been observed).
+ [self reportHasCertForSecureConnectionUMAWithValue:false];
+ SSLStatus.security_style = web::SECURITY_STYLE_UNKNOWN;
+ } else {
+ // HTTPS, iOS8.
+ // iOS8 cannot load unauthenticated HTTPS content.
+ SSLStatus.security_style = web::SECURITY_STYLE_AUTHENTICATED;
+ }
}
if (!previousSSLStatus.Equals(SSLStatus)) {
[self didUpdateSSLStatusForCurrentNavigationItem];
}
}
+
+- (void)reportHasCertForSecureConnectionUMAWithValue:(bool)value {
+ UMA_HISTOGRAM_BOOLEAN("WebController.WKWebViewHasCertForSecureConnection",
+ value);
+}
+
#endif // !defined(ENABLE_CHROME_NET_STACK_FOR_WKWEBVIEW)
- (void)registerLoadRequest:(const GURL&)url {

Powered by Google App Engine
This is Rietveld 408576698