1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
30 DocumentDecryption::DocumentDecryption(oox::ole::OleStorage
& rOleStorage
) :
31 mrOleStorage(rOleStorage
),
35 bool DocumentDecryption::generateEncryptionKey(const OUString
& rPassword
)
38 return mEngine
->generateEncryptionKey(rPassword
);
42 bool DocumentDecryption::readEncryptionInfo()
44 if (!mrOleStorage
.isStorage())
47 uno::Reference
<io::XInputStream
> xEncryptionInfo(mrOleStorage
.openInputStream("EncryptionInfo"), uno::UNO_QUERY
);
49 BinaryXInputStream
aBinaryInputStream(xEncryptionInfo
, true);
50 sal_uInt32 aVersion
= aBinaryInputStream
.readuInt32();
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
);
59 case msfilter::VERSION_INFO_AGILE
:
60 mCryptoType
= AGILE
; // Set encryption info format
61 mEngine
.reset(new AgileEngine
);
67 return mEngine
->readEncryptionInfo(xEncryptionInfo
);
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
)
92 if (!mrOleStorage
.isStorage())
95 // open the required input streams in the encrypted package
96 uno::Reference
<io::XInputStream
> xEncryptedPackage(mrOleStorage
.openInputStream("EncryptedPackage"), uno::UNO_QUERY
);
98 // create temporary file for unencrypted package
99 uno::Reference
<io::XOutputStream
> xDecryptedPackage(xDocumentStream
->getOutputStream(), uno::UNO_QUERY
);
100 BinaryXOutputStream
aDecryptedPackage(xDecryptedPackage
, true);
101 BinaryXInputStream
aEncryptedPackage(xEncryptedPackage
, true);
103 bResult
= mEngine
->decrypt(aEncryptedPackage
, aDecryptedPackage
);
105 xDecryptedPackage
->flush();
106 aDecryptedPackage
.seekToStart();
109 return mEngine
->checkDataIntegrity();
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */