Bump version to 24.04.3.4
[LibreOffice.git] / oox / source / crypto / DocumentEncryption.cxx
blob6b88549299a128f59bd8812fbfba6803119eb402
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/DocumentEncryption.hxx>
13 #include <com/sun/star/io/XInputStream.hpp>
14 #include <com/sun/star/io/XOutputStream.hpp>
15 #include <com/sun/star/io/XStream.hpp>
16 #include <com/sun/star/io/XSeekable.hpp>
17 #include <com/sun/star/packages/XPackageEncryption.hpp>
18 #include <com/sun/star/uno/XComponentContext.hpp>
20 #include <oox/helper/binaryoutputstream.hxx>
21 #include <oox/ole/olestorage.hxx>
22 #include <sal/log.hxx>
24 namespace oox::crypto {
26 using namespace css::io;
27 using namespace css::uno;
28 using namespace css::beans;
30 DocumentEncryption::DocumentEncryption(const Reference< XComponentContext >& rxContext,
31 Reference<XStream> const & xDocumentStream,
32 oox::ole::OleStorage& rOleStorage,
33 const Sequence<NamedValue>& rMediaEncData)
34 : mxContext(rxContext)
35 , mxDocumentStream(xDocumentStream)
36 , mrOleStorage(rOleStorage)
37 , mMediaEncData(rMediaEncData)
39 // Select engine
40 for (int i = 0; i < rMediaEncData.getLength(); i++)
42 if (rMediaEncData[i].Name == "CryptoType")
44 OUString sCryptoType;
45 rMediaEncData[i].Value >>= sCryptoType;
47 if (sCryptoType == "Standard")
48 sCryptoType = "StrongEncryptionDataSpace";
50 Sequence<Any> aArguments;
51 mxPackageEncryption.set(
52 mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
53 "com.sun.star.comp.oox.crypto." + sCryptoType, aArguments, mxContext), css::uno::UNO_QUERY);
55 if (!mxPackageEncryption.is())
57 SAL_WARN("oox", "Requested encryption method \"" << sCryptoType << "\" is not supported");
60 break;
65 bool DocumentEncryption::encrypt()
67 if (!mxPackageEncryption.is())
68 return false;
70 Reference<XInputStream> xInputStream (mxDocumentStream->getInputStream(), UNO_SET_THROW);
71 Reference<XSeekable> xSeekable(xInputStream, UNO_QUERY);
73 if (!xSeekable.is())
74 return false;
76 xSeekable->seek(0); // seek to begin of the document stream
78 if (!mrOleStorage.isStorage())
79 return false;
81 mxPackageEncryption->setupEncryption(mMediaEncData);
83 Sequence<NamedValue> aStreams = mxPackageEncryption->encrypt(xInputStream);
85 for (const NamedValue & aStream : std::as_const(aStreams))
87 Reference<XOutputStream> xOutputStream(mrOleStorage.openOutputStream(aStream.Name), UNO_SET_THROW);
88 BinaryXOutputStream aBinaryOutputStream(xOutputStream, true);
90 css::uno::Sequence<sal_Int8> aStreamSequence;
91 aStream.Value >>= aStreamSequence;
93 aBinaryOutputStream.writeData(aStreamSequence);
95 aBinaryOutputStream.close();
98 return true;
101 } // namespace oox::crypto
103 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */