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