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 * XMLSecSignatureImpl.cpp
20 * Signature class for XMLSec-based signature-handling
24 #include "exceptions.h"
25 #include "impl/UnknownElement.h"
26 #include "security/Credential.h"
27 #include "signature/KeyInfo.h"
28 #include "signature/Signature.h"
30 #include "util/XMLConstants.h"
31 #include "util/XMLHelper.h"
33 #include <log4cpp/Category.hh>
34 #include <xercesc/framework/MemBufInputSource.hpp>
35 #include <xercesc/framework/Wrapper4InputSource.hpp>
36 #include <xercesc/util/XMLUniDefs.hpp>
37 #include <xsec/dsig/DSIGKeyInfoX509.hpp>
38 #include <xsec/dsig/DSIGReference.hpp>
39 #include <xsec/enc/XSECCryptoException.hpp>
40 #include <xsec/framework/XSECAlgorithmHandler.hpp>
41 #include <xsec/framework/XSECAlgorithmMapper.hpp>
42 #include <xsec/framework/XSECException.hpp>
43 #include <xsec/transformers/TXFMSB.hpp>
44 #include <xsec/transformers/TXFMChain.hpp>
45 #include <xsec/transformers/TXFMOutputFile.hpp>
47 using namespace xmlsignature;
48 using namespace xmltooling;
49 using namespace log4cpp;
51 using xmlconstants::XMLSIG_NS;
52 using xmlconstants::XMLSIG_PREFIX;
54 #if defined (_MSC_VER)
55 #pragma warning( push )
56 #pragma warning( disable : 4250 4251 )
59 namespace xmlsignature {
61 class XMLTOOL_DLLLOCAL XMLSecSignatureImpl : public UnknownElementImpl, public virtual Signature
64 XMLSecSignatureImpl() : UnknownElementImpl(XMLSIG_NS, Signature::LOCAL_NAME, XMLSIG_PREFIX),
65 m_signature(NULL), m_c14n(NULL), m_sm(NULL), m_key(NULL), m_keyInfo(NULL), m_reference(NULL) {}
66 virtual ~XMLSecSignatureImpl();
68 void releaseDOM() const;
69 void releaseChildrenDOM(bool propagateRelease=true) const {
71 m_keyInfo->releaseDOM();
73 m_keyInfo->releaseChildrenDOM();
76 XMLObject* clone() const;
77 Signature* cloneSignature() const;
79 DOMElement* marshall(DOMDocument* document=NULL, const vector<Signature*>* sigs=NULL, const Credential* credential=NULL) const;
80 DOMElement* marshall(DOMElement* parentElement, const vector<Signature*>* sigs=NULL, const Credential* credential=NULL) const;
81 XMLObject* unmarshall(DOMElement* element, bool bindDocument=false);
84 const XMLCh* getCanonicalizationMethod() const { return m_c14n ? m_c14n : DSIGConstants::s_unicodeStrURIEXC_C14N_NOC; }
85 const XMLCh* getSignatureAlgorithm() const { return m_sm ? m_sm : DSIGConstants::s_unicodeStrURIRSA_SHA1; }
86 KeyInfo* getKeyInfo() const { return m_keyInfo; }
87 ContentReference* getContentReference() const { return m_reference; }
88 DSIGSignature* getXMLSignature() const { return m_signature; }
91 void setCanonicalizationMethod(const XMLCh* c14n) { m_c14n = prepareForAssignment(m_c14n,c14n); }
92 void setSignatureAlgorithm(const XMLCh* sm) { m_sm = prepareForAssignment(m_sm,sm); }
93 void setSigningKey(XSECCryptoKey* signingKey) {
97 void setKeyInfo(KeyInfo* keyInfo) {
98 prepareForAssignment(m_keyInfo, keyInfo);
101 void setContentReference(ContentReference* reference) {
103 m_reference=reference;
106 void sign(const Credential* credential=NULL);
109 mutable DSIGSignature* m_signature;
112 XSECCryptoKey* m_key;
113 mutable KeyInfo* m_keyInfo;
114 ContentReference* m_reference;
119 #if defined (_MSC_VER)
120 #pragma warning( pop )
123 XMLSecSignatureImpl::~XMLSecSignatureImpl()
125 // Release the associated signature.
127 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
129 XMLString::release(&m_c14n);
130 XMLString::release(&m_sm);
136 void XMLSecSignatureImpl::releaseDOM() const
139 // This should save off the DOM
140 UnknownElementImpl::releaseDOM();
142 // Release the associated signature.
144 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
150 XMLObject* XMLSecSignatureImpl::clone() const
152 return cloneSignature();
155 Signature* XMLSecSignatureImpl::cloneSignature() const
157 XMLSecSignatureImpl* ret=new XMLSecSignatureImpl();
159 ret->m_c14n=XMLString::replicate(m_c14n);
160 ret->m_sm=XMLString::replicate(m_sm);
162 ret->m_key=m_key->clone();
164 ret->m_keyInfo=m_keyInfo->cloneKeyInfo();
166 // If there's no XML locally, serialize this object into the new one, otherwise just copy it over.
168 serialize(ret->m_xml);
175 void XMLSecSignatureImpl::sign(const Credential* credential)
177 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
178 log.debug("applying signature");
181 throw SignatureException("Only a marshalled Signature object can be signed.");
182 else if (!m_reference)
183 throw SignatureException("No ContentReference object set for signature creation.");
185 XSECCryptoKey* key = credential ? credential->getPrivateKey() : m_key;
187 throw SignatureException("No signing key available for signature creation.");
190 log.debug("creating signature reference(s)");
191 DSIGReferenceList* refs = m_signature->getReferenceList();
192 while (refs && refs->getSize())
193 delete refs->removeReference(0);
194 m_reference->createReferences(m_signature);
196 log.debug("computing signature");
197 m_signature->setSigningKey(key->clone());
200 catch(XSECException& e) {
201 auto_ptr_char temp(e.getMsg());
202 throw SignatureException(string("Caught an XMLSecurity exception while signing: ") + temp.get());
204 catch(XSECCryptoException& e) {
205 throw SignatureException(string("Caught an XMLSecurity exception while signing: ") + e.getMsg());
209 DOMElement* XMLSecSignatureImpl::marshall(DOMDocument* document, const vector<Signature*>* sigs, const Credential* credential) const
212 xmltooling::NDC ndc("marshall");
215 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject.Signature");
216 log.debug("marshalling ds:Signature");
218 DOMElement* cachedDOM=getDOM();
220 if (!document || document==cachedDOM->getOwnerDocument()) {
221 log.debug("Signature has a usable cached DOM, reusing it");
223 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
224 releaseParentDOM(true);
228 // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
229 // Without an adoptNode option to maintain the child pointers, we have to either import the
230 // DOM while somehow reassigning all the nested references (which amounts to a complete
231 // *unmarshall* operation), or we just release the existing DOM and hope that we can get
232 // it back. This depends on all objects being able to preserve their DOM at all costs.
233 releaseChildrenDOM(true);
237 // If we get here, we didn't have a usable DOM.
238 bool bindDocument=false;
240 // Fresh signature, so we just create an empty one.
241 log.debug("creating empty Signature element");
243 document=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
246 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignature();
247 m_signature->setDSIGNSPrefix(XMLSIG_PREFIX);
248 cachedDOM=m_signature->createBlankSignature(document, getCanonicalizationMethod(), getSignatureAlgorithm());
251 // We need to reparse the XML we saved off into a new DOM.
252 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl");
253 Wrapper4InputSource dsrc(&src,false);
254 log.debug("parsing Signature XML back into DOM tree");
255 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
257 // The caller insists on using his own document, so we now have to import the thing
258 // into it. Then we're just dumping the one we built.
259 log.debug("reimporting new DOM into caller-supplied document");
260 cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
261 internalDoc->release();
264 // We just bind the document we built to the object as the result.
265 cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
266 document=internalDoc;
270 // Now reload the signature from the DOM.
272 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
277 catch(XSECException& e) {
280 auto_ptr_char temp(e.getMsg());
281 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
283 catch(XSECCryptoException& e) {
286 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
290 // Marshall KeyInfo data.
294 const KeyInfo* fromcred = credential->getKeyInfo();
296 m_keyInfo = fromcred->cloneKeyInfo();
298 if (m_keyInfo && (!m_signature->getKeyInfoList() || m_signature->getKeyInfoList()->isEmpty())) {
299 m_keyInfo->marshall(cachedDOM);
302 // Recache the DOM and clear the serialized copy.
303 setDocumentElement(document, cachedDOM);
304 log.debug("caching DOM for Signature (document is %sbound)", bindDocument ? "" : "not ");
305 setDOM(cachedDOM, bindDocument);
306 releaseParentDOM(true);
311 DOMElement* XMLSecSignatureImpl::marshall(DOMElement* parentElement, const vector<Signature*>* sigs, const Credential* credential) const
314 xmltooling::NDC ndc("marshall");
317 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject.Signature");
318 log.debug("marshalling ds:Signature");
320 DOMElement* cachedDOM=getDOM();
322 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
323 log.debug("Signature has a usable cached DOM, reusing it");
324 if (parentElement!=cachedDOM->getParentNode()) {
325 parentElement->appendChild(cachedDOM);
326 releaseParentDOM(true);
331 // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
332 // Without an adoptNode option to maintain the child pointers, we have to either import the
333 // DOM while somehow reassigning all the nested references (which amounts to a complete
334 // *unmarshall* operation), or we just release the existing DOM and hope that we can get
335 // it back. This depends on all objects being able to preserve their DOM at all costs.
336 releaseChildrenDOM(true);
340 // If we get here, we didn't have a usable DOM.
342 // Fresh signature, so we just create an empty one.
343 log.debug("creating empty Signature element");
344 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignature();
345 m_signature->setDSIGNSPrefix(XMLSIG_PREFIX);
346 cachedDOM=m_signature->createBlankSignature(
347 parentElement->getOwnerDocument(), getCanonicalizationMethod(), getSignatureAlgorithm()
351 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl");
352 Wrapper4InputSource dsrc(&src,false);
353 log.debug("parsing XML back into DOM tree");
354 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
356 log.debug("reimporting new DOM into caller-supplied document");
357 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(),true));
358 internalDoc->release();
360 // Now reload the signature from the DOM.
362 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
363 parentElement->getOwnerDocument(), cachedDOM
367 catch(XSECException& e) {
368 auto_ptr_char temp(e.getMsg());
369 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
371 catch(XSECCryptoException& e) {
372 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
376 // Marshall KeyInfo data.
380 const KeyInfo* fromcred = credential->getKeyInfo();
382 m_keyInfo = fromcred->cloneKeyInfo();
384 if (m_keyInfo && (!m_signature->getKeyInfoList() || m_signature->getKeyInfoList()->isEmpty())) {
385 m_keyInfo->marshall(cachedDOM);
388 // Recache the DOM and clear the serialized copy.
389 parentElement->appendChild(cachedDOM);
390 log.debug("caching DOM for Signature");
391 setDOM(cachedDOM, false);
392 releaseParentDOM(true);
397 XMLObject* XMLSecSignatureImpl::unmarshall(DOMElement* element, bool bindDocument)
399 Category::getInstance(XMLTOOLING_LOGCAT".XMLObject.Signature").debug("unmarshalling ds:Signature");
402 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
403 element->getOwnerDocument(), element
407 catch(XSECException& e) {
408 auto_ptr_char temp(e.getMsg());
409 throw UnmarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
411 catch(XSECCryptoException& e) {
412 throw UnmarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
415 setDOM(element, bindDocument);
419 #ifdef HAVE_COVARIANT_RETURNS
424 SignatureBuilder::buildObject(
425 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType
428 if (!XMLString::equals(nsURI,XMLSIG_NS) || !XMLString::equals(localName,Signature::LOCAL_NAME))
429 throw XMLObjectException("XMLSecSignatureBuilder requires standard Signature element name.");
430 return buildObject();
433 #ifdef HAVE_COVARIANT_RETURNS
438 SignatureBuilder::buildObject() const
440 return new XMLSecSignatureImpl();
443 const XMLCh Signature::LOCAL_NAME[] = UNICODE_LITERAL_9(S,i,g,n,a,t,u,r,e);
445 // Raw signature methods.
447 unsigned int Signature::createRawSignature(
448 XSECCryptoKey* key, const XMLCh* sigAlgorithm, const char* in, unsigned int in_len, char* out, unsigned int out_len
452 XSECAlgorithmHandler* handler = XSECPlatformUtils::g_algorithmMapper->mapURIToHandler(sigAlgorithm);
454 auto_ptr_char alg(sigAlgorithm);
455 throw SignatureException("Unsupported signature algorithm ($1).", params(1,alg.get()));
458 // Move input into a safeBuffer to source the transform chain.
460 sb.sbStrncpyIn(in,in_len);
461 TXFMSB* sbt = new TXFMSB(NULL);
462 sbt->setInput(sb, in_len);
466 unsigned int siglen = handler->signToSafeBuffer(&tx, sigAlgorithm, key, out_len-1, sbout);
467 if (siglen >= out_len)
468 throw SignatureException("Signature size exceeded output buffer size.");
470 // Push all non-whitespace into buffer.
471 unsigned int ret_len = 0;
472 const char* source = sbout.rawCharBuffer();
474 if (isspace(*source))
484 catch(XSECException& e) {
485 auto_ptr_char temp(e.getMsg());
486 throw SignatureException(string("Caught an XMLSecurity exception while creating raw signature: ") + temp.get());
488 catch(XSECCryptoException& e) {
489 throw SignatureException(string("Caught an XMLSecurity exception while creating raw signature: ") + e.getMsg());
493 bool Signature::verifyRawSignature(
494 XSECCryptoKey* key, const XMLCh* sigAlgorithm, const char* signature, const char* in, unsigned int in_len
498 XSECAlgorithmHandler* handler = XSECPlatformUtils::g_algorithmMapper->mapURIToHandler(sigAlgorithm);
500 auto_ptr_char alg(sigAlgorithm);
501 throw SignatureException("Unsupported signature algorithm ($1).", params(1,alg.get()));
504 // Move input into a safeBuffer to source the transform chain.
506 sb.sbStrncpyIn(in,in_len);
507 TXFMSB* sbt = new TXFMSB(NULL);
508 sbt->setInput(sb, in_len);
512 return handler->verifyBase64Signature(&tx, sigAlgorithm, signature, 0, key);
514 catch(XSECException& e) {
515 auto_ptr_char temp(e.getMsg());
516 throw SignatureException(string("Caught an XMLSecurity exception while verifying raw signature: ") + temp.get());
518 catch(XSECCryptoException& e) {
519 throw SignatureException(string("Caught an XMLSecurity exception while verifying raw signature: ") + e.getMsg());