2 * Copyright 2001-2007 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 * SAML2POSTEncoder.cpp
20 * SAML 2.0 HTTP-POST binding message encoder
24 #include "exceptions.h"
25 #include "binding/MessageEncoder.h"
26 #include "saml2/core/Protocols.h"
30 #include <log4cpp/Category.hh>
31 #include <xercesc/util/Base64.hpp>
32 #include <xmltooling/util/NDC.h>
33 #include <xmltooling/util/TemplateEngine.h>
35 using namespace opensaml::saml2p;
36 using namespace opensaml;
37 using namespace xmlsignature;
38 using namespace xmltooling;
39 using namespace log4cpp;
44 class SAML_DLLLOCAL SAML2POSTEncoder : public MessageEncoder
47 SAML2POSTEncoder(const DOMElement* e, bool simple=false);
48 virtual ~SAML2POSTEncoder() {}
51 GenericResponse& genericResponse,
53 const char* destination,
54 const char* recipientID=NULL,
55 const char* relayState=NULL,
56 const Credential* credential=NULL,
57 const XMLCh* sigAlgorithm=NULL
65 MessageEncoder* SAML_DLLLOCAL SAML2POSTEncoderFactory(const DOMElement* const & e)
67 return new SAML2POSTEncoder(e, false);
70 MessageEncoder* SAML_DLLLOCAL SAML2POSTSimpleSignEncoderFactory(const DOMElement* const & e)
72 return new SAML2POSTEncoder(e, true);
77 static const XMLCh _template[] = UNICODE_LITERAL_8(t,e,m,p,l,a,t,e);
79 SAML2POSTEncoder::SAML2POSTEncoder(const DOMElement* e, bool simple) : m_simple(simple)
82 auto_ptr_char t(e->getAttributeNS(NULL, _template));
86 if (m_template.empty())
87 throw XMLToolingException("SAML2POSTEncoder requires template XML attribute.");
90 long SAML2POSTEncoder::encode(
91 GenericResponse& genericResponse,
93 const char* destination,
94 const char* recipientID,
95 const char* relayState,
96 const Credential* credential,
97 const XMLCh* sigAlgorithm
101 xmltooling::NDC ndc("encode");
103 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2POST");
105 log.debug("validating input");
106 if (xmlObject->getParent())
107 throw BindingException("Cannot encode XML content with parent.");
109 StatusResponseType* response = NULL;
110 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(xmlObject);
112 response = dynamic_cast<StatusResponseType*>(xmlObject);
114 throw BindingException("XML content for SAML 2.0 HTTP-POST Encoder must be a SAML 2.0 protocol message.");
117 DOMElement* rootElement = NULL;
118 if (credential && !m_simple) {
119 // Signature based on native XML signing.
120 if (request ? request->getSignature() : response->getSignature()) {
121 log.debug("message already signed, skipping signature operation");
124 log.debug("signing and marshalling the message");
126 // Build a Signature.
127 Signature* sig = SignatureBuilder::buildSignature();
128 request ? request->setSignature(sig) : response->setSignature(sig);
130 sig->setSignatureAlgorithm(sigAlgorithm);
132 // Sign response while marshalling.
133 vector<Signature*> sigs(1,sig);
134 rootElement = xmlObject->marshall((DOMDocument*)NULL,&sigs,credential);
138 log.debug("marshalling the message");
139 rootElement = xmlObject->marshall((DOMDocument*)NULL);
142 // Start tracking data.
143 TemplateEngine::TemplateParameters pmap;
145 pmap.m_map["RelayState"] = relayState;
147 // Serialize the message.
148 string& msg = pmap.m_map[(request ? "SAMLRequest" : "SAMLResponse")];
149 XMLHelper::serialize(rootElement, msg);
152 if (credential && m_simple) {
153 log.debug("applying simple signature to message data");
154 string input = (request ? "SAMLRequest=" : "SAMLResponse=") + msg;
156 input = input + "&RelayState=" + relayState;
158 sigAlgorithm = DSIGConstants::s_unicodeStrURIRSA_SHA1;
159 auto_ptr_char alg(sigAlgorithm);
160 pmap.m_map["SigAlg"] = alg.get();
161 input = input + "&SigAlg=" + alg.get();
164 memset(sigbuf,0,sizeof(sigbuf));
165 Signature::createRawSignature(credential->getPrivateKey(), sigAlgorithm, input.c_str(), input.length(), sigbuf, sizeof(sigbuf)-1);
166 pmap.m_map["Signature"] = sigbuf;
169 // Base64 the message.
171 XMLByte* out=Base64::encode(reinterpret_cast<const XMLByte*>(msg.data()),msg.size(),&len);
173 throw BindingException("Base64 encoding of XML failed.");
175 msg.append(reinterpret_cast<char*>(out),len);
176 XMLString::release(&out);
178 // Push message into template and send result to client.
179 log.debug("message encoded, sending HTML form template to client");
180 TemplateEngine* engine = XMLToolingConfig::getConfig().getTemplateEngine();
182 throw BindingException("Encoding message using POST requires a TemplateEngine instance.");
183 ifstream infile(m_template.c_str());
185 throw BindingException("Failed to open HTML template for POST message ($1).", params(1,m_template.c_str()));
186 pmap.m_map["action"] = destination;
188 engine->run(infile, s, pmap);
189 genericResponse.setContentType("text/html");
190 long ret = genericResponse.sendResponse(s);
192 // Cleanup by destroying XML.