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