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() {}
58 GenericResponse& genericResponse,
60 const char* destination,
61 const EntityDescriptor* recipient=nullptr,
62 const char* relayState=nullptr,
63 const ArtifactGenerator* artifactGenerator=nullptr,
64 const Credential* credential=nullptr,
65 const XMLCh* signatureAlg=nullptr,
66 const XMLCh* digestAlg=nullptr
70 /** Pathname of HTML template for transmission of message via POST. */
74 MessageEncoder* SAML_DLLLOCAL SAML1POSTEncoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
76 return new SAML1POSTEncoder(p.first, p.second);
81 static const XMLCh _template[] = UNICODE_LITERAL_8(t,e,m,p,l,a,t,e);
83 SAML1POSTEncoder::SAML1POSTEncoder(const DOMElement* e, const XMLCh* ns)
84 : m_template(XMLHelper::getAttrString(e, nullptr, _template, ns))
86 if (m_template.empty())
87 throw XMLToolingException("SAML1POSTEncoder requires template XML attribute.");
88 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_template, PathResolver::XMLTOOLING_CFG_FILE);
91 long SAML1POSTEncoder::encode(
92 GenericResponse& genericResponse,
94 const char* destination,
95 const EntityDescriptor* recipient,
96 const char* relayState,
97 const ArtifactGenerator* artifactGenerator,
98 const Credential* credential,
99 const XMLCh* signatureAlg,
100 const XMLCh* digestAlg
104 xmltooling::NDC ndc("encode");
106 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML1POST");
107 log.debug("validating input");
109 TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine();
110 if (!engine || !destination)
111 throw BindingException("Encoding response using POST requires a TemplateEngine instance and a destination.");
112 HTTPResponse::sanitizeURL(destination);
113 if (xmlObject->getParent())
114 throw BindingException("Cannot encode XML content with parent.");
115 Response* response = dynamic_cast<Response*>(xmlObject);
117 throw BindingException("XML content for SAML 1.x POST Encoder must be a SAML 1.x <Response>.");
119 throw BindingException("SAML 1.x POST Encoder requires relay state (TARGET) value.");
121 DOMElement* rootElement = nullptr;
123 // Signature based on native XML signing.
124 if (response->getSignature()) {
125 log.debug("response already signed, skipping signature operation");
128 log.debug("signing and marshalling the response");
130 // Build a Signature.
131 Signature* sig = SignatureBuilder::buildSignature();
132 response->setSignature(sig);
134 sig->setSignatureAlgorithm(signatureAlg);
136 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
138 cr->setDigestAlgorithm(digestAlg);
141 // Sign response while marshalling.
142 vector<Signature*> sigs(1,sig);
143 rootElement = response->marshall((DOMDocument*)nullptr,&sigs,credential);
147 log.debug("marshalling the response");
148 rootElement = response->marshall();
151 // Push message into template.
152 TemplateEngine::TemplateParameters pmap;
153 string& xmlbuf = pmap.m_map["SAMLResponse"];
154 XMLHelper::serialize(rootElement, xmlbuf);
155 log.debug("marshalled response:\n%s", xmlbuf.c_str());
157 // Replace with base-64 encoded version.
159 XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(xmlbuf.data()),xmlbuf.size(),&len);
162 xmlbuf.append(reinterpret_cast<char*>(out),len);
163 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
164 XMLString::release(&out);
166 XMLString::release((char**)&out);
170 throw BindingException("Base64 encoding of XML failed.");
173 // Fill in the rest of the data and send to the client.
174 log.debug("message encoded, sending HTML form template to client");
175 ifstream infile(m_template.c_str());
177 throw BindingException("Failed to open HTML template for POST response ($1).", params(1,m_template.c_str()));
178 pmap.m_map["action"] = destination;
179 pmap.m_map["TARGET"] = relayState;
181 engine->run(infile, s, pmap);
182 genericResponse.setContentType("text/html");
183 HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
185 httpResponse->setResponseHeader("Expires", "01-Jan-1997 12:00:00 GMT");
186 httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
187 httpResponse->setResponseHeader("Pragma", "no-cache");
189 long ret = genericResponse.sendResponse(s);
191 // Cleanup by destroying XML.