Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / oox / source / crypto / DocumentDecryption.cxx
blob6ccbfc18d225bb3d4ce79ec06cc21aca5c22916a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
11 #include <oox/crypto/DocumentDecryption.hxx>
13 #include <comphelper/sequenceashashmap.hxx>
14 #include <cppuhelper/implbase.hxx>
16 #include <com/sun/star/beans/NamedValue.hpp>
17 #include <com/sun/star/io/XStream.hpp>
18 #include <com/sun/star/uno/XComponentContext.hpp>
19 #include <oox/crypto/AgileEngine.hxx>
20 #include <oox/crypto/Standard2007Engine.hxx>
21 #include <oox/helper/binaryinputstream.hxx>
22 #include <oox/helper/binaryoutputstream.hxx>
23 #include <oox/ole/olestorage.hxx>
25 namespace oox {
26 namespace core {
28 using namespace css;
30 DocumentDecryption::DocumentDecryption(oox::ole::OleStorage& rOleStorage) :
31 mrOleStorage(rOleStorage),
32 mCryptoType(UNKNOWN)
35 bool DocumentDecryption::generateEncryptionKey(const OUString& rPassword)
37 if (mEngine)
38 return mEngine->generateEncryptionKey(rPassword);
39 return false;
42 bool DocumentDecryption::readEncryptionInfo()
44 if (!mrOleStorage.isStorage())
45 return false;
47 uno::Reference<io::XInputStream> xEncryptionInfo = mrOleStorage.openInputStream("EncryptionInfo");
49 BinaryXInputStream aBinaryInputStream(xEncryptionInfo, true);
50 sal_uInt32 aVersion = aBinaryInputStream.readuInt32();
52 switch (aVersion)
54 case msfilter::VERSION_INFO_2007_FORMAT:
55 case msfilter::VERSION_INFO_2007_FORMAT_SP2:
56 mCryptoType = STANDARD_2007; // Set encryption info format
57 mEngine.reset(new Standard2007Engine);
58 break;
59 case msfilter::VERSION_INFO_AGILE:
60 mCryptoType = AGILE; // Set encryption info format
61 mEngine.reset(new AgileEngine);
62 break;
63 default:
64 break;
66 if (mEngine)
67 return mEngine->readEncryptionInfo(xEncryptionInfo);
68 return false;
71 uno::Sequence<beans::NamedValue> DocumentDecryption::createEncryptionData(const OUString& rPassword)
73 comphelper::SequenceAsHashMap aEncryptionData;
75 if (mCryptoType == AGILE)
77 aEncryptionData["CryptoType"] <<= OUString("Agile");
79 else if (mCryptoType == STANDARD_2007)
81 aEncryptionData["CryptoType"] <<= OUString("Standard");
84 aEncryptionData["OOXPassword"] <<= rPassword;
85 return aEncryptionData.getAsConstNamedValueList();
88 bool DocumentDecryption::decrypt(const uno::Reference<io::XStream>& xDocumentStream)
90 bool bResult = false;
92 if (!mrOleStorage.isStorage())
93 return false;
95 // open the required input streams in the encrypted package
96 uno::Reference<io::XInputStream> xEncryptedPackage = mrOleStorage.openInputStream("EncryptedPackage");
98 // create temporary file for unencrypted package
99 uno::Reference<io::XOutputStream> xDecryptedPackage = xDocumentStream->getOutputStream();
100 BinaryXOutputStream aDecryptedPackage(xDecryptedPackage, true);
101 BinaryXInputStream aEncryptedPackage(xEncryptedPackage, true);
103 bResult = mEngine->decrypt(aEncryptedPackage, aDecryptedPackage);
105 xDecryptedPackage->flush();
106 aDecryptedPackage.seekToStart();
108 if (bResult)
109 return mEngine->checkDataIntegrity();
111 return bResult;
114 } // namespace core
115 } // namespace oox
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */