blob: 9a5ec842f13db97d7ad3d75d3f5e0352c83abc01 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
/* -*- 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>
#include <com/sun/star/xml/crypto/SEInitializer.hpp>
#include <comphelper/sequence.hxx>
#include <tools/stream.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <pdfio/pdfdocument.hxx>
using namespace ::com::sun::star;
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());
}
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)
{
security::DocumentSignatureInformation aInfo;
bool bDigestMatch;
if (!xmlsecurity::pdfio::PDFDocument::ValidateSignature(*pStream, aSignatures[i], bDigestMatch))
{
SAL_WARN("xmlsecurity.helper", "failed to determine digest match");
continue;
}
aInfo.SignatureIsValid = bDigestMatch;
m_aSignatureInfos.push_back(aInfo);
}
return true;
}
uno::Sequence<security::DocumentSignatureInformation> PDFSignatureHelper::GetDocumentSignatureInformations()
{
return comphelper::containerToSequence(m_aSignatureInfos);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|