tizen beta release
[framework/web/webkit-efl.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     CFURLProtectionSpaceRef protectionSpace = createCF(coreChallenge.protectionSpace());
90     CFURLCredentialRef credential = createCF(coreChallenge.proposedCredential());
91     
92     CFURLAuthChallengeRef result = CFURLAuthChallengeCreate(0, protectionSpace, credential,
93                                         coreChallenge.previousFailureCount(),
94                                         coreChallenge.failureResponse().cfURLResponse(),
95                                         coreChallenge.error());
96     CFRelease(protectionSpace);
97     CFRelease(credential);
98     return result;
99 }
100
101 CFURLCredentialRef createCF(const Credential& coreCredential)
102 {
103     CFURLCredentialPersistence persistence = kCFURLCredentialPersistenceNone;
104     switch (coreCredential.persistence()) {
105     case CredentialPersistenceNone:
106         break;
107     case CredentialPersistenceForSession:
108         persistence = kCFURLCredentialPersistenceForSession;
109         break;
110     case CredentialPersistencePermanent:
111         persistence = kCFURLCredentialPersistencePermanent;
112         break;
113     default:
114         ASSERT_NOT_REACHED();
115     }
116     
117 #if CERTIFICATE_CREDENTIALS_SUPPORTED
118     if (coreCredential.type() == CredentialTypeClientCertificate)
119         return CFURLCredentialCreateWithIdentityAndCertificateArray(kCFAllocatorDefault, coreCredential.identity(), coreCredential.certificates(), persistence);
120 #endif
121
122     CFStringRef user = coreCredential.user().createCFString();
123     CFStringRef password = coreCredential.password().createCFString();
124     CFURLCredentialRef result = CFURLCredentialCreate(0, user, password, 0, persistence);
125     CFRelease(user);
126     CFRelease(password);
127
128     return result;
129 }
130
131 CFURLProtectionSpaceRef createCF(const ProtectionSpace& coreSpace)
132 {
133     CFURLProtectionSpaceServerType serverType = kCFURLProtectionSpaceServerHTTP;
134     switch (coreSpace.serverType()) {
135     case ProtectionSpaceServerHTTP:
136         serverType = kCFURLProtectionSpaceServerHTTP;
137         break;
138     case ProtectionSpaceServerHTTPS:
139         serverType = kCFURLProtectionSpaceServerHTTPS;
140         break;
141     case ProtectionSpaceServerFTP:
142         serverType = kCFURLProtectionSpaceServerFTP;
143         break;
144     case ProtectionSpaceServerFTPS:
145         serverType = kCFURLProtectionSpaceServerFTPS;
146         break;
147     case ProtectionSpaceProxyHTTP:
148         serverType = kCFURLProtectionSpaceProxyHTTP;
149         break;
150     case ProtectionSpaceProxyHTTPS:
151         serverType = kCFURLProtectionSpaceProxyHTTPS;
152         break;
153     case ProtectionSpaceProxyFTP:
154         serverType = kCFURLProtectionSpaceProxyFTP;
155         break;
156     case ProtectionSpaceProxySOCKS:
157         serverType = kCFURLProtectionSpaceProxySOCKS;
158         break;
159     default:
160         ASSERT_NOT_REACHED();
161     }
162
163     CFURLProtectionSpaceAuthenticationScheme scheme = kCFURLProtectionSpaceAuthenticationSchemeDefault;
164     switch (coreSpace.authenticationScheme()) {
165     case ProtectionSpaceAuthenticationSchemeDefault:
166         scheme = kCFURLProtectionSpaceAuthenticationSchemeDefault;
167         break;
168     case ProtectionSpaceAuthenticationSchemeHTTPBasic:
169         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTTPBasic;
170         break;
171     case ProtectionSpaceAuthenticationSchemeHTTPDigest:
172         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTTPDigest;
173         break;
174     case ProtectionSpaceAuthenticationSchemeHTMLForm:
175         scheme = kCFURLProtectionSpaceAuthenticationSchemeHTMLForm;
176         break;
177     case ProtectionSpaceAuthenticationSchemeNTLM:
178         scheme = kCFURLProtectionSpaceAuthenticationSchemeNTLM;
179         break;
180     case ProtectionSpaceAuthenticationSchemeNegotiate:
181         scheme = kCFURLProtectionSpaceAuthenticationSchemeNegotiate;
182         break;
183 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
184     case ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested:
185         scheme = kCFURLProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested;
186         break;
187     case ProtectionSpaceAuthenticationSchemeClientCertificateRequested:
188         scheme = kCFURLProtectionSpaceAuthenticationSchemeClientCertificateRequested;
189         break;
190 #endif
191     default:
192         ASSERT_NOT_REACHED();
193     }
194
195     CFStringRef host = coreSpace.host().createCFString();
196     CFStringRef realm = coreSpace.realm().createCFString();
197     CFURLProtectionSpaceRef result = CFURLProtectionSpaceCreate(0, host, coreSpace.port(), serverType, realm, scheme);
198     CFRelease(host);
199     CFRelease(realm);
200     
201     return result;
202 }
203
204 Credential core(CFURLCredentialRef cfCredential)
205 {
206     if (!cfCredential)
207         return Credential();
208
209     CredentialPersistence persistence = CredentialPersistenceNone;
210     switch (CFURLCredentialGetPersistence(cfCredential)) {
211     case kCFURLCredentialPersistenceNone:
212         break;
213     case kCFURLCredentialPersistenceForSession:
214         persistence = CredentialPersistenceForSession;
215         break;
216     case kCFURLCredentialPersistencePermanent:
217         persistence = CredentialPersistencePermanent;
218         break;
219     default:
220         ASSERT_NOT_REACHED();
221     }
222
223 #if CERTIFICATE_CREDENTIALS_SUPPORTED
224     SecIdentityRef identity = CFURLCredentialGetCertificateIdentity(cfCredential);
225     if (identity)
226         return Credential(identity, CFURLCredentialGetCertificateArray(cfCredential), persistence);
227 #endif
228
229     RetainPtr<CFStringRef> password(AdoptCF, CFURLCredentialCopyPassword(cfCredential));
230     return Credential(CFURLCredentialGetUsername(cfCredential), password.get(), persistence);
231 }
232
233 ProtectionSpace core(CFURLProtectionSpaceRef cfSpace)
234 {
235     ProtectionSpaceServerType serverType = ProtectionSpaceServerHTTP;
236     
237     switch (CFURLProtectionSpaceGetServerType(cfSpace)) {
238     case kCFURLProtectionSpaceServerHTTP:
239         break;
240     case kCFURLProtectionSpaceServerHTTPS:
241         serverType = ProtectionSpaceServerHTTPS;
242         break;
243     case kCFURLProtectionSpaceServerFTP:
244         serverType = ProtectionSpaceServerFTP;
245         break;
246     case kCFURLProtectionSpaceServerFTPS:
247         serverType = ProtectionSpaceServerFTPS;
248         break;
249     case kCFURLProtectionSpaceProxyHTTP:
250         serverType = ProtectionSpaceProxyHTTP;
251         break;
252     case kCFURLProtectionSpaceProxyHTTPS:
253         serverType = ProtectionSpaceProxyHTTPS;
254         break;
255     case kCFURLProtectionSpaceProxyFTP:
256         serverType = ProtectionSpaceProxyFTP;
257         break;
258     case kCFURLProtectionSpaceProxySOCKS:
259         serverType = ProtectionSpaceProxySOCKS;
260         break;
261     default:
262         ASSERT_NOT_REACHED();
263     }
264
265     ProtectionSpaceAuthenticationScheme scheme = ProtectionSpaceAuthenticationSchemeDefault;
266     
267     switch (CFURLProtectionSpaceGetAuthenticationScheme(cfSpace)) {
268     case kCFURLProtectionSpaceAuthenticationSchemeDefault:
269         scheme = ProtectionSpaceAuthenticationSchemeDefault;
270         break;
271     case kCFURLProtectionSpaceAuthenticationSchemeHTTPBasic:
272         scheme = ProtectionSpaceAuthenticationSchemeHTTPBasic;
273         break;
274     case kCFURLProtectionSpaceAuthenticationSchemeHTTPDigest:
275         scheme = ProtectionSpaceAuthenticationSchemeHTTPDigest;
276         break;
277     case kCFURLProtectionSpaceAuthenticationSchemeHTMLForm:
278         scheme = ProtectionSpaceAuthenticationSchemeHTMLForm;
279         break;
280     case kCFURLProtectionSpaceAuthenticationSchemeNTLM:
281         scheme = ProtectionSpaceAuthenticationSchemeNTLM;
282         break;
283     case kCFURLProtectionSpaceAuthenticationSchemeNegotiate:
284         scheme = ProtectionSpaceAuthenticationSchemeNegotiate;
285         break;
286 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
287     case kCFURLProtectionSpaceAuthenticationSchemeClientCertificateRequested:
288         scheme = ProtectionSpaceAuthenticationSchemeClientCertificateRequested;
289         break;
290     case kCFURLProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested:
291         scheme = ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested;
292         break;
293 #endif
294     default:
295         scheme = ProtectionSpaceAuthenticationSchemeUnknown;
296         ASSERT_NOT_REACHED();
297     }
298         
299     return ProtectionSpace(CFURLProtectionSpaceGetHost(cfSpace), 
300                            CFURLProtectionSpaceGetPort(cfSpace),
301                            serverType,
302                            CFURLProtectionSpaceGetRealm(cfSpace),
303                            scheme);
304 }
305
306 };
307
308 #endif // USE(CFNETWORK)