summaryrefslogtreecommitdiffstats
path: root/xmlsecurity/source/helper/ooxmlsecexporter.cxx
blob: 2294af680010d3fc9ff00361ed9716c7e9d65d09 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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 "ooxmlsecexporter.hxx"

#include <algorithm>
#include <memory>
#include <string_view>

#include <com/sun/star/embed/ElementModes.hpp>
#include <com/sun/star/embed/XHierarchicalStorageAccess.hpp>
#include <com/sun/star/embed/XStorage.hpp>
#include <com/sun/star/beans/StringPair.hpp>
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>

#include <comphelper/ofopxmlhelper.hxx>
#include <o3tl/string_view.hxx>
#include <rtl/ref.hxx>
#include <sal/log.hxx>
#include <svx/xoutbmp.hxx>
#include <unotools/datetime.hxx>
#include <vcl/salctype.hxx>
#include <xmloff/attrlist.hxx>

#include <documentsignaturehelper.hxx>
#include <xsecctl.hxx>

using namespace com::sun::star;
using namespace css::xml::sax;

struct OOXMLSecExporter::Impl
{
private:
    const uno::Reference<uno::XComponentContext>& m_xComponentContext;
    const uno::Reference<embed::XStorage>& m_xRootStorage;
    const uno::Reference<xml::sax::XDocumentHandler>& m_xDocumentHandler;
    const SignatureInformation& m_rInformation;
    OUString m_aSignatureTimeValue;

public:
    Impl(const uno::Reference<uno::XComponentContext>& xComponentContext,
         const uno::Reference<embed::XStorage>& xRootStorage,
         const uno::Reference<xml::sax::XDocumentHandler>& xDocumentHandler,
         const SignatureInformation& rInformation)
        : m_xComponentContext(xComponentContext)
        , m_xRootStorage(xRootStorage)
        , m_xDocumentHandler(xDocumentHandler)
        , m_rInformation(rInformation)
    {
    }

    /// Should we intentionally not sign this stream?
    static bool isOOXMLDenylist(std::u16string_view rStreamName);
    /// Should we intentionally not sign this relation type?
    static bool isOOXMLRelationDenylist(const OUString& rRelationName);

    const uno::Reference<xml::sax::XDocumentHandler>& getDocumentHandler() const
    {
        return m_xDocumentHandler;
    }

    void writeSignature();
    void writeSignedInfo();
    void writeCanonicalizationMethod();
    void writeCanonicalizationTransform();
    void writeSignatureMethod();
    void writeSignedInfoReferences();
    void writeSignatureValue();
    void writeKeyInfo();
    void writePackageObject();
    void writeManifest();
    void writeRelationshipTransform(const OUString& rURI);
    /// Writes <SignatureProperties> inside idPackageObject.
    void writePackageObjectSignatureProperties();
    /// Writes a single <Reference> inside <Manifest>.
    void writeManifestReference(const SignatureReferenceInformation& rReference);
    void writeOfficeObject();
    /// Writes <SignatureInfoV1>.
    void writeSignatureInfo();
    void writePackageSignature();
    void writeSignatureLineImages();
};

bool OOXMLSecExporter::Impl::isOOXMLDenylist(std::u16string_view rStreamName)
{
    static const std::initializer_list<std::u16string_view> vDenylist
        = { u"/%5BContent_Types%5D.xml", u"/docProps/app.xml", u"/docProps/core.xml",
            // Don't attempt to sign other signatures for now.
            u"/_xmlsignatures" };
    // Just check the prefix, as we don't care about the content type part of the stream name.
    return std::any_of(vDenylist.begin(), vDenylist.end(),
                       [&](const std::u16string_view& rLiteral) {
                           return o3tl::starts_with(rStreamName, rLiteral);
                       });
}

bool OOXMLSecExporter::Impl::isOOXMLRelationDenylist(const OUString& rRelationName)
{
    static const std::initializer_list<std::u16string_view> vDenylist = {
        u"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties",
        u"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties",
        u"http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/origin"
    };
    return std::find(vDenylist.begin(), vDenylist.end(), rRelationName) != vDenylist.end();
}

void OOXMLSecExporter::Impl::writeSignedInfo()
{
    m_xDocumentHandler->startElement(
        "SignedInfo", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));

    writeCanonicalizationMethod();
    writeSignatureMethod();
    writeSignedInfoReferences();

    m_xDocumentHandler->endElement("SignedInfo");
}

void OOXMLSecExporter::Impl::writeCanonicalizationMethod()
{
    rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
    pAttributeList->AddAttribute("Algorithm", ALGO_C14N);
    m_xDocumentHandler->startElement("CanonicalizationMethod",
                                     uno::Reference<xml::sax::XAttributeList>(pAttributeList));
    m_xDocumentHandler->endElement("CanonicalizationMethod");
}

void OOXMLSecExporter::Impl::writeCanonicalizationTransform()
{
    rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
    pAttributeList->AddAttribute("Algorithm", ALGO_C14N);
    m_xDocumentHandler->startElement("Transform",
                                     uno::Reference<xml::sax::XAttributeList>(pAttributeList));
    m_xDocumentHandler->endElement("Transform");
}

void OOXMLSecExporter::Impl::writeSignatureMethod()
{
    rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());

    if (m_rInformation.eAlgorithmID == svl::crypto::SignatureMethodAlgorithm::ECDSA)
        pAttributeList->AddAttribute("Algorithm", ALGO_ECDSASHA256);
    else
        pAttributeList->AddAttribute("Algorithm", ALGO_RSASHA256);

    m_xDocumentHandler->startElement("SignatureMethod",
                                     uno::Reference<xml::sax::XAttributeList>(pAttributeList));
    m_xDocumentHandler->endElement("SignatureMethod");
}

void OOXMLSecExporter::Impl::writeSignedInfoReferences()
{
    const SignatureReferenceInformations& rReferences = m_rInformation.vSignatureReferenceInfors;
    for (const SignatureReferenceInformation& rReference : rReferences)
    {
        if (rReference.nType == SignatureReferenceType::SAMEDOCUMENT)
        {
            {
                rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
                if (!rReference.ouURI.startsWith("idSignedProperties"))
                    pAttributeList->AddAttribute("Type",
                                                 "http://www.w3.org/2000/09/xmldsig#Object");
                else
                    pAttributeList->AddAttribute("Type",
                                                 "http://uri.etsi.org/01903#SignedProperties");
                pAttributeList->AddAttribute("URI", "#" + rReference.ouURI);
                m_xDocumentHandler->startElement(
                    "Reference", uno::Reference<xml::sax::XAttributeList>(pAttributeList));
            }
            if (rReference.ouURI.startsWith("idSignedProperties"))
            {
                m_xDocumentHandler->startElement(
                    "Transforms",
                    uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
                writeCanonicalizationTransform();
                m_xDocumentHandler->endElement("Transforms");
            }

            DocumentSignatureHelper::writeDigestMethod(m_xDocumentHandler);
            m_xDocumentHandler->startElement(
                "DigestValue", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
            m_xDocumentHandler->characters(rReference.ouDigestValue);
            m_xDocumentHandler->endElement("DigestValue");
            m_xDocumentHandler->endElement("Reference");
        }
    }
}

void OOXMLSecExporter::Impl::writeSignatureValue()
{
    m_xDocumentHandler->startElement(
        "SignatureValue", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters(m_rInformation.ouSignatureValue);
    m_xDocumentHandler->endElement("SignatureValue");
}

void OOXMLSecExporter::Impl::writeKeyInfo()
{
    m_xDocumentHandler->startElement(
        "KeyInfo", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    assert(m_rInformation.GetSigningCertificate());
    for (auto const& rData : m_rInformation.X509Datas)
    {
        m_xDocumentHandler->startElement(
            "X509Data", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
        for (auto const& it : rData)
        {
            m_xDocumentHandler->startElement(
                "X509Certificate",
                uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
            m_xDocumentHandler->characters(it.X509Certificate);
            m_xDocumentHandler->endElement("X509Certificate");
        }
        m_xDocumentHandler->endElement("X509Data");
    }
    m_xDocumentHandler->endElement("KeyInfo");
}

void OOXMLSecExporter::Impl::writePackageObject()
{
    rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
    pAttributeList->AddAttribute("Id", "idPackageObject_" + m_rInformation.ouSignatureId);
    m_xDocumentHandler->startElement("Object",
                                     uno::Reference<xml::sax::XAttributeList>(pAttributeList));

    writeManifest();
    writePackageObjectSignatureProperties();

    m_xDocumentHandler->endElement("Object");
}

void OOXMLSecExporter::Impl::writeManifest()
{
    m_xDocumentHandler->startElement(
        "Manifest", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    const SignatureReferenceInformations& rReferences = m_rInformation.vSignatureReferenceInfors;
    for (const SignatureReferenceInformation& rReference : rReferences)
    {
        if (rReference.nType != SignatureReferenceType::SAMEDOCUMENT)
        {
            if (OOXMLSecExporter::Impl::isOOXMLDenylist(rReference.ouURI))
                continue;

            writeManifestReference(rReference);
        }
    }
    m_xDocumentHandler->endElement("Manifest");
}

void OOXMLSecExporter::Impl::writeRelationshipTransform(const OUString& rURI)
{
    uno::Reference<embed::XHierarchicalStorageAccess> xHierarchicalStorageAccess(m_xRootStorage,
                                                                                 uno::UNO_QUERY);
    uno::Reference<io::XInputStream> xRelStream(
        xHierarchicalStorageAccess->openStreamElementByHierarchicalName(rURI,
                                                                        embed::ElementModes::READ),
        uno::UNO_QUERY);
    {
        rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
        pAttributeList->AddAttribute("Algorithm", ALGO_RELATIONSHIP);
        m_xDocumentHandler->startElement("Transform",
                                         uno::Reference<xml::sax::XAttributeList>(pAttributeList));
    }

    const uno::Sequence<uno::Sequence<beans::StringPair>> aRelationsInfo
        = comphelper::OFOPXMLHelper::ReadRelationsInfoSequence(xRelStream, rURI,
                                                               m_xComponentContext);
    for (const uno::Sequence<beans::StringPair>& rPairs : aRelationsInfo)
    {
        OUString aId;
        OUString aType;
        for (const beans::StringPair& rPair : rPairs)
        {
            if (rPair.First == "Id")
                aId = rPair.Second;
            else if (rPair.First == "Type")
                aType = rPair.Second;
        }

        if (OOXMLSecExporter::Impl::isOOXMLRelationDenylist(aType))
            continue;

        rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
        pAttributeList->AddAttribute("xmlns:mdssi", NS_MDSSI);
        pAttributeList->AddAttribute("SourceId", aId);
        m_xDocumentHandler->startElement("mdssi:RelationshipReference",
                                         uno::Reference<xml::sax::XAttributeList>(pAttributeList));
        m_xDocumentHandler->endElement("mdssi:RelationshipReference");
    }

    m_xDocumentHandler->endElement("Transform");
}

void OOXMLSecExporter::Impl::writePackageObjectSignatureProperties()
{
    m_xDocumentHandler->startElement(
        "SignatureProperties", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    {
        rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
        pAttributeList->AddAttribute("Id", "idSignatureTime_" + m_rInformation.ouSignatureId);
        pAttributeList->AddAttribute("Target", "#" + m_rInformation.ouSignatureId);
        m_xDocumentHandler->startElement("SignatureProperty",
                                         uno::Reference<xml::sax::XAttributeList>(pAttributeList));
    }
    {
        rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
        pAttributeList->AddAttribute("xmlns:mdssi", NS_MDSSI);
        m_xDocumentHandler->startElement("mdssi:SignatureTime",
                                         uno::Reference<xml::sax::XAttributeList>(pAttributeList));
    }
    m_xDocumentHandler->startElement(
        "mdssi:Format", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters("YYYY-MM-DDThh:mm:ssTZD");
    m_xDocumentHandler->endElement("mdssi:Format");

    m_xDocumentHandler->startElement(
        "mdssi:Value", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    if (!m_rInformation.ouDateTime.isEmpty())
        m_aSignatureTimeValue = m_rInformation.ouDateTime;
    else
    {
        m_aSignatureTimeValue = utl::toISO8601(m_rInformation.stDateTime);
        // Ignore sub-seconds.
        sal_Int32 nCommaPos = m_aSignatureTimeValue.indexOf(',');
        if (nCommaPos != -1)
        {
            m_aSignatureTimeValue
                = OUString::Concat(m_aSignatureTimeValue.subView(0, nCommaPos)) + "Z";
        }
    }
    m_xDocumentHandler->characters(m_aSignatureTimeValue);
    m_xDocumentHandler->endElement("mdssi:Value");

    m_xDocumentHandler->endElement("mdssi:SignatureTime");
    m_xDocumentHandler->endElement("SignatureProperty");
    m_xDocumentHandler->endElement("SignatureProperties");
}

void OOXMLSecExporter::Impl::writeManifestReference(const SignatureReferenceInformation& rReference)
{
    rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
    pAttributeList->AddAttribute("URI", rReference.ouURI);
    m_xDocumentHandler->startElement("Reference",
                                     uno::Reference<xml::sax::XAttributeList>(pAttributeList));

    // Transforms
    if (rReference.ouURI.endsWith(
            "?ContentType=application/vnd.openxmlformats-package.relationships+xml"))
    {
        OUString aURI = rReference.ouURI;
        // Ignore leading slash.
        if (aURI.startsWith("/"))
            aURI = aURI.copy(1);
        // Ignore query part of the URI.
        sal_Int32 nQueryPos = aURI.indexOf('?');
        if (nQueryPos != -1)
            aURI = aURI.copy(0, nQueryPos);

        m_xDocumentHandler->startElement(
            "Transforms", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));

        writeRelationshipTransform(aURI);
        writeCanonicalizationTransform();

        m_xDocumentHandler->endElement("Transforms");
    }

    DocumentSignatureHelper::writeDigestMethod(m_xDocumentHandler);
    m_xDocumentHandler->startElement(
        "DigestValue", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters(rReference.ouDigestValue);
    m_xDocumentHandler->endElement("DigestValue");
    m_xDocumentHandler->endElement("Reference");
}

void OOXMLSecExporter::Impl::writeOfficeObject()
{
    {
        rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
        pAttributeList->AddAttribute("Id", "idOfficeObject_" + m_rInformation.ouSignatureId);
        m_xDocumentHandler->startElement("Object",
                                         uno::Reference<xml::sax::XAttributeList>(pAttributeList));
    }
    m_xDocumentHandler->startElement(
        "SignatureProperties", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    {
        rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
        pAttributeList->AddAttribute("Id", "idOfficeV1Details_" + m_rInformation.ouSignatureId);
        pAttributeList->AddAttribute("Target", "#" + m_rInformation.ouSignatureId);
        m_xDocumentHandler->startElement("SignatureProperty",
                                         uno::Reference<xml::sax::XAttributeList>(pAttributeList));
    }
    writeSignatureInfo();
    m_xDocumentHandler->endElement("SignatureProperty");
    m_xDocumentHandler->endElement("SignatureProperties");
    m_xDocumentHandler->endElement("Object");
}

void OOXMLSecExporter::Impl::writeSignatureInfo()
{
    rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
    pAttributeList->AddAttribute("xmlns", "http://schemas.microsoft.com/office/2006/digsig");
    m_xDocumentHandler->startElement("SignatureInfoV1",
                                     uno::Reference<xml::sax::XAttributeList>(pAttributeList));

    m_xDocumentHandler->startElement(
        "SetupID", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters(m_rInformation.ouSignatureLineId);
    m_xDocumentHandler->endElement("SetupID");
    m_xDocumentHandler->startElement(
        "SignatureText", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->endElement("SignatureText");
    m_xDocumentHandler->startElement(
        "SignatureImage", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->endElement("SignatureImage");
    m_xDocumentHandler->startElement(
        "SignatureComments", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters(m_rInformation.ouDescription);
    m_xDocumentHandler->endElement("SignatureComments");
    // Just hardcode something valid according to [MS-OFFCRYPTO].
    m_xDocumentHandler->startElement(
        "WindowsVersion", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters("6.1");
    m_xDocumentHandler->endElement("WindowsVersion");
    m_xDocumentHandler->startElement(
        "OfficeVersion", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters("16.0");
    m_xDocumentHandler->endElement("OfficeVersion");
    m_xDocumentHandler->startElement(
        "ApplicationVersion", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters("16.0");
    m_xDocumentHandler->endElement("ApplicationVersion");
    m_xDocumentHandler->startElement(
        "Monitors", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters("1");
    m_xDocumentHandler->endElement("Monitors");
    m_xDocumentHandler->startElement(
        "HorizontalResolution", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters("1280");
    m_xDocumentHandler->endElement("HorizontalResolution");
    m_xDocumentHandler->startElement(
        "VerticalResolution", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters("800");
    m_xDocumentHandler->endElement("VerticalResolution");
    m_xDocumentHandler->startElement(
        "ColorDepth", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters("32");
    m_xDocumentHandler->endElement("ColorDepth");
    m_xDocumentHandler->startElement(
        "SignatureProviderId", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters("{00000000-0000-0000-0000-000000000000}");
    m_xDocumentHandler->endElement("SignatureProviderId");
    m_xDocumentHandler->startElement(
        "SignatureProviderUrl", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->endElement("SignatureProviderUrl");
    m_xDocumentHandler->startElement(
        "SignatureProviderDetails",
        uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters(
        "9"); // This is what MSO 2016 writes, though [MS-OFFCRYPTO] doesn't document what the value means.
    m_xDocumentHandler->endElement("SignatureProviderDetails");
    m_xDocumentHandler->startElement(
        "SignatureType", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    m_xDocumentHandler->characters("2");
    m_xDocumentHandler->endElement("SignatureType");

    m_xDocumentHandler->endElement("SignatureInfoV1");
}

void OOXMLSecExporter::Impl::writePackageSignature()
{
    m_xDocumentHandler->startElement(
        "Object", uno::Reference<xml::sax::XAttributeList>(new SvXMLAttributeList()));
    {
        rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
        pAttributeList->AddAttribute("xmlns:xd", NS_XD);
        pAttributeList->AddAttribute("Target", "#" + m_rInformation.ouSignatureId);
        m_xDocumentHandler->startElement("xd:QualifyingProperties",
                                         uno::Reference<xml::sax::XAttributeList>(pAttributeList));
    }

    DocumentSignatureHelper::writeSignedProperties(m_xDocumentHandler, m_rInformation,
                                                   m_aSignatureTimeValue, false);

    m_xDocumentHandler->endElement("xd:QualifyingProperties");
    m_xDocumentHandler->endElement("Object");
}

void OOXMLSecExporter::Impl::writeSignatureLineImages()
{
    if (m_rInformation.aValidSignatureImage.is())
    {
        rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
        pAttributeList->AddAttribute("Id", "idValidSigLnImg");
        m_xDocumentHandler->startElement("Object",
                                         uno::Reference<xml::sax::XAttributeList>(pAttributeList));
        OUString aGraphicInBase64;
        Graphic aGraphic(m_rInformation.aValidSignatureImage);
        if (!XOutBitmap::GraphicToBase64(aGraphic, aGraphicInBase64, false, ConvertDataFormat::EMF))
            SAL_WARN("xmlsecurity.helper", "could not convert graphic to base64");
        m_xDocumentHandler->characters(aGraphicInBase64);
        m_xDocumentHandler->endElement("Object");
    }
    if (!m_rInformation.aInvalidSignatureImage.is())
        return;

    rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
    pAttributeList->AddAttribute("Id", "idInvalidSigLnImg");
    m_xDocumentHandler->startElement("Object",
                                     uno::Reference<xml::sax::XAttributeList>(pAttributeList));
    OUString aGraphicInBase64;
    Graphic aGraphic(m_rInformation.aInvalidSignatureImage);
    if (!XOutBitmap::GraphicToBase64(aGraphic, aGraphicInBase64, false, ConvertDataFormat::EMF))
        SAL_WARN("xmlsecurity.helper", "could not convert graphic to base64");
    m_xDocumentHandler->characters(aGraphicInBase64);
    m_xDocumentHandler->endElement("Object");
}

void OOXMLSecExporter::Impl::writeSignature()
{
    rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
    pAttributeList->AddAttribute("xmlns", NS_XMLDSIG);
    pAttributeList->AddAttribute("Id", m_rInformation.ouSignatureId);
    getDocumentHandler()->startElement("Signature",
                                       uno::Reference<xml::sax::XAttributeList>(pAttributeList));

    writeSignedInfo();
    writeSignatureValue();
    writeKeyInfo();
    writePackageObject();
    writeOfficeObject();
    writePackageSignature();
    writeSignatureLineImages();

    getDocumentHandler()->endElement("Signature");
}

OOXMLSecExporter::OOXMLSecExporter(
    const uno::Reference<uno::XComponentContext>& xComponentContext,
    const uno::Reference<embed::XStorage>& xRootStorage,
    const uno::Reference<xml::sax::XDocumentHandler>& xDocumentHandler,
    const SignatureInformation& rInformation)
    : m_pImpl(
          std::make_unique<Impl>(xComponentContext, xRootStorage, xDocumentHandler, rInformation))
{
}

OOXMLSecExporter::~OOXMLSecExporter() = default;

void OOXMLSecExporter::writeSignature() { m_pImpl->writeSignature(); }

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */