Encryption.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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, DeprecatedString 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. DeprecatedString stream_filter = "Identity";
  121. if (encryption_dict->contains(CommonNames::StmF))
  122. stream_filter = TRY(encryption_dict->get_name(document, CommonNames::StmF))->name();
  123. DeprecatedString 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. return adopt_ref(*new StandardSecurityHandler(document, revision, o, u, p, encrypt_metadata, length, method));
  141. }
  142. StandardSecurityHandler::StandardSecurityHandler(Document* document, size_t revision, DeprecatedString const& o_entry, DeprecatedString const& u_entry, u32 flags, bool encrypt_metadata, size_t length, CryptFilterMethod method)
  143. : m_document(document)
  144. , m_revision(revision)
  145. , m_o_entry(o_entry)
  146. , m_u_entry(u_entry)
  147. , m_flags(flags)
  148. , m_encrypt_metadata(encrypt_metadata)
  149. , m_length(length)
  150. , m_method(method)
  151. {
  152. }
  153. ByteBuffer StandardSecurityHandler::compute_user_password_value_r2(ByteBuffer password_string)
  154. {
  155. // Algorithm 4: Computing the encryption dictionary's U (user password)
  156. // value (Security handlers of revision 2)
  157. // a) Create an encryption key based on the user password string, as
  158. // described in [Algorithm 2]
  159. auto encryption_key = compute_encryption_key_r2_to_r5(password_string);
  160. // b) Encrypt the 32-byte padding string shown in step (a) of [Algorithm 2],
  161. // using an RC4 encryption function with the encryption key from the
  162. // preceding step.
  163. RC4 rc4(encryption_key);
  164. auto output = rc4.encrypt(standard_encryption_key_padding_bytes);
  165. // c) Store the result of step (b) as the value of the U entry in the
  166. // encryption dictionary.
  167. return output;
  168. }
  169. ByteBuffer StandardSecurityHandler::compute_user_password_value_r3_to_r5(ByteBuffer password_string)
  170. {
  171. // Algorithm 5: Computing the encryption dictionary's U (user password)
  172. // value (Security handlers of revision 3 or greater)
  173. // a) Create an encryption key based on the user password string, as
  174. // described in [Algorithm 2]
  175. auto encryption_key = compute_encryption_key_r2_to_r5(password_string);
  176. // b) Initialize the MD5 hash function and pass the 32-byte padding string
  177. // shown in step (a) of [Algorithm 2] as input to this function
  178. Crypto::Hash::MD5 md5;
  179. md5.update(standard_encryption_key_padding_bytes);
  180. // e) Pass the first element of the file's file identifier array to the MD5
  181. // hash function.
  182. auto id_array = MUST(m_document->trailer()->get_array(m_document, CommonNames::ID));
  183. auto first_element_string = MUST(id_array->get_string_at(m_document, 0))->string();
  184. md5.update(first_element_string);
  185. // d) Encrypt the 16-byte result of the hash, using an RC4 encryption function
  186. // with the encryption key from step (a).
  187. RC4 rc4(encryption_key);
  188. auto out = md5.peek();
  189. auto buffer = rc4.encrypt(out.bytes());
  190. // e) Do the following 19 times:
  191. //
  192. // Take the output from the previous invocation of the RC4 function and pass
  193. // it as input to a new invocation of the function; use an encryption key generated
  194. // by taking each byte of the original encryption key obtained in step (a) and
  195. // performing an XOR operation between the that byte and the single-byte value of
  196. // the iteration counter (from 1 to 19).
  197. auto new_encryption_key = MUST(ByteBuffer::create_uninitialized(encryption_key.size()));
  198. for (size_t i = 1; i <= 19; i++) {
  199. for (size_t j = 0; j < encryption_key.size(); j++)
  200. new_encryption_key[j] = encryption_key[j] ^ i;
  201. RC4 new_rc4(new_encryption_key);
  202. buffer = new_rc4.encrypt(buffer);
  203. }
  204. // f) Append 16 bytes of the arbitrary padding to the output from the final invocation
  205. // of the RC4 function and store the 32-byte result as the value of the U entry in
  206. // the encryption dictionary.
  207. VERIFY(buffer.size() == 16);
  208. for (size_t i = 0; i < 16; i++)
  209. buffer.append(0xab);
  210. return buffer;
  211. }
  212. bool StandardSecurityHandler::authenticate_user_password_r2_to_r5(StringView password_string)
  213. {
  214. // Algorithm 6: Authenticating the user password
  215. // a) Perform all but the last step of [Algorithm 4] or [Algorithm 5] using the
  216. // supplied password string.
  217. ByteBuffer password_buffer = MUST(ByteBuffer::copy(password_string.bytes()));
  218. if (m_revision == 2) {
  219. password_buffer = compute_user_password_value_r2(password_buffer);
  220. } else {
  221. password_buffer = compute_user_password_value_r3_to_r5(password_buffer);
  222. }
  223. // b) If the result of step (a) is equal to the value of the encryption
  224. // dictionary's "U" entry (comparing the first 16 bytes in the case of security
  225. // handlers of revision 3 or greater), the password supplied is the correct user
  226. // password.
  227. auto u_bytes = m_u_entry.bytes();
  228. if (m_revision >= 3)
  229. return u_bytes.slice(0, 16) == password_buffer.bytes().slice(0, 16);
  230. return u_bytes == password_buffer.bytes();
  231. }
  232. bool StandardSecurityHandler::authenticate_user_password_r6_and_later(StringView)
  233. {
  234. // ISO 32000 (PDF 2.0), 7.6.4.4.10 Algorithm 11: Authenticating the user password (Security handlers of
  235. // revision 6)
  236. // a) Test the password against the user key by computing the 32-byte hash using 7.6.4.3.4, "Algorithm 2.B:
  237. // Computing a hash (revision 6 or later)" with an input string consisting of the UTF-8 password
  238. // concatenated with the 8 bytes of User Validation Salt (see 7.6.4.4.7, "Algorithm 8: Computing the
  239. // encryption dictionary's U (user password) and UE (user encryption) values (Security handlers of
  240. // revision 6)"). If the 32- byte result matches the first 32 bytes of the U string, this is the user password.
  241. TODO();
  242. }
  243. bool StandardSecurityHandler::try_provide_user_password(StringView password_string)
  244. {
  245. bool has_user_password;
  246. if (m_revision >= 6)
  247. has_user_password = authenticate_user_password_r6_and_later(password_string);
  248. else
  249. has_user_password = authenticate_user_password_r2_to_r5(password_string);
  250. if (!has_user_password)
  251. m_encryption_key = {};
  252. return has_user_password;
  253. }
  254. ByteBuffer StandardSecurityHandler::compute_encryption_key_r2_to_r5(ByteBuffer password_string)
  255. {
  256. // This function should never be called after we have a valid encryption key.
  257. VERIFY(!m_encryption_key.has_value());
  258. // 7.6.3.3 Encryption Key Algorithm
  259. // Algorithm 2: Computing an encryption key
  260. // a) Pad or truncate the password string to exactly 32 bytes. If the password string
  261. // is more than 32 bytes long, use only its first 32 bytes; if it is less than 32
  262. // bytes long, pad it by appending the required number of additional bytes from the
  263. // beginning of the following padding string: [omitted]
  264. if (password_string.size() > 32) {
  265. password_string.resize(32);
  266. } else {
  267. password_string.append(standard_encryption_key_padding_bytes.data(), 32 - password_string.size());
  268. }
  269. // b) Initialize the MD5 hash function and pass the result of step (a) as input to
  270. // this function.
  271. Crypto::Hash::MD5 md5;
  272. md5.update(password_string);
  273. // c) Pass the value of the encryption dictionary's "O" entry to the MD5 hash function.
  274. md5.update(m_o_entry);
  275. // d) Convert the integer value of the P entry to a 32-bit unsigned binary number and pass
  276. // these bytes to the MD5 hash function, low-order byte first.
  277. md5.update(reinterpret_cast<u8 const*>(&m_flags), sizeof(m_flags));
  278. // e) Pass the first element of the file's file identifier array to the MD5 hash function.
  279. auto id_array = MUST(m_document->trailer()->get_array(m_document, CommonNames::ID));
  280. auto first_element_string = MUST(id_array->get_string_at(m_document, 0))->string();
  281. md5.update(first_element_string);
  282. // f) (Security handlers of revision 4 or greater) if the document metadata is not being
  283. // encrypted, pass 4 bytes with the value 0xffffffff to the MD5 hash function.
  284. if (m_revision >= 4 && !m_encrypt_metadata) {
  285. u32 value = 0xffffffff;
  286. md5.update(reinterpret_cast<u8 const*>(&value), 4);
  287. }
  288. // g) Finish the hash.
  289. // h) (Security handlers of revision 3 or greater) Do the following 50 times:
  290. //
  291. // Take the output from the previous MD5 hash and pass the first n bytes
  292. // of the output as input into a new MD5 hash, where n is the number of
  293. // bytes of the encryption key as defined by the value of the encryption
  294. // dictionary's Length entry.
  295. if (m_revision >= 3) {
  296. ByteBuffer n_bytes;
  297. for (u32 i = 0; i < 50; i++) {
  298. Crypto::Hash::MD5 new_md5;
  299. n_bytes.ensure_capacity(m_length);
  300. while (n_bytes.size() < m_length) {
  301. auto out = md5.peek();
  302. for (size_t j = 0; j < out.data_length() && n_bytes.size() < m_length; j++)
  303. n_bytes.append(out.data[j]);
  304. }
  305. VERIFY(n_bytes.size() == m_length);
  306. new_md5.update(n_bytes);
  307. md5 = move(new_md5);
  308. n_bytes.clear();
  309. }
  310. }
  311. // i) Set the encryption key to the first n bytes of the output from the final MD5
  312. // hash, where n shall always be 5 for security handlers of revision 2 but, for
  313. // security handlers of revision 3 or greater, shall depend on the value of the
  314. // encryption dictionary's Length entry.
  315. size_t n;
  316. if (m_revision == 2) {
  317. n = 5;
  318. } else if (m_revision >= 3) {
  319. n = m_length;
  320. } else {
  321. VERIFY_NOT_REACHED();
  322. }
  323. ByteBuffer encryption_key;
  324. encryption_key.ensure_capacity(n);
  325. while (encryption_key.size() < n) {
  326. auto out = md5.peek();
  327. for (size_t i = 0; encryption_key.size() < n && i < out.data_length(); i++)
  328. encryption_key.append(out.bytes()[i]);
  329. }
  330. m_encryption_key = encryption_key;
  331. return encryption_key;
  332. }
  333. ByteBuffer StandardSecurityHandler::compute_encryption_key_r6_and_later(ByteBuffer password_string)
  334. {
  335. // This function should never be called after we have a valid encryption key.
  336. VERIFY(!m_encryption_key.has_value());
  337. // ISO 32000 (PDF 2.0), 7.6.4.3.3 Algorithm 2.A: Retrieving the file encryption key from an encrypted
  338. // document in order to decrypt it (revision 6 or later)
  339. // "It is necessary to treat the 48-bytes of the O and U strings in the
  340. // Encrypt dictionary as made up of three sections [...]. The first 32 bytes
  341. // are a hash value (explained below). The next 8 bytes are called the Validation Salt. The final 8 bytes are
  342. // called the Key Salt."
  343. // a) The UTF-8 password string shall be generated from Unicode input by processing the input string with
  344. // the SASLprep (Internet RFC 4013) profile of stringprep (Internet RFC 3454) using the Normalize and BiDi
  345. // options, and then converting to a UTF-8 representation.
  346. // FIXME
  347. // b) Truncate the UTF-8 representation to 127 bytes if it is longer than 127 bytes.
  348. if (password_string.size() > 127)
  349. password_string.resize(127);
  350. // c) Test the password against the owner key by computing a hash using algorithm 2.B with an input string
  351. // consisting of the UTF-8 password concatenated with the 8 bytes of owner Validation Salt, concatenated
  352. // with the 48-byte U string. If the 32-byte result matches the first 32 bytes of the O string, this is the owner
  353. // password.
  354. // d) Compute an intermediate owner key by computing a hash using algorithm 2.B with an input string
  355. // consisting of the UTF-8 owner password concatenated with the 8 bytes of owner Key Salt, concatenated
  356. // with the 48-byte U string. The 32-byte result is the key used to decrypt the 32-byte OE string using AES-
  357. // 256 in CBC mode with no padding and an initialization vector of zero. The 32-byte result is the file
  358. // encryption key.
  359. // e) Compute an intermediate user key by computing a hash using algorithm 2.B with an input string
  360. // consisting of the UTF-8 user password concatenated with the 8 bytes of user Key Salt. The 32-byte result
  361. // is the key used to decrypt the 32-byte UE string using AES-256 in CBC mode with no padding and an
  362. // initialization vector of zero. The 32-byte result is the file encryption key.
  363. // f) Decrypt the 16-bye Perms string using AES-256 in ECB mode with an initialization vector of zero and
  364. // the file encryption key as the key. Verify that bytes 9-11 of the result are the characters "a", "d", "b". Bytes
  365. // 0-3 of the decrypted Perms entry, treated as a little-endian integer, are the user permissions. They shall
  366. // match the value in the P key.
  367. TODO();
  368. }
  369. ByteBuffer StandardSecurityHandler::computing_a_hash_r6_and_later(ByteBuffer original_input, StringView input_password, HashKind kind)
  370. {
  371. // ISO 32000 (PDF 2.0), 7.6.4.3.4 Algorithm 2.B: Computing a hash (revision 6 or later)
  372. // Take the SHA-256 hash of the original input to the algorithm and name the resulting 32 bytes, K.
  373. static_assert(Crypto::Hash::SHA256::DigestType::Size == 32);
  374. Crypto::Hash::SHA256 sha;
  375. sha.update(original_input);
  376. auto K = MUST(ByteBuffer::copy(sha.digest().bytes()));
  377. // Perform the following steps (a)-(d) 64 times:
  378. int round_number;
  379. for (round_number = 0;; ++round_number) {
  380. // a) Make a new string, K1, consisting of 64 repetitions of the sequence: Input password, K, the 48-byte user
  381. // key. The 48 byte user key is only used when checking the owner password or creating the owner key. If
  382. // checking the user password or creating the user key, K1 is the concatenation of the input password and K.
  383. ByteBuffer K1_part;
  384. K1_part.append(input_password.bytes());
  385. K1_part.append(K.bytes());
  386. if (kind == HashKind::Owner)
  387. K1_part.append(m_u_entry.bytes());
  388. ByteBuffer K1;
  389. for (int i = 0; i < 64; ++i)
  390. K1.append(K1_part);
  391. // b) Encrypt K1 with the AES-128 (CBC, no padding) algorithm, using the first 16 bytes of K as the key and
  392. // the second 16 bytes of K as the initialization vector. The result of this encryption is E.
  393. ReadonlyBytes key = K.bytes().trim(16);
  394. ReadonlyBytes initialization_vector = K.bytes().slice(16);
  395. // (PaddingMode doesn't matter here since input is block-aligned.)
  396. auto cipher = Crypto::Cipher::AESCipher::CBCMode(key, 128, Crypto::Cipher::Intent::Encryption, Crypto::Cipher::PaddingMode::Null);
  397. auto E = MUST(cipher.create_aligned_buffer(K1.size()));
  398. Bytes E_span = E.bytes();
  399. cipher.encrypt(K1, E_span, initialization_vector);
  400. // c) Taking the first 16 bytes of E as an unsigned big-endian integer, compute the remainder, modulo 3. If the
  401. // 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
  402. // 2, the next hash used is SHA-512.
  403. u128 remainder(0);
  404. for (int i = 0; i < 16; ++i)
  405. remainder = (remainder << 8) | E[i];
  406. remainder %= u128(3);
  407. Crypto::Hash::HashKind hash_kind;
  408. switch (u8 { remainder }) {
  409. case 0:
  410. hash_kind = Crypto::Hash::HashKind::SHA256;
  411. break;
  412. case 1:
  413. hash_kind = Crypto::Hash::HashKind::SHA384;
  414. break;
  415. case 2:
  416. hash_kind = Crypto::Hash::HashKind::SHA512;
  417. break;
  418. }
  419. // d) Using the hash algorithm determined in step c, take the hash of E. The result is a new value of K, which
  420. // will be 32, 48, or 64 bytes in length.
  421. Crypto::Hash::Manager hash(hash_kind);
  422. hash.update(E);
  423. K = MUST(ByteBuffer::copy(hash.digest().bytes()));
  424. // Repeat the process (a-d) with this new value of K. Following 64 rounds (round number 0 to round
  425. // number 63), do the following, starting with round number 64:
  426. if (round_number < 64)
  427. continue;
  428. // NOTE 2 The reason for multiple rounds is to defeat the possibility of running all paths in parallel. With 64
  429. // rounds (minimum) there are 3^64 paths through the algorithm.
  430. // e) Look at the very last byte of E. If the value of that byte (taken as an unsigned integer) is greater than the
  431. // round number - 32, repeat steps (a-d) again.
  432. // f) Repeat from steps (a-e) until the value of the last byte is <= (round number) - 32.
  433. // NOTE 3 Tests indicate that the total number of rounds will most likely be between 65 and 80.
  434. if (E.bytes().last() <= round_number - 32)
  435. break;
  436. }
  437. // The first 32 bytes of the final K are the output of the algorithm.
  438. VERIFY(K.size() >= 32);
  439. K.resize(32);
  440. return K;
  441. }
  442. void StandardSecurityHandler::crypt(NonnullRefPtr<Object> object, Reference reference, Crypto::Cipher::Intent direction) const
  443. {
  444. VERIFY(m_encryption_key.has_value());
  445. if (m_method == CryptFilterMethod::None)
  446. return;
  447. if (m_method == CryptFilterMethod::AESV3) {
  448. // ISO 32000 (PDF 2.0), 7.6.3.3 Algorithm 1.A: Encryption of data using the AES algorithms
  449. // a) Use the 32-byte file encryption key for the AES-256 symmetric key algorithm, along with the string or
  450. // stream data to be encrypted.
  451. //
  452. // Use the AES algorithm in Cipher Block Chaining (CBC) mode, which requires an initialization
  453. // vector. The block size parameter is set to 16 bytes, and the initialization vector is a 16-byte random
  454. // number that is stored as the first 16 bytes of the encrypted stream or string.
  455. TODO();
  456. }
  457. // 7.6.2 General Encryption Algorithm
  458. // Algorithm 1: Encryption of data using the RC3 or AES algorithms
  459. // a) Obtain the object number and generation number from the object identifier of
  460. // the string or stream to be encrypted. If the string is a direct object, use
  461. // the identifier of the indirect object containing it.
  462. //
  463. // Note: This is always passed in at parse time because objects don't know their own
  464. // object number.
  465. // b) For all strings and streams with crypt filter specifier; treating the object
  466. // number as binary integers, extend the original n-byte encryption key to n + 5
  467. // bytes by appending the low-order 3 bytes of the object number and the low-order
  468. // 2 bytes of the generation number in that order, low-order byte first. ...
  469. auto encryption_key = m_encryption_key.value();
  470. ReadonlyBytes bytes;
  471. Function<void(ByteBuffer)> assign;
  472. if (object->is<StreamObject>()) {
  473. auto stream = object->cast<StreamObject>();
  474. bytes = stream->bytes();
  475. assign = [&object](ByteBuffer buffer) {
  476. object->cast<StreamObject>()->buffer() = move(buffer);
  477. };
  478. if (stream->dict()->contains(CommonNames::Filter)) {
  479. auto filter = MUST(stream->dict()->get_name(m_document, CommonNames::Filter))->name();
  480. if (filter == "Crypt")
  481. TODO();
  482. }
  483. } else if (object->is<StringObject>()) {
  484. auto string = object->cast<StringObject>();
  485. bytes = string->string().bytes();
  486. assign = [&object](ByteBuffer buffer) {
  487. object->cast<StringObject>()->set_string(DeprecatedString(buffer.bytes()));
  488. };
  489. } else {
  490. VERIFY_NOT_REACHED();
  491. }
  492. auto index = reference.as_ref_index();
  493. auto generation = reference.as_ref_generation_index();
  494. encryption_key.append(index & 0xff);
  495. encryption_key.append((index >> 8) & 0xff);
  496. encryption_key.append((index >> 16) & 0xff);
  497. encryption_key.append(generation & 0xff);
  498. encryption_key.append((generation >> 8) & 0xff);
  499. if (m_method == CryptFilterMethod::AESV2) {
  500. encryption_key.append('s');
  501. encryption_key.append('A');
  502. encryption_key.append('l');
  503. encryption_key.append('T');
  504. }
  505. // c) Initialize the MD5 hash function and pass the result of step (b) as input to this
  506. // function.
  507. Crypto::Hash::MD5 md5;
  508. md5.update(encryption_key);
  509. // d) Use the first (n + 5) bytes, up to a maximum of 16, of the output from the MD5
  510. // hash as the key for the RC4 or AES symmetric key algorithms, along with the string
  511. // or stream data to be encrypted.
  512. auto key = MUST(ByteBuffer::copy(md5.peek().bytes()));
  513. if (key.size() > min(encryption_key.size(), 16))
  514. key.resize(encryption_key.size());
  515. if (m_method == CryptFilterMethod::AESV2) {
  516. auto cipher = Crypto::Cipher::AESCipher::CBCMode(key, m_length * 8, direction, Crypto::Cipher::PaddingMode::CMS);
  517. // "The block size parameter is 16 bytes, and the initialization vector is a 16-byte random number
  518. // that is stored as the first 16 bytes of the encrypted stream or string."
  519. static_assert(Crypto::Cipher::AESCipher::block_size() == 16);
  520. if (direction == Crypto::Cipher::Intent::Encryption) {
  521. auto output = MUST(cipher.create_aligned_buffer(16 + bytes.size()));
  522. auto iv_span = output.bytes().trim(16);
  523. auto encrypted_span = output.bytes().slice(16);
  524. fill_with_random(iv_span);
  525. cipher.encrypt(bytes, encrypted_span, iv_span);
  526. assign(move(output));
  527. } else {
  528. VERIFY(direction == Crypto::Cipher::Intent::Decryption);
  529. auto iv = bytes.trim(16);
  530. bytes = bytes.slice(16);
  531. auto decrypted = MUST(cipher.create_aligned_buffer(bytes.size()));
  532. auto decrypted_span = decrypted.bytes();
  533. cipher.decrypt(bytes, decrypted_span, iv);
  534. decrypted.resize(decrypted_span.size());
  535. assign(move(decrypted));
  536. }
  537. return;
  538. }
  539. // RC4 is symmetric, so decryption is the same as encryption.
  540. VERIFY(m_method == CryptFilterMethod::V2);
  541. RC4 rc4(key);
  542. auto output = rc4.encrypt(bytes);
  543. assign(move(output));
  544. }
  545. void StandardSecurityHandler::encrypt(NonnullRefPtr<Object> object, Reference reference) const
  546. {
  547. crypt(object, reference, Crypto::Cipher::Intent::Encryption);
  548. }
  549. void StandardSecurityHandler::decrypt(NonnullRefPtr<Object> object, Reference reference) const
  550. {
  551. crypt(object, reference, Crypto::Cipher::Intent::Decryption);
  552. }
  553. static constexpr auto identity_permutation = iota_array<size_t, 256>(0);
  554. RC4::RC4(ReadonlyBytes key)
  555. : m_bytes(identity_permutation)
  556. {
  557. size_t j = 0;
  558. for (size_t i = 0; i < 256; i++) {
  559. j = (j + m_bytes[i] + key[i % key.size()]) & 0xff;
  560. swap(m_bytes[i], m_bytes[j]);
  561. }
  562. }
  563. void RC4::generate_bytes(ByteBuffer& bytes)
  564. {
  565. size_t i = 0;
  566. size_t j = 0;
  567. for (size_t count = 0; count < bytes.size(); count++) {
  568. i = (i + 1) % 256;
  569. j = (j + m_bytes[i]) % 256;
  570. swap(m_bytes[i], m_bytes[j]);
  571. bytes[count] = m_bytes[(m_bytes[i] + m_bytes[j]) % 256];
  572. }
  573. }
  574. ByteBuffer RC4::encrypt(ReadonlyBytes bytes)
  575. {
  576. auto output = MUST(ByteBuffer::create_uninitialized(bytes.size()));
  577. generate_bytes(output);
  578. for (size_t i = 0; i < bytes.size(); i++)
  579. output[i] ^= bytes[i];
  580. return output;
  581. }
  582. }