From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- sfx2/source/doc/DocumentSigner.cxx | 121 +++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 sfx2/source/doc/DocumentSigner.cxx (limited to 'sfx2/source/doc/DocumentSigner.cxx') diff --git a/sfx2/source/doc/DocumentSigner.cxx b/sfx2/source/doc/DocumentSigner.cxx new file mode 100644 index 000000000..0106a6477 --- /dev/null +++ b/sfx2/source/doc/DocumentSigner.cxx @@ -0,0 +1,121 @@ +/* -*- 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 + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +using namespace css; + +namespace sfx2 +{ +bool DocumentSigner::signDocument(uno::Reference const& rxCertificate) +{ + std::unique_ptr pStream( + utl::UcbStreamHelper::CreateStream(m_aUrl, StreamMode::READ | StreamMode::WRITE)); + uno::Reference xInputStream(new utl::OStreamWrapper(std::move(pStream))); + + bool bResult = false; + uno::Reference xWriteableZipStore; + try + { + xWriteableZipStore = comphelper::OStorageHelper::GetStorageOfFormatFromStream( + ZIP_STORAGE_FORMAT_STRING, xInputStream); + } + catch (const io::IOException&) + { + } + + OUString aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(xWriteableZipStore)); + + uno::Reference xSigner( + security::DocumentDigitalSignatures::createWithVersionAndValidSignature( + comphelper::getProcessComponentContext(), aODFVersion, + /*bHasValidDocumentSignature*/ true)); + + try + { + uno::Reference xMetaInf; + if (xWriteableZipStore.is() && xWriteableZipStore->hasByName("META-INF")) + { + xMetaInf = xWriteableZipStore->openStorageElement("META-INF", + embed::ElementModes::READWRITE); + if (!xMetaInf.is()) + throw uno::RuntimeException(); + } + if (xMetaInf.is()) + { + uno::Reference xStorage + = comphelper::OStorageHelper::GetStorageOfFormatFromStream( + ZIP_STORAGE_FORMAT_STRING, xInputStream); + + // ODF. + uno::Reference xStream; + xStream.set( + xMetaInf->openStreamElement(xSigner->getDocumentContentSignatureDefaultStreamName(), + embed::ElementModes::READWRITE), + uno::UNO_SET_THROW); + bool bSuccess = xSigner->signDocumentWithCertificate(rxCertificate, xStorage, xStream); + if (bSuccess) + { + uno::Reference xTransact(xMetaInf, uno::UNO_QUERY_THROW); + xTransact->commit(); + xTransact.set(xWriteableZipStore, uno::UNO_QUERY_THROW); + xTransact->commit(); + bResult = true; + } + } + else if (xWriteableZipStore.is()) + { + uno::Reference xStorage + = comphelper::OStorageHelper::GetStorageOfFormatFromStream( + ZIP_STORAGE_FORMAT_STRING, xInputStream); + + // OOXML. + uno::Reference xStream; + + // We need read-write to be able to add the signature relation. + bool bSuccess = xSigner->signDocumentWithCertificate(rxCertificate, xStorage, xStream); + + if (bSuccess) + { + uno::Reference xTransact(xWriteableZipStore, + uno::UNO_QUERY_THROW); + xTransact->commit(); + bResult = true; + } + } + else + { + // Something not ZIP based: e.g. PDF. + bResult = xSigner->signDocumentWithCertificate( + rxCertificate, uno::Reference(), xInputStream); + } + } + catch (const uno::Exception&) + { + } + return bResult; +} + +} // namespace sfx2 + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3