Branch data 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 <xmlsecurity/documentsignaturehelper.hxx>
22 : :
23 : : #include <com/sun/star/container/XNameAccess.hpp>
24 : : #include <com/sun/star/lang/XComponent.hpp>
25 : : #include <com/sun/star/lang/DisposedException.hpp>
26 : : #include <com/sun/star/embed/XStorage.hpp>
27 : : #include <com/sun/star/embed/ElementModes.hpp>
28 : : #include "com/sun/star/beans/XPropertySet.hpp"
29 : :
30 : : #include "comphelper/documentconstants.hxx"
31 : : #include <tools/debug.hxx>
32 : : #include "rtl/uri.hxx"
33 : :
34 : : using namespace ::com::sun::star::uno;
35 : : namespace css = ::com::sun::star;
36 : : using rtl::OUString;
37 : :
38 : :
39 : : namespace
40 : : {
41 : 0 : ::rtl::OUString getElement(::rtl::OUString const & version, ::sal_Int32 * index)
42 : : {
43 [ # # ][ # # ]: 0 : while (*index < version.getLength() && version[*index] == '0') {
[ # # ]
44 : 0 : ++*index;
45 : : }
46 : 0 : return version.getToken(0, '.', *index);
47 : : }
48 : :
49 : :
50 : :
51 : : // Return 1 if version1 is greater then version 2, 0 if they are equal
52 : : //and -1 if version1 is less version 2
53 : 0 : int compareVersions(
54 : : ::rtl::OUString const & version1, ::rtl::OUString const & version2)
55 : : {
56 [ # # ][ # # ]: 0 : for (::sal_Int32 i1 = 0, i2 = 0; i1 >= 0 || i2 >= 0;) {
[ # # ]
57 : 0 : ::rtl::OUString e1(getElement(version1, &i1));
58 : 0 : ::rtl::OUString e2(getElement(version2, &i2));
59 [ # # ]: 0 : if (e1.getLength() < e2.getLength()) {
60 : 0 : return -1;
61 [ # # ]: 0 : } else if (e1.getLength() > e2.getLength()) {
62 : 0 : return 1;
63 [ # # ]: 0 : } else if (e1 < e2) {
64 : 0 : return -1;
65 [ # # ]: 0 : } else if (e1 > e2) {
66 : 0 : return 1;
67 : : }
68 [ # # ][ # # ]: 0 : }
69 : 0 : return 0;
70 : : }
71 : : }
72 : : //If the OOo 3.0 mode is used then we exclude
73 : : //'mimetype' and all content of 'META-INF'.
74 : : //If the argument 'bSigning' is true then the element list is created for a signing
75 : : //operation in which case we use the latest signing algorithm. That is all elements
76 : : //we find in the zip storage are added to the list. We do not support the old signatures
77 : : //which did not contain all files.
78 : : //If 'bSigning' is false, then we validate. If the user enabled validating according to OOo 3.0
79 : : //then mimetype and all content of META-INF must be excluded.
80 : 0 : void ImplFillElementList(
81 : : std::vector< rtl::OUString >& rList, const Reference < css::embed::XStorage >& rxStore,
82 : : const ::rtl::OUString rRootStorageName, const bool bRecursive,
83 : : const DocumentSignatureAlgorithm mode)
84 : : {
85 : 0 : ::rtl::OUString aMetaInfName( "META-INF" );
86 : 0 : ::rtl::OUString sMimeTypeName ("mimetype");
87 : 0 : ::rtl::OUString aSep( "/" );
88 : :
89 [ # # ]: 0 : Reference < css::container::XNameAccess > xElements( rxStore, UNO_QUERY );
90 [ # # ][ # # ]: 0 : Sequence< ::rtl::OUString > aElements = xElements->getElementNames();
91 : 0 : sal_Int32 nElements = aElements.getLength();
92 : 0 : const ::rtl::OUString* pNames = aElements.getConstArray();
93 : :
94 [ # # ]: 0 : for ( sal_Int32 n = 0; n < nElements; n++ )
95 : : {
96 [ # # # # : 0 : if (mode != OOo3_2Document
# # ][ # # ]
97 : 0 : && (pNames[n] == aMetaInfName
98 : 0 : || pNames[n] == sMimeTypeName))
99 : : {
100 : 0 : continue;
101 : : }
102 : : else
103 : : {
104 : : ::rtl::OUString sEncName = ::rtl::Uri::encode(
105 : 0 : pNames[n], rtl_UriCharClassRelSegment,
106 : 0 : rtl_UriEncodeStrict, RTL_TEXTENCODING_UTF8);
107 [ # # ][ # # ]: 0 : if (sEncName.isEmpty() && !pNames[n].isEmpty())
[ # # ]
108 : : throw css::uno::Exception(::rtl::OUString(
109 [ # # ][ # # ]: 0 : "Failed to encode element name of XStorage"), 0);
110 : :
111 [ # # ][ # # ]: 0 : if ( rxStore->isStreamElement( pNames[n] ) )
[ # # ]
112 : : {
113 : : //Exclude documentsignatures.xml!
114 [ # # ]: 0 : if (pNames[n].equals(
115 [ # # ]: 0 : DocumentSignatureHelper::GetDocumentContentSignatureDefaultStreamName()))
116 : 0 : continue;
117 : 0 : ::rtl::OUString aFullName( rRootStorageName + sEncName );
118 [ # # ]: 0 : rList.push_back(aFullName);
119 : : }
120 [ # # ][ # # ]: 0 : else if ( bRecursive && rxStore->isStorageElement( pNames[n] ) )
[ # # ][ # # ]
[ # # ]
121 : : {
122 [ # # ][ # # ]: 0 : Reference < css::embed::XStorage > xSubStore = rxStore->openStorageElement( pNames[n], css::embed::ElementModes::READ );
123 : 0 : rtl::OUString aFullRootName( rRootStorageName + sEncName + aSep );
124 [ # # ]: 0 : ImplFillElementList(rList, xSubStore, aFullRootName, bRecursive, mode);
125 [ # # ]: 0 : }
126 : : }
127 [ # # ]: 0 : }
128 : 0 : }
129 : :
130 : :
131 : 0 : bool DocumentSignatureHelper::isODFPre_1_2(const ::rtl::OUString & sVersion)
132 : : {
133 : : //The property version exists only if the document is at least version 1.2
134 : : //That is, if the document has version 1.1 and sVersion is empty.
135 : : //The constant is defined in comphelper/documentconstants.hxx
136 [ # # ]: 0 : if (compareVersions(sVersion, ODFVER_012_TEXT) == -1)
137 : 0 : return true;
138 : 0 : return false;
139 : : }
140 : :
141 : 0 : bool DocumentSignatureHelper::isOOo3_2_Signature(const SignatureInformation & sigInfo)
142 : : {
143 : 0 : ::rtl::OUString sManifestURI("META-INF/manifest.xml");
144 : 0 : bool bOOo3_2 = false;
145 : : typedef ::std::vector< SignatureReferenceInformation >::const_iterator CIT;
146 [ # # ][ # # ]: 0 : for (CIT i = sigInfo.vSignatureReferenceInfors.begin();
147 : 0 : i < sigInfo.vSignatureReferenceInfors.end(); ++i)
148 : : {
149 [ # # ]: 0 : if (i->ouURI.equals(sManifestURI))
150 : : {
151 : 0 : bOOo3_2 = true;
152 : 0 : break;
153 : : }
154 : : }
155 : 0 : return bOOo3_2;
156 : : }
157 : :
158 : : DocumentSignatureAlgorithm
159 : 0 : DocumentSignatureHelper::getDocumentAlgorithm(
160 : : const ::rtl::OUString & sODFVersion, const SignatureInformation & sigInfo)
161 : : {
162 : : OSL_ASSERT(!sODFVersion.isEmpty());
163 : 0 : DocumentSignatureAlgorithm mode = OOo3_2Document;
164 [ # # ]: 0 : if (!isOOo3_2_Signature(sigInfo))
165 : : {
166 [ # # ]: 0 : if (isODFPre_1_2(sODFVersion))
167 : 0 : mode = OOo2Document;
168 : : else
169 : 0 : mode = OOo3_0Document;
170 : : }
171 : 0 : return mode;
172 : : }
173 : :
174 : : //The function creates a list of files which are to be signed or for which
175 : : //the signature is to be validated. The strings are UTF8 encoded URIs which
176 : : //contain '/' as path separators.
177 : : //
178 : : //The algorithm how document signatures are created and validated has
179 : : //changed over time. The change affects only which files within the document
180 : : //are changed. Document signatures created by OOo 2.x only used particular files. Since
181 : : //OOo 3.0 everything except "mimetype" and "META-INF" are signed. As of OOo 3.2 everything
182 : : //except META-INF/documentsignatures.xml is signed.
183 : : //Signatures are validated according to the algorithm which was then used for validation.
184 : : //That is, when validating a signature which was created by OOo 3.0, then mimetype and
185 : : //META-INF are not used.
186 : : //
187 : : //When a signature is created then we always use the latest algorithm. That is, we use
188 : : //that of OOo 3.2
189 : : std::vector< rtl::OUString >
190 : 0 : DocumentSignatureHelper::CreateElementList(
191 : : const Reference < css::embed::XStorage >& rxStore,
192 : : const ::rtl::OUString /*rRootStorageName*/, DocumentSignatureMode eMode,
193 : : const DocumentSignatureAlgorithm mode)
194 : : {
195 [ # # ]: 0 : std::vector< rtl::OUString > aElements;
196 : 0 : ::rtl::OUString aSep( "/" );
197 : :
198 [ # # # # ]: 0 : switch ( eMode )
199 : : {
200 : : case SignatureModeDocumentContent:
201 : : {
202 [ # # ]: 0 : if (mode == OOo2Document) //that is, ODF 1.0, 1.1
203 : : {
204 : : // 1) Main content
205 [ # # ]: 0 : ImplFillElementList(aElements, rxStore, ::rtl::OUString(), false, mode);
206 : :
207 : : // 2) Pictures...
208 : 0 : rtl::OUString aSubStorageName( "Pictures" );
209 : : try
210 : : {
211 [ # # ][ # # ]: 0 : Reference < css::embed::XStorage > xSubStore = rxStore->openStorageElement( aSubStorageName, css::embed::ElementModes::READ );
212 [ # # ][ # # ]: 0 : ImplFillElementList(aElements, xSubStore, aSubStorageName+aSep, true, mode);
213 : : }
214 [ # # ]: 0 : catch(css::io::IOException& )
215 : : {
216 : : ; // Doesn't have to exist...
217 : : }
218 : : // 3) OLE....
219 : 0 : aSubStorageName = rtl::OUString("ObjectReplacements");
220 : : try
221 : : {
222 [ # # ][ # # ]: 0 : Reference < css::embed::XStorage > xSubStore = rxStore->openStorageElement( aSubStorageName, css::embed::ElementModes::READ );
223 [ # # ]: 0 : ImplFillElementList(aElements, xSubStore, aSubStorageName+aSep, true, mode);
224 : 0 : xSubStore.clear();
225 : :
226 : : // Object folders...
227 : 0 : rtl::OUString aMatchStr( "Object " );
228 [ # # ]: 0 : Reference < css::container::XNameAccess > xElements( rxStore, UNO_QUERY );
229 [ # # ][ # # ]: 0 : Sequence< ::rtl::OUString > aElementNames = xElements->getElementNames();
230 : 0 : sal_Int32 nElements = aElementNames.getLength();
231 : 0 : const ::rtl::OUString* pNames = aElementNames.getConstArray();
232 [ # # ]: 0 : for ( sal_Int32 n = 0; n < nElements; n++ )
233 : : {
234 [ # # ][ # # ]: 0 : if ( ( pNames[n].match( aMatchStr ) ) && rxStore->isStorageElement( pNames[n] ) )
[ # # ][ # # ]
[ # # ]
235 : : {
236 [ # # ][ # # ]: 0 : Reference < css::embed::XStorage > xTmpSubStore = rxStore->openStorageElement( pNames[n], css::embed::ElementModes::READ );
237 [ # # ]: 0 : ImplFillElementList(aElements, xTmpSubStore, pNames[n]+aSep, true, mode);
238 : : }
239 [ # # ][ # # ]: 0 : }
240 : : }
241 [ # # ]: 0 : catch( com::sun::star::io::IOException& )
242 : : {
243 : : ; // Doesn't have to exist...
244 : 0 : }
245 : : }
246 : : else
247 : : {
248 : : // Everything except META-INF
249 [ # # ]: 0 : ImplFillElementList(aElements, rxStore, ::rtl::OUString(), true, mode);
250 : : }
251 : : }
252 : 0 : break;
253 : : case SignatureModeMacros:
254 : : {
255 : : // 1) Macros
256 : 0 : rtl::OUString aSubStorageName( "Basic" );
257 : : try
258 : : {
259 [ # # ][ # # ]: 0 : Reference < css::embed::XStorage > xSubStore = rxStore->openStorageElement( aSubStorageName, css::embed::ElementModes::READ );
260 [ # # ][ # # ]: 0 : ImplFillElementList(aElements, xSubStore, aSubStorageName+aSep, true, mode);
261 : : }
262 [ # # ]: 0 : catch( com::sun::star::io::IOException& )
263 : : {
264 : : ; // Doesn't have to exist...
265 : : }
266 : :
267 : : // 2) Dialogs
268 : 0 : aSubStorageName = rtl::OUString("Dialogs") ;
269 : : try
270 : : {
271 [ # # ][ # # ]: 0 : Reference < css::embed::XStorage > xSubStore = rxStore->openStorageElement( aSubStorageName, css::embed::ElementModes::READ );
272 [ # # ][ # # ]: 0 : ImplFillElementList(aElements, xSubStore, aSubStorageName+aSep, true, mode);
273 : : }
274 [ # # ]: 0 : catch( com::sun::star::io::IOException& )
275 : : {
276 : : ; // Doesn't have to exist...
277 : : }
278 : : // 3) Scripts
279 : 0 : aSubStorageName = rtl::OUString("Scripts") ;
280 : : try
281 : : {
282 [ # # ][ # # ]: 0 : Reference < css::embed::XStorage > xSubStore = rxStore->openStorageElement( aSubStorageName, css::embed::ElementModes::READ );
283 [ # # ][ # # ]: 0 : ImplFillElementList(aElements, xSubStore, aSubStorageName+aSep, true, mode);
284 : : }
285 [ # # ]: 0 : catch( css::io::IOException& )
286 : : {
287 : : ; // Doesn't have to exist...
288 : 0 : }
289 : : }
290 : 0 : break;
291 : : case SignatureModePackage:
292 : : {
293 : : // Everything except META-INF
294 [ # # ]: 0 : ImplFillElementList(aElements, rxStore, ::rtl::OUString(), true, mode);
295 : : }
296 : 0 : break;
297 : : }
298 : :
299 : 0 : return aElements;
300 : : }
301 : :
302 : 108 : SignatureStreamHelper DocumentSignatureHelper::OpenSignatureStream(
303 : : const Reference < css::embed::XStorage >& rxStore, sal_Int32 nOpenMode, DocumentSignatureMode eDocSigMode )
304 : : {
305 : 108 : sal_Int32 nSubStorageOpenMode = css::embed::ElementModes::READ;
306 [ - + ]: 108 : if ( nOpenMode & css::embed::ElementModes::WRITE )
307 : 0 : nSubStorageOpenMode = css::embed::ElementModes::WRITE;
308 : :
309 : 108 : SignatureStreamHelper aHelper;
310 : :
311 : : try
312 : : {
313 : 108 : ::rtl::OUString aSIGStoreName( "META-INF" );
314 [ + - ][ + - ]: 108 : aHelper.xSignatureStorage = rxStore->openStorageElement( aSIGStoreName, nSubStorageOpenMode );
[ + - ]
315 [ + - ]: 108 : if ( aHelper.xSignatureStorage.is() )
316 : : {
317 : 108 : ::rtl::OUString aSIGStreamName;
318 [ + - ]: 108 : if ( eDocSigMode == SignatureModeDocumentContent )
319 [ + - ]: 108 : aSIGStreamName = DocumentSignatureHelper::GetDocumentContentSignatureDefaultStreamName();
320 [ # # ]: 0 : else if ( eDocSigMode == SignatureModeMacros )
321 [ # # ]: 0 : aSIGStreamName = DocumentSignatureHelper::GetScriptingContentSignatureDefaultStreamName();
322 : : else
323 [ # # ]: 0 : aSIGStreamName = DocumentSignatureHelper::GetPackageSignatureDefaultStreamName();
324 : :
325 [ + - ][ - + ]: 108 : aHelper.xSignatureStream = aHelper.xSignatureStorage->openStreamElement( aSIGStreamName, nOpenMode );
[ # # ]
326 [ - + ]: 108 : }
327 : : }
328 [ + - ]: 108 : catch(css::io::IOException& )
329 : : {
330 : : // Doesn't have to exist...
331 : : DBG_ASSERT( nOpenMode == css::embed::ElementModes::READ, "Error creating signature stream..." );
332 : : }
333 : :
334 : 108 : return aHelper;
335 : : }
336 : :
337 : : //sElementList contains all files which are expected to be signed. Only those files must me signed,
338 : : //no more, no less.
339 : : //The DocumentSignatureAlgorithm indicates if the document was created with OOo 2.x. Then
340 : : //the uri s in the Reference elements in the signature, were not properly encoded.
341 : : // For example: <Reference URI="ObjectReplacements/Object 1">
342 : 0 : bool DocumentSignatureHelper::checkIfAllFilesAreSigned(
343 : : const ::std::vector< ::rtl::OUString > & sElementList,
344 : : const SignatureInformation & sigInfo,
345 : : const DocumentSignatureAlgorithm alg)
346 : : {
347 : : // Can only be valid if ALL streams are signed, which means real stream count == signed stream count
348 : 0 : unsigned int nRealCount = 0;
349 [ # # ]: 0 : for ( int i = sigInfo.vSignatureReferenceInfors.size(); i; )
350 : : {
351 : 0 : const SignatureReferenceInformation& rInf = sigInfo.vSignatureReferenceInfors[--i];
352 : : // There is also an extra entry of type TYPE_SAMEDOCUMENT_REFERENCE because of signature date.
353 [ # # ][ # # ]: 0 : if ( ( rInf.nType == TYPE_BINARYSTREAM_REFERENCE ) || ( rInf.nType == TYPE_XMLSTREAM_REFERENCE ) )
354 : : {
355 : 0 : ::rtl::OUString sReferenceURI = rInf.ouURI;
356 [ # # ]: 0 : if (alg == OOo2Document)
357 : : {
358 : : //Comparing URIs is a difficult. Therefore we kind of normalize
359 : : //it before comparing. We assume that our URI do not have a leading "./"
360 : : //and fragments at the end (...#...)
361 : : sReferenceURI = ::rtl::Uri::encode(
362 : : sReferenceURI, rtl_UriCharClassPchar,
363 : 0 : rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8);
364 : : }
365 : :
366 : : //find the file in the element list
367 : : typedef ::std::vector< ::rtl::OUString >::const_iterator CIT;
368 [ # # ][ # # ]: 0 : for (CIT aIter = sElementList.begin(); aIter != sElementList.end(); ++aIter)
369 : : {
370 : 0 : ::rtl::OUString sElementListURI = *aIter;
371 [ # # ]: 0 : if (alg == OOo2Document)
372 : : {
373 : : sElementListURI =
374 : : ::rtl::Uri::encode(
375 : : sElementListURI, rtl_UriCharClassPchar,
376 : 0 : rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8);
377 : : }
378 [ # # ]: 0 : if (sElementListURI.equals(sReferenceURI))
379 : : {
380 : 0 : nRealCount++;
381 : : break;
382 : : }
383 [ # # ]: 0 : }
384 : : }
385 : : }
386 : 0 : return sElementList.size() == nRealCount;
387 : : }
388 : :
389 : : /*Compares the Uri which are obtained from CreateElementList with
390 : : the path obtained from the manifest.xml.
391 : : Returns true if both strings are equal.
392 : : */
393 : 0 : bool DocumentSignatureHelper::equalsReferenceUriManifestPath(
394 : : const OUString & rUri, const OUString & rPath)
395 : : {
396 : 0 : bool retVal = false;
397 : : //split up the uri and path into segments. Both are separated by '/'
398 [ # # ]: 0 : std::vector<OUString> vUriSegments;
399 : 0 : sal_Int32 nIndex = 0;
400 [ # # ]: 0 : do
401 : : {
402 : 0 : OUString aToken = rUri.getToken( 0, '/', nIndex );
403 [ # # ]: 0 : vUriSegments.push_back(aToken);
404 : : }
405 : : while (nIndex >= 0);
406 : :
407 [ # # ]: 0 : std::vector<OUString> vPathSegments;
408 : 0 : nIndex = 0;
409 [ # # ]: 0 : do
410 : : {
411 : 0 : OUString aToken = rPath.getToken( 0, '/', nIndex );
412 [ # # ]: 0 : vPathSegments.push_back(aToken);
413 : : }
414 : : while (nIndex >= 0);
415 : :
416 : : //Now compare each segment of the uri with its counterpart from the path
417 [ # # ]: 0 : if (vUriSegments.size() == vPathSegments.size())
418 : : {
419 : 0 : retVal = true;
420 : : typedef std::vector<OUString>::const_iterator CIT;
421 [ # # ][ # # ]: 0 : for (CIT i = vUriSegments.begin(), j = vPathSegments.begin();
[ # # ][ # # ]
422 : 0 : i != vUriSegments.end(); ++i, ++j)
423 : : {
424 : : //Decode the uri segment, so that %20 becomes ' ', etc.
425 : : OUString sDecUri = ::rtl::Uri::decode(
426 : 0 : *i, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8);
427 [ # # ]: 0 : if (!sDecUri.equals(*j))
428 : : {
429 : 0 : retVal = false;
430 : : break;
431 : : }
432 [ # # ]: 0 : }
433 : : }
434 : :
435 : 0 : return retVal;
436 : : }
437 : :
438 : 108 : ::rtl::OUString DocumentSignatureHelper::GetDocumentContentSignatureDefaultStreamName()
439 : : {
440 : 108 : return ::rtl::OUString( "documentsignatures.xml" );
441 : : }
442 : :
443 : 0 : ::rtl::OUString DocumentSignatureHelper::GetScriptingContentSignatureDefaultStreamName()
444 : : {
445 : 0 : return ::rtl::OUString( "macrosignatures.xml" );
446 : : }
447 : :
448 : 0 : ::rtl::OUString DocumentSignatureHelper::GetPackageSignatureDefaultStreamName()
449 : : {
450 : 0 : return ::rtl::OUString( "packagesignatures.xml" );
451 : : }
452 : :
453 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|