2006-03-21 14:12:51 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-03-21 14:12:51 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-09-21 15:31:24 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-03-21 14:12:51 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "PeerConnection.h"
|
|
|
|
#include "message.h"
|
|
|
|
#include "DlAbortEx.h"
|
2006-04-17 16:17:20 +00:00
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2006-12-24 06:25:21 +00:00
|
|
|
#include "BtHandshakeMessage.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Socket.h"
|
2007-07-23 13:04:48 +00:00
|
|
|
#include "a2netcompat.h"
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
#include "ARC4Encryptor.h"
|
|
|
|
#include "ARC4Decryptor.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include <cstring>
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <algorithm>
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
PeerConnection::PeerConnection(int32_t cuid,
|
2006-07-19 17:07:45 +00:00
|
|
|
const SocketHandle& socket,
|
2006-05-18 17:08:29 +00:00
|
|
|
const Option* op)
|
|
|
|
:cuid(cuid),
|
|
|
|
socket(socket),
|
|
|
|
option(op),
|
2007-01-28 14:18:35 +00:00
|
|
|
logger(LogFactory::getInstance()),
|
2006-05-18 17:08:29 +00:00
|
|
|
resbufLength(0),
|
|
|
|
currentPayloadLength(0),
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
lenbufLength(0),
|
2008-04-20 00:50:22 +00:00
|
|
|
_encryptionEnabled(false)
|
2008-02-08 15:53:45 +00:00
|
|
|
{}
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
PeerConnection::~PeerConnection() {}
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
ssize_t PeerConnection::sendMessage(const unsigned char* data, size_t dataLength) {
|
2006-03-21 14:12:51 +00:00
|
|
|
if(socket->isWritable(0)) {
|
2006-12-24 06:25:21 +00:00
|
|
|
// TODO fix this
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
sendData(data, dataLength, _encryptionEnabled);
|
2008-03-09 12:24:01 +00:00
|
|
|
return dataLength;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
bool PeerConnection::receiveMessage(unsigned char* data, size_t& dataLength) {
|
2008-01-10 15:12:32 +00:00
|
|
|
if(resbufLength == 0 && 4 > lenbufLength) {
|
2007-12-12 13:49:09 +00:00
|
|
|
if(!socket->isReadable(0)) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-01-10 15:12:32 +00:00
|
|
|
// read payload size, 32bit unsigned integer
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t remaining = 4-lenbufLength;
|
|
|
|
size_t temp = remaining;
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
readData(lenbuf+lenbufLength, remaining, _encryptionEnabled);
|
2008-01-10 15:12:32 +00:00
|
|
|
if(remaining == 0) {
|
2006-03-21 14:12:51 +00:00
|
|
|
// we got EOF
|
2008-03-09 12:24:01 +00:00
|
|
|
logger->debug("CUID#%d - In PeerConnection::receiveMessage(), remain=%zu",
|
2008-01-10 15:12:32 +00:00
|
|
|
cuid, temp);
|
2006-03-21 14:12:51 +00:00
|
|
|
throw new DlAbortEx(EX_EOF_FROM_PEER);
|
|
|
|
}
|
2008-01-10 15:12:32 +00:00
|
|
|
lenbufLength += remaining;
|
|
|
|
if(4 > lenbufLength) {
|
|
|
|
// still 4-lenbufLength bytes to go
|
2006-05-09 15:54:14 +00:00
|
|
|
return false;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2008-01-10 15:12:32 +00:00
|
|
|
uint32_t payloadLength = ntohl(*(reinterpret_cast<uint32_t*>(lenbuf)));
|
|
|
|
if(payloadLength > MAX_PAYLOAD_LEN) {
|
|
|
|
throw new DlAbortEx(EX_TOO_LONG_PAYLOAD, payloadLength);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
currentPayloadLength = payloadLength;
|
|
|
|
}
|
2007-01-11 16:32:31 +00:00
|
|
|
if(!socket->isReadable(0)) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
// we have currentPayloadLen-resbufLen bytes to read
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t remaining = currentPayloadLength-resbufLength;
|
|
|
|
size_t temp = remaining;
|
2006-03-21 14:12:51 +00:00
|
|
|
if(remaining > 0) {
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
readData(resbuf+resbufLength, remaining, _encryptionEnabled);
|
2006-03-21 14:12:51 +00:00
|
|
|
if(remaining == 0) {
|
|
|
|
// we got EOF
|
2008-03-09 12:24:01 +00:00
|
|
|
logger->debug("CUID#%d - In PeerConnection::receiveMessage(), payloadlen=%zu, remaining=%zu",
|
2008-01-10 15:12:32 +00:00
|
|
|
cuid, currentPayloadLength, temp);
|
2006-03-21 14:12:51 +00:00
|
|
|
throw new DlAbortEx(EX_EOF_FROM_PEER);
|
|
|
|
}
|
|
|
|
resbufLength += remaining;
|
2008-01-10 15:12:32 +00:00
|
|
|
if(currentPayloadLength > resbufLength) {
|
2006-05-09 15:54:14 +00:00
|
|
|
return false;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// we got whole payload.
|
|
|
|
resbufLength = 0;
|
|
|
|
lenbufLength = 0;
|
2006-05-09 15:54:14 +00:00
|
|
|
|
2006-12-24 06:25:21 +00:00
|
|
|
memcpy(data, resbuf, currentPayloadLength);
|
|
|
|
dataLength = currentPayloadLength;
|
2006-05-09 15:54:14 +00:00
|
|
|
return true;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
bool PeerConnection::receiveHandshake(unsigned char* data, size_t& dataLength,
|
2007-10-11 16:58:24 +00:00
|
|
|
bool peek) {
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t remaining = BtHandshakeMessage::MESSAGE_LENGTH-resbufLength;
|
2008-01-10 15:12:32 +00:00
|
|
|
if(remaining > 0 && !socket->isReadable(0)) {
|
2006-12-24 06:25:21 +00:00
|
|
|
dataLength = 0;
|
2006-05-09 15:54:14 +00:00
|
|
|
return false;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2008-01-10 15:12:32 +00:00
|
|
|
bool retval = true;
|
|
|
|
if(remaining > 0) {
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t temp = remaining;
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
readData(resbuf+resbufLength, remaining, _encryptionEnabled);
|
2008-01-10 15:12:32 +00:00
|
|
|
if(remaining == 0) {
|
2007-10-11 16:58:24 +00:00
|
|
|
// we got EOF
|
2008-03-09 12:24:01 +00:00
|
|
|
logger->debug("CUID#%d - In PeerConnection::receiveHandshake(), remain=%zu",
|
2008-01-10 15:12:32 +00:00
|
|
|
cuid, temp);
|
2007-10-11 16:58:24 +00:00
|
|
|
throw new DlAbortEx(EX_EOF_FROM_PEER);
|
|
|
|
}
|
2008-01-10 15:12:32 +00:00
|
|
|
resbufLength += remaining;
|
|
|
|
if(BtHandshakeMessage::MESSAGE_LENGTH > resbufLength) {
|
|
|
|
retval = false;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t writeLength = std::min(resbufLength, dataLength);
|
2006-12-24 06:25:21 +00:00
|
|
|
memcpy(data, resbuf, writeLength);
|
|
|
|
dataLength = writeLength;
|
2007-10-11 16:58:24 +00:00
|
|
|
if(retval && !peek) {
|
2006-06-22 15:26:18 +00:00
|
|
|
resbufLength = 0;
|
|
|
|
}
|
|
|
|
return retval;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
void PeerConnection::readData(unsigned char* data, size_t& length, bool encryption)
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
{
|
|
|
|
if(encryption) {
|
|
|
|
unsigned char temp[MAX_PAYLOAD_LEN];
|
|
|
|
assert(MAX_PAYLOAD_LEN >= length);
|
|
|
|
socket->readData(temp, length);
|
2008-03-07 12:05:50 +00:00
|
|
|
_decryptor->decrypt(data, length, temp, length);
|
2008-02-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added Message Stream Encryption(MSE) support.
Currently, aria2 accepts incoming connections with Obfuscation Header
and legacy BitTorrent Header and establishes connections with
Obfuscation Header first and if failed then retry with legacy
BitTorrent header. If plain text and ARC4 is provided, aria2 always
choose ARC4. The new option to change the default behavior is planned.
For tracker extension, "supportcrypto=1" is added statically.
* src/PeerInitiateConnectionCommand.{h, cc}
* src/PeerConnection.{h, cc}
* src/HandleRegistry.h
* src/SocketCore.h
* src/PeerReceiveHandshakeCommand.{h, cc}
* src/BtRegistry.{h, cc}
* src/PeerListenCommand.cc
* src/InitiatorMSEHandshakeCommand.{h, cc}
* src/ReceiverMSEHandshakeCommand.{h, cc}
* src/MSEHandshake.{h, cc}
* src/ARC4Encryptor.h
* src/ARC4Decryptor.h
* src/LibgcryptARC4Encryptor.h
* src/LibgcryptARC4Decryptor.h
* src/LibgcryptARC4Context.h
* src/LibsslARC4Encryptor.h
* src/LibsslARC4Decryptor.h
* src/LibsslARC4Context.h
* src/DHKeyExchange.h
* src/LibgcryptDHKeyExchange.h
* src/LibsslDHKeyExchange.h
* src/DefaultBtAnnounce.cc: Just added "supportcrypto=1" parameter.
* test/DefaultBtAnnounceTest.cc
* test/ARC4Test.cc
* test/DHKeyExchangeTest.cc
Removed prepareForRetry() because it is not used.
* src/PeerAbstractCommand.{h, cc}
* src/PeerInteractionCommand.{h, cc}
* src/PeerInitiateConnectionCommand.{h, cc}
2008-02-17 15:56:47 +00:00
|
|
|
} else {
|
|
|
|
socket->readData(data, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerConnection::sendData(const unsigned char* data, size_t length, bool encryption)
|
|
|
|
{
|
|
|
|
if(encryption) {
|
|
|
|
unsigned char temp[4096];
|
|
|
|
const unsigned char* dptr = data;
|
|
|
|
size_t r = length;
|
|
|
|
while(r > 0) {
|
|
|
|
size_t s = std::min(r, sizeof(temp));
|
|
|
|
_encryptor->encrypt(temp, s, dptr, s);
|
|
|
|
socket->writeData(temp, s);
|
|
|
|
dptr += s;
|
|
|
|
r -= s;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
socket->writeData(data, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerConnection::enableEncryption(const SharedHandle<ARC4Encryptor>& encryptor,
|
|
|
|
const SharedHandle<ARC4Decryptor>& decryptor)
|
|
|
|
{
|
|
|
|
_encryptor = encryptor;
|
|
|
|
_decryptor = decryptor;
|
|
|
|
|
|
|
|
_encryptionEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerConnection::presetBuffer(const unsigned char* data, size_t length)
|
|
|
|
{
|
|
|
|
size_t nwrite = std::min((size_t)MAX_PAYLOAD_LEN, length);
|
|
|
|
memcpy(resbuf, data, nwrite);
|
|
|
|
resbufLength = length;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|