Encryption.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. * Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteBuffer.h>
  7. #include <AK/Debug.h>
  8. #include <AK/Random.h>
  9. #include <AK/UFixedBigIntDivision.h>
  10. #include <LibCrypto/Cipher/AES.h>
  11. #include <LibCrypto/Hash/HashManager.h>
  12. #include <LibCrypto/Hash/MD5.h>
  13. #include <LibPDF/CommonNames.h>
  14. #include <LibPDF/Document.h>
  15. #include <LibPDF/Encryption.h>
  16. namespace PDF {
  17. static constexpr Array<u8, 32> standard_encryption_key_padding_bytes = {
  18. 0x28,
  19. 0xBF,
  20. 0x4E,
  21. 0x5E,
  22. 0x4E,
  23. 0x75,
  24. 0x8A,
  25. 0x41,
  26. 0x64,
  27. 0x00,
  28. 0x4E,
  29. 0x56,
  30. 0xFF,
  31. 0xFA,
  32. 0x01,
  33. 0x08,
  34. 0x2E,
  35. 0x2E,
  36. 0x00,
  37. 0xB6,
  38. 0xD0,
  39. 0x68,
  40. 0x3E,
  41. 0x80,
  42. 0x2F,
  43. 0x0C,
  44. 0xA9,
  45. 0xFE,
  46. 0x64,
  47. 0x53,
  48. 0x69,
  49. 0x7A,
  50. };
  51. PDFErrorOr<NonnullRefPtr<SecurityHandler>> SecurityHandler::create(Document* document, NonnullRefPtr<DictObject> encryption_dict)
  52. {
  53. auto filter = TRY(encryption_dict->get_name(document, CommonNames::Filter))->name();
  54. if (filter == "Standard")
  55. return TRY(StandardSecurityHandler::create(document, encryption_dict));
  56. dbgln("Unrecognized security handler filter: {}", filter);
  57. TODO();
  58. }
  59. struct CryptFilter {
  60. CryptFilterMethod method { CryptFilterMethod::None };
  61. int length_in_bits { 0 };
  62. };
  63. static PDFErrorOr<CryptFilter> parse_v4_or_newer_crypt(Document* document, NonnullRefPtr<DictObject> encryption_dict, ByteString filter)
  64. {
  65. // See 3.5 Encryption, Table 3.18 "Entries common to all encryption dictionaries" for StmF and StrF,
  66. // and 3.5.4 Crypt Filters in the 1.7 spec, in particular Table 3.22 "Entries common to all crypt filter dictionaries".
  67. if (filter == "Identity")
  68. return CryptFilter {};
  69. // "Every crypt filter used in the document must have an entry in this dictionary"
  70. if (!encryption_dict->contains(CommonNames::CF))
  71. return Error(Error::Type::Parse, "Missing CF key in encryption dict for v4");
  72. auto crypt_filter_dicts = TRY(encryption_dict->get_dict(document, CommonNames::CF));
  73. if (!crypt_filter_dicts->contains(filter))
  74. return Error(Error::Type::Parse, "Missing key in CF dict for v4");
  75. auto crypt_filter_dict = TRY(crypt_filter_dicts->get_dict(document, filter));
  76. // "Default value: None"
  77. if (!crypt_filter_dict->contains(CommonNames::CFM))
  78. return CryptFilter {};
  79. auto crypt_filter_method = TRY(crypt_filter_dict->get_name(document, CommonNames::CFM))->name();
  80. if (crypt_filter_method == "None")
  81. return CryptFilter {};
  82. // Table 3.22 in the 1.7 spec says this is optional but doesn't give a default value.
  83. // But the 2.0 spec (ISO 32000 2020) says it's required.
  84. // The 2.0 spec also says "The standard security handler expresses the Length entry in bytes" (!).
  85. if (!crypt_filter_dict->contains(CommonNames::Length))
  86. return Error(Error::Type::Parse, "crypt filter /Length missing");
  87. auto length_in_bits = crypt_filter_dict->get_value(CommonNames::Length).get<int>() * 8;
  88. // NOTE: /CFM's /AuthEvent should be ignored for /StmF, /StrF.
  89. if (crypt_filter_method == "V2")
  90. return CryptFilter { CryptFilterMethod::V2, length_in_bits };
  91. if (crypt_filter_method == "AESV2") {
  92. // "the AES algorithm in Cipher Block Chaining (CBC) mode with a 16-byte block size [...] The key size (Length) shall be 128 bits."
  93. if (length_in_bits != 128)
  94. return Error(Error::Type::Parse, "Unexpected bit size for AESV2");
  95. return CryptFilter { CryptFilterMethod::AESV2, length_in_bits };
  96. }
  97. if (crypt_filter_method == "AESV3") {
  98. // "the AES-256 algorithm in Cipher Block Chaining (CBC) with padding mode with a 16-byte block size [...] The key size (Length) shall be 256 bits."
  99. if (length_in_bits != 256)
  100. return Error(Error::Type::Parse, "Unexpected bit size for AESV3");
  101. return CryptFilter { CryptFilterMethod::AESV3, length_in_bits };
  102. }
  103. return Error(Error::Type::Parse, "Unknown crypt filter method");
  104. }
  105. PDFErrorOr<NonnullRefPtr<StandardSecurityHandler>> StandardSecurityHandler::create(Document* document, NonnullRefPtr<DictObject> encryption_dict)
  106. {
  107. auto revision = encryption_dict->get_value(CommonNames::R).get<int>();
  108. auto o = TRY(encryption_dict->get_string(document, CommonNames::O))->string();
  109. auto u = TRY(encryption_dict->get_string(document, CommonNames::U))->string();
  110. auto p = encryption_dict->get_value(CommonNames::P).get<int>();
  111. // V, number: [...] 1 "Algorithm 1 Encryption of data using the RC4 or AES algorithms" in 7.6.2,
  112. // "General Encryption Algorithm," with an encryption key length of 40 bits, see below [...]
  113. // Length, integer: (Optional; PDF 1.4; only if V is 2 or 3) The length of the encryption key, in bits.
  114. // The value shall be a multiple of 8, in the range 40 to 128. Default value: 40.
  115. auto v = encryption_dict->get_value(CommonNames::V).get<int>();
  116. auto method = CryptFilterMethod::V2;
  117. size_t length_in_bits = 40;
  118. if (v >= 4) {
  119. // "Default value: Identity"
  120. ByteString stream_filter = "Identity";
  121. if (encryption_dict->contains(CommonNames::StmF))
  122. stream_filter = TRY(encryption_dict->get_name(document, CommonNames::StmF))->name();
  123. ByteString string_filter = "Identity";
  124. if (encryption_dict->contains(CommonNames::StrF))
  125. string_filter = TRY(encryption_dict->get_name(document, CommonNames::StrF))->name();
  126. if (stream_filter != string_filter)
  127. return Error(Error::Type::Parse, "Can't handle StmF and StrF being different");
  128. auto crypt_filter = TRY(parse_v4_or_newer_crypt(document, encryption_dict, stream_filter));
  129. method = crypt_filter.method;
  130. length_in_bits = crypt_filter.length_in_bits;
  131. } else if (encryption_dict->contains(CommonNames::Length))
  132. length_in_bits = encryption_dict->get_value(CommonNames::Length).get<int>();
  133. else if (v != 1)
  134. return Error(Error::Type::Parse, "Can't determine length of encryption key");
  135. auto length = length_in_bits / 8;
  136. dbgln_if(PDF_DEBUG, "encryption v{}, method {}, length {}", v, (int)method, length);
  137. bool encrypt_metadata = true;
  138. if (encryption_dict->contains(CommonNames::EncryptMetadata))
  139. encryption_dict->get_value(CommonNames::EncryptMetadata).get<bool>();
  140. ByteString oe, ue, perms;
  141. if (v >= 5) {
  142. oe = TRY(encryption_dict->get_string(document, CommonNames::OE))->string();
  143. ue = TRY(encryption_dict->get_string(document, CommonNames::UE))->string();
  144. perms = TRY(encryption_dict->get_string(document, CommonNames::Perms))->string();
  145. // O and U are 48 bytes for V == 5, but some files pad them with nul bytes to 127 bytes. So trim them, if necessary.
  146. if (o.length() > 48)
  147. o = o.substring(0, 48);
  148. if (u.length() > 48)
  149. u = u.substring(0, 48);
  150. if (o.length() != 48)
  151. return Error(Error::Type::Parse, "Invalid O size");
  152. if (oe.length() != 32)
  153. return Error(Error::Type::Parse, "Invalid OE size");
  154. if (u.length() != 48)
  155. return Error(Error::Type::Parse, "Invalid U size");
  156. if (ue.length() != 32)
  157. return Error(Error::Type::Parse, "Invalid UE size");
  158. if (perms.length() != 16)
  159. return Error(Error::Type::Parse, "Invalid Perms size");
  160. }
  161. return adopt_ref(*new StandardSecurityHandler(document, revision, o, oe, u, ue, perms, p, encrypt_metadata, length, method));
  162. }
  163. StandardSecurityHandler::StandardSecurityHandler(Document* document, size_t revision, ByteString const& o_entry, ByteString const& oe_entry, ByteString const& u_entry, ByteString const& ue_entry, ByteString const& perms_entry, u32 flags, bool encrypt_metadata, size_t length, CryptFilterMethod method)
  164. : m_document(document)
  165. , m_revision(revision)
  166. , m_o_entry(o_entry)
  167. , m_oe_entry(oe_entry)
  168. , m_u_entry(u_entry)
  169. , m_ue_entry(ue_entry)
  170. , m_perms_entry(perms_entry)
  171. , m_flags(flags)
  172. , m_encrypt_metadata(encrypt_metadata)
  173. , m_length(length)
  174. , m_method(method)
  175. {
  176. }
  177. ByteBuffer StandardSecurityHandler::compute_user_password_value_r2(ByteBuffer password_string)
  178. {
  179. // Algorithm 4: Computing the encryption dictionary's U (user password)
  180. // value (Security handlers of revision 2)
  181. // a) Create an encryption key based on the user password string, as
  182. // described in [Algorithm 2]
  183. auto encryption_key = compute_encryption_key_r2_to_r5(password_string);
  184. // b) Encrypt the 32-byte padding string shown in step (a) of [Algorithm 2],
  185. // using an RC4 encryption function with the encryption key from the
  186. // preceding step.
  187. RC4 rc4(encryption_key);
  188. auto output = rc4.encrypt(standard_encryption_key_padding_bytes);
  189. // c) Store the result of step (b) as the value of the U entry in the
  190. // encryption dictionary.
  191. return output;
  192. }
  193. ByteBuffer StandardSecurityHandler::compute_user_password_value_r3_to_r5(ByteBuffer password_string)
  194. {
  195. // Algorithm 5: Computing the encryption dictionary's U (user password)
  196. // value (Security handlers of revision 3 or greater)
  197. // a) Create an encryption key based on the user password string, as
  198. // described in [Algorithm 2]
  199. auto encryption_key = compute_encryption_key_r2_to_r5(password_string);
  200. // b) Initialize the MD5 hash function and pass the 32-byte padding string
  201. // shown in step (a) of [Algorithm 2] as input to this function
  202. Crypto::Hash::MD5 md5;
  203. md5.update(standard_encryption_key_padding_bytes);
  204. // e) Pass the first element of the file's file identifier array to the MD5
  205. // hash function.
  206. auto id_array = m_document->trailer()->get_array(m_document, CommonNames::ID).release_value_but_fixme_should_propagate_errors();
  207. auto first_element_string = id_array->get_string_at(m_document, 0).release_value_but_fixme_should_propagate_errors()->string();
  208. md5.update(first_element_string);
  209. // d) Encrypt the 16-byte result of the hash, using an RC4 encryption function
  210. // with the encryption key from step (a).
  211. RC4 rc4(encryption_key);
  212. auto out = md5.peek();
  213. auto buffer = rc4.encrypt(out.bytes());
  214. // e) Do the following 19 times:
  215. //
  216. // Take the output from the previous invocation of the RC4 function and pass
  217. // it as input to a new invocation of the function; use an encryption key generated
  218. // by taking each byte of the original encryption key obtained in step (a) and
  219. // performing an XOR operation between the that byte and the single-byte value of
  220. // the iteration counter (from 1 to 19).
  221. auto new_encryption_key = ByteBuffer::create_uninitialized(encryption_key.size()).release_value_but_fixme_should_propagate_errors();
  222. for (size_t i = 1; i <= 19; i++) {
  223. for (size_t j = 0; j < encryption_key.size(); j++)
  224. new_encryption_key[j] = encryption_key[j] ^ i;
  225. RC4 new_rc4(new_encryption_key);
  226. buffer = new_rc4.encrypt(buffer);
  227. }
  228. // f) Append 16 bytes of the arbitrary padding to the output from the final invocation
  229. // of the RC4 function and store the 32-byte result as the value of the U entry in
  230. // the encryption dictionary.
  231. VERIFY(buffer.size() == 16);
  232. for (size_t i = 0; i < 16; i++)
  233. buffer.append(0xab);
  234. return buffer;
  235. }
  236. bool StandardSecurityHandler::authenticate_user_password_r2_to_r5(StringView password_string)
  237. {
  238. // Algorithm 6: Authenticating the user password
  239. // a) Perform all but the last step of [Algorithm 4] or [Algorithm 5] using the
  240. // supplied password string.
  241. ByteBuffer password_buffer = ByteBuffer::copy(password_string.bytes()).release_value_but_fixme_should_propagate_errors();
  242. if (m_revision == 2) {
  243. password_buffer = compute_user_password_value_r2(password_buffer);
  244. } else {
  245. password_buffer = compute_user_password_value_r3_to_r5(password_buffer);
  246. }
  247. // b) If the result of step (a) is equal to the value of the encryption
  248. // dictionary's "U" entry (comparing the first 16 bytes in the case of security
  249. // handlers of revision 3 or greater), the password supplied is the correct user
  250. // password.
  251. auto u_bytes = m_u_entry.bytes();
  252. if (m_revision >= 3)
  253. return u_bytes.slice(0, 16) == password_buffer.bytes().slice(0, 16);
  254. return u_bytes == password_buffer.bytes();
  255. }
  256. bool StandardSecurityHandler::authenticate_user_password_r6_and_later(StringView password)
  257. {
  258. // ISO 32000 (PDF 2.0), 7.6.4.4.10 Algorithm 11: Authenticating the user password (Security handlers of
  259. // revision 6)
  260. // a) Test the password against the user key by computing the 32-byte hash using 7.6.4.3.4, "Algorithm 2.B:
  261. // Computing a hash (revision 6 or later)" with an input string consisting of the UTF-8 password
  262. // concatenated with the 8 bytes of User Validation Salt (see 7.6.4.4.7, "Algorithm 8: Computing the
  263. // encryption dictionary's U (user password) and UE (user encryption) values (Security handlers of
  264. // revision 6)"). If the 32- byte result matches the first 32 bytes of the U string, this is the user password.
  265. ByteBuffer input;
  266. input.append(password.bytes());
  267. input.append(m_u_entry.bytes().slice(32, 8)); // See comment in compute_encryption_key_r6_and_later() re "Validation Salt".
  268. auto hash = computing_a_hash_r6_and_later(input, password, HashKind::User);
  269. return hash == m_u_entry.bytes().trim(32);
  270. }
  271. bool StandardSecurityHandler::authenticate_owner_password_r6_and_later(StringView password)
  272. {
  273. // ISO 32000 (PDF 2.0), 7.6.4.4.11 Algorithm 12: Authenticating the owner password (Security handlers of
  274. // revision 6)
  275. // a) Test the password against the owner key by computing the 32-byte hash using algorithm 2.B with an
  276. // input string consisting of the UTF-8 password concatenated with the 8 bytes of Owner Validation Salt
  277. // and the 48 byte U string. If the 32- byte result matches the first 32 bytes of the O string, this is the owner
  278. // password.
  279. ByteBuffer input;
  280. input.append(password.bytes());
  281. input.append(m_o_entry.bytes().slice(32, 8)); // See comment in compute_encryption_key_r6_and_later() re "Validation Salt".
  282. input.append(m_u_entry.bytes());
  283. auto hash = computing_a_hash_r6_and_later(input, password, HashKind::Owner);
  284. return hash == m_o_entry.bytes().trim(32);
  285. }
  286. bool StandardSecurityHandler::try_provide_user_password(StringView password_string)
  287. {
  288. bool has_user_password;
  289. if (m_revision >= 6) {
  290. // This checks both owner and user password.
  291. auto password = ByteBuffer::copy(password_string.bytes()).release_value_but_fixme_should_propagate_errors();
  292. has_user_password = compute_encryption_key_r6_and_later(move(password));
  293. } else {
  294. has_user_password = authenticate_user_password_r2_to_r5(password_string);
  295. }
  296. if (!has_user_password)
  297. m_encryption_key = {};
  298. return has_user_password;
  299. }
  300. ByteBuffer StandardSecurityHandler::compute_encryption_key_r2_to_r5(ByteBuffer password_string)
  301. {
  302. // This function should never be called after we have a valid encryption key.
  303. VERIFY(!m_encryption_key.has_value());
  304. // 7.6.3.3 Encryption Key Algorithm
  305. // Algorithm 2: Computing an encryption key
  306. // a) Pad or truncate the password string to exactly 32 bytes. If the password string
  307. // is more than 32 bytes long, use only its first 32 bytes; if it is less than 32
  308. // bytes long, pad it by appending the required number of additional bytes from the
  309. // beginning of the following padding string: [omitted]
  310. if (password_string.size() > 32) {
  311. password_string.resize(32);
  312. } else {
  313. password_string.append(standard_encryption_key_padding_bytes.data(), 32 - password_string.size());
  314. }
  315. // b) Initialize the MD5 hash function and pass the result of step (a) as input to
  316. // this function.
  317. Crypto::Hash::MD5 md5;
  318. md5.update(password_string);
  319. // c) Pass the value of the encryption dictionary's "O" entry to the MD5 hash function.
  320. md5.update(m_o_entry);
  321. // d) Convert the integer value of the P entry to a 32-bit unsigned binary number and pass
  322. // these bytes to the MD5 hash function, low-order byte first.
  323. md5.update(reinterpret_cast<u8 const*>(&m_flags), sizeof(m_flags));
  324. // e) Pass the first element of the file's file identifier array to the MD5 hash function.
  325. auto id_array = m_document->trailer()->get_array(m_document, CommonNames::ID).release_value_but_fixme_should_propagate_errors();
  326. auto first_element_string = id_array->get_string_at(m_document, 0).release_value_but_fixme_should_propagate_errors()->string();
  327. md5.update(first_element_string);
  328. // f) (Security handlers of revision 4 or greater) if the document metadata is not being
  329. // encrypted, pass 4 bytes with the value 0xffffffff to the MD5 hash function.
  330. if (m_revision >= 4 && !m_encrypt_metadata) {
  331. u32 value = 0xffffffff;
  332. md5.update(reinterpret_cast<u8 const*>(&value), 4);
  333. }
  334. // g) Finish the hash.
  335. // h) (Security handlers of revision 3 or greater) Do the following 50 times:
  336. //
  337. // Take the output from the previous MD5 hash and pass the first n bytes
  338. // of the output as input into a new MD5 hash, where n is the number of
  339. // bytes of the encryption key as defined by the value of the encryption
  340. // dictionary's Length entry.
  341. if (m_revision >= 3) {
  342. ByteBuffer n_bytes;
  343. for (u32 i = 0; i < 50; i++) {
  344. Crypto::Hash::MD5 new_md5;
  345. n_bytes.ensure_capacity(m_length);
  346. while (n_bytes.size() < m_length) {
  347. auto out = md5.peek();
  348. for (size_t j = 0; j < out.data_length() && n_bytes.size() < m_length; j++)
  349. n_bytes.append(out.data[j]);
  350. }
  351. VERIFY(n_bytes.size() == m_length);
  352. new_md5.update(n_bytes);
  353. md5 = move(new_md5);
  354. n_bytes.clear();
  355. }
  356. }
  357. // i) Set the encryption key to the first n bytes of the output from the final MD5
  358. // hash, where n shall always be 5 for security handlers of revision 2 but, for
  359. // security handlers of revision 3 or greater, shall depend on the value of the
  360. // encryption dictionary's Length entry.
  361. size_t n;
  362. if (m_revision == 2) {
  363. n = 5;
  364. } else if (m_revision >= 3) {
  365. n = m_length;
  366. } else {
  367. VERIFY_NOT_REACHED();
  368. }
  369. ByteBuffer encryption_key;
  370. encryption_key.ensure_capacity(n);
  371. while (encryption_key.size() < n) {
  372. auto out = md5.peek();
  373. for (size_t i = 0; encryption_key.size() < n && i < out.data_length(); i++)
  374. encryption_key.append(out.bytes()[i]);
  375. }
  376. m_encryption_key = encryption_key;
  377. return encryption_key;
  378. }
  379. bool StandardSecurityHandler::compute_encryption_key_r6_and_later(ByteBuffer password_string)
  380. {
  381. // This function should never be called after we have a valid encryption key.
  382. VERIFY(!m_encryption_key.has_value());
  383. auto const zero_iv = ByteBuffer::create_zeroed(16).release_value_but_fixme_should_propagate_errors();
  384. // ISO 32000 (PDF 2.0), 7.6.4.3.3 Algorithm 2.A: Retrieving the file encryption key from an encrypted
  385. // document in order to decrypt it (revision 6 or later)
  386. // "It is necessary to treat the 48-bytes of the O and U strings in the
  387. // Encrypt dictionary as made up of three sections [...]. The first 32 bytes
  388. // are a hash value (explained below). The next 8 bytes are called the Validation Salt. The final 8 bytes are
  389. // called the Key Salt."
  390. // a) The UTF-8 password string shall be generated from Unicode input by processing the input string with
  391. // the SASLprep (Internet RFC 4013) profile of stringprep (Internet RFC 3454) using the Normalize and BiDi
  392. // options, and then converting to a UTF-8 representation.
  393. // FIXME
  394. // b) Truncate the UTF-8 representation to 127 bytes if it is longer than 127 bytes.
  395. if (password_string.size() > 127)
  396. password_string.resize(127);
  397. // c) Test the password against the owner key by computing a hash using algorithm 2.B with an input string
  398. // consisting of the UTF-8 password concatenated with the 8 bytes of owner Validation Salt, concatenated
  399. // with the 48-byte U string. If the 32-byte result matches the first 32 bytes of the O string, this is the owner
  400. // password.
  401. // [Implementor's note: This is the same as Algorithm 12 in the spec.]
  402. if (authenticate_owner_password_r6_and_later(password_string)) {
  403. // d) Compute an intermediate owner key by computing a hash using algorithm 2.B with an input string
  404. // consisting of the UTF-8 owner password concatenated with the 8 bytes of owner Key Salt, concatenated
  405. // with the 48-byte U string. The 32-byte result is the key used to decrypt the 32-byte OE string using AES-
  406. // 256 in CBC mode with no padding and an initialization vector of zero. The 32-byte result is the file
  407. // encryption key.
  408. ByteBuffer input;
  409. input.append(password_string);
  410. input.append(m_o_entry.bytes().slice(40, 8));
  411. input.append(m_u_entry.bytes());
  412. auto key = computing_a_hash_r6_and_later(input, password_string, HashKind::Owner);
  413. // [Implementor's note: PaddingMode doesn't matter here since input is block-aligned.]
  414. auto cipher = Crypto::Cipher::AESCipher::CBCMode(key, 256, Crypto::Cipher::Intent::Decryption, Crypto::Cipher::PaddingMode::Null);
  415. auto decrypted = cipher.create_aligned_buffer(m_oe_entry.length()).release_value_but_fixme_should_propagate_errors();
  416. Bytes decrypted_span = decrypted.bytes();
  417. cipher.decrypt(m_oe_entry.bytes(), decrypted_span, zero_iv);
  418. m_encryption_key = ByteBuffer::copy(decrypted_span).release_value_but_fixme_should_propagate_errors();
  419. }
  420. // [Implementor's note: The spec seems to miss a step like c) but for the user password here.]
  421. else if (authenticate_user_password_r6_and_later(password_string)) {
  422. // e) Compute an intermediate user key by computing a hash using algorithm 2.B with an input string
  423. // consisting of the UTF-8 user password concatenated with the 8 bytes of user Key Salt. The 32-byte result
  424. // is the key used to decrypt the 32-byte UE string using AES-256 in CBC mode with no padding and an
  425. // initialization vector of zero. The 32-byte result is the file encryption key.
  426. ByteBuffer input;
  427. input.append(password_string);
  428. input.append(m_u_entry.bytes().slice(40, 8));
  429. auto key = computing_a_hash_r6_and_later(input, password_string, HashKind::User);
  430. // [Implementor's note: PaddingMode doesn't matter here since input is block-aligned.]
  431. auto cipher = Crypto::Cipher::AESCipher::CBCMode(key, 256, Crypto::Cipher::Intent::Decryption, Crypto::Cipher::PaddingMode::Null);
  432. auto decrypted = cipher.create_aligned_buffer(m_ue_entry.length()).release_value_but_fixme_should_propagate_errors();
  433. Bytes decrypted_span = decrypted.bytes();
  434. cipher.decrypt(m_ue_entry.bytes(), decrypted_span, zero_iv);
  435. m_encryption_key = ByteBuffer::copy(decrypted_span).release_value_but_fixme_should_propagate_errors();
  436. }
  437. // [Implementor's note: No explicit step for this in the spec, but if we get here the password was neither owner nor user password.]
  438. else {
  439. return false;
  440. }
  441. // f) Decrypt the 16-byte Perms string using AES-256 in ECB mode with an initialization vector of zero and
  442. // the file encryption key as the key. Verify that bytes 9-11 of the result are the characters "a", "d", "b". Bytes
  443. // 0-3 of the decrypted Perms entry, treated as a little-endian integer, are the user permissions. They shall
  444. // match the value in the P key.
  445. // [Implementor's note: For 16-byte long messages, CBC with an IV of zero is the same as ECB. ECB with an IV doesn't make a lot of sense (?) Maybe the spec means CBC.]
  446. auto cipher = Crypto::Cipher::AESCipher::CBCMode(m_encryption_key.value(), 256, Crypto::Cipher::Intent::Decryption, Crypto::Cipher::PaddingMode::Null);
  447. auto decrypted = cipher.create_aligned_buffer(m_perms_entry.length()).release_value_but_fixme_should_propagate_errors();
  448. Bytes decrypted_span = decrypted.bytes();
  449. cipher.decrypt(m_perms_entry.bytes(), decrypted_span, zero_iv);
  450. return decrypted_span[9] == 'a' && decrypted_span[10] == 'd' && decrypted_span[11] == 'b' && *bit_cast<LittleEndian<u32>*>(decrypted_span.data()) == m_flags;
  451. }
  452. ByteBuffer StandardSecurityHandler::computing_a_hash_r6_and_later(ByteBuffer original_input, StringView input_password, HashKind kind)
  453. {
  454. // ISO 32000 (PDF 2.0), 7.6.4.3.4 Algorithm 2.B: Computing a hash (revision 6 or later)
  455. // Take the SHA-256 hash of the original input to the algorithm and name the resulting 32 bytes, K.
  456. static_assert(Crypto::Hash::SHA256::DigestType::Size == 32);
  457. Crypto::Hash::SHA256 sha;
  458. sha.update(original_input);
  459. auto K = ByteBuffer::copy(sha.digest().bytes()).release_value_but_fixme_should_propagate_errors();
  460. // Perform the following steps (a)-(d) 64 times:
  461. int round_number;
  462. for (round_number = 0;; ++round_number) {
  463. // a) Make a new string, K1, consisting of 64 repetitions of the sequence: Input password, K, the 48-byte user
  464. // key. The 48 byte user key is only used when checking the owner password or creating the owner key. If
  465. // checking the user password or creating the user key, K1 is the concatenation of the input password and K.
  466. ByteBuffer K1_part;
  467. K1_part.append(input_password.bytes());
  468. K1_part.append(K.bytes());
  469. if (kind == HashKind::Owner)
  470. K1_part.append(m_u_entry.bytes());
  471. ByteBuffer K1;
  472. for (int i = 0; i < 64; ++i)
  473. K1.append(K1_part);
  474. // b) Encrypt K1 with the AES-128 (CBC, no padding) algorithm, using the first 16 bytes of K as the key and
  475. // the second 16 bytes of K as the initialization vector. The result of this encryption is E.
  476. ReadonlyBytes key = K.bytes().trim(16);
  477. ReadonlyBytes initialization_vector = K.bytes().slice(16);
  478. // [Implementor's note: PaddingMode doesn't matter here since input is block-aligned.]
  479. auto cipher = Crypto::Cipher::AESCipher::CBCMode(key, 128, Crypto::Cipher::Intent::Encryption, Crypto::Cipher::PaddingMode::Null);
  480. auto E = cipher.create_aligned_buffer(K1.size()).release_value_but_fixme_should_propagate_errors();
  481. Bytes E_span = E.bytes();
  482. cipher.encrypt(K1, E_span, initialization_vector);
  483. // c) Taking the first 16 bytes of E as an unsigned big-endian integer, compute the remainder, modulo 3. If the
  484. // result is 0, the next hash used is SHA-256, if the result is 1, the next hash used is SHA-384, if the result is
  485. // 2, the next hash used is SHA-512.
  486. u128 remainder(0);
  487. for (int i = 0; i < 16; ++i)
  488. remainder = (remainder << 8) | E[i];
  489. remainder %= u128(3);
  490. Crypto::Hash::HashKind hash_kind;
  491. switch (u8 { remainder }) {
  492. case 0:
  493. hash_kind = Crypto::Hash::HashKind::SHA256;
  494. break;
  495. case 1:
  496. hash_kind = Crypto::Hash::HashKind::SHA384;
  497. break;
  498. case 2:
  499. hash_kind = Crypto::Hash::HashKind::SHA512;
  500. break;
  501. }
  502. // d) Using the hash algorithm determined in step c, take the hash of E. The result is a new value of K, which
  503. // will be 32, 48, or 64 bytes in length.
  504. Crypto::Hash::Manager hash(hash_kind);
  505. hash.update(E);
  506. K = ByteBuffer::copy(hash.digest().bytes()).release_value_but_fixme_should_propagate_errors();
  507. // Repeat the process (a-d) with this new value of K. Following 64 rounds (round number 0 to round
  508. // number 63), do the following, starting with round number 64:
  509. // [Implementor's note: Conceptually, steps e)-f) are at the top of the loop for rounds >= 64, so this has to continue for < 63, not for < 64.]
  510. if (round_number < 63)
  511. continue;
  512. // NOTE 2 The reason for multiple rounds is to defeat the possibility of running all paths in parallel. With 64
  513. // rounds (minimum) there are 3^64 paths through the algorithm.
  514. // e) Look at the very last byte of E. If the value of that byte (taken as an unsigned integer) is greater than the
  515. // round number - 32, repeat steps (a-d) again.
  516. // f) Repeat from steps (a-e) until the value of the last byte is <= (round number) - 32.
  517. // NOTE 3 Tests indicate that the total number of rounds will most likely be between 65 and 80.
  518. if (E.bytes().last() <= round_number - 32)
  519. break;
  520. }
  521. // The first 32 bytes of the final K are the output of the algorithm.
  522. VERIFY(K.size() >= 32);
  523. K.resize(32);
  524. return K;
  525. }
  526. void StandardSecurityHandler::crypt(NonnullRefPtr<Object> object, Reference reference, Crypto::Cipher::Intent direction) const
  527. {
  528. VERIFY(m_encryption_key.has_value());
  529. if (m_method == CryptFilterMethod::None)
  530. return;
  531. auto aes = [&](ReadonlyBytes bytes, ByteBuffer const& key) {
  532. auto cipher = Crypto::Cipher::AESCipher::CBCMode(key, m_length * 8, direction, Crypto::Cipher::PaddingMode::CMS);
  533. // "The block size parameter is 16 bytes, and the initialization vector is a 16-byte random number
  534. // that is stored as the first 16 bytes of the encrypted stream or string."
  535. static_assert(Crypto::Cipher::AESCipher::block_size() == 16);
  536. if (direction == Crypto::Cipher::Intent::Encryption) {
  537. auto output = cipher.create_aligned_buffer(16 + bytes.size()).release_value_but_fixme_should_propagate_errors();
  538. auto iv_span = output.bytes().trim(16);
  539. auto encrypted_span = output.bytes().slice(16);
  540. fill_with_random(iv_span);
  541. cipher.encrypt(bytes, encrypted_span, iv_span);
  542. return output;
  543. } else {
  544. VERIFY(direction == Crypto::Cipher::Intent::Decryption);
  545. auto iv = bytes.trim(16);
  546. bytes = bytes.slice(16);
  547. auto decrypted = cipher.create_aligned_buffer(bytes.size()).release_value_but_fixme_should_propagate_errors();
  548. auto decrypted_span = decrypted.bytes();
  549. cipher.decrypt(bytes, decrypted_span, iv);
  550. decrypted.resize(decrypted_span.size());
  551. return decrypted;
  552. }
  553. };
  554. ReadonlyBytes bytes;
  555. Function<void(ByteBuffer)> assign;
  556. if (object->is<StreamObject>()) {
  557. auto stream = object->cast<StreamObject>();
  558. bytes = stream->bytes();
  559. assign = [&object](ByteBuffer buffer) {
  560. object->cast<StreamObject>()->buffer() = move(buffer);
  561. };
  562. if (stream->dict()->contains(CommonNames::Filter)) {
  563. // ISO 32000 (PDF 2.0), 7.4.10 Crypt filter
  564. // "The Crypt filter shall be the first filter in the Filter array entry."
  565. auto filters = m_document->read_filters(stream->dict()).release_value_but_fixme_should_propagate_errors();
  566. if (!filters.is_empty() && filters[0] == "Crypt")
  567. TODO();
  568. }
  569. } else if (object->is<StringObject>()) {
  570. auto string = object->cast<StringObject>();
  571. bytes = string->string().bytes();
  572. assign = [&object](ByteBuffer buffer) {
  573. object->cast<StringObject>()->set_string(ByteString(buffer.bytes()));
  574. };
  575. } else {
  576. VERIFY_NOT_REACHED();
  577. }
  578. if (m_method == CryptFilterMethod::AESV3) {
  579. // ISO 32000 (PDF 2.0), 7.6.3.3 Algorithm 1.A: Encryption of data using the AES algorithms
  580. // a) Use the 32-byte file encryption key for the AES-256 symmetric key algorithm, along with the string or
  581. // stream data to be encrypted.
  582. //
  583. // Use the AES algorithm in Cipher Block Chaining (CBC) mode, which requires an initialization
  584. // vector. The block size parameter is set to 16 bytes, and the initialization vector is a 16-byte random
  585. // number that is stored as the first 16 bytes of the encrypted stream or string.
  586. assign(aes(bytes, m_encryption_key.value()));
  587. return;
  588. }
  589. // 7.6.2 General Encryption Algorithm
  590. // Algorithm 1: Encryption of data using the RC3 or AES algorithms
  591. // a) Obtain the object number and generation number from the object identifier of
  592. // the string or stream to be encrypted. If the string is a direct object, use
  593. // the identifier of the indirect object containing it.
  594. //
  595. // Note: This is always passed in at parse time because objects don't know their own
  596. // object number.
  597. // b) For all strings and streams with crypt filter specifier; treating the object
  598. // number as binary integers, extend the original n-byte encryption key to n + 5
  599. // bytes by appending the low-order 3 bytes of the object number and the low-order
  600. // 2 bytes of the generation number in that order, low-order byte first. ...
  601. auto encryption_key = m_encryption_key.value();
  602. auto index = reference.as_ref_index();
  603. auto generation = reference.as_ref_generation_index();
  604. encryption_key.append(index & 0xff);
  605. encryption_key.append((index >> 8) & 0xff);
  606. encryption_key.append((index >> 16) & 0xff);
  607. encryption_key.append(generation & 0xff);
  608. encryption_key.append((generation >> 8) & 0xff);
  609. if (m_method == CryptFilterMethod::AESV2) {
  610. encryption_key.append('s');
  611. encryption_key.append('A');
  612. encryption_key.append('l');
  613. encryption_key.append('T');
  614. }
  615. // c) Initialize the MD5 hash function and pass the result of step (b) as input to this
  616. // function.
  617. Crypto::Hash::MD5 md5;
  618. md5.update(encryption_key);
  619. // d) Use the first (n + 5) bytes, up to a maximum of 16, of the output from the MD5
  620. // hash as the key for the RC4 or AES symmetric key algorithms, along with the string
  621. // or stream data to be encrypted.
  622. auto key = ByteBuffer::copy(md5.peek().bytes()).release_value_but_fixme_should_propagate_errors();
  623. if (key.size() > min(encryption_key.size(), 16))
  624. key.resize(encryption_key.size());
  625. if (m_method == CryptFilterMethod::AESV2) {
  626. assign(aes(bytes, key));
  627. return;
  628. }
  629. // RC4 is symmetric, so decryption is the same as encryption.
  630. VERIFY(m_method == CryptFilterMethod::V2);
  631. RC4 rc4(key);
  632. auto output = rc4.encrypt(bytes);
  633. assign(move(output));
  634. }
  635. void StandardSecurityHandler::encrypt(NonnullRefPtr<Object> object, Reference reference) const
  636. {
  637. crypt(object, reference, Crypto::Cipher::Intent::Encryption);
  638. }
  639. void StandardSecurityHandler::decrypt(NonnullRefPtr<Object> object, Reference reference) const
  640. {
  641. crypt(object, reference, Crypto::Cipher::Intent::Decryption);
  642. }
  643. static constexpr auto identity_permutation = iota_array<size_t, 256>(0);
  644. RC4::RC4(ReadonlyBytes key)
  645. : m_bytes(identity_permutation)
  646. {
  647. size_t j = 0;
  648. for (size_t i = 0; i < 256; i++) {
  649. j = (j + m_bytes[i] + key[i % key.size()]) & 0xff;
  650. swap(m_bytes[i], m_bytes[j]);
  651. }
  652. }
  653. void RC4::generate_bytes(ByteBuffer& bytes)
  654. {
  655. size_t i = 0;
  656. size_t j = 0;
  657. for (size_t count = 0; count < bytes.size(); count++) {
  658. i = (i + 1) % 256;
  659. j = (j + m_bytes[i]) % 256;
  660. swap(m_bytes[i], m_bytes[j]);
  661. bytes[count] = m_bytes[(m_bytes[i] + m_bytes[j]) % 256];
  662. }
  663. }
  664. ByteBuffer RC4::encrypt(ReadonlyBytes bytes)
  665. {
  666. auto output = ByteBuffer::create_uninitialized(bytes.size()).release_value_but_fixme_should_propagate_errors();
  667. generate_bytes(output);
  668. for (size_t i = 0; i < bytes.size(); i++)
  669. output[i] ^= bytes[i];
  670. return output;
  671. }
  672. }