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