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 * SAML1ArtifactDecoder.cpp
20 * SAML 1.x Artifact binding/profile message decoder
24 #include "exceptions.h"
25 #include "binding/MessageDecoder.h"
26 #include "binding/SAMLArtifact.h"
27 #include "saml1/core/Protocols.h"
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataProvider.h"
31 #include <log4cpp/Category.hh>
32 #include <xmltooling/io/HTTPRequest.h>
33 #include <xmltooling/util/NDC.h>
34 #include <xmltooling/util/ReplayCache.h>
36 using namespace opensaml::saml2md;
37 using namespace opensaml::saml1p;
38 using namespace opensaml;
39 using namespace xmltooling;
40 using namespace log4cpp;
45 class SAML_DLLLOCAL SAML1ArtifactDecoder : public MessageDecoder
48 SAML1ArtifactDecoder() {}
49 virtual ~SAML1ArtifactDecoder() {}
51 xmltooling::XMLObject* decode(
52 std::string& relayState,
53 const GenericRequest& genericRequest,
54 SecurityPolicy& policy
58 MessageDecoder* SAML_DLLLOCAL SAML1ArtifactDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
60 return new SAML1ArtifactDecoder();
65 XMLObject* SAML1ArtifactDecoder::decode(
67 const GenericRequest& genericRequest,
68 SecurityPolicy& policy
72 xmltooling::NDC ndc("decode");
74 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1Artifact");
76 log.debug("validating input");
77 const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
79 throw BindingException("Unable to cast request object to HTTPRequest type.");
80 if (strcmp(httpRequest->getMethod(),"GET"))
81 throw BindingException("Invalid HTTP method ($1).", params(1, httpRequest->getMethod()));
82 vector<const char*> SAMLart;
83 const char* TARGET = httpRequest->getParameter("TARGET");
84 if (httpRequest->getParameters("SAMLart", SAMLart)==0 || !TARGET)
85 throw BindingException("Request missing SAMLart or TARGET parameters.");
88 if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
89 throw BindingException("Artifact profile requires ArtifactResolver and MetadataProvider implementations be supplied.");
91 // Import the artifacts.
92 vector<SAMLArtifact*> artifacts;
93 for (vector<const char*>::const_iterator raw=SAMLart.begin(); raw!=SAMLart.end(); ++raw) {
95 log.debug("processing encoded artifact (%s)", *raw);
98 ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
100 if (!replayCache->check("SAML1Artifact", *raw, time(NULL) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
101 log.error("replay detected of artifact (%s)", *raw);
102 throw BindingException("Rejecting replayed artifact ($1).", params(1,*raw));
106 log.warn("replay cache was not provided, this is a serious security risk!");
108 artifacts.push_back(SAMLArtifact::parse(*raw));
110 catch (ArtifactException&) {
111 log.error("error parsing artifact (%s)", *raw);
112 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
115 catch (XMLToolingException&) {
116 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
121 log.debug("attempting to determine source of artifact(s)...");
122 const EntityDescriptor* provider=policy.getMetadataProvider()->getEntityDescriptor(artifacts.front());
125 "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
126 SAMLArtifact::toHex(artifacts.front()->getBytes()).c_str()
128 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
129 throw BindingException("Metadata lookup failed, unable to determine artifact issuer");
132 if (log.isDebugEnabled()) {
133 auto_ptr_char issuer(provider->getEntityID());
134 log.debug("lookup succeeded, artifact issued by (%s)", issuer.get());
137 // Mock up an Issuer object for the policy.
138 auto_ptr<saml2::Issuer> issuer(saml2::IssuerBuilder::buildIssuer());
139 issuer->setName(provider->getEntityID());
140 policy.setIssuer(issuer.get());
141 issuer.release(); // owned by policy now
143 log.debug("attempting to find artifact issuing role...");
144 const RoleDescriptor* roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML11_PROTOCOL_ENUM);
146 roledesc=provider->getRoleDescriptor(*(policy.getRole()), samlconstants::SAML10_PROTOCOL_ENUM);
147 if (!roledesc || !dynamic_cast<const IDPSSODescriptor*>(roledesc)) {
148 log.error("unable to find compatible SAML role (%s) in metadata", policy.getRole()->toString().c_str());
149 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
150 throw BindingException("Unable to find compatible metadata role for artifact issuer.");
152 policy.setIssuerMetadata(roledesc);
155 log.debug("calling ArtifactResolver...");
156 auto_ptr<Response> response(
157 m_artifactResolver->resolve(artifacts, dynamic_cast<const IDPSSODescriptor&>(*roledesc), policy)
160 // The policy should be enforced against the Response by the resolve step.
162 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
163 return response.release();
165 catch (XMLToolingException&) {
166 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());