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.
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
10 #include "SignatureLineContext.hxx"
11
12 #include <com/sun/star/beans/XPropertySet.hpp>
13 #include <com/sun/star/embed/XStorage.hpp>
14 #include <com/sun/star/frame/XStorable.hpp>
15 #include <com/sun/star/graphic/XGraphic.hpp>
16 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
17 #include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
18 #include <com/sun/star/xml/sax/XAttributeList.hpp>
19
20 #include <sal/log.hxx>
21 #include <comphelper/processfactory.hxx>
22 #include <comphelper/storagehelper.hxx>
23 #include <xmloff/xmltoken.hxx>
24 #include <xmloff/xmlimp.hxx>
25
26 using namespace css;
27 using namespace css::xml::sax;
28 using namespace css::uno;
29 using namespace css::drawing;
30 using namespace css::embed;
31 using namespace css::frame;
32 using namespace css::io;
33 using namespace css::graphic;
34 using namespace css::security;
35 using namespace xmloff::token;
36
SignatureLineContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLocalName,const Reference<XAttributeList> & xAttrList,const Reference<XShape> & rxShape)37 SignatureLineContext::SignatureLineContext(SvXMLImport& rImport, sal_uInt16 nPrfx,
38 const OUString& rLocalName,
39 const Reference<XAttributeList>& xAttrList,
40 const Reference<XShape>& rxShape)
41 : SvXMLImportContext(rImport, nPrfx, rLocalName)
42 {
43 Reference<beans::XPropertySet> xPropSet(rxShape, UNO_QUERY_THROW);
44
45 xPropSet->setPropertyValue("IsSignatureLine", Any(true));
46
47 xPropSet->setPropertyValue("SignatureLineId", Any(xAttrList->getValueByName("loext:id")));
48 xPropSet->setPropertyValue("SignatureLineSuggestedSignerName",
49 Any(xAttrList->getValueByName("loext:suggested-signer-name")));
50 xPropSet->setPropertyValue("SignatureLineSuggestedSignerTitle",
51 Any(xAttrList->getValueByName("loext:suggested-signer-title")));
52 xPropSet->setPropertyValue("SignatureLineSuggestedSignerEmail",
53 Any(xAttrList->getValueByName("loext:suggested-signer-email")));
54 xPropSet->setPropertyValue("SignatureLineSigningInstructions",
55 Any(xAttrList->getValueByName("loext:signing-instructions")));
56
57 bool bShowSignDate = xAttrList->getValueByName("loext:show-sign-date") == GetXMLToken(XML_TRUE);
58 bool bCanAddComment
59 = xAttrList->getValueByName("loext:can-add-comment") == GetXMLToken(XML_TRUE);
60 xPropSet->setPropertyValue("SignatureLineShowSignDate", Any(bShowSignDate));
61 xPropSet->setPropertyValue("SignatureLineCanAddComment", Any(bCanAddComment));
62
63 // Save unsigned graphic (need it when exporting)
64 Reference<XGraphic> xUnsignedGraphic;
65 xPropSet->getPropertyValue("Graphic") >>= xUnsignedGraphic;
66 if (xUnsignedGraphic.is())
67 xPropSet->setPropertyValue("SignatureLineUnsignedImage", Any(xUnsignedGraphic));
68
69 Reference<XGraphic> xGraphic;
70 bool bIsSigned(false);
71 try
72 {
73 // Get the document signatures
74 Reference<XDocumentDigitalSignatures> xSignatures(
75 security::DocumentDigitalSignatures::createWithVersion(
76 comphelper::getProcessComponentContext(), "1.2"));
77
78 css::uno::Reference<XStorable> xStorable(GetImport().GetModel(), UNO_QUERY_THROW);
79 Reference<XStorage> xStorage = comphelper::OStorageHelper::GetStorageOfFormatFromURL(
80 ZIP_STORAGE_FORMAT_STRING, xStorable->getLocation(), ElementModes::READ);
81
82 if (!xStorage.is())
83 {
84 SAL_WARN("xmloff", "No xStorage!");
85 return;
86 }
87
88 Sequence<DocumentSignatureInformation> xSignatureInfo
89 = xSignatures->verifyDocumentContentSignatures(xStorage, Reference<XInputStream>());
90
91 // Try to find matching signature line image - if none exists that is fine,
92 // then the signature line is not digitally signed.
93 auto pSignatureInfo = std::find_if(
94 xSignatureInfo.begin(), xSignatureInfo.end(),
95 [&xAttrList](const DocumentSignatureInformation& rSignatureInfo) {
96 return rSignatureInfo.SignatureLineId == xAttrList->getValueByName("loext:id");
97 });
98 if (pSignatureInfo != xSignatureInfo.end())
99 {
100 bIsSigned = true;
101 if (pSignatureInfo->SignatureIsValid)
102 {
103 // Signature is valid, use the 'valid' image
104 SAL_WARN_IF(!pSignatureInfo->ValidSignatureLineImage.is(), "xmloff",
105 "No ValidSignatureLineImage!");
106 xGraphic = pSignatureInfo->ValidSignatureLineImage;
107 }
108 else
109 {
110 // Signature is invalid, use the 'invalid' image
111 SAL_WARN_IF(!pSignatureInfo->InvalidSignatureLineImage.is(), "xmloff",
112 "No InvalidSignatureLineImage!");
113 xGraphic = pSignatureInfo->InvalidSignatureLineImage;
114 }
115
116 xPropSet->setPropertyValue("Graphic", Any(xGraphic));
117 }
118 xPropSet->setPropertyValue("SignatureLineIsSigned", Any(bIsSigned));
119 }
120 catch (css::uno::Exception&)
121 {
122 // DocumentDigitalSignatures service not available.
123 // We render the "unsigned" shape instead.
124 }
125 }
126
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
128