Encryption.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 <LibCrypto/Hash/MD5.h>
  8. #include <LibPDF/CommonNames.h>
  9. #include <LibPDF/Document.h>
  10. #include <LibPDF/Encryption.h>
  11. namespace PDF {
  12. static constexpr Array<u8, 32> standard_encryption_key_padding_bytes = {
  13. 0x28,
  14. 0xBF,
  15. 0x4E,
  16. 0x5E,
  17. 0x4E,
  18. 0x75,
  19. 0x8A,
  20. 0x41,
  21. 0x64,
  22. 0x00,
  23. 0x4E,
  24. 0x56,
  25. 0xFF,
  26. 0xFA,
  27. 0x01,
  28. 0x08,
  29. 0x2E,
  30. 0x2E,
  31. 0x00,
  32. 0xB6,
  33. 0xD0,
  34. 0x68,
  35. 0x3E,
  36. 0x80,
  37. 0x2F,
  38. 0x0C,
  39. 0xA9,
  40. 0xFE,
  41. 0x64,
  42. 0x53,
  43. 0x69,
  44. 0x7A,
  45. };
  46. PDFErrorOr<NonnullRefPtr<SecurityHandler>> SecurityHandler::create(Document* document, NonnullRefPtr<DictObject> encryption_dict)
  47. {
  48. auto filter = TRY(encryption_dict->get_name(document, CommonNames::Filter))->name();
  49. if (filter == "Standard")
  50. return TRY(StandardSecurityHandler::create(document, encryption_dict));
  51. dbgln("Unrecognized security handler filter: {}", filter);
  52. TODO();
  53. }
  54. PDFErrorOr<NonnullRefPtr<StandardSecurityHandler>> StandardSecurityHandler::create(Document* document, NonnullRefPtr<DictObject> encryption_dict)
  55. {
  56. auto revision = encryption_dict->get_value(CommonNames::R).get<int>();
  57. auto o = TRY(encryption_dict->get_string(document, CommonNames::O))->string();
  58. auto u = TRY(encryption_dict->get_string(document, CommonNames::U))->string();
  59. auto p = encryption_dict->get_value(CommonNames::P).get<int>();
  60. // V, number: [...] 1 "Algorithm 1 Encryption of data using the RC4 or AES algorithms" in 7.6.2,
  61. // "General Encryption Algorithm," with an encryption key length of 40 bits, see below [...]
  62. // Lenght, integer: (Optional; PDF 1.4; only if V is 2 or 3) The length of the encryption key, in bits.
  63. // The value shall be a multiple of 8, in the range 40 to 128. Default value: 40.
  64. int length_in_bits;
  65. auto v = encryption_dict->get_value(CommonNames::V).get<int>();
  66. if (encryption_dict->contains(CommonNames::Length))
  67. length_in_bits = encryption_dict->get_value(CommonNames::Length).get<int>();
  68. else if (v == 1)
  69. length_in_bits = 40;
  70. else
  71. return Error(Error::Type::Parse, "Can't determine length of encryption key");
  72. auto length = length_in_bits / 8;
  73. bool encrypt_metadata = true;
  74. if (encryption_dict->contains(CommonNames::EncryptMetadata))
  75. encryption_dict->get_value(CommonNames::EncryptMetadata).get<bool>();
  76. return adopt_ref(*new StandardSecurityHandler(document, revision, o, u, p, encrypt_metadata, length));
  77. }
  78. StandardSecurityHandler::StandardSecurityHandler(Document* document, size_t revision, DeprecatedString const& o_entry, DeprecatedString const& u_entry, u32 flags, bool encrypt_metadata, size_t length)
  79. : m_document(document)
  80. , m_revision(revision)
  81. , m_o_entry(o_entry)
  82. , m_u_entry(u_entry)
  83. , m_flags(flags)
  84. , m_encrypt_metadata(encrypt_metadata)
  85. , m_length(length)
  86. {
  87. }
  88. template<>
  89. ByteBuffer StandardSecurityHandler::compute_user_password_value<true>(ByteBuffer password_string)
  90. {
  91. // Algorithm 4: Computing the encryption dictionary's U (user password)
  92. // value (Security handlers of revision 2)
  93. // a) Create an encryption key based on the user password string, as
  94. // described in [Algorithm 2]
  95. auto encryption_key = compute_encryption_key(password_string);
  96. // b) Encrypt the 32-byte padding string shown in step (a) of [Algorithm 2],
  97. // using an RC4 encryption function with the encryption key from the
  98. // preceding step.
  99. RC4 rc4(encryption_key);
  100. auto output = rc4.encrypt(standard_encryption_key_padding_bytes);
  101. // c) Store the result of step (b) as the value of the U entry in the
  102. // encryption dictionary.
  103. return output;
  104. }
  105. template<>
  106. ByteBuffer StandardSecurityHandler::compute_user_password_value<false>(ByteBuffer password_string)
  107. {
  108. // Algorithm 5: Computing the encryption dictionary's U (user password)
  109. // value (Security handlers of revision 3 or greater)
  110. // a) Create an encryption key based on the user password string, as
  111. // described in [Algorithm 2]
  112. auto encryption_key = compute_encryption_key(password_string);
  113. // b) Initialize the MD5 hash function and pass the 32-byte padding string
  114. // shown in step (a) of [Algorithm 2] as input to this function
  115. Crypto::Hash::MD5 md5;
  116. md5.update(standard_encryption_key_padding_bytes);
  117. // e) Pass the first element of the file's file identifier array to the MD5
  118. // hash function.
  119. auto id_array = MUST(m_document->trailer()->get_array(m_document, CommonNames::ID));
  120. auto first_element_string = MUST(id_array->get_string_at(m_document, 0))->string();
  121. md5.update(first_element_string);
  122. // d) Encrypt the 16-byte result of the hash, using an RC4 encryption function
  123. // with the encryption key from step (a).
  124. RC4 rc4(encryption_key);
  125. auto out = md5.peek();
  126. auto buffer = rc4.encrypt(out.bytes());
  127. // e) Do the following 19 times:
  128. //
  129. // Take the output from the previous invocation of the RC4 function and pass
  130. // it as input to a new invocation of the function; use an encryption key generated
  131. // by taking each byte of the original encryption key obtained in step (a) and
  132. // performing an XOR operation between the that byte and the single-byte value of
  133. // the iteration counter (from 1 to 19).
  134. auto new_encryption_key = MUST(ByteBuffer::create_uninitialized(encryption_key.size()));
  135. for (size_t i = 1; i <= 19; i++) {
  136. for (size_t j = 0; j < encryption_key.size(); j++)
  137. new_encryption_key[j] = encryption_key[j] ^ i;
  138. RC4 new_rc4(new_encryption_key);
  139. buffer = new_rc4.encrypt(buffer);
  140. }
  141. // f) Append 16 bytes of the arbitrary padding to the output from the final invocation
  142. // of the RC4 function and store the 32-byte result as the value of the U entry in
  143. // the encryption dictionary.
  144. VERIFY(buffer.size() == 16);
  145. for (size_t i = 0; i < 16; i++)
  146. buffer.append(0xab);
  147. return buffer;
  148. }
  149. bool StandardSecurityHandler::try_provide_user_password(StringView password_string)
  150. {
  151. // Algorithm 6: Authenticating the user password
  152. // a) Perform all but the last step of [Algorithm 4] or [Algorithm 5] using the
  153. // supplied password string.
  154. ByteBuffer password_buffer = MUST(ByteBuffer::copy(password_string.bytes()));
  155. if (m_revision == 2) {
  156. password_buffer = compute_user_password_value<true>(password_buffer);
  157. } else {
  158. password_buffer = compute_user_password_value<false>(password_buffer);
  159. }
  160. // b) If the result of step (a) is equal to the value of the encryption
  161. // dictionary's "U" entry (comparing the first 16 bytes in the case of security
  162. // handlers of revision 3 or greater), the password supplied is the correct user
  163. // password.
  164. auto u_bytes = m_u_entry.bytes();
  165. bool has_user_password;
  166. if (m_revision >= 3)
  167. has_user_password = u_bytes.slice(0, 16) == password_buffer.bytes().slice(0, 16);
  168. else
  169. has_user_password = u_bytes == password_buffer.bytes();
  170. if (!has_user_password)
  171. m_encryption_key = {};
  172. return has_user_password;
  173. }
  174. ByteBuffer StandardSecurityHandler::compute_encryption_key(ByteBuffer password_string)
  175. {
  176. // This function should never be called after we have a valid encryption key.
  177. VERIFY(!m_encryption_key.has_value());
  178. // 7.6.3.3 Encryption Key Algorithm
  179. // Algorithm 2: Computing an encryption key
  180. // a) Pad or truncate the password string to exactly 32 bytes. If the password string
  181. // is more than 32 bytes long, use only its first 32 bytes; if it is less than 32
  182. // bytes long, pad it by appending the required number of additional bytes from the
  183. // beginning of the following padding string: [omitted]
  184. if (password_string.size() > 32) {
  185. password_string.resize(32);
  186. } else {
  187. password_string.append(standard_encryption_key_padding_bytes.data(), 32 - password_string.size());
  188. }
  189. // b) Initialize the MD5 hash function and pass the result of step (a) as input to
  190. // this function.
  191. Crypto::Hash::MD5 md5;
  192. md5.update(password_string);
  193. // c) Pass the value of the encryption dictionary's "O" entry to the MD5 hash function.
  194. md5.update(m_o_entry);
  195. // d) Convert the integer value of the P entry to a 32-bit unsigned binary number and pass
  196. // these bytes to the MD5 hash function, low-order byte first.
  197. md5.update(reinterpret_cast<u8 const*>(&m_flags), sizeof(m_flags));
  198. // e) Pass the first element of the file's file identifier array to the MD5 hash function.
  199. auto id_array = MUST(m_document->trailer()->get_array(m_document, CommonNames::ID));
  200. auto first_element_string = MUST(id_array->get_string_at(m_document, 0))->string();
  201. md5.update(first_element_string);
  202. // f) (Security handlers of revision 4 or greater) if the document metadata is not being
  203. // encrypted, pass 4 bytes with the value 0xffffffff to the MD5 hash function.
  204. if (m_revision >= 4 && !m_encrypt_metadata) {
  205. u32 value = 0xffffffff;
  206. md5.update(reinterpret_cast<u8 const*>(&value), 4);
  207. }
  208. // g) Finish the hash.
  209. // h) (Security handlers of revision 3 or greater) Do the following 50 times:
  210. //
  211. // Take the output from the previous MD5 hash and pass the first n bytes
  212. // of the output as input into a new MD5 hash, where n is the number of
  213. // bytes of the encryption key as defined by the value of the encryption
  214. // dictionary's Length entry.
  215. if (m_revision >= 3) {
  216. ByteBuffer n_bytes;
  217. for (u32 i = 0; i < 50; i++) {
  218. Crypto::Hash::MD5 new_md5;
  219. n_bytes.ensure_capacity(m_length);
  220. while (n_bytes.size() < m_length) {
  221. auto out = md5.peek();
  222. for (size_t j = 0; j < out.data_length() && n_bytes.size() < m_length; j++)
  223. n_bytes.append(out.data[j]);
  224. }
  225. VERIFY(n_bytes.size() == m_length);
  226. new_md5.update(n_bytes);
  227. md5 = move(new_md5);
  228. n_bytes.clear();
  229. }
  230. }
  231. // i) Set the encryption key to the first n bytes of the output from the final MD5
  232. // hash, where n shall always be 5 for security handlers of revision 2 but, for
  233. // security handlers of revision 3 or greater, shall depend on the value of the
  234. // encryption dictionary's Length entry.
  235. size_t n;
  236. if (m_revision == 2) {
  237. n = 5;
  238. } else if (m_revision >= 3) {
  239. n = m_length;
  240. } else {
  241. VERIFY_NOT_REACHED();
  242. }
  243. ByteBuffer encryption_key;
  244. encryption_key.ensure_capacity(n);
  245. while (encryption_key.size() < n) {
  246. auto out = md5.peek();
  247. for (size_t i = 0; encryption_key.size() < n && i < out.data_length(); i++)
  248. encryption_key.append(out.bytes()[i]);
  249. }
  250. m_encryption_key = encryption_key;
  251. return encryption_key;
  252. }
  253. void StandardSecurityHandler::encrypt(NonnullRefPtr<Object> object, Reference reference) const
  254. {
  255. // 7.6.2 General Encryption Algorithm
  256. // Algorithm 1: Encryption of data using the RC3 or AES algorithms
  257. // FIXME: Support AES
  258. VERIFY(m_encryption_key.has_value());
  259. // a) Obtain the object number and generation number from the object identifier of
  260. // the string or stream to be encrypted. If the string is a direct object, use
  261. // the identifier of the indirect object containing it.
  262. //
  263. // Note: This is always passed in at parse time because objects don't know their own
  264. // object number.
  265. // b) For all strings and streams with crypt filter specifier; treating the object
  266. // number as binary integers, extends the origin n-byte encryption key to n + 5
  267. // bytes by appending the low-order 3 bytes of the object number and the low-order
  268. // 2 bytes of the generation number in that order, low-order byte first. ...
  269. auto encryption_key = m_encryption_key.value();
  270. ReadonlyBytes bytes;
  271. Function<void(ByteBuffer const&)> assign;
  272. if (object->is<StreamObject>()) {
  273. auto stream = object->cast<StreamObject>();
  274. bytes = stream->bytes();
  275. assign = [&stream](ByteBuffer const& buffer) {
  276. stream->buffer() = buffer;
  277. };
  278. if (stream->dict()->contains(CommonNames::Filter)) {
  279. auto filter = MUST(stream->dict()->get_name(m_document, CommonNames::Filter))->name();
  280. if (filter == "Crypt")
  281. TODO();
  282. }
  283. } else if (object->is<StringObject>()) {
  284. auto string = object->cast<StringObject>();
  285. bytes = string->string().bytes();
  286. assign = [&string](ByteBuffer const& buffer) {
  287. string->set_string(DeprecatedString(buffer.bytes()));
  288. };
  289. } else {
  290. VERIFY_NOT_REACHED();
  291. }
  292. auto index = reference.as_ref_index();
  293. auto generation = reference.as_ref_generation_index();
  294. encryption_key.append(index & 0xff);
  295. encryption_key.append((index >> 8) & 0xff);
  296. encryption_key.append((index >> 16) & 0xff);
  297. encryption_key.append(generation & 0xff);
  298. encryption_key.append((generation >> 8) & 0xff);
  299. // c) Initialize the MD5 hash function and pass the result of step (b) as input to this
  300. // function.
  301. Crypto::Hash::MD5 md5;
  302. md5.update(encryption_key);
  303. // d) Use the first (n + 5) bytes, up to a maximum of 16, of the output from the MD5
  304. // hash as the key for the RC4 or AES symmetric key algorithms, along with the string
  305. // or stream data to be encrypted.
  306. auto key = MUST(ByteBuffer::copy(md5.peek().bytes()));
  307. if (key.size() > min(encryption_key.size(), 16))
  308. key.resize(encryption_key.size());
  309. RC4 rc4(key);
  310. auto output = rc4.encrypt(bytes);
  311. assign(output);
  312. }
  313. void StandardSecurityHandler::decrypt(NonnullRefPtr<Object> object, Reference reference) const
  314. {
  315. // AES and RC4 are both symmetric, so decryption is the same as encryption
  316. encrypt(object, reference);
  317. }
  318. static constexpr auto identity_permutation = iota_array<size_t, 256>(0);
  319. RC4::RC4(ReadonlyBytes key)
  320. : m_bytes(identity_permutation)
  321. {
  322. size_t j = 0;
  323. for (size_t i = 0; i < 256; i++) {
  324. j = (j + m_bytes[i] + key[i % key.size()]) & 0xff;
  325. swap(m_bytes[i], m_bytes[j]);
  326. }
  327. }
  328. void RC4::generate_bytes(ByteBuffer& bytes)
  329. {
  330. size_t i = 0;
  331. size_t j = 0;
  332. for (size_t count = 0; count < bytes.size(); count++) {
  333. i = (i + 1) % 256;
  334. j = (j + m_bytes[i]) % 256;
  335. swap(m_bytes[i], m_bytes[j]);
  336. bytes[count] = m_bytes[(m_bytes[i] + m_bytes[j]) % 256];
  337. }
  338. }
  339. ByteBuffer RC4::encrypt(ReadonlyBytes bytes)
  340. {
  341. auto output = MUST(ByteBuffer::create_uninitialized(bytes.size()));
  342. generate_bytes(output);
  343. for (size_t i = 0; i < bytes.size(); i++)
  344. output[i] ^= bytes[i];
  345. return output;
  346. }
  347. }