PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfSigningContext.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_SIGNING_CONTEXT_H
5#define PDF_SIGNING_CONTEXT_H
6
7#include "PdfSigner.h"
8
9namespace PoDoFo
10{
11 class PODOFO_API PdfSignerId final
12 {
13 public:
14 PdfSignerId();
15 PdfSignerId(const PdfReference& ref, unsigned signerIndex);
16
17 const PdfReference& GetSignatureRef() const { return m_SignatureRef; }
18 unsigned GetSignerIndex() const { return m_SignerIndex; }
19
20 bool operator==(const PdfSignerId& rhs) const
21 {
22 return m_SignatureRef == rhs.m_SignatureRef && m_SignerIndex == rhs.m_SignerIndex;
23 }
24
25 bool operator!=(const PdfSignerId& rhs) const
26 {
27 return m_SignatureRef != rhs.m_SignatureRef || m_SignerIndex != rhs.m_SignerIndex;
28 }
29
30 private:
31 PdfReference m_SignatureRef;
32 unsigned m_SignerIndex;
33 };
34}
35
36namespace std
37{
39 template<>
40 struct hash<PoDoFo::PdfSignerId>
41 {
42 std::size_t operator()(const PoDoFo::PdfSignerId& id) const noexcept
43 {
44 return id.GetSignatureRef().ObjectNumber() ^ (id.GetSignatureRef().GenerationNumber() << 16)
45 ^ ((size_t)id.GetSignerIndex() << 24);
46 }
47 };
48}
49
50namespace PoDoFo
51{
53 struct PODOFO_API PdfSigningResults final
54 {
55 std::unordered_map<PdfSignerId, charbuff> Intermediate;
56 };
57
63 class PODOFO_API PdfSigningContext final
64 {
65 friend PODOFO_API void SignDocument(PdfMemDocument& doc, StreamDevice& device, PdfSigner& signer,
66 PdfSignature& signature, PdfSaveOptions saveOptions);
67 public:
69
71 std::unique_ptr<PdfMemDocument> Restore(std::shared_ptr<StreamDevice> device);
72
74 PdfSignerId AddSigner(const PdfSignature& signature, std::shared_ptr<PdfSigner> signer);
75
77 void Sign(PdfMemDocument& doc, StreamDevice& device, PdfSaveOptions options = PdfSaveOptions::None);
78
84 void StartSigning(PdfMemDocument& doc, std::shared_ptr<StreamDevice> device, PdfSigningResults& results,
85 PdfSaveOptions saveOptions = PdfSaveOptions::None);
86
89 void FinishSigning(const PdfSigningResults& processedResults);
90
94 void DumpInPlace();
95
97 std::shared_ptr<PdfSigner> GetSigner(const PdfReference& signatureRef);
98
100 std::shared_ptr<PdfSigner> GetSigner(const std::string_view& fullName,
102
104 void GetSignerReferences(std::vector<PdfReference>& signatureRefs) const;
105
106 unsigned GetSignerCount() const;
107
108 bool IsEmpty() const;
109
110 private:
111 struct SignerDescriptors
112 {
113 std::string SignatureFullName; // Signature field full name
114 unsigned SignaturePageIndex = 0u - 1u; // Necessary to correctly recover the PdfSignature field
115 unsigned ContextIndex = 0u - 1u; // Index in the context list
116 PdfSigner* Signer;
117 std::shared_ptr<PdfSigner> SignerStorage; // Unnecessary for PoDoFo::SignDocument()
118 };
119
120 // Context data for signing operations
121 struct SignerContext
122 {
123 // Buffer for the final signature /Contents
124 charbuff Contents;
125 size_t BeaconSize = 0;
126 PdfSignatureBeacons Beacons;
127 PdfArray ByteRangeArr;
128 };
129
130 enum class Status
131 {
132 Config,
133 Started,
134 Finished,
135 Dumped,
136 Restored,
137 };
138
139 private:
140 // Used by PoDoFo::SignDocument
141 void AddSignerUnsafe(const PdfSignature& signature, PdfSigner& signer);
142
143 private:
144 PdfSignerId addSigner(const PdfSignature& signature, PdfSigner* signer,
145 std::shared_ptr<PdfSigner>&& storage);
146 void ensureNotStarted() const;
147 void checkDocument(PdfMemDocument& doc, PdfSaveOptions saveOptions) const;
148 std::vector<SignerContext> prepareSignatureContexts(PdfMemDocument& doc, bool deferredSigning);
149 void saveDocForSigning(PdfMemDocument& doc, StreamDevice& device, PdfSaveOptions saveOptions);
150 void appendDataForSigning(PdfMemDocument& doc, StreamDevice& device, std::vector<SignerContext>& contexts,
151 std::unordered_map<PdfSignerId, charbuff>* intermediateResults, charbuff& tmpbuff);
152 void computeSignatures(std::vector<SignerContext>& contexts, PdfMemDocument& doc, StreamDevice& device,
153 const PdfSigningResults* processedResults, charbuff& tmpbuff);
154
155 private:
156 PdfSigningContext(const PdfSigningContext&) = delete;
157 PdfSigningContext& operator==(const PdfSigningContext&) = delete;
158
159 private:
160 std::unordered_map<PdfReference, SignerDescriptors> m_signers;
161 // Used during deferred signing
162 PdfMemDocument* m_doc;
163 std::shared_ptr<StreamDevice> m_device;
164 // Signer contexts used when preparing/finish signing operations
165 std::vector<SignerContext> m_contexts;
166 Status m_status;
167 };
168}
169
170#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:76
PdfMemDocument is the core class for reading and manipulating PDF files and writing them back to disk...
Definition PdfMemDocument.h:35
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:20
A context that can be used to customize the signing process.
Definition PdfSigningContext.h:64
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:28
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
PdfSaveOptions
Definition PdfDeclarations.h:429
Interchange signing procedure results. Used when starting and finishing a deferred (aka "async") sign...
Definition PdfSigningContext.h:54