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