This commit is contained in:
Elena Subbotina
2024-07-06 14:00:09 +03:00
parent f465f17b93
commit adfc64f01e

View File

@ -573,7 +573,7 @@ bool WriteStandartEncryptionInfo(unsigned char* data, int &size, _ecmaCryptData
} }
bool ReadStandartEncryptionInfo(unsigned char* data, int size, _ecmaCryptData & cryptData) bool ReadStandartEncryptionInfo(unsigned char* data, int size, _ecmaCryptData & cryptData)
{ {
if (!data || size < 1) return false; if (!data || size < 36) return false;
MemoryStream mem_stream(data, size, false); MemoryStream mem_stream(data, size, false);
//EncryptionHeader //EncryptionHeader
@ -780,43 +780,11 @@ bool ECMACryptFile::EncryptOfficeFile(const std::wstring &file_name_inp, const s
bool bLargeFile = (lengthData > 3 * 1024 * 1024); bool bLargeFile = (lengthData > 3 * 1024 * 1024);
bLargeFile = true; // test ???
//------------------------------------------------------------------- //-------------------------------------------------------------------
POLE::Storage *pStorage = NULL; CFCPP::CompoundFile *pStorageNew = new CFCPP::CompoundFile(CFCPP::Ver_3, CFCPP::Default);
CFCPP::CompoundFile *pStorageNew = NULL;
if (bLargeFile)
{
pStorageNew = new CFCPP::CompoundFile(CFCPP::Ver_3, CFCPP::Default);
}
else
{
pStorage = new POLE::Storage(file_name_out.c_str());
if (!pStorage)return false;
if (!pStorage->open(true, true))
{
delete pStorage;
return false;
}
}
//-------------------------------------------------------------------
if (bLargeFile)
{
std::shared_ptr<CFCPP::CFStream> oPackage = pStorageNew->RootStorage()->AddStream(L"EncryptedPackage");
oPackage->Write((char*)data_out, 0, lengthData);
}
else
{
POLE::Stream *pStream = new POLE::Stream(pStorage, L"EncryptedPackage", true, lengthData);
pStream->write(data_out, lengthData);
pStream->flush();
delete pStream;
}
//------------------------------------------------------------------- //-------------------------------------------------------------------
std::shared_ptr<CFCPP::CFStream> oPackage = pStorageNew->RootStorage()->AddStream(L"EncryptedPackage");
oPackage->Write((char*)data_out, 0, lengthData);
if (data_out) if (data_out)
{ {
@ -826,134 +794,64 @@ bool ECMACryptFile::EncryptOfficeFile(const std::wstring &file_name_inp, const s
cryptor.GetCryptData(cryptData); cryptor.GetCryptData(cryptData);
if (bLargeFile) std::shared_ptr<CFCPP::CFStream> oInfo = pStorageNew->RootStorage()->AddStream(L"EncryptionInfo");
{
std::shared_ptr<CFCPP::CFStream> oInfo = pStorageNew->RootStorage()->AddStream(L"EncryptionInfo");
if (cryptData.bAgile) if (cryptData.bAgile)
{ {
_UINT16 VersionInfoMajor = 0x0004, VersionInfoMinor = 0x0004; //agile _UINT16 VersionInfoMajor = 0x0004, VersionInfoMinor = 0x0004; //agile
std::streamsize position = 0; std::streamsize position = 0;
oInfo->Write((char*)&VersionInfoMajor, position, 2); position += 2; oInfo->Write((char*)&VersionInfoMajor, position, 2); position += 2;
oInfo->Write((char*)&VersionInfoMinor, position, 2); position += 2; oInfo->Write((char*)&VersionInfoMinor, position, 2); position += 2;
_UINT32 nEncryptionInfoFlags = 64; _UINT32 nEncryptionInfoFlags = 64;
oInfo->Write((char*)&nEncryptionInfoFlags, position, 4); position += 4; oInfo->Write((char*)&nEncryptionInfoFlags, position, 4); position += 4;
std::string strXml; std::string strXml;
WriteXmlEncryptionInfo(cryptData, strXml); WriteXmlEncryptionInfo(cryptData, strXml);
oInfo->Write(strXml.c_str(), position, strXml.length()); position += strXml.length(); oInfo->Write(strXml.c_str(), position, strXml.length()); position += strXml.length();
} }
else else
{ {
_UINT16 VersionInfoMajor = 0x0004, VersionInfoMinor = 0x0002; // standart _UINT16 VersionInfoMajor = 0x0004, VersionInfoMinor = 0x0002; // standart
std::streamsize position = 0; std::streamsize position = 0;
oInfo->Write((char*)&VersionInfoMajor, position, 2); position += 2; oInfo->Write((char*)&VersionInfoMajor, position, 2); position += 2;
oInfo->Write((char*)&VersionInfoMinor, position, 2); position += 2; oInfo->Write((char*)&VersionInfoMinor, position, 2); position += 2;
_UINT32 nEncryptionInfoFlags = 0; _UINT32 nEncryptionInfoFlags = 0;
bool fCryptoAPI = true, fDocProps = false, fExternal = false, fAES = cryptData.cipherAlgorithm != CRYPT_METHOD::RC4; bool fCryptoAPI = true, fDocProps = false, fExternal = false, fAES = cryptData.cipherAlgorithm != CRYPT_METHOD::RC4;
SETBIT(nEncryptionInfoFlags, 2, fCryptoAPI); SETBIT(nEncryptionInfoFlags, 2, fCryptoAPI);
SETBIT(nEncryptionInfoFlags, 3, fDocProps); SETBIT(nEncryptionInfoFlags, 3, fDocProps);
SETBIT(nEncryptionInfoFlags, 4, fExternal); SETBIT(nEncryptionInfoFlags, 4, fExternal);
SETBIT(nEncryptionInfoFlags, 5, fAES); SETBIT(nEncryptionInfoFlags, 5, fAES);
oInfo->Write((char*)&nEncryptionInfoFlags, position, 4); position += 4; oInfo->Write((char*)&nEncryptionInfoFlags, position, 4); position += 4;
int nEncryptionInfoSize = 4096; int nEncryptionInfoSize = 4096;
unsigned char* byteEncryptionInfo = new unsigned char[nEncryptionInfoSize]; unsigned char* byteEncryptionInfo = new unsigned char[nEncryptionInfoSize];
WriteStandartEncryptionInfo(byteEncryptionInfo, nEncryptionInfoSize, cryptData); WriteStandartEncryptionInfo(byteEncryptionInfo, nEncryptionInfoSize, cryptData);
oInfo->Write((char*)byteEncryptionInfo, position, 4); position += nEncryptionInfoSize; oInfo->Write((char*)byteEncryptionInfo, position, 4); position += nEncryptionInfoSize;
delete[]byteEncryptionInfo; delete[]byteEncryptionInfo;
} }
}
else
{
POLE::Stream *pStream = new POLE::Stream(pStorage, L"EncryptionInfo", true);
if (cryptData.bAgile)
{
_UINT16 VersionInfoMajor = 0x0004, VersionInfoMinor = 0x0004; //agile
pStream->write((unsigned char*)&VersionInfoMajor, 2);
pStream->write((unsigned char*)&VersionInfoMinor, 2);
_UINT32 nEncryptionInfoFlags = 64;
pStream->write((unsigned char*)&nEncryptionInfoFlags, 4);
std::string strXml;
WriteXmlEncryptionInfo(cryptData, strXml);
pStream->write((unsigned char*)strXml.c_str(), strXml.length());
}
else
{
_UINT16 VersionInfoMajor = 0x0004, VersionInfoMinor = 0x0002; // standart
pStream->write((unsigned char*)&VersionInfoMajor, 2);
pStream->write((unsigned char*)&VersionInfoMinor, 2);
_UINT32 nEncryptionInfoFlags = 0;
bool fCryptoAPI = true, fDocProps = false, fExternal = false, fAES = cryptData.cipherAlgorithm != CRYPT_METHOD::RC4;
SETBIT(nEncryptionInfoFlags, 2, fCryptoAPI);
SETBIT(nEncryptionInfoFlags, 3, fDocProps);
SETBIT(nEncryptionInfoFlags, 4, fExternal);
SETBIT(nEncryptionInfoFlags, 5, fAES);
pStream->write((unsigned char*)&nEncryptionInfoFlags, 4);
int nEncryptionInfoSize = 4096;
unsigned char* byteEncryptionInfo = new unsigned char[nEncryptionInfoSize];
WriteStandartEncryptionInfo(byteEncryptionInfo, nEncryptionInfoSize, cryptData);
pStream->write(byteEncryptionInfo, nEncryptionInfoSize);
delete[]byteEncryptionInfo;
}
pStream->flush();
delete pStream;
}
//------------------------------------------------------------------- //-------------------------------------------------------------------
if (false == documentID.empty()) if (false == documentID.empty())
{ {
std::string utfDocumentID = NSFile::CUtf8Converter::GetUtf8StringFromUnicode(documentID); std::string utfDocumentID = NSFile::CUtf8Converter::GetUtf8StringFromUnicode(documentID);
if (bLargeFile)
{
std::shared_ptr<CFCPP::CFStream> oDocumentID = pStorageNew->RootStorage()->AddStream(L"DocumentID"); std::shared_ptr<CFCPP::CFStream> oDocumentID = pStorageNew->RootStorage()->AddStream(L"DocumentID");
oDocumentID->Write(utfDocumentID.c_str(), 0, utfDocumentID.length()); oDocumentID->Write(utfDocumentID.c_str(), 0, utfDocumentID.length());
}
else
{
POLE::Stream *pStream = new POLE::Stream(pStorage, L"DocumentID", true, utfDocumentID.length());
pStream->write((BYTE*)utfDocumentID.c_str(), utfDocumentID.length());
pStream->flush();
delete pStream;
}
} }
//------------------------------------------------------------------- //-------------------------------------------------------------------
bool result = true; bool result = pStorageNew->Save(file_name_out);
if (bLargeFile) pStorageNew->Close();
{ delete pStorageNew;
result = pStorageNew->Save(file_name_out);
pStorageNew->Close();
delete pStorageNew;
}
else
{
pStorage->close();
delete pStorage;
}
// //
////test back---------------------------------------------------------------------------------test back ////test back---------------------------------------------------------------------------------test back
// ECMADecryptor decryptor; // ECMADecryptor decryptor;
@ -998,7 +896,7 @@ bool ECMACryptFile::DecryptOfficeFile(const std::wstring &file_name_inp, const s
if (pStream) if (pStream)
{ {
if (pStream->fail()) if (pStream->fail() || pStream->size() < 8)
{ {
delete pStream; delete pStream;
delete pStorage; delete pStorage;