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-24 14:41:18 +02:00
|
|
|
#include <com/sun/star/io/XTruncate.hpp>
|
2016-10-17 08:12:17 +02:00
|
|
|
#include <com/sun/star/security/CertificateValidity.hpp>
|
2017-02-06 17:08:38 +01:00
|
|
|
#include <com/sun/star/uno/SecurityException.hpp>
|
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>
|
|
|
|
|
2017-02-24 13:46:52 +01:00
|
|
|
#include <xmlsecurity/pdfio/pdfdocument.hxx>
|
2016-10-13 10:37:02 +02:00
|
|
|
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
|
2017-02-01 10:28:56 +01:00
|
|
|
PDFSignatureHelper::PDFSignatureHelper() = default;
|
2016-10-13 13:13:44 +02:00
|
|
|
|
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;
|
|
|
|
|
2016-10-20 13:44:03 +02:00
|
|
|
m_aSignatureInfos.clear();
|
|
|
|
|
2016-10-13 10:37:02 +02:00
|
|
|
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-26 17:52:28 +02:00
|
|
|
bool bLast = i == aSignatures.size() - 1;
|
2017-03-21 16:26:11 +01:00
|
|
|
if (!xmlsecurity::pdfio::ValidateSignature(*pStream, aSignatures[i], aInfo, bLast))
|
2016-10-13 10:37:02 +02:00
|
|
|
SAL_WARN("xmlsecurity.helper", "failed to determine digest match");
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-08 14:10:05 +01:00
|
|
|
uno::Sequence<security::DocumentSignatureInformation> PDFSignatureHelper::GetDocumentSignatureInformations(const uno::Reference<xml::crypto::XSecurityEnvironment>& xSecEnv) const
|
2016-10-13 16:11:02 +02:00
|
|
|
{
|
|
|
|
uno::Sequence<security::DocumentSignatureInformation> aRet(m_aSignatureInfos.size());
|
|
|
|
|
|
|
|
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-11-08 14:10:05 +01:00
|
|
|
rExternal.Signer = xSecEnv->createCertificateFromAscii(rInternal.ouX509Certificate);
|
2016-12-01 14:26:55 +01:00
|
|
|
rExternal.PartialDocumentSignature = rInternal.bPartialDocumentSignature;
|
2016-10-17 08:12:17 +02:00
|
|
|
|
|
|
|
// Verify certificate.
|
|
|
|
if (rExternal.Signer.is())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-11-08 14:10:05 +01:00
|
|
|
rExternal.CertificateStatus = xSecEnv->verifyCertificate(rExternal.Signer, {});
|
2016-10-17 08:12:17 +02:00
|
|
|
}
|
|
|
|
catch (const uno::SecurityException& rException)
|
|
|
|
{
|
|
|
|
SAL_WARN("xmlsecurity.helper", "failed to verify certificate: " << rException.Message);
|
|
|
|
rExternal.CertificateStatus = security::CertificateValidity::INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rExternal.CertificateStatus = security::CertificateValidity::INVALID;
|
2016-10-13 16:11:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return aRet;
|
2016-10-13 10:37:02 +02:00
|
|
|
}
|
|
|
|
|
2016-10-20 13:44:03 +02:00
|
|
|
sal_Int32 PDFSignatureHelper::GetNewSecurityId() const
|
|
|
|
{
|
|
|
|
return m_aSignatureInfos.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFSignatureHelper::SetX509Certificate(const uno::Reference<security::XCertificate>& xCertificate)
|
|
|
|
{
|
|
|
|
m_xCertificate = xCertificate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDFSignatureHelper::SetDescription(const OUString& rDescription)
|
|
|
|
{
|
|
|
|
m_aDescription = rDescription;
|
|
|
|
}
|
|
|
|
|
2016-11-17 12:40:06 +01:00
|
|
|
bool PDFSignatureHelper::Sign(const uno::Reference<io::XInputStream>& xInputStream, bool bAdES)
|
2016-10-20 13:44:03 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-17 12:40:06 +01:00
|
|
|
if (!aDocument.Sign(m_xCertificate, m_aDescription, bAdES))
|
2016-10-20 13:44:03 +02:00
|
|
|
{
|
|
|
|
SAL_WARN("xmlsecurity.helper", "failed to sign");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uno::Reference<io::XStream> xStream(xInputStream, uno::UNO_QUERY);
|
|
|
|
std::unique_ptr<SvStream> pOutStream(utl::UcbStreamHelper::CreateStream(xStream, true));
|
|
|
|
if (!aDocument.Write(*pOutStream))
|
|
|
|
{
|
|
|
|
SAL_WARN("xmlsecurity.helper", "failed to write signed data");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-24 14:41:18 +02:00
|
|
|
bool PDFSignatureHelper::RemoveSignature(const uno::Reference<io::XInputStream>& xInputStream, sal_uInt16 nPosition)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aDocument.RemoveSignature(nPosition))
|
|
|
|
{
|
|
|
|
SAL_WARN("xmlsecurity.helper", "failed to remove signature");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uno::Reference<io::XStream> xStream(xInputStream, uno::UNO_QUERY);
|
|
|
|
uno::Reference<io::XTruncate> xTruncate(xStream, uno::UNO_QUERY);
|
|
|
|
if (!xTruncate.is())
|
|
|
|
{
|
|
|
|
SAL_WARN("xmlsecurity.helper", "failed to truncate");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
xTruncate->truncate();
|
|
|
|
std::unique_ptr<SvStream> pOutStream(utl::UcbStreamHelper::CreateStream(xStream, true));
|
|
|
|
if (!aDocument.Write(*pOutStream))
|
|
|
|
{
|
|
|
|
SAL_WARN("xmlsecurity.helper", "failed to write without signature");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-13 10:37:02 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|