diff options
Diffstat (limited to 'java/src/main/java/org/whispersystems/libaxolotl/SessionBuilder.java')
-rw-r--r-- | java/src/main/java/org/whispersystems/libaxolotl/SessionBuilder.java | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/java/src/main/java/org/whispersystems/libaxolotl/SessionBuilder.java b/java/src/main/java/org/whispersystems/libaxolotl/SessionBuilder.java index 736d9ab1..826fcb44 100644 --- a/java/src/main/java/org/whispersystems/libaxolotl/SessionBuilder.java +++ b/java/src/main/java/org/whispersystems/libaxolotl/SessionBuilder.java @@ -49,8 +49,7 @@ public class SessionBuilder { private final PreKeyStore preKeyStore; private final SignedPreKeyStore signedPreKeyStore; private final IdentityKeyStore identityKeyStore; - private final long recipientId; - private final int deviceId; + private final AxolotlAddress remoteAddress; /** * Constructs a SessionBuilder. @@ -58,31 +57,28 @@ public class SessionBuilder { * @param sessionStore The {@link org.whispersystems.libaxolotl.state.SessionStore} to store the constructed session in. * @param preKeyStore The {@link org.whispersystems.libaxolotl.state.PreKeyStore} where the client's local {@link org.whispersystems.libaxolotl.state.PreKeyRecord}s are stored. * @param identityKeyStore The {@link org.whispersystems.libaxolotl.state.IdentityKeyStore} containing the client's identity key information. - * @param recipientId The recipient ID of the remote user to build a session with. - * @param deviceId The device ID of the remote user's physical device. + * @param remoteAddress The address of the remote user to build a session with. */ public SessionBuilder(SessionStore sessionStore, PreKeyStore preKeyStore, SignedPreKeyStore signedPreKeyStore, IdentityKeyStore identityKeyStore, - long recipientId, int deviceId) + AxolotlAddress remoteAddress) { this.sessionStore = sessionStore; this.preKeyStore = preKeyStore; this.signedPreKeyStore = signedPreKeyStore; this.identityKeyStore = identityKeyStore; - this.recipientId = recipientId; - this.deviceId = deviceId; + this.remoteAddress = remoteAddress; } /** * Constructs a SessionBuilder * @param store The {@link org.whispersystems.libaxolotl.state.AxolotlStore} to store all state information in. - * @param recipientId The recipient ID of the remote user to build a session with. - * @param deviceId The device ID of the remote user's physical device. + * @param remoteAddress The address of the remote user to build a session with. */ - public SessionBuilder(AxolotlStore store, long recipientId, int deviceId) { - this(store, store, store, store, recipientId, deviceId); + public SessionBuilder(AxolotlStore store, AxolotlAddress remoteAddress) { + this(store, store, store, store, remoteAddress); } /** @@ -107,7 +103,7 @@ public class SessionBuilder { Optional<Integer> unsignedPreKeyId; - if (!identityKeyStore.isTrustedIdentity(recipientId, theirIdentityKey)) { + if (!identityKeyStore.isTrustedIdentity(remoteAddress.getName(), theirIdentityKey)) { throw new UntrustedIdentityException(); } @@ -117,7 +113,7 @@ public class SessionBuilder { default: throw new AssertionError("Unknown version: " + messageVersion); } - identityKeyStore.saveIdentity(recipientId, theirIdentityKey); + identityKeyStore.saveIdentity(remoteAddress.getName(), theirIdentityKey); return unsignedPreKeyId; } @@ -169,7 +165,7 @@ public class SessionBuilder { } if (!preKeyStore.containsPreKey(message.getPreKeyId().get()) && - sessionStore.containsSession(recipientId, deviceId)) + sessionStore.containsSession(remoteAddress)) { Log.w(TAG, "We've already processed the prekey part of this V2 session, letting bundled message fall through..."); return Optional.absent(); @@ -214,7 +210,7 @@ public class SessionBuilder { */ public void process(PreKeyBundle preKey) throws InvalidKeyException, UntrustedIdentityException { synchronized (SessionCipher.SESSION_LOCK) { - if (!identityKeyStore.isTrustedIdentity(recipientId, preKey.getIdentityKey())) { + if (!identityKeyStore.isTrustedIdentity(remoteAddress.getName(), preKey.getIdentityKey())) { throw new UntrustedIdentityException(); } @@ -231,7 +227,7 @@ public class SessionBuilder { } boolean supportsV3 = preKey.getSignedPreKey() != null; - SessionRecord sessionRecord = sessionStore.loadSession(recipientId, deviceId); + SessionRecord sessionRecord = sessionStore.loadSession(remoteAddress); ECKeyPair ourBaseKey = Curve.generateKeyPair(); ECPublicKey theirSignedPreKey = supportsV3 ? preKey.getSignedPreKey() : preKey.getPreKey(); Optional<ECPublicKey> theirOneTimePreKey = Optional.fromNullable(preKey.getPreKey()); @@ -258,8 +254,8 @@ public class SessionBuilder { sessionRecord.getSessionState().setRemoteRegistrationId(preKey.getRegistrationId()); sessionRecord.getSessionState().setAliceBaseKey(ourBaseKey.getPublicKey().serialize()); - sessionStore.storeSession(recipientId, deviceId, sessionRecord); - identityKeyStore.saveIdentity(recipientId, preKey.getIdentityKey()); + sessionStore.storeSession(remoteAddress, sessionRecord); + identityKeyStore.saveIdentity(remoteAddress.getName(), preKey.getIdentityKey()); } } @@ -275,7 +271,7 @@ public class SessionBuilder { throws InvalidKeyException, UntrustedIdentityException, StaleKeyExchangeException { synchronized (SessionCipher.SESSION_LOCK) { - if (!identityKeyStore.isTrustedIdentity(recipientId, message.getIdentityKey())) { + if (!identityKeyStore.isTrustedIdentity(remoteAddress.getName(), message.getIdentityKey())) { throw new UntrustedIdentityException(); } @@ -290,7 +286,7 @@ public class SessionBuilder { private KeyExchangeMessage processInitiate(KeyExchangeMessage message) throws InvalidKeyException { int flags = KeyExchangeMessage.RESPONSE_FLAG; - SessionRecord sessionRecord = sessionStore.loadSession(recipientId, deviceId); + SessionRecord sessionRecord = sessionStore.loadSession(remoteAddress); if (message.getVersion() >= 3 && !Curve.verifySignature(message.getIdentityKey().getPublicKey(), @@ -325,8 +321,8 @@ public class SessionBuilder { Math.min(message.getMaxVersion(), CiphertextMessage.CURRENT_VERSION), parameters); - sessionStore.storeSession(recipientId, deviceId, sessionRecord); - identityKeyStore.saveIdentity(recipientId, message.getIdentityKey()); + sessionStore.storeSession(remoteAddress, sessionRecord); + identityKeyStore.saveIdentity(remoteAddress.getName(), message.getIdentityKey()); byte[] baseKeySignature = Curve.calculateSignature(parameters.getOurIdentityKey().getPrivateKey(), parameters.getOurBaseKey().getPublicKey().serialize()); @@ -341,7 +337,7 @@ public class SessionBuilder { private void processResponse(KeyExchangeMessage message) throws StaleKeyExchangeException, InvalidKeyException { - SessionRecord sessionRecord = sessionStore.loadSession(recipientId, deviceId); + SessionRecord sessionRecord = sessionStore.loadSession(remoteAddress); SessionState sessionState = sessionRecord.getSessionState(); boolean hasPendingKeyExchange = sessionState.hasPendingKeyExchange(); boolean isSimultaneousInitiateResponse = message.isResponseForSimultaneousInitiate(); @@ -375,8 +371,8 @@ public class SessionBuilder { throw new InvalidKeyException("Base key signature doesn't match!"); } - sessionStore.storeSession(recipientId, deviceId, sessionRecord); - identityKeyStore.saveIdentity(recipientId, message.getIdentityKey()); + sessionStore.storeSession(remoteAddress, sessionRecord); + identityKeyStore.saveIdentity(remoteAddress.getName(), message.getIdentityKey()); } @@ -394,10 +390,10 @@ public class SessionBuilder { ECKeyPair ratchetKey = Curve.generateKeyPair(); IdentityKeyPair identityKey = identityKeyStore.getIdentityKeyPair(); byte[] baseKeySignature = Curve.calculateSignature(identityKey.getPrivateKey(), baseKey.getPublicKey().serialize()); - SessionRecord sessionRecord = sessionStore.loadSession(recipientId, deviceId); + SessionRecord sessionRecord = sessionStore.loadSession(remoteAddress); sessionRecord.getSessionState().setPendingKeyExchange(sequence, baseKey, ratchetKey, identityKey); - sessionStore.storeSession(recipientId, deviceId, sessionRecord); + sessionStore.storeSession(remoteAddress, sessionRecord); return new KeyExchangeMessage(2, sequence, flags, baseKey.getPublicKey(), baseKeySignature, ratchetKey.getPublicKey(), identityKey.getPublicKey()); |