09271745d214bf02116a2d19489d22e758642e4b
[shibboleth/cpp-opensaml.git] / saml / saml1 / binding / impl / SAML1ArtifactDecoder.cpp
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * SAML1ArtifactDecoder.cpp
23  *
24  * SAML 1.x Artifact binding/profile message decoder
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "binding/SAMLArtifact.h"
30 #include "binding/SecurityPolicy.h"
31 #include "saml1/binding/SAML1MessageDecoder.h"
32 #include "saml1/core/Protocols.h"
33 #include "saml2/metadata/Metadata.h"
34 #include "saml2/metadata/MetadataProvider.h"
35
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>
41
42 using namespace opensaml::saml2md;
43 using namespace opensaml::saml1p;
44 using namespace opensaml;
45 using namespace xmltooling::logging;
46 using namespace xmltooling;
47 using namespace std;
48
49 namespace opensaml {
50     namespace saml1p {
51         class SAML_DLLLOCAL SAML1ArtifactDecoder : public SAML1MessageDecoder
52         {
53         public:
54             SAML1ArtifactDecoder() {}
55             virtual ~SAML1ArtifactDecoder() {}
56
57             xmltooling::XMLObject* decode(
58                 std::string& relayState,
59                 const GenericRequest& genericRequest,
60                 SecurityPolicy& policy
61                 ) const;
62         };
63
64         MessageDecoder* SAML_DLLLOCAL SAML1ArtifactDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
65         {
66             return new SAML1ArtifactDecoder();
67         }
68     };
69 };
70
71 XMLObject* SAML1ArtifactDecoder::decode(
72     string& relayState,
73     const GenericRequest& genericRequest,
74     SecurityPolicy& policy
75     ) const
76 {
77 #ifdef _DEBUG
78     xmltooling::NDC ndc("decode");
79 #endif
80     Category& log = Category::getInstance(SAML_LOGCAT".MessageDecoder.SAML1Artifact");
81
82     log.debug("validating input");
83     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
84     if (!httpRequest)
85         throw BindingException("Unable to cast request object to HTTPRequest type.");
86     vector<const char*> SAMLart;
87     const char* TARGET = httpRequest->getParameter("TARGET");
88     if (httpRequest->getParameters("SAMLart", SAMLart)==0 || !TARGET)
89         throw BindingException("Request missing SAMLart or TARGET query string parameters.");
90     relayState = TARGET;
91
92     if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
93         throw BindingException("Artifact profile requires ArtifactResolver and MetadataProvider implementations be supplied.");
94
95     // Import the artifacts.
96     vector<SAMLArtifact*> artifacts;
97     for (vector<const char*>::const_iterator raw=SAMLart.begin(); raw!=SAMLart.end(); ++raw) {
98         try {
99             log.debug("processing encoded artifact (%s)", *raw);
100
101             // Check replay.
102             ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
103             if (replayCache) {
104                 if (!replayCache->check("SAML1Artifact", *raw, time(nullptr) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
105                     log.error("replay detected of artifact (%s)", *raw);
106                     throw BindingException("Rejecting replayed artifact ($1).", params(1,*raw));
107                 }
108             }
109             else
110                 log.warn("replay cache was not provided, this is a serious security risk!");
111
112             artifacts.push_back(SAMLArtifact::parse(*raw));
113         }
114         catch (ArtifactException&) {
115             log.error("error parsing artifact (%s)", *raw);
116             for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
117             throw;
118         }
119         catch (XMLToolingException&) {
120             for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
121             throw;
122         }
123     }
124
125     log.debug("attempting to determine source of artifact(s)...");
126     MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
127     mc.artifact = artifacts.front();
128     mc.role = policy.getRole();
129     mc.protocol = samlconstants::SAML11_PROTOCOL_ENUM;
130     mc.protocol2 = samlconstants::SAML10_PROTOCOL_ENUM;
131     pair<const EntityDescriptor*,const RoleDescriptor*> provider=policy.getMetadataProvider()->getEntityDescriptor(mc);
132     if (!provider.first) {
133         log.error(
134             "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
135             SAMLArtifact::toHex(artifacts.front()->getBytes()).c_str()
136             );
137         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
138         throw BindingException("Metadata lookup failed, unable to determine artifact issuer");
139     }
140
141     if (log.isDebugEnabled()) {
142         auto_ptr_char issuer(provider.first->getEntityID());
143         log.debug("artifact issued by (%s)", issuer.get());
144     }
145
146     if (!provider.second || !dynamic_cast<const IDPSSODescriptor*>(provider.second)) {
147         log.error("unable to find compatible SAML 1.x role (%s) in metadata", policy.getRole()->toString().c_str());
148         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
149         throw BindingException("Unable to find compatible metadata role for artifact issuer.");
150     }
151     // Set Issuer for the policy.
152     policy.setIssuer(provider.first->getEntityID());
153     policy.setIssuerMetadata(provider.second);
154
155     try {
156         log.debug("calling ArtifactResolver...");
157         auto_ptr<Response> response(
158             m_artifactResolver->resolve(artifacts, dynamic_cast<const IDPSSODescriptor&>(*provider.second), policy)
159             );
160
161         // The policy should be enforced against the Response by the resolve step.
162
163         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
164         return response.release();
165     }
166     catch (XMLToolingException&) {
167         for_each(artifacts.begin(), artifacts.end(), xmltooling::cleanup<SAMLArtifact>());
168         throw;
169     }
170 }