Files
odfstudio/xmlsecurity/qa/xmlsec/xmlsec.cxx
T
Miklos Vajna 0de900cec7 cool#9992 lok doc sign, hash extract: time for getCommandValues('Signature')
The final goal of this API is to give time & hash information about the
PDF signature, so once a 3rd-party produces the PKCS#7 signature, that
can be added to the document and the actual PDF sign can be re-run with
the same parameters.

This commit continues the replacement of XCertificate with
svl::crypto::SigningContext up to the point that the timestamp used in
svl/ can be exposed on the LOK API.

This is done by updating DocumentSignatureManager::add(),
PDFSignatureHelper::SetX509Certificate(),
vcl::filter::PDFDocument::Sign() and finally the svl::crypto::Signing
ctor to work with the signing context instead of an XCertificate
directly.

Time reporting works now, so add a test for that. The digest part still
needs doing.

Change-Id: I83f1274cd420b67194b7caf12b1027e623d4f7fe
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176404
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
2024-11-11 21:54:23 +01:00

109 lines
3.9 KiB
C++

/* -*- 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 <test/unoapi_test.hxx>
#include <com/sun/star/embed/XStorage.hpp>
#include <com/sun/star/xml/crypto/SEInitializer.hpp>
#include <comphelper/storagehelper.hxx>
#include <sfx2/lokhelper.hxx>
#include <svl/cryptosign.hxx>
#include <documentsignaturemanager.hxx>
using namespace ::com::sun::star;
namespace
{
/// Covers xmlsecurity/source/xmlsec/ fixes.
class Test : public UnoApiTest
{
public:
Test()
: UnoApiTest("/xmlsecurity/qa/xmlsec/data/")
{
}
void setUp() override
{
UnoApiTest::setUp();
MacrosTest::setUpX509(m_directories, "xmlsecurity_xmlsec");
}
};
OString ReadToString(const OUString& rUrl)
{
SvFileStream aStream(rUrl, StreamMode::READ);
return read_uInt8s_ToOString(aStream, aStream.remainingSize());
}
CPPUNIT_TEST_FIXTURE(Test, testInsertPrivateKey)
{
// Given a view that has CA/cert/key data associated:
uno::Reference<xml::crypto::XSEInitializer> mxSEInitializer
= xml::crypto::SEInitializer::create(getComponentContext());
uno::Reference<xml::crypto::XXMLSecurityContext> xSecurityContext
= mxSEInitializer->createSecurityContext(OUString());
loadFromURL("private:factory/swriter");
save("writer8");
DocumentSignatureManager aManager(getComponentContext(), DocumentSignatureMode::Content);
CPPUNIT_ASSERT(aManager.init());
uno::Reference<embed::XStorage> xStorage
= comphelper::OStorageHelper::GetStorageOfFormatFromURL(
ZIP_STORAGE_FORMAT_STRING, maTempFile.GetURL(), embed::ElementModes::READWRITE);
CPPUNIT_ASSERT(xStorage.is());
aManager.setStore(xStorage);
aManager.getSignatureHelper().SetStorage(xStorage, u"1.2");
OUString aCaPath = createFileURL(u"ca.pem");
std::string aCa;
aCa = ReadToString(aCaPath);
std::vector<std::string> aCerts = SfxLokHelper::extractCertificates(aCa);
SfxLokHelper::addCertificates(aCerts);
OUString aCertPath = createFileURL(u"cert.pem");
std::string aCert;
aCert = ReadToString(aCertPath);
OUString aKeyPath;
aKeyPath = createFileURL(u"key.pem");
std::string aKey;
aKey = ReadToString(aKeyPath);
uno::Reference<security::XCertificate> xCertificate
= SfxLokHelper::getSigningCertificate(aCert, aKey);
CPPUNIT_ASSERT(xCertificate.is());
// When getting the certificate flags and signing:
uno::Reference<xml::crypto::XSecurityEnvironment> xSecurityEnvironment
= xSecurityContext->getSecurityEnvironment();
// Get the certificate flags, the certificate chooser dialog does this:
xSecurityEnvironment->getCertificateCharacters(xCertificate);
OUString aDescription;
sal_Int32 nSecurityId;
svl::crypto::SigningContext aSigningContext;
aSigningContext.m_xCertificate = xCertificate;
CPPUNIT_ASSERT(
aManager.add(aSigningContext, xSecurityContext, aDescription, nSecurityId, false));
// Then make sure that signing succeeds:
aManager.read(/*bUseTempStream=*/true);
std::vector<SignatureInformation>& rInformations = aManager.getCurrentSignatureInformations();
CPPUNIT_ASSERT_EQUAL(static_cast<std::size_t>(1), rInformations.size());
// Without the accompanying fix in place, this test would have failed with:
// - Expected: 0 (UNKNOWN)
// - Actual : 1 (OPERATION_SUCCEEDED)
// i.e. the signing failed with an incorrectly imported private key.
CPPUNIT_ASSERT_EQUAL(
xml::crypto::SecurityOperationStatus::SecurityOperationStatus_OPERATION_SUCCEEDED,
rInformations[0].nStatus);
}
}
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */