From 5b30a94842388d136f645ed7d16a6941da86b760 Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Sun, 22 Jan 2017 22:26:41 +0100 Subject: oox: clean-up crypto classes, use c++11 features MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - remove "using namespace std;" - &vector[0] to vector.data() - use nullptr in OPENSSL Change-Id: Ib4067b0256801f94d448bc8d3faf5a2902d694e5 Reviewed-on: https://gerrit.libreoffice.org/33629 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl --- oox/source/crypto/AgileEngine.cxx | 106 ++++++++++++-------------- oox/source/crypto/CryptTools.cxx | 100 ++++++++++++------------- oox/source/crypto/DocumentDecryption.cxx | 125 ++++++++++++++----------------- oox/source/crypto/DocumentEncryption.cxx | 30 ++++---- oox/source/crypto/Standard2007Engine.cxx | 95 +++++++++++------------ 5 files changed, 211 insertions(+), 245 deletions(-) (limited to 'oox') diff --git a/oox/source/crypto/AgileEngine.cxx b/oox/source/crypto/AgileEngine.cxx index 3897b877b294..9c1095a01f4d 100644 --- a/oox/source/crypto/AgileEngine.cxx +++ b/oox/source/crypto/AgileEngine.cxx @@ -16,34 +16,23 @@ namespace oox { namespace core { -using namespace std; - namespace { -const sal_uInt8 constBlock1[] = { 0xfe, 0xa7, 0xd2, 0x76, 0x3b, 0x4b, 0x9e, 0x79 }; -const sal_uInt8 constBlock2[] = { 0xd7, 0xaa, 0x0f, 0x6d, 0x30, 0x61, 0x34, 0x4e }; -const sal_uInt8 constBlock3[] = { 0x14, 0x6e, 0x0b, 0xe7, 0xab, 0xac, 0xd0, 0xd6 }; +const sal_uInt32 constSegmentLength = 4096; -bool hashCalc( std::vector& output, - std::vector& input, - const OUString& algorithm ) +bool hashCalc(std::vector& output, + std::vector& input, + const OUString& sAlgorithm ) { - if (algorithm == "SHA1") + if (sAlgorithm == "SHA1") return Digest::sha1(output, input); - else if (algorithm == "SHA512") + else if (sAlgorithm == "SHA512") return Digest::sha512(output, input); return false; } } // namespace -AgileEngine::AgileEngine() : - CryptoEngine() -{} - -AgileEngine::~AgileEngine() -{} - Crypto::CryptoType AgileEngine::cryptoType(const AgileEncryptionInfo& rInfo) { if (rInfo.keyBits == 128 && rInfo.cipherAlgorithm == "AES" && rInfo.cipherChaining == "ChainingModeCBC") @@ -54,39 +43,35 @@ Crypto::CryptoType AgileEngine::cryptoType(const AgileEncryptionInfo& rInfo) } void AgileEngine::calculateBlock( - const sal_uInt8* rBlock, - sal_uInt32 aBlockSize, - vector& rHashFinal, - vector& rInput, - vector& rOutput) + std::vector const & rBlock, + std::vector& rHashFinal, + std::vector& rInput, + std::vector& rOutput) { - vector hash(mInfo.hashSize, 0); - vector salt = mInfo.saltValue; - vector dataFinal(mInfo.hashSize + aBlockSize, 0); + std::vector hash(mInfo.hashSize, 0); + std::vector dataFinal(mInfo.hashSize + rBlock.size(), 0); std::copy(rHashFinal.begin(), rHashFinal.end(), dataFinal.begin()); - std::copy( - rBlock, - rBlock + aBlockSize, - dataFinal.begin() + mInfo.hashSize); + std::copy(rBlock.begin(), rBlock.end(), dataFinal.begin() + mInfo.hashSize); hashCalc(hash, dataFinal, mInfo.hashAlgorithm); sal_Int32 keySize = mInfo.keyBits / 8; - vector key(keySize, 0); + std::vector key(keySize, 0); std::copy(hash.begin(), hash.begin() + keySize, key.begin()); - Decrypt aDecryptor(key, salt, cryptoType(mInfo)); + Decrypt aDecryptor(key, mInfo.saltValue, cryptoType(mInfo)); aDecryptor.update(rOutput, rInput); } -void AgileEngine::calculateHashFinal(const OUString& rPassword, vector& aHashFinal) +void AgileEngine::calculateHashFinal(const OUString& rPassword, std::vector& aHashFinal) { sal_Int32 saltSize = mInfo.saltSize; - vector salt = mInfo.saltValue; + std::vector& salt = mInfo.saltValue; + sal_uInt32 passwordByteLength = rPassword.getLength() * 2; - vector initialData(saltSize + passwordByteLength); + std::vector initialData(saltSize + passwordByteLength); std::copy(salt.begin(), salt.end(), initialData.begin()); const sal_uInt8* passwordByteArray = reinterpret_cast(rPassword.getStr()); @@ -96,15 +81,15 @@ void AgileEngine::calculateHashFinal(const OUString& rPassword, vector hash(mInfo.hashSize, 0); + std::vector hash(mInfo.hashSize, 0); hashCalc(hash, initialData, mInfo.hashAlgorithm); - vector data(mInfo.hashSize + 4, 0); + std::vector data(mInfo.hashSize + 4, 0); for (sal_Int32 i = 0; i < mInfo.spinCount; i++) { - ByteOrderConverter::writeLittleEndian( &data[0], i ); + ByteOrderConverter::writeLittleEndian(data.data(), i); std::copy(hash.begin(), hash.end(), data.begin() + 4); hashCalc(hash, data, mInfo.hashAlgorithm); } @@ -114,60 +99,63 @@ void AgileEngine::calculateHashFinal(const OUString& rPassword, vector constBlock1{ 0xfe, 0xa7, 0xd2, 0x76, 0x3b, 0x4b, 0x9e, 0x79 }; + static const std::vector constBlock2{ 0xd7, 0xaa, 0x0f, 0x6d, 0x30, 0x61, 0x34, 0x4e }; + static const std::vector constBlock3{ 0x14, 0x6e, 0x0b, 0xe7, 0xab, 0xac, 0xd0, 0xd6 }; + mKey.clear(); mKey.resize(mInfo.keyBits / 8, 0); - vector hashFinal(mInfo.hashSize, 0); + std::vector hashFinal(mInfo.hashSize, 0); calculateHashFinal(rPassword, hashFinal); - vector encryptedHashInput = mInfo.encryptedVerifierHashInput; - vector hashInput(mInfo.saltSize, 0); - calculateBlock(constBlock1, sizeof(constBlock1), hashFinal, encryptedHashInput, hashInput); + std::vector& encryptedHashInput = mInfo.encryptedVerifierHashInput; + std::vector hashInput(mInfo.saltSize, 0); + calculateBlock(constBlock1, hashFinal, encryptedHashInput, hashInput); - vector encryptedHashValue = mInfo.encryptedVerifierHashValue; - vector hashValue(encryptedHashValue.size(), 0); - calculateBlock(constBlock2, sizeof(constBlock2), hashFinal, encryptedHashValue, hashValue); + std::vector& encryptedHashValue = mInfo.encryptedVerifierHashValue; + std::vector hashValue(encryptedHashValue.size(), 0); + calculateBlock(constBlock2, hashFinal, encryptedHashValue, hashValue); - vector hash(mInfo.hashSize, 0); + std::vector hash(mInfo.hashSize, 0); hashCalc(hash, hashInput, mInfo.hashAlgorithm); if (std::equal (hash.begin(), hash.end(), hashValue.begin()) ) { - vector encryptedKeyValue = mInfo.encryptedKeyValue; - calculateBlock(constBlock3, sizeof(constBlock3), hashFinal, encryptedKeyValue, mKey); + std::vector& encryptedKeyValue = mInfo.encryptedKeyValue; + calculateBlock(constBlock3, hashFinal, encryptedKeyValue, mKey); return true; } return false; } -bool AgileEngine::decrypt( - BinaryXInputStream& aInputStream, - BinaryXOutputStream& aOutputStream) +bool AgileEngine::decrypt(BinaryXInputStream& aInputStream, + BinaryXOutputStream& aOutputStream) { sal_uInt32 totalSize = aInputStream.readuInt32(); // Document unencrypted size - 4 bytes - aInputStream.skip( 4 ); // Reserved 4 Bytes + aInputStream.skip(4); // Reserved 4 Bytes - vector keyDataSalt = mInfo.keyDataSalt; + std::vector& keyDataSalt = mInfo.keyDataSalt; sal_uInt32 saltSize = mInfo.saltSize; sal_uInt32 keySize = mInfo.keyBits / 8; sal_uInt32 segment = 0; - vector saltWithBlockKey(saltSize + sizeof(segment), 0); + std::vector saltWithBlockKey(saltSize + sizeof(segment), 0); std::copy(keyDataSalt.begin(), keyDataSalt.end(), saltWithBlockKey.begin()); - vector hash(mInfo.hashSize, 0); - vector iv(keySize, 0); + std::vector hash(mInfo.hashSize, 0); + std::vector iv(keySize, 0); - vector inputBuffer (SEGMENT_LENGTH); - vector outputBuffer(SEGMENT_LENGTH); + std::vector inputBuffer(constSegmentLength); + std::vector outputBuffer(constSegmentLength); sal_uInt32 inputLength; sal_uInt32 outputLength; sal_uInt32 remaining = totalSize; - while( (inputLength = aInputStream.readMemory( &inputBuffer[0], SEGMENT_LENGTH )) > 0 ) + while ((inputLength = aInputStream.readMemory(inputBuffer.data(), constSegmentLength)) > 0) { sal_uInt8* segmentBegin = reinterpret_cast(&segment); sal_uInt8* segmentEnd = segmentBegin + sizeof(segment); @@ -182,7 +170,7 @@ bool AgileEngine::decrypt( outputLength = aDecryptor.update(outputBuffer, inputBuffer, inputLength); sal_uInt32 writeLength = outputLength > remaining ? remaining : outputLength; - aOutputStream.writeMemory( &outputBuffer[0], writeLength ); + aOutputStream.writeMemory(outputBuffer.data(), writeLength); remaining -= outputLength; segment++; diff --git a/oox/source/crypto/CryptTools.cxx b/oox/source/crypto/CryptTools.cxx index 4bb3ec2f2d28..b1e3345c5453 100644 --- a/oox/source/crypto/CryptTools.cxx +++ b/oox/source/crypto/CryptTools.cxx @@ -15,8 +15,6 @@ namespace oox { namespace core { -using namespace std; - Crypto::Crypto() #if USE_TLS_NSS : mContext(nullptr) @@ -61,7 +59,7 @@ const EVP_CIPHER* Crypto::getCipher(CryptoType type) #endif #if USE_TLS_NSS -void Crypto::setupContext(vector& key, vector& iv, CryptoType type, CK_ATTRIBUTE_TYPE operation) +void Crypto::setupContext(std::vector& key, std::vector& iv, CryptoType type, CK_ATTRIBUTE_TYPE operation) { CK_MECHANISM_TYPE mechanism = static_cast(-1); @@ -70,7 +68,7 @@ void Crypto::setupContext(vector& key, vector& iv, CryptoT if(iv.empty()) ivItem.data = nullptr; else - ivItem.data = &iv[0]; + ivItem.data = iv.data(); ivItem.len = iv.size(); SECItem* pIvItem = nullptr; @@ -92,37 +90,37 @@ void Crypto::setupContext(vector& key, vector& iv, CryptoT break; } - PK11SlotInfo* pSlot( PK11_GetBestSlot( mechanism, nullptr ) ); + PK11SlotInfo* pSlot(PK11_GetBestSlot(mechanism, nullptr)); if (!pSlot) throw css::uno::RuntimeException("NSS Slot failure", css::uno::Reference()); SECItem keyItem; keyItem.type = siBuffer; - keyItem.data = &key[0]; + keyItem.data = key.data(); keyItem.len = key.size(); - mSymKey = PK11_ImportSymKey( pSlot, mechanism, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, nullptr ); - mSecParam = PK11_ParamFromIV( mechanism, pIvItem ); - mContext = PK11_CreateContextBySymKey( mechanism, operation, mSymKey, mSecParam ); + mSymKey = PK11_ImportSymKey(pSlot, mechanism, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, nullptr); + mSecParam = PK11_ParamFromIV(mechanism, pIvItem); + mContext = PK11_CreateContextBySymKey(mechanism, operation, mSymKey, mSecParam); } #endif // USE_TLS_NSS // DECRYPT -Decrypt::Decrypt(vector& key, vector& iv, CryptoType type) : - Crypto() +Decrypt::Decrypt(std::vector& key, std::vector& iv, CryptoType type) + : Crypto() { #if USE_TLS_OPENSSL - EVP_CIPHER_CTX_init( &mContext ); + EVP_CIPHER_CTX_init(&mContext); const EVP_CIPHER* cipher = getCipher(type); if (iv.empty()) - EVP_DecryptInit_ex( &mContext, cipher, NULL, &key[0], 0 ); + EVP_DecryptInit_ex(&mContext, cipher, nullptr, key.data(), 0); else - EVP_DecryptInit_ex( &mContext, cipher, NULL, &key[0], &iv[0] ); - EVP_CIPHER_CTX_set_padding( &mContext, 0 ); + EVP_DecryptInit_ex(&mContext, cipher, nullptr, key.data(), iv.data()); + EVP_CIPHER_CTX_set_padding(&mContext, 0); #endif #if USE_TLS_NSS @@ -130,27 +128,27 @@ Decrypt::Decrypt(vector& key, vector& iv, CryptoType type) #endif // USE_TLS_NSS } -sal_uInt32 Decrypt::update(vector& output, vector& input, sal_uInt32 inputLength) +sal_uInt32 Decrypt::update(std::vector& output, std::vector& input, sal_uInt32 inputLength) { int outputLength = 0; sal_uInt32 actualInputLength = inputLength == 0 || inputLength > input.size() ? input.size() : inputLength; #if USE_TLS_OPENSSL - (void)EVP_DecryptUpdate( &mContext, &output[0], &outputLength, &input[0], actualInputLength ); + (void)EVP_DecryptUpdate(&mContext, output.data(), &outputLength, input.data(), actualInputLength); #endif // USE_TLS_OPENSSL #if USE_TLS_NSS - (void)PK11_CipherOp( mContext, &output[0], &outputLength, actualInputLength, &input[0], actualInputLength ); + (void)PK11_CipherOp( mContext, output.data(), &outputLength, actualInputLength, input.data(), actualInputLength ); #endif // USE_TLS_NSS return static_cast(outputLength); } -sal_uInt32 Decrypt::aes128ecb(vector& output, vector& input, vector& key) +sal_uInt32 Decrypt::aes128ecb(std::vector& output, std::vector& input, std::vector& key) { sal_uInt32 outputLength = 0; - vector iv; + std::vector iv; Decrypt crypto(key, iv, Crypto::AES_128_ECB); outputLength = crypto.update(output, input); return outputLength; @@ -158,19 +156,19 @@ sal_uInt32 Decrypt::aes128ecb(vector& output, vector& inpu // ENCRYPT -Encrypt::Encrypt(vector& key, vector& iv, CryptoType type) : - Crypto() +Encrypt::Encrypt(std::vector& key, std::vector& iv, CryptoType type) + : Crypto() { #if USE_TLS_OPENSSL - EVP_CIPHER_CTX_init( &mContext ); + EVP_CIPHER_CTX_init(&mContext); const EVP_CIPHER* cipher = getCipher(type); if (iv.empty()) - EVP_EncryptInit_ex( &mContext, cipher, NULL, &key[0], 0 ); + EVP_EncryptInit_ex(&mContext, cipher, nullptr, key.data(), 0); else - EVP_EncryptInit_ex( &mContext, cipher, NULL, &key[0], &iv[0] ); - EVP_CIPHER_CTX_set_padding( &mContext, 0 ); + EVP_EncryptInit_ex(&mContext, cipher, nullptr, key.data(), iv.data()); + EVP_CIPHER_CTX_set_padding(&mContext, 0); #endif #if USE_TLS_NSS @@ -178,18 +176,18 @@ Encrypt::Encrypt(vector& key, vector& iv, CryptoType type) #endif // USE_TLS_NSS } -sal_uInt32 Encrypt::update(vector& output, vector& input, sal_uInt32 inputLength) +sal_uInt32 Encrypt::update(std::vector& output, std::vector& input, sal_uInt32 inputLength) { int outputLength = 0; sal_uInt32 actualInputLength = inputLength == 0 || inputLength > input.size() ? input.size() : inputLength; #if USE_TLS_OPENSSL - (void)EVP_EncryptUpdate( &mContext, &output[0], &outputLength, &input[0], actualInputLength ); + (void)EVP_EncryptUpdate(&mContext, output.data(), &outputLength, input.data(), actualInputLength); #endif // USE_TLS_OPENSSL #if USE_TLS_NSS - (void)PK11_CipherOp( mContext, &output[0], &outputLength, actualInputLength, &input[0], actualInputLength ); + (void)PK11_CipherOp(mContext, output.data(), &outputLength, actualInputLength, input.data(), actualInputLength); #endif // USE_TLS_NSS return static_cast(outputLength); @@ -237,29 +235,29 @@ HASH_HashType lclNSSgetHashType(Digest::DigestType eType) Digest::Digest(DigestType eType) : meType(eType) { - #if USE_TLS_OPENSSL +#if USE_TLS_OPENSSL mpContext = EVP_MD_CTX_create(); EVP_DigestInit_ex(mpContext, lclOpenSSLgetEngine(eType), NULL); - #endif +#endif - #if USE_TLS_NSS +#if USE_TLS_NSS NSS_NoDB_Init(nullptr); mpContext = HASH_Create(lclNSSgetHashType(eType)); HASH_Begin(mpContext); - #endif +#endif } Digest::~Digest() { - #if USE_TLS_OPENSSL +#if USE_TLS_OPENSSL if(mpContext) EVP_MD_CTX_destroy(mpContext); - #endif +#endif - #if USE_TLS_NSS +#if USE_TLS_NSS if(mpContext) HASH_Destroy(mpContext); - #endif +#endif } sal_uInt32 Digest::getLength() @@ -278,33 +276,33 @@ sal_uInt32 Digest::getLength() void Digest::update(std::vector& input) { - #if USE_TLS_OPENSSL - EVP_DigestUpdate(mpContext, &input[0], input.size()); - #endif - #if USE_TLS_NSS - HASH_Update(mpContext, &input[0], input.size()); - #endif +#if USE_TLS_OPENSSL + EVP_DigestUpdate(mpContext, input.data(), input.size()); +#endif +#if USE_TLS_NSS + HASH_Update(mpContext, input.data(), input.size()); +#endif } void Digest::finalize(std::vector& digest) { digest.clear(); - #if USE_TLS_OPENSSL +#if USE_TLS_OPENSSL unsigned int digestWrittenLength; digest.resize(getLength(), 0); - EVP_DigestFinal_ex(mpContext, &digest[0], &digestWrittenLength); - #endif + EVP_DigestFinal_ex(mpContext, digest.data(), &digestWrittenLength); +#endif - #if USE_TLS_NSS +#if USE_TLS_NSS unsigned int digestWrittenLength; unsigned int digestLength = static_cast(getLength()); digest.resize(digestLength, 0); - HASH_End(mpContext, &digest[0], &digestWrittenLength, digestLength); - #endif + HASH_End(mpContext, digest.data(), &digestWrittenLength, digestLength); +#endif } -bool Digest::sha1(vector& output, vector& input) +bool Digest::sha1(std::vector& output, std::vector& input) { bool aResult = false; @@ -315,7 +313,7 @@ bool Digest::sha1(vector& output, vector& input) return aResult; } -bool Digest::sha512(vector& output, vector& input) +bool Digest::sha512(std::vector& output, std::vector& input) { bool aResult = false; diff --git a/oox/source/crypto/DocumentDecryption.cxx b/oox/source/crypto/DocumentDecryption.cxx index ed1111e8a549..8f0084c639b1 100644 --- a/oox/source/crypto/DocumentDecryption.cxx +++ b/oox/source/crypto/DocumentDecryption.cxx @@ -37,20 +37,15 @@ using namespace css::uno; using namespace css::xml::sax; using namespace css::xml; -using namespace std; - -using ::comphelper::SequenceAsHashMap; -using ::sax::Converter; - namespace { -vector convertToVector(Sequence& input) +std::vector convertToVector(Sequence& input) { - const sal_uInt8* inputArray = reinterpret_cast( input.getConstArray() ); - return vector(inputArray, inputArray + input.getLength()); + const sal_uInt8* inputArray = reinterpret_cast(input.getConstArray()); + return std::vector(inputArray, inputArray + input.getLength()); } -class AgileTokenHandler : public cppu::WeakImplHelper< XFastTokenHandler > +class AgileTokenHandler : public cppu::WeakImplHelper { public: virtual sal_Int32 SAL_CALL getTokenFromUTF8( const Sequence< sal_Int8 >& /*nIdentifier*/ ) override @@ -64,7 +59,7 @@ public: } }; -class AgileDocumentHandler : public ::cppu::WeakImplHelper< XFastDocumentHandler > +class AgileDocumentHandler : public ::cppu::WeakImplHelper { AgileEncryptionInfo& mInfo; @@ -82,81 +77,78 @@ public: void SAL_CALL startFastElement( sal_Int32 /*Element*/, const Reference< XFastAttributeList >& /*Attribs*/ ) override {} - void SAL_CALL startUnknownElement( const OUString& /*aNamespace*/, const OUString& aName, const Reference< XFastAttributeList >& aAttributeList ) override + void SAL_CALL startUnknownElement( const OUString& /*aNamespace*/, const OUString& rName, const Reference< XFastAttributeList >& aAttributeList ) override { - if(aName == "keyData") + if (rName == "keyData") { - Sequence aAttributes(aAttributeList->getUnknownAttributes()); - - for (int i=0; igetUnknownAttributes()) { - if (aAttributes[i].Name == "saltValue") + if (rAttribute.Name == "saltValue") { Sequence keyDataSalt; - Converter::decodeBase64(keyDataSalt, aAttributes[i].Value); + ::sax::Converter::decodeBase64(keyDataSalt, rAttribute.Value); mInfo.keyDataSalt = convertToVector(keyDataSalt); } } } - else if(aName == "encryptedKey") + else if (rName == "encryptedKey") { - Sequence aAttributes(aAttributeList->getUnknownAttributes()); - for (int i=0; igetUnknownAttributes()) { - if (aAttributes[i].Name == "spinCount") + if (rAttribute.Name == "spinCount") { - Converter::convertNumber(mInfo.spinCount, aAttributes[i].Value); + ::sax::Converter::convertNumber(mInfo.spinCount, rAttribute.Value); } - else if (aAttributes[i].Name == "saltSize") + else if (rAttribute.Name == "saltSize") { - Converter::convertNumber(mInfo.saltSize, aAttributes[i].Value); + ::sax::Converter::convertNumber(mInfo.saltSize, rAttribute.Value); } - else if (aAttributes[i].Name == "blockSize") + else if (rAttribute.Name == "blockSize") { - Converter::convertNumber(mInfo.blockSize, aAttributes[i].Value); + ::sax::Converter::convertNumber(mInfo.blockSize, rAttribute.Value); } - else if (aAttributes[i].Name == "keyBits") + else if (rAttribute.Name == "keyBits") { - Converter::convertNumber(mInfo.keyBits, aAttributes[i].Value); + ::sax::Converter::convertNumber(mInfo.keyBits, rAttribute.Value); } - else if (aAttributes[i].Name == "hashSize") + else if (rAttribute.Name == "hashSize") { - Converter::convertNumber(mInfo.hashSize, aAttributes[i].Value); + ::sax::Converter::convertNumber(mInfo.hashSize, rAttribute.Value); } - else if (aAttributes[i].Name == "cipherAlgorithm") + else if (rAttribute.Name == "cipherAlgorithm") { - mInfo.cipherAlgorithm = aAttributes[i].Value; + mInfo.cipherAlgorithm = rAttribute.Value; } - else if (aAttributes[i].Name == "cipherChaining") + else if (rAttribute.Name == "cipherChaining") { - mInfo.cipherChaining = aAttributes[i].Value; + mInfo.cipherChaining = rAttribute.Value; } - else if (aAttributes[i].Name == "hashAlgorithm") + else if (rAttribute.Name == "hashAlgorithm") { - mInfo.hashAlgorithm = aAttributes[i].Value; + mInfo.hashAlgorithm = rAttribute.Value; } - else if (aAttributes[i].Name == "saltValue") + else if (rAttribute.Name == "saltValue") { Sequence saltValue; - Converter::decodeBase64(saltValue, aAttributes[i].Value); + ::sax::Converter::decodeBase64(saltValue, rAttribute.Value); mInfo.saltValue = convertToVector(saltValue); } - else if (aAttributes[i].Name == "encryptedVerifierHashInput") + else if (rAttribute.Name == "encryptedVerifierHashInput") { Sequence encryptedVerifierHashInput; - Converter::decodeBase64(encryptedVerifierHashInput, aAttributes[i].Value); + ::sax::Converter::decodeBase64(encryptedVerifierHashInput, rAttribute.Value); mInfo.encryptedVerifierHashInput = convertToVector(encryptedVerifierHashInput); } - else if (aAttributes[i].Name == "encryptedVerifierHashValue") + else if (rAttribute.Name == "encryptedVerifierHashValue") { Sequence encryptedVerifierHashValue; - Converter::decodeBase64(encryptedVerifierHashValue, aAttributes[i].Value); + ::sax::Converter::decodeBase64(encryptedVerifierHashValue, rAttribute.Value); mInfo.encryptedVerifierHashValue = convertToVector(encryptedVerifierHashValue); } - else if (aAttributes[i].Name == "encryptedKeyValue") + else if (rAttribute.Name == "encryptedKeyValue") { Sequence encryptedKeyValue; - Converter::decodeBase64(encryptedKeyValue, aAttributes[i].Value); + ::sax::Converter::decodeBase64(encryptedKeyValue, rAttribute.Value); mInfo.encryptedKeyValue = convertToVector(encryptedKeyValue); } } @@ -206,15 +198,14 @@ bool DocumentDecryption::readAgileEncryptionInfo(Reference< XInputStream >& xInp Reference xFastDocumentHandler( new AgileDocumentHandler(info) ); Reference xFastTokenHandler ( new AgileTokenHandler ); - Reference xParser( - css::xml::sax::FastParser::create(mxContext)); + Reference xParser(css::xml::sax::FastParser::create(mxContext)); - xParser->setFastDocumentHandler( xFastDocumentHandler ); - xParser->setTokenHandler( xFastTokenHandler ); + xParser->setFastDocumentHandler(xFastDocumentHandler); + xParser->setTokenHandler(xFastTokenHandler); InputSource aInputSource; aInputSource.aInputStream = xInputStream; - xParser->parseStream( aInputSource ); + xParser->parseStream(aInputSource); // CHECK info data if (2 > info.blockSize || info.blockSize > 4096) @@ -256,14 +247,14 @@ bool DocumentDecryption::readStandard2007EncryptionInfo(BinaryInputStream& rStre msfilter::StandardEncryptionInfo& info = engine->getInfo(); info.header.flags = rStream.readuInt32(); - if( getFlag( info.header.flags, msfilter::ENCRYPTINFO_EXTERNAL ) ) + if (getFlag(info.header.flags, msfilter::ENCRYPTINFO_EXTERNAL)) return false; sal_uInt32 nHeaderSize = rStream.readuInt32(); sal_uInt32 actualHeaderSize = sizeof(info.header); - if( (nHeaderSize < actualHeaderSize) ) + if (nHeaderSize < actualHeaderSize) return false; info.header.flags = rStream.readuInt32(); @@ -275,7 +266,7 @@ bool DocumentDecryption::readStandard2007EncryptionInfo(BinaryInputStream& rStre info.header.reserved1 = rStream.readuInt32(); info.header.reserved2 = rStream.readuInt32(); - rStream.skip( nHeaderSize - actualHeaderSize ); + rStream.skip(nHeaderSize - actualHeaderSize); info.verifier.saltSize = rStream.readuInt32(); rStream.readArray(info.verifier.salt, SAL_N_ELEMENTS(info.verifier.salt)); @@ -283,25 +274,25 @@ bool DocumentDecryption::readStandard2007EncryptionInfo(BinaryInputStream& rStre info.verifier.encryptedVerifierHashSize = rStream.readuInt32(); rStream.readArray(info.verifier.encryptedVerifierHash, SAL_N_ELEMENTS(info.verifier.encryptedVerifierHash)); - if( info.verifier.saltSize != 16 ) + if (info.verifier.saltSize != 16) return false; // check flags and algorithm IDs, required are AES128 and SHA-1 - if( !getFlag( info.header.flags, msfilter::ENCRYPTINFO_CRYPTOAPI ) ) + if (!getFlag(info.header.flags, msfilter::ENCRYPTINFO_CRYPTOAPI)) return false; - if( !getFlag( info.header.flags, msfilter::ENCRYPTINFO_AES ) ) + if (!getFlag(info.header.flags, msfilter::ENCRYPTINFO_AES)) return false; // algorithm ID 0 defaults to AES128 too, if ENCRYPTINFO_AES flag is set - if( info.header.algId != 0 && info.header.algId != msfilter::ENCRYPT_ALGO_AES128 ) + if (info.header.algId != 0 && info.header.algId != msfilter::ENCRYPT_ALGO_AES128) return false; // hash algorithm ID 0 defaults to SHA-1 too - if( info.header.algIdHash != 0 && info.header.algIdHash != msfilter::ENCRYPT_HASH_SHA1 ) + if (info.header.algIdHash != 0 && info.header.algIdHash != msfilter::ENCRYPT_HASH_SHA1) return false; - if( info.verifier.encryptedVerifierHashSize != 20 ) + if (info.verifier.encryptedVerifierHashSize != 20) return false; return !rStream.isEof(); @@ -309,14 +300,14 @@ bool DocumentDecryption::readStandard2007EncryptionInfo(BinaryInputStream& rStre bool DocumentDecryption::readEncryptionInfo() { - if( !mrOleStorage.isStorage() ) + if (!mrOleStorage.isStorage()) return false; - Reference< XInputStream > xEncryptionInfo( mrOleStorage.openInputStream( "EncryptionInfo" ), UNO_SET_THROW ); + Reference xEncryptionInfo(mrOleStorage.openInputStream("EncryptionInfo"), UNO_SET_THROW); bool bResult = false; - BinaryXInputStream aBinaryInputStream( xEncryptionInfo, true ); + BinaryXInputStream aBinaryInputStream(xEncryptionInfo, true); sal_uInt32 aVersion = aBinaryInputStream.readuInt32(); @@ -341,7 +332,7 @@ bool DocumentDecryption::readEncryptionInfo() Sequence DocumentDecryption::createEncryptionData(const OUString& rPassword) { - SequenceAsHashMap aEncryptionData; + comphelper::SequenceAsHashMap aEncryptionData; if (mCryptoType == AGILE) { @@ -360,16 +351,16 @@ bool DocumentDecryption::decrypt(const Reference& xDocumentStream) { bool aResult = false; - if( !mrOleStorage.isStorage() ) + if (!mrOleStorage.isStorage()) return false; // open the required input streams in the encrypted package - Reference< XInputStream > xEncryptedPackage( mrOleStorage.openInputStream( "EncryptedPackage" ), UNO_SET_THROW ); + Reference xEncryptedPackage(mrOleStorage.openInputStream("EncryptedPackage"), UNO_SET_THROW); // create temporary file for unencrypted package - Reference< XOutputStream > xDecryptedPackage( xDocumentStream->getOutputStream(), UNO_SET_THROW ); - BinaryXOutputStream aDecryptedPackage( xDecryptedPackage, true ); - BinaryXInputStream aEncryptedPackage( xEncryptedPackage, true ); + Reference xDecryptedPackage(xDocumentStream->getOutputStream(), UNO_SET_THROW); + BinaryXOutputStream aDecryptedPackage(xDecryptedPackage, true); + BinaryXInputStream aEncryptedPackage(xEncryptedPackage, true); aResult = mEngine->decrypt(aEncryptedPackage, aDecryptedPackage); diff --git a/oox/source/crypto/DocumentEncryption.cxx b/oox/source/crypto/DocumentEncryption.cxx index d9bb07bc9ea6..493fcbf6ee85 100644 --- a/oox/source/crypto/DocumentEncryption.cxx +++ b/oox/source/crypto/DocumentEncryption.cxx @@ -26,18 +26,18 @@ using namespace css::io; using namespace css::lang; using namespace css::uno; -using namespace std; - -DocumentEncryption::DocumentEncryption(Reference< XStream > const & xDocumentStream, oox::ole::OleStorage& rOleStorage, const OUString& aPassword) : - mxDocumentStream(xDocumentStream), - mrOleStorage(rOleStorage), - maPassword(aPassword) +DocumentEncryption::DocumentEncryption(Reference const & xDocumentStream, + oox::ole::OleStorage& rOleStorage, + const OUString& rPassword) + : mxDocumentStream(xDocumentStream) + , mrOleStorage(rOleStorage) + , maPassword(rPassword) {} bool DocumentEncryption::encrypt() { - Reference< XInputStream > xInputStream ( mxDocumentStream->getInputStream(), UNO_SET_THROW ); - Reference< XSeekable > xSeekable( xInputStream, UNO_QUERY ); + Reference xInputStream (mxDocumentStream->getInputStream(), UNO_SET_THROW); + Reference xSeekable(xInputStream, UNO_QUERY); if (!xSeekable.is()) return false; @@ -47,8 +47,8 @@ bool DocumentEncryption::encrypt() if (!mrOleStorage.isStorage()) return false; - Reference< XOutputStream > xEncryptionInfo( mrOleStorage.openOutputStream( "EncryptionInfo" ), UNO_SET_THROW ); - BinaryXOutputStream aEncryptionInfoBinaryOutputStream( xEncryptionInfo, false ); + Reference xEncryptionInfo(mrOleStorage.openOutputStream("EncryptionInfo"), UNO_SET_THROW); + BinaryXOutputStream aEncryptionInfoBinaryOutputStream(xEncryptionInfo, false); mEngine.writeEncryptionInfo(maPassword, aEncryptionInfoBinaryOutputStream); @@ -56,14 +56,14 @@ bool DocumentEncryption::encrypt() xEncryptionInfo->flush(); xEncryptionInfo->closeOutput(); - Reference< XOutputStream > xEncryptedPackage( mrOleStorage.openOutputStream( "EncryptedPackage" ), UNO_SET_THROW ); - BinaryXOutputStream aEncryptedPackageStream( xEncryptedPackage, false ); + Reference xEncryptedPackage(mrOleStorage.openOutputStream("EncryptedPackage"), UNO_SET_THROW); + BinaryXOutputStream aEncryptedPackageStream(xEncryptedPackage, false); - BinaryXInputStream aDocumentInputStream( xInputStream, false ); + BinaryXInputStream aDocumentInputStream(xInputStream, false); aDocumentInputStream.seekToStart(); - aEncryptedPackageStream.WriteUInt32( aLength ); // size - aEncryptedPackageStream.WriteUInt32( 0U ); // reserved + aEncryptedPackageStream.WriteUInt32(aLength); // size + aEncryptedPackageStream.WriteUInt32(0U); // reserved mEngine.encrypt(aDocumentInputStream, aEncryptedPackageStream); diff --git a/oox/source/crypto/Standard2007Engine.cxx b/oox/source/crypto/Standard2007Engine.cxx index ecf9d8c5dfe8..d95f3153f564 100644 --- a/oox/source/crypto/Standard2007Engine.cxx +++ b/oox/source/crypto/Standard2007Engine.cxx @@ -20,8 +20,6 @@ namespace oox { namespace core { -using namespace std; - /* =========================================================================== */ /* Kudos to Caolan McNamara who provided the core decryption implementations. */ /* =========================================================================== */ @@ -31,23 +29,16 @@ namespace void lclRandomGenerateValues(sal_uInt8* aArray, sal_uInt32 aSize) { TimeValue aTime; - osl_getSystemTime( &aTime ); - rtlRandomPool aRandomPool = rtl_random_createPool (); - rtl_random_addBytes ( aRandomPool, &aTime, 8 ); - rtl_random_getBytes ( aRandomPool, aArray, aSize ); - rtl_random_destroyPool ( aRandomPool ); + osl_getSystemTime(&aTime); + rtlRandomPool aRandomPool = rtl_random_createPool(); + rtl_random_addBytes(aRandomPool, &aTime, 8); + rtl_random_getBytes(aRandomPool, aArray, aSize); + rtl_random_destroyPool(aRandomPool); } static const OUString lclCspName = "Microsoft Enhanced RSA and AES Cryptographic Provider"; -} // namespace - -Standard2007Engine::Standard2007Engine() : - CryptoEngine() -{} - -Standard2007Engine::~Standard2007Engine() -{} +} // end anonymous namespace bool Standard2007Engine::generateVerifier() { @@ -55,23 +46,23 @@ bool Standard2007Engine::generateVerifier() if (mKey.size() != 16) return false; - vector verifier(msfilter::ENCRYPTED_VERIFIER_LENGTH); - vector encryptedVerifier(msfilter::ENCRYPTED_VERIFIER_LENGTH); + std::vector verifier(msfilter::ENCRYPTED_VERIFIER_LENGTH); + std::vector encryptedVerifier(msfilter::ENCRYPTED_VERIFIER_LENGTH); - lclRandomGenerateValues(&verifier[0], verifier.size()); + lclRandomGenerateValues(verifier.data(), verifier.size()); - vector iv; + std::vector iv; Encrypt aEncryptorVerifier(mKey, iv, Crypto::AES_128_ECB); if (aEncryptorVerifier.update(encryptedVerifier, verifier) != msfilter::ENCRYPTED_VERIFIER_LENGTH) return false; std::copy(encryptedVerifier.begin(), encryptedVerifier.end(), mInfo.verifier.encryptedVerifier); - vector hash(msfilter::SHA1_HASH_LENGTH, 0); + std::vector hash(msfilter::SHA1_HASH_LENGTH, 0); mInfo.verifier.encryptedVerifierHashSize = msfilter::SHA1_HASH_LENGTH; Digest::sha1(hash, verifier); hash.resize(msfilter::SHA256_HASH_LENGTH, 0); - vector encryptedHash(msfilter::SHA256_HASH_LENGTH, 0); + std::vector encryptedHash(msfilter::SHA256_HASH_LENGTH, 0); Encrypt aEncryptorHash(mKey, iv, Crypto::AES_128_ECB); aEncryptorHash.update(encryptedHash, hash, hash.size()); @@ -87,7 +78,7 @@ bool Standard2007Engine::calculateEncryptionKey(const OUString& rPassword) const sal_uInt8* saltArray = mInfo.verifier.salt; // Prepare initial data -> salt + password (in 16-bit chars) - vector initialData(saltSize + passwordByteLength); + std::vector initialData(saltSize + passwordByteLength); std::copy(saltArray, saltArray + saltSize, initialData.begin()); const sal_uInt8* passwordByteArray = reinterpret_cast(rPassword.getStr()); @@ -98,17 +89,17 @@ bool Standard2007Engine::calculateEncryptionKey(const OUString& rPassword) initialData.begin() + saltSize); // use "hash" vector for result of sha1 hashing - vector hash(msfilter::SHA1_HASH_LENGTH, 0); + std::vector hash(msfilter::SHA1_HASH_LENGTH, 0); // calculate SHA1 hash of initialData Digest::sha1(hash, initialData); // data = iterator (4bytes) + hash - vector data(msfilter::SHA1_HASH_LENGTH + 4, 0); + std::vector data(msfilter::SHA1_HASH_LENGTH + 4, 0); for (sal_Int32 i = 0; i < 50000; ++i) { - ByteOrderConverter::writeLittleEndian( &data[0], i ); + ByteOrderConverter::writeLittleEndian(data.data(), i); std::copy(hash.begin(), hash.end(), data.begin() + 4); Digest::sha1(hash, data); } @@ -118,8 +109,8 @@ bool Standard2007Engine::calculateEncryptionKey(const OUString& rPassword) Digest::sha1(hash, data); // derive key - vector buffer(64, 0x36); - for( size_t i = 0; i < hash.size(); ++i ) + std::vector buffer(64, 0x36); + for (size_t i = 0; i < hash.size(); ++i) buffer[i] ^= hash[i]; Digest::sha1(hash, buffer); @@ -135,48 +126,47 @@ bool Standard2007Engine::generateEncryptionKey(const OUString& password) calculateEncryptionKey(password); - vector encryptedVerifier(msfilter::ENCRYPTED_VERIFIER_LENGTH); + std::vector encryptedVerifier(msfilter::ENCRYPTED_VERIFIER_LENGTH); std::copy( mInfo.verifier.encryptedVerifier, mInfo.verifier.encryptedVerifier + msfilter::ENCRYPTED_VERIFIER_LENGTH, encryptedVerifier.begin()); - vector encryptedHash(msfilter::SHA256_HASH_LENGTH); + std::vector encryptedHash(msfilter::SHA256_HASH_LENGTH); std::copy( mInfo.verifier.encryptedVerifierHash, mInfo.verifier.encryptedVerifierHash + msfilter::SHA256_HASH_LENGTH, encryptedHash.begin()); - vector verifier(encryptedVerifier.size(), 0); + std::vector verifier(encryptedVerifier.size(), 0); Decrypt::aes128ecb(verifier, encryptedVerifier, mKey); - vector verifierHash(encryptedHash.size(), 0); + std::vector verifierHash(encryptedHash.size(), 0); Decrypt::aes128ecb(verifierHash, encryptedHash, mKey); - vector hash(msfilter::SHA1_HASH_LENGTH, 0); + std::vector hash(msfilter::SHA1_HASH_LENGTH, 0); Digest::sha1(hash, verifier); - return std::equal( hash.begin(), hash.end(), verifierHash.begin() ); + return std::equal(hash.begin(), hash.end(), verifierHash.begin()); } -bool Standard2007Engine::decrypt( - BinaryXInputStream& aInputStream, - BinaryXOutputStream& aOutputStream) +bool Standard2007Engine::decrypt(BinaryXInputStream& aInputStream, + BinaryXOutputStream& aOutputStream) { - aInputStream.skip(4); // Document unencrypted size - 4 bytes - aInputStream.skip(4); // Reserved 4 Bytes + aInputStream.skip(4); // Document unencrypted size - 4 bytes + aInputStream.skip(4); // Reserved 4 Bytes - vector iv; + std::vector iv; Decrypt aDecryptor(mKey, iv, Crypto::AES_128_ECB); - vector inputBuffer (4096); - vector outputBuffer(4096); + std::vector inputBuffer (4096); + std::vector outputBuffer(4096); sal_uInt32 inputLength; sal_uInt32 outputLength; - while( (inputLength = aInputStream.readMemory( &inputBuffer[0], inputBuffer.size() )) > 0 ) + while ((inputLength = aInputStream.readMemory(inputBuffer.data(), inputBuffer.size())) > 0) { outputLength = aDecryptor.update(outputBuffer, inputBuffer, inputLength); - aOutputStream.writeMemory( &outputBuffer[0], outputLength ); + aOutputStream.writeMemory(outputBuffer.data(), outputLength); } return true; } @@ -207,9 +197,9 @@ void Standard2007Engine::writeEncryptionInfo(const OUString& password, BinaryXOu sal_uInt32 encryptionHeaderSize = static_cast(sizeof(msfilter::EncryptionStandardHeader)); - rStream.WriteUInt32( mInfo.header.flags ); + rStream.WriteUInt32(mInfo.header.flags); sal_uInt32 headerSize = encryptionHeaderSize + cspNameSize; - rStream.WriteUInt32( headerSize ); + rStream.WriteUInt32(headerSize); rStream.writeMemory(&mInfo.header, encryptionHeaderSize); rStream.writeUnicodeArray(lclCspName); @@ -219,24 +209,23 @@ void Standard2007Engine::writeEncryptionInfo(const OUString& password, BinaryXOu rStream.writeMemory(&mInfo.verifier, encryptionVerifierSize); } -void Standard2007Engine::encrypt( - BinaryXInputStream& aInputStream, - BinaryXOutputStream& aOutputStream) +void Standard2007Engine::encrypt(BinaryXInputStream& aInputStream, + BinaryXOutputStream& aOutputStream) { - vector inputBuffer(1024); - vector outputBuffer(1024); + std::vector inputBuffer(1024); + std::vector outputBuffer(1024); sal_uInt32 inputLength; sal_uInt32 outputLength; - vector iv; + std::vector iv; Encrypt aEncryptor(mKey, iv, Crypto::AES_128_ECB); - while( (inputLength = aInputStream.readMemory( &inputBuffer[0], inputBuffer.size() )) > 0 ) + while ((inputLength = aInputStream.readMemory(inputBuffer.data(), inputBuffer.size())) > 0) { inputLength = inputLength % 16 == 0 ? inputLength : ((inputLength / 16) * 16) + 16; outputLength = aEncryptor.update(outputBuffer, inputBuffer, inputLength); - aOutputStream.writeMemory( &outputBuffer[0], outputLength ); + aOutputStream.writeMemory(outputBuffer.data(), outputLength); } } -- cgit