Encryption.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 <LibCrypto/Cipher/AES.h>
  10. #include <LibCrypto/Hash/MD5.h>
  11. #include <LibPDF/CommonNames.h>
  12. #include <LibPDF/Document.h>
  13. #include <LibPDF/Encryption.h>
  14. namespace PDF {
  15. static constexpr Array<u8, 32> standard_encryption_key_padding_bytes = {
  16. 0x28,
  17. 0xBF,
  18. 0x4E,
  19. 0x5E,
  20. 0x4E,
  21. 0x75,
  22. 0x8A,
  23. 0x41,
  24. 0x64,
  25. 0x00,
  26. 0x4E,
  27. 0x56,
  28. 0xFF,
  29. 0xFA,
  30. 0x01,
  31. 0x08,
  32. 0x2E,
  33. 0x2E,
  34. 0x00,
  35. 0xB6,
  36. 0xD0,
  37. 0x68,
  38. 0x3E,
  39. 0x80,
  40. 0x2F,
  41. 0x0C,
  42. 0xA9,
  43. 0xFE,
  44. 0x64,
  45. 0x53,
  46. 0x69,
  47. 0x7A,
  48. };
  49. PDFErrorOr<NonnullRefPtr<SecurityHandler>> SecurityHandler::create(Document* document, NonnullRefPtr<DictObject> encryption_dict)
  50. {
  51. auto filter = TRY(encryption_dict->get_name(document, CommonNames::Filter))->name();
  52. if (filter == "Standard")
  53. return TRY(StandardSecurityHandler::create(document, encryption_dict));
  54. dbgln("Unrecognized security handler filter: {}", filter);
  55. TODO();
  56. }
  57. struct CryptFilter {
  58. CryptFilterMethod method { CryptFilterMethod::None };
  59. int length_in_bits { 0 };
  60. };
  61. static PDFErrorOr<CryptFilter> parse_v4_crypt(Document* document, NonnullRefPtr<DictObject> encryption_dict, DeprecatedString filter)
  62. {
  63. // See 3.5 Encryption, Table 3.18 "Entries common to all encryption dictionaries" for StmF and StrF,
  64. // and 3.5.4 Crypt Filters in the 1.7 spec, in particular Table 3.22 "Entries common to all crypt filter dictionaries".
  65. if (filter == "Identity")
  66. return CryptFilter {};
  67. // "Every crypt filter used in the document must have an entry in this dictionary"
  68. if (!encryption_dict->contains(CommonNames::CF))
  69. return Error(Error::Type::Parse, "Missing CF key in encryption dict for v4");
  70. auto crypt_filter_dicts = TRY(encryption_dict->get_dict(document, CommonNames::CF));
  71. if (!crypt_filter_dicts->contains(filter))
  72. return Error(Error::Type::Parse, "Missing key in CF dict for v4");
  73. auto crypt_filter_dict = TRY(crypt_filter_dicts->get_dict(document, filter));
  74. // "Default value: None"
  75. if (!crypt_filter_dict->contains(CommonNames::CFM))
  76. return CryptFilter {};
  77. auto crypt_filter_method = TRY(crypt_filter_dict->get_name(document, CommonNames::CFM))->name();
  78. if (crypt_filter_method == "None")
  79. return CryptFilter {};
  80. // Table 3.22 in the 1.7 spec says this is optional but doesn't give a default value.
  81. // But the 2.0 spec (ISO 32000 2020) says it's required.
  82. // The 2.0 spec also says "The standard security handler expresses the Length entry in bytes" (!).
  83. if (!crypt_filter_dict->contains(CommonNames::Length))
  84. return Error(Error::Type::Parse, "crypt filter /Length missing");
  85. auto length_in_bits = crypt_filter_dict->get_value(CommonNames::Length).get<int>() * 8;
  86. // NOTE: /CFM's /AuthEvent should be ignored for /StmF, /StrF.
  87. if (crypt_filter_method == "V2")
  88. return CryptFilter { CryptFilterMethod::V2, length_in_bits };
  89. if (crypt_filter_method == "AESV2") {
  90. // "the AES algorithm in Cipher Block Chaining (CBC) mode with a 16-byte block size"
  91. if (length_in_bits != 128)
  92. return Error(Error::Type::Parse, "Unexpected bit size for AESV2");
  93. return CryptFilter { CryptFilterMethod::AESV2, length_in_bits };
  94. }
  95. return Error(Error::Type::Parse, "Unknown crypt filter method");
  96. }
  97. PDFErrorOr<NonnullRefPtr<StandardSecurityHandler>> StandardSecurityHandler::create(Document* document, NonnullRefPtr<DictObject> encryption_dict)
  98. {
  99. auto revision = encryption_dict->get_value(CommonNames::R).get<int>();
  100. auto o = TRY(encryption_dict->get_string(document, CommonNames::O))->string();
  101. auto u = TRY(encryption_dict->get_string(document, CommonNames::U))->string();
  102. auto p = encryption_dict->get_value(CommonNames::P).get<int>();
  103. // V, number: [...] 1 "Algorithm 1 Encryption of data using the RC4 or AES algorithms" in 7.6.2,
  104. // "General Encryption Algorithm," with an encryption key length of 40 bits, see below [...]
  105. // Length, integer: (Optional; PDF 1.4; only if V is 2 or 3) The length of the encryption key, in bits.
  106. // The value shall be a multiple of 8, in the range 40 to 128. Default value: 40.
  107. auto v = encryption_dict->get_value(CommonNames::V).get<int>();
  108. auto method = CryptFilterMethod::V2;
  109. size_t length_in_bits = 40;
  110. if (v == 4) {
  111. // "Default value: Identity"
  112. DeprecatedString stream_filter = "Identity";
  113. if (encryption_dict->contains(CommonNames::StmF))
  114. stream_filter = TRY(encryption_dict->get_name(document, CommonNames::StmF))->name();
  115. DeprecatedString string_filter = "Identity";
  116. if (encryption_dict->contains(CommonNames::StrF))
  117. string_filter = TRY(encryption_dict->get_name(document, CommonNames::StrF))->name();
  118. if (stream_filter != string_filter)
  119. return Error(Error::Type::Parse, "Can't handle StmF and StrF being different");
  120. auto crypt_filter = TRY(parse_v4_crypt(document, encryption_dict, stream_filter));
  121. method = crypt_filter.method;
  122. length_in_bits = crypt_filter.length_in_bits;
  123. } else if (encryption_dict->contains(CommonNames::Length))
  124. length_in_bits = encryption_dict->get_value(CommonNames::Length).get<int>();
  125. else if (v != 1)
  126. return Error(Error::Type::Parse, "Can't determine length of encryption key");
  127. auto length = length_in_bits / 8;
  128. dbgln_if(PDF_DEBUG, "encryption v{}, method {}, length {}", v, (int)method, length);
  129. bool encrypt_metadata = true;
  130. if (encryption_dict->contains(CommonNames::EncryptMetadata))
  131. encryption_dict->get_value(CommonNames::EncryptMetadata).get<bool>();
  132. return adopt_ref(*new StandardSecurityHandler(document, revision, o, u, p, encrypt_metadata, length, method));
  133. }
  134. 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)
  135. : m_document(document)
  136. , m_revision(revision)
  137. , m_o_entry(o_entry)
  138. , m_u_entry(u_entry)
  139. , m_flags(flags)
  140. , m_encrypt_metadata(encrypt_metadata)
  141. , m_length(length)
  142. , m_method(method)
  143. {
  144. }
  145. ByteBuffer StandardSecurityHandler::compute_user_password_value_v2(ByteBuffer password_string)
  146. {
  147. // Algorithm 4: Computing the encryption dictionary's U (user password)
  148. // value (Security handlers of revision 2)
  149. // a) Create an encryption key based on the user password string, as
  150. // described in [Algorithm 2]
  151. auto encryption_key = compute_encryption_key(password_string);
  152. // b) Encrypt the 32-byte padding string shown in step (a) of [Algorithm 2],
  153. // using an RC4 encryption function with the encryption key from the
  154. // preceding step.
  155. RC4 rc4(encryption_key);
  156. auto output = rc4.encrypt(standard_encryption_key_padding_bytes);
  157. // c) Store the result of step (b) as the value of the U entry in the
  158. // encryption dictionary.
  159. return output;
  160. }
  161. ByteBuffer StandardSecurityHandler::compute_user_password_value_v3_and_newer(ByteBuffer password_string)
  162. {
  163. // Algorithm 5: Computing the encryption dictionary's U (user password)
  164. // value (Security handlers of revision 3 or greater)
  165. // a) Create an encryption key based on the user password string, as
  166. // described in [Algorithm 2]
  167. auto encryption_key = compute_encryption_key(password_string);
  168. // b) Initialize the MD5 hash function and pass the 32-byte padding string
  169. // shown in step (a) of [Algorithm 2] as input to this function
  170. Crypto::Hash::MD5 md5;
  171. md5.update(standard_encryption_key_padding_bytes);
  172. // e) Pass the first element of the file's file identifier array to the MD5
  173. // hash function.
  174. auto id_array = MUST(m_document->trailer()->get_array(m_document, CommonNames::ID));
  175. auto first_element_string = MUST(id_array->get_string_at(m_document, 0))->string();
  176. md5.update(first_element_string);
  177. // d) Encrypt the 16-byte result of the hash, using an RC4 encryption function
  178. // with the encryption key from step (a).
  179. RC4 rc4(encryption_key);
  180. auto out = md5.peek();
  181. auto buffer = rc4.encrypt(out.bytes());
  182. // e) Do the following 19 times:
  183. //
  184. // Take the output from the previous invocation of the RC4 function and pass
  185. // it as input to a new invocation of the function; use an encryption key generated
  186. // by taking each byte of the original encryption key obtained in step (a) and
  187. // performing an XOR operation between the that byte and the single-byte value of
  188. // the iteration counter (from 1 to 19).
  189. auto new_encryption_key = MUST(ByteBuffer::create_uninitialized(encryption_key.size()));
  190. for (size_t i = 1; i <= 19; i++) {
  191. for (size_t j = 0; j < encryption_key.size(); j++)
  192. new_encryption_key[j] = encryption_key[j] ^ i;
  193. RC4 new_rc4(new_encryption_key);
  194. buffer = new_rc4.encrypt(buffer);
  195. }
  196. // f) Append 16 bytes of the arbitrary padding to the output from the final invocation
  197. // of the RC4 function and store the 32-byte result as the value of the U entry in
  198. // the encryption dictionary.
  199. VERIFY(buffer.size() == 16);
  200. for (size_t i = 0; i < 16; i++)
  201. buffer.append(0xab);
  202. return buffer;
  203. }
  204. bool StandardSecurityHandler::try_provide_user_password(StringView password_string)
  205. {
  206. // Algorithm 6: Authenticating the user password
  207. // a) Perform all but the last step of [Algorithm 4] or [Algorithm 5] using the
  208. // supplied password string.
  209. ByteBuffer password_buffer = MUST(ByteBuffer::copy(password_string.bytes()));
  210. if (m_revision == 2) {
  211. password_buffer = compute_user_password_value_v2(password_buffer);
  212. } else {
  213. password_buffer = compute_user_password_value_v3_and_newer(password_buffer);
  214. }
  215. // b) If the result of step (a) is equal to the value of the encryption
  216. // dictionary's "U" entry (comparing the first 16 bytes in the case of security
  217. // handlers of revision 3 or greater), the password supplied is the correct user
  218. // password.
  219. auto u_bytes = m_u_entry.bytes();
  220. bool has_user_password;
  221. if (m_revision >= 3)
  222. has_user_password = u_bytes.slice(0, 16) == password_buffer.bytes().slice(0, 16);
  223. else
  224. has_user_password = u_bytes == password_buffer.bytes();
  225. if (!has_user_password)
  226. m_encryption_key = {};
  227. return has_user_password;
  228. }
  229. ByteBuffer StandardSecurityHandler::compute_encryption_key(ByteBuffer password_string)
  230. {
  231. // This function should never be called after we have a valid encryption key.
  232. VERIFY(!m_encryption_key.has_value());
  233. // 7.6.3.3 Encryption Key Algorithm
  234. // Algorithm 2: Computing an encryption key
  235. // a) Pad or truncate the password string to exactly 32 bytes. If the password string
  236. // is more than 32 bytes long, use only its first 32 bytes; if it is less than 32
  237. // bytes long, pad it by appending the required number of additional bytes from the
  238. // beginning of the following padding string: [omitted]
  239. if (password_string.size() > 32) {
  240. password_string.resize(32);
  241. } else {
  242. password_string.append(standard_encryption_key_padding_bytes.data(), 32 - password_string.size());
  243. }
  244. // b) Initialize the MD5 hash function and pass the result of step (a) as input to
  245. // this function.
  246. Crypto::Hash::MD5 md5;
  247. md5.update(password_string);
  248. // c) Pass the value of the encryption dictionary's "O" entry to the MD5 hash function.
  249. md5.update(m_o_entry);
  250. // d) Convert the integer value of the P entry to a 32-bit unsigned binary number and pass
  251. // these bytes to the MD5 hash function, low-order byte first.
  252. md5.update(reinterpret_cast<u8 const*>(&m_flags), sizeof(m_flags));
  253. // e) Pass the first element of the file's file identifier array to the MD5 hash function.
  254. auto id_array = MUST(m_document->trailer()->get_array(m_document, CommonNames::ID));
  255. auto first_element_string = MUST(id_array->get_string_at(m_document, 0))->string();
  256. md5.update(first_element_string);
  257. // f) (Security handlers of revision 4 or greater) if the document metadata is not being
  258. // encrypted, pass 4 bytes with the value 0xffffffff to the MD5 hash function.
  259. if (m_revision >= 4 && !m_encrypt_metadata) {
  260. u32 value = 0xffffffff;
  261. md5.update(reinterpret_cast<u8 const*>(&value), 4);
  262. }
  263. // g) Finish the hash.
  264. // h) (Security handlers of revision 3 or greater) Do the following 50 times:
  265. //
  266. // Take the output from the previous MD5 hash and pass the first n bytes
  267. // of the output as input into a new MD5 hash, where n is the number of
  268. // bytes of the encryption key as defined by the value of the encryption
  269. // dictionary's Length entry.
  270. if (m_revision >= 3) {
  271. ByteBuffer n_bytes;
  272. for (u32 i = 0; i < 50; i++) {
  273. Crypto::Hash::MD5 new_md5;
  274. n_bytes.ensure_capacity(m_length);
  275. while (n_bytes.size() < m_length) {
  276. auto out = md5.peek();
  277. for (size_t j = 0; j < out.data_length() && n_bytes.size() < m_length; j++)
  278. n_bytes.append(out.data[j]);
  279. }
  280. VERIFY(n_bytes.size() == m_length);
  281. new_md5.update(n_bytes);
  282. md5 = move(new_md5);
  283. n_bytes.clear();
  284. }
  285. }
  286. // i) Set the encryption key to the first n bytes of the output from the final MD5
  287. // hash, where n shall always be 5 for security handlers of revision 2 but, for
  288. // security handlers of revision 3 or greater, shall depend on the value of the
  289. // encryption dictionary's Length entry.
  290. size_t n;
  291. if (m_revision == 2) {
  292. n = 5;
  293. } else if (m_revision >= 3) {
  294. n = m_length;
  295. } else {
  296. VERIFY_NOT_REACHED();
  297. }
  298. ByteBuffer encryption_key;
  299. encryption_key.ensure_capacity(n);
  300. while (encryption_key.size() < n) {
  301. auto out = md5.peek();
  302. for (size_t i = 0; encryption_key.size() < n && i < out.data_length(); i++)
  303. encryption_key.append(out.bytes()[i]);
  304. }
  305. m_encryption_key = encryption_key;
  306. return encryption_key;
  307. }
  308. void StandardSecurityHandler::crypt(NonnullRefPtr<Object> object, Reference reference, Crypto::Cipher::Intent direction) const
  309. {
  310. // 7.6.2 General Encryption Algorithm
  311. // Algorithm 1: Encryption of data using the RC3 or AES algorithms
  312. VERIFY(m_encryption_key.has_value());
  313. if (m_method == CryptFilterMethod::None)
  314. return;
  315. // a) Obtain the object number and generation number from the object identifier of
  316. // the string or stream to be encrypted. If the string is a direct object, use
  317. // the identifier of the indirect object containing it.
  318. //
  319. // Note: This is always passed in at parse time because objects don't know their own
  320. // object number.
  321. // b) For all strings and streams with crypt filter specifier; treating the object
  322. // number as binary integers, extend the original n-byte encryption key to n + 5
  323. // bytes by appending the low-order 3 bytes of the object number and the low-order
  324. // 2 bytes of the generation number in that order, low-order byte first. ...
  325. auto encryption_key = m_encryption_key.value();
  326. ReadonlyBytes bytes;
  327. Function<void(ReadonlyBytes)> assign;
  328. if (object->is<StreamObject>()) {
  329. auto stream = object->cast<StreamObject>();
  330. bytes = stream->bytes();
  331. assign = [&object](ReadonlyBytes bytes) {
  332. object->cast<StreamObject>()->buffer() = MUST(ByteBuffer::copy(bytes));
  333. };
  334. if (stream->dict()->contains(CommonNames::Filter)) {
  335. auto filter = MUST(stream->dict()->get_name(m_document, CommonNames::Filter))->name();
  336. if (filter == "Crypt")
  337. TODO();
  338. }
  339. } else if (object->is<StringObject>()) {
  340. auto string = object->cast<StringObject>();
  341. bytes = string->string().bytes();
  342. assign = [&object](ReadonlyBytes bytes) {
  343. object->cast<StringObject>()->set_string(DeprecatedString(bytes));
  344. };
  345. } else {
  346. VERIFY_NOT_REACHED();
  347. }
  348. auto index = reference.as_ref_index();
  349. auto generation = reference.as_ref_generation_index();
  350. encryption_key.append(index & 0xff);
  351. encryption_key.append((index >> 8) & 0xff);
  352. encryption_key.append((index >> 16) & 0xff);
  353. encryption_key.append(generation & 0xff);
  354. encryption_key.append((generation >> 8) & 0xff);
  355. if (m_method == CryptFilterMethod::AESV2) {
  356. encryption_key.append('s');
  357. encryption_key.append('A');
  358. encryption_key.append('l');
  359. encryption_key.append('T');
  360. }
  361. // c) Initialize the MD5 hash function and pass the result of step (b) as input to this
  362. // function.
  363. Crypto::Hash::MD5 md5;
  364. md5.update(encryption_key);
  365. // d) Use the first (n + 5) bytes, up to a maximum of 16, of the output from the MD5
  366. // hash as the key for the RC4 or AES symmetric key algorithms, along with the string
  367. // or stream data to be encrypted.
  368. auto key = MUST(ByteBuffer::copy(md5.peek().bytes()));
  369. if (key.size() > min(encryption_key.size(), 16))
  370. key.resize(encryption_key.size());
  371. if (m_method == CryptFilterMethod::AESV2) {
  372. auto cipher = Crypto::Cipher::AESCipher::CBCMode(key, m_length * 8, direction, Crypto::Cipher::PaddingMode::CMS);
  373. // "The block size parameter is 16 bytes, and the initialization vector is a 16-byte random number
  374. // that is stored as the first 16 bytes of the encrypted stream or string."
  375. static_assert(Crypto::Cipher::AESCipher::block_size() == 16);
  376. if (direction == Crypto::Cipher::Intent::Encryption) {
  377. auto encrypted = MUST(cipher.create_aligned_buffer(bytes.size()));
  378. auto encrypted_span = encrypted.bytes();
  379. auto iv = MUST(ByteBuffer::create_uninitialized(Crypto::Cipher::AESCipher::block_size()));
  380. fill_with_random(iv);
  381. cipher.encrypt(bytes, encrypted_span, iv);
  382. ByteBuffer output;
  383. output.append(iv);
  384. output.append(encrypted_span);
  385. assign(output);
  386. } else {
  387. VERIFY(direction == Crypto::Cipher::Intent::Decryption);
  388. auto iv = bytes.trim(16);
  389. bytes = bytes.slice(16);
  390. auto decrypted = MUST(cipher.create_aligned_buffer(bytes.size()));
  391. auto decrypted_span = decrypted.bytes();
  392. cipher.decrypt(bytes, decrypted_span, iv);
  393. assign(decrypted_span);
  394. }
  395. return;
  396. }
  397. // RC4 is symmetric, so decryption is the same as encryption.
  398. VERIFY(m_method == CryptFilterMethod::V2);
  399. RC4 rc4(key);
  400. auto output = rc4.encrypt(bytes);
  401. assign(output);
  402. }
  403. void StandardSecurityHandler::encrypt(NonnullRefPtr<Object> object, Reference reference) const
  404. {
  405. crypt(object, reference, Crypto::Cipher::Intent::Encryption);
  406. }
  407. void StandardSecurityHandler::decrypt(NonnullRefPtr<Object> object, Reference reference) const
  408. {
  409. crypt(object, reference, Crypto::Cipher::Intent::Decryption);
  410. }
  411. static constexpr auto identity_permutation = iota_array<size_t, 256>(0);
  412. RC4::RC4(ReadonlyBytes key)
  413. : m_bytes(identity_permutation)
  414. {
  415. size_t j = 0;
  416. for (size_t i = 0; i < 256; i++) {
  417. j = (j + m_bytes[i] + key[i % key.size()]) & 0xff;
  418. swap(m_bytes[i], m_bytes[j]);
  419. }
  420. }
  421. void RC4::generate_bytes(ByteBuffer& bytes)
  422. {
  423. size_t i = 0;
  424. size_t j = 0;
  425. for (size_t count = 0; count < bytes.size(); count++) {
  426. i = (i + 1) % 256;
  427. j = (j + m_bytes[i]) % 256;
  428. swap(m_bytes[i], m_bytes[j]);
  429. bytes[count] = m_bytes[(m_bytes[i] + m_bytes[j]) % 256];
  430. }
  431. }
  432. ByteBuffer RC4::encrypt(ReadonlyBytes bytes)
  433. {
  434. auto output = MUST(ByteBuffer::create_uninitialized(bytes.size()));
  435. generate_bytes(output);
  436. for (size_t i = 0; i < bytes.size(); i++)
  437. output[i] ^= bytes[i];
  438. return output;
  439. }
  440. }