x2t - decrtypt Open Office Document

This commit is contained in:
ElenaSubbotina
2018-04-27 15:42:25 +03:00
parent d4a70d306f
commit 2a4ce1f93f
7 changed files with 163 additions and 169 deletions

View File

@ -297,14 +297,11 @@ std::string DecodeBase64(const std::wstring & value1)
}
return result;
}
bool odf_document::Impl::decrypt_file (const std::wstring & srcPath, const std::wstring & dstPath, office_element_ptr element, int size )
bool odf_document::Impl::decrypt_file (const std::wstring & srcPath, const std::wstring & dstPath, office_element_ptr element, int file_size )
{
manifest_encryption_data* encryption_data = dynamic_cast<manifest_encryption_data*>(element.get());
if (!encryption_data) return false;
//std::wstring checksum_;
//std::wstring checksum_type_;
manifest_algorithm* algorithm = dynamic_cast<manifest_algorithm*>(encryption_data->algorithm_.get());
manifest_key_derivation* key_derivation = dynamic_cast<manifest_key_derivation*>(encryption_data->key_derivation_.get());
manifest_start_key_generation* start_key_generation = dynamic_cast<manifest_start_key_generation*>(encryption_data->start_key_generation_.get());
@ -312,86 +309,82 @@ bool odf_document::Impl::decrypt_file (const std::wstring & srcPath, const std::
CRYPT::ODFDecryptor decryptor;
CRYPT::_odfCryptData cryptData;
cryptData.saltValue = DecodeBase64(key_derivation->salt_);
cryptData.saltSize = cryptData.saltValue.length();
cryptData.spinCount = key_derivation->iteration_count_;
//------------------------------------------------------------------------------------------
if (std::wstring::npos != start_key_generation->start_key_generation_name_.find(L"sha"))
if (key_derivation)
{
if (std::wstring::npos != start_key_generation->start_key_generation_name_.find(L"512"))
{
cryptData.hashAlgorithm = CRYPT_METHOD::SHA512;
cryptData.hashSize = 40; //320 bit
}
if (std::wstring::npos != start_key_generation->start_key_generation_name_.find(L"256"))
{
cryptData.hashAlgorithm = CRYPT_METHOD::SHA256;
cryptData.hashSize = 40; //320 bit
}
else
{
cryptData.hashAlgorithm = CRYPT_METHOD::SHA1;
cryptData.hashSize = 20; //160 bit
}
}
else
{
cryptData.hashAlgorithm = CRYPT_METHOD::MD5; //????
cryptData.hashSize = 16; //128 bit
cryptData.saltValue = DecodeBase64(key_derivation->salt_);
cryptData.saltSize = cryptData.saltValue.length();
cryptData.spinCount = key_derivation->iteration_count_;
cryptData.keySize = key_derivation->key_size_;
}
//------------------------------------------------------------------------------------------
cryptData.initializationVector = DecodeBase64(algorithm->initialisation_vector_);
if (start_key_generation)
{
cryptData.start_hashAlgorithm = CRYPT_METHOD::SHA1;
cryptData.start_hashSize = 0x14; //128 bit
if (std::wstring::npos != algorithm->algorithm_name.find(L"aes"))
{
if (std::wstring::npos != algorithm->algorithm_name.find(L"cbc"))
cryptData.cipherAlgorithm = CRYPT_METHOD::AES_CBC;
else
cryptData.cipherAlgorithm = CRYPT_METHOD::AES_ECB;//??
if (std::wstring::npos != algorithm->algorithm_name.find(L"512"))
cryptData.keySize = 512 /8;
else if (std::wstring::npos != algorithm->algorithm_name.find(L"256"))
cryptData.keySize = 256 /8;
else
cryptData.keySize = 128 /8; //??
}
else if (std::wstring::npos != algorithm->algorithm_name.find(L"blowfish"))
{
cryptData.cipherAlgorithm = CRYPT_METHOD::Blowfish;//CFB
if (std::wstring::npos != start_key_generation->start_key_generation_name_.find(L"sha"))
{
if (std::wstring::npos != start_key_generation->start_key_generation_name_.find(L"512"))
{
cryptData.start_hashAlgorithm = CRYPT_METHOD::SHA512;
cryptData.start_hashSize = 0x40; //320 bit
}
if (std::wstring::npos != start_key_generation->start_key_generation_name_.find(L"256"))
{
cryptData.start_hashAlgorithm = CRYPT_METHOD::SHA256;
cryptData.start_hashSize = 0x20; //320 bit
}
}
}
//------------------------------------------------------------------------------------------
cryptData.checksum = DecodeBase64(encryption_data->checksum_);
if (std::wstring::npos != encryption_data->checksum_type_.find(L"sha"))
if (algorithm)
{
if (std::wstring::npos != start_key_generation->start_key_generation_name_.find(L"512"))
cryptData.initializationVector = DecodeBase64(algorithm->initialisation_vector_);
if (std::wstring::npos != algorithm->algorithm_name_.find(L"aes"))
{
cryptData.checksum_hashAlgorithm = CRYPT_METHOD::SHA512;
if (std::wstring::npos != algorithm->algorithm_name_.find(L"cbc"))
cryptData.cipherAlgorithm = CRYPT_METHOD::AES_CBC;
else
cryptData.cipherAlgorithm = CRYPT_METHOD::AES_ECB;//??
}
if (std::wstring::npos != start_key_generation->start_key_generation_name_.find(L"256"))
else if (std::wstring::npos != algorithm->algorithm_name_.find(L"blowfish"))
{
cryptData.checksum_hashAlgorithm = CRYPT_METHOD::SHA256;
}
else
{
cryptData.checksum_hashAlgorithm = CRYPT_METHOD::SHA1;
cryptData.cipherAlgorithm = CRYPT_METHOD::Blowfish_CFB;
}
}
else
//------------------------------------------------------------------------------------------
if (encryption_data)
{
cryptData.checksum_hashAlgorithm = CRYPT_METHOD::MD5; //????
}
size_t nPosChecksumSize = encryption_data->checksum_type_.find(L"-");
if (std::wstring::npos != nPosChecksumSize)
{
std::wstring strSize = encryption_data->checksum_type_.substr(nPosChecksumSize + 1);
if (strSize == L"1k")
cryptData.checksum = DecodeBase64(encryption_data->checksum_);
cryptData.checksum_hashAlgorithm = CRYPT_METHOD::SHA1;
if (std::wstring::npos != encryption_data->checksum_type_.find(L"sha"))
{
cryptData.checksum_size = 1024;
if (std::wstring::npos != encryption_data->checksum_type_.find(L"512"))
{
cryptData.checksum_hashAlgorithm = CRYPT_METHOD::SHA512;
}
if (std::wstring::npos != encryption_data->checksum_type_.find(L"256"))
{
cryptData.checksum_hashAlgorithm = CRYPT_METHOD::SHA256;
}
}
else
size_t nPosChecksumSize = encryption_data->checksum_type_.find(L"-");
if (std::wstring::npos == nPosChecksumSize)
nPosChecksumSize = encryption_data->checksum_type_.find(L"/");
if (std::wstring::npos != nPosChecksumSize)
{
//???
std::wstring strSize = encryption_data->checksum_type_.substr(nPosChecksumSize + 1);
if (strSize == L"1k")
{
cryptData.checksum_size = 1024;
}
else
{
//???
}
}
}
@ -401,14 +394,14 @@ bool odf_document::Impl::decrypt_file (const std::wstring & srcPath, const std::
return false;
DWORD dwSizeRead = 0;
_UINT64 lengthData, lengthRead = file_inp.GetFileSize();
_UINT64 lengthRead = file_inp.GetFileSize();
unsigned char* data = new unsigned char[lengthRead];
unsigned char* data_out = NULL;
file_inp.ReadFile(data, lengthRead, dwSizeRead);
cryptData.input = std::string((char*)data, cryptData.checksum_size) ;
cryptData.checksum_input = std::string((char*)data, cryptData.checksum_size) ;
//------------------------------------------------------------------------------------------
decryptor.SetCryptData(cryptData);
@ -418,26 +411,21 @@ bool odf_document::Impl::decrypt_file (const std::wstring & srcPath, const std::
return false;
}
//------------------------------------------------------------------------------------------------------------
int readData = dwSizeRead;// - 8;
lengthData = *((_UINT64*)data);
bool result = false;
decryptor.Decrypt(data /*+ 8*/, readData, data_out, 0);//todoo сделать покусочное чтение декриптование
decryptor.Decrypt(data, dwSizeRead, data_out, file_size);
delete []data;
if (data_out)
{
NSFile::CFileBinary file_out;
file_out.CreateFileW(dstPath);
file_out.WriteFile(data_out, lengthData);
file_out.WriteFile(data_out, file_size);
file_out.CloseFile();
delete []data_out;
result = true;
delete []data_out;
return true;
}
delete []data;
return result;
return false;
}
const std::wstring & odf_document::Impl::get_temp_folder() const
{

View File

@ -164,6 +164,7 @@ void manifest_encryption_data::add_attributes( const xml::attributes_wc_ptr & At
{
checksum_type_ = checksum_type_.substr(nFind + 1);
}
checksum_type_ = XmlUtils::GetLower(checksum_type_);
}
void manifest_encryption_data::add_child_element(cpdoccore::xml::sax *Reader, const std::wstring &Ns, const std::wstring &Name)
{
@ -187,14 +188,15 @@ const wchar_t * manifest_algorithm::name = L"algorithm";
void manifest_algorithm::add_attributes( const xml::attributes_wc_ptr & Attributes )
{
CP_APPLY_ATTR(L"manifest:algorithm-name", algorithm_name, std::wstring(L""));
CP_APPLY_ATTR(L"manifest:algorithm-name", algorithm_name_, std::wstring(L""));
CP_APPLY_ATTR(L"manifest:initialisation-vector", initialisation_vector_, std::wstring(L""));
size_t nFind = algorithm_name.find(L"#");
size_t nFind = algorithm_name_.find(L"#");
if (nFind != std::wstring::npos)
{
algorithm_name = algorithm_name.substr(nFind + 1);
algorithm_name_ = algorithm_name_.substr(nFind + 1);
}
algorithm_name_ = XmlUtils::GetLower(algorithm_name_);
}
// manifest:key-derivation
//////////////////////////////////////////////////////////////////////////////////////////////////
@ -213,6 +215,7 @@ void manifest_key_derivation::add_attributes( const xml::attributes_wc_ptr & Att
{
key_derivation_name_ = key_derivation_name_.substr(nFind + 1);
}
key_derivation_name_ = XmlUtils::GetLower(key_derivation_name_);
}
// manifest:start-key-generation
//////////////////////////////////////////////////////////////////////////////////////////////////
@ -229,6 +232,7 @@ void manifest_start_key_generation::add_attributes( const xml::attributes_wc_ptr
{
start_key_generation_name_ = start_key_generation_name_.substr(nFind + 1);
}
start_key_generation_name_ = XmlUtils::GetLower(start_key_generation_name_);
}
}
}

View File

@ -253,7 +253,7 @@ public:
CPDOCCORE_DEFINE_VISITABLE();
std::wstring algorithm_name;
std::wstring algorithm_name_;
std::wstring initialisation_vector_;
};
CP_REGISTER_OFFICE_ELEMENT2(manifest_algorithm);

View File

@ -46,10 +46,14 @@
#include "../../Common/3dParty/cryptopp/filters.h"
#include "../../Common/3dParty/cryptopp/osrng.h"
#include "../../Common/3dParty/cryptopp/hex.h"
#include "../../Common/3dParty/cryptopp/blowfish.h"
#include "../../Common/DocxFormat/Source/Base/unicode_util.h"
#include "../../Common/DocxFormat/Source/Base/Types_32.h"
#include "../../DesktopEditor/common/File.h"
#include "../../OfficeUtils/src/OfficeUtils.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 };
@ -284,36 +288,18 @@ _buf GenerateAgileKey(_buf & salt, _buf & password, _buf & blockKey, int hashSiz
return _buf(pHashBuf.ptr, pHashBuf.size);
}
_buf GenerateOdfKey(_buf & salt, _buf & password, _buf & blockKey, int hashSize, int spin, CRYPT_METHOD::_hashAlgorithm algorithm, int block_index = 0)
_buf GenerateOdfKey(_buf & pSalt, _buf & pPassword, int keySize, int spin, CRYPT_METHOD::_hashAlgorithm algorithm)
{
_buf pHashBuf = HashAppend(salt, password, algorithm);
_buf pKey (keySize);
_buf empty (NULL, 0, false);
for (int i = 0; i < spin; i++)
{
_buf iterator((unsigned char*)&i, 4, false);
pHashBuf = HashAppend(iterator, pHashBuf, algorithm);
}
_buf pPassword_hash = HashAppend(pPassword, empty, algorithm);
pHashBuf = HashAppend(pHashBuf, blockKey, algorithm);
PKCS5_PBKDF2_HMAC<SHA1> pbkdf;
pbkdf.DeriveKey(pKey.ptr, pKey.size, 0, pPassword_hash.ptr, pPassword_hash.size, pSalt.ptr, pSalt.size, spin);
CorrectHashSize(pHashBuf, hashSize, 0x36);
return _buf(pHashBuf.ptr, pHashBuf.size);
//unsigned char *out = NULL;
//SecByteBlock derived(hashSize);
//PKCS5_PBKDF2_HMAC<SHA256> pbkdf2;
//pbkdf2.DeriveKey(
// derived.data(),
// derived.size(),
// 0,
// password.ptr, password.size,
// salt.ptr, salt.size,
// spin);
//
//return _buf(derived.data(), derived.size());
return _buf(pKey.ptr, pKey.size);
}
_buf GenerateHashKey(_buf & salt, _buf & password, int hashSize, int spin, CRYPT_METHOD::_hashAlgorithm algorithm, int block_index = 0)
@ -434,6 +420,11 @@ bool DecryptCipher(_buf & key, _buf & iv, _buf & data_inp, _buf & data_out, CRY
{
rc4Decryption.ProcessData(data_out.ptr, data_inp.ptr, data_inp.size);
}
else if (algorithm == CRYPT_METHOD::Blowfish_CFB)
{
CFB_Mode<Blowfish>::Decryption decryption(key.ptr, key.size, iv.ptr);
decryption.ProcessData(data_out.ptr, data_inp.ptr, data_inp.size);
}
else //AES
{
StreamTransformation *modeDecryption = NULL;
@ -951,36 +942,22 @@ ODFDecryptor::~ODFDecryptor()
bool ODFDecryptor::SetPassword(std::wstring _password)
{
bVerify = false;
password = _password;
wpassword = _password;
password = NSFile::CUtf8Converter::GetUtf8StringFromUnicode(_password);
if (password.empty()) return false;
_buf pPassword (password);
_buf pSalt (cryptData.saltValue);
_buf empty (NULL, 0, false);
_buf pEncVerInput (cryptData.input);
_buf pEncVerValue (cryptData.checksum);
_buf pInput (cryptData.checksum_input);
_buf pChecksum (cryptData.checksum);
_buf pOutput (pInput.size);
_buf pIvi (cryptData.initializationVector);
_buf hashBufIvi = HashAppend(pIvi, empty, cryptData.hashAlgorithm);
Decrypt(pInput.ptr, pInput.size, pOutput.ptr, cryptData.checksum_size);
//_buf verifierKey1 = GenerateHashKey(pSalt, pPassword, cryptData.hashSize, cryptData.spinCount, cryptData.checksum_hashAlgorithm);
_buf verifierKey = GenerateOdfKey(pSalt, pPassword, pIvi, cryptData.hashSize, cryptData.spinCount, cryptData.checksum_hashAlgorithm);
CorrectHashSize(verifierKey, cryptData.keySize, 0);//??
_buf pOutputHash = HashAppend(pOutput, empty, cryptData.checksum_hashAlgorithm);
////--------------------------------------------
_buf decryptedVerifierHashInputBytes;
DecryptCipher(verifierKey, pIvi, pEncVerInput, decryptedVerifierHashInputBytes, cryptData.cipherAlgorithm);
//--------------------------------------------
_buf hashBuf = HashAppend(decryptedVerifierHashInputBytes, empty, cryptData.hashAlgorithm);
//--------------------------------------------
_buf decryptedVerifierHashBytes;
DecryptCipher(verifierKey, pIvi, pEncVerValue, decryptedVerifierHashBytes, cryptData.cipherAlgorithm);
bVerify = (decryptedVerifierHashBytes==hashBuf);
bVerify = (pChecksum == pOutputHash);
return true;
}
@ -995,36 +972,55 @@ void ODFDecryptor::SetCryptData(_odfCryptData & data)
cryptData = data;
}
void ODFDecryptor::Decrypt(char* data, const size_t size, const unsigned long start_iv_block)
{
if (!bVerify) return;
{//without deflate
//if (!bVerify) return;
unsigned char* data_out = NULL;
Decrypt((unsigned char*)data, size, data_out, start_iv_block);
_buf pPassword (wpassword);
_buf pSalt (cryptData.saltValue);
_buf ivi (cryptData.initializationVector);
_buf empty (NULL, 0, false);
if (data_out)
{
memcpy(data, data_out, size);
delete []data_out;
}
_buf pKey = GenerateOdfKey(pSalt, pPassword, cryptData.keySize, cryptData.spinCount, cryptData.start_hashAlgorithm);
_buf pInp((unsigned char*)data, size, false);
_buf pOut(size);
DecryptCipher(pKey, ivi, pInp, pOut, cryptData.cipherAlgorithm);
memcpy(data, pOut.ptr, size);
}
void ODFDecryptor::Decrypt(unsigned char* data_inp, int size, unsigned char*& data_out, unsigned long start_iv_block)
void ODFDecryptor::Decrypt(unsigned char* data_inp, int size_inp, unsigned char*& data_out, int &size_out)
{
data_out = new unsigned char[size];
//if (!bVerify)
//{
// size_out = 0;
// return;
//}
if (size_out < 1) size_out = size_inp;
_buf pPassword (password);
_buf pSalt (cryptData.saltValue);
_buf ivi (cryptData.initializationVector);
_buf empty (NULL, 0, false);
_buf start (cryptData.initializationVector);
_buf hashKey = GenerateOdfKey(pSalt, pPassword, start, cryptData.hashSize, cryptData.spinCount, cryptData.hashAlgorithm, start_iv_block);
CorrectHashSize(hashKey, cryptData.keySize, 0);
_buf pInp(data_inp, size, false);
_buf pOut(data_out, size, false);
_buf ivi(cryptData.initializationVector);
_buf pKey = GenerateOdfKey(pSalt, pPassword, cryptData.keySize, cryptData.spinCount, cryptData.start_hashAlgorithm);
DecryptCipher(hashKey, ivi, pInp, pOut, cryptData.cipherAlgorithm);
_buf pInp(data_inp, size_inp, false);
_buf pOut(size_inp);
DecryptCipher(pKey, ivi, pInp, pOut, cryptData.cipherAlgorithm);
data_out = new unsigned char[size_out];
CInflate inflate;
inflate.SetOut(data_out, size_out);
inflate.SetIn(pOut.ptr, pOut.size);
inflate.Init2();
int nRes = inflate.Process(DEFLATE_FINISH);
inflate.End();
}

View File

@ -55,7 +55,7 @@ namespace CRYPT_METHOD
AES_CBC,
AES_CFB,
AES_ECB,
Blowfish
Blowfish_CFB
};
}
namespace CRYPT
@ -109,20 +109,20 @@ struct _ecmaCryptData
struct _odfCryptData
{
CRYPT_METHOD::_cipherAlgorithm cipherAlgorithm = CRYPT_METHOD::AES_CBC;
CRYPT_METHOD::_hashAlgorithm hashAlgorithm = CRYPT_METHOD::SHA1;
CRYPT_METHOD::_hashAlgorithm start_hashAlgorithm = CRYPT_METHOD::SHA256;
int start_hashSize = 0x20;
int spinCount = 100000;
int keySize = 0x10;
int hashSize = 0x14;
int keySize = 0x20;
int saltSize = 0x10;
std::string saltValue;
std::string initializationVector;
std::string input;
std::string checksum_input;
std::string checksum;
int checksum_size = 1024;
CRYPT_METHOD::_hashAlgorithm checksum_hashAlgorithm = CRYPT_METHOD::SHA1;
CRYPT_METHOD::_hashAlgorithm checksum_hashAlgorithm = CRYPT_METHOD::SHA256;
};
//---------------------------------------------------------------------------------------------------
class ECMAEncryptor
@ -182,17 +182,19 @@ public:
virtual void Decrypt (char* data, const size_t size, const unsigned long stream_pos, const size_t block_size){}
virtual void Decrypt (char* data, const size_t size, const unsigned long start_iv_block);
//without deflate
virtual bool SetPassword (std::wstring password);
virtual bool IsVerify();
void SetCryptData(_odfCryptData &data);
void Decrypt (unsigned char* data, int size, unsigned char*& data_out, unsigned long start_iv_block);
void Decrypt (unsigned char* data_inp, int size_inp, unsigned char*& data_out, int &size_out);
private:
std::wstring password;
std::wstring wpassword;
std::string password; //utf8
_odfCryptData cryptData;
bool bVerify;
};

View File

@ -282,7 +282,10 @@ void CInflate::Init()
{
inflateInit(&m_internal->m_stream);
}
void CInflate::Init2()
{
inflateInit2(&m_internal->m_stream,-MAX_WBITS);
}
int CInflate::Process(int flush)
{
return inflate(&m_internal->m_stream, flush);

View File

@ -121,6 +121,7 @@ public:
void ClearFuncs();
void Init();
void Init2();
int Process(int flush);
void End();
};