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 .
21 #include <documentsignaturehelper.hxx>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/io/IOException.hpp>
27 #include <com/sun/star/lang/XComponent.hpp>
28 #include <com/sun/star/lang/DisposedException.hpp>
29 #include <com/sun/star/embed/XStorage.hpp>
30 #include <com/sun/star/embed/StorageFormats.hpp>
31 #include <com/sun/star/embed/ElementModes.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/beans/StringPair.hpp>
35 #include <comphelper/documentconstants.hxx>
36 #include <comphelper/ofopxmlhelper.hxx>
37 #include <comphelper/processfactory.hxx>
38 #include <osl/diagnose.h>
39 #include <rtl/ref.hxx>
40 #include <rtl/uri.hxx>
41 #include <xmloff/attrlist.hxx>
43 #include <xsecctl.hxx>
45 using namespace ::com::sun::star
;
46 using namespace ::com::sun::star::uno
;
50 OUString
getElement(OUString
const & version
, ::sal_Int32
* index
)
52 while (*index
< version
.getLength() && version
[*index
] == '0') {
55 return version
.getToken(0, '.', *index
);
59 // Return 1 if version1 is greater then version 2, 0 if they are equal
60 //and -1 if version1 is less version 2
62 OUString
const & version1
, OUString
const & version2
)
64 for (::sal_Int32 i1
= 0, i2
= 0; i1
>= 0 || i2
>= 0;) {
65 OUString
e1(getElement(version1
, &i1
));
66 OUString
e2(getElement(version2
, &i2
));
67 if (e1
.getLength() < e2
.getLength()) {
69 } else if (e1
.getLength() > e2
.getLength()) {
81 void ImplFillElementList(
82 std::vector
< OUString
>& rList
, const Reference
< css::embed::XStorage
>& rxStore
,
83 const OUString
& rRootStorageName
, const bool bRecursive
,
84 const DocumentSignatureAlgorithm mode
)
86 Reference
< css::container::XNameAccess
> xElements( rxStore
, UNO_QUERY
);
87 Sequence
< OUString
> aElements
= xElements
->getElementNames();
88 sal_Int32 nElements
= aElements
.getLength();
89 const OUString
* pNames
= aElements
.getConstArray();
91 for ( sal_Int32 n
= 0; n
< nElements
; n
++ )
93 if (pNames
[n
] == "[Content_Types].xml")
97 // If the user enabled validating according to OOo 3.0
98 // then mimetype and all content of META-INF must be excluded.
99 if (mode
!= DocumentSignatureAlgorithm::OOo3_2
100 && (pNames
[n
] == "META-INF" || pNames
[n
] == "mimetype"))
106 OUString sEncName
= ::rtl::Uri::encode(
107 pNames
[n
], rtl_UriCharClassRelSegment
,
108 rtl_UriEncodeStrict
, RTL_TEXTENCODING_UTF8
);
109 if (sEncName
.isEmpty() && !pNames
[n
].isEmpty())
110 throw css::uno::RuntimeException("Failed to encode element name of XStorage", nullptr);
112 if ( rxStore
->isStreamElement( pNames
[n
] ) )
114 //Exclude documentsignatures.xml!
116 DocumentSignatureHelper::GetDocumentContentSignatureDefaultStreamName())
118 OUString
aFullName( rRootStorageName
+ sEncName
);
119 rList
.push_back(aFullName
);
121 else if ( bRecursive
&& rxStore
->isStorageElement( pNames
[n
] ) )
123 Reference
< css::embed::XStorage
> xSubStore
= rxStore
->openStorageElement( pNames
[n
], css::embed::ElementModes::READ
);
124 OUString
aFullRootName( rRootStorageName
+ sEncName
+ "/" );
125 ImplFillElementList(rList
, xSubStore
, aFullRootName
, bRecursive
, mode
);
132 bool DocumentSignatureHelper::isODFPre_1_2(const OUString
& sVersion
)
134 //The property version exists only if the document is at least version 1.2
135 //That is, if the document has version 1.1 and sVersion is empty.
136 //The constant is defined in comphelper/documentconstants.hxx
137 return compareVersions(sVersion
, ODFVER_012_TEXT
) == -1;
140 bool DocumentSignatureHelper::isOOo3_2_Signature(const SignatureInformation
& sigInfo
)
142 bool bOOo3_2
= false;
143 typedef ::std::vector
< SignatureReferenceInformation
>::const_iterator CIT
;
144 for (CIT i
= sigInfo
.vSignatureReferenceInfors
.begin();
145 i
< sigInfo
.vSignatureReferenceInfors
.end(); ++i
)
147 if (i
->ouURI
== "META-INF/manifest.xml")
156 DocumentSignatureAlgorithm
157 DocumentSignatureHelper::getDocumentAlgorithm(
158 const OUString
& sODFVersion
, const SignatureInformation
& sigInfo
)
160 OSL_ASSERT(!sODFVersion
.isEmpty());
161 DocumentSignatureAlgorithm mode
= DocumentSignatureAlgorithm::OOo3_2
;
162 if (!isOOo3_2_Signature(sigInfo
))
164 if (isODFPre_1_2(sODFVersion
))
165 mode
= DocumentSignatureAlgorithm::OOo2
;
167 mode
= DocumentSignatureAlgorithm::OOo3_0
;
172 //The function creates a list of files which are to be signed or for which
173 //the signature is to be validated. The strings are UTF8 encoded URIs which
174 //contain '/' as path separators.
176 //The algorithm how document signatures are created and validated has
177 //changed over time. The change affects only which files within the document
178 //are changed. Document signatures created by OOo 2.x only used particular files. Since
179 //OOo 3.0 everything except "mimetype" and "META-INF" are signed. As of OOo 3.2 everything
180 //except META-INF/documentsignatures.xml is signed.
181 //Signatures are validated according to the algorithm which was then used for validation.
182 //That is, when validating a signature which was created by OOo 3.0, then mimetype and
183 //META-INF are not used.
185 //When a signature is created then we always use the latest algorithm. That is, we use
187 std::vector
< OUString
>
188 DocumentSignatureHelper::CreateElementList(
189 const Reference
< css::embed::XStorage
>& rxStore
,
190 DocumentSignatureMode eMode
,
191 const DocumentSignatureAlgorithm mode
)
193 std::vector
< OUString
> aElements
;
194 OUString
aSep( "/" );
198 case DocumentSignatureMode::Content
:
200 if (mode
== DocumentSignatureAlgorithm::OOo2
) //that is, ODF 1.0, 1.1
203 ImplFillElementList(aElements
, rxStore
, OUString(), false, mode
);
206 OUString
aSubStorageName( "Pictures" );
209 Reference
< css::embed::XStorage
> xSubStore
= rxStore
->openStorageElement( aSubStorageName
, css::embed::ElementModes::READ
);
210 ImplFillElementList(aElements
, xSubStore
, aSubStorageName
+aSep
, true, mode
);
212 catch(css::io::IOException
& )
214 ; // Doesn't have to exist...
217 aSubStorageName
= "ObjectReplacements";
220 Reference
< css::embed::XStorage
> xSubStore
= rxStore
->openStorageElement( aSubStorageName
, css::embed::ElementModes::READ
);
221 ImplFillElementList(aElements
, xSubStore
, aSubStorageName
+aSep
, true, mode
);
225 Reference
< css::container::XNameAccess
> xElements( rxStore
, UNO_QUERY
);
226 Sequence
< OUString
> aElementNames
= xElements
->getElementNames();
227 sal_Int32 nElements
= aElementNames
.getLength();
228 const OUString
* pNames
= aElementNames
.getConstArray();
229 for ( sal_Int32 n
= 0; n
< nElements
; n
++ )
231 if ( ( pNames
[n
].match( "Object " ) ) && rxStore
->isStorageElement( pNames
[n
] ) )
233 Reference
< css::embed::XStorage
> xTmpSubStore
= rxStore
->openStorageElement( pNames
[n
], css::embed::ElementModes::READ
);
234 ImplFillElementList(aElements
, xTmpSubStore
, pNames
[n
]+aSep
, true, mode
);
238 catch( css::io::IOException
& )
240 ; // Doesn't have to exist...
245 // Everything except META-INF
246 ImplFillElementList(aElements
, rxStore
, OUString(), true, mode
);
250 case DocumentSignatureMode::Macros
:
253 OUString
aSubStorageName( "Basic" );
256 Reference
< css::embed::XStorage
> xSubStore
= rxStore
->openStorageElement( aSubStorageName
, css::embed::ElementModes::READ
);
257 ImplFillElementList(aElements
, xSubStore
, aSubStorageName
+aSep
, true, mode
);
259 catch( css::io::IOException
& )
261 ; // Doesn't have to exist...
265 aSubStorageName
= "Dialogs";
268 Reference
< css::embed::XStorage
> xSubStore
= rxStore
->openStorageElement( aSubStorageName
, css::embed::ElementModes::READ
);
269 ImplFillElementList(aElements
, xSubStore
, aSubStorageName
+aSep
, true, mode
);
271 catch( css::io::IOException
& )
273 ; // Doesn't have to exist...
276 aSubStorageName
= "Scripts";
279 Reference
< css::embed::XStorage
> xSubStore
= rxStore
->openStorageElement( aSubStorageName
, css::embed::ElementModes::READ
);
280 ImplFillElementList(aElements
, xSubStore
, aSubStorageName
+aSep
, true, mode
);
282 catch( css::io::IOException
& )
284 ; // Doesn't have to exist...
288 case DocumentSignatureMode::Package
:
290 // Everything except META-INF
291 ImplFillElementList(aElements
, rxStore
, OUString(), true, mode
);
299 void DocumentSignatureHelper::AppendContentTypes(const uno::Reference
<embed::XStorage
>& xStorage
, std::vector
<OUString
>& rElements
)
301 uno::Reference
<container::XNameAccess
> xNameAccess(xStorage
, uno::UNO_QUERY
);
302 if (!xNameAccess
.is() || !xNameAccess
->hasByName("[Content_Types].xml"))
306 uno::Reference
<io::XInputStream
> xRelStream(xStorage
->openStreamElement("[Content_Types].xml", embed::ElementModes::READ
), uno::UNO_QUERY
);
307 uno::Sequence
< uno::Sequence
<beans::StringPair
> > aContentTypeInfo
= comphelper::OFOPXMLHelper::ReadContentTypeSequence(xRelStream
, comphelper::getProcessComponentContext());
308 if (aContentTypeInfo
.getLength() < 2)
310 SAL_WARN("xmlsecurity.helper", "no defaults or overrides in aContentTypeInfo");
313 uno::Sequence
<beans::StringPair
>& rDefaults
= aContentTypeInfo
[0];
314 uno::Sequence
<beans::StringPair
>& rOverrides
= aContentTypeInfo
[1];
316 for (OUString
& rElement
: rElements
)
318 auto it
= std::find_if(rOverrides
.begin(), rOverrides
.end(), [&](const beans::StringPair
& rPair
)
320 return rPair
.First
== "/" + rElement
;
323 if (it
!= rOverrides
.end())
325 rElement
= "/" + rElement
+ "?ContentType=" + it
->Second
;
329 it
= std::find_if(rDefaults
.begin(), rDefaults
.end(), [&](const beans::StringPair
& rPair
)
331 return rElement
.endsWith("." + rPair
.First
);
334 if (it
!= rDefaults
.end())
336 rElement
= "/" + rElement
+ "?ContentType=" + it
->Second
;
339 SAL_WARN("xmlsecurity.helper", "found no content type for " << rElement
);
342 std::sort(rElements
.begin(), rElements
.end());
345 SignatureStreamHelper
DocumentSignatureHelper::OpenSignatureStream(
346 const Reference
< css::embed::XStorage
>& rxStore
, sal_Int32 nOpenMode
, DocumentSignatureMode eDocSigMode
)
348 sal_Int32 nSubStorageOpenMode
= css::embed::ElementModes::READ
;
349 if ( nOpenMode
& css::embed::ElementModes::WRITE
)
350 nSubStorageOpenMode
= css::embed::ElementModes::WRITE
;
352 SignatureStreamHelper aHelper
;
354 uno::Reference
<container::XNameAccess
> xNameAccess(rxStore
, uno::UNO_QUERY
);
355 if (!xNameAccess
.is())
358 if (xNameAccess
->hasByName("META-INF"))
362 aHelper
.xSignatureStorage
= rxStore
->openStorageElement( "META-INF", nSubStorageOpenMode
);
363 if ( aHelper
.xSignatureStorage
.is() )
365 OUString aSIGStreamName
;
366 if ( eDocSigMode
== DocumentSignatureMode::Content
)
367 aSIGStreamName
= DocumentSignatureHelper::GetDocumentContentSignatureDefaultStreamName();
368 else if ( eDocSigMode
== DocumentSignatureMode::Macros
)
369 aSIGStreamName
= DocumentSignatureHelper::GetScriptingContentSignatureDefaultStreamName();
371 aSIGStreamName
= DocumentSignatureHelper::GetPackageSignatureDefaultStreamName();
373 aHelper
.xSignatureStream
= aHelper
.xSignatureStorage
->openStreamElement( aSIGStreamName
, nOpenMode
);
376 catch(css::io::IOException
& )
378 // Doesn't have to exist...
379 SAL_WARN_IF( nOpenMode
!= css::embed::ElementModes::READ
, "xmlsecurity.helper", "Error creating signature stream..." );
382 else if(xNameAccess
->hasByName("[Content_Types].xml"))
386 if (xNameAccess
->hasByName("_xmlsignatures") && (nOpenMode
& embed::ElementModes::TRUNCATE
))
387 // Truncate, then all signatures will be written -> remove previous ones.
388 rxStore
->removeElement("_xmlsignatures");
390 aHelper
.xSignatureStorage
= rxStore
->openStorageElement("_xmlsignatures", nSubStorageOpenMode
);
391 aHelper
.nStorageFormat
= embed::StorageFormats::OFOPXML
;
393 catch (const io::IOException
& rException
)
395 SAL_WARN_IF(nOpenMode
!= css::embed::ElementModes::READ
, "xmlsecurity.helper", "DocumentSignatureHelper::OpenSignatureStream: " << rException
);
402 /** Check whether the current file can be signed with GPG (only ODF >= 1.2 can currently) */
403 bool DocumentSignatureHelper::CanSignWithGPG(
404 const Reference
< css::embed::XStorage
>& rxStore
,
405 const OUString
& sOdfVersion
)
407 uno::Reference
<container::XNameAccess
> xNameAccess(rxStore
, uno::UNO_QUERY
);
408 if (!xNameAccess
.is())
411 if (xNameAccess
->hasByName("META-INF")) // ODF
413 return !isODFPre_1_2(sOdfVersion
);
421 //sElementList contains all files which are expected to be signed. Only those files must me signed,
423 //The DocumentSignatureAlgorithm indicates if the document was created with OOo 2.x. Then
424 //the uri s in the Reference elements in the signature, were not properly encoded.
425 // For example: <Reference URI="ObjectReplacements/Object 1">
426 bool DocumentSignatureHelper::checkIfAllFilesAreSigned(
427 const ::std::vector
< OUString
> & sElementList
,
428 const SignatureInformation
& sigInfo
,
429 const DocumentSignatureAlgorithm alg
)
431 // Can only be valid if ALL streams are signed, which means real stream count == signed stream count
432 unsigned int nRealCount
= 0;
433 for ( int i
= sigInfo
.vSignatureReferenceInfors
.size(); i
; )
435 const SignatureReferenceInformation
& rInf
= sigInfo
.vSignatureReferenceInfors
[--i
];
436 // There is also an extra entry of type SignatureReferenceType::SAMEDOCUMENT because of signature date.
437 if ( ( rInf
.nType
== SignatureReferenceType::BINARYSTREAM
) || ( rInf
.nType
== SignatureReferenceType::XMLSTREAM
) )
439 OUString sReferenceURI
= rInf
.ouURI
;
440 if (alg
== DocumentSignatureAlgorithm::OOo2
)
442 //Comparing URIs is a difficult. Therefore we kind of normalize
443 //it before comparing. We assume that our URI do not have a leading "./"
444 //and fragments at the end (...#...)
445 sReferenceURI
= ::rtl::Uri::encode(
446 sReferenceURI
, rtl_UriCharClassPchar
,
447 rtl_UriEncodeCheckEscapes
, RTL_TEXTENCODING_UTF8
);
450 //find the file in the element list
451 typedef ::std::vector
< OUString
>::const_iterator CIT
;
452 for (CIT aIter
= sElementList
.begin(); aIter
!= sElementList
.end(); ++aIter
)
454 OUString sElementListURI
= *aIter
;
455 if (alg
== DocumentSignatureAlgorithm::OOo2
)
459 sElementListURI
, rtl_UriCharClassPchar
,
460 rtl_UriEncodeCheckEscapes
, RTL_TEXTENCODING_UTF8
);
462 if (sElementListURI
== sReferenceURI
)
470 return sElementList
.size() == nRealCount
;
473 /*Compares the Uri which are obtained from CreateElementList with
474 the path obtained from the manifest.xml.
475 Returns true if both strings are equal.
477 bool DocumentSignatureHelper::equalsReferenceUriManifestPath(
478 const OUString
& rUri
, const OUString
& rPath
)
481 //split up the uri and path into segments. Both are separated by '/'
482 std::vector
<OUString
> vUriSegments
;
483 sal_Int32 nIndex
= 0;
486 OUString aToken
= rUri
.getToken( 0, '/', nIndex
);
487 vUriSegments
.push_back(aToken
);
491 std::vector
<OUString
> vPathSegments
;
495 OUString aToken
= rPath
.getToken( 0, '/', nIndex
);
496 vPathSegments
.push_back(aToken
);
500 //Now compare each segment of the uri with its counterpart from the path
501 if (vUriSegments
.size() == vPathSegments
.size())
504 typedef std::vector
<OUString
>::const_iterator CIT
;
505 for (CIT i
= vUriSegments
.begin(), j
= vPathSegments
.begin();
506 i
!= vUriSegments
.end(); ++i
, ++j
)
508 //Decode the uri segment, so that %20 becomes ' ', etc.
509 OUString sDecUri
= ::rtl::Uri::decode(
510 *i
, rtl_UriDecodeWithCharset
, RTL_TEXTENCODING_UTF8
);
522 OUString
DocumentSignatureHelper::GetDocumentContentSignatureDefaultStreamName()
524 return OUString("documentsignatures.xml");
527 OUString
DocumentSignatureHelper::GetScriptingContentSignatureDefaultStreamName()
529 return OUString("macrosignatures.xml");
532 OUString
DocumentSignatureHelper::GetPackageSignatureDefaultStreamName()
534 return OUString("packagesignatures.xml");
537 void DocumentSignatureHelper::writeDigestMethod(
538 const uno::Reference
<xml::sax::XDocumentHandler
>& xDocumentHandler
)
540 rtl::Reference
<SvXMLAttributeList
> pAttributeList(new SvXMLAttributeList());
541 pAttributeList
->AddAttribute("Algorithm", ALGO_XMLDSIGSHA256
);
542 xDocumentHandler
->startElement("DigestMethod", uno::Reference
<xml::sax::XAttributeList
>(pAttributeList
.get()));
543 xDocumentHandler
->endElement("DigestMethod");
546 void DocumentSignatureHelper::writeSignedProperties(
547 const uno::Reference
<xml::sax::XDocumentHandler
>& xDocumentHandler
,
548 const SignatureInformation
& signatureInfo
,
549 const OUString
& sDate
)
552 rtl::Reference
<SvXMLAttributeList
> pAttributeList(new SvXMLAttributeList());
553 pAttributeList
->AddAttribute("Id", "idSignedProperties");
554 xDocumentHandler
->startElement("xd:SignedProperties", uno::Reference
<xml::sax::XAttributeList
>(pAttributeList
.get()));
557 xDocumentHandler
->startElement("xd:SignedSignatureProperties", uno::Reference
<xml::sax::XAttributeList
>(new SvXMLAttributeList()));
558 xDocumentHandler
->startElement("xd:SigningTime", uno::Reference
<xml::sax::XAttributeList
>(new SvXMLAttributeList()));
559 xDocumentHandler
->characters(sDate
);
560 xDocumentHandler
->endElement("xd:SigningTime");
561 xDocumentHandler
->startElement("xd:SigningCertificate", uno::Reference
<xml::sax::XAttributeList
>(new SvXMLAttributeList()));
562 xDocumentHandler
->startElement("xd:Cert", uno::Reference
<xml::sax::XAttributeList
>(new SvXMLAttributeList()));
563 xDocumentHandler
->startElement("xd:CertDigest", uno::Reference
<xml::sax::XAttributeList
>(new SvXMLAttributeList()));
564 writeDigestMethod(xDocumentHandler
);
566 xDocumentHandler
->startElement("DigestValue", uno::Reference
<xml::sax::XAttributeList
>(new SvXMLAttributeList()));
567 // TODO: this is empty for gpg signatures currently
568 //assert(!signatureInfo.ouCertDigest.isEmpty());
569 xDocumentHandler
->characters(signatureInfo
.ouCertDigest
);
570 xDocumentHandler
->endElement("DigestValue");
572 xDocumentHandler
->endElement("xd:CertDigest");
573 xDocumentHandler
->startElement("xd:IssuerSerial", uno::Reference
<xml::sax::XAttributeList
>(new SvXMLAttributeList()));
574 xDocumentHandler
->startElement("X509IssuerName", uno::Reference
<xml::sax::XAttributeList
>(new SvXMLAttributeList()));
575 xDocumentHandler
->characters(signatureInfo
.ouX509IssuerName
);
576 xDocumentHandler
->endElement("X509IssuerName");
577 xDocumentHandler
->startElement("X509SerialNumber", uno::Reference
<xml::sax::XAttributeList
>(new SvXMLAttributeList()));
578 xDocumentHandler
->characters(signatureInfo
.ouX509SerialNumber
);
579 xDocumentHandler
->endElement("X509SerialNumber");
580 xDocumentHandler
->endElement("xd:IssuerSerial");
581 xDocumentHandler
->endElement("xd:Cert");
582 xDocumentHandler
->endElement("xd:SigningCertificate");
583 xDocumentHandler
->startElement("xd:SignaturePolicyIdentifier", uno::Reference
<xml::sax::XAttributeList
>(new SvXMLAttributeList()));
584 xDocumentHandler
->startElement("xd:SignaturePolicyImplied", uno::Reference
<xml::sax::XAttributeList
>(new SvXMLAttributeList()));
585 xDocumentHandler
->endElement("xd:SignaturePolicyImplied");
586 xDocumentHandler
->endElement("xd:SignaturePolicyIdentifier");
587 xDocumentHandler
->endElement("xd:SignedSignatureProperties");
589 xDocumentHandler
->endElement("xd:SignedProperties");
592 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */