2020-05-22 16:18:41 +03:00
|
|
|
/*
|
2024-01-30 23:41:57 +03:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 16:18:41 +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
|
|
|
|
*/
|
|
|
|
|
2020-01-15 23:13:43 +03:00
|
|
|
#ifndef ECIES_X25519_AEAD_RATCHET_SESSION_H__
|
|
|
|
#define ECIES_X25519_AEAD_RATCHET_SESSION_H__
|
|
|
|
|
2020-01-17 03:33:00 +03:00
|
|
|
#include <string.h>
|
2020-01-15 23:13:43 +03:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <functional>
|
2020-01-21 22:40:23 +03:00
|
|
|
#include <memory>
|
2020-01-17 19:21:41 +03:00
|
|
|
#include <vector>
|
2020-03-27 02:03:38 +03:00
|
|
|
#include <list>
|
2020-04-17 04:30:18 +03:00
|
|
|
#include <unordered_map>
|
2020-01-15 23:13:43 +03:00
|
|
|
#include "Identity.h"
|
2020-01-17 00:34:13 +03:00
|
|
|
#include "Crypto.h"
|
2020-01-16 22:59:19 +03:00
|
|
|
#include "Garlic.h"
|
2020-04-17 04:30:18 +03:00
|
|
|
#include "Tag.h"
|
2020-01-15 23:13:43 +03:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace garlic
|
|
|
|
{
|
2020-05-23 22:58:11 +03:00
|
|
|
const int ECIESX25519_RESTART_TIMEOUT = 120; // number of second since session creation we can restart session after
|
2020-07-15 23:20:35 +03:00
|
|
|
const int ECIESX25519_INACTIVITY_TIMEOUT = 90; // number of seconds we receive nothing and should restart if we can
|
2021-11-27 23:30:35 +03:00
|
|
|
const int ECIESX25519_SEND_INACTIVITY_TIMEOUT = 5000; // number of milliseconds we can send empty(pyaload only) packet after
|
2021-01-10 02:59:09 +03:00
|
|
|
const int ECIESX25519_SEND_EXPIRATION_TIMEOUT = 480; // in seconds
|
|
|
|
const int ECIESX25519_RECEIVE_EXPIRATION_TIMEOUT = 600; // in seconds
|
2020-05-01 04:27:35 +03:00
|
|
|
const int ECIESX25519_PREVIOUS_TAGSET_EXPIRATION_TIMEOUT = 180; // 180
|
2020-11-06 05:21:46 +03:00
|
|
|
const int ECIESX25519_TAGSET_MAX_NUM_TAGS = 8192; // number of tags we request new tagset after
|
2020-05-01 04:27:35 +03:00
|
|
|
const int ECIESX25519_MIN_NUM_GENERATED_TAGS = 24;
|
2020-12-24 22:06:34 +03:00
|
|
|
const int ECIESX25519_MAX_NUM_GENERATED_TAGS = 320;
|
2020-05-01 04:27:35 +03:00
|
|
|
const int ECIESX25519_NSR_NUM_GENERATED_TAGS = 12;
|
2020-03-01 13:25:50 +03:00
|
|
|
|
|
|
|
const size_t ECIESX25519_OPTIMAL_PAYLOAD_SIZE = 1912; // 1912 = 1956 /* to fit 2 tunnel messages */
|
2020-05-18 23:42:06 +03:00
|
|
|
// - 16 /* I2NP header */ - 16 /* poly hash */ - 8 /* tag */ - 4 /* garlic length */
|
2020-03-01 13:25:50 +03:00
|
|
|
|
2021-01-05 02:20:16 +03:00
|
|
|
class RatchetTagSet
|
2020-03-01 13:25:50 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2021-01-05 02:20:16 +03:00
|
|
|
RatchetTagSet () {};
|
|
|
|
virtual ~RatchetTagSet () {};
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
void DHInitialize (const uint8_t * rootKey, const uint8_t * k);
|
|
|
|
void NextSessionTagRatchet ();
|
|
|
|
uint64_t GetNextSessionTag ();
|
2020-04-25 04:36:08 +03:00
|
|
|
const uint8_t * GetNextRootKey () const { return m_NextRootKey; };
|
2020-03-01 13:25:50 +03:00
|
|
|
int GetNextIndex () const { return m_NextIndex; };
|
2020-03-08 02:46:40 +03:00
|
|
|
void GetSymmKey (int index, uint8_t * key);
|
2020-09-08 14:46:55 +03:00
|
|
|
void DeleteSymmKey (int index);
|
2020-04-24 22:46:02 +03:00
|
|
|
|
2020-04-25 04:36:08 +03:00
|
|
|
int GetTagSetID () const { return m_TagSetID; };
|
|
|
|
void SetTagSetID (int tagsetID) { m_TagSetID = tagsetID; };
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2020-02-09 05:51:02 +03:00
|
|
|
private:
|
2020-03-01 13:25:50 +03:00
|
|
|
|
2021-08-02 01:42:13 +03:00
|
|
|
i2p::data::Tag<64> m_SessionTagKeyData;
|
2020-03-01 13:25:50 +03:00
|
|
|
uint8_t m_SessTagConstant[32], m_SymmKeyCK[32], m_CurrentSymmKeyCK[64], m_NextRootKey[32];
|
2021-01-05 02:20:16 +03:00
|
|
|
int m_NextIndex, m_NextSymmKeyIndex;
|
2020-03-01 13:25:50 +03:00
|
|
|
std::unordered_map<int, i2p::data::Tag<32> > m_ItermediateSymmKeys;
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
int m_TagSetID = 0;
|
|
|
|
};
|
|
|
|
|
2021-01-05 02:20:16 +03:00
|
|
|
class ECIESX25519AEADRatchetSession;
|
|
|
|
class ReceiveRatchetTagSet: public RatchetTagSet,
|
|
|
|
public std::enable_shared_from_this<ReceiveRatchetTagSet>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2021-11-27 23:30:35 +03:00
|
|
|
ReceiveRatchetTagSet (std::shared_ptr<ECIESX25519AEADRatchetSession> session, bool isNS = false):
|
2021-01-05 04:15:48 +03:00
|
|
|
m_Session (session), m_IsNS (isNS) {};
|
2021-01-05 02:20:16 +03:00
|
|
|
|
2021-01-05 04:15:48 +03:00
|
|
|
bool IsNS () const { return m_IsNS; };
|
|
|
|
std::shared_ptr<ECIESX25519AEADRatchetSession> GetSession () { return m_Session; };
|
2021-11-27 23:30:35 +03:00
|
|
|
void SetTrimBehind (int index) { if (index > m_TrimBehindIndex) m_TrimBehindIndex = index; };
|
2021-05-11 03:20:25 +03:00
|
|
|
int GetTrimBehind () const { return m_TrimBehindIndex; };
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2021-01-05 23:56:48 +03:00
|
|
|
void Expire ();
|
2021-11-27 23:30:35 +03:00
|
|
|
bool IsExpired (uint64_t ts) const;
|
|
|
|
|
2021-01-05 04:15:48 +03:00
|
|
|
virtual bool IsIndexExpired (int index) const;
|
2021-01-05 02:20:16 +03:00
|
|
|
virtual bool HandleNextMessage (uint8_t * buf, size_t len, int index);
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2021-01-05 02:20:16 +03:00
|
|
|
private:
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2021-01-05 02:20:16 +03:00
|
|
|
int m_TrimBehindIndex = 0;
|
2021-01-05 04:15:48 +03:00
|
|
|
std::shared_ptr<ECIESX25519AEADRatchetSession> m_Session;
|
|
|
|
bool m_IsNS;
|
2021-01-05 23:56:48 +03:00
|
|
|
uint64_t m_ExpirationTimestamp = 0;
|
2021-11-27 23:30:35 +03:00
|
|
|
};
|
2020-09-16 02:39:18 +03:00
|
|
|
|
2021-05-23 01:41:25 +03:00
|
|
|
class SymmetricKeyTagSet: public ReceiveRatchetTagSet
|
2020-09-16 02:39:18 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2021-05-23 01:41:25 +03:00
|
|
|
SymmetricKeyTagSet (GarlicDestination * destination, const uint8_t * key);
|
2020-09-16 02:39:18 +03:00
|
|
|
|
|
|
|
bool IsIndexExpired (int index) const { return false; };
|
|
|
|
bool HandleNextMessage (uint8_t * buf, size_t len, int index);
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2020-09-16 02:39:18 +03:00
|
|
|
private:
|
|
|
|
|
|
|
|
GarlicDestination * m_Destination;
|
|
|
|
uint8_t m_Key[32];
|
2021-11-27 23:30:35 +03:00
|
|
|
};
|
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
enum ECIESx25519BlockType
|
2020-01-15 23:13:43 +03:00
|
|
|
{
|
2020-03-01 13:25:50 +03:00
|
|
|
eECIESx25519BlkDateTime = 0,
|
|
|
|
eECIESx25519BlkSessionID = 1,
|
2020-01-15 23:13:43 +03:00
|
|
|
eECIESx25519BlkTermination = 4,
|
2020-03-01 13:25:50 +03:00
|
|
|
eECIESx25519BlkOptions = 5,
|
|
|
|
eECIESx25519BlkNextKey = 7,
|
|
|
|
eECIESx25519BlkAck = 8,
|
|
|
|
eECIESx25519BlkAckRequest = 9,
|
|
|
|
eECIESx25519BlkGalicClove = 11,
|
|
|
|
eECIESx25519BlkPadding = 254
|
|
|
|
};
|
2020-01-15 23:13:43 +03:00
|
|
|
|
2020-04-25 04:36:08 +03:00
|
|
|
const uint8_t ECIESX25519_NEXT_KEY_KEY_PRESENT_FLAG = 0x01;
|
|
|
|
const uint8_t ECIESX25519_NEXT_KEY_REVERSE_KEY_FLAG = 0x02;
|
|
|
|
const uint8_t ECIESX25519_NEXT_KEY_REQUEST_REVERSE_KEY_FLAG = 0x04;
|
2020-03-01 13:25:50 +03:00
|
|
|
|
2021-11-27 23:30:35 +03:00
|
|
|
class ECIESX25519AEADRatchetSession: public GarlicRoutingSession,
|
2020-11-06 05:21:46 +03:00
|
|
|
private i2p::crypto::NoiseSymmetricState,
|
|
|
|
public std::enable_shared_from_this<ECIESX25519AEADRatchetSession>
|
2020-03-01 13:25:50 +03:00
|
|
|
{
|
|
|
|
enum SessionState
|
|
|
|
{
|
|
|
|
eSessionStateNew = 0,
|
|
|
|
eSessionStateNewSessionReceived,
|
2020-02-04 00:21:07 +03:00
|
|
|
eSessionStateNewSessionSent,
|
2020-04-03 04:48:39 +03:00
|
|
|
eSessionStateNewSessionReplySent,
|
2020-11-06 05:21:46 +03:00
|
|
|
eSessionStateEstablished,
|
2021-07-16 01:18:55 +03:00
|
|
|
eSessionStateOneTime
|
2020-03-01 13:25:50 +03:00
|
|
|
};
|
2020-01-17 19:21:41 +03:00
|
|
|
|
2020-04-26 04:09:03 +03:00
|
|
|
struct DHRatchet
|
|
|
|
{
|
|
|
|
int keyID = 0;
|
2020-06-30 20:00:41 +03:00
|
|
|
std::shared_ptr<i2p::crypto::X25519Keys> key;
|
2020-04-26 04:09:03 +03:00
|
|
|
uint8_t remote[32]; // last remote public key
|
2020-04-27 02:27:31 +03:00
|
|
|
bool newKey = true;
|
2020-03-01 13:25:50 +03:00
|
|
|
};
|
2020-01-15 23:13:43 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
public:
|
|
|
|
|
2021-05-09 14:33:26 +03:00
|
|
|
ECIESX25519AEADRatchetSession (GarlicDestination * owner, bool attachLeaseSetNS);
|
2020-03-01 13:25:50 +03:00
|
|
|
~ECIESX25519AEADRatchetSession ();
|
2020-01-15 23:13:43 +03:00
|
|
|
|
2021-01-05 02:20:16 +03:00
|
|
|
bool HandleNextMessage (uint8_t * buf, size_t len, std::shared_ptr<ReceiveRatchetTagSet> receiveTagset, int index = 0);
|
2020-03-01 13:25:50 +03:00
|
|
|
std::shared_ptr<I2NPMessage> WrapSingleMessage (std::shared_ptr<const I2NPMessage> msg);
|
2021-07-16 01:18:55 +03:00
|
|
|
std::shared_ptr<I2NPMessage> WrapOneTimeMessage (std::shared_ptr<const I2NPMessage> msg);
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
const uint8_t * GetRemoteStaticKey () const { return m_RemoteStaticKey; }
|
2020-01-17 03:33:00 +03:00
|
|
|
void SetRemoteStaticKey (const uint8_t * key) { memcpy (m_RemoteStaticKey, key, 32); }
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2021-01-05 23:56:48 +03:00
|
|
|
void Terminate () { m_IsTerminated = true; }
|
2020-01-30 19:48:32 +03:00
|
|
|
void SetDestination (const i2p::data::IdentHash& dest) // TODO:
|
|
|
|
{
|
|
|
|
if (!m_Destination) m_Destination.reset (new i2p::data::IdentHash (dest));
|
|
|
|
}
|
2020-03-01 13:25:50 +03:00
|
|
|
|
2020-03-14 23:35:34 +03:00
|
|
|
bool CheckExpired (uint64_t ts); // true is expired
|
2020-05-23 17:20:22 +03:00
|
|
|
bool CanBeRestarted (uint64_t ts) const { return ts > m_SessionCreatedTimestamp + ECIESX25519_RESTART_TIMEOUT; }
|
2020-05-23 22:58:11 +03:00
|
|
|
bool IsInactive (uint64_t ts) const { return ts > m_LastActivityTimestamp + ECIESX25519_INACTIVITY_TIMEOUT && CanBeRestarted (ts); }
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2020-05-02 18:13:40 +03:00
|
|
|
bool IsRatchets () const { return true; };
|
2020-10-27 15:32:38 +03:00
|
|
|
bool IsReadyToSend () const { return m_State != eSessionStateNewSessionSent; };
|
2021-01-05 23:56:48 +03:00
|
|
|
bool IsTerminated () const { return m_IsTerminated; }
|
2020-07-15 23:20:35 +03:00
|
|
|
uint64_t GetLastActivityTimestamp () const { return m_LastActivityTimestamp; };
|
2020-03-01 13:25:50 +03:00
|
|
|
|
2021-11-27 23:30:35 +03:00
|
|
|
protected:
|
|
|
|
|
2021-03-03 23:30:13 +03:00
|
|
|
i2p::crypto::NoiseSymmetricState& GetNoiseState () { return *this; };
|
|
|
|
void SetNoiseState (const i2p::crypto::NoiseSymmetricState& state) { GetNoiseState () = state; };
|
|
|
|
void CreateNonce (uint64_t seqn, uint8_t * nonce);
|
|
|
|
void HandlePayload (const uint8_t * buf, size_t len, const std::shared_ptr<ReceiveRatchetTagSet>& receiveTagset, int index);
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
private:
|
2020-01-15 23:13:43 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
bool GenerateEphemeralKeysAndEncode (uint8_t * buf); // buf is 32 bytes
|
2021-01-05 04:15:48 +03:00
|
|
|
void InitNewSessionTagset (std::shared_ptr<RatchetTagSet> tagsetNsr) const;
|
2020-01-15 23:13:43 +03:00
|
|
|
|
2020-02-04 00:21:07 +03:00
|
|
|
bool HandleNewIncomingSession (const uint8_t * buf, size_t len);
|
2020-03-01 13:25:50 +03:00
|
|
|
bool HandleNewOutgoingSessionReply (uint8_t * buf, size_t len);
|
2021-01-05 02:20:16 +03:00
|
|
|
bool HandleExistingSessionMessage (uint8_t * buf, size_t len, std::shared_ptr<ReceiveRatchetTagSet> receiveTagset, int index);
|
|
|
|
void HandleNextKey (const uint8_t * buf, size_t len, const std::shared_ptr<ReceiveRatchetTagSet>& receiveTagset);
|
2020-03-01 13:25:50 +03:00
|
|
|
|
2020-11-06 05:21:46 +03:00
|
|
|
bool NewOutgoingSessionMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen, bool isStatic = true);
|
2020-03-01 13:25:50 +03:00
|
|
|
bool NewSessionReplyMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen);
|
2020-04-03 04:48:39 +03:00
|
|
|
bool NextNewSessionReplyMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen);
|
2020-02-05 23:48:51 +03:00
|
|
|
bool NewExistingSessionMessage (const uint8_t * payload, size_t len, uint8_t * out, size_t outLen);
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2021-07-19 01:45:08 +03:00
|
|
|
size_t CreatePayload (std::shared_ptr<const I2NPMessage> msg, bool first, uint8_t * payload);
|
2020-07-30 00:47:46 +03:00
|
|
|
size_t CreateGarlicClove (std::shared_ptr<const I2NPMessage> msg, uint8_t * buf, size_t len);
|
2020-05-14 01:09:26 +03:00
|
|
|
size_t CreateLeaseSetClove (std::shared_ptr<const i2p::data::LocalLeaseSet> ls, uint64_t ts, uint8_t * buf, size_t len);
|
2020-03-01 13:25:50 +03:00
|
|
|
|
2021-01-05 02:20:16 +03:00
|
|
|
void GenerateMoreReceiveTags (std::shared_ptr<ReceiveRatchetTagSet> receiveTagset, int numTags);
|
2020-04-27 02:27:31 +03:00
|
|
|
void NewNextSendRatchet ();
|
2020-01-15 23:13:43 +03:00
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
private:
|
|
|
|
|
2020-11-06 05:21:46 +03:00
|
|
|
uint8_t m_RemoteStaticKey[32];
|
2020-04-03 04:48:39 +03:00
|
|
|
uint8_t m_Aepk[32]; // Alice's ephemeral keys, for incoming only
|
2020-04-29 01:23:13 +03:00
|
|
|
uint8_t m_NSREncodedKey[32], m_NSRH[32], m_NSRKey[32]; // new session reply, for incoming only
|
2020-06-30 20:00:41 +03:00
|
|
|
std::shared_ptr<i2p::crypto::X25519Keys> m_EphemeralKeys;
|
2020-03-01 13:25:50 +03:00
|
|
|
SessionState m_State = eSessionStateNew;
|
2022-05-20 19:56:05 +03:00
|
|
|
uint64_t m_SessionCreatedTimestamp = 0, m_LastActivityTimestamp = 0, // incoming (in seconds)
|
2020-07-15 23:20:35 +03:00
|
|
|
m_LastSentTimestamp = 0; // in milliseconds
|
2020-06-05 01:19:38 +03:00
|
|
|
std::shared_ptr<RatchetTagSet> m_SendTagset, m_NSRSendTagset;
|
2020-03-01 13:25:50 +03:00
|
|
|
std::unique_ptr<i2p::data::IdentHash> m_Destination;// TODO: might not need it
|
2020-04-01 00:35:51 +03:00
|
|
|
std::list<std::pair<uint16_t, int> > m_AckRequests; // (tagsetid, index)
|
2021-01-05 23:56:48 +03:00
|
|
|
bool m_SendReverseKey = false, m_SendForwardKey = false, m_IsTerminated = false;
|
2020-04-27 02:27:31 +03:00
|
|
|
std::unique_ptr<DHRatchet> m_NextReceiveRatchet, m_NextSendRatchet;
|
2020-06-14 04:24:16 +03:00
|
|
|
uint8_t m_PaddingSizes[32], m_NextPaddingSize;
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2020-04-30 03:50:31 +03:00
|
|
|
public:
|
|
|
|
|
|
|
|
// for HTTP only
|
|
|
|
int GetState () const { return (int)m_State; }
|
|
|
|
i2p::data::IdentHash GetDestination () const
|
|
|
|
{
|
|
|
|
return m_Destination ? *m_Destination : i2p::data::IdentHash ();
|
2020-03-01 13:25:50 +03:00
|
|
|
}
|
2022-05-20 19:56:05 +03:00
|
|
|
};
|
2020-04-07 18:40:18 +03:00
|
|
|
|
2021-03-03 23:30:13 +03:00
|
|
|
// single session for all incoming messages
|
|
|
|
class RouterIncomingRatchetSession: public ECIESX25519AEADRatchetSession
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
RouterIncomingRatchetSession (const i2p::crypto::NoiseSymmetricState& initState);
|
|
|
|
bool HandleNextMessage (const uint8_t * buf, size_t len);
|
2021-06-09 19:49:50 +03:00
|
|
|
i2p::crypto::NoiseSymmetricState& GetCurrentNoiseState () { return m_CurrentNoiseState; };
|
2021-11-27 23:30:35 +03:00
|
|
|
|
2021-06-09 19:49:50 +03:00
|
|
|
private:
|
|
|
|
|
|
|
|
i2p::crypto::NoiseSymmetricState m_CurrentNoiseState;
|
2021-11-27 23:30:35 +03:00
|
|
|
};
|
|
|
|
|
2021-07-10 21:33:23 +03:00
|
|
|
std::shared_ptr<I2NPMessage> WrapECIESX25519Message (std::shared_ptr<const I2NPMessage> msg, const uint8_t * key, uint64_t tag);
|
2024-01-30 23:41:57 +03:00
|
|
|
std::shared_ptr<I2NPMessage> WrapECIESX25519MessageForRouter (std::shared_ptr<I2NPMessage> msg, const uint8_t * routerPublicKey);
|
2020-01-15 23:13:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2021-07-16 01:18:55 +03:00
|
|
|
|