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.
22 * SAML2ArtifactDecoder.cpp
24 * SAML 2.0 Artifact binding message decoder.
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "saml2/binding/SAML2Artifact.h"
31 #include "saml2/binding/SAML2MessageDecoder.h"
32 #include "saml2/core/Protocols.h"
33 #include "saml2/metadata/Metadata.h"
34 #include "saml2/metadata/MetadataProvider.h"
36 #include <boost/scoped_ptr.hpp>
37 #include <xmltooling/logging.h>
38 #include <xmltooling/XMLToolingConfig.h>
39 #include <xmltooling/io/HTTPRequest.h>
40 #include <xmltooling/util/NDC.h>
41 #include <xmltooling/util/ReplayCache.h>
43 using namespace opensaml::saml2md;
44 using namespace opensaml::saml2p;
45 using namespace opensaml::saml2;
46 using namespace opensaml;
47 using namespace xmltooling::logging;
48 using namespace xmltooling;
49 using namespace boost;
54 class SAML_DLLLOCAL SAML2ArtifactDecoder : public SAML2MessageDecoder
57 SAML2ArtifactDecoder() {}
58 virtual ~SAML2ArtifactDecoder() {}
60 xmltooling::XMLObject* decode(
61 std::string& relayState,
62 const GenericRequest& genericRequest,
63 SecurityPolicy& policy
67 MessageDecoder* SAML_DLLLOCAL SAML2ArtifactDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
69 return new SAML2ArtifactDecoder();
74 XMLObject* SAML2ArtifactDecoder::decode(
76 const GenericRequest& genericRequest,
77 SecurityPolicy& policy
81 xmltooling::NDC ndc("decode");
83 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Artifact");
85 log.debug("validating input");
86 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
88 throw BindingException("Unable to cast request object to HTTPRequest type.");
89 const char* SAMLart = httpRequest->getParameter("SAMLart");
91 throw BindingException("Request missing SAMLart query string or form parameter.");
92 const char* state = httpRequest->getParameter("RelayState");
96 if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
97 throw BindingException("Artifact binding requires ArtifactResolver and MetadataProvider implementations be supplied.");
99 // Import the artifact.
100 scoped_ptr<SAMLArtifact> artifact;
102 log.debug("processing encoded artifact (%s)", SAMLart);
105 ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
107 if (!replayCache->check("SAML2Artifact", SAMLart, time(nullptr) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
108 log.error("replay detected of artifact (%s)", SAMLart);
109 throw BindingException("Rejecting replayed artifact ($1).", params(1,SAMLart));
113 log.warn("replay cache was not provided, this is a serious security risk!");
115 artifact.reset(SAMLArtifact::parse(SAMLart));
117 catch (ArtifactException&) {
118 log.error("error parsing artifact (%s)", SAMLart);
123 SAML2Artifact* artifact2 = dynamic_cast<SAML2Artifact*>(artifact.get());
125 log.error("wrong artifact type");
126 throw BindingException("Artifact binding requires SAML 2.0 artifact.");
129 log.debug("attempting to determine source of artifact...");
130 MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
131 mc.artifact = artifact.get();
132 mc.role = policy.getRole();
133 mc.protocol = samlconstants::SAML20P_NS;
134 pair<const EntityDescriptor*,const RoleDescriptor*> provider=policy.getMetadataProvider()->getEntityDescriptor(mc);
135 if (!provider.first) {
137 "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
138 SAMLArtifact::toHex(artifact->getBytes()).c_str()
140 throw BindingException("Metadata lookup failed, unable to determine artifact issuer.");
143 if (log.isDebugEnabled()) {
144 auto_ptr_char issuer(provider.first->getEntityID());
145 log.debug("lookup succeeded, artifact issued by (%s)", issuer.get());
148 if (!provider.second || !dynamic_cast<const SSODescriptorType*>(provider.second)) {
149 log.error("unable to find compatible SAML 2.0 role (%s) in metadata", policy.getRole()->toString().c_str());
150 throw BindingException("Unable to find compatible metadata role for artifact issuer.");
152 // Set issuer into policy.
153 policy.setIssuer(provider.first->getEntityID());
154 policy.setIssuerMetadata(provider.second);
156 log.debug("calling ArtifactResolver...");
157 auto_ptr<ArtifactResponse> response(
158 m_artifactResolver->resolve(*artifact2, dynamic_cast<const SSODescriptorType&>(*provider.second), policy)
161 // The policy should be enforced against the ArtifactResponse by the resolve step.
162 // Reset only the message state.
165 // Now extract details from the payload and check that message.
166 XMLObject* payload = response->getPayload();
168 log.error("ArtifactResponse message did not contain a protocol message");
169 throw BindingException("ArtifactResponse message did not contain a protocol message.");
171 extractMessageDetails(*payload, genericRequest, samlconstants::SAML20P_NS, policy);
172 policy.evaluate(*payload, &genericRequest);
174 // Return the payload only.