PoDoFo 1.0.0-dev
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 PdfSignerId AddSigner(const PdfSignature& signature, const std::shared_ptr<PdfSigner>& signer);
81
84 void Sign(PdfMemDocument& doc, StreamDevice& device, PdfSaveOptions options = PdfSaveOptions::None);
85
89 void StartSigning(PdfMemDocument& doc, const std::shared_ptr<StreamDevice>& device, PdfSigningResults& results,
90 PdfSaveOptions saveOptions = PdfSaveOptions::None);
91
95 void FinishSigning(const PdfSigningResults& processedResults);
96
97 private:
98 struct SignatureAttrs
99 {
100 unsigned PageIndex = 0u - 1u; // Necessary to correctly recover the PdfSignature field
101 std::vector<PdfSigner*> Signers;
102 std::vector<std::shared_ptr<PdfSigner>> SignersStorage; // Unnecessary for PoDoFo::SignDocument()
103 };
104
105 struct SignatureCtx
106 {
107 charbuff Contents;
108 size_t BeaconSize = 0;
109 PdfSignatureBeacons Beacons;
110 PdfArray ByteRangeArr;
111 };
112
113 private:
114 // Used by PoDoFo::SignDocument
115 void AddSignerUnsafe(const PdfSignature& signature, PdfSigner& signer);
116
117 private:
118 PdfSignerId addSigner(const PdfSignature& signature, PdfSigner* signer,
119 const std::shared_ptr<PdfSigner>& storage);
120 void ensureNotStarted() const;
121 std::unordered_map<PdfSignerId, SignatureCtx> prepareSignatureContexts(PdfDocument& doc, bool deferredSigning);
122 void saveDocForSigning(PdfMemDocument& doc, StreamDevice& device, PdfSaveOptions saveOptions);
123 void appendDataForSigning(std::unordered_map<PdfSignerId, SignatureCtx>& contexts, StreamDevice& device,
124 std::unordered_map<PdfSignerId, charbuff>* intermediateResults, charbuff& tmpbuff);
125 void computeSignatures(std::unordered_map<PdfSignerId, SignatureCtx>& contexts,
126 PdfDocument& doc, StreamDevice& device,
127 const PdfSigningResults* processedResults, charbuff& tmpbuff);
128
129 private:
130 PdfSigningContext(const PdfSigningContext&) = delete;
131 PdfSigningContext& operator==(const PdfSigningContext&) = delete;
132
133 private:
134 std::unordered_map<PdfReference, SignatureAttrs> m_signers;
135 // Used during deferred signing
136 PdfMemDocument* m_doc;
137 std::shared_ptr<StreamDevice> m_device;
138 std::unordered_map<PdfSignerId, SignatureCtx> m_contexts;
139 };
140}
141
142#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 context that can be used to customize the signing process.
Definition PdfSigningContext.h:72
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