Reducing header overuse, non-inlining selected methods (CPPOST-35).
[shibboleth/cpp-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 "ServiceProvider.h"
26 #include "binding/SOAPClient.h"
27 #include "security/SecurityPolicy.h"
28
29 #include <saml/exceptions.h>
30 #include <saml/saml2/metadata/Metadata.h>
31 #include <saml/signature/ContentReference.h>
32 #include <xmltooling/security/Credential.h>
33 #include <xmltooling/signature/Signature.h>
34 #include <xmltooling/soap/SOAP.h>
35 #include <xmltooling/soap/HTTPSOAPTransport.h>
36 #include <xmltooling/util/NDC.h>
37
38 using namespace shibsp;
39 using namespace opensaml::saml2md;
40 using namespace xmlsignature;
41 using namespace xmltooling;
42 using namespace std;
43
44 SOAPClient::SOAPClient(SecurityPolicy& policy)
45     : opensaml::SOAPClient(policy), m_app(policy.getApplication()), m_relyingParty(NULL), m_credResolver(NULL)
46 {
47 }
48
49 SOAPClient::~SOAPClient()
50 {
51     if (m_credResolver)
52         m_credResolver->unlock();
53 }
54
55 void SOAPClient::send(const soap11::Envelope& env, const char* from, MetadataCredentialCriteria& to, const char* endpoint)
56 {
57     // Check for message signing requirements.   
58     m_relyingParty = m_app.getRelyingParty(dynamic_cast<const EntityDescriptor*>(to.getRole().getParent()));
59     pair<bool,const char*> flag = m_relyingParty->getString("signing");
60     if (flag.first && (!strcmp(flag.second, "true") || !strcmp(flag.second, "back"))) {
61         m_credResolver=m_app.getCredentialResolver();
62         if (m_credResolver) {
63             m_credResolver->lock();
64             // Fill in criteria to use.
65             to.setUsage(Credential::SIGNING_CREDENTIAL);
66             pair<bool,const char*> keyName = m_relyingParty->getString("keyName");
67             if (keyName.first)
68                 to.getKeyNames().insert(keyName.second);
69             pair<bool,const XMLCh*> sigalg = m_relyingParty->getXMLString("signingAlg");
70             if (sigalg.first)
71                 to.setXMLAlgorithm(sigalg.second);
72             const Credential* cred = m_credResolver->resolve(&to);
73             // Reset criteria back.
74             to.setKeyAlgorithm(NULL);
75             to.setKeySize(0);
76             to.getKeyNames().clear();
77
78             if (cred) {
79                 // Check for message.
80                 const vector<XMLObject*>& bodies=const_cast<const soap11::Body*>(env.getBody())->getUnknownXMLObjects();
81                 if (!bodies.empty()) {
82                     opensaml::SignableObject* msg = dynamic_cast<opensaml::SignableObject*>(bodies.front());
83                     if (msg) {
84                         // Build a Signature.
85                         Signature* sig = SignatureBuilder::buildSignature();
86                         msg->setSignature(sig);
87                         if (sigalg.first)
88                             sig->setSignatureAlgorithm(sigalg.second);
89                         sigalg = m_relyingParty->getXMLString("digestAlg");
90                         if (sigalg.first)
91                             dynamic_cast<opensaml::ContentReference*>(sig->getContentReference())->setDigestAlgorithm(sigalg.second);
92
93                         // Sign it. The marshalling step in the base class should be a no-op.
94                         vector<Signature*> sigs(1,sig);
95                         env.marshall((DOMDocument*)NULL,&sigs,cred);
96                     }
97                 }
98             }
99             else {
100                 Category::getInstance(SHIBSP_LOGCAT".SOAPClient").error("no signing credential supplied, leaving unsigned.");
101             }
102         }
103         else {
104             Category::getInstance(SHIBSP_LOGCAT".SOAPClient").error("no CredentialResolver available, leaving unsigned.");
105         }
106     }
107     
108     opensaml::SOAPClient::send(env, from, to, endpoint);
109 }
110
111 void SOAPClient::prepareTransport(SOAPTransport& transport)
112 {
113 #ifdef _DEBUG
114     xmltooling::NDC("prepareTransport");
115 #endif
116     Category& log=Category::getInstance(SHIBSP_LOGCAT".SOAPClient");
117     log.debug("prepping SOAP transport for use by application (%s)", m_app.getId());
118
119     pair<bool,bool> flag = m_relyingParty->getBool("requireConfidentiality");
120     if ((!flag.first || flag.second) && !transport.isConfidential())
121         throw opensaml::BindingException("Transport confidentiality required, but not available."); 
122
123     setValidating(getPolicy().getValidating());
124     flag = m_relyingParty->getBool("requireTransportAuth");
125     forceTransportAuthentication(!flag.first || flag.second);
126
127     opensaml::SOAPClient::prepareTransport(transport);
128
129     pair<bool,const char*> authType=m_relyingParty->getString("authType");
130     if (!authType.first || !strcmp(authType.second,"TLS")) {
131         if (!m_credResolver) {
132             m_credResolver = m_app.getCredentialResolver();
133             if (m_credResolver)
134                 m_credResolver->lock();
135         }
136         if (m_credResolver) {
137             m_criteria->setUsage(Credential::TLS_CREDENTIAL);
138             authType = m_relyingParty->getString("keyName");
139             if (authType.first)
140                 m_criteria->getKeyNames().insert(authType.second);
141             const Credential* cred = m_credResolver->resolve(m_criteria);
142             m_criteria->getKeyNames().clear();
143             if (cred) {
144                 if (!transport.setCredential(cred))
145                     log.error("failed to load Credential into SOAPTransport");
146             }
147             else {
148                 log.error("no TLS credential supplied");
149             }
150         }
151         else {
152             log.error("no CredentialResolver available for TLS");
153         }
154     }
155     else {
156         SOAPTransport::transport_auth_t type=SOAPTransport::transport_auth_none;
157         pair<bool,const char*> username=m_relyingParty->getString("authUsername");
158         pair<bool,const char*> password=m_relyingParty->getString("authPassword");
159         if (!username.first || !password.first)
160             log.error("transport authType (%s) specified but authUsername or authPassword was missing", authType.second);
161         else if (!strcmp(authType.second,"basic"))
162             type = SOAPTransport::transport_auth_basic;
163         else if (!strcmp(authType.second,"digest"))
164             type = SOAPTransport::transport_auth_digest;
165         else if (!strcmp(authType.second,"ntlm"))
166             type = SOAPTransport::transport_auth_ntlm;
167         else if (!strcmp(authType.second,"gss"))
168             type = SOAPTransport::transport_auth_gss;
169         else if (strcmp(authType.second,"none"))
170             log.error("unknown authType (%s) specified for RelyingParty", authType.second);
171         if (type > SOAPTransport::transport_auth_none) {
172             if (transport.setAuth(type,username.second,password.second))
173                 log.debug("configured for transport authentication (method=%s, username=%s)", authType.second, username.second);
174             else
175                 log.error("failed to configure transport authentication (method=%s)", authType.second);
176         }
177     }
178     
179     pair<bool,unsigned int> timeout = m_relyingParty->getUnsignedInt("connectTimeout"); 
180     transport.setConnectTimeout(timeout.first ? timeout.second : 10);
181     timeout = m_relyingParty->getUnsignedInt("timeout");
182     transport.setTimeout(timeout.first ? timeout.second : 20);
183     m_app.getServiceProvider().setTransportOptions(transport);
184
185     HTTPSOAPTransport* http = dynamic_cast<HTTPSOAPTransport*>(&transport);
186     if (http) {
187         flag = m_relyingParty->getBool("chunkedEncoding");
188         http->useChunkedEncoding(flag.first && flag.second);
189         http->setRequestHeader("User-Agent", PACKAGE_NAME);
190         http->setRequestHeader(PACKAGE_NAME, PACKAGE_VERSION);
191     }
192 }
193
194 void SOAPClient::reset()
195 {
196     m_relyingParty = NULL;
197     if (m_credResolver)
198         m_credResolver->unlock();
199     m_credResolver = NULL;
200     opensaml::SOAPClient::reset();
201 }
202