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,
40 Deterministic = 4,
45 SkipVerification = 8,
46 };
47
48 // NOTE: The deprecated Encryption field below will cause deprecation warnings
49 // because of implictly generated costructors, hence we suppress warnings for
50 // the whole struct
51 PODOFO_SUPPRESS_DEPRECATED_PUSH
52 struct PODOFO_API PdfSignerCmsParams final
53 {
54 PdfSignatureType SignatureType = PdfSignatureType::PAdES_B;
55 [[deprecated("Unused: the signing algorithm is automatically detected from the public key in the certificate")]]
56 PdfSignatureEncryption Encryption = PdfSignatureEncryption::RSA;
57 PdfHashingAlgorithm Hashing = PdfHashingAlgorithm::SHA256;
58 PdfSigningService SigningService;
59 nullable<std::chrono::seconds> SigningTimeUTC;
60 PdfSignedHashHandler SignedHashHandler;
61 PdfSignerCmsFlags Flags = PdfSignerCmsFlags::None;
62 };
63 PODOFO_SUPPRESS_DEPRECATED_POP
64
66 {
67 None = 0,
71 AsOctetString = 2,
72 };
73
75 class PODOFO_API PdfSignerCms : public PdfSigner
76 {
77 friend class PdfSigningContext;
78 public:
85 const PdfSignerCmsParams& parameters = { });
86
90 PdfSignerCms(const bufferview& cert, const PdfSignerCmsParams& parameters = { });
91
93
94 private:
97
98 public:
101 void ValidateSignatureDate(const nullable<PdfDate>& date) override;
102 void AppendData(const bufferview& data) override;
103 void ComputeSignature(charbuff& buffer, bool dryrun) override;
104 void FetchIntermediateResult(charbuff& result) override;
105 void ComputeSignatureDeferred(const bufferview& processedResult, charbuff& contents, bool dryrun) override;
106 void Reset() override;
107 std::string GetSignatureFilter() const override;
108 std::string GetSignatureSubFilter() const override;
109 std::string GetSignatureType() const override;
110 bool SkipBufferClear() const override;
111
115 void AddAttribute(const std::string_view& nid, const bufferview& attr, PdfSignatureAttributeFlags flags = PdfSignatureAttributeFlags::None);
116
119 void ReserveAttributeSize(unsigned attrSize);
120
121 public:
122 unsigned GetSignedHashSize() const;
123
124 const PdfSignerCmsParams& GetParameters() const { return m_parameters; }
125
126 private:
127 // Called by PdfSigningContext
128 void Dump(xmlNodePtr signerElem, std::string& temp);
129 void Restore(xmlNodePtr signerElem, charbuff& temp);
130 private:
131 void ensureEventBasedSigning();
132 void ensureDeferredSigning();
133 void checkContextInitialized();
134 void ensureContextInitialized();
135 void resetContext();
136 bool shouldVerify() const;
137 void doSign(const bufferview& input, charbuff& output);
138 void tryEnlargeSignatureContents(charbuff& contents);
139 private:
140 nullable<bool> m_deferredSigning;
141 charbuff m_certificate;
142 std::unique_ptr<CmsContext> m_cmsContext;
143 struct evp_pkey_st* m_privKey;
144 PdfSignerCmsParams m_parameters;
145 unsigned m_reservedSize;
146
147 // Temporary buffer variables
148 // NOTE: Don't clear it in Reset() override
149 charbuff m_encryptedHash;
150 };
151}
152
153ENABLE_BITMASK_OPERATORS(PoDoFo::PdfSignerCmsFlags);
154ENABLE_BITMASK_OPERATORS(PoDoFo::PdfSignatureAttributeFlags);
155
156#endif // PDF_SIGNER_CMS_H
This class computes a CMS signature according to RFC 5652.
Definition PdfSignerCms.h:76
A context that can be used to customize the signing process.
Definition PdfSigningContext.h:64
Convenient type for char array storage and/or buffer with std::string compatibility.
Definition basetypes.h:30
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13
PdfSignatureAttributeFlags
Definition PdfSignerCms.h:66
@ 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:15
PdfSignerCmsFlags
Definition PdfSignerCms.h:27
@ ServiceDoDryRun
For signing algorithms that have a random component, make it deterministic Applies to ECDSA,...
@ ServiceDoWrapDigest
When supplying an external PdfSigningService, specify if the service should be called for a dry run.
@ Deterministic
Skip the inline verification of the signed hash supplied by an external signing service or by a defer...