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/URLEncoder.h"
27 #include "saml2/binding/SAML2Redirect.h"
28 #include "saml2/binding/SAML2RedirectEncoder.h"
29 #include "saml2/core/Protocols.h"
33 #include <log4cpp/Category.hh>
34 #include <xercesc/util/Base64.hpp>
35 #include <xmltooling/util/NDC.h>
37 using namespace opensaml::saml2p;
38 using namespace opensaml;
39 using namespace xmlsignature;
40 using namespace xmltooling;
41 using namespace log4cpp;
46 MessageEncoder* SAML_DLLLOCAL SAML2RedirectEncoderFactory(const DOMElement* const & e)
48 return new SAML2RedirectEncoder(e);
53 long SAML2RedirectEncoder::encode(
54 GenericResponse& genericResponse,
56 const char* destination,
57 const char* recipientID,
58 const char* relayState,
59 const CredentialResolver* credResolver,
60 const XMLCh* sigAlgorithm
64 xmltooling::NDC ndc("encode");
66 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2Redirect");
68 log.debug("validating input");
69 HTTPResponse* httpResponse=dynamic_cast<HTTPResponse*>(&genericResponse);
71 throw BindingException("Unable to cast response interface to HTTPResponse type.");
72 if (xmlObject->getParent())
73 throw BindingException("Cannot encode XML content with parent.");
75 StatusResponseType* response = NULL;
76 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(xmlObject);
78 response = dynamic_cast<StatusResponseType*>(xmlObject);
80 throw BindingException("XML content for SAML 2.0 HTTP-Redirect Encoder must be a SAML 2.0 protocol message.");
83 // Check for XML signature.
84 if (request ? request->getSignature() : response->getSignature()) {
85 log.debug("message already signed, removing native signature due to size considerations");
86 request ? request->setSignature(NULL) : response->setSignature(NULL);
89 log.debug("marshalling, deflating, base64-encoding the message");
90 DOMElement* rootElement = xmlObject->marshall();
92 XMLHelper::serialize(rootElement, xmlbuf);
95 char* deflated = deflate(const_cast<char*>(xmlbuf.c_str()), xmlbuf.length(), &len);
97 throw BindingException("Failed to deflate message.");
99 XMLByte* encoded=Base64::encode(reinterpret_cast<XMLByte*>(deflated),len,&len);
102 throw BindingException("Base64 encoding of XML failed.");
104 // Create beginnings of redirect query string.
105 URLEncoder* escaper = SAMLConfig::getConfig().getURLEncoder();
107 xmlbuf.append(reinterpret_cast<char*>(encoded),len);
108 xmlbuf = (request ? "SAMLRequest=" : "SAMLResponse=") + escaper->encode(xmlbuf.c_str());
110 xmlbuf = xmlbuf + "&RelayState=" + escaper->encode(relayState);
113 // Sign the query string after adding the algorithm.
115 sigAlgorithm = DSIGConstants::s_unicodeStrURIRSA_SHA1;
116 auto_ptr_char alg(sigAlgorithm);
117 xmlbuf = xmlbuf + "&SigAlg=" + escaper->encode(alg.get());
120 memset(sigbuf,0,sizeof(sigbuf));
121 auto_ptr<XSECCryptoKey> key(credResolver->getKey());
122 Signature::createRawSignature(key.get(), sigAlgorithm, xmlbuf.c_str(), xmlbuf.length(), sigbuf, sizeof(sigbuf)-1);
123 xmlbuf = xmlbuf + "&Signature=" + escaper->encode(sigbuf);
126 // Generate redirect.
127 log.debug("message encoded, sending redirect to client");
128 xmlbuf.insert(0,1,(strchr(destination,'?') ? '&' : '?'));
129 xmlbuf.insert(0,destination);
130 long ret = httpResponse->sendRedirect(xmlbuf.c_str());
132 // Cleanup by destroying XML.