Multi-line svn commit, see body.
[shibboleth/cpp-xmltooling.git] / xmltooling / signature / impl / XMLSecSignatureImpl.cpp
1 /*
2 *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * XMLSecSignatureImpl.cpp
19  * 
20  * Signature class for XMLSec-based signature-handling
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "logging.h"
26 #include "impl/UnknownElement.h"
27 #include "security/Credential.h"
28 #include "signature/KeyInfo.h"
29 #include "signature/Signature.h"
30 #include "util/NDC.h"
31 #include "util/XMLConstants.h"
32 #include "util/XMLHelper.h"
33
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>
46
47 using namespace xmlsignature;
48 using namespace xmltooling::logging;
49 using namespace xmltooling;
50 using namespace std;
51 using xmlconstants::XMLSIG_NS;
52 using xmlconstants::XMLSIG_PREFIX;
53
54 #if defined (_MSC_VER)
55     #pragma warning( push )
56     #pragma warning( disable : 4250 4251 )
57 #endif
58
59 namespace xmlsignature {
60     
61     class XMLTOOL_DLLLOCAL XMLSecSignatureImpl : public UnknownElementImpl, public virtual Signature
62     {
63     public:
64         XMLSecSignatureImpl() : AbstractXMLObject(XMLSIG_NS, Signature::LOCAL_NAME, XMLSIG_PREFIX),
65             UnknownElementImpl(XMLSIG_NS, Signature::LOCAL_NAME, XMLSIG_PREFIX),
66             m_signature(NULL), m_c14n(NULL), m_sm(NULL), m_key(NULL), m_keyInfo(NULL), m_reference(NULL) {}
67         virtual ~XMLSecSignatureImpl();
68         
69         void releaseDOM() const;
70         void releaseChildrenDOM(bool propagateRelease=true) const {
71             if (m_keyInfo) {
72                 m_keyInfo->releaseDOM();
73                 if (propagateRelease)
74                     m_keyInfo->releaseChildrenDOM();
75             }
76         }
77         XMLObject* clone() const;
78         Signature* cloneSignature() const;
79
80         DOMElement* marshall(DOMDocument* document=NULL, const vector<Signature*>* sigs=NULL, const Credential* credential=NULL) const;
81         DOMElement* marshall(DOMElement* parentElement, const vector<Signature*>* sigs=NULL, const Credential* credential=NULL) const;
82         XMLObject* unmarshall(DOMElement* element, bool bindDocument=false);
83         
84         // Getters
85         const XMLCh* getCanonicalizationMethod() const {
86             if (m_signature)
87                 return canonicalizationMethod2UNICODEURI(m_signature->getCanonicalizationMethod());
88             return m_c14n ? m_c14n : DSIGConstants::s_unicodeStrURIEXC_C14N_NOC;
89         }
90         const XMLCh* getSignatureAlgorithm() const {
91             if (!m_sm && m_signature) {
92                 safeBuffer sURI;
93                 if (signatureHashMethod2URI(sURI, m_signature->getSignatureMethod(), m_signature->getHashMethod()) == false)
94                     return NULL;
95                 m_sm = XMLString::replicate(sURI.sbStrToXMLCh());
96             }
97             return m_sm ? m_sm : DSIGConstants::s_unicodeStrURIRSA_SHA1;
98         }
99
100         KeyInfo* getKeyInfo() const { return m_keyInfo; }
101         ContentReference* getContentReference() const { return m_reference; }
102         DSIGSignature* getXMLSignature() const { return m_signature; }
103         
104         // Setters
105         void setCanonicalizationMethod(const XMLCh* c14n) { m_c14n = prepareForAssignment(m_c14n,c14n); }
106         void setSignatureAlgorithm(const XMLCh* sm) { m_sm = prepareForAssignment(m_sm,sm); }
107         void setSigningKey(XSECCryptoKey* signingKey) {
108             delete m_key;
109             m_key=signingKey;
110         }
111         void setKeyInfo(KeyInfo* keyInfo) {
112             prepareForAssignment(m_keyInfo, keyInfo);
113             m_keyInfo=keyInfo;
114         }
115         void setContentReference(ContentReference* reference) {
116             delete m_reference;
117             m_reference=reference;
118         }
119         
120         void sign(const Credential* credential=NULL);
121
122     private:
123         mutable DSIGSignature* m_signature;
124         XMLCh* m_c14n;
125         mutable XMLCh* m_sm;
126         XSECCryptoKey* m_key;
127         mutable KeyInfo* m_keyInfo;
128         ContentReference* m_reference;
129     };
130     
131 };
132
133 #if defined (_MSC_VER)
134     #pragma warning( pop )
135 #endif
136
137 XMLSecSignatureImpl::~XMLSecSignatureImpl()
138 {
139     // Release the associated signature.
140     if (m_signature)
141         XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
142
143     XMLString::release(&m_c14n);
144     XMLString::release(&m_sm);
145     delete m_key;
146     delete m_keyInfo;
147     delete m_reference;
148 }
149
150 void XMLSecSignatureImpl::releaseDOM() const
151 {
152     if (getDOM()) {
153         // This should save off the DOM
154         UnknownElementImpl::releaseDOM();
155         
156         // Release the associated signature.
157         if (m_signature) {
158             XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
159             m_signature=NULL;
160         }
161     }
162 }
163
164 XMLObject* XMLSecSignatureImpl::clone() const
165 {
166     return cloneSignature();
167 }
168
169 Signature* XMLSecSignatureImpl::cloneSignature() const
170 {
171     XMLSecSignatureImpl* ret=new XMLSecSignatureImpl();
172
173     ret->m_c14n=XMLString::replicate(m_c14n);
174     ret->m_sm=XMLString::replicate(m_sm);
175     if (m_key)
176         ret->m_key=m_key->clone();
177     if (m_keyInfo)
178         ret->m_keyInfo=m_keyInfo->cloneKeyInfo();
179
180     // If there's no XML locally, serialize this object into the new one, otherwise just copy it over.
181     if (m_xml.empty())
182         serialize(ret->m_xml);
183     else
184         ret->m_xml=m_xml;
185
186     return ret;
187 }
188
189 void XMLSecSignatureImpl::sign(const Credential* credential)
190 {
191     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Signature");
192     log.debug("applying signature");
193
194     if (!m_signature)
195         throw SignatureException("Only a marshalled Signature object can be signed.");
196     else if (!m_reference)
197         throw SignatureException("No ContentReference object set for signature creation.");
198
199     XSECCryptoKey* key = credential ? credential->getPrivateKey() : m_key;
200     if (!key)
201         throw SignatureException("No signing key available for signature creation.");
202
203     try {
204         log.debug("creating signature reference(s)");
205         DSIGReferenceList* refs = m_signature->getReferenceList();
206         while (refs && refs->getSize())
207             delete refs->removeReference(0);
208         m_reference->createReferences(m_signature);
209         
210         log.debug("computing signature");
211         m_signature->setSigningKey(key->clone());
212         m_signature->sign();
213     }
214     catch(XSECException& e) {
215         auto_ptr_char temp(e.getMsg());
216         throw SignatureException(string("Caught an XMLSecurity exception while signing: ") + temp.get());
217     }
218     catch(XSECCryptoException& e) {
219         throw SignatureException(string("Caught an XMLSecurity exception while signing: ") + e.getMsg());
220     }
221 }
222
223 DOMElement* XMLSecSignatureImpl::marshall(DOMDocument* document, const vector<Signature*>* sigs, const Credential* credential) const
224 {
225 #ifdef _DEBUG
226     xmltooling::NDC ndc("marshall");
227 #endif
228     
229     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject.Signature");
230     log.debug("marshalling ds:Signature");
231
232     DOMElement* cachedDOM=getDOM();
233     if (cachedDOM) {
234         if (!document || document==cachedDOM->getOwnerDocument()) {
235             log.debug("Signature has a usable cached DOM, reusing it");
236             if (document)
237                 setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM);
238             releaseParentDOM(true);
239             return cachedDOM;
240         }
241         
242         // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
243         // Without an adoptNode option to maintain the child pointers, we have to either import the
244         // DOM while somehow reassigning all the nested references (which amounts to a complete
245         // *unmarshall* operation), or we just release the existing DOM and hope that we can get
246         // it back. This depends on all objects being able to preserve their DOM at all costs.
247         releaseChildrenDOM(true);
248         releaseDOM();
249     }
250     
251     // If we get here, we didn't have a usable DOM.
252     bool bindDocument=false;
253     if (m_xml.empty()) {
254         // Fresh signature, so we just create an empty one.
255         log.debug("creating empty Signature element");
256         if (!document) {
257             document=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
258             bindDocument=true;
259         }
260         DSIGSignature* temp=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignature();
261         temp->setDSIGNSPrefix(XMLSIG_PREFIX);
262         cachedDOM=temp->createBlankSignature(document, getCanonicalizationMethod(), getSignatureAlgorithm());
263         m_signature = temp;
264     }
265     else {
266         // We need to reparse the XML we saved off into a new DOM.
267         MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl");
268         Wrapper4InputSource dsrc(&src,false);
269         log.debug("parsing Signature XML back into DOM tree");
270         DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
271         if (document) {
272             // The caller insists on using his own document, so we now have to import the thing
273             // into it. Then we're just dumping the one we built.
274             log.debug("reimporting new DOM into caller-supplied document");
275             cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
276             internalDoc->release();
277         }
278         else {
279             // We just bind the document we built to the object as the result.
280             cachedDOM=static_cast<DOMElement*>(internalDoc->getDocumentElement());
281             document=internalDoc;
282             bindDocument=true;
283         }
284
285         // Now reload the signature from the DOM.
286         try {
287             m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
288                 document, cachedDOM
289                 );
290             m_signature->load();
291         }
292         catch(XSECException& e) {
293             if (bindDocument)
294                 document->release();
295             auto_ptr_char temp(e.getMsg());
296             throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
297         }
298         catch(XSECCryptoException& e) {
299             if (bindDocument)
300                 document->release();
301             throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
302         }
303     }
304     
305     // Marshall KeyInfo data.
306     if (credential) {
307         delete m_keyInfo;
308         m_keyInfo = NULL;
309         m_keyInfo = credential->getKeyInfo();
310     }
311     if (m_keyInfo && (!m_signature->getKeyInfoList() || m_signature->getKeyInfoList()->isEmpty())) {
312         m_keyInfo->marshall(cachedDOM);
313     }
314
315     // Recache the DOM and clear the serialized copy.
316     setDocumentElement(document, cachedDOM);
317     log.debug("caching DOM for Signature (document is %sbound)", bindDocument ? "" : "not ");
318     setDOM(cachedDOM, bindDocument);
319     releaseParentDOM(true);
320     m_xml.erase();
321     return cachedDOM;
322 }
323
324 DOMElement* XMLSecSignatureImpl::marshall(DOMElement* parentElement, const vector<Signature*>* sigs, const Credential* credential) const
325 {
326 #ifdef _DEBUG
327     xmltooling::NDC ndc("marshall");
328 #endif
329     
330     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject.Signature");
331     log.debug("marshalling ds:Signature");
332
333     DOMElement* cachedDOM=getDOM();
334     if (cachedDOM) {
335         if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) {
336             log.debug("Signature has a usable cached DOM, reusing it");
337             if (parentElement!=cachedDOM->getParentNode()) {
338                 parentElement->appendChild(cachedDOM);
339                 releaseParentDOM(true);
340             }
341             return cachedDOM;
342         }
343         
344         // We have a DOM but it doesn't match the document we were given. This both sucks and blows.
345         // Without an adoptNode option to maintain the child pointers, we have to either import the
346         // DOM while somehow reassigning all the nested references (which amounts to a complete
347         // *unmarshall* operation), or we just release the existing DOM and hope that we can get
348         // it back. This depends on all objects being able to preserve their DOM at all costs.
349         releaseChildrenDOM(true);
350         releaseDOM();
351     }
352     
353     // If we get here, we didn't have a usable DOM.
354     if (m_xml.empty()) {
355         // Fresh signature, so we just create an empty one.
356         log.debug("creating empty Signature element");
357         DSIGSignature* temp=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignature();
358         temp->setDSIGNSPrefix(XMLSIG_PREFIX);
359         cachedDOM=temp->createBlankSignature(parentElement->getOwnerDocument(), getCanonicalizationMethod(), getSignatureAlgorithm());
360         m_signature = temp;
361     }
362     else {
363         MemBufInputSource src(reinterpret_cast<const XMLByte*>(m_xml.c_str()),m_xml.length(),"XMLSecSignatureImpl");
364         Wrapper4InputSource dsrc(&src,false);
365         log.debug("parsing XML back into DOM tree");
366         DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
367         
368         log.debug("reimporting new DOM into caller-supplied document");
369         cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(),true));
370         internalDoc->release();
371
372         // Now reload the signature from the DOM.
373         try {
374             m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
375                 parentElement->getOwnerDocument(), cachedDOM
376                 );
377             m_signature->load();
378         }
379         catch(XSECException& e) {
380             auto_ptr_char temp(e.getMsg());
381             throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
382         }
383         catch(XSECCryptoException& e) {
384             throw MarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
385         }
386     }
387
388     // Marshall KeyInfo data.
389     if (credential) {
390         delete m_keyInfo;
391         m_keyInfo = NULL;
392         m_keyInfo = credential->getKeyInfo();
393     }
394     if (m_keyInfo && (!m_signature->getKeyInfoList() || m_signature->getKeyInfoList()->isEmpty())) {
395         m_keyInfo->marshall(cachedDOM);
396     }
397
398     // Recache the DOM and clear the serialized copy.
399     parentElement->appendChild(cachedDOM);
400     log.debug("caching DOM for Signature");
401     setDOM(cachedDOM, false);
402     releaseParentDOM(true);
403     m_xml.erase();
404     return cachedDOM;
405 }
406
407 XMLObject* XMLSecSignatureImpl::unmarshall(DOMElement* element, bool bindDocument)
408 {
409     Category::getInstance(XMLTOOLING_LOGCAT".XMLObject.Signature").debug("unmarshalling ds:Signature");
410
411     try {
412         m_signature=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignatureFromDOM(
413             element->getOwnerDocument(), element
414             );
415         m_signature->load();
416     }
417     catch(XSECException& e) {
418         auto_ptr_char temp(e.getMsg());
419         throw UnmarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + temp.get());
420     }
421     catch(XSECCryptoException& e) {
422         throw UnmarshallingException(string("Caught an XMLSecurity exception while loading signature: ") + e.getMsg());
423     }
424
425     setDOM(element, bindDocument);
426     return this;
427 }
428
429 #ifdef HAVE_COVARIANT_RETURNS
430 Signature*
431 #else
432 XMLObject*
433 #endif
434 SignatureBuilder::buildObject(
435     const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType
436     ) const
437 {
438     if (!XMLString::equals(nsURI,XMLSIG_NS) || !XMLString::equals(localName,Signature::LOCAL_NAME))
439         throw XMLObjectException("XMLSecSignatureBuilder requires standard Signature element name.");
440     return buildObject();
441 }
442
443 #ifdef HAVE_COVARIANT_RETURNS
444 Signature*
445 #else
446 XMLObject*
447 #endif
448 SignatureBuilder::buildObject() const
449 {
450     return new XMLSecSignatureImpl();
451 }
452
453 const XMLCh Signature::LOCAL_NAME[] = UNICODE_LITERAL_9(S,i,g,n,a,t,u,r,e);
454
455 // Raw signature methods.
456
457 unsigned int Signature::createRawSignature(
458     XSECCryptoKey* key, const XMLCh* sigAlgorithm, const char* in, unsigned int in_len, char* out, unsigned int out_len
459     )
460 {
461     try {
462         XSECAlgorithmHandler* handler = XSECPlatformUtils::g_algorithmMapper->mapURIToHandler(sigAlgorithm);
463         if (!handler) {
464             auto_ptr_char alg(sigAlgorithm);
465             throw SignatureException("Unsupported signature algorithm ($1).", params(1,alg.get()));
466         }
467         
468         // Move input into a safeBuffer to source the transform chain.
469         safeBuffer sb,sbout;
470         sb.sbStrncpyIn(in,in_len);
471         TXFMSB* sbt = new TXFMSB(NULL);
472         sbt->setInput(sb, in_len);
473         TXFMChain tx(sbt);
474         
475         // Sign the chain.
476         unsigned int siglen = handler->signToSafeBuffer(&tx, sigAlgorithm, key, out_len-1, sbout);
477         if (siglen >= out_len)
478             throw SignatureException("Signature size exceeded output buffer size.");
479         
480         // Push all non-whitespace into buffer.
481         unsigned int ret_len = 0;
482         const char* source = sbout.rawCharBuffer();
483         while (siglen--) {
484             if (isspace(*source))
485                 ++source;
486             else {
487                 *out++ = *source++;
488                 ++ret_len;
489             }
490         }
491         *out = 0;
492         return ret_len;
493     }
494     catch(XSECException& e) {
495         auto_ptr_char temp(e.getMsg());
496         throw SignatureException(string("Caught an XMLSecurity exception while creating raw signature: ") + temp.get());
497     }
498     catch(XSECCryptoException& e) {
499         throw SignatureException(string("Caught an XMLSecurity exception while creating raw signature: ") + e.getMsg());
500     }
501 }
502
503 bool Signature::verifyRawSignature(
504     XSECCryptoKey* key, const XMLCh* sigAlgorithm, const char* signature, const char* in, unsigned int in_len
505     )
506 {
507     try {
508         XSECAlgorithmHandler* handler = XSECPlatformUtils::g_algorithmMapper->mapURIToHandler(sigAlgorithm);
509         if (!handler) {
510             auto_ptr_char alg(sigAlgorithm);
511             throw SignatureException("Unsupported signature algorithm ($1).", params(1,alg.get()));
512         }
513         
514         // Move input into a safeBuffer to source the transform chain.
515         safeBuffer sb;
516         sb.sbStrncpyIn(in,in_len);
517         TXFMSB* sbt = new TXFMSB(NULL);
518         sbt->setInput(sb, in_len);
519         TXFMChain tx(sbt);
520         
521         // Verify the chain.
522         return handler->verifyBase64Signature(&tx, sigAlgorithm, signature, 0, key);
523     }
524     catch(XSECException& e) {
525         auto_ptr_char temp(e.getMsg());
526         throw SignatureException(string("Caught an XMLSecurity exception while verifying raw signature: ") + temp.get());
527     }
528     catch(XSECCryptoException& e) {
529         throw SignatureException(string("Caught an XMLSecurity exception while verifying raw signature: ") + e.getMsg());
530     }
531 }
532
533 void Signature::extractNames(DSIGKeyInfoList* keyInfo, set<string>& names)
534 {
535     char* kn;
536     const XMLCh* n;
537
538     for (size_t s=0; s<keyInfo->getSize(); s++) {
539         n=keyInfo->item(s)->getKeyName();
540         if (n && *n) {
541             kn=toUTF8(n);
542             names.insert(kn);
543             delete[] kn;
544         }
545     }
546 }