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/StrongEncryptionDataSpace.hxx>
12 #include <oox/crypto/AgileEngine.hxx>
13 #include <oox/crypto/Standard2007Engine.hxx>
14 #include <oox/helper/binaryoutputstream.hxx>
15 #include <oox/helper/binaryinputstream.hxx>
16 #include <com/sun/star/io/SequenceInputStream.hpp>
17 #include <com/sun/star/io/XSequenceOutputStream.hpp>
19 #include <comphelper/sequenceashashmap.hxx>
20 #include <cppuhelper/supportsservice.hxx>
23 using namespace css::beans
;
24 using namespace css::io
;
25 using namespace css::lang
;
26 using namespace css::uno
;
30 StrongEncryptionDataSpace::StrongEncryptionDataSpace(const Reference
<XComponentContext
>& rxContext
)
31 : mxContext(rxContext
)
32 , mCryptoEngine(new Standard2007Engine
)
36 sal_Bool
StrongEncryptionDataSpace::generateEncryptionKey(const OUString
& rPassword
)
41 return mCryptoEngine
->generateEncryptionKey(rPassword
);
44 sal_Bool
StrongEncryptionDataSpace::checkDataIntegrity()
49 return mCryptoEngine
->checkDataIntegrity();
52 sal_Bool
StrongEncryptionDataSpace::decrypt(const Reference
<XInputStream
>& rxInputStream
,
53 Reference
<XOutputStream
>& rxOutputStream
)
58 BinaryXInputStream
aInputStream(rxInputStream
, true);
59 BinaryXOutputStream
aOutputStream(rxOutputStream
, true);
61 mCryptoEngine
->decrypt(aInputStream
, aOutputStream
);
63 rxOutputStream
->flush();
67 Reference
<XInputStream
> StrongEncryptionDataSpace::getStream(const Sequence
<NamedValue
>& rStreams
,
68 std::u16string_view sStreamName
)
70 for (const auto& aStream
: rStreams
)
72 if (aStream
.Name
== sStreamName
)
74 Sequence
<sal_Int8
> aSeq
;
75 aStream
.Value
>>= aSeq
;
76 Reference
<XInputStream
> aStream2(
77 io::SequenceInputStream::createStreamFromSequence(mxContext
, aSeq
),
85 sal_Bool
StrongEncryptionDataSpace::readEncryptionInfo(const Sequence
<NamedValue
>& aStreams
)
87 Reference
<XInputStream
> xEncryptionInfo
= getStream(aStreams
, u
"EncryptionInfo");
88 if (!xEncryptionInfo
.is())
91 BinaryXInputStream
aBinaryInputStream(xEncryptionInfo
, true);
92 sal_uInt32 aVersion
= aBinaryInputStream
.readuInt32();
96 case msfilter::VERSION_INFO_2007_FORMAT
:
97 case msfilter::VERSION_INFO_2007_FORMAT_SP2
:
98 mCryptoEngine
.reset(new Standard2007Engine
);
100 case msfilter::VERSION_INFO_AGILE
:
101 mCryptoEngine
.reset(new AgileEngine());
110 return mCryptoEngine
->readEncryptionInfo(xEncryptionInfo
);
113 sal_Bool
StrongEncryptionDataSpace::setupEncryption(const Sequence
<NamedValue
>& rMediaEncData
)
119 for (const auto& aParam
: rMediaEncData
)
121 if (aParam
.Name
== "OOXPassword")
123 aParam
.Value
>>= sPassword
;
127 return mCryptoEngine
->setupEncryption(sPassword
);
130 Sequence
<NamedValue
> StrongEncryptionDataSpace::createEncryptionData(const OUString
& rPassword
)
132 comphelper::SequenceAsHashMap aEncryptionData
;
133 aEncryptionData
["OOXPassword"] <<= rPassword
;
134 aEncryptionData
["CryptoType"] <<= OUString("StrongEncryptionDataSpace");
136 return aEncryptionData
.getAsConstNamedValueList();
140 StrongEncryptionDataSpace::encrypt(const Reference
<XInputStream
>& rxInputStream
)
143 return Sequence
<NamedValue
>();
145 Reference
<XSeekable
> xSeekable(rxInputStream
, UNO_QUERY
);
147 return Sequence
<NamedValue
>();
149 sal_uInt32 aLength
= xSeekable
->getLength(); // check length of the stream
151 Reference
<XOutputStream
> xOutputStream(
152 mxContext
->getServiceManager()->createInstanceWithContext(
153 "com.sun.star.io.SequenceOutputStream", mxContext
),
156 mCryptoEngine
->encrypt(rxInputStream
, xOutputStream
, aLength
);
158 comphelper::SequenceAsHashMap aStreams
;
160 Reference
<XSequenceOutputStream
> xEncodedFileSequenceStream(xOutputStream
, UNO_QUERY
);
161 aStreams
["EncryptedPackage"] <<= xEncodedFileSequenceStream
->getWrittenBytes();
163 Reference
<XOutputStream
> aEncryptionInfoStream(
164 mxContext
->getServiceManager()->createInstanceWithContext(
165 "com.sun.star.io.SequenceOutputStream", mxContext
),
167 BinaryXOutputStream
rStream(aEncryptionInfoStream
, false);
168 mCryptoEngine
->writeEncryptionInfo(rStream
);
169 aEncryptionInfoStream
->flush();
170 Reference
<XSequenceOutputStream
> aEncryptionInfoSequenceStream(aEncryptionInfoStream
,
173 aStreams
["EncryptionInfo"] <<= aEncryptionInfoSequenceStream
->getWrittenBytes();
175 return aStreams
.getAsConstNamedValueList();
178 OUString SAL_CALL
StrongEncryptionDataSpace::getImplementationName()
180 return "com.sun.star.comp.oox.crypto.StrongEncryptionDataSpace";
183 sal_Bool SAL_CALL
StrongEncryptionDataSpace::supportsService(const OUString
& rServiceName
)
185 return cppu::supportsService(this, rServiceName
);
188 css::uno::Sequence
<OUString
> SAL_CALL
StrongEncryptionDataSpace::getSupportedServiceNames()
190 Sequence
<OUString
> aServices
{ "com.sun.star.packages.PackageEncryption" };
194 } // namespace oox::crypto
196 extern "C" SAL_DLLPUBLIC_EXPORT
uno::XInterface
*
197 com_sun_star_comp_oox_crypto_StrongEncryptionDataSpace_get_implementation(
198 uno::XComponentContext
* pCtx
, uno::Sequence
<uno::Any
> const& /*rSeq*/)
200 return cppu::acquire(new oox::crypto::StrongEncryptionDataSpace(pCtx
));
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */