Bump version to 24.04.3.4
[LibreOffice.git] / oox / source / crypto / StrongEncryptionDataSpace.cxx
blobfd1b823b8870e8b714bd52efb045352b2b0d28bd
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/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>
22 using namespace css;
23 using namespace css::beans;
24 using namespace css::io;
25 using namespace css::lang;
26 using namespace css::uno;
28 namespace oox::crypto
30 StrongEncryptionDataSpace::StrongEncryptionDataSpace(const Reference<XComponentContext>& rxContext)
31 : mxContext(rxContext)
32 , mCryptoEngine(new Standard2007Engine)
36 sal_Bool StrongEncryptionDataSpace::generateEncryptionKey(const OUString& rPassword)
38 if (!mCryptoEngine)
39 return false;
41 return mCryptoEngine->generateEncryptionKey(rPassword);
44 sal_Bool StrongEncryptionDataSpace::checkDataIntegrity()
46 if (!mCryptoEngine)
47 return false;
49 return mCryptoEngine->checkDataIntegrity();
52 sal_Bool StrongEncryptionDataSpace::decrypt(const Reference<XInputStream>& rxInputStream,
53 Reference<XOutputStream>& rxOutputStream)
55 if (!mCryptoEngine)
56 return false;
58 BinaryXInputStream aInputStream(rxInputStream, true);
59 BinaryXOutputStream aOutputStream(rxOutputStream, true);
61 mCryptoEngine->decrypt(aInputStream, aOutputStream);
63 rxOutputStream->flush();
64 return true;
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),
78 UNO_QUERY_THROW);
79 return aStream2;
82 return nullptr;
85 sal_Bool StrongEncryptionDataSpace::readEncryptionInfo(const Sequence<NamedValue>& aStreams)
87 Reference<XInputStream> xEncryptionInfo = getStream(aStreams, u"EncryptionInfo");
88 if (!xEncryptionInfo.is())
89 return false;
91 BinaryXInputStream aBinaryInputStream(xEncryptionInfo, true);
92 sal_uInt32 aVersion = aBinaryInputStream.readuInt32();
94 switch (aVersion)
96 case msfilter::VERSION_INFO_2007_FORMAT:
97 case msfilter::VERSION_INFO_2007_FORMAT_SP2:
98 mCryptoEngine.reset(new Standard2007Engine);
99 break;
100 case msfilter::VERSION_INFO_AGILE:
101 mCryptoEngine.reset(new AgileEngine());
102 break;
103 default:
104 break;
107 if (!mCryptoEngine)
108 return false;
110 return mCryptoEngine->readEncryptionInfo(xEncryptionInfo);
113 sal_Bool StrongEncryptionDataSpace::setupEncryption(const Sequence<NamedValue>& rMediaEncData)
115 if (!mCryptoEngine)
116 return false;
118 OUString sPassword;
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();
139 Sequence<NamedValue>
140 StrongEncryptionDataSpace::encrypt(const Reference<XInputStream>& rxInputStream)
142 if (!mCryptoEngine)
143 return Sequence<NamedValue>();
145 Reference<XSeekable> xSeekable(rxInputStream, UNO_QUERY);
146 if (!xSeekable.is())
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),
154 UNO_QUERY);
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),
166 UNO_QUERY);
167 BinaryXOutputStream rStream(aEncryptionInfoStream, false);
168 mCryptoEngine->writeEncryptionInfo(rStream);
169 aEncryptionInfoStream->flush();
170 Reference<XSequenceOutputStream> aEncryptionInfoSequenceStream(aEncryptionInfoStream,
171 UNO_QUERY);
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" };
191 return aServices;
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: */