Class CryptoBox
- java.lang.Object
-
- com.wire.cryptobox.CryptoBox
-
public final class CryptoBox extends java.lang.ObjectA CryptoBox is an opaque container of all the necessary key material needed for exchanging end-to-end encrypted messages with peers for a single, logical client or device. It maintains a pool ofCryptoSessions for all remote peers.Every cryptographic session with a peer is represented by a
CryptoSession. These sessions are pooled by a CryptoBox, i.e. if a session with the same session ID is requested multiple times, the same instance is returned. Consequently, CryptoSessions are kept in memory once loaded. They can be explicitly closed throughcloseSession(com.wire.cryptobox.CryptoSession)orcloseAllSessions(). All loaded sessions are implicitly closed when the CryptoBox itself is closed viaclose(). Note that it is considered programmer error to let a CryptoBox become unreachable and thus eligible for garbage collection without having calledclose(), even though this class overridesObject.finalize()as an additional safety net for deallocating all native resources.A CryptoBox is thread-safe.
- See Also:
CryptoSession
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCryptoBox.IdentityModeThe desired local storage mode for use withopenWith(java.lang.String, byte[], com.wire.cryptobox.CryptoBox.IdentityMode).
-
Field Summary
Fields Modifier and Type Field Description static intMAX_PREKEY_IDThe max ID of an ephemeral prekey generated bynewPreKeys(int, int).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Close the CryptoBox.voidcloseAllSessions()Close all open sessions.voidcloseSession(CryptoSession sess)Close a session.byte[]copyIdentity()Copy the long-term identity from this CryptoBox.voiddeleteSession(java.lang.String sid)Delete a session.static byte[]getFingerprintFromPrekey(PreKey preKey)Get the public key fingerprint from a prekey.byte[]getLocalFingerprint()Get the local fingerprint as a hex-encoded byte array.CryptoSessiongetSession(java.lang.String sid)Get an existing session by ID.SessionMessageinitSessionFromMessage(java.lang.String sid, byte[] message)Initialise aCryptoSessionusing a received encrypted message.CryptoSessioninitSessionFromPreKey(java.lang.String sid, PreKey prekey)Initialise aCryptoSessionusing the prekey of a peer.booleanisClosed()PreKeynewLastPreKey()Generate a new last prekey.PreKey[]newPreKeys(int start, int num)Generate a new batch of ephemeral prekeys.static CryptoBoxopen(java.lang.String dir)Open a CryptoBox that operates on the given directory.static CryptoBoxopenWith(java.lang.String dir, byte[] id, CryptoBox.IdentityMode mode)Open a CryptoBox that operates on the given directory, using an existing external identity.CryptoSessiontryGetSession(java.lang.String sid)Try to get an existing session by ID.
-
-
-
Field Detail
-
MAX_PREKEY_ID
public static final int MAX_PREKEY_ID
The max ID of an ephemeral prekey generated bynewPreKeys(int, int).- See Also:
- Constant Field Values
-
-
Method Detail
-
open
public static CryptoBox open(java.lang.String dir) throws CryptoException
Open a CryptoBox that operates on the given directory. The given directory must exist and be writeable.Note: Do not open multiple boxes that operate on the same or overlapping directories. Doing so results in undefined behaviour.
- Parameters:
dir- The root storage directory of the box.- Throws:
CryptoException
-
openWith
public static CryptoBox openWith(java.lang.String dir, byte[] id, CryptoBox.IdentityMode mode) throws CryptoException
Open a CryptoBox that operates on the given directory, using an existing external identity. The given identity must match the (public or complete) identity that the CryptoBox already has, if any. The given directory must exist and be writeable.Note: Do not open multiple boxes that operate on the same or overlapping directories. Doing so results in undefined behaviour.
- Parameters:
dir- The root storage directory of the box.id- The serialised external identity to use.mode- The desired local identity storage.- Throws:
CryptoException
-
getFingerprintFromPrekey
public static byte[] getFingerprintFromPrekey(PreKey preKey) throws CryptoException
Get the public key fingerprint from a prekey.- Returns:
- The HEX encoded fingerprint.
- Throws:
CryptoException
-
copyIdentity
public byte[] copyIdentity() throws CryptoExceptionCopy the long-term identity from this CryptoBox.- Returns:
- The opaque, serialised identity to be stored in a safe place or
transmitted over a safe channel for subsequent use with
openWith(java.lang.String, byte[], com.wire.cryptobox.CryptoBox.IdentityMode). - Throws:
CryptoException
-
getLocalFingerprint
public byte[] getLocalFingerprint() throws CryptoExceptionGet the local fingerprint as a hex-encoded byte array.- Throws:
CryptoException
-
newLastPreKey
public PreKey newLastPreKey() throws CryptoException
Generate a new last prekey. The last prekey is never removed as a result ofinitSessionFromMessage(java.lang.String, byte[]).- Throws:
CryptoException
-
newPreKeys
public PreKey[] newPreKeys(int start, int num) throws CryptoException
Generate a new batch of ephemeral prekeys. If start + num >MAX_PREKEY_IDthe IDs wrap around and start over at 0. Thus after any valid invocation of this method, the last generated prekey ID is always (start + num) % (MAX_PREKEY_ID+ 1). The caller can remember that ID and feed it back intonewPreKeys(int, int)as the start ID when the next batch of ephemeral keys needs to be generated.- Parameters:
start- The ID (>= 0 and <=MAX_PREKEY_ID) of the first prekey to generate.num- The total number of prekeys to generate (> 0 and <=MAX_PREKEY_ID).- Throws:
CryptoException
-
initSessionFromPreKey
public CryptoSession initSessionFromPreKey(java.lang.String sid, PreKey prekey) throws CryptoException
Initialise aCryptoSessionusing the prekey of a peer.This is the entry point for the initiator of a session, i.e. the side that wishes to send the first message.
- Parameters:
sid- The ID of the new session.prekey- The prekey of the peer.- Throws:
CryptoException
-
initSessionFromMessage
public SessionMessage initSessionFromMessage(java.lang.String sid, byte[] message) throws CryptoException
Initialise aCryptoSessionusing a received encrypted message.This is the entry point for the recipient of an encrypted message.
- Parameters:
sid- The ID of the new session.message- The encrypted (prekey) message.- Throws:
CryptoException
-
getSession
public CryptoSession getSession(java.lang.String sid) throws CryptoException
Get an existing session by ID.If the session does not exist, a
CryptoExceptionis thrown with the codeCryptoException.Code.SESSION_NOT_FOUND.- Parameters:
sid- The ID of the session to get.- Throws:
CryptoException
-
tryGetSession
public CryptoSession tryGetSession(java.lang.String sid) throws CryptoException
Try to get an existing session by ID.Equivalent to
getSession(java.lang.String), except that null is returned if the session does not exist.- Parameters:
sid- The ID of the session to get.- Throws:
CryptoException
-
closeSession
public void closeSession(CryptoSession sess)
Close a session.Note: After a session has been closed, any operations other than closeSession are considered programmer error and result in an
IllegalStateException.If the session is already closed, this is a no-op.
- Parameters:
sess- The session to close.
-
closeAllSessions
public void closeAllSessions()
Close all open sessions.
-
deleteSession
public void deleteSession(java.lang.String sid) throws CryptoExceptionDelete a session. If the session is currently loaded, it is automatically closed before being deleted.Note: After a session has been deleted, further messages received from the peer can no longer be decrypted.
- Parameters:
sid- The ID of the session to delete.- Throws:
CryptoException
-
close
public void close()
Close the CryptoBox.Note: After a box has been closed, any operations other than close are considered programmer error and result in an
IllegalStateException.If the box is already closed, this is a no-op.
-
isClosed
public boolean isClosed()
-
-