forked from vbuch/node-signpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signpdf.test.js
354 lines (321 loc) · 13.4 KB
/
signpdf.test.js
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import PDFDocument from 'pdfkit';
import forge from 'node-forge';
import fs from 'fs';
import signer from './signpdf';
import {pdfkitAddPlaceholder, extractSignature, plainAddPlaceholder} from './helpers';
import SignPdfError from './SignPdfError';
/**
* Creates a Buffer containing a PDF.
* Returns a Promise that is resolved with the resulting Buffer of the PDFDocument.
* @returns {Promise<Buffer>}
*/
const createPdf = (params) => new Promise((resolve) => {
const requestParams = {
placeholder: {},
text: 'node-signpdf',
addSignaturePlaceholder: true,
pages: 1,
...params,
};
const pdf = new PDFDocument({
autoFirstPage: false,
size: 'A4',
layout: 'portrait',
bufferPages: true,
});
pdf.info.CreationDate = '';
if (requestParams.pages < 1) {
requestParams.pages = 1;
}
// Add some content to the page(s)
for (let i = 0; i < requestParams.pages; i += 1) {
pdf
.addPage()
.fillColor('#333')
.fontSize(25)
.moveDown()
.text(requestParams.text)
.save();
}
// Collect the ouput PDF
// and, when done, resolve with it stored in a Buffer
const pdfChunks = [];
pdf.on('data', (data) => {
pdfChunks.push(data);
});
pdf.on('end', () => {
resolve(Buffer.concat(pdfChunks));
});
if (requestParams.addSignaturePlaceholder) {
// Externally (to PDFKit) add the signature placeholder.
const refs = pdfkitAddPlaceholder({
pdf,
pdfBuffer: Buffer.from([pdf]),
reason: 'I am the author',
...requestParams.placeholder,
});
// Externally end the streams of the created objects.
// PDFKit doesn't know much about them, so it won't .end() them.
Object.keys(refs).forEach((key) => refs[key].end());
}
// Also end the PDFDocument stream.
// See pdf.on('end'... on how it is then converted to Buffer.
pdf.end();
});
describe('Test signing', () => {
it('expects PDF to be Buffer', () => {
try {
signer.sign('non-buffer', Buffer.from(''));
expect('here').not.toBe('here');
} catch (e) {
expect(e instanceof SignPdfError).toBe(true);
expect(e.type).toBe(SignPdfError.TYPE_INPUT);
expect(e.message).toMatchSnapshot();
}
});
it('expects P12 certificate to be Buffer', () => {
try {
signer.sign(Buffer.from(''), 'non-buffer');
expect('here').not.toBe('here');
} catch (e) {
expect(e instanceof SignPdfError).toBe(true);
expect(e.type).toBe(SignPdfError.TYPE_INPUT);
expect(e.message).toMatchSnapshot();
}
});
it('expects PDF to contain a ByteRange placeholder', () => {
try {
signer.sign(Buffer.from('No BR placeholder\n%%EOF'), Buffer.from(''));
expect('here').not.toBe('here');
} catch (e) {
expect(e instanceof SignPdfError).toBe(true);
expect(e.type).toBe(SignPdfError.TYPE_PARSE);
expect(e.message).toMatchSnapshot();
}
});
it('expects a reasonably sized placeholder', async () => {
try {
const pdfBuffer = await createPdf({
placeholder: {
signatureLength: 2,
},
});
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`);
signer.sign(pdfBuffer, p12Buffer);
expect('here').not.toBe('here');
} catch (e) {
expect(e instanceof SignPdfError).toBe(true);
expect(e.type).toBe(SignPdfError.TYPE_INPUT);
expect(e.message).toMatchSnapshot();
}
});
it('signs input PDF', async () => {
let pdfBuffer = await createPdf();
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`);
pdfBuffer = signer.sign(pdfBuffer, p12Buffer);
expect(pdfBuffer instanceof Buffer).toBe(true);
const {signature, signedData} = extractSignature(pdfBuffer);
expect(typeof signature === 'string').toBe(true);
expect(signedData instanceof Buffer).toBe(true);
});
it('signs detached', async () => {
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`);
let pdfBuffer = await createPdf({text: 'Some text'});
signer.sign(pdfBuffer, p12Buffer);
const signature1 = signer.lastSignature;
pdfBuffer = await createPdf({text: 'some other text '.repeat(30)});
signer.sign(pdfBuffer, p12Buffer);
const signature2 = signer.lastSignature;
expect(signature1).not.toBe(signature2);
expect(signature1).toHaveLength(signature2.length);
});
it('signs a ready pdf', async () => {
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`);
let pdfBuffer = fs.readFileSync(`${__dirname}/../resources/w3dummy.pdf`);
pdfBuffer = plainAddPlaceholder({
pdfBuffer,
reason: 'I have reviewed it.',
signatureLength: 1612,
});
pdfBuffer = signer.sign(pdfBuffer, p12Buffer);
const {signature, signedData} = extractSignature(pdfBuffer);
expect(typeof signature === 'string').toBe(true);
expect(signedData instanceof Buffer).toBe(true);
});
it('signs a ready pdf that does not have Annots', async () => {
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`);
let pdfBuffer = fs.readFileSync(`${__dirname}/../resources/no-annotations.pdf`);
pdfBuffer = plainAddPlaceholder({
pdfBuffer,
reason: 'I have reviewed it.',
signatureLength: 1612,
});
pdfBuffer = signer.sign(pdfBuffer, p12Buffer);
const {signature, signedData} = extractSignature(pdfBuffer);
expect(typeof signature === 'string').toBe(true);
expect(signedData instanceof Buffer).toBe(true);
});
it('signs a ready pdf that does not have metadata', async () => {
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`);
let pdfBuffer = fs.readFileSync(`${__dirname}/../resources/no-metadata.pdf`);
pdfBuffer = plainAddPlaceholder({
pdfBuffer,
reason: 'I have reviewed it.',
signatureLength: 1612,
});
pdfBuffer = signer.sign(pdfBuffer, p12Buffer);
const {signature, signedData} = extractSignature(pdfBuffer);
expect(typeof signature === 'string').toBe(true);
expect(signedData instanceof Buffer).toBe(true);
});
it('signs a ready pdf two times', async () => {
const secondP12Buffer = fs.readFileSync(`${__dirname}/../resources/withpass.p12`);
let signedPdfBuffer = fs.readFileSync(`${__dirname}/../resources/signed-once.pdf`);
signedPdfBuffer = plainAddPlaceholder({
pdfBuffer: signedPdfBuffer,
reason: 'second',
location: 'test location',
signatureLength: 1592,
});
signedPdfBuffer = signer.sign(signedPdfBuffer, secondP12Buffer, {passphrase: 'node-signpdf'});
const {signature, signedData} = extractSignature(signedPdfBuffer, 2);
expect(typeof signature === 'string').toBe(true);
expect(signedData instanceof Buffer).toBe(true);
});
it('signs big PDF twice producing big AcroForm ID on the first time', async () => {
let pdfBuffer = await createPdf({
pages: 100,
});
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`);
pdfBuffer = signer.sign(pdfBuffer, p12Buffer);
expect(pdfBuffer instanceof Buffer).toBe(true);
const {signature, signedData} = extractSignature(pdfBuffer);
expect(typeof signature === 'string').toBe(true);
expect(signedData instanceof Buffer).toBe(true);
const secondP12Buffer = fs.readFileSync(`${__dirname}/../resources/withpass.p12`);
pdfBuffer = plainAddPlaceholder({
pdfBuffer,
reason: 'second',
location: 'test location',
signatureLength: 1592,
});
pdfBuffer = signer.sign(pdfBuffer, secondP12Buffer, {passphrase: 'node-signpdf'});
expect(pdfBuffer instanceof Buffer).toBe(true);
const {
signature: secondSignature,
signedData: secondSignatureData,
} = extractSignature(pdfBuffer, 2);
expect(typeof secondSignature === 'string').toBe(true);
expect(secondSignatureData instanceof Buffer).toBe(true);
});
it('signs a ready pdf containing a link', async () => {
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`);
let pdfBuffer = fs.readFileSync(`${__dirname}/../resources/including-a-link.pdf`);
pdfBuffer = plainAddPlaceholder({
pdfBuffer,
reason: 'I have reviewed it.',
location: 'some city',
signatureLength: 1612,
});
pdfBuffer = signer.sign(pdfBuffer, p12Buffer);
const {signature, signedData} = extractSignature(pdfBuffer);
expect(typeof signature === 'string').toBe(true);
expect(signedData instanceof Buffer).toBe(true);
});
it('signs with ca, intermediate and multiple certificates bundle', async () => {
let pdfBuffer = await createPdf();
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/bundle.p12`);
pdfBuffer = signer.sign(pdfBuffer, p12Buffer);
expect(pdfBuffer instanceof Buffer).toBe(true);
const {signature, signedData} = extractSignature(pdfBuffer);
expect(typeof signature === 'string').toBe(true);
expect(signedData instanceof Buffer).toBe(true);
});
it('signs with passphrased certificate', async () => {
let pdfBuffer = await createPdf();
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/withpass.p12`);
pdfBuffer = signer.sign(
pdfBuffer,
p12Buffer,
{passphrase: 'node-signpdf'},
);
expect(pdfBuffer instanceof Buffer).toBe(true);
const {signature, signedData} = extractSignature(pdfBuffer);
expect(typeof signature === 'string').toBe(true);
expect(signedData instanceof Buffer).toBe(true);
});
it('errors on wrong certificate passphrase', async () => {
const pdfBuffer = await createPdf();
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/withpass.p12`);
try {
signer.sign(
pdfBuffer,
p12Buffer,
{passphrase: 'Wrong passphrase'},
);
expect('here').not.toBe('here');
} catch (e) {
expect(e instanceof Error).toBe(true);
expect(e.message).toMatchSnapshot();
}
});
it('errors when no matching certificate is found in bags', async () => {
const pdfBuffer = await createPdf();
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/bundle.p12`);
// Monkey-patch pkcs12 to return no matching certificates although bundle.p12 is correct.
const originalPkcs12FromAsn1 = forge.pkcs12.pkcs12FromAsn1;
let p12Instance;
forge.pkcs12.pkcs12FromAsn1 = (...params) => {
// This instance will be used for all non-mocked code.
p12Instance = originalPkcs12FromAsn1(...params);
return {
...p12Instance,
getBags: ({bagType}) => {
if (bagType === forge.pki.oids.certBag) {
// Only mock this case.
// Make sure there will be no matching certificate.
return {
[forge.pki.oids.certBag]: [],
};
}
return p12Instance.getBags({bagType});
},
};
};
try {
signer.sign(
pdfBuffer,
p12Buffer,
);
expect('here').not.toBe('here');
} catch (e) {
expect(e instanceof SignPdfError).toBe(true);
expect(e.type).toBe(SignPdfError.TYPE_INPUT);
expect(e.message).toMatchSnapshot();
} finally {
forge.pkcs12.pkcs12FromAsn1 = originalPkcs12FromAsn1;
}
});
it('error when final key inside trailer dictionary is /Root', async () => {
let pdfBuffer = fs.readFileSync(`${__dirname}/../resources/w3dummy-different-trailer.pdf`);
pdfBuffer = plainAddPlaceholder({
pdfBuffer,
reason: 'I have reviewed it.',
signatureLength: 1612,
});
const trailer = pdfBuffer.slice(pdfBuffer.lastIndexOf('trailer')).toString();
// the trailer should contain only one startxref
expect(trailer.match(/startxref/g).length).toBe(1);
});
it('expects siging to fail because of no byteRangePlaceholder available to sign', async () => {
try {
const pdfBuffer = fs.readFileSync(`${__dirname}/../resources/signed.pdf`);
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`);
signer.sign(pdfBuffer, p12Buffer);
expect('here').not.toBe('here');
} catch (e) {
expect(e instanceof SignPdfError).toBe(true);
expect(e.type).toBe(SignPdfError.TYPE_PARSE);
}
});
});