2 * Copyright 2001-2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SAML1POSTEncoder.cpp
20 * SAML 1.x POST binding/profile message encoder.
24 #include "exceptions.h"
25 #include "binding/MessageEncoder.h"
26 #include "signature/ContentReference.h"
27 #include "saml1/core/Protocols.h"
31 #include <xercesc/util/Base64.hpp>
32 #include <xsec/framework/XSECDefs.hpp>
33 #include <xmltooling/io/HTTPResponse.h>
34 #include <xmltooling/logging.h>
35 #include <xmltooling/XMLToolingConfig.h>
36 #include <xmltooling/signature/Signature.h>
37 #include <xmltooling/util/NDC.h>
38 #include <xmltooling/util/PathResolver.h>
39 #include <xmltooling/util/TemplateEngine.h>
41 using namespace opensaml::saml1p;
42 using namespace opensaml::saml2md;
43 using namespace opensaml;
44 using namespace xmlsignature;
45 using namespace xmltooling::logging;
46 using namespace xmltooling;
51 class SAML_DLLLOCAL SAML1POSTEncoder : public MessageEncoder
54 SAML1POSTEncoder(const DOMElement* e, const XMLCh* ns);
55 virtual ~SAML1POSTEncoder() {}
57 const XMLCh* getProtocolFamily() const {
58 return samlconstants::SAML11_PROTOCOL_ENUM;
61 const char* getShortName() const {
66 GenericResponse& genericResponse,
68 const char* destination,
69 const EntityDescriptor* recipient=nullptr,
70 const char* relayState=nullptr,
71 const ArtifactGenerator* artifactGenerator=nullptr,
72 const Credential* credential=nullptr,
73 const XMLCh* signatureAlg=nullptr,
74 const XMLCh* digestAlg=nullptr
78 /** Pathname of HTML template for transmission of message via POST. */
82 MessageEncoder* SAML_DLLLOCAL SAML1POSTEncoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
84 return new SAML1POSTEncoder(p.first, p.second);
89 static const XMLCh _template[] = UNICODE_LITERAL_8(t,e,m,p,l,a,t,e);
91 SAML1POSTEncoder::SAML1POSTEncoder(const DOMElement* e, const XMLCh* ns)
92 : m_template(XMLHelper::getAttrString(e, "bindingTemplate.html", _template, ns))
94 if (m_template.empty())
95 throw XMLToolingException("SAML1POSTEncoder requires template XML attribute.");
96 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_template, PathResolver::XMLTOOLING_CFG_FILE);
99 long SAML1POSTEncoder::encode(
100 GenericResponse& genericResponse,
101 XMLObject* xmlObject,
102 const char* destination,
103 const EntityDescriptor* recipient,
104 const char* relayState,
105 const ArtifactGenerator* artifactGenerator,
106 const Credential* credential,
107 const XMLCh* signatureAlg,
108 const XMLCh* digestAlg
112 xmltooling::NDC ndc("encode");
114 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1POST");
115 log.debug("validating input");
117 TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine();
118 if (!engine || !destination)
119 throw BindingException("Encoding response using POST requires a TemplateEngine instance and a destination.");
120 HTTPResponse::sanitizeURL(destination);
121 if (xmlObject->getParent())
122 throw BindingException("Cannot encode XML content with parent.");
123 Response* response = dynamic_cast<Response*>(xmlObject);
125 throw BindingException("XML content for SAML 1.x POST Encoder must be a SAML 1.x <Response>.");
127 throw BindingException("SAML 1.x POST Encoder requires relay state (TARGET) value.");
129 DOMElement* rootElement = nullptr;
131 // Signature based on native XML signing.
132 if (response->getSignature()) {
133 log.debug("response already signed, skipping signature operation");
136 log.debug("signing and marshalling the response");
138 // Build a Signature.
139 Signature* sig = SignatureBuilder::buildSignature();
140 response->setSignature(sig);
142 sig->setSignatureAlgorithm(signatureAlg);
144 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
146 cr->setDigestAlgorithm(digestAlg);
149 // Sign response while marshalling.
150 vector<Signature*> sigs(1,sig);
151 rootElement = response->marshall((DOMDocument*)nullptr,&sigs,credential);
155 log.debug("marshalling the response");
156 rootElement = response->marshall();
159 // Push message into template.
160 TemplateEngine::TemplateParameters pmap;
161 string& xmlbuf = pmap.m_map["SAMLResponse"];
162 XMLHelper::serialize(rootElement, xmlbuf);
163 log.debug("marshalled response:\n%s", xmlbuf.c_str());
165 // Replace with base-64 encoded version.
167 XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(xmlbuf.data()),xmlbuf.size(),&len);
170 xmlbuf.append(reinterpret_cast<char*>(out),len);
171 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
172 XMLString::release(&out);
174 XMLString::release((char**)&out);
178 throw BindingException("Base64 encoding of XML failed.");
181 // Fill in the rest of the data and send to the client.
182 log.debug("message encoded, sending HTML form template to client");
183 ifstream infile(m_template.c_str());
185 throw BindingException("Failed to open HTML template for POST response ($1).", params(1,m_template.c_str()));
186 pmap.m_map["action"] = destination;
187 pmap.m_map["TARGET"] = relayState;
189 engine->run(infile, s, pmap);
190 genericResponse.setContentType("text/html");
191 HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
193 httpResponse->setResponseHeader("Expires", "01-Jan-1997 12:00:00 GMT");
194 httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
195 httpResponse->setResponseHeader("Pragma", "no-cache");
197 long ret = genericResponse.sendResponse(s);
199 // Cleanup by destroying XML.