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 0 : 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 0 : , sPBKDF2_URL ( PBKDF2_URL )
92 : {
93 0 : aStack.reserve( 10 );
94 0 : }
95 :
96 0 : ManifestImport::~ManifestImport ( void )
97 : {
98 0 : }
99 :
100 0 : void SAL_CALL ManifestImport::startDocument( )
101 : throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
102 : {
103 0 : }
104 :
105 0 : void SAL_CALL ManifestImport::endDocument( )
106 : throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
107 : {
108 0 : }
109 :
110 0 : void ManifestImport::doFileEntry(StringHashMap &rConvertedAttribs)
111 : throw( uno::RuntimeException )
112 : {
113 0 : aSequence.resize(PKG_SIZE_ENCR_MNFST);
114 :
115 0 : aSequence[PKG_MNFST_FULLPATH].Name = sFullPathProperty;
116 0 : aSequence[PKG_MNFST_FULLPATH].Value <<= rConvertedAttribs[sFullPathAttribute];
117 0 : aSequence[PKG_MNFST_MEDIATYPE].Name = sMediaTypeProperty;
118 0 : aSequence[PKG_MNFST_MEDIATYPE].Value <<= rConvertedAttribs[sMediaTypeAttribute];
119 :
120 0 : OUString sVersion = rConvertedAttribs[sVersionAttribute];
121 0 : if ( sVersion.getLength() )
122 : {
123 0 : aSequence[PKG_MNFST_VERSION].Name = sVersionProperty;
124 0 : aSequence[PKG_MNFST_VERSION].Value <<= sVersion;
125 : }
126 :
127 0 : OUString sSize = rConvertedAttribs[sSizeAttribute];
128 0 : if ( sSize.getLength() )
129 : {
130 0 : sal_Int64 nSize = sSize.toInt64();
131 0 : aSequence[PKG_MNFST_UCOMPSIZE].Name = sSizeProperty;
132 0 : aSequence[PKG_MNFST_UCOMPSIZE].Value <<= nSize;
133 0 : }
134 0 : }
135 :
136 0 : 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 0 : nDerivedKeySize = 0;
142 0 : OUString aString = rConvertedAttribs[sChecksumTypeAttribute];
143 0 : if ( !bIgnoreEncryptData )
144 : {
145 0 : if ( aString.equals( sSHA1_1k_Name ) || aString.equals( sSHA1_1k_URL ) )
146 : {
147 0 : aSequence[PKG_MNFST_DIGESTALG].Name = sDigestAlgProperty;
148 0 : aSequence[PKG_MNFST_DIGESTALG].Value <<= xml::crypto::DigestID::SHA1_1K;
149 : }
150 0 : else if ( aString.equals( sSHA256_1k_URL ) )
151 : {
152 0 : aSequence[PKG_MNFST_DIGESTALG].Name = sDigestAlgProperty;
153 0 : aSequence[PKG_MNFST_DIGESTALG].Value <<= xml::crypto::DigestID::SHA256_1K;
154 : }
155 : else
156 0 : bIgnoreEncryptData = sal_True;
157 :
158 0 : if ( !bIgnoreEncryptData )
159 : {
160 0 : aString = rConvertedAttribs[sChecksumAttribute];
161 0 : uno::Sequence < sal_Int8 > aDecodeBuffer;
162 0 : ::sax::Converter::decodeBase64(aDecodeBuffer, aString);
163 0 : aSequence[PKG_MNFST_DIGEST].Name = sDigestProperty;
164 0 : aSequence[PKG_MNFST_DIGEST].Value <<= aDecodeBuffer;
165 : }
166 0 : }
167 0 : }
168 :
169 0 : void ManifestImport::doAlgorithm(StringHashMap &rConvertedAttribs)
170 : throw( uno::RuntimeException )
171 : {
172 0 : if ( !bIgnoreEncryptData )
173 : {
174 0 : OUString aString = rConvertedAttribs[sAlgorithmNameAttribute];
175 0 : if ( aString.equals( sBlowfish_Name ) || aString.equals( sBlowfish_URL ) )
176 : {
177 0 : aSequence[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
178 0 : aSequence[PKG_MNFST_ENCALG].Value <<= xml::crypto::CipherID::BLOWFISH_CFB_8;
179 : }
180 0 : else if ( aString.equals( sAES256_URL ) )
181 : {
182 0 : aSequence[PKG_MNFST_ENCALG].Name = sEncryptionAlgProperty;
183 0 : aSequence[PKG_MNFST_ENCALG].Value <<= xml::crypto::CipherID::AES_CBC_W3C_PADDING;
184 : OSL_ENSURE( !nDerivedKeySize || nDerivedKeySize == 32, "Unexpected derived key length!" );
185 0 : 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 0 : if ( !bIgnoreEncryptData )
205 : {
206 0 : aString = rConvertedAttribs[sInitialisationVectorAttribute];
207 0 : uno::Sequence < sal_Int8 > aDecodeBuffer;
208 0 : ::sax::Converter::decodeBase64(aDecodeBuffer, aString);
209 0 : aSequence[PKG_MNFST_INIVECTOR].Name = sInitialisationVectorProperty;
210 0 : aSequence[PKG_MNFST_INIVECTOR].Value <<= aDecodeBuffer;
211 0 : }
212 : }
213 0 : }
214 :
215 0 : void ManifestImport::doKeyDerivation(StringHashMap &rConvertedAttribs)
216 : throw( uno::RuntimeException )
217 : {
218 0 : if ( !bIgnoreEncryptData )
219 : {
220 0 : OUString aString = rConvertedAttribs[sKeyDerivationNameAttribute];
221 0 : if ( aString.equals( sPBKDF2_Name ) || aString.equals( sPBKDF2_URL ) )
222 : {
223 0 : aString = rConvertedAttribs[sSaltAttribute];
224 0 : uno::Sequence < sal_Int8 > aDecodeBuffer;
225 0 : ::sax::Converter::decodeBase64(aDecodeBuffer, aString);
226 0 : aSequence[PKG_MNFST_SALT].Name = sSaltProperty;
227 0 : aSequence[PKG_MNFST_SALT].Value <<= aDecodeBuffer;
228 :
229 0 : aString = rConvertedAttribs[sIterationCountAttribute];
230 0 : aSequence[PKG_MNFST_ITERATION].Name = sIterationCountProperty;
231 0 : aSequence[PKG_MNFST_ITERATION].Value <<= aString.toInt32();
232 :
233 0 : aString = rConvertedAttribs[sKeySizeAttribute];
234 0 : if ( aString.getLength() )
235 : {
236 0 : sal_Int32 nKey = aString.toInt32();
237 : OSL_ENSURE( !nDerivedKeySize || nKey == nDerivedKeySize , "Provided derived key length differs from the expected one!" );
238 0 : nDerivedKeySize = nKey;
239 : }
240 0 : else if ( !nDerivedKeySize )
241 0 : nDerivedKeySize = 16;
242 0 : else if ( nDerivedKeySize != 16 )
243 : OSL_ENSURE( false, "Default derived key length differs from the expected one!" );
244 :
245 0 : aSequence[PKG_MNFST_DERKEYSIZE].Name = sDerivedKeySizeProperty;
246 0 : aSequence[PKG_MNFST_DERKEYSIZE].Value <<= nDerivedKeySize;
247 : }
248 : else
249 0 : bIgnoreEncryptData = sal_True;
250 : }
251 0 : }
252 :
253 0 : void ManifestImport::doStartKeyAlg(StringHashMap &rConvertedAttribs)
254 : throw( uno::RuntimeException )
255 : {
256 0 : OUString aString = rConvertedAttribs[sStartKeyAlgNameAttribute];
257 0 : if ( aString.equals( sSHA256_URL ) )
258 : {
259 0 : aSequence[PKG_MNFST_STARTALG].Name = sStartKeyAlgProperty;
260 0 : aSequence[PKG_MNFST_STARTALG].Value <<= xml::crypto::DigestID::SHA256;
261 : }
262 0 : else if ( aString.equals( sSHA1_Name ) || aString.equals( sSHA1_URL ) )
263 : {
264 0 : aSequence[PKG_MNFST_STARTALG].Name = sStartKeyAlgProperty;
265 0 : aSequence[PKG_MNFST_STARTALG].Value <<= xml::crypto::DigestID::SHA1;
266 : }
267 : else
268 0 : bIgnoreEncryptData = sal_True;
269 0 : }
270 :
271 0 : 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 0 : StringHashMap aConvertedAttribs;
275 0 : OUString aConvertedName = PushNameAndNamespaces( aName, xAttribs, aConvertedAttribs );
276 :
277 0 : size_t nLevel = aStack.size();
278 :
279 : assert(nLevel >= 1);
280 :
281 0 : switch (nLevel)
282 : {
283 : case 1:
284 : {
285 0 : if (!aConvertedName.equalsAscii(ELEMENT_MANIFEST)) //manifest:manifest
286 0 : aStack.back().m_bValid = false;
287 0 : break;
288 : }
289 : case 2:
290 : {
291 0 : if (aConvertedName == sFileEntryElement) //manifest:file-entry
292 0 : doFileEntry(aConvertedAttribs);
293 : else
294 0 : aStack.back().m_bValid = false;
295 0 : break;
296 : }
297 : case 3:
298 : {
299 0 : ManifestStack::reverse_iterator aIter = aStack.rbegin();
300 0 : ++aIter;
301 :
302 0 : if (!aIter->m_bValid)
303 0 : aStack.back().m_bValid = false;
304 0 : else if (aConvertedName.equals(sEncryptionDataElement)) //manifest:encryption-data
305 0 : doEncryptionData(aConvertedAttribs);
306 : else
307 0 : aStack.back().m_bValid = false;
308 0 : break;
309 : }
310 : case 4:
311 : {
312 0 : ManifestStack::reverse_iterator aIter = aStack.rbegin();
313 0 : ++aIter;
314 :
315 0 : if (!aIter->m_bValid)
316 0 : aStack.back().m_bValid = false;
317 0 : else if (aConvertedName.equals(sAlgorithmElement)) //manifest:algorithm,
318 0 : doAlgorithm(aConvertedAttribs);
319 0 : else if (aConvertedName.equals(sKeyDerivationElement)) //manifest:key-derivation,
320 0 : doKeyDerivation(aConvertedAttribs);
321 0 : else if (aConvertedName.equals(sStartKeyAlgElement)) //manifest:start-key-generation
322 0 : doStartKeyAlg(aConvertedAttribs);
323 : else
324 0 : aStack.back().m_bValid = false;
325 0 : break;
326 : }
327 : default:
328 0 : aStack.back().m_bValid = false;
329 0 : break;
330 0 : }
331 0 : }
332 :
333 : namespace
334 : {
335 0 : bool isEmpty(const com::sun::star::beans::PropertyValue &rProp)
336 : {
337 0 : return rProp.Name.isEmpty();
338 : }
339 : }
340 :
341 0 : void SAL_CALL ManifestImport::endElement( const OUString& aName )
342 : throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
343 : {
344 0 : OUString aConvertedName = ConvertName( aName );
345 0 : if ( !aStack.empty() && aStack.rbegin()->m_aConvertedName.equals( aConvertedName ) )
346 : {
347 0 : if ( aConvertedName.equals( sFileEntryElement ) && aStack.back().m_bValid )
348 : {
349 0 : com::sun::star::beans::PropertyValue aEmpty;
350 : aSequence.erase(std::remove_if(aSequence.begin(), aSequence.end(),
351 0 : isEmpty), aSequence.end());
352 :
353 0 : bIgnoreEncryptData = sal_False;
354 0 : rManVector.push_back ( aSequence.getAsConstList() );
355 :
356 0 : aSequence.clear();
357 : }
358 :
359 0 : aStack.pop_back();
360 0 : }
361 0 : }
362 :
363 0 : void SAL_CALL ManifestImport::characters( const OUString& /*aChars*/ )
364 : throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
365 : {
366 0 : }
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 0 : void SAL_CALL ManifestImport::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& /*xLocator*/ )
379 : throw( xml::sax::SAXException, uno::RuntimeException, std::exception )
380 : {
381 0 : }
382 :
383 0 : OUString ManifestImport::PushNameAndNamespaces( const OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs, StringHashMap& o_aConvertedAttribs )
384 : {
385 0 : StringHashMap aNamespaces;
386 0 : ::std::vector< ::std::pair< OUString, OUString > > aAttribsStrs;
387 :
388 0 : if ( xAttribs.is() )
389 : {
390 0 : sal_Int16 nAttrCount = xAttribs.is() ? xAttribs->getLength() : 0;
391 0 : aAttribsStrs.reserve( nAttrCount );
392 :
393 0 : for( sal_Int16 nInd = 0; nInd < nAttrCount; nInd++ )
394 : {
395 0 : OUString aAttrName = xAttribs->getNameByIndex( nInd );
396 0 : OUString aAttrValue = xAttribs->getValueByIndex( nInd );
397 0 : if ( aAttrName.getLength() >= 5
398 0 : && aAttrName.startsWith("xmlns")
399 0 : && ( aAttrName.getLength() == 5 || aAttrName[5] == ':' ) )
400 : {
401 : // this is a namespace declaration
402 0 : OUString aNsName( ( aAttrName.getLength() == 5 ) ? OUString() : aAttrName.copy( 6 ) );
403 0 : aNamespaces[aNsName] = aAttrValue;
404 : }
405 : else
406 : {
407 : // this is no namespace declaration
408 0 : aAttribsStrs.push_back( pair< OUString, OUString >( aAttrName, aAttrValue ) );
409 : }
410 0 : }
411 : }
412 :
413 0 : OUString aConvertedName = ConvertNameWithNamespace( aName, aNamespaces );
414 0 : if ( !aConvertedName.getLength() )
415 0 : aConvertedName = ConvertName( aName );
416 :
417 0 : aStack.push_back( ManifestScopeEntry( aConvertedName, aNamespaces ) );
418 :
419 0 : for ( sal_uInt16 nInd = 0; nInd < aAttribsStrs.size(); nInd++ )
420 : {
421 : // convert the attribute names on filling
422 0 : o_aConvertedAttribs[ConvertName( aAttribsStrs[nInd].first )] = aAttribsStrs[nInd].second;
423 : }
424 :
425 0 : return aConvertedName;
426 : }
427 :
428 0 : OUString ManifestImport::ConvertNameWithNamespace( const OUString& aName, const StringHashMap& aNamespaces )
429 : {
430 0 : OUString aNsAlias;
431 0 : OUString aPureName = aName;
432 :
433 0 : sal_Int32 nInd = aName.indexOf( ( sal_Unicode )':' );
434 0 : if ( nInd != -1 && nInd < aName.getLength() )
435 : {
436 0 : aNsAlias = aName.copy( 0, nInd );
437 0 : aPureName = aName.copy( nInd + 1 );
438 : }
439 :
440 0 : OUString aResult;
441 :
442 0 : StringHashMap::const_iterator aIter = aNamespaces.find( aNsAlias );
443 0 : if ( aIter != aNamespaces.end()
444 0 : && ( 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 0 : aResult = MANIFEST_NSPREFIX;
448 0 : aResult += aPureName;
449 : }
450 :
451 0 : return aResult;
452 : }
453 :
454 0 : OUString ManifestImport::ConvertName( const OUString& aName )
455 : {
456 0 : OUString aConvertedName;
457 0 : for ( ManifestStack::reverse_iterator aIter = aStack.rbegin(); !aConvertedName.getLength() && aIter != aStack.rend(); ++aIter )
458 : {
459 0 : if ( !aIter->m_aNamespaces.empty() )
460 0 : aConvertedName = ConvertNameWithNamespace( aName, aIter->m_aNamespaces );
461 : }
462 :
463 0 : if ( !aConvertedName.getLength() )
464 0 : aConvertedName = aName;
465 :
466 0 : return aConvertedName;
467 : }
468 :
469 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|