LibreOffice Module xmloff (master) 1
SignatureLineContext.cxx
Go to the documentation of this file.
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
11
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>
20
21#include <sal/log.hxx>
24#include <xmloff/xmltoken.hxx>
25#include <xmloff/xmlimp.hxx>
27
28using namespace css;
29using namespace css::xml::sax;
30using namespace css::uno;
31using namespace css::drawing;
32using namespace css::embed;
33using namespace css::frame;
34using namespace css::io;
35using namespace css::graphic;
36using namespace css::security;
37using namespace xmloff::token;
38
39SignatureLineContext::SignatureLineContext(SvXMLImport& rImport, sal_Int32 /*nElement*/,
40 const Reference<XFastAttributeList>& xAttrList,
41 const Reference<XShape>& rxShape)
42 : SvXMLImportContext(rImport)
43{
44 Reference<beans::XPropertySet> xPropSet(rxShape, UNO_QUERY_THROW);
45
46 xPropSet->setPropertyValue("IsSignatureLine", Any(true));
47
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))));
62
63 bool bShowSignDate = xAttrList->getOptionalValue(XML_ELEMENT(LO_EXT, XML_SHOW_SIGN_DATE))
65 bool bCanAddComment = xAttrList->getOptionalValue(XML_ELEMENT(LO_EXT, XML_CAN_ADD_COMMENT))
67 xPropSet->setPropertyValue("SignatureLineShowSignDate", Any(bShowSignDate));
68 xPropSet->setPropertyValue("SignatureLineCanAddComment", Any(bCanAddComment));
69
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));
75
76 Reference<XGraphic> xGraphic;
77 try
78 {
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);
83
84 if (!xStorage.is())
85 {
86 SAL_WARN("xmloff", "No xStorage!");
87 return;
88 }
89
90 OUString const aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(xStorage));
91 Reference<XDocumentDigitalSignatures> xSignatures(
92 security::DocumentDigitalSignatures::createWithVersion(
94
95 const Sequence<DocumentSignatureInformation> xSignatureInfo
96 = xSignatures->verifyDocumentContentSignatures(xStorage, Reference<XInputStream>());
97
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));
105 });
106 bool bIsSigned(false);
107 if (pSignatureInfo != xSignatureInfo.end())
108 {
109 bIsSigned = true;
110 if (pSignatureInfo->SignatureIsValid)
111 {
112 // Signature is valid, use the 'valid' image
113 SAL_WARN_IF(!pSignatureInfo->ValidSignatureLineImage.is(), "xmloff",
114 "No ValidSignatureLineImage!");
115 xGraphic = pSignatureInfo->ValidSignatureLineImage;
116 }
117 else
118 {
119 // Signature is invalid, use the 'invalid' image
120 SAL_WARN_IF(!pSignatureInfo->InvalidSignatureLineImage.is(), "xmloff",
121 "No InvalidSignatureLineImage!");
122 xGraphic = pSignatureInfo->InvalidSignatureLineImage;
123 }
124
125 xPropSet->setPropertyValue("Graphic", Any(xGraphic));
126 }
127 xPropSet->setPropertyValue("SignatureLineIsSigned", Any(bIsSigned));
128 }
129 catch (css::uno::Exception&)
130 {
131 // DocumentDigitalSignatures service not available.
132 // We render the "unsigned" shape instead.
133 }
134}
135
136/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
SignatureLineContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, const css::uno::Reference< css::drawing::XShape > &rxShape)
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
static OUString GetODFVersionFromStorage(const css::uno::Reference< css::embed::XStorage > &xStorage)
static css::uno::Reference< css::embed::XStorage > GetStorageOfFormatFromURL(const OUString &aFormat, const OUString &aURL, sal_Int32 nStorageMode, const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
Reference< XComponentContext > getProcessComponentContext()
Handling of tokens in XML:
@ XML_SUGGESTED_SIGNER_TITLE
Definition: xmltoken.hxx:2047
@ XML_SUGGESTED_SIGNER_EMAIL
Definition: xmltoken.hxx:2045
@ XML_SIGNING_INSTRUCTIONS
Definition: xmltoken.hxx:2042
@ XML_SUGGESTED_SIGNER_NAME
Definition: xmltoken.hxx:2046
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3527
constexpr OUStringLiteral ZIP_STORAGE_FORMAT_STRING
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:98