-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypt_test.go
More file actions
287 lines (266 loc) · 9.77 KB
/
Copy pathcrypt_test.go
File metadata and controls
287 lines (266 loc) · 9.77 KB
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
package pdfdisassembler
import (
"bytes"
"crypto/md5"
"crypto/rc4"
"encoding/hex"
"fmt"
"strings"
"testing"
)
// buildEncryptedPDF constructs a PDF secured with the /Standard handler
// (V2/R3 RC4) whose /Encrypt dict declares the given /Length in bits. /O and
// /U are 32-byte placeholders; the empty-password key derivation runs during
// Open regardless of whether they validate.
func buildEncryptedPDF(t *testing.T, length int) []byte {
t.Helper()
var buf bytes.Buffer
off := func() int { return buf.Len() }
fmt.Fprint(&buf, "%PDF-1.7\n%\xE2\xE3\xCF\xD3\n")
offsets := make([]int, 4) // index 1..3
offsets[1] = off()
fmt.Fprint(&buf, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n")
offsets[2] = off()
fmt.Fprint(&buf, "2 0 obj\n<< /Type /Pages /Count 0 /Kids [] >>\nendobj\n")
o := strings.Repeat("ab", 32) // 32 bytes, hex-encoded
u := strings.Repeat("cd", 32)
offsets[3] = off()
fmt.Fprintf(&buf,
"3 0 obj\n<< /Filter /Standard /V 2 /R 3 /Length %d /O <%s> /U <%s> /P -44 >>\nendobj\n",
length, o, u)
xrefOff := off()
fmt.Fprint(&buf, "xref\n0 4\n")
fmt.Fprintf(&buf, "%010d %05d f \n", 0, 65535)
for i := 1; i <= 3; i++ {
fmt.Fprintf(&buf, "%010d %05d n \n", offsets[i], 0)
}
id := "<00112233445566778899aabbccddeeff>"
fmt.Fprintf(&buf,
"trailer\n<< /Size 4 /Root 1 0 R /Encrypt 3 0 R /ID [%s %s] >>\n", id, id)
fmt.Fprintf(&buf, "startxref\n%d\n%%%%EOF\n", xrefOff)
return buf.Bytes()
}
// A malicious /Encrypt dict can declare a /Length whose key size exceeds the
// 16-byte MD5 digest (or is negative). Open must surface an error, not panic.
func TestEncryptHostileKeyLengthNoPanic(t *testing.T) {
for _, length := range []int{256, 4096, -8} {
t.Run(fmt.Sprintf("length_%d", length), func(t *testing.T) {
data := buildEncryptedPDF(t, length)
if _, err := Open(bytes.NewReader(data)); err == nil {
t.Fatal("expected an error for hostile /Length, got nil")
}
})
}
}
// stdPassPad is the 32-byte padding string from PDF 32000-1:2008 algorithm 2,
// used to build an empty-password V2/R3 fixture.
var stdPassPad = []byte{
0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41,
0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08,
0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80,
0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a,
}
// emptyPwRC4Key derives the V2/R3 file key for the empty user password.
func emptyPwRC4Key(owner, id0 []byte, p int32, bits int) []byte {
h := md5.New()
h.Write(stdPassPad)
h.Write(owner)
h.Write([]byte{byte(uint32(p)), byte(uint32(p) >> 8), byte(uint32(p) >> 16), byte(uint32(p) >> 24)})
h.Write(id0)
sum := h.Sum(nil)
keyLen := bits / 8
for i := 0; i < 50; i++ {
s := md5.Sum(sum[:keyLen])
sum = s[:]
}
key := make([]byte, keyLen)
copy(key, sum[:keyLen])
return key
}
// emptyPwU computes the /U value (algorithm 5, R>=3) for the empty password,
// so Open's password validation accepts the fixture.
func emptyPwU(key, id0 []byte) []byte {
h := md5.New()
h.Write(stdPassPad)
h.Write(id0)
digest := h.Sum(nil)
out := make([]byte, 16)
c, _ := rc4.NewCipher(key)
c.XORKeyStream(out, digest)
for i := 1; i <= 19; i++ {
tweaked := make([]byte, len(key))
for j, b := range key {
tweaked[j] = b ^ byte(i)
}
c2, _ := rc4.NewCipher(tweaked)
c2.XORKeyStream(out, out)
}
u := make([]byte, 32)
copy(u, out)
return u
}
// objKeyRC4 derives the per-object RC4 key (algorithm 1).
func objKeyRC4(fileKey []byte, num, gen int) []byte {
buf := append([]byte{}, fileKey...)
buf = append(buf, byte(num), byte(num>>8), byte(num>>16), byte(gen), byte(gen>>8))
sum := md5.Sum(buf)
n := len(fileKey) + 5
if n > 16 {
n = 16
}
return sum[:n]
}
func rc4Crypt(key, data []byte) []byte {
out := make([]byte, len(data))
c, _ := rc4.NewCipher(key)
c.XORKeyStream(out, data)
return out
}
// assembleEncryptedPDF builds a classical-xref PDF — catalog (1), pages (2),
// the given /Encrypt dict body (3), and an already-encrypted stream (4) — with
// the trailer wired to /Encrypt 3 0 R and /ID [id0 id0].
func assembleEncryptedPDF(encryptBody string, encStream, id0 []byte) []byte {
var buf bytes.Buffer
off := func() int { return buf.Len() }
fmt.Fprint(&buf, "%PDF-1.7\n%\xE2\xE3\xCF\xD3\n")
offsets := make([]int, 5) // 1..4
offsets[1] = off()
fmt.Fprint(&buf, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n")
offsets[2] = off()
fmt.Fprint(&buf, "2 0 obj\n<< /Type /Pages /Count 0 /Kids [] >>\nendobj\n")
offsets[3] = off()
fmt.Fprintf(&buf, "3 0 obj\n%s\nendobj\n", encryptBody)
offsets[4] = off()
fmt.Fprintf(&buf, "4 0 obj\n<< /Length %d >>\nstream\n", len(encStream))
buf.Write(encStream)
fmt.Fprint(&buf, "\nendstream\nendobj\n")
xrefOff := off()
fmt.Fprint(&buf, "xref\n0 5\n")
fmt.Fprintf(&buf, "%010d %05d f \n", 0, 65535)
for i := 1; i <= 4; i++ {
fmt.Fprintf(&buf, "%010d %05d n \n", offsets[i], 0)
}
id := hex.EncodeToString(id0)
fmt.Fprintf(&buf,
"trailer\n<< /Size 5 /Root 1 0 R /Encrypt 3 0 R /ID [<%s> <%s>] >>\n", id, id)
fmt.Fprintf(&buf, "startxref\n%d\n%%%%EOF\n", xrefOff)
return buf.Bytes()
}
// buildRC4EncryptedStreamPDF builds a V2/R3 RC4-encrypted PDF (empty password)
// whose object 4 is a stream carrying RC4-encrypted plaintext.
func buildRC4EncryptedStreamPDF(t *testing.T, plaintext []byte) []byte {
t.Helper()
owner := bytes.Repeat([]byte{0x5a}, 32)
id0 := bytes.Repeat([]byte{0x7c}, 16)
const bits = 128
var p int32 = -44
fileKey := emptyPwRC4Key(owner, id0, p, bits)
u := emptyPwU(fileKey, id0)
enc := rc4Crypt(objKeyRC4(fileKey, 4, 0), plaintext)
body := fmt.Sprintf("<< /Filter /Standard /V 2 /R 3 /Length %d /O <%s> /U <%s> /P %d >>",
bits, hex.EncodeToString(owner), hex.EncodeToString(u), p)
return assembleEncryptedPDF(body, enc, id0)
}
// buildV4RC4EncryptedStreamPDF builds a V4/R4 PDF whose StdCF crypt filter uses
// CFM /V2 (RC4) — same empty-password key derivation as V2/R3, reached through
// the V4 /CF + /StmF parsing path.
func buildV4RC4EncryptedStreamPDF(t *testing.T, plaintext []byte) []byte {
t.Helper()
owner := bytes.Repeat([]byte{0x5a}, 32)
id0 := bytes.Repeat([]byte{0x7c}, 16)
const bits = 128
var p int32 = -44
fileKey := emptyPwRC4Key(owner, id0, p, bits)
u := emptyPwU(fileKey, id0)
enc := rc4Crypt(objKeyRC4(fileKey, 4, 0), plaintext)
body := fmt.Sprintf("<< /Filter /Standard /V 4 /R 4 /Length %d /O <%s> /U <%s> /P %d "+
"/CF << /StdCF << /CFM /V2 /Length 16 >> >> /StmF /StdCF /StrF /StdCF /EncryptMetadata true >>",
bits, hex.EncodeToString(owner), hex.EncodeToString(u), p)
return assembleEncryptedPDF(body, enc, id0)
}
// Open must accept an RC4-encrypted PDF secured with the empty user password
// and decrypt its stream content end-to-end.
func TestOpenDecryptsRC4Stream(t *testing.T) {
plaintext := []byte("BT (top secret invoice) Tj ET")
data := buildRC4EncryptedStreamPDF(t, plaintext)
r, err := Open(bytes.NewReader(data))
if err != nil {
t.Fatalf("Open: %v", err)
}
defer r.Close()
got, err := r.DecodeStream(Reference{Number: 4, Generation: 0})
if err != nil {
t.Fatalf("DecodeStream: %v", err)
}
if !bytes.Equal(got, plaintext) {
t.Fatalf("decrypted stream mismatch:\n got %q\nwant %q", got, plaintext)
}
}
// The V4 path resolves the stream cipher through /CF + /StmF rather than /V
// directly, so it must be exercised end-to-end too.
func TestOpenDecryptsV4RC4Stream(t *testing.T) {
plaintext := []byte("BT (V4 crypt filter) Tj ET")
data := buildV4RC4EncryptedStreamPDF(t, plaintext)
r, err := Open(bytes.NewReader(data))
if err != nil {
t.Fatalf("Open: %v", err)
}
defer r.Close()
got, err := r.DecodeStream(Reference{Number: 4, Generation: 0})
if err != nil {
t.Fatalf("DecodeStream: %v", err)
}
if !bytes.Equal(got, plaintext) {
t.Fatalf("V4 decrypted stream mismatch:\n got %q\nwant %q", got, plaintext)
}
}
// buildPDFWithEncryptObj wraps an arbitrary /Encrypt dict body as object 3 of a
// classical-xref PDF, with the trailer pointing /Encrypt at it.
func buildPDFWithEncryptObj(t *testing.T, encryptBody string) []byte {
t.Helper()
var buf bytes.Buffer
off := func() int { return buf.Len() }
fmt.Fprint(&buf, "%PDF-1.7\n%\xE2\xE3\xCF\xD3\n")
offsets := make([]int, 4)
offsets[1] = off()
fmt.Fprint(&buf, "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n")
offsets[2] = off()
fmt.Fprint(&buf, "2 0 obj\n<< /Type /Pages /Count 0 /Kids [] >>\nendobj\n")
offsets[3] = off()
fmt.Fprintf(&buf, "3 0 obj\n%s\nendobj\n", encryptBody)
xrefOff := off()
fmt.Fprint(&buf, "xref\n0 4\n")
fmt.Fprintf(&buf, "%010d %05d f \n", 0, 65535)
for i := 1; i <= 3; i++ {
fmt.Fprintf(&buf, "%010d %05d n \n", offsets[i], 0)
}
id := "<00112233445566778899aabbccddeeff>"
fmt.Fprintf(&buf, "trailer\n<< /Size 4 /Root 1 0 R /Encrypt 3 0 R /ID [%s %s] >>\n", id, id)
fmt.Fprintf(&buf, "startxref\n%d\n%%%%EOF\n", xrefOff)
return buf.Bytes()
}
// Only the /Standard security handler is supported; any other /Filter must be
// rejected rather than silently treated as unencrypted.
func TestEncryptNonStandardFilterRejected(t *testing.T) {
data := buildPDFWithEncryptObj(t, "<< /Filter /FooSecurity /V 2 /R 3 /Length 128 >>")
if _, err := Open(bytes.NewReader(data)); err == nil {
t.Fatal("expected an error for a non-Standard /Filter, got nil")
}
}
// Malformed /Encrypt dictionaries (wrong type, missing fields, short V5 entries)
// must surface as errors during Open, never panics.
func TestEncryptMalformedNoPanic(t *testing.T) {
cases := map[string]string{
"encrypt not a dict": "42",
"missing V and R": "<< /Filter /Standard >>",
"short O and U": "<< /Filter /Standard /V 2 /R 3 /Length 128 /O <00> /U <00> /P 0 >>",
"v5 short entries": "<< /Filter /Standard /V 5 /R 6 /Length 256 /O <00> /U <00> /OE <00> /UE <00> /Perms <00> /P 0 >>",
}
for name, body := range cases {
t.Run(name, func(t *testing.T) {
if _, err := Open(bytes.NewReader(buildPDFWithEncryptObj(t, body))); err == nil {
t.Fatal("expected an error, got nil")
}
})
}
}