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 * SAML2RedirectEncoder.cpp
20 * SAML 2.0 HTTP-POST binding message encoder
24 #include "exceptions.h"
25 #include "binding/HTTPResponse.h"
26 #include "binding/MessageEncoder.h"
27 #include "saml2/binding/SAML2Redirect.h"
28 #include "saml2/core/Protocols.h"
32 #include <log4cpp/Category.hh>
33 #include <xercesc/util/Base64.hpp>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/util/URLEncoder.h>
37 using namespace opensaml::saml2p;
38 using namespace opensaml;
39 using namespace xmlsignature;
40 using namespace xmltooling;
41 using namespace log4cpp;
46 class SAML_DLLLOCAL SAML2RedirectEncoder : public MessageEncoder
49 SAML2RedirectEncoder(const DOMElement* e) {}
50 virtual ~SAML2RedirectEncoder() {}
53 GenericResponse& genericResponse,
55 const char* destination,
56 const char* recipientID=NULL,
57 const char* relayState=NULL,
58 const Credential* credential=NULL,
59 const XMLCh* signatureAlg=NULL,
60 const XMLCh* digestAlg=NULL
64 MessageEncoder* SAML_DLLLOCAL SAML2RedirectEncoderFactory(const DOMElement* const & e)
66 return new SAML2RedirectEncoder(e);
71 long SAML2RedirectEncoder::encode(
72 GenericResponse& genericResponse,
74 const char* destination,
75 const char* recipientID,
76 const char* relayState,
77 const Credential* credential,
78 const XMLCh* signatureAlg,
79 const XMLCh* digestAlg
83 xmltooling::NDC ndc("encode");
85 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2Redirect");
87 log.debug("validating input");
88 HTTPResponse* httpResponse=dynamic_cast<HTTPResponse*>(&genericResponse);
90 throw BindingException("Unable to cast response interface to HTTPResponse type.");
91 if (xmlObject->getParent())
92 throw BindingException("Cannot encode XML content with parent.");
94 StatusResponseType* response = NULL;
95 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(xmlObject);
97 response = dynamic_cast<StatusResponseType*>(xmlObject);
99 throw BindingException("XML content for SAML 2.0 HTTP-Redirect Encoder must be a SAML 2.0 protocol message.");
102 // Check for XML signature.
103 if (request ? request->getSignature() : response->getSignature()) {
104 log.debug("message already signed, removing native signature due to size considerations");
105 request ? request->setSignature(NULL) : response->setSignature(NULL);
108 log.debug("marshalling, deflating, base64-encoding the message");
109 DOMElement* rootElement = xmlObject->marshall();
111 XMLHelper::serialize(rootElement, xmlbuf);
114 char* deflated = deflate(const_cast<char*>(xmlbuf.c_str()), xmlbuf.length(), &len);
116 throw BindingException("Failed to deflate message.");
118 XMLByte* encoded=Base64::encode(reinterpret_cast<XMLByte*>(deflated),len,&len);
121 throw BindingException("Base64 encoding of XML failed.");
123 // Create beginnings of redirect query string.
124 const URLEncoder* escaper = XMLToolingConfig::getConfig().getURLEncoder();
126 xmlbuf.append(reinterpret_cast<char*>(encoded),len);
127 xmlbuf = (request ? "SAMLRequest=" : "SAMLResponse=") + escaper->encode(xmlbuf.c_str());
128 if (relayState && *relayState)
129 xmlbuf = xmlbuf + "&RelayState=" + escaper->encode(relayState);
132 // Sign the query string after adding the algorithm.
134 signatureAlg = DSIGConstants::s_unicodeStrURIRSA_SHA1;
135 auto_ptr_char alg(signatureAlg);
136 xmlbuf = xmlbuf + "&SigAlg=" + escaper->encode(alg.get());
139 memset(sigbuf,0,sizeof(sigbuf));
140 Signature::createRawSignature(credential->getPrivateKey(), signatureAlg, xmlbuf.c_str(), xmlbuf.length(), sigbuf, sizeof(sigbuf)-1);
141 xmlbuf = xmlbuf + "&Signature=" + escaper->encode(sigbuf);
144 // Generate redirect.
145 log.debug("message encoded, sending redirect to client");
146 xmlbuf.insert(0,1,(strchr(destination,'?') ? '&' : '?'));
147 xmlbuf.insert(0,destination);
148 long ret = httpResponse->sendRedirect(xmlbuf.c_str());
150 // Cleanup by destroying XML.