PoDoFo 1.1.0
Loading...
Searching...
No Matches
PdfSigningContext.h
1
7#ifndef PDF_SIGNING_CONTEXT_H
8#define PDF_SIGNING_CONTEXT_H
9
10#include "PdfSigner.h"
11
12namespace PoDoFo
13{
14 class PODOFO_API PdfSignerId final
15 {
16 public:
17 PdfSignerId();
18 PdfSignerId(const PdfReference& ref, unsigned signerIndex);
19
20 const PdfReference& GetSignatureRef() const { return m_SignatureRef; }
21 unsigned GetSignerIndex() const { return m_SignerIndex; }
22
23 bool operator==(const PdfSignerId& rhs) const
24 {
25 return m_SignatureRef == rhs.m_SignatureRef && m_SignerIndex == rhs.m_SignerIndex;
26 }
27
28 bool operator!=(const PdfSignerId& rhs) const
29 {
30 return m_SignatureRef != rhs.m_SignatureRef || m_SignerIndex != rhs.m_SignerIndex;
31 }
32
33 private:
34 PdfReference m_SignatureRef;
35 unsigned m_SignerIndex;
36 };
37}
38
39namespace std
40{
43 template<>
44 struct hash<PoDoFo::PdfSignerId>
45 {
46 std::size_t operator()(const PoDoFo::PdfSignerId& id) const noexcept
47 {
48 return id.GetSignatureRef().ObjectNumber() ^ (id.GetSignatureRef().GenerationNumber() << 16)
49 ^ ((size_t)id.GetSignerIndex() << 24);
50 }
51 };
52}
53
54namespace PoDoFo
55{
59 struct PODOFO_API PdfSigningResults final
60 {
61 std::unordered_map<PdfSignerId, charbuff> Intermediate;
62 };
63
71 class PODOFO_API PdfSigningContext final
72 {
73 friend PODOFO_API void SignDocument(PdfMemDocument& doc, StreamDevice& device, PdfSigner& signer,
74 PdfSignature& signature, PdfSaveOptions saveOptions);
75 public:
77
80 std::unique_ptr<PdfMemDocument> Restore(std::shared_ptr<StreamDevice> device);
81
84 PdfSignerId AddSigner(const PdfSignature& signature, std::shared_ptr<PdfSigner> signer);
85
88 void Sign(PdfMemDocument& doc, StreamDevice& device, PdfSaveOptions options = PdfSaveOptions::None);
89
93 void StartSigning(PdfMemDocument& doc, std::shared_ptr<StreamDevice> device, PdfSigningResults& results,
94 PdfSaveOptions saveOptions = PdfSaveOptions::None);
95
99 void FinishSigning(const PdfSigningResults& processedResults);
100
105 void DumpInPlace();
106
109 std::shared_ptr<PdfSigner> GetSigner(const PdfReference& signatureRef);
110
113 std::shared_ptr<PdfSigner> GetSigner(const std::string_view& fullName,
115
118 void GetSignerReferences(std::vector<PdfReference>& signatureRefs) const;
119
120 unsigned GetSignerCount() const;
121
122 bool IsEmpty() const;
123
124 private:
125 struct SignerDescriptors
126 {
127 std::string SignatureFullName; // Signature field full name
128 unsigned SignaturePageIndex = 0u - 1u; // Necessary to correctly recover the PdfSignature field
129 unsigned ContextIndex = 0u - 1u; // Index in the context list
130 PdfSigner* Signer;
131 std::shared_ptr<PdfSigner> SignerStorage; // Unnecessary for PoDoFo::SignDocument()
132 };
133
134 // Context data for signing operations
135 struct SignerContext
136 {
137 // Buffer for the final signature /Contents
138 charbuff Contents;
139 size_t BeaconSize = 0;
140 PdfSignatureBeacons Beacons;
141 PdfArray ByteRangeArr;
142 };
143
144 enum class Status
145 {
146 Config,
147 Started,
148 Finished,
149 Dumped,
150 Restored,
151 };
152
153 private:
154 // Used by PoDoFo::SignDocument
155 void AddSignerUnsafe(const PdfSignature& signature, PdfSigner& signer);
156
157 private:
158 PdfSignerId addSigner(const PdfSignature& signature, PdfSigner* signer,
159 std::shared_ptr<PdfSigner>&& storage);
160 void ensureNotStarted() const;
161 std::vector<SignerContext> prepareSignatureContexts(PdfMemDocument& doc, bool deferredSigning);
162 void saveDocForSigning(PdfMemDocument& doc, StreamDevice& device, PdfSaveOptions saveOptions);
163 void appendDataForSigning(PdfMemDocument& doc, StreamDevice& device, std::vector<SignerContext>& contexts,
164 std::unordered_map<PdfSignerId, charbuff>* intermediateResults, charbuff& tmpbuff);
165 void computeSignatures(std::vector<SignerContext>& contexts, PdfMemDocument& doc, StreamDevice& device,
166 const PdfSigningResults* processedResults, charbuff& tmpbuff);
167
168 private:
169 PdfSigningContext(const PdfSigningContext&) = delete;
170 PdfSigningContext& operator==(const PdfSigningContext&) = delete;
171
172 private:
173 std::unordered_map<PdfReference, SignerDescriptors> m_signers;
174 // Used during deferred signing
175 PdfMemDocument* m_doc;
176 std::shared_ptr<StreamDevice> m_device;
177 // Signer contexts used when preparing/finish signing operations
178 std::vector<SignerContext> m_contexts;
179 Status m_status;
180 };
181}
182
183#endif // PDF_SIGNING_CONTEXT_H
This class represents a PdfArray Use it for all arrays that are written to a PDF file.
Definition PdfArray.h:81
PdfMemDocument is the core class for reading and manipulating PDF files and writing them back to disk...
Definition PdfMemDocument.h:38
A reference is a pointer to a object in the PDF file of the form "4 0 R", where 4 is the object numbe...
Definition PdfReference.h:24
A context that can be used to customize the signing process.
Definition PdfSigningContext.h:72
std::shared_ptr< PdfSigner > GetSigner(const std::string_view &fullName, PdfReference &signatureRef)
Get the fist signer from the context for the given input signature.
friend PODOFO_API void SignDocument(PdfMemDocument &doc, StreamDevice &device, PdfSigner &signer, PdfSignature &signature, PdfSaveOptions saveOptions)
Sign the document on the given signature field.
This class provides an output device which operates either on a file or on a buffer in memory.
Definition StreamDevice.h:31
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
PdfSaveOptions
Definition PdfDeclarations.h:469
Interchange signing procedure results.
Definition PdfSigningContext.h:60