summaryrefslogtreecommitdiffstats
path: root/xmlsecurity/qa/unit/signing/signing2.cxx
blob: 50baa9822360e42b84d5cf5de701a0eb45c98941 (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
/* -*- 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 <sal/config.h>

#include <config_crypto.h>

#if USE_CRYPTO_NSS
#include <secoid.h>
#endif

#include <test/unoapixml_test.hxx>

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/embed/XStorage.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/util/XCloseable.hpp>
#include <com/sun/star/xml/crypto/SEInitializer.hpp>

#include <officecfg/Office/Common.hxx>

#include <sfx2/sfxbasemodel.hxx>
#include <sfx2/objsh.hxx>
#include <comphelper/documentconstants.hxx>
#include <comphelper/propertysequence.hxx>
#include <unotools/tempfile.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <comphelper/storagehelper.hxx>
#include <osl/file.hxx>
#include <osl/process.h>
#include <osl/thread.hxx>

using namespace css;

/// Testsuite for the document signing feature.
class SigningTest2 : public UnoApiXmlTest
{
protected:
    uno::Reference<xml::crypto::XSEInitializer> mxSEInitializer;
    uno::Reference<xml::crypto::XXMLSecurityContext> mxSecurityContext;

public:
    SigningTest2();
    virtual void setUp() override;
    virtual void tearDown() override;
    void registerNamespaces(xmlXPathContextPtr& pXmlXpathCtx) override;
};

SigningTest2::SigningTest2()
    : UnoApiXmlTest("/xmlsecurity/qa/unit/signing/data/")
{
}

void SigningTest2::setUp()
{
    UnoApiXmlTest::setUp();

    MacrosTest::setUpX509(m_directories, "xmlsecurity_signing2");
    MacrosTest::setUpGpg(m_directories, "xmlsecurity_signing2");

    // Initialize crypto after setting up the environment variables.
    mxSEInitializer = xml::crypto::SEInitializer::create(mxComponentContext);
    mxSecurityContext = mxSEInitializer->createSecurityContext(OUString());
#if USE_CRYPTO_NSS
#ifdef NSS_USE_ALG_IN_ANY_SIGNATURE
    // policy may disallow using SHA1 for signatures but unit test documents
    // have such existing signatures (call this after createSecurityContext!)
    NSS_SetAlgorithmPolicy(SEC_OID_SHA1, NSS_USE_ALG_IN_ANY_SIGNATURE, 0);
#endif
#endif
}

void SigningTest2::tearDown()
{
    MacrosTest::tearDownGpg();

    UnoApiXmlTest::tearDown();
}

/// Test if a macro signature from a ODF Database is preserved when saving
CPPUNIT_TEST_FIXTURE(SigningTest2, testPreserveMacroSignatureODB)
{
    loadFromFile(u"odb_signed_macros.odb");

    // save as ODB
    save("StarOffice XML (Base)");

    // Parse the resulting XML.
    uno::Reference<embed::XStorage> xStorage
        = comphelper::OStorageHelper::GetStorageOfFormatFromURL(
            ZIP_STORAGE_FORMAT_STRING, maTempFile.GetURL(), embed::ElementModes::READ);
    CPPUNIT_ASSERT(xStorage.is());
    uno::Reference<embed::XStorage> xMetaInf
        = xStorage->openStorageElement("META-INF", embed::ElementModes::READ);
    uno::Reference<io::XInputStream> xInputStream(
        xMetaInf->openStreamElement("macrosignatures.xml", embed::ElementModes::READ),
        uno::UNO_QUERY);
    std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true));
    xmlDocUniquePtr pXmlDoc = parseXmlStream(pStream.get());

    // Make sure the signature is still there
    assertXPath(pXmlDoc, "//dsig:Signature"_ostr, "Id"_ostr,
                "ID_00a7002f009000bc00ce00f7004400460080002f002e00e400e0003700df00e8");
}

CPPUNIT_TEST_FIXTURE(SigningTest2, testPasswordPreserveMacroSignatureODF13)
{
    // load ODF 1.3 encrypted document
    load(createFileURL(u"encrypted_scriptsig_odf13.odt"), "password");
    {
        uno::Reference<text::XTextDocument> xTextDoc(mxComponent, uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT_EQUAL(OUString("secret"), xTextDoc->getText()->getString());
        // test macro signature
        SfxBaseModel* pBaseModel(dynamic_cast<SfxBaseModel*>(mxComponent.get()));
        CPPUNIT_ASSERT(pBaseModel);
        SfxObjectShell* pObjectShell(pBaseModel->GetObjectShell());
        uno::Reference<beans::XPropertySet> xPropSet(pObjectShell->GetStorage(),
                                                     uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT_EQUAL(ODFVER_013_TEXT,
                             xPropSet->getPropertyValue("Version").get<OUString>());
        CPPUNIT_ASSERT_EQUAL(SignatureState::OK, pObjectShell->GetScriptingSignatureState());
    }

    saveAndReload("writer8", "password");
    {
        // test standard ODF 1.2/1.3/1.4 encryption
        xmlDocUniquePtr pXmlDoc = parseExport("META-INF/manifest.xml");
        assertXPath(pXmlDoc, "/manifest:manifest"_ostr, "version"_ostr, "1.3");
        assertXPath(pXmlDoc, "/manifest:manifest/manifest:file-entry[@manifest:size != '0']"_ostr,
                    8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data[@manifest:checksum-type and @manifest:checksum]"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:algorithm[@manifest:algorithm-name='http://www.w3.org/2001/04/xmlenc#aes256-cbc']"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:algorithm[string-length(@manifest:initialisation-vector) = 24]"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:start-key-generation[@manifest:start-key-generation-name='http://www.w3.org/2000/09/xmldsig#sha256' and @manifest:key-size='32']"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[@manifest:key-derivation-name='PBKDF2' and @manifest:key-size='32']"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[@manifest:iteration-count='100000']"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[string-length(@manifest:salt) = 24]"_ostr,
            8);
        // test reimport
        uno::Reference<text::XTextDocument> xTextDoc(mxComponent, uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT_EQUAL(OUString("secret"), xTextDoc->getText()->getString());
        // test macro signature - this didn't actually work!
        // using Zip Storage means the encrypted streams are signed, so
        // after encrypting again the sigature didn't match and was dropped
        //        assertDocument(CPPUNIT_SOURCELINE(), "writer8", SignatureState::NOSIGNATURES,
        //                       SignatureState::OK, ODFVER_013_TEXT);
    }

    {
        Resetter resetter([]() {
            std::shared_ptr<comphelper::ConfigurationChanges> pBatch(
                comphelper::ConfigurationChanges::create());
            officecfg::Office::Common::Misc::ExperimentalMode::set(false, pBatch);
            return pBatch->commit();
        });
        std::shared_ptr<comphelper::ConfigurationChanges> pBatch(
            comphelper::ConfigurationChanges::create());
        officecfg::Office::Common::Misc::ExperimentalMode::set(true, pBatch);
        pBatch->commit();

        // store it experimental - reload
        saveAndReload("writer8", "password");

        // test wholesome ODF extended encryption
        xmlDocUniquePtr pXmlDoc = parseExport("META-INF/manifest.xml");
        assertXPath(pXmlDoc, "/manifest:manifest"_ostr, "version"_ostr, "1.3");
        assertXPath(pXmlDoc, "/manifest:manifest/manifest:file-entry"_ostr, 1);
        assertXPath(pXmlDoc, "/manifest:manifest/manifest:file-entry"_ostr, "full-path"_ostr,
                    "encrypted-package");
        assertXPath(pXmlDoc, "/manifest:manifest/manifest:file-entry[@manifest:size != '0']"_ostr,
                    1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data[@manifest:checksum-type or @manifest:checksum]"_ostr,
            0);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:algorithm[@manifest:algorithm-name='http://www.w3.org/2009/xmlenc11#aes256-gcm']"_ostr,
            1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:algorithm[string-length(@manifest:initialisation-vector) = 16]"_ostr,
            1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:start-key-generation[@manifest:start-key-generation-name='http://www.w3.org/2001/04/xmlenc#sha256' and @manifest:key-size='32']"_ostr,
            1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[@manifest:key-derivation-name='urn:org:documentfoundation:names:experimental:office:manifest:argon2id' and @manifest:key-size='32']"_ostr,
            1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[@manifest:iteration-count]"_ostr,
            0);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[string-length(@manifest:salt) = 24]"_ostr,
            1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[@loext:argon2-iterations='3' and @loext:argon2-memory='65536' and @loext:argon2-lanes='4']"_ostr,
            1);
        // test reimport
        uno::Reference<text::XTextDocument> xTextDoc(mxComponent, uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT_EQUAL(OUString("secret"), xTextDoc->getText()->getString());
    }
}

CPPUNIT_TEST_FIXTURE(SigningTest2, testPasswordPreserveMacroSignatureODFWholesomeLO242)
{
    // load wholesome ODF (extended) encrypted document
    load(createFileURL(u"encrypted_scriptsig_lo242.odt"), "password");
    {
        uno::Reference<text::XTextDocument> xTextDoc(mxComponent, uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT_EQUAL(OUString("secret"), xTextDoc->getText()->getString());
        // test macro signature
        SfxBaseModel* pBaseModel(dynamic_cast<SfxBaseModel*>(mxComponent.get()));
        CPPUNIT_ASSERT(pBaseModel);
        SfxObjectShell* pObjectShell(pBaseModel->GetObjectShell());
        uno::Reference<beans::XPropertySet> xPropSet(pObjectShell->GetStorage(),
                                                     uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT_EQUAL(ODFVER_013_TEXT,
                             xPropSet->getPropertyValue("Version").get<OUString>());
        CPPUNIT_ASSERT_EQUAL(SignatureState::OK, pObjectShell->GetScriptingSignatureState());
    }

    {
        Resetter resetter([]() {
            std::shared_ptr<comphelper::ConfigurationChanges> pBatch(
                comphelper::ConfigurationChanges::create());
            officecfg::Office::Common::Misc::ExperimentalMode::set(false, pBatch);
            return pBatch->commit();
        });
        std::shared_ptr<comphelper::ConfigurationChanges> pBatch(
            comphelper::ConfigurationChanges::create());
        officecfg::Office::Common::Misc::ExperimentalMode::set(true, pBatch);
        pBatch->commit();

        // store it experimental - reload
        saveAndReload("writer8", "password");

        // test wholesome ODF extended encryption
        xmlDocUniquePtr pXmlDoc = parseExport("META-INF/manifest.xml");
        assertXPath(pXmlDoc, "/manifest:manifest"_ostr, "version"_ostr, "1.3");
        assertXPath(pXmlDoc, "/manifest:manifest/manifest:file-entry"_ostr, 1);
        assertXPath(pXmlDoc, "/manifest:manifest/manifest:file-entry"_ostr, "full-path"_ostr,
                    "encrypted-package");
        assertXPath(pXmlDoc, "/manifest:manifest/manifest:file-entry[@manifest:size != '0']"_ostr,
                    1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data[@manifest:checksum-type or @manifest:checksum]"_ostr,
            0);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:algorithm[@manifest:algorithm-name='http://www.w3.org/2009/xmlenc11#aes256-gcm']"_ostr,
            1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:algorithm[string-length(@manifest:initialisation-vector) = 16]"_ostr,
            1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:start-key-generation[@manifest:start-key-generation-name='http://www.w3.org/2001/04/xmlenc#sha256' and @manifest:key-size='32']"_ostr,
            1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[@manifest:key-derivation-name='urn:org:documentfoundation:names:experimental:office:manifest:argon2id' and @manifest:key-size='32']"_ostr,
            1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[@manifest:iteration-count]"_ostr,
            0);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[string-length(@manifest:salt) = 24]"_ostr,
            1);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[@loext:argon2-iterations='3' and @loext:argon2-memory='65536' and @loext:argon2-lanes='4']"_ostr,
            1);
        // test reimport
        uno::Reference<text::XTextDocument> xTextDoc(mxComponent, uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT_EQUAL(OUString("secret"), xTextDoc->getText()->getString());
        // test macro signature - this should work now
        SfxBaseModel* pBaseModel(dynamic_cast<SfxBaseModel*>(mxComponent.get()));
        CPPUNIT_ASSERT(pBaseModel);
        SfxObjectShell* pObjectShell(pBaseModel->GetObjectShell());
        uno::Reference<beans::XPropertySet> xPropSet(pObjectShell->GetStorage(),
                                                     uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT_EQUAL(ODFVER_013_TEXT,
                             xPropSet->getPropertyValue("Version").get<OUString>());
        CPPUNIT_ASSERT_EQUAL(SignatureState::OK, pObjectShell->GetScriptingSignatureState());
    }

    saveAndReload("writer8", "password");
    {
        // test standard ODF 1.2/1.3/1.4 encryption
        xmlDocUniquePtr pXmlDoc = parseExport("META-INF/manifest.xml");
        assertXPath(pXmlDoc, "/manifest:manifest"_ostr, "version"_ostr, "1.3");
        assertXPath(pXmlDoc, "/manifest:manifest/manifest:file-entry[@manifest:size != '0']"_ostr,
                    8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data[@manifest:checksum-type and @manifest:checksum]"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:algorithm[@manifest:algorithm-name='http://www.w3.org/2001/04/xmlenc#aes256-cbc']"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:algorithm[string-length(@manifest:initialisation-vector) = 24]"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:start-key-generation[@manifest:start-key-generation-name='http://www.w3.org/2000/09/xmldsig#sha256' and @manifest:key-size='32']"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[@manifest:key-derivation-name='PBKDF2' and @manifest:key-size='32']"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[@manifest:iteration-count='100000']"_ostr,
            8);
        assertXPath(
            pXmlDoc,
            "/manifest:manifest/manifest:file-entry/manifest:encryption-data/manifest:key-derivation[string-length(@manifest:salt) = 24]"_ostr,
            8);
        // test reimport
        uno::Reference<text::XTextDocument> xTextDoc(mxComponent, uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT_EQUAL(OUString("secret"), xTextDoc->getText()->getString());
        // test macro signature - this didn't actually work!
        // using Zip Storage means the encrypted streams are signed, so
        // after encrypting again the sigature didn't match and was dropped
        //        assertDocument(CPPUNIT_SOURCELINE(), "writer8", SignatureState::NOSIGNATURES,
        //                       SignatureState::OK, ODFVER_013_TEXT);
    }
}

void SigningTest2::registerNamespaces(xmlXPathContextPtr& pXmlXpathCtx)
{
    xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("odfds"),
                       BAD_CAST("urn:oasis:names:tc:opendocument:xmlns:digitalsignature:1.0"));
    xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("dsig"),
                       BAD_CAST("http://www.w3.org/2000/09/xmldsig#"));
    xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("xd"), BAD_CAST("http://uri.etsi.org/01903/v1.3.2#"));

    // manifest.xml
    xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("manifest"),
                       BAD_CAST("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
    xmlXPathRegisterNs(
        pXmlXpathCtx, BAD_CAST("loext"),
        BAD_CAST("urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0"));
}

CPPUNIT_PLUGIN_IMPLEMENT();

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