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