2 * Copyright 2001-2006 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * XMLSecSignatureImpl.cpp
\r
20 * Signature class for XMLSec-based signature-handling
\r
23 #include "internal.h"
\r
24 #include "exceptions.h"
\r
25 #include "impl/UnknownElement.h"
\r
26 #include "signature/Signature.h"
\r
27 #include "util/NDC.h"
\r
28 #include "util/XMLConstants.h"
\r
29 #include "util/XMLHelper.h"
\r
31 #include <log4cpp/Category.hh>
\r
32 #include <xercesc/framework/MemBufInputSource.hpp>
\r
33 #include <xercesc/framework/Wrapper4InputSource.hpp>
\r
34 #include <xercesc/util/XMLUniDefs.hpp>
\r
35 #include <xsec/dsig/DSIGKeyInfoX509.hpp>
\r
36 #include <xsec/enc/XSECCryptoException.hpp>
\r
37 #include <xsec/framework/XSECException.hpp>
\r
39 using namespace xmlsignature;
\r
40 using namespace xmltooling;
\r
41 using namespace log4cpp;
\r
42 using namespace std;
\r
44 #if defined (_MSC_VER)
\r
45 #pragma warning( push )
\r
46 #pragma warning( disable : 4250 4251 )
\r
49 namespace xmlsignature {
\r
51 class XMLTOOL_DLLLOCAL XMLSecSignatureImpl : public UnknownElementImpl, public virtual Signature
\r
54 XMLSecSignatureImpl() : UnknownElementImpl(XMLConstants::XMLSIG_NS, Signature::LOCAL_NAME, XMLConstants::XMLSIG_PREFIX),
\r
55 m_signature(NULL), m_c14n(NULL), m_sm(NULL) {}
\r
56 virtual ~XMLSecSignatureImpl();
\r
59 XMLObject* clone() const;
\r
60 Signature* cloneSignature() const;
\r
62 DOMElement* marshall(DOMDocument* document=NULL, MarshallingContext* ctx=NULL) const;
\r
63 DOMElement* marshall(DOMElement* parentElement, MarshallingContext* ctx=NULL) const;
\r
64 XMLObject* unmarshall(DOMElement* element, bool bindDocument=false);
\r
67 const XMLCh* getCanonicalizationMethod() const { return m_c14n ? m_c14n : DSIGConstants::s_unicodeStrURIEXC_C14N_NOC; }
\r
68 const XMLCh* getSignatureAlgorithm() const { return m_sm ? m_sm : DSIGConstants::s_unicodeStrURIRSA_SHA1; }
\r
71 void setCanonicalizationMethod(const XMLCh* c14n) { m_c14n = prepareForAssignment(m_c14n,c14n); }
\r
72 void setSignatureAlgorithm(const XMLCh* sm) { m_sm = prepareForAssignment(m_sm,sm); }
\r
74 void sign(const SigningContext& ctx);
\r
75 void verify(const VerifyingContext& ctx) const;
\r
78 mutable DSIGSignature* m_signature;
\r
85 #if defined (_MSC_VER)
\r
86 #pragma warning( pop )
\r
89 XMLSecSignatureImpl::~XMLSecSignatureImpl()
\r
91 // Release the associated signature.
\r
93 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
\r
95 XMLString::release(&m_c14n);
\r
96 XMLString::release(&m_sm);
\r
99 void XMLSecSignatureImpl::releaseDOM()
\r
101 // This should save off the DOM
\r
102 UnknownElementImpl::releaseDOM();
\r
104 // Release the associated signature.
\r
106 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
\r
111 XMLObject* XMLSecSignatureImpl::clone() const
\r
113 return cloneSignature();
\r
116 Signature* XMLSecSignatureImpl::cloneSignature() const
\r
118 XMLSecSignatureImpl* ret=new XMLSecSignatureImpl();
\r
120 ret->m_c14n=XMLString::replicate(m_c14n);
\r
121 ret->m_sm=XMLString::replicate(m_sm);
\r
123 // If there's no XML locally, serialize this object into the new one, otherwise just copy it over.
\r
125 serialize(ret->m_xml);
\r
132 class _addcert : public std::binary_function<DSIGKeyInfoX509*,XSECCryptoX509*,void> {
\r
134 void operator()(DSIGKeyInfoX509* bag, XSECCryptoX509* cert) const {
\r
135 safeBuffer& buf=cert->getDEREncodingSB();
\r
136 bag->appendX509Certificate(buf.sbStrToXMLCh());
\r
140 void XMLSecSignatureImpl::sign(const SigningContext& ctx)
\r
142 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
\r
143 log.debug("applying signature");
\r
146 throw SignatureException("Only a marshalled Signature object can be signed.");
\r
149 log.debug("creating signature content");
\r
150 ctx.createSignature(m_signature);
\r
151 const std::vector<XSECCryptoX509*>* certs=ctx.getX509Certificates();
\r
152 if (certs && !certs->empty()) {
\r
153 DSIGKeyInfoX509* x509Data=m_signature->appendX509Data();
\r
154 for_each(certs->begin(),certs->end(),bind1st(_addcert(),x509Data));
\r
157 auto_ptr<KeyInfo> keyInfo(ctx.getKeyInfo());
\r
158 if (keyInfo.get()) {
\r
159 DOMElement* domElement=keyInfo->marshall(m_signature->getParentDocument());
\r
160 getDOM()->appendChild(domElement);
\r
164 log.debug("computing signature");
\r
165 m_signature->setSigningKey(ctx.getSigningKey());
\r
166 m_signature->sign();
\r
168 catch(XSECException& e) {
\r
169 auto_ptr_char temp(e.getMsg());
\r
170 throw SignatureException(string("Caught an XMLSecurity exception while signing: ") + temp.get());
\r
172 catch(XSECCryptoException& e) {
\r
173 throw SignatureException(string("Caught an XMLSecurity exception while signing: ") + e.getMsg());
\r
177 void XMLSecSignatureImpl::verify(const VerifyingContext& ctx) const
\r
179 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
\r
180 log.debug("verifying signature");
\r
183 throw SignatureException("Only a marshalled Signature object can be verified.");
\r
186 ctx.verifySignature(m_signature);
\r
188 catch(XSECException& e) {
\r
189 auto_ptr_char temp(e.getMsg());
\r
190 throw SignatureException(string("Caught an XMLSecurity exception verifying signature: ") + temp.get());
\r
192 catch(XSECCryptoException& e) {
\r
193 throw SignatureException(string("Caught an XMLSecurity exception verifying signature: ") + e.getMsg());
\r
197 DOMElement* XMLSecSignatureImpl::marshall(DOMDocument* document, MarshallingContext* ctx) const
\r
200 xmltooling::NDC ndc("marshall");
\r
203 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
\r
204 log.debug("marshalling ds:Signature");
\r
206 DOMElement* cachedDOM=getDOM();
\r
208 if (!document || document==cachedDOM->getOwnerDocument()) {
\r
209 log.debug("Signature has a usable cached DOM, reusing it");
\r
211 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
\r
212 releaseParentDOM(true);
\r
216 // We have a DOM but it doesn't match the document we were given, so we import
\r
217 // it into the new document.
\r
218 cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
\r
221 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
\r
223 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
224 document, cachedDOM
\r
226 m_signature->load();
\r
228 catch(XSECException& e) {
\r
229 auto_ptr_char temp(e.getMsg());
\r
230 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
232 catch(XSECCryptoException& e) {
\r
233 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
236 // Recache the DOM.
\r
237 setDocumentElement(document, cachedDOM);
\r
238 log.debug("caching imported DOM for Signature");
\r
239 setDOM(cachedDOM, false);
\r
240 releaseParentDOM(true);
\r
244 // If we get here, we didn't have a usable DOM.
\r
245 bool bindDocument=false;
\r
246 if (m_xml.empty()) {
\r
247 // Fresh signature, so we just create an empty one.
\r
248 log.debug("creating empty Signature element");
\r
250 document=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
\r
253 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignature();
\r
254 m_signature->setDSIGNSPrefix(XMLConstants::XMLSIG_PREFIX);
\r
255 cachedDOM=m_signature->createBlankSignature(document, getCanonicalizationMethod(), getSignatureAlgorithm());
\r
258 // We need to reparse the XML we saved off into a new DOM.
\r
259 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl");
\r
260 Wrapper4InputSource dsrc(&src,false);
\r
261 log.debug("parsing Signature XML back into DOM tree");
\r
262 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
\r
264 // The caller insists on using his own document, so we now have to import the thing
\r
265 // into it. Then we're just dumping the one we built.
\r
266 log.debug("reimporting new DOM into caller-supplied document");
\r
267 cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
\r
268 internalDoc->release();
\r
271 // We just bind the document we built to the object as the result.
\r
272 cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
\r
273 document=internalDoc;
\r
277 // Now reload the signature from the DOM.
\r
279 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
280 document, cachedDOM
\r
282 m_signature->load();
\r
284 catch(XSECException& e) {
\r
286 document->release();
\r
287 auto_ptr_char temp(e.getMsg());
\r
288 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
290 catch(XSECCryptoException& e) {
\r
292 document->release();
\r
293 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
297 // Recache the DOM and clear the serialized copy.
\r
298 setDocumentElement(document, cachedDOM);
\r
299 log.debug("caching DOM for Signature (document is %sbound)", bindDocument ? "" : "not ");
\r
300 setDOM(cachedDOM, bindDocument);
\r
301 releaseParentDOM(true);
\r
306 DOMElement* XMLSecSignatureImpl::marshall(DOMElement* parentElement, MarshallingContext* ctx) const
\r
309 xmltooling::NDC ndc("marshall");
\r
312 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
\r
313 log.debug("marshalling ds:Signature");
\r
315 DOMElement* cachedDOM=getDOM();
\r
317 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
\r
318 log.debug("Signature has a usable cached DOM, reusing it");
\r
319 parentElement->appendChild(cachedDOM);
\r
320 releaseParentDOM(true);
\r
324 // We have a DOM but it doesn't match the document we were given, so we import
\r
325 // it into the new document.
\r
326 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
\r
329 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
\r
331 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
332 parentElement->getOwnerDocument(), cachedDOM
\r
334 m_signature->load();
\r
336 catch(XSECException& e) {
\r
337 auto_ptr_char temp(e.getMsg());
\r
338 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
340 catch(XSECCryptoException& e) {
\r
341 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
344 // Recache the DOM.
\r
345 parentElement->appendChild(cachedDOM);
\r
346 log.debug("caching imported DOM for Signature");
\r
347 setDOM(cachedDOM, false);
\r
348 releaseParentDOM(true);
\r
352 // If we get here, we didn't have a usable DOM.
\r
353 if (m_xml.empty()) {
\r
354 // Fresh signature, so we just create an empty one.
\r
355 log.debug("creating empty Signature element");
\r
356 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignature();
\r
357 m_signature->setDSIGNSPrefix(XMLConstants::XMLSIG_PREFIX);
\r
358 cachedDOM=m_signature->createBlankSignature(
\r
359 parentElement->getOwnerDocument(), getCanonicalizationMethod(), getSignatureAlgorithm()
\r
363 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl");
\r
364 Wrapper4InputSource dsrc(&src,false);
\r
365 log.debug("parsing XML back into DOM tree");
\r
366 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
\r
368 log.debug("reimporting new DOM into caller-supplied document");
\r
369 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(),true));
\r
370 internalDoc->release();
\r
372 // Now reload the signature from the DOM.
\r
374 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
375 parentElement->getOwnerDocument(), cachedDOM
\r
377 m_signature->load();
\r
379 catch(XSECException& e) {
\r
380 auto_ptr_char temp(e.getMsg());
\r
381 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
383 catch(XSECCryptoException& e) {
\r
384 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
388 // Recache the DOM and clear the serialized copy.
\r
389 parentElement->appendChild(cachedDOM);
\r
390 log.debug("caching DOM for Signature");
\r
391 setDOM(cachedDOM, false);
\r
392 releaseParentDOM(true);
\r
397 XMLObject* XMLSecSignatureImpl::unmarshall(DOMElement* element, bool bindDocument)
\r
399 Category::getInstance(XMLTOOLING_LOGCAT".Signature").debug("unmarshalling ds:Signature");
\r
402 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
403 element->getOwnerDocument(), element
\r
405 m_signature->load();
\r
407 catch(XSECException& e) {
\r
408 auto_ptr_char temp(e.getMsg());
\r
409 throw UnmarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
411 catch(XSECCryptoException& e) {
\r
412 throw UnmarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
415 setDOM(element, bindDocument);
\r
419 Signature* SignatureBuilder::buildObject(
\r
420 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType
\r
423 if (!XMLString::equals(nsURI,XMLConstants::XMLSIG_NS) || !XMLString::equals(localName,Signature::LOCAL_NAME))
\r
424 throw XMLObjectException("XMLSecSignatureBuilder requires standard Signature element name.");
\r
425 return buildObject();
\r
428 Signature* SignatureBuilder::buildObject() const
\r
430 return new XMLSecSignatureImpl();
\r
433 const XMLCh Signature::LOCAL_NAME[] = UNICODE_LITERAL_9(S,i,g,n,a,t,u,r,e);
\r