1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#include <oox/crypto/DocumentDecryption.hxx>
#include <comphelper/sequenceashashmap.hxx>
#include <com/sun/star/beans/NamedValue.hpp>
#include <com/sun/star/io/XSeekable.hpp>
#include <com/sun/star/io/XStream.hpp>
#include <com/sun/star/io/IOException.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/packages/XPackageEncryption.hpp>
#include <oox/ole/olestorage.hxx>
#include <oox/helper/binaryinputstream.hxx>
#include <sal/log.hxx>
#include <utility>
namespace
{
void lcl_getListOfStreams(oox::StorageBase* pStorage, std::vector<OUString>& rElementNames)
{
std::vector<OUString> oElementNames;
pStorage->getElementNames(oElementNames);
for (const auto& sName : oElementNames)
{
oox::StorageRef rSubStorage = pStorage->openSubStorage(sName, false);
if (rSubStorage && rSubStorage->isStorage())
{
lcl_getListOfStreams(rSubStorage.get(), rElementNames);
}
else
{
if (pStorage->isRootStorage())
rElementNames.push_back(sName);
else
rElementNames.push_back(pStorage->getPath() + "/" + sName);
}
}
}
}
namespace oox::crypto
{
using namespace css;
DocumentDecryption::DocumentDecryption(css::uno::Reference<css::uno::XComponentContext> xContext,
oox::ole::OleStorage& rOleStorage)
: mxContext(std::move(xContext))
, mrOleStorage(rOleStorage)
{
// Get OLE streams into sequences for later use in CryptoEngine
std::vector<OUString> aStreamNames;
lcl_getListOfStreams(&mrOleStorage, aStreamNames);
comphelper::SequenceAsHashMap aStreamsData;
for (const auto& sStreamName : aStreamNames)
{
uno::Reference<io::XInputStream> xStream = mrOleStorage.openInputStream(sStreamName);
if (!xStream.is())
throw io::IOException("Cannot open OLE input stream for " + sStreamName + "!");
BinaryXInputStream aBinaryInputStream(xStream, true);
css::uno::Sequence<sal_Int8> oData;
sal_Int32 nStreamSize = aBinaryInputStream.size();
sal_Int32 nReadBytes = aBinaryInputStream.readData(oData, nStreamSize);
if (nStreamSize != nReadBytes)
{
SAL_WARN("oox", "OLE stream invalid content");
throw io::IOException("OLE stream invalid content for " + sStreamName + "!");
}
aStreamsData[sStreamName] <<= oData;
}
maStreamsSequence = aStreamsData.getAsConstNamedValueList();
}
bool DocumentDecryption::generateEncryptionKey(const OUString& rPassword)
{
if (mxPackageEncryption.is())
return mxPackageEncryption->generateEncryptionKey(rPassword);
return false;
}
bool DocumentDecryption::readEncryptionInfo()
{
if (!mrOleStorage.isStorage())
return false;
// Read 0x6DataSpaces/DataSpaceMap
uno::Reference<io::XInputStream> xDataSpaceMap
= mrOleStorage.openInputStream("\006DataSpaces/DataSpaceMap");
OUString sDataSpaceName;
if (xDataSpaceMap.is())
{
bool bBroken = false;
BinaryXInputStream aDataSpaceStream(xDataSpaceMap, true);
sal_uInt32 aHeaderLength = aDataSpaceStream.readuInt32();
SAL_WARN_IF(aHeaderLength != 8, "oox",
"DataSpaceMap length != 8 is not supported. Some content may be skipped");
sal_uInt32 aEntryCount = aDataSpaceStream.readuInt32();
SAL_WARN_IF(aEntryCount != 1, "oox",
"DataSpaceMap contains more than one entry. Some content may be skipped");
// Read each DataSpaceMapEntry (MS-OFFCRYPTO 2.1.6.1)
for (sal_uInt32 i = 0; i < aEntryCount && !bBroken; i++)
{
// entryLen unused for the moment
aDataSpaceStream.skip(sizeof(sal_uInt32));
// Read each DataSpaceReferenceComponent (MS-OFFCRYPTO 2.1.6.2)
sal_uInt32 aReferenceComponentCount = aDataSpaceStream.readuInt32();
for (sal_uInt32 j = 0; j < aReferenceComponentCount && !bBroken; j++)
{
// Read next reference component
// refComponentType unused for the moment
aDataSpaceStream.skip(sizeof(sal_uInt32));
sal_uInt32 aReferenceComponentNameLength = aDataSpaceStream.readuInt32();
// sReferenceComponentName unused for the moment
if (aDataSpaceStream.getRemaining() < aReferenceComponentNameLength)
{
bBroken = true;
break;
}
aDataSpaceStream.readUnicodeArray(aReferenceComponentNameLength / 2);
aDataSpaceStream.skip((4 - (aReferenceComponentNameLength & 3))
& 3); // Skip padding
bBroken |= aDataSpaceStream.isEof();
}
sal_uInt32 aDataSpaceNameLength = aDataSpaceStream.readuInt32();
if (aDataSpaceStream.getRemaining() < aDataSpaceNameLength)
{
bBroken = true;
break;
}
sDataSpaceName = aDataSpaceStream.readUnicodeArray(aDataSpaceNameLength / 2);
aDataSpaceStream.skip((4 - (aDataSpaceNameLength & 3)) & 3); // Skip padding
bBroken |= aDataSpaceStream.isEof();
}
if (bBroken)
{
SAL_WARN("oox", "EOF on parsing DataSpaceMapEntry table");
return false;
}
}
else
{
// Fallback for documents generated by LO: they sometimes do not have all
// required by MS-OFFCRYPTO specification streams (0x6DataSpaces/DataSpaceMap and others)
SAL_WARN("oox", "Encrypted package does not contain DataSpaceMap");
sDataSpaceName = "StrongEncryptionDataSpace";
}
uno::Sequence<uno::Any> aArguments;
mxPackageEncryption.set(
mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
"com.sun.star.comp.oox.crypto." + sDataSpaceName, aArguments, mxContext),
css::uno::UNO_QUERY);
if (!mxPackageEncryption.is())
{
// we do not know how to decrypt this document
return false;
}
return mxPackageEncryption->readEncryptionInfo(maStreamsSequence);
}
uno::Sequence<beans::NamedValue> DocumentDecryption::createEncryptionData(const OUString& rPassword)
{
if (!mxPackageEncryption.is())
return uno::Sequence<beans::NamedValue>();
return mxPackageEncryption->createEncryptionData(rPassword);
}
bool DocumentDecryption::decrypt(const uno::Reference<io::XStream>& xDocumentStream)
{
bool bResult = false;
if (!mrOleStorage.isStorage())
return false;
if (!mxPackageEncryption.is())
return false;
// open the required input streams in the encrypted package
uno::Reference<io::XInputStream> xEncryptedPackage
= mrOleStorage.openInputStream("EncryptedPackage");
// create temporary file for unencrypted package
uno::Reference<io::XOutputStream> xDecryptedPackage = xDocumentStream->getOutputStream();
bResult = mxPackageEncryption->decrypt(xEncryptedPackage, xDecryptedPackage);
css::uno::Reference<io::XSeekable> xSeekable(xDecryptedPackage, css::uno::UNO_QUERY);
xSeekable->seek(0);
if (bResult)
return mxPackageEncryption->checkDataIntegrity();
return bResult;
}
} // namespace oox::crypto
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|