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.empty()) {
\r
153 DSIGKeyInfoX509* x509Data=m_signature->appendX509Data();
\r
154 for_each(certs.begin(),certs.end(),bind1st(_addcert(),x509Data));
\r
157 log.debug("computing signature");
\r
158 m_signature->setSigningKey(ctx.getSigningKey());
\r
159 m_signature->sign();
\r
161 catch(XSECException& e) {
\r
162 auto_ptr_char temp(e.getMsg());
\r
163 throw SignatureException(string("Caught an XMLSecurity exception while signing: ") + temp.get());
\r
165 catch(XSECCryptoException& e) {
\r
166 throw SignatureException(string("Caught an XMLSecurity exception while signing: ") + e.getMsg());
\r
170 void XMLSecSignatureImpl::verify(const VerifyingContext& ctx) const
\r
172 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
\r
173 log.debug("verifying signature");
\r
176 throw SignatureException("Only a marshalled Signature object can be verified.");
\r
179 ctx.verifySignature(m_signature);
\r
181 catch(XSECException& e) {
\r
182 auto_ptr_char temp(e.getMsg());
\r
183 throw SignatureException(string("Caught an XMLSecurity exception verifying signature: ") + temp.get());
\r
185 catch(XSECCryptoException& e) {
\r
186 throw SignatureException(string("Caught an XMLSecurity exception verifying signature: ") + e.getMsg());
\r
190 DOMElement* XMLSecSignatureImpl::marshall(DOMDocument* document, MarshallingContext* ctx) const
\r
193 xmltooling::NDC ndc("marshall");
\r
196 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
\r
197 log.debug("marshalling ds:Signature");
\r
199 DOMElement* cachedDOM=getDOM();
\r
201 if (!document || document==cachedDOM->getOwnerDocument()) {
\r
202 log.debug("Signature has a usable cached DOM, reusing it");
\r
204 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
\r
205 releaseParentDOM(true);
\r
209 // We have a DOM but it doesn't match the document we were given, so we import
\r
210 // it into the new document.
\r
211 cachedDOM=static_cast<DOMElement*>(document->importNode(cachedDOM, true));
\r
214 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
\r
216 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
217 document, cachedDOM
\r
219 m_signature->load();
\r
221 catch(XSECException& e) {
\r
222 auto_ptr_char temp(e.getMsg());
\r
223 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
225 catch(XSECCryptoException& e) {
\r
226 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
229 // Recache the DOM.
\r
230 setDocumentElement(document, cachedDOM);
\r
231 log.debug("caching imported DOM for Signature");
\r
232 setDOM(cachedDOM, false);
\r
233 releaseParentDOM(true);
\r
237 // If we get here, we didn't have a usable DOM.
\r
238 bool bindDocument=false;
\r
239 if (m_xml.empty()) {
\r
240 // Fresh signature, so we just create an empty one.
\r
241 log.debug("creating empty Signature element");
\r
243 document=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
\r
246 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignature();
\r
247 m_signature->setDSIGNSPrefix(XMLConstants::XMLSIG_PREFIX);
\r
248 cachedDOM=m_signature->createBlankSignature(document, getCanonicalizationMethod(), getSignatureAlgorithm());
\r
251 // We need to reparse the XML we saved off into a new DOM.
\r
252 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl");
\r
253 Wrapper4InputSource dsrc(&src,false);
\r
254 log.debug("parsing Signature XML back into DOM tree");
\r
255 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
\r
257 // The caller insists on using his own document, so we now have to import the thing
\r
258 // into it. Then we're just dumping the one we built.
\r
259 log.debug("reimporting new DOM into caller-supplied document");
\r
260 cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
\r
261 internalDoc->release();
\r
264 // We just bind the document we built to the object as the result.
\r
265 cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
\r
266 document=internalDoc;
\r
270 // Now reload the signature from the DOM.
\r
272 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
273 document, cachedDOM
\r
275 m_signature->load();
\r
277 catch(XSECException& e) {
\r
279 document->release();
\r
280 auto_ptr_char temp(e.getMsg());
\r
281 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
283 catch(XSECCryptoException& e) {
\r
285 document->release();
\r
286 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
290 // Recache the DOM and clear the serialized copy.
\r
291 setDocumentElement(document, cachedDOM);
\r
292 log.debug("caching DOM for Signature (document is %sbound)", bindDocument ? "" : "not ");
\r
293 setDOM(cachedDOM, bindDocument);
\r
294 releaseParentDOM(true);
\r
299 DOMElement* XMLSecSignatureImpl::marshall(DOMElement* parentElement, MarshallingContext* ctx) const
\r
302 xmltooling::NDC ndc("marshall");
\r
305 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
\r
306 log.debug("marshalling ds:Signature");
\r
308 DOMElement* cachedDOM=getDOM();
\r
310 if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
\r
311 log.debug("Signature has a usable cached DOM, reusing it");
\r
312 parentElement->appendChild(cachedDOM);
\r
313 releaseParentDOM(true);
\r
317 // We have a DOM but it doesn't match the document we were given, so we import
\r
318 // it into the new document.
\r
319 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(cachedDOM, true));
\r
322 XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
\r
324 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
325 parentElement->getOwnerDocument(), cachedDOM
\r
327 m_signature->load();
\r
329 catch(XSECException& e) {
\r
330 auto_ptr_char temp(e.getMsg());
\r
331 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
333 catch(XSECCryptoException& e) {
\r
334 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
337 // Recache the DOM.
\r
338 parentElement->appendChild(cachedDOM);
\r
339 log.debug("caching imported DOM for Signature");
\r
340 setDOM(cachedDOM, false);
\r
341 releaseParentDOM(true);
\r
345 // If we get here, we didn't have a usable DOM.
\r
346 if (m_xml.empty()) {
\r
347 // Fresh signature, so we just create an empty one.
\r
348 log.debug("creating empty Signature element");
\r
349 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignature();
\r
350 m_signature->setDSIGNSPrefix(XMLConstants::XMLSIG_PREFIX);
\r
351 cachedDOM=m_signature->createBlankSignature(
\r
352 parentElement->getOwnerDocument(), getCanonicalizationMethod(), getSignatureAlgorithm()
\r
356 MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl");
\r
357 Wrapper4InputSource dsrc(&src,false);
\r
358 log.debug("parsing XML back into DOM tree");
\r
359 DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
\r
361 log.debug("reimporting new DOM into caller-supplied document");
\r
362 cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(),true));
\r
363 internalDoc->release();
\r
365 // Now reload the signature from the DOM.
\r
367 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
368 parentElement->getOwnerDocument(), cachedDOM
\r
370 m_signature->load();
\r
372 catch(XSECException& e) {
\r
373 auto_ptr_char temp(e.getMsg());
\r
374 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
376 catch(XSECCryptoException& e) {
\r
377 throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
381 // Recache the DOM and clear the serialized copy.
\r
382 parentElement->appendChild(cachedDOM);
\r
383 log.debug("caching DOM for Signature");
\r
384 setDOM(cachedDOM, false);
\r
385 releaseParentDOM(true);
\r
390 XMLObject* XMLSecSignatureImpl::unmarshall(DOMElement* element, bool bindDocument)
\r
392 Category::getInstance(XMLTOOLING_LOGCAT".Signature").debug("unmarshalling ds:Signature");
\r
395 m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
\r
396 element->getOwnerDocument(), element
\r
398 m_signature->load();
\r
400 catch(XSECException& e) {
\r
401 auto_ptr_char temp(e.getMsg());
\r
402 throw UnmarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
\r
404 catch(XSECCryptoException& e) {
\r
405 throw UnmarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
\r
408 setDOM(element, bindDocument);
\r
412 Signature* SignatureBuilder::buildObject(
\r
413 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType
\r
416 if (!XMLString::equals(nsURI,XMLConstants::XMLSIG_NS) || !XMLString::equals(localName,Signature::LOCAL_NAME))
\r
417 throw XMLObjectException("XMLSecSignatureBuilder requires standard Signature element name.");
\r
418 return buildObject();
\r
421 Signature* SignatureBuilder::buildObject() const
\r
423 return new XMLSecSignatureImpl();
\r
426 const XMLCh Signature::LOCAL_NAME[] = UNICODE_LITERAL_9(S,i,g,n,a,t,u,r,e);
\r