2007-06-06 02:41:20 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2007-2018, Un Shyam & Arvid Norberg
|
2007-06-06 02:41:20 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2018-07-24 08:18:48 +02:00
|
|
|
#if !defined TORRENT_DISABLE_ENCRYPTION
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
#ifndef TORRENT_PE_CRYPTO_HPP_INCLUDED
|
|
|
|
#define TORRENT_PE_CRYPTO_HPP_INCLUDED
|
|
|
|
|
2009-12-02 18:46:25 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
2015-12-14 01:44:42 +01:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2016-05-30 20:33:24 +02:00
|
|
|
#include <boost/multiprecision/cpp_int.hpp>
|
2015-12-14 01:44:42 +01:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
#include "libtorrent/receive_buffer.hpp"
|
2016-05-30 20:33:24 +02:00
|
|
|
#include "libtorrent/sha1_hash.hpp"
|
2014-11-23 07:14:47 +01:00
|
|
|
#include "libtorrent/extensions.hpp"
|
2007-09-01 06:08:39 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2016-07-22 18:31:42 +02:00
|
|
|
#include "libtorrent/span.hpp"
|
2016-07-24 00:54:39 +02:00
|
|
|
#include "libtorrent/buffer.hpp"
|
2017-02-08 05:36:13 +01:00
|
|
|
#include "libtorrent/aux_/array.hpp"
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2016-05-27 18:39:03 +02:00
|
|
|
#include <list>
|
2016-05-30 20:33:24 +02:00
|
|
|
#include <array>
|
|
|
|
#include <cstdint>
|
2016-05-27 18:39:03 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2016-05-30 20:33:24 +02:00
|
|
|
namespace mp = boost::multiprecision;
|
|
|
|
|
2016-07-24 00:54:39 +02:00
|
|
|
using key_t = mp::number<mp::cpp_int_backend<768, 768, mp::unsigned_magnitude, mp::unchecked, void>>;
|
2016-05-30 20:33:24 +02:00
|
|
|
|
2016-08-16 04:50:25 +02:00
|
|
|
TORRENT_EXTRA_EXPORT std::array<char, 96> export_key(key_t const& k);
|
2016-08-06 19:18:48 +02:00
|
|
|
|
2016-05-30 20:33:24 +02:00
|
|
|
// RC4 state from libtomcrypt
|
|
|
|
struct rc4 {
|
2017-09-12 23:10:11 +02:00
|
|
|
int x;
|
|
|
|
int y;
|
2017-02-08 05:36:13 +01:00
|
|
|
aux::array<std::uint8_t, 256> buf;
|
2016-05-30 20:33:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: 3 dh_key_exchange should probably move into its own file
|
2012-03-19 00:31:04 +01:00
|
|
|
class TORRENT_EXTRA_EXPORT dh_key_exchange
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2008-06-28 12:10:05 +02:00
|
|
|
dh_key_exchange();
|
2009-11-08 04:09:19 +01:00
|
|
|
bool good() const { return true; }
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2016-05-30 20:33:24 +02:00
|
|
|
// Get local public key
|
|
|
|
key_t const& get_local_key() const { return m_dh_local_key; }
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
// read remote_pubkey, generate and store shared secret in
|
2009-11-08 04:09:19 +01:00
|
|
|
// m_dh_shared_secret.
|
2016-06-18 20:01:38 +02:00
|
|
|
void compute_secret(std::uint8_t const* remote_pubkey);
|
2016-05-30 20:33:24 +02:00
|
|
|
void compute_secret(key_t const& remote_pubkey);
|
2007-06-06 02:41:20 +02:00
|
|
|
|
2016-05-30 20:33:24 +02:00
|
|
|
key_t const& get_secret() const { return m_dh_shared_secret; }
|
2008-08-21 01:05:12 +02:00
|
|
|
|
|
|
|
sha1_hash const& get_hash_xor_mask() const { return m_xor_mask; }
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
private:
|
|
|
|
|
2016-05-30 20:33:24 +02:00
|
|
|
key_t m_dh_local_key;
|
|
|
|
key_t m_dh_local_secret;
|
|
|
|
key_t m_dh_shared_secret;
|
2008-08-21 01:05:12 +02:00
|
|
|
sha1_hash m_xor_mask;
|
2007-06-06 02:41:20 +02:00
|
|
|
};
|
2009-11-09 08:26:40 +01:00
|
|
|
|
2016-07-28 20:57:26 +02:00
|
|
|
struct TORRENT_EXTRA_EXPORT encryption_handler
|
2011-08-29 04:00:17 +02:00
|
|
|
{
|
2016-07-28 04:31:08 +02:00
|
|
|
std::tuple<int, span<span<char const>>>
|
|
|
|
encrypt(span<span<char>> iovec);
|
2016-06-20 05:05:59 +02:00
|
|
|
|
|
|
|
int decrypt(crypto_receive_buffer& recv_buffer
|
|
|
|
, std::size_t& bytes_transferred);
|
2014-11-23 07:14:47 +01:00
|
|
|
|
2016-08-17 20:30:24 +02:00
|
|
|
bool switch_send_crypto(std::shared_ptr<crypto_plugin> crypto
|
2014-11-23 07:14:47 +01:00
|
|
|
, int pending_encryption);
|
|
|
|
|
2016-08-17 20:30:24 +02:00
|
|
|
void switch_recv_crypto(std::shared_ptr<crypto_plugin> crypto
|
2014-11-23 07:14:47 +01:00
|
|
|
, crypto_receive_buffer& recv_buffer);
|
|
|
|
|
|
|
|
bool is_send_plaintext() const
|
|
|
|
{
|
|
|
|
return m_send_barriers.empty() || m_send_barriers.back().next != INT_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_recv_plaintext() const
|
|
|
|
{
|
2016-06-20 17:32:06 +02:00
|
|
|
return m_dec_handler.get() == nullptr;
|
2014-11-23 07:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct barrier
|
|
|
|
{
|
2016-08-17 20:30:24 +02:00
|
|
|
barrier(std::shared_ptr<crypto_plugin> plugin, int n)
|
2015-08-02 05:57:11 +02:00
|
|
|
: enc_handler(plugin), next(n) {}
|
2016-08-17 20:30:24 +02:00
|
|
|
std::shared_ptr<crypto_plugin> enc_handler;
|
2014-11-23 07:14:47 +01:00
|
|
|
// number of bytes to next barrier
|
|
|
|
int next;
|
|
|
|
};
|
|
|
|
std::list<barrier> m_send_barriers;
|
2016-08-17 20:30:24 +02:00
|
|
|
std::shared_ptr<crypto_plugin> m_dec_handler;
|
2011-08-29 04:00:17 +02:00
|
|
|
};
|
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
struct TORRENT_EXTRA_EXPORT rc4_handler : crypto_plugin
|
2007-06-06 02:41:20 +02:00
|
|
|
{
|
|
|
|
public:
|
2015-05-05 04:32:14 +02:00
|
|
|
rc4_handler();
|
2011-06-18 18:58:36 +02:00
|
|
|
|
2015-05-05 04:32:14 +02:00
|
|
|
// Input keys must be 20 bytes
|
2016-08-16 04:50:25 +02:00
|
|
|
void set_incoming_key(span<char const> key) override;
|
|
|
|
void set_outgoing_key(span<char const> key) override;
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2016-07-28 04:31:08 +02:00
|
|
|
std::tuple<int, span<span<char const>>>
|
|
|
|
encrypt(span<span<char>> buf) override;
|
2016-06-20 05:05:59 +02:00
|
|
|
|
2016-08-16 16:21:17 +02:00
|
|
|
std::tuple<int, int, int> decrypt(span<span<char>> buf) override;
|
2007-06-06 02:41:20 +02:00
|
|
|
|
|
|
|
private:
|
2009-11-09 08:26:40 +01:00
|
|
|
rc4 m_rc4_incoming;
|
|
|
|
rc4 m_rc4_outgoing;
|
2014-12-29 17:50:22 +01:00
|
|
|
|
2011-06-18 18:58:36 +02:00
|
|
|
// determines whether or not encryption and decryption is enabled
|
|
|
|
bool m_encrypt;
|
|
|
|
bool m_decrypt;
|
2007-06-06 02:41:20 +02:00
|
|
|
};
|
2008-12-27 17:44:20 +01:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
} // namespace libtorrent
|
|
|
|
|
|
|
|
#endif // TORRENT_PE_CRYPTO_HPP_INCLUDED
|
|
|
|
#endif // TORRENT_DISABLE_ENCRYPTION
|