Line data Source code
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 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : // MARKER( update_precomp.py ): autogen include statement, do not remove
21 : #include <ManifestImport.hxx>
22 : #include <ManifestDefines.hxx>
23 : #include <sax/tools/converter.hxx>
24 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
25 : #include <com/sun/star/xml/crypto/DigestID.hpp>
26 : #include <com/sun/star/xml/crypto/CipherID.hpp>
27 : #include <com/sun/star/beans/PropertyValue.hpp>
28 :
29 : using namespace com::sun::star::uno;
30 : using namespace com::sun::star::beans;
31 : using namespace com::sun::star;
32 : using namespace std;
33 :
34 :
35 : // ---------------------------------------------------
36 423 : ManifestImport::ManifestImport( vector < Sequence < PropertyValue > > & rNewManVector )
37 : : bIgnoreEncryptData ( sal_False )
38 : , nDerivedKeySize( 0 )
39 : , rManVector ( rNewManVector )
40 :
41 : , sFileEntryElement ( ELEMENT_FILE_ENTRY )
42 : , sManifestElement ( ELEMENT_MANIFEST )
43 : , sEncryptionDataElement( ELEMENT_ENCRYPTION_DATA )
44 : , sAlgorithmElement ( ELEMENT_ALGORITHM )
45 : , sStartKeyAlgElement ( ELEMENT_START_KEY_GENERATION )
46 : , sKeyDerivationElement( ELEMENT_KEY_DERIVATION )
47 :
48 : , sCdataAttribute ( ATTRIBUTE_CDATA )
49 : , sMediaTypeAttribute ( ATTRIBUTE_MEDIA_TYPE )
50 : , sVersionAttribute ( ATTRIBUTE_VERSION )
51 : , sFullPathAttribute ( ATTRIBUTE_FULL_PATH )
52 : , sSizeAttribute ( ATTRIBUTE_SIZE )
53 : , sSaltAttribute ( ATTRIBUTE_SALT )
54 : , sInitialisationVectorAttribute ( ATTRIBUTE_INITIALISATION_VECTOR )
55 : , sIterationCountAttribute ( ATTRIBUTE_ITERATION_COUNT )
56 : , sKeySizeAttribute ( ATTRIBUTE_KEY_SIZE )
57 : , sAlgorithmNameAttribute ( ATTRIBUTE_ALGORITHM_NAME )
58 : , sStartKeyAlgNameAttribute ( ATTRIBUTE_START_KEY_GENERATION_NAME )
59 : , sKeyDerivationNameAttribute ( ATTRIBUTE_KEY_DERIVATION_NAME )
60 : , sChecksumAttribute ( ATTRIBUTE_CHECKSUM )
61 : , sChecksumTypeAttribute ( ATTRIBUTE_CHECKSUM_TYPE )
62 :
63 : , sFullPathProperty ( "FullPath" )
64 : , sMediaTypeProperty ( "MediaType" )
65 : , sVersionProperty ( "Version" )
66 : , sIterationCountProperty ( "IterationCount" )
67 : , sDerivedKeySizeProperty ( "DerivedKeySize" )
68 : , sSaltProperty ( "Salt" )
69 : , sInitialisationVectorProperty ( "InitialisationVector" )
70 : , sSizeProperty ( "Size" )
71 : , sDigestProperty ( "Digest" )
72 : , sEncryptionAlgProperty ( "EncryptionAlgorithm" )
73 : , sStartKeyAlgProperty ( "StartKeyAlgorithm" )
74 : , sDigestAlgProperty ( "DigestAlgorithm" )
75 :
76 : , sWhiteSpace ( " " )
77 :
78 : , sSHA256_URL ( SHA256_URL )
79 : , sSHA1_Name ( SHA1_NAME )
80 : , sSHA1_URL ( SHA1_URL )
81 :
82 : , sSHA256_1k_URL ( SHA256_1K_URL )
83 : , sSHA1_1k_Name ( SHA1_1K_NAME )
84 : , sSHA1_1k_URL ( SHA1_1K_URL )
85 :
86 : , sBlowfish_Name ( BLOWFISH_NAME )
87 : , sBlowfish_URL ( BLOWFISH_URL )
88 : , sAES128_URL ( AES128_URL )
89 : , sAES192_URL ( AES192_URL )
90 : , sAES256_URL ( AES256_URL )
91 :
92 : , sPBKDF2_Name ( PBKDF2_NAME )
93 423 : , sPBKDF2_URL ( PBKDF2_URL )
94 : {
95 423 : aStack.reserve( 10 );
96 423 : }
97 :
98 : // ---------------------------------------------------
99 846 : ManifestImport::~ManifestImport ( void )
100 : {
101 846 : }
102 :
103 : // ---------------------------------------------------
104 423 : void SAL_CALL ManifestImport::startDocument( )
105 : throw( xml::sax::SAXException, uno::RuntimeException )
106 : {
107 423 : }
108 :
109 : // ---------------------------------------------------
110 423 : void SAL_CALL ManifestImport::endDocument( )
111 : throw( xml::sax::SAXException, uno::RuntimeException )
112 : {
113 423 : }
114 :
115 3585 : void ManifestImport::doFileEntry(StringHashMap &rConvertedAttribs)
116 : throw( uno::RuntimeException )
117 : {
118 3585 : aSequence.resize(PKG_SIZE_ENCR_MNFST);
119 :
120 3585 : aSequence[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
121 3585 : aSequence[PKG_MNFST_FULLPATH].Value <<= rConvertedAttribs[sFullPathAttribute];
122 3585 : aSequence[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty;
123 3585 : aSequence[PKG_MNFST_MEDIATYPE].Value <<= rConvertedAttribs[sMediaTypeAttribute];
124 :
125 3585 : OUString sVersion = rConvertedAttribs[sVersionAttribute];
126 3585 : if ( sVersion.getLength() )
127 : {
128 221 : aSequence[PKG_MNFST_VERSION].Name = sVersionProperty;
129 221 : aSequence[PKG_MNFST_VERSION].Value <<= sVersion;
130 : }
131 :
132 7170 : OUString sSize = rConvertedAttribs[sSizeAttribute];
133 3585 : if ( sSize.getLength() )
134 : {
135 15 : sal_Int64 nSize = sSize.toInt64();
136 15 : aSequence[PKG_MNFST_UCOMPSIZE].Name = sSizeProperty;
137 15 : aSequence[PKG_MNFST_UCOMPSIZE].Value <<= nSize;
138 3585 : }
139 3585 : }
140 :
141 15 : void ManifestImport::doEncryptionData(StringHashMap &rConvertedAttribs)
142 : throw( uno::RuntimeException )
143 : {
144 : // If this element exists, then this stream is encrypted and we need
145 : // to import the initialisation vector, salt and iteration count used
146 15 : nDerivedKeySize = 0;
147 15 : OUString aString = rConvertedAttribs[sChecksumTypeAttribute];
148 15 : if ( !bIgnoreEncryptData )
149 : {
150 15 : if ( aString.equals( sSHA1_1k_Name ) || aString.equals( sSHA1_1k_URL ) )
151 : {
152 5 : aSequence[PKG_MNFST_DIGESTALG].Name = sDigestAlgProperty;
153 5 : aSequence[PKG_MNFST_DIGESTALG].Value <<= xml::crypto::DigestID::SHA1_1K;
154 : }
155 10 : else if ( aString.equals( sSHA256_1k_URL ) )
156 : {
157 10 : aSequence[PKG_MNFST_DIGESTALG].Name = sDigestAlgProperty;
158 10 : aSequence[PKG_MNFST_DIGESTALG].Value <<= xml::crypto::DigestID::SHA256_1K;
159 : }
160 : else
161 0 : bIgnoreEncryptData = sal_True;
162 :
163 15 : if ( !bIgnoreEncryptData )
164 : {
165 15 : aString = rConvertedAttribs[sChecksumAttribute];
166 15 : uno::Sequence < sal_Int8 > aDecodeBuffer;
167 15 : ::sax::Converter::decodeBase64(aDecodeBuffer, aString);
168 15 : aSequence[PKG_MNFST_DIGEST].Name = sDigestProperty;
169 15 : aSequence[PKG_MNFST_DIGEST].Value <<= aDecodeBuffer;
170 : }
171 15 : }
172 15 : }
173 :
174 15 : void ManifestImport::doAlgorithm(StringHashMap &rConvertedAttribs)
175 : throw( uno::RuntimeException )
176 : {
177 15 : if ( !bIgnoreEncryptData )
178 : {
179 15 : OUString aString = rConvertedAttribs[sAlgorithmNameAttribute];
180 15 : if ( aString.equals( sBlowfish_Name ) || aString.equals( sBlowfish_URL ) )
181 : {
182 5 : aSequence[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
183 5 : aSequence[PKG_MNFST_ENCALG].Value <<= xml::crypto::CipherID::BLOWFISH_CFB_8;
184 : }
185 10 : else if ( aString.equals( sAES256_URL ) )
186 : {
187 10 : aSequence[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
188 10 : aSequence[PKG_MNFST_ENCALG].Value <<= xml::crypto::CipherID::AES_CBC_W3C_PADDING;
189 : OSL_ENSURE( !nDerivedKeySize || nDerivedKeySize == 32, "Unexpected derived key length!" );
190 10 : nDerivedKeySize = 32;
191 : }
192 0 : else if ( aString.equals( sAES192_URL ) )
193 : {
194 0 : aSequence[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
195 0 : aSequence[PKG_MNFST_ENCALG].Value <<= xml::crypto::CipherID::AES_CBC_W3C_PADDING;
196 : OSL_ENSURE( !nDerivedKeySize || nDerivedKeySize == 24, "Unexpected derived key length!" );
197 0 : nDerivedKeySize = 24;
198 : }
199 0 : else if ( aString.equals( sAES128_URL ) )
200 : {
201 0 : aSequence[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
202 0 : aSequence[PKG_MNFST_ENCALG].Value <<= xml::crypto::CipherID::AES_CBC_W3C_PADDING;
203 : OSL_ENSURE( !nDerivedKeySize || nDerivedKeySize == 16, "Unexpected derived key length!" );
204 0 : nDerivedKeySize = 16;
205 : }
206 : else
207 0 : bIgnoreEncryptData = sal_True;
208 :
209 15 : if ( !bIgnoreEncryptData )
210 : {
211 15 : aString = rConvertedAttribs[sInitialisationVectorAttribute];
212 15 : uno::Sequence < sal_Int8 > aDecodeBuffer;
213 15 : ::sax::Converter::decodeBase64(aDecodeBuffer, aString);
214 15 : aSequence[PKG_MNFST_INIVECTOR].Name = sInitialisationVectorProperty;
215 15 : aSequence[PKG_MNFST_INIVECTOR].Value <<= aDecodeBuffer;
216 15 : }
217 : }
218 15 : }
219 :
220 15 : void ManifestImport::doKeyDerivation(StringHashMap &rConvertedAttribs)
221 : throw( uno::RuntimeException )
222 : {
223 15 : if ( !bIgnoreEncryptData )
224 : {
225 15 : OUString aString = rConvertedAttribs[sKeyDerivationNameAttribute];
226 15 : if ( aString.equals( sPBKDF2_Name ) || aString.equals( sPBKDF2_URL ) )
227 : {
228 15 : aString = rConvertedAttribs[sSaltAttribute];
229 15 : uno::Sequence < sal_Int8 > aDecodeBuffer;
230 15 : ::sax::Converter::decodeBase64(aDecodeBuffer, aString);
231 15 : aSequence[PKG_MNFST_SALT].Name = sSaltProperty;
232 15 : aSequence[PKG_MNFST_SALT].Value <<= aDecodeBuffer;
233 :
234 15 : aString = rConvertedAttribs[sIterationCountAttribute];
235 15 : aSequence[PKG_MNFST_ITERATION].Name = sIterationCountProperty;
236 15 : aSequence[PKG_MNFST_ITERATION].Value <<= aString.toInt32();
237 :
238 15 : aString = rConvertedAttribs[sKeySizeAttribute];
239 15 : if ( aString.getLength() )
240 : {
241 15 : sal_Int32 nKey = aString.toInt32();
242 : OSL_ENSURE( !nDerivedKeySize || nKey == nDerivedKeySize , "Provided derived key length differs from the expected one!" );
243 15 : nDerivedKeySize = nKey;
244 : }
245 0 : else if ( !nDerivedKeySize )
246 0 : nDerivedKeySize = 16;
247 0 : else if ( nDerivedKeySize != 16 )
248 : OSL_ENSURE( sal_False, "Default derived key length differs from the expected one!" );
249 :
250 15 : aSequence[PKG_MNFST_DERKEYSIZE].Name = sDerivedKeySizeProperty;
251 15 : aSequence[PKG_MNFST_DERKEYSIZE].Value <<= nDerivedKeySize;
252 : }
253 : else
254 0 : bIgnoreEncryptData = sal_True;
255 : }
256 15 : }
257 :
258 15 : void ManifestImport::doStartKeyAlg(StringHashMap &rConvertedAttribs)
259 : throw( uno::RuntimeException )
260 : {
261 15 : OUString aString = rConvertedAttribs[sStartKeyAlgNameAttribute];
262 15 : if ( aString.equals( sSHA256_URL ) )
263 : {
264 10 : aSequence[PKG_MNFST_STARTALG].Name = sStartKeyAlgProperty;
265 10 : aSequence[PKG_MNFST_STARTALG].Value <<= xml::crypto::DigestID::SHA256;
266 : }
267 5 : else if ( aString.equals( sSHA1_Name ) || aString.equals( sSHA1_URL ) )
268 : {
269 5 : aSequence[PKG_MNFST_STARTALG].Name = sStartKeyAlgProperty;
270 5 : aSequence[PKG_MNFST_STARTALG].Value <<= xml::crypto::DigestID::SHA1;
271 : }
272 : else
273 0 : bIgnoreEncryptData = sal_True;
274 15 : }
275 :
276 : // ---------------------------------------------------
277 4068 : void SAL_CALL ManifestImport::startElement( const OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs )
278 : throw( xml::sax::SAXException, uno::RuntimeException )
279 : {
280 4068 : StringHashMap aConvertedAttribs;
281 8136 : OUString aConvertedName = PushNameAndNamespaces( aName, xAttribs, aConvertedAttribs );
282 :
283 4068 : size_t nLevel = aStack.size();
284 :
285 : assert(nLevel >= 1);
286 :
287 4068 : switch (nLevel)
288 : {
289 : case 1:
290 : {
291 423 : if (!aConvertedName.equalsAscii(ELEMENT_MANIFEST)) //manifest:manifest
292 0 : aStack.back().m_bValid = false;
293 423 : break;
294 : }
295 : case 2:
296 : {
297 3585 : if (aConvertedName == sFileEntryElement) //manifest:file-entry
298 3585 : doFileEntry(aConvertedAttribs);
299 : else
300 0 : aStack.back().m_bValid = false;
301 3585 : break;
302 : }
303 : case 3:
304 : {
305 15 : ManifestStack::reverse_iterator aIter = aStack.rbegin();
306 15 : ++aIter;
307 :
308 15 : if (!aIter->m_bValid)
309 0 : aStack.back().m_bValid = false;
310 15 : else if (aConvertedName.equals(sEncryptionDataElement)) //manifest:encryption-data
311 15 : doEncryptionData(aConvertedAttribs);
312 : else
313 0 : aStack.back().m_bValid = false;
314 15 : break;
315 : }
316 : case 4:
317 : {
318 45 : ManifestStack::reverse_iterator aIter = aStack.rbegin();
319 45 : ++aIter;
320 :
321 45 : if (!aIter->m_bValid)
322 0 : aStack.back().m_bValid = false;
323 45 : else if (aConvertedName.equals(sAlgorithmElement)) //manifest:algorithm,
324 15 : doAlgorithm(aConvertedAttribs);
325 30 : else if (aConvertedName.equals(sKeyDerivationElement)) //manifest:key-derivation,
326 15 : doKeyDerivation(aConvertedAttribs);
327 15 : else if (aConvertedName.equals(sStartKeyAlgElement)) //manifest:start-key-generation
328 15 : doStartKeyAlg(aConvertedAttribs);
329 : else
330 0 : aStack.back().m_bValid = false;
331 45 : break;
332 : }
333 : default:
334 0 : aStack.back().m_bValid = false;
335 0 : break;
336 4068 : }
337 4068 : }
338 :
339 : namespace
340 : {
341 43020 : bool isEmpty(const com::sun::star::beans::PropertyValue &rProp)
342 : {
343 43020 : return rProp.Name.isEmpty();
344 : }
345 : }
346 :
347 : // ---------------------------------------------------
348 4068 : void SAL_CALL ManifestImport::endElement( const OUString& aName )
349 : throw( xml::sax::SAXException, uno::RuntimeException )
350 : {
351 4068 : OUString aConvertedName = ConvertName( aName );
352 4068 : if ( !aStack.empty() && aStack.rbegin()->m_aConvertedName.equals( aConvertedName ) )
353 : {
354 4068 : if ( aConvertedName.equals( sFileEntryElement ) && aStack.back().m_bValid )
355 : {
356 3585 : com::sun::star::beans::PropertyValue aEmpty;
357 : aSequence.erase(std::remove_if(aSequence.begin(), aSequence.end(),
358 3585 : isEmpty), aSequence.end());
359 :
360 3585 : bIgnoreEncryptData = sal_False;
361 3585 : rManVector.push_back ( aSequence.getAsConstList() );
362 :
363 3585 : aSequence.clear();
364 : }
365 :
366 4068 : aStack.pop_back();
367 4068 : }
368 4068 : }
369 :
370 : // ---------------------------------------------------
371 7677 : void SAL_CALL ManifestImport::characters( const OUString& /*aChars*/ )
372 : throw( xml::sax::SAXException, uno::RuntimeException )
373 : {
374 7677 : }
375 :
376 : // ---------------------------------------------------
377 0 : void SAL_CALL ManifestImport::ignorableWhitespace( const OUString& /*aWhitespaces*/ )
378 : throw( xml::sax::SAXException, uno::RuntimeException )
379 : {
380 0 : }
381 :
382 : // ---------------------------------------------------
383 0 : void SAL_CALL ManifestImport::processingInstruction( const OUString& /*aTarget*/, const OUString& /*aData*/ )
384 : throw( xml::sax::SAXException, uno::RuntimeException )
385 : {
386 0 : }
387 :
388 : // ---------------------------------------------------
389 423 : void SAL_CALL ManifestImport::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
390 : throw( xml::sax::SAXException, uno::RuntimeException )
391 : {
392 423 : }
393 :
394 : // ---------------------------------------------------
395 4068 : OUString ManifestImport::PushNameAndNamespaces( const OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs, StringHashMap& o_aConvertedAttribs )
396 : {
397 4068 : StringHashMap aNamespaces;
398 8136 : ::std::vector< ::std::pair< OUString, OUString > > aAttribsStrs;
399 :
400 4068 : if ( xAttribs.is() )
401 : {
402 4068 : sal_Int16 nAttrCount = xAttribs.is() ? xAttribs->getLength() : 0;
403 4068 : aAttribsStrs.reserve( nAttrCount );
404 :
405 12155 : for( sal_Int16 nInd = 0; nInd < nAttrCount; nInd++ )
406 : {
407 8087 : OUString aAttrName = xAttribs->getNameByIndex( nInd );
408 16174 : OUString aAttrValue = xAttribs->getValueByIndex( nInd );
409 16174 : if ( aAttrName.getLength() >= 5
410 8087 : && aAttrName.startsWith("xmlns")
411 8432 : && ( aAttrName.getLength() == 5 || aAttrName.getStr()[5] == ( sal_Unicode )':' ) )
412 : {
413 : // this is a namespace declaration
414 345 : OUString aNsName( ( aAttrName.getLength() == 5 ) ? OUString() : aAttrName.copy( 6 ) );
415 345 : aNamespaces[aNsName] = aAttrValue;
416 : }
417 : else
418 : {
419 : // this is no namespace declaration
420 7742 : aAttribsStrs.push_back( pair< OUString, OUString >( aAttrName, aAttrValue ) );
421 : }
422 8087 : }
423 : }
424 :
425 4068 : OUString aConvertedName = ConvertNameWithNamespace( aName, aNamespaces );
426 4068 : if ( !aConvertedName.getLength() )
427 3723 : aConvertedName = ConvertName( aName );
428 :
429 4068 : aStack.push_back( ManifestScopeEntry( aConvertedName, aNamespaces ) );
430 :
431 11810 : for ( sal_uInt16 nInd = 0; nInd < aAttribsStrs.size(); nInd++ )
432 : {
433 : // convert the attribute names on filling
434 7742 : o_aConvertedAttribs[ConvertName( aAttribsStrs[nInd].first )] = aAttribsStrs[nInd].second;
435 : }
436 :
437 8136 : return aConvertedName;
438 : }
439 :
440 : // ---------------------------------------------------
441 18009 : OUString ManifestImport::ConvertNameWithNamespace( const OUString& aName, const StringHashMap& aNamespaces )
442 : {
443 18009 : OUString aNsAlias;
444 36018 : OUString aPureName = aName;
445 :
446 18009 : sal_Int32 nInd = aName.indexOf( ( sal_Unicode )':' );
447 18009 : if ( nInd != -1 && nInd < aName.getLength() )
448 : {
449 18009 : aNsAlias = aName.copy( 0, nInd );
450 18009 : aPureName = aName.copy( nInd + 1 );
451 : }
452 :
453 18009 : OUString aResult;
454 :
455 18009 : StringHashMap::const_iterator aIter = aNamespaces.find( aNsAlias );
456 54027 : if ( aIter != aNamespaces.end()
457 54027 : && ( aIter->second == MANIFEST_NAMESPACE || aIter->second == MANIFEST_OASIS_NAMESPACE ) )
458 : {
459 : // no check for manifest.xml consistency currently since the old versions have supported inconsistent documents as well
460 14286 : aResult = MANIFEST_NSPREFIX;
461 14286 : aResult += aPureName;
462 : }
463 :
464 36018 : return aResult;
465 : }
466 :
467 : // ---------------------------------------------------
468 15533 : OUString ManifestImport::ConvertName( const OUString& aName )
469 : {
470 15533 : OUString aConvertedName;
471 42669 : for ( ManifestStack::reverse_iterator aIter = aStack.rbegin(); !aConvertedName.getLength() && aIter != aStack.rend(); ++aIter )
472 : {
473 27136 : if ( !aIter->m_aNamespaces.empty() )
474 13941 : aConvertedName = ConvertNameWithNamespace( aName, aIter->m_aNamespaces );
475 : }
476 :
477 15533 : if ( !aConvertedName.getLength() )
478 1592 : aConvertedName = aName;
479 :
480 15533 : return aConvertedName;
481 : }
482 :
483 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|