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,
50 SkipDateValidation = 16,
51 };
52
53 // NOTE: The deprecated Encryption field below will cause deprecation warnings
54 // because of implictly generated costructors, hence we suppress warnings for
55 // the whole struct
56 PODOFO_SUPPRESS_DEPRECATED_PUSH
57 struct PODOFO_API PdfSignerCmsParams final
58 {
59 PdfSignatureType SignatureType = PdfSignatureType::PAdES_B;
60 [[deprecated("Unused: the signing algorithm is automatically detected from the public key in the certificate")]]
61 PdfSignatureEncryption Encryption = PdfSignatureEncryption::RSA;
62 PdfHashingAlgorithm Hashing = PdfHashingAlgorithm::SHA256;
63 PdfSigningService SigningService;
64 nullable<std::chrono::seconds> SigningTimeUTC;
65 PdfSignedHashHandler SignedHashHandler;
66 PdfSignerCmsFlags Flags = PdfSignerCmsFlags::None;
67 };
68 PODOFO_SUPPRESS_DEPRECATED_POP
69
71 {
72 None = 0,
76 AsOctetString = 2,
77 };
78
80 class PODOFO_API PdfSignerCms : public PdfSigner
81 {
82 friend class PdfSigningContext;
83 public:
90 const PdfSignerCmsParams& parameters = { });
91
95 PdfSignerCms(const bufferview& cert, const PdfSignerCmsParams& parameters = { });
96
98
99 private:
101 PdfSignerCms();
102
103 public:
107 void ValidateSignatureDate(const nullable<PdfDate>& date) override;
108 void AppendData(const bufferview& data) override;
109 void ComputeSignature(charbuff& buffer, bool dryrun) override;
110 void FetchIntermediateResult(charbuff& result) override;
111 void ComputeSignatureDeferred(const bufferview& processedResult, charbuff& contents, bool dryrun) override;
112 void Reset() override;
113 std::string GetSignatureFilter() const override;
114 std::string GetSignatureSubFilter() const override;
115 std::string GetSignatureType() const override;
116 bool SkipBufferClear() const override;
117
121 void AddAttribute(const std::string_view& nid, const bufferview& attr, PdfSignatureAttributeFlags flags = PdfSignatureAttributeFlags::None);
122
131 void SetTrustedTimestamp(const bufferview& timeStamp);
132
135 void ReserveAttributeSize(unsigned attrSize);
136
137 public:
138 unsigned GetSignedHashSize() const;
139
140 const PdfSignerCmsParams& GetParameters() const { return m_parameters; }
141
142 private:
143 // Called by PdfSigningContext
144 void Dump(xmlNodePtr signerElem, std::string& temp);
145 void Restore(xmlNodePtr signerElem, charbuff& temp);
146 private:
147 void ensureEventBasedSigning();
148 void ensureDeferredSigning();
149 void checkContextInitialized();
150 void ensureContextInitialized();
151 void resetContext();
152 bool shouldVerify() const;
153 bool shouldVerifyTimeStamp() const;
154 void doSign(const bufferview& input, charbuff& output);
155 void tryEnlargeSignatureContents(charbuff& contents);
156 private:
157 nullable<bool> m_deferredSigning;
158 charbuff m_certificate;
159 std::unique_ptr<CmsContext> m_cmsContext;
160 struct evp_pkey_st* m_privKey;
161 PdfSignerCmsParams m_parameters;
162 unsigned m_reservedSize;
163
164 // Temporary buffer variables
165 // NOTE: Don't clear it in Reset() override
166 charbuff m_encryptedHash;
167 };
168}
169
170ENABLE_BITMASK_OPERATORS(PoDoFo::PdfSignerCmsFlags);
171ENABLE_BITMASK_OPERATORS(PoDoFo::PdfSignatureAttributeFlags);
172
173#endif // PDF_SIGNER_CMS_H
This class computes a CMS signature according to RFC 5652.
Definition PdfSignerCms.h:81
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:71
@ 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...
@ SkipVerification
Skip the validation of the signature date against the validity period of the supplied certificate.