8228c6
From 96b088a62174a70441ebe959495756e9d86203a2 Mon Sep 17 00:00:00 2001
8228c6
From: Stephan Bergmann <sbergman@redhat.com>
8228c6
Date: Thu, 24 Sep 2020 14:51:16 +0200
8228c6
Subject: [PATCH] Fix endianness issues in OOX crypto routines
8228c6
8228c6
...without which CppunitTest_sw_ooxmlencryption failed on (big-endian) s390x:
8228c6
8228c6
* The 32-bit segment counter in AgileEngine::de-/encrypt apparently needs to be
8228c6
  stored in LSB format (at least, if it is, CppunitTest_sw_ooxmlencryption
8228c6
  ultimately succeeded, whereas otherwise it failed).
8228c6
8228c6
* The UTF-16 string in Standard2007Engine::calculateEncryptionKey apparently
8228c6
  needs to be in LSB format (at least, if it is, CppunitTest_sw_ooxmlencryption
8228c6
  ultimately succeeded, whereas otherwise it failed).
8228c6
8228c6
* The various 32-bit values in the EncryptionStandardHeader and
8228c6
  EncryptionVerifierAES data structures apparently need to be written out in LSB
8228c6
  format in Standard2007Engine::writeEncryptionInfo, given that they are always
8228c6
  read in LSB format in Standard2007Engine::readEncryptionInfo.
8228c6
8228c6
Change-Id: I3a1efbfe324b1bbd539b88dc5d40bb44f9676ffa
8228c6
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103315
8228c6
Tested-by: Jenkins
8228c6
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
8228c6
(cherry picked from commit 646a69757b928aeaf6e0d0d41c4b30c02803a3a3)
8228c6
---
8228c6
 oox/source/crypto/AgileEngine.cxx        | 16 +++++++++-----
8228c6
 oox/source/crypto/Standard2007Engine.cxx | 28 +++++++++++++++++-------
8228c6
 2 files changed, 30 insertions(+), 14 deletions(-)
8228c6
8228c6
diff --git a/oox/source/crypto/AgileEngine.cxx b/oox/source/crypto/AgileEngine.cxx
8228c6
index 7c2a0e9c93d2..0fc972bf2ca5 100644
8228c6
--- a/oox/source/crypto/AgileEngine.cxx
8228c6
+++ b/oox/source/crypto/AgileEngine.cxx
8228c6
@@ -457,9 +457,11 @@ bool AgileEngine::decrypt(BinaryXInputStream& aInputStream,
8228c6
 
8228c6
     while ((inputLength = aInputStream.readMemory(inputBuffer.data(), inputBuffer.size())) > 0)
8228c6
     {
8228c6
-        sal_uInt8* segmentBegin = reinterpret_cast<sal_uInt8*>(&segment);
8228c6
-        sal_uInt8* segmentEnd   = segmentBegin + sizeof(segment);
8228c6
-        std::copy(segmentBegin, segmentEnd, saltWithBlockKey.begin() + saltSize);
8228c6
+        auto p = saltWithBlockKey.begin() + saltSize;
8228c6
+        p[0] = segment & 0xFF;
8228c6
+        p[1] = (segment >> 8) & 0xFF;
8228c6
+        p[2] = (segment >> 16) & 0xFF;
8228c6
+        p[3] = segment >> 24;
8228c6
 
8228c6
         hashCalc(hash, saltWithBlockKey, mInfo.hashAlgorithm);
8228c6
 
8228c6
@@ -800,9 +802,11 @@ void AgileEngine::encrypt(css::uno::Reference<css::io::XInputStream> &  rxInputS
8228c6
                         inputLength : oox::core::roundUp(inputLength, sal_uInt32(mInfo.blockSize));
8228c6
 
8228c6
         // Update Key
8228c6
-        sal_uInt8* segmentBegin = reinterpret_cast<sal_uInt8*>(&nSegment);
8228c6
-        sal_uInt8* segmentEnd   = segmentBegin + nSegmentByteSize;
8228c6
-        std::copy(segmentBegin, segmentEnd, saltWithBlockKey.begin() + saltSize);
8228c6
+        auto p = saltWithBlockKey.begin() + saltSize;
8228c6
+        p[0] = nSegment & 0xFF;
8228c6
+        p[1] = (nSegment >> 8) & 0xFF;
8228c6
+        p[2] = (nSegment >> 16) & 0xFF;
8228c6
+        p[3] = nSegment >> 24;
8228c6
 
8228c6
         hashCalc(hash, saltWithBlockKey, mInfo.hashAlgorithm);
8228c6
 
8228c6
diff --git a/oox/source/crypto/Standard2007Engine.cxx b/oox/source/crypto/Standard2007Engine.cxx
8228c6
index 38c4e03baf15..e96fc8f841f2 100644
8228c6
--- a/oox/source/crypto/Standard2007Engine.cxx
8228c6
+++ b/oox/source/crypto/Standard2007Engine.cxx
8228c6
@@ -79,12 +79,12 @@ bool Standard2007Engine::calculateEncryptionKey(const OUString& rPassword)
8228c6
     std::vector<sal_uInt8> initialData(saltSize + passwordByteLength);
8228c6
     std::copy(saltArray, saltArray + saltSize, initialData.begin());
8228c6
 
8228c6
-    const sal_uInt8* passwordByteArray = reinterpret_cast<const sal_uInt8*>(rPassword.getStr());
8228c6
-
8228c6
-    std::copy(
8228c6
-        passwordByteArray,
8228c6
-        passwordByteArray + passwordByteLength,
8228c6
-        initialData.begin() + saltSize);
8228c6
+    auto p = initialData.begin() + saltSize;
8228c6
+    for (sal_Int32 i = 0; i != rPassword.getLength(); ++i) {
8228c6
+        auto c = rPassword[i];
8228c6
+        *p++ = c & 0xFF;
8228c6
+        *p++ = c >> 8;
8228c6
+    }
8228c6
 
8228c6
     // use "hash" vector for result of sha1 hashing
8228c6
     // calculate SHA1 hash of initialData
8228c6
@@ -223,11 +223,23 @@ void Standard2007Engine::writeEncryptionInfo(BinaryXOutputStream& rStream)
8228c6
     sal_uInt32 headerSize = encryptionHeaderSize + cspNameSize;
8228c6
     rStream.WriteUInt32(headerSize);
8228c6
 
8228c6
-    rStream.writeMemory(&mInfo.header, encryptionHeaderSize);
8228c6
+    rStream.WriteUInt32(mInfo.header.flags);
8228c6
+    rStream.WriteUInt32(mInfo.header.sizeExtra);
8228c6
+    rStream.WriteUInt32(mInfo.header.algId);
8228c6
+    rStream.WriteUInt32(mInfo.header.algIdHash);
8228c6
+    rStream.WriteUInt32(mInfo.header.keyBits);
8228c6
+    rStream.WriteUInt32(mInfo.header.providedType);
8228c6
+    rStream.WriteUInt32(mInfo.header.reserved1);
8228c6
+    rStream.WriteUInt32(mInfo.header.reserved2);
8228c6
     rStream.writeUnicodeArray(lclCspName);
8228c6
     rStream.WriteUInt16(0);
8228c6
 
8228c6
-    rStream.writeMemory(&mInfo.verifier, sizeof(msfilter::EncryptionVerifierAES));
8228c6
+    rStream.WriteUInt32(mInfo.verifier.saltSize);
8228c6
+    rStream.writeMemory(&mInfo.verifier.salt, sizeof mInfo.verifier.salt);
8228c6
+    rStream.writeMemory(&mInfo.verifier.encryptedVerifier, sizeof mInfo.verifier.encryptedVerifier);
8228c6
+    rStream.WriteUInt32(mInfo.verifier.encryptedVerifierHashSize);
8228c6
+    rStream.writeMemory(
8228c6
+        &mInfo.verifier.encryptedVerifierHash, sizeof mInfo.verifier.encryptedVerifierHash);
8228c6
 }
8228c6
 
8228c6
 void Standard2007Engine::encrypt(css::uno::Reference<css::io::XInputStream> &  rxInputStream,
8228c6
-- 
8228c6
2.33.1
8228c6