SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2ArtifactDecoder.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  * SAML2ArtifactDecoder.cpp
23  *
24  * SAML 2.0 Artifact binding message decoder.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "saml2/binding/SAML2Artifact.h"
31 #include "saml2/binding/SAML2MessageDecoder.h"
32 #include "saml2/core/Protocols.h"
33 #include "saml2/metadata/Metadata.h"
34 #include "saml2/metadata/MetadataProvider.h"
35
36 #include <boost/scoped_ptr.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::saml2p;
45 using namespace opensaml::saml2;
46 using namespace opensaml;
47 using namespace xmltooling::logging;
48 using namespace xmltooling;
49 using namespace boost;
50 using namespace std;
51
52 namespace opensaml {
53     namespace saml2p {
54         class SAML_DLLLOCAL SAML2ArtifactDecoder : public SAML2MessageDecoder
55         {
56         public:
57             SAML2ArtifactDecoder() {}
58             virtual ~SAML2ArtifactDecoder() {}
59
60             xmltooling::XMLObject* decode(
61                 std::string& relayState,
62                 const GenericRequest& genericRequest,
63                 SecurityPolicy& policy
64                 ) const;
65         };
66
67         MessageDecoder* SAML_DLLLOCAL SAML2ArtifactDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
68         {
69             return new SAML2ArtifactDecoder();
70         }
71     };
72 };
73
74 XMLObject* SAML2ArtifactDecoder::decode(
75     string& relayState,
76     const GenericRequest& genericRequest,
77     SecurityPolicy& policy
78     ) const
79 {
80 #ifdef _DEBUG
81     xmltooling::NDC ndc("decode");
82 #endif
83     Category& log = Category::getInstance(SAML_LOGCAT ".MessageDecoder.SAML2Artifact");
84
85     log.debug("validating input");
86     const HTTPRequest* httpRequest=dynamic_cast<const HTTPRequest*>(&genericRequest);
87     if (!httpRequest)
88         throw BindingException("Unable to cast request object to HTTPRequest type.");
89     const char* SAMLart = httpRequest->getParameter("SAMLart");
90     if (!SAMLart)
91         throw BindingException("Request missing SAMLart query string or form parameter.");
92     const char* state = httpRequest->getParameter("RelayState");
93     if (state)
94         relayState = state;
95
96     if (!m_artifactResolver || !policy.getMetadataProvider() || !policy.getRole())
97         throw BindingException("Artifact binding requires ArtifactResolver and MetadataProvider implementations be supplied.");
98
99     // Import the artifact.
100     scoped_ptr<SAMLArtifact> artifact;
101     try {
102         log.debug("processing encoded artifact (%s)", SAMLart);
103
104         // Check replay.
105         ReplayCache* replayCache = XMLToolingConfig::getConfig().getReplayCache();
106         if (replayCache) {
107             if (!replayCache->check("SAML2Artifact", SAMLart, time(nullptr) + (2*XMLToolingConfig::getConfig().clock_skew_secs))) {
108                 log.error("replay detected of artifact (%s)", SAMLart);
109                 throw BindingException("Rejecting replayed artifact ($1).", params(1,SAMLart));
110             }
111         }
112         else
113             log.warn("replay cache was not provided, this is a serious security risk!");
114
115         artifact.reset(SAMLArtifact::parse(SAMLart));
116     }
117     catch (ArtifactException&) {
118         log.error("error parsing artifact (%s)", SAMLart);
119         throw;
120     }
121
122     // Check the type.
123     SAML2Artifact* artifact2 = dynamic_cast<SAML2Artifact*>(artifact.get());
124     if (!artifact2) {
125         log.error("wrong artifact type");
126         throw BindingException("Artifact binding requires SAML 2.0 artifact.");
127     }
128
129     log.debug("attempting to determine source of artifact...");
130     MetadataProvider::Criteria& mc = policy.getMetadataProviderCriteria();
131     mc.artifact = artifact.get();
132     mc.role = policy.getRole();
133     mc.protocol = samlconstants::SAML20P_NS;
134     pair<const EntityDescriptor*,const RoleDescriptor*> provider=policy.getMetadataProvider()->getEntityDescriptor(mc);
135     if (!provider.first) {
136         log.error(
137             "metadata lookup failed, unable to determine issuer of artifact (0x%s)",
138             SAMLArtifact::toHex(artifact->getBytes()).c_str()
139             );
140         throw BindingException("Metadata lookup failed, unable to determine artifact issuer.");
141     }
142
143     if (log.isDebugEnabled()) {
144         auto_ptr_char issuer(provider.first->getEntityID());
145         log.debug("lookup succeeded, artifact issued by (%s)", issuer.get());
146     }
147
148     if (!provider.second || !dynamic_cast<const SSODescriptorType*>(provider.second)) {
149         log.error("unable to find compatible SAML 2.0 role (%s) in metadata", policy.getRole()->toString().c_str());
150         throw BindingException("Unable to find compatible metadata role for artifact issuer.");
151     }
152     // Set issuer into policy.
153     policy.setIssuer(provider.first->getEntityID());
154     policy.setIssuerMetadata(provider.second);
155
156     log.debug("calling ArtifactResolver...");
157     auto_ptr<ArtifactResponse> response(
158         m_artifactResolver->resolve(*artifact2, dynamic_cast<const SSODescriptorType&>(*provider.second), policy)
159         );
160
161     // The policy should be enforced against the ArtifactResponse by the resolve step.
162     // Reset only the message state.
163     policy.reset(true);
164
165     // Now extract details from the payload and check that message.
166     XMLObject* payload = response->getPayload();
167     if (!payload) {
168         log.error("ArtifactResponse message did not contain a protocol message");
169         throw BindingException("ArtifactResponse message did not contain a protocol message.");
170     }
171     extractMessageDetails(*payload, genericRequest, samlconstants::SAML20P_NS, policy);
172     policy.evaluate(*payload, &genericRequest);
173
174     // Return the payload only.
175     response.release();
176     payload->detach();
177     return payload;
178 }