net.i2p.crypto

## Class SessionKeyManager

- java.lang.Object
- - net.i2p.crypto.SessionKeyManager

- Direct Known Subclasses:
    MuxedPQSKM, MuxedSKM, RatchetSKM, TransientSessionKeyManager

      public class SessionKeyManager
      extends Object

  Manage the session keys and session tags used for encryption and decryption. This base implementation simply ignores sessions and acts as if everything is unknown (and hence always forces a full ElGamal encryption for each message). See TransientSessionKeyManager subclass which manages and persists keys and tags.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `SessionKeyManager()` A dummy SessionKeyManager for testing or for passing to ElGamalAESEngine.decrypt()
    `SessionKeyManager(I2PAppContext context)` A dummy SessionKeyManager for testing or for passing to ElGamalAESEngine.decrypt()

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `SessionTag` | `consumeNextAvailableTag(PublicKey target, SessionKey key)` Retrieve the next available session tag for identifying the use of the given key when communicating with the target.
    `SessionKey` | `consumeTag(SessionTag tag)` Determine if we have received a session key associated with the given session tag, and if so, discard it (but keep track for frequent dups) and return the decryption key it was received with (via tagsReceived(...)).
    `SessionKey` | `createSession(PublicKey target)` Generate a new session key and associate it with the specified target.
    `void` | `createSession(PublicKey target, SessionKey key)` Associate a new session key with the specified target.
    `void` | `failTags(PublicKey target)` Mark all of the tags delivered to the target up to this point as invalid, since the peer has failed to respond when they should have.
    `void` | `failTags(PublicKey target, SessionKey key, TagSetHandle ts)`
    `int` | `getAvailableTags(PublicKey target, SessionKey key)` Determine (approximately) how many available session tags for the current target have been confirmed and are available
    `long` | `getAvailableTimeLeft(PublicKey target, SessionKey key)` Determine how long the available tags will be available for before expiring, in milliseconds
    `SessionKey` | `getCurrentKey(PublicKey target)` Retrieve the session key currently associated with encryption to the target, or null if a new session key should be generated.
    `SessionKey` | `getCurrentOrNewKey(PublicKey target)` Retrieve the session key currently associated with encryption to the target.
    `int` | `getLowThreshold()` Get the low threshold for tag sending.
    `int` | `getTagsToSend()` Get the number of tags to send.
    `void` | `renderStatusHTML(Writer out)`
    `boolean` | `shouldSendTags(PublicKey target, SessionKey key)` Determine if tags should be sent to the target.
    `boolean` | `shouldSendTags(PublicKey target, SessionKey key, int lowThreshold)` Determine if tags should be sent to the target with the specified threshold.
    `void` | `shutdown()` Called when the system is closing down, instructing the session key manager to take whatever precautions are necessary (saving state, etc)
    `void` | `tagsAcked(PublicKey target, SessionKey key, TagSetHandle ts)`
    `TagSetHandle` | `tagsDelivered(PublicKey target, SessionKey key, Set<SessionTag> sessionTags)` Take note of the fact that the given sessionTags associated with the key for encryption to the target have definitely been received at the target (aka call this method after receiving an ack to a message delivering them)
    `void` | `tagsReceived(SessionKey key, Set<SessionTag> sessionTags)` Accept the given tags and associate them with the given key for decryption, with the default expiration.
    `void` | `tagsReceived(SessionKey key, Set<SessionTag> sessionTags, long expire)` Accept the given tags and associate them with the given key for decryption, with specified expiration.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SessionKeyManager

          public SessionKeyManager()

      A dummy SessionKeyManager for testing or for passing to ElGamalAESEngine.decrypt()

    - #### SessionKeyManager

          public SessionKeyManager(I2PAppContext context)

      A dummy SessionKeyManager for testing or for passing to ElGamalAESEngine.decrypt()
      Parameters:
        `context` - unused

  - ### Method Detail

    - #### consumeNextAvailableTag

          public SessionTag consumeNextAvailableTag(PublicKey target,
                                                    SessionKey key)

      Retrieve the next available session tag for identifying the use of the given key when communicating with the target. If this returns null, no tags are available so ElG should be used with the given key (a new sessionKey should NOT be used)

    - #### consumeTag

          public SessionKey consumeTag(SessionTag tag)

      Determine if we have received a session key associated with the given session tag, and if so, discard it (but keep track for frequent dups) and return the decryption key it was received with (via tagsReceived(...)). returns null if no session key matches

    - #### createSession

          public SessionKey createSession(PublicKey target)

      Generate a new session key and associate it with the specified target. Racy if called after getCurrentKey() to check for a current session; use getCurrentOrNewKey() in that case.

    - #### createSession

          public void createSession(PublicKey target,
                                    SessionKey key)

      Associate a new session key with the specified target. Metrics to determine when to expire that key begin with this call. Racy if called after getCurrentKey() to check for a current session; use getCurrentOrNewKey() in that case.

    - #### failTags

          public void failTags(PublicKey target)

      Mark all of the tags delivered to the target up to this point as invalid, since the peer has failed to respond when they should have. This call essentially lets the system recover from corrupted tag sets and crashes

    - #### failTags

          public void failTags(PublicKey target,
                               SessionKey key,
                               TagSetHandle ts)

    - #### getAvailableTags

          public int getAvailableTags(PublicKey target,
                                      SessionKey key)

      Determine (approximately) how many available session tags for the current target have been confirmed and are available

    - #### getAvailableTimeLeft

          public long getAvailableTimeLeft(PublicKey target,
                                           SessionKey key)

      Determine how long the available tags will be available for before expiring, in milliseconds

    - #### getCurrentKey

          public SessionKey getCurrentKey(PublicKey target)

      Retrieve the session key currently associated with encryption to the target, or null if a new session key should be generated. Warning - don't generate a new session if this returns null, it's racy, use getCurrentOrNewKey()

    - #### getCurrentOrNewKey

          public SessionKey getCurrentOrNewKey(PublicKey target)

      Retrieve the session key currently associated with encryption to the target. Generates a new session and session key if not previously exising.
      Returns:
        non-null

    - #### getLowThreshold

          public int getLowThreshold()

      Get the low threshold for tag sending.
      Returns:
        the low threshold, or 0 if not implemented

    - #### getTagsToSend

          public int getTagsToSend()

      Get the number of tags to send.
      Returns:
        the number of tags to send, or 0 if not implemented

    - #### renderStatusHTML

          public void renderStatusHTML(Writer out)
                                throws IOException

      Throws:
        `IOException`

    - #### shouldSendTags

          public boolean shouldSendTags(PublicKey target,
                                        SessionKey key)

      Determine if tags should be sent to the target.
      Parameters:
        `target` - the target public key
        `key` - the session key
      Returns:
        true if we have less than the threshold or what we have is about to expire

    - #### shouldSendTags

          public boolean shouldSendTags(PublicKey target,
                                        SessionKey key,
                                        int lowThreshold)

      Determine if tags should be sent to the target with the specified threshold.
      Parameters:
        `target` - the target public key
        `key` - the session key
        `lowThreshold` - the low threshold
      Returns:
        true if we have less than the threshold or what we have is about to expire

    - #### shutdown

          public void shutdown()

      Called when the system is closing down, instructing the session key manager to take whatever precautions are necessary (saving state, etc)

    - #### tagsAcked

          public void tagsAcked(PublicKey target,
                                SessionKey key,
                                TagSetHandle ts)

    - #### tagsDelivered

          public TagSetHandle tagsDelivered(PublicKey target,
                                            SessionKey key,
                                            Set<SessionTag> sessionTags)

      Take note of the fact that the given sessionTags associated with the key for encryption to the target have definitely been received at the target (aka call this method after receiving an ack to a message delivering them)

    - #### tagsReceived

          public void tagsReceived(SessionKey key,
                                   Set<SessionTag> sessionTags)

      Accept the given tags and associate them with the given key for decryption, with the default expiration.

    - #### tagsReceived

          public void tagsReceived(SessionKey key,
                                   Set<SessionTag> sessionTags,
                                   long expire)

      Accept the given tags and associate them with the given key for decryption, with specified expiration.
      Parameters:
        `sessionTags` - modifiable; NOT copied
        `expire` - time from now