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

Side by Side Diff: components/cronet/ios/Cronet.mm

Issue 2928653002: [Cronet-iOS] Public-Key-Pinning Tests (Closed)
Patch Set: Fixed DEPS Created 3 years, 6 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 unified diff | Download patch
« no previous file with comments | « components/cronet/ios/Cronet.h ('k') | components/cronet/ios/cronet_environment.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/cronet/ios/Cronet.h" 5 #import "components/cronet/ios/Cronet.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 23 matching lines...) Expand all
34 34
35 BOOL gHttp2Enabled = YES; 35 BOOL gHttp2Enabled = YES;
36 BOOL gQuicEnabled = NO; 36 BOOL gQuicEnabled = NO;
37 cronet::URLRequestContextConfig::HttpCacheType gHttpCache = 37 cronet::URLRequestContextConfig::HttpCacheType gHttpCache =
38 cronet::URLRequestContextConfig::HttpCacheType::DISK; 38 cronet::URLRequestContextConfig::HttpCacheType::DISK;
39 ScopedVector<cronet::URLRequestContextConfig::QuicHint> gQuicHints; 39 ScopedVector<cronet::URLRequestContextConfig::QuicHint> gQuicHints;
40 NSString* gExperimentalOptions = @"{}"; 40 NSString* gExperimentalOptions = @"{}";
41 NSString* gUserAgent = nil; 41 NSString* gUserAgent = nil;
42 BOOL gUserAgentPartial = NO; 42 BOOL gUserAgentPartial = NO;
43 NSString* gSslKeyLogFileName = nil; 43 NSString* gSslKeyLogFileName = nil;
44 ScopedVector<cronet::URLRequestContextConfig::Pkp> gPkpList = {};
44 RequestFilterBlock gRequestFilterBlock = nil; 45 RequestFilterBlock gRequestFilterBlock = nil;
45 base::LazyInstance<std::unique_ptr<CronetHttpProtocolHandlerDelegate>>::Leaky 46 base::LazyInstance<std::unique_ptr<CronetHttpProtocolHandlerDelegate>>::Leaky
46 gHttpProtocolHandlerDelegate = LAZY_INSTANCE_INITIALIZER; 47 gHttpProtocolHandlerDelegate = LAZY_INSTANCE_INITIALIZER;
47 NSURLCache* gPreservedSharedURLCache = nil; 48 NSURLCache* gPreservedSharedURLCache = nil;
48 BOOL gEnableTestCertVerifierForTesting = FALSE; 49 BOOL gEnableTestCertVerifierForTesting = FALSE;
50 std::unique_ptr<net::CertVerifier> gMockCertVerifier;
49 NSString* gAcceptLanguages = nil; 51 NSString* gAcceptLanguages = nil;
50 52
51 // CertVerifier, which allows any certificates for testing. 53 // CertVerifier, which allows any certificates for testing.
52 class TestCertVerifier : public net::CertVerifier { 54 class TestCertVerifier : public net::CertVerifier {
53 int Verify(const RequestParams& params, 55 int Verify(const RequestParams& params,
54 net::CRLSet* crl_set, 56 net::CRLSet* crl_set,
55 net::CertVerifyResult* verify_result, 57 net::CertVerifyResult* verify_result,
56 const net::CompletionCallback& callback, 58 const net::CompletionCallback& callback,
57 std::unique_ptr<Request>* out_req, 59 std::unique_ptr<Request>* out_req,
58 const net::NetLogWithSource& net_log) override { 60 const net::NetLogWithSource& net_log) override {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 112
111 @implementation Cronet 113 @implementation Cronet
112 114
113 + (void)configureCronetEnvironmentForTesting: 115 + (void)configureCronetEnvironmentForTesting:
114 (cronet::CronetEnvironment*)cronetEnvironment { 116 (cronet::CronetEnvironment*)cronetEnvironment {
115 if (gEnableTestCertVerifierForTesting) { 117 if (gEnableTestCertVerifierForTesting) {
116 std::unique_ptr<TestCertVerifier> test_cert_verifier = 118 std::unique_ptr<TestCertVerifier> test_cert_verifier =
117 base::MakeUnique<TestCertVerifier>(); 119 base::MakeUnique<TestCertVerifier>();
118 cronetEnvironment->set_mock_cert_verifier(std::move(test_cert_verifier)); 120 cronetEnvironment->set_mock_cert_verifier(std::move(test_cert_verifier));
119 } 121 }
122 if (gMockCertVerifier) {
123 gChromeNet.Get()->set_mock_cert_verifier(std::move(gMockCertVerifier));
124 }
120 } 125 }
121 126
122 + (NSString*)getAcceptLanguagesFromPreferredLanguages: 127 + (NSString*)getAcceptLanguagesFromPreferredLanguages:
123 (NSArray<NSString*>*)languages { 128 (NSArray<NSString*>*)languages {
124 NSMutableArray* acceptLanguages = [NSMutableArray new]; 129 NSMutableArray* acceptLanguages = [NSMutableArray new];
125 for (NSString* lang_region in languages) { 130 for (NSString* lang_region in languages) {
126 NSString* lang = [lang_region componentsSeparatedByString:@"-"][0]; 131 NSString* lang = [lang_region componentsSeparatedByString:@"-"][0];
127 NSString* localeAcceptLangs = acceptLangs[lang_region] ?: acceptLangs[lang]; 132 NSString* localeAcceptLangs = acceptLangs[lang_region] ?: acceptLangs[lang];
128 if (localeAcceptLangs) 133 if (localeAcceptLangs)
129 [acceptLanguages 134 [acceptLanguages
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 } 208 }
204 } 209 }
205 210
206 + (void)setRequestFilterBlock:(RequestFilterBlock)block { 211 + (void)setRequestFilterBlock:(RequestFilterBlock)block {
207 if (gHttpProtocolHandlerDelegate.Get().get()) 212 if (gHttpProtocolHandlerDelegate.Get().get())
208 gHttpProtocolHandlerDelegate.Get().get()->SetRequestFilterBlock(block); 213 gHttpProtocolHandlerDelegate.Get().get()->SetRequestFilterBlock(block);
209 else 214 else
210 gRequestFilterBlock = block; 215 gRequestFilterBlock = block;
211 } 216 }
212 217
218 + (BOOL)addPublicKeyPinsForHost:(NSString*)host
219 pinHashes:(NSSet<NSData*>*)pinHashes
220 includeSubdomains:(BOOL)includeSubdomains
221 expirationDate:(NSDate*)expirationDate
222 error:(NSError**)outError {
223 [self checkNotStarted];
224
225 auto pkp = base::MakeUnique<cronet::URLRequestContextConfig::Pkp>(
226 base::SysNSStringToUTF8(host), includeSubdomains,
227 base::Time::FromCFAbsoluteTime(
228 [expirationDate timeIntervalSinceReferenceDate]));
229
230 for (NSData* hash in pinHashes) {
231 net::SHA256HashValue hashValue = net::SHA256HashValue();
232 if (sizeof(hashValue.data) != hash.length) {
233 *outError =
234 [self createIllegalArgumentErrorWithArgument:@"pinHashes"
235 reason:
236 @"The length of PKP SHA256 "
237 @"hash should be 256 bits"];
238 return NO;
239 }
240 memcpy((void*)(hashValue.data), [hash bytes], sizeof(hashValue.data));
241 pkp->pin_hashes.push_back(net::HashValue(hashValue));
242 }
243 gPkpList.push_back(std::move(pkp));
244 if (outError) {
245 *outError = nil;
246 }
247 return YES;
248 }
249
213 + (void)startInternal { 250 + (void)startInternal {
214 std::string user_agent = base::SysNSStringToUTF8(gUserAgent); 251 std::string user_agent = base::SysNSStringToUTF8(gUserAgent);
215 252
216 gChromeNet.Get().reset( 253 gChromeNet.Get().reset(
217 new cronet::CronetEnvironment(user_agent, gUserAgentPartial)); 254 new cronet::CronetEnvironment(user_agent, gUserAgentPartial));
218 255
219 gChromeNet.Get()->set_accept_language( 256 gChromeNet.Get()->set_accept_language(
220 base::SysNSStringToUTF8(gAcceptLanguages ?: [self getAcceptLanguages])); 257 base::SysNSStringToUTF8(gAcceptLanguages ?: [self getAcceptLanguages]));
221 258
222 gChromeNet.Get()->set_http2_enabled(gHttp2Enabled); 259 gChromeNet.Get()->set_http2_enabled(gHttp2Enabled);
223 gChromeNet.Get()->set_quic_enabled(gQuicEnabled); 260 gChromeNet.Get()->set_quic_enabled(gQuicEnabled);
224 gChromeNet.Get()->set_experimental_options( 261 gChromeNet.Get()->set_experimental_options(
225 base::SysNSStringToUTF8(gExperimentalOptions)); 262 base::SysNSStringToUTF8(gExperimentalOptions));
226 gChromeNet.Get()->set_http_cache(gHttpCache); 263 gChromeNet.Get()->set_http_cache(gHttpCache);
227 gChromeNet.Get()->set_ssl_key_log_file_name( 264 gChromeNet.Get()->set_ssl_key_log_file_name(
228 base::SysNSStringToUTF8(gSslKeyLogFileName)); 265 base::SysNSStringToUTF8(gSslKeyLogFileName));
266 gChromeNet.Get()->set_pkp_list(std::move(gPkpList));
229 for (const auto* quicHint : gQuicHints) { 267 for (const auto* quicHint : gQuicHints) {
230 gChromeNet.Get()->AddQuicHint(quicHint->host, quicHint->port, 268 gChromeNet.Get()->AddQuicHint(quicHint->host, quicHint->port,
231 quicHint->alternate_port); 269 quicHint->alternate_port);
232 } 270 }
233 271
234 [self configureCronetEnvironmentForTesting:gChromeNet.Get().get()]; 272 [self configureCronetEnvironmentForTesting:gChromeNet.Get().get()];
235 gChromeNet.Get()->Start(); 273 gChromeNet.Get()->Start();
236 gHttpProtocolHandlerDelegate.Get().reset( 274 gHttpProtocolHandlerDelegate.Get().reset(
237 new CronetHttpProtocolHandlerDelegate( 275 new CronetHttpProtocolHandlerDelegate(
238 gChromeNet.Get()->GetURLRequestContextGetter(), gRequestFilterBlock)); 276 gChromeNet.Get()->GetURLRequestContextGetter(), gRequestFilterBlock));
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 return nil; 370 return nil;
333 } 371 }
334 std::vector<uint8_t> deltas(gChromeNet.Get()->GetHistogramDeltas()); 372 std::vector<uint8_t> deltas(gChromeNet.Get()->GetHistogramDeltas());
335 return [NSData dataWithBytes:deltas.data() length:deltas.size()]; 373 return [NSData dataWithBytes:deltas.data() length:deltas.size()];
336 } 374 }
337 375
338 + (void)enableTestCertVerifierForTesting { 376 + (void)enableTestCertVerifierForTesting {
339 gEnableTestCertVerifierForTesting = YES; 377 gEnableTestCertVerifierForTesting = YES;
340 } 378 }
341 379
380 + (void)setMockCertVerifierForTesting:
381 (std::unique_ptr<net::CertVerifier>)certVerifier {
382 gMockCertVerifier = std::move(certVerifier);
383 }
384
342 + (void)setHostResolverRulesForTesting:(NSString*)hostResolverRulesForTesting { 385 + (void)setHostResolverRulesForTesting:(NSString*)hostResolverRulesForTesting {
343 DCHECK(gChromeNet.Get().get()); 386 DCHECK(gChromeNet.Get().get());
344 gChromeNet.Get()->SetHostResolverRules( 387 gChromeNet.Get()->SetHostResolverRules(
345 base::SysNSStringToUTF8(hostResolverRulesForTesting)); 388 base::SysNSStringToUTF8(hostResolverRulesForTesting));
346 } 389 }
347 390
348 // This is a non-public dummy method that prevents the linker from stripping out 391 // This is a non-public dummy method that prevents the linker from stripping out
349 // the otherwise non-referenced methods from 'bidirectional_stream.cc'. 392 // the otherwise non-referenced methods from 'bidirectional_stream.cc'.
350 + (void)preventStrippingCronetBidirectionalStream { 393 + (void)preventStrippingCronetBidirectionalStream {
351 bidirectional_stream_create(NULL, 0, 0); 394 bidirectional_stream_create(NULL, 0, 0);
352 } 395 }
353 396
397 + (NSError*)createIllegalArgumentErrorWithArgument:(NSString*)argumentName
398 reason:(NSString*)reason {
399 NSMutableDictionary* errorDictionary =
400 [[NSMutableDictionary alloc] initWithDictionary:@{
401 NSLocalizedDescriptionKey :
402 [NSString stringWithFormat:@"Invalid argument: %@", argumentName],
403 CRNInvalidArgumentKey : argumentName
404 }];
405 if (reason) {
406 errorDictionary[NSLocalizedFailureReasonErrorKey] = reason;
407 }
408 return [self createCronetErrorWith:CRNErrorInvalidArgument
409 userInfo:errorDictionary];
410 }
411
412 + (NSError*)createCronetErrorWith:(int)errorCode
413 userInfo:(NSDictionary*)userInfo {
414 return [NSError errorWithDomain:CRNCronetErrorDomain
415 code:errorCode
416 userInfo:userInfo];
417 }
418
354 @end 419 @end
OLDNEW
« no previous file with comments | « components/cronet/ios/Cronet.h ('k') | components/cronet/ios/cronet_environment.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698