summaryrefslogtreecommitdiff
path: root/xmloff/source/draw/SignatureLineContext.cxx
blob: 445d9c73c2bc4a3e3aadbd90bb18f6398f5011f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "SignatureLineContext.hxx"

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/embed/XStorage.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/graphic/XGraphic.hpp>
#include <com/sun/star/security/DocumentDigitalSignatures.hpp>
#include <com/sun/star/security/XDocumentDigitalSignatures.hpp>
#include <com/sun/star/xml/sax/XAttributeList.hpp>

#include <comphelper/processfactory.hxx>
#include <comphelper/storagehelper.hxx>
#include <xmloff/xmltoken.hxx>
#include <xmloff/xmlimp.hxx>

using namespace css;
using namespace css::xml::sax;
using namespace css::uno;
using namespace css::drawing;
using namespace css::embed;
using namespace css::frame;
using namespace css::io;
using namespace css::graphic;
using namespace css::security;
using namespace xmloff::token;

SignatureLineContext::SignatureLineContext(SvXMLImport& rImport, sal_uInt16 nPrfx,
                                           const OUString& rLocalName,
                                           const Reference<XAttributeList>& xAttrList,
                                           const Reference<XShape>& rxShape)
    : SvXMLImportContext(rImport, nPrfx, rLocalName)
{
    Reference<beans::XPropertySet> xPropSet(rxShape, UNO_QUERY_THROW);

    xPropSet->setPropertyValue("IsSignatureLine", Any(true));

    xPropSet->setPropertyValue("SignatureLineId", Any(xAttrList->getValueByName("loext:id")));
    xPropSet->setPropertyValue("SignatureLineSuggestedSignerName",
                               Any(xAttrList->getValueByName("loext:suggested-signer-name")));
    xPropSet->setPropertyValue("SignatureLineSuggestedSignerTitle",
                               Any(xAttrList->getValueByName("loext:suggested-signer-title")));
    xPropSet->setPropertyValue("SignatureLineSuggestedSignerEmail",
                               Any(xAttrList->getValueByName("loext:suggested-signer-email")));
    xPropSet->setPropertyValue("SignatureLineSigningInstructions",
                               Any(xAttrList->getValueByName("loext:signing-instructions")));

    bool bShowSignDate = xAttrList->getValueByName("loext:show-sign-date") == GetXMLToken(XML_TRUE);
    bool bCanAddComment
        = xAttrList->getValueByName("loext:can-add-comment") == GetXMLToken(XML_TRUE);
    xPropSet->setPropertyValue("SignatureLineShowSignDate", Any(bShowSignDate));
    xPropSet->setPropertyValue("SignatureLineCanAddComment", Any(bCanAddComment));

    Reference<XGraphic> xGraphic;
    try
    {
        // Get the document signatures
        Reference<XDocumentDigitalSignatures> xSignatures(
            security::DocumentDigitalSignatures::createWithVersion(
                comphelper::getProcessComponentContext(), "1.2"));

        css::uno::Reference<XStorable> xStorable(GetImport().GetModel(), UNO_QUERY_THROW);
        Reference<XStorage> xStorage = comphelper::OStorageHelper::GetStorageOfFormatFromURL(
            ZIP_STORAGE_FORMAT_STRING, xStorable->getLocation(), ElementModes::READ);

        if (!xStorage.is())
        {
            SAL_WARN("xmloff", "No xStorage!");
            return;
        }

        Sequence<DocumentSignatureInformation> xSignatureInfo
            = xSignatures->verifyDocumentContentSignatures(xStorage, Reference<XInputStream>());

        for (int i = 0; i < xSignatureInfo.getLength(); i++)
        {
            // Try to find matching signature line image - if none exists that is fine,
            // then the signature line is not digitally signed.
            if (xSignatureInfo[i].SignatureLineId == xAttrList->getValueByName("loext:id"))
            {
                if (xSignatureInfo[i].SignatureIsValid)
                {
                    // Signature is valid, use the 'valid' image
                    SAL_WARN_IF(!xSignatureInfo[i].ValidSignatureLineImage.is(), "xmloff",
                                "No ValidSignatureLineImage!");
                    xGraphic = xSignatureInfo[i].ValidSignatureLineImage;
                }
                else
                {
                    // Signature is invalid, use the 'invalid' image
                    SAL_WARN_IF(!xSignatureInfo[i].InvalidSignatureLineImage.is(), "xmloff",
                                "No InvalidSignatureLineImage!");
                    xGraphic = xSignatureInfo[i].InvalidSignatureLineImage;
                }

                // Save unsigned graphic
                Reference<XGraphic> xUnsignedGraphic;
                xPropSet->getPropertyValue("Graphic") >>= xUnsignedGraphic;
                if (xUnsignedGraphic.is())
                    xPropSet->setPropertyValue("SignatureLineUnsignedImage", Any(xUnsignedGraphic));

                xPropSet->setPropertyValue("Graphic", Any(xGraphic));
                break;
            }
        }
    }
    catch (css::uno::Exception&)
    {
        // DocumentDigitalSignatures service not available.
        // We render the "unsigned" shape instead.
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */