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
30 #include "validation/ValidatingXMLObject.h"
\r
32 #include <log4cpp/Category.hh>
\r
33 #include <xercesc/framework/MemBufInputSource.hpp>
\r
34 #include <xercesc/framework/Wrapper4InputSource.hpp>
\r
35 #include <xercesc/util/XMLUniDefs.hpp>
\r
36 #include <xsec/dsig/DSIGKeyInfoX509.hpp>
\r
37 #include <xsec/enc/XSECCryptoException.hpp>
\r
38 #include <xsec/framework/XSECException.hpp>
\r
40 using namespace xmlsignature;
\r
41 using namespace xmltooling;
\r
42 using namespace log4cpp;
\r
43 using namespace std;
\r
45 #if defined (_MSC_VER)
\r
46 #pragma warning( push )
\r
47 #pragma warning( disable : 4250 4251 )
\r
50 namespace xmlsignature {
\r
52 class XMLTOOL_DLLLOCAL XMLSecSignatureImpl
\r
53 : public UnknownElementImpl, public virtual Signature, public virtual ValidatingXMLObject
\r
56 XMLSecSignatureImpl() : UnknownElementImpl(XMLConstants::XMLSIG_NS, Signature::LOCAL_NAME, XMLConstants::XMLSIG_PREFIX),
\r
57 m_signature(NULL), m_c14n(NULL), m_sm(NULL), m_key(NULL), m_keyInfo(NULL), m_reference(NULL) {}
\r
58 virtual ~XMLSecSignatureImpl();
\r
61 void releaseChildrenDOM(bool propagateRelease=true) {
\r
63 m_keyInfo->releaseDOM();
\r
64 if (propagateRelease)
\r
65 m_keyInfo->releaseChildrenDOM();
\r
68 XMLObject* clone() const;
\r
69 Signature* cloneSignature() const;
\r
71 DOMElement* marshall(DOMDocument* document=NULL, const vector<Signature*>* sigs=NULL) const;
\r
72 DOMElement* marshall(DOMElement* parentElement, const vector<Signature*>* sigs=NULL) const;
\r
73 XMLObject* unmarshall(DOMElement* element, bool bindDocument=false);
\r
76 const XMLCh* getCanonicalizationMethod() const { return m_c14n ? m_c14n : DSIGConstants::s_unicodeStrURIEXC_C14N_NOC; }
\r
77 const XMLCh* getSignatureAlgorithm() const { return m_sm ? m_sm : DSIGConstants::s_unicodeStrURIRSA_SHA1; }
\r
78 KeyInfo* getKeyInfo() const { return m_keyInfo; }
\r
79 ContentReference* getContentReference() const { return m_reference; }
\r
80 DSIGSignature* getXMLSignature() const { return m_signature; }
\r
83 void setCanonicalizationMethod(const XMLCh* c14n) { m_c14n = prepareForAssignment(m_c14n,c14n); }
\r
84 void setSignatureAlgorithm(const XMLCh* sm) { m_sm = prepareForAssignment(m_sm,sm); }
\r
85 void setSigningKey(XSECCryptoKey* signingKey) {
\r
89 releaseThisandParentDOM();
\r
91 void setKeyInfo(KeyInfo* keyInfo) {
\r
92 prepareForAssignment(m_keyInfo, keyInfo);
\r
95 void setContentReference(ContentReference* reference) {
\r
97 m_reference=reference;
\r
98 releaseThisandParentDOM();
\r
103 void registerValidator(Validator* validator);
\r
104 void deregisterValidator(Validator* validator);
\r
105 void deregisterAll();
\r
106 void validate(bool validateDescendants) const;
\r
109 mutable DSIGSignature* m_signature;
\r
112 XSECCryptoKey* m_key;
\r
113 KeyInfo* m_keyInfo;
\r
114 ContentReference* m_reference;
\r
115 vector<Validator*> m_validators;
\r
120 #if defined (_MSC_VER)
\r
121 #pragma warning( pop )
\r
124 XMLSecSignatureImpl::~XMLSecSignatureImpl()
\r
126 // Release the associated signature.
\r
128 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
\r
130 XMLString::release(&m_c14n);
\r
131 XMLString::release(&m_sm);
\r
134 delete m_reference;
\r
135 for_each(m_validators.begin(),m_validators.end(),cleanup<Validator>());
\r
138 void XMLSecSignatureImpl::releaseDOM()
\r
140 // This should save off the DOM
\r
141 UnknownElementImpl::releaseDOM();
\r
143 // Release the associated signature.
\r
145 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
\r
150 XMLObject* XMLSecSignatureImpl::clone() const
\r
152 return cloneSignature();
\r
155 Signature* XMLSecSignatureImpl::cloneSignature() const
\r
157 XMLSecSignatureImpl* ret=new XMLSecSignatureImpl();
\r
159 ret->m_c14n=XMLString::replicate(m_c14n);
\r
160 ret->m_sm=XMLString::replicate(m_sm);
\r
162 ret->m_key=m_key->clone();
\r
164 ret->m_keyInfo=m_keyInfo->cloneKeyInfo();
\r
166 ret->m_reference=m_reference->clone();
\r
168 xmltooling::clone(m_validators,ret->m_validators);
\r
170 // If there's no XML locally, serialize this object into the new one, otherwise just copy it over.
\r
172 serialize(ret->m_xml);
\r
179 void XMLSecSignatureImpl::sign()
\r
181 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
\r
182 log.debug("applying signature");
\r
185 throw SignatureException("Only a marshalled Signature object can be signed.");
\r
187 throw SignatureException("No signing key available for signature creation.");
\r
188 else if (!m_reference)
\r
189 throw SignatureException("No ContentReference object set for signature creation.");
\r
192 log.debug("creating signature reference(s)");
\r
193 m_reference->createReferences(m_signature);
\r
195 m_keyInfo->marshall(getDOM());
\r
198 log.debug("computing signature");
\r
199 m_signature->setSigningKey(m_key->clone());
\r
200 m_signature->sign();
\r
202 catch(XSECException& e) {
\r
203 auto_ptr_char temp(e.getMsg());
\r
204 throw SignatureException(string("Caught an XMLSecurity exception while signing: ") + temp.get());
\r
206 catch(XSECCryptoException& e) {
\r
207 throw SignatureException(string("Caught an XMLSecurity exception while signing: ") + e.getMsg());
\r
211 DOMElement* XMLSecSignatureImpl::marshall(DOMDocument* document, const vector<Signature*>* sigs) const
\r
214 xmltooling::NDC ndc("marshall");
\r
217 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
\r
218 log.debug("marshalling ds:Signature");
\r
220 DOMElement* cachedDOM=getDOM();
\r
222 if (!document || document==cachedDOM->getOwnerDocument()) {
\r
223 log.debug("Signature has a usable cached DOM, reusing it");
\r
225 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
\r
226 releaseParentDOM(true);
\r
230 // We have a DOM but it doesn't match the document we were given, so we import
\r
231 // it into the new document.
\r
232 cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
\r
235 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
\r
237 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
238 document, cachedDOM
\r
240 m_signature->load();
\r
242 catch(XSECException& e) {
\r
243 auto_ptr_char temp(e.getMsg());
\r
244 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
246 catch(XSECCryptoException& e) {
\r
247 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
250 // Recache the DOM.
\r
251 setDocumentElement(document, cachedDOM);
\r
252 log.debug("caching imported DOM for Signature");
\r
253 setDOM(cachedDOM, false);
\r
254 releaseParentDOM(true);
\r
258 // If we get here, we didn't have a usable DOM.
\r
259 bool bindDocument=false;
\r
260 if (m_xml.empty()) {
\r
261 // Fresh signature, so we just create an empty one.
\r
262 log.debug("creating empty Signature element");
\r
264 document=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
\r
267 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignature();
\r
268 m_signature->setDSIGNSPrefix(XMLConstants::XMLSIG_PREFIX);
\r
269 cachedDOM=m_signature->createBlankSignature(document, getCanonicalizationMethod(), getSignatureAlgorithm());
\r
272 // We need to reparse the XML we saved off into a new DOM.
\r
273 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl");
\r
274 Wrapper4InputSource dsrc(&src,false);
\r
275 log.debug("parsing Signature XML back into DOM tree");
\r
276 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
\r
278 // The caller insists on using his own document, so we now have to import the thing
\r
279 // into it. Then we're just dumping the one we built.
\r
280 log.debug("reimporting new DOM into caller-supplied document");
\r
281 cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
\r
282 internalDoc->release();
\r
285 // We just bind the document we built to the object as the result.
\r
286 cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
\r
287 document=internalDoc;
\r
291 // Now reload the signature from the DOM.
\r
293 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
294 document, cachedDOM
\r
296 m_signature->load();
\r
298 catch(XSECException& e) {
\r
300 document->release();
\r
301 auto_ptr_char temp(e.getMsg());
\r
302 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
304 catch(XSECCryptoException& e) {
\r
306 document->release();
\r
307 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
311 // Recache the DOM and clear the serialized copy.
\r
312 setDocumentElement(document, cachedDOM);
\r
313 log.debug("caching DOM for Signature (document is %sbound)", bindDocument ? "" : "not ");
\r
314 setDOM(cachedDOM, bindDocument);
\r
315 releaseParentDOM(true);
\r
320 DOMElement* XMLSecSignatureImpl::marshall(DOMElement* parentElement, const vector<Signature*>* sigs) const
\r
323 xmltooling::NDC ndc("marshall");
\r
326 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
\r
327 log.debug("marshalling ds:Signature");
\r
329 DOMElement* cachedDOM=getDOM();
\r
331 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
\r
332 log.debug("Signature has a usable cached DOM, reusing it");
\r
333 parentElement->appendChild(cachedDOM);
\r
334 releaseParentDOM(true);
\r
338 // We have a DOM but it doesn't match the document we were given, so we import
\r
339 // it into the new document.
\r
340 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
\r
343 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
\r
345 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
346 parentElement->getOwnerDocument(), cachedDOM
\r
348 m_signature->load();
\r
350 catch(XSECException& e) {
\r
351 auto_ptr_char temp(e.getMsg());
\r
352 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
354 catch(XSECCryptoException& e) {
\r
355 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
358 // Recache the DOM.
\r
359 parentElement->appendChild(cachedDOM);
\r
360 log.debug("caching imported DOM for Signature");
\r
361 setDOM(cachedDOM, false);
\r
362 releaseParentDOM(true);
\r
366 // If we get here, we didn't have a usable DOM.
\r
367 if (m_xml.empty()) {
\r
368 // Fresh signature, so we just create an empty one.
\r
369 log.debug("creating empty Signature element");
\r
370 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignature();
\r
371 m_signature->setDSIGNSPrefix(XMLConstants::XMLSIG_PREFIX);
\r
372 cachedDOM=m_signature->createBlankSignature(
\r
373 parentElement->getOwnerDocument(), getCanonicalizationMethod(), getSignatureAlgorithm()
\r
377 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl");
\r
378 Wrapper4InputSource dsrc(&src,false);
\r
379 log.debug("parsing XML back into DOM tree");
\r
380 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
\r
382 log.debug("reimporting new DOM into caller-supplied document");
\r
383 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(),true));
\r
384 internalDoc->release();
\r
386 // Now reload the signature from the DOM.
\r
388 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
389 parentElement->getOwnerDocument(), cachedDOM
\r
391 m_signature->load();
\r
393 catch(XSECException& e) {
\r
394 auto_ptr_char temp(e.getMsg());
\r
395 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
397 catch(XSECCryptoException& e) {
\r
398 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
402 // Recache the DOM and clear the serialized copy.
\r
403 parentElement->appendChild(cachedDOM);
\r
404 log.debug("caching DOM for Signature");
\r
405 setDOM(cachedDOM, false);
\r
406 releaseParentDOM(true);
\r
411 XMLObject* XMLSecSignatureImpl::unmarshall(DOMElement* element, bool bindDocument)
\r
413 Category::getInstance(XMLTOOLING_LOGCAT".Signature").debug("unmarshalling ds:Signature");
\r
416 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
417 element->getOwnerDocument(), element
\r
419 m_signature->load();
\r
421 catch(XSECException& e) {
\r
422 auto_ptr_char temp(e.getMsg());
\r
423 throw UnmarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
425 catch(XSECCryptoException& e) {
\r
426 throw UnmarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
429 setDOM(element, bindDocument);
\r
433 void XMLSecSignatureImpl::registerValidator(Validator* validator)
\r
435 m_validators.push_back(validator);
\r
438 void XMLSecSignatureImpl::deregisterValidator(Validator* validator)
\r
440 for (vector<Validator*>::iterator i=m_validators.begin(); i!=m_validators.end(); i++) {
\r
441 if ((*i)==validator) {
\r
442 m_validators.erase(i);
\r
448 void XMLSecSignatureImpl::deregisterAll()
\r
450 for_each(m_validators.begin(),m_validators.end(),cleanup<Validator>());
\r
451 m_validators.clear();
\r
454 class _validate : public binary_function<const XMLObject*,bool,void> {
\r
456 void operator()(const XMLObject* obj, bool propagate) const {
\r
457 const ValidatingXMLObject* val = dynamic_cast<const ValidatingXMLObject*>(obj);
\r
459 val->validate(propagate);
\r
464 void XMLSecSignatureImpl::validate(bool validateDescendants) const
\r
467 m_validators.begin(),m_validators.end(),
\r
468 bind2nd(mem_fun<void,Validator,const XMLObject*>(&Validator::validate),this)
\r
471 if (validateDescendants && m_keyInfo) {
\r
472 m_keyInfo->validate(validateDescendants);
\r
476 Signature* SignatureBuilder::buildObject(
\r
477 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType
\r
480 if (!XMLString::equals(nsURI,XMLConstants::XMLSIG_NS) || !XMLString::equals(localName,Signature::LOCAL_NAME))
\r
481 throw XMLObjectException("XMLSecSignatureBuilder requires standard Signature element name.");
\r
482 return buildObject();
\r
485 Signature* SignatureBuilder::buildObject() const
\r
487 return new XMLSecSignatureImpl();
\r
490 const XMLCh Signature::LOCAL_NAME[] = UNICODE_LITERAL_9(S,i,g,n,a,t,u,r,e);
\r