Set fourth file version digit to signify rebuild.
[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 <boost/ptr_container/ptr_vector.hpp>
37 #include <xmltooling/logging.h>
38 #include <xmltooling/XMLToolingConfig.h>
39 #include <xmltooling/io/HTTPRequest.h>
40 #include <xmltooling/util/NDC.h>
41 #include <xmltooling/util/ReplayCache.h>
42
43 using namespace opensaml::saml2md;
44 using namespace opensaml::saml1p;
45 using namespace opensaml;
46 using namespace xmltooling::logging;
47 using namespace xmltooling;
48 using namespace std;
49 using boost::ptr_vector;
50
51 namespace opensaml {
52     namespace saml1p {
53         class SAML_DLLLOCAL SAML1ArtifactDecoder : public SAML1MessageDecoder
54         {
55         public:
56             SAML1ArtifactDecoder() {}
57             virtual ~SAML1ArtifactDecoder() {}
58
59             xmltooling::XMLObject* decode(
60                 std::string& relayState,
61                 const GenericRequest& genericRequest,
62                 SecurityPolicy& policy
63                 ) const;
64         };
65
66         MessageDecoder* SAML_DLLLOCAL SAML1ArtifactDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
67         {
68             return new SAML1ArtifactDecoder();
69         }
70     };
71 };
72
73 XMLObject* SAML1ArtifactDecoder::decode(
74     string& relayState,
75     const GenericRequest& genericRequest,
76     SecurityPolicy& policy
77     ) const
78 {
79 #ifdef _DEBUG
80     xmltooling::NDC ndc("decode");
81 #endif
82     Category& log = Category::getInstance(SAML_LOGCAT ".MessageDecoder.SAML1Artifact");
83
84     log.debug("validating input");
85     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
86     if (!httpRequest)
87         throw BindingException("Unable to cast request object to HTTPRequest type.");
88     vector<const char*> SAMLart;
89     const char* TARGET = httpRequest->getParameter("TARGET");
90     if (httpRequest->getParameters("SAMLart", SAMLart)==0 || !TARGET)
91         throw BindingException("Request missing SAMLart or TARGET query string parameters.");
92     relayState = TARGET;
93
94     if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
95         throw BindingException("Artifact profile requires ArtifactResolver and MetadataProvider implementations be supplied.");
96
97     // Import the artifacts.
98     vector<SAMLArtifact*> artifactptrs; // needed for compatibility with non-Boost API in ArtifactResolver
99     ptr_vector<SAMLArtifact> artifacts;
100     for (vector<const char*>::const_iterator raw=SAMLart.begin(); raw!=SAMLart.end(); ++raw) {
101         try {
102             log.debug("processing encoded artifact (%s)", *raw);
103
104             // Check replay.
105             ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
106             if (replayCache) {
107                 if (!replayCache->check("SAML1Artifact", *raw, time(nullptr) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
108                     log.error("replay detected of artifact (%s)", *raw);
109                     throw BindingException("Rejecting replayed artifact ($1).", params(1,*raw));
110                 }
111             }
112             else
113                 log.warn("replay cache was not provided, this is a serious security risk!");
114
115             artifacts.push_back(SAMLArtifact::parse(*raw));
116             artifactptrs.push_back(&(artifacts.back()));
117         }
118         catch (ArtifactException&) {
119             log.error("error parsing artifact (%s)", *raw);
120             throw;
121         }
122     }
123
124     log.debug("attempting to determine source of artifact(s)...");
125     MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
126     mc.artifact = &(artifacts.front());
127     mc.role = policy.getRole();
128     mc.protocol = samlconstants::SAML11_PROTOCOL_ENUM;
129     mc.protocol2 = samlconstants::SAML10_PROTOCOL_ENUM;
130     pair<const EntityDescriptor*,const RoleDescriptor*> provider=policy.getMetadataProvider()->getEntityDescriptor(mc);
131     if (!provider.first) {
132         log.error(
133             "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
134             SAMLArtifact::toHex(artifacts.front().getBytes()).c_str()
135             );
136         throw BindingException("Metadata lookup failed, unable to determine artifact issuer");
137     }
138
139     if (log.isDebugEnabled()) {
140         auto_ptr_char issuer(provider.first->getEntityID());
141         log.debug("artifact issued by (%s)", issuer.get());
142     }
143
144     if (!provider.second || !dynamic_cast<const IDPSSODescriptor*>(provider.second)) {
145         log.error("unable to find compatible SAML 1.x role (%s) in metadata", policy.getRole()->toString().c_str());
146         throw BindingException("Unable to find compatible metadata role for artifact issuer.");
147     }
148     // Set Issuer for the policy.
149     policy.setIssuer(provider.first->getEntityID());
150     policy.setIssuerMetadata(provider.second);
151
152     log.debug("calling ArtifactResolver...");
153     auto_ptr<Response> response(
154         m_artifactResolver->resolve(artifactptrs, dynamic_cast<const IDPSSODescriptor&>(*provider.second), policy)
155         );
156
157     // The policy should be enforced against the Response by the resolve step.
158
159     return response.release();
160 }