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 * SAML1ArtifactDecoder.cpp
20 * SAML 1.x Artifact binding/profile message decoder
24 #include "exceptions.h"
25 #include "saml/binding/SAMLArtifact.h"
26 #include "saml1/binding/SAML1ArtifactDecoder.h"
27 #include "saml2/metadata/Metadata.h"
28 #include "saml2/metadata/MetadataProvider.h"
29 #include "security/X509TrustEngine.h"
31 #include <log4cpp/Category.hh>
32 #include <xmltooling/util/NDC.h>
33 #include <xmltooling/util/ReplayCache.h>
35 using namespace opensaml::saml2md;
36 using namespace opensaml::saml1p;
37 using namespace opensaml::saml1;
38 using namespace opensaml;
39 using namespace xmlsignature;
40 using namespace xmltooling;
41 using namespace log4cpp;
46 MessageDecoder* SAML_DLLLOCAL SAML1ArtifactDecoderFactory(const DOMElement* const & e)
48 return new SAML1ArtifactDecoder(e);
53 SAML1ArtifactDecoder::SAML1ArtifactDecoder(const DOMElement* e) {}
55 SAML1ArtifactDecoder::~SAML1ArtifactDecoder() {}
57 Response* SAML1ArtifactDecoder::decode(
59 const RoleDescriptor*& issuer,
61 const HTTPRequest& httpRequest,
62 const MetadataProvider* metadataProvider,
64 const opensaml::TrustEngine* trustEngine
68 xmltooling::NDC ndc("decode");
70 Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1Artifact");
72 log.debug("validating input");
73 if (strcmp(httpRequest.getMethod(),"GET"))
75 vector<const char*> SAMLart;
76 const char* TARGET = httpRequest.getParameter("TARGET");
77 if (httpRequest.getParameters("SAMLart", SAMLart)==0 || !TARGET)
81 if (!m_artifactResolver || !metadataProvider)
82 throw BindingException("Artifact binding requires ArtifactResolver and MetadataProvider implementations be supplied.");
84 // Import the artifacts.
85 vector<SAMLArtifact*> artifacts;
86 for (vector<const char*>::const_iterator raw=SAMLart.begin(); raw!=SAMLart.end(); ++raw) {
88 log.debug("processing encoded artifact (%s)", *raw);
91 ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
93 if (!replayCache->check("SAML1Artifact", *raw, time(NULL) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
94 log.error("replay detected of artifact (%s)", *raw);
95 throw BindingException("Rejecting replayed artifact ($1).", params(1,*raw));
99 log.warn("replay cache was not provided, this is a serious security risk!");
101 artifacts.push_back(SAMLArtifact::parse(*raw));
103 catch (ArtifactException&) {
104 log.error("error parsing artifact (%s)", *raw);
105 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
108 catch (XMLToolingException&) {
109 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
115 issuerTrusted = false;
116 log.debug("attempting to determine source of artifact(s)...");
117 const EntityDescriptor* provider=metadataProvider->getEntityDescriptor(artifacts.front());
120 "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
121 SAMLArtifact::toHex(artifacts.front()->getBytes()).c_str()
123 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
124 throw BindingException("Metadata lookup failed, unable to determine artifact issuer");
127 if (log.isDebugEnabled()) {
128 auto_ptr_char issuer(provider->getEntityID());
129 log.debug("lookup succeeded, artifact issued by (%s)", issuer.get());
132 log.debug("attempting to find artifact issuing role...");
133 issuer=provider->getRoleDescriptor(*role, SAMLConstants::SAML11_PROTOCOL_ENUM);
135 issuer=provider->getRoleDescriptor(*role, SAMLConstants::SAML10_PROTOCOL_ENUM);
136 if (!issuer || !dynamic_cast<const IDPSSODescriptor*>(issuer)) {
137 log.error("unable to find compatible SAML role (%s) in metadata", role->toString().c_str());
138 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
139 BindingException ex("Unable to find compatible metadata role for artifact issuer.");
140 annotateException(&ex,provider); // throws it
144 auto_ptr<Response> response(
145 m_artifactResolver->resolve(
148 dynamic_cast<const IDPSSODescriptor&>(*issuer),
149 dynamic_cast<const X509TrustEngine*>(trustEngine)
153 if (trustEngine && response->getSignature()) {
154 issuerTrusted = trustEngine->validate(*(response->getSignature()), *issuer, metadataProvider->getKeyResolver());
155 if (!issuerTrusted) {
156 log.error("unable to verify signature on message with supplied trust engine");
157 throw BindingException("Message signature failed verification.");
160 else if (!issuerTrusted) {
161 log.warn("unable to verify integrity of the message, leaving untrusted");
164 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
165 return response.release();
167 catch (XMLToolingException& ex) {
168 for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
169 annotateException(&ex,issuer,false);