Merge branch '1.x' of ssh://authdev.it.ohio-state.edu/~scantor/git/cpp-xmltooling...
[shibboleth/cpp-xmltooling.git] / xmltooling / signature / impl / XMLSecSignatureImpl.cpp
index dec07c1..1d69463 100644 (file)
@@ -1,23 +1,27 @@
-/*
-*  Copyright 2001-2009 Internet2
- * 
-* Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
+ *
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
  */
 
 /**
  * XMLSecSignatureImpl.cpp
  * 
- * Signature class for XMLSec-based signature-handling
+ * Signature class for XMLSec-based signature-handling.
  */
 
 #include "internal.h"
@@ -65,7 +69,7 @@ namespace xmlsignature {
     public:
         XMLSecSignatureImpl() : AbstractXMLObject(XMLSIG_NS, Signature::LOCAL_NAME, XMLSIG_PREFIX),
             UnknownElementImpl(XMLSIG_NS, Signature::LOCAL_NAME, XMLSIG_PREFIX),
-            m_signature(NULL), m_c14n(NULL), m_sm(NULL), m_key(NULL), m_keyInfo(NULL), m_reference(NULL) {}
+            m_signature(nullptr), m_c14n(nullptr), m_sm(nullptr), m_key(nullptr), m_keyInfo(nullptr), m_reference(nullptr) {}
         virtual ~XMLSecSignatureImpl();
         
         void releaseDOM() const;
@@ -79,8 +83,8 @@ namespace xmlsignature {
         XMLObject* clone() const;
         Signature* cloneSignature() const;
 
-        DOMElement* marshall(DOMDocument* document=NULL, const vector<Signature*>* sigs=NULL, const Credential* credential=NULL) const;
-        DOMElement* marshall(DOMElement* parentElement, const vector<Signature*>* sigs=NULL, const Credential* credential=NULL) const;
+        DOMElement* marshall(DOMDocument* document=nullptr, const vector<Signature*>* sigs=nullptr, const Credential* credential=nullptr) const;
+        DOMElement* marshall(DOMElement* parentElement, const vector<Signature*>* sigs=nullptr, const Credential* credential=nullptr) const;
         XMLObject* unmarshall(DOMElement* element, bool bindDocument=false);
         
         // Getters
@@ -122,7 +126,7 @@ namespace xmlsignature {
             m_reference=reference;
         }
         
-        void sign(const Credential* credential=NULL);
+        void sign(const Credential* credential=nullptr);
 
     private:
         mutable DSIGSignature* m_signature;
@@ -176,7 +180,7 @@ void XMLSecSignatureImpl::releaseDOM() const
         // Release the associated signature.
         if (m_signature) {
             XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->releaseSignature(m_signature);
-            m_signature=NULL;
+            m_signature=nullptr;
         }
     }
 }
@@ -274,7 +278,7 @@ DOMElement* XMLSecSignatureImpl::marshall(DOMDocument* document, const vector<Si
         // Fresh signature, so we just create an empty one.
         log.debug("creating empty Signature element");
         if (!document) {
-            document=DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
+            document=DOMImplementationRegistry::getDOMImplementation(nullptr)->createDocument();
             bindDocument=true;
         }
         DSIGSignature* temp=XMLToolingInternalConfig::getInternalConfig().m_xsecProvider->newSignature();
@@ -295,7 +299,16 @@ DOMElement* XMLSecSignatureImpl::marshall(DOMDocument* document, const vector<Si
             // The caller insists on using his own document, so we now have to import the thing
             // into it. Then we're just dumping the one we built.
             log.debug("reimporting new DOM into caller-supplied document");
-            cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
+            try {
+                cachedDOM=static_cast<DOMElement*>(document->importNode(internalDoc->getDocumentElement(), true));
+            }
+            catch (XMLException& ex) {
+                internalDoc->release();
+                auto_ptr_char temp(ex.getMessage());
+                throw XMLParserException(
+                    string("Error importing DOM into caller-supplied document: ") + (temp.get() ? temp.get() : "no message")
+                    );
+            }
             internalDoc->release();
         }
         else {
@@ -328,7 +341,7 @@ DOMElement* XMLSecSignatureImpl::marshall(DOMDocument* document, const vector<Si
     // Marshall KeyInfo data.
     if (credential) {
         delete m_keyInfo;
-        m_keyInfo = NULL;
+        m_keyInfo = nullptr;
         m_keyInfo = credential->getKeyInfo();
     }
     if (m_keyInfo && (!m_signature->getKeyInfoList() || m_signature->getKeyInfoList()->isEmpty())) {
@@ -392,7 +405,16 @@ DOMElement* XMLSecSignatureImpl::marshall(DOMElement* parentElement, const vecto
         DOMDocument* internalDoc=XMLToolingConfig::getConfig().getParser().parse(dsrc);
         
         log.debug("reimporting new DOM into caller-supplied document");
-        cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(),true));
+        try {
+            cachedDOM=static_cast<DOMElement*>(parentElement->getOwnerDocument()->importNode(internalDoc->getDocumentElement(),true));
+        }
+        catch (XMLException& ex) {
+            internalDoc->release();
+            auto_ptr_char temp(ex.getMessage());
+            throw XMLParserException(
+                string("Error importing DOM into caller-supplied document: ") + (temp.get() ? temp.get() : "no message")
+                );
+        }
         internalDoc->release();
 
         // Now reload the signature from the DOM.
@@ -414,7 +436,7 @@ DOMElement* XMLSecSignatureImpl::marshall(DOMElement* parentElement, const vecto
     // Marshall KeyInfo data.
     if (credential) {
         delete m_keyInfo;
-        m_keyInfo = NULL;
+        m_keyInfo = nullptr;
         m_keyInfo = credential->getKeyInfo();
     }
     if (m_keyInfo && (!m_signature->getKeyInfoList() || m_signature->getKeyInfoList()->isEmpty())) {
@@ -508,7 +530,7 @@ unsigned int Signature::createRawSignature(
         // Move input into a safeBuffer to source the transform chain.
         safeBuffer sb,sbout;
         sb.sbStrncpyIn(in,in_len);
-        TXFMSB* sbt = new TXFMSB(NULL);
+        TXFMSB* sbt = new TXFMSB(nullptr);
         sbt->setInput(sb, in_len);
         TXFMChain tx(sbt);
         
@@ -554,7 +576,7 @@ bool Signature::verifyRawSignature(
         // Move input into a safeBuffer to source the transform chain.
         safeBuffer sb;
         sb.sbStrncpyIn(in,in_len);
-        TXFMSB* sbt = new TXFMSB(NULL);
+        TXFMSB* sbt = new TXFMSB(nullptr);
         sbt->setInput(sb, in_len);
         TXFMChain tx(sbt);