LibreOffice Module oox (master) 1
DocumentEncryption.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 */
10
12
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>
19
22#include <sal/log.hxx>
23
24namespace oox::crypto {
25
26using namespace css::io;
27using namespace css::uno;
28using namespace css::beans;
29
30DocumentEncryption::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)
38{
39 // Select engine
40 for (int i = 0; i < rMediaEncData.getLength(); i++)
41 {
42 if (rMediaEncData[i].Name == "CryptoType")
43 {
44 OUString sCryptoType;
45 rMediaEncData[i].Value >>= sCryptoType;
46
47 if (sCryptoType == "Standard")
48 sCryptoType = "StrongEncryptionDataSpace";
49
50 Sequence<Any> aArguments;
52 mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
53 "com.sun.star.comp.oox.crypto." + sCryptoType, aArguments, mxContext), css::uno::UNO_QUERY);
54
55 if (!mxPackageEncryption.is())
56 {
57 SAL_WARN("oox", "Requested encryption method \"" << sCryptoType << "\" is not supported");
58 }
59
60 break;
61 }
62 }
63}
64
66{
67 if (!mxPackageEncryption.is())
68 return false;
69
70 Reference<XInputStream> xInputStream (mxDocumentStream->getInputStream(), UNO_SET_THROW);
71 Reference<XSeekable> xSeekable(xInputStream, UNO_QUERY);
72
73 if (!xSeekable.is())
74 return false;
75
76 xSeekable->seek(0); // seek to begin of the document stream
77
79 return false;
80
81 mxPackageEncryption->setupEncryption(mMediaEncData);
82
83 Sequence<NamedValue> aStreams = mxPackageEncryption->encrypt(xInputStream);
84
85 for (const NamedValue & aStream : std::as_const(aStreams))
86 {
87 Reference<XOutputStream> xOutputStream(mrOleStorage.openOutputStream(aStream.Name), UNO_SET_THROW);
88 BinaryXOutputStream aBinaryOutputStream(xOutputStream, true);
89
90 css::uno::Sequence<sal_Int8> aStreamSequence;
91 aStream.Value >>= aStreamSequence;
92
93 aBinaryOutputStream.writeData(aStreamSequence);
94
95 aBinaryOutputStream.close();
96 }
97
98 return true;
99}
100
101} // namespace oox::crypto
102
103/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Wraps a UNO output stream and provides convenient access functions.
virtual void writeData(const StreamDataSequence &rData, size_t nAtomSize=1) override
Writes the passed data sequence.
void close() override
Flushes and closes the output stream.
bool isStorage() const
Returns true, if the object represents a valid storage.
Definition: storagebase.cxx:90
css::uno::Reference< css::io::XOutputStream > openOutputStream(const OUString &rStreamName)
Opens and returns the specified output stream from the storage.
oox::ole::OleStorage & mrOleStorage
css::uno::Reference< css::packages::XPackageEncryption > mxPackageEncryption
css::uno::Reference< css::uno::XComponentContext > mxContext
DocumentEncryption(const css::uno::Reference< css::uno::XComponentContext > &rxContext, css::uno::Reference< css::io::XStream > const &xDocumentStream, oox::ole::OleStorage &rOleStorage, const css::uno::Sequence< css::beans::NamedValue > &rMediaEncData)
css::uno::Reference< css::io::XStream > mxDocumentStream
const css::uno::Sequence< css::beans::NamedValue > & mMediaEncData
Implements stream access for binary OLE storages.
Definition: olestorage.hxx:44
uno::Reference< uno::XComponentContext > mxContext
Sequence< PropertyValue > aArguments
#define SAL_WARN(area, stream)
OUString Name