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

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