From 6971159bb4468110d79c8367fcd776138302c1b9 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Tue, 22 Nov 2016 11:16:24 +0100 Subject: Executable_pdfverify: move pdfverify.cxx to workben/ That's where the implementation of such internal test binaries usually are. Change-Id: Ib7d2eb95de96d0d82e90e51f58da3a0c15a2ec71 Reviewed-on: https://gerrit.libreoffice.org/31073 Tested-by: Jenkins Reviewed-by: Miklos Vajna --- xmlsecurity/Executable_pdfverify.mk | 2 +- xmlsecurity/source/pdfio/pdfverify.cxx | 155 --------------------------------- xmlsecurity/workben/pdfverify.cxx | 155 +++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 156 deletions(-) delete mode 100644 xmlsecurity/source/pdfio/pdfverify.cxx create mode 100644 xmlsecurity/workben/pdfverify.cxx diff --git a/xmlsecurity/Executable_pdfverify.mk b/xmlsecurity/Executable_pdfverify.mk index 5cfbcd2dd43b..446c68f4969e 100644 --- a/xmlsecurity/Executable_pdfverify.mk +++ b/xmlsecurity/Executable_pdfverify.mk @@ -26,7 +26,7 @@ $(eval $(call gb_Executable_use_libraries,pdfverify,\ )) $(eval $(call gb_Executable_add_exception_objects,pdfverify,\ - xmlsecurity/source/pdfio/pdfverify \ + xmlsecurity/workben/pdfverify \ )) # vim:set noet sw=4 ts=4: diff --git a/xmlsecurity/source/pdfio/pdfverify.cxx b/xmlsecurity/source/pdfio/pdfverify.cxx deleted file mode 100644 index 04c33d8fc139..000000000000 --- a/xmlsecurity/source/pdfio/pdfverify.cxx +++ /dev/null @@ -1,155 +0,0 @@ -/* -*- 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 - -using namespace com::sun::star; - -SAL_IMPLEMENT_MAIN_WITH_ARGS(nArgc, pArgv) -{ - if (nArgc < 2) - { - SAL_WARN("xmlsecurity.pdfio", "not enough parameters"); - return 1; - } - - // Initialize nss / mscrypto. - uno::Reference xComponentContext; - try - { - xComponentContext = cppu::defaultBootstrap_InitialComponentContext(); - } - catch (const uno::RuntimeException& rException) - { - SAL_WARN("xmlsecurity.pdfio", "cppu::defaultBootstrap_InitialComponentContext() failed: " << rException.Message); - return 1; - } - uno::Reference xMultiComponentFactory = xComponentContext->getServiceManager(); - uno::Reference xMultiServiceFactory(xMultiComponentFactory, uno::UNO_QUERY); - comphelper::setProcessServiceFactory(xMultiServiceFactory); - uno::Reference xSEInitializer; - try - { - xSEInitializer = xml::crypto::SEInitializer::create(xComponentContext); - } - catch (const uno::DeploymentException& rException) - { - SAL_WARN("xmlsecurity.pdfio", "DeploymentException while creating SEInitializer: " << rException.Message); - return 1; - } - uno::Reference xSecurityContext = xSEInitializer->createSecurityContext(OUString()); - - OUString aInURL; - osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(pArgv[1]), aInURL); - OUString aOutURL; - if (nArgc > 2) - osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(pArgv[2]), aOutURL); - - bool bRemoveSignature = false; - if (nArgc > 3 && OString(pArgv[3]) == "-r") - bRemoveSignature = true; - - SvFileStream aStream(aInURL, StreamMode::READ); - xmlsecurity::pdfio::PDFDocument aDocument; - if (!aDocument.Read(aStream)) - { - SAL_WARN("xmlsecurity.pdfio", "failed to read the document"); - return 1; - } - - if (bRemoveSignature) - { - std::cerr << "removing the last signature" << std::endl; - std::vector aSignatures = aDocument.GetSignatureWidgets(); - if (aSignatures.empty()) - { - std::cerr << "found no signatures" << std::endl; - return 1; - } - - size_t nPosition = aSignatures.size() - 1; - if (!aDocument.RemoveSignature(nPosition)) - { - SAL_WARN("xmlsecurity.pdfio", "failed to remove signature #" << nPosition); - return 1; - } - - SvFileStream aOutStream(aOutURL, StreamMode::WRITE | StreamMode::TRUNC); - if (!aDocument.Write(aOutStream)) - { - SAL_WARN("xmlsecurity.pdfio", "failed to write the document"); - return 1; - } - - return 0; - } - - if (aOutURL.isEmpty()) - { - std::cerr << "verifying signatures" << std::endl; - std::vector aSignatures = aDocument.GetSignatureWidgets(); - if (aSignatures.empty()) - std::cerr << "found no signatures" << std::endl; - else - { - std::cerr << "found " << aSignatures.size() << " signatures" << std::endl; - for (size_t i = 0; i < aSignatures.size(); ++i) - { - SignatureInformation aInfo(i); - bool bLast = i == aSignatures.size() - 1; - if (!xmlsecurity::pdfio::PDFDocument::ValidateSignature(aStream, aSignatures[i], aInfo, bLast)) - { - SAL_WARN("xmlsecurity.pdfio", "failed to determine digest match"); - return 1; - } - - bool bSuccess = aInfo.nStatus == xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED; - std::cerr << "signature #" << i << ": digest match? " << bSuccess << std::endl; - } - } - - return 0; - } - - std::cerr << "adding a new signature" << std::endl; - uno::Reference xSecurityEnvironment = xSecurityContext->getSecurityEnvironment(); - uno::Sequence> aCertificates = xSecurityEnvironment->getPersonalCertificates(); - if (!aCertificates.hasElements()) - { - SAL_WARN("xmlsecurity.pdfio", "no signing certificates found"); - return 1; - } - if (!aDocument.Sign(aCertificates[0], "pdfverify", /*bAdES=*/true)) - { - SAL_WARN("xmlsecurity.pdfio", "failed to sign"); - return 1; - } - - SvFileStream aOutStream(aOutURL, StreamMode::WRITE | StreamMode::TRUNC); - if (!aDocument.Write(aOutStream)) - { - SAL_WARN("xmlsecurity.pdfio", "failed to write the document"); - return 1; - } - - return 0; -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/xmlsecurity/workben/pdfverify.cxx b/xmlsecurity/workben/pdfverify.cxx new file mode 100644 index 000000000000..04c33d8fc139 --- /dev/null +++ b/xmlsecurity/workben/pdfverify.cxx @@ -0,0 +1,155 @@ +/* -*- 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 + +using namespace com::sun::star; + +SAL_IMPLEMENT_MAIN_WITH_ARGS(nArgc, pArgv) +{ + if (nArgc < 2) + { + SAL_WARN("xmlsecurity.pdfio", "not enough parameters"); + return 1; + } + + // Initialize nss / mscrypto. + uno::Reference xComponentContext; + try + { + xComponentContext = cppu::defaultBootstrap_InitialComponentContext(); + } + catch (const uno::RuntimeException& rException) + { + SAL_WARN("xmlsecurity.pdfio", "cppu::defaultBootstrap_InitialComponentContext() failed: " << rException.Message); + return 1; + } + uno::Reference xMultiComponentFactory = xComponentContext->getServiceManager(); + uno::Reference xMultiServiceFactory(xMultiComponentFactory, uno::UNO_QUERY); + comphelper::setProcessServiceFactory(xMultiServiceFactory); + uno::Reference xSEInitializer; + try + { + xSEInitializer = xml::crypto::SEInitializer::create(xComponentContext); + } + catch (const uno::DeploymentException& rException) + { + SAL_WARN("xmlsecurity.pdfio", "DeploymentException while creating SEInitializer: " << rException.Message); + return 1; + } + uno::Reference xSecurityContext = xSEInitializer->createSecurityContext(OUString()); + + OUString aInURL; + osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(pArgv[1]), aInURL); + OUString aOutURL; + if (nArgc > 2) + osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(pArgv[2]), aOutURL); + + bool bRemoveSignature = false; + if (nArgc > 3 && OString(pArgv[3]) == "-r") + bRemoveSignature = true; + + SvFileStream aStream(aInURL, StreamMode::READ); + xmlsecurity::pdfio::PDFDocument aDocument; + if (!aDocument.Read(aStream)) + { + SAL_WARN("xmlsecurity.pdfio", "failed to read the document"); + return 1; + } + + if (bRemoveSignature) + { + std::cerr << "removing the last signature" << std::endl; + std::vector aSignatures = aDocument.GetSignatureWidgets(); + if (aSignatures.empty()) + { + std::cerr << "found no signatures" << std::endl; + return 1; + } + + size_t nPosition = aSignatures.size() - 1; + if (!aDocument.RemoveSignature(nPosition)) + { + SAL_WARN("xmlsecurity.pdfio", "failed to remove signature #" << nPosition); + return 1; + } + + SvFileStream aOutStream(aOutURL, StreamMode::WRITE | StreamMode::TRUNC); + if (!aDocument.Write(aOutStream)) + { + SAL_WARN("xmlsecurity.pdfio", "failed to write the document"); + return 1; + } + + return 0; + } + + if (aOutURL.isEmpty()) + { + std::cerr << "verifying signatures" << std::endl; + std::vector aSignatures = aDocument.GetSignatureWidgets(); + if (aSignatures.empty()) + std::cerr << "found no signatures" << std::endl; + else + { + std::cerr << "found " << aSignatures.size() << " signatures" << std::endl; + for (size_t i = 0; i < aSignatures.size(); ++i) + { + SignatureInformation aInfo(i); + bool bLast = i == aSignatures.size() - 1; + if (!xmlsecurity::pdfio::PDFDocument::ValidateSignature(aStream, aSignatures[i], aInfo, bLast)) + { + SAL_WARN("xmlsecurity.pdfio", "failed to determine digest match"); + return 1; + } + + bool bSuccess = aInfo.nStatus == xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED; + std::cerr << "signature #" << i << ": digest match? " << bSuccess << std::endl; + } + } + + return 0; + } + + std::cerr << "adding a new signature" << std::endl; + uno::Reference xSecurityEnvironment = xSecurityContext->getSecurityEnvironment(); + uno::Sequence> aCertificates = xSecurityEnvironment->getPersonalCertificates(); + if (!aCertificates.hasElements()) + { + SAL_WARN("xmlsecurity.pdfio", "no signing certificates found"); + return 1; + } + if (!aDocument.Sign(aCertificates[0], "pdfverify", /*bAdES=*/true)) + { + SAL_WARN("xmlsecurity.pdfio", "failed to sign"); + return 1; + } + + SvFileStream aOutStream(aOutURL, StreamMode::WRITE | StreamMode::TRUNC); + if (!aDocument.Write(aOutStream)) + { + SAL_WARN("xmlsecurity.pdfio", "failed to write the document"); + return 1; + } + + return 0; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit