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/MessageEncoder.h"
26 #include "saml2/binding/SAML2Redirect.h"
27 #include "saml2/core/Protocols.h"
31 #include <xercesc/util/Base64.hpp>
32 #include <xmltooling/logging.h>
33 #include <xmltooling/io/HTTPResponse.h>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/util/URLEncoder.h>
37 using namespace opensaml::saml2p;
38 using namespace opensaml::saml2md;
39 using namespace opensaml;
40 using namespace xmlsignature;
41 using namespace xmltooling::logging;
42 using namespace xmltooling;
47 class SAML_DLLLOCAL SAML2RedirectEncoder : public MessageEncoder
50 SAML2RedirectEncoder() {}
51 virtual ~SAML2RedirectEncoder() {}
53 bool isCompact() const {
58 GenericResponse& genericResponse,
60 const char* destination,
61 const EntityDescriptor* recipient=NULL,
62 const char* relayState=NULL,
63 const ArtifactGenerator* artifactGenerator=NULL,
64 const Credential* credential=NULL,
65 const XMLCh* signatureAlg=NULL,
66 const XMLCh* digestAlg=NULL
70 MessageEncoder* SAML_DLLLOCAL SAML2RedirectEncoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
72 return new SAML2RedirectEncoder();
77 long SAML2RedirectEncoder::encode(
78 GenericResponse& genericResponse,
80 const char* destination,
81 const EntityDescriptor* recipient,
82 const char* relayState,
83 const ArtifactGenerator* artifactGenerator,
84 const Credential* credential,
85 const XMLCh* signatureAlg,
86 const XMLCh* digestAlg
90 xmltooling::NDC ndc("encode");
92 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2Redirect");
94 log.debug("validating input");
95 HTTPResponse* httpResponse=dynamic_cast<HTTPResponse*>(&genericResponse);
97 throw BindingException("Unable to cast response interface to HTTPResponse type.");
98 if (xmlObject->getParent())
99 throw BindingException("Cannot encode XML content with parent.");
101 StatusResponseType* response = NULL;
102 RequestAbstractType* request = dynamic_cast<RequestAbstractType*>(xmlObject);
104 response = dynamic_cast<StatusResponseType*>(xmlObject);
106 throw BindingException("XML content for SAML 2.0 HTTP-Redirect Encoder must be a SAML 2.0 protocol message.");
109 // Check for XML signature.
110 if (request ? request->getSignature() : response->getSignature()) {
111 log.debug("message already signed, removing native signature due to size considerations");
112 request ? request->setSignature(NULL) : response->setSignature(NULL);
115 log.debug("marshalling, deflating, base64-encoding the message");
116 DOMElement* rootElement = xmlObject->marshall();
118 XMLHelper::serialize(rootElement, xmlbuf);
119 log.debug("marshalled message: %s", xmlbuf.c_str());
122 char* deflated = deflate(const_cast<char*>(xmlbuf.c_str()), xmlbuf.length(), &len);
124 throw BindingException("Failed to deflate message.");
126 XMLByte* encoded=Base64::encode(reinterpret_cast<XMLByte*>(deflated),len,&len);
129 throw BindingException("Base64 encoding of XML failed.");
131 // Create beginnings of redirect query string.
132 const URLEncoder* escaper = XMLToolingConfig::getConfig().getURLEncoder();
134 xmlbuf.append(reinterpret_cast<char*>(encoded),len);
135 xmlbuf = (request ? "SAMLRequest=" : "SAMLResponse=") + escaper->encode(xmlbuf.c_str());
136 if (relayState && *relayState)
137 xmlbuf = xmlbuf + "&RelayState=" + escaper->encode(relayState);
140 log.debug("signing the message");
142 // Sign the query string after adding the algorithm.
144 signatureAlg = DSIGConstants::s_unicodeStrURIRSA_SHA1;
145 auto_ptr_char alg(signatureAlg);
146 xmlbuf = xmlbuf + "&SigAlg=" + escaper->encode(alg.get());
149 memset(sigbuf,0,sizeof(sigbuf));
150 Signature::createRawSignature(credential->getPrivateKey(), signatureAlg, xmlbuf.c_str(), xmlbuf.length(), sigbuf, sizeof(sigbuf)-1);
151 xmlbuf = xmlbuf + "&Signature=" + escaper->encode(sigbuf);
154 // Generate redirect.
155 log.debug("message encoded, sending redirect to client");
156 xmlbuf.insert((string::size_type)0,(string::size_type)1,(strchr(destination,'?') ? '&' : '?'));
157 xmlbuf.insert(0,destination);
158 long ret = httpResponse->sendRedirect(xmlbuf.c_str());
160 // Cleanup by destroying XML.