java.lang.Object
net.i2p.router.crypto.ratchet.RatchetSKM.OutboundSession
- Enclosing class:
RatchetSKM
The state for a crypto session to a single public key.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final LinkedBlockingQueue<Integer> Acknowledgements waiting to be sent.private SimpleTimer2.TimedEventTimer that flushes the pending acks.private final ConcurrentHashMap<Integer, ReplyCallback> Reply callbacks by tag id.private SessionKeyKey used for the most recent messages.private DestinationThe local destination owning the session.private final longWhen the session was created.private NextSessionKeyThe last key received from the peer, possibly without data, for the duplicate check.private NextSessionKeyThe last key received from the peer that carried data.private NextSessionKeyTheir latest outbound key, possibly without data.private NextSessionKeyTheir latest outbound key that carried data.private longWhen we last received through the session.private longWhen we last sent through the session.private NextSessionKeyOur next inbound key, sent ahead of use.private intKey id for our next inbound ratchet key.private KeyPairOur current inbound ratchet key pair.private intMessages sent under our current inbound key.private intKey id for our next outbound ratchet key.private KeyPairOur current outbound ratchet key pair.private SessionKeyRoot key for the next inbound receive chain.private ReplyCallbackCallback for an NS reply.private ReplyCallbackCallback for an NSR reply.private final HandshakeStateCurrent state of the handshake.private RatchetTagSetThere is only one active outbound tagset.private final PublicKeyThe peer's ratchet public key.private final Set<RatchetTagSet> Before the first ack, all tagsets go here.private static final StringHKDF info string for the reply tags.private static final StringHKDF info string for the ratchet tag set.private static final intLargest ES receive window, in keys.private static final intLargest NSR receive window, in keys.private static final intMost acknowledgements sent in one message.private static final intMost reverse keys sent in one message.private static final intSmallest ES receive window, in keys.private static final intSmallest NSR receive window, in keys.Constructor Summary
ConstructorsConstructorDescriptionOutboundSession(PublicKey target, Destination d, SessionKey key, HandshakeState state, ReplyCallback callback) Outbound session for the given target.Method Summary
Modifier and TypeMethodDescriptionvoidackRequested(int id, int n) Queue the ACK for one ratchet tag to be sent back.intNumber of tags remaining in acked tag sets.Consume the next available tag and advance.intexpireCallbacks(long now) Drop reply callbacks whose expiration has passed.intexpireTags(long now) Expire old tags, returning the number of tag sets removed.(package private) voidFirst tag was received for this inbound (ES) tagset.Pending acks drained from the queue, up to the send limit.The session key stored at construction; never updated afterwards.Destination for the current session's far end.longWhen this session was established.Original outbound state, null for inbound.longFurthest away tag set expiration date, after which all of the tags will have expired.longONLY updated for inbound NS/NSR/ES tag used.longNOT updated for inbound except for NSR and first ES tag used.private NextSessionKeyReverse key to send, or null.(package private) List<RatchetTagSet> This is used only by renderStatusHTML().The far-end public key this session talks to.voidProcess a Next Session Key message, validating the ID sequence before advancing the ratchet to the new tag set key.voidreceivedACK(int id, int n) Run and remove the callback registered for the ACKed tag, if any.voidregisterCallback(int id, int n, ReplyCallback callback) Register the callback to run when the ACK for one ratchet tag arrives; a live callback for the same tag is not replaced.booleanregisterTimer(Destination d, SimpleTimer2.TimedEvent timer) A timer that we will cancel when we send something.(package private) voidtagConsumed(RatchetTagSet set) A tag was received for this inbound (ES) tagset.(package private) voidupdateSession(HandshakeState state, ReplyCallback callback, SplitKeys split) Inbound or outbound.
Field Details
_target
The peer's ratchet public key._state
Current state of the handshake._NScallback
Callback for an NS reply._NSRcallback
Callback for an NSR reply._currentKey
Key used for the most recent messages._established
private final long _establishedWhen the session was created._lastUsed
private long _lastUsedWhen we last sent through the session._lastReceived
private long _lastReceivedWhen we last received through the session._unackedTagSets
Before the first ack, all tagsets go here. These are dropped by clear() when the session's tag state is reset or the session dies; a failsafe expire also removes stale sets. Unsynchronized, sync to use. No particular order._tagSet
There is only one active outbound tagset. Synch on _unackedTagSets to access this._callbacks
Reply callbacks by tag id._acksToSend
Acknowledgements waiting to be sent._ackTimer
Timer that flushes the pending acks._destination
The local destination owning the session._myOBKeyID
private int _myOBKeyIDKey id for our next outbound ratchet key._myIBKeyID
private int _myIBKeyIDKey id for our next inbound ratchet key._myIBKeySendCount
private int _myIBKeySendCountMessages sent under our current inbound key._myIBKeys
Our current inbound ratchet key pair._myOBKeys
Our current outbound ratchet key pair._myIBKey
Our next inbound key, sent ahead of use._hisIBKey
The last key received from the peer, possibly without data, for the duplicate check._hisOBKey
Their latest outbound key, possibly without data._hisIBKeyWithData
The last key received from the peer that carried data._hisOBKeyWithData
Their latest outbound key that carried data._nextIBRootKey
Root key for the next inbound receive chain.MIN_RCV_WINDOW_NSR
private static final int MIN_RCV_WINDOW_NSRSmallest NSR receive window, in keys.- See Also:
MAX_RCV_WINDOW_NSR
private static final int MAX_RCV_WINDOW_NSRLargest NSR receive window, in keys.- See Also:
MIN_RCV_WINDOW_ES
private static final int MIN_RCV_WINDOW_ESSmallest ES receive window, in keys.- See Also:
MAX_RCV_WINDOW_ES
private static final int MAX_RCV_WINDOW_ESLargest ES receive window, in keys.- See Also:
INFO_0
HKDF info string for the reply tags.- See Also:
INFO_7
HKDF info string for the ratchet tag set.- See Also:
MAX_SEND_ACKS
private static final int MAX_SEND_ACKSMost acknowledgements sent in one message.- See Also:
MAX_SEND_REVERSE_KEY
private static final int MAX_SEND_REVERSE_KEYMost reverse keys sent in one message.- See Also:
Constructor Details
OutboundSession
public OutboundSession(PublicKey target, Destination d, SessionKey key, HandshakeState state, ReplyCallback callback) Outbound session for the given target.- Parameters:
target- the far-end public key this session talks tod- may be nullkey- may be nullstate- the completed handshake state, source of the chaining key and the role that decides the tagset directioncallback- may be null. Always null for IB.
Method Details
updateSession
Inbound or outbound. Checks state.getRole() to determine. For outbound (NSR rcvd by Alice), sets session to transition to ES mode outbound. For inbound (NSR sent by Bob), sets up inbound ES tagset.- Parameters:
state- the handshake state to advance, whose role decides the directioncallback- only for inbound (NSR sent by Bob), may be nullsplit- the NSR split keys - new root chaining key ck plus the per-direction session keys k_ab and k_ba
nextKeyReceived
Process a Next Session Key message, validating the ID sequence before advancing the ratchet to the new tag set key.- Parameters:
key- the received next-session-key message
getReverseSendKey
tagConsumed
A tag was received for this inbound (ES) tagset.- Parameters:
set- the inbound tagset
firstTagConsumed
First tag was received for this inbound (ES) tagset. Find the corresponding outbound (ES) tagset in _unackedTagSets, move it to _tagSets, and remove all others from _unackedTagSets.Note: We do NOT remove remaining inbound tags from _inboundTagSets here. The addTags() sliding window in consume() already handles tag lifecycle — generating new tags ahead and trimming old ones via
RatchetSKM.expireTag(RatchetSessionTag, RatchetTagSet). Purging all tags on first consumption broke subsequent ES decryptions because Alice's retransmitted SYNs (using pre-generated tags 1-N) could no longer be found in _inboundTagSets.- Parameters:
set- the inbound tagset
getTagSets
List<RatchetTagSet> getTagSets()This is used only by renderStatusHTML(). It includes both acked and unacked RatchetTagSets.- Returns:
- list of RatchetTagSet objects
getTarget
The far-end public key this session talks to.- Returns:
- the target key, set at construction
getHandshakeState
Original outbound state, null for inbound.- Returns:
- the handshake state
getCurrentKey
The session key stored at construction; never updated afterwards.- Returns:
- the key, or null if the session was created without one
getEstablishedDate
public long getEstablishedDate()When this session was established.- Returns:
- creation time in ms
getLastUsedDate
public long getLastUsedDate()NOT updated for inbound except for NSR and first ES tag used.- Returns:
- the last used date
getLastReceivedDate
public long getLastReceivedDate()ONLY updated for inbound NS/NSR/ES tag used.- Returns:
- the last received date
expireTags
public int expireTags(long now) Expire old tags, returning the number of tag sets removed.- Parameters:
now- current time in ms, tag sets expiring at or before it are removed- Returns:
- the number of tag sets removed
consumeNext
Consume the next available tag and advance.- Returns:
- the next tag with its key, rotation keys and pending acks, or null if the session or tag set has expired or drained
registerTimer
A timer that we will cancel when we send something. Side effect - binds this session to the supplied destination.- Parameters:
d- the far-end Destination for this PublicKey if known, or nulltimer- the ack timer to hold, cancelled when this session sends; ignored if a timer is already registered- Returns:
- true if registered
getDestination
Destination for the current session's far end.- Returns:
- the far-end Destination for this PublicKey, or null
availableTags
public int availableTags()Number of tags remaining in acked tag sets.- Returns:
- the total number of tags in acked RatchetTagSets
getLastExpirationDate
public long getLastExpirationDate()Furthest away tag set expiration date, after which all of the tags will have expired.- Returns:
- the last expiration date
registerCallback
Register the callback to run when the ACK for one ratchet tag arrives; a live callback for the same tag is not replaced.- Parameters:
id- tag set ID the ACK is forn- tag index within the tag setcallback- callback to run on ACK
receivedACK
public void receivedACK(int id, int n) Run and remove the callback registered for the ACKed tag, if any.- Parameters:
id- tag set ID the ACK is forn- tag index within the tag set
ackRequested
public void ackRequested(int id, int n) Queue the ACK for one ratchet tag to be sent back.- Parameters:
id- tag set ID being ACKedn- tag index within the tag set
getAcksToSend
expireCallbacks
public int expireCallbacks(long now) Drop reply callbacks whose expiration has passed.- Parameters:
now- current time in ms- Returns:
- the number of callbacks removed