Class OutboundGroupSession
- All Implemented Interfaces:
AutoCloseable
A group session consists of a ratchet, used for encryption, and an Ed25519
signing key pair, used for authenticity. The session key (obtained via
sessionKey()) can be shared with other participants so that they
can create InboundGroupSession objects to decrypt messages sent
by this session.
This class implements AutoCloseable and should be used in a
try-with-resources block to ensure native resources are properly released.
- Author:
- François HERBRETEAU
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new outbound group session with a random ratchet state and signing key pair.OutboundGroupSession(MegolmSessionVersion version) Constructs a new outbound group session with a random ratchet state and signing key pair. -
Method Summary
Modifier and TypeMethodDescriptionencrypt(byte[] plaintext) Encrypts the given plaintext with the group session.intReturns the current message index.pickle()Converts the group session into a JSON string representation.pickle(byte[] key) Encrypts the group session using the given 32-byte key and returns the encrypted representation.Returns the session configuration (protocol version) used by this session.Returns the globally unique session ID, in base64-encoded form.Exports the group session into a session key.static OutboundGroupSessionRestores anOutboundGroupSessionfrom a previously saved JSON string.static OutboundGroupSessionRestores anOutboundGroupSessionfrom an encrypted string using a 32-byte key.static OutboundGroupSessionunpickleLegacy(String pickleData, byte[] pickleKey) Restores anOutboundGroupSessionfrom a legacy libolm pickle format.Methods inherited from class NativeHandle
checkNotClosed, close, invalidate, nativePtr
-
Constructor Details
-
OutboundGroupSession
public OutboundGroupSession()Constructs a new outbound group session with a random ratchet state and signing key pair. -
OutboundGroupSession
Constructs a new outbound group session with a random ratchet state and signing key pair.- Parameters:
version- the Megolm session protocol version to use
-
-
Method Details
-
sessionId
Returns the globally unique session ID, in base64-encoded form.The session ID is the public part of the Ed25519 key pair associated with the group session. Due to the construction, every session ID is probabilistically globally unique.
- Returns:
- the session ID as a base64 string
- Throws:
IllegalStateException- if this session has been closed
-
messageIndex
public int messageIndex()Returns the current message index.The message index is incremented each time a message is encrypted with the group session.
- Returns:
- the current message index
- Throws:
IllegalStateException- if this session has been closed
-
sessionKey
Exports the group session into a session key.The session key contains the key version constant, the current message index, the ratchet state, and the public part of the signing key pair. It is signed by the signing key pair for authenticity. The session key is typically sent to other group participants via a secure peer-to-peer channel (e.g. an Olm channel) so that they can reconstruct an
InboundGroupSessionto decrypt messages.- Returns:
- the session key as a base64 string
- Throws:
IllegalStateException- if this session has been closed
-
sessionConfig
Returns the session configuration (protocol version) used by this session.The version determines the MAC truncation behaviour used for encryption and authentication.
- Returns:
- the
MegolmSessionVersionof this session - Throws:
IllegalStateException- if this session has been closed
-
encrypt
Encrypts the given plaintext with the group session.The resulting ciphertext is MAC-ed, signed with the group session's Ed25519 key pair, and returned as a structured
MegolmMessage.- Parameters:
plaintext- the plaintext to encrypt- Returns:
- the encrypted message as a
MegolmMessage - Throws:
IllegalStateException- if this session has been closed
-
pickle
Converts the group session into a JSON string representation.- Returns:
- a JSON string representing the session
- Throws:
IllegalStateException- if this session has been closed
-
pickle
Encrypts the group session using the given 32-byte key and returns the encrypted representation.- Parameters:
key- a 256-bit (32-byte) key for encrypting the session- Returns:
- an encrypted string representation of the session
- Throws:
IllegalStateException- if this session has been closedKeyException- if the key is not 32 bytes
-
unpickle
Restores anOutboundGroupSessionfrom a previously saved JSON string.- Parameters:
pickleData- the JSON string frompickle()- Returns:
- a restored
OutboundGroupSession - Throws:
PickleException- if the data cannot be deserialized
-
unpickle
Restores anOutboundGroupSessionfrom an encrypted string using a 32-byte key.- Parameters:
pickleData- the encrypted pickle data frompickle(byte[])key- a 256-bit (32-byte) key for decrypting the session- Returns:
- a restored
OutboundGroupSession - Throws:
KeyException- if the key is not 32 bytesPickleException- if the data cannot be decrypted or deserialized
-
unpickleLegacy
Restores anOutboundGroupSessionfrom a legacy libolm pickle format. The pickle must be encrypted with the provided key.- Parameters:
pickleData- the libolm pickle datapickleKey- the key used to encrypt the pickle data- Returns:
- a restored
OutboundGroupSession - Throws:
PickleException- if the data cannot be decrypted or deserialized
-