2 * Copyright 2001-2009 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SAML 2.0 ECP profile message decoder.
24 #include "exceptions.h"
25 #include "binding/SecurityPolicy.h"
26 #include "saml2/binding/SAML2MessageDecoder.h"
27 #include "saml2/core/Protocols.h"
29 #include <xmltooling/logging.h>
30 #include <xmltooling/XMLToolingConfig.h>
31 #include <xmltooling/io/HTTPRequest.h>
32 #include <xmltooling/soap/SOAP.h>
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/util/ParserPool.h>
35 #include <xmltooling/util/Predicates.h>
36 #include <xmltooling/validation/ValidatorSuite.h>
38 using namespace opensaml::saml2p;
39 using namespace opensaml;
40 using namespace soap11;
41 using namespace xmltooling::logging;
42 using namespace xmltooling;
47 class SAML_DLLLOCAL SAML2ECPDecoder : public SAML2MessageDecoder
51 virtual ~SAML2ECPDecoder() {}
53 xmltooling::XMLObject* decode(
54 std::string& relayState,
55 const GenericRequest& genericRequest,
56 SecurityPolicy& policy
60 MessageDecoder* SAML_DLLLOCAL SAML2ECPDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
62 return new SAML2ECPDecoder();
67 XMLObject* SAML2ECPDecoder::decode(
69 const GenericRequest& genericRequest,
70 SecurityPolicy& policy
74 xmltooling::NDC ndc("decode");
76 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2ECP");
78 log.debug("validating input");
79 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
81 throw BindingException("Unable to cast request object to HTTPRequest type.");
82 string s = genericRequest.getContentType();
83 if (s.find("application/vnd.paos+xml") == string::npos) {
84 log.warn("ignoring incorrect content type (%s)", s.c_str() ? s.c_str() : "none");
85 throw BindingException("Invalid content type for PAOS message.");
88 const char* data = genericRequest.getRequestBody();
90 throw BindingException("PAOS message had an empty request body.");
91 log.debug("received message:\n%s", data);
92 istringstream is(data);
94 // Parse and bind the document into an XMLObject.
95 DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
96 : XMLToolingConfig::getConfig().getParser()).parse(is);
97 XercesJanitor<DOMDocument> janitor(doc);
98 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
101 Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
103 throw BindingException("Decoded message was not a SOAP 1.1 Envelope.");
105 SchemaValidators.validate(env);
107 Body* body = env->getBody();
108 if (body && body->hasChildren()) {
109 Response* response = dynamic_cast<Response*>(body->getUnknownXMLObjects().front());
111 // Run through the policy at two layers.
112 extractMessageDetails(*env, genericRequest, samlconstants::SAML20P_NS, policy);
113 policy.evaluate(*env, &genericRequest);
115 extractMessageDetails(*response, genericRequest, samlconstants::SAML20P_NS, policy);
116 policy.evaluate(*response, &genericRequest);
118 // Check destination URL.
119 auto_ptr_char dest(response->getDestination());
120 const char* dest2 = httpRequest->getRequestURL();
121 const char* delim = strchr(dest2, '?');
122 if (response->getSignature() && (!dest.get() || !*(dest.get()))) {
123 log.error("signed SAML message missing Destination attribute");
124 throw BindingException("Signed SAML message missing Destination attribute identifying intended destination.");
126 else if (dest.get() && *dest.get() && ((delim && strncmp(dest.get(), dest2, delim - dest2)) || (!delim && strcmp(dest.get(),dest2)))) {
127 log.error("PAOS response targeted at (%s), but delivered to (%s)", dest.get(), dest2);
128 throw BindingException("SAML message delivered with PAOS to incorrect server URL.");
131 // Check for RelayState header.
132 if (env->getHeader()) {
133 static const XMLCh RelayState[] = UNICODE_LITERAL_10(R,e,l,a,y,S,t,a,t,e);
134 const vector<XMLObject*>& blocks = const_cast<const Header*>(env->getHeader())->getUnknownXMLObjects();
135 vector<XMLObject*>::const_iterator h =
136 find_if(blocks.begin(), blocks.end(), hasQName(xmltooling::QName(samlconstants::SAML20ECP_NS, RelayState)));
137 const ElementProxy* ep = dynamic_cast<const ElementProxy*>(h != blocks.end() ? *h : NULL);
139 auto_ptr_char rs(ep->getTextContent());
141 relayState = rs.get();
146 body->detach(); // frees Envelope
147 response->detach(); // frees Body
152 throw BindingException("SOAP Envelope did not contain a SAML Response.");