mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-02-10 18:05:41 +08:00
ooxml files encrypting
This commit is contained in:
21
OfficeCryptReader/Test/Test.cpp
Normal file
21
OfficeCryptReader/Test/Test.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
// Test.cpp : Defines the entry point for the console application.
|
||||
//
|
||||
#include "tchar.h"
|
||||
#include "../source/ECMACryptFile.h"
|
||||
|
||||
int _tmain(int argc, _TCHAR* argv[])
|
||||
{
|
||||
std::wstring srcFileName = L"D:\\test\\_crypted\\test-password-2016.docx";
|
||||
std::wstring dstFileName = srcFileName + L".oox";
|
||||
std::wstring dstFileName2 = dstFileName + L"-mycrypt.docx";
|
||||
|
||||
std::wstring password = L"password";
|
||||
|
||||
ECMACryptFile crypt_file;
|
||||
crypt_file.DecryptOfficeFile(srcFileName, dstFileName, password);
|
||||
|
||||
crypt_file.EncryptOfficeFile(dstFileName, dstFileName2, password);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
95
OfficeCryptReader/Test/Test.sln
Normal file
95
OfficeCryptReader/Test/Test.sln
Normal file
@ -0,0 +1,95 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 9.00
|
||||
# Visual Studio 2005
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Test", "Test.vcproj", "{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C} = {C27E9A9F-3A17-4482-9C5F-BF15C01E747C}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OfficeFileCrypt", "..\win32\ECMACryptReader.vcproj", "{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0} = {21663823-DE45-479B-91D0-B4FEF4916EF0}
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF} = {3423EC9A-52E4-4A4D-9753-EDEBC38785EF}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cryptlib", "..\..\Common\3dParty\cryptopp\cryptlib.vcproj", "{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libxml2", "..\..\DesktopEditor\xml\build\vs2005\libxml2.vcproj", "{21663823-DE45-479B-91D0-B4FEF4916EF0}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
Debug|x64 = Debug|x64
|
||||
DLL-Import Debug|Win32 = DLL-Import Debug|Win32
|
||||
DLL-Import Debug|x64 = DLL-Import Debug|x64
|
||||
DLL-Import Release|Win32 = DLL-Import Release|Win32
|
||||
DLL-Import Release|x64 = DLL-Import Release|x64
|
||||
Release|Win32 = Release|Win32
|
||||
Release|x64 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.Debug|x64.ActiveCfg = Debug|Win32
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.DLL-Import Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.DLL-Import Debug|Win32.Build.0 = Debug|Win32
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.DLL-Import Debug|x64.ActiveCfg = Debug|Win32
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.DLL-Import Release|Win32.ActiveCfg = Release|Win32
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.DLL-Import Release|Win32.Build.0 = Release|Win32
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.DLL-Import Release|x64.ActiveCfg = Release|Win32
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.Release|Win32.Build.0 = Release|Win32
|
||||
{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}.Release|x64.ActiveCfg = Release|Win32
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.Debug|x64.Build.0 = Debug|x64
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.DLL-Import Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.DLL-Import Debug|Win32.Build.0 = Debug|Win32
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.DLL-Import Debug|x64.ActiveCfg = Debug|x64
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.DLL-Import Debug|x64.Build.0 = Debug|x64
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.DLL-Import Release|Win32.ActiveCfg = Release|Win32
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.DLL-Import Release|Win32.Build.0 = Release|Win32
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.DLL-Import Release|x64.ActiveCfg = Release|x64
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.DLL-Import Release|x64.Build.0 = Release|x64
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.Release|Win32.Build.0 = Release|Win32
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.Release|x64.ActiveCfg = Release|x64
|
||||
{C27E9A9F-3A17-4482-9C5F-BF15C01E747C}.Release|x64.Build.0 = Release|x64
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Debug|x64.Build.0 = Debug|x64
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.DLL-Import Debug|Win32.ActiveCfg = DLL-Import Debug|Win32
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.DLL-Import Debug|Win32.Build.0 = DLL-Import Debug|Win32
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.DLL-Import Debug|x64.ActiveCfg = DLL-Import Debug|x64
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.DLL-Import Debug|x64.Build.0 = DLL-Import Debug|x64
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.DLL-Import Release|Win32.ActiveCfg = DLL-Import Release|Win32
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.DLL-Import Release|Win32.Build.0 = DLL-Import Release|Win32
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.DLL-Import Release|x64.ActiveCfg = DLL-Import Release|x64
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.DLL-Import Release|x64.Build.0 = DLL-Import Release|x64
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Release|Win32.Build.0 = Release|Win32
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Release|x64.ActiveCfg = Release|x64
|
||||
{3423EC9A-52E4-4A4D-9753-EDEBC38785EF}.Release|x64.Build.0 = Release|x64
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.Debug|x64.Build.0 = Debug|x64
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.DLL-Import Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.DLL-Import Debug|Win32.Build.0 = Debug|Win32
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.DLL-Import Debug|x64.ActiveCfg = Debug|x64
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.DLL-Import Debug|x64.Build.0 = Debug|x64
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.DLL-Import Release|Win32.ActiveCfg = Release|Win32
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.DLL-Import Release|Win32.Build.0 = Release|Win32
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.DLL-Import Release|x64.ActiveCfg = Release|x64
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.DLL-Import Release|x64.Build.0 = Release|x64
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.Release|Win32.Build.0 = Release|Win32
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.Release|x64.ActiveCfg = Release|x64
|
||||
{21663823-DE45-479B-91D0-B4FEF4916EF0}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
199
OfficeCryptReader/Test/Test.vcproj
Normal file
199
OfficeCryptReader/Test/Test.vcproj
Normal file
@ -0,0 +1,199 @@
|
||||
<?xml version="1.0" encoding="windows-1251"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="8,00"
|
||||
Name="Test"
|
||||
ProjectGUID="{BE4AA52B-8AF0-48DD-8240-CCBA6F84D7A2}"
|
||||
RootNamespace="Test"
|
||||
Keyword="Win32Proj"
|
||||
>
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"
|
||||
/>
|
||||
</Platforms>
|
||||
<ToolFiles>
|
||||
</ToolFiles>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="3"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="3"
|
||||
Detect64BitPortabilityProblems="true"
|
||||
DebugInformationFormat="4"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
LinkIncremental="2"
|
||||
GenerateDebugInformation="true"
|
||||
SubSystem="1"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebDeploymentTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="1"
|
||||
CharacterSet="1"
|
||||
WholeProgramOptimization="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
|
||||
RuntimeLibrary="2"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="3"
|
||||
Detect64BitPortabilityProblems="true"
|
||||
DebugInformationFormat="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManagedResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
LinkIncremental="1"
|
||||
GenerateDebugInformation="true"
|
||||
SubSystem="1"
|
||||
OptimizeReferences="2"
|
||||
EnableCOMDATFolding="2"
|
||||
TargetMachine="1"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCALinkTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCManifestTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCXDCMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCBscMakeTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCFxCopTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCAppVerifierTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCWebDeploymentTool"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"
|
||||
/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<Filter
|
||||
Name="Common"
|
||||
>
|
||||
<File
|
||||
RelativePath="..\..\Common\3dParty\pole\pole.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\Common\DocxFormat\Source\Base\unicode_util.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\DesktopEditor\xml\src\xmldom.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\DesktopEditor\xml\src\xmllight.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<File
|
||||
RelativePath=".\Test.cpp"
|
||||
>
|
||||
</File>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
||||
@ -48,14 +48,16 @@
|
||||
#include "../../Common/3dParty/cryptopp/hex.h"
|
||||
|
||||
#include "../../Common/DocxFormat/Source/Base/unicode_util.h"
|
||||
|
||||
#include "../../Common/DocxFormat/Source/Base/Types_32.h"
|
||||
|
||||
static const unsigned char encrVerifierHashInputBlockKey[8] = { 0xfe, 0xa7, 0xd2, 0x76, 0x3b, 0x4b, 0x9e, 0x79 };
|
||||
static const unsigned char encrVerifierHashValueBlockKey[8] = { 0xd7, 0xaa, 0x0f, 0x6d, 0x30, 0x61, 0x34, 0x4e };
|
||||
static const unsigned char encrKeyValueBlockKey[8] = { 0x14, 0x6e, 0x0b, 0xe7, 0xab, 0xac, 0xd0, 0xd6 };
|
||||
|
||||
static const unsigned char encrDataIntegritySaltBlockKey[8] = { 0x5f, 0xb2, 0xad, 0x01, 0x0c, 0xb9, 0xe1, 0xf6 };
|
||||
static const unsigned char encrDataIntegrityHmacValueBlockKey[8] = { 0xa0, 0x67, 0x7f, 0x02, 0xb2, 0x2c, 0x84, 0x33 };
|
||||
|
||||
using namespace CryptoPP;
|
||||
|
||||
class _buf
|
||||
{
|
||||
@ -173,50 +175,82 @@ void CorrectHashSize(_buf & hashBuf, int size, unsigned char padding)
|
||||
hashBuf.size = size;
|
||||
}
|
||||
}
|
||||
_buf Hmac(_buf & buf, CRYPT_METHOD::_hashAlgorithm algorithm, std::string & plain)
|
||||
{
|
||||
if (algorithm == CRYPT_METHOD::SHA1)
|
||||
{
|
||||
CryptoPP::HMAC<CryptoPP::SHA1> hmac(buf.ptr, buf.size);
|
||||
|
||||
//return _buf(mac.BytePtr(), mac.SizeInBytes());
|
||||
}
|
||||
else if (algorithm == CRYPT_METHOD::SHA256)
|
||||
{
|
||||
CryptoPP::HMAC<CryptoPP::SHA256> hmac(buf.ptr, buf.size);
|
||||
|
||||
//return _buf(mac.BytePtr(), mac.SizeInBytes());
|
||||
}
|
||||
else if (algorithm == CRYPT_METHOD::SHA512)
|
||||
{
|
||||
CryptoPP::HMAC<CryptoPP::SHA512> hmac(buf.ptr, buf.size);
|
||||
|
||||
std::string mac;
|
||||
CryptoPP::StringSource(plain, true,
|
||||
new CryptoPP::HashFilter(hmac,
|
||||
new CryptoPP::StringSink(mac)
|
||||
) // HashFilter
|
||||
); // StringSource
|
||||
|
||||
return _buf(mac);
|
||||
|
||||
}
|
||||
//else
|
||||
return _buf();
|
||||
}
|
||||
|
||||
|
||||
_buf HashAppend(_buf & hashBuf, _buf & block, CRYPT_METHOD::_hashAlgorithm algorithm)
|
||||
{//todooo переделать
|
||||
if (algorithm == CRYPT_METHOD::SHA1)
|
||||
{
|
||||
//CryptoPP::MD5 hash;
|
||||
//MD5 hash;
|
||||
|
||||
//if (hashBuf.ptr && hashBuf.size > 0) hash.Update( hashBuf.ptr, hashBuf.size);
|
||||
//if (block.ptr && block.size > 0) hash.Update( block.ptr , block.size);
|
||||
|
||||
//CryptoPP::SecByteBlock buffer(hash.DigestSize());
|
||||
//SecByteBlock buffer(hash.DigestSize());
|
||||
//hash.Final(buffer);
|
||||
|
||||
//return _buf(buffer.BytePtr(), buffer.SizeInBytes());
|
||||
CryptoPP::SHA1 hash;
|
||||
SHA1 hash;
|
||||
|
||||
if (hashBuf.ptr && hashBuf.size > 0) hash.Update( hashBuf.ptr, hashBuf.size);
|
||||
if (block.ptr && block.size > 0) hash.Update( block.ptr , block.size);
|
||||
|
||||
CryptoPP::SecByteBlock buffer(hash.DigestSize());
|
||||
SecByteBlock buffer(hash.DigestSize());
|
||||
hash.Final(buffer);
|
||||
|
||||
return _buf(buffer.BytePtr(), buffer.SizeInBytes());
|
||||
}
|
||||
else if (algorithm == CRYPT_METHOD::SHA256)
|
||||
{
|
||||
CryptoPP::SHA256 hash;
|
||||
SHA256 hash;
|
||||
|
||||
if (hashBuf.ptr && hashBuf.size > 0) hash.Update( hashBuf.ptr, hashBuf.size);
|
||||
if (block.ptr && block.size > 0) hash.Update( block.ptr , block.size);
|
||||
|
||||
CryptoPP::SecByteBlock buffer(hash.DigestSize());
|
||||
SecByteBlock buffer(hash.DigestSize());
|
||||
hash.Final(buffer);
|
||||
|
||||
return _buf(buffer.BytePtr(), buffer.SizeInBytes());
|
||||
}
|
||||
else if (algorithm == CRYPT_METHOD::SHA512)
|
||||
{
|
||||
CryptoPP::SHA512 hash;
|
||||
SHA512 hash;
|
||||
|
||||
if (hashBuf.ptr && hashBuf.size > 0) hash.Update( hashBuf.ptr, hashBuf.size);
|
||||
if (block.ptr && block.size > 0) hash.Update( block.ptr , block.size);
|
||||
|
||||
CryptoPP::SecByteBlock buffer(hash.DigestSize());
|
||||
SecByteBlock buffer(hash.DigestSize());
|
||||
hash.Final(buffer);
|
||||
|
||||
return _buf(buffer.BytePtr(), buffer.SizeInBytes());
|
||||
@ -268,9 +302,59 @@ _buf GenerateHashKey(_buf & salt, _buf & password, int hashSize, int spin, CRYPT
|
||||
return _buf(pHashBuf.ptr, hashSize);
|
||||
}
|
||||
|
||||
bool DecryptCipher(_buf & key, _buf & iv, _buf & data_inp, _buf & data_out, CRYPT_METHOD::_cipherAlgorithm algorithm)
|
||||
bool EncryptCipher(_buf & key, _buf & iv, _buf & data_inp, _buf & data_out, CRYPT_METHOD::_cipherAlgorithm algorithm,
|
||||
StreamTransformationFilter::BlockPaddingScheme padding = StreamTransformationFilter::PKCS_PADDING)
|
||||
{
|
||||
if (algorithm == CRYPT_METHOD::RC4)
|
||||
if (algorithm == CRYPT_METHOD::XOR)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (algorithm == CRYPT_METHOD::RC4)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else //AES
|
||||
{
|
||||
StreamTransformation *modeEncryption = NULL;
|
||||
AES::Encryption aesEncryption(key.ptr, key.size);
|
||||
|
||||
switch(algorithm)
|
||||
{
|
||||
case CRYPT_METHOD::AES_ECB:
|
||||
modeEncryption = new ECB_Mode_ExternalCipher::Encryption(aesEncryption, iv.ptr );
|
||||
break;
|
||||
case CRYPT_METHOD::AES_CBC:
|
||||
modeEncryption = new CBC_Mode_ExternalCipher::Encryption(aesEncryption, iv.ptr );
|
||||
break;
|
||||
case CRYPT_METHOD::AES_CFB:
|
||||
modeEncryption = new CFB_Mode_ExternalCipher::Encryption(aesEncryption, iv.ptr );
|
||||
break;
|
||||
}
|
||||
|
||||
if (!modeEncryption) return false;
|
||||
|
||||
if (!data_out.ptr)
|
||||
{
|
||||
data_out = _buf(data_inp.size);
|
||||
}
|
||||
StreamTransformationFilter stfEncryption(*modeEncryption, new ArraySink( data_out.ptr, data_out.size), padding);
|
||||
|
||||
stfEncryption.Put( data_inp.ptr, data_inp.size );
|
||||
stfEncryption.MessageEnd();
|
||||
|
||||
delete modeEncryption;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
bool DecryptCipher(_buf & key, _buf & iv, _buf & data_inp, _buf & data_out, CRYPT_METHOD::_cipherAlgorithm algorithm,
|
||||
StreamTransformationFilter::BlockPaddingScheme padding = StreamTransformationFilter::NO_PADDING)
|
||||
{
|
||||
if (algorithm == CRYPT_METHOD::XOR)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (algorithm == CRYPT_METHOD::RC4)
|
||||
{
|
||||
//CryptoPP::ARC4 rc4(key.ptr, key.size);
|
||||
//data_out.ptr = new unsigned char[data_inp.size];
|
||||
@ -309,19 +393,21 @@ bool DecryptCipher(_buf & key, _buf & iv, _buf & data_inp, _buf & data_out, CRYP
|
||||
//return true;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
else //AES
|
||||
{
|
||||
CryptoPP::StreamTransformation *modeDecryption = NULL;
|
||||
CryptoPP::AES::Decryption aesDecryption(key.ptr, key.size);
|
||||
StreamTransformation *modeDecryption = NULL;
|
||||
AES::Decryption aesDecryption(key.ptr, key.size);
|
||||
|
||||
switch(algorithm)
|
||||
{
|
||||
case CRYPT_METHOD::AES_ECB:
|
||||
modeDecryption = new CryptoPP::ECB_Mode_ExternalCipher::Decryption(aesDecryption, iv.ptr );
|
||||
modeDecryption = new ECB_Mode_ExternalCipher::Decryption(aesDecryption, iv.ptr );
|
||||
break;
|
||||
case CRYPT_METHOD::AES_CBC:
|
||||
modeDecryption = new CryptoPP::CBC_Mode_ExternalCipher::Decryption(aesDecryption, iv.ptr );
|
||||
modeDecryption = new CBC_Mode_ExternalCipher::Decryption(aesDecryption, iv.ptr );
|
||||
break;
|
||||
case CRYPT_METHOD::AES_CFB:
|
||||
modeDecryption = new CFB_Mode_ExternalCipher::Decryption(aesDecryption, iv.ptr );
|
||||
}
|
||||
|
||||
if (!modeDecryption) return false;
|
||||
@ -330,7 +416,7 @@ bool DecryptCipher(_buf & key, _buf & iv, _buf & data_inp, _buf & data_out, CRYP
|
||||
{
|
||||
data_out = _buf(data_inp.size);
|
||||
}
|
||||
CryptoPP::StreamTransformationFilter stfDecryptor(*modeDecryption, new CryptoPP::ArraySink( data_out.ptr, data_out.size), CryptoPP::StreamTransformationFilter::NO_PADDING);
|
||||
StreamTransformationFilter stfDecryptor(*modeDecryption, new ArraySink( data_out.ptr, data_out.size), padding);
|
||||
|
||||
stfDecryptor.Put( data_inp.ptr, data_inp.size );
|
||||
stfDecryptor.MessageEnd();
|
||||
@ -371,16 +457,16 @@ bool ECMADecryptor::SetPassword(std::wstring _password)
|
||||
_buf decryptedVerifierHashInputBytes;
|
||||
|
||||
DecryptCipher(verifierInputKey, pSalt, pEncVerInput, decryptedVerifierHashInputBytes, cryptData.cipherAlgorithm);
|
||||
//--------------------------------------------
|
||||
//--------------------------------------------
|
||||
|
||||
_buf hashBuf = HashAppend(decryptedVerifierHashInputBytes, empty, cryptData.hashAlgorithm);
|
||||
|
||||
//--------------------------------------------
|
||||
//--------------------------------------------
|
||||
_buf decryptedVerifierHashBytes;
|
||||
|
||||
_buf verifierHashKey = GenerateAgileKey(pSalt, pPassword, pValueBlockKey, cryptData.keySize, cryptData.spinCount, cryptData.hashAlgorithm);
|
||||
DecryptCipher(verifierHashKey, pSalt, pEncVerValue, decryptedVerifierHashBytes, cryptData.cipherAlgorithm);
|
||||
|
||||
//--------------------------------------------
|
||||
bVerify = (decryptedVerifierHashBytes==hashBuf);
|
||||
}
|
||||
else
|
||||
@ -416,7 +502,7 @@ bool ECMADecryptor::IsVerify()
|
||||
return bVerify;
|
||||
}
|
||||
|
||||
void ECMADecryptor::SetCryptData(_cryptData &data)
|
||||
void ECMADecryptor::SetCryptData(_ecmaCryptData & data)
|
||||
{
|
||||
cryptData = data;
|
||||
}
|
||||
@ -429,12 +515,49 @@ void ECMADecryptor::Decrypt(char* data , const size_t size, const unsigned long
|
||||
|
||||
if (data_out)
|
||||
{
|
||||
delete []data;
|
||||
data = (char*)data_out;
|
||||
memcpy(data, data_out, size);
|
||||
delete []data_out;
|
||||
}
|
||||
}
|
||||
}
|
||||
void ECMADecryptor::Decrypt(unsigned char* data_inp, int size, unsigned char*& data_out)
|
||||
bool ECMADecryptor::IsDataIntegrity(unsigned char* data, int size)
|
||||
{
|
||||
_buf pBlockKey ((unsigned char*)encrKeyValueBlockKey, 8);
|
||||
_buf pBlockHmacKey ((unsigned char*)encrDataIntegritySaltBlockKey, 8);
|
||||
_buf pBlockHmacValue((unsigned char*)encrDataIntegrityHmacValueBlockKey, 8);
|
||||
|
||||
_buf pPassword (password);
|
||||
_buf pSalt (cryptData.saltValue);
|
||||
_buf empty (NULL, 0, false);
|
||||
|
||||
_buf pDataSalt (cryptData.dataSaltValue);
|
||||
_buf pKeyValue (cryptData.encryptedKeyValue);
|
||||
_buf pEncHmacKey (cryptData.encryptedHmacKey);
|
||||
_buf pEncHmacValue (cryptData.encryptedHmacValue);
|
||||
|
||||
_buf agileKey = GenerateAgileKey( pSalt, pPassword, pBlockKey, cryptData.keySize, cryptData.spinCount, cryptData.hashAlgorithm);
|
||||
|
||||
_buf secretKey;
|
||||
DecryptCipher( agileKey, pSalt, pKeyValue, secretKey, cryptData.cipherAlgorithm);
|
||||
//----
|
||||
_buf iv1 = HashAppend(pDataSalt, pBlockHmacKey, cryptData.hashAlgorithm);
|
||||
CorrectHashSize(iv1, cryptData.blockSize, 0x36);
|
||||
|
||||
_buf iv2 = HashAppend(pDataSalt, pBlockHmacValue, cryptData.hashAlgorithm);
|
||||
CorrectHashSize(iv2, cryptData.blockSize, 0x36);
|
||||
|
||||
_buf salt;
|
||||
DecryptCipher(secretKey, iv1, pEncHmacKey, salt, cryptData.cipherAlgorithm);
|
||||
|
||||
_buf expected;
|
||||
DecryptCipher(secretKey, iv2, pEncHmacValue, expected, cryptData.cipherAlgorithm);
|
||||
|
||||
std::string sData((char*)data, size);
|
||||
_buf hmac = Hmac(salt, cryptData.hashAlgorithm, sData);
|
||||
|
||||
return (hmac == expected);
|
||||
}
|
||||
void ECMADecryptor::Decrypt(unsigned char* data_ptr, int data_size, unsigned char*& data_out)
|
||||
{
|
||||
data_out = NULL;
|
||||
|
||||
@ -442,10 +565,15 @@ void ECMADecryptor::Decrypt(unsigned char* data_inp, int size, unsigned char*&
|
||||
_buf pSalt (cryptData.saltValue);
|
||||
_buf empty (NULL, 0, false);
|
||||
|
||||
int size = data_size - 8;
|
||||
unsigned char* data_inp = data_ptr + 8;
|
||||
|
||||
data_out = new unsigned char[size];
|
||||
|
||||
if (cryptData.bAgile)
|
||||
{
|
||||
bool isDataIntegrity = IsDataIntegrity(data_ptr, data_size);
|
||||
|
||||
_buf pBlockKey ((unsigned char*)encrKeyValueBlockKey, 8);
|
||||
_buf pDataSalt (cryptData.dataSaltValue);
|
||||
_buf pKeyValue (cryptData.encryptedKeyValue);
|
||||
@ -459,7 +587,7 @@ void ECMADecryptor::Decrypt(unsigned char* data_inp, int size, unsigned char*&
|
||||
memset( iv.ptr, 0x00, cryptData.blockSize );
|
||||
|
||||
int i = 0, sz = 4096, pos = 0;
|
||||
|
||||
|
||||
while (pos < size)
|
||||
{
|
||||
if (pos + sz > size)
|
||||
@ -472,11 +600,30 @@ void ECMADecryptor::Decrypt(unsigned char* data_inp, int size, unsigned char*&
|
||||
|
||||
_buf pInp(data_inp + pos, sz, false);
|
||||
_buf pOut(data_out + pos, sz, false);
|
||||
|
||||
DecryptCipher(pDecryptedKey, iv, pInp, pOut, cryptData.cipherAlgorithm);
|
||||
|
||||
DecryptCipher(pDecryptedKey, iv, pInp, pOut, cryptData.cipherAlgorithm);
|
||||
|
||||
pos += sz; i++;
|
||||
}
|
||||
//--------------------------------------------
|
||||
_buf pEncVerKeyMac (cryptData.encryptedHmacKey);
|
||||
_buf pEncVerValueMac (cryptData.encryptedHmacValue);
|
||||
|
||||
_buf iv1(cryptData.blockSize);
|
||||
memset( iv1.ptr, 0x00, cryptData.blockSize );
|
||||
|
||||
i = 0;
|
||||
_buf pIndex1((unsigned char*)&i, 4);
|
||||
iv1 = HashAppend(pDataSalt, pIndex1, cryptData.hashAlgorithm);
|
||||
|
||||
CorrectHashSize(iv1, cryptData.blockSize, 0x36);
|
||||
|
||||
_buf pOut1(pEncVerKeyMac.size);
|
||||
_buf pOut2(pEncVerValueMac.size);
|
||||
|
||||
DecryptCipher(pDecryptedKey, iv, pEncVerKeyMac, pOut1, cryptData.cipherAlgorithm);
|
||||
DecryptCipher(pDecryptedKey, iv, pEncVerValueMac, pOut2, cryptData.cipherAlgorithm);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -488,4 +635,153 @@ void ECMADecryptor::Decrypt(unsigned char* data_inp, int size, unsigned char*&
|
||||
DecryptCipher(hashKey, empty, pInp, pOut, cryptData.cipherAlgorithm);
|
||||
}
|
||||
}
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
ECMAEncryptor::ECMAEncryptor()
|
||||
{
|
||||
}
|
||||
|
||||
void ECMAEncryptor::SetPassword(std::wstring _password)
|
||||
{
|
||||
password = _password;
|
||||
}
|
||||
|
||||
void ECMAEncryptor::SetCryptData(_ecmaCryptData & data)
|
||||
{
|
||||
cryptData = data;
|
||||
}
|
||||
|
||||
void ECMAEncryptor::GetCryptData(_ecmaCryptData &data)
|
||||
{
|
||||
data = cryptData;
|
||||
}
|
||||
void ECMAEncryptor::UpdateDataIntegrity(unsigned char* data, int size)
|
||||
{
|
||||
if (cryptData.bAgile == false) return;
|
||||
|
||||
_buf pBlockKey ((unsigned char*)encrKeyValueBlockKey, 8);
|
||||
_buf pBlockHmacKey ((unsigned char*)encrDataIntegritySaltBlockKey, 8);
|
||||
_buf pBlockHmacValue((unsigned char*)encrDataIntegrityHmacValueBlockKey, 8);
|
||||
|
||||
_buf pPassword (password);
|
||||
_buf pSalt (cryptData.saltValue);
|
||||
_buf empty (NULL, 0, false);
|
||||
|
||||
_buf pDataSalt (cryptData.dataSaltValue);
|
||||
_buf pKeyValue (cryptData.encryptedKeyValue);
|
||||
_buf pEncHmacKey (cryptData.encryptedHmacKey);
|
||||
_buf pEncHmacValue (cryptData.encryptedHmacValue);
|
||||
|
||||
_buf agileKey = GenerateAgileKey( pSalt, pPassword, pBlockKey, cryptData.keySize, cryptData.spinCount, cryptData.hashAlgorithm);
|
||||
|
||||
_buf secretKey;
|
||||
DecryptCipher( agileKey, pSalt, pKeyValue, secretKey, cryptData.cipherAlgorithm);
|
||||
//----
|
||||
_buf iv1 = HashAppend(pDataSalt, pBlockHmacKey, cryptData.hashAlgorithm);
|
||||
CorrectHashSize(iv1, cryptData.blockSize, 0x36);
|
||||
|
||||
_buf iv2 = HashAppend(pDataSalt, pBlockHmacValue, cryptData.hashAlgorithm);
|
||||
CorrectHashSize(iv2, cryptData.blockSize, 0x36);
|
||||
|
||||
_buf salt;
|
||||
DecryptCipher(secretKey, iv1, pEncHmacKey, salt, cryptData.cipherAlgorithm);
|
||||
|
||||
_buf expected;
|
||||
DecryptCipher(secretKey, iv2, pEncHmacValue, expected, cryptData.cipherAlgorithm);
|
||||
|
||||
std::string sData((char*)data, size);
|
||||
_buf hmac = Hmac(salt, cryptData.hashAlgorithm, sData);
|
||||
|
||||
//return (hmac == expected);
|
||||
}
|
||||
int ECMAEncryptor::Encrypt(unsigned char* data_inp_ptr, int size, unsigned char*& data_out_ptr)
|
||||
{
|
||||
data_out_ptr = NULL;
|
||||
|
||||
_buf pPassword (password);
|
||||
_buf pSalt (cryptData.saltValue);
|
||||
_buf empty (NULL, 0, false);
|
||||
|
||||
int size_out = size;
|
||||
if (size_out % 8 != 0)
|
||||
size_out = (size_out / 8 + 1) * 8;
|
||||
|
||||
data_out_ptr = new unsigned char[size_out + 8]; // real size + padding + size for realsize
|
||||
|
||||
_UINT64 nSize = size;
|
||||
memcpy(data_out_ptr, (unsigned char*)&nSize, 8);
|
||||
|
||||
unsigned char* data_inp = data_inp_ptr;
|
||||
unsigned char* data_out = data_out_ptr + 8;
|
||||
|
||||
_buf pBlockKey ((unsigned char*)encrKeyValueBlockKey, 8);
|
||||
_buf pDataSalt (cryptData.dataSaltValue);
|
||||
_buf pKeyValue (cryptData.encryptedKeyValue);
|
||||
|
||||
//------------------------------------------------------------------------------------------------
|
||||
//соль нужно сгенерить
|
||||
|
||||
_buf agileKey = GenerateAgileKey( pSalt, pPassword, pBlockKey, cryptData.keySize, cryptData.spinCount, cryptData.hashAlgorithm);
|
||||
|
||||
//тут нужно именно дешифрованый генерить - пока их файла берем
|
||||
_buf pDecryptedKey;
|
||||
DecryptCipher( agileKey, pSalt, pKeyValue, pDecryptedKey, cryptData.cipherAlgorithm);
|
||||
|
||||
//зашифровать ключь
|
||||
_buf pEncryptedKey;
|
||||
EncryptCipher( agileKey, pSalt, pDecryptedKey, pEncryptedKey, cryptData.cipherAlgorithm);
|
||||
|
||||
//??? pEncryptedKey == pKeyValue;
|
||||
|
||||
//-------------------------------------------------------------------------------------------------
|
||||
_buf iv(cryptData.blockSize);
|
||||
memset( iv.ptr, 0x00, cryptData.blockSize );
|
||||
|
||||
int i = 0, sz = 4096, enc_size = 0;
|
||||
|
||||
while (enc_size < size)
|
||||
{
|
||||
if (enc_size + sz > size)
|
||||
{
|
||||
sz = size - enc_size;
|
||||
}
|
||||
|
||||
_buf pIndex((unsigned char*)&i, 4);
|
||||
iv = HashAppend(pDataSalt, pIndex, cryptData.hashAlgorithm);
|
||||
|
||||
CorrectHashSize(iv, cryptData.blockSize, 0x36);
|
||||
|
||||
if (sz < 4096)
|
||||
{
|
||||
_buf pInp(4096);
|
||||
memcpy(pInp.ptr, data_inp, sz );
|
||||
pInp.size = sz;
|
||||
_buf pOut(4096);
|
||||
|
||||
EncryptCipher(pDecryptedKey, iv, pInp, pOut, cryptData.cipherAlgorithm);
|
||||
|
||||
if (sz % 8 != 0)
|
||||
sz = (sz / 8 + 1) * 8;
|
||||
|
||||
memcpy(data_out, pOut.ptr, sz);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
_buf pInp(data_inp, sz, false);
|
||||
_buf pOut(data_out, sz, false);
|
||||
|
||||
EncryptCipher(pDecryptedKey, iv, pInp, pOut, cryptData.cipherAlgorithm);
|
||||
}
|
||||
data_inp += sz;
|
||||
data_out += sz;
|
||||
|
||||
enc_size += sz; i++;
|
||||
}
|
||||
|
||||
return enc_size + 8;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -68,57 +68,78 @@ class Decryptor
|
||||
};
|
||||
typedef boost::shared_ptr<Decryptor> DecryptorPtr;
|
||||
|
||||
class ECMADecryptor : public Decryptor
|
||||
struct _ecmaCryptData
|
||||
{
|
||||
public:
|
||||
|
||||
struct _cryptData
|
||||
{
|
||||
//default ms2010
|
||||
_cryptData() : cipherAlgorithm(CRYPT_METHOD::AES_CBC), hashAlgorithm(CRYPT_METHOD::SHA1), spinCount(100000),
|
||||
keySize(0x10), hashSize(0x14), blockSize(0x10), saltSize(0x10), bAgile(true)
|
||||
_ecmaCryptData() : cipherAlgorithm(CRYPT_METHOD::AES_CBC), hashAlgorithm(CRYPT_METHOD::SHA1), spinCount(100000),
|
||||
keySize(0x10), hashSize(0x14), blockSize(0x10), saltSize(0x10), bAgile(true)
|
||||
//default ms2013/ms2016
|
||||
//_cryptData(): cipherAlgorithm(CRYPT_METHOD::AES_CBC), hashAlgorithm(CRYPT_METHOD::SHA256), spinCount(100000),
|
||||
// keySize(0x20), hashSize(0x40), blockSize(0x10), saltSize(0x10), bAgile(true)
|
||||
{
|
||||
}
|
||||
CRYPT_METHOD::_cipherAlgorithm cipherAlgorithm;
|
||||
CRYPT_METHOD::_hashAlgorithm hashAlgorithm;
|
||||
//_cryptData(): cipherAlgorithm(CRYPT_METHOD::AES_CBC), hashAlgorithm(CRYPT_METHOD::SHA256), spinCount(100000),
|
||||
// keySize(0x20), hashSize(0x40), blockSize(0x10), saltSize(0x10), bAgile(true)
|
||||
{
|
||||
}
|
||||
CRYPT_METHOD::_cipherAlgorithm cipherAlgorithm;
|
||||
CRYPT_METHOD::_hashAlgorithm hashAlgorithm;
|
||||
|
||||
int spinCount;
|
||||
int keySize;
|
||||
int hashSize;
|
||||
int blockSize;
|
||||
int saltSize;
|
||||
int spinCount;
|
||||
int keySize;
|
||||
int hashSize;
|
||||
int blockSize;
|
||||
int saltSize;
|
||||
|
||||
std::string dataSaltValue;
|
||||
std::string saltValue;
|
||||
std::string encryptedKeyValue;
|
||||
std::string encryptedVerifierInput;
|
||||
std::string encryptedVerifierValue;
|
||||
|
||||
std::string encryptedHmacKey;
|
||||
std::string encryptedHmacValue;
|
||||
std::string dataSaltValue;
|
||||
std::string saltValue;
|
||||
std::string encryptedKeyValue;
|
||||
std::string encryptedVerifierInput;
|
||||
std::string encryptedVerifierValue;
|
||||
|
||||
std::string encryptedHmacKey;
|
||||
std::string encryptedHmacValue;
|
||||
|
||||
bool bAgile;
|
||||
bool bAgile;
|
||||
|
||||
//..........
|
||||
|
||||
};
|
||||
};
|
||||
class ECMAEncryptor
|
||||
{
|
||||
public:
|
||||
ECMAEncryptor();
|
||||
virtual ~ECMAEncryptor(){}
|
||||
|
||||
void SetPassword (std::wstring password);
|
||||
|
||||
void SetCryptData(_ecmaCryptData &data);
|
||||
void GetCryptData(_ecmaCryptData &data);
|
||||
|
||||
int Encrypt (unsigned char* data, int size, unsigned char*& data_out);
|
||||
|
||||
void UpdateDataIntegrity(unsigned char* data, int size);
|
||||
|
||||
private:
|
||||
std::wstring password;
|
||||
_ecmaCryptData cryptData;
|
||||
};
|
||||
|
||||
class ECMADecryptor : public Decryptor
|
||||
{
|
||||
public:
|
||||
ECMADecryptor();
|
||||
virtual ~ECMADecryptor(){}
|
||||
|
||||
void Decrypt (unsigned char* data, int size, unsigned char*& data_out);
|
||||
|
||||
virtual void Decrypt (char* data , const size_t size, const unsigned long stream_pos);
|
||||
virtual void Decrypt (char* data, const size_t size, const unsigned long stream_pos);
|
||||
virtual bool SetPassword (std::wstring password);
|
||||
virtual bool IsVerify();
|
||||
|
||||
void SetCryptData(_cryptData &data);
|
||||
void SetCryptData(_ecmaCryptData &data);
|
||||
|
||||
private:
|
||||
bool IsDataIntegrity(unsigned char* data, int size);
|
||||
|
||||
std::wstring password;
|
||||
_cryptData cryptData;
|
||||
_ecmaCryptData cryptData;
|
||||
bool bVerify;
|
||||
};
|
||||
|
||||
|
||||
770
OfficeCryptReader/source/ECMACryptFile.cpp
Normal file
770
OfficeCryptReader/source/ECMACryptFile.cpp
Normal file
@ -0,0 +1,770 @@
|
||||
/*
|
||||
* (c) Copyright Ascensio System SIA 2010-2017
|
||||
*
|
||||
* This program is a free software product. You can redistribute it and/or
|
||||
* modify it under the terms of the GNU Affero General Public License (AGPL)
|
||||
* version 3 as published by the Free Software Foundation. In accordance with
|
||||
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
|
||||
* that Ascensio System SIA expressly excludes the warranty of non-infringement
|
||||
* of any third-party rights.
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
|
||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
|
||||
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
||||
*
|
||||
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
|
||||
* EU, LV-1021.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of the Program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU AGPL version 3.
|
||||
*
|
||||
* Pursuant to Section 7(b) of the License you must retain the original Product
|
||||
* logo when distributing the program. Pursuant to Section 7(e) we decline to
|
||||
* grant you any rights under trademark law for use of our trademarks.
|
||||
*
|
||||
* All the Product's GUI elements, including illustrations and icon sets, as
|
||||
* well as technical writing content are licensed under the terms of the
|
||||
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
|
||||
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ECMACryptFile.h"
|
||||
#include "CryptTransform.h"
|
||||
|
||||
#include "../../Common/3dParty/pole/pole.h"
|
||||
#include "../../Common/DocxFormat/Source/Base/Types_32.h"
|
||||
|
||||
#include "../../DesktopEditor/common/File.h"
|
||||
#include "../../DesktopEditor/xml/include/xmlutils.h"
|
||||
|
||||
#include "../../ASCOfficeDocFile/DocDocxConverter/MemoryStream.h"
|
||||
#include "simple_xml_writer.h"
|
||||
|
||||
CRYPT::_ecmaCryptData cryptDataGlobal;
|
||||
|
||||
using namespace CRYPT;
|
||||
|
||||
#define GETBIT(from, num) ((from & (1 << num)) != 0)
|
||||
|
||||
#define WritingElement_ReadAttributes_Start(Reader) \
|
||||
if ( Reader.GetAttributesCount() <= 0 )\
|
||||
return false;\
|
||||
if ( !Reader.MoveToFirstAttribute() )\
|
||||
return false;\
|
||||
std::string wsName = Reader.GetNameA();\
|
||||
while( !wsName.empty() )\
|
||||
{
|
||||
|
||||
#define WritingElement_ReadAttributes_Read_if(Reader, AttrName, Value) \
|
||||
if ( AttrName == wsName )\
|
||||
{\
|
||||
Value = Reader.GetTextA();\
|
||||
}
|
||||
|
||||
#define WritingElement_ReadAttributes_Read_else_if(Reader, AttrName, Value) \
|
||||
else if ( AttrName == wsName )\
|
||||
Value = Reader.GetTextA();
|
||||
|
||||
#define WritingElement_ReadAttributes_ReadSingle(Reader, AttrName, Value) \
|
||||
if ( AttrName == wsName )\
|
||||
{\
|
||||
Value = Reader.GetTextA();\
|
||||
break;\
|
||||
}
|
||||
|
||||
#define WritingElement_ReadAttributes_End(Reader) \
|
||||
if ( !Reader.MoveToNextAttribute() ) \
|
||||
break;\
|
||||
wsName = Reader.GetNameA();\
|
||||
}\
|
||||
Reader.MoveToElement();
|
||||
|
||||
std::wstring ReadUnicodeLP(POLE::Stream *pStream)
|
||||
{
|
||||
if (!pStream) return L"";
|
||||
|
||||
_UINT32 length = 0;
|
||||
pStream->read((unsigned char*)&length, 4);
|
||||
|
||||
unsigned char* Data = new unsigned char[length * 2];
|
||||
pStream->read(Data, length * 2);
|
||||
|
||||
std::wstring res;
|
||||
|
||||
if (sizeof(wchar_t) == 4)
|
||||
{
|
||||
unsigned int nLength = length;
|
||||
|
||||
wchar_t* ptr = new wchar_t [length];
|
||||
|
||||
UTF16* pStrUtf16 = (UTF16*) Data;
|
||||
UTF32 *pStrUtf32 = (UTF32 *) ptr;
|
||||
|
||||
const UTF16 *pStrUtf16_Conv = pStrUtf16;
|
||||
UTF32 *pStrUtf32_Conv = pStrUtf32;
|
||||
|
||||
ConversionResult eUnicodeConversionResult = ConvertUTF16toUTF32 (&pStrUtf16_Conv, &pStrUtf16[nLength]
|
||||
, &pStrUtf32_Conv, &pStrUtf32 [nLength], strictConversion);
|
||||
|
||||
if (conversionOK != eUnicodeConversionResult)
|
||||
{
|
||||
}
|
||||
res = std::wstring(ptr, length);
|
||||
delete ptr;
|
||||
}
|
||||
else
|
||||
res = std::wstring((wchar_t*)Data, length);
|
||||
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
void ReadMapEntry(POLE::Stream *pStream, ECMACryptFile::_mapEntry & m)
|
||||
{
|
||||
if (!pStream) return;
|
||||
|
||||
_UINT32 length = 0;
|
||||
pStream->read((unsigned char*)&length, 4);
|
||||
|
||||
_UINT32 refCount = 0;
|
||||
pStream->read((unsigned char*)&refCount, 4);
|
||||
|
||||
for (int i = 0 ; i < refCount; i++)
|
||||
{
|
||||
ECMACryptFile::_refComponent r;
|
||||
pStream->read((unsigned char*)&r.type, 4);
|
||||
|
||||
r.ref = ReadUnicodeLP(pStream);
|
||||
m.refComponents.push_back(r);
|
||||
}
|
||||
m.dataSpaceName= ReadUnicodeLP(pStream);
|
||||
}
|
||||
|
||||
std::string DecodeBase64(const std::string & value)
|
||||
{
|
||||
int nLength = 0;
|
||||
unsigned char *pData = NULL;
|
||||
std::string result;
|
||||
|
||||
NSFile::CBase64Converter::Decode(value.c_str(), value.length(), pData, nLength);
|
||||
if (pData)
|
||||
{
|
||||
result = std::string((char*)pData, nLength);
|
||||
delete []pData; pData = NULL;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
std::string EncodeBase64(const std::string & value)
|
||||
{
|
||||
int nLength = 0;
|
||||
char *pData = NULL;
|
||||
std::string result;
|
||||
|
||||
NSFile::CBase64Converter::Encode((BYTE*)value.c_str(), value.length(), pData, nLength, NSBase64::B64_BASE64_FLAG_NOCRLF);
|
||||
if (pData)
|
||||
{
|
||||
result = std::string(pData, nLength);
|
||||
delete []pData; pData = NULL;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//-----------------------------------------------------------------------------------------------------------------------
|
||||
struct _keyEncryptor
|
||||
{
|
||||
std::string spinCount;
|
||||
std::string saltSize;
|
||||
std::string blockSize;
|
||||
std::string keyBits;
|
||||
std::string hashSize;
|
||||
|
||||
std::string cipherAlgorithm;
|
||||
std::string cipherChaining;
|
||||
std::string hashAlgorithm;
|
||||
|
||||
std::string saltValue;
|
||||
std::string encryptedVerifierHashInput;
|
||||
std::string encryptedVerifierHashValue;
|
||||
std::string encryptedKeyValue;
|
||||
};
|
||||
struct _dataIntegrity
|
||||
{
|
||||
std::string encryptedHmacKey;
|
||||
std::string encryptedHmacValue;
|
||||
};
|
||||
bool ReadXmlEncryptionInfo(const std::string & xml_string, _ecmaCryptData & cryptData)
|
||||
{
|
||||
XmlUtils::CXmlLiteReader xmlReader;
|
||||
|
||||
if (!xmlReader.FromStringA(xml_string))
|
||||
return false;
|
||||
|
||||
if ( !xmlReader.ReadNextNode() )
|
||||
return false;
|
||||
|
||||
_dataIntegrity dataIntegrity;
|
||||
_keyEncryptor keyData;
|
||||
std::vector<_keyEncryptor> keyEncryptors;
|
||||
|
||||
int nCurDepth = xmlReader.GetDepth();
|
||||
while( xmlReader.ReadNextSiblingNode( nCurDepth ) )
|
||||
{
|
||||
std::wstring sName = xmlReader.GetName();
|
||||
if ( L"keyData" == sName )
|
||||
{
|
||||
WritingElement_ReadAttributes_Start( xmlReader)
|
||||
WritingElement_ReadAttributes_Read_if ( xmlReader, "saltSize", keyData.saltSize )
|
||||
WritingElement_ReadAttributes_Read_else_if ( xmlReader, "blockSize", keyData.blockSize )
|
||||
WritingElement_ReadAttributes_Read_else_if ( xmlReader, "keyBits", keyData.keyBits )
|
||||
WritingElement_ReadAttributes_Read_else_if ( xmlReader, "hashSize", keyData.hashSize )
|
||||
WritingElement_ReadAttributes_Read_else_if ( xmlReader, "cipherAlgorithm", keyData.cipherAlgorithm )
|
||||
WritingElement_ReadAttributes_Read_else_if ( xmlReader, "cipherChaining", keyData.cipherChaining )
|
||||
WritingElement_ReadAttributes_Read_else_if ( xmlReader, "hashAlgorithm", keyData.hashAlgorithm )
|
||||
WritingElement_ReadAttributes_Read_else_if ( xmlReader, "saltValue", keyData.saltValue )
|
||||
WritingElement_ReadAttributes_End( xmlReader )
|
||||
}
|
||||
else if ( L"dataIntegrity" == sName )
|
||||
{
|
||||
WritingElement_ReadAttributes_Start( xmlReader)
|
||||
WritingElement_ReadAttributes_Read_if ( xmlReader, "encryptedHmacKey", dataIntegrity.encryptedHmacKey)
|
||||
WritingElement_ReadAttributes_Read_else_if ( xmlReader, "encryptedHmacValue", dataIntegrity.encryptedHmacValue)
|
||||
WritingElement_ReadAttributes_End( xmlReader )
|
||||
}
|
||||
else if (L"keyEncryptors" == sName)
|
||||
{
|
||||
while( xmlReader.ReadNextSiblingNode( nCurDepth + 1 ) )
|
||||
{
|
||||
sName = xmlReader.GetName();
|
||||
if (L"keyEncryptor" == sName)
|
||||
{
|
||||
while( xmlReader.ReadNextSiblingNode( nCurDepth + 2 ) )
|
||||
{
|
||||
sName = xmlReader.GetName();
|
||||
if (L"p:encryptedKey" == sName)
|
||||
{
|
||||
_keyEncryptor k={};
|
||||
|
||||
WritingElement_ReadAttributes_Start( xmlReader)
|
||||
WritingElement_ReadAttributes_Read_if ( xmlReader, "spinCount", k.spinCount )
|
||||
WritingElement_ReadAttributes_Read_else_if( xmlReader, "saltSize", k.saltSize )
|
||||
WritingElement_ReadAttributes_Read_else_if( xmlReader, "blockSize", k.blockSize )
|
||||
WritingElement_ReadAttributes_Read_else_if( xmlReader, "keyBits", k.keyBits )
|
||||
WritingElement_ReadAttributes_Read_else_if( xmlReader, "hashSize", k.hashSize )
|
||||
WritingElement_ReadAttributes_Read_else_if( xmlReader, "cipherAlgorithm", k.cipherAlgorithm )
|
||||
WritingElement_ReadAttributes_Read_else_if( xmlReader, "cipherChaining", k.cipherChaining )
|
||||
WritingElement_ReadAttributes_Read_else_if( xmlReader, "hashAlgorithm", k.hashAlgorithm )
|
||||
WritingElement_ReadAttributes_Read_else_if( xmlReader, "saltValue", k.saltValue )
|
||||
WritingElement_ReadAttributes_Read_else_if( xmlReader, "encryptedVerifierHashInput", k.encryptedVerifierHashInput )
|
||||
WritingElement_ReadAttributes_Read_else_if( xmlReader, "encryptedVerifierHashValue", k.encryptedVerifierHashValue )
|
||||
WritingElement_ReadAttributes_Read_else_if( xmlReader, "encryptedKeyValue", k.encryptedKeyValue )
|
||||
WritingElement_ReadAttributes_End( xmlReader )
|
||||
|
||||
keyEncryptors.push_back(k);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (keyEncryptors.empty()) return false;
|
||||
|
||||
cryptData.spinCount = atoi(keyEncryptors[0].spinCount.c_str());
|
||||
cryptData.blockSize = atoi(keyEncryptors[0].blockSize.c_str());
|
||||
cryptData.hashSize = atoi(keyEncryptors[0].hashSize.c_str());
|
||||
cryptData.saltSize = atoi(keyEncryptors[0].saltSize.c_str());
|
||||
cryptData.keySize = atoi(keyEncryptors[0].keyBits.c_str() ) / 8;
|
||||
|
||||
cryptData.dataSaltValue = DecodeBase64(keyData.saltValue);
|
||||
cryptData.saltValue = DecodeBase64(keyEncryptors[0].saltValue);
|
||||
cryptData.encryptedKeyValue = DecodeBase64(keyEncryptors[0].encryptedKeyValue);
|
||||
cryptData.encryptedVerifierInput = DecodeBase64(keyEncryptors[0].encryptedVerifierHashInput);
|
||||
cryptData.encryptedVerifierValue = DecodeBase64(keyEncryptors[0].encryptedVerifierHashValue);
|
||||
|
||||
cryptData.encryptedHmacKey = DecodeBase64(dataIntegrity.encryptedHmacKey);
|
||||
cryptData.encryptedHmacValue = DecodeBase64(dataIntegrity.encryptedHmacValue);
|
||||
|
||||
if (keyData.cipherAlgorithm == "AES")
|
||||
{
|
||||
if (keyData.cipherChaining == "ChainingModeCBC") cryptData.cipherAlgorithm = CRYPT_METHOD::AES_CBC;
|
||||
if (keyData.cipherChaining == "ChainingModeCFB") cryptData.cipherAlgorithm = CRYPT_METHOD::AES_CFB;
|
||||
}
|
||||
|
||||
if (keyData.hashAlgorithm == "SHA1") cryptData.hashAlgorithm = CRYPT_METHOD::SHA1;
|
||||
if (keyData.hashAlgorithm == "SHA224") cryptData.hashAlgorithm = CRYPT_METHOD::SHA224;
|
||||
if (keyData.hashAlgorithm == "SHA256") cryptData.hashAlgorithm = CRYPT_METHOD::SHA256;
|
||||
if (keyData.hashAlgorithm == "SHA384") cryptData.hashAlgorithm = CRYPT_METHOD::SHA384;
|
||||
if (keyData.hashAlgorithm == "SHA512") cryptData.hashAlgorithm = CRYPT_METHOD::SHA512;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WriteXmlEncryptionInfo(const _ecmaCryptData & cryptData, std::string & xml_string)
|
||||
{
|
||||
XmlUtils::CXmlWriter xmlWriter;
|
||||
|
||||
_dataIntegrity dataIntegrity;
|
||||
_keyEncryptor keyData;
|
||||
|
||||
keyData.spinCount = std::to_string(cryptData.spinCount);
|
||||
keyData.blockSize = std::to_string(cryptData.blockSize);
|
||||
keyData.hashSize = std::to_string(cryptData.hashSize);
|
||||
keyData.saltSize = std::to_string(cryptData.saltSize);
|
||||
keyData.keyBits = std::to_string(cryptData.keySize * 8);
|
||||
keyData.saltValue = EncodeBase64(cryptData.dataSaltValue);
|
||||
|
||||
keyData.cipherAlgorithm = "AES";
|
||||
|
||||
if (keyData.cipherAlgorithm == "AES")
|
||||
{
|
||||
if (cryptData.cipherAlgorithm == CRYPT_METHOD::AES_CBC) keyData.cipherChaining = "ChainingModeCBC";
|
||||
if (cryptData.cipherAlgorithm == CRYPT_METHOD::AES_CFB) keyData.cipherChaining = "ChainingModeCFB";
|
||||
}
|
||||
|
||||
switch(cryptData.hashAlgorithm)
|
||||
{
|
||||
case CRYPT_METHOD::SHA1: keyData.hashAlgorithm = "SHA1"; break;
|
||||
case CRYPT_METHOD::SHA224: keyData.hashAlgorithm = "SHA224"; break;
|
||||
case CRYPT_METHOD::SHA256: keyData.hashAlgorithm = "SHA256"; break;
|
||||
case CRYPT_METHOD::SHA384: keyData.hashAlgorithm = "SHA384"; break;
|
||||
case CRYPT_METHOD::SHA512: keyData.hashAlgorithm = "SHA512"; break;
|
||||
}
|
||||
|
||||
std::vector<_keyEncryptor> keyEncryptors;
|
||||
keyEncryptors.push_back(keyData);
|
||||
|
||||
keyEncryptors[0].saltValue = EncodeBase64(cryptData.saltValue);
|
||||
keyEncryptors[0].encryptedKeyValue = EncodeBase64(cryptData.encryptedKeyValue);
|
||||
keyEncryptors[0].encryptedVerifierHashInput = EncodeBase64(cryptData.encryptedVerifierInput);
|
||||
keyEncryptors[0].encryptedVerifierHashValue = EncodeBase64(cryptData.encryptedVerifierValue);
|
||||
|
||||
dataIntegrity.encryptedHmacKey = EncodeBase64(cryptData.encryptedHmacKey);
|
||||
dataIntegrity.encryptedHmacValue = EncodeBase64(cryptData.encryptedHmacValue);
|
||||
|
||||
std::stringstream stream;
|
||||
|
||||
CP_XML_WRITER(stream)
|
||||
{
|
||||
CP_XML_NODE("encryption")
|
||||
{
|
||||
CP_XML_ATTR("xmlns", "http://schemas.microsoft.com/office/2006/encryption");
|
||||
CP_XML_ATTR("xmlns:p", "http://schemas.microsoft.com/office/2006/keyEncryptor/password");
|
||||
CP_XML_ATTR("xmlns:c", "http://schemas.microsoft.com/office/2006/keyEncryptor/certificate");
|
||||
|
||||
CP_XML_NODE("keyData")
|
||||
{
|
||||
CP_XML_ATTR("saltSize", keyData.saltSize);
|
||||
CP_XML_ATTR("blockSize", keyData.blockSize);
|
||||
CP_XML_ATTR("keyBits", keyData.keyBits);
|
||||
CP_XML_ATTR("hashSize", keyData.hashSize);
|
||||
CP_XML_ATTR("cipherAlgorithm", keyData.cipherAlgorithm);
|
||||
CP_XML_ATTR("cipherChaining", keyData.cipherChaining);
|
||||
CP_XML_ATTR("hashAlgorithm", keyData.hashAlgorithm);
|
||||
CP_XML_ATTR("saltValue", keyData.saltValue);
|
||||
}
|
||||
CP_XML_NODE("dataIntegrity")
|
||||
{
|
||||
CP_XML_ATTR("encryptedHmacKey", dataIntegrity.encryptedHmacKey);
|
||||
CP_XML_ATTR("encryptedHmacValue", dataIntegrity.encryptedHmacValue);
|
||||
}
|
||||
int i = 0;
|
||||
CP_XML_NODE("keyEncryptors")
|
||||
{
|
||||
CP_XML_NODE("keyEncryptor")
|
||||
{
|
||||
CP_XML_ATTR("uri", "http://schemas.microsoft.com/office/2006/keyEncryptor/password");
|
||||
|
||||
CP_XML_NODE("p:encryptedKey")
|
||||
{
|
||||
CP_XML_ATTR("spinCount", keyEncryptors[i].spinCount );
|
||||
CP_XML_ATTR("saltSize", keyEncryptors[i].saltSize );
|
||||
CP_XML_ATTR("blockSize", keyEncryptors[i].blockSize );
|
||||
CP_XML_ATTR("keyBits", keyEncryptors[i].keyBits );
|
||||
CP_XML_ATTR("hashSize", keyEncryptors[i].hashSize );
|
||||
CP_XML_ATTR("cipherAlgorithm", keyEncryptors[i].cipherAlgorithm );
|
||||
CP_XML_ATTR("cipherChaining", keyEncryptors[i].cipherChaining );
|
||||
CP_XML_ATTR("hashAlgorithm", keyEncryptors[i].hashAlgorithm );
|
||||
CP_XML_ATTR("saltValue", keyEncryptors[i].saltValue );
|
||||
CP_XML_ATTR("encryptedVerifierHashInput", keyEncryptors[i].encryptedVerifierHashInput );
|
||||
CP_XML_ATTR("encryptedVerifierHashValue", keyEncryptors[i].encryptedVerifierHashValue );
|
||||
CP_XML_ATTR("encryptedKeyValue", keyEncryptors[i].encryptedKeyValue );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n" + stream.str();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadStandartEncryptionInfo(unsigned char* data, int size, _ecmaCryptData & cryptData)
|
||||
{
|
||||
if (!data || size < 1) return false;
|
||||
MemoryStream mem_stream(data, size, false);
|
||||
|
||||
//EncryptionHeader
|
||||
int HeaderSize = mem_stream.ReadUInt32();
|
||||
int Flags = mem_stream.ReadUInt32();
|
||||
int SizeExtra = mem_stream.ReadUInt32();
|
||||
int AlgID = mem_stream.ReadUInt32();
|
||||
int AlgIDHash = mem_stream.ReadUInt32();
|
||||
int KeySize = mem_stream.ReadUInt32();
|
||||
int ProviderType= mem_stream.ReadUInt32();
|
||||
int Reserved1 = mem_stream.ReadUInt32();
|
||||
int Reserved2 = mem_stream.ReadUInt32();
|
||||
|
||||
int pos = mem_stream.GetPosition();
|
||||
|
||||
while(pos < size - 1)
|
||||
{
|
||||
if (data[pos] == 0 && data[pos + 1] == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
pos+=2;//unicode null-terminate string
|
||||
}
|
||||
int szCSPName = pos - mem_stream.GetPosition() + 2;
|
||||
|
||||
unsigned char* strData = mem_stream.ReadBytes(szCSPName, true);
|
||||
if (strData)
|
||||
{
|
||||
delete []strData;
|
||||
}
|
||||
//EncryptionVerifier
|
||||
cryptData.saltSize = mem_stream.ReadUInt32();
|
||||
|
||||
cryptData.saltValue = std::string((char*)data + mem_stream.GetPosition(), cryptData.saltSize);
|
||||
mem_stream.ReadBytes(cryptData.saltSize, false);
|
||||
|
||||
cryptData.encryptedVerifierInput = std::string((char*)data + mem_stream.GetPosition(), 0x10);
|
||||
mem_stream.ReadBytes(0x10, false);
|
||||
|
||||
cryptData.hashSize = mem_stream.ReadUInt32();
|
||||
|
||||
int szEncryptedVerifierHash = (ProviderType == 0x0001) ? 0x14 : 0x20;
|
||||
cryptData.encryptedVerifierValue = std::string((char*)data + mem_stream.GetPosition(), szEncryptedVerifierHash);
|
||||
mem_stream.ReadBytes(szEncryptedVerifierHash, false);
|
||||
|
||||
pos = mem_stream.GetPosition();
|
||||
|
||||
//------------------------------------------------------------------------------------------
|
||||
cryptData.hashAlgorithm = CRYPT_METHOD::SHA1; //by AlgIDHash -> 0x0000 || 0x8004
|
||||
cryptData.spinCount = 50000;
|
||||
|
||||
switch(AlgID)
|
||||
{
|
||||
case 0x6801:
|
||||
cryptData.cipherAlgorithm = CRYPT_METHOD::RC4;
|
||||
cryptData.keySize = KeySize / 8;
|
||||
break;
|
||||
case 0x660E:
|
||||
cryptData.cipherAlgorithm = CRYPT_METHOD::AES_ECB;
|
||||
cryptData.keySize = 128 /8;
|
||||
break;
|
||||
case 0x660F:
|
||||
cryptData.cipherAlgorithm = CRYPT_METHOD::AES_ECB;
|
||||
cryptData.keySize = 192 /8;
|
||||
break;
|
||||
case 0x6610:
|
||||
cryptData.cipherAlgorithm = CRYPT_METHOD::AES_ECB;
|
||||
cryptData.keySize = 256 /8;
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadExtensibleEncryptionInfo(unsigned char* data, int size, _ecmaCryptData & cryptData)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------
|
||||
bool ECMACryptFile::EncryptOfficeFile(std::wstring file_name_inp, std::wstring file_name_out, std::wstring password)
|
||||
{
|
||||
_ecmaCryptData cryptData;
|
||||
|
||||
cryptData.bAgile = true;
|
||||
cryptData.hashAlgorithm = CRYPT_METHOD::SHA256;
|
||||
cryptData.keySize = 0x20;
|
||||
cryptData.hashSize = 0x40;
|
||||
cryptData.blockSize = 0x10;
|
||||
cryptData.saltSize = 0x10;
|
||||
|
||||
ECMAEncryptor cryptor;
|
||||
|
||||
cryptor.SetPassword(password);
|
||||
|
||||
cryptor.SetCryptData(cryptDataGlobal); //for test !!!
|
||||
//cryptor.SetCryptData(cryptData); //basic settings
|
||||
|
||||
NSFile::CFileBinary file;
|
||||
if (!file.OpenFile(file_name_inp)) return false;
|
||||
|
||||
_UINT64 lengthFileSize = file.GetFileSize();
|
||||
DWORD lengthData = lengthFileSize, lengthDataRead = 0 ;
|
||||
|
||||
unsigned char* data = new unsigned char[lengthData];
|
||||
unsigned char* data_out = NULL;
|
||||
|
||||
file.ReadFile(data, lengthData, lengthDataRead);
|
||||
|
||||
file.CloseFile();
|
||||
|
||||
lengthData = cryptor.Encrypt(data, lengthData, data_out);
|
||||
|
||||
cryptor.UpdateDataIntegrity(data_out, lengthData);
|
||||
|
||||
if (!data_out)
|
||||
{
|
||||
delete []data;
|
||||
return false;
|
||||
}
|
||||
//-------------------------------------------------------------------
|
||||
POLE::Storage *pStorage = new POLE::Storage(file_name_out.c_str());
|
||||
|
||||
if (!pStorage)return false;
|
||||
|
||||
if (!pStorage->open(true, true))
|
||||
{
|
||||
delete pStorage;
|
||||
return false;
|
||||
}
|
||||
//-------------------------------------------------------------------
|
||||
POLE::Stream *pStream = new POLE::Stream(pStorage, "EncryptionInfo", true);
|
||||
|
||||
cryptor.GetCryptData(cryptData);
|
||||
|
||||
std::string strXml;
|
||||
WriteXmlEncryptionInfo(cryptData, strXml);
|
||||
|
||||
_UINT16 VersionInfoMajor = 0x0004, VersionInfoMinor = 0x0004; //agile standart
|
||||
|
||||
pStream->write((unsigned char*)&VersionInfoMajor, 2);
|
||||
pStream->write((unsigned char*)&VersionInfoMinor, 2);
|
||||
|
||||
_UINT32 nEncryptionInfoFlags = 64;
|
||||
pStream->write((unsigned char*)&nEncryptionInfoFlags, 4);
|
||||
|
||||
pStream->write((unsigned char*)strXml.c_str(), strXml.length());
|
||||
|
||||
pStream->flush();
|
||||
delete pStream;
|
||||
//-------------------------------------------------------------------
|
||||
pStream = new POLE::Stream(pStorage, "EncryptedPackage", true, lengthData);
|
||||
|
||||
pStream->write(data_out, lengthData);
|
||||
|
||||
pStream->flush();
|
||||
delete pStream;
|
||||
|
||||
pStorage->close();
|
||||
delete pStorage;
|
||||
|
||||
|
||||
//test back---------------------------------------------------------------------------------test back
|
||||
ECMADecryptor decryptor;
|
||||
|
||||
decryptor.SetCryptData(cryptData);
|
||||
|
||||
if (decryptor.SetPassword(password))
|
||||
{
|
||||
unsigned char* data_out2 = NULL;
|
||||
decryptor.Decrypt(data_out, lengthData, data_out2);
|
||||
NSFile::CFileBinary test;
|
||||
|
||||
test.CreateFileW(file_name_out + L"-back.oox");
|
||||
test.WriteFile(data_out2, lengthFileSize);
|
||||
test.CloseFile();
|
||||
}
|
||||
//test back---------------------------------------------------------------------------------test back
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ECMACryptFile::DecryptOfficeFile(std::wstring file_name_inp, std::wstring file_name_out, std::wstring password)
|
||||
{
|
||||
POLE::Storage *pStorage = new POLE::Storage(file_name_inp.c_str());
|
||||
|
||||
if (!pStorage)return false;
|
||||
|
||||
if (!pStorage->open())
|
||||
{
|
||||
delete pStorage;
|
||||
return false;
|
||||
}
|
||||
_ecmaCryptData cryptData;
|
||||
bool result = false;
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
//{
|
||||
// std::wstring f = file_name_out + L"-1.docx";
|
||||
// POLE::Storage *pStorage1 = new POLE::Storage(f.c_str());
|
||||
// pStorage1->open(true, true);
|
||||
|
||||
// POLE::Stream *pStrIn = new POLE::Stream(pStorage, "EncryptionInfo");
|
||||
// POLE::uint64 sz = pStrIn->size();
|
||||
// POLE::Stream *pStrOut = new POLE::Stream(pStorage1, "EncryptionInfo", true, sz);
|
||||
|
||||
// BYTE *d = new BYTE [sz];
|
||||
// pStrIn->read(d, sz);
|
||||
// pStrOut->write(d, sz);
|
||||
// delete d;
|
||||
|
||||
// pStrOut->flush();
|
||||
// delete pStrOut;
|
||||
// delete pStrIn;
|
||||
|
||||
// pStrIn = new POLE::Stream(pStorage, "EncryptedPackage");
|
||||
// sz = pStrIn->size();
|
||||
// pStrOut = new POLE::Stream(pStorage1, "EncryptedPackage", true, sz);
|
||||
|
||||
// d = new BYTE [sz];
|
||||
// pStrIn->read(d, sz);
|
||||
// pStrOut->write(d, sz);
|
||||
// delete d;
|
||||
|
||||
// pStrOut->flush();
|
||||
// delete pStrOut;
|
||||
// delete pStrIn;
|
||||
|
||||
// pStorage1->close();
|
||||
// delete pStorage1;
|
||||
//}
|
||||
//----------------------------------------------------------------------------
|
||||
POLE::Stream *pStream = new POLE::Stream(pStorage, "EncryptionInfo");
|
||||
|
||||
if (pStream)
|
||||
{
|
||||
_UINT16 VersionInfoMajor = 0, VersionInfoMinor = 0;
|
||||
|
||||
pStream->read((unsigned char*)&VersionInfoMajor, 2);
|
||||
pStream->read((unsigned char*)&VersionInfoMinor, 2);
|
||||
|
||||
_UINT32 nEncryptionInfoFlags = 0;
|
||||
pStream->read((unsigned char*)&nEncryptionInfoFlags, 4);
|
||||
|
||||
int nEncryptionInfoSize = pStream->size() - 8;
|
||||
unsigned char* byteEncryptionInfo = new unsigned char[nEncryptionInfoSize];
|
||||
if (!byteEncryptionInfo)
|
||||
{
|
||||
delete pStream;
|
||||
delete pStorage;
|
||||
return false;
|
||||
}
|
||||
nEncryptionInfoSize = pStream->read(byteEncryptionInfo, nEncryptionInfoSize);
|
||||
delete pStream;
|
||||
|
||||
if (VersionInfoMajor == 0x0004 && VersionInfoMinor == 0x0004)
|
||||
{//agile info
|
||||
std::string xml_string((char*) byteEncryptionInfo, nEncryptionInfoSize);
|
||||
delete []byteEncryptionInfo;
|
||||
|
||||
cryptData.bAgile = true;
|
||||
result = ReadXmlEncryptionInfo(xml_string, cryptData);
|
||||
}
|
||||
else
|
||||
{
|
||||
cryptData.bAgile = false;
|
||||
bool fCryptoAPI = GETBIT(nEncryptionInfoFlags, 1);
|
||||
bool fDocProps = GETBIT(nEncryptionInfoFlags, 2);
|
||||
bool fExternal = GETBIT(nEncryptionInfoFlags, 3);
|
||||
bool fAES = GETBIT(nEncryptionInfoFlags, 4);
|
||||
|
||||
if ((VersionInfoMajor == 0x0003 || VersionInfoMajor == 0x0004) && VersionInfoMinor == 0x0003) //extensible info
|
||||
{
|
||||
result = ReadExtensibleEncryptionInfo(byteEncryptionInfo, nEncryptionInfoSize, cryptData);
|
||||
}
|
||||
else if ((VersionInfoMajor == 0x0003 || VersionInfoMajor == 0x0004) && VersionInfoMinor == 0x0002) //standart info
|
||||
{
|
||||
result = ReadStandartEncryptionInfo(byteEncryptionInfo, nEncryptionInfoSize, cryptData);
|
||||
}
|
||||
else
|
||||
{
|
||||
// look in DocFormat
|
||||
}
|
||||
delete []byteEncryptionInfo;
|
||||
}
|
||||
}
|
||||
if (!result)
|
||||
{
|
||||
delete pStorage;
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------------------------------------
|
||||
//pStream = new POLE::Stream(pStorage, "DataSpaces/DataSpaceMap");
|
||||
//if (pStream)
|
||||
//{
|
||||
// delete pStream;
|
||||
// pStorage->deleteByName("DataSpaces");
|
||||
|
||||
// //_UINT32 size = 0;
|
||||
// //_UINT32 count = 0;
|
||||
// //
|
||||
// //pStream->read((unsigned char*)&size, 4);
|
||||
// //pStream->read((unsigned char*)&count, 4);
|
||||
|
||||
// //for (int i = 0 ; i < count; i++)
|
||||
// //{
|
||||
// // _mapEntry m;
|
||||
// // ReadMapEntry(pStream, m);
|
||||
|
||||
// // mapEntries.push_back(m);
|
||||
// //}
|
||||
// //delete pStream;
|
||||
//}
|
||||
//------------------------------------------------------------------------------------------------------------
|
||||
ECMADecryptor decryptor;
|
||||
|
||||
decryptor.SetCryptData(cryptData);
|
||||
|
||||
if (!decryptor.SetPassword(password))
|
||||
{
|
||||
if (password.empty())
|
||||
{
|
||||
password = L"VelvetSweatshop";
|
||||
if (!decryptor.SetPassword(password))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
//------------------------------------------------------------------------------------------------------------
|
||||
pStream = new POLE::Stream(pStorage, "EncryptedPackage");
|
||||
if ((pStream) && (pStream->size() > 0))
|
||||
{
|
||||
_UINT64 lengthData, lengthRead = pStream->size();
|
||||
|
||||
unsigned char* data = new unsigned char[lengthRead];
|
||||
unsigned char* data_out = NULL;
|
||||
|
||||
int readTrue = pStream->read(data, lengthRead);
|
||||
|
||||
lengthData = *((_UINT64*)data);
|
||||
|
||||
decryptor.Decrypt(data, readTrue, data_out);//todoo сделать покусочное чтение декриптование
|
||||
delete pStream;
|
||||
delete []data;
|
||||
|
||||
if (data_out)
|
||||
{
|
||||
NSFile::CFileBinary f;
|
||||
f.CreateFileW(file_name_out);
|
||||
f.WriteFile(data_out, lengthData);
|
||||
f.CloseFile();
|
||||
|
||||
delete []data_out;
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
//-------------------------------------------------------------------
|
||||
delete pStorage;
|
||||
|
||||
cryptDataGlobal = cryptData;
|
||||
|
||||
return result;
|
||||
}
|
||||
54
OfficeCryptReader/source/ECMACryptFile.h
Normal file
54
OfficeCryptReader/source/ECMACryptFile.h
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* (c) Copyright Ascensio System SIA 2010-2017
|
||||
*
|
||||
* This program is a free software product. You can redistribute it and/or
|
||||
* modify it under the terms of the GNU Affero General Public License (AGPL)
|
||||
* version 3 as published by the Free Software Foundation. In accordance with
|
||||
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
|
||||
* that Ascensio System SIA expressly excludes the warranty of non-infringement
|
||||
* of any third-party rights.
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
|
||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
|
||||
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
||||
*
|
||||
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
|
||||
* EU, LV-1021.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of the Program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU AGPL version 3.
|
||||
*
|
||||
* Pursuant to Section 7(b) of the License you must retain the original Product
|
||||
* logo when distributing the program. Pursuant to Section 7(e) we decline to
|
||||
* grant you any rights under trademark law for use of our trademarks.
|
||||
*
|
||||
* All the Product's GUI elements, including illustrations and icon sets, as
|
||||
* well as technical writing content are licensed under the terms of the
|
||||
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
|
||||
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class ECMACryptFile
|
||||
{
|
||||
public:
|
||||
bool DecryptOfficeFile(std::wstring file_name_inp, std::wstring file_name_out, std::wstring password);
|
||||
bool EncryptOfficeFile(std::wstring file_name_inp, std::wstring file_name_out, std::wstring password);
|
||||
|
||||
struct _refComponent
|
||||
{
|
||||
int type;
|
||||
std::wstring ref;
|
||||
};
|
||||
struct _mapEntry
|
||||
{
|
||||
std::vector<_refComponent> refComponents;
|
||||
std::wstring dataSpaceName;
|
||||
};
|
||||
std::vector<_mapEntry> mapEntries;
|
||||
};
|
||||
321
OfficeCryptReader/source/simple_xml_writer.h
Normal file
321
OfficeCryptReader/source/simple_xml_writer.h
Normal file
@ -0,0 +1,321 @@
|
||||
/*
|
||||
* (c) Copyright Ascensio System SIA 2010-2017
|
||||
*
|
||||
* This program is a free software product. You can redistribute it and/or
|
||||
* modify it under the terms of the GNU Affero General Public License (AGPL)
|
||||
* version 3 as published by the Free Software Foundation. In accordance with
|
||||
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
|
||||
* that Ascensio System SIA expressly excludes the warranty of non-infringement
|
||||
* of any third-party rights.
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
|
||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
|
||||
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
||||
*
|
||||
* You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia,
|
||||
* EU, LV-1021.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of the Program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU AGPL version 3.
|
||||
*
|
||||
* Pursuant to Section 7(b) of the License you must retain the original Product
|
||||
* logo when distributing the program. Pursuant to Section 7(e) we decline to
|
||||
* grant you any rights under trademark law for use of our trademarks.
|
||||
*
|
||||
* All the Product's GUI elements, including illustrations and icon sets, as
|
||||
* well as technical writing content are licensed under the terms of the
|
||||
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
|
||||
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stack>
|
||||
#include <cassert>
|
||||
#include <boost/optional/optional_io.hpp>
|
||||
|
||||
namespace xml
|
||||
{
|
||||
|
||||
namespace writer
|
||||
{
|
||||
template <class T> struct chars { };
|
||||
|
||||
template <>
|
||||
struct chars<char>
|
||||
{
|
||||
static const char eq = '=';
|
||||
static const char quote = '\"';
|
||||
static const char space = ' ';
|
||||
static const char left_brocket = '<';
|
||||
static const char right_brocket = '>';
|
||||
static const char slash = '/';
|
||||
static const char colon = ':';
|
||||
static const char amp = '&';
|
||||
static const char apos = '\'';
|
||||
static const char * cdata_open() { return "<![CDATA["; }
|
||||
static const char * cdata_close() { return "]]>"; }
|
||||
static const char * amp_str(){ return "&"; }
|
||||
static const char * left_brocket_str() { return "<"; }
|
||||
static const char * right_brocket_str() { return ">"; }
|
||||
static const char * apos_str() { return "'"; }
|
||||
static const char * quote_str() { return """; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct chars<wchar_t>
|
||||
{
|
||||
static const wchar_t eq = L'=';
|
||||
static const wchar_t quote = L'\"';
|
||||
static const wchar_t space = L' ';
|
||||
static const wchar_t left_brocket = L'<';
|
||||
static const wchar_t right_brocket = L'>';
|
||||
static const wchar_t slash = L'/';
|
||||
static const wchar_t colon = L':';
|
||||
static const wchar_t amp = L'&';
|
||||
static const wchar_t apos = L'\'';
|
||||
static const wchar_t * cdata_open() { return L"<![CDATA["; }
|
||||
static const wchar_t * cdata_close() { return L"]]>"; }
|
||||
static const wchar_t * amp_str(){ return L"&"; }
|
||||
static const wchar_t * left_brocket_str() { return L"<"; }
|
||||
static const wchar_t * right_brocket_str() { return L">"; }
|
||||
static const wchar_t * apos_str() { return L"'"; }
|
||||
static const wchar_t * quote_str() { return L"""; }
|
||||
};
|
||||
|
||||
template <class V>
|
||||
class element;
|
||||
|
||||
//
|
||||
// xml::writer class
|
||||
//
|
||||
template <class T>
|
||||
class writer
|
||||
{
|
||||
public:
|
||||
// writer must be bound to an ostream
|
||||
writer(std::basic_ostream<T>& os, bool need_header = false) : level_(0), os_(os), need_header_(need_header) {}
|
||||
~writer(void) { assert(elements_.empty()); }
|
||||
|
||||
private:
|
||||
size_t level_;
|
||||
std::basic_ostream<T>& os_; // output stream
|
||||
bool need_header_; // have we written an XML header yet?
|
||||
std::stack<element<T>*> elements_; // stack of open element tags
|
||||
|
||||
|
||||
// write XML header, if necessary
|
||||
writer& header()
|
||||
{
|
||||
if (need_header_)
|
||||
{
|
||||
// TODO
|
||||
//os_ << "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
|
||||
need_header_ = false;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// write a single character to the output stream
|
||||
writer& putc(T c)
|
||||
{
|
||||
os_.put(c);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// write a string to the output stream
|
||||
writer& puts(const T* str) {
|
||||
os_ << str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class V>
|
||||
friend class element;
|
||||
};
|
||||
|
||||
//
|
||||
// xml::element class
|
||||
//
|
||||
template <class T>
|
||||
class element
|
||||
{
|
||||
public:
|
||||
// create a new element tag, bound to an xml::writer
|
||||
element(writer<T>& wr, std::basic_string<T, std::char_traits<T>, std::allocator<T> > const & name) : wr_(wr), name_(name)
|
||||
{
|
||||
wr_.level_++;
|
||||
check_parent();
|
||||
wr_.header().putc('<').puts(name_.c_str());
|
||||
tagopen_ = true;
|
||||
wr_.elements_.push(this);
|
||||
}
|
||||
|
||||
// close the current element tag
|
||||
~element()
|
||||
{
|
||||
if (!wr_.elements_.empty() && wr_.elements_.top() == this)
|
||||
{
|
||||
wr_.elements_.pop();
|
||||
if (tagopen_)
|
||||
wr_.putc(chars<T>::slash)
|
||||
.putc(chars<T>::right_brocket);
|
||||
else
|
||||
wr_.putc(chars<T>::left_brocket)
|
||||
.putc(chars<T>::slash)
|
||||
.puts(name_.c_str())
|
||||
.putc(chars<T>::right_brocket);
|
||||
}
|
||||
wr_.level_--;
|
||||
}
|
||||
|
||||
// write an attribute for the current element
|
||||
element& attr(const T* name, const T* value)
|
||||
{
|
||||
assert(name != 0);
|
||||
assert(value != 0);
|
||||
assert(tagopen_);
|
||||
wr_.putc(chars<T>::space)
|
||||
.puts(name)
|
||||
.putc(chars<T>::eq)
|
||||
.putc(chars<T>::quote);
|
||||
qputs(value);
|
||||
wr_.putc(chars<T>::quote);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// attr() overload for std::string type
|
||||
element& attr(const T* name, const std::basic_string<T, std::char_traits<T>, std::allocator<T> >& value)
|
||||
{
|
||||
return attr(name, value.c_str());
|
||||
}
|
||||
|
||||
// attr() function template for all streamable types
|
||||
template <class V>
|
||||
element& attr(const T* name, V value)
|
||||
{
|
||||
std::basic_stringstream<T, std::char_traits<T>, std::allocator<T> > ss;
|
||||
ss << value;
|
||||
attr(name, ss.str());
|
||||
return *this;
|
||||
}
|
||||
|
||||
// write text content for the current element
|
||||
element& contents(const T* str)
|
||||
{
|
||||
assert(str != 0);
|
||||
check_parent();
|
||||
qputs(str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// contents() overload for std::string type
|
||||
element& contents(const std::basic_string<T, std::char_traits<T>, std::allocator<T> >& str)
|
||||
{
|
||||
return contents(str.c_str());
|
||||
}
|
||||
|
||||
// contents() function template for all streamable types
|
||||
template <class V>
|
||||
element& contents(V value)
|
||||
{
|
||||
std::basic_stringstream<T, std::char_traits<T>, std::allocator<T> > ss;
|
||||
ss << value;
|
||||
contents(ss.str());
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::basic_ostream<T>& stream()
|
||||
{
|
||||
check_parent();
|
||||
return wr_.os_;
|
||||
}
|
||||
|
||||
// write CDATA section
|
||||
element& cdata(const T * str)
|
||||
{
|
||||
assert(str != 0);
|
||||
check_parent();
|
||||
wr_.puts(chars<T>::cdata_open());
|
||||
wr_.puts(str);
|
||||
wr_.puts(chars<T>::cdata_close());
|
||||
return *this;
|
||||
}
|
||||
|
||||
// cdata() overload for std::string type
|
||||
element& cdata(const std::basic_string<T, std::char_traits<T>, std::allocator<T> >& str)
|
||||
{
|
||||
return cdata(str.c_str());
|
||||
}
|
||||
|
||||
private:
|
||||
writer<T>& wr_; // bound XML writer
|
||||
//const T* name_; // name of current element
|
||||
const std::basic_string<T, std::char_traits<T>, std::allocator<T> > name_;
|
||||
bool tagopen_; // is the element tag for this element still open?
|
||||
|
||||
// write a string quoting characters which have meaning in xml
|
||||
element& qputs(const T* str)
|
||||
{
|
||||
for (; *str; ++str)
|
||||
{
|
||||
switch (*str)
|
||||
{
|
||||
case chars<T>::amp:
|
||||
wr_.puts(chars<T>::amp_str()); break;
|
||||
|
||||
case chars<T>::left_brocket:
|
||||
wr_.puts(chars<T>::left_brocket_str()); break;
|
||||
|
||||
case chars<T>::right_brocket:
|
||||
wr_.puts(chars<T>::right_brocket_str()); break;
|
||||
|
||||
case chars<T>::apos:
|
||||
wr_.puts(chars<T>::apos_str()); break;
|
||||
|
||||
case chars<T>::quote:
|
||||
wr_.puts(chars<T>::quote_str()); break;
|
||||
|
||||
default:
|
||||
wr_.putc(*str); break;
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// check to see if we have a parent tag which needs to be closed
|
||||
void check_parent()
|
||||
{
|
||||
if (!wr_.elements_.empty() && wr_.elements_.top()->tagopen_)
|
||||
{
|
||||
wr_.putc(chars<T>::right_brocket);
|
||||
wr_.elements_.top()->tagopen_ = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
typedef xml::writer::writer<char> xml_writer;
|
||||
typedef xml::writer::element<char> xml_element;
|
||||
|
||||
#define CP_XML_WRITER(STRM) if (bool _b_ = false) {} else for (xml_writer _xml_wr_((STRM));!_b_;_b_=true)
|
||||
#define CP_XML_NODE(NAME) if (bool _b_ = false) {} else for (xml_element _xml_node_(_xml_wr_, (NAME));!_b_;_b_=true)
|
||||
#define CP_XML_ATTR(NAME, VAL) _xml_node_.attr((NAME),(VAL))
|
||||
#define CP_XML_CONTENT(VAL) _xml_node_.contents((VAL))
|
||||
#define CP_XML_STREAM() _xml_node_.stream()
|
||||
|
||||
#define CP_GET_XML_NODE() _xml_node_
|
||||
#define CP_ATTR_NODE xml_element& _xml_node_
|
||||
|
||||
|
||||
|
||||
#define CP_XML_ATTR_OPT(NAME, VAL) if (VAL)CP_XML_ATTR(NAME, (*VAL))
|
||||
|
||||
#define CP_XML_NODE_SIMPLE() std::string NS_NAME = std::string(ns) + std::string(L":") + std::string(name); CP_XML_NODE(NS_NAME)
|
||||
|
||||
@ -81,12 +81,11 @@
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
Name="Debug|x64"
|
||||
OutputDirectory="$(SolutionDir)$(PlatformName)\$(ConfigurationName)"
|
||||
IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
|
||||
ConfigurationType="4"
|
||||
CharacterSet="1"
|
||||
WholeProgramOptimization="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
@ -102,11 +101,15 @@
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
TargetEnvironment="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_LIB"
|
||||
RuntimeLibrary="2"
|
||||
Optimization="0"
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_LIB;USE_LITE_READER;_USE_XMLLITE_READER_;_USE_LIBXML2_READER_;LIBXML_READER_ENABLED"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="3"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="3"
|
||||
Detect64BitPortabilityProblems="true"
|
||||
@ -141,11 +144,12 @@
|
||||
/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Debug|x64"
|
||||
OutputDirectory="$(SolutionDir)$(PlatformName)\$(ConfigurationName)"
|
||||
IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"
|
||||
Name="Release|Win32"
|
||||
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
|
||||
IntermediateDirectory="$(ConfigurationName)"
|
||||
ConfigurationType="4"
|
||||
CharacterSet="1"
|
||||
WholeProgramOptimization="1"
|
||||
>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"
|
||||
@ -161,15 +165,11 @@
|
||||
/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
TargetEnvironment="3"
|
||||
/>
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
PreprocessorDefinitions="WIN32;_DEBUG;_LIB;USE_LITE_READER;_USE_XMLLITE_READER_;_USE_LIBXML2_READER_;LIBXML_READER_ENABLED"
|
||||
MinimalRebuild="true"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="3"
|
||||
PreprocessorDefinitions="WIN32;NDEBUG;_LIB"
|
||||
RuntimeLibrary="2"
|
||||
UsePrecompiledHeader="0"
|
||||
WarningLevel="3"
|
||||
Detect64BitPortabilityProblems="true"
|
||||
@ -277,11 +277,11 @@
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\source\ECMACryptReader.cpp"
|
||||
RelativePath="..\source\ECMACryptFile.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\source\ECMACryptReader.h"
|
||||
RelativePath="..\source\ECMACryptFile.h"
|
||||
>
|
||||
</File>
|
||||
</Files>
|
||||
|
||||
Reference in New Issue
Block a user