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 :
21 : #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
22 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
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 : #include <com/sun/star/uno/RuntimeException.hpp>
28 :
29 : #include <ManifestDefines.hxx>
30 : #include <ManifestExport.hxx>
31 : #include <sax/tools/converter.hxx>
32 :
33 : #include <rtl/ustrbuf.hxx>
34 : #include <comphelper/documentconstants.hxx>
35 : #include <comphelper/attributelist.hxx>
36 :
37 : using namespace ::com::sun::star;
38 :
39 : using ::rtl::OUString;
40 : using ::rtl::OUStringBuffer;
41 :
42 8 : ManifestExport::ManifestExport( uno::Reference< xml::sax::XDocumentHandler > xHandler, const uno::Sequence< uno::Sequence < beans::PropertyValue > >& rManList )
43 : {
44 8 : const OUString sFileEntryElement ( ELEMENT_FILE_ENTRY );
45 8 : const OUString sManifestElement ( ELEMENT_MANIFEST );
46 8 : const OUString sEncryptionDataElement( ELEMENT_ENCRYPTION_DATA );
47 8 : const OUString sAlgorithmElement ( ELEMENT_ALGORITHM );
48 8 : const OUString sStartKeyGenerationElement ( ELEMENT_START_KEY_GENERATION );
49 8 : const OUString sKeyDerivationElement ( ELEMENT_KEY_DERIVATION );
50 :
51 8 : const OUString sCdataAttribute ( ATTRIBUTE_CDATA );
52 8 : const OUString sMediaTypeAttribute ( ATTRIBUTE_MEDIA_TYPE );
53 8 : const OUString sVersionAttribute ( ATTRIBUTE_VERSION );
54 8 : const OUString sFullPathAttribute ( ATTRIBUTE_FULL_PATH );
55 8 : const OUString sSizeAttribute ( ATTRIBUTE_SIZE );
56 8 : const OUString sKeySizeAttribute ( ATTRIBUTE_KEY_SIZE );
57 8 : const OUString sSaltAttribute ( ATTRIBUTE_SALT );
58 8 : const OUString sInitialisationVectorAttribute ( ATTRIBUTE_INITIALISATION_VECTOR );
59 8 : const OUString sIterationCountAttribute ( ATTRIBUTE_ITERATION_COUNT );
60 8 : const OUString sAlgorithmNameAttribute ( ATTRIBUTE_ALGORITHM_NAME );
61 8 : const OUString sStartKeyGenerationNameAttribute ( ATTRIBUTE_START_KEY_GENERATION_NAME );
62 8 : const OUString sKeyDerivationNameAttribute ( ATTRIBUTE_KEY_DERIVATION_NAME );
63 8 : const OUString sChecksumTypeAttribute ( ATTRIBUTE_CHECKSUM_TYPE );
64 8 : const OUString sChecksumAttribute ( ATTRIBUTE_CHECKSUM);
65 :
66 8 : const OUString sFullPathProperty ( "FullPath" );
67 8 : const OUString sVersionProperty ( "Version" );
68 8 : const OUString sMediaTypeProperty ( "MediaType" );
69 8 : const OUString sIterationCountProperty ( "IterationCount" );
70 8 : const OUString sDerivedKeySizeProperty ( "DerivedKeySize" );
71 8 : const OUString sSaltProperty ( "Salt" );
72 8 : const OUString sInitialisationVectorProperty( "InitialisationVector" );
73 8 : const OUString sSizeProperty ( "Size" );
74 8 : const OUString sDigestProperty ( "Digest" );
75 8 : const OUString sEncryptionAlgProperty ( "EncryptionAlgorithm" );
76 8 : const OUString sStartKeyAlgProperty ( "StartKeyAlgorithm" );
77 8 : const OUString sDigestAlgProperty ( "DigestAlgorithm" );
78 :
79 8 : const OUString sWhiteSpace ( " " );
80 :
81 8 : const OUString sSHA256_URL ( SHA256_URL );
82 8 : const OUString sSHA1_Name ( SHA1_NAME );
83 :
84 8 : const OUString sSHA1_1k_Name ( SHA1_1K_NAME );
85 8 : const OUString sSHA256_1k_URL ( SHA256_1K_URL );
86 :
87 8 : const OUString sBlowfish_Name ( BLOWFISH_NAME );
88 8 : const OUString sAES256_URL ( AES256_URL );
89 :
90 8 : const OUString sPBKDF2_Name ( PBKDF2_NAME );
91 :
92 8 : ::comphelper::AttributeList * pRootAttrList = new ::comphelper::AttributeList;
93 8 : const uno::Sequence < beans::PropertyValue > *pSequence = rManList.getConstArray();
94 8 : const sal_uInt32 nManLength = rManList.getLength();
95 :
96 : // find the mediatype of the document if any
97 8 : ::rtl::OUString aDocMediaType;
98 8 : ::rtl::OUString aDocVersion;
99 8 : for (sal_uInt32 nInd = 0; nInd < nManLength ; nInd++ )
100 : {
101 8 : ::rtl::OUString aMediaType;
102 8 : ::rtl::OUString aPath;
103 8 : ::rtl::OUString aVersion;
104 :
105 8 : const beans::PropertyValue *pValue = pSequence[nInd].getConstArray();
106 24 : for (sal_uInt32 j = 0, nNum = pSequence[nInd].getLength(); j < nNum; j++, pValue++)
107 : {
108 24 : if (pValue->Name.equals (sMediaTypeProperty) )
109 : {
110 8 : pValue->Value >>= aMediaType;
111 : }
112 16 : else if (pValue->Name.equals (sFullPathProperty) )
113 : {
114 8 : pValue->Value >>= aPath;
115 : }
116 8 : else if (pValue->Name.equals (sVersionProperty) )
117 : {
118 8 : pValue->Value >>= aVersion;
119 : }
120 :
121 24 : if ( !aPath.isEmpty() && !aMediaType.isEmpty() && !aVersion.isEmpty() )
122 8 : break;
123 : }
124 :
125 8 : if ( aPath == "/" )
126 : {
127 8 : aDocMediaType = aMediaType;
128 8 : aDocVersion = aVersion;
129 : break;
130 : }
131 8 : }
132 :
133 8 : sal_Bool bProvideDTD = sal_False;
134 8 : sal_Bool bAcceptNonemptyVersion = sal_False;
135 8 : sal_Bool bStoreStartKeyGeneration = sal_False;
136 8 : if ( !aDocMediaType.isEmpty() )
137 : {
138 42 : if ( aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII
139 6 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII
140 6 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII
141 6 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII
142 6 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII
143 6 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII
144 1 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII
145 1 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII
146 1 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII
147 1 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII
148 0 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII
149 0 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII
150 0 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII
151 0 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_CHART_TEMPLATE_ASCII
152 0 : || aDocMediaType == MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_TEMPLATE_ASCII )
153 :
154 : {
155 : // oasis format
156 : pRootAttrList->AddAttribute ( ATTRIBUTE_XMLNS,
157 : sCdataAttribute,
158 8 : MANIFEST_OASIS_NAMESPACE );
159 8 : bAcceptNonemptyVersion = sal_True;
160 8 : if ( aDocVersion.compareTo( ODFVER_012_TEXT ) >= 0 )
161 : {
162 : // this is ODF12 generation, let encrypted streams contain start-key-generation entry
163 8 : bStoreStartKeyGeneration = sal_True;
164 8 : pRootAttrList->AddAttribute ( sVersionAttribute, sCdataAttribute, aDocVersion );
165 : }
166 : }
167 : else
168 : {
169 : // even if it is no SO6 format the namespace must be specified
170 : // thus SO6 format is used as default one
171 : pRootAttrList->AddAttribute ( ATTRIBUTE_XMLNS,
172 : sCdataAttribute,
173 0 : MANIFEST_NAMESPACE );
174 :
175 0 : bProvideDTD = sal_True;
176 : }
177 : }
178 :
179 8 : uno::Reference < xml::sax::XAttributeList > xRootAttrList (pRootAttrList);
180 :
181 8 : xHandler->startDocument();
182 8 : uno::Reference < xml::sax::XExtendedDocumentHandler > xExtHandler ( xHandler, uno::UNO_QUERY );
183 8 : if ( xExtHandler.is() && bProvideDTD )
184 : {
185 0 : OUString aDocType ( MANIFEST_DOCTYPE );
186 0 : xExtHandler->unknown ( aDocType );
187 0 : xHandler->ignorableWhitespace ( sWhiteSpace );
188 : }
189 8 : xHandler->startElement( sManifestElement, xRootAttrList );
190 :
191 52 : for (sal_uInt32 i = 0 ; i < nManLength ; i++)
192 : {
193 44 : ::comphelper::AttributeList *pAttrList = new ::comphelper::AttributeList;
194 44 : const beans::PropertyValue *pValue = pSequence[i].getConstArray();
195 44 : ::rtl::OUString aString;
196 44 : const uno::Any *pVector = NULL, *pSalt = NULL, *pIterationCount = NULL, *pDigest = NULL, *pDigestAlg = NULL, *pEncryptAlg = NULL, *pStartKeyAlg = NULL, *pDerivedKeySize = NULL;
197 212 : for (sal_uInt32 j = 0, nNum = pSequence[i].getLength(); j < nNum; j++, pValue++)
198 : {
199 168 : if (pValue->Name.equals (sMediaTypeProperty) )
200 : {
201 44 : pValue->Value >>= aString;
202 44 : pAttrList->AddAttribute ( sMediaTypeAttribute, sCdataAttribute, aString );
203 : }
204 124 : else if (pValue->Name.equals (sVersionProperty) )
205 : {
206 44 : pValue->Value >>= aString;
207 : // the version is stored only if it is not empty
208 44 : if ( bAcceptNonemptyVersion && !aString.isEmpty() )
209 8 : pAttrList->AddAttribute ( sVersionAttribute, sCdataAttribute, aString );
210 : }
211 80 : else if (pValue->Name.equals (sFullPathProperty) )
212 : {
213 44 : pValue->Value >>= aString;
214 44 : pAttrList->AddAttribute ( sFullPathAttribute, sCdataAttribute, aString );
215 : }
216 36 : else if (pValue->Name.equals (sSizeProperty) )
217 : {
218 4 : sal_Int64 nSize = 0;
219 4 : pValue->Value >>= nSize;
220 4 : ::rtl::OUStringBuffer aBuffer;
221 4 : aBuffer.append ( nSize );
222 4 : pAttrList->AddAttribute ( sSizeAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
223 : }
224 32 : else if (pValue->Name.equals (sInitialisationVectorProperty) )
225 4 : pVector = &pValue->Value;
226 28 : else if (pValue->Name.equals (sSaltProperty) )
227 4 : pSalt = &pValue->Value;
228 24 : else if (pValue->Name.equals (sIterationCountProperty) )
229 4 : pIterationCount = &pValue->Value;
230 20 : else if (pValue->Name.equals ( sDigestProperty ) )
231 4 : pDigest = &pValue->Value;
232 16 : else if (pValue->Name.equals ( sDigestAlgProperty ) )
233 4 : pDigestAlg = &pValue->Value;
234 12 : else if (pValue->Name.equals ( sEncryptionAlgProperty ) )
235 4 : pEncryptAlg = &pValue->Value;
236 8 : else if (pValue->Name.equals ( sStartKeyAlgProperty ) )
237 4 : pStartKeyAlg = &pValue->Value;
238 4 : else if (pValue->Name.equals ( sDerivedKeySizeProperty ) )
239 4 : pDerivedKeySize = &pValue->Value;
240 : }
241 :
242 44 : xHandler->ignorableWhitespace ( sWhiteSpace );
243 44 : uno::Reference < xml::sax::XAttributeList > xAttrList ( pAttrList );
244 44 : xHandler->startElement( sFileEntryElement , xAttrList);
245 44 : if ( pVector && pSalt && pIterationCount && pDigest && pDigestAlg && pEncryptAlg && pStartKeyAlg && pDerivedKeySize )
246 : {
247 : // ==== Encryption Data
248 4 : ::comphelper::AttributeList * pNewAttrList = new ::comphelper::AttributeList;
249 4 : uno::Reference < xml::sax::XAttributeList > xNewAttrList (pNewAttrList);
250 4 : ::rtl::OUStringBuffer aBuffer;
251 4 : uno::Sequence < sal_Int8 > aSequence;
252 :
253 4 : xHandler->ignorableWhitespace ( sWhiteSpace );
254 :
255 : // ==== Digest
256 4 : ::rtl::OUString sChecksumType;
257 4 : sal_Int32 nDigestAlgID = 0;
258 4 : *pDigestAlg >>= nDigestAlgID;
259 4 : if ( nDigestAlgID == xml::crypto::DigestID::SHA256_1K )
260 4 : sChecksumType = sSHA256_1k_URL;
261 0 : else if ( nDigestAlgID == xml::crypto::DigestID::SHA1_1K )
262 0 : sChecksumType = sSHA1_1k_Name;
263 : else
264 0 : throw uno::RuntimeException( OSL_LOG_PREFIX "Unexpected digest algorithm is provided!", uno::Reference< uno::XInterface >() );
265 :
266 4 : pNewAttrList->AddAttribute ( sChecksumTypeAttribute, sCdataAttribute, sChecksumType );
267 4 : *pDigest >>= aSequence;
268 4 : ::sax::Converter::encodeBase64(aBuffer, aSequence);
269 4 : pNewAttrList->AddAttribute ( sChecksumAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
270 :
271 4 : xHandler->startElement( sEncryptionDataElement , xNewAttrList);
272 :
273 : // ==== Algorithm
274 4 : pNewAttrList = new ::comphelper::AttributeList;
275 4 : xNewAttrList = pNewAttrList;
276 :
277 4 : sal_Int32 nEncAlgID = 0;
278 4 : sal_Int32 nDerivedKeySize = 0;
279 4 : *pEncryptAlg >>= nEncAlgID;
280 4 : *pDerivedKeySize >>= nDerivedKeySize;
281 :
282 4 : ::rtl::OUString sEncAlgName;
283 4 : if ( nEncAlgID == xml::crypto::CipherID::AES_CBC_W3C_PADDING )
284 : {
285 : OSL_ENSURE( nDerivedKeySize, "Unexpected key size is provided!" );
286 4 : if ( nDerivedKeySize != 32 )
287 0 : throw uno::RuntimeException( OSL_LOG_PREFIX "Unexpected key size is provided!", uno::Reference< uno::XInterface >() );
288 :
289 4 : sEncAlgName = sAES256_URL;
290 : }
291 0 : else if ( nEncAlgID == xml::crypto::CipherID::BLOWFISH_CFB_8 )
292 : {
293 0 : sEncAlgName = sBlowfish_Name;
294 : }
295 : else
296 0 : throw uno::RuntimeException( OSL_LOG_PREFIX "Unexpected encryption algorithm is provided!", uno::Reference< uno::XInterface >() );
297 :
298 4 : pNewAttrList->AddAttribute ( sAlgorithmNameAttribute, sCdataAttribute, sEncAlgName );
299 :
300 4 : *pVector >>= aSequence;
301 4 : ::sax::Converter::encodeBase64(aBuffer, aSequence);
302 4 : pNewAttrList->AddAttribute ( sInitialisationVectorAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
303 :
304 4 : xHandler->ignorableWhitespace ( sWhiteSpace );
305 4 : xHandler->startElement( sAlgorithmElement , xNewAttrList);
306 4 : xHandler->ignorableWhitespace ( sWhiteSpace );
307 4 : xHandler->endElement( sAlgorithmElement );
308 :
309 : // ==== Key Derivation
310 4 : pNewAttrList = new ::comphelper::AttributeList;
311 4 : xNewAttrList = pNewAttrList;
312 :
313 4 : pNewAttrList->AddAttribute ( sKeyDerivationNameAttribute, sCdataAttribute, sPBKDF2_Name );
314 :
315 4 : if ( bStoreStartKeyGeneration )
316 : {
317 4 : aBuffer.append( nDerivedKeySize );
318 4 : pNewAttrList->AddAttribute ( sKeySizeAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
319 : }
320 :
321 4 : sal_Int32 nCount = 0;
322 4 : *pIterationCount >>= nCount;
323 4 : aBuffer.append (nCount);
324 4 : pNewAttrList->AddAttribute ( sIterationCountAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
325 :
326 4 : *pSalt >>= aSequence;
327 4 : ::sax::Converter::encodeBase64(aBuffer, aSequence);
328 4 : pNewAttrList->AddAttribute ( sSaltAttribute, sCdataAttribute, aBuffer.makeStringAndClear() );
329 :
330 4 : xHandler->ignorableWhitespace ( sWhiteSpace );
331 4 : xHandler->startElement( sKeyDerivationElement , xNewAttrList);
332 4 : xHandler->ignorableWhitespace ( sWhiteSpace );
333 4 : xHandler->endElement( sKeyDerivationElement );
334 :
335 : // we have to store start-key-generation element as the last one to workaround the parsing problem
336 : // in OOo3.1 and older versions
337 4 : if ( bStoreStartKeyGeneration )
338 : {
339 : // ==== Start Key Generation
340 4 : pNewAttrList = new ::comphelper::AttributeList;
341 4 : xNewAttrList = pNewAttrList;
342 :
343 4 : ::rtl::OUString sStartKeyAlg;
344 4 : ::rtl::OUString sStartKeySize;
345 4 : sal_Int32 nStartKeyAlgID = 0;
346 4 : *pStartKeyAlg >>= nStartKeyAlgID;
347 4 : if ( nStartKeyAlgID == xml::crypto::DigestID::SHA256 )
348 : {
349 4 : sStartKeyAlg = sSHA256_URL;
350 4 : aBuffer.append( (sal_Int32)32 );
351 4 : sStartKeySize = aBuffer.makeStringAndClear();
352 : }
353 0 : else if ( nStartKeyAlgID == xml::crypto::DigestID::SHA1 )
354 : {
355 0 : sStartKeyAlg = sSHA1_Name;
356 0 : aBuffer.append( (sal_Int32)20 );
357 0 : sStartKeySize = aBuffer.makeStringAndClear();
358 : }
359 : else
360 0 : throw uno::RuntimeException( OSL_LOG_PREFIX "Unexpected start key algorithm is provided!", uno::Reference< uno::XInterface >() );
361 :
362 4 : pNewAttrList->AddAttribute ( sStartKeyGenerationNameAttribute, sCdataAttribute, sStartKeyAlg );
363 4 : pNewAttrList->AddAttribute ( sKeySizeAttribute, sCdataAttribute, sStartKeySize );
364 :
365 4 : xHandler->ignorableWhitespace ( sWhiteSpace );
366 4 : xHandler->startElement( sStartKeyGenerationElement , xNewAttrList);
367 4 : xHandler->ignorableWhitespace ( sWhiteSpace );
368 4 : xHandler->endElement( sStartKeyGenerationElement );
369 : }
370 :
371 4 : xHandler->ignorableWhitespace ( sWhiteSpace );
372 4 : xHandler->endElement( sEncryptionDataElement );
373 : }
374 44 : xHandler->ignorableWhitespace ( sWhiteSpace );
375 44 : xHandler->endElement( sFileEntryElement );
376 44 : }
377 8 : xHandler->ignorableWhitespace ( sWhiteSpace );
378 8 : xHandler->endElement( sManifestElement );
379 8 : xHandler->endDocument();
380 8 : }
381 :
382 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|