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