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> GetSignerEntry(const PdfReference& signatureRef);
110
113 std::shared_ptr<PdfSigner> GetSignerEntry(const std::string_view& fullName,
115
116 bool IsEmpty() const;
117
118 private:
119 struct SignerDescriptors
120 {
121 std::string SignatureFullName; // Signature field full name
122 unsigned SignaturePageIndex = 0u - 1u; // Necessary to correctly recover the PdfSignature field
123 unsigned ContextIndex = 0u - 1u; // Index in the context list
124 PdfSigner* Signer;
125 std::shared_ptr<PdfSigner> SignerStorage; // Unnecessary for PoDoFo::SignDocument()
126 };
127
128 // Context data for signing operations
129 struct SignerContext
130 {
131 // Buffer for the final signature /Contents
132 charbuff Contents;
133 size_t BeaconSize = 0;
134 PdfSignatureBeacons Beacons;
135 PdfArray ByteRangeArr;
136 };
137
138 enum class Status
139 {
140 Config,
141 Started,
142 Finished,
143 Dumped,
144 Restored,
145 };
146
147 private:
148 // Used by PoDoFo::SignDocument
149 void AddSignerUnsafe(const PdfSignature& signature, PdfSigner& signer);
150
151 private:
152 PdfSignerId addSigner(const PdfSignature& signature, PdfSigner* signer,
153 std::shared_ptr<PdfSigner>&& storage);
154 void ensureNotStarted() const;
155 std::vector<SignerContext> prepareSignatureContexts(PdfMemDocument& doc, bool deferredSigning);
156 void saveDocForSigning(PdfMemDocument& doc, StreamDevice& device, PdfSaveOptions saveOptions);
157 void appendDataForSigning(PdfMemDocument& doc, StreamDevice& device, std::vector<SignerContext>& contexts,
158 std::unordered_map<PdfSignerId, charbuff>* intermediateResults, charbuff& tmpbuff);
159 void computeSignatures(std::vector<SignerContext>& contexts, PdfMemDocument& doc, StreamDevice& device,
160 const PdfSigningResults* processedResults, charbuff& tmpbuff);
161
162 private:
163 PdfSigningContext(const PdfSigningContext&) = delete;
164 PdfSigningContext& operator==(const PdfSigningContext&) = delete;
165
166 private:
167 std::unordered_map<PdfReference, SignerDescriptors> m_signers;
168 // Used during deferred signing
169 PdfMemDocument* m_doc;
170 std::shared_ptr<StreamDevice> m_device;
171 // Signer contexts used when preparing/finish signing operations
172 std::vector<SignerContext> m_contexts;
173 Status m_status;
174 };
175}
176
177#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 > GetSignerEntry(const std::string_view &fullName, PdfReference &signatureRef)
Get the fist signer entry 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