2 * Copyright 2001-2006 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/HTTPRequest.h"
26 #include "saml2/binding/SAML2Redirect.h"
27 #include "saml2/binding/SAML2RedirectDecoder.h"
28 #include "saml2/core/Protocols.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataProvider.h"
32 #include <log4cpp/Category.hh>
33 #include <xercesc/util/Base64.hpp>
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;
43 using namespace log4cpp;
48 MessageDecoder* SAML_DLLLOCAL SAML2RedirectDecoderFactory(const DOMElement* const & e)
50 return new SAML2RedirectDecoder(e);
55 SAML2RedirectDecoder::SAML2RedirectDecoder(const DOMElement* e) {}
57 SAML2RedirectDecoder::~SAML2RedirectDecoder() {}
59 XMLObject* SAML2RedirectDecoder::decode(
61 const GenericRequest& genericRequest,
62 SecurityPolicy& policy
66 xmltooling::NDC ndc("decode");
68 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Redirect");
70 log.debug("validating input");
71 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
73 log.error("unable to cast request to HTTPRequest type");
76 if (strcmp(httpRequest->getMethod(),"GET"))
78 const char* msg = httpRequest->getParameter("SAMLResponse");
80 msg = httpRequest->getParameter("SAMLRequest");
83 const char* state = httpRequest->getParameter("RelayState");
88 state = httpRequest->getParameter("SAMLEncoding");
89 if (state && strcmp(state,samlconstants::SAML20_BINDING_URL_ENCODING_DEFLATE)) {
90 log.warn("SAMLEncoding (%s) was not recognized", state);
94 // Decode the compressed message into SAML. First we base64-decode it.
96 XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(msg),&x);
98 throw BindingException("Unable to decode base64 in Redirect binding message.");
100 // Now we have to inflate it.
102 if (inflate((char*)decoded, x, s)==0) {
103 XMLString::release(&decoded);
104 throw BindingException("Unable to inflate Redirect binding message.");
106 if (log.isDebugEnabled())
107 log.debug("decoded SAML message:\n%s", s.str().c_str());
108 XMLString::release(&decoded);
110 // Parse and bind the document into an XMLObject.
111 DOMDocument* doc = (m_validate ? XMLToolingConfig::getConfig().getValidatingParser()
112 : XMLToolingConfig::getConfig().getParser()).parse(s);
113 XercesJanitor<DOMDocument> janitor(doc);
114 auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
117 saml2::RootObject* root = NULL;
118 StatusResponseType* response = NULL;
119 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(xmlObject.get());
121 response = dynamic_cast<StatusResponseType*>(xmlObject.get());
123 throw BindingException("XML content for SAML 2.0 HTTP-POST Decoder must be a SAML 2.0 protocol message.");
124 root = static_cast<saml2::RootObject*>(response);
127 root = static_cast<saml2::RootObject*>(request);
133 SchemaValidators.validate(xmlObject.get());
135 // Check destination URL.
136 auto_ptr_char dest(request ? request->getDestination() : response->getDestination());
137 const char* dest2 = httpRequest->getRequestURL();
138 if ((root->getSignature() || httpRequest->getParameter("Signature")) && !dest.get() || !*(dest.get())) {
139 log.error("signed SAML message missing Destination attribute");
140 throw BindingException("Signed SAML message missing Destination attribute identifying intended destination.");
142 else if (dest.get() && (!dest2 || !*dest2 || strcmp(dest.get(),dest2))) {
143 log.error("Redirect targeted at (%s), but delivered to (%s)", dest.get(), dest2 ? dest2 : "none");
144 throw BindingException("SAML message delivered with Redirect to incorrect server URL.");
147 // Run through the policy.
148 policy.evaluate(genericRequest, *root);
150 catch (XMLToolingException& ex) {
151 // This is just to maximize the likelihood of attaching a source to the message for support purposes.
152 if (policy.getIssuerMetadata())
153 annotateException(&ex,policy.getIssuerMetadata()); // throws it
155 const Issuer* claimedIssuer = root->getIssuer();
156 if (!claimedIssuer || !claimedIssuer->getName())
158 const EntityDescriptor* provider=NULL;
159 if (!policy.getMetadataProvider() ||
160 !(provider=policy.getMetadataProvider()->getEntityDescriptor(claimedIssuer->getName(), false))) {
162 auto_ptr_char iname(claimedIssuer->getName());
164 ex.addProperty("entityID", iname.get());
168 if (policy.getRole()) {
169 const RoleDescriptor* roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML20P_NS);
170 if (roledesc) annotateException(&ex,roledesc); // throws it
172 annotateException(&ex,provider); // throws it