2 * Copyright 2001-2007 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 * SAML2RedirectDecoder.cpp
20 * SAML 2.0 HTTP Redirect binding message encoder
24 #include "exceptions.h"
25 #include "binding/MessageDecoder.h"
26 #include "saml2/binding/SAML2Redirect.h"
27 #include "saml2/core/Protocols.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
31 #include <xercesc/util/Base64.hpp>
32 #include <xmltooling/logging.h>
33 #include <xmltooling/io/HTTPRequest.h>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/validation/ValidatorSuite.h>
37 using namespace opensaml::saml2md;
38 using namespace opensaml::saml2p;
39 using namespace opensaml::saml2;
40 using namespace opensaml;
41 using namespace xmlsignature;
42 using namespace xmltooling::logging;
43 using namespace xmltooling;
48 class SAML_DLLLOCAL SAML2RedirectDecoder : public MessageDecoder
51 SAML2RedirectDecoder() {}
52 virtual ~SAML2RedirectDecoder() {}
54 xmltooling::XMLObject* decode(
55 std::string& relayState,
56 const GenericRequest& genericRequest,
57 SecurityPolicy& policy
61 MessageDecoder* SAML_DLLLOCAL SAML2RedirectDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
63 return new SAML2RedirectDecoder();
68 XMLObject* SAML2RedirectDecoder::decode(
70 const GenericRequest& genericRequest,
71 SecurityPolicy& policy
75 xmltooling::NDC ndc("decode");
77 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Redirect");
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(),"GET"))
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 parameter.");
90 const char* state = httpRequest->getParameter("RelayState");
95 state = httpRequest->getParameter("SAMLEncoding");
96 if (state && strcmp(state,samlconstants::SAML20_BINDING_URL_ENCODING_DEFLATE)) {
97 log.warn("SAMLEncoding (%s) was not recognized", state);
98 throw BindingException("Unsupported SAMLEncoding value.");
101 // Decode the compressed message into SAML. First we base64-decode it.
103 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(msg),&x);
105 throw BindingException("Unable to decode base64 in Redirect binding message.");
107 // Now we have to inflate it.
109 if (inflate((char*)decoded, x, s)==0) {
110 XMLString::release(&decoded);
111 throw BindingException("Unable to inflate Redirect binding message.");
113 if (log.isDebugEnabled())
114 log.debug("decoded SAML message:\n%s", s.str().c_str());
115 XMLString::release(&decoded);
117 // Parse and bind the document into an XMLObject.
118 DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
119 : XMLToolingConfig::getConfig().getParser()).parse(s);
120 XercesJanitor<DOMDocument> janitor(doc);
121 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
124 saml2::RootObject* root = NULL;
125 StatusResponseType* response = NULL;
126 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(xmlObject.get());
128 response = dynamic_cast<StatusResponseType*>(xmlObject.get());
130 throw BindingException("XML content for SAML 2.0 HTTP-POST Decoder must be a SAML 2.0 protocol message.");
131 root = static_cast<saml2::RootObject*>(response);
134 root = static_cast<saml2::RootObject*>(request);
137 if (!policy.getValidating())
138 SchemaValidators.validate(xmlObject.get());
140 // Run through the policy.
141 policy.evaluate(*root, &genericRequest, samlconstants::SAML20P_NS);
143 // Check destination URL.
144 auto_ptr_char dest(request ? request->getDestination() : response->getDestination());
145 const char* dest2 = httpRequest->getRequestURL();
146 const char* delim = strchr(dest2, '?');
147 if ((root->getSignature() || httpRequest->getParameter("Signature")) && (!dest.get() || !*(dest.get()))) {
148 log.error("signed SAML message missing Destination attribute");
149 throw BindingException("Signed SAML message missing Destination attribute identifying intended destination.");
151 else if (dest.get() && *dest.get() && ((delim && strncmp(dest.get(), dest2, delim - dest2)) || (!delim && strcmp(dest.get(),dest2)))) {
152 log.error("Redirect targeted at (%s), but delivered to (%s)", dest.get(), dest2);
153 throw BindingException("SAML message delivered with Redirect to incorrect server URL.");
156 return xmlObject.release();