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