SSPCPP-616 - clean up concatenated string literals
[shibboleth/cpp-opensaml.git] / saml / saml2 / binding / impl / SAML2ECPDecoder.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  * SAML2ECPDecoder.cpp
23  * 
24  * SAML 2.0 ECP profile message decoder.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "saml2/binding/SAML2MessageDecoder.h"
31 #include "saml2/core/Protocols.h"
32
33 #include <xmltooling/logging.h>
34 #include <xmltooling/XMLToolingConfig.h>
35 #include <xmltooling/io/HTTPRequest.h>
36 #include <xmltooling/soap/SOAP.h>
37 #include <xmltooling/util/NDC.h>
38 #include <xmltooling/util/ParserPool.h>
39 #include <xmltooling/util/Predicates.h>
40 #include <xmltooling/validation/ValidatorSuite.h>
41
42 using namespace opensaml::saml2p;
43 using namespace opensaml;
44 using namespace soap11;
45 using namespace xmltooling::logging;
46 using namespace xmltooling;
47 using namespace std;
48
49 namespace opensaml {
50     namespace saml2p {              
51         class SAML_DLLLOCAL SAML2ECPDecoder : public SAML2MessageDecoder
52         {
53         public:
54             SAML2ECPDecoder() {}
55             virtual ~SAML2ECPDecoder() {}
56
57             xmltooling::XMLObject* decode(
58                 std::string& relayState,
59                 const GenericRequest& genericRequest,
60                 SecurityPolicy& policy
61                 ) const;
62         };                
63
64         MessageDecoder* SAML_DLLLOCAL SAML2ECPDecoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
65         {
66             return new SAML2ECPDecoder();
67         }
68     };
69 };
70
71 XMLObject* SAML2ECPDecoder::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.SAML2ECP");
81
82     log.debug("validating input");
83     const HTTPRequest* httpRequest = dynamic_cast<const HTTPRequest*>(&genericRequest);
84     if (httpRequest) {
85         string s = httpRequest->getContentType();
86         if (s.find("application/vnd.paos+xml") == string::npos) {
87             log.warn("ignoring incorrect content type (%s)", s.c_str() ? s.c_str() : "none");
88             throw BindingException("Invalid content type for PAOS message.");
89         }
90     }
91
92     const char* data = genericRequest.getRequestBody();
93     if (!data)
94         throw BindingException("PAOS message had an empty request body.");
95     log.debug("received message:\n%s", data);
96     istringstream is(data);
97     
98     // Parse and bind the document into an XMLObject.
99     DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
100         : XMLToolingConfig::getConfig().getParser()).parse(is); 
101     XercesJanitor<DOMDocument> janitor(doc);
102     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
103     janitor.release();
104
105     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
106     if (!env)
107         throw BindingException("Decoded message was not a SOAP 1.1 Envelope.");
108
109     SchemaValidators.validate(env);
110     
111     Body* body = env->getBody();
112     if (body && body->hasChildren()) {
113         Response* response = dynamic_cast<Response*>(body->getUnknownXMLObjects().front());
114         if (response) {
115             // Run through the policy at two layers.
116             extractMessageDetails(*env, genericRequest, samlconstants::SAML20P_NS, policy);
117             policy.evaluate(*env, &genericRequest);
118             policy.reset(true);
119             extractMessageDetails(*response, genericRequest, samlconstants::SAML20P_NS, policy);
120             policy.evaluate(*response, &genericRequest);
121
122             // Check destination URL if this is HTTP.
123             if (httpRequest) {
124                 auto_ptr_char dest(response->getDestination());
125                 const char* dest2 = httpRequest->getRequestURL();
126                 const char* delim = strchr(dest2, '?');
127                 if (response->getSignature() && (!dest.get() || !*(dest.get()))) {
128                     log.error("signed SAML message missing Destination attribute");
129                     throw BindingException("Signed SAML message missing Destination attribute identifying intended destination.");
130                 }
131                 else if (dest.get() && *dest.get() && ((delim && strncmp(dest.get(), dest2, delim - dest2)) || (!delim && strcmp(dest.get(), dest2)))) {
132                     log.error("PAOS response targeted at (%s), but delivered to (%s)", dest.get(), dest2);
133                     throw BindingException("SAML message delivered with PAOS to incorrect server URL.");
134                 }
135             }
136
137             // Check for RelayState header.
138             if (env->getHeader()) {
139                 static const XMLCh RelayState[] = UNICODE_LITERAL_10(R,e,l,a,y,S,t,a,t,e);
140                 const vector<XMLObject*>& blocks = const_cast<const Header*>(env->getHeader())->getUnknownXMLObjects();
141                 vector<XMLObject*>::const_iterator h =
142                     find_if(blocks.begin(), blocks.end(), hasQName(xmltooling::QName(samlconstants::SAML20ECP_NS, RelayState)));
143                 const ElementProxy* ep = dynamic_cast<const ElementProxy*>(h != blocks.end() ? *h : nullptr);
144                 if (ep) {
145                     auto_ptr_char rs(ep->getTextContent());
146                     if (rs.get())
147                         relayState = rs.get();
148                 }
149             }
150             
151             xmlObject.release();
152             body->detach(); // frees Envelope
153             response->detach();   // frees Body
154             return response;
155         }
156     }
157     
158     throw BindingException("SOAP Envelope did not contain a SAML Response.");
159 }