-
Notifications
You must be signed in to change notification settings - Fork 1
/
encryption.go
378 lines (317 loc) · 10.3 KB
/
encryption.go
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package file
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rc4"
"fmt"
"github.com/benoitkugler/pdf/model"
)
// IncorrectPasswordErr is returned when the given password is not correct.
type IncorrectPasswordErr string
func (ip IncorrectPasswordErr) Error() string {
return fmt.Sprintf("incorrect password: %s", string(ip))
}
type encrypt struct {
enc model.Encrypt // found in the PDF file
ID [2]string // found in the PDF file
revision uint8 // cached value of enc.R
key []byte
aesStrings, aesStreams bool
}
// Read the trailer and the Config to build
// the data needed to decode the document
// In particular, authenticates the user provided passwords.
func (ctx *context) setupEncryption() (err error) {
if ctx.trailer.encrypt == nil { // not encrypted
return nil
}
var info encrypt
info.enc, err = ctx.processEncryptDict()
if err != nil {
return err
}
if info.enc.Filter != "Standard" {
return fmt.Errorf("unsupported encryption handler %s", info.enc.Filter)
}
if info.enc.SubFilter != "" {
return fmt.Errorf("unsupported encryption handler SubFilter %s", info.enc.SubFilter)
}
if len(ctx.trailer.id) != 2 {
return fmt.Errorf("invalid ID entry for trailer, expected 2-length string array, got %v", ctx.trailer.id)
}
info.ID[0], _ = IsString(ctx.trailer.id[0])
info.ID[1], _ = IsString(ctx.trailer.id[1])
e, _ := info.enc.EncryptionHandler.(model.EncryptionStandard)
info.revision = e.R
var sh model.SecuriyHandler
// use Revision as default for RC4 vs AES
if e.R == 5 {
info.aesStreams, info.aesStrings = true, true
sh = info.enc.NewAESSecurityHandler(info.ID[0], e.R, e.DontEncryptMetadata)
} else {
sh = info.enc.NewRC4SecurityHandler(info.ID[0], e.R, e.DontEncryptMetadata)
}
var ok bool
info.key, ok = sh.AuthenticatePasswords(ctx.Configuration.Password, ctx.Configuration.Password, e)
if !ok {
return IncorrectPasswordErr(ctx.Configuration.Password)
}
if info.enc.StmF != "" && info.enc.StmF != "Identity" {
d, ok := info.enc.CF[info.enc.StmF]
if !ok {
return fmt.Errorf("missing entry for StmF %s in CF encrypt dict", info.enc.StmF)
}
info.aesStreams, err = isSupportedCryptFilter(d)
if err != nil {
return err
}
}
if info.enc.StrF != "" && info.enc.StrF != "Identity" {
d, ok := info.enc.CF[info.enc.StrF]
if !ok {
return fmt.Errorf("missing entry for StrF %s in CF encrypt dict", info.enc.StrF)
}
info.aesStrings, err = isSupportedCryptFilter(d)
if err != nil {
return err
}
}
ctx.enc = &info
return nil
}
// isSupportedCryptFilter returns true if AES should be used,
// or an error is the fields are invalid
func isSupportedCryptFilter(d model.CrypFilter) (bool, error) {
cfm := d.CFM
if cfm != "" && cfm != "V2" && cfm != "AESV2" && cfm != "AESV3" {
return false, fmt.Errorf("invalid CFM entry %s", cfm)
}
// don't check for d.AuthEvent since :
// If this filter is used as the value of StrF or StmF in the encryption
// dictionary (see Table 20), the conforming reader shall ignore this key
// and behave as if the value is DocOpen.
if l := d.Length; l != 0 && (l < 5 || l > 16) && l != 32 {
return false, fmt.Errorf("invalid Length entry %d", l)
}
return cfm == "AESV2" || cfm == "AESV3", nil
}
// the returned key is 5 to 16 byte long
func decryptKey(key []byte, objNumber, generationNumber int, useAES bool) []byte {
b := append(key,
byte(objNumber), byte(objNumber>>8), byte(objNumber>>16),
byte(generationNumber), byte(generationNumber>>8),
)
if useAES {
b = append(b, "sAlT"...)
}
dk := md5.Sum(b)
l := len(key) + 5
if l < 16 {
return dk[:l]
}
return dk[:]
}
// content may be overwritten
func decryptBytes(content []byte, ref model.ObjIndirectRef, useAES bool, revision uint8, key []byte) ([]byte, error) {
if revision != 5 {
key = decryptKey(key, ref.ObjectNumber, ref.GenerationNumber, useAES)
}
if useAES {
return decryptAESBytes(content, key)
}
return decryptRC4Bytes(content, key)
}
func decryptRC4Bytes(buf, key []byte) ([]byte, error) {
c, _ := rc4.NewCipher(key)
c.XORKeyStream(buf, buf)
return buf, nil
}
func decryptAESBytes(b, key []byte) ([]byte, error) {
if len(b) < aes.BlockSize {
return nil, fmt.Errorf("decryptAESBytes: Ciphertext too short (%d)", len(b))
}
if len(b)%aes.BlockSize != 0 {
return nil, fmt.Errorf("decryptAESBytes: Ciphertext not a multiple of block size (%d)", len(b))
}
cb, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
iv := make([]byte, aes.BlockSize)
copy(iv, b[:aes.BlockSize])
data := b[aes.BlockSize:]
mode := cipher.NewCBCDecrypter(cb, iv)
mode.CryptBlocks(data, data)
// Remove padding.
// Note: For some reason not all AES ciphertexts are padded.
if len(data) > 0 && data[len(data)-1] <= 0x10 {
e := len(data) - int(data[len(data)-1])
data = data[:e]
}
return data, nil
}
// recursively walk through the object and decrypt strings and streams
func (enc *encrypt) decryptObject(o model.Object, contextRef model.ObjIndirectRef) (model.Object, error) {
var err error
switch oT := o.(type) {
case model.ObjHexLiteral: // do the actual decryption
decrypted, err := decryptBytes([]byte(oT), contextRef, enc.aesStrings, enc.revision, enc.key)
if err != nil {
return nil, err
}
o = model.ObjHexLiteral(string(decrypted))
case model.ObjStringLiteral: // do the actual decryption
decrypted, err := decryptBytes([]byte(oT), contextRef, enc.aesStrings, enc.revision, enc.key)
if err != nil {
return nil, err
}
o = model.ObjStringLiteral(string(decrypted))
case model.ObjDict: // recurse
for k, v := range oT {
oT[k], err = enc.decryptObject(v, contextRef)
if err != nil {
return nil, err
}
}
case model.ObjStream: // recurse
argsO, err := enc.decryptObject(oT.Args, contextRef)
if err != nil {
return nil, err
}
content, err := enc.decryptStream(oT.Content, contextRef)
if err != nil {
return nil, err
}
// correct the Length args so that it matches the decrypted content
args := argsO.(model.ObjDict)
args["Length"] = model.ObjInt(len(content))
o = model.ObjStream{Args: args, Content: content}
case model.ObjArray: // recurse
for i, v := range oT {
oT[i], err = enc.decryptObject(v, contextRef)
if err != nil {
return nil, err
}
}
}
return o, nil
}
func (enc *encrypt) decryptStream(content []byte, ref model.ObjIndirectRef) ([]byte, error) {
return decryptBytes(content, ref, enc.aesStreams, enc.revision, enc.key)
}
// used only for the encrypt dict, where all object should probably be direct
func (ctx *context) res(obj model.Object) model.Object {
out, _ := ctx.resolve(obj)
return out
}
func (ctx *context) processEncryptDict() (model.Encrypt, error) {
var out model.Encrypt
encryptO, err := ctx.resolve(ctx.trailer.encrypt)
if err != nil {
return out, err
}
d, _ := encryptO.(model.ObjDict)
out.Filter, _ = ctx.res(d["Filter"]).(model.ObjName)
out.SubFilter, _ = ctx.res(d["SubFilter"]).(model.ObjName)
v, _ := ctx.res(d["V"]).(model.ObjInt)
out.V = model.EncryptionAlgorithm(v)
length, _ := ctx.res(d["Length"]).(model.ObjInt)
if length%8 != 0 {
return out, fmt.Errorf("field Length must be a multiple of 8")
}
// we accept up to 256, which is the limit of the RC4 cipher
if length < 0 || length > 256 {
return out, fmt.Errorf("field Length must be between 0 and 256, got %d", length)
}
out.Length = uint8(length / 8)
cf, _ := ctx.res(d["CF"]).(model.ObjDict)
out.CF = make(map[model.ObjName]model.CrypFilter, len(cf))
for name, c := range cf {
out.CF[model.ObjName(name)] = ctx.processCryptFilter(c)
}
out.StmF, _ = ctx.res(d["StmF"]).(model.ObjName)
out.StrF, _ = ctx.res(d["StrF"]).(model.ObjName)
out.EFF, _ = ctx.res(d["EFF"]).(model.ObjName)
p, _ := ctx.res(d["P"]).(model.ObjInt)
out.P = model.UserPermissions(p)
// subtypes
if out.Filter == "Standard" {
out.EncryptionHandler, err = ctx.processStandardSecurityHandler(d)
if err != nil {
return out, err
}
} else {
out.EncryptionHandler = ctx.processPublicKeySecurityHandler(d)
}
return out, nil
}
func (ctx *context) processStandardSecurityHandler(dict model.ObjDict) (model.EncryptionStandard, error) {
var out model.EncryptionStandard
r_, _ := ctx.res(dict["R"]).(model.ObjInt)
out.R = uint8(r_)
hashLength := 32
if out.R == 5 { // AES
hashLength = 48
}
o, _ := IsString(ctx.res(dict["O"]))
if len(o) != hashLength {
return out, fmt.Errorf("expected %d-length byte string for entry O, got %v", hashLength, o)
}
copy(out.O[:], o)
u, _ := IsString(ctx.res(dict["U"]))
if len(u) != hashLength {
return out, fmt.Errorf("expected %d-length byte string for entry U, got %v", hashLength, u)
}
copy(out.U[:], u)
if out.R == 5 {
ue, _ := IsString(ctx.res(dict["UE"]))
if len(ue) != 32 {
return out, fmt.Errorf("expected %d-length byte string for entry UE, got %v", 32, ue)
}
copy(out.UE[:], ue)
oe, _ := IsString(ctx.res(dict["OE"]))
if len(oe) != 32 {
return out, fmt.Errorf("expected %d-length byte string for entry OE, got %v", 32, oe)
}
copy(out.OE[:], oe)
perms, _ := IsString(ctx.res(dict["Perms"]))
if len(perms) != 16 {
return out, fmt.Errorf("expected %d-length byte string for entry Perms, got %v", 16, perms)
}
copy(out.Perms[:], perms)
}
if meta, ok := ctx.res(dict["EncryptMetadata"]).(model.ObjBool); ok {
out.DontEncryptMetadata = !bool(meta)
}
return out, nil
}
func (ctx *context) processPublicKeySecurityHandler(dict model.ObjDict) model.EncryptionPublicKey {
rec, _ := ctx.res(dict["Recipients"]).(model.ObjArray)
out := make(model.EncryptionPublicKey, len(rec))
for i, re := range rec {
out[i], _ = IsString(ctx.res(re))
}
return out
}
func (ctx *context) processCryptFilter(crypt model.Object) model.CrypFilter {
cryptDict, _ := ctx.res(crypt).(model.ObjDict)
var out model.CrypFilter
out.CFM, _ = ctx.res(cryptDict["CFM"]).(model.ObjName)
out.AuthEvent, _ = ctx.res(cryptDict["AuthEvent"]).(model.ObjName)
l, _ := ctx.res(cryptDict["AuthEvent"]).(model.ObjInt)
out.Length = int(l)
recipients := ctx.res(cryptDict["Recipients"])
if rec, ok := IsString(recipients); ok {
out.Recipients = []string{rec}
} else if ar, ok := recipients.(model.ObjArray); ok {
out.Recipients = make([]string, len(ar))
for i, re := range ar {
out.Recipients[i], _ = IsString(ctx.res(re))
}
}
if enc, ok := ctx.res(cryptDict["EncryptMetadata"]).(model.ObjBool); ok {
out.DontEncryptMetadata = !bool(enc)
}
return out
}