Class Account
- All Implemented Interfaces:
AutoCloseable
An account holds identity keys (Ed25519 for signing, Curve25519 for key agreement), one-time keys, and fallback keys. It is used to create Olm sessions for end-to-end encrypted communication with other devices.
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateInboundSession(OlmSessionVersion sessionVersion, Curve25519PublicKey theirIdentityKey, OlmMessage preKeyMessage) Creates an inboundOlmSessionfrom a pre-key message.createInboundSession(Curve25519PublicKey theirIdentityKey, OlmMessage preKeyMessage) Creates an inboundOlmSessionfrom a pre-key message, using the default session version.createOutboundSession(OlmSessionVersion sessionVersion, Curve25519PublicKey identityKey, Curve25519PublicKey oneTimeKey) Creates anOlmSessionwith the given identity key and one-time key.createOutboundSession(Curve25519PublicKey identityKey, Curve25519PublicKey oneTimeKey) Creates anOlmSessionwith the given identity key and one-time key, using the default session version.Returns the public Curve25519 key of this account.Returns the public Ed25519 key of this account.booleanForgets the previously used fallback key.static AccountfromDehydratedDevice(String ciphertext, String nonce, byte[] key) Creates anAccountobject from a dehydrated device.Generates a single new fallback key.generateOneTimeKeys(long count) Generates the supplied number of one-time keys.Returns the identity keys of this account.voidMarks all currently unpublished one-time and fallback keys as published.longReturns the maximum number of one-time keys the client should keep on the server.pickle()Converts the account into a JSON string representation.pickle(byte[] key) Encrypts the account using the given 32-byte key and returns the encrypted representation.pickleLegacy(byte[] pickleKey) Converts the account into the libolm pickle format, encrypted with the given 32-byte key.sign(byte[] message) Signs the given message using the Ed25519 fingerprint key.Signs the given message using the Ed25519 fingerprint key.longReturns the number of one-time keys stored locally.toDehydratedDevice(byte[] key) Creates a dehydrated device from the account.static AccountRestores anAccountfrom a previously saved JSON string.static AccountRestores anAccountfrom an encrypted string using a 32-byte key.static AccountunpickleLegacy(String pickleData, byte[] pickleKey) Restores anAccountfrom a legacy libolm pickle format.Returns the currently unpublished fallback key.Returns the currently unpublished one-time keys.Methods inherited from class NativeHandle
checkNotClosed, close, invalidate, nativePtr
-
Constructor Details
-
Account
public Account()Creates a newAccountwith new random identity keys.
-
-
Method Details
-
identityKeys
Returns the identity keys of this account.- Returns:
- the
IdentityKeyscontaining both Ed25519 and Curve25519 public keys - Throws:
IllegalStateException- if this account has been closed
-
ed25519Key
Returns the public Ed25519 key of this account.- Returns:
- the Ed25519 public key
- Throws:
IllegalStateException- if this account has been closed
-
curve25519Key
Returns the public Curve25519 key of this account.- Returns:
- the Curve25519 public key
- Throws:
IllegalStateException- if this account has been closed
-
sign
Signs the given message using the Ed25519 fingerprint key.- Parameters:
message- the message to sign- Returns:
- the signature
- Throws:
IllegalStateException- if this account has been closed
-
sign
Signs the given message using the Ed25519 fingerprint key.- Parameters:
message- the message to sign- Returns:
- the signature
- Throws:
IllegalStateException- if this account has been closed
-
maxNumberOfOneTimeKeys
public long maxNumberOfOneTimeKeys()Returns the maximum number of one-time keys the client should keep on the server.- Returns:
- the maximum number of one-time keys
- Throws:
IllegalStateException- if this account has been closed
-
createOutboundSession
public OlmSession createOutboundSession(Curve25519PublicKey identityKey, Curve25519PublicKey oneTimeKey) Creates anOlmSessionwith the given identity key and one-time key, using the default session version.- Parameters:
identityKey- the recipient's Curve25519 identity keyoneTimeKey- the recipient's Curve25519 one-time key- Returns:
- a new
OlmSession - Throws:
IllegalStateException- if this account has been closedKeyException- if the keys cannot be decodedSessionCreationException- if session creation fails
-
createOutboundSession
public OlmSession createOutboundSession(OlmSessionVersion sessionVersion, Curve25519PublicKey identityKey, Curve25519PublicKey oneTimeKey) Creates anOlmSessionwith the given identity key and one-time key.- Parameters:
sessionVersion- the Olm session protocol version to useidentityKey- the recipient's Curve25519 identity keyoneTimeKey- the recipient's Curve25519 one-time key- Returns:
- a new
OlmSession - Throws:
IllegalStateException- if this account has been closedKeyException- if the keys cannot be decodedSessionCreationException- if session creation fails
-
createInboundSession
public InboundCreationResult createInboundSession(Curve25519PublicKey theirIdentityKey, OlmMessage preKeyMessage) Creates an inboundOlmSessionfrom a pre-key message, using the default session version.- Parameters:
theirIdentityKey- the sender's Curve25519 identity keypreKeyMessage- theOlmMessagereceived from the sender; must be apre-key message- Returns:
- an
InboundCreationResultcontaining the new session and the decrypted plaintext of the pre-key message - Throws:
IllegalStateException- if this account has been closedKeyException- if the identity key cannot be decodedSessionCreationException- if session creation fails
-
createInboundSession
public InboundCreationResult createInboundSession(OlmSessionVersion sessionVersion, Curve25519PublicKey theirIdentityKey, OlmMessage preKeyMessage) Creates an inboundOlmSessionfrom a pre-key message.- Parameters:
sessionVersion- the Olm session protocol version to usetheirIdentityKey- the sender's Curve25519 identity keypreKeyMessage- theOlmMessagereceived from the sender; must be apre-key message- Returns:
- an
InboundCreationResultcontaining the new session and the decrypted plaintext of the pre-key message - Throws:
IllegalStateException- if this account has been closedKeyException- if the identity key cannot be decodedSessionCreationException- if session creation fails
-
generateOneTimeKeys
Generates the supplied number of one-time keys.The one-time key store has a limited capacity. If new keys are generated while the store is full, the oldest keys are discarded.
- Parameters:
count- the number of keys to generate- Returns:
- a
OneTimeKeyGenerationResultcontaining the created and discarded keys - Throws:
IllegalStateException- if this account has been closed
-
storedOneTimeKeyCount
public long storedOneTimeKeyCount()Returns the number of one-time keys stored locally.This will be equal to or greater than the number of one-time keys that have been published. Each time a new session is created using
createInboundSession(Curve25519PublicKey, OlmMessage), a one-time key is used and removed.- Returns:
- the number of stored one-time keys
- Throws:
IllegalStateException- if this account has been closed
-
unpublishedOneTimeKeys
Returns the currently unpublished one-time keys.The one-time keys should be published to a server and marked as published using
markKeysAsPublished().- Returns:
- a map of key ID to base64-encoded Curve25519 public key
- Throws:
IllegalStateException- if this account has been closed
-
generateFallbackKey
Generates a single new fallback key.The fallback key will be used by other users to establish a session if all the one-time keys on the server have been used up.
- Returns:
- the public Curve25519 key of the previous fallback key,
or an empty
Optionalif there was no previous key - Throws:
IllegalStateException- if this account has been closed
-
unpublishedFallbackKey
Returns the currently unpublished fallback key.The fallback key should be published just like the one-time keys, and marked as published using
markKeysAsPublished().- Returns:
- a map of key ID to base64-encoded Curve25519 public key
- Throws:
IllegalStateException- if this account has been closed
-
forgetFallbackKey
public boolean forgetFallbackKey()Forgets the previously used fallback key.The account stores at most two private parts of the fallback key. This method lets us forget the previously used one.
- Returns:
trueif the previous key was forgotten,falseotherwise- Throws:
IllegalStateException- if this account has been closed
-
markKeysAsPublished
public void markKeysAsPublished()Marks all currently unpublished one-time and fallback keys as published.- Throws:
IllegalStateException- if this account has been closed
-
pickle
Converts the account into a JSON string representation.- Returns:
- a JSON string representing the account
- Throws:
IllegalStateException- if this account has been closed
-
pickle
Encrypts the account using the given 32-byte key and returns the encrypted representation.- Parameters:
key- a 256-bit (32-byte) key for encrypting the account- Returns:
- an encrypted string representation of the account
- Throws:
IllegalStateException- if this account has been closedKeyException- if the key is not 32 bytes
-
pickleLegacy
Converts the account into the libolm pickle format, encrypted with the given 32-byte key.This produces a binary pickle compatible with the original libolm library, useful for migrating away from libolm. The counterpart is
unpickleLegacy(String, byte[]).- Parameters:
pickleKey- a 256-bit (32-byte) key for encrypting the pickle- Returns:
- the libolm-format pickle string of the
Account - Throws:
IllegalStateException- if this account has been closedKeyException- if the key is not 32 bytesPickleException- if the pickle could not be created
-
unpickle
Restores anAccountfrom a previously saved JSON string.- Parameters:
pickleData- the JSON string frompickle()- Returns:
- a restored
Account - Throws:
PickleException- if the data cannot be deserialized
-
unpickle
Restores anAccountfrom 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 account- Returns:
- a restored
Account - Throws:
KeyException- if the key is not 32 bytesPickleException- if the data cannot be decrypted or deserialized
-
unpickleLegacy
Restores anAccountfrom 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
Account - Throws:
PickleException- if the data cannot be decrypted or deserialized
-
toDehydratedDevice
Creates a dehydrated device from the account.A dehydrated device is a device that is stored encrypted on the server that can receive messages when the user has no other active devices. Upon login, the user can rehydrate the device (using
fromDehydratedDevice(String, String, byte[])) and decrypt the messages sent to the dehydrated device.The account must be a newly-created account that does not have any Olm sessions, since the dehydrated device format does not store sessions.
The format used is defined in MSC3814.
- Parameters:
key- a 256-bit (32-byte) key for encrypting the device- Returns:
- the ciphertext and nonce
- Throws:
IllegalStateException- if this account has been closedKeyException- if the key is not 32 bytesPickleException- if creating the dehydrated device fails
-
fromDehydratedDevice
Creates anAccountobject from a dehydrated device.- Parameters:
ciphertext- the ciphertext generated bytoDehydratedDevice(byte[])nonce- the nonce generated bytoDehydratedDevice(byte[])key- a 256-bit (32-byte) key for decrypting the device- Returns:
- a restored
Account - Throws:
KeyException- if the key is not 32 bytesPickleException- if the dehydrated device cannot be decrypted
-