Imported Upstream version 2.2.1+dfsg
[shibboleth/sp.git] / shibsp / binding / impl / SOAPClient.cpp
1 /*
2  *  Copyright 2001-2009 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * SOAPClient.cpp
19  * 
20  * Specialized SOAPClient for SP environment.
21  */
22
23 #include "internal.h"
24 #include "Application.h"
25 #include "exceptions.h"
26 #include "ServiceProvider.h"
27 #include "binding/SOAPClient.h"
28
29 #include <saml/saml2/metadata/Metadata.h>
30 #include <xmltooling/soap/SOAP.h>
31 #include <xmltooling/soap/HTTPSOAPTransport.h>
32 #include <xmltooling/util/NDC.h>
33
34 using namespace shibsp;
35 using namespace opensaml::saml2md;
36 using namespace xmlsignature;
37 using namespace xmltooling;
38 using namespace std;
39
40 SOAPClient::SOAPClient(SecurityPolicy& policy)
41     : opensaml::SOAPClient(policy), m_app(policy.getApplication()), m_relyingParty(NULL), m_credResolver(NULL)
42 {
43 }
44
45 void SOAPClient::send(const soap11::Envelope& env, const char* from, MetadataCredentialCriteria& to, const char* endpoint)
46 {
47     // Check for message signing requirements.   
48     m_relyingParty = m_app.getRelyingParty(dynamic_cast<const EntityDescriptor*>(to.getRole().getParent()));
49     pair<bool,const char*> flag = m_relyingParty->getString("signing");
50     if (flag.first && (!strcmp(flag.second, "true") || !strcmp(flag.second, "back"))) {
51         m_credResolver=m_app.getCredentialResolver();
52         if (m_credResolver) {
53             m_credResolver->lock();
54             // Fill in criteria to use.
55             to.setUsage(Credential::SIGNING_CREDENTIAL);
56             pair<bool,const char*> keyName = m_relyingParty->getString("keyName");
57             if (keyName.first)
58                 to.getKeyNames().insert(keyName.second);
59             pair<bool,const XMLCh*> sigalg = m_relyingParty->getXMLString("signingAlg");
60             if (sigalg.first)
61                 to.setXMLAlgorithm(sigalg.second);
62             const Credential* cred = m_credResolver->resolve(&to);
63             // Reset criteria back.
64             to.setKeyAlgorithm(NULL);
65             to.setKeySize(0);
66             to.getKeyNames().clear();
67
68             if (cred) {
69                 // Check for message.
70                 const vector<XMLObject*>& bodies=const_cast<const soap11::Body*>(env.getBody())->getUnknownXMLObjects();
71                 if (!bodies.empty()) {
72                     opensaml::SignableObject* msg = dynamic_cast<opensaml::SignableObject*>(bodies.front());
73                     if (msg) {
74                         // Build a Signature.
75                         Signature* sig = SignatureBuilder::buildSignature();
76                         msg->setSignature(sig);
77                         if (sigalg.first)
78                             sig->setSignatureAlgorithm(sigalg.second);
79                         sigalg = m_relyingParty->getXMLString("digestAlg");
80                         if (sigalg.first)
81                             dynamic_cast<opensaml::ContentReference*>(sig->getContentReference())->setDigestAlgorithm(sigalg.second);
82
83                         // Sign it. The marshalling step in the base class should be a no-op.
84                         vector<Signature*> sigs(1,sig);
85                         env.marshall((DOMDocument*)NULL,&sigs,cred);
86                     }
87                 }
88             }
89             else {
90                 Category::getInstance(SHIBSP_LOGCAT".SOAPClient").error("no signing credential supplied, leaving unsigned.");
91             }
92         }
93         else {
94             Category::getInstance(SHIBSP_LOGCAT".SOAPClient").error("no CredentialResolver available, leaving unsigned.");
95         }
96     }
97     
98     opensaml::SOAPClient::send(env, from, to, endpoint);
99 }
100
101 void SOAPClient::prepareTransport(SOAPTransport& transport)
102 {
103 #ifdef _DEBUG
104     xmltooling::NDC("prepareTransport");
105 #endif
106     Category& log=Category::getInstance(SHIBSP_LOGCAT".SOAPClient");
107     log.debug("prepping SOAP transport for use by application (%s)", m_app.getId());
108
109     pair<bool,bool> flag = m_relyingParty->getBool("requireConfidentiality");
110     if ((!flag.first || flag.second) && !transport.isConfidential())
111         throw opensaml::BindingException("Transport confidentiality required, but not available."); 
112
113     setValidating(getPolicy().getValidating());
114     flag = m_relyingParty->getBool("requireTransportAuth");
115     forceTransportAuthentication(!flag.first || flag.second);
116
117     opensaml::SOAPClient::prepareTransport(transport);
118
119     pair<bool,const char*> authType=m_relyingParty->getString("authType");
120     if (!authType.first || !strcmp(authType.second,"TLS")) {
121         if (!m_credResolver) {
122             m_credResolver = m_app.getCredentialResolver();
123             if (m_credResolver)
124                 m_credResolver->lock();
125         }
126         if (m_credResolver) {
127             m_criteria->setUsage(Credential::TLS_CREDENTIAL);
128             authType = m_relyingParty->getString("keyName");
129             if (authType.first)
130                 m_criteria->getKeyNames().insert(authType.second);
131             const Credential* cred = m_credResolver->resolve(m_criteria);
132             m_criteria->getKeyNames().clear();
133             if (cred) {
134                 if (!transport.setCredential(cred))
135                     log.error("failed to load Credential into SOAPTransport");
136             }
137             else {
138                 log.error("no TLS credential supplied");
139             }
140         }
141         else {
142             log.error("no CredentialResolver available for TLS");
143         }
144     }
145     else {
146         SOAPTransport::transport_auth_t type=SOAPTransport::transport_auth_none;
147         pair<bool,const char*> username=m_relyingParty->getString("authUsername");
148         pair<bool,const char*> password=m_relyingParty->getString("authPassword");
149         if (!username.first || !password.first)
150             log.error("transport authType (%s) specified but authUsername or authPassword was missing", authType.second);
151         else if (!strcmp(authType.second,"basic"))
152             type = SOAPTransport::transport_auth_basic;
153         else if (!strcmp(authType.second,"digest"))
154             type = SOAPTransport::transport_auth_digest;
155         else if (!strcmp(authType.second,"ntlm"))
156             type = SOAPTransport::transport_auth_ntlm;
157         else if (!strcmp(authType.second,"gss"))
158             type = SOAPTransport::transport_auth_gss;
159         else if (strcmp(authType.second,"none"))
160             log.error("unknown authType (%s) specified for RelyingParty", authType.second);
161         if (type > SOAPTransport::transport_auth_none) {
162             if (transport.setAuth(type,username.second,password.second))
163                 log.debug("configured for transport authentication (method=%s, username=%s)", authType.second, username.second);
164             else
165                 log.error("failed to configure transport authentication (method=%s)", authType.second);
166         }
167     }
168     
169     pair<bool,unsigned int> timeout = m_relyingParty->getUnsignedInt("connectTimeout"); 
170     transport.setConnectTimeout(timeout.first ? timeout.second : 10);
171     timeout = m_relyingParty->getUnsignedInt("timeout");
172     transport.setTimeout(timeout.first ? timeout.second : 20);
173     m_app.getServiceProvider().setTransportOptions(transport);
174
175     HTTPSOAPTransport* http = dynamic_cast<HTTPSOAPTransport*>(&transport);
176     if (http) {
177         flag = m_relyingParty->getBool("chunkedEncoding");
178         http->useChunkedEncoding(flag.first && flag.second);
179         http->setRequestHeader("User-Agent", PACKAGE_NAME);
180         http->setRequestHeader(PACKAGE_NAME, PACKAGE_VERSION);
181     }
182 }
183
184 void SOAPClient::reset()
185 {
186     m_relyingParty = NULL;
187     if (m_credResolver)
188         m_credResolver->unlock();
189     m_credResolver = NULL;
190     opensaml::SOAPClient::reset();
191 }
192