1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/types.h>
14 #include <rtl/strbuf.hxx>
15 #include <rtl/ustring.hxx>
17 #include <com/sun/star/uno/Reference.hxx>
19 #include <svl/svldllapi.h>
21 namespace com::sun::star::security
{ class XCertificate
; }
23 struct SignatureInformation
;
25 namespace svl::crypto
{
27 /// Converts a hex-encoded string into a byte array.
28 SVL_DLLPUBLIC
std::vector
<unsigned char> DecodeHexString(const OString
& rHex
);
30 /// Helper to cryptographically sign and verify
31 /// arbitrary data blocks.
32 class SVL_DLLPUBLIC Signing
36 Signing(const css::uno::Reference
<css::security::XCertificate
>& xCertificate
) :
37 m_xCertificate(xCertificate
)
41 /// Add a range to sign.
42 /// Note: for efficiency this takes a naked pointer, which must remain valid
43 /// until this object is discarded.
44 void AddDataRange(const void* pData
, sal_Int32 size
)
46 m_dataBlocks
.emplace_back(pData
, size
);
49 void SetSignTSA(const OUString
& tsa
) { m_aSignTSA
= tsa
; }
50 void SetSignPassword(const OUString
& password
) { m_aSignPassword
= password
; }
52 /// Signs one or more data blocks (as a single, contiguous, array).
53 /// Returns the signature (in PKCS#7 format) as string (hex).
54 bool Sign(OStringBuffer
& rCMSHexBuffer
);
56 /// Verify and get Signature Information given a byte array.
57 static bool Verify(const std::vector
<unsigned char>& aData
,
58 const bool bNonDetached
,
59 const std::vector
<unsigned char>& aSignature
,
60 SignatureInformation
& rInformation
);
62 /// Verify and get Signature Information given a signature and stream.
63 static bool Verify(SvStream
& rStream
,
64 const std::vector
<std::pair
<size_t, size_t>>& aByteRanges
,
65 const bool bNonDetached
,
66 const std::vector
<unsigned char>& aSignature
,
67 SignatureInformation
& rInformation
);
70 /// The certificate to use for signing.
71 const css::uno::Reference
<css::security::XCertificate
> m_xCertificate
;
73 /// Data blocks (pointer-size pairs).
74 std::vector
<std::pair
<const void*, sal_Int32
>> m_dataBlocks
;
76 OUString m_aSignPassword
;
81 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */