pdf_crypt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. #include "fitz-internal.h"
  2. #include "mupdf-internal.h"
  3. enum
  4. {
  5. PDF_CRYPT_NONE,
  6. PDF_CRYPT_RC4,
  7. PDF_CRYPT_AESV2,
  8. PDF_CRYPT_AESV3,
  9. PDF_CRYPT_UNKNOWN,
  10. };
  11. typedef struct pdf_crypt_filter_s pdf_crypt_filter;
  12. struct pdf_crypt_filter_s
  13. {
  14. int method;
  15. int length;
  16. };
  17. struct pdf_crypt_s
  18. {
  19. pdf_obj *id;
  20. int v;
  21. int length;
  22. pdf_obj *cf;
  23. pdf_crypt_filter stmf;
  24. pdf_crypt_filter strf;
  25. int r;
  26. unsigned char o[48];
  27. unsigned char u[48];
  28. unsigned char oe[32];
  29. unsigned char ue[32];
  30. int p;
  31. int encrypt_metadata;
  32. unsigned char key[32]; /* decryption key generated from password */
  33. fz_context *ctx;
  34. };
  35. static void pdf_parse_crypt_filter(fz_context *ctx, pdf_crypt_filter *cf, pdf_crypt *crypt, char *name);
  36. /*
  37. * Create crypt object for decrypting strings and streams
  38. * given the Encryption and ID objects.
  39. */
  40. pdf_crypt *
  41. pdf_new_crypt(fz_context *ctx, pdf_obj *dict, pdf_obj *id)
  42. {
  43. pdf_crypt *crypt;
  44. pdf_obj *obj;
  45. crypt = fz_malloc_struct(ctx, pdf_crypt);
  46. /* Common to all security handlers (PDF 1.7 table 3.18) */
  47. obj = pdf_dict_gets(dict, "Filter");
  48. if (!pdf_is_name(obj))
  49. {
  50. pdf_free_crypt(ctx, crypt);
  51. fz_throw(ctx, "unspecified encryption handler");
  52. }
  53. if (strcmp(pdf_to_name(obj), "Standard") != 0)
  54. {
  55. pdf_free_crypt(ctx, crypt);
  56. fz_throw(ctx, "unknown encryption handler: '%s'", pdf_to_name(obj));
  57. }
  58. crypt->v = 0;
  59. obj = pdf_dict_gets(dict, "V");
  60. if (pdf_is_int(obj))
  61. crypt->v = pdf_to_int(obj);
  62. if (crypt->v != 1 && crypt->v != 2 && crypt->v != 4 && crypt->v != 5)
  63. {
  64. pdf_free_crypt(ctx, crypt);
  65. fz_throw(ctx, "unknown encryption version");
  66. }
  67. /* Standard security handler (PDF 1.7 table 3.19) */
  68. obj = pdf_dict_gets(dict, "R");
  69. if (pdf_is_int(obj))
  70. crypt->r = pdf_to_int(obj);
  71. else if (crypt->v <= 4)
  72. {
  73. fz_warn(ctx, "encryption dictionary missing revision value, guessing...");
  74. if (crypt->v < 2)
  75. crypt->r = 2;
  76. else if (crypt->v == 2)
  77. crypt->r = 3;
  78. else if (crypt->v == 4)
  79. crypt->r = 4;
  80. }
  81. else
  82. {
  83. pdf_free_crypt(ctx, crypt);
  84. fz_throw(ctx, "encryption dictionary missing version and revision value");
  85. }
  86. obj = pdf_dict_gets(dict, "O");
  87. if (pdf_is_string(obj) && pdf_to_str_len(obj) == 32)
  88. memcpy(crypt->o, pdf_to_str_buf(obj), 32);
  89. /* /O and /U are supposed to be 48 bytes long for revision 5 and 6, they're often longer, though */
  90. else if (crypt->r >= 5 && pdf_is_string(obj) && pdf_to_str_len(obj) >= 48)
  91. memcpy(crypt->o, pdf_to_str_buf(obj), 48);
  92. else
  93. {
  94. pdf_free_crypt(ctx, crypt);
  95. fz_throw(ctx, "encryption dictionary missing owner password");
  96. }
  97. obj = pdf_dict_gets(dict, "U");
  98. if (pdf_is_string(obj) && pdf_to_str_len(obj) == 32)
  99. memcpy(crypt->u, pdf_to_str_buf(obj), 32);
  100. /* /O and /U are supposed to be 48 bytes long for revision 5 and 6, they're often longer, though */
  101. else if (crypt->r >= 5 && pdf_is_string(obj) && pdf_to_str_len(obj) >= 48)
  102. memcpy(crypt->u, pdf_to_str_buf(obj), 48);
  103. else if (pdf_is_string(obj) && pdf_to_str_len(obj) < 32)
  104. {
  105. fz_warn(ctx, "encryption password key too short (%d)", pdf_to_str_len(obj));
  106. memcpy(crypt->u, pdf_to_str_buf(obj), pdf_to_str_len(obj));
  107. }
  108. else
  109. {
  110. pdf_free_crypt(ctx, crypt);
  111. fz_throw(ctx, "encryption dictionary missing user password");
  112. }
  113. obj = pdf_dict_gets(dict, "P");
  114. if (pdf_is_int(obj))
  115. crypt->p = pdf_to_int(obj);
  116. else
  117. {
  118. fz_warn(ctx, "encryption dictionary missing permissions");
  119. crypt->p = 0xfffffffc;
  120. }
  121. if (crypt->r == 5 || crypt->r == 6)
  122. {
  123. obj = pdf_dict_gets(dict, "OE");
  124. if (!pdf_is_string(obj) || pdf_to_str_len(obj) != 32)
  125. {
  126. pdf_free_crypt(ctx, crypt);
  127. fz_throw(ctx, "encryption dictionary missing owner encryption key");
  128. }
  129. memcpy(crypt->oe, pdf_to_str_buf(obj), 32);
  130. obj = pdf_dict_gets(dict, "UE");
  131. if (!pdf_is_string(obj) || pdf_to_str_len(obj) != 32)
  132. {
  133. pdf_free_crypt(ctx, crypt);
  134. fz_throw(ctx, "encryption dictionary missing user encryption key");
  135. }
  136. memcpy(crypt->ue, pdf_to_str_buf(obj), 32);
  137. }
  138. crypt->encrypt_metadata = 1;
  139. obj = pdf_dict_gets(dict, "EncryptMetadata");
  140. if (pdf_is_bool(obj))
  141. crypt->encrypt_metadata = pdf_to_bool(obj);
  142. /* Extract file identifier string */
  143. if (pdf_is_array(id) && pdf_array_len(id) == 2)
  144. {
  145. obj = pdf_array_get(id, 0);
  146. if (pdf_is_string(obj))
  147. crypt->id = pdf_keep_obj(obj);
  148. }
  149. else
  150. fz_warn(ctx, "missing file identifier, may not be able to do decryption");
  151. /* Determine encryption key length */
  152. crypt->length = 40;
  153. if (crypt->v == 2 || crypt->v == 4)
  154. {
  155. obj = pdf_dict_gets(dict, "Length");
  156. if (pdf_is_int(obj))
  157. crypt->length = pdf_to_int(obj);
  158. /* work-around for pdf generators that assume length is in bytes */
  159. if (crypt->length < 40)
  160. crypt->length = crypt->length * 8;
  161. if (crypt->length % 8 != 0)
  162. {
  163. pdf_free_crypt(ctx, crypt);
  164. fz_throw(ctx, "invalid encryption key length");
  165. }
  166. if (crypt->length < 0 || crypt->length > 256)
  167. {
  168. pdf_free_crypt(ctx, crypt);
  169. fz_throw(ctx, "invalid encryption key length");
  170. }
  171. }
  172. if (crypt->v == 5)
  173. crypt->length = 256;
  174. if (crypt->v == 1 || crypt->v == 2)
  175. {
  176. crypt->stmf.method = PDF_CRYPT_RC4;
  177. crypt->stmf.length = crypt->length;
  178. crypt->strf.method = PDF_CRYPT_RC4;
  179. crypt->strf.length = crypt->length;
  180. }
  181. if (crypt->v == 4 || crypt->v == 5)
  182. {
  183. crypt->stmf.method = PDF_CRYPT_NONE;
  184. crypt->stmf.length = crypt->length;
  185. crypt->strf.method = PDF_CRYPT_NONE;
  186. crypt->strf.length = crypt->length;
  187. obj = pdf_dict_gets(dict, "CF");
  188. if (pdf_is_dict(obj))
  189. {
  190. crypt->cf = pdf_keep_obj(obj);
  191. }
  192. else
  193. {
  194. crypt->cf = NULL;
  195. }
  196. fz_try(ctx)
  197. {
  198. obj = pdf_dict_gets(dict, "StmF");
  199. if (pdf_is_name(obj))
  200. pdf_parse_crypt_filter(ctx, &crypt->stmf, crypt, pdf_to_name(obj));
  201. obj = pdf_dict_gets(dict, "StrF");
  202. if (pdf_is_name(obj))
  203. pdf_parse_crypt_filter(ctx, &crypt->strf, crypt, pdf_to_name(obj));
  204. }
  205. fz_catch(ctx)
  206. {
  207. pdf_free_crypt(ctx, crypt);
  208. fz_throw(ctx, "cannot parse string crypt filter (%d %d R)", pdf_to_num(obj), pdf_to_gen(obj));
  209. }
  210. /* in crypt revision 4, the crypt filter determines the key length */
  211. if (crypt->strf.method != PDF_CRYPT_NONE)
  212. crypt->length = crypt->stmf.length;
  213. }
  214. return crypt;
  215. }
  216. void
  217. pdf_free_crypt(fz_context *ctx, pdf_crypt *crypt)
  218. {
  219. pdf_drop_obj(crypt->id);
  220. pdf_drop_obj(crypt->cf);
  221. fz_free(ctx, crypt);
  222. }
  223. /*
  224. * Parse a CF dictionary entry (PDF 1.7 table 3.22)
  225. */
  226. static void
  227. pdf_parse_crypt_filter(fz_context *ctx, pdf_crypt_filter *cf, pdf_crypt *crypt, char *name)
  228. {
  229. pdf_obj *obj;
  230. pdf_obj *dict;
  231. int is_identity = (strcmp(name, "Identity") == 0);
  232. int is_stdcf = (!is_identity && (strcmp(name, "StdCF") == 0));
  233. if (!is_identity && !is_stdcf)
  234. fz_throw(ctx, "Crypt Filter not Identity or StdCF (%d %d R)", pdf_to_num(crypt->cf), pdf_to_gen(crypt->cf));
  235. cf->method = PDF_CRYPT_NONE;
  236. cf->length = crypt->length;
  237. if (!crypt->cf)
  238. {
  239. cf->method = (is_identity ? PDF_CRYPT_NONE : PDF_CRYPT_RC4);
  240. return;
  241. }
  242. dict = pdf_dict_gets(crypt->cf, name);
  243. if (!pdf_is_dict(dict))
  244. fz_throw(ctx, "cannot parse crypt filter (%d %d R)", pdf_to_num(crypt->cf), pdf_to_gen(crypt->cf));
  245. obj = pdf_dict_gets(dict, "CFM");
  246. if (pdf_is_name(obj))
  247. {
  248. if (!strcmp(pdf_to_name(obj), "None"))
  249. cf->method = PDF_CRYPT_NONE;
  250. else if (!strcmp(pdf_to_name(obj), "V2"))
  251. cf->method = PDF_CRYPT_RC4;
  252. else if (!strcmp(pdf_to_name(obj), "AESV2"))
  253. cf->method = PDF_CRYPT_AESV2;
  254. else if (!strcmp(pdf_to_name(obj), "AESV3"))
  255. cf->method = PDF_CRYPT_AESV3;
  256. else
  257. fz_warn(ctx, "unknown encryption method: %s", pdf_to_name(obj));
  258. }
  259. obj = pdf_dict_gets(dict, "Length");
  260. if (pdf_is_int(obj))
  261. cf->length = pdf_to_int(obj);
  262. /* the length for crypt filters is supposed to be in bytes not bits */
  263. if (cf->length < 40)
  264. cf->length = cf->length * 8;
  265. if ((cf->length % 8) != 0)
  266. fz_throw(ctx, "invalid key length: %d", cf->length);
  267. if ((crypt->r == 1 || crypt->r == 2 || crypt->r == 4) &&
  268. (cf->length < 0 || cf->length > 128))
  269. fz_throw(ctx, "invalid key length: %d", cf->length);
  270. if ((crypt->r == 5 || crypt->r == 6) && cf->length != 256)
  271. fz_throw(ctx, "invalid key length: %d", cf->length);
  272. }
  273. /*
  274. * Compute an encryption key (PDF 1.7 algorithm 3.2)
  275. */
  276. static const unsigned char padding[32] =
  277. {
  278. 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41,
  279. 0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08,
  280. 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80,
  281. 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a
  282. };
  283. static void
  284. pdf_compute_encryption_key(pdf_crypt *crypt, unsigned char *password, int pwlen, unsigned char *key)
  285. {
  286. unsigned char buf[32];
  287. unsigned int p;
  288. int i, n;
  289. fz_md5 md5;
  290. n = crypt->length / 8;
  291. /* Step 1 - copy and pad password string */
  292. if (pwlen > 32)
  293. pwlen = 32;
  294. memcpy(buf, password, pwlen);
  295. memcpy(buf + pwlen, padding, 32 - pwlen);
  296. /* Step 2 - init md5 and pass value of step 1 */
  297. fz_md5_init(&md5);
  298. fz_md5_update(&md5, buf, 32);
  299. /* Step 3 - pass O value */
  300. fz_md5_update(&md5, crypt->o, 32);
  301. /* Step 4 - pass P value as unsigned int, low-order byte first */
  302. p = (unsigned int) crypt->p;
  303. buf[0] = (p) & 0xFF;
  304. buf[1] = (p >> 8) & 0xFF;
  305. buf[2] = (p >> 16) & 0xFF;
  306. buf[3] = (p >> 24) & 0xFF;
  307. fz_md5_update(&md5, buf, 4);
  308. /* Step 5 - pass first element of ID array */
  309. fz_md5_update(&md5, (unsigned char *)pdf_to_str_buf(crypt->id), pdf_to_str_len(crypt->id));
  310. /* Step 6 (revision 4 or greater) - if metadata is not encrypted pass 0xFFFFFFFF */
  311. if (crypt->r >= 4)
  312. {
  313. if (!crypt->encrypt_metadata)
  314. {
  315. buf[0] = 0xFF;
  316. buf[1] = 0xFF;
  317. buf[2] = 0xFF;
  318. buf[3] = 0xFF;
  319. fz_md5_update(&md5, buf, 4);
  320. }
  321. }
  322. /* Step 7 - finish the hash */
  323. fz_md5_final(&md5, buf);
  324. /* Step 8 (revision 3 or greater) - do some voodoo 50 times */
  325. if (crypt->r >= 3)
  326. {
  327. for (i = 0; i < 50; i++)
  328. {
  329. fz_md5_init(&md5);
  330. fz_md5_update(&md5, buf, n);
  331. fz_md5_final(&md5, buf);
  332. }
  333. }
  334. /* Step 9 - the key is the first 'n' bytes of the result */
  335. memcpy(key, buf, n);
  336. }
  337. /*
  338. * Compute an encryption key (PDF 1.7 ExtensionLevel 3 algorithm 3.2a)
  339. */
  340. static void
  341. pdf_compute_encryption_key_r5(fz_context *ctx, pdf_crypt *crypt, unsigned char *password, int pwlen, int ownerkey, unsigned char *validationkey)
  342. {
  343. unsigned char buffer[128 + 8 + 48];
  344. fz_sha256 sha256;
  345. fz_aes aes;
  346. /* Step 2 - truncate UTF-8 password to 127 characters */
  347. if (pwlen > 127)
  348. pwlen = 127;
  349. /* Step 3/4 - test password against owner/user key and compute encryption key */
  350. memcpy(buffer, password, pwlen);
  351. if (ownerkey)
  352. {
  353. memcpy(buffer + pwlen, crypt->o + 32, 8);
  354. memcpy(buffer + pwlen + 8, crypt->u, 48);
  355. }
  356. else
  357. memcpy(buffer + pwlen, crypt->u + 32, 8);
  358. fz_sha256_init(&sha256);
  359. fz_sha256_update(&sha256, buffer, pwlen + 8 + (ownerkey ? 48 : 0));
  360. fz_sha256_final(&sha256, validationkey);
  361. /* Step 3.5/4.5 - compute file encryption key from OE/UE */
  362. memcpy(buffer + pwlen, crypt->u + 40, 8);
  363. fz_sha256_init(&sha256);
  364. fz_sha256_update(&sha256, buffer, pwlen + 8);
  365. fz_sha256_final(&sha256, buffer);
  366. /* clear password buffer and use it as iv */
  367. memset(buffer + 32, 0, sizeof(buffer) - 32);
  368. if (aes_setkey_dec(&aes, buffer, crypt->length))
  369. fz_throw(ctx, "AES key init failed (keylen=%d)", crypt->length);
  370. aes_crypt_cbc(&aes, AES_DECRYPT, 32, buffer + 32, ownerkey ? crypt->oe : crypt->ue, crypt->key);
  371. }
  372. /*
  373. * Compute an encryption key (PDF 1.7 ExtensionLevel 8 algorithm)
  374. *
  375. * Adobe has not yet released the details, so the algorithm reference is:
  376. * http://esec-lab.sogeti.com/post/The-undocumented-password-validation-algorithm-of-Adobe-Reader-X
  377. */
  378. static void
  379. pdf_compute_hardened_hash_r6(fz_context *ctx, unsigned char *password, int pwlen, unsigned char salt[16], unsigned char *ownerkey, unsigned char hash[32])
  380. {
  381. unsigned char data[(128 + 64 + 48) * 64];
  382. unsigned char block[64];
  383. int block_size = 32;
  384. int data_len = 0;
  385. int i, j, sum;
  386. fz_sha256 sha256;
  387. fz_sha384 sha384;
  388. fz_sha512 sha512;
  389. fz_aes aes;
  390. /* Step 1: calculate initial data block */
  391. fz_sha256_init(&sha256);
  392. fz_sha256_update(&sha256, password, pwlen);
  393. fz_sha256_update(&sha256, salt, 8);
  394. if (ownerkey)
  395. fz_sha256_update(&sha256, ownerkey, 48);
  396. fz_sha256_final(&sha256, block);
  397. for (i = 0; i < 64 || i < data[data_len * 64 - 1] + 32; i++)
  398. {
  399. /* Step 2: repeat password and data block 64 times */
  400. memcpy(data, password, pwlen);
  401. memcpy(data + pwlen, block, block_size);
  402. memcpy(data + pwlen + block_size, ownerkey, ownerkey ? 48 : 0);
  403. data_len = pwlen + block_size + (ownerkey ? 48 : 0);
  404. for (j = 1; j < 64; j++)
  405. memcpy(data + j * data_len, data, data_len);
  406. /* Step 3: encrypt data using data block as key and iv */
  407. if (aes_setkey_enc(&aes, block, 128))
  408. fz_throw(ctx, "AES key init failed (keylen=%d)", 128);
  409. aes_crypt_cbc(&aes, AES_ENCRYPT, data_len * 64, block + 16, data, data);
  410. /* Step 4: determine SHA-2 hash size for this round */
  411. for (j = 0, sum = 0; j < 16; j++)
  412. sum += data[j];
  413. /* Step 5: calculate data block for next round */
  414. block_size = 32 + (sum % 3) * 16;
  415. switch (block_size)
  416. {
  417. case 32:
  418. fz_sha256_init(&sha256);
  419. fz_sha256_update(&sha256, data, data_len * 64);
  420. fz_sha256_final(&sha256, block);
  421. break;
  422. case 48:
  423. fz_sha384_init(&sha384);
  424. fz_sha384_update(&sha384, data, data_len * 64);
  425. fz_sha384_final(&sha384, block);
  426. break;
  427. case 64:
  428. fz_sha512_init(&sha512);
  429. fz_sha512_update(&sha512, data, data_len * 64);
  430. fz_sha512_final(&sha512, block);
  431. break;
  432. }
  433. }
  434. memset(data, 0, sizeof(data));
  435. memcpy(hash, block, 32);
  436. }
  437. static void
  438. pdf_compute_encryption_key_r6(fz_context *ctx, pdf_crypt *crypt, unsigned char *password, int pwlen, int ownerkey, unsigned char *validationkey)
  439. {
  440. unsigned char hash[32];
  441. unsigned char iv[16];
  442. fz_aes aes;
  443. if (pwlen > 127)
  444. pwlen = 127;
  445. pdf_compute_hardened_hash_r6(ctx, password, pwlen,
  446. (ownerkey ? crypt->o : crypt->u) + 32,
  447. ownerkey ? crypt->u : NULL, validationkey);
  448. pdf_compute_hardened_hash_r6(ctx, password, pwlen,
  449. crypt->u + 40, NULL, hash);
  450. memset(iv, 0, sizeof(iv));
  451. if (aes_setkey_dec(&aes, hash, 256))
  452. fz_throw(ctx, "AES key init failed (keylen=256)");
  453. aes_crypt_cbc(&aes, AES_DECRYPT, 32, iv,
  454. ownerkey ? crypt->oe : crypt->ue, crypt->key);
  455. }
  456. /*
  457. * Computing the user password (PDF 1.7 algorithm 3.4 and 3.5)
  458. * Also save the generated key for decrypting objects and streams in crypt->key.
  459. */
  460. static void
  461. pdf_compute_user_password(fz_context *ctx, pdf_crypt *crypt, unsigned char *password, int pwlen, unsigned char *output)
  462. {
  463. if (crypt->r == 2)
  464. {
  465. fz_arc4 arc4;
  466. pdf_compute_encryption_key(crypt, password, pwlen, crypt->key);
  467. fz_arc4_init(&arc4, crypt->key, crypt->length / 8);
  468. fz_arc4_encrypt(&arc4, output, padding, 32);
  469. }
  470. if (crypt->r == 3 || crypt->r == 4)
  471. {
  472. unsigned char xor[32];
  473. unsigned char digest[16];
  474. fz_md5 md5;
  475. fz_arc4 arc4;
  476. int i, x, n;
  477. n = crypt->length / 8;
  478. pdf_compute_encryption_key(crypt, password, pwlen, crypt->key);
  479. fz_md5_init(&md5);
  480. fz_md5_update(&md5, padding, 32);
  481. fz_md5_update(&md5, (unsigned char*)pdf_to_str_buf(crypt->id), pdf_to_str_len(crypt->id));
  482. fz_md5_final(&md5, digest);
  483. fz_arc4_init(&arc4, crypt->key, n);
  484. fz_arc4_encrypt(&arc4, output, digest, 16);
  485. for (x = 1; x <= 19; x++)
  486. {
  487. for (i = 0; i < n; i++)
  488. xor[i] = crypt->key[i] ^ x;
  489. fz_arc4_init(&arc4, xor, n);
  490. fz_arc4_encrypt(&arc4, output, output, 16);
  491. }
  492. memcpy(output + 16, padding, 16);
  493. }
  494. if (crypt->r == 5)
  495. {
  496. pdf_compute_encryption_key_r5(ctx, crypt, password, pwlen, 0, output);
  497. }
  498. if (crypt->r == 6)
  499. {
  500. pdf_compute_encryption_key_r6(ctx, crypt, password, pwlen, 0, output);
  501. }
  502. }
  503. /*
  504. * Authenticating the user password (PDF 1.7 algorithm 3.6
  505. * and ExtensionLevel 3 algorithm 3.11)
  506. * This also has the side effect of saving a key generated
  507. * from the password for decrypting objects and streams.
  508. */
  509. static int
  510. pdf_authenticate_user_password(fz_context *ctx, pdf_crypt *crypt, unsigned char *password, int pwlen)
  511. {
  512. unsigned char output[32];
  513. pdf_compute_user_password(ctx, crypt, password, pwlen, output);
  514. if (crypt->r == 2 || crypt->r == 5 || crypt->r == 6)
  515. return memcmp(output, crypt->u, 32) == 0;
  516. if (crypt->r == 3 || crypt->r == 4)
  517. return memcmp(output, crypt->u, 16) == 0;
  518. return 0;
  519. }
  520. /*
  521. * Authenticating the owner password (PDF 1.7 algorithm 3.7
  522. * and ExtensionLevel 3 algorithm 3.12)
  523. * Generates the user password from the owner password
  524. * and calls pdf_authenticate_user_password.
  525. */
  526. static int
  527. pdf_authenticate_owner_password(fz_context *ctx, pdf_crypt *crypt, unsigned char *ownerpass, int pwlen)
  528. {
  529. unsigned char pwbuf[32];
  530. unsigned char key[32];
  531. unsigned char xor[32];
  532. unsigned char userpass[32];
  533. int i, n, x;
  534. fz_md5 md5;
  535. fz_arc4 arc4;
  536. if (crypt->r == 5)
  537. {
  538. /* PDF 1.7 ExtensionLevel 3 algorithm 3.12 */
  539. pdf_compute_encryption_key_r5(ctx, crypt, ownerpass, pwlen, 1, key);
  540. return !memcmp(key, crypt->o, 32);
  541. }
  542. else if (crypt->r == 6)
  543. {
  544. /* PDF 1.7 ExtensionLevel 8 algorithm */
  545. pdf_compute_encryption_key_r6(ctx, crypt, ownerpass, pwlen, 1, key);
  546. return !memcmp(key, crypt->o, 32);
  547. }
  548. n = crypt->length / 8;
  549. /* Step 1 -- steps 1 to 4 of PDF 1.7 algorithm 3.3 */
  550. /* copy and pad password string */
  551. if (pwlen > 32)
  552. pwlen = 32;
  553. memcpy(pwbuf, ownerpass, pwlen);
  554. memcpy(pwbuf + pwlen, padding, 32 - pwlen);
  555. /* take md5 hash of padded password */
  556. fz_md5_init(&md5);
  557. fz_md5_update(&md5, pwbuf, 32);
  558. fz_md5_final(&md5, key);
  559. /* do some voodoo 50 times (Revision 3 or greater) */
  560. if (crypt->r >= 3)
  561. {
  562. for (i = 0; i < 50; i++)
  563. {
  564. fz_md5_init(&md5);
  565. fz_md5_update(&md5, key, 16);
  566. fz_md5_final(&md5, key);
  567. }
  568. }
  569. /* Step 2 (Revision 2) */
  570. if (crypt->r == 2)
  571. {
  572. fz_arc4_init(&arc4, key, n);
  573. fz_arc4_encrypt(&arc4, userpass, crypt->o, 32);
  574. }
  575. /* Step 2 (Revision 3 or greater) */
  576. if (crypt->r >= 3)
  577. {
  578. memcpy(userpass, crypt->o, 32);
  579. for (x = 0; x < 20; x++)
  580. {
  581. for (i = 0; i < n; i++)
  582. xor[i] = key[i] ^ (19 - x);
  583. fz_arc4_init(&arc4, xor, n);
  584. fz_arc4_encrypt(&arc4, userpass, userpass, 32);
  585. }
  586. }
  587. return pdf_authenticate_user_password(ctx, crypt, userpass, 32);
  588. }
  589. int
  590. pdf_authenticate_password(pdf_document *xref, char *password)
  591. {
  592. if (xref->crypt)
  593. {
  594. if (!password)
  595. password = "";
  596. if (pdf_authenticate_user_password(xref->ctx, xref->crypt, (unsigned char *)password, strlen(password)))
  597. return 1;
  598. if (pdf_authenticate_owner_password(xref->ctx, xref->crypt, (unsigned char *)password, strlen(password)))
  599. return 1;
  600. return 0;
  601. }
  602. return 1;
  603. }
  604. int
  605. pdf_needs_password(pdf_document *xref)
  606. {
  607. if (!xref->crypt)
  608. return 0;
  609. if (pdf_authenticate_password(xref, ""))
  610. return 0;
  611. return 1;
  612. }
  613. int
  614. pdf_has_permission(pdf_document *xref, int p)
  615. {
  616. if (!xref->crypt)
  617. return 1;
  618. return xref->crypt->p & p;
  619. }
  620. unsigned char *
  621. pdf_crypt_key(pdf_document *xref)
  622. {
  623. if (xref->crypt)
  624. return xref->crypt->key;
  625. return NULL;
  626. }
  627. int
  628. pdf_crypt_version(pdf_document *xref)
  629. {
  630. if (xref->crypt)
  631. return xref->crypt->v;
  632. return 0;
  633. }
  634. int pdf_crypt_revision(pdf_document *xref)
  635. {
  636. if (xref->crypt)
  637. return xref->crypt->r;
  638. return 0;
  639. }
  640. char *
  641. pdf_crypt_method(pdf_document *xref)
  642. {
  643. if (xref->crypt)
  644. {
  645. switch (xref->crypt->strf.method)
  646. {
  647. case PDF_CRYPT_NONE: return "None";
  648. case PDF_CRYPT_RC4: return "RC4";
  649. case PDF_CRYPT_AESV2: return "AES";
  650. case PDF_CRYPT_AESV3: return "AES";
  651. case PDF_CRYPT_UNKNOWN: return "Unknown";
  652. }
  653. }
  654. return "None";
  655. }
  656. int
  657. pdf_crypt_length(pdf_document *xref)
  658. {
  659. if (xref->crypt)
  660. return xref->crypt->length;
  661. return 0;
  662. }
  663. /*
  664. * PDF 1.7 algorithm 3.1 and ExtensionLevel 3 algorithm 3.1a
  665. *
  666. * Using the global encryption key that was generated from the
  667. * password, create a new key that is used to decrypt individual
  668. * objects and streams. This key is based on the object and
  669. * generation numbers.
  670. */
  671. static int
  672. pdf_compute_object_key(pdf_crypt *crypt, pdf_crypt_filter *cf, int num, int gen, unsigned char *key, int max_len)
  673. {
  674. fz_md5 md5;
  675. unsigned char message[5];
  676. int key_len = crypt->length / 8;
  677. if (key_len > max_len)
  678. key_len = max_len;
  679. if (cf->method == PDF_CRYPT_AESV3)
  680. {
  681. memcpy(key, crypt->key, key_len);
  682. return key_len;
  683. }
  684. fz_md5_init(&md5);
  685. fz_md5_update(&md5, crypt->key, key_len);
  686. message[0] = (num) & 0xFF;
  687. message[1] = (num >> 8) & 0xFF;
  688. message[2] = (num >> 16) & 0xFF;
  689. message[3] = (gen) & 0xFF;
  690. message[4] = (gen >> 8) & 0xFF;
  691. fz_md5_update(&md5, message, 5);
  692. if (cf->method == PDF_CRYPT_AESV2)
  693. fz_md5_update(&md5, (unsigned char *)"sAlT", 4);
  694. fz_md5_final(&md5, key);
  695. if (key_len + 5 > 16)
  696. return 16;
  697. return key_len + 5;
  698. }
  699. /*
  700. * PDF 1.7 algorithm 3.1 and ExtensionLevel 3 algorithm 3.1a
  701. *
  702. * Decrypt all strings in obj modifying the data in-place.
  703. * Recurse through arrays and dictionaries, but do not follow
  704. * indirect references.
  705. */
  706. static void
  707. pdf_crypt_obj_imp(fz_context *ctx, pdf_crypt *crypt, pdf_obj *obj, unsigned char *key, int keylen)
  708. {
  709. unsigned char *s;
  710. int i, n;
  711. if (pdf_is_indirect(obj))
  712. return;
  713. if (pdf_is_string(obj))
  714. {
  715. s = (unsigned char *)pdf_to_str_buf(obj);
  716. n = pdf_to_str_len(obj);
  717. if (crypt->strf.method == PDF_CRYPT_RC4)
  718. {
  719. fz_arc4 arc4;
  720. fz_arc4_init(&arc4, key, keylen);
  721. fz_arc4_encrypt(&arc4, s, s, n);
  722. }
  723. if (crypt->strf.method == PDF_CRYPT_AESV2 || crypt->strf.method == PDF_CRYPT_AESV3)
  724. {
  725. if (n == 0)
  726. {
  727. /* Empty strings are permissible */
  728. }
  729. else if (n & 15 || n < 32)
  730. fz_warn(ctx, "invalid string length for aes encryption");
  731. else
  732. {
  733. unsigned char iv[16];
  734. fz_aes aes;
  735. memcpy(iv, s, 16);
  736. if (aes_setkey_dec(&aes, key, keylen * 8))
  737. fz_throw(ctx, "AES key init failed (keylen=%d)", keylen * 8);
  738. aes_crypt_cbc(&aes, AES_DECRYPT, n - 16, iv, s + 16, s);
  739. /* delete space used for iv and padding bytes at end */
  740. if (s[n - 17] < 1 || s[n - 17] > 16)
  741. fz_warn(ctx, "aes padding out of range");
  742. else
  743. pdf_set_str_len(obj, n - 16 - s[n - 17]);
  744. }
  745. }
  746. }
  747. else if (pdf_is_array(obj))
  748. {
  749. n = pdf_array_len(obj);
  750. for (i = 0; i < n; i++)
  751. {
  752. pdf_crypt_obj_imp(ctx, crypt, pdf_array_get(obj, i), key, keylen);
  753. }
  754. }
  755. else if (pdf_is_dict(obj))
  756. {
  757. n = pdf_dict_len(obj);
  758. for (i = 0; i < n; i++)
  759. {
  760. pdf_crypt_obj_imp(ctx, crypt, pdf_dict_get_val(obj, i), key, keylen);
  761. }
  762. }
  763. }
  764. void
  765. pdf_crypt_obj(fz_context *ctx, pdf_crypt *crypt, pdf_obj *obj, int num, int gen)
  766. {
  767. unsigned char key[32];
  768. int len;
  769. len = pdf_compute_object_key(crypt, &crypt->strf, num, gen, key, 32);
  770. pdf_crypt_obj_imp(ctx, crypt, obj, key, len);
  771. }
  772. /*
  773. * PDF 1.7 algorithm 3.1 and ExtensionLevel 3 algorithm 3.1a
  774. *
  775. * Create filter suitable for de/encrypting a stream.
  776. */
  777. static fz_stream *
  778. pdf_open_crypt_imp(fz_stream *chain, pdf_crypt *crypt, pdf_crypt_filter *stmf, int num, int gen)
  779. {
  780. unsigned char key[32];
  781. int len;
  782. crypt->ctx = chain->ctx;
  783. len = pdf_compute_object_key(crypt, stmf, num, gen, key, 32);
  784. if (stmf->method == PDF_CRYPT_RC4)
  785. return fz_open_arc4(chain, key, len);
  786. if (stmf->method == PDF_CRYPT_AESV2 || stmf->method == PDF_CRYPT_AESV3)
  787. return fz_open_aesd(chain, key, len);
  788. return fz_open_copy(chain);
  789. }
  790. fz_stream *
  791. pdf_open_crypt(fz_stream *chain, pdf_crypt *crypt, int num, int gen)
  792. {
  793. return pdf_open_crypt_imp(chain, crypt, &crypt->stmf, num, gen);
  794. }
  795. fz_stream *
  796. pdf_open_crypt_with_filter(fz_stream *chain, pdf_crypt *crypt, char *name, int num, int gen)
  797. {
  798. if (strcmp(name, "Identity"))
  799. {
  800. pdf_crypt_filter cf;
  801. pdf_parse_crypt_filter(chain->ctx, &cf, crypt, name);
  802. return pdf_open_crypt_imp(chain, crypt, &cf, num, gen);
  803. }
  804. return chain;
  805. }
  806. #ifndef NDEBUG
  807. void pdf_print_crypt(pdf_crypt *crypt)
  808. {
  809. int i;
  810. printf("crypt {\n");
  811. printf("\tv=%d length=%d\n", crypt->v, crypt->length);
  812. printf("\tstmf method=%d length=%d\n", crypt->stmf.method, crypt->stmf.length);
  813. printf("\tstrf method=%d length=%d\n", crypt->strf.method, crypt->strf.length);
  814. printf("\tr=%d\n", crypt->r);
  815. printf("\to=<");
  816. for (i = 0; i < 32; i++)
  817. printf("%02X", crypt->o[i]);
  818. printf(">\n");
  819. printf("\tu=<");
  820. for (i = 0; i < 32; i++)
  821. printf("%02X", crypt->u[i]);
  822. printf(">\n");
  823. printf("}\n");
  824. }
  825. #endif