Coverage Report

Created: 2023-06-07 06:20

/src/mupdf/source/pdf/pdf-crypt.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright (C) 2004-2021 Artifex Software, Inc.
2
//
3
// This file is part of MuPDF.
4
//
5
// MuPDF is free software: you can redistribute it and/or modify it under the
6
// terms of the GNU Affero General Public License as published by the Free
7
// Software Foundation, either version 3 of the License, or (at your option)
8
// any later version.
9
//
10
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13
// details.
14
//
15
// You should have received a copy of the GNU Affero General Public License
16
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17
//
18
// Alternative licensing terms are available from the licensor.
19
// For commercial licensing, see <https://www.artifex.com/> or contact
20
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21
// CA 94129, USA, for further information.
22
23
#include "mupdf/fitz.h"
24
#include "mupdf/pdf.h"
25
26
#include <string.h>
27
28
enum
29
{
30
  PDF_CRYPT_NONE,
31
  PDF_CRYPT_RC4,
32
  PDF_CRYPT_AESV2,
33
  PDF_CRYPT_AESV3,
34
  PDF_CRYPT_UNKNOWN,
35
};
36
37
typedef struct
38
{
39
  int method;
40
  int length;
41
} pdf_crypt_filter;
42
43
struct pdf_crypt
44
{
45
  pdf_obj *id;
46
47
  int v;
48
  int length;
49
  pdf_obj *cf;
50
  pdf_crypt_filter stmf;
51
  pdf_crypt_filter strf;
52
53
  int r;
54
  unsigned char o[48];
55
  unsigned char u[48];
56
  unsigned char oe[32];
57
  unsigned char ue[32];
58
  unsigned char perms[16];
59
  int p;
60
  int encrypt_metadata;
61
62
  unsigned char key[32]; /* decryption key generated from password */
63
};
64
65
static void pdf_parse_crypt_filter(fz_context *ctx, pdf_crypt_filter *cf, pdf_crypt *crypt, pdf_obj *name);
66
67
pdf_crypt *
68
pdf_new_crypt(fz_context *ctx, pdf_obj *dict, pdf_obj *id)
69
236
{
70
236
  pdf_crypt *crypt;
71
236
  pdf_obj *obj;
72
73
236
  crypt = fz_malloc_struct(ctx, pdf_crypt);
74
75
  /* Common to all security handlers (PDF 1.7 table 3.18) */
76
77
236
  obj = pdf_dict_get(ctx, dict, PDF_NAME(Filter));
78
236
  if (!pdf_is_name(ctx, obj))
79
1
  {
80
1
    pdf_drop_crypt(ctx, crypt);
81
1
    fz_throw(ctx, FZ_ERROR_GENERIC, "unspecified encryption handler");
82
1
  }
83
235
  if (!pdf_name_eq(ctx, PDF_NAME(Standard), obj))
84
1
  {
85
1
    pdf_drop_crypt(ctx, crypt);
86
1
    fz_throw(ctx, FZ_ERROR_GENERIC, "unknown encryption handler: '%s'", pdf_to_name(ctx, obj));
87
1
  }
88
89
234
  crypt->v = 0;
90
234
  obj = pdf_dict_get(ctx, dict, PDF_NAME(V));
91
234
  if (pdf_is_int(ctx, obj))
92
223
    crypt->v = pdf_to_int(ctx, obj);
93
234
  if (crypt->v != 0 && crypt->v != 1 && crypt->v != 2 && crypt->v != 4 && crypt->v != 5)
94
1
  {
95
1
    pdf_drop_crypt(ctx, crypt);
96
1
    fz_throw(ctx, FZ_ERROR_GENERIC, "unknown encryption version");
97
1
  }
98
99
  /* Standard security handler (PDF 1.7 table 3.19) */
100
101
233
  obj = pdf_dict_get(ctx, dict, PDF_NAME(R));
102
233
  if (pdf_is_int(ctx, obj))
103
221
    crypt->r = pdf_to_int(ctx, obj);
104
12
  else if (crypt->v <= 4)
105
11
  {
106
11
    fz_warn(ctx, "encryption dictionary missing revision value, guessing...");
107
11
    if (crypt->v < 2)
108
2
      crypt->r = 2;
109
9
    else if (crypt->v == 2)
110
1
      crypt->r = 3;
111
8
    else if (crypt->v == 4)
112
8
      crypt->r = 4;
113
11
  }
114
1
  else
115
1
  {
116
1
    pdf_drop_crypt(ctx, crypt);
117
1
    fz_throw(ctx, FZ_ERROR_GENERIC, "encryption dictionary missing version and revision value");
118
1
  }
119
232
  if (crypt->r < 1 || crypt->r > 6)
120
0
  {
121
0
    int r = crypt->r;
122
0
    pdf_drop_crypt(ctx, crypt);
123
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "unknown crypt revision %d", r);
124
0
  }
125
126
232
  obj = pdf_dict_get(ctx, dict, PDF_NAME(O));
127
232
  if (pdf_is_string(ctx, obj) && pdf_to_str_len(ctx, obj) == 32)
128
140
    memcpy(crypt->o, pdf_to_str_buf(ctx, obj), 32);
129
  /* /O and /U are supposed to be 48 bytes long for revision 5 and 6, they're often longer, though */
130
92
  else if (crypt->r >= 5 && pdf_is_string(ctx, obj) && pdf_to_str_len(ctx, obj) >= 48)
131
88
    memcpy(crypt->o, pdf_to_str_buf(ctx, obj), 48);
132
4
  else
133
4
  {
134
4
    pdf_drop_crypt(ctx, crypt);
135
4
    fz_throw(ctx, FZ_ERROR_GENERIC, "encryption dictionary missing owner password");
136
4
  }
137
138
228
  obj = pdf_dict_get(ctx, dict, PDF_NAME(U));
139
228
  if (pdf_is_string(ctx, obj) && pdf_to_str_len(ctx, obj) == 32)
140
136
    memcpy(crypt->u, pdf_to_str_buf(ctx, obj), 32);
141
  /* /O and /U are supposed to be 48 bytes long for revision 5 and 6, they're often longer, though */
142
92
  else if (crypt->r >= 5 && pdf_is_string(ctx, obj) && pdf_to_str_len(ctx, obj) >= 48)
143
82
    memcpy(crypt->u, pdf_to_str_buf(ctx, obj), 48);
144
10
  else if (pdf_is_string(ctx, obj) && pdf_to_str_len(ctx, obj) < 32)
145
7
  {
146
7
    fz_warn(ctx, "encryption password key too short (%zu)", pdf_to_str_len(ctx, obj));
147
7
    memcpy(crypt->u, pdf_to_str_buf(ctx, obj), pdf_to_str_len(ctx, obj));
148
7
  }
149
3
  else
150
3
  {
151
3
    pdf_drop_crypt(ctx, crypt);
152
3
    fz_throw(ctx, FZ_ERROR_GENERIC, "encryption dictionary missing user password");
153
3
  }
154
155
225
  obj = pdf_dict_get(ctx, dict, PDF_NAME(P));
156
225
  if (pdf_is_int(ctx, obj))
157
215
    crypt->p = pdf_to_int(ctx, obj);
158
10
  else
159
10
  {
160
10
    fz_warn(ctx, "encryption dictionary missing permissions");
161
10
    crypt->p = 0xfffffffc;
162
10
  }
163
164
225
  if (crypt->r == 5 || crypt->r == 6)
165
87
  {
166
87
    obj = pdf_dict_get(ctx, dict, PDF_NAME(OE));
167
87
    if (!pdf_is_string(ctx, obj) || pdf_to_str_len(ctx, obj) != 32)
168
3
    {
169
3
      pdf_drop_crypt(ctx, crypt);
170
3
      fz_throw(ctx, FZ_ERROR_GENERIC, "encryption dictionary missing owner encryption key");
171
3
    }
172
84
    memcpy(crypt->oe, pdf_to_str_buf(ctx, obj), 32);
173
174
84
    obj = pdf_dict_get(ctx, dict, PDF_NAME(UE));
175
84
    if (!pdf_is_string(ctx, obj) || pdf_to_str_len(ctx, obj) != 32)
176
1
    {
177
1
      pdf_drop_crypt(ctx, crypt);
178
1
      fz_throw(ctx, FZ_ERROR_GENERIC, "encryption dictionary missing user encryption key");
179
1
    }
180
83
    memcpy(crypt->ue, pdf_to_str_buf(ctx, obj), 32);
181
83
  }
182
183
221
  crypt->encrypt_metadata = 1;
184
221
  obj = pdf_dict_get(ctx, dict, PDF_NAME(EncryptMetadata));
185
221
  if (pdf_is_bool(ctx, obj))
186
14
    crypt->encrypt_metadata = pdf_to_bool(ctx, obj);
187
188
  /* Extract file identifier string */
189
190
221
  if (pdf_is_array(ctx, id) && pdf_array_len(ctx, id) == 2)
191
180
  {
192
180
    obj = pdf_array_get(ctx, id, 0);
193
180
    if (pdf_is_string(ctx, obj))
194
180
      crypt->id = pdf_keep_obj(ctx, obj);
195
180
  }
196
41
  else
197
41
    fz_warn(ctx, "missing file identifier, may not be able to do decryption");
198
199
  /* Determine encryption key length */
200
201
221
  crypt->length = 40;
202
221
  if (crypt->v == 2 || crypt->v == 4)
203
111
  {
204
111
    obj = pdf_dict_get(ctx, dict, PDF_NAME(Length));
205
111
    if (pdf_is_int(ctx, obj))
206
105
      crypt->length = pdf_to_int(ctx, obj);
207
208
    /* work-around for pdf generators that assume length is in bytes */
209
111
    if (crypt->length < 40)
210
0
      crypt->length = crypt->length * 8;
211
212
111
    if (crypt->length % 8 != 0)
213
1
    {
214
1
      pdf_drop_crypt(ctx, crypt);
215
1
      fz_throw(ctx, FZ_ERROR_GENERIC, "invalid encryption key length");
216
1
    }
217
110
    if (crypt->length < 40 || crypt->length > 128)
218
1
    {
219
1
      pdf_drop_crypt(ctx, crypt);
220
1
      fz_throw(ctx, FZ_ERROR_GENERIC, "invalid encryption key length");
221
1
    }
222
110
  }
223
224
219
  if (crypt->v == 5)
225
77
    crypt->length = 256;
226
227
219
  if (crypt->v == 0 || crypt->v == 1 || crypt->v == 2)
228
39
  {
229
39
    crypt->stmf.method = PDF_CRYPT_RC4;
230
39
    crypt->stmf.length = crypt->length;
231
232
39
    crypt->strf.method = PDF_CRYPT_RC4;
233
39
    crypt->strf.length = crypt->length;
234
39
  }
235
236
219
  if (crypt->v == 4 || crypt->v == 5)
237
180
  {
238
180
    crypt->stmf.method = PDF_CRYPT_NONE;
239
180
    crypt->stmf.length = crypt->length;
240
241
180
    crypt->strf.method = PDF_CRYPT_NONE;
242
180
    crypt->strf.length = crypt->length;
243
244
180
    obj = pdf_dict_get(ctx, dict, PDF_NAME(CF));
245
180
    if (pdf_is_dict(ctx, obj))
246
160
    {
247
160
      crypt->cf = pdf_keep_obj(ctx, obj);
248
160
    }
249
20
    else
250
20
    {
251
20
      crypt->cf = NULL;
252
20
    }
253
254
360
    fz_try(ctx)
255
360
    {
256
180
      obj = pdf_dict_get(ctx, dict, PDF_NAME(StmF));
257
180
      if (pdf_is_name(ctx, obj))
258
178
        pdf_parse_crypt_filter(ctx, &crypt->stmf, crypt, obj);
259
260
180
      obj = pdf_dict_get(ctx, dict, PDF_NAME(StrF));
261
180
      if (pdf_is_name(ctx, obj))
262
174
        pdf_parse_crypt_filter(ctx, &crypt->strf, crypt, obj);
263
180
    }
264
360
    fz_catch(ctx)
265
4
    {
266
4
      pdf_drop_crypt(ctx, crypt);
267
4
      fz_rethrow(ctx);
268
4
    }
269
270
    /* in crypt revision 4, the crypt filter determines the key length */
271
176
    if (crypt->strf.method != PDF_CRYPT_NONE)
272
150
      crypt->length = crypt->stmf.length;
273
176
  }
274
275
215
  return crypt;
276
219
}
277
278
void
279
pdf_drop_crypt(fz_context *ctx, pdf_crypt *crypt)
280
14.7k
{
281
14.7k
  if (!crypt)
282
14.5k
    return;
283
284
236
  pdf_drop_obj(ctx, crypt->id);
285
236
  pdf_drop_obj(ctx, crypt->cf);
286
236
  fz_free(ctx, crypt);
287
236
}
288
289
/*
290
 * Parse a CF dictionary entry (PDF 1.7 table 3.22)
291
 */
292
293
static void
294
pdf_parse_crypt_filter(fz_context *ctx, pdf_crypt_filter *cf, pdf_crypt *crypt, pdf_obj *name)
295
352
{
296
352
  pdf_obj *obj;
297
352
  pdf_obj *dict;
298
352
  int is_identity = (pdf_name_eq(ctx, name, PDF_NAME(Identity)));
299
352
  int is_stdcf = (!is_identity && pdf_name_eq(ctx, name, PDF_NAME(StdCF)));
300
301
352
  if (!is_identity && !is_stdcf)
302
1
    fz_throw(ctx, FZ_ERROR_GENERIC, "Crypt Filter not Identity or StdCF (%d 0 R)", pdf_to_num(ctx, crypt->cf));
303
304
351
  cf->method = PDF_CRYPT_NONE;
305
351
  cf->length = crypt->length;
306
307
351
  if (!crypt->cf)
308
39
  {
309
39
    cf->method = (is_identity ? PDF_CRYPT_NONE : PDF_CRYPT_RC4);
310
39
    return;
311
39
  }
312
313
312
  dict = pdf_dict_get(ctx, crypt->cf, name);
314
312
  if (pdf_is_dict(ctx, dict))
315
302
  {
316
302
    obj = pdf_dict_get(ctx, dict, PDF_NAME(CFM));
317
302
    if (pdf_is_name(ctx, obj))
318
296
    {
319
296
      if (pdf_name_eq(ctx, PDF_NAME(None), obj))
320
0
        cf->method = PDF_CRYPT_NONE;
321
296
      else if (pdf_name_eq(ctx, PDF_NAME(V2), obj))
322
0
        cf->method = PDF_CRYPT_RC4;
323
296
      else if (pdf_name_eq(ctx, PDF_NAME(AESV2), obj))
324
168
        cf->method = PDF_CRYPT_AESV2;
325
128
      else if (pdf_name_eq(ctx, PDF_NAME(AESV3), obj))
326
103
        cf->method = PDF_CRYPT_AESV3;
327
25
      else
328
25
        fz_warn(ctx, "unknown encryption method: %s", pdf_to_name(ctx, obj));
329
296
    }
330
331
302
    obj = pdf_dict_get(ctx, dict, PDF_NAME(Length));
332
302
    if (pdf_is_int(ctx, obj))
333
276
      cf->length = pdf_to_int(ctx, obj);
334
302
  }
335
10
  else if (!is_identity)
336
1
    fz_throw(ctx, FZ_ERROR_GENERIC, "cannot parse crypt filter (%d 0 R)", pdf_to_num(ctx, crypt->cf));
337
338
311
  if (cf->method != PDF_CRYPT_NONE)
339
271
  {
340
271
    if (crypt->r == 4 && cf->method != PDF_CRYPT_RC4 && cf->method != PDF_CRYPT_AESV2)
341
2
      fz_warn(ctx, "unexpected encryption method for revision 4 crypto: %s", pdf_crypt_method(ctx, crypt));
342
269
    else if (crypt->r >= 5 && cf->method != PDF_CRYPT_AESV3)
343
0
    {
344
0
      fz_warn(ctx, "illegal encryption method for revision 5/6, assuming AESV3");
345
0
      cf->method = PDF_CRYPT_AESV3;
346
0
    }
347
271
  }
348
349
  /* the length for crypt filters is supposed to be in bytes not bits */
350
311
  if (cf->length < 40)
351
276
    cf->length = cf->length * 8;
352
353
311
  if ((cf->length % 8) != 0)
354
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "invalid key length: %d", cf->length);
355
356
311
  if ((crypt->r == 1 || crypt->r == 2 || crypt->r == 3 || crypt->r == 4) &&
357
311
    (cf->length < 40 || cf->length > 128))
358
2
    fz_throw(ctx, FZ_ERROR_GENERIC, "invalid key length: %d", cf->length);
359
309
  if ((crypt->r == 5 || crypt->r == 6) && cf->length != 256)
360
2
  {
361
2
    fz_warn(ctx, "illegal key length for revision 5/6, assuming 256 bits");
362
2
    cf->length = 256;
363
2
  }
364
309
}
365
366
/*
367
 * Compute an encryption key (PDF 1.7 algorithm 3.2)
368
 */
369
370
static const unsigned char padding[32] =
371
{
372
  0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41,
373
  0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08,
374
  0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80,
375
  0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a
376
};
377
378
static void
379
pdf_compute_encryption_key(fz_context *ctx, pdf_crypt *crypt, unsigned char *password, size_t pwlen, unsigned char *key)
380
353
{
381
353
  unsigned char buf[32];
382
353
  unsigned int p;
383
353
  int i, n;
384
353
  fz_md5 md5;
385
386
353
  n = fz_clampi(crypt->length / 8, 0, 16);
387
388
  /* Step 1 - copy and pad password string */
389
353
  if (pwlen > 32)
390
0
    pwlen = 32;
391
353
  memcpy(buf, password, pwlen);
392
353
  if (pwlen < 32)
393
219
    memcpy(buf + pwlen, padding, 32 - pwlen);
394
395
  /* Step 2 - init md5 and pass value of step 1 */
396
353
  fz_md5_init(&md5);
397
353
  fz_md5_update(&md5, buf, 32);
398
399
  /* Step 3 - pass O value */
400
353
  fz_md5_update(&md5, crypt->o, 32);
401
402
  /* Step 4 - pass P value as unsigned int, low-order byte first */
403
353
  p = (unsigned int) crypt->p;
404
353
  buf[0] = (p) & 0xFF;
405
353
  buf[1] = (p >> 8) & 0xFF;
406
353
  buf[2] = (p >> 16) & 0xFF;
407
353
  buf[3] = (p >> 24) & 0xFF;
408
353
  fz_md5_update(&md5, buf, 4);
409
410
  /* Step 5 - pass first element of ID array */
411
353
  fz_md5_update(&md5, (unsigned char *)pdf_to_str_buf(ctx, crypt->id), pdf_to_str_len(ctx, crypt->id));
412
413
  /* Step 6 (revision 4 or greater) - if metadata is not encrypted pass 0xFFFFFFFF */
414
353
  if (crypt->r >= 4)
415
256
  {
416
256
    if (!crypt->encrypt_metadata)
417
18
    {
418
18
      buf[0] = 0xFF;
419
18
      buf[1] = 0xFF;
420
18
      buf[2] = 0xFF;
421
18
      buf[3] = 0xFF;
422
18
      fz_md5_update(&md5, buf, 4);
423
18
    }
424
256
  }
425
426
  /* Step 7 - finish the hash */
427
353
  fz_md5_final(&md5, buf);
428
429
  /* Step 8 (revision 3 or greater) - do some voodoo 50 times */
430
353
  if (crypt->r >= 3)
431
268
  {
432
13.6k
    for (i = 0; i < 50; i++)
433
13.4k
    {
434
13.4k
      fz_md5_init(&md5);
435
13.4k
      fz_md5_update(&md5, buf, n);
436
13.4k
      fz_md5_final(&md5, buf);
437
13.4k
    }
438
268
  }
439
440
  /* Step 9 - the key is the first 'n' bytes of the result */
441
353
  memcpy(key, buf, n);
442
353
}
443
444
/*
445
 * Compute an encryption key (PDF 1.7 ExtensionLevel 3 algorithm 3.2a)
446
 */
447
448
static void
449
pdf_compute_encryption_key_r5(fz_context *ctx, pdf_crypt *crypt, unsigned char *password, size_t pwlen, int ownerkey, unsigned char *validationkey)
450
0
{
451
0
  unsigned char buffer[128 + 8 + 48];
452
0
  fz_sha256 sha256;
453
0
  fz_aes aes;
454
455
  /* Step 2 - truncate UTF-8 password to 127 characters */
456
457
0
  if (pwlen > 127)
458
0
    pwlen = 127;
459
460
  /* Step 3/4 - test password against owner/user key and compute encryption key */
461
462
0
  memcpy(buffer, password, pwlen);
463
0
  if (ownerkey)
464
0
  {
465
0
    memcpy(buffer + pwlen, crypt->o + 32, 8);
466
0
    memcpy(buffer + pwlen + 8, crypt->u, 48);
467
0
  }
468
0
  else
469
0
    memcpy(buffer + pwlen, crypt->u + 32, 8);
470
471
0
  fz_sha256_init(&sha256);
472
0
  fz_sha256_update(&sha256, buffer, pwlen + 8 + (ownerkey ? 48 : 0));
473
0
  fz_sha256_final(&sha256, validationkey);
474
475
  /* Step 3.5/4.5 - compute file encryption key from OE/UE */
476
477
0
  if (ownerkey)
478
0
  {
479
0
    memcpy(buffer + pwlen, crypt->o + 40, 8);
480
0
    memcpy(buffer + pwlen + 8, crypt->u, 48);
481
0
  }
482
0
  else
483
0
    memcpy(buffer + pwlen, crypt->u + 40, 8);
484
485
0
  fz_sha256_init(&sha256);
486
0
  fz_sha256_update(&sha256, buffer, pwlen + 8 + (ownerkey ? 48 : 0));
487
0
  fz_sha256_final(&sha256, buffer);
488
489
  /* clear password buffer and use it as iv */
490
0
  memset(buffer + 32, 0, sizeof(buffer) - 32);
491
0
  if (fz_aes_setkey_dec(&aes, buffer, crypt->length))
492
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "AES key init failed (keylen=%d)", crypt->length);
493
0
  fz_aes_crypt_cbc(&aes, FZ_AES_DECRYPT, 32, buffer + 32, ownerkey ? crypt->oe : crypt->ue, crypt->key);
494
0
}
495
496
/*
497
 * Compute an encryption key (PDF 1.7 ExtensionLevel 8 algorithm)
498
 *
499
 * Adobe has not yet released the details, so the algorithm reference is:
500
 * http://esec-lab.sogeti.com/post/The-undocumented-password-validation-algorithm-of-Adobe-Reader-X
501
 */
502
503
static void
504
pdf_compute_hardened_hash_r6(fz_context *ctx, unsigned char *password, size_t pwlen, unsigned char salt[8], unsigned char *ownerkey, unsigned char hash[32])
505
384
{
506
384
  unsigned char data[(128 + 64 + 48) * 64];
507
384
  unsigned char block[64];
508
384
  int block_size = 32;
509
384
  size_t data_len = 0;
510
384
  int i, j, sum;
511
512
384
  fz_sha256 sha256;
513
384
  fz_sha384 sha384;
514
384
  fz_sha512 sha512;
515
384
  fz_aes aes;
516
517
  /* Step 1: calculate initial data block */
518
384
  fz_sha256_init(&sha256);
519
384
  fz_sha256_update(&sha256, password, pwlen);
520
384
  fz_sha256_update(&sha256, salt, 8);
521
384
  if (ownerkey)
522
160
    fz_sha256_update(&sha256, ownerkey, 48);
523
384
  fz_sha256_final(&sha256, block);
524
525
26.5k
  for (i = 0; i < 64 || i < data[data_len * 64 - 1] + 32; i++)
526
26.2k
  {
527
    /* Step 2: repeat password and data block 64 times */
528
26.2k
    memcpy(data, password, pwlen);
529
26.2k
    memcpy(data + pwlen, block, block_size);
530
26.2k
    if (ownerkey)
531
11.0k
      memcpy(data + pwlen + block_size, ownerkey, 48);
532
26.2k
    data_len = pwlen + block_size + (ownerkey ? 48 : 0);
533
1.67M
    for (j = 1; j < 64; j++)
534
1.65M
      memcpy(data + j * data_len, data, data_len);
535
536
    /* Step 3: encrypt data using data block as key and iv */
537
26.2k
    if (fz_aes_setkey_enc(&aes, block, 128))
538
0
      fz_throw(ctx, FZ_ERROR_GENERIC, "AES key init failed (keylen=%d)", 128);
539
26.2k
    fz_aes_crypt_cbc(&aes, FZ_AES_ENCRYPT, data_len * 64, block + 16, data, data);
540
541
    /* Step 4: determine SHA-2 hash size for this round */
542
445k
    for (j = 0, sum = 0; j < 16; j++)
543
419k
      sum += data[j];
544
545
    /* Step 5: calculate data block for next round */
546
26.2k
    block_size = 32 + (sum % 3) * 16;
547
26.2k
    switch (block_size)
548
26.2k
    {
549
9.07k
    case 32:
550
9.07k
      fz_sha256_init(&sha256);
551
9.07k
      fz_sha256_update(&sha256, data, data_len * 64);
552
9.07k
      fz_sha256_final(&sha256, block);
553
9.07k
      break;
554
8.36k
    case 48:
555
8.36k
      fz_sha384_init(&sha384);
556
8.36k
      fz_sha384_update(&sha384, data, data_len * 64);
557
8.36k
      fz_sha384_final(&sha384, block);
558
8.36k
      break;
559
8.76k
    case 64:
560
8.76k
      fz_sha512_init(&sha512);
561
8.76k
      fz_sha512_update(&sha512, data, data_len * 64);
562
8.76k
      fz_sha512_final(&sha512, block);
563
8.76k
      break;
564
26.2k
    }
565
26.2k
  }
566
567
384
  memset(data, 0, sizeof(data));
568
384
  memcpy(hash, block, 32);
569
384
}
570
571
static void
572
pdf_compute_encryption_key_r6(fz_context *ctx, pdf_crypt *crypt, unsigned char *password, size_t pwlen, int ownerkey, unsigned char *validationkey)
573
192
{
574
192
  unsigned char hash[32];
575
192
  unsigned char iv[16];
576
192
  fz_aes aes;
577
578
192
  if (pwlen > 127)
579
0
    pwlen = 127;
580
581
192
  pdf_compute_hardened_hash_r6(ctx, password, pwlen,
582
192
    (ownerkey ? crypt->o : crypt->u) + 32,
583
192
    ownerkey ? crypt->u : NULL, validationkey);
584
192
  pdf_compute_hardened_hash_r6(ctx, password, pwlen,
585
192
    (ownerkey ? crypt->o : crypt->u) + 40,
586
192
    (ownerkey ? crypt->u : NULL),
587
192
    hash);
588
589
192
  memset(iv, 0, sizeof(iv));
590
192
  if (fz_aes_setkey_dec(&aes, hash, 256))
591
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "AES key init failed (keylen=256)");
592
192
  fz_aes_crypt_cbc(&aes, FZ_AES_DECRYPT, 32, iv, ownerkey ? crypt->oe : crypt->ue, crypt->key);
593
192
}
594
595
/*
596
 * Computing the user password (PDF 1.7 algorithm 3.4 and 3.5)
597
 * Also save the generated key for decrypting objects and streams in crypt->key.
598
 */
599
600
static void
601
pdf_compute_user_password(fz_context *ctx, pdf_crypt *crypt, unsigned char *password, size_t pwlen, unsigned char *output)
602
466
{
603
466
  int n = fz_clampi(crypt->length / 8, 0, 16);
604
605
466
  if (crypt->r == 2)
606
85
  {
607
85
    fz_arc4 arc4;
608
609
85
    pdf_compute_encryption_key(ctx, crypt, password, pwlen, crypt->key);
610
85
    fz_arc4_init(&arc4, crypt->key, n);
611
85
    fz_arc4_encrypt(&arc4, output, padding, 32);
612
85
  }
613
614
466
  if (crypt->r == 3 || crypt->r == 4)
615
268
  {
616
268
    unsigned char xor[32];
617
268
    unsigned char digest[16];
618
268
    fz_md5 md5;
619
268
    fz_arc4 arc4;
620
268
    int i, x;
621
622
268
    pdf_compute_encryption_key(ctx, crypt, password, pwlen, crypt->key);
623
624
268
    fz_md5_init(&md5);
625
268
    fz_md5_update(&md5, padding, 32);
626
268
    fz_md5_update(&md5, (unsigned char*)pdf_to_str_buf(ctx, crypt->id), pdf_to_str_len(ctx, crypt->id));
627
268
    fz_md5_final(&md5, digest);
628
629
268
    fz_arc4_init(&arc4, crypt->key, n);
630
268
    fz_arc4_encrypt(&arc4, output, digest, 16);
631
632
5.36k
    for (x = 1; x <= 19; x++)
633
5.09k
    {
634
83.4k
      for (i = 0; i < n; i++)
635
78.3k
        xor[i] = crypt->key[i] ^ x;
636
5.09k
      fz_arc4_init(&arc4, xor, n);
637
5.09k
      fz_arc4_encrypt(&arc4, output, output, 16);
638
5.09k
    }
639
640
268
    memcpy(output + 16, padding, 16);
641
268
  }
642
643
466
  if (crypt->r == 5)
644
0
  {
645
0
    pdf_compute_encryption_key_r5(ctx, crypt, password, pwlen, 0, output);
646
0
  }
647
648
466
  if (crypt->r == 6)
649
112
  {
650
112
    pdf_compute_encryption_key_r6(ctx, crypt, password, pwlen, 0, output);
651
112
  }
652
466
}
653
654
/*
655
 * Authenticating the user password (PDF 1.7 algorithm 3.6
656
 * and ExtensionLevel 3 algorithm 3.11)
657
 * This also has the side effect of saving a key generated
658
 * from the password for decrypting objects and streams.
659
 */
660
661
static int
662
pdf_authenticate_user_password(fz_context *ctx, pdf_crypt *crypt, unsigned char *password, size_t pwlen)
663
466
{
664
466
  unsigned char output[32];
665
466
  pdf_compute_user_password(ctx, crypt, password, pwlen, output);
666
466
  if (crypt->r == 2 || crypt->r == 5 || crypt->r == 6)
667
197
    return memcmp(output, crypt->u, 32) == 0;
668
269
  if (crypt->r == 3 || crypt->r == 4)
669
268
    return memcmp(output, crypt->u, 16) == 0;
670
1
  return 0;
671
269
}
672
673
/*
674
 * Authenticating the owner password (PDF 1.7 algorithm 3.7,
675
 * ExtensionLevel 3 algorithm 3.12, ExtensionLevel 8 algorithm)
676
 * Generates the user password from the owner password
677
 * and calls pdf_authenticate_user_password.
678
 */
679
680
static int
681
pdf_authenticate_owner_password(fz_context *ctx, pdf_crypt *crypt, unsigned char *ownerpass, size_t pwlen)
682
215
{
683
215
  int n = fz_clampi(crypt->length / 8, 0, 16);
684
685
215
  if (crypt->r == 2)
686
29
  {
687
29
    unsigned char pwbuf[32];
688
29
    unsigned char key[16];
689
29
    unsigned char userpass[32];
690
29
    fz_md5 md5;
691
29
    fz_arc4 arc4;
692
693
29
    if (pwlen > 32)
694
0
      pwlen = 32;
695
29
    memcpy(pwbuf, ownerpass, pwlen);
696
29
    if (pwlen < 32)
697
29
      memcpy(pwbuf + pwlen, padding, 32 - pwlen);
698
699
29
    fz_md5_init(&md5);
700
29
    fz_md5_update(&md5, pwbuf, 32);
701
29
    fz_md5_final(&md5, key);
702
703
29
    fz_arc4_init(&arc4, key, n);
704
29
    fz_arc4_encrypt(&arc4, userpass, crypt->o, 32);
705
706
29
    return pdf_authenticate_user_password(ctx, crypt, userpass, 32);
707
29
  }
708
709
186
  if (crypt->r == 3 || crypt->r == 4)
710
105
  {
711
105
    unsigned char pwbuf[32];
712
105
    unsigned char key[16];
713
105
    unsigned char xor[32];
714
105
    unsigned char userpass[32];
715
105
    int i, x;
716
105
    fz_md5 md5;
717
105
    fz_arc4 arc4;
718
719
105
    if (pwlen > 32)
720
0
      pwlen = 32;
721
105
    memcpy(pwbuf, ownerpass, pwlen);
722
105
    if (pwlen < 32)
723
105
      memcpy(pwbuf + pwlen, padding, 32 - pwlen);
724
725
105
    fz_md5_init(&md5);
726
105
    fz_md5_update(&md5, pwbuf, 32);
727
105
    fz_md5_final(&md5, key);
728
729
5.35k
    for (i = 0; i < 50; i++)
730
5.25k
    {
731
5.25k
      fz_md5_init(&md5);
732
5.25k
      fz_md5_update(&md5, key, n);
733
5.25k
      fz_md5_final(&md5, key);
734
5.25k
    }
735
736
105
    memcpy(userpass, crypt->o, 32);
737
2.20k
    for (x = 0; x < 20; x++)
738
2.10k
    {
739
34.0k
      for (i = 0; i < n; i++)
740
31.9k
        xor[i] = key[i] ^ (19 - x);
741
2.10k
      fz_arc4_init(&arc4, xor, n);
742
2.10k
      fz_arc4_encrypt(&arc4, userpass, userpass, 32);
743
2.10k
    }
744
745
105
    return pdf_authenticate_user_password(ctx, crypt, userpass, 32);
746
105
  }
747
748
81
  if (crypt->r == 5)
749
0
  {
750
0
    unsigned char key[32];
751
0
    pdf_compute_encryption_key_r5(ctx, crypt, ownerpass, pwlen, 1, key);
752
0
    return !memcmp(key, crypt->o, 32);
753
0
  }
754
755
81
  if (crypt->r == 6)
756
80
  {
757
80
    unsigned char key[32];
758
80
    pdf_compute_encryption_key_r6(ctx, crypt, ownerpass, pwlen, 1, key);
759
80
    return !memcmp(key, crypt->o, 32);
760
80
  }
761
762
1
  return 0;
763
81
}
764
765
static void pdf_docenc_from_utf8(char *password, const char *utf8, int n)
766
135
{
767
135
  int i = 0, k, c;
768
135
  while (*utf8 && i + 1 < n)
769
0
  {
770
0
    utf8 += fz_chartorune(&c, utf8);
771
0
    for (k = 0; k < 256; k++)
772
0
    {
773
0
      if (c == fz_unicode_from_pdf_doc_encoding[k])
774
0
      {
775
0
        password[i++] = k;
776
0
        break;
777
0
      }
778
0
    }
779
    /* FIXME: drop characters that can't be encoded or return an error? */
780
0
  }
781
135
  password[i] = 0;
782
135
}
783
784
static void pdf_saslprep_from_utf8(char *password, const char *utf8, int n)
785
80
{
786
  /* TODO: stringprep with SALSprep profile */
787
80
  fz_strlcpy(password, utf8, n);
788
80
}
789
790
int
791
pdf_authenticate_password(fz_context *ctx, pdf_document *doc, const char *pwd_utf8)
792
14.3k
{
793
14.3k
  char password[2048];
794
14.3k
  int auth;
795
796
14.3k
  if (!doc->crypt)
797
14.1k
    return 1; /* No password required */
798
799
215
  password[0] = 0;
800
215
  if (pwd_utf8)
801
215
  {
802
215
    if (doc->crypt->r <= 4)
803
135
      pdf_docenc_from_utf8(password, pwd_utf8, sizeof password);
804
80
    else
805
80
      pdf_saslprep_from_utf8(password, pwd_utf8, sizeof password);
806
215
  }
807
808
215
  auth = 0;
809
215
  if (pdf_authenticate_user_password(ctx, doc->crypt, (unsigned char *)password, strlen(password)))
810
117
    auth = 2;
811
215
  if (pdf_authenticate_owner_password(ctx, doc->crypt, (unsigned char *)password, strlen(password)))
812
4
    auth |= 4;
813
211
  else if (auth & 2)
814
117
  {
815
    /* We need to reauthenticate the user password,
816
     * because the failed attempt to authenticate
817
     * the owner password will have invalidated the
818
     * stored keys. */
819
117
    (void)pdf_authenticate_user_password(ctx, doc->crypt, (unsigned char *)password, strlen(password));
820
117
  }
821
822
  /* To match Acrobat, we choose not to allow an empty owner
823
   * password, unless the user password is also the empty one. */
824
215
  if (*password == 0 && auth == 4)
825
4
    return 0;
826
827
211
  return auth;
828
215
}
829
830
int
831
pdf_needs_password(fz_context *ctx, pdf_document *doc)
832
0
{
833
0
  if (!doc->crypt)
834
0
    return 0;
835
0
  if (pdf_authenticate_password(ctx, doc, ""))
836
0
    return 0;
837
0
  return 1;
838
0
}
839
840
int
841
pdf_has_permission(fz_context *ctx, pdf_document *doc, fz_permission p)
842
0
{
843
0
  if (!doc->crypt)
844
0
    return 1;
845
0
  switch (p)
846
0
  {
847
0
  case FZ_PERMISSION_PRINT: return doc->crypt->p & PDF_PERM_PRINT;
848
0
  case FZ_PERMISSION_EDIT: return doc->crypt->p & PDF_PERM_MODIFY;
849
0
  case FZ_PERMISSION_COPY: return doc->crypt->p & PDF_PERM_COPY;
850
0
  case FZ_PERMISSION_ANNOTATE: return doc->crypt->p & PDF_PERM_ANNOTATE;
851
0
  case FZ_PERMISSION_FORM: return doc->crypt->p & PDF_PERM_FORM;
852
0
  case FZ_PERMISSION_ACCESSIBILITY: return doc->crypt->p & PDF_PERM_ACCESSIBILITY;
853
0
  case FZ_PERMISSION_ASSEMBLE: return doc->crypt->p & PDF_PERM_ASSEMBLE;
854
0
  case FZ_PERMISSION_PRINT_HQ: return doc->crypt->p & PDF_PERM_PRINT_HQ;
855
0
  }
856
0
  return 1;
857
0
}
858
859
int
860
pdf_document_permissions(fz_context *ctx, pdf_document *doc)
861
0
{
862
0
  if (doc->crypt)
863
0
    return doc->crypt->p;
864
  /* all permissions granted, reserved bits set appropriately */
865
0
  return (int)0xFFFFFFFC;
866
0
}
867
868
/*
869
 * Compute the owner password (PDF 1.7 algorithm 3.3)
870
 */
871
872
static void
873
pdf_compute_owner_password(fz_context *ctx, pdf_crypt *crypt, unsigned char *opassword, size_t opwlen, unsigned char *upassword, size_t upwlen, unsigned char *output)
874
0
{
875
0
  unsigned char obuf[32];
876
0
  unsigned char ubuf[32];
877
0
  unsigned char digest[32];
878
0
  int i, n;
879
0
  fz_md5 md5;
880
0
  fz_arc4 arc4;
881
882
0
  n = fz_clampi(crypt->length / 8, 0, 16);
883
884
  /* Step 1 - copy and pad owner password string */
885
0
  if (opwlen > 32)
886
0
    opwlen = 32;
887
0
  memcpy(obuf, opassword, opwlen);
888
0
  if (opwlen < 32)
889
0
    memcpy(obuf + opwlen, padding, 32 - opwlen);
890
891
  /* Step 2 - init md5 and pass value of step 1 */
892
0
  fz_md5_init(&md5);
893
0
  fz_md5_update(&md5, obuf, 32);
894
0
  fz_md5_final(&md5, obuf);
895
896
  /* Step 3 (revision 3 or greater) - do some voodoo 50 times */
897
0
  if (crypt->r >= 3)
898
0
  {
899
0
    for (i = 0; i < 50; i++)
900
0
    {
901
0
      fz_md5_init(&md5);
902
0
      fz_md5_update(&md5, obuf, n);
903
0
      fz_md5_final(&md5, obuf);
904
0
    }
905
0
  }
906
907
  /* Step 4 - encrypt owner password md5 hash */
908
0
  fz_arc4_init(&arc4, obuf, n);
909
910
  /* Step 5 - copy and pad user password string */
911
0
  if (upwlen > 32)
912
0
    upwlen = 32;
913
0
  memcpy(ubuf, upassword, upwlen);
914
0
  if (upwlen < 32)
915
0
    memcpy(ubuf + upwlen, padding, 32 - upwlen);
916
917
  /* Step 6 - encrypt user password md5 hash */
918
0
  fz_arc4_encrypt(&arc4, digest, ubuf, 32);
919
920
  /* Step 7 - */
921
0
  if (crypt->r >= 3)
922
0
  {
923
0
    unsigned char xor[32];
924
0
    int x;
925
926
0
    for (x = 1; x <= 19; x++)
927
0
    {
928
0
      for (i = 0; i < n; i++)
929
0
        xor[i] = obuf[i] ^ x;
930
0
      fz_arc4_init(&arc4, xor, n);
931
0
      fz_arc4_encrypt(&arc4, digest, digest, 32);
932
0
    }
933
0
  }
934
935
  /* Step 8 - the owner password is the first 16 bytes of the result */
936
0
  memcpy(output, digest, 32);
937
0
}
938
939
unsigned char *
940
pdf_crypt_key(fz_context *ctx, pdf_crypt *crypt)
941
0
{
942
0
  if (crypt)
943
0
    return crypt->key;
944
0
  return NULL;
945
0
}
946
947
int
948
pdf_crypt_version(fz_context *ctx, pdf_crypt *crypt)
949
0
{
950
0
  if (crypt)
951
0
    return crypt->v;
952
0
  return 0;
953
0
}
954
955
int pdf_crypt_revision(fz_context *ctx, pdf_crypt *crypt)
956
0
{
957
0
  if (crypt)
958
0
    return crypt->r;
959
0
  return 0;
960
0
}
961
962
static char *
963
crypt_method(fz_context *ctx, int method)
964
2
{
965
2
  switch (method)
966
2
  {
967
0
  default:
968
0
  case PDF_CRYPT_UNKNOWN: return "Unknown";
969
1
  case PDF_CRYPT_NONE: return "None";
970
0
  case PDF_CRYPT_RC4: return "RC4";
971
0
  case PDF_CRYPT_AESV2: return "AES";
972
1
  case PDF_CRYPT_AESV3: return "AES";
973
2
  }
974
2
}
975
976
const char *
977
pdf_crypt_string_method(fz_context *ctx, pdf_crypt *crypt)
978
2
{
979
2
  if (crypt)
980
2
    return crypt_method(ctx, crypt->strf.method);
981
0
  return "None";
982
2
}
983
984
const char *
985
pdf_crypt_stream_method(fz_context *ctx, pdf_crypt *crypt)
986
0
{
987
0
  if (crypt)
988
0
    return crypt_method(ctx, crypt->stmf.method);
989
0
  return "None";
990
0
}
991
992
const char *
993
pdf_crypt_method(fz_context *ctx, pdf_crypt *crypt)
994
2
{
995
2
  return pdf_crypt_string_method(ctx, crypt);
996
2
}
997
998
int
999
pdf_crypt_length(fz_context *ctx, pdf_crypt *crypt)
1000
0
{
1001
0
  if (crypt)
1002
0
    return crypt->length;
1003
0
  return 0;
1004
0
}
1005
1006
int
1007
pdf_crypt_permissions(fz_context *ctx, pdf_crypt *crypt)
1008
0
{
1009
0
  if (crypt)
1010
0
    return crypt->p;
1011
0
  return 0;
1012
0
}
1013
1014
int
1015
pdf_crypt_encrypt_metadata(fz_context *ctx, pdf_crypt *crypt)
1016
0
{
1017
0
  if (crypt)
1018
0
    return crypt->encrypt_metadata;
1019
0
  return 0;
1020
0
}
1021
1022
unsigned char *
1023
pdf_crypt_owner_password(fz_context *ctx, pdf_crypt *crypt)
1024
0
{
1025
0
  if (crypt)
1026
0
    return crypt->o;
1027
0
  return NULL;
1028
0
}
1029
1030
unsigned char *
1031
pdf_crypt_user_password(fz_context *ctx, pdf_crypt *crypt)
1032
0
{
1033
0
  if (crypt)
1034
0
    return crypt->u;
1035
0
  return NULL;
1036
0
}
1037
1038
unsigned char *
1039
pdf_crypt_owner_encryption(fz_context *ctx, pdf_crypt *crypt)
1040
0
{
1041
0
  if (crypt)
1042
0
    return crypt->oe;
1043
0
  return NULL;
1044
0
}
1045
1046
unsigned char *
1047
pdf_crypt_user_encryption(fz_context *ctx, pdf_crypt *crypt)
1048
0
{
1049
0
  if (crypt)
1050
0
    return crypt->ue;
1051
0
  return NULL;
1052
0
}
1053
1054
unsigned char *
1055
pdf_crypt_permissions_encryption(fz_context *ctx, pdf_crypt *crypt)
1056
0
{
1057
0
  if (crypt)
1058
0
    return crypt->perms;
1059
0
  return 0;
1060
0
}
1061
1062
/*
1063
 * PDF 1.7 algorithm 3.1 and ExtensionLevel 3 algorithm 3.1a
1064
 *
1065
 * Using the global encryption key that was generated from the
1066
 * password, create a new key that is used to decrypt individual
1067
 * objects and streams. This key is based on the object and
1068
 * generation numbers.
1069
 */
1070
1071
static int
1072
pdf_compute_object_key(pdf_crypt *crypt, pdf_crypt_filter *cf, int num, int gen, unsigned char *key, int max_len)
1073
8.24k
{
1074
8.24k
  fz_md5 md5;
1075
8.24k
  unsigned char message[5];
1076
8.24k
  int key_len = crypt->length / 8;
1077
1078
8.24k
  if (key_len > max_len)
1079
0
    key_len = max_len;
1080
1081
  /* Encryption method version 0 is undocumented, but a lucky
1082
     guess revealed that all streams/strings in those PDFs are
1083
     encrypted using the same 40 bit file enryption key using RC4. */
1084
8.24k
  if (crypt->v == 0 || cf->method == PDF_CRYPT_AESV3)
1085
1.59k
  {
1086
1.59k
    memcpy(key, crypt->key, key_len);
1087
1.59k
    return key_len;
1088
1.59k
  }
1089
1090
6.65k
  fz_md5_init(&md5);
1091
6.65k
  fz_md5_update(&md5, crypt->key, key_len);
1092
6.65k
  message[0] = (num) & 0xFF;
1093
6.65k
  message[1] = (num >> 8) & 0xFF;
1094
6.65k
  message[2] = (num >> 16) & 0xFF;
1095
6.65k
  message[3] = (gen) & 0xFF;
1096
6.65k
  message[4] = (gen >> 8) & 0xFF;
1097
6.65k
  fz_md5_update(&md5, message, 5);
1098
1099
6.65k
  if (cf->method == PDF_CRYPT_AESV2)
1100
5.24k
    fz_md5_update(&md5, (unsigned char *)"sAlT", 4);
1101
1102
6.65k
  fz_md5_final(&md5, key);
1103
1104
6.65k
  if (key_len + 5 > 16)
1105
5.61k
    return 16;
1106
1.04k
  return key_len + 5;
1107
6.65k
}
1108
1109
/*
1110
 * PDF 1.7 algorithm 3.1 and ExtensionLevel 3 algorithm 3.1a
1111
 *
1112
 * Decrypt all strings in obj modifying the data in-place.
1113
 * Recurse through arrays and dictionaries, but do not follow
1114
 * indirect references.
1115
 */
1116
1117
static int is_signature(fz_context *ctx, pdf_obj *obj)
1118
222
{
1119
222
  if (pdf_dict_get(ctx, obj, PDF_NAME(Type)) == PDF_NAME(Sig))
1120
0
    if (pdf_dict_get(ctx, obj, PDF_NAME(Contents)) && pdf_dict_get(ctx, obj, PDF_NAME(ByteRange)) && pdf_dict_get(ctx, obj, PDF_NAME(Filter)))
1121
0
      return 1;
1122
222
  return 0;
1123
222
}
1124
1125
static void
1126
pdf_crypt_obj_imp(fz_context *ctx, pdf_crypt *crypt, pdf_obj *obj, unsigned char *key, int keylen)
1127
84.4k
{
1128
84.4k
  unsigned char *s;
1129
84.4k
  int i;
1130
1131
84.4k
  if (pdf_is_indirect(ctx, obj))
1132
10.8k
    return;
1133
1134
73.5k
  if (pdf_is_string(ctx, obj))
1135
12.9k
  {
1136
12.9k
    size_t n = pdf_to_str_len(ctx, obj);
1137
12.9k
    s = (unsigned char *)pdf_to_str_buf(ctx, obj);
1138
1139
12.9k
    if (crypt->strf.method == PDF_CRYPT_RC4)
1140
11.3k
    {
1141
11.3k
      fz_arc4 arc4;
1142
11.3k
      fz_arc4_init(&arc4, key, keylen);
1143
11.3k
      fz_arc4_encrypt(&arc4, s, s, n);
1144
11.3k
    }
1145
1146
12.9k
    if (crypt->strf.method == PDF_CRYPT_AESV2 || crypt->strf.method == PDF_CRYPT_AESV3)
1147
1.57k
    {
1148
1.57k
      if (n == 0)
1149
5
      {
1150
        /* Empty strings are permissible */
1151
5
      }
1152
1.56k
      else if (n & 15 || n < 32)
1153
301
        fz_warn(ctx, "invalid string length for aes encryption");
1154
1.26k
      else
1155
1.26k
      {
1156
1.26k
        unsigned char iv[16];
1157
1.26k
        fz_aes aes;
1158
1.26k
        memcpy(iv, s, 16);
1159
1.26k
        if (fz_aes_setkey_dec(&aes, key, keylen * 8))
1160
23
          fz_throw(ctx, FZ_ERROR_GENERIC, "AES key init failed (keylen=%d)", keylen * 8);
1161
1.24k
        fz_aes_crypt_cbc(&aes, FZ_AES_DECRYPT, n - 16, iv, s + 16, s);
1162
        /* delete space used for iv and padding bytes at end */
1163
1.24k
        if (s[n - 17] < 1 || s[n - 17] > 16)
1164
199
          fz_warn(ctx, "aes padding out of range");
1165
1.04k
        else
1166
1.04k
          pdf_set_str_len(ctx, obj, n - 16 - s[n - 17]);
1167
1.24k
      }
1168
1.57k
    }
1169
12.9k
  }
1170
1171
60.6k
  else if (pdf_is_array(ctx, obj))
1172
5.56k
  {
1173
5.56k
    int n = pdf_array_len(ctx, obj);
1174
45.4k
    for (i = 0; i < n; i++)
1175
39.9k
    {
1176
39.9k
      pdf_crypt_obj_imp(ctx, crypt, pdf_array_get(ctx, obj, i), key, keylen);
1177
39.9k
    }
1178
5.56k
  }
1179
1180
55.0k
  else if (pdf_is_dict(ctx, obj))
1181
9.15k
  {
1182
9.15k
    int n = pdf_dict_len(ctx, obj);
1183
47.4k
    for (i = 0; i < n; i++)
1184
38.3k
    {
1185
38.3k
      if (pdf_dict_get_key(ctx, obj, i) == PDF_NAME(Contents) && is_signature(ctx, obj))
1186
0
        continue;
1187
1188
38.3k
      pdf_crypt_obj_imp(ctx, crypt, pdf_dict_get_val(ctx, obj, i), key, keylen);
1189
38.3k
    }
1190
9.15k
  }
1191
73.5k
}
1192
1193
void
1194
pdf_crypt_obj(fz_context *ctx, pdf_crypt *crypt, pdf_obj *obj, int num, int gen)
1195
6.20k
{
1196
6.20k
  unsigned char key[32];
1197
6.20k
  int len;
1198
1199
6.20k
  len = pdf_compute_object_key(crypt, &crypt->strf, num, gen, key, 32);
1200
1201
6.20k
  pdf_crypt_obj_imp(ctx, crypt, obj, key, len);
1202
6.20k
}
1203
1204
/*
1205
 * PDF 1.7 algorithm 3.1 and ExtensionLevel 3 algorithm 3.1a
1206
 *
1207
 * Create filter suitable for de/encrypting a stream.
1208
 */
1209
static fz_stream *
1210
pdf_open_crypt_imp(fz_context *ctx, fz_stream *chain, pdf_crypt *crypt, pdf_crypt_filter *stmf, int num, int gen)
1211
2.04k
{
1212
2.04k
  unsigned char key[32];
1213
2.04k
  int len;
1214
1215
2.04k
  len = pdf_compute_object_key(crypt, stmf, num, gen, key, 32);
1216
1217
2.04k
  if (stmf->method == PDF_CRYPT_RC4)
1218
193
    return fz_open_arc4(ctx, chain, key, len);
1219
1220
1.84k
  if (stmf->method == PDF_CRYPT_AESV2 || stmf->method == PDF_CRYPT_AESV3)
1221
1.83k
    return fz_open_aesd(ctx, chain, key, len);
1222
1223
12
  return fz_keep_stream(ctx, chain);
1224
1.84k
}
1225
1226
fz_stream *
1227
pdf_open_crypt(fz_context *ctx, fz_stream *chain, pdf_crypt *crypt, int num, int gen)
1228
2.04k
{
1229
2.04k
  return pdf_open_crypt_imp(ctx, chain, crypt, &crypt->stmf, num, gen);
1230
2.04k
}
1231
1232
fz_stream *
1233
pdf_open_crypt_with_filter(fz_context *ctx, fz_stream *chain, pdf_crypt *crypt, pdf_obj *name, int num, int gen)
1234
0
{
1235
0
  if (!pdf_name_eq(ctx, name, PDF_NAME(Identity)))
1236
0
  {
1237
0
    pdf_crypt_filter cf;
1238
0
    pdf_parse_crypt_filter(ctx, &cf, crypt, name);
1239
0
    return pdf_open_crypt_imp(ctx, chain, crypt, &cf, num, gen);
1240
0
  }
1241
0
  return fz_keep_stream(ctx, chain);
1242
0
}
1243
1244
void
1245
pdf_print_crypt(fz_context *ctx, fz_output *out, pdf_crypt *crypt)
1246
0
{
1247
0
  int i;
1248
1249
0
  fz_write_printf(ctx, out, "crypt {\n");
1250
1251
0
  fz_write_printf(ctx, out, "\tv=%d length=%d\n", crypt->v, crypt->length);
1252
0
  fz_write_printf(ctx, out, "\tstmf method=%d length=%d\n", crypt->stmf.method, crypt->stmf.length);
1253
0
  fz_write_printf(ctx, out, "\tstrf method=%d length=%d\n", crypt->strf.method, crypt->strf.length);
1254
0
  fz_write_printf(ctx, out, "\tr=%d\n", crypt->r);
1255
1256
0
  fz_write_printf(ctx, out, "\to=<");
1257
0
  for (i = 0; i < 32; i++)
1258
0
    fz_write_printf(ctx, out, "%02X", crypt->o[i]);
1259
0
  fz_write_printf(ctx, out, ">\n");
1260
1261
0
  fz_write_printf(ctx, out, "\tu=<");
1262
0
  for (i = 0; i < 32; i++)
1263
0
    fz_write_printf(ctx, out, "%02X", crypt->u[i]);
1264
0
  fz_write_printf(ctx, out, ">\n");
1265
1266
0
  fz_write_printf(ctx, out, "}\n");
1267
0
}
1268
1269
void pdf_encrypt_data(fz_context *ctx, pdf_crypt *crypt, int num, int gen, void (*write_data)(fz_context *ctx, void *, const unsigned char *, size_t), void *arg, const unsigned char *s, size_t n)
1270
0
{
1271
0
  unsigned char buffer[256];
1272
0
  unsigned char key[32];
1273
0
  int keylen;
1274
1275
0
  if (crypt == NULL)
1276
0
  {
1277
0
    write_data(ctx, arg, s, n);
1278
0
    return;
1279
0
  }
1280
1281
0
  keylen = pdf_compute_object_key(crypt, &crypt->strf, num, gen, key, 32);
1282
1283
0
  if (crypt->strf.method == PDF_CRYPT_RC4)
1284
0
  {
1285
0
    fz_arc4 arc4;
1286
0
    fz_arc4_init(&arc4, key, keylen);
1287
0
    while (n > 0)
1288
0
    {
1289
0
      size_t len = n;
1290
0
      if (len > (int)sizeof(buffer))
1291
0
        len = sizeof(buffer);
1292
0
      fz_arc4_encrypt(&arc4, buffer, s, len);
1293
0
      write_data(ctx, arg, buffer, len);
1294
0
      s += len;
1295
0
      n -= len;
1296
0
    }
1297
0
    return;
1298
0
  }
1299
1300
0
  if (crypt->strf.method == PDF_CRYPT_AESV2 || crypt->strf.method == PDF_CRYPT_AESV3)
1301
0
  {
1302
0
    size_t len = 0;
1303
0
    fz_aes aes;
1304
0
    unsigned char iv[16];
1305
1306
    /* Empty strings can be represented by empty strings */
1307
0
    if (n == 0)
1308
0
      return;
1309
1310
0
    if (fz_aes_setkey_enc(&aes, key, keylen * 8))
1311
0
      fz_throw(ctx, FZ_ERROR_GENERIC, "AES key init failed (keylen=%d)", keylen * 8);
1312
1313
0
    fz_memrnd(ctx, iv, 16);
1314
0
    write_data(ctx, arg, iv, 16);
1315
1316
0
    while (n > 0)
1317
0
    {
1318
0
      len = n;
1319
0
      if (len > 16)
1320
0
        len = 16;
1321
0
      memcpy(buffer, s, len);
1322
0
      if (len != 16)
1323
0
        memset(&buffer[len], 16-(int)len, 16-len);
1324
0
      fz_aes_crypt_cbc(&aes, FZ_AES_ENCRYPT, 16, iv, buffer, buffer+16);
1325
0
      write_data(ctx, arg, buffer+16, 16);
1326
0
      s += len;
1327
0
      n -= len;
1328
0
    }
1329
0
    if (len == 16) {
1330
0
      memset(buffer, 16, 16);
1331
0
      fz_aes_crypt_cbc(&aes, FZ_AES_ENCRYPT, 16, iv, buffer, buffer+16);
1332
0
      write_data(ctx, arg, buffer+16, 16);
1333
0
    }
1334
0
    return;
1335
0
  }
1336
1337
  /* Should never happen, but... */
1338
0
  write_data(ctx, arg, s, n);
1339
0
}
1340
1341
size_t pdf_encrypted_len(fz_context *ctx, pdf_crypt *crypt, int num, int gen, size_t len)
1342
0
{
1343
0
  if (crypt == NULL)
1344
0
    return len;
1345
1346
0
  if (crypt->strf.method == PDF_CRYPT_AESV2 || crypt->strf.method == PDF_CRYPT_AESV3)
1347
0
  {
1348
0
    len += 16; /* 16 for IV */
1349
0
    if ((len & 15) == 0)
1350
0
      len += 16; /* Another 16 if our last block is full anyway */
1351
0
    len = (len + 15) & ~15; /* And pad to the block */
1352
0
  }
1353
1354
0
  return len;
1355
0
}
1356
1357
/* PDF 2.0 algorithm 8 */
1358
static void
1359
pdf_compute_user_password_r6(fz_context *ctx, pdf_crypt *crypt, unsigned char *password, size_t pwlen, unsigned char *outputpw, unsigned char *outputencryption)
1360
0
{
1361
0
  unsigned char validationsalt[8];
1362
0
  unsigned char keysalt[8];
1363
0
  unsigned char hash[32];
1364
0
  unsigned char iv[16];
1365
0
  fz_aes aes;
1366
1367
  /* Step a) - Generate random salts. */
1368
0
  fz_memrnd(ctx, validationsalt, nelem(validationsalt));
1369
0
  fz_memrnd(ctx, keysalt, nelem(keysalt));
1370
1371
  /* Step a) - Compute 32 byte hash given password and validation salt. */
1372
0
  pdf_compute_hardened_hash_r6(ctx, password, pwlen, validationsalt, NULL, outputpw);
1373
0
  memcpy(outputpw + 32, validationsalt, nelem(validationsalt));
1374
0
  memcpy(outputpw + 40, keysalt, nelem(keysalt));
1375
1376
  /* Step b) - Compute 32 byte hash given password and user salt. */
1377
0
  pdf_compute_hardened_hash_r6(ctx, password, pwlen, keysalt, NULL, hash);
1378
1379
  /* Step b) - Use hash as AES-key when encrypting the file encryption key. */
1380
0
  memset(iv, 0, sizeof(iv));
1381
0
  if (fz_aes_setkey_enc(&aes, hash, 256))
1382
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "AES key init failed (keylen=256)");
1383
0
  fz_aes_crypt_cbc(&aes, FZ_AES_ENCRYPT, 32, iv, crypt->key, outputencryption);
1384
0
}
1385
1386
/* PDF 2.0 algorithm 9 */
1387
static void
1388
pdf_compute_owner_password_r6(fz_context *ctx, pdf_crypt *crypt, unsigned char *password, size_t pwlen, unsigned char *outputpw, unsigned char *outputencryption)
1389
0
{
1390
0
  unsigned char validationsalt[8];
1391
0
  unsigned char keysalt[8];
1392
0
  unsigned char hash[32];
1393
0
  unsigned char iv[16];
1394
0
  fz_aes aes;
1395
1396
  /* Step a) - Generate random salts. */
1397
0
  fz_memrnd(ctx, validationsalt, nelem(validationsalt));
1398
0
  fz_memrnd(ctx, keysalt, nelem(keysalt));
1399
1400
  /* Step a) - Compute 32 byte hash given owner password, validation salt and user password. */
1401
0
  pdf_compute_hardened_hash_r6(ctx, password, pwlen, validationsalt, crypt->u, outputpw);
1402
0
  memcpy(outputpw + 32, validationsalt, nelem(validationsalt));
1403
0
  memcpy(outputpw + 40, keysalt, nelem(keysalt));
1404
1405
  /* Step b) - Compute 32 byte hash given owner password, user salt and user password. */
1406
0
  pdf_compute_hardened_hash_r6(ctx, password, pwlen, keysalt, crypt->u, hash);
1407
1408
  /* Step b) - Use hash as AES-key when encrypting the file encryption key. */
1409
0
  memset(iv, 0, sizeof(iv));
1410
0
  if (fz_aes_setkey_enc(&aes, hash, 256))
1411
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "AES key init failed (keylen=256)");
1412
0
  fz_aes_crypt_cbc(&aes, FZ_AES_ENCRYPT, 32, iv, crypt->key, outputencryption);
1413
0
}
1414
1415
/* PDF 2.0 algorithm 10 */
1416
static void
1417
pdf_compute_permissions_r6(fz_context *ctx, pdf_crypt *crypt, unsigned char *output)
1418
0
{
1419
0
  unsigned char buf[16];
1420
0
  unsigned char iv[16];
1421
0
  fz_aes aes;
1422
1423
  /* Steps a) and b) - Extend permissions field and put into lower order bytes. */
1424
0
  memcpy(buf, (unsigned char *) &crypt->p, 4);
1425
0
  memset(&buf[4], 0xff, 4);
1426
1427
  /* Step c) - Encode EncryptMetadata as T/F. */
1428
0
  buf[8] = crypt->encrypt_metadata ? 'T' : 'F';
1429
1430
  /* Step d) - Encode ASCII characters "adb". */
1431
0
  buf[9] = 'a';
1432
0
  buf[10] = 'd';
1433
0
  buf[11] = 'b';
1434
1435
  /* Step e) - Encode 4 random bytes. */
1436
0
  fz_memrnd(ctx, &buf[12], 4);
1437
1438
  /* Step f) - Use file encryption key as AES-key when encrypting buffer. */
1439
0
  memset(iv, 0, sizeof(iv));
1440
0
  if (fz_aes_setkey_enc(&aes, crypt->key, 256))
1441
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "AES key init failed (keylen=256)");
1442
0
  fz_aes_crypt_cbc(&aes, FZ_AES_ENCRYPT, 16, iv, buf, output);
1443
0
}
1444
1445
pdf_crypt *
1446
pdf_new_encrypt(fz_context *ctx, const char *opwd_utf8, const char *upwd_utf8, pdf_obj *id, int permissions, int algorithm)
1447
0
{
1448
0
  pdf_crypt *crypt;
1449
0
  int v, r, method, length;
1450
0
  unsigned char opwd[2048];
1451
0
  unsigned char upwd[2048];
1452
0
  size_t opwdlen, upwdlen;
1453
1454
0
  crypt = fz_malloc_struct(ctx, pdf_crypt);
1455
1456
  /* Extract file identifier string */
1457
1458
0
  if (pdf_is_string(ctx, id))
1459
0
    crypt->id = pdf_keep_obj(ctx, id);
1460
0
  else
1461
0
    fz_warn(ctx, "missing file identifier, may not be able to do decryption");
1462
1463
0
  switch (algorithm)
1464
0
  {
1465
0
  case PDF_ENCRYPT_RC4_40:
1466
0
    v = 1; r = 2; method = PDF_CRYPT_RC4; length = 40; break;
1467
0
  case PDF_ENCRYPT_RC4_128:
1468
0
    v = 2; r = 3; method = PDF_CRYPT_RC4; length = 128; break;
1469
0
  case PDF_ENCRYPT_AES_128:
1470
0
    v = 4; r = 4; method = PDF_CRYPT_AESV2; length = 128; break;
1471
0
  case PDF_ENCRYPT_AES_256:
1472
0
    v = 5; r = 6; method = PDF_CRYPT_AESV3; length = 256; break;
1473
0
  default:
1474
0
    fz_throw(ctx, FZ_ERROR_GENERIC, "invalid encryption method");
1475
0
  }
1476
1477
0
  crypt->v = v;
1478
0
  crypt->r = r;
1479
0
  crypt->length = length;
1480
0
  crypt->cf = NULL;
1481
0
  crypt->stmf.method = method;
1482
0
  crypt->stmf.length = length;
1483
0
  crypt->strf.method = method;
1484
0
  crypt->strf.length = length;
1485
0
  crypt->encrypt_metadata = 1;
1486
0
  crypt->p = (permissions & 0xf3c) | 0xfffff0c0;
1487
0
  memset(crypt->o, 0, sizeof (crypt->o));
1488
0
  memset(crypt->u, 0, sizeof (crypt->u));
1489
0
  memset(crypt->oe, 0, sizeof (crypt->oe));
1490
0
  memset(crypt->ue, 0, sizeof (crypt->ue));
1491
1492
0
  if (crypt->r <= 4)
1493
0
  {
1494
0
    pdf_docenc_from_utf8((char *) opwd, opwd_utf8, sizeof opwd);
1495
0
    pdf_docenc_from_utf8((char *) upwd, upwd_utf8, sizeof upwd);
1496
0
  }
1497
0
  else
1498
0
  {
1499
0
    pdf_saslprep_from_utf8((char *) opwd, opwd_utf8, sizeof opwd);
1500
0
    pdf_saslprep_from_utf8((char *) upwd, upwd_utf8, sizeof upwd);
1501
0
  }
1502
1503
0
  opwdlen = strlen((char *) opwd);
1504
0
  upwdlen = strlen((char *) upwd);
1505
1506
0
  if (crypt->r <= 4)
1507
0
  {
1508
0
    pdf_compute_owner_password(ctx, crypt, opwd, opwdlen, upwd, upwdlen, crypt->o);
1509
0
    pdf_compute_user_password(ctx, crypt, upwd, upwdlen, crypt->u);
1510
0
  }
1511
0
  else if (crypt->r == 6)
1512
0
  {
1513
    /* 7.6.4.4.1 states that the file encryption key are 256 random bits. */
1514
0
    fz_memrnd(ctx, crypt->key, nelem(crypt->key));
1515
1516
0
    pdf_compute_user_password_r6(ctx, crypt, upwd, upwdlen, crypt->u, crypt->ue);
1517
0
    pdf_compute_owner_password_r6(ctx, crypt, opwd, opwdlen, crypt->o, crypt->oe);
1518
0
    pdf_compute_permissions_r6(ctx, crypt, crypt->perms);
1519
0
  }
1520
1521
0
  return crypt;
1522
0
}