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.
20 * SAML 2.0 ECP profile message encoder
24 #include "exceptions.h"
25 #include "binding/MessageEncoder.h"
26 #include "signature/ContentReference.h"
27 #include "saml1/core/Protocols.h"
28 #include "saml2/core/Protocols.h"
31 #include <xmltooling/logging.h>
32 #include <xmltooling/impl/AnyElement.h>
33 #include <xmltooling/io/HTTPResponse.h>
34 #include <xmltooling/util/NDC.h>
35 #include <xmltooling/soap/SOAP.h>
37 using namespace samlconstants;
38 using namespace opensaml::saml2p;
39 using namespace opensaml::saml2md;
40 using namespace opensaml;
41 using namespace xmlconstants;
42 using namespace xmlsignature;
43 using namespace soap11;
44 using namespace xmltooling::logging;
45 using namespace xmltooling;
51 static const XMLCh ProviderName[] = UNICODE_LITERAL_12(P,r,o,v,i,d,e,r,N,a,m,e);
53 class SAML_DLLLOCAL SAML2ECPEncoder : public MessageEncoder
56 SAML2ECPEncoder(const DOMElement* e, const XMLCh* ns) : m_actor("http://schemas.xmlsoap.org/soap/actor/next"),
57 m_providerName(e ? e->getAttributeNS(ns, ProviderName) : NULL), m_idpList(NULL) {
58 DOMElement* child = e ? XMLHelper::getFirstChildElement(e, SAML20P_NS, IDPList::LOCAL_NAME) : NULL;
60 m_idpList = dynamic_cast<IDPList*>(XMLObjectBuilder::buildOneFromElement(child));
62 virtual ~SAML2ECPEncoder() {
67 GenericResponse& genericResponse,
69 const char* destination,
70 const EntityDescriptor* recipient=NULL,
71 const char* relayState=NULL,
72 const ArtifactGenerator* artifactGenerator=NULL,
73 const Credential* credential=NULL,
74 const XMLCh* signatureAlg=NULL,
75 const XMLCh* digestAlg=NULL
79 auto_ptr_XMLCh m_actor;
80 const XMLCh* m_providerName;
82 AnyElementBuilder m_anyBuilder;
85 MessageEncoder* SAML_DLLLOCAL SAML2ECPEncoderFactory(const pair<const DOMElement*,const XMLCh*>& p)
87 return new SAML2ECPEncoder(p.first, p.second);
92 long SAML2ECPEncoder::encode(
93 GenericResponse& genericResponse,
95 const char* destination,
96 const EntityDescriptor* recipient,
97 const char* relayState,
98 const ArtifactGenerator* artifactGenerator,
99 const Credential* credential,
100 const XMLCh* signatureAlg,
101 const XMLCh* digestAlg
105 xmltooling::NDC ndc("encode");
107 Category& log = Category::getInstance(SAML_LOGCAT".MessageEncoder.SAML2ECP");
109 log.debug("validating input");
110 if (xmlObject->getParent())
111 throw BindingException("Cannot encode XML content with parent.");
113 Response* response = NULL;
114 AuthnRequest* request = dynamic_cast<AuthnRequest*>(xmlObject);
116 response = dynamic_cast<Response*>(xmlObject);
118 throw BindingException("XML content for SAML 2.0 ECP Encoder must be a SAML 2.0 AuthnRequest or Response.");
121 if (request && !request->getAssertionConsumerServiceURL())
122 throw BindingException("AuthnRequest must carry an AssertionConsumerServiceURL by value.");
123 else if (response && !response->getDestination())
124 throw BindingException("Response must carry a Destination attribute.");
126 // PAOS request leg is a custom MIME type, SOAP response leg is just text/xml.
127 genericResponse.setContentType(request ? "application/vnd.paos+xml" : "text/xml");
128 HTTPResponse* httpResponse = dynamic_cast<HTTPResponse*>(&genericResponse);
130 httpResponse->setResponseHeader("Cache-Control", "no-cache, no-store, must-revalidate, private");
131 httpResponse->setResponseHeader("Pragma", "no-cache");
134 // Wrap it in a SOAP envelope.
135 Envelope* env = EnvelopeBuilder::buildEnvelope();
136 Header* header = HeaderBuilder::buildHeader();
137 env->setHeader(header);
138 Body* body = BodyBuilder::buildBody();
140 body->getUnknownXMLObjects().push_back(xmlObject);
142 ElementProxy* hdrblock;
143 xmltooling::QName qMU(SOAP11ENV_NS, Header::MUSTUNDERSTAND_ATTRIB_NAME, SOAP11ENV_PREFIX);
144 xmltooling::QName qActor(SOAP11ENV_NS, Header::ACTOR_ATTRIB_NAME, SOAP11ENV_PREFIX);
147 // Create paos:Request header.
148 static const XMLCh service[] = UNICODE_LITERAL_7(s,e,r,v,i,c,e);
149 static const XMLCh responseConsumerURL[] = UNICODE_LITERAL_19(r,e,s,p,o,n,s,e,C,o,n,s,u,m,e,r,U,R,L);
150 hdrblock = dynamic_cast<ElementProxy*>(m_anyBuilder.buildObject(PAOS_NS, saml1p::Request::LOCAL_NAME, PAOS_PREFIX));
151 hdrblock->setAttribute(qMU, XML_ONE);
152 hdrblock->setAttribute(qActor, m_actor.get());
153 hdrblock->setAttribute(xmltooling::QName(NULL, service), SAML20ECP_NS);
154 hdrblock->setAttribute(xmltooling::QName(NULL, responseConsumerURL), request->getAssertionConsumerServiceURL());
155 header->getUnknownXMLObjects().push_back(hdrblock);
157 // Create ecp:Request header.
158 static const XMLCh IsPassive[] = UNICODE_LITERAL_9(I,s,P,a,s,s,i,v,e);
159 hdrblock = dynamic_cast<ElementProxy*>(m_anyBuilder.buildObject(SAML20ECP_NS, saml1p::Request::LOCAL_NAME, SAML20ECP_PREFIX));
160 hdrblock->setAttribute(qMU, XML_ONE);
161 hdrblock->setAttribute(qActor, m_actor.get());
162 if (!request->IsPassive())
163 hdrblock->setAttribute(xmltooling::QName(NULL,IsPassive), XML_ZERO);
165 hdrblock->setAttribute(xmltooling::QName(NULL,ProviderName), m_providerName);
166 hdrblock->getUnknownXMLObjects().push_back(request->getIssuer()->clone());
167 if (request->getScoping() && request->getScoping()->getIDPList())
168 hdrblock->getUnknownXMLObjects().push_back(request->getScoping()->getIDPList()->clone());
170 hdrblock->getUnknownXMLObjects().push_back(m_idpList->clone());
171 header->getUnknownXMLObjects().push_back(hdrblock);
174 // Create ecp:Response header.
175 hdrblock = dynamic_cast<ElementProxy*>(m_anyBuilder.buildObject(SAML20ECP_NS, Response::LOCAL_NAME, SAML20ECP_PREFIX));
176 hdrblock->setAttribute(qMU, XML_ONE);
177 hdrblock->setAttribute(qActor, m_actor.get());
178 hdrblock->setAttribute(xmltooling::QName(NULL,AuthnRequest::ASSERTIONCONSUMERSERVICEURL_ATTRIB_NAME), response->getDestination());
179 header->getUnknownXMLObjects().push_back(hdrblock);
183 // Create ecp:RelayState header.
184 static const XMLCh RelayState[] = UNICODE_LITERAL_10(R,e,l,a,y,S,t,a,t,e);
185 hdrblock = dynamic_cast<ElementProxy*>(m_anyBuilder.buildObject(SAML20ECP_NS, RelayState, SAML20ECP_PREFIX));
186 hdrblock->setAttribute(qMU, XML_ONE);
187 hdrblock->setAttribute(qActor, m_actor.get());
188 auto_ptr_XMLCh rs(relayState);
189 hdrblock->setTextContent(rs.get());
190 header->getUnknownXMLObjects().push_back(hdrblock);
194 DOMElement* rootElement = NULL;
196 if (request->getSignature()) {
197 log.debug("message already signed, skipping signature operation");
198 rootElement = env->marshall();
201 log.debug("signing the message and marshalling the envelope");
203 // Build a Signature.
204 Signature* sig = SignatureBuilder::buildSignature();
205 request->setSignature(sig);
207 sig->setSignatureAlgorithm(signatureAlg);
209 opensaml::ContentReference* cr = dynamic_cast<opensaml::ContentReference*>(sig->getContentReference());
211 cr->setDigestAlgorithm(digestAlg);
214 // Sign message while marshalling.
215 vector<Signature*> sigs(1,sig);
216 rootElement = env->marshall((DOMDocument*)NULL,&sigs,credential);
220 log.debug("marshalling the envelope");
221 rootElement = env->marshall();
227 if (log.isDebugEnabled())
228 log.debug("marshalled envelope:\n%s", s.str().c_str());
230 log.debug("sending serialized envelope");
231 long ret = genericResponse.sendResponse(s);
233 // Cleanup by destroying XML.
237 catch (XMLToolingException&) {
238 // A bit weird...we have to "revert" things so that the message is isolated
239 // so the caller can free it.
240 xmlObject->getParent()->detach();