/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * */ #include #include #include #include #include #include #include #include #include #include #include namespace oox::core { using namespace css; DocumentDecryption::DocumentDecryption(oox::ole::OleStorage& rOleStorage) : mrOleStorage(rOleStorage), mCryptoType(UNKNOWN) {} bool DocumentDecryption::generateEncryptionKey(const OUString& rPassword) { if (mEngine) return mEngine->generateEncryptionKey(rPassword); return false; } bool DocumentDecryption::readEncryptionInfo() { if (!mrOleStorage.isStorage()) return false; uno::Reference xEncryptionInfo = mrOleStorage.openInputStream("EncryptionInfo"); BinaryXInputStream aBinaryInputStream(xEncryptionInfo, true); sal_uInt32 aVersion = aBinaryInputStream.readuInt32(); switch (aVersion) { case msfilter::VERSION_INFO_2007_FORMAT: case msfilter::VERSION_INFO_2007_FORMAT_SP2: mCryptoType = STANDARD_2007; // Set encryption info format mEngine.reset(new Standard2007Engine); break; case msfilter::VERSION_INFO_AGILE: mCryptoType = AGILE; // Set encryption info format mEngine.reset(new AgileEngine); break; default: break; } if (mEngine) return mEngine->readEncryptionInfo(xEncryptionInfo); return false; } uno::Sequence DocumentDecryption::createEncryptionData(const OUString& rPassword) { comphelper::SequenceAsHashMap aEncryptionData; if (mCryptoType == AGILE) { aEncryptionData["CryptoType"] <<= OUString("Agile"); } else if (mCryptoType == STANDARD_2007) { aEncryptionData["CryptoType"] <<= OUString("Standard"); } aEncryptionData["OOXPassword"] <<= rPassword; return aEncryptionData.getAsConstNamedValueList(); } bool DocumentDecryption::decrypt(const uno::Reference& xDocumentStream) { bool bResult = false; if (!mrOleStorage.isStorage()) return false; // open the required input streams in the encrypted package uno::Reference xEncryptedPackage = mrOleStorage.openInputStream("EncryptedPackage"); // create temporary file for unencrypted package uno::Reference xDecryptedPackage = xDocumentStream->getOutputStream(); BinaryXOutputStream aDecryptedPackage(xDecryptedPackage, true); BinaryXInputStream aEncryptedPackage(xEncryptedPackage, true); bResult = mEngine->decrypt(aEncryptedPackage, aDecryptedPackage); xDecryptedPackage->flush(); aDecryptedPackage.seekToStart(); if (bResult) return mEngine->checkDataIntegrity(); return bResult; } } // namespace oox /* vim:set shiftwidth=4 softtabstop=4 expandtab: */