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 <xmltooling/logging.h>
37 #include <xmltooling/XMLToolingConfig.h>
38 #include <xmltooling/io/HTTPRequest.h>
39 #include <xmltooling/util/NDC.h>
40 #include <xmltooling/util/ReplayCache.h>
42 using namespace opensaml::saml2md;
43 using namespace opensaml::saml2p;
44 using namespace opensaml::saml2;
45 using namespace opensaml;
46 using namespace xmltooling::logging;
47 using namespace xmltooling;
52 class SAML_DLLLOCAL SAML2ArtifactDecoder : public SAML2MessageDecoder
55 SAML2ArtifactDecoder() {}
56 virtual ~SAML2ArtifactDecoder() {}
58 xmltooling::XMLObject* decode(
59 std::string& relayState,
60 const GenericRequest& genericRequest,
61 SecurityPolicy& policy
65 MessageDecoder* SAML_DLLLOCAL SAML2ArtifactDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
67 return new SAML2ArtifactDecoder();
72 XMLObject* SAML2ArtifactDecoder::decode(
74 const GenericRequest& genericRequest,
75 SecurityPolicy& policy
79 xmltooling::NDC ndc("decode");
81 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Artifact");
83 log.debug("validating input");
84 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
86 throw BindingException("Unable to cast request object to HTTPRequest type.");
87 const char* SAMLart = httpRequest->getParameter("SAMLart");
89 throw BindingException("Request missing SAMLart query string or form parameter.");
90 const char* state = httpRequest->getParameter("RelayState");
94 if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
95 throw BindingException("Artifact binding requires ArtifactResolver and MetadataProvider implementations be supplied.");
97 // Import the artifact.
98 SAMLArtifact* artifact=nullptr;
100 log.debug("processing encoded artifact (%s)", SAMLart);
103 ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
105 if (!replayCache->check("SAML2Artifact", SAMLart, time(nullptr) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
106 log.error("replay detected of artifact (%s)", SAMLart);
107 throw BindingException("Rejecting replayed artifact ($1).", params(1,SAMLart));
111 log.warn("replay cache was not provided, this is a serious security risk!");
113 artifact = SAMLArtifact::parse(SAMLart);
115 catch (ArtifactException&) {
116 log.error("error parsing artifact (%s)", SAMLart);
121 auto_ptr<SAML2Artifact> artifact2(dynamic_cast<SAML2Artifact*>(artifact));
122 if (!artifact2.get()) {
124 log.error("wrong artifact type");
125 throw BindingException("Artifact binding requires SAML 2.0 artifact.");
128 log.debug("attempting to determine source of artifact...");
129 MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
130 mc.artifact = artifact;
131 mc.role = policy.getRole();
132 mc.protocol = samlconstants::SAML20P_NS;
133 pair<const EntityDescriptor*,const RoleDescriptor*> provider=policy.getMetadataProvider()->getEntityDescriptor(mc);
134 if (!provider.first) {
136 "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
137 SAMLArtifact::toHex(artifact->getBytes()).c_str()
139 throw BindingException("Metadata lookup failed, unable to determine artifact issuer.");
142 if (log.isDebugEnabled()) {
143 auto_ptr_char issuer(provider.first->getEntityID());
144 log.debug("lookup succeeded, artifact issued by (%s)", issuer.get());
147 if (!provider.second || !dynamic_cast<const SSODescriptorType*>(provider.second)) {
148 log.error("unable to find compatible SAML 2.0 role (%s) in metadata", policy.getRole()->toString().c_str());
149 throw BindingException("Unable to find compatible metadata role for artifact issuer.");
151 // Set issuer into policy.
152 policy.setIssuer(provider.first->getEntityID());
153 policy.setIssuerMetadata(provider.second);
155 log.debug("calling ArtifactResolver...");
156 auto_ptr<ArtifactResponse> response(
157 m_artifactResolver->resolve(*(artifact2.get()), dynamic_cast<const SSODescriptorType&>(*provider.second), policy)
160 // The policy should be enforced against the ArtifactResponse by the resolve step.
161 // Reset only the message state.
164 // Now extract details from the payload and check that message.
165 XMLObject* payload = response->getPayload();
167 log.error("ArtifactResponse message did not contain a protocol message");
168 throw BindingException("ArtifactResponse message did not contain a protocol message.");
170 extractMessageDetails(*payload, genericRequest, samlconstants::SAML20P_NS, policy);
171 policy.evaluate(*payload, &genericRequest);
173 // Return the payload only.