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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/* -*- 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "oox/core/filterdetect.hxx"
#include <com/sun/star/io/TempFile.hpp>
#include <com/sun/star/io/XStream.hpp>
#include <comphelper/docpasswordhelper.hxx>
#include <unotools/mediadescriptor.hxx>
#include <cppuhelper/supportsservice.hxx>
#include "oox/core/fastparser.hxx"
#include "oox/helper/attributelist.hxx"
#include "oox/helper/zipstorage.hxx"
#include "oox/ole/olestorage.hxx"
#include "oox/crypto/DocumentDecryption.hxx"
#include <com/sun/star/uri/UriReferenceFactory.hpp>
namespace oox {
namespace core {
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::io;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::xml::sax;
using namespace ::com::sun::star::uri;
using utl::MediaDescriptor;
using comphelper::SequenceAsHashMap;
using comphelper::IDocPasswordVerifier;
using comphelper::DocPasswordVerifierResult;
FilterDetectDocHandler::FilterDetectDocHandler( const Reference< XComponentContext >& rxContext, OUString& rFilterName ) :
mrFilterName( rFilterName ),
mxContext( rxContext )
{
maContextStack.reserve( 2 );
}
FilterDetectDocHandler::~FilterDetectDocHandler()
{
}
void SAL_CALL FilterDetectDocHandler::startDocument()
throw (SAXException, RuntimeException, std::exception)
{
}
void SAL_CALL FilterDetectDocHandler::endDocument()
throw (SAXException, RuntimeException, std::exception)
{
}
void SAL_CALL FilterDetectDocHandler::setDocumentLocator( const Reference<XLocator>& /*xLocator*/ )
throw (SAXException, RuntimeException, std::exception)
{
}
void SAL_CALL FilterDetectDocHandler::startFastElement(
sal_Int32 nElement, const Reference< XFastAttributeList >& rAttribs )
throw (SAXException,RuntimeException, std::exception)
{
AttributeList aAttribs( rAttribs );
switch ( nElement )
{
// cases for _rels/.rels
case PR_TOKEN( Relationships ):
break;
case PR_TOKEN( Relationship ):
if( !maContextStack.empty() && (maContextStack.back() == PR_TOKEN( Relationships )) )
parseRelationship( aAttribs );
break;
// cases for [Content_Types].xml
case PC_TOKEN( Types ):
break;
case PC_TOKEN( Default ):
if( !maContextStack.empty() && (maContextStack.back() == PC_TOKEN( Types )) )
parseContentTypesDefault( aAttribs );
break;
case PC_TOKEN( Override ):
if( !maContextStack.empty() && (maContextStack.back() == PC_TOKEN( Types )) )
parseContentTypesOverride( aAttribs );
break;
}
maContextStack.push_back( nElement );
}
void SAL_CALL FilterDetectDocHandler::startUnknownElement(
const OUString& /*Namespace*/, const OUString& /*Name*/, const Reference<XFastAttributeList>& /*Attribs*/ )
throw (SAXException, RuntimeException, std::exception)
{
}
void SAL_CALL FilterDetectDocHandler::endFastElement( sal_Int32 /*nElement*/ )
throw (SAXException, RuntimeException, std::exception)
{
maContextStack.pop_back();
}
void SAL_CALL FilterDetectDocHandler::endUnknownElement(
const OUString& /*Namespace*/, const OUString& /*Name*/ ) throw (SAXException, RuntimeException, std::exception)
{
}
Reference<XFastContextHandler> SAL_CALL FilterDetectDocHandler::createFastChildContext(
sal_Int32 /*Element*/, const Reference<XFastAttributeList>& /*Attribs*/ )
throw (SAXException, RuntimeException, std::exception)
{
return this;
}
Reference<XFastContextHandler> SAL_CALL FilterDetectDocHandler::createUnknownChildContext(
const OUString& /*Namespace*/, const OUString& /*Name*/, const Reference<XFastAttributeList>& /*Attribs*/)
throw (SAXException, RuntimeException, std::exception)
{
return this;
}
void SAL_CALL FilterDetectDocHandler::characters( const OUString& /*aChars*/ )
throw (SAXException, RuntimeException, std::exception)
{
}
void SAL_CALL FilterDetectDocHandler::ignorableWhitespace( const OUString& /*aWhitespaces*/ )
throw (SAXException, RuntimeException)
{
}
void SAL_CALL FilterDetectDocHandler::processingInstruction(
const OUString& /*aTarget*/, const OUString& /*aData*/ )
throw (SAXException, RuntimeException)
{
}
void FilterDetectDocHandler::parseRelationship( const AttributeList& rAttribs )
{
OUString aType = rAttribs.getString( XML_Type, OUString() );
if ( aType == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" // OOXML Transitional
|| aType == "http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument" ) //OOXML strict
{
Reference<XUriReferenceFactory> xFactory = UriReferenceFactory::create( mxContext );
try
{
// use '/' to representent the root of the zip package ( and provide a 'file' scheme to
// keep the XUriReference implementation happy )
Reference< XUriReference > xBase = xFactory->parse( OUString("file:///") );
Reference< XUriReference > xPart = xFactory->parse( rAttribs.getString( XML_Target, OUString() ) );
Reference< XUriReference > xAbs = xFactory->makeAbsolute( xBase, xPart, sal_True, RelativeUriExcessParentSegments_RETAIN );
if ( xAbs.is() )
maTargetPath = xAbs->getPath();
}
catch( const Exception& )
{
}
}
}
OUString FilterDetectDocHandler::getFilterNameFromContentType( const OUString& rContentType ) const
{
if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" ) ||
rContentType.equalsAscii("application/vnd.ms-word.document.macroEnabled.main+xml" ) )
return OUString( "writer_MS_Word_2007" );
if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml") ||
rContentType.equalsAscii("application/vnd.ms-word.template.macroEnabledTemplate.main+xml") )
return OUString( "writer_MS_Word_2007_Template" );
if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml") ||
rContentType.equalsAscii("application/vnd.ms-excel.sheet.macroEnabled.main+xml") )
return OUString( "MS Excel 2007 XML" );
if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml") ||
rContentType.equalsAscii("application/vnd.ms-excel.template.macroEnabled.main+xml") )
return OUString( "MS Excel 2007 XML Template" );
if ( rContentType == "application/vnd.ms-excel.sheet.binary.macroEnabled.main" )
return OUString( "MS Excel 2007 Binary" );
if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml") ||
rContentType.equalsAscii("application/vnd.ms-powerpoint.presentation.macroEnabled.main+xml") )
return OUString( "MS PowerPoint 2007 XML" );
if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.presentationml.slideshow.main+xml") ||
rContentType.equalsAscii("application/vnd.ms-powerpoint.slideshow.macroEnabled.main+xml") )
return OUString( "MS PowerPoint 2007 XML AutoPlay" );
if( rContentType.equalsAscii("application/vnd.openxmlformats-officedocument.presentationml.template.main+xml") ||
rContentType.equalsAscii("application/vnd.ms-powerpoint.template.macroEnabled.main+xml") )
return OUString( "MS PowerPoint 2007 XML Template" );
return OUString();
}
void FilterDetectDocHandler::parseContentTypesDefault( const AttributeList& rAttribs )
{
// only if no overridden part name found
if( mrFilterName.isEmpty() )
{
// check if target path ends with extension
OUString aExtension = rAttribs.getString( XML_Extension, OUString() );
sal_Int32 nExtPos = maTargetPath.getLength() - aExtension.getLength();
if( (nExtPos > 0) && (maTargetPath[ nExtPos - 1 ] == '.') && maTargetPath.match( aExtension, nExtPos ) )
mrFilterName = getFilterNameFromContentType( rAttribs.getString( XML_ContentType, OUString() ) );
}
}
void FilterDetectDocHandler::parseContentTypesOverride( const AttributeList& rAttribs )
{
if( rAttribs.getString( XML_PartName, OUString() ).equals( maTargetPath ) )
mrFilterName = getFilterNameFromContentType( rAttribs.getString( XML_ContentType, OUString() ) );
}
/* Helper for XServiceInfo */
Sequence< OUString > FilterDetect_getSupportedServiceNames()
{
Sequence< OUString > aServiceNames( 1 );
aServiceNames[ 0 ] = "com.sun.star.frame.ExtendedTypeDetection";
return aServiceNames;
}
/* Helper for XServiceInfo */
OUString FilterDetect_getImplementationName()
{
return OUString( "com.sun.star.comp.oox.FormatDetector" );
}
/* Helper for registry */
Reference< XInterface > SAL_CALL FilterDetect_createInstance( const Reference< XComponentContext >& rxContext ) throw( Exception )
{
return static_cast< ::cppu::OWeakObject* >( new FilterDetect( rxContext ) );
}
FilterDetect::FilterDetect( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) :
mxContext( rxContext, UNO_SET_THROW )
{
}
FilterDetect::~FilterDetect()
{
}
namespace
{
bool lclIsZipPackage( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm )
{
ZipStorage aZipStorage( rxContext, rxInStrm );
return aZipStorage.isStorage();
}
class PasswordVerifier : public IDocPasswordVerifier
{
public:
explicit PasswordVerifier( DocumentDecryption& aDecryptor );
virtual DocPasswordVerifierResult verifyPassword( const OUString& rPassword, Sequence<NamedValue>& rEncryptionData ) SAL_OVERRIDE;
virtual DocPasswordVerifierResult verifyEncryptionData( const Sequence<NamedValue>& rEncryptionData ) SAL_OVERRIDE;
private:
DocumentDecryption& mDecryptor;
};
PasswordVerifier::PasswordVerifier( DocumentDecryption& aDecryptor ) :
mDecryptor(aDecryptor)
{}
comphelper::DocPasswordVerifierResult PasswordVerifier::verifyPassword( const OUString& rPassword, Sequence<NamedValue>& rEncryptionData )
{
if(mDecryptor.generateEncryptionKey(rPassword))
rEncryptionData = mDecryptor.createEncryptionData(rPassword);
return rEncryptionData.hasElements() ? comphelper::DocPasswordVerifierResult_OK : comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
}
comphelper::DocPasswordVerifierResult PasswordVerifier::verifyEncryptionData( const Sequence<NamedValue>& rEncryptionData )
{
comphelper::DocPasswordVerifierResult aResult = comphelper::DocPasswordVerifierResult_WRONG_PASSWORD;
if (DocumentDecryption::checkEncryptionData(rEncryptionData))
aResult = comphelper::DocPasswordVerifierResult_OK;
return aResult;
}
} // namespace
Reference< XInputStream > FilterDetect::extractUnencryptedPackage( MediaDescriptor& rMediaDescriptor ) const
{
// try the plain input stream
Reference<XInputStream> xInputStream( rMediaDescriptor[ MediaDescriptor::PROP_INPUTSTREAM() ], UNO_QUERY );
if( !xInputStream.is() || lclIsZipPackage( mxContext, xInputStream ) )
return xInputStream;
// check if a temporary file is passed in the 'ComponentData' property
Reference<XStream> xDecrypted( rMediaDescriptor.getComponentDataEntry( "DecryptedPackage" ), UNO_QUERY );
if( xDecrypted.is() )
{
Reference<XInputStream> xDecryptedInputStream = xDecrypted->getInputStream();
if( lclIsZipPackage( mxContext, xDecryptedInputStream ) )
return xDecryptedInputStream;
}
// try to decrypt an encrypted OLE package
oox::ole::OleStorage aOleStorage( mxContext, xInputStream, false );
if( aOleStorage.isStorage() )
{
try
{
DocumentDecryption aDecryptor(aOleStorage, mxContext);
if( aDecryptor.readEncryptionInfo() )
{
/* "VelvetSweatshop" is the built-in default encryption
password used by MS Excel for the "workbook protection"
feature with password. Try this first before prompting the
user for a password. */
std::vector<OUString> aDefaultPasswords;
aDefaultPasswords.push_back("VelvetSweatshop");
/* Use the comphelper password helper to request a password.
This helper returns either with the correct password
(according to the verifier), or with an empty string if
user has cancelled the password input dialog. */
PasswordVerifier aVerifier( aDecryptor );
Sequence<NamedValue> aEncryptionData;
aEncryptionData = rMediaDescriptor.requestAndVerifyDocPassword(
aVerifier,
comphelper::DocPasswordRequestType_MS,
&aDefaultPasswords );
if( aEncryptionData.getLength() == 0 )
{
rMediaDescriptor[ MediaDescriptor::PROP_ABORTED() ] <<= true;
}
else
{
// create temporary file for unencrypted package
Reference<XStream> xTempFile( TempFile::create(mxContext), UNO_QUERY_THROW );
aDecryptor.decrypt( xTempFile );
// store temp file in media descriptor to keep it alive
rMediaDescriptor.setComponentDataEntry( "DecryptedPackage", Any( xTempFile ) );
Reference<XInputStream> xDecryptedInputStream = xTempFile->getInputStream();
if( lclIsZipPackage( mxContext, xDecryptedInputStream ) )
return xDecryptedInputStream;
}
}
}
catch( const Exception& )
{
}
}
return Reference<XInputStream>();
}
// com.sun.star.lang.XServiceInfo interface -----------------------------------
OUString SAL_CALL FilterDetect::getImplementationName() throw( RuntimeException, std::exception )
{
return FilterDetect_getImplementationName();
}
sal_Bool SAL_CALL FilterDetect::supportsService( const OUString& rServiceName ) throw( RuntimeException, std::exception )
{
return cppu::supportsService(this, rServiceName);
}
Sequence< OUString > SAL_CALL FilterDetect::getSupportedServiceNames() throw( RuntimeException, std::exception )
{
return FilterDetect_getSupportedServiceNames();
}
// com.sun.star.document.XExtendedFilterDetection interface -------------------
OUString SAL_CALL FilterDetect::detect( Sequence< PropertyValue >& rMediaDescSeq ) throw( RuntimeException, std::exception )
{
OUString aFilterName;
MediaDescriptor aMediaDescriptor( rMediaDescSeq );
/* Check that the user has not choosen to abort detection, e.g. by hitting
'Cancel' in the password input dialog. This may happen because this
filter detection is used by different filters. */
bool bAborted = aMediaDescriptor.getUnpackedValueOrDefault( MediaDescriptor::PROP_ABORTED(), false );
if( !bAborted ) try
{
aMediaDescriptor.addInputStream();
/* Get the unencrypted input stream. This may include creation of a
temporary file that contains the decrypted package. This temporary
file will be stored in the 'ComponentData' property of the media
descriptor. */
Reference< XInputStream > xInputStream( extractUnencryptedPackage( aMediaDescriptor ), UNO_SET_THROW );
// stream must be a ZIP package
ZipStorage aZipStorage( mxContext, xInputStream );
if( aZipStorage.isStorage() )
{
// create the fast parser, register the XML namespaces, set document handler
FastParser aParser( mxContext );
aParser.registerNamespace( NMSP_packageRel );
aParser.registerNamespace( NMSP_officeRel );
aParser.registerNamespace( NMSP_packageContentTypes );
aParser.setDocumentHandler( new FilterDetectDocHandler( mxContext, aFilterName ) );
/* Parse '_rels/.rels' to get the target path and '[Content_Types].xml'
to determine the content type of the part at the target path. */
aParser.parseStream( aZipStorage, "_rels/.rels" );
aParser.parseStream( aZipStorage, "[Content_Types].xml" );
}
}
catch( const Exception& )
{
}
// write back changed media descriptor members
aMediaDescriptor >> rMediaDescSeq;
return aFilterName;
}
} // namespace core
} // namespace oox
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|