PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfSignerCms.h
1// SPDX-FileCopyrightText: 2023 Francesco Pretto <ceztko@gmail.com>
2// SPDX-License-Identifier: LGPL-2.0-or-later OR MPL-2.0
3
4#ifndef PDF_SIGNER_CMS_H
5#define PDF_SIGNER_CMS_H
6
7#include <chrono>
8#include "PdfSigner.h"
9
10extern "C"
11{
12 // OpenSSL forward declaration
13 struct evp_pkey_st;
14 // libxml2 forward declaration
15 typedef struct _xmlNode xmlNode;
16 typedef xmlNode* xmlNodePtr;
17}
18
19namespace PoDoFo
20{
21 class CmsContext;
22
23 using PdfSigningService = std::function<void(bufferview hashToSign, bool dryrun, charbuff& signedHash)>;
24 using PdfSignedHashHandler = std::function<void(bufferview signedhHash, bool dryrun)>;
25
27 {
28 None = 0,
37 ServiceDoDryRun = 2,
38 };
39
40 struct PODOFO_API PdfSignerCmsParams final
41 {
42 PdfSignatureType SignatureType = PdfSignatureType::PAdES_B;
43 [[deprecated("Encryption should be automatically detected from the public key in the certificate")]]
44 PdfSignatureEncryption Encryption = PdfSignatureEncryption::RSA;
45 PdfHashingAlgorithm Hashing = PdfHashingAlgorithm::SHA256;
46 PdfSigningService SigningService;
47 nullable<std::chrono::seconds> SigningTimeUTC;
48 PdfSignedHashHandler SignedHashHandler;
49 PdfSignerCmsFlags Flags = PdfSignerCmsFlags::None;
50 };
51
53 {
54 None = 0,
58 AsOctetString = 2,
59 };
60
63 class PODOFO_API PdfSignerCms : public PdfSigner
64 {
65 friend class PdfSigningContext;
66 public:
74 const PdfSignerCmsParams& parameters = { });
75
80 PdfSignerCms(const bufferview& cert, const PdfSignerCmsParams& parameters = { });
81
83
84 private:
88
89 public:
90 void AppendData(const bufferview& data) override;
91 void ComputeSignature(charbuff& buffer, bool dryrun) override;
92 void FetchIntermediateResult(charbuff& result) override;
93 void ComputeSignatureDeferred(const bufferview& processedResult, charbuff& contents, bool dryrun) override;
94 void Reset() override;
95 std::string GetSignatureFilter() const override;
96 std::string GetSignatureSubFilter() const override;
97 std::string GetSignatureType() const override;
98 bool SkipBufferClear() const override;
99
104 void AddAttribute(const std::string_view& nid, const bufferview& attr, PdfSignatureAttributeFlags flags = PdfSignatureAttributeFlags::None);
105
110 void ReserveAttributeSize(unsigned attrSize);
111
112 public:
113 unsigned GetSignedHashSize() const;
114
115 const PdfSignerCmsParams& GetParameters() const { return m_parameters; }
116
117 private:
118 // Called by PdfSigningContext
119 void Dump(xmlNodePtr signerElem, std::string& temp);
120 void Restore(xmlNodePtr signerElem, charbuff& temp);
121 private:
122 void ensureEventBasedSigning();
123 void ensureDeferredSigning();
124 void checkContextInitialized();
125 void ensureContextInitialized();
126 void resetContext();
127 void doSign(const bufferview& input, charbuff& output);
128 void tryEnlargeSignatureContents(charbuff& contents);
129 private:
130 nullable<bool> m_deferredSigning;
131 charbuff m_certificate;
132 std::unique_ptr<CmsContext> m_cmsContext;
133 struct evp_pkey_st* m_privKey;
134 PdfSignerCmsParams m_parameters;
135 unsigned m_reservedSize;
136
137 // Temporary buffer variables
138 // NOTE: Don't clear it in Reset() override
139 charbuff m_encryptedHash;
140 };
141}
142
143ENABLE_BITMASK_OPERATORS(PoDoFo::PdfSignerCmsFlags);
144ENABLE_BITMASK_OPERATORS(PoDoFo::PdfSignatureAttributeFlags);
145
146#endif // PDF_SIGNER_CMS_H
This class computes a CMS signature according to RFC 5652.
Definition PdfSignerCms.h:64
A context that can be used to customize the signing process.
Definition PdfSigningContext.h:69
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:35
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
PdfSignatureAttributeFlags
Definition PdfSignerCms.h:53
@ SignedAttribute
The input is interpreted as a raw octet string.
@ None
Do not add a default appearance.
cspan< char > bufferview
Convenient read-only char buffer span.
Definition basetypes.h:16
PdfSignerCmsFlags
Definition PdfSignerCms.h:27
@ ServiceDoWrapDigest
When supplying an external PdfSigningService, specify if the service should be called for a dry run.