OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ios/web/web_state/wk_web_view_security_util.h" | 5 #import "ios/web/web_state/wk_web_view_security_util.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 #include <Security/Security.h> | 8 #include <Security/Security.h> |
9 | 9 |
10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "crypto/rsa_private_key.h" | 12 #include "crypto/rsa_private_key.h" |
13 #include "ios/web/public/test/web_test_util.h" | 13 #include "ios/web/public/test/web_test_util.h" |
14 #include "net/cert/x509_cert_types.h" | 14 #include "net/cert/x509_cert_types.h" |
15 #include "net/cert/x509_certificate.h" | 15 #include "net/cert/x509_certificate.h" |
16 #include "net/cert/x509_util.h" | 16 #include "net/cert/x509_util.h" |
17 #include "net/ssl/ssl_info.h" | 17 #include "net/ssl/ssl_info.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "testing/gtest_mac.h" |
19 #include "testing/platform_test.h" | 20 #include "testing/platform_test.h" |
20 | 21 |
21 namespace web { | 22 namespace web { |
22 namespace { | 23 namespace { |
23 // Subject for testing self-signed certificate. | 24 // Subject for testing self-signed certificate. |
24 const char kTestSubject[] = "self-signed"; | 25 const char kTestSubject[] = "self-signed"; |
| 26 // Hostname for testing SecTrustRef objects. |
| 27 NSString* const kTestHost = @"www.example.com"; |
25 | 28 |
26 // Returns an autoreleased certificate chain for testing. Chain will contain a | 29 // Returns an autoreleased certificate chain for testing. Chain will contain a |
27 // single self-signed cert with |subject| as a subject. | 30 // single self-signed cert with |subject| as a subject. |
28 NSArray* MakeTestCertChain(const std::string& subject) { | 31 NSArray* MakeTestCertChain(const std::string& subject) { |
29 scoped_ptr<crypto::RSAPrivateKey> private_key; | 32 scoped_ptr<crypto::RSAPrivateKey> private_key; |
30 std::string der_cert; | 33 std::string der_cert; |
31 net::x509_util::CreateKeyAndSelfSignedCert( | 34 net::x509_util::CreateKeyAndSelfSignedCert( |
32 "CN=" + subject, 1, base::Time::Now(), | 35 "CN=" + subject, 1, base::Time::Now(), |
33 base::Time::Now() + base::TimeDelta::FromDays(1), &private_key, | 36 base::Time::Now() + base::TimeDelta::FromDays(1), &private_key, |
34 &der_cert); | 37 &der_cert); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 CreateTestTrust(MakeTestCertChain(kTestSubject)); | 104 CreateTestTrust(MakeTestCertChain(kTestSubject)); |
102 scoped_refptr<net::X509Certificate> cert = CreateCertFromTrust(trust); | 105 scoped_refptr<net::X509Certificate> cert = CreateCertFromTrust(trust); |
103 EXPECT_TRUE(cert->subject().GetDisplayName() == kTestSubject); | 106 EXPECT_TRUE(cert->subject().GetDisplayName() == kTestSubject); |
104 } | 107 } |
105 | 108 |
106 // Tests CreateCertFromTrust with nil trust. | 109 // Tests CreateCertFromTrust with nil trust. |
107 TEST_F(WKWebViewSecurityUtilTest, CreationCertFromNilTrust) { | 110 TEST_F(WKWebViewSecurityUtilTest, CreationCertFromNilTrust) { |
108 EXPECT_FALSE(CreateCertFromTrust(nil)); | 111 EXPECT_FALSE(CreateCertFromTrust(nil)); |
109 } | 112 } |
110 | 113 |
| 114 // Tests CreateServerTrustFromChain with valid input. |
| 115 TEST_F(WKWebViewSecurityUtilTest, CreationServerTrust) { |
| 116 // Create server trust. |
| 117 NSArray* chain = MakeTestCertChain(kTestSubject); |
| 118 base::ScopedCFTypeRef<SecTrustRef> server_trust( |
| 119 CreateServerTrustFromChain(chain, kTestHost)); |
| 120 EXPECT_TRUE(server_trust); |
| 121 |
| 122 // Verify chain. |
| 123 EXPECT_EQ(static_cast<CFIndex>(chain.count), |
| 124 SecTrustGetCertificateCount(server_trust)); |
| 125 [chain enumerateObjectsUsingBlock:^(id expected_cert, NSUInteger i, BOOL*) { |
| 126 id actual_cert = static_cast<id>(SecTrustGetCertificateAtIndex( |
| 127 server_trust.get(), static_cast<CFIndex>(i))); |
| 128 EXPECT_EQ(expected_cert, actual_cert); |
| 129 }]; |
| 130 |
| 131 // Verify policies. |
| 132 CFArrayRef policies = nullptr; |
| 133 EXPECT_EQ(errSecSuccess, SecTrustCopyPolicies(server_trust.get(), &policies)); |
| 134 EXPECT_EQ(1, CFArrayGetCount(policies)); |
| 135 SecPolicyRef policy = (SecPolicyRef)CFArrayGetValueAtIndex(policies, 0); |
| 136 base::ScopedCFTypeRef<CFDictionaryRef> properties( |
| 137 SecPolicyCopyProperties(policy)); |
| 138 NSString* name = static_cast<NSString*>( |
| 139 CFDictionaryGetValue(properties.get(), kSecPolicyName)); |
| 140 EXPECT_NSEQ(kTestHost, name); |
| 141 CFRelease(policies); |
| 142 } |
| 143 |
| 144 // Tests CreateServerTrustFromChain with nil chain. |
| 145 TEST_F(WKWebViewSecurityUtilTest, CreationServerTrustFromNilChain) { |
| 146 EXPECT_FALSE(CreateServerTrustFromChain(nil, kTestHost)); |
| 147 } |
| 148 |
| 149 // Tests CreateServerTrustFromChain with empty chain. |
| 150 TEST_F(WKWebViewSecurityUtilTest, CreationServerTrustFromEmptyChain) { |
| 151 EXPECT_FALSE(CreateServerTrustFromChain(@[], kTestHost)); |
| 152 } |
| 153 |
111 // Tests that IsWKWebViewSSLCertError returns YES for NSError with | 154 // Tests that IsWKWebViewSSLCertError returns YES for NSError with |
112 // NSURLErrorDomain domain, NSURLErrorSecureConnectionFailed error code and | 155 // NSURLErrorDomain domain, NSURLErrorSecureConnectionFailed error code and |
113 // certificate chain. | 156 // certificate chain. |
114 TEST_F(WKWebViewSecurityUtilTest, CheckSecureConnectionFailedWithCertError) { | 157 TEST_F(WKWebViewSecurityUtilTest, CheckSecureConnectionFailedWithCertError) { |
115 CR_TEST_REQUIRES_WK_WEB_VIEW(); | 158 CR_TEST_REQUIRES_WK_WEB_VIEW(); |
116 | 159 |
117 EXPECT_TRUE(IsWKWebViewSSLCertError([NSError | 160 EXPECT_TRUE(IsWKWebViewSSLCertError([NSError |
118 errorWithDomain:NSURLErrorDomain | 161 errorWithDomain:NSURLErrorDomain |
119 code:NSURLErrorSecureConnectionFailed | 162 code:NSURLErrorSecureConnectionFailed |
120 userInfo:MakeTestSSLCertErrorUserInfo()])); | 163 userInfo:MakeTestSSLCertErrorUserInfo()])); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 code:NSURLErrorServerCertificateHasUnknownRoot | 244 code:NSURLErrorServerCertificateHasUnknownRoot |
202 userInfo:MakeTestSSLCertErrorUserInfo()]; | 245 userInfo:MakeTestSSLCertErrorUserInfo()]; |
203 | 246 |
204 net::SSLInfo info; | 247 net::SSLInfo info; |
205 GetSSLInfoFromWKWebViewSSLCertError(unknownCertError, &info); | 248 GetSSLInfoFromWKWebViewSSLCertError(unknownCertError, &info); |
206 EXPECT_TRUE(info.is_valid()); | 249 EXPECT_TRUE(info.is_valid()); |
207 EXPECT_EQ(net::CERT_STATUS_INVALID, info.cert_status); | 250 EXPECT_EQ(net::CERT_STATUS_INVALID, info.cert_status); |
208 EXPECT_TRUE(info.cert->subject().GetDisplayName() == kTestSubject); | 251 EXPECT_TRUE(info.cert->subject().GetDisplayName() == kTestSubject); |
209 } | 252 } |
210 | 253 |
| 254 // Tests GetSecurityStyleFromTrustResult with bad SecTrustResultType result. |
| 255 TEST_F(WKWebViewSecurityUtilTest, GetSecurityStyleFromBadResult) { |
| 256 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATION_BROKEN, |
| 257 GetSecurityStyleFromTrustResult(kSecTrustResultDeny)); |
| 258 EXPECT_EQ( |
| 259 SECURITY_STYLE_AUTHENTICATION_BROKEN, |
| 260 GetSecurityStyleFromTrustResult(kSecTrustResultRecoverableTrustFailure)); |
| 261 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATION_BROKEN, |
| 262 GetSecurityStyleFromTrustResult(kSecTrustResultFatalTrustFailure)); |
| 263 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATION_BROKEN, |
| 264 GetSecurityStyleFromTrustResult(kSecTrustResultOtherError)); |
| 265 } |
| 266 |
| 267 // Tests GetSecurityStyleFromTrustResult with good SecTrustResultType result. |
| 268 TEST_F(WKWebViewSecurityUtilTest, GetSecurityStyleFromGoodResult) { |
| 269 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATED, |
| 270 GetSecurityStyleFromTrustResult(kSecTrustResultProceed)); |
| 271 EXPECT_EQ(SECURITY_STYLE_AUTHENTICATED, |
| 272 GetSecurityStyleFromTrustResult(kSecTrustResultUnspecified)); |
| 273 } |
| 274 |
| 275 // Tests GetSecurityStyleFromTrustResult with invalid SecTrustResultType result. |
| 276 TEST_F(WKWebViewSecurityUtilTest, GetSecurityStyleFromInvalidResult) { |
| 277 EXPECT_EQ(SECURITY_STYLE_UNKNOWN, |
| 278 GetSecurityStyleFromTrustResult(kSecTrustResultInvalid)); |
| 279 } |
| 280 |
211 } // namespace web | 281 } // namespace web |
OLD | NEW |