2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
24 * SAML 2.0 ECP profile message decoder.
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "saml2/binding/SAML2MessageDecoder.h"
31 #include "saml2/core/Protocols.h"
33 #include <xmltooling/logging.h>
34 #include <xmltooling/XMLToolingConfig.h>
35 #include <xmltooling/io/HTTPRequest.h>
36 #include <xmltooling/soap/SOAP.h>
37 #include <xmltooling/util/NDC.h>
38 #include <xmltooling/util/ParserPool.h>
39 #include <xmltooling/util/Predicates.h>
40 #include <xmltooling/validation/ValidatorSuite.h>
42 using namespace opensaml::saml2p;
43 using namespace opensaml;
44 using namespace soap11;
45 using namespace xmltooling::logging;
46 using namespace xmltooling;
51 class SAML_DLLLOCAL SAML2ECPDecoder : public SAML2MessageDecoder
55 virtual ~SAML2ECPDecoder() {}
57 xmltooling::XMLObject* decode(
58 std::string& relayState,
59 const GenericRequest& genericRequest,
60 SecurityPolicy& policy
64 MessageDecoder* SAML_DLLLOCAL SAML2ECPDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
66 return new SAML2ECPDecoder();
71 XMLObject* SAML2ECPDecoder::decode(
73 const GenericRequest& genericRequest,
74 SecurityPolicy& policy
78 xmltooling::NDC ndc("decode");
80 Category& log = Category::getInstance(SAML_LOGCAT ".MessageDecoder.SAML2ECP");
82 log.debug("validating input");
83 const HTTPRequest* httpRequest = dynamic_cast<const HTTPRequest*>(&genericRequest);
85 string s = httpRequest->getContentType();
86 if (s.find("application/vnd.paos+xml") == string::npos) {
87 log.warn("ignoring incorrect content type (%s)", s.c_str() ? s.c_str() : "none");
88 throw BindingException("Invalid content type for PAOS message.");
92 const char* data = genericRequest.getRequestBody();
94 throw BindingException("PAOS message had an empty request body.");
95 log.debug("received message:\n%s", data);
96 istringstream is(data);
98 // Parse and bind the document into an XMLObject.
99 DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
100 : XMLToolingConfig::getConfig().getParser()).parse(is);
101 XercesJanitor<DOMDocument> janitor(doc);
102 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
105 Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
107 throw BindingException("Decoded message was not a SOAP 1.1 Envelope.");
109 SchemaValidators.validate(env);
111 Body* body = env->getBody();
112 if (body && body->hasChildren()) {
113 Response* response = dynamic_cast<Response*>(body->getUnknownXMLObjects().front());
115 // Run through the policy at two layers.
116 extractMessageDetails(*env, genericRequest, samlconstants::SAML20P_NS, policy);
117 policy.evaluate(*env, &genericRequest);
119 extractMessageDetails(*response, genericRequest, samlconstants::SAML20P_NS, policy);
120 policy.evaluate(*response, &genericRequest);
122 // Check destination URL if this is HTTP.
124 auto_ptr_char dest(response->getDestination());
125 const char* dest2 = httpRequest->getRequestURL();
126 const char* delim = strchr(dest2, '?');
127 if (response->getSignature() && (!dest.get() || !*(dest.get()))) {
128 log.error("signed SAML message missing Destination attribute");
129 throw BindingException("Signed SAML message missing Destination attribute identifying intended destination.");
131 else if (dest.get() && *dest.get() && ((delim && strncmp(dest.get(), dest2, delim - dest2)) || (!delim && strcmp(dest.get(), dest2)))) {
132 log.error("PAOS response targeted at (%s), but delivered to (%s)", dest.get(), dest2);
133 throw BindingException("SAML message delivered with PAOS to incorrect server URL.");
137 // Check for RelayState header.
138 if (env->getHeader()) {
139 static const XMLCh RelayState[] = UNICODE_LITERAL_10(R,e,l,a,y,S,t,a,t,e);
140 const vector<XMLObject*>& blocks = const_cast<const Header*>(env->getHeader())->getUnknownXMLObjects();
141 vector<XMLObject*>::const_iterator h =
142 find_if(blocks.begin(), blocks.end(), hasQName(xmltooling::QName(samlconstants::SAML20ECP_NS, RelayState)));
143 const ElementProxy* ep = dynamic_cast<const ElementProxy*>(h != blocks.end() ? *h : nullptr);
145 auto_ptr_char rs(ep->getTextContent());
147 relayState = rs.get();
152 body->detach(); // frees Envelope
153 response->detach(); // frees Body
158 throw BindingException("SOAP Envelope did not contain a SAML Response.");