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