2017-01-03 09:35:18 +08:00
|
|
|
// Copyright (c) 2014-2017 Thomas Fussell
|
2016-10-25 10:09:15 +08:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE
|
|
|
|
//
|
|
|
|
// @license: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
// @author: see AUTHORS file
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2017-04-12 00:02:35 +08:00
|
|
|
#include <array>
|
2017-04-21 02:03:03 +08:00
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-04-24 06:18:35 +08:00
|
|
|
#include <detail/binary.hpp>
|
2017-04-12 00:02:35 +08:00
|
|
|
#include <detail/constants.hpp>
|
2017-04-21 02:03:03 +08:00
|
|
|
#include <detail/unicode.hpp>
|
|
|
|
#include <detail/cryptography/encryption_info.hpp>
|
|
|
|
#include <detail/cryptography/aes.hpp>
|
|
|
|
#include <detail/cryptography/base64.hpp>
|
2017-04-22 07:52:02 +08:00
|
|
|
#include <detail/cryptography/compound_document.hpp>
|
2017-04-21 02:03:03 +08:00
|
|
|
#include <detail/cryptography/value_traits.hpp>
|
2017-04-23 23:53:52 +08:00
|
|
|
#include <detail/cryptography/xlsx_crypto_consumer.hpp>
|
2017-04-21 02:03:03 +08:00
|
|
|
#include <detail/external/include_libstudxml.hpp>
|
|
|
|
#include <detail/serialization/vector_streambuf.hpp>
|
|
|
|
#include <detail/serialization/xlsx_consumer.hpp>
|
|
|
|
#include <xlnt/utils/exceptions.hpp>
|
2016-10-10 19:28:49 +08:00
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
namespace {
|
2016-10-10 19:28:49 +08:00
|
|
|
|
2017-04-21 02:03:03 +08:00
|
|
|
using xlnt::detail::byte;
|
2017-04-24 06:18:35 +08:00
|
|
|
using xlnt::detail::binary_reader;
|
2017-04-21 02:03:03 +08:00
|
|
|
using xlnt::detail::encryption_info;
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2017-04-12 02:23:54 +08:00
|
|
|
std::vector<std::uint8_t> decrypt_xlsx_standard(
|
2017-04-22 07:52:02 +08:00
|
|
|
encryption_info info,
|
2017-04-23 02:25:27 +08:00
|
|
|
const std::vector<std::uint8_t> &encrypted_package)
|
2017-02-18 13:11:06 +08:00
|
|
|
{
|
2017-04-22 07:52:02 +08:00
|
|
|
const auto key = info.calculate_key();
|
|
|
|
|
2017-04-24 06:18:35 +08:00
|
|
|
auto reader = binary_reader(encrypted_package);
|
2017-04-23 02:25:27 +08:00
|
|
|
auto decrypted_size = reader.read<std::uint64_t>();
|
|
|
|
auto decrypted = xlnt::detail::aes_ecb_decrypt(encrypted_package, key, reader.offset());
|
2017-04-22 07:52:02 +08:00
|
|
|
decrypted.resize(static_cast<std::size_t>(decrypted_size));
|
|
|
|
|
|
|
|
return decrypted;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::uint8_t> decrypt_xlsx_agile(
|
|
|
|
const encryption_info &info,
|
2017-04-22 09:58:40 +08:00
|
|
|
const std::vector<std::uint8_t> &encrypted_package)
|
2017-04-22 07:52:02 +08:00
|
|
|
{
|
2017-04-22 09:58:40 +08:00
|
|
|
static const auto segment_length = std::size_t(4096);
|
|
|
|
|
2017-04-22 07:52:02 +08:00
|
|
|
const auto key = info.calculate_key();
|
|
|
|
|
|
|
|
auto salt_size = info.agile.key_data.salt_size;
|
|
|
|
auto salt_with_block_key = info.agile.key_data.salt_value;
|
|
|
|
salt_with_block_key.resize(salt_size + sizeof(std::uint32_t), 0);
|
|
|
|
|
|
|
|
auto &segment = *reinterpret_cast<std::uint32_t *>(salt_with_block_key.data() + salt_size);
|
|
|
|
auto total_size = static_cast<std::size_t>(*reinterpret_cast<const std::uint64_t *>(encrypted_package.data()));
|
|
|
|
|
|
|
|
std::vector<std::uint8_t> encrypted_segment(segment_length, 0);
|
|
|
|
std::vector<std::uint8_t> decrypted_package;
|
|
|
|
decrypted_package.reserve(encrypted_package.size() - 8);
|
2016-12-14 07:48:02 +08:00
|
|
|
|
2017-04-22 07:52:02 +08:00
|
|
|
for (std::size_t i = 8; i < encrypted_package.size(); i += segment_length)
|
|
|
|
{
|
|
|
|
auto iv = hash(info.agile.key_encryptor.hash, salt_with_block_key);
|
|
|
|
iv.resize(16);
|
|
|
|
|
|
|
|
auto segment_begin = encrypted_package.begin() + static_cast<std::ptrdiff_t>(i);
|
|
|
|
auto current_segment_length = std::min(segment_length, encrypted_package.size() - i);
|
|
|
|
auto segment_end = encrypted_package.begin() + static_cast<std::ptrdiff_t>(i + current_segment_length);
|
|
|
|
encrypted_segment.assign(segment_begin, segment_end);
|
|
|
|
auto decrypted_segment = xlnt::detail::aes_cbc_decrypt(encrypted_segment, key, iv);
|
|
|
|
decrypted_segment.resize(current_segment_length);
|
|
|
|
|
|
|
|
decrypted_package.insert(
|
|
|
|
decrypted_package.end(),
|
|
|
|
decrypted_segment.begin(),
|
|
|
|
decrypted_segment.end());
|
|
|
|
|
|
|
|
++segment;
|
|
|
|
}
|
|
|
|
|
|
|
|
decrypted_package.resize(total_size);
|
|
|
|
|
|
|
|
return decrypted_package;
|
|
|
|
}
|
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
encryption_info::standard_encryption_info read_standard_encryption_info(const std::vector<std::uint8_t> &info_bytes)
|
2017-04-22 07:52:02 +08:00
|
|
|
{
|
2017-04-24 04:56:01 +08:00
|
|
|
encryption_info::standard_encryption_info result;
|
2016-12-14 07:48:02 +08:00
|
|
|
|
2017-04-24 06:18:35 +08:00
|
|
|
auto reader = binary_reader(info_bytes);
|
2017-04-23 02:25:27 +08:00
|
|
|
|
|
|
|
// skip version info
|
|
|
|
reader.read<std::uint32_t>();
|
|
|
|
reader.read<std::uint32_t>();
|
2017-04-21 02:03:03 +08:00
|
|
|
|
2017-04-23 02:25:27 +08:00
|
|
|
auto header_length = reader.read<std::uint32_t>();
|
|
|
|
auto index_at_start = reader.offset();
|
|
|
|
/*auto skip_flags = */ reader.read<std::uint32_t>();
|
|
|
|
/*auto size_extra = */ reader.read<std::uint32_t>();
|
|
|
|
auto alg_id = reader.read<std::uint32_t>();
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
if (alg_id == 0 || alg_id == 0x0000660E || alg_id == 0x0000660F || alg_id == 0x00006610)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-04-24 04:56:01 +08:00
|
|
|
result.cipher = xlnt::detail::cipher_algorithm::aes;
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
2017-02-18 13:11:06 +08:00
|
|
|
else
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
throw xlnt::exception("invalid cipher algorithm");
|
2016-11-08 10:11:30 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-04-23 02:25:27 +08:00
|
|
|
auto alg_id_hash = reader.read<std::uint32_t>();
|
2017-02-18 13:11:06 +08:00
|
|
|
if (alg_id_hash != 0x00008004 && alg_id_hash == 0)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
throw xlnt::exception("invalid hash algorithm");
|
|
|
|
}
|
2016-12-24 23:04:57 +08:00
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
result.key_bits = reader.read<std::uint32_t>();
|
|
|
|
result.key_bytes = result.key_bits / 8;
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-04-23 02:25:27 +08:00
|
|
|
auto provider_type = reader.read<std::uint32_t>();
|
2017-02-18 13:11:06 +08:00
|
|
|
if (provider_type != 0 && provider_type != 0x00000018)
|
|
|
|
{
|
|
|
|
throw xlnt::exception("invalid provider type");
|
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-04-23 02:25:27 +08:00
|
|
|
reader.read<std::uint32_t>(); // reserved 1
|
|
|
|
if (reader.read<std::uint32_t>() != 0) // reserved 2
|
2017-02-18 13:11:06 +08:00
|
|
|
{
|
|
|
|
throw xlnt::exception("invalid header");
|
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-04-23 02:25:27 +08:00
|
|
|
const auto csp_name_length = header_length - (reader.offset() - index_at_start);
|
2017-02-18 13:11:06 +08:00
|
|
|
std::vector<std::uint16_t> csp_name_wide(
|
2017-04-23 02:25:27 +08:00
|
|
|
reinterpret_cast<const std::uint16_t *>(&*(info_bytes.begin() + static_cast<std::ptrdiff_t>(reader.offset()))),
|
2017-02-18 13:11:06 +08:00
|
|
|
reinterpret_cast<const std::uint16_t *>(
|
2017-04-23 02:25:27 +08:00
|
|
|
&*(info_bytes.begin() + static_cast<std::ptrdiff_t>(reader.offset() + csp_name_length))));
|
2017-02-18 13:11:06 +08:00
|
|
|
std::string csp_name(csp_name_wide.begin(), csp_name_wide.end() - 1); // without trailing null
|
|
|
|
if (csp_name != "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
|
|
|
|
&& csp_name != "Microsoft Enhanced RSA and AES Cryptographic Provider")
|
|
|
|
{
|
|
|
|
throw xlnt::exception("invalid cryptographic provider");
|
|
|
|
}
|
2017-04-23 08:43:26 +08:00
|
|
|
reader.offset(reader.offset() + csp_name_length);
|
2017-02-18 13:11:06 +08:00
|
|
|
|
2017-04-23 02:25:27 +08:00
|
|
|
const auto salt_size = reader.read<std::uint32_t>();
|
2017-04-24 04:56:01 +08:00
|
|
|
result.salt = std::vector<std::uint8_t>(
|
2017-04-23 02:25:27 +08:00
|
|
|
info_bytes.begin() + static_cast<std::ptrdiff_t>(reader.offset()),
|
|
|
|
info_bytes.begin() + static_cast<std::ptrdiff_t>(reader.offset() + salt_size));
|
|
|
|
reader.offset(reader.offset() + salt_size);
|
2017-02-18 13:11:06 +08:00
|
|
|
|
|
|
|
static const auto verifier_size = std::size_t(16);
|
2017-04-24 04:56:01 +08:00
|
|
|
result.encrypted_verifier = std::vector<std::uint8_t>(
|
2017-04-23 02:25:27 +08:00
|
|
|
info_bytes.begin() + static_cast<std::ptrdiff_t>(reader.offset()),
|
|
|
|
info_bytes.begin() + static_cast<std::ptrdiff_t>(reader.offset() + verifier_size));
|
|
|
|
reader.offset(reader.offset() + verifier_size);
|
2017-02-18 13:11:06 +08:00
|
|
|
|
2017-04-23 08:43:26 +08:00
|
|
|
/*const auto verifier_hash_size = */reader.read<std::uint32_t>();
|
2017-04-20 04:00:33 +08:00
|
|
|
const auto encrypted_verifier_hash_size = std::size_t(32);
|
2017-04-24 04:56:01 +08:00
|
|
|
result.encrypted_verifier_hash = std::vector<std::uint8_t>(
|
2017-04-23 02:25:27 +08:00
|
|
|
info_bytes.begin() + static_cast<std::ptrdiff_t>(reader.offset()),
|
|
|
|
info_bytes.begin() + static_cast<std::ptrdiff_t>(reader.offset() + encrypted_verifier_hash_size));
|
|
|
|
reader.offset(reader.offset() + encrypted_verifier_hash_size);
|
2017-04-20 04:00:33 +08:00
|
|
|
|
2017-04-23 02:25:27 +08:00
|
|
|
if (reader.offset() != info_bytes.size())
|
2017-04-20 04:00:33 +08:00
|
|
|
{
|
|
|
|
throw xlnt::exception("extra data after encryption info");
|
|
|
|
}
|
2017-02-18 13:11:06 +08:00
|
|
|
|
2017-04-22 07:52:02 +08:00
|
|
|
return result;
|
2017-02-18 13:11:06 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
encryption_info::agile_encryption_info read_agile_encryption_info(const std::vector<std::uint8_t> &info_bytes)
|
2017-02-18 13:11:06 +08:00
|
|
|
{
|
2017-04-12 05:49:07 +08:00
|
|
|
using xlnt::detail::decode_base64;
|
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
static const auto &xmlns = xlnt::constants::ns("encryption");
|
|
|
|
static const auto &xmlns_p = xlnt::constants::ns("encryption-password");
|
|
|
|
// static const auto &xmlns_c = xlnt::constants::namespace_("encryption-certificate");
|
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
encryption_info::agile_encryption_info result;
|
2017-02-18 13:11:06 +08:00
|
|
|
|
2017-04-22 09:58:40 +08:00
|
|
|
auto header_size = std::size_t(8);
|
|
|
|
xml::parser parser(info_bytes.data() + header_size, info_bytes.size() - header_size, "EncryptionInfo");
|
2017-02-18 13:11:06 +08:00
|
|
|
|
|
|
|
parser.next_expect(xml::parser::event_type::start_element, xmlns, "encryption");
|
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
auto &key_data = result.key_data;
|
2017-02-18 13:11:06 +08:00
|
|
|
parser.next_expect(xml::parser::event_type::start_element, xmlns, "keyData");
|
2017-04-24 04:56:01 +08:00
|
|
|
key_data.salt_size = parser.attribute<std::size_t>("saltSize");
|
|
|
|
key_data.block_size = parser.attribute<std::size_t>("blockSize");
|
|
|
|
key_data.key_bits = parser.attribute<std::size_t>("keyBits");
|
|
|
|
key_data.hash_size = parser.attribute<std::size_t>("hashSize");
|
|
|
|
key_data.cipher_algorithm = parser.attribute("cipherAlgorithm");
|
|
|
|
key_data.cipher_chaining = parser.attribute("cipherChaining");
|
|
|
|
key_data.hash_algorithm = parser.attribute("hashAlgorithm");
|
|
|
|
key_data.salt_value = decode_base64(parser.attribute("saltValue"));
|
2017-02-18 13:11:06 +08:00
|
|
|
parser.next_expect(xml::parser::event_type::end_element, xmlns, "keyData");
|
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
auto &data_integrity = result.data_integrity;
|
2017-02-18 13:11:06 +08:00
|
|
|
parser.next_expect(xml::parser::event_type::start_element, xmlns, "dataIntegrity");
|
2017-04-24 04:56:01 +08:00
|
|
|
data_integrity.hmac_key = decode_base64(parser.attribute("encryptedHmacKey"));
|
|
|
|
data_integrity.hmac_value = decode_base64(parser.attribute("encryptedHmacValue"));
|
2017-02-18 13:11:06 +08:00
|
|
|
parser.next_expect(xml::parser::event_type::end_element, xmlns, "dataIntegrity");
|
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
auto &key_encryptor = result.key_encryptor;
|
2017-02-18 13:11:06 +08:00
|
|
|
parser.next_expect(xml::parser::event_type::start_element, xmlns, "keyEncryptors");
|
|
|
|
parser.next_expect(xml::parser::event_type::start_element, xmlns, "keyEncryptor");
|
|
|
|
parser.attribute("uri");
|
|
|
|
bool any_password_key = false;
|
|
|
|
|
|
|
|
while (parser.peek() != xml::parser::event_type::end_element)
|
2016-11-10 08:52:18 +08:00
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
parser.next_expect(xml::parser::event_type::start_element);
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
if (parser.namespace_() == xmlns_p && parser.name() == "encryptedKey")
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
any_password_key = true;
|
2017-04-24 04:56:01 +08:00
|
|
|
key_encryptor.spin_count = parser.attribute<std::size_t>("spinCount");
|
|
|
|
key_encryptor.salt_size = parser.attribute<std::size_t>("saltSize");
|
|
|
|
key_encryptor.block_size = parser.attribute<std::size_t>("blockSize");
|
|
|
|
key_encryptor.key_bits = parser.attribute<std::size_t>("keyBits");
|
|
|
|
key_encryptor.hash_size = parser.attribute<std::size_t>("hashSize");
|
|
|
|
key_encryptor.cipher_algorithm = parser.attribute("cipherAlgorithm");
|
|
|
|
key_encryptor.cipher_chaining = parser.attribute("cipherChaining");
|
|
|
|
key_encryptor.hash = parser.attribute<xlnt::detail::hash_algorithm>("hashAlgorithm");
|
|
|
|
key_encryptor.salt_value = decode_base64(parser.attribute("saltValue"));
|
|
|
|
key_encryptor.verifier_hash_input = decode_base64(parser.attribute("encryptedVerifierHashInput"));
|
|
|
|
key_encryptor.verifier_hash_value = decode_base64(parser.attribute("encryptedVerifierHashValue"));
|
|
|
|
key_encryptor.encrypted_key_value = decode_base64(parser.attribute("encryptedKeyValue"));
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
2017-02-18 13:11:06 +08:00
|
|
|
else
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
2017-02-18 13:11:06 +08:00
|
|
|
throw xlnt::unsupported("other encryption key types not supported");
|
2016-11-01 08:48:43 +08:00
|
|
|
}
|
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
parser.next_expect(xml::parser::event_type::end_element);
|
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
if (!any_password_key)
|
|
|
|
{
|
2017-04-19 07:50:37 +08:00
|
|
|
throw xlnt::exception("no password key in keyEncryptors");
|
2017-02-18 13:11:06 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
parser.next_expect(xml::parser::event_type::end_element, xmlns, "keyEncryptor");
|
|
|
|
parser.next_expect(xml::parser::event_type::end_element, xmlns, "keyEncryptors");
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-02-18 13:11:06 +08:00
|
|
|
parser.next_expect(xml::parser::event_type::end_element, xmlns, "encryption");
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-04-22 07:52:02 +08:00
|
|
|
return result;
|
2017-02-18 13:11:06 +08:00
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
encryption_info read_encryption_info(const std::vector<std::uint8_t> &info_bytes, const std::u16string &password)
|
2017-02-18 13:11:06 +08:00
|
|
|
{
|
2017-04-22 07:52:02 +08:00
|
|
|
encryption_info info;
|
2017-04-24 04:56:01 +08:00
|
|
|
|
|
|
|
info.password = password;
|
2017-02-18 13:11:06 +08:00
|
|
|
|
2017-04-24 06:18:35 +08:00
|
|
|
auto reader = binary_reader(info_bytes);
|
2017-02-18 13:11:06 +08:00
|
|
|
|
2017-04-23 02:25:27 +08:00
|
|
|
auto version_major = reader.read<std::uint16_t>();
|
|
|
|
auto version_minor = reader.read<std::uint16_t>();
|
|
|
|
auto encryption_flags = reader.read<std::uint32_t>();
|
2017-04-24 04:56:01 +08:00
|
|
|
|
|
|
|
info.is_agile = version_major == 4 && version_minor == 4;
|
2017-02-18 13:11:06 +08:00
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
if (info.is_agile)
|
2017-02-18 13:11:06 +08:00
|
|
|
{
|
|
|
|
if (encryption_flags != 0x40)
|
2016-11-01 08:48:43 +08:00
|
|
|
{
|
|
|
|
throw xlnt::exception("bad header");
|
|
|
|
}
|
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
info.agile = read_agile_encryption_info(info_bytes);
|
2017-02-18 13:11:06 +08:00
|
|
|
}
|
2017-04-24 04:56:01 +08:00
|
|
|
else
|
2017-02-18 13:11:06 +08:00
|
|
|
{
|
2017-04-24 04:56:01 +08:00
|
|
|
if (version_minor != 2 || (version_major != 2 && version_major != 3 && version_major != 4))
|
|
|
|
{
|
|
|
|
throw xlnt::exception("unsupported encryption version");
|
|
|
|
}
|
2016-11-01 08:48:43 +08:00
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
if ((encryption_flags & 0b00000011) != 0) // Reserved1 and Reserved2, MUST be 0
|
|
|
|
{
|
|
|
|
throw xlnt::exception("bad header");
|
|
|
|
}
|
2016-11-08 10:55:40 +08:00
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
if ((encryption_flags & 0b00000100) == 0 // fCryptoAPI
|
|
|
|
|| (encryption_flags & 0b00010000) != 0) // fExternal
|
|
|
|
{
|
|
|
|
throw xlnt::exception("extensible encryption is not supported");
|
|
|
|
}
|
2016-11-08 10:55:40 +08:00
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
if ((encryption_flags & 0b00100000) == 0) // fAES
|
|
|
|
{
|
|
|
|
throw xlnt::exception("not an OOXML document");
|
|
|
|
}
|
2017-02-18 13:11:06 +08:00
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
info.standard = read_standard_encryption_info(info_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
2017-04-22 07:52:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::uint8_t> decrypt_xlsx(
|
|
|
|
const std::vector<std::uint8_t> &bytes,
|
|
|
|
const std::u16string &password)
|
|
|
|
{
|
|
|
|
if (bytes.empty())
|
|
|
|
{
|
|
|
|
throw xlnt::exception("empty file");
|
|
|
|
}
|
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
xlnt::detail::compound_document_reader document(bytes);
|
2017-04-22 07:52:02 +08:00
|
|
|
|
2017-04-24 04:56:01 +08:00
|
|
|
auto encryption_info = read_encryption_info(
|
|
|
|
document.read_stream(u"EncryptionInfo"), password);
|
|
|
|
auto encrypted_package = document.read_stream(u"EncryptedPackage");
|
2017-04-22 07:52:02 +08:00
|
|
|
|
|
|
|
return encryption_info.is_agile
|
2017-04-22 09:58:40 +08:00
|
|
|
? decrypt_xlsx_agile(encryption_info, encrypted_package)
|
2017-04-22 07:52:02 +08:00
|
|
|
: decrypt_xlsx_standard(encryption_info, encrypted_package);
|
2017-02-18 13:11:06 +08:00
|
|
|
}
|
2016-11-10 08:52:18 +08:00
|
|
|
|
2017-04-12 00:02:35 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace xlnt {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
std::vector<std::uint8_t> XLNT_API decrypt_xlsx(const std::vector<std::uint8_t> &data, const std::string &password)
|
|
|
|
{
|
2017-04-13 07:21:21 +08:00
|
|
|
return ::decrypt_xlsx(data, utf8_to_utf16(password));
|
2017-04-12 00:02:35 +08:00
|
|
|
}
|
|
|
|
|
2016-10-10 19:28:49 +08:00
|
|
|
void xlsx_consumer::read(std::istream &source, const std::string &password)
|
|
|
|
{
|
2016-12-24 23:04:57 +08:00
|
|
|
std::vector<std::uint8_t> data((std::istreambuf_iterator<char>(source)), (std::istreambuf_iterator<char>()));
|
2017-04-12 02:23:54 +08:00
|
|
|
const auto decrypted = decrypt_xlsx(data, password);
|
2016-12-24 23:04:57 +08:00
|
|
|
vector_istreambuf decrypted_buffer(decrypted);
|
|
|
|
std::istream decrypted_stream(&decrypted_buffer);
|
|
|
|
read(decrypted_stream);
|
2016-10-10 19:28:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace xlnt
|