Blame SOURCES/0002-CVE-2022-26307-make-hash-encoding-match-decoding.patch

0c03dd
From 780c42cdd8006dc60e281be2fe6566f101e909bc Mon Sep 17 00:00:00 2001
0c03dd
From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= <caolanm@redhat.com>
0c03dd
Date: Mon, 21 Mar 2022 20:58:34 +0000
0c03dd
Subject: [PATCH 2/5] CVE-2022-26307 make hash encoding match decoding
0c03dd
0c03dd
Seeing as old versions of the hash may be in the users config, add a
0c03dd
StorageVersion field to the office config Passwords section which
0c03dd
defaults to 0 to indicate the old hash is in use.
0c03dd
0c03dd
Try the old varient when StorageVersion is 0. When a new encoded master
0c03dd
password it set write StorageVersion of 1 to indicate a new hash is in
0c03dd
use and use the new style when StorageVersion is 1.
0c03dd
0c03dd
Change-Id: I3174c37a5891bfc849984e0ec5c2c392b9c6e7b1
0c03dd
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132080
0c03dd
Tested-by: Jenkins
0c03dd
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
0c03dd
(cherry picked from commit e890f54dbac57f3ab5acf4fbd31222095d3e8ab6)
0c03dd
---
0c03dd
 .../schema/org/openoffice/Office/Common.xcs   |  6 +++
0c03dd
 .../passwordcontainer/passwordcontainer.cxx   | 45 +++++++++++++++++--
0c03dd
 .../passwordcontainer/passwordcontainer.hxx   |  6 +++
0c03dd
 uui/source/iahndl-authentication.cxx          |  5 ++-
0c03dd
 4 files changed, 57 insertions(+), 5 deletions(-)
0c03dd
0c03dd
diff --git a/officecfg/registry/schema/org/openoffice/Office/Common.xcs b/officecfg/registry/schema/org/openoffice/Office/Common.xcs
0c03dd
index 9097c23c3c6a..922efc33cca7 100644
0c03dd
--- a/officecfg/registry/schema/org/openoffice/Office/Common.xcs
0c03dd
+++ b/officecfg/registry/schema/org/openoffice/Office/Common.xcs
0c03dd
@@ -942,6 +942,12 @@
0c03dd
         </info>
0c03dd
         <value>false</value>
0c03dd
       </prop>
0c03dd
+      <prop oor:name="StorageVersion" oor:type="xs:int" oor:nillable="false">
0c03dd
+        <info>
0c03dd
+          <desc>Specifies what version of encoding scheme the password container uses.</desc>
0c03dd
+        </info>
0c03dd
+        <value>0</value>
0c03dd
+      </prop>
0c03dd
       <prop oor:name="HasMaster" oor:type="xs:boolean" oor:nillable="false">
0c03dd
         <info>
0c03dd
           <desc>Specifies if there is a valid master password.</desc>
0c03dd
diff --git a/svl/source/passwordcontainer/passwordcontainer.cxx b/svl/source/passwordcontainer/passwordcontainer.cxx
0c03dd
index 51fb129cddb1..b674844f91d3 100644
0c03dd
--- a/svl/source/passwordcontainer/passwordcontainer.cxx
0c03dd
+++ b/svl/source/passwordcontainer/passwordcontainer.cxx
0c03dd
@@ -17,7 +17,6 @@
0c03dd
  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
0c03dd
  */
0c03dd
 
0c03dd
-
0c03dd
 #include "passwordcontainer.hxx"
0c03dd
 
0c03dd
 #include <cppuhelper/factory.hxx>
0c03dd
@@ -259,6 +258,23 @@ bool StorageItem::useStorage()
0c03dd
     return aResult;
0c03dd
 }
0c03dd
 
0c03dd
+sal_Int32 StorageItem::getStorageVersion()
0c03dd
+{
0c03dd
+    Sequence<OUString> aNodeNames { "StorageVersion" };
0c03dd
+
0c03dd
+    Sequence< Any > aPropertyValues = ConfigItem::GetProperties( aNodeNames );
0c03dd
+
0c03dd
+    if( aPropertyValues.getLength() != aNodeNames.getLength() )
0c03dd
+    {
0c03dd
+        OSL_FAIL( "Problems during reading" );
0c03dd
+        return 0;
0c03dd
+    }
0c03dd
+
0c03dd
+    sal_Int32 nResult = 0;
0c03dd
+    aPropertyValues[0] >>= nResult;
0c03dd
+
0c03dd
+    return nResult;
0c03dd
+}
0c03dd
 
0c03dd
 bool StorageItem::getEncodedMP( OUString& aResult )
0c03dd
 {
0c03dd
@@ -291,15 +307,17 @@ bool StorageItem::getEncodedMP( OUString& aResult )
0c03dd
 
0c03dd
 void StorageItem::setEncodedMP( const OUString& aEncoded, bool bAcceptEmpty )
0c03dd
 {
0c03dd
-    Sequence< OUString > sendNames(2);
0c03dd
-    Sequence< uno::Any > sendVals(2);
0c03dd
+    Sequence< OUString > sendNames(3);
0c03dd
+    Sequence< uno::Any > sendVals(3);
0c03dd
 
0c03dd
     sendNames[0] = "HasMaster";
0c03dd
     sendNames[1] = "Master";
0c03dd
+    sendNames[2] = "StorageVersion";
0c03dd
 
0c03dd
     bool bHasMaster = ( !aEncoded.isEmpty() || bAcceptEmpty );
0c03dd
     sendVals[0] <<= bHasMaster;
0c03dd
     sendVals[1] <<= aEncoded;
0c03dd
+    sendVals[2] <<= nCurrentStorageVersion;
0c03dd
 
0c03dd
     ConfigItem::SetModified();
0c03dd
     ConfigItem::PutProperties( sendNames, sendVals );
0c03dd
@@ -800,6 +818,18 @@ OUString PasswordContainer::RequestPasswordFromUser( PasswordRequestMode aRMode,
0c03dd
     return aResult;
0c03dd
 }
0c03dd
 
0c03dd
+// Mangle the key to match an old bug
0c03dd
+static OUString ReencodeAsOldHash(const OUString& rPass)
0c03dd
+{
0c03dd
+    OUStringBuffer aBuffer;
0c03dd
+    for (int ind = 0; ind < RTL_DIGEST_LENGTH_MD5; ++ind)
0c03dd
+    {
0c03dd
+        unsigned char i = static_cast<char>(rPass.copy(ind * 2, 2).toUInt32(16));
0c03dd
+        aBuffer.append(static_cast< sal_Unicode >('a' + (i >> 4)));
0c03dd
+        aBuffer.append(static_cast< sal_Unicode >('a' + (i & 15)));
0c03dd
+    }
0c03dd
+    return aBuffer.makeStringAndClear();
0c03dd
+}
0c03dd
 
0c03dd
 OUString const & PasswordContainer::GetMasterPassword( const Reference< XInteractionHandler >& aHandler )
0c03dd
 {
0c03dd
@@ -838,6 +868,9 @@ OUString const & PasswordContainer::GetMasterPassword( const Reference< XInterac
0c03dd
                     }
0c03dd
                     else
0c03dd
                     {
0c03dd
+                        if (m_pStorageFile->getStorageVersion() == 0)
0c03dd
+                            aPass = ReencodeAsOldHash(aPass);
0c03dd
+
0c03dd
                         std::vector< OUString > aRM( DecodePasswords( aEncodedMP, aPass, aRMode ) );
0c03dd
                         if( aRM.empty() || aPass != aRM[0] )
0c03dd
                         {
0c03dd
@@ -1042,6 +1075,12 @@ sal_Bool SAL_CALL PasswordContainer::authorizateWithMasterPassword( const uno::R
0c03dd
 
0c03dd
                 do {
0c03dd
                     aPass = RequestPasswordFromUser( aRMode, xTmpHandler );
0c03dd
+
0c03dd
+                    if (!aPass.isEmpty() && m_pStorageFile->getStorageVersion() == 0)
0c03dd
+                    {
0c03dd
+                        aPass = ReencodeAsOldHash(aPass);
0c03dd
+                    }
0c03dd
+
0c03dd
                     bResult = ( !aPass.isEmpty() && aPass == m_aMasterPasswd );
0c03dd
                     aRMode = PasswordRequestMode_PASSWORD_REENTER; // further questions with error notification
0c03dd
                 } while( !bResult && !aPass.isEmpty() );
0c03dd
diff --git a/svl/source/passwordcontainer/passwordcontainer.hxx b/svl/source/passwordcontainer/passwordcontainer.hxx
0c03dd
index 46ffec888602..bf43b5903602 100644
0c03dd
--- a/svl/source/passwordcontainer/passwordcontainer.hxx
0c03dd
+++ b/svl/source/passwordcontainer/passwordcontainer.hxx
0c03dd
@@ -168,6 +168,10 @@ public:
0c03dd
 typedef ::std::pair< const OUString, ::std::vector< NamePassRecord > > PairUrlRecord;
0c03dd
 typedef ::std::map< OUString, ::std::vector< NamePassRecord > > PassMap;
0c03dd
 
0c03dd
+// org.openoffice.Office.Common/Passwords/StorageVersion bump if details of
0c03dd
+// how password details are saved changes. Enables migration from previous
0c03dd
+// schemes.
0c03dd
+constexpr sal_Int32 nCurrentStorageVersion = 1;
0c03dd
 
0c03dd
 class PasswordContainer;
0c03dd
 
0c03dd
@@ -196,6 +200,8 @@ public:
0c03dd
     void remove( const OUString& url, const OUString& rec );
0c03dd
     void clear();
0c03dd
 
0c03dd
+    sal_Int32 getStorageVersion();
0c03dd
+
0c03dd
     bool getEncodedMP( OUString& aResult );
0c03dd
     void setEncodedMP( const OUString& aResult, bool bAcceptEmpty = false );
0c03dd
     void setUseStorage( bool bUse );
0c03dd
diff --git a/uui/source/iahndl-authentication.cxx b/uui/source/iahndl-authentication.cxx
0c03dd
index ad975d3f9ae7..951f0b8a1c6b 100644
0c03dd
--- a/uui/source/iahndl-authentication.cxx
0c03dd
+++ b/uui/source/iahndl-authentication.cxx
0c03dd
@@ -436,8 +436,9 @@ executeMasterPasswordDialog(
0c03dd
     OUStringBuffer aBuffer;
0c03dd
     for (sal_uInt8 i : aKey)
0c03dd
     {
0c03dd
-        aBuffer.append(static_cast< sal_Unicode >('a' + (i >> 4)));
0c03dd
-        aBuffer.append(static_cast< sal_Unicode >('a' + (i & 15)));
0c03dd
+        // match PasswordContainer::DecodePasswords aMasterPasswd.copy(index * 2, 2).toUInt32(16));
0c03dd
+        aBuffer.append(OUString::number(i >> 4, 16));
0c03dd
+        aBuffer.append(OUString::number(i & 15, 16));
0c03dd
     }
0c03dd
     rInfo.SetPassword(aBuffer.makeStringAndClear());
0c03dd
 }
0c03dd
-- 
0c03dd
2.37.3
0c03dd