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 
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/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>
26 #include <xmloff/xmlnamespace.hxx>
27 
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;
38 
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  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: */
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:59
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 >())
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
#define SAL_WARN_IF(condition, area, stream)
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3452
Handling of tokens in XML:
SignatureLineContext(SvXMLImport &rImport, sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList, const css::uno::Reference< css::drawing::XShape > &rxShape)
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
#define ZIP_STORAGE_FORMAT_STRING
Reference< XComponentContext > getProcessComponentContext()
static OUString GetODFVersionFromStorage(const css::uno::Reference< css::embed::XStorage > &xStorage)
#define SAL_WARN(area, stream)