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 * SAML2ArtifactDecoder.cpp
20 * SAML 2.0 Artifact binding message decoder
24 #include "exceptions.h"
25 #include "binding/HTTPRequest.h"
26 #include "saml/binding/SAMLArtifact.h"
27 #include "saml2/binding/SAML2Artifact.h"
28 #include "saml2/binding/SAML2ArtifactDecoder.h"
29 #include "saml2/core/Protocols.h"
30 #include "saml2/metadata/Metadata.h"
31 #include "saml2/metadata/MetadataProvider.h"
33 #include <log4cpp/Category.hh>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/util/ReplayCache.h>
37 using namespace opensaml::saml2md;
38 using namespace opensaml::saml2p;
39 using namespace opensaml::saml2;
40 using namespace opensaml;
41 using namespace xmltooling;
42 using namespace log4cpp;
47 MessageDecoder* SAML_DLLLOCAL SAML2ArtifactDecoderFactory(const DOMElement* const & e)
49 return new SAML2ArtifactDecoder(e);
54 SAML2ArtifactDecoder::SAML2ArtifactDecoder(const DOMElement* e) {}
56 XMLObject* SAML2ArtifactDecoder::decode(
58 const GenericRequest& genericRequest,
59 SecurityPolicy& policy
63 xmltooling::NDC ndc("decode");
65 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML2Artifact");
67 log.debug("validating input");
68 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
70 log.error("unable to cast request to HTTPRequest type");
73 const char* SAMLart = httpRequest->getParameter("SAMLart");
76 const char* state = httpRequest->getParameter("RelayState");
80 if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
81 throw BindingException("Artifact binding requires ArtifactResolver and MetadataProvider implementations be supplied.");
83 // Import the artifact.
84 SAMLArtifact* artifact=NULL;
86 log.debug("processing encoded artifact (%s)", SAMLart);
89 ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
91 if (!replayCache->check("SAML2Artifact", SAMLart, time(NULL) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
92 log.error("replay detected of artifact (%s)", SAMLart);
93 throw BindingException("Rejecting replayed artifact ($1).", params(1,SAMLart));
97 log.warn("replay cache was not provided, this is a serious security risk!");
99 artifact = SAMLArtifact::parse(SAMLart);
101 catch (ArtifactException&) {
102 log.error("error parsing artifact (%s)", SAMLart);
107 auto_ptr<SAML2Artifact> artifact2(dynamic_cast<SAML2Artifact*>(artifact));
108 if (!artifact2.get()) {
109 throw BindingException("Artifact binding requires SAML 2.0 artifact.");
113 log.debug("attempting to determine source of artifact...");
114 const EntityDescriptor* provider=policy.getMetadataProvider()->getEntityDescriptor(artifact);
117 "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
118 SAMLArtifact::toHex(artifact->getBytes()).c_str()
120 throw BindingException("Metadata lookup failed, unable to determine artifact issuer.");
123 if (log.isDebugEnabled()) {
124 auto_ptr_char issuer(provider->getEntityID());
125 log.debug("lookup succeeded, artifact issued by (%s)", issuer.get());
128 // Mock up an Issuer object for the policy.
129 auto_ptr<Issuer> issuer(IssuerBuilder::buildIssuer());
130 issuer->setName(provider->getEntityID());
131 policy.setIssuer(issuer.get());
132 issuer.release(); // owned by policy now
134 log.debug("attempting to find artifact issuing role...");
135 const RoleDescriptor* roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML20P_NS);
136 if (!roledesc || !dynamic_cast<const SSODescriptorType*>(roledesc)) {
137 log.error("unable to find compatible SAML role (%s) in metadata", policy.getRole()->toString().c_str());
138 throw BindingException("Unable to find compatible metadata role for artifact issuer.");
140 policy.setIssuerMetadata(roledesc);
142 auto_ptr<ArtifactResponse> response(
143 m_artifactResolver->resolve(*(artifact2.get()), dynamic_cast<const SSODescriptorType&>(*roledesc), policy)
146 // The policy should be enforced against the ArtifactResponse by the resolve step.
148 // Extract payload and check that message.
149 XMLObject* payload = response->getPayload();
150 policy.evaluate(*payload, &genericRequest);
152 // Return the payload only.