Kaydet (Commit) 3ba169bd authored tarafından Markus Mohrhard's avatar Markus Mohrhard

add initial tests for vba encryption

Change-Id: Ic6128ecade39e8947863c9162523e0d9690f0026
üst c5975818
...@@ -111,13 +111,14 @@ private: ...@@ -111,13 +111,14 @@ private:
SvMemoryStream& mrUncompressedStream; SvMemoryStream& mrUncompressedStream;
}; };
class VBAEncryption class OOX_DLLPUBLIC VBAEncryption
{ {
public: public:
VBAEncryption(const sal_uInt8* pData, VBAEncryption(const sal_uInt8* pData,
const sal_uInt16 nLength, const sal_uInt16 nLength,
SvStream& rEncryptedData SvStream& rEncryptedData,
); sal_uInt8* pSeed,
sal_uInt8 nProjKey);
void write(); void write();
......
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
$(eval $(call gb_CppunitTest_CppunitTest,oox_vba_encryption))
$(eval $(call gb_CppunitTest_add_exception_objects,oox_vba_encryption,\
oox/qa/unit/vba_encryption \
))
$(eval $(call gb_CppunitTest_use_api,oox_vba_encryption,\
offapi \
udkapi \
))
$(eval $(call gb_CppunitTest_use_libraries,oox_vba_encryption,\
basegfx \
comphelper \
cppu \
cppuhelper \
editeng \
expwrap \
drawinglayer \
msfilter \
sal \
i18nlangtag \
oox \
sax \
sfx \
svl \
svt \
svx \
svxcore \
sot \
tl \
unotest \
utl \
vcl \
xo \
xmlscript \
$(gb_UWINAPI) \
))
# vim: set noet sw=4 ts=4:
...@@ -19,6 +19,7 @@ $(eval $(call gb_Module_add_targets,oox,\ ...@@ -19,6 +19,7 @@ $(eval $(call gb_Module_add_targets,oox,\
$(eval $(call gb_Module_add_check_targets,oox,\ $(eval $(call gb_Module_add_check_targets,oox,\
CppunitTest_oox_tokenmap \ CppunitTest_oox_tokenmap \
CppunitTest_oox_vba_compression \ CppunitTest_oox_vba_compression \
CppunitTest_oox_vba_encryption \
)) ))
# vim: set noet sw=4 ts=4: # vim: set noet sw=4 ts=4:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <unotest/bootstrapfixturebase.hxx>
#include <cppunit/plugin/TestPlugIn.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestFixture.h>
#include <oox/ole/vbaexport.hxx>
#include <algorithm>
class TestVbaEncryption : public test::BootstrapFixtureBase
{
public:
// an initial test for the encryption taken from the spec
void testSimple1();
void testSimple2();
// avoid the BootstrapFixtureBase::setUp and tearDown
virtual void setUp() SAL_OVERRIDE;
virtual void tearDown() SAL_OVERRIDE;
CPPUNIT_TEST_SUITE(TestVbaEncryption);
// CPPUNIT_TEST(testSimple1);
// CPPUNIT_TEST(testSimple2);
CPPUNIT_TEST_SUITE_END();
private:
};
void TestVbaEncryption::testSimple1()
{
sal_uInt8 nSeed = 0x07;
sal_uInt8 nProjKey = 0xDF;
sal_uInt16 nLength = 0x04;
sal_uInt8 pData[] = { 0x00, 0x00, 0x00, 0x00 };
SvMemoryStream aEncryptedStream(4096, 4096);
VBAEncryption aEncryption(pData, nLength, aEncryptedStream,
&nSeed, nProjKey);
aEncryption.write();
}
void TestVbaEncryption::testSimple2()
{
sal_uInt8 nSeed = 0x15;
sal_uInt8 nProjKey = 0xDF;
sal_uInt16 nLength = 0x01;
sal_uInt8 pData[] = { 0xFF };
SvMemoryStream aEncryptedStream(4096, 4096);
VBAEncryption aEncryption(pData, nLength, aEncryptedStream,
&nSeed, nProjKey);
aEncryption.write();
sal_uInt8 pExpectedData[] = "1517CAF1D6F9D7F9D706";
size_t length = sizeof(pExpectedData);
aEncryptedStream.Seek(0);
for (size_t i = 0; i < length; ++i)
{
unsigned char val = 0;
aEncryptedStream.ReadUChar(val);
CPPUNIT_ASSERT_EQUAL((int)pExpectedData[i], (int)sal_uInt8(val));
}
}
void TestVbaEncryption::setUp()
{
}
void TestVbaEncryption::tearDown()
{
}
CPPUNIT_TEST_SUITE_REGISTRATION(TestVbaEncryption);
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -47,10 +47,10 @@ ...@@ -47,10 +47,10 @@
#define VBA_USE_ORIGINAL_PROJECT_STREAM 0 #define VBA_USE_ORIGINAL_PROJECT_STREAM 0
#define VBA_USE_ORIGINAL_VBA_PROJECT 0 #define VBA_USE_ORIGINAL_VBA_PROJECT 0
#define VBA_ENCRYPTION 0
/* Enable to see VBA Encryption work. For now the input data and length values /* Enable to see VBA Encryption work. For now the input data and length values
* for encryption correspond to the case when the VBA macro is not protected. * for encryption correspond to the case when the VBA macro is not protected.
*/ */
#define VBA_ENCRYPTION 1
namespace { namespace {
...@@ -344,23 +344,24 @@ void VBACompression::write() ...@@ -344,23 +344,24 @@ void VBACompression::write()
// section 2.4.3 // section 2.4.3
#if VBA_ENCRYPTION #if VBA_ENCRYPTION
VBAEncryption::VBAEncryption(const sal_uInt8* pData, const sal_uInt16 length, SvStream& rEncryptedData) VBAEncryption::VBAEncryption(const sal_uInt8* pData, const sal_uInt16 length, SvStream& rEncryptedData, sal_uInt8* pSeed, sal_uInt8 nProjKey)
:mpData(pData) :mpData(pData)
,mnLength(length) ,mnLength(length)
,mrEncryptedData(rEncryptedData) ,mrEncryptedData(rEncryptedData)
,mnEncryptedByte1(0) ,mnEncryptedByte1(0)
,mnEncryptedByte2(0) ,mnEncryptedByte2(0)
,mnVersion(2) ,mnVersion(2)
,mnProjKey(0) ,mnProjKey(nProjKey)
,mnIgnoredLength(0) ,mnIgnoredLength(0)
,mnSeed(0) ,mnSeed(pSeed ? *pSeed : 0x00)
,mnVersionEnc(0) ,mnVersionEnc(0)
{ {
if (!pSeed)
mnSeed = 0xBE; // sample seed value TODO:Generate random seed values
} }
void VBAEncryption::writeSeed() void VBAEncryption::writeSeed()
{ {
mnSeed = 0xBE; // sample seed value TODO:Generate random seed values
mrEncryptedData.WriteUInt8(mnSeed); mrEncryptedData.WriteUInt8(mnSeed);
} }
...@@ -372,6 +373,7 @@ void VBAEncryption::writeVersionEnc() ...@@ -372,6 +373,7 @@ void VBAEncryption::writeVersionEnc()
void VBAEncryption::writeProjKeyEnc() void VBAEncryption::writeProjKeyEnc()
{ {
/*
OUString mrProjectCLSID = "{9F10AB9C-89AC-4C0F-8AFB-8E9B96D5F170}"; //TODO:Find the real ProjectId.ProjectClSID OUString mrProjectCLSID = "{9F10AB9C-89AC-4C0F-8AFB-8E9B96D5F170}"; //TODO:Find the real ProjectId.ProjectClSID
sal_Int32 n = mrProjectCLSID.getLength(); sal_Int32 n = mrProjectCLSID.getLength();
const sal_Unicode* pString = mrProjectCLSID.getStr(); const sal_Unicode* pString = mrProjectCLSID.getStr();
...@@ -380,6 +382,7 @@ void VBAEncryption::writeProjKeyEnc() ...@@ -380,6 +382,7 @@ void VBAEncryption::writeProjKeyEnc()
sal_Unicode character = pString[i]; sal_Unicode character = pString[i];
mnProjKey += character; mnProjKey += character;
} }
*/
sal_uInt8 nProjKeyEnc = mnSeed ^ mnProjKey; sal_uInt8 nProjKeyEnc = mnSeed ^ mnProjKey;
mrEncryptedData.WriteUInt8(nProjKeyEnc); mrEncryptedData.WriteUInt8(nProjKeyEnc);
mnUnencryptedByte1 = mnProjKey; mnUnencryptedByte1 = mnProjKey;
...@@ -877,7 +880,7 @@ void exportPROJECTStream(SvStream& rStrm, css::uno::Reference<css::container::XN ...@@ -877,7 +880,7 @@ void exportPROJECTStream(SvStream& rStrm, css::uno::Reference<css::container::XN
SvMemoryStream aProtectedStream(4096, 4096); SvMemoryStream aProtectedStream(4096, 4096);
aProtectedStream.WriteUInt32(0x00000000); aProtectedStream.WriteUInt32(0x00000000);
const sal_uInt8* pData = static_cast<const sal_uInt8*>(aProtectedStream.GetData()); const sal_uInt8* pData = static_cast<const sal_uInt8*>(aProtectedStream.GetData());
VBAEncryption aProtectionState(pData, 4, rStrm); VBAEncryption aProtectionState(pData, 4, rStrm, NULL, 0);
aProtectionState.write(); aProtectionState.write();
exportString(rStrm, "\"\r\n"); exportString(rStrm, "\"\r\n");
#else #else
...@@ -890,7 +893,7 @@ void exportPROJECTStream(SvStream& rStrm, css::uno::Reference<css::container::XN ...@@ -890,7 +893,7 @@ void exportPROJECTStream(SvStream& rStrm, css::uno::Reference<css::container::XN
aProtectedStream.Seek(0); aProtectedStream.Seek(0);
aProtectedStream.WriteUInt8(0x00); aProtectedStream.WriteUInt8(0x00);
pData = static_cast<const sal_uInt8*>(aProtectedStream.GetData()); pData = static_cast<const sal_uInt8*>(aProtectedStream.GetData());
VBAEncryption aProjectPassword(pData, 1, rStrm); VBAEncryption aProjectPassword(pData, 1, rStrm, NULL, 0);
aProjectPassword.write(); aProjectPassword.write();
exportString(rStrm, "\"\r\n"); exportString(rStrm, "\"\r\n");
#else #else
...@@ -903,7 +906,7 @@ void exportPROJECTStream(SvStream& rStrm, css::uno::Reference<css::container::XN ...@@ -903,7 +906,7 @@ void exportPROJECTStream(SvStream& rStrm, css::uno::Reference<css::container::XN
aProtectedStream.Seek(0); aProtectedStream.Seek(0);
aProtectedStream.WriteUInt8(0xFF); aProtectedStream.WriteUInt8(0xFF);
pData = static_cast<const sal_uInt8*>(aProtectedStream.GetData()); pData = static_cast<const sal_uInt8*>(aProtectedStream.GetData());
VBAEncryption aVisibilityState(pData, 1, rStrm); VBAEncryption aVisibilityState(pData, 1, rStrm, NULL, 0);
aVisibilityState.write(); aVisibilityState.write();
exportString(rStrm, "\"\r\n\r\n"); exportString(rStrm, "\"\r\n\r\n");
#else #else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment