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  * SAML1SOAPDecoder.cpp
23  *
24  * SAML 1.x SOAP binding message decoder.
25  */
26 
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "binding/SecurityPolicy.h"
30 #include "saml1/binding/SAML1MessageDecoder.h"
31 #include "saml1/core/Protocols.h"
32 
33 #include <xmltooling/logging.h>
34 #include <xmltooling/XMLToolingConfig.h>
35 #include <xmltooling/io/GenericRequest.h>
36 #include <xmltooling/soap/SOAP.h>
37 #include <xmltooling/util/NDC.h>
38 #include <xmltooling/util/ParserPool.h>
39 #include <xmltooling/validation/ValidatorSuite.h>
40 
41 using namespace opensaml::saml1p;
42 using namespace opensaml;
43 using namespace soap11;
44 using namespace xmltooling::logging;
45 using namespace xmltooling;
46 using namespace std;
47 
48 namespace opensaml {
49     namespace saml1p {
50         class SAML_DLLLOCAL SAML1SOAPDecoder : public SAML1MessageDecoder
51         {
52         public:
SAML1SOAPDecoder()53             SAML1SOAPDecoder() {}
~SAML1SOAPDecoder()54             virtual ~SAML1SOAPDecoder() {}
55 
isUserAgentPresent() const56             bool isUserAgentPresent() const {
57                 return false;
58             }
59 
60             xmltooling::XMLObject* decode(
61                 std::string& relayState,
62                 const GenericRequest& genericRequest,
63                 GenericResponse* genericResponse,
64                 SecurityPolicy& policy
65                 ) const;
66         };
67 
SAML1SOAPDecoderFactory(const DOMElement * const &,bool)68         MessageDecoder* SAML_DLLLOCAL SAML1SOAPDecoderFactory(const DOMElement* const &, bool)
69         {
70             return new SAML1SOAPDecoder();
71         }
72     };
73 };
74 
decode(string & relayState,const GenericRequest & genericRequest,GenericResponse * genericResponse,SecurityPolicy & policy) const75 XMLObject* SAML1SOAPDecoder::decode(
76     string& relayState,
77     const GenericRequest& genericRequest,
78     GenericResponse* genericResponse,
79     SecurityPolicy& policy
80     ) const
81 {
82 #ifdef _DEBUG
83     xmltooling::NDC ndc("decode");
84 #endif
85     Category& log = Category::getInstance(SAML_LOGCAT ".MessageDecoder.SAML1SOAP");
86 
87     log.debug("validating input");
88     string s = genericRequest.getContentType();
89     if (s.find("text/xml") == string::npos) {
90         log.warn("ignoring incorrect content type (%s)", s.c_str() ? s.c_str() : "none");
91         throw BindingException("Invalid content type for SOAP message.");
92     }
93 
94     const char* data = genericRequest.getRequestBody();
95     if (!data)
96         throw BindingException("SOAP message had an empty request body.");
97     log.debug("received message:\n%s", data);
98     istringstream is(data);
99 
100     // Parse and bind the document into an XMLObject.
101     DOMDocument* doc = (policy.getValidating() ? XMLToolingConfig::getConfig().getValidatingParser()
102         : XMLToolingConfig::getConfig().getParser()).parse(is);
103     XercesJanitor<DOMDocument> janitor(doc);
104     auto_ptr<XMLObject> xmlObject(XMLObjectBuilder::buildOneFromElement(doc->getDocumentElement(), true));
105     janitor.release();
106 
107     Envelope* env = dynamic_cast<Envelope*>(xmlObject.get());
108     if (!env)
109         throw BindingException("Decoded message was not a SOAP 1.1 Envelope.");
110 
111     SchemaValidators.validate(env);
112 
113     Body* body = env->getBody();
114     if (body && body->hasChildren()) {
115         Request* request = dynamic_cast<Request*>(body->getUnknownXMLObjects().front());
116         if (request) {
117             // Run through the policy at two layers.
118             pair<bool,int> minor = request->getMinorVersion();
119             extractMessageDetails(
120                 *env,
121                 genericRequest,
122                 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM,
123                 policy
124                 );
125             policy.evaluate(*env,&genericRequest);
126 
127             // Reset, extract, and run again.
128             policy.reset(true);
129             extractMessageDetails(
130                 *request,
131                 genericRequest,
132                 (minor.first && minor.second==0) ? samlconstants::SAML10_PROTOCOL_ENUM : samlconstants::SAML11_PROTOCOL_ENUM,
133                 policy
134                 );
135             policy.evaluate(*request,&genericRequest);
136             xmlObject.release();
137             body->detach(); // frees Envelope
138             request->detach();   // frees Body
139             return request;
140         }
141     }
142 
143     throw BindingException("SOAP Envelope did not contain a SAML 1.x Request.");
144 }
145