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

461f4a
From 61f8673fb44150bd629d88f6626aff8d5b026449 Mon Sep 17 00:00:00 2001
461f4a
From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= <caolanm@redhat.com>
461f4a
Date: Mon, 21 Mar 2022 20:58:34 +0000
461f4a
Subject: [PATCH] make hash encoding match decoding
461f4a
461f4a
Seeing as old versions of the hash may be in the users config, add a
461f4a
StorageVersion field to the office config Passwords section which
461f4a
defaults to 0 to indicate the old hash is in use.
461f4a
461f4a
Try the old varient when StorageVersion is 0. When a new encoded master
461f4a
password it set write StorageVersion of 1 to indicate a new hash is in
461f4a
use and use the new style when StorageVersion is 1.
461f4a
461f4a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132080
461f4a
Tested-by: Jenkins
461f4a
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
461f4a
(cherry picked from commit e890f54dbac57f3ab5acf4fbd31222095d3e8ab6)
461f4a
461f4a
svl: fix crash if user cancels/closes master password dialog
461f4a
461f4a
(regression from d7ba5614d90381d68f880ca7e7c5ef8bbb1b1c43)
461f4a
461f4a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/133932
461f4a
Tested-by: Jenkins
461f4a
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
461f4a
(cherry picked from commit bbb8617ece6d946957c2eb96287081029bce530f)
461f4a
461f4a
Change-Id: I3174c37a5891bfc849984e0ec5c2c392b9c6e7b1
461f4a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/133905
461f4a
Tested-by: Michael Stahl <michael.stahl@allotropia.de>
461f4a
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
461f4a
---
461f4a
 .../schema/org/openoffice/Office/Common.xcs   |  6 +++
461f4a
 .../passwordcontainer/passwordcontainer.cxx   | 47 ++++++++++++++++++-
461f4a
 .../passwordcontainer/passwordcontainer.hxx   |  6 +++
461f4a
 uui/source/iahndl-authentication.cxx          |  5 +-
461f4a
 4 files changed, 60 insertions(+), 4 deletions(-)
461f4a
461f4a
diff --git a/officecfg/registry/schema/org/openoffice/Office/Common.xcs b/officecfg/registry/schema/org/openoffice/Office/Common.xcs
461f4a
index b317f616deeb..b033b29b60d7 100644
461f4a
--- a/officecfg/registry/schema/org/openoffice/Office/Common.xcs
461f4a
+++ b/officecfg/registry/schema/org/openoffice/Office/Common.xcs
461f4a
@@ -911,6 +911,12 @@
461f4a
         </info>
461f4a
         <value>false</value>
461f4a
       </prop>
461f4a
+      <prop oor:name="StorageVersion" oor:type="xs:int" oor:nillable="false">
461f4a
+        <info>
461f4a
+          <desc>Specifies what version of encoding scheme the password container uses.</desc>
461f4a
+        </info>
461f4a
+        <value>0</value>
461f4a
+      </prop>
461f4a
       <prop oor:name="HasMaster" oor:type="xs:boolean" oor:nillable="false">
461f4a
         <info>
461f4a
           <desc>Specifies if there is a valid master password.</desc>
461f4a
diff --git a/svl/source/passwordcontainer/passwordcontainer.cxx b/svl/source/passwordcontainer/passwordcontainer.cxx
461f4a
index 02947cd3892c..ff0b40df4016 100644
461f4a
--- a/svl/source/passwordcontainer/passwordcontainer.cxx
461f4a
+++ b/svl/source/passwordcontainer/passwordcontainer.cxx
461f4a
@@ -17,6 +17,8 @@
461f4a
  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
461f4a
  */
461f4a
 
461f4a
+#include <sal/config.h>
461f4a
+#include <sal/log.hxx>
461f4a
 
461f4a
 #include "passwordcontainer.hxx"
461f4a
 
461f4a
@@ -259,6 +261,23 @@ bool StorageItem::useStorage()
461f4a
     return aResult;
461f4a
 }
461f4a
 
461f4a
+sal_Int32 StorageItem::getStorageVersion()
461f4a
+{
461f4a
+    Sequence<OUString> aNodeNames { "StorageVersion" };
461f4a
+
461f4a
+    Sequence< Any > aPropertyValues = ConfigItem::GetProperties( aNodeNames );
461f4a
+
461f4a
+    if( aPropertyValues.getLength() != aNodeNames.getLength() )
461f4a
+    {
461f4a
+        OSL_FAIL( "Problems during reading" );
461f4a
+        return 0;
461f4a
+    }
461f4a
+
461f4a
+    sal_Int32 nResult = 0;
461f4a
+    aPropertyValues[0] >>= nResult;
461f4a
+
461f4a
+    return nResult;
461f4a
+}
461f4a
 
461f4a
 bool StorageItem::getEncodedMP( OUString& aResult )
461f4a
 {
461f4a
@@ -291,15 +310,17 @@ bool StorageItem::getEncodedMP( OUString& aResult )
461f4a
 
461f4a
 void StorageItem::setEncodedMP( const OUString& aEncoded, bool bAcceptEmpty )
461f4a
 {
461f4a
-    Sequence< OUString > sendNames(2);
461f4a
-    Sequence< uno::Any > sendVals(2);
461f4a
+    Sequence< OUString > sendNames(3);
461f4a
+    Sequence< uno::Any > sendVals(3);
461f4a
 
461f4a
     sendNames[0] = "HasMaster";
461f4a
     sendNames[1] = "Master";
461f4a
+    sendNames[2] = "StorageVersion";
461f4a
 
461f4a
     bool bHasMaster = ( !aEncoded.isEmpty() || bAcceptEmpty );
461f4a
     sendVals[0] <<= bHasMaster;
461f4a
     sendVals[1] <<= aEncoded;
461f4a
+    sendVals[2] <<= nCurrentStorageVersion;
461f4a
 
461f4a
     ConfigItem::SetModified();
461f4a
     ConfigItem::PutProperties( sendNames, sendVals );
461f4a
@@ -800,6 +821,18 @@ OUString PasswordContainer::RequestPasswordFromUser( PasswordRequestMode aRMode,
461f4a
     return aResult;
461f4a
 }
461f4a
 
461f4a
+// Mangle the key to match an old bug
461f4a
+static OUString ReencodeAsOldHash(const OUString& rPass)
461f4a
+{
461f4a
+    OUStringBuffer aBuffer;
461f4a
+    for (int ind = 0; ind < RTL_DIGEST_LENGTH_MD5; ++ind)
461f4a
+    {
461f4a
+        unsigned char i = static_cast<char>(rPass.copy(ind * 2, 2).toUInt32(16));
461f4a
+        aBuffer.append(static_cast< sal_Unicode >('a' + (i >> 4)));
461f4a
+        aBuffer.append(static_cast< sal_Unicode >('a' + (i & 15)));
461f4a
+    }
461f4a
+    return aBuffer.makeStringAndClear();
461f4a
+}
461f4a
 
461f4a
 OUString const & PasswordContainer::GetMasterPassword( const Reference< XInteractionHandler >& aHandler )
461f4a
 {
461f4a
@@ -838,6 +871,9 @@ OUString const & PasswordContainer::GetMasterPassword( const Reference< XInterac
461f4a
                     }
461f4a
                     else
461f4a
                     {
461f4a
+                        if (m_pStorageFile->getStorageVersion() == 0)
461f4a
+                            aPass = ReencodeAsOldHash(aPass);
461f4a
+
461f4a
                         std::vector< OUString > aRM( DecodePasswords( aEncodedMP, aPass, aRMode ) );
461f4a
                         if( aRM.empty() || aPass != aRM[0] )
461f4a
                         {
461f4a
@@ -1042,6 +1078,13 @@ sal_Bool SAL_CALL PasswordContainer::authorizateWithMasterPassword( const uno::R
461f4a
 
461f4a
                 do {
461f4a
                     aPass = RequestPasswordFromUser( aRMode, xTmpHandler );
461f4a
+
461f4a
+
461f4a
+                    if (!aPass.isEmpty() && m_pStorageFile->getStorageVersion() == 0)
461f4a
+                    {
461f4a
+                        aPass = ReencodeAsOldHash(aPass);
461f4a
+                    }
461f4a
+
461f4a
                     bResult = ( !aPass.isEmpty() && aPass == m_aMasterPasswd );
461f4a
                     aRMode = PasswordRequestMode_PASSWORD_REENTER; // further questions with error notification
461f4a
                 } while( !bResult && !aPass.isEmpty() );
461f4a
diff --git a/svl/source/passwordcontainer/passwordcontainer.hxx b/svl/source/passwordcontainer/passwordcontainer.hxx
461f4a
index 09fb7e03629d..cf5c717d0c9e 100644
461f4a
--- a/svl/source/passwordcontainer/passwordcontainer.hxx
461f4a
+++ b/svl/source/passwordcontainer/passwordcontainer.hxx
461f4a
@@ -167,6 +167,10 @@ public:
461f4a
 typedef ::std::pair< const OUString, ::std::vector< NamePassRecord > > PairUrlRecord;
461f4a
 typedef ::std::map< OUString, ::std::vector< NamePassRecord > > PassMap;
461f4a
 
461f4a
+// org.openoffice.Office.Common/Passwords/StorageVersion bump if details of
461f4a
+// how password details are saved changes. Enables migration from previous
461f4a
+// schemes.
461f4a
+constexpr sal_Int32 nCurrentStorageVersion = 1;
461f4a
 
461f4a
 class PasswordContainer;
461f4a
 
461f4a
@@ -195,6 +199,8 @@ public:
461f4a
     void remove( const OUString& url, const OUString& rec );
461f4a
     void clear();
461f4a
 
461f4a
+    sal_Int32 getStorageVersion();
461f4a
+
461f4a
     bool getEncodedMP( OUString& aResult );
461f4a
     void setEncodedMP( const OUString& aResult, bool bAcceptEnmpty = false );
461f4a
     void setUseStorage( bool bUse );
461f4a
diff --git a/uui/source/iahndl-authentication.cxx b/uui/source/iahndl-authentication.cxx
461f4a
index 4835a485dd2a..5764e62cb1c6 100644
461f4a
--- a/uui/source/iahndl-authentication.cxx
461f4a
+++ b/uui/source/iahndl-authentication.cxx
461f4a
@@ -436,8 +436,9 @@ executeMasterPasswordDialog(
461f4a
     OUStringBuffer aBuffer;
461f4a
     for (sal_uInt8 i : aKey)
461f4a
     {
461f4a
-        aBuffer.append(static_cast< sal_Unicode >('a' + (i >> 4)));
461f4a
-        aBuffer.append(static_cast< sal_Unicode >('a' + (i & 15)));
461f4a
+        // match PasswordContainer::DecodePasswords aMasterPasswd.copy(index * 2, 2).toUInt32(16));
461f4a
+        aBuffer.append(OUString::number(i >> 4, 16));
461f4a
+        aBuffer.append(OUString::number(i & 15, 16));
461f4a
     }
461f4a
     rInfo.SetPassword(aBuffer.makeStringAndClear());
461f4a
 }
461f4a
-- 
461f4a
2.37.1
461f4a