af1f78fa3e600653b5ae533f9f31d612b7528846
[WebKit-https.git] / Source / WebKit / UIProcess / Authentication / AuthenticationChallengeProxy.cpp
1 /*
2  * Copyright (C) 2010 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 #include "config.h"
27 #include "AuthenticationChallengeProxy.h"
28
29 #include "AuthenticationDecisionListener.h"
30 #include "AuthenticationManagerMessages.h"
31 #include "ChildProcessProxy.h"
32 #include "WebCertificateInfo.h"
33 #include "WebCoreArgumentCoders.h"
34 #include "WebCredential.h"
35 #include "WebProcessProxy.h"
36 #include "WebProtectionSpace.h"
37
38 #if HAVE(SEC_KEY_PROXY)
39 #include "SecKeyProxyStore.h"
40 #endif
41
42 namespace WebKit {
43
44 AuthenticationChallengeProxy::AuthenticationChallengeProxy(WebCore::AuthenticationChallenge&& authenticationChallenge, uint64_t challengeID, IPC::Connection* connection)
45     : m_coreAuthenticationChallenge(WTFMove(authenticationChallenge))
46     , m_challengeID(challengeID)
47     , m_connection(connection)
48 {
49     ASSERT(m_challengeID);
50     m_listener = AuthenticationDecisionListener::create(this);
51 }
52
53 AuthenticationChallengeProxy::~AuthenticationChallengeProxy()
54 {
55     // If an outstanding AuthenticationChallengeProxy is being destroyed even though it hasn't been responded to yet,
56     // we cancel it here so the process isn't waiting for an answer forever.
57     if (m_challengeID)
58         m_connection->send(Messages::AuthenticationManager::CancelChallenge(m_challengeID), 0);
59
60     if (m_listener)
61         m_listener->detachChallenge();
62 }
63
64 void AuthenticationChallengeProxy::useCredential(WebCredential* credential)
65 {
66     if (!m_challengeID)
67         return;
68
69     uint64_t challengeID = m_challengeID;
70     m_challengeID = 0;
71
72     if (!credential) {
73         m_connection->send(Messages::AuthenticationManager::ContinueWithoutCredentialForChallenge(challengeID), 0);
74         return;
75     }
76
77 #if HAVE(SEC_KEY_PROXY)
78     if (protectionSpace()->authenticationScheme() == WebCore::ProtectionSpaceAuthenticationSchemeClientCertificateRequested) {
79         if (!m_secKeyProxyStore) {
80             m_connection->send(Messages::AuthenticationManager::ContinueWithoutCredentialForChallenge(challengeID), 0);
81             return;
82         }
83         m_secKeyProxyStore->initialize(credential->credential());
84         sendClientCertificateCredentialOverXpc(challengeID, credential->credential());
85         return;
86     }
87 #endif
88     m_connection->send(Messages::AuthenticationManager::UseCredentialForChallenge(challengeID, credential->credential()), 0);
89 }
90
91 void AuthenticationChallengeProxy::cancel()
92 {
93     if (!m_challengeID)
94         return;
95
96     m_connection->send(Messages::AuthenticationManager::CancelChallenge(m_challengeID), 0);
97
98     m_challengeID = 0;
99 }
100
101 void AuthenticationChallengeProxy::performDefaultHandling()
102 {
103     if (!m_challengeID)
104         return;
105
106     m_connection->send(Messages::AuthenticationManager::PerformDefaultHandling(m_challengeID), 0);
107
108     m_challengeID = 0;
109 }
110
111 void AuthenticationChallengeProxy::rejectProtectionSpaceAndContinue()
112 {
113     if (!m_challengeID)
114         return;
115
116     m_connection->send(Messages::AuthenticationManager::RejectProtectionSpaceAndContinue(m_challengeID), 0);
117
118     m_challengeID = 0;
119 }
120
121 WebCredential* AuthenticationChallengeProxy::proposedCredential() const
122 {
123     if (!m_webCredential)
124         m_webCredential = WebCredential::create(m_coreAuthenticationChallenge.proposedCredential());
125         
126     return m_webCredential.get();
127 }
128
129 WebProtectionSpace* AuthenticationChallengeProxy::protectionSpace() const
130 {
131     if (!m_webProtectionSpace)
132         m_webProtectionSpace = WebProtectionSpace::create(m_coreAuthenticationChallenge.protectionSpace());
133         
134     return m_webProtectionSpace.get();
135 }
136
137 #if HAVE(SEC_KEY_PROXY)
138 void AuthenticationChallengeProxy::setSecKeyProxyStore(SecKeyProxyStore& store)
139 {
140     m_secKeyProxyStore = makeWeakPtr(store);
141 }
142 #endif
143
144 } // namespace WebKit