932935d895bdb89321f7c8ab8214193b42ce5a57
[WebKit-https.git] / Source / WebKit / NetworkProcess / cocoa / NetworkSessionCocoa.mm
1 /*
2  * Copyright (C) 2015-2018 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 using namespace WebKit;
57
58 CFStringRef const WebKit2HTTPProxyDefaultsKey = static_cast<CFStringRef>(@"WebKit2HTTPProxy");
59 CFStringRef const WebKit2HTTPSProxyDefaultsKey = static_cast<CFStringRef>(@"WebKit2HTTPSProxy");
60
61 static NSURLSessionResponseDisposition toNSURLSessionResponseDisposition(WebCore::PolicyAction disposition)
62 {
63     switch (disposition) {
64     case WebCore::PolicyAction::Ignore:
65         return NSURLSessionResponseCancel;
66     case WebCore::PolicyAction::Use:
67         return NSURLSessionResponseAllow;
68     case WebCore::PolicyAction::Download:
69         return NSURLSessionResponseBecomeDownload;
70     }
71 }
72
73 static NSURLSessionAuthChallengeDisposition toNSURLSessionAuthChallengeDisposition(WebKit::AuthenticationChallengeDisposition disposition)
74 {
75     switch (disposition) {
76     case WebKit::AuthenticationChallengeDisposition::UseCredential:
77         return NSURLSessionAuthChallengeUseCredential;
78     case WebKit::AuthenticationChallengeDisposition::PerformDefaultHandling:
79         return NSURLSessionAuthChallengePerformDefaultHandling;
80     case WebKit::AuthenticationChallengeDisposition::Cancel:
81         return NSURLSessionAuthChallengeCancelAuthenticationChallenge;
82     case WebKit::AuthenticationChallengeDisposition::RejectProtectionSpaceAndContinue:
83         return NSURLSessionAuthChallengeRejectProtectionSpace;
84     }
85 }
86
87 static WebCore::NetworkLoadPriority toNetworkLoadPriority(float priority)
88 {
89     if (priority <= NSURLSessionTaskPriorityLow)
90         return WebCore::NetworkLoadPriority::Low;
91     if (priority >= NSURLSessionTaskPriorityHigh)
92         return WebCore::NetworkLoadPriority::High;
93     return WebCore::NetworkLoadPriority::Medium;
94 }
95
96 #if HAVE(CFNETWORK_NEGOTIATED_SSL_PROTOCOL_CIPHER)
97 static String stringForSSLProtocol(SSLProtocol protocol)
98 {
99     switch (protocol) {
100     case kDTLSProtocol1:
101         return "DTLS 1.0"_s;
102     case kSSLProtocol2:
103         return "SSL 2.0"_s;
104     case kSSLProtocol3:
105         return "SSL 3.0"_s;
106     case kSSLProtocol3Only:
107         return "SSL 3.0 (Only)"_s;
108     case kTLSProtocol1:
109         return "TLS 1.0"_s;
110     case kTLSProtocol1Only:
111         return "TLS 1.0 (Only)"_s;
112     case kTLSProtocol11:
113         return "TLS 1.1"_s;
114     case kTLSProtocol12:
115         return "TLS 1.2"_s;
116     case kTLSProtocol13:
117         return "TLS 1.3"_s;
118     case kSSLProtocolAll:
119         return "All";
120     case kSSLProtocolUnknown:
121         return "Unknown";
122     case kTLSProtocolMaxSupported:
123     default:
124         ASSERT_NOT_REACHED();
125         return emptyString();
126     }
127 }
128
129 static String stringForSSLCipher(SSLCipherSuite cipher)
130 {
131 #define STRINGIFY_CIPHER(cipher) \
132     case cipher: \
133         return "" #cipher ""_s
134
135     switch (cipher) {
136     STRINGIFY_CIPHER(SSL_RSA_EXPORT_WITH_RC4_40_MD5);
137     STRINGIFY_CIPHER(SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5);
138     STRINGIFY_CIPHER(SSL_RSA_WITH_IDEA_CBC_SHA);
139     STRINGIFY_CIPHER(SSL_RSA_EXPORT_WITH_DES40_CBC_SHA);
140     STRINGIFY_CIPHER(SSL_RSA_WITH_DES_CBC_SHA);
141     STRINGIFY_CIPHER(SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA);
142     STRINGIFY_CIPHER(SSL_DH_DSS_WITH_DES_CBC_SHA);
143     STRINGIFY_CIPHER(SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA);
144     STRINGIFY_CIPHER(SSL_DH_RSA_WITH_DES_CBC_SHA);
145     STRINGIFY_CIPHER(SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA);
146     STRINGIFY_CIPHER(SSL_DHE_DSS_WITH_DES_CBC_SHA);
147     STRINGIFY_CIPHER(SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA);
148     STRINGIFY_CIPHER(SSL_DHE_RSA_WITH_DES_CBC_SHA);
149     STRINGIFY_CIPHER(SSL_DH_anon_EXPORT_WITH_RC4_40_MD5);
150     STRINGIFY_CIPHER(SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA);
151     STRINGIFY_CIPHER(SSL_DH_anon_WITH_DES_CBC_SHA);
152     STRINGIFY_CIPHER(SSL_FORTEZZA_DMS_WITH_NULL_SHA);
153     STRINGIFY_CIPHER(SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA);
154     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_128_CBC_SHA);
155     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_128_CBC_SHA);
156     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_128_CBC_SHA);
157     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_128_CBC_SHA);
158     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_128_CBC_SHA);
159     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_128_CBC_SHA);
160     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_256_CBC_SHA);
161     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_256_CBC_SHA);
162     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_256_CBC_SHA);
163     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_256_CBC_SHA);
164     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_256_CBC_SHA);
165     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_256_CBC_SHA);
166     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_NULL_SHA);
167     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_RC4_128_SHA);
168     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA);
169     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
170     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA);
171     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_NULL_SHA);
172     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_RC4_128_SHA);
173     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA);
174     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
175     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA);
176     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_NULL_SHA);
177     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_RC4_128_SHA);
178     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA);
179     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
180     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
181     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_NULL_SHA);
182     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_RC4_128_SHA);
183     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA);
184     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
185     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA);
186     STRINGIFY_CIPHER(TLS_ECDH_anon_WITH_NULL_SHA);
187     STRINGIFY_CIPHER(TLS_ECDH_anon_WITH_RC4_128_SHA);
188     STRINGIFY_CIPHER(TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA);
189     STRINGIFY_CIPHER(TLS_ECDH_anon_WITH_AES_128_CBC_SHA);
190     STRINGIFY_CIPHER(TLS_ECDH_anon_WITH_AES_256_CBC_SHA);
191     // STRINGIFY_CIPHER(SSL_NULL_WITH_NULL_NULL);
192     STRINGIFY_CIPHER(TLS_NULL_WITH_NULL_NULL);
193     // STRINGIFY_CIPHER(SSL_RSA_WITH_NULL_MD5);
194     STRINGIFY_CIPHER(TLS_RSA_WITH_NULL_MD5);
195     // STRINGIFY_CIPHER(SSL_RSA_WITH_NULL_SHA);
196     STRINGIFY_CIPHER(TLS_RSA_WITH_NULL_SHA);
197     // STRINGIFY_CIPHER(SSL_RSA_WITH_RC4_128_MD5);
198     STRINGIFY_CIPHER(TLS_RSA_WITH_RC4_128_MD5);
199     // STRINGIFY_CIPHER(SSL_RSA_WITH_RC4_128_SHA);
200     STRINGIFY_CIPHER(TLS_RSA_WITH_RC4_128_SHA);
201     // STRINGIFY_CIPHER(SSL_RSA_WITH_3DES_EDE_CBC_SHA);
202     STRINGIFY_CIPHER(TLS_RSA_WITH_3DES_EDE_CBC_SHA);
203     STRINGIFY_CIPHER(TLS_RSA_WITH_NULL_SHA256);
204     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_128_CBC_SHA256);
205     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_256_CBC_SHA256);
206     // STRINGIFY_CIPHER(SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA);
207     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA);
208     // STRINGIFY_CIPHER(SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA);
209     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA);
210     // STRINGIFY_CIPHER(SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA);
211     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA);
212     // STRINGIFY_CIPHER(SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA);
213     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA);
214     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_128_CBC_SHA256);
215     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_128_CBC_SHA256);
216     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_128_CBC_SHA256);
217     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_128_CBC_SHA256);
218     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_256_CBC_SHA256);
219     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_256_CBC_SHA256);
220     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_256_CBC_SHA256);
221     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_256_CBC_SHA256);
222     // STRINGIFY_CIPHER(SSL_DH_anon_WITH_RC4_128_MD5);
223     STRINGIFY_CIPHER(TLS_DH_anon_WITH_RC4_128_MD5);
224     // STRINGIFY_CIPHER(SSL_DH_anon_WITH_3DES_EDE_CBC_SHA);
225     STRINGIFY_CIPHER(TLS_DH_anon_WITH_3DES_EDE_CBC_SHA);
226     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_128_CBC_SHA256);
227     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_256_CBC_SHA256);
228     STRINGIFY_CIPHER(TLS_PSK_WITH_RC4_128_SHA);
229     STRINGIFY_CIPHER(TLS_PSK_WITH_3DES_EDE_CBC_SHA);
230     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_128_CBC_SHA);
231     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_256_CBC_SHA);
232     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_RC4_128_SHA);
233     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA);
234     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_128_CBC_SHA);
235     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_256_CBC_SHA);
236     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_RC4_128_SHA);
237     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA);
238     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_128_CBC_SHA);
239     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_256_CBC_SHA);
240     STRINGIFY_CIPHER(TLS_PSK_WITH_NULL_SHA);
241     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_NULL_SHA);
242     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_NULL_SHA);
243     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_128_GCM_SHA256);
244     STRINGIFY_CIPHER(TLS_RSA_WITH_AES_256_GCM_SHA384);
245     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_128_GCM_SHA256);
246     STRINGIFY_CIPHER(TLS_DHE_RSA_WITH_AES_256_GCM_SHA384);
247     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_128_GCM_SHA256);
248     STRINGIFY_CIPHER(TLS_DH_RSA_WITH_AES_256_GCM_SHA384);
249     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_128_GCM_SHA256);
250     STRINGIFY_CIPHER(TLS_DHE_DSS_WITH_AES_256_GCM_SHA384);
251     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_128_GCM_SHA256);
252     STRINGIFY_CIPHER(TLS_DH_DSS_WITH_AES_256_GCM_SHA384);
253     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_128_GCM_SHA256);
254     STRINGIFY_CIPHER(TLS_DH_anon_WITH_AES_256_GCM_SHA384);
255     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_128_GCM_SHA256);
256     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_256_GCM_SHA384);
257     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_128_GCM_SHA256);
258     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_256_GCM_SHA384);
259     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_128_GCM_SHA256);
260     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_256_GCM_SHA384);
261     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_128_CBC_SHA256);
262     STRINGIFY_CIPHER(TLS_PSK_WITH_AES_256_CBC_SHA384);
263     STRINGIFY_CIPHER(TLS_PSK_WITH_NULL_SHA256);
264     STRINGIFY_CIPHER(TLS_PSK_WITH_NULL_SHA384);
265     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_128_CBC_SHA256);
266     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_AES_256_CBC_SHA384);
267     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_NULL_SHA256);
268     STRINGIFY_CIPHER(TLS_DHE_PSK_WITH_NULL_SHA384);
269     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_128_CBC_SHA256);
270     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_AES_256_CBC_SHA384);
271     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_NULL_SHA256);
272     STRINGIFY_CIPHER(TLS_RSA_PSK_WITH_NULL_SHA384);
273     STRINGIFY_CIPHER(TLS_AES_128_GCM_SHA256);
274     STRINGIFY_CIPHER(TLS_AES_256_GCM_SHA384);
275     STRINGIFY_CIPHER(TLS_CHACHA20_POLY1305_SHA256);
276     STRINGIFY_CIPHER(TLS_AES_128_CCM_SHA256);
277     STRINGIFY_CIPHER(TLS_AES_128_CCM_8_SHA256);
278     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256);
279     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384);
280     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256);
281     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384);
282     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256);
283     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384);
284     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256);
285     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384);
286     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256);
287     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384);
288     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256);
289     STRINGIFY_CIPHER(TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384);
290     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256);
291     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384);
292     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256);
293     STRINGIFY_CIPHER(TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384);
294     STRINGIFY_CIPHER(TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
295     STRINGIFY_CIPHER(TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
296     STRINGIFY_CIPHER(TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
297     STRINGIFY_CIPHER(SSL_RSA_WITH_RC2_CBC_MD5);
298     STRINGIFY_CIPHER(SSL_RSA_WITH_IDEA_CBC_MD5);
299     STRINGIFY_CIPHER(SSL_RSA_WITH_DES_CBC_MD5);
300     STRINGIFY_CIPHER(SSL_RSA_WITH_3DES_EDE_CBC_MD5);
301     STRINGIFY_CIPHER(SSL_NO_SUCH_CIPHERSUITE);
302     default:
303         ASSERT_NOT_REACHED();
304         return emptyString();
305     }
306
307 #undef STRINGIFY_CIPHER
308 }
309 #endif
310
311 @interface WKNetworkSessionDelegate : NSObject <NSURLSessionDataDelegate> {
312     RefPtr<WebKit::NetworkSessionCocoa> _session;
313     bool _withCredentials;
314 }
315
316 - (id)initWithNetworkSession:(WebKit::NetworkSessionCocoa&)session withCredentials:(bool)withCredentials;
317 - (void)sessionInvalidated;
318
319 @end
320
321 @implementation WKNetworkSessionDelegate
322
323 - (id)initWithNetworkSession:(WebKit::NetworkSessionCocoa&)session withCredentials:(bool)withCredentials
324 {
325     self = [super init];
326     if (!self)
327         return nil;
328
329     _session = &session;
330     _withCredentials = withCredentials;
331
332     return self;
333 }
334
335 - (void)sessionInvalidated
336 {
337     _session = nullptr;
338 }
339
340 - (NetworkDataTaskCocoa*)existingTask:(NSURLSessionTask *)task
341 {
342     if (!_session)
343         return nullptr;
344
345     if (!task)
346         return nullptr;
347
348     auto storedCredentialsPolicy = _withCredentials ? WebCore::StoredCredentialsPolicy::Use : WebCore::StoredCredentialsPolicy::DoNotUse;
349     return _session->dataTaskForIdentifier(task.taskIdentifier, storedCredentialsPolicy);
350 }
351
352 - (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error
353 {
354     ASSERT(!_session);
355 }
356
357 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
358 {
359     if (auto* networkDataTask = [self existingTask:task])
360         networkDataTask->didSendData(totalBytesSent, totalBytesExpectedToSend);
361 }
362
363 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task needNewBodyStream:(void (^)(NSInputStream *bodyStream))completionHandler
364 {
365     auto* networkDataTask = [self existingTask:task];
366     if (!networkDataTask) {
367         completionHandler(nil);
368         return;
369     }
370
371     auto* body = networkDataTask->firstRequest().httpBody();
372     if (!body) {
373         completionHandler(nil);
374         return;
375     }
376
377     completionHandler(WebCore::createHTTPBodyNSInputStream(*body).get());
378 }
379
380 #if HAVE(CFNETWORK_WITH_IGNORE_HSTS) && ENABLE(RESOURCE_LOAD_STATISTICS)
381 static NSURLRequest* downgradeRequest(NSURLRequest *request)
382 {
383     NSMutableURLRequest *nsMutableRequest = [[request mutableCopy] autorelease];
384     if ([nsMutableRequest.URL.scheme isEqualToString:@"https"]) {
385         NSURLComponents *components = [NSURLComponents componentsWithURL:nsMutableRequest.URL resolvingAgainstBaseURL:NO];
386         components.scheme = @"http";
387         [nsMutableRequest setURL:components.URL];
388         ASSERT([nsMutableRequest.URL.scheme isEqualToString:@"http"]);
389         return nsMutableRequest;
390     }
391
392     ASSERT_NOT_REACHED();
393     return request;
394 }
395
396 static bool schemeWasUpgradedDueToDynamicHSTS(NSURLRequest *request)
397 {
398     return [request respondsToSelector:@selector(_schemeWasUpgradedDueToDynamicHSTS)]
399         && [request _schemeWasUpgradedDueToDynamicHSTS];
400 }
401 #endif
402
403 #if HAVE(CFNETWORK_WITH_IGNORE_HSTS)
404 static void setIgnoreHSTS(NSMutableURLRequest *request, bool ignoreHSTS)
405 {
406     if ([request respondsToSelector:@selector(_setIgnoreHSTS:)])
407         [request _setIgnoreHSTS:ignoreHSTS];
408 }
409
410 static bool ignoreHSTS(NSURLRequest *request)
411 {
412     return [request respondsToSelector:@selector(_ignoreHSTS)]
413         && [request _ignoreHSTS];
414 }
415 #endif
416
417 static NSURLRequest* updateIgnoreStrictTransportSecuritySettingIfNecessary(NSURLRequest *request, bool shouldIgnoreHSTS)
418 {
419 #if HAVE(CFNETWORK_WITH_IGNORE_HSTS)
420     if ([request.URL.scheme isEqualToString:@"https"] && shouldIgnoreHSTS && ignoreHSTS(request)) {
421         // The request was upgraded for some other reason than HSTS.
422         // Don't ignore HSTS to avoid the risk of another downgrade.
423         NSMutableURLRequest *nsMutableRequest = [[request mutableCopy] autorelease];
424         setIgnoreHSTS(nsMutableRequest, false);
425         return nsMutableRequest;
426     }
427     
428     if ([request.URL.scheme isEqualToString:@"http"] && ignoreHSTS(request) != shouldIgnoreHSTS) {
429         NSMutableURLRequest *nsMutableRequest = [[request mutableCopy] autorelease];
430         setIgnoreHSTS(nsMutableRequest, shouldIgnoreHSTS);
431         return nsMutableRequest;
432     }
433 #else
434     UNUSED_PARAM(shouldIgnoreHSTS);
435 #endif
436
437     return request;
438 }
439
440 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler
441 {
442     auto taskIdentifier = task.taskIdentifier;
443     LOG(NetworkSession, "%llu willPerformHTTPRedirection from %s to %s", taskIdentifier, response.URL.absoluteString.UTF8String, request.URL.absoluteString.UTF8String);
444
445     if (auto* networkDataTask = [self existingTask:task]) {
446         auto completionHandlerCopy = Block_copy(completionHandler);
447
448         bool shouldIgnoreHSTS = false;
449 #if HAVE(CFNETWORK_WITH_IGNORE_HSTS) && ENABLE(RESOURCE_LOAD_STATISTICS)        
450         shouldIgnoreHSTS = schemeWasUpgradedDueToDynamicHSTS(request) && WebCore::NetworkStorageSession::storageSession(_session->sessionID())->shouldBlockCookies(request, networkDataTask->frameID(), networkDataTask->pageID());
451         if (shouldIgnoreHSTS) {
452             request = downgradeRequest(request);
453             ASSERT([request.URL.scheme isEqualToString:@"http"]);
454             LOG(NetworkSession, "%llu Downgraded %s from https to http", taskIdentifier, request.URL.absoluteString.UTF8String);
455         }
456 #endif
457
458         networkDataTask->willPerformHTTPRedirection(response, request, [completionHandlerCopy, taskIdentifier, shouldIgnoreHSTS](auto&& request) {
459 #if !LOG_DISABLED
460             LOG(NetworkSession, "%llu willPerformHTTPRedirection completionHandler (%s)", taskIdentifier, request.url().string().utf8().data());
461 #else
462             UNUSED_PARAM(taskIdentifier);
463 #endif
464             auto nsRequest = request.nsURLRequest(WebCore::HTTPBodyUpdatePolicy::UpdateHTTPBody);
465             nsRequest = updateIgnoreStrictTransportSecuritySettingIfNecessary(nsRequest, shouldIgnoreHSTS);
466             completionHandlerCopy(nsRequest);
467             Block_release(completionHandlerCopy);
468         });
469     } else {
470         LOG(NetworkSession, "%llu willPerformHTTPRedirection completionHandler (nil)", taskIdentifier);
471         completionHandler(nil);
472     }
473 }
474
475 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask*)task _schemeUpgraded:(NSURLRequest*)request completionHandler:(void (^)(NSURLRequest*))completionHandler
476 {
477     auto taskIdentifier = task.taskIdentifier;
478     LOG(NetworkSession, "%llu _schemeUpgraded %s", taskIdentifier, request.URL.absoluteString.UTF8String);
479
480     if (auto* networkDataTask = [self existingTask:task]) {
481         bool shouldIgnoreHSTS = false;
482 #if HAVE(CFNETWORK_WITH_IGNORE_HSTS) && ENABLE(RESOURCE_LOAD_STATISTICS)
483         shouldIgnoreHSTS = schemeWasUpgradedDueToDynamicHSTS(request) && WebCore::NetworkStorageSession::storageSession(_session->sessionID())->shouldBlockCookies(request, networkDataTask->frameID(), networkDataTask->pageID());
484         if (shouldIgnoreHSTS) {
485             request = downgradeRequest(request);
486             ASSERT([request.URL.scheme isEqualToString:@"http"]);
487             LOG(NetworkSession, "%llu Downgraded %s from https to http", taskIdentifier, request.URL.absoluteString.UTF8String);
488         }
489 #endif
490
491         auto completionHandlerCopy = Block_copy(completionHandler);
492         networkDataTask->willPerformHTTPRedirection(WebCore::synthesizeRedirectResponseIfNecessary([task currentRequest], request, nil), request, [completionHandlerCopy, taskIdentifier, shouldIgnoreHSTS](auto&& request) {
493 #if !LOG_DISABLED
494             LOG(NetworkSession, "%llu _schemeUpgraded completionHandler (%s)", taskIdentifier, request.url().string().utf8().data());
495 #else
496             UNUSED_PARAM(taskIdentifier);
497 #endif
498             auto nsRequest = request.nsURLRequest(WebCore::HTTPBodyUpdatePolicy::UpdateHTTPBody);
499             nsRequest = updateIgnoreStrictTransportSecuritySettingIfNecessary(nsRequest, shouldIgnoreHSTS);
500             completionHandlerCopy(nsRequest);
501             Block_release(completionHandlerCopy);
502         });
503     } else {
504         LOG(NetworkSession, "%llu _schemeUpgraded completionHandler (nil)", taskIdentifier);
505         completionHandler(nil);
506     }
507 }
508
509 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask willCacheResponse:(NSCachedURLResponse *)proposedResponse completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler
510 {
511     if (!_session) {
512         completionHandler(nil);
513         return;
514     }
515
516     // FIXME: remove if <rdar://problem/20001985> is ever resolved.
517     if ([proposedResponse.response respondsToSelector:@selector(allHeaderFields)]
518         && [[(id)proposedResponse.response allHeaderFields] objectForKey:@"Content-Range"])
519         completionHandler(nil);
520     else
521         completionHandler(proposedResponse);
522 }
523
524 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
525 {
526     if (!_session) {
527         completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
528         return;
529     }
530
531     auto taskIdentifier = task.taskIdentifier;
532     LOG(NetworkSession, "%llu didReceiveChallenge", taskIdentifier);
533     
534     // Proxy authentication is handled by CFNetwork internally. We can get here if the user cancels
535     // CFNetwork authentication dialog, and we shouldn't ask the client to display another one in that case.
536     if (challenge.protectionSpace.isProxy) {
537         completionHandler(NSURLSessionAuthChallengeUseCredential, nil);
538         return;
539     }
540
541     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
542         if (NetworkSessionCocoa::allowsSpecificHTTPSCertificateForHost(challenge))
543             return completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
544
545         // Handle server trust evaluation at platform-level if requested, for performance reasons and to use ATS defaults.
546         if (!_session->networkProcess().canHandleHTTPSServerTrustEvaluation())
547             return completionHandler(NSURLSessionAuthChallengeRejectProtectionSpace, nil);
548     }
549
550     if (auto* networkDataTask = [self existingTask:task]) {
551         WebCore::AuthenticationChallenge authenticationChallenge(challenge);
552         auto completionHandlerCopy = Block_copy(completionHandler);
553         auto sessionID = _session->sessionID();
554         auto challengeCompletionHandler = [completionHandlerCopy, sessionID, authenticationChallenge, taskIdentifier, partition = networkDataTask->partition()](WebKit::AuthenticationChallengeDisposition disposition, const WebCore::Credential& credential)
555         {
556 #if !LOG_DISABLED
557             LOG(NetworkSession, "%llu didReceiveChallenge completionHandler %d", taskIdentifier, disposition);
558 #else
559             UNUSED_PARAM(taskIdentifier);
560 #endif
561 #if !USE(CREDENTIAL_STORAGE_WITH_NETWORK_SESSION)
562             UNUSED_PARAM(sessionID);
563             UNUSED_PARAM(authenticationChallenge);
564 #else
565             if (credential.persistence() == WebCore::CredentialPersistenceForSession && authenticationChallenge.protectionSpace().isPasswordBased()) {
566
567                 WebCore::Credential nonPersistentCredential(credential.user(), credential.password(), WebCore::CredentialPersistenceNone);
568                 URL urlToStore;
569                 if (authenticationChallenge.failureResponse().httpStatusCode() == 401)
570                     urlToStore = authenticationChallenge.failureResponse().url();
571                 if (auto storageSession = WebCore::NetworkStorageSession::storageSession(sessionID))
572                     storageSession->credentialStorage().set(partition, nonPersistentCredential, authenticationChallenge.protectionSpace(), urlToStore);
573                 else
574                     ASSERT_NOT_REACHED();
575
576                 completionHandlerCopy(toNSURLSessionAuthChallengeDisposition(disposition), nonPersistentCredential.nsCredential());
577             } else
578 #endif
579                 completionHandlerCopy(toNSURLSessionAuthChallengeDisposition(disposition), credential.nsCredential());
580             Block_release(completionHandlerCopy);
581         };
582         networkDataTask->didReceiveChallenge(challenge, WTFMove(challengeCompletionHandler));
583     } else {
584         auto downloadID = _session->downloadID(taskIdentifier);
585         if (downloadID.downloadID()) {
586             if (auto* download = _session->networkProcess().downloadManager().download(downloadID)) {
587                 // Received an authentication challenge for a download being resumed.
588                 WebCore::AuthenticationChallenge authenticationChallenge { challenge };
589                 auto completionHandlerCopy = Block_copy(completionHandler);
590                 auto challengeCompletionHandler = [completionHandlerCopy, authenticationChallenge](WebKit::AuthenticationChallengeDisposition disposition, const WebCore::Credential& credential) {
591                     completionHandlerCopy(toNSURLSessionAuthChallengeDisposition(disposition), credential.nsCredential());
592                     Block_release(completionHandlerCopy);
593                 };
594                 download->didReceiveChallenge(challenge, WTFMove(challengeCompletionHandler));
595                 return;
596             }
597         }
598         LOG(NetworkSession, "%llu didReceiveChallenge completionHandler (cancel)", taskIdentifier);
599         completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
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(WebCore::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 #if PLATFORM(IOS_FAMILY)
809 static String& globalCTDataConnectionServiceType()
810 {
811     static NeverDestroyed<String> ctDataConnectionServiceType;
812     return ctDataConnectionServiceType.get();
813 }
814 #endif
815
816 #if PLATFORM(IOS_FAMILY)
817 void NetworkSessionCocoa::setCTDataConnectionServiceType(const String& type)
818 {
819     ASSERT(!sessionsCreated);
820     globalCTDataConnectionServiceType() = type;
821 }
822 #endif
823
824 Ref<NetworkSession> NetworkSessionCocoa::create(NetworkProcess& networkProcess, NetworkSessionCreationParameters&& parameters)
825 {
826     return adoptRef(*new NetworkSessionCocoa(networkProcess, WTFMove(parameters)));
827 }
828
829 static NSDictionary *proxyDictionary(const URL& httpProxy, const URL& httpsProxy)
830 {
831     if (!httpProxy.isValid() && !httpsProxy.isValid())
832         return nil;
833
834     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
835
836     NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];
837     if (httpProxy.isValid()) {
838         [dictionary setObject:httpProxy.host().toString() forKey:(NSString *)kCFStreamPropertyHTTPProxyHost];
839         if (auto port = httpProxy.port())
840             [dictionary setObject:@(*port) forKey:(NSString *)kCFStreamPropertyHTTPProxyPort];
841     }
842     if (httpsProxy.isValid()) {
843         [dictionary setObject:httpsProxy.host().toString() forKey:(NSString *)kCFStreamPropertyHTTPSProxyHost];
844         if (auto port = httpsProxy.port())
845             [dictionary setObject:@(*port) forKey:(NSString *)kCFStreamPropertyHTTPSProxyPort];
846     }
847     return dictionary;
848
849     ALLOW_DEPRECATED_DECLARATIONS_END
850 }
851
852 NetworkSessionCocoa::NetworkSessionCocoa(NetworkProcess& networkProcess, NetworkSessionCreationParameters&& parameters)
853     : NetworkSession(networkProcess, parameters.sessionID)
854     , m_boundInterfaceIdentifier(parameters.boundInterfaceIdentifier)
855     , m_proxyConfiguration(parameters.proxyConfiguration)
856     , m_shouldLogCookieInformation(parameters.shouldLogCookieInformation)
857     , m_loadThrottleLatency(parameters.loadThrottleLatency)
858 {
859     ASSERT(hasProcessPrivilege(ProcessPrivilege::CanAccessRawCookies));
860
861     relaxAdoptionRequirement();
862
863 #if !ASSERT_DISABLED
864     sessionsCreated = true;
865 #endif
866
867     NSURLSessionConfiguration *configuration = configurationForSessionID(m_sessionID);
868
869 #if USE(CFNETWORK_AUTO_ADDED_HTTP_HEADER_SUPPRESSION)
870     // Without this, CFNetwork would sometimes add a Content-Type header to our requests (rdar://problem/34748470).
871     configuration._suppressedAutoAddedHTTPHeaders = [NSSet setWithObject:@"Content-Type"];
872 #endif
873
874     if (parameters.allowsCellularAccess == AllowsCellularAccess::No)
875         configuration.allowsCellularAccess = NO;
876
877     // The WebKit network cache was already queried.
878     configuration.URLCache = nil;
879
880     if (auto data = networkProcess.sourceApplicationAuditData())
881         configuration._sourceApplicationAuditTokenData = (__bridge NSData *)data.get();
882
883     if (!parameters.sourceApplicationBundleIdentifier.isEmpty()) {
884         configuration._sourceApplicationBundleIdentifier = parameters.sourceApplicationBundleIdentifier;
885         configuration._sourceApplicationAuditTokenData = nil;
886     }
887
888     if (!parameters.sourceApplicationSecondaryIdentifier.isEmpty())
889         configuration._sourceApplicationSecondaryIdentifier = parameters.sourceApplicationSecondaryIdentifier;
890
891     configuration.connectionProxyDictionary = proxyDictionary(parameters.httpProxy, parameters.httpsProxy);
892
893 #if PLATFORM(IOS_FAMILY)
894     auto& ctDataConnectionServiceType = globalCTDataConnectionServiceType();
895     if (!ctDataConnectionServiceType.isEmpty())
896         configuration._CTDataConnectionServiceType = ctDataConnectionServiceType;
897 #endif
898
899 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
900     networkProcess.supplement<LegacyCustomProtocolManager>()->registerProtocolClass(configuration);
901 #endif
902
903 #if HAVE(TIMINGDATAOPTIONS)
904     configuration._timingDataOptions = _TimingDataOptionsEnableW3CNavigationTiming;
905 #else
906     setCollectsTimingData();
907 #endif
908
909 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101400) || (PLATFORM(IOS_FAMILY) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 120000)
910     // FIXME: Replace @"kCFStreamPropertyAutoErrorOnSystemChange" with a constant from the SDK once rdar://problem/40650244 is in a build.
911     if (networkProcess.suppressesConnectionTerminationOnSystemChange())
912         configuration._socketStreamProperties = @{ @"kCFStreamPropertyAutoErrorOnSystemChange" : @(NO) };
913 #endif
914
915 #if PLATFORM(WATCHOS) && __WATCH_OS_VERSION_MIN_REQUIRED >= 60000
916     configuration._companionProxyPreference = NSURLSessionCompanionProxyPreferencePreferDirectToCloud;
917 #endif
918
919     auto* storageSession = WebCore::NetworkStorageSession::storageSession(parameters.sessionID);
920     RELEASE_ASSERT(storageSession);
921
922     NSHTTPCookieStorage* cookieStorage;
923     if (CFHTTPCookieStorageRef storage = storageSession->cookieStorage().get()) {
924         cookieStorage = [[[NSHTTPCookieStorage alloc] _initWithCFHTTPCookieStorage:storage] autorelease];
925         configuration.HTTPCookieStorage = cookieStorage;
926     } else
927         cookieStorage = storageSession->nsCookieStorage();
928
929 #if HAVE(CFNETWORK_OVERRIDE_SESSION_COOKIE_ACCEPT_POLICY)
930     // We still need to check the selector since CFNetwork updates and WebKit updates are separate
931     // on older macOS.
932     if ([cookieStorage respondsToSelector:@selector(_overrideSessionCookieAcceptPolicy)])
933         cookieStorage._overrideSessionCookieAcceptPolicy = YES;
934 #endif
935
936     m_sessionWithCredentialStorageDelegate = adoptNS([[WKNetworkSessionDelegate alloc] initWithNetworkSession:*this withCredentials:true]);
937     m_sessionWithCredentialStorage = [NSURLSession sessionWithConfiguration:configuration delegate:static_cast<id>(m_sessionWithCredentialStorageDelegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
938     LOG(NetworkSession, "Created NetworkSession with cookieAcceptPolicy %lu", configuration.HTTPCookieStorage.cookieAcceptPolicy);
939
940     configuration.URLCredentialStorage = nil;
941     configuration._shouldSkipPreferredClientCertificateLookup = YES;
942     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=177394
943     // configuration.HTTPCookieStorage = nil;
944     // configuration.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever;
945
946     m_statelessSessionDelegate = adoptNS([[WKNetworkSessionDelegate alloc] initWithNetworkSession:*this withCredentials:false]);
947     m_statelessSession = [NSURLSession sessionWithConfiguration:configuration delegate:static_cast<id>(m_statelessSessionDelegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
948
949 #if ENABLE(RESOURCE_LOAD_STATISTICS)
950     m_resourceLoadStatisticsDirectory = parameters.resourceLoadStatisticsDirectory;
951     setResourceLoadStatisticsEnabled(parameters.enableResourceLoadStatistics);
952 #endif
953 }
954
955 NetworkSessionCocoa::~NetworkSessionCocoa()
956 {
957 }
958
959 void NetworkSessionCocoa::invalidateAndCancel()
960 {
961     NetworkSession::invalidateAndCancel();
962
963     [m_sessionWithCredentialStorage invalidateAndCancel];
964     [m_statelessSession invalidateAndCancel];
965     [m_sessionWithCredentialStorageDelegate sessionInvalidated];
966     [m_statelessSessionDelegate sessionInvalidated];
967 }
968
969 void NetworkSessionCocoa::clearCredentials()
970 {
971 #if !USE(CREDENTIAL_STORAGE_WITH_NETWORK_SESSION)
972     ASSERT(m_dataTaskMapWithCredentials.isEmpty());
973     ASSERT(m_dataTaskMapWithoutState.isEmpty());
974     ASSERT(m_downloadMap.isEmpty());
975     // FIXME: Use resetWithCompletionHandler instead.
976     m_sessionWithCredentialStorage = [NSURLSession sessionWithConfiguration:m_sessionWithCredentialStorage.get().configuration delegate:static_cast<id>(m_sessionWithCredentialStorageDelegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
977     m_statelessSession = [NSURLSession sessionWithConfiguration:m_statelessSession.get().configuration delegate:static_cast<id>(m_statelessSessionDelegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
978 #endif
979 }
980
981 NetworkDataTaskCocoa* NetworkSessionCocoa::dataTaskForIdentifier(NetworkDataTaskCocoa::TaskIdentifier taskIdentifier, WebCore::StoredCredentialsPolicy storedCredentialsPolicy)
982 {
983     ASSERT(RunLoop::isMain());
984     if (storedCredentialsPolicy == WebCore::StoredCredentialsPolicy::Use)
985         return m_dataTaskMapWithCredentials.get(taskIdentifier);
986     return m_dataTaskMapWithoutState.get(taskIdentifier);
987 }
988
989 NSURLSessionDownloadTask* NetworkSessionCocoa::downloadTaskWithResumeData(NSData* resumeData)
990 {
991     return [m_sessionWithCredentialStorage downloadTaskWithResumeData:resumeData];
992 }
993
994 void NetworkSessionCocoa::addDownloadID(NetworkDataTaskCocoa::TaskIdentifier taskIdentifier, DownloadID downloadID)
995 {
996 #ifndef NDEBUG
997     ASSERT(!m_downloadMap.contains(taskIdentifier));
998     for (auto idInMap : m_downloadMap.values())
999         ASSERT(idInMap != downloadID);
1000 #endif
1001     m_downloadMap.add(taskIdentifier, downloadID);
1002 }
1003
1004 DownloadID NetworkSessionCocoa::downloadID(NetworkDataTaskCocoa::TaskIdentifier taskIdentifier)
1005 {
1006     ASSERT(m_downloadMap.get(taskIdentifier).downloadID());
1007     return m_downloadMap.get(taskIdentifier);
1008 }
1009
1010 DownloadID NetworkSessionCocoa::takeDownloadID(NetworkDataTaskCocoa::TaskIdentifier taskIdentifier)
1011 {
1012     auto downloadID = m_downloadMap.take(taskIdentifier);
1013     return downloadID;
1014 }
1015
1016 static bool certificatesMatch(SecTrustRef trust1, SecTrustRef trust2)
1017 {
1018     if (!trust1 || !trust2)
1019         return false;
1020
1021     CFIndex count1 = SecTrustGetCertificateCount(trust1);
1022     CFIndex count2 = SecTrustGetCertificateCount(trust2);
1023     if (count1 != count2)
1024         return false;
1025
1026     for (CFIndex i = 0; i < count1; i++) {
1027         auto cert1 = SecTrustGetCertificateAtIndex(trust1, i);
1028         auto cert2 = SecTrustGetCertificateAtIndex(trust2, i);
1029         RELEASE_ASSERT(cert1);
1030         RELEASE_ASSERT(cert2);
1031         if (!CFEqual(cert1, cert2))
1032             return false;
1033     }
1034
1035     return true;
1036 }
1037
1038 bool NetworkSessionCocoa::allowsSpecificHTTPSCertificateForHost(const WebCore::AuthenticationChallenge& challenge)
1039 {
1040     const String& host = challenge.protectionSpace().host();
1041     NSArray *certificates = [NSURLRequest allowsSpecificHTTPSCertificateForHost:host];
1042     if (!certificates)
1043         return false;
1044
1045     bool requireServerCertificates = challenge.protectionSpace().authenticationScheme() == WebCore::ProtectionSpaceAuthenticationScheme::ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested;
1046     RetainPtr<SecPolicyRef> policy = adoptCF(SecPolicyCreateSSL(requireServerCertificates, host.createCFString().get()));
1047
1048     SecTrustRef trustRef = nullptr;
1049     if (SecTrustCreateWithCertificates((CFArrayRef)certificates, policy.get(), &trustRef) != noErr)
1050         return false;
1051     RetainPtr<SecTrustRef> trust = adoptCF(trustRef);
1052
1053     return certificatesMatch(trust.get(), challenge.nsURLAuthenticationChallenge().protectionSpace.serverTrust);
1054 }
1055
1056 }