Remove code for handling NetworkProcess authentication challenges in WebProcess
[WebKit-https.git] / Source / WebCore / platform / network / cf / AuthenticationCF.cpp
1 /*
2  * Copyright (C) 2007 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 COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "AuthenticationCF.h"
28
29 #if USE(CFNETWORK)
30
31 #include "AuthenticationChallenge.h"
32 #include "AuthenticationClient.h"
33 #include "Credential.h"
34 #include "ProtectionSpace.h"
35
36 // This header must come before all other CFNetwork headers to work around a CFNetwork bug. It can
37 // be removed entirely once <rdar://problem/9042114> is fixed.
38 #include <CFNetwork/CFURLConnectionPriv.h>
39
40 #include <CFNetwork/CFURLAuthChallengePriv.h>
41 #include <CFNetwork/CFURLCredentialPriv.h>
42 #include <CFNetwork/CFURLProtectionSpacePriv.h>
43
44 namespace WebCore {
45
46 AuthenticationChallenge::AuthenticationChallenge(const ProtectionSpace& protectionSpace,
47                                                  const Credential& proposedCredential,
48                                                  unsigned previousFailureCount,
49                                                  const ResourceResponse& response,
50                                                  const ResourceError& error)
51     : AuthenticationChallengeBase(protectionSpace,
52                                   proposedCredential,
53                                   previousFailureCount,
54                                   response,
55                                   error)
56 {
57 }
58
59 AuthenticationChallenge::AuthenticationChallenge(CFURLAuthChallengeRef cfChallenge,
60                                                  AuthenticationClient* authenticationClient)
61     : AuthenticationChallengeBase(core(CFURLAuthChallengeGetProtectionSpace(cfChallenge)),
62                                   core(CFURLAuthChallengeGetProposedCredential(cfChallenge)),
63                                   CFURLAuthChallengeGetPreviousFailureCount(cfChallenge),
64                                   (CFURLResponseRef)CFURLAuthChallengeGetFailureResponse(cfChallenge),
65                                   CFURLAuthChallengeGetError(cfChallenge))
66     , m_authenticationClient(authenticationClient)
67     , m_cfChallenge(cfChallenge)
68 {
69 }
70
71 AuthenticationClient* AuthenticationChallenge::authenticationClient() const
72 {
73     return m_authenticationClient.get();
74 }
75
76 bool AuthenticationChallenge::platformCompare(const AuthenticationChallenge& a, const AuthenticationChallenge& b)
77 {
78     if (a.authenticationClient() != b.authenticationClient())
79         return false;
80
81     if (a.cfURLAuthChallengeRef() != b.cfURLAuthChallengeRef())
82         return false;
83         
84     return true;
85 }
86
87 CFURLAuthChallengeRef createCF(const AuthenticationChallenge& coreChallenge)
88 {
89     // FIXME: Why not cache CFURLAuthChallengeRef in m_cfChallenge? Foundation counterpart does that.
90
91     CFURLProtectionSpaceRef protectionSpace = createCF(coreChallenge.protectionSpace());
92     CFURLCredentialRef credential = createCF(coreChallenge.proposedCredential());
93     
94     CFURLAuthChallengeRef result = CFURLAuthChallengeCreate(0, protectionSpace, credential,
95                                         coreChallenge.previousFailureCount(),
96                                         coreChallenge.failureResponse().cfURLResponse(),
97                                         coreChallenge.error());
98     CFRelease(protectionSpace);
99     CFRelease(credential);
100     return result;
101 }
102
103 CFURLCredentialRef createCF(const Credential& coreCredential)
104 {
105     CFURLCredentialPersistence persistence = kCFURLCredentialPersistenceNone;
106     switch (coreCredential.persistence()) {
107     case CredentialPersistenceNone:
108         break;
109     case CredentialPersistenceForSession:
110         persistence = kCFURLCredentialPersistenceForSession;
111         break;
112     case CredentialPersistencePermanent:
113         persistence = kCFURLCredentialPersistencePermanent;
114         break;
115     default:
116         ASSERT_NOT_REACHED();
117     }
118     
119 #if CERTIFICATE_CREDENTIALS_SUPPORTED
120     if (coreCredential.type() == CredentialTypeClientCertificate)
121         return CFURLCredentialCreateWithIdentityAndCertificateArray(kCFAllocatorDefault, coreCredential.identity(), coreCredential.certificates(), persistence);
122 #endif
123
124     return CFURLCredentialCreate(0, coreCredential.user().createCFString().get(), coreCredential.password().createCFString().get(), 0, persistence);
125 }
126
127 CFURLProtectionSpaceRef createCF(const ProtectionSpace& coreSpace)
128 {
129     CFURLProtectionSpaceServerType serverType = kCFURLProtectionSpaceServerHTTP;
130     switch (coreSpace.serverType()) {
131     case ProtectionSpaceServerHTTP:
132         serverType = kCFURLProtectionSpaceServerHTTP;
133         break;
134     case ProtectionSpaceServerHTTPS:
135         serverType = kCFURLProtectionSpaceServerHTTPS;
136         break;
137     case ProtectionSpaceServerFTP:
138         serverType = kCFURLProtectionSpaceServerFTP;
139         break;
140     case ProtectionSpaceServerFTPS:
141         serverType = kCFURLProtectionSpaceServerFTPS;
142         break;
143     case ProtectionSpaceProxyHTTP:
144         serverType = kCFURLProtectionSpaceProxyHTTP;
145         break;
146     case ProtectionSpaceProxyHTTPS:
147         serverType = kCFURLProtectionSpaceProxyHTTPS;
148         break;
149     case ProtectionSpaceProxyFTP:
150         serverType = kCFURLProtectionSpaceProxyFTP;
151         break;
152     case ProtectionSpaceProxySOCKS:
153         serverType = kCFURLProtectionSpaceProxySOCKS;
154         break;
155     default:
156         ASSERT_NOT_REACHED();
157     }
158
159     CFURLProtectionSpaceAuthenticationScheme scheme = kCFURLProtectionSpaceAuthenticationSchemeDefault;
160     switch (coreSpace.authenticationScheme()) {
161     case ProtectionSpaceAuthenticationSchemeDefault:
162         scheme = kCFURLProtectionSpaceAuthenticationSchemeDefault;
163         break;
164     case ProtectionSpaceAuthenticationSchemeHTTPBasic:
165         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTTPBasic;
166         break;
167     case ProtectionSpaceAuthenticationSchemeHTTPDigest:
168         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTTPDigest;
169         break;
170     case ProtectionSpaceAuthenticationSchemeHTMLForm:
171         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTMLForm;
172         break;
173     case ProtectionSpaceAuthenticationSchemeNTLM:
174         scheme = kCFURLProtectionSpaceAuthenticationSchemeNTLM;
175         break;
176     case ProtectionSpaceAuthenticationSchemeNegotiate:
177         scheme = kCFURLProtectionSpaceAuthenticationSchemeNegotiate;
178         break;
179 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
180     case ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested:
181         scheme = kCFURLProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested;
182         break;
183     case ProtectionSpaceAuthenticationSchemeClientCertificateRequested:
184         scheme = kCFURLProtectionSpaceAuthenticationSchemeClientCertificateRequested;
185         break;
186 #endif
187     default:
188         ASSERT_NOT_REACHED();
189     }
190
191     return CFURLProtectionSpaceCreate(0, coreSpace.host().createCFString().get(), coreSpace.port(), serverType, coreSpace.realm().createCFString().get(), scheme);
192 }
193
194 Credential core(CFURLCredentialRef cfCredential)
195 {
196     if (!cfCredential)
197         return Credential();
198
199     CredentialPersistence persistence = CredentialPersistenceNone;
200     switch (CFURLCredentialGetPersistence(cfCredential)) {
201     case kCFURLCredentialPersistenceNone:
202         break;
203     case kCFURLCredentialPersistenceForSession:
204         persistence = CredentialPersistenceForSession;
205         break;
206     case kCFURLCredentialPersistencePermanent:
207         persistence = CredentialPersistencePermanent;
208         break;
209     default:
210         ASSERT_NOT_REACHED();
211     }
212
213 #if CERTIFICATE_CREDENTIALS_SUPPORTED
214     SecIdentityRef identity = CFURLCredentialGetCertificateIdentity(cfCredential);
215     if (identity)
216         return Credential(identity, CFURLCredentialGetCertificateArray(cfCredential), persistence);
217 #endif
218
219     RetainPtr<CFStringRef> password(AdoptCF, CFURLCredentialCopyPassword(cfCredential));
220     return Credential(CFURLCredentialGetUsername(cfCredential), password.get(), persistence);
221 }
222
223 ProtectionSpace core(CFURLProtectionSpaceRef cfSpace)
224 {
225     ProtectionSpaceServerType serverType = ProtectionSpaceServerHTTP;
226     
227     switch (CFURLProtectionSpaceGetServerType(cfSpace)) {
228     case kCFURLProtectionSpaceServerHTTP:
229         break;
230     case kCFURLProtectionSpaceServerHTTPS:
231         serverType = ProtectionSpaceServerHTTPS;
232         break;
233     case kCFURLProtectionSpaceServerFTP:
234         serverType = ProtectionSpaceServerFTP;
235         break;
236     case kCFURLProtectionSpaceServerFTPS:
237         serverType = ProtectionSpaceServerFTPS;
238         break;
239     case kCFURLProtectionSpaceProxyHTTP:
240         serverType = ProtectionSpaceProxyHTTP;
241         break;
242     case kCFURLProtectionSpaceProxyHTTPS:
243         serverType = ProtectionSpaceProxyHTTPS;
244         break;
245     case kCFURLProtectionSpaceProxyFTP:
246         serverType = ProtectionSpaceProxyFTP;
247         break;
248     case kCFURLProtectionSpaceProxySOCKS:
249         serverType = ProtectionSpaceProxySOCKS;
250         break;
251     default:
252         ASSERT_NOT_REACHED();
253     }
254
255     ProtectionSpaceAuthenticationScheme scheme = ProtectionSpaceAuthenticationSchemeDefault;
256     
257     switch (CFURLProtectionSpaceGetAuthenticationScheme(cfSpace)) {
258     case kCFURLProtectionSpaceAuthenticationSchemeDefault:
259         scheme = ProtectionSpaceAuthenticationSchemeDefault;
260         break;
261     case kCFURLProtectionSpaceAuthenticationSchemeHTTPBasic:
262         scheme = ProtectionSpaceAuthenticationSchemeHTTPBasic;
263         break;
264     case kCFURLProtectionSpaceAuthenticationSchemeHTTPDigest:
265         scheme = ProtectionSpaceAuthenticationSchemeHTTPDigest;
266         break;
267     case kCFURLProtectionSpaceAuthenticationSchemeHTMLForm:
268         scheme = ProtectionSpaceAuthenticationSchemeHTMLForm;
269         break;
270     case kCFURLProtectionSpaceAuthenticationSchemeNTLM:
271         scheme = ProtectionSpaceAuthenticationSchemeNTLM;
272         break;
273     case kCFURLProtectionSpaceAuthenticationSchemeNegotiate:
274         scheme = ProtectionSpaceAuthenticationSchemeNegotiate;
275         break;
276 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
277     case kCFURLProtectionSpaceAuthenticationSchemeClientCertificateRequested:
278         scheme = ProtectionSpaceAuthenticationSchemeClientCertificateRequested;
279         break;
280     case kCFURLProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested:
281         scheme = ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested;
282         break;
283 #endif
284     default:
285         scheme = ProtectionSpaceAuthenticationSchemeUnknown;
286         ASSERT_NOT_REACHED();
287     }
288         
289     return ProtectionSpace(CFURLProtectionSpaceGetHost(cfSpace), 
290                            CFURLProtectionSpaceGetPort(cfSpace),
291                            serverType,
292                            CFURLProtectionSpaceGetRealm(cfSpace),
293                            scheme);
294 }
295
296 };
297
298 #endif // USE(CFNETWORK)