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