2020-10-23 04:06:23 +03:00
|
|
|
/*
|
2021-06-08 22:36:27 +03:00
|
|
|
* Copyright (c) 2013-2021, The PurpleI2P Project
|
2020-10-23 04:06:23 +03:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <openssl/rand.h>
|
2020-10-23 22:53:22 +03:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include "Log.h"
|
2020-10-23 04:06:23 +03:00
|
|
|
#include "Transports.h"
|
|
|
|
#include "Timestamp.h"
|
|
|
|
#include "I2PEndian.h"
|
|
|
|
#include "I2NPProtocol.h"
|
|
|
|
#include "TunnelConfig.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace tunnel
|
|
|
|
{
|
|
|
|
TunnelHopConfig::TunnelHopConfig (std::shared_ptr<const i2p::data::IdentityEx> r)
|
|
|
|
{
|
|
|
|
RAND_bytes (layerKey, 32);
|
|
|
|
RAND_bytes (ivKey, 32);
|
|
|
|
RAND_bytes (replyKey, 32);
|
|
|
|
RAND_bytes (replyIV, 16);
|
|
|
|
RAND_bytes ((uint8_t *)&tunnelID, 4);
|
|
|
|
if (!tunnelID) tunnelID = 1; // tunnelID can't be zero
|
|
|
|
isGateway = true;
|
|
|
|
isEndpoint = true;
|
|
|
|
ident = r;
|
|
|
|
//nextRouter = nullptr;
|
|
|
|
nextTunnelID = 0;
|
|
|
|
|
|
|
|
next = nullptr;
|
|
|
|
prev = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TunnelHopConfig::SetNextIdent (const i2p::data::IdentHash& ident)
|
|
|
|
{
|
|
|
|
nextIdent = ident;
|
|
|
|
isEndpoint = false;
|
|
|
|
RAND_bytes ((uint8_t *)&nextTunnelID, 4);
|
|
|
|
if (!nextTunnelID) nextTunnelID = 1; // tunnelID can't be zero
|
|
|
|
}
|
|
|
|
|
|
|
|
void TunnelHopConfig::SetReplyHop (uint32_t replyTunnelID, const i2p::data::IdentHash& replyIdent)
|
|
|
|
{
|
|
|
|
nextIdent = replyIdent;
|
|
|
|
nextTunnelID = replyTunnelID;
|
|
|
|
isEndpoint = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TunnelHopConfig::SetNext (TunnelHopConfig * n)
|
|
|
|
{
|
|
|
|
next = n;
|
|
|
|
if (next)
|
|
|
|
{
|
|
|
|
next->prev = this;
|
|
|
|
next->isGateway = false;
|
|
|
|
isEndpoint = false;
|
|
|
|
nextIdent = next->ident->GetIdentHash ();
|
|
|
|
nextTunnelID = next->tunnelID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TunnelHopConfig::SetPrev (TunnelHopConfig * p)
|
|
|
|
{
|
|
|
|
prev = p;
|
|
|
|
if (prev)
|
|
|
|
{
|
|
|
|
prev->next = this;
|
|
|
|
prev->isEndpoint = false;
|
|
|
|
isGateway = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 22:41:33 +03:00
|
|
|
void ElGamalTunnelHopConfig::CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID, BN_CTX * ctx)
|
2020-10-23 04:06:23 +03:00
|
|
|
{
|
2021-07-02 22:41:33 +03:00
|
|
|
// fill clear text
|
2020-10-23 04:06:23 +03:00
|
|
|
uint8_t flag = 0;
|
2021-06-08 22:36:27 +03:00
|
|
|
if (isGateway) flag |= TUNNEL_BUILD_RECORD_GATEWAY_FLAG;
|
|
|
|
if (isEndpoint) flag |= TUNNEL_BUILD_RECORD_ENDPOINT_FLAG;
|
2021-07-02 22:41:33 +03:00
|
|
|
uint8_t clearText[BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
|
|
|
|
htobe32buf (clearText + BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID);
|
|
|
|
memcpy (clearText + BUILD_REQUEST_RECORD_OUR_IDENT_OFFSET, ident->GetIdentHash (), 32);
|
|
|
|
htobe32buf (clearText + BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET, nextTunnelID);
|
|
|
|
memcpy (clearText + BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET, nextIdent, 32);
|
|
|
|
memcpy (clearText + BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET, layerKey, 32);
|
|
|
|
memcpy (clearText + BUILD_REQUEST_RECORD_IV_KEY_OFFSET, ivKey, 32);
|
|
|
|
memcpy (clearText + BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET, replyKey, 32);
|
|
|
|
memcpy (clearText + BUILD_REQUEST_RECORD_REPLY_IV_OFFSET, replyIV, 16);
|
|
|
|
clearText[BUILD_REQUEST_RECORD_FLAG_OFFSET] = flag;
|
|
|
|
htobe32buf (clearText + BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET, i2p::util::GetHoursSinceEpoch ());
|
|
|
|
htobe32buf (clearText + BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET, replyMsgID);
|
|
|
|
RAND_bytes (clearText + BUILD_REQUEST_RECORD_PADDING_OFFSET, BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE - BUILD_REQUEST_RECORD_PADDING_OFFSET);
|
|
|
|
// encrypt
|
2020-10-23 04:06:23 +03:00
|
|
|
auto encryptor = ident->CreateEncryptor (nullptr);
|
2021-07-02 22:41:33 +03:00
|
|
|
if (encryptor)
|
|
|
|
encryptor->Encrypt (clearText, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET, ctx, false);
|
2020-10-23 04:06:23 +03:00
|
|
|
memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16);
|
2021-07-02 22:41:33 +03:00
|
|
|
}
|
2020-10-23 04:06:23 +03:00
|
|
|
|
2021-07-02 22:41:33 +03:00
|
|
|
bool ElGamalTunnelHopConfig::DecryptBuildResponseRecord (const uint8_t * encrypted, uint8_t * clearText)
|
|
|
|
{
|
|
|
|
i2p::crypto::CBCDecryption decryption;
|
|
|
|
decryption.SetKey (replyKey);
|
|
|
|
decryption.SetIV (replyIV);
|
|
|
|
decryption.Decrypt (encrypted, TUNNEL_BUILD_RECORD_SIZE, clearText);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-03 05:06:24 +03:00
|
|
|
void ECIESTunnelHopConfig::EncryptECIES (const uint8_t * plainText, size_t len, uint8_t * encrypted)
|
2020-10-23 04:06:23 +03:00
|
|
|
{
|
2021-07-03 05:06:24 +03:00
|
|
|
auto encryptor = ident->CreateEncryptor (nullptr);
|
|
|
|
if (!encryptor) return;
|
2020-10-23 22:53:22 +03:00
|
|
|
uint8_t hepk[32];
|
|
|
|
encryptor->Encrypt (nullptr, hepk, nullptr, false);
|
2020-12-04 01:58:37 +03:00
|
|
|
i2p::crypto::InitNoiseNState (*this, hepk);
|
2020-10-23 22:53:22 +03:00
|
|
|
auto ephemeralKeys = i2p::transport::transports.GetNextX25519KeysPair ();
|
2020-10-24 22:48:56 +03:00
|
|
|
memcpy (encrypted, ephemeralKeys->GetPublicKey (), 32);
|
|
|
|
MixHash (encrypted, 32); // h = SHA256(h || sepk)
|
|
|
|
encrypted += 32;
|
2020-10-23 22:53:22 +03:00
|
|
|
uint8_t sharedSecret[32];
|
|
|
|
ephemeralKeys->Agree (hepk, sharedSecret); // x25519(sesk, hepk)
|
2020-10-29 04:53:11 +03:00
|
|
|
MixKey (sharedSecret);
|
2020-10-23 22:53:22 +03:00
|
|
|
uint8_t nonce[12];
|
|
|
|
memset (nonce, 0, 12);
|
2021-07-03 05:06:24 +03:00
|
|
|
if (!i2p::crypto::AEADChaCha20Poly1305 (plainText, len, m_H, 32, m_CK + 32, nonce, encrypted, len + 16, true)) // encrypt
|
2020-10-23 22:53:22 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "Tunnel: Plaintext AEAD encryption failed");
|
|
|
|
return;
|
|
|
|
}
|
2021-07-03 05:06:24 +03:00
|
|
|
MixHash (encrypted, len + 16); // h = SHA256(h || ciphertext)
|
|
|
|
}
|
|
|
|
|
2021-07-05 21:31:07 +03:00
|
|
|
bool ECIESTunnelHopConfig::DecryptECIES (const uint8_t * key, const uint8_t * encrypted, size_t len, uint8_t * clearText)
|
2021-07-03 05:06:24 +03:00
|
|
|
{
|
|
|
|
uint8_t nonce[12];
|
|
|
|
memset (nonce, 0, 12);
|
2021-07-05 21:31:07 +03:00
|
|
|
return i2p::crypto::AEADChaCha20Poly1305 (encrypted, len - 16, m_H, 32, key, nonce, clearText, len - 16, false); // decrypt
|
2020-10-23 04:06:23 +03:00
|
|
|
}
|
2021-07-02 22:41:33 +03:00
|
|
|
|
|
|
|
void LongECIESTunnelHopConfig::CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID, BN_CTX * ctx)
|
|
|
|
{
|
|
|
|
// fill clear text
|
|
|
|
uint8_t flag = 0;
|
|
|
|
if (isGateway) flag |= TUNNEL_BUILD_RECORD_GATEWAY_FLAG;
|
|
|
|
if (isEndpoint) flag |= TUNNEL_BUILD_RECORD_ENDPOINT_FLAG;
|
|
|
|
uint8_t clearText[ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE];
|
|
|
|
htobe32buf (clearText + ECIES_BUILD_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID);
|
|
|
|
htobe32buf (clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_TUNNEL_OFFSET, nextTunnelID);
|
|
|
|
memcpy (clearText + ECIES_BUILD_REQUEST_RECORD_NEXT_IDENT_OFFSET, nextIdent, 32);
|
|
|
|
memcpy (clearText + ECIES_BUILD_REQUEST_RECORD_LAYER_KEY_OFFSET, layerKey, 32);
|
|
|
|
memcpy (clearText + ECIES_BUILD_REQUEST_RECORD_IV_KEY_OFFSET, ivKey, 32);
|
|
|
|
memcpy (clearText + ECIES_BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET, replyKey, 32);
|
|
|
|
memcpy (clearText + ECIES_BUILD_REQUEST_RECORD_REPLY_IV_OFFSET, replyIV, 16);
|
|
|
|
clearText[ECIES_BUILD_REQUEST_RECORD_FLAG_OFFSET] = flag;
|
|
|
|
memset (clearText + ECIES_BUILD_REQUEST_RECORD_MORE_FLAGS_OFFSET, 0, 3); // set to 0 for compatibility
|
|
|
|
htobe32buf (clearText + ECIES_BUILD_REQUEST_RECORD_REQUEST_TIME_OFFSET, i2p::util::GetMinutesSinceEpoch ());
|
|
|
|
htobe32buf (clearText + ECIES_BUILD_REQUEST_RECORD_REQUEST_EXPIRATION_OFFSET, 600); // +10 minutes
|
|
|
|
htobe32buf (clearText + ECIES_BUILD_REQUEST_RECORD_SEND_MSG_ID_OFFSET, replyMsgID);
|
|
|
|
memset (clearText + ECIES_BUILD_REQUEST_RECORD_PADDING_OFFSET, 0, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE - ECIES_BUILD_REQUEST_RECORD_PADDING_OFFSET);
|
|
|
|
// encrypt
|
2021-07-03 05:06:24 +03:00
|
|
|
EncryptECIES (clearText, ECIES_BUILD_REQUEST_RECORD_CLEAR_TEXT_SIZE, record + BUILD_REQUEST_RECORD_ENCRYPTED_OFFSET);
|
2021-07-02 22:41:33 +03:00
|
|
|
memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16);
|
|
|
|
}
|
2021-07-02 20:20:24 +03:00
|
|
|
|
2021-07-02 22:41:33 +03:00
|
|
|
bool LongECIESTunnelHopConfig::DecryptBuildResponseRecord (const uint8_t * encrypted, uint8_t * clearText)
|
2021-07-02 20:20:24 +03:00
|
|
|
{
|
2021-07-05 21:31:07 +03:00
|
|
|
if (!DecryptECIES (m_CK, encrypted, TUNNEL_BUILD_RECORD_SIZE, clearText))
|
2021-07-03 05:06:24 +03:00
|
|
|
{
|
|
|
|
LogPrint (eLogWarning, "Tunnel: Response AEAD decryption failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShortECIESTunnelHopConfig::CreateBuildRequestRecord (uint8_t * record, uint32_t replyMsgID, BN_CTX * ctx)
|
|
|
|
{
|
|
|
|
// fill clear text
|
|
|
|
uint8_t flag = 0;
|
|
|
|
if (isGateway) flag |= TUNNEL_BUILD_RECORD_GATEWAY_FLAG;
|
|
|
|
if (isEndpoint) flag |= TUNNEL_BUILD_RECORD_ENDPOINT_FLAG;
|
|
|
|
uint8_t clearText[SHORT_REQUEST_RECORD_CLEAR_TEXT_SIZE ];
|
|
|
|
htobe32buf (clearText + SHORT_REQUEST_RECORD_RECEIVE_TUNNEL_OFFSET, tunnelID);
|
|
|
|
htobe32buf (clearText + SHORT_REQUEST_RECORD_NEXT_TUNNEL_OFFSET, nextTunnelID);
|
|
|
|
memcpy (clearText + SHORT_REQUEST_RECORD_NEXT_IDENT_OFFSET, nextIdent, 32);
|
|
|
|
clearText[SHORT_REQUEST_RECORD_FLAG_OFFSET] = flag;
|
|
|
|
memset (clearText + SHORT_REQUEST_RECORD_MORE_FLAGS_OFFSET, 0, 2);
|
|
|
|
clearText[SHORT_REQUEST_RECORD_LAYER_ENCRYPTION_TYPE] = 0; // AES
|
|
|
|
htobe32buf (clearText + SHORT_REQUEST_RECORD_REQUEST_TIME_OFFSET, i2p::util::GetMinutesSinceEpoch ());
|
|
|
|
htobe32buf (clearText + SHORT_REQUEST_RECORD_REQUEST_EXPIRATION_OFFSET , 600); // +10 minutes
|
|
|
|
htobe32buf (clearText + SHORT_REQUEST_RECORD_SEND_MSG_ID_OFFSET, replyMsgID);
|
|
|
|
memset (clearText + SHORT_REQUEST_RECORD_PADDING_OFFSET, 0, SHORT_REQUEST_RECORD_CLEAR_TEXT_SIZE - SHORT_REQUEST_RECORD_PADDING_OFFSET);
|
|
|
|
// encrypt
|
|
|
|
EncryptECIES (clearText, SHORT_REQUEST_RECORD_CLEAR_TEXT_SIZE, record + SHORT_REQUEST_RECORD_ENCRYPTED_OFFSET);
|
2021-07-05 21:31:07 +03:00
|
|
|
// derive reply and layer key
|
|
|
|
i2p::crypto::HKDF (m_CK, nullptr, 0, "SMTunnelReplyKey", m_CK);
|
|
|
|
memcpy (replyKey, m_CK + 32, 32);
|
|
|
|
i2p::crypto::HKDF (m_CK, nullptr, 0, "SMTunnelLayerKey", m_CK);
|
|
|
|
memcpy (layerKey, m_CK + 32, 32);
|
|
|
|
if (isEndpoint)
|
|
|
|
{
|
|
|
|
i2p::crypto::HKDF (m_CK, nullptr, 0, "TunnelLayerIVKey", m_CK);
|
|
|
|
memcpy (ivKey, m_CK + 32, 32);
|
|
|
|
i2p::crypto::HKDF (m_CK, nullptr, 0, "RGarlicKeyAndTag", m_CK); // OTBRM garlic key m_CK + 32, tag first 8 bytes of m_CK
|
|
|
|
}
|
|
|
|
else
|
|
|
|
memcpy (ivKey, m_CK, 32); // last HKDF
|
2021-07-03 05:06:24 +03:00
|
|
|
memcpy (record + BUILD_REQUEST_RECORD_TO_PEER_OFFSET, (const uint8_t *)ident->GetIdentHash (), 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShortECIESTunnelHopConfig::DecryptBuildResponseRecord (const uint8_t * encrypted, uint8_t * clearText)
|
|
|
|
{
|
2021-07-05 21:31:07 +03:00
|
|
|
if (!DecryptECIES (replyKey, encrypted, SHORT_TUNNEL_BUILD_RECORD_SIZE, clearText))
|
2021-07-02 20:20:24 +03:00
|
|
|
{
|
2021-07-02 22:41:33 +03:00
|
|
|
LogPrint (eLogWarning, "Tunnel: Response AEAD decryption failed");
|
|
|
|
return false;
|
2021-07-02 20:20:24 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-23 04:06:23 +03:00
|
|
|
}
|
|
|
|
}
|