1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use crate::PDFSigningDocument;
use crate::{error::Error, UserSignatureInfo};
use chrono::Utc;
use lopdf::ObjectId;
impl PDFSigningDocument {
// Change the signature to add extra info about the signing application
pub(crate) fn add_general_info_to_signature(
&mut self,
signature_obj_id: ObjectId,
new_appearance_id: ObjectId,
user_signature_info: &UserSignatureInfo,
_signature_name: &str,
) -> Result<(), Error> {
use lopdf::{Object::*, StringFormat};
let _root_obj_id = self
.raw_document
.get_prev_documents()
.trailer
.get(b"Root")?
.as_reference()?;
// Update Annot to include image
// Update `AP` in `Kids`
// This code assumes that the objects in Kids are References.
// Get list of `Kids` ObjectIDs
let form_dict = self
.raw_document
.get_prev_documents()
.get_object(signature_obj_id)?
.as_dict()?;
let kids = if form_dict.has(b"Kids") {
Some(form_dict.get(b"Kids")?.as_array()?)
} else {
None
};
// Convert `Option<&Vec<Object>>` to `Option<Vec<ObjectId>>`
let kids: Option<Vec<ObjectId>> = match kids {
Some(list) => {
let mut new_list = vec![];
for obj in list {
new_list.push(obj.as_reference()?);
}
Some(new_list)
}
None => None,
};
// TODO if kids is `None` we have to create it.
if kids.is_none() {
log::error!("Unimplemented state: `Kids` entry is missing in Signature.");
}
let kids = kids.unwrap_or_default();
// Loop over Kids that are `Annot`.
let mut found_and_replace_appearance = false;
for child_obj_id in kids {
let child_dict = self
.raw_document
.get_prev_documents()
.get_object(child_obj_id)?
.as_dict()?;
if child_dict.get(b"Type")?.as_name_str()? == "Annot" {
// Copy child to new incremental update
self.raw_document
.opt_clone_object_to_new_document(child_obj_id)?;
let child_dict_mut = self
.raw_document
.new_document
.get_object_mut(child_obj_id)?
.as_dict_mut()?;
child_dict_mut.set(
"AP",
lopdf::Object::Dictionary(lopdf::Dictionary::from_iter(vec![(
"N",
lopdf::Object::Reference(new_appearance_id),
)])),
);
// TODO Set the `F` value to 132: For docs see page 385.
// This will `Locked` and `Print`
found_and_replace_appearance = true;
}
}
if !found_and_replace_appearance {
log::error!("None of the `Kids` are of type `Annot`.");
}
// Update `V` tag in `FT = Sig`
self.raw_document
.opt_clone_object_to_new_document(signature_obj_id)?;
// The `sign_dict.extend(` is bugged see: lopdf issue #120
// TODO: Implementing locking of file
// sign_dict.set(
// "Lock",
// Dictionary(lopdf::Dictionary::from_iter(vec![
// ("Type", Name("SigFieldLock".as_bytes().to_vec())),
// ("Action", Name("All".as_bytes().to_vec())),
// ])),
// );
// Get system time in UTC
let now = Utc::now();
let v_dictionary = Dictionary(lopdf::Dictionary::from_iter(vec![
("Type", Name("Sig".as_bytes().to_vec())),
("Filter", Name("Adobe.PPKLite".as_bytes().to_vec())),
("SubFilter", Name("adbe.pkcs7.detached".as_bytes().to_vec())),
// The order of `ByteRange` and `Contents` is important.
// They should not be moved or switched in ordering.
(
"ByteRange", // Set default value. This will later be filled in.
Array(vec![
Integer(0),
Integer(10000), // byte of `<`
Integer(20000), // Byte of char after `>`
Integer(10000), // until end of file
]),
),
(
"Contents", // Will be filled in later
String(vec![0u8; 9000], StringFormat::Hexadecimal),
),
(
"M",
String(
now.format("D:%Y%m%d%H%M%S+00'00'")
.to_string()
.as_bytes()
.to_vec(),
StringFormat::Literal,
),
),
(
"Name",
String(
user_signature_info.user_name.as_bytes().to_vec(),
StringFormat::Literal,
),
),
(
"Prob_Build",
Dictionary(lopdf::Dictionary::from_iter(vec![
(
"Filter",
Dictionary(lopdf::Dictionary::from_iter(vec![
("Name", Name("Adobe.PPKLite".as_bytes().to_vec())),
(
"Date",
String(
"Mar 2 2022 20:56:26".as_bytes().to_vec(),
StringFormat::Literal,
),
), // TODO: Date of build, no Cargo Env variable for this
("R", Integer(0x0000_0001_0101)), // TODO encode version number, in hex
])),
),
(
"PubSec",
Dictionary(lopdf::Dictionary::from_iter(vec![
(
"Date",
String(
"Mar 2 2022 20:56:26".as_bytes().to_vec(),
StringFormat::Literal,
),
), // TODO
("R", Integer(0x0000_0001_0101)), // TODO encode version number, in hex
("NonEFontNoWarn", Boolean(true)),
])),
),
(
"App",
Dictionary(lopdf::Dictionary::from_iter(vec![
("Name", Name("Rust PDF Signing".as_bytes().to_vec())),
("R", Integer(0x0000_0001_0001)), // TODO encode version number, in hex
(
"OS",
Array(vec![Name(std::env::consts::OS.as_bytes().to_vec())]),
),
(
"REx",
String(
env!("CARGO_PKG_VERSION").as_bytes().to_vec(),
StringFormat::Literal,
),
), // Semversion number
])),
),
])),
),
// (
// "Reference",
// Array(vec![Dictionary(lopdf::Dictionary::from_iter(vec![
// ("Type", Name("SigRef".as_bytes().to_vec())),
// ("TransformMethod", Name("FieldMDP".as_bytes().to_vec())), // TODO: maybe DocMDP?
// (
// "TransformParams",
// Dictionary(lopdf::Dictionary::from_iter(vec![
// ("Type", Name("TransformParams".as_bytes().to_vec())),
// ("Action", Name("Include".as_bytes().to_vec())),
// (
// "Fields",
// Array(vec![String(
// signature_name.as_bytes().to_vec(),
// StringFormat::Literal,
// )]),
// ),
// ("V", Name("1.2".as_bytes().to_vec())),
// ])),
// ),
// ("Data", Reference(root_obj_id)), // Signature is over whole document
// ("DigestMethod", Name("SHA1".as_bytes().to_vec())),
// (
// "DigestValue",
// String(vec![0u8; 20], StringFormat::Hexadecimal),
// ), // TODO: Sha1 = 20 bytes
// ("DigestLocation", Array(vec![Integer(1500), Integer(34)])), // TODO
// ]))]),
// ),
]));
// Add `V` as new object
let v_ref = self.raw_document.new_document.add_object(v_dictionary);
let sign_dict = self
.raw_document
.new_document
.get_object_mut(signature_obj_id)?
.as_dict_mut()?;
sign_dict.set("V", Reference(v_ref));
Ok(())
}
}