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

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