forked from vbuch/node-signpdf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
signpdf.test.js
232 lines (210 loc) · 8.24 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
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,
...params,
};
const pdf = new PDFDocument({
autoFirstPage: true,
size: 'A4',
layout: 'portrait',
bufferPages: true,
});
pdf.info.CreationDate = '';
// Add some content to the page
pdf
.fillColor('#333')
.fontSize(25)
.moveDown()
.text(requestParams.text);
// 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,
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 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;
}
});
});