1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
30 using namespace com::sun::star::uno
;
31 using namespace com::sun::star::beans
;
32 using namespace com::sun::star
;
35 ManifestImport::ManifestImport( vector
< Sequence
< PropertyValue
> > & rNewManVector
)
36 : bIgnoreEncryptData ( false )
37 , nDerivedKeySize( 0 )
38 , rManVector ( rNewManVector
)
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
)
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
)
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" )
77 , sSHA256_URL ( SHA256_URL
)
78 , sSHA1_Name ( SHA1_NAME
)
79 , sSHA1_URL ( SHA1_URL
)
81 , sSHA256_1k_URL ( SHA256_1K_URL
)
82 , sSHA1_1k_Name ( SHA1_1K_NAME
)
83 , sSHA1_1k_URL ( SHA1_1K_URL
)
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
)
91 , sPBKDF2_Name ( PBKDF2_NAME
)
92 , sPBKDF2_URL ( PBKDF2_URL
)
97 ManifestImport::~ManifestImport()
101 void SAL_CALL
ManifestImport::startDocument( )
102 throw( xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
106 void SAL_CALL
ManifestImport::endDocument( )
107 throw( xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
111 void ManifestImport::doFileEntry(StringHashMap
&rConvertedAttribs
)
112 throw( uno::RuntimeException
)
114 aSequence
.resize(PKG_SIZE_ENCR_MNFST
);
116 aSequence
[PKG_MNFST_FULLPATH
].Name
= sFullPathProperty
;
117 aSequence
[PKG_MNFST_FULLPATH
].Value
<<= rConvertedAttribs
[sFullPathAttribute
];
118 aSequence
[PKG_MNFST_MEDIATYPE
].Name
= sMediaTypeProperty
;
119 aSequence
[PKG_MNFST_MEDIATYPE
].Value
<<= rConvertedAttribs
[sMediaTypeAttribute
];
121 OUString sVersion
= rConvertedAttribs
[sVersionAttribute
];
122 if ( sVersion
.getLength() )
124 aSequence
[PKG_MNFST_VERSION
].Name
= sVersionProperty
;
125 aSequence
[PKG_MNFST_VERSION
].Value
<<= sVersion
;
128 OUString sSize
= rConvertedAttribs
[sSizeAttribute
];
129 if ( sSize
.getLength() )
131 sal_Int64 nSize
= sSize
.toInt64();
132 aSequence
[PKG_MNFST_UCOMPSIZE
].Name
= sSizeProperty
;
133 aSequence
[PKG_MNFST_UCOMPSIZE
].Value
<<= nSize
;
137 void ManifestImport::doEncryptionData(StringHashMap
&rConvertedAttribs
)
138 throw( uno::RuntimeException
)
140 // If this element exists, then this stream is encrypted and we need
141 // to import the initialisation vector, salt and iteration count used
143 OUString aString
= rConvertedAttribs
[sChecksumTypeAttribute
];
144 if ( !bIgnoreEncryptData
)
146 if ( aString
.equals( sSHA1_1k_Name
) || aString
.equals( sSHA1_1k_URL
) )
148 aSequence
[PKG_MNFST_DIGESTALG
].Name
= sDigestAlgProperty
;
149 aSequence
[PKG_MNFST_DIGESTALG
].Value
<<= xml::crypto::DigestID::SHA1_1K
;
151 else if ( aString
.equals( sSHA256_1k_URL
) )
153 aSequence
[PKG_MNFST_DIGESTALG
].Name
= sDigestAlgProperty
;
154 aSequence
[PKG_MNFST_DIGESTALG
].Value
<<= xml::crypto::DigestID::SHA256_1K
;
157 bIgnoreEncryptData
= true;
159 if ( !bIgnoreEncryptData
)
161 aString
= rConvertedAttribs
[sChecksumAttribute
];
162 uno::Sequence
< sal_Int8
> aDecodeBuffer
;
163 ::sax::Converter::decodeBase64(aDecodeBuffer
, aString
);
164 aSequence
[PKG_MNFST_DIGEST
].Name
= sDigestProperty
;
165 aSequence
[PKG_MNFST_DIGEST
].Value
<<= aDecodeBuffer
;
170 void ManifestImport::doAlgorithm(StringHashMap
&rConvertedAttribs
)
171 throw( uno::RuntimeException
)
173 if ( !bIgnoreEncryptData
)
175 OUString aString
= rConvertedAttribs
[sAlgorithmNameAttribute
];
176 if ( aString
.equals( sBlowfish_Name
) || aString
.equals( sBlowfish_URL
) )
178 aSequence
[PKG_MNFST_ENCALG
].Name
= sEncryptionAlgProperty
;
179 aSequence
[PKG_MNFST_ENCALG
].Value
<<= xml::crypto::CipherID::BLOWFISH_CFB_8
;
181 else if ( aString
.equals( sAES256_URL
) )
183 aSequence
[PKG_MNFST_ENCALG
].Name
= sEncryptionAlgProperty
;
184 aSequence
[PKG_MNFST_ENCALG
].Value
<<= xml::crypto::CipherID::AES_CBC_W3C_PADDING
;
185 OSL_ENSURE( !nDerivedKeySize
|| nDerivedKeySize
== 32, "Unexpected derived key length!" );
186 nDerivedKeySize
= 32;
188 else if ( aString
.equals( sAES192_URL
) )
190 aSequence
[PKG_MNFST_ENCALG
].Name
= sEncryptionAlgProperty
;
191 aSequence
[PKG_MNFST_ENCALG
].Value
<<= xml::crypto::CipherID::AES_CBC_W3C_PADDING
;
192 OSL_ENSURE( !nDerivedKeySize
|| nDerivedKeySize
== 24, "Unexpected derived key length!" );
193 nDerivedKeySize
= 24;
195 else if ( aString
.equals( sAES128_URL
) )
197 aSequence
[PKG_MNFST_ENCALG
].Name
= sEncryptionAlgProperty
;
198 aSequence
[PKG_MNFST_ENCALG
].Value
<<= xml::crypto::CipherID::AES_CBC_W3C_PADDING
;
199 OSL_ENSURE( !nDerivedKeySize
|| nDerivedKeySize
== 16, "Unexpected derived key length!" );
200 nDerivedKeySize
= 16;
203 bIgnoreEncryptData
= true;
205 if ( !bIgnoreEncryptData
)
207 aString
= rConvertedAttribs
[sInitialisationVectorAttribute
];
208 uno::Sequence
< sal_Int8
> aDecodeBuffer
;
209 ::sax::Converter::decodeBase64(aDecodeBuffer
, aString
);
210 aSequence
[PKG_MNFST_INIVECTOR
].Name
= sInitialisationVectorProperty
;
211 aSequence
[PKG_MNFST_INIVECTOR
].Value
<<= aDecodeBuffer
;
216 void ManifestImport::doKeyDerivation(StringHashMap
&rConvertedAttribs
)
217 throw( uno::RuntimeException
)
219 if ( !bIgnoreEncryptData
)
221 OUString aString
= rConvertedAttribs
[sKeyDerivationNameAttribute
];
222 if ( aString
.equals( sPBKDF2_Name
) || aString
.equals( sPBKDF2_URL
) )
224 aString
= rConvertedAttribs
[sSaltAttribute
];
225 uno::Sequence
< sal_Int8
> aDecodeBuffer
;
226 ::sax::Converter::decodeBase64(aDecodeBuffer
, aString
);
227 aSequence
[PKG_MNFST_SALT
].Name
= sSaltProperty
;
228 aSequence
[PKG_MNFST_SALT
].Value
<<= aDecodeBuffer
;
230 aString
= rConvertedAttribs
[sIterationCountAttribute
];
231 aSequence
[PKG_MNFST_ITERATION
].Name
= sIterationCountProperty
;
232 aSequence
[PKG_MNFST_ITERATION
].Value
<<= aString
.toInt32();
234 aString
= rConvertedAttribs
[sKeySizeAttribute
];
235 if ( aString
.getLength() )
237 sal_Int32 nKey
= aString
.toInt32();
238 OSL_ENSURE( !nDerivedKeySize
|| nKey
== nDerivedKeySize
, "Provided derived key length differs from the expected one!" );
239 nDerivedKeySize
= nKey
;
241 else if ( !nDerivedKeySize
)
242 nDerivedKeySize
= 16;
243 else if ( nDerivedKeySize
!= 16 )
244 OSL_ENSURE( false, "Default derived key length differs from the expected one!" );
246 aSequence
[PKG_MNFST_DERKEYSIZE
].Name
= sDerivedKeySizeProperty
;
247 aSequence
[PKG_MNFST_DERKEYSIZE
].Value
<<= nDerivedKeySize
;
250 bIgnoreEncryptData
= true;
254 void ManifestImport::doStartKeyAlg(StringHashMap
&rConvertedAttribs
)
255 throw( uno::RuntimeException
)
257 OUString aString
= rConvertedAttribs
[sStartKeyAlgNameAttribute
];
258 if ( aString
.equals( sSHA256_URL
) )
260 aSequence
[PKG_MNFST_STARTALG
].Name
= sStartKeyAlgProperty
;
261 aSequence
[PKG_MNFST_STARTALG
].Value
<<= xml::crypto::DigestID::SHA256
;
263 else if ( aString
.equals( sSHA1_Name
) || aString
.equals( sSHA1_URL
) )
265 aSequence
[PKG_MNFST_STARTALG
].Name
= sStartKeyAlgProperty
;
266 aSequence
[PKG_MNFST_STARTALG
].Value
<<= xml::crypto::DigestID::SHA1
;
269 bIgnoreEncryptData
= true;
272 void SAL_CALL
ManifestImport::startElement( const OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
)
273 throw( xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
275 StringHashMap aConvertedAttribs
;
276 OUString aConvertedName
= PushNameAndNamespaces( aName
, xAttribs
, aConvertedAttribs
);
278 size_t nLevel
= aStack
.size();
286 if (aConvertedName
!= ELEMENT_MANIFEST
) //manifest:manifest
287 aStack
.back().m_bValid
= false;
292 if (aConvertedName
== sFileEntryElement
) //manifest:file-entry
293 doFileEntry(aConvertedAttribs
);
295 aStack
.back().m_bValid
= false;
300 ManifestStack::reverse_iterator aIter
= aStack
.rbegin();
303 if (!aIter
->m_bValid
)
304 aStack
.back().m_bValid
= false;
305 else if (aConvertedName
.equals(sEncryptionDataElement
)) //manifest:encryption-data
306 doEncryptionData(aConvertedAttribs
);
308 aStack
.back().m_bValid
= false;
313 ManifestStack::reverse_iterator aIter
= aStack
.rbegin();
316 if (!aIter
->m_bValid
)
317 aStack
.back().m_bValid
= false;
318 else if (aConvertedName
.equals(sAlgorithmElement
)) //manifest:algorithm,
319 doAlgorithm(aConvertedAttribs
);
320 else if (aConvertedName
.equals(sKeyDerivationElement
)) //manifest:key-derivation,
321 doKeyDerivation(aConvertedAttribs
);
322 else if (aConvertedName
.equals(sStartKeyAlgElement
)) //manifest:start-key-generation
323 doStartKeyAlg(aConvertedAttribs
);
325 aStack
.back().m_bValid
= false;
329 aStack
.back().m_bValid
= false;
336 bool isEmpty(const com::sun::star::beans::PropertyValue
&rProp
)
338 return rProp
.Name
.isEmpty();
342 void SAL_CALL
ManifestImport::endElement( const OUString
& aName
)
343 throw( xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
345 OUString aConvertedName
= ConvertName( aName
);
346 if ( !aStack
.empty() && aStack
.rbegin()->m_aConvertedName
.equals( aConvertedName
) )
348 if ( aConvertedName
.equals( sFileEntryElement
) && aStack
.back().m_bValid
)
350 com::sun::star::beans::PropertyValue aEmpty
;
351 aSequence
.erase(std::remove_if(aSequence
.begin(), aSequence
.end(),
352 isEmpty
), aSequence
.end());
354 bIgnoreEncryptData
= false;
355 rManVector
.push_back ( comphelper::containerToSequence(aSequence
) );
364 void SAL_CALL
ManifestImport::characters( const OUString
& /*aChars*/ )
365 throw( xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
369 void SAL_CALL
ManifestImport::ignorableWhitespace( const OUString
& /*aWhitespaces*/ )
370 throw( xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
374 void SAL_CALL
ManifestImport::processingInstruction( const OUString
& /*aTarget*/, const OUString
& /*aData*/ )
375 throw( xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
379 void SAL_CALL
ManifestImport::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& /*xLocator*/ )
380 throw( xml::sax::SAXException
, uno::RuntimeException
, std::exception
)
384 OUString
ManifestImport::PushNameAndNamespaces( const OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
, StringHashMap
& o_aConvertedAttribs
)
386 StringHashMap aNamespaces
;
387 ::std::vector
< ::std::pair
< OUString
, OUString
> > aAttribsStrs
;
391 sal_Int16 nAttrCount
= xAttribs
.is() ? xAttribs
->getLength() : 0;
392 aAttribsStrs
.reserve( nAttrCount
);
394 for( sal_Int16 nInd
= 0; nInd
< nAttrCount
; nInd
++ )
396 OUString aAttrName
= xAttribs
->getNameByIndex( nInd
);
397 OUString aAttrValue
= xAttribs
->getValueByIndex( nInd
);
398 if ( aAttrName
.getLength() >= 5
399 && aAttrName
.startsWith("xmlns")
400 && ( aAttrName
.getLength() == 5 || aAttrName
[5] == ':' ) )
402 // this is a namespace declaration
403 OUString
aNsName( ( aAttrName
.getLength() == 5 ) ? OUString() : aAttrName
.copy( 6 ) );
404 aNamespaces
[aNsName
] = aAttrValue
;
408 // this is no namespace declaration
409 aAttribsStrs
.push_back( pair
< OUString
, OUString
>( aAttrName
, aAttrValue
) );
414 OUString aConvertedName
= ConvertNameWithNamespace( aName
, aNamespaces
);
415 if ( !aConvertedName
.getLength() )
416 aConvertedName
= ConvertName( aName
);
418 aStack
.push_back( ManifestScopeEntry( aConvertedName
, aNamespaces
) );
420 for ( sal_uInt16 nInd
= 0; nInd
< aAttribsStrs
.size(); nInd
++ )
422 // convert the attribute names on filling
423 o_aConvertedAttribs
[ConvertName( aAttribsStrs
[nInd
].first
)] = aAttribsStrs
[nInd
].second
;
426 return aConvertedName
;
429 OUString
ManifestImport::ConvertNameWithNamespace( const OUString
& aName
, const StringHashMap
& aNamespaces
)
432 OUString aPureName
= aName
;
434 sal_Int32 nInd
= aName
.indexOf( ( sal_Unicode
)':' );
435 if ( nInd
!= -1 && nInd
< aName
.getLength() )
437 aNsAlias
= aName
.copy( 0, nInd
);
438 aPureName
= aName
.copy( nInd
+ 1 );
443 StringHashMap::const_iterator aIter
= aNamespaces
.find( aNsAlias
);
444 if ( aIter
!= aNamespaces
.end()
445 && ( aIter
->second
== MANIFEST_NAMESPACE
|| aIter
->second
== MANIFEST_OASIS_NAMESPACE
) )
447 // no check for manifest.xml consistency currently since the old versions have supported inconsistent documents as well
448 aResult
= MANIFEST_NSPREFIX
;
449 aResult
+= aPureName
;
455 OUString
ManifestImport::ConvertName( const OUString
& aName
)
457 OUString aConvertedName
;
458 for ( ManifestStack::reverse_iterator aIter
= aStack
.rbegin(); !aConvertedName
.getLength() && aIter
!= aStack
.rend(); ++aIter
)
460 if ( !aIter
->m_aNamespaces
.empty() )
461 aConvertedName
= ConvertNameWithNamespace( aName
, aIter
->m_aNamespaces
);
464 if ( !aConvertedName
.getLength() )
465 aConvertedName
= aName
;
467 return aConvertedName
;
470 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */