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