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 * SAML2SOAPEncoder.cpp
20 * SAML 2.0 SOAP binding message encoder.
24 #include "exceptions.h"
25 #include "binding/MessageEncoder.h"
26 #include "signature/ContentReference.h"
27 #include "saml2/core/Protocols.h"
30 #include <xmltooling/logging.h>
31 #include <xmltooling/io/HTTPResponse.h>
32 #include <xmltooling/util/NDC.h>
33 #include <xmltooling/signature/Signature.h>
34 #include <xmltooling/soap/SOAP.h>
36 using namespace opensaml::saml2p;
37 using namespace opensaml::saml2md;
38 using namespace opensaml;
39 using namespace xmlsignature;
40 using namespace soap11;
41 using namespace xmltooling::logging;
42 using namespace xmltooling;
47 class SAML_DLLLOCAL SAML2SOAPEncoder : public MessageEncoder
51 virtual ~SAML2SOAPEncoder() {}
53 bool isUserAgentPresent() const {
57 const XMLCh* getProtocolFamily() const {
58 return samlconstants::SAML20P_NS;
62 GenericResponse& genericResponse,
64 const char* destination,
65 const EntityDescriptor* recipient=nullptr,
66 const char* relayState=nullptr,
67 const ArtifactGenerator* artifactGenerator=nullptr,
68 const Credential* credential=nullptr,
69 const XMLCh* signatureAlg=nullptr,
70 const XMLCh* digestAlg=nullptr
74 MessageEncoder* SAML_DLLLOCAL SAML2SOAPEncoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
76 return new SAML2SOAPEncoder();
81 long SAML2SOAPEncoder::encode(
82 GenericResponse& genericResponse,
84 const char* destination,
85 const EntityDescriptor* recipient,
86 const char* relayState,
87 const ArtifactGenerator* artifactGenerator,
88 const Credential* credential,
89 const XMLCh* signatureAlg,
90 const XMLCh* digestAlg
94 xmltooling::NDC ndc("encode");
96 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2SOAP");
98 log.debug("validating input");
99 if (xmlObject->getParent())
100 throw BindingException("Cannot encode XML content with parent.");
102 genericResponse.setContentType("text/xml");
103 HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
105 httpResponse->setResponseHeader("Expires", "01-Jan-1997 12:00:00 GMT");
106 httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
107 httpResponse->setResponseHeader("Pragma", "no-cache");
110 bool detachOnFailure = false;
111 DOMElement* rootElement = nullptr;
113 // Check for a naked message.
114 SignableObject* msg = dynamic_cast<SignableObject*>(xmlObject);
116 // Wrap it in a SOAP envelope and point xmlObject at that.
117 detachOnFailure = true;
118 Envelope* env = EnvelopeBuilder::buildEnvelope();
119 Body* body = BodyBuilder::buildBody();
121 body->getUnknownXMLObjects().push_back(msg);
125 Envelope* env = dynamic_cast<Envelope*>(xmlObject);
128 msg = (env->getBody() && env->getBody()->hasChildren()) ?
129 dynamic_cast<SignableObject*>(env->getBody()->getUnknownXMLObjects().front()) : nullptr;
132 if (msg && credential) {
133 if (msg->getSignature()) {
134 log.debug("message already signed, skipping signature operation");
135 rootElement = env->marshall();
138 log.debug("signing the message and marshalling the envelope");
140 // Build a Signature.
141 Signature* sig = SignatureBuilder::buildSignature();
142 msg->setSignature(sig);
144 sig->setSignatureAlgorithm(signatureAlg);
146 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
148 cr->setDigestAlgorithm(digestAlg);
151 // Sign message while marshalling.
152 vector<Signature*> sigs(1,sig);
153 rootElement = env->marshall((DOMDocument*)nullptr,&sigs,credential);
157 log.debug("marshalling the envelope");
158 rootElement = env->marshall();
164 if (log.isDebugEnabled())
165 log.debug("marshalled envelope:\n%s", s.str().c_str());
167 log.debug("sending serialized envelope");
168 bool error = (!msg && env->getBody() && env->getBody()->hasChildren() &&
169 dynamic_cast<Fault*>(env->getBody()->getUnknownXMLObjects().front()));
170 long ret = error ? genericResponse.sendError(s) : genericResponse.sendResponse(s);
172 // Cleanup by destroying XML.
176 catch (XMLToolingException&) {
177 if (msg && detachOnFailure) {
178 // A bit weird...we have to "revert" things so that the message is isolated
179 // so the caller can free it.
180 if (msg->getParent()) {
181 msg->getParent()->detach();
189 Fault* fault = dynamic_cast<Fault*>(xmlObject);
192 log.debug("building envelope and marshalling fault");
193 Envelope* env = EnvelopeBuilder::buildEnvelope();
194 Body* body = BodyBuilder::buildBody();
196 body->getUnknownXMLObjects().push_back(fault);
197 rootElement = env->marshall();
202 if (log.isDebugEnabled())
203 log.debug("marshalled envelope:\n%s", s.str().c_str());
205 log.debug("sending serialized envelope");
206 long ret = genericResponse.sendError(s);
208 // Cleanup by destroying XML.
212 catch (XMLToolingException&) {
213 // A bit weird...we have to "revert" things so that the fault is isolated
214 // so the caller can free it.
215 if (fault->getParent()) {
216 fault->getParent()->detach();
223 throw BindingException("XML content for SAML 2.0 SOAP Encoder must be a SAML 2.0 message or SOAP Fault/Envelope.");