24cd51
From 9ef423d4e7c85629772131b3216b98e17d7b8d7e Mon Sep 17 00:00:00 2001
24cd51
From: Michael Stahl <michael.stahl@allotropia.de>
24cd51
Date: Thu, 18 Feb 2021 19:22:31 +0100
24cd51
Subject: [PATCH 4/6] CVE-2021-25634
24cd51
MIME-Version: 1.0
24cd51
Content-Type: text/plain; charset=UTF-8
24cd51
Content-Transfer-Encoding: 8bit
24cd51
24cd51
xmlsecurity: XSecParser confused about multiple timestamps
24cd51
24cd51
LO writes timestamp both to dc:date and xades:SigningTime elements.
24cd51
24cd51
The parser tries to avoid reading multiple dc:date, preferring the first
24cd51
one, but doesn't care about multiple xades:SigningTime, for undocumented
24cd51
reasons.
24cd51
24cd51
Ideally something should check all read values for consistency.
24cd51
24cd51
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111160
24cd51
Tested-by: Jenkins
24cd51
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
24cd51
(cherry picked from commit 4ab8d9c09a5873ca0aea56dafa1ab34758d52ef7)
24cd51
24cd51
xmlsecurity: remove XSecController::setPropertyId()
24cd51
24cd51
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111252
24cd51
Tested-by: Jenkins
24cd51
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
24cd51
(cherry picked from commit d2a345e1163616fe3201ef1d6c758e2e819214e0)
24cd51
24cd51
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111908
24cd51
Tested-by: Jenkins
24cd51
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
24cd51
(cherry picked from commit abe77c4fcb9ea97d9fff07eaea6d8863bcba5b02)
24cd51
24cd51
Change-Id: Ic018ee89797a1c8a4f870ae102af48006de930ef
24cd51
---
24cd51
 include/svl/sigstruct.hxx                    |  7 +-
24cd51
 xmlsecurity/inc/xsecctl.hxx                  |  5 +-
24cd51
 xmlsecurity/source/helper/ooxmlsecparser.cxx |  4 +-
24cd51
 xmlsecurity/source/helper/xsecctl.cxx        |  2 +-
24cd51
 xmlsecurity/source/helper/xsecparser.cxx     | 81 ++++++++++----------
24cd51
 xmlsecurity/source/helper/xsecparser.hxx     |  6 --
24cd51
 xmlsecurity/source/helper/xsecsign.cxx       |  4 +-
24cd51
 xmlsecurity/source/helper/xsecverify.cxx     | 39 ++++------
24cd51
 8 files changed, 68 insertions(+), 80 deletions(-)
24cd51
24cd51
diff --git a/include/svl/sigstruct.hxx b/include/svl/sigstruct.hxx
24cd51
index f6ee242c84d1..7a0296fa9fae 100644
24cd51
--- a/include/svl/sigstruct.hxx
24cd51
+++ b/include/svl/sigstruct.hxx
24cd51
@@ -103,6 +103,9 @@ struct SignatureInformation
24cd51
     // XAdES EncapsulatedX509Certificate values
24cd51
     std::set<OUString> maEncapsulatedX509Certificates;
24cd51
 
24cd51
+    OUString ouSignatureId;
24cd51
+    // signature may contain multiple time stamps - check they're consistent
24cd51
+    bool hasInconsistentSigningTime = false;
24cd51
     //We also keep the date and time as string. This is done when this
24cd51
     //structure is created as a result of a XML signature being read.
24cd51
     //When then a signature is added or another removed, then the original
24cd51
@@ -115,8 +118,8 @@ struct SignatureInformation
24cd51
     //and the converted time is written back, then the string looks different
24cd51
     //and the signature is broken.
24cd51
     OUString ouDateTime;
24cd51
-    OUString ouSignatureId;
24cd51
-    OUString ouPropertyId;
24cd51
+    /// The Id attribute of the <SignatureProperty> element that contains the <dc:date>.
24cd51
+    OUString ouDateTimePropertyId;
24cd51
     /// Characters of the <dc:description> element inside the signature.
24cd51
     OUString ouDescription;
24cd51
     /// The Id attribute of the <SignatureProperty> element that contains the <dc:description>.
24cd51
diff --git a/xmlsecurity/inc/xsecctl.hxx b/xmlsecurity/inc/xsecctl.hxx
24cd51
index 351c94a2a3e6..7baa219fb13c 100644
24cd51
--- a/xmlsecurity/inc/xsecctl.hxx
24cd51
+++ b/xmlsecurity/inc/xsecctl.hxx
24cd51
@@ -271,8 +271,8 @@ private:
24cd51
     void setGpgCertificate( OUString const & ouGpgCert );
24cd51
     void setGpgOwner( OUString const & ouGpgOwner );
24cd51
 
24cd51
-    void setDate( OUString const & ouDate );
24cd51
-    void setDescription(const OUString& rDescription);
24cd51
+    void setDate(OUString const& rId, OUString const& ouDate);
24cd51
+    void setDescription(OUString const& rId, OUString const& rDescription);
24cd51
     void setCertDigest(const OUString& rCertDigest);
24cd51
     void setValidSignatureImage(const OUString& rValidSigImg);
24cd51
     void setInvalidSignatureImage(const OUString& rInvalidSigImg);
24cd51
@@ -283,7 +283,6 @@ public:
24cd51
 
24cd51
 private:
24cd51
     void setId( OUString const & ouId );
24cd51
-    void setPropertyId( OUString const & ouPropertyId );
24cd51
 
24cd51
     css::uno::Reference< css::xml::crypto::sax::XReferenceResolvedListener > prepareSignatureToRead(
24cd51
         sal_Int32 nSecurityId );
24cd51
diff --git a/xmlsecurity/source/helper/ooxmlsecparser.cxx b/xmlsecurity/source/helper/ooxmlsecparser.cxx
24cd51
index c22e8c2261bf..a200de60c07a 100644
24cd51
--- a/xmlsecurity/source/helper/ooxmlsecparser.cxx
24cd51
+++ b/xmlsecurity/source/helper/ooxmlsecparser.cxx
24cd51
@@ -192,12 +192,12 @@ void SAL_CALL OOXMLSecParser::endElement(const OUString& rName)
24cd51
     }
24cd51
     else if (rName == "mdssi:Value")
24cd51
     {
24cd51
-        m_pXSecController->setDate(m_aMdssiValue);
24cd51
+        m_pXSecController->setDate("", m_aMdssiValue);
24cd51
         m_bInMdssiValue = false;
24cd51
     }
24cd51
     else if (rName == "SignatureComments")
24cd51
     {
24cd51
-        m_pXSecController->setDescription(m_aSignatureComments);
24cd51
+        m_pXSecController->setDescription("", m_aSignatureComments);
24cd51
         m_bInSignatureComments = false;
24cd51
     }
24cd51
     else if (rName == "X509IssuerName")
24cd51
diff --git a/xmlsecurity/source/helper/xsecctl.cxx b/xmlsecurity/source/helper/xsecctl.cxx
24cd51
index ab108d13c224..8d5ea68c768b 100644
24cd51
--- a/xmlsecurity/source/helper/xsecctl.cxx
24cd51
+++ b/xmlsecurity/source/helper/xsecctl.cxx
24cd51
@@ -819,7 +819,7 @@ void XSecController::exportSignature(
24cd51
                 pAttributeList = new SvXMLAttributeList();
24cd51
                 pAttributeList->AddAttribute(
24cd51
                     "Id",
24cd51
-                    signatureInfo.ouPropertyId);
24cd51
+                    signatureInfo.ouDateTimePropertyId);
24cd51
                 pAttributeList->AddAttribute(
24cd51
                     "Target",
24cd51
                     "#" + signatureInfo.ouSignatureId);
24cd51
diff --git a/xmlsecurity/source/helper/xsecparser.cxx b/xmlsecurity/source/helper/xsecparser.cxx
24cd51
index 5c92e5efa104..9cc9312b4d9f 100644
24cd51
--- a/xmlsecurity/source/helper/xsecparser.cxx
24cd51
+++ b/xmlsecurity/source/helper/xsecparser.cxx
24cd51
@@ -978,6 +978,9 @@ class XSecParser::XadesSigningCertificateContext
24cd51
 class XSecParser::XadesSigningTimeContext
24cd51
     : public XSecParser::Context
24cd51
 {
24cd51
+    private:
24cd51
+        OUString m_Value;
24cd51
+
24cd51
     public:
24cd51
         XadesSigningTimeContext(XSecParser & rParser,
24cd51
                 std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap)
24cd51
@@ -985,20 +988,14 @@ class XSecParser::XadesSigningTimeContext
24cd51
         {
24cd51
         }
24cd51
 
24cd51
-        virtual void StartElement(
24cd51
-            css::uno::Reference<css::xml::sax::XAttributeList> const& /*xAttrs*/) override
24cd51
-        {
24cd51
-            m_rParser.m_ouDate.clear();
24cd51
-        }
24cd51
-
24cd51
         virtual void EndElement() override
24cd51
         {
24cd51
-            m_rParser.m_pXSecController->setDate( m_rParser.m_ouDate );
24cd51
+            m_rParser.m_pXSecController->setDate("", m_Value);
24cd51
         }
24cd51
 
24cd51
         virtual void Characters(OUString const& rChars) override
24cd51
         {
24cd51
-            m_rParser.m_ouDate += rChars;
24cd51
+            m_Value += rChars;
24cd51
         }
24cd51
 };
24cd51
 
24cd51
@@ -1104,35 +1101,20 @@ class XSecParser::DcDateContext
24cd51
     : public XSecParser::Context
24cd51
 {
24cd51
     private:
24cd51
-        bool m_isIgnore = false;
24cd51
+        OUString & m_rValue;
24cd51
 
24cd51
     public:
24cd51
         DcDateContext(XSecParser & rParser,
24cd51
-                std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap)
24cd51
+                std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap,
24cd51
+                OUString & rValue)
24cd51
             : XSecParser::Context(rParser, std::move(pOldNamespaceMap))
24cd51
+            , m_rValue(rValue)
24cd51
         {
24cd51
         }
24cd51
 
24cd51
-        virtual void StartElement(
24cd51
-            css::uno::Reference<css::xml::sax::XAttributeList> const& /*xAttrs*/) override
24cd51
-        {
24cd51
-            m_isIgnore = !m_rParser.m_ouDate.isEmpty();
24cd51
-        }
24cd51
-
24cd51
-        virtual void EndElement() override
24cd51
-        {
24cd51
-            if (!m_isIgnore)
24cd51
-            {
24cd51
-                m_rParser.m_pXSecController->setDate( m_rParser.m_ouDate );
24cd51
-            }
24cd51
-        }
24cd51
-
24cd51
         virtual void Characters(OUString const& rChars) override
24cd51
         {
24cd51
-            if (!m_isIgnore)
24cd51
-            {
24cd51
-                m_rParser.m_ouDate += rChars;
24cd51
-            }
24cd51
+            m_rValue += rChars;
24cd51
         }
24cd51
 };
24cd51
 
24cd51
@@ -1140,29 +1122,32 @@ class XSecParser::DcDescriptionContext
24cd51
     : public XSecParser::Context
24cd51
 {
24cd51
     private:
24cd51
-        OUString m_Value;
24cd51
+        OUString & m_rValue;
24cd51
 
24cd51
     public:
24cd51
         DcDescriptionContext(XSecParser & rParser,
24cd51
-                std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap)
24cd51
+                std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap,
24cd51
+                OUString & rValue)
24cd51
             : XSecParser::Context(rParser, std::move(pOldNamespaceMap))
24cd51
+            , m_rValue(rValue)
24cd51
         {
24cd51
         }
24cd51
 
24cd51
-        virtual void EndElement() override
24cd51
-        {
24cd51
-            m_rParser.m_pXSecController->setDescription(m_Value);
24cd51
-        }
24cd51
-
24cd51
         virtual void Characters(OUString const& rChars) override
24cd51
         {
24cd51
-            m_Value += rChars;
24cd51
+            m_rValue += rChars;
24cd51
         }
24cd51
 };
24cd51
 
24cd51
 class XSecParser::DsSignaturePropertyContext
24cd51
     : public XSecParser::Context
24cd51
 {
24cd51
+    private:
24cd51
+        enum class SignatureProperty { Unknown, Date, Description };
24cd51
+        SignatureProperty m_Property = SignatureProperty::Unknown;
24cd51
+        OUString m_Id;
24cd51
+        OUString m_Value;
24cd51
+
24cd51
     public:
24cd51
         DsSignaturePropertyContext(XSecParser & rParser,
24cd51
                 std::unique_ptr<SvXMLNamespaceMap> pOldNamespaceMap)
24cd51
@@ -1173,10 +1158,22 @@ class XSecParser::DsSignaturePropertyContext
24cd51
         virtual void StartElement(
24cd51
             css::uno::Reference<css::xml::sax::XAttributeList> const& xAttrs) override
24cd51
         {
24cd51
-            OUString const ouIdAttr(m_rParser.HandleIdAttr(xAttrs));
24cd51
-            if (!ouIdAttr.isEmpty())
24cd51
+            m_Id = m_rParser.HandleIdAttr(xAttrs);
24cd51
+        }
24cd51
+
24cd51
+        virtual void EndElement() override
24cd51
+        {
24cd51
+            switch (m_Property)
24cd51
             {
24cd51
-                m_rParser.m_pXSecController->setPropertyId( ouIdAttr );
24cd51
+                case SignatureProperty::Unknown:
24cd51
+                    SAL_INFO("xmlsecurity.helper", "Unknown property in ds:Object ignored");
24cd51
+                    break;
24cd51
+                case SignatureProperty::Date:
24cd51
+                    m_rParser.m_pXSecController->setDate(m_Id, m_Value);
24cd51
+                    break;
24cd51
+                case SignatureProperty::Description:
24cd51
+                    m_rParser.m_pXSecController->setDescription(m_Id, m_Value);
24cd51
+                    break;
24cd51
             }
24cd51
         }
24cd51
 
24cd51
@@ -1186,11 +1183,13 @@ class XSecParser::DsSignaturePropertyContext
24cd51
         {
24cd51
             if (nNamespace == XML_NAMESPACE_DC && rName == "date")
24cd51
             {
24cd51
-                return std::make_unique<DcDateContext>(m_rParser, std::move(pOldNamespaceMap));
24cd51
+                m_Property = SignatureProperty::Date;
24cd51
+                return std::make_unique<DcDateContext>(m_rParser, std::move(pOldNamespaceMap), m_Value);
24cd51
             }
24cd51
             if (nNamespace == XML_NAMESPACE_DC && rName == "description")
24cd51
             {
24cd51
-                return std::make_unique<DcDescriptionContext>(m_rParser, std::move(pOldNamespaceMap));
24cd51
+                m_Property = SignatureProperty::Description;
24cd51
+                return std::make_unique<DcDescriptionContext>(m_rParser, std::move(pOldNamespaceMap), m_Value);
24cd51
             }
24cd51
             return XSecParser::Context::CreateChildContext(std::move(pOldNamespaceMap), nNamespace, rName);
24cd51
         }
24cd51
diff --git a/xmlsecurity/source/helper/xsecparser.hxx b/xmlsecurity/source/helper/xsecparser.hxx
24cd51
index 93efcb766e3e..7a0eb08bca28 100644
24cd51
--- a/xmlsecurity/source/helper/xsecparser.hxx
24cd51
+++ b/xmlsecurity/source/helper/xsecparser.hxx
24cd51
@@ -97,12 +97,6 @@ private:
24cd51
     class DsSignatureContext;
24cd51
     class DsigSignaturesContext;
24cd51
 
24cd51
-    /*
24cd51
-     * the following members are used to reserve the signature information,
24cd51
-     * including X509IssuerName, X509SerialNumber, and X509Certificate,etc.
24cd51
-     */
24cd51
-    OUString m_ouDate;
24cd51
-
24cd51
     std::stack<std::unique_ptr<Context>> m_ContextStack;
24cd51
     std::unique_ptr<SvXMLNamespaceMap> m_pNamespaceMap;
24cd51
 
24cd51
diff --git a/xmlsecurity/source/helper/xsecsign.cxx b/xmlsecurity/source/helper/xsecsign.cxx
24cd51
index 4d1b89949feb..5ed23281f083 100644
24cd51
--- a/xmlsecurity/source/helper/xsecsign.cxx
24cd51
+++ b/xmlsecurity/source/helper/xsecsign.cxx
24cd51
@@ -132,8 +132,8 @@ cssu::Reference< cssxc::sax::XReferenceResolvedListener > XSecController::prepar
24cd51
     if (nStorageFormat != embed::StorageFormats::OFOPXML)
24cd51
     {
24cd51
         internalSignatureInfor.signatureInfor.ouSignatureId = createId();
24cd51
-        internalSignatureInfor.signatureInfor.ouPropertyId = createId();
24cd51
-        internalSignatureInfor.addReference(SignatureReferenceType::SAMEDOCUMENT, digestID, internalSignatureInfor.signatureInfor.ouPropertyId, -1, OUString() );
24cd51
+        internalSignatureInfor.signatureInfor.ouDateTimePropertyId = createId();
24cd51
+        internalSignatureInfor.addReference(SignatureReferenceType::SAMEDOCUMENT, digestID, internalSignatureInfor.signatureInfor.ouDateTimePropertyId, -1, OUString() );
24cd51
         size++;
24cd51
 
24cd51
         if (bXAdESCompliantIfODF)
24cd51
diff --git a/xmlsecurity/source/helper/xsecverify.cxx b/xmlsecurity/source/helper/xsecverify.cxx
24cd51
index 1f7fa9ac8ca8..5f5840334254 100644
24cd51
--- a/xmlsecurity/source/helper/xsecverify.cxx
24cd51
+++ b/xmlsecurity/source/helper/xsecverify.cxx
24cd51
@@ -321,7 +321,7 @@ void XSecController::setGpgOwner( OUString const & ouGpgOwner )
24cd51
     isi.signatureInfor.ouGpgOwner = ouGpgOwner;
24cd51
 }
24cd51
 
24cd51
-void XSecController::setDate( OUString const & ouDate )
24cd51
+void XSecController::setDate(OUString const& rId, OUString const& ouDate)
24cd51
 {
24cd51
     if (m_vInternalSignatureInformations.empty())
24cd51
     {
24cd51
@@ -329,17 +329,31 @@ void XSecController::setDate( OUString const & ouDate )
24cd51
         return;
24cd51
     }
24cd51
     InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
24cd51
+    // there may be multiple timestamps in a signature - check them for consistency
24cd51
+    if (!isi.signatureInfor.ouDateTime.isEmpty()
24cd51
+        && isi.signatureInfor.ouDateTime != ouDate)
24cd51
+    {
24cd51
+        isi.signatureInfor.hasInconsistentSigningTime = true;
24cd51
+    }
24cd51
     (void)utl::ISO8601parseDateTime( ouDate, isi.signatureInfor.stDateTime);
24cd51
     isi.signatureInfor.ouDateTime = ouDate;
24cd51
+    if (!rId.isEmpty())
24cd51
+    {
24cd51
+        isi.signatureInfor.ouDateTimePropertyId = rId;
24cd51
+    }
24cd51
 }
24cd51
 
24cd51
-void XSecController::setDescription(const OUString& rDescription)
24cd51
+void XSecController::setDescription(OUString const& rId, OUString const& rDescription)
24cd51
 {
24cd51
     if (m_vInternalSignatureInformations.empty())
24cd51
         return;
24cd51
 
24cd51
     InternalSignatureInformation& rInformation = m_vInternalSignatureInformations.back();
24cd51
     rInformation.signatureInfor.ouDescription = rDescription;
24cd51
+    if (!rId.isEmpty())
24cd51
+    {
24cd51
+        rInformation.signatureInfor.ouDescriptionPropertyId = rId;
24cd51
+    }
24cd51
 }
24cd51
 
24cd51
 void XSecController::setSignatureBytes(const uno::Sequence<sal_Int8>& rBytes)
24cd51
@@ -433,27 +447,6 @@ void XSecController::setId( OUString const & ouId )
24cd51
     isi.signatureInfor.ouSignatureId = ouId;
24cd51
 }
24cd51
 
24cd51
-void XSecController::setPropertyId( OUString const & ouPropertyId )
24cd51
-{
24cd51
-    if (m_vInternalSignatureInformations.empty())
24cd51
-    {
24cd51
-        SAL_INFO("xmlsecurity.helper","XSecController::setPropertyId: no signature");
24cd51
-        return;
24cd51
-    }
24cd51
-    InternalSignatureInformation &isi = m_vInternalSignatureInformations.back();
24cd51
-
24cd51
-    if (isi.signatureInfor.ouPropertyId.isEmpty())
24cd51
-    {
24cd51
-        // <SignatureProperty> ID attribute is for the date.
24cd51
-        isi.signatureInfor.ouPropertyId = ouPropertyId;
24cd51
-    }
24cd51
-    else
24cd51
-    {
24cd51
-        // <SignatureProperty> ID attribute is for the description.
24cd51
-        isi.signatureInfor.ouDescriptionPropertyId = ouPropertyId;
24cd51
-    }
24cd51
-}
24cd51
-
24cd51
 /* public: for signature verify */
24cd51
 void XSecController::collectToVerify( const OUString& referenceId )
24cd51
 {
24cd51
-- 
24cd51
2.32.0
24cd51