K. Moon | 832a694 | 2022-10-31 20:11:31 +0000 | [diff] [blame] | 1 | // Copyright 2014 The PDFium Authors |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | 95e854f | 2015-06-13 00:58:06 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 7 | #include "core/fpdfapi/parser/cpdf_crypto_handler.h" |
Lei Zhang | a9fa50f | 2015-11-10 09:45:32 -0800 | [diff] [blame] | 8 | |
Tom Sepez | a1193ae | 2016-03-09 17:42:18 -0800 | [diff] [blame] | 9 | #include <time.h> |
| 10 | |
Artem Strygin | a081931 | 2017-10-03 21:51:18 +0300 | [diff] [blame] | 11 | #include <algorithm> |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 12 | #include <stack> |
| 13 | #include <utility> |
| 14 | |
Lei Zhang | 865ffb1 | 2019-02-26 20:18:19 +0000 | [diff] [blame] | 15 | #include "constants/form_fields.h" |
Lei Zhang | d145e4b | 2018-10-12 18:54:31 +0000 | [diff] [blame] | 16 | #include "core/fdrm/fx_crypt.h" |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 17 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
| 18 | #include "core/fpdfapi/parser/cpdf_number.h" |
| 19 | #include "core/fpdfapi/parser/cpdf_object_walker.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 20 | #include "core/fpdfapi/parser/cpdf_parser.h" |
| 21 | #include "core/fpdfapi/parser/cpdf_security_handler.h" |
| 22 | #include "core/fpdfapi/parser/cpdf_simple_parser.h" |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 23 | #include "core/fpdfapi/parser/cpdf_stream.h" |
| 24 | #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
| 25 | #include "core/fpdfapi/parser/cpdf_string.h" |
Tom Sepez | a60e6a2 | 2021-01-27 01:21:59 +0000 | [diff] [blame] | 26 | #include "third_party/base/check.h" |
Lei Zhang | 4582920 | 2021-04-16 16:42:11 +0000 | [diff] [blame] | 27 | #include "third_party/base/check_op.h" |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
| 31 | constexpr char kContentsKey[] = "Contents"; |
| 32 | constexpr char kTypeKey[] = "Type"; |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | // static |
| 37 | bool CPDF_CryptoHandler::IsSignatureDictionary( |
| 38 | const CPDF_Dictionary* dictionary) { |
| 39 | if (!dictionary) |
| 40 | return false; |
Tom Sepez | e0dcb6b | 2022-09-08 00:29:53 +0000 | [diff] [blame] | 41 | RetainPtr<const CPDF_Object> type_obj = |
| 42 | dictionary->GetDirectObjectFor(kTypeKey); |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 43 | if (!type_obj) |
Lei Zhang | 865ffb1 | 2019-02-26 20:18:19 +0000 | [diff] [blame] | 44 | type_obj = dictionary->GetDirectObjectFor(pdfium::form_fields::kFT); |
Lei Zhang | f496e25 | 2019-02-26 20:20:19 +0000 | [diff] [blame] | 45 | return type_obj && type_obj->GetString() == pdfium::form_fields::kSig; |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 46 | } |
Lei Zhang | a9fa50f | 2015-11-10 09:45:32 -0800 | [diff] [blame] | 47 | |
Tom Sepez | 652c189 | 2022-04-05 15:41:49 +0000 | [diff] [blame] | 48 | void CPDF_CryptoHandler::EncryptContent(uint32_t objnum, |
| 49 | uint32_t gennum, |
| 50 | pdfium::span<const uint8_t> source, |
| 51 | uint8_t* dest_buf, |
| 52 | size_t& dest_size) const { |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 53 | if (m_Cipher == Cipher::kNone) { |
Tom Sepez | debd2d2 | 2018-05-16 18:32:43 +0000 | [diff] [blame] | 54 | memcpy(dest_buf, source.data(), source.size()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | uint8_t realkey[16]; |
Lei Zhang | 5cedaeb | 2019-12-05 00:23:13 +0000 | [diff] [blame] | 58 | size_t realkeylen = sizeof(realkey); |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 59 | if (m_Cipher != Cipher::kAES || m_KeyLen != 32) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 60 | uint8_t key1[32]; |
dsinclair | 0690c35 | 2016-08-02 10:48:28 -0700 | [diff] [blame] | 61 | PopulateKey(objnum, gennum, key1); |
| 62 | |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 63 | if (m_Cipher == Cipher::kAES) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 64 | memcpy(key1 + m_KeyLen + 5, "sAlT", 4); |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 65 | size_t len = m_Cipher == Cipher::kAES ? m_KeyLen + 9 : m_KeyLen + 5; |
Lei Zhang | ea20851 | 2019-12-18 00:42:11 +0000 | [diff] [blame] | 66 | CRYPT_MD5Generate({key1, len}, realkey); |
Lei Zhang | b92ff3c | 2019-12-05 22:25:28 +0000 | [diff] [blame] | 67 | realkeylen = std::min(m_KeyLen + 5, sizeof(realkey)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 68 | } |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 69 | if (m_Cipher == Cipher::kAES) { |
Tom Sepez | bd8855b | 2018-10-03 19:53:38 +0000 | [diff] [blame] | 70 | CRYPT_AESSetKey(m_pAESContext.get(), |
Tom Sepez | cf83295 | 2021-05-24 18:36:55 +0000 | [diff] [blame] | 71 | m_KeyLen == 32 ? m_EncryptKey : realkey, m_KeyLen); |
Tom Sepez | 652c189 | 2022-04-05 15:41:49 +0000 | [diff] [blame] | 72 | uint8_t iv[16]; |
| 73 | for (int i = 0; i < 16; i++) { |
| 74 | iv[i] = (uint8_t)rand(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | } |
Tom Sepez | 652c189 | 2022-04-05 15:41:49 +0000 | [diff] [blame] | 76 | CRYPT_AESSetIV(m_pAESContext.get(), iv); |
| 77 | memcpy(dest_buf, iv, 16); |
| 78 | int nblocks = source.size() / 16; |
| 79 | CRYPT_AESEncrypt(m_pAESContext.get(), dest_buf + 16, source.data(), |
| 80 | nblocks * 16); |
| 81 | uint8_t padding[16]; |
| 82 | memcpy(padding, source.data() + nblocks * 16, source.size() % 16); |
| 83 | memset(padding + source.size() % 16, 16 - source.size() % 16, |
| 84 | 16 - source.size() % 16); |
| 85 | CRYPT_AESEncrypt(m_pAESContext.get(), dest_buf + nblocks * 16 + 16, padding, |
| 86 | 16); |
| 87 | dest_size = 32 + nblocks * 16; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 88 | } else { |
Lei Zhang | 4582920 | 2021-04-16 16:42:11 +0000 | [diff] [blame] | 89 | DCHECK_EQ(dest_size, source.size()); |
Lei Zhang | 5cedaeb | 2019-12-05 00:23:13 +0000 | [diff] [blame] | 90 | if (dest_buf != source.data()) |
Tom Sepez | debd2d2 | 2018-05-16 18:32:43 +0000 | [diff] [blame] | 91 | memcpy(dest_buf, source.data(), source.size()); |
Lei Zhang | 2e0549b | 2019-12-05 21:59:34 +0000 | [diff] [blame] | 92 | CRYPT_ArcFourCryptBlock({dest_buf, dest_size}, {realkey, realkeylen}); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
Tom Sepez | d21cdda | 2016-02-23 10:11:11 -0800 | [diff] [blame] | 95 | |
| 96 | struct AESCryptContext { |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 97 | bool m_bIV; |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 98 | uint32_t m_BlockOffset; |
Tom Sepez | 332ef54 | 2017-05-05 17:08:07 -0700 | [diff] [blame] | 99 | CRYPT_aes_context m_Context; |
Tom Sepez | 232b918 | 2018-04-03 16:32:19 +0000 | [diff] [blame] | 100 | uint8_t m_Block[16]; |
Tom Sepez | d21cdda | 2016-02-23 10:11:11 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
Tom Sepez | 97197f4 | 2021-10-22 00:50:04 +0000 | [diff] [blame] | 103 | void* CPDF_CryptoHandler::DecryptStart(uint32_t objnum, uint32_t gennum) { |
| 104 | if (m_Cipher == Cipher::kNone) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | return this; |
Tom Sepez | 97197f4 | 2021-10-22 00:50:04 +0000 | [diff] [blame] | 106 | |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 107 | if (m_Cipher == Cipher::kAES && m_KeyLen == 32) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 108 | AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 109 | pContext->m_bIV = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 110 | pContext->m_BlockOffset = 0; |
Tom Sepez | cf83295 | 2021-05-24 18:36:55 +0000 | [diff] [blame] | 111 | CRYPT_AESSetKey(&pContext->m_Context, m_EncryptKey, 32); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 112 | return pContext; |
| 113 | } |
| 114 | uint8_t key1[48]; |
dsinclair | 0690c35 | 2016-08-02 10:48:28 -0700 | [diff] [blame] | 115 | PopulateKey(objnum, gennum, key1); |
| 116 | |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 117 | if (m_Cipher == Cipher::kAES) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 118 | memcpy(key1 + m_KeyLen + 5, "sAlT", 4); |
Lei Zhang | b92ff3c | 2019-12-05 22:25:28 +0000 | [diff] [blame] | 119 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | uint8_t realkey[16]; |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 121 | size_t len = m_Cipher == Cipher::kAES ? m_KeyLen + 9 : m_KeyLen + 5; |
Lei Zhang | ea20851 | 2019-12-18 00:42:11 +0000 | [diff] [blame] | 122 | CRYPT_MD5Generate({key1, len}, realkey); |
Lei Zhang | b92ff3c | 2019-12-05 22:25:28 +0000 | [diff] [blame] | 123 | size_t realkeylen = std::min(m_KeyLen + 5, sizeof(realkey)); |
| 124 | |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 125 | if (m_Cipher == Cipher::kAES) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | AESCryptContext* pContext = FX_Alloc(AESCryptContext, 1); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 127 | pContext->m_bIV = true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 128 | pContext->m_BlockOffset = 0; |
Tom Sepez | cf83295 | 2021-05-24 18:36:55 +0000 | [diff] [blame] | 129 | CRYPT_AESSetKey(&pContext->m_Context, realkey, 16); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 130 | return pContext; |
| 131 | } |
tsepez | d88090f | 2016-11-21 14:29:12 -0800 | [diff] [blame] | 132 | CRYPT_rc4_context* pContext = FX_Alloc(CRYPT_rc4_context, 1); |
Lei Zhang | c4d5acc | 2019-12-06 16:51:20 +0000 | [diff] [blame] | 133 | CRYPT_ArcFourSetup(pContext, {realkey, realkeylen}); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 134 | return pContext; |
| 135 | } |
dsinclair | 0690c35 | 2016-08-02 10:48:28 -0700 | [diff] [blame] | 136 | |
Tom Sepez | 66c6890 | 2022-04-05 15:39:29 +0000 | [diff] [blame] | 137 | bool CPDF_CryptoHandler::DecryptStream(void* context, |
| 138 | pdfium::span<const uint8_t> source, |
Tom Sepez | 44a3bf3 | 2022-08-02 20:21:39 +0000 | [diff] [blame] | 139 | BinaryBuffer& dest_buf) { |
Tom Sepez | debd2d2 | 2018-05-16 18:32:43 +0000 | [diff] [blame] | 140 | if (!context) |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 141 | return false; |
Tom Sepez | debd2d2 | 2018-05-16 18:32:43 +0000 | [diff] [blame] | 142 | |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 143 | if (m_Cipher == Cipher::kNone) { |
Tom Sepez | 5c18e87 | 2022-11-17 20:41:38 +0000 | [diff] [blame] | 144 | dest_buf.AppendSpan(source); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 145 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 146 | } |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 147 | if (m_Cipher == Cipher::kRC4) { |
Lei Zhang | a5747ca | 2022-08-16 18:53:12 +0000 | [diff] [blame] | 148 | size_t old_size = dest_buf.GetSize(); |
Tom Sepez | 5c18e87 | 2022-11-17 20:41:38 +0000 | [diff] [blame] | 149 | dest_buf.AppendSpan(source); |
Tom Sepez | bd17761 | 2022-09-16 21:04:42 +0000 | [diff] [blame] | 150 | CRYPT_ArcFourCrypt( |
| 151 | static_cast<CRYPT_rc4_context*>(context), |
| 152 | dest_buf.GetMutableSpan().subspan(old_size, source.size())); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 153 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 154 | } |
Tom Sepez | 9b8b217 | 2018-04-25 22:12:34 +0000 | [diff] [blame] | 155 | AESCryptContext* pContext = static_cast<AESCryptContext*>(context); |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 156 | uint32_t src_off = 0; |
Tom Sepez | debd2d2 | 2018-05-16 18:32:43 +0000 | [diff] [blame] | 157 | uint32_t src_left = source.size(); |
Anton Bikineev | 7ac1334 | 2022-01-24 21:25:15 +0000 | [diff] [blame] | 158 | while (true) { |
tsepez | b5e8f14 | 2016-03-25 15:18:35 -0700 | [diff] [blame] | 159 | uint32_t copy_size = 16 - pContext->m_BlockOffset; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 160 | if (copy_size > src_left) { |
| 161 | copy_size = src_left; |
| 162 | } |
Tom Sepez | debd2d2 | 2018-05-16 18:32:43 +0000 | [diff] [blame] | 163 | memcpy(pContext->m_Block + pContext->m_BlockOffset, source.data() + src_off, |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 164 | copy_size); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 165 | src_off += copy_size; |
| 166 | src_left -= copy_size; |
| 167 | pContext->m_BlockOffset += copy_size; |
| 168 | if (pContext->m_BlockOffset == 16) { |
Tom Sepez | 66c6890 | 2022-04-05 15:39:29 +0000 | [diff] [blame] | 169 | if (pContext->m_bIV) { |
Tom Sepez | 332ef54 | 2017-05-05 17:08:07 -0700 | [diff] [blame] | 170 | CRYPT_AESSetIV(&pContext->m_Context, pContext->m_Block); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 171 | pContext->m_bIV = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 172 | pContext->m_BlockOffset = 0; |
Tom Sepez | debd2d2 | 2018-05-16 18:32:43 +0000 | [diff] [blame] | 173 | } else if (src_off < source.size()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 174 | uint8_t block_buf[16]; |
Tom Sepez | 66c6890 | 2022-04-05 15:39:29 +0000 | [diff] [blame] | 175 | CRYPT_AESDecrypt(&pContext->m_Context, block_buf, pContext->m_Block, |
| 176 | 16); |
Tom Sepez | 5c18e87 | 2022-11-17 20:41:38 +0000 | [diff] [blame] | 177 | dest_buf.AppendSpan(block_buf); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | pContext->m_BlockOffset = 0; |
| 179 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 180 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 181 | if (!src_left) { |
| 182 | break; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 183 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 184 | } |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 185 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | } |
Tom Sepez | 652c189 | 2022-04-05 15:41:49 +0000 | [diff] [blame] | 187 | |
Tom Sepez | 44a3bf3 | 2022-08-02 20:21:39 +0000 | [diff] [blame] | 188 | bool CPDF_CryptoHandler::DecryptFinish(void* context, BinaryBuffer& dest_buf) { |
Tom Sepez | 652c189 | 2022-04-05 15:41:49 +0000 | [diff] [blame] | 189 | if (!context) |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 190 | return false; |
Tom Sepez | 652c189 | 2022-04-05 15:41:49 +0000 | [diff] [blame] | 191 | |
| 192 | if (m_Cipher == Cipher::kNone) |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 193 | return true; |
Tom Sepez | 652c189 | 2022-04-05 15:41:49 +0000 | [diff] [blame] | 194 | |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 195 | if (m_Cipher == Cipher::kRC4) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | FX_Free(context); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 197 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 198 | } |
Tom Sepez | ea3a252 | 2018-04-26 18:33:58 +0000 | [diff] [blame] | 199 | auto* pContext = static_cast<AESCryptContext*>(context); |
Tom Sepez | 652c189 | 2022-04-05 15:41:49 +0000 | [diff] [blame] | 200 | if (pContext->m_BlockOffset == 16) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 201 | uint8_t block_buf[16]; |
Tom Sepez | 332ef54 | 2017-05-05 17:08:07 -0700 | [diff] [blame] | 202 | CRYPT_AESDecrypt(&pContext->m_Context, block_buf, pContext->m_Block, 16); |
Tom Sepez | 5c18e87 | 2022-11-17 20:41:38 +0000 | [diff] [blame] | 203 | if (block_buf[15] < 16) { |
| 204 | dest_buf.AppendSpan( |
| 205 | pdfium::make_span(block_buf).first(16 - block_buf[15])); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | FX_Free(pContext); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 209 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | } |
dsinclair | 2fa0e13 | 2016-04-19 10:32:45 -0700 | [diff] [blame] | 211 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 212 | ByteString CPDF_CryptoHandler::Decrypt(uint32_t objnum, |
| 213 | uint32_t gennum, |
| 214 | const ByteString& str) { |
Tom Sepez | 44a3bf3 | 2022-08-02 20:21:39 +0000 | [diff] [blame] | 215 | BinaryBuffer dest_buf; |
dsinclair | 2fa0e13 | 2016-04-19 10:32:45 -0700 | [diff] [blame] | 216 | void* context = DecryptStart(objnum, gennum); |
Tom Sepez | 24b3a20 | 2019-07-22 17:47:48 +0000 | [diff] [blame] | 217 | DecryptStream(context, str.raw_span(), dest_buf); |
dsinclair | 2fa0e13 | 2016-04-19 10:32:45 -0700 | [diff] [blame] | 218 | DecryptFinish(context, dest_buf); |
Tom Sepez | 5adae6d | 2021-12-16 23:16:35 +0000 | [diff] [blame] | 219 | return ByteString(dest_buf.GetSpan()); |
dsinclair | 2fa0e13 | 2016-04-19 10:32:45 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Tom Sepez | c73f971 | 2021-11-09 17:04:29 +0000 | [diff] [blame] | 222 | size_t CPDF_CryptoHandler::DecryptGetSize(size_t src_size) { |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 223 | return m_Cipher == Cipher::kAES ? src_size - 16 : src_size; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 224 | } |
Lei Zhang | aa23e70 | 2016-01-29 18:03:40 -0800 | [diff] [blame] | 225 | |
Ryan Harrison | 5b2092a | 2017-09-12 15:30:55 -0400 | [diff] [blame] | 226 | bool CPDF_CryptoHandler::IsCipherAES() const { |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 227 | return m_Cipher == Cipher::kAES; |
Ryan Harrison | 5b2092a | 2017-09-12 15:30:55 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Tom Sepez | a3097da | 2019-05-01 16:42:36 +0000 | [diff] [blame] | 230 | bool CPDF_CryptoHandler::DecryptObjectTree(RetainPtr<CPDF_Object> object) { |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 231 | if (!object) |
Tom Sepez | a3097da | 2019-05-01 16:42:36 +0000 | [diff] [blame] | 232 | return false; |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 233 | |
| 234 | struct MayBeSignature { |
Tom Sepez | a8e77a1 | 2023-05-05 23:40:52 +0000 | [diff] [blame] | 235 | RetainPtr<const CPDF_Dictionary> parent; |
Tom Sepez | 65f7bed | 2022-09-06 18:22:02 +0000 | [diff] [blame] | 236 | RetainPtr<CPDF_Object> contents; |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | std::stack<MayBeSignature> may_be_sign_dictionaries; |
| 240 | const uint32_t obj_num = object->GetObjNum(); |
| 241 | const uint32_t gen_num = object->GetGenNum(); |
| 242 | |
Tom Sepez | 65f7bed | 2022-09-06 18:22:02 +0000 | [diff] [blame] | 243 | RetainPtr<CPDF_Object> object_to_decrypt = object; |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 244 | while (object_to_decrypt) { |
Tom Sepez | c3ff418 | 2022-09-06 19:06:02 +0000 | [diff] [blame] | 245 | CPDF_NonConstObjectWalker walker(std::move(object_to_decrypt)); |
Tom Sepez | 65f7bed | 2022-09-06 18:22:02 +0000 | [diff] [blame] | 246 | while (RetainPtr<CPDF_Object> child = walker.GetNext()) { |
Tom Sepez | a416d84 | 2022-09-16 22:24:24 +0000 | [diff] [blame] | 247 | RetainPtr<const CPDF_Dictionary> parent_dict = |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 248 | walker.GetParent() ? walker.GetParent()->GetDict() : nullptr; |
| 249 | if (walker.dictionary_key() == kContentsKey && |
Lei Zhang | 865ffb1 | 2019-02-26 20:18:19 +0000 | [diff] [blame] | 250 | (parent_dict->KeyExist(kTypeKey) || |
| 251 | parent_dict->KeyExist(pdfium::form_fields::kFT))) { |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 252 | // This object may be contents of signature dictionary. |
| 253 | // But now values of 'Type' and 'FT' of dictionary keys are encrypted, |
| 254 | // and we can not check this. |
| 255 | // Temporary skip it, to prevent signature corruption. |
| 256 | // It will be decrypted on next interations, if this is not contents of |
| 257 | // signature dictionary. |
Tom Sepez | a8e77a1 | 2023-05-05 23:40:52 +0000 | [diff] [blame] | 258 | may_be_sign_dictionaries.push( |
| 259 | {std::move(parent_dict), std::move(child)}); |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 260 | walker.SkipWalkIntoCurrentObject(); |
| 261 | continue; |
| 262 | } |
| 263 | // Strings decryption. |
| 264 | if (child->IsString()) { |
| 265 | // TODO(art-snake): Move decryption into the CPDF_String class. |
Tom Sepez | d6daaed | 2022-09-02 23:58:32 +0000 | [diff] [blame] | 266 | CPDF_String* str = child->AsMutableString(); |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 267 | str->SetString(Decrypt(obj_num, gen_num, str->GetString())); |
| 268 | } |
| 269 | // Stream decryption. |
| 270 | if (child->IsStream()) { |
| 271 | // TODO(art-snake): Move decryption into the CPDF_Stream class. |
Tom Sepez | d6daaed | 2022-09-02 23:58:32 +0000 | [diff] [blame] | 272 | CPDF_Stream* stream = child->AsMutableStream(); |
Tom Sepez | 23705d9 | 2022-09-13 00:32:26 +0000 | [diff] [blame] | 273 | auto stream_access = |
| 274 | pdfium::MakeRetain<CPDF_StreamAcc>(pdfium::WrapRetain(stream)); |
Lei Zhang | 07401ba | 2017-12-11 22:12:08 +0000 | [diff] [blame] | 275 | stream_access->LoadAllDataRaw(); |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 276 | |
| 277 | if (IsCipherAES() && stream_access->GetSize() < 16) { |
Tom Sepez | 367ed46 | 2018-08-23 23:52:53 +0000 | [diff] [blame] | 278 | stream->SetData({}); |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 279 | continue; |
| 280 | } |
| 281 | |
Tom Sepez | 44a3bf3 | 2022-08-02 20:21:39 +0000 | [diff] [blame] | 282 | BinaryBuffer decrypted_buf; |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 283 | decrypted_buf.EstimateSize(DecryptGetSize(stream_access->GetSize())); |
| 284 | |
| 285 | void* context = DecryptStart(obj_num, gen_num); |
| 286 | bool decrypt_result = |
Lei Zhang | dd4e75a | 2019-12-05 22:00:04 +0000 | [diff] [blame] | 287 | DecryptStream(context, stream_access->GetSpan(), decrypted_buf); |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 288 | decrypt_result &= DecryptFinish(context, decrypted_buf); |
| 289 | if (decrypt_result) { |
Lei Zhang | d8db304 | 2022-09-07 08:46:13 +0000 | [diff] [blame] | 290 | stream->TakeData(decrypted_buf.DetachBuffer()); |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 291 | } else { |
| 292 | // Decryption failed, set the stream to empty |
Tom Sepez | 367ed46 | 2018-08-23 23:52:53 +0000 | [diff] [blame] | 293 | stream->SetData({}); |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
| 297 | // Signature dictionaries check. |
| 298 | while (!may_be_sign_dictionaries.empty()) { |
asweintraub | 84cf0b2 | 2019-06-07 16:36:20 +0000 | [diff] [blame] | 299 | auto dict_and_contents = may_be_sign_dictionaries.top(); |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 300 | may_be_sign_dictionaries.pop(); |
| 301 | if (!IsSignatureDictionary(dict_and_contents.parent)) { |
| 302 | // This is not signature dictionary. Do decrypt its contents. |
| 303 | object_to_decrypt = dict_and_contents.contents; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | } |
Tom Sepez | a3097da | 2019-05-01 16:42:36 +0000 | [diff] [blame] | 308 | return true; |
Artem Strygin | d8169d7 | 2017-10-02 19:19:28 +0300 | [diff] [blame] | 309 | } |
| 310 | |
Tom Sepez | debd2d2 | 2018-05-16 18:32:43 +0000 | [diff] [blame] | 311 | size_t CPDF_CryptoHandler::EncryptGetSize( |
Tom Sepez | 6facd15 | 2018-05-16 17:07:02 +0000 | [diff] [blame] | 312 | pdfium::span<const uint8_t> source) const { |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 313 | return m_Cipher == Cipher::kAES ? source.size() + 32 : source.size(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | } |
Tom Sepez | 332ef54 | 2017-05-05 17:08:07 -0700 | [diff] [blame] | 315 | |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 316 | CPDF_CryptoHandler::CPDF_CryptoHandler(Cipher cipher, |
Artem Strygin | a081931 | 2017-10-03 21:51:18 +0300 | [diff] [blame] | 317 | const uint8_t* key, |
Lei Zhang | b92ff3c | 2019-12-05 22:25:28 +0000 | [diff] [blame] | 318 | size_t keylen) |
| 319 | : m_KeyLen(std::min<size_t>(keylen, 32)), m_Cipher(cipher) { |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 320 | DCHECK(cipher != Cipher::kAES || keylen == 16 || keylen == 24 || |
Artem Strygin | a081931 | 2017-10-03 21:51:18 +0300 | [diff] [blame] | 321 | keylen == 32); |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 322 | DCHECK(cipher != Cipher::kAES2 || keylen == 32); |
| 323 | DCHECK(cipher != Cipher::kRC4 || (keylen >= 5 && keylen <= 16)); |
Artem Strygin | a081931 | 2017-10-03 21:51:18 +0300 | [diff] [blame] | 324 | |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 325 | if (m_Cipher != Cipher::kNone) |
Artem Strygin | a081931 | 2017-10-03 21:51:18 +0300 | [diff] [blame] | 326 | memcpy(m_EncryptKey, key, m_KeyLen); |
| 327 | |
Tom Sepez | 12d343e | 2021-05-19 01:50:24 +0000 | [diff] [blame] | 328 | if (m_Cipher == Cipher::kAES) |
Artem Strygin | a081931 | 2017-10-03 21:51:18 +0300 | [diff] [blame] | 329 | m_pAESContext.reset(FX_Alloc(CRYPT_aes_context, 1)); |
| 330 | } |
Tom Sepez | 332ef54 | 2017-05-05 17:08:07 -0700 | [diff] [blame] | 331 | |
Lei Zhang | b92ff3c | 2019-12-05 22:25:28 +0000 | [diff] [blame] | 332 | CPDF_CryptoHandler::~CPDF_CryptoHandler() = default; |
dsinclair | 0690c35 | 2016-08-02 10:48:28 -0700 | [diff] [blame] | 333 | |
| 334 | void CPDF_CryptoHandler::PopulateKey(uint32_t objnum, |
| 335 | uint32_t gennum, |
Tom Sepez | fe7dcb0 | 2021-04-22 16:53:45 +0000 | [diff] [blame] | 336 | uint8_t* key) const { |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 337 | memcpy(key, m_EncryptKey, m_KeyLen); |
dsinclair | 0690c35 | 2016-08-02 10:48:28 -0700 | [diff] [blame] | 338 | key[m_KeyLen + 0] = (uint8_t)objnum; |
| 339 | key[m_KeyLen + 1] = (uint8_t)(objnum >> 8); |
| 340 | key[m_KeyLen + 2] = (uint8_t)(objnum >> 16); |
| 341 | key[m_KeyLen + 3] = (uint8_t)gennum; |
| 342 | key[m_KeyLen + 4] = (uint8_t)(gennum >> 8); |
| 343 | } |