Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / draw / SignatureLineContext.cxx
blob0217156e35fbc525d9a52ad35647689425f303fc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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/.
8 */
10 #include "SignatureLineContext.hxx"
12 #include <com/sun/star/beans/XPropertySet.hpp>
13 #include <com/sun/star/embed/XStorage.hpp>
14 #include <com/sun/star/frame/XModel.hpp>
15 #include <com/sun/star/frame/XStorable.hpp>
16 #include <com/sun/star/graphic/XGraphic.hpp>
17 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
18 #include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
19 #include <com/sun/star/xml/sax/XAttributeList.hpp>
21 #include <sal/log.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <comphelper/storagehelper.hxx>
24 #include <xmloff/xmltoken.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/xmlnamespace.hxx>
28 using namespace css;
29 using namespace css::xml::sax;
30 using namespace css::uno;
31 using namespace css::drawing;
32 using namespace css::embed;
33 using namespace css::frame;
34 using namespace css::io;
35 using namespace css::graphic;
36 using namespace css::security;
37 using namespace xmloff::token;
39 SignatureLineContext::SignatureLineContext(SvXMLImport& rImport, sal_Int32 /*nElement*/,
40 const Reference<XFastAttributeList>& xAttrList,
41 const Reference<XShape>& rxShape)
42 : SvXMLImportContext(rImport)
44 Reference<beans::XPropertySet> xPropSet(rxShape, UNO_QUERY_THROW);
46 xPropSet->setPropertyValue("IsSignatureLine", Any(true));
48 xPropSet->setPropertyValue("SignatureLineId",
49 Any(xAttrList->getOptionalValue(XML_ELEMENT(LO_EXT, XML_ID))));
50 xPropSet->setPropertyValue(
51 "SignatureLineSuggestedSignerName",
52 Any(xAttrList->getOptionalValue(XML_ELEMENT(LO_EXT, XML_SUGGESTED_SIGNER_NAME))));
53 xPropSet->setPropertyValue(
54 "SignatureLineSuggestedSignerTitle",
55 Any(xAttrList->getOptionalValue(XML_ELEMENT(LO_EXT, XML_SUGGESTED_SIGNER_TITLE))));
56 xPropSet->setPropertyValue(
57 "SignatureLineSuggestedSignerEmail",
58 Any(xAttrList->getOptionalValue(XML_ELEMENT(LO_EXT, XML_SUGGESTED_SIGNER_EMAIL))));
59 xPropSet->setPropertyValue(
60 "SignatureLineSigningInstructions",
61 Any(xAttrList->getOptionalValue(XML_ELEMENT(LO_EXT, XML_SIGNING_INSTRUCTIONS))));
63 bool bShowSignDate = xAttrList->getOptionalValue(XML_ELEMENT(LO_EXT, XML_SHOW_SIGN_DATE))
64 == GetXMLToken(XML_TRUE);
65 bool bCanAddComment = xAttrList->getOptionalValue(XML_ELEMENT(LO_EXT, XML_CAN_ADD_COMMENT))
66 == GetXMLToken(XML_TRUE);
67 xPropSet->setPropertyValue("SignatureLineShowSignDate", Any(bShowSignDate));
68 xPropSet->setPropertyValue("SignatureLineCanAddComment", Any(bCanAddComment));
70 // Save unsigned graphic (need it when exporting)
71 Reference<XGraphic> xUnsignedGraphic;
72 xPropSet->getPropertyValue("Graphic") >>= xUnsignedGraphic;
73 if (xUnsignedGraphic.is())
74 xPropSet->setPropertyValue("SignatureLineUnsignedImage", Any(xUnsignedGraphic));
76 Reference<XGraphic> xGraphic;
77 try
79 // Get the document signatures
80 css::uno::Reference<XStorable> xStorable(GetImport().GetModel(), UNO_QUERY_THROW);
81 Reference<XStorage> xStorage = comphelper::OStorageHelper::GetStorageOfFormatFromURL(
82 ZIP_STORAGE_FORMAT_STRING, xStorable->getLocation(), ElementModes::READ);
84 if (!xStorage.is())
86 SAL_WARN("xmloff", "No xStorage!");
87 return;
90 OUString const aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(xStorage));
91 Reference<XDocumentDigitalSignatures> xSignatures(
92 security::DocumentDigitalSignatures::createWithVersion(
93 comphelper::getProcessComponentContext(), aODFVersion));
95 const Sequence<DocumentSignatureInformation> xSignatureInfo
96 = xSignatures->verifyDocumentContentSignatures(xStorage, Reference<XInputStream>());
98 // Try to find matching signature line image - if none exists that is fine,
99 // then the signature line is not digitally signed.
100 auto pSignatureInfo
101 = std::find_if(xSignatureInfo.begin(), xSignatureInfo.end(),
102 [&xAttrList](const DocumentSignatureInformation& rSignatureInfo) {
103 return rSignatureInfo.SignatureLineId
104 == xAttrList->getOptionalValue(XML_ELEMENT(LO_EXT, XML_ID));
106 bool bIsSigned(false);
107 if (pSignatureInfo != xSignatureInfo.end())
109 bIsSigned = true;
110 if (pSignatureInfo->SignatureIsValid)
112 // Signature is valid, use the 'valid' image
113 SAL_WARN_IF(!pSignatureInfo->ValidSignatureLineImage.is(), "xmloff",
114 "No ValidSignatureLineImage!");
115 xGraphic = pSignatureInfo->ValidSignatureLineImage;
117 else
119 // Signature is invalid, use the 'invalid' image
120 SAL_WARN_IF(!pSignatureInfo->InvalidSignatureLineImage.is(), "xmloff",
121 "No InvalidSignatureLineImage!");
122 xGraphic = pSignatureInfo->InvalidSignatureLineImage;
125 xPropSet->setPropertyValue("Graphic", Any(xGraphic));
127 xPropSet->setPropertyValue("SignatureLineIsSigned", Any(bIsSigned));
129 catch (css::uno::Exception&)
131 // DocumentDigitalSignatures service not available.
132 // We render the "unsigned" shape instead.
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */