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.
18 * SAML2POSTDecoder.cpp
20 * SAML 2.0 HTTP POST binding message encoder
24 #include "exceptions.h"
25 #include "saml2/binding/SAML2MessageDecoder.h"
26 #include "saml2/core/Protocols.h"
27 #include "saml2/metadata/Metadata.h"
28 #include "saml2/metadata/MetadataProvider.h"
30 #include <xercesc/framework/MemBufInputSource.hpp>
31 #include <xercesc/framework/Wrapper4InputSource.hpp>
32 #include <xercesc/util/Base64.hpp>
33 #include <xmltooling/logging.h>
34 #include <xmltooling/io/HTTPRequest.h>
35 #include <xmltooling/util/NDC.h>
36 #include <xmltooling/validation/ValidatorSuite.h>
38 using namespace opensaml::saml2md;
39 using namespace opensaml::saml2p;
40 using namespace opensaml::saml2;
41 using namespace opensaml;
42 using namespace xmltooling::logging;
43 using namespace xmltooling;
48 class SAML_DLLLOCAL SAML2POSTDecoder : public SAML2MessageDecoder
52 virtual ~SAML2POSTDecoder() {}
54 xmltooling::XMLObject* decode(
55 std::string& relayState,
56 const GenericRequest& genericRequest,
57 SecurityPolicy& policy
61 MessageDecoder* SAML_DLLLOCAL SAML2POSTDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
63 return new SAML2POSTDecoder();
68 XMLObject* SAML2POSTDecoder::decode(
69 std::string& relayState,
70 const GenericRequest& genericRequest,
71 SecurityPolicy& policy
75 xmltooling::NDC ndc("decode");
77 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2POST");
79 log.debug("validating input");
80 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
82 throw BindingException("Unable to cast request object to HTTPRequest type.");
83 if (strcmp(httpRequest->getMethod(),"POST"))
84 throw BindingException("Invalid HTTP method ($1).", params(1, httpRequest->getMethod()));
85 const char* msg = httpRequest->getParameter("SAMLResponse");
87 msg = httpRequest->getParameter("SAMLRequest");
89 throw BindingException("Request missing SAMLRequest or SAMLResponse form parameter.");
90 const char* state = httpRequest->getParameter("RelayState");
96 // Decode the base64 into SAML.
98 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(msg),&x);
100 throw BindingException("Unable to decode base64 in POST binding message.");
101 log.debugStream() << "decoded SAML message:\n" << decoded << logging::eol;
103 // Parse and bind the document into an XMLObject.
104 MemBufInputSource src(decoded, x, "SAMLMessage", true);
105 Wrapper4InputSource dsrc(&src, false);
106 DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
107 : XMLToolingConfig::getConfig().getParser()).parse(dsrc);
108 XercesJanitor<DOMDocument> janitor(doc);
109 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
112 saml2::RootObject* root = NULL;
113 StatusResponseType* response = NULL;
114 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(xmlObject.get());
116 response = dynamic_cast<StatusResponseType*>(xmlObject.get());
118 throw BindingException("XML content for SAML 2.0 HTTP-POST Decoder must be a SAML 2.0 protocol message.");
119 root = static_cast<saml2::RootObject*>(response);
122 root = static_cast<saml2::RootObject*>(request);
125 SchemaValidators.validate(root);
127 // Run through the policy.
128 extractMessageDetails(*root, genericRequest, samlconstants::SAML20P_NS, policy);
129 policy.evaluate(*root, &genericRequest);
131 // Check destination URL.
132 auto_ptr_char dest(request ? request->getDestination() : response->getDestination());
133 const char* dest2 = httpRequest->getRequestURL();
134 const char* delim = strchr(dest2, '?');
135 if ((root->getSignature() || httpRequest->getParameter("Signature")) && (!dest.get() || !*(dest.get()))) {
136 log.error("signed SAML message missing Destination attribute");
137 throw BindingException("Signed SAML message missing Destination attribute identifying intended destination.");
139 else if (dest.get() && *dest.get() && ((delim && strncmp(dest.get(), dest2, delim - dest2)) || (!delim && strcmp(dest.get(),dest2)))) {
140 log.error("POST targeted at (%s), but delivered to (%s)", dest.get(), dest2);
141 throw BindingException("SAML message delivered with POST to incorrect server URL.");
144 return xmlObject.release();