pdf-crypt.c 39 KB

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