Undo collision with r243390.
[WebKit-https.git] / Source / WebKit / NetworkProcess / cocoa / NetworkSessionCocoa.mm
1 /*
2  * Copyright (C) 2015-2019 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "NetworkSessionCocoa.h"
28
29 #import "AuthenticationChallengeDisposition.h"
30 #import "AuthenticationManager.h"
31 #import "DataReference.h"
32 #import "Download.h"
33 #import "LegacyCustomProtocolManager.h"
34 #import "Logging.h"
35 #import "NetworkLoad.h"
36 #import "NetworkProcess.h"
37 #import "NetworkSessionCreationParameters.h"
38 #import <Foundation/NSURLSession.h>
39 #import <WebCore/Credential.h>
40 #import <WebCore/FormDataStreamMac.h>
41 #import <WebCore/FrameLoaderTypes.h>
42 #import <WebCore/NetworkStorageSession.h>
43 #import <WebCore/NotImplemented.h>
44 #import <WebCore/ResourceError.h>
45 #import <WebCore/ResourceRequest.h>
46 #import <WebCore/ResourceResponse.h>
47 #import <WebCore/SharedBuffer.h>
48 #import <WebCore/WebCoreURLResponse.h>
49 #import <pal/spi/cf/CFNetworkSPI.h>
50 #import <wtf/MainThread.h>
51 #import <wtf/NeverDestroyed.h>
52 #import <wtf/ProcessPrivilege.h>
53 #import <wtf/URL.h>
54 #import <wtf/text/WTFString.h>
55
56 #if USE(APPLE_INTERNAL_SDK)
57 #include <WebKitAdditions/NetworkSessionCocoaAdditions.h>
58 #endif
59
60 using namespace WebKit;
61
62 CFStringRef const WebKit2HTTPProxyDefaultsKey = static_cast<CFStringRef>(@"WebKit2HTTPProxy");
63 CFStringRef const WebKit2HTTPSProxyDefaultsKey = static_cast<CFStringRef>(@"WebKit2HTTPSProxy");
64
65 static NSURLSessionResponseDisposition toNSURLSessionResponseDisposition(WebCore::PolicyAction disposition)
66 {
67     switch (disposition) {
68     case WebCore::PolicyAction::StopAllLoads:
69         ASSERT_NOT_REACHED();
70 #if ASSERT_DISABLED
71         FALLTHROUGH;
72 #endif
73     case WebCore::PolicyAction::Ignore:
74         return NSURLSessionResponseCancel;
75     case WebCore::PolicyAction::Use:
76         return NSURLSessionResponseAllow;
77     case WebCore::PolicyAction::Download:
78         return NSURLSessionResponseBecomeDownload;
79     }
80 }
81
82 static NSURLSessionAuthChallengeDisposition toNSURLSessionAuthChallengeDisposition(WebKit::AuthenticationChallengeDisposition disposition)
83 {
84     switch (disposition) {
85     case WebKit::AuthenticationChallengeDisposition::UseCredential:
86         return NSURLSessionAuthChallengeUseCredential;
87     case WebKit::AuthenticationChallengeDisposition::PerformDefaultHandling:
88         return NSURLSessionAuthChallengePerformDefaultHandling;
89     case WebKit::AuthenticationChallengeDisposition::Cancel:
90         return NSURLSessionAuthChallengeCancelAuthenticationChallenge;
91     case WebKit::AuthenticationChallengeDisposition::RejectProtectionSpaceAndContinue:
92         return NSURLSessionAuthChallengeRejectProtectionSpace;
93     }
94 }
95
96 static WebCore::NetworkLoadPriority toNetworkLoadPriority(float priority)
97 {
98     if (priority <= NSURLSessionTaskPriorityLow)
99         return WebCore::NetworkLoadPriority::Low;
100     if (priority >= NSURLSessionTaskPriorityHigh)
101         return WebCore::NetworkLoadPriority::High;
102     return WebCore::NetworkLoadPriority::Medium;
103 }
104
105 #if HAVE(CFNETWORK_NEGOTIATED_SSL_PROTOCOL_CIPHER)
106 static String stringForSSLProtocol(SSLProtocol protocol)
107 {
108     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
109     switch (protocol) {
110     case kDTLSProtocol1:
111         return "DTLS 1.0"_s;
112     case kSSLProtocol2:
113         return "SSL 2.0"_s;
114     case kSSLProtocol3:
115         return "SSL 3.0"_s;
116     case kSSLProtocol3Only:
117         return "SSL 3.0 (Only)"_s;
118     case kTLSProtocol1:
119         return "TLS 1.0"_s;
120     case kTLSProtocol1Only:
121         return "TLS 1.0 (Only)"_s;
122     case kTLSProtocol11:
123         return "TLS 1.1"_s;
124     case kTLSProtocol12:
125         return "TLS 1.2"_s;
126     case kTLSProtocol13:
127         return "TLS 1.3"_s;
128     case kSSLProtocolAll:
129         return "All";
130     case kSSLProtocolUnknown:
131         return "Unknown";
132     case kTLSProtocolMaxSupported:
133     default:
134         ASSERT_NOT_REACHED();
135         return emptyString();
136     }
137     ALLOW_DEPRECATED_DECLARATIONS_END
138 }
139
140 static String stringForSSLCipher(SSLCipherSuite cipher)
141 {
142 #define STRINGIFY_CIPHER(cipher) \
143     case cipher: \
144         return "" #cipher ""_s
145
146     switch (cipher) {
147     STRINGIFY_CIPHER(SSL_RSA_EXPORT_WITH_RC4_40_MD5);
148     STRINGIFY_CIPHER(SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5);
149     STRINGIFY_CIPHER(SSL_RSA_WITH_IDEA_CBC_SHA);
150     STRINGIFY_CIPHER(SSL_RSA_EXPORT_WITH_DES40_CBC_SHA);
151     STRINGIFY_CIPHER(SSL_RSA_WITH_DES_CBC_SHA);
152     STRINGIFY_CIPHER(SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA);
153     STRINGIFY_CIPHER(SSL_DH_DSS_WITH_DES_CBC_SHA);
154     STRINGIFY_CIPHER(SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA);
155     STRINGIFY_CIPHER(SSL_DH_RSA_WITH_DES_CBC_SHA);
156     STRINGIFY_CIPHER(SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA);
157     STRINGIFY_CIPHER(SSL_DHE_DSS_WITH_DES_CBC_SHA);
158     STRINGIFY_CIPHER(SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA);
159     STRINGIFY_CIPHER(SSL_DHE_RSA_WITH_DES_CBC_SHA);
160     STRINGIFY_CIPHER(SSL_DH_anon_EXPORT_WITH_RC4_40_MD5);
161     STRINGIFY_CIPHER(SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA);
162     STRINGIFY_CIPHER(SSL_DH_anon_WITH_DES_CBC_SHA);
163     STRINGIFY_CIPHER(SSL_FORTEZZA_DMS_WITH_NULL_SHA);
164     STRINGIFY_CIPHER(SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA);
165     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_128_CBC_SHA);
166     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_128_CBC_SHA);
167     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_128_CBC_SHA);
168     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_128_CBC_SHA);
169     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_128_CBC_SHA);
170     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_128_CBC_SHA);
171     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_256_CBC_SHA);
172     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_256_CBC_SHA);
173     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_256_CBC_SHA);
174     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_256_CBC_SHA);
175     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_256_CBC_SHA);
176     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_256_CBC_SHA);
177     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_NULL_SHA);
178     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_RC4_128_SHA);
179     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA);
180     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
181     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA);
182     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_NULL_SHA);
183     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_RC4_128_SHA);
184     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA);
185     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
186     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA);
187     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_NULL_SHA);
188     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_RC4_128_SHA);
189     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA);
190     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
191     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
192     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_NULL_SHA);
193     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_RC4_128_SHA);
194     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA);
195     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
196     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA);
197     STRINGIFY_CIPHER(TLS_ECDH_anon_WITH_NULL_SHA);
198     STRINGIFY_CIPHER(TLS_ECDH_anon_WITH_RC4_128_SHA);
199     STRINGIFY_CIPHER(TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA);
200     STRINGIFY_CIPHER(TLS_ECDH_anon_WITH_AES_128_CBC_SHA);
201     STRINGIFY_CIPHER(TLS_ECDH_anon_WITH_AES_256_CBC_SHA);
202     // STRINGIFY_CIPHER(SSL_NULL_WITH_NULL_NULL);
203     STRINGIFY_CIPHER(TLS_NULL_WITH_NULL_NULL);
204     // STRINGIFY_CIPHER(SSL_RSA_WITH_NULL_MD5);
205     STRINGIFY_CIPHER(TLS_RSA_WITH_NULL_MD5);
206     // STRINGIFY_CIPHER(SSL_RSA_WITH_NULL_SHA);
207     STRINGIFY_CIPHER(TLS_RSA_WITH_NULL_SHA);
208     // STRINGIFY_CIPHER(SSL_RSA_WITH_RC4_128_MD5);
209     STRINGIFY_CIPHER(TLS_RSA_WITH_RC4_128_MD5);
210     // STRINGIFY_CIPHER(SSL_RSA_WITH_RC4_128_SHA);
211     STRINGIFY_CIPHER(TLS_RSA_WITH_RC4_128_SHA);
212     // STRINGIFY_CIPHER(SSL_RSA_WITH_3DES_EDE_CBC_SHA);
213     STRINGIFY_CIPHER(TLS_RSA_WITH_3DES_EDE_CBC_SHA);
214     STRINGIFY_CIPHER(TLS_RSA_WITH_NULL_SHA256);
215     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_128_CBC_SHA256);
216     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_256_CBC_SHA256);
217     // STRINGIFY_CIPHER(SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA);
218     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA);
219     // STRINGIFY_CIPHER(SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA);
220     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA);
221     // STRINGIFY_CIPHER(SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA);
222     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA);
223     // STRINGIFY_CIPHER(SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA);
224     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA);
225     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_128_CBC_SHA256);
226     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_128_CBC_SHA256);
227     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_128_CBC_SHA256);
228     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_128_CBC_SHA256);
229     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_256_CBC_SHA256);
230     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_256_CBC_SHA256);
231     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_256_CBC_SHA256);
232     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_256_CBC_SHA256);
233     // STRINGIFY_CIPHER(SSL_DH_anon_WITH_RC4_128_MD5);
234     STRINGIFY_CIPHER(TLS_DH_anon_WITH_RC4_128_MD5);
235     // STRINGIFY_CIPHER(SSL_DH_anon_WITH_3DES_EDE_CBC_SHA);
236     STRINGIFY_CIPHER(TLS_DH_anon_WITH_3DES_EDE_CBC_SHA);
237     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_128_CBC_SHA256);
238     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_256_CBC_SHA256);
239     STRINGIFY_CIPHER(TLS_PSK_WITH_RC4_128_SHA);
240     STRINGIFY_CIPHER(TLS_PSK_WITH_3DES_EDE_CBC_SHA);
241     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_128_CBC_SHA);
242     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_256_CBC_SHA);
243     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_RC4_128_SHA);
244     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA);
245     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_128_CBC_SHA);
246     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_256_CBC_SHA);
247     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_RC4_128_SHA);
248     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA);
249     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_128_CBC_SHA);
250     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_256_CBC_SHA);
251     STRINGIFY_CIPHER(TLS_PSK_WITH_NULL_SHA);
252     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_NULL_SHA);
253     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_NULL_SHA);
254     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_128_GCM_SHA256);
255     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_256_GCM_SHA384);
256     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_128_GCM_SHA256);
257     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_256_GCM_SHA384);
258     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_128_GCM_SHA256);
259     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_256_GCM_SHA384);
260     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_128_GCM_SHA256);
261     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_256_GCM_SHA384);
262     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_128_GCM_SHA256);
263     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_256_GCM_SHA384);
264     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_128_GCM_SHA256);
265     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_256_GCM_SHA384);
266     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_128_GCM_SHA256);
267     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_256_GCM_SHA384);
268     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_128_GCM_SHA256);
269     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_256_GCM_SHA384);
270     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_128_GCM_SHA256);
271     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_256_GCM_SHA384);
272     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_128_CBC_SHA256);
273     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_256_CBC_SHA384);
274     STRINGIFY_CIPHER(TLS_PSK_WITH_NULL_SHA256);
275     STRINGIFY_CIPHER(TLS_PSK_WITH_NULL_SHA384);
276     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_128_CBC_SHA256);
277     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_256_CBC_SHA384);
278     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_NULL_SHA256);
279     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_NULL_SHA384);
280     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_128_CBC_SHA256);
281     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_256_CBC_SHA384);
282     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_NULL_SHA256);
283     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_NULL_SHA384);
284     STRINGIFY_CIPHER(TLS_AES_128_GCM_SHA256);
285     STRINGIFY_CIPHER(TLS_AES_256_GCM_SHA384);
286     STRINGIFY_CIPHER(TLS_CHACHA20_POLY1305_SHA256);
287     STRINGIFY_CIPHER(TLS_AES_128_CCM_SHA256);
288     STRINGIFY_CIPHER(TLS_AES_128_CCM_8_SHA256);
289     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256);
290     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384);
291     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256);
292     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384);
293     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256);
294     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384);
295     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256);
296     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384);
297     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256);
298     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384);
299     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256);
300     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384);
301     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256);
302     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384);
303     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256);
304     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384);
305     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
306     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
307     STRINGIFY_CIPHER(TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
308     STRINGIFY_CIPHER(SSL_RSA_WITH_RC2_CBC_MD5);
309     STRINGIFY_CIPHER(SSL_RSA_WITH_IDEA_CBC_MD5);
310     STRINGIFY_CIPHER(SSL_RSA_WITH_DES_CBC_MD5);
311     STRINGIFY_CIPHER(SSL_RSA_WITH_3DES_EDE_CBC_MD5);
312     STRINGIFY_CIPHER(SSL_NO_SUCH_CIPHERSUITE);
313     default:
314         ASSERT_NOT_REACHED();
315         return emptyString();
316     }
317
318 #undef STRINGIFY_CIPHER
319 }
320 #endif
321
322 @interface WKNetworkSessionDelegate : NSObject <NSURLSessionDataDelegate> {
323     RefPtr<WebKit::NetworkSessionCocoa> _session;
324     bool _withCredentials;
325 }
326
327 - (id)initWithNetworkSession:(WebKit::NetworkSessionCocoa&)session withCredentials:(bool)withCredentials;
328 - (void)sessionInvalidated;
329
330 @end
331
332 @implementation WKNetworkSessionDelegate
333
334 - (id)initWithNetworkSession:(WebKit::NetworkSessionCocoa&)session withCredentials:(bool)withCredentials
335 {
336     self = [super init];
337     if (!self)
338         return nil;
339
340     _session = &session;
341     _withCredentials = withCredentials;
342
343     return self;
344 }
345
346 - (void)sessionInvalidated
347 {
348     _session = nullptr;
349 }
350
351 - (NetworkDataTaskCocoa*)existingTask:(NSURLSessionTask *)task
352 {
353     if (!_session)
354         return nullptr;
355
356     if (!task)
357         return nullptr;
358
359     auto storedCredentialsPolicy = _withCredentials ? WebCore::StoredCredentialsPolicy::Use : WebCore::StoredCredentialsPolicy::DoNotUse;
360     return _session->dataTaskForIdentifier(task.taskIdentifier, storedCredentialsPolicy);
361 }
362
363 - (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error
364 {
365     ASSERT(!_session);
366 }
367
368 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
369 {
370     if (auto* networkDataTask = [self existingTask:task])
371         networkDataTask->didSendData(totalBytesSent, totalBytesExpectedToSend);
372 }
373
374 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task needNewBodyStream:(void (^)(NSInputStream *bodyStream))completionHandler
375 {
376     auto* networkDataTask = [self existingTask:task];
377     if (!networkDataTask) {
378         completionHandler(nil);
379         return;
380     }
381
382     auto* body = networkDataTask->firstRequest().httpBody();
383     if (!body) {
384         completionHandler(nil);
385         return;
386     }
387
388     // FIXME: Call the completionHandler immediately once rdar://problem/28233746 is fixed.
389     RunLoop::main().dispatch([body = makeRef(*body), completionHandler = makeBlockPtr(completionHandler)] {
390         completionHandler(WebCore::createHTTPBodyNSInputStream(body.get()).get());
391     });
392 }
393
394 #if HAVE(CFNETWORK_WITH_IGNORE_HSTS) && ENABLE(RESOURCE_LOAD_STATISTICS)
395 static NSURLRequest* downgradeRequest(NSURLRequest *request)
396 {
397     NSMutableURLRequest *nsMutableRequest = [[request mutableCopy] autorelease];
398     if ([nsMutableRequest.URL.scheme isEqualToString:@"https"]) {
399         NSURLComponents *components = [NSURLComponents componentsWithURL:nsMutableRequest.URL resolvingAgainstBaseURL:NO];
400         components.scheme = @"http";
401         [nsMutableRequest setURL:components.URL];
402         ASSERT([nsMutableRequest.URL.scheme isEqualToString:@"http"]);
403         return nsMutableRequest;
404     }
405
406     ASSERT_NOT_REACHED();
407     return request;
408 }
409
410 static bool schemeWasUpgradedDueToDynamicHSTS(NSURLRequest *request)
411 {
412     return [request respondsToSelector:@selector(_schemeWasUpgradedDueToDynamicHSTS)]
413         && [request _schemeWasUpgradedDueToDynamicHSTS];
414 }
415 #endif
416
417 #if HAVE(CFNETWORK_WITH_IGNORE_HSTS)
418 static void setIgnoreHSTS(NSMutableURLRequest *request, bool ignoreHSTS)
419 {
420     if ([request respondsToSelector:@selector(_setIgnoreHSTS:)])
421         [request _setIgnoreHSTS:ignoreHSTS];
422 }
423
424 static bool ignoreHSTS(NSURLRequest *request)
425 {
426     return [request respondsToSelector:@selector(_ignoreHSTS)]
427         && [request _ignoreHSTS];
428 }
429 #endif
430
431 static NSURLRequest* updateIgnoreStrictTransportSecuritySettingIfNecessary(NSURLRequest *request, bool shouldIgnoreHSTS)
432 {
433 #if HAVE(CFNETWORK_WITH_IGNORE_HSTS)
434     if ([request.URL.scheme isEqualToString:@"https"] && shouldIgnoreHSTS && ignoreHSTS(request)) {
435         // The request was upgraded for some other reason than HSTS.
436         // Don't ignore HSTS to avoid the risk of another downgrade.
437         NSMutableURLRequest *nsMutableRequest = [[request mutableCopy] autorelease];
438         setIgnoreHSTS(nsMutableRequest, false);
439         return nsMutableRequest;
440     }
441     
442     if ([request.URL.scheme isEqualToString:@"http"] && ignoreHSTS(request) != shouldIgnoreHSTS) {
443         NSMutableURLRequest *nsMutableRequest = [[request mutableCopy] autorelease];
444         setIgnoreHSTS(nsMutableRequest, shouldIgnoreHSTS);
445         return nsMutableRequest;
446     }
447 #else
448     UNUSED_PARAM(shouldIgnoreHSTS);
449 #endif
450
451     return request;
452 }
453
454 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler
455 {
456     auto taskIdentifier = task.taskIdentifier;
457     LOG(NetworkSession, "%llu willPerformHTTPRedirection from %s to %s", taskIdentifier, response.URL.absoluteString.UTF8String, request.URL.absoluteString.UTF8String);
458
459     if (auto* networkDataTask = [self existingTask:task]) {
460         auto completionHandlerCopy = Block_copy(completionHandler);
461
462         bool shouldIgnoreHSTS = false;
463 #if HAVE(CFNETWORK_WITH_IGNORE_HSTS) && ENABLE(RESOURCE_LOAD_STATISTICS)        
464         shouldIgnoreHSTS = schemeWasUpgradedDueToDynamicHSTS(request) && _session->networkProcess().storageSession(_session->sessionID())->shouldBlockCookies(request, networkDataTask->frameID(), networkDataTask->pageID());
465         if (shouldIgnoreHSTS) {
466             request = downgradeRequest(request);
467             ASSERT([request.URL.scheme isEqualToString:@"http"]);
468             LOG(NetworkSession, "%llu Downgraded %s from https to http", taskIdentifier, request.URL.absoluteString.UTF8String);
469         }
470 #endif
471
472         networkDataTask->willPerformHTTPRedirection(response, request, [completionHandlerCopy, taskIdentifier, shouldIgnoreHSTS](auto&& request) {
473 #if !LOG_DISABLED
474             LOG(NetworkSession, "%llu willPerformHTTPRedirection completionHandler (%s)", taskIdentifier, request.url().string().utf8().data());
475 #else
476             UNUSED_PARAM(taskIdentifier);
477 #endif
478             auto nsRequest = request.nsURLRequest(WebCore::HTTPBodyUpdatePolicy::UpdateHTTPBody);
479             nsRequest = updateIgnoreStrictTransportSecuritySettingIfNecessary(nsRequest, shouldIgnoreHSTS);
480             completionHandlerCopy(nsRequest);
481             Block_release(completionHandlerCopy);
482         });
483     } else {
484         LOG(NetworkSession, "%llu willPerformHTTPRedirection completionHandler (nil)", taskIdentifier);
485         completionHandler(nil);
486     }
487 }
488
489 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask*)task _schemeUpgraded:(NSURLRequest*)request completionHandler:(void (^)(NSURLRequest*))completionHandler
490 {
491     auto taskIdentifier = task.taskIdentifier;
492     LOG(NetworkSession, "%llu _schemeUpgraded %s", taskIdentifier, request.URL.absoluteString.UTF8String);
493
494     if (auto* networkDataTask = [self existingTask:task]) {
495         bool shouldIgnoreHSTS = false;
496 #if HAVE(CFNETWORK_WITH_IGNORE_HSTS) && ENABLE(RESOURCE_LOAD_STATISTICS)
497         shouldIgnoreHSTS = schemeWasUpgradedDueToDynamicHSTS(request) && _session->networkProcess().storageSession(_session->sessionID())->shouldBlockCookies(request, networkDataTask->frameID(), networkDataTask->pageID());
498         if (shouldIgnoreHSTS) {
499             request = downgradeRequest(request);
500             ASSERT([request.URL.scheme isEqualToString:@"http"]);
501             LOG(NetworkSession, "%llu Downgraded %s from https to http", taskIdentifier, request.URL.absoluteString.UTF8String);
502         }
503 #endif
504
505         auto completionHandlerCopy = Block_copy(completionHandler);
506         networkDataTask->willPerformHTTPRedirection(WebCore::synthesizeRedirectResponseIfNecessary([task currentRequest], request, nil), request, [completionHandlerCopy, taskIdentifier, shouldIgnoreHSTS](auto&& request) {
507 #if !LOG_DISABLED
508             LOG(NetworkSession, "%llu _schemeUpgraded completionHandler (%s)", taskIdentifier, request.url().string().utf8().data());
509 #else
510             UNUSED_PARAM(taskIdentifier);
511 #endif
512             auto nsRequest = request.nsURLRequest(WebCore::HTTPBodyUpdatePolicy::UpdateHTTPBody);
513             nsRequest = updateIgnoreStrictTransportSecuritySettingIfNecessary(nsRequest, shouldIgnoreHSTS);
514             completionHandlerCopy(nsRequest);
515             Block_release(completionHandlerCopy);
516         });
517     } else {
518         LOG(NetworkSession, "%llu _schemeUpgraded completionHandler (nil)", taskIdentifier);
519         completionHandler(nil);
520     }
521 }
522
523 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask willCacheResponse:(NSCachedURLResponse *)proposedResponse completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler
524 {
525     if (!_session) {
526         completionHandler(nil);
527         return;
528     }
529
530     // FIXME: remove if <rdar://problem/20001985> is ever resolved.
531     if ([proposedResponse.response respondsToSelector:@selector(allHeaderFields)]
532         && [[(id)proposedResponse.response allHeaderFields] objectForKey:@"Content-Range"])
533         completionHandler(nil);
534     else
535         completionHandler(proposedResponse);
536 }
537
538 #if HAVE(CFNETWORK_NSURLSESSION_STRICTRUSTEVALUATE)
539 static bool canNSURLSessionTrustEvaluate()
540 {
541     return [NSURLSession respondsToSelector:@selector(_strictTrustEvaluate: queue: completionHandler:)];
542 }
543
544 static inline void processServerTrustEvaluation(NetworkSessionCocoa *session, NSURLAuthenticationChallenge *challenge, OSStatus trustResult, NetworkDataTaskCocoa::TaskIdentifier taskIdentifier, NetworkDataTaskCocoa* networkDataTask, CompletionHandler<void(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential)>&& completionHandler)
545 {
546     if (trustResult == noErr) {
547         completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
548         return;
549     }
550
551     session->continueDidReceiveChallenge(challenge, taskIdentifier, networkDataTask, [completionHandler = WTFMove(completionHandler), secTrust = retainPtr(challenge.protectionSpace.serverTrust)] (WebKit::AuthenticationChallengeDisposition disposition, const WebCore::Credential& credential) mutable {
552         // FIXME: UIProcess should send us back non nil credentials but the credential IPC encoder currently only serializes ns credentials for username/password.
553         if (disposition == WebKit::AuthenticationChallengeDisposition::UseCredential && !credential.nsCredential()) {
554             completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust: secTrust.get()]);
555             return;
556         }
557         completionHandler(toNSURLSessionAuthChallengeDisposition(disposition), credential.nsCredential());
558     });
559 }
560 #endif
561
562 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
563 {
564     if (!_session) {
565         completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
566         return;
567     }
568
569     auto taskIdentifier = task.taskIdentifier;
570     LOG(NetworkSession, "%llu didReceiveChallenge", taskIdentifier);
571     
572     // Proxy authentication is handled by CFNetwork internally. We can get here if the user cancels
573     // CFNetwork authentication dialog, and we shouldn't ask the client to display another one in that case.
574     if (challenge.protectionSpace.isProxy) {
575         completionHandler(NSURLSessionAuthChallengeUseCredential, nil);
576         return;
577     }
578
579     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
580         if (NetworkSessionCocoa::allowsSpecificHTTPSCertificateForHost(challenge))
581             return completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
582
583         // Handle server trust evaluation at platform-level if requested, for performance reasons and to use ATS defaults.
584         if (!_session->networkProcess().canHandleHTTPSServerTrustEvaluation()) {
585 #if HAVE(CFNETWORK_NSURLSESSION_STRICTRUSTEVALUATE)
586             if (canNSURLSessionTrustEvaluate()) {
587                 auto* networkDataTask = [self existingTask:task];
588                 auto decisionHandler = makeBlockPtr([_session = _session.copyRef(), completionHandler = makeBlockPtr(completionHandler), taskIdentifier, networkDataTask = RefPtr<NetworkDataTaskCocoa>(networkDataTask)](NSURLAuthenticationChallenge *challenge, OSStatus trustResult) mutable {
589                     processServerTrustEvaluation(_session.get(), challenge, trustResult, taskIdentifier, networkDataTask.get(), WTFMove(completionHandler));
590                 });
591                 [NSURLSession _strictTrustEvaluate:challenge queue:[NSOperationQueue mainQueue].underlyingQueue completionHandler:decisionHandler.get()];
592                 return;
593             }
594 #endif
595             return completionHandler(NSURLSessionAuthChallengeRejectProtectionSpace, nil);
596         }
597     }
598     _session->continueDidReceiveChallenge(challenge, taskIdentifier, [self existingTask:task], [completionHandler = makeBlockPtr(completionHandler)] (WebKit::AuthenticationChallengeDisposition disposition, const WebCore::Credential& credential) mutable {
599         completionHandler(toNSURLSessionAuthChallengeDisposition(disposition), credential.nsCredential());
600     });
601 }
602
603 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
604 {
605     if (!_session)
606         return;
607
608     LOG(NetworkSession, "%llu didCompleteWithError %@", task.taskIdentifier, error);
609     if (auto* networkDataTask = [self existingTask:task])
610         networkDataTask->didCompleteWithError(error, networkDataTask->networkLoadMetrics());
611     else if (error) {
612         auto downloadID = _session->takeDownloadID(task.taskIdentifier);
613         if (downloadID.downloadID()) {
614             if (auto* download = _session->networkProcess().downloadManager().download(downloadID)) {
615                 NSData *resumeData = nil;
616                 if (id userInfo = error.userInfo) {
617                     if ([userInfo isKindOfClass:[NSDictionary class]])
618                         resumeData = userInfo[@"NSURLSessionDownloadTaskResumeData"];
619                 }
620                 
621                 if (resumeData && [resumeData isKindOfClass:[NSData class]])
622                     download->didFail(error, { static_cast<const uint8_t*>(resumeData.bytes), resumeData.length });
623                 else
624                     download->didFail(error, { });
625             }
626         }
627     }
628 }
629
630 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics
631 {
632     LOG(NetworkSession, "%llu didFinishCollectingMetrics", task.taskIdentifier);
633     if (auto* networkDataTask = [self existingTask:task]) {
634         NSURLSessionTaskTransactionMetrics *m = metrics.transactionMetrics.lastObject;
635         NSDate *fetchStartDate = m.fetchStartDate;
636         NSTimeInterval domainLookupStartInterval = m.domainLookupStartDate ? [m.domainLookupStartDate timeIntervalSinceDate:fetchStartDate] : -1;
637         NSTimeInterval domainLookupEndInterval = m.domainLookupEndDate ? [m.domainLookupEndDate timeIntervalSinceDate:fetchStartDate] : -1;
638         NSTimeInterval connectStartInterval = m.connectStartDate ? [m.connectStartDate timeIntervalSinceDate:fetchStartDate] : -1;
639         NSTimeInterval secureConnectionStartInterval = m.secureConnectionStartDate ? [m.secureConnectionStartDate timeIntervalSinceDate:fetchStartDate] : -1;
640         NSTimeInterval connectEndInterval = m.connectEndDate ? [m.connectEndDate timeIntervalSinceDate:fetchStartDate] : -1;
641         NSTimeInterval requestStartInterval = [m.requestStartDate timeIntervalSinceDate:fetchStartDate];
642         NSTimeInterval responseStartInterval = [m.responseStartDate timeIntervalSinceDate:fetchStartDate];
643         NSTimeInterval responseEndInterval = [m.responseEndDate timeIntervalSinceDate:fetchStartDate];
644
645         auto& networkLoadMetrics = networkDataTask->networkLoadMetrics();
646         networkLoadMetrics.domainLookupStart = Seconds(domainLookupStartInterval);
647         networkLoadMetrics.domainLookupEnd = Seconds(domainLookupEndInterval);
648         networkLoadMetrics.connectStart = Seconds(connectStartInterval);
649         networkLoadMetrics.secureConnectionStart = Seconds(secureConnectionStartInterval);
650         networkLoadMetrics.connectEnd = Seconds(connectEndInterval);
651         networkLoadMetrics.requestStart = Seconds(requestStartInterval);
652         networkLoadMetrics.responseStart = Seconds(responseStartInterval);
653         networkLoadMetrics.responseEnd = Seconds(responseEndInterval);
654         networkLoadMetrics.markComplete();
655         networkLoadMetrics.protocol = String(m.networkProtocolName);
656
657         if (networkDataTask->shouldCaptureExtraNetworkLoadMetrics()) {
658             networkLoadMetrics.priority = toNetworkLoadPriority(task.priority);
659
660 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS_FAMILY) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000)
661             networkLoadMetrics.remoteAddress = String(m._remoteAddressAndPort);
662             networkLoadMetrics.connectionIdentifier = String([m._connectionIdentifier UUIDString]);
663 #endif
664
665 #if HAVE(CFNETWORK_NEGOTIATED_SSL_PROTOCOL_CIPHER)
666             networkLoadMetrics.tlsProtocol = stringForSSLProtocol(m._negotiatedTLSProtocol);
667             networkLoadMetrics.tlsCipher = stringForSSLCipher(m._negotiatedTLSCipher);
668 #endif
669
670             __block WebCore::HTTPHeaderMap requestHeaders;
671             [m.request.allHTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *name, NSString *value, BOOL *) {
672                 requestHeaders.set(String(name), String(value));
673             }];
674             networkLoadMetrics.requestHeaders = WTFMove(requestHeaders);
675
676 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS_FAMILY) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000)
677             uint64_t requestHeaderBytesSent = 0;
678             uint64_t responseHeaderBytesReceived = 0;
679             uint64_t responseBodyBytesReceived = 0;
680             uint64_t responseBodyDecodedSize = 0;
681
682             for (NSURLSessionTaskTransactionMetrics *transactionMetrics in metrics.transactionMetrics) {
683                 requestHeaderBytesSent += transactionMetrics._requestHeaderBytesSent;
684                 responseHeaderBytesReceived += transactionMetrics._responseHeaderBytesReceived;
685                 responseBodyBytesReceived += transactionMetrics._responseBodyBytesReceived;
686                 responseBodyDecodedSize += transactionMetrics._responseBodyBytesDecoded ? transactionMetrics._responseBodyBytesDecoded : transactionMetrics._responseBodyBytesReceived;
687             }
688
689             networkLoadMetrics.requestHeaderBytesSent = requestHeaderBytesSent;
690             networkLoadMetrics.requestBodyBytesSent = task.countOfBytesSent;
691             networkLoadMetrics.responseHeaderBytesReceived = responseHeaderBytesReceived;
692             networkLoadMetrics.responseBodyBytesReceived = responseBodyBytesReceived;
693             networkLoadMetrics.responseBodyDecodedSize = responseBodyDecodedSize;
694 #endif
695         }
696     }
697 }
698
699 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
700 {
701     auto taskIdentifier = dataTask.taskIdentifier;
702     LOG(NetworkSession, "%llu didReceiveResponse", taskIdentifier);
703     if (auto* networkDataTask = [self existingTask:dataTask]) {
704         ASSERT(RunLoop::isMain());
705         
706         // Avoid MIME type sniffing if the response comes back as 304 Not Modified.
707         int statusCode = [response respondsToSelector:@selector(statusCode)] ? [(id)response statusCode] : 0;
708         if (statusCode != 304) {
709             bool isMainResourceLoad = networkDataTask->firstRequest().requester() == WebCore::ResourceRequest::Requester::Main;
710             WebCore::adjustMIMETypeIfNecessary(response._CFURLResponse, isMainResourceLoad);
711         }
712
713         WebCore::ResourceResponse resourceResponse(response);
714         // Lazy initialization is not helpful in the WebKit2 case because we always end up initializing
715         // all the fields when sending the response to the WebContent process over IPC.
716         resourceResponse.disableLazyInitialization();
717
718         // FIXME: This cannot be eliminated until other code no longer relies on ResourceResponse's
719         // NetworkLoadMetrics. For example, PerformanceTiming.
720         copyTimingData([dataTask _timingData], resourceResponse.deprecatedNetworkLoadMetrics());
721
722         auto completionHandlerCopy = Block_copy(completionHandler);
723         networkDataTask->didReceiveResponse(WTFMove(resourceResponse), [completionHandlerCopy, taskIdentifier](WebCore::PolicyAction policyAction) {
724 #if !LOG_DISABLED
725             LOG(NetworkSession, "%llu didReceiveResponse completionHandler (%d)", taskIdentifier, policyAction);
726 #else
727             UNUSED_PARAM(taskIdentifier);
728 #endif
729             completionHandlerCopy(toNSURLSessionResponseDisposition(policyAction));
730             Block_release(completionHandlerCopy);
731         });
732     } else {
733         LOG(NetworkSession, "%llu didReceiveResponse completionHandler (cancel)", taskIdentifier);
734         completionHandler(NSURLSessionResponseCancel);
735     }
736 }
737
738 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
739 {
740     if (auto* networkDataTask = [self existingTask:dataTask])
741         networkDataTask->didReceiveData(WebCore::SharedBuffer::create(data));
742 }
743
744 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
745 {
746     if (!_session)
747         return;
748
749     auto downloadID = _session->takeDownloadID([downloadTask taskIdentifier]);
750     if (auto* download = _session->networkProcess().downloadManager().download(downloadID))
751         download->didFinish();
752 }
753
754 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
755 {
756     if (!_session)
757         return;
758
759     ASSERT_WITH_MESSAGE(![self existingTask:downloadTask], "The NetworkDataTask should be destroyed immediately after didBecomeDownloadTask returns");
760
761     auto downloadID = _session->downloadID([downloadTask taskIdentifier]);
762     if (auto* download = _session->networkProcess().downloadManager().download(downloadID))
763         download->didReceiveData(bytesWritten);
764 }
765
766 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
767 {
768     if (!_session)
769         return;
770
771     notImplemented();
772 }
773
774 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask
775 {
776     if (auto* networkDataTask = [self existingTask:dataTask]) {
777         Ref<NetworkDataTaskCocoa> protectedNetworkDataTask(*networkDataTask);
778         auto downloadID = networkDataTask->pendingDownloadID();
779         auto& downloadManager = _session->networkProcess().downloadManager();
780         auto download = std::make_unique<WebKit::Download>(downloadManager, downloadID, downloadTask, _session->sessionID(), networkDataTask->suggestedFilename());
781         networkDataTask->transferSandboxExtensionToDownload(*download);
782         ASSERT(FileSystem::fileExists(networkDataTask->pendingDownloadLocation()));
783         download->didCreateDestination(networkDataTask->pendingDownloadLocation());
784         downloadManager.dataTaskBecameDownloadTask(downloadID, WTFMove(download));
785
786         _session->addDownloadID([downloadTask taskIdentifier], downloadID);
787     }
788 }
789
790 @end
791
792 namespace WebKit {
793
794 #if !ASSERT_DISABLED
795 static bool sessionsCreated = false;
796 #endif
797
798 static NSURLSessionConfiguration *configurationForSessionID(const PAL::SessionID& session)
799 {
800     if (session.isEphemeral()) {
801         NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
802         configuration._shouldSkipPreferredClientCertificateLookup = YES;
803         return configuration;
804     }
805     return [NSURLSessionConfiguration defaultSessionConfiguration];
806 }
807
808 const String& NetworkSessionCocoa::sourceApplicationBundleIdentifier() const
809 {
810     return m_sourceApplicationBundleIdentifier;
811 }
812
813 const String& NetworkSessionCocoa::sourceApplicationSecondaryIdentifier() const
814 {
815     return m_sourceApplicationSecondaryIdentifier;
816 }
817
818 #if PLATFORM(IOS_FAMILY)
819 static String& globalCTDataConnectionServiceType()
820 {
821     static NeverDestroyed<String> ctDataConnectionServiceType;
822     return ctDataConnectionServiceType.get();
823 }
824
825 const String& NetworkSessionCocoa::ctDataConnectionServiceType() const
826 {
827     return globalCTDataConnectionServiceType();
828 }
829
830 void NetworkSessionCocoa::setCTDataConnectionServiceType(const String& type)
831 {
832     ASSERT(!sessionsCreated);
833     globalCTDataConnectionServiceType() = type;
834 }
835 #endif
836
837 Ref<NetworkSession> NetworkSessionCocoa::create(NetworkProcess& networkProcess, NetworkSessionCreationParameters&& parameters)
838 {
839     return adoptRef(*new NetworkSessionCocoa(networkProcess, WTFMove(parameters)));
840 }
841
842 static NSDictionary *proxyDictionary(const URL& httpProxy, const URL& httpsProxy)
843 {
844     if (!httpProxy.isValid() && !httpsProxy.isValid())
845         return nil;
846
847     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
848
849     NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];
850     if (httpProxy.isValid()) {
851         [dictionary setObject:httpProxy.host().toString() forKey:(NSString *)kCFStreamPropertyHTTPProxyHost];
852         if (auto port = httpProxy.port())
853             [dictionary setObject:@(*port) forKey:(NSString *)kCFStreamPropertyHTTPProxyPort];
854     }
855     if (httpsProxy.isValid()) {
856         [dictionary setObject:httpsProxy.host().toString() forKey:(NSString *)kCFStreamPropertyHTTPSProxyHost];
857         if (auto port = httpsProxy.port())
858             [dictionary setObject:@(*port) forKey:(NSString *)kCFStreamPropertyHTTPSProxyPort];
859     }
860     return dictionary;
861
862     ALLOW_DEPRECATED_DECLARATIONS_END
863 }
864
865 NetworkSessionCocoa::NetworkSessionCocoa(NetworkProcess& networkProcess, NetworkSessionCreationParameters&& parameters)
866     : NetworkSession(networkProcess, parameters.sessionID)
867     , m_boundInterfaceIdentifier(parameters.boundInterfaceIdentifier)
868     , m_sourceApplicationBundleIdentifier(parameters.sourceApplicationBundleIdentifier)
869     , m_sourceApplicationSecondaryIdentifier(parameters.sourceApplicationSecondaryIdentifier)
870     , m_proxyConfiguration(parameters.proxyConfiguration)
871     , m_shouldLogCookieInformation(parameters.shouldLogCookieInformation)
872     , m_loadThrottleLatency(parameters.loadThrottleLatency)
873 {
874     ASSERT(hasProcessPrivilege(ProcessPrivilege::CanAccessRawCookies));
875
876     relaxAdoptionRequirement();
877
878 #if !ASSERT_DISABLED
879     sessionsCreated = true;
880 #endif
881
882     NSURLSessionConfiguration *configuration = configurationForSessionID(m_sessionID);
883
884 #if HAVE(LOAD_OPTIMIZER)
885     NETWORKSESSIONCOCOA_LOADOPTIMIZER_ADDITIONS
886 #endif
887
888 #if USE(CFNETWORK_AUTO_ADDED_HTTP_HEADER_SUPPRESSION)
889     // Without this, CFNetwork would sometimes add a Content-Type header to our requests (rdar://problem/34748470).
890     configuration._suppressedAutoAddedHTTPHeaders = [NSSet setWithObject:@"Content-Type"];
891 #endif
892
893     if (parameters.allowsCellularAccess == AllowsCellularAccess::No)
894         configuration.allowsCellularAccess = NO;
895
896     // The WebKit network cache was already queried.
897     configuration.URLCache = nil;
898
899     if (auto data = networkProcess.sourceApplicationAuditData())
900         configuration._sourceApplicationAuditTokenData = (__bridge NSData *)data.get();
901
902     if (!m_sourceApplicationBundleIdentifier.isEmpty()) {
903         configuration._sourceApplicationBundleIdentifier = m_sourceApplicationBundleIdentifier;
904         configuration._sourceApplicationAuditTokenData = nil;
905     }
906
907     if (!m_sourceApplicationSecondaryIdentifier.isEmpty())
908         configuration._sourceApplicationSecondaryIdentifier = m_sourceApplicationSecondaryIdentifier;
909
910     configuration.connectionProxyDictionary = proxyDictionary(parameters.httpProxy, parameters.httpsProxy);
911
912 #if PLATFORM(IOS_FAMILY)
913     auto& ctDataConnectionServiceType = globalCTDataConnectionServiceType();
914     if (!ctDataConnectionServiceType.isEmpty())
915         configuration._CTDataConnectionServiceType = ctDataConnectionServiceType;
916 #endif
917
918 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
919     networkProcess.supplement<LegacyCustomProtocolManager>()->registerProtocolClass(configuration);
920 #endif
921
922 #if HAVE(TIMINGDATAOPTIONS)
923     configuration._timingDataOptions = _TimingDataOptionsEnableW3CNavigationTiming;
924 #else
925     setCollectsTimingData();
926 #endif
927
928 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101400) || (PLATFORM(IOS_FAMILY) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 120000)
929     // FIXME: Replace @"kCFStreamPropertyAutoErrorOnSystemChange" with a constant from the SDK once rdar://problem/40650244 is in a build.
930     if (networkProcess.suppressesConnectionTerminationOnSystemChange())
931         configuration._socketStreamProperties = @{ @"kCFStreamPropertyAutoErrorOnSystemChange" : @(NO) };
932 #endif
933
934 #if PLATFORM(WATCHOS) && __WATCH_OS_VERSION_MIN_REQUIRED >= 60000
935     configuration._companionProxyPreference = NSURLSessionCompanionProxyPreferencePreferDirectToCloud;
936 #endif
937
938     auto* storageSession = networkProcess.storageSession(parameters.sessionID);
939     RELEASE_ASSERT(storageSession);
940
941     NSHTTPCookieStorage* cookieStorage;
942     if (CFHTTPCookieStorageRef storage = storageSession->cookieStorage().get()) {
943         cookieStorage = [[[NSHTTPCookieStorage alloc] _initWithCFHTTPCookieStorage:storage] autorelease];
944         configuration.HTTPCookieStorage = cookieStorage;
945     } else
946         cookieStorage = storageSession->nsCookieStorage();
947
948 #if HAVE(CFNETWORK_OVERRIDE_SESSION_COOKIE_ACCEPT_POLICY)
949     // We still need to check the selector since CFNetwork updates and WebKit updates are separate
950     // on older macOS.
951     if ([cookieStorage respondsToSelector:@selector(_overrideSessionCookieAcceptPolicy)])
952         cookieStorage._overrideSessionCookieAcceptPolicy = YES;
953 #endif
954
955     m_sessionWithCredentialStorageDelegate = adoptNS([[WKNetworkSessionDelegate alloc] initWithNetworkSession:*this withCredentials:true]);
956     m_sessionWithCredentialStorage = [NSURLSession sessionWithConfiguration:configuration delegate:static_cast<id>(m_sessionWithCredentialStorageDelegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
957     LOG(NetworkSession, "Created NetworkSession with cookieAcceptPolicy %lu", configuration.HTTPCookieStorage.cookieAcceptPolicy);
958
959     configuration.URLCredentialStorage = nil;
960     configuration._shouldSkipPreferredClientCertificateLookup = YES;
961     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=177394
962     // configuration.HTTPCookieStorage = nil;
963     // configuration.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever;
964
965     m_statelessSessionDelegate = adoptNS([[WKNetworkSessionDelegate alloc] initWithNetworkSession:*this withCredentials:false]);
966     m_statelessSession = [NSURLSession sessionWithConfiguration:configuration delegate:static_cast<id>(m_statelessSessionDelegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
967
968 #if ENABLE(RESOURCE_LOAD_STATISTICS)
969     m_resourceLoadStatisticsDirectory = parameters.resourceLoadStatisticsDirectory;
970     m_shouldIncludeLocalhostInResourceLoadStatistics = parameters.shouldIncludeLocalhostInResourceLoadStatistics ? ShouldIncludeLocalhost::Yes : ShouldIncludeLocalhost::No;
971     m_enableResourceLoadStatisticsDebugMode = parameters.enableResourceLoadStatisticsDebugMode ? EnableResourceLoadStatisticsDebugMode::Yes : EnableResourceLoadStatisticsDebugMode::No;
972     m_resourceLoadStatisticsManualPrevalentResource = parameters.resourceLoadStatisticsManualPrevalentResource;
973     setResourceLoadStatisticsEnabled(parameters.enableResourceLoadStatistics);
974 #endif
975
976 #if HAVE(SESSION_CLEANUP)
977     activateSessionCleanup(*this);
978 #endif
979 }
980
981 NetworkSessionCocoa::~NetworkSessionCocoa()
982 {
983 }
984
985 void NetworkSessionCocoa::invalidateAndCancel()
986 {
987     NetworkSession::invalidateAndCancel();
988
989     [m_sessionWithCredentialStorage invalidateAndCancel];
990     [m_statelessSession invalidateAndCancel];
991     [m_sessionWithCredentialStorageDelegate sessionInvalidated];
992     [m_statelessSessionDelegate sessionInvalidated];
993 }
994
995 void NetworkSessionCocoa::clearCredentials()
996 {
997 #if !USE(CREDENTIAL_STORAGE_WITH_NETWORK_SESSION)
998     ASSERT(m_dataTaskMapWithCredentials.isEmpty());
999     ASSERT(m_dataTaskMapWithoutState.isEmpty());
1000     ASSERT(m_downloadMap.isEmpty());
1001     // FIXME: Use resetWithCompletionHandler instead.
1002     m_sessionWithCredentialStorage = [NSURLSession sessionWithConfiguration:m_sessionWithCredentialStorage.get().configuration delegate:static_cast<id>(m_sessionWithCredentialStorageDelegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
1003     m_statelessSession = [NSURLSession sessionWithConfiguration:m_statelessSession.get().configuration delegate:static_cast<id>(m_statelessSessionDelegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
1004 #endif
1005 }
1006
1007 NetworkDataTaskCocoa* NetworkSessionCocoa::dataTaskForIdentifier(NetworkDataTaskCocoa::TaskIdentifier taskIdentifier, WebCore::StoredCredentialsPolicy storedCredentialsPolicy)
1008 {
1009     ASSERT(RunLoop::isMain());
1010     if (storedCredentialsPolicy == WebCore::StoredCredentialsPolicy::Use)
1011         return m_dataTaskMapWithCredentials.get(taskIdentifier);
1012     return m_dataTaskMapWithoutState.get(taskIdentifier);
1013 }
1014
1015 NSURLSessionDownloadTask* NetworkSessionCocoa::downloadTaskWithResumeData(NSData* resumeData)
1016 {
1017     return [m_sessionWithCredentialStorage downloadTaskWithResumeData:resumeData];
1018 }
1019
1020 void NetworkSessionCocoa::addDownloadID(NetworkDataTaskCocoa::TaskIdentifier taskIdentifier, DownloadID downloadID)
1021 {
1022 #ifndef NDEBUG
1023     ASSERT(!m_downloadMap.contains(taskIdentifier));
1024     for (auto idInMap : m_downloadMap.values())
1025         ASSERT(idInMap != downloadID);
1026 #endif
1027     m_downloadMap.add(taskIdentifier, downloadID);
1028 }
1029
1030 DownloadID NetworkSessionCocoa::downloadID(NetworkDataTaskCocoa::TaskIdentifier taskIdentifier)
1031 {
1032     ASSERT(m_downloadMap.get(taskIdentifier).downloadID());
1033     return m_downloadMap.get(taskIdentifier);
1034 }
1035
1036 DownloadID NetworkSessionCocoa::takeDownloadID(NetworkDataTaskCocoa::TaskIdentifier taskIdentifier)
1037 {
1038     auto downloadID = m_downloadMap.take(taskIdentifier);
1039     return downloadID;
1040 }
1041
1042 static bool certificatesMatch(SecTrustRef trust1, SecTrustRef trust2)
1043 {
1044     if (!trust1 || !trust2)
1045         return false;
1046
1047     CFIndex count1 = SecTrustGetCertificateCount(trust1);
1048     CFIndex count2 = SecTrustGetCertificateCount(trust2);
1049     if (count1 != count2)
1050         return false;
1051
1052     for (CFIndex i = 0; i < count1; i++) {
1053         auto cert1 = SecTrustGetCertificateAtIndex(trust1, i);
1054         auto cert2 = SecTrustGetCertificateAtIndex(trust2, i);
1055         RELEASE_ASSERT(cert1);
1056         RELEASE_ASSERT(cert2);
1057         if (!CFEqual(cert1, cert2))
1058             return false;
1059     }
1060
1061     return true;
1062 }
1063
1064 bool NetworkSessionCocoa::allowsSpecificHTTPSCertificateForHost(const WebCore::AuthenticationChallenge& challenge)
1065 {
1066     const String& host = challenge.protectionSpace().host();
1067     NSArray *certificates = [NSURLRequest allowsSpecificHTTPSCertificateForHost:host];
1068     if (!certificates)
1069         return false;
1070
1071     bool requireServerCertificates = challenge.protectionSpace().authenticationScheme() == WebCore::ProtectionSpaceAuthenticationScheme::ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested;
1072     RetainPtr<SecPolicyRef> policy = adoptCF(SecPolicyCreateSSL(requireServerCertificates, host.createCFString().get()));
1073
1074     SecTrustRef trustRef = nullptr;
1075     if (SecTrustCreateWithCertificates((CFArrayRef)certificates, policy.get(), &trustRef) != noErr)
1076         return false;
1077     RetainPtr<SecTrustRef> trust = adoptCF(trustRef);
1078
1079     return certificatesMatch(trust.get(), challenge.nsURLAuthenticationChallenge().protectionSpace.serverTrust);
1080 }
1081
1082 void NetworkSessionCocoa::continueDidReceiveChallenge(const WebCore::AuthenticationChallenge& challenge, NetworkDataTaskCocoa::TaskIdentifier taskIdentifier, NetworkDataTaskCocoa* networkDataTask, CompletionHandler<void(WebKit::AuthenticationChallengeDisposition, const WebCore::Credential&)>&& completionHandler)
1083 {
1084     if (!networkDataTask) {
1085         auto downloadID = this->downloadID(taskIdentifier);
1086         if (downloadID.downloadID()) {
1087             if (auto* download = networkProcess().downloadManager().download(downloadID)) {
1088                 WebCore::AuthenticationChallenge authenticationChallenge { challenge };
1089                 // Received an authentication challenge for a download being resumed.
1090                 download->didReceiveChallenge(authenticationChallenge, WTFMove(completionHandler));
1091                 return;
1092             }
1093         }
1094         LOG(NetworkSession, "%llu didReceiveChallenge completionHandler (cancel)", taskIdentifier);
1095         completionHandler(AuthenticationChallengeDisposition::Cancel, { });
1096         return;
1097     }
1098
1099     auto sessionID = this->sessionID();
1100     WebCore::AuthenticationChallenge authenticationChallenge { challenge };
1101     auto challengeCompletionHandler = [completionHandler = WTFMove(completionHandler), networkProcess = makeRef(networkProcess()), sessionID, authenticationChallenge, taskIdentifier, partition = networkDataTask->partition()](WebKit::AuthenticationChallengeDisposition disposition, const WebCore::Credential& credential) mutable {
1102 #if !LOG_DISABLED
1103         LOG(NetworkSession, "%llu didReceiveChallenge completionHandler %d", taskIdentifier, disposition);
1104 #else
1105         UNUSED_PARAM(taskIdentifier);
1106 #endif
1107 #if !USE(CREDENTIAL_STORAGE_WITH_NETWORK_SESSION)
1108         UNUSED_PARAM(sessionID);
1109         UNUSED_PARAM(authenticationChallenge);
1110 #else
1111         if (credential.persistence() == WebCore::CredentialPersistenceForSession && authenticationChallenge.protectionSpace().isPasswordBased()) {
1112             WebCore::Credential nonPersistentCredential(credential.user(), credential.password(), WebCore::CredentialPersistenceNone);
1113             URL urlToStore;
1114             if (authenticationChallenge.failureResponse().httpStatusCode() == 401)
1115                 urlToStore = authenticationChallenge.failureResponse().url();
1116             if (auto storageSession = networkProcess->storageSession(sessionID))
1117                 storageSession->credentialStorage().set(partition, nonPersistentCredential, authenticationChallenge.protectionSpace(), urlToStore);
1118             else
1119                 ASSERT_NOT_REACHED();
1120
1121             completionHandler(disposition, nonPersistentCredential);
1122             return;
1123         }
1124 #endif
1125         completionHandler(disposition, credential);
1126     };
1127     networkDataTask->didReceiveChallenge(WTFMove(authenticationChallenge), WTFMove(challengeCompletionHandler));
1128 }
1129
1130 }