2016-10-13 10:37:02 +02:00
|
|
|
/* -*- 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 <pdfsignaturehelper.hxx>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2016-10-13 13:13:44 +02:00
|
|
|
#include <com/sun/star/xml/crypto/SEInitializer.hpp>
|
|
|
|
|
2016-10-13 10:37:02 +02:00
|
|
|
#include <comphelper/sequence.hxx>
|
|
|
|
#include <tools/stream.hxx>
|
|
|
|
#include <unotools/ucbstreamhelper.hxx>
|
|
|
|
|
|
|
|
#include <pdfio/pdfdocument.hxx>
|
|
|
|
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
|
2016-10-13 13:13:44 +02:00
|
|
|
PDFSignatureHelper::PDFSignatureHelper(const uno::Reference<uno::XComponentContext>& xComponentContext)
|
|
|
|
: m_xComponentContext(xComponentContext)
|
|
|
|
{
|
|
|
|
m_xSEInitializer = xml::crypto::SEInitializer::create(m_xComponentContext);
|
|
|
|
if (m_xSEInitializer.is())
|
|
|
|
// This initializes nss / mscrypto.
|
|
|
|
m_xSecurityContext = m_xSEInitializer->createSecurityContext(OUString());
|
|
|
|
}
|
|
|
|
|
2016-10-13 10:37:02 +02:00
|
|
|
bool PDFSignatureHelper::ReadAndVerifySignature(const uno::Reference<io::XInputStream>& xInputStream)
|
|
|
|
{
|
|
|
|
if (!xInputStream.is())
|
|
|
|
{
|
|
|
|
SAL_WARN("xmlsecurity.helper", "input stream missing");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true));
|
|
|
|
xmlsecurity::pdfio::PDFDocument aDocument;
|
|
|
|
if (!aDocument.Read(*pStream))
|
|
|
|
{
|
|
|
|
SAL_WARN("xmlsecurity.helper", "failed to read the document");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<xmlsecurity::pdfio::PDFObjectElement*> aSignatures = aDocument.GetSignatureWidgets();
|
|
|
|
if (aSignatures.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < aSignatures.size(); ++i)
|
|
|
|
{
|
2016-10-13 16:11:02 +02:00
|
|
|
SignatureInformation aInfo(i);
|
2016-10-13 10:37:02 +02:00
|
|
|
|
2016-10-13 21:07:55 +02:00
|
|
|
if (!xmlsecurity::pdfio::PDFDocument::ValidateSignature(*pStream, aSignatures[i], aInfo))
|
2016-10-13 10:37:02 +02:00
|
|
|
{
|
|
|
|
SAL_WARN("xmlsecurity.helper", "failed to determine digest match");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_aSignatureInfos.push_back(aInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:11:02 +02:00
|
|
|
SignatureInformations PDFSignatureHelper::GetSignatureInformations() const
|
2016-10-13 10:37:02 +02:00
|
|
|
{
|
2016-10-13 16:11:02 +02:00
|
|
|
return m_aSignatureInfos;
|
|
|
|
}
|
|
|
|
|
|
|
|
uno::Sequence<security::DocumentSignatureInformation> PDFSignatureHelper::GetDocumentSignatureInformations() const
|
|
|
|
{
|
|
|
|
uno::Sequence<security::DocumentSignatureInformation> aRet(m_aSignatureInfos.size());
|
|
|
|
|
2016-10-13 21:07:55 +02:00
|
|
|
uno::Reference<xml::crypto::XSecurityEnvironment> xSecurityEnvironment = m_xSecurityContext->getSecurityEnvironment();
|
2016-10-13 16:11:02 +02:00
|
|
|
for (size_t i = 0; i < m_aSignatureInfos.size(); ++i)
|
|
|
|
{
|
|
|
|
const SignatureInformation& rInternal = m_aSignatureInfos[i];
|
|
|
|
security::DocumentSignatureInformation& rExternal = aRet[i];
|
|
|
|
rExternal.SignatureIsValid = rInternal.nStatus == xml::crypto::SecurityOperationStatus_OPERATION_SUCCEEDED;
|
2016-10-13 21:07:55 +02:00
|
|
|
rExternal.Signer = xSecurityEnvironment->createCertificateFromAscii(rInternal.ouX509Certificate);
|
2016-10-13 16:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return aRet;
|
2016-10-13 10:37:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|