From 7262e6970b86fe0a23ee48afca7bba37bcad1cdf Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Wed, 4 Mar 2015 18:49:20 -0800 Subject: Move in memory data structures into project. --- .../state/impl/InMemoryAxolotlStore.java | 120 +++++++++++++++++++++ .../state/impl/InMemoryIdentityKeyStore.java | 46 ++++++++ .../libaxolotl/state/impl/InMemoryPreKeyStore.java | 42 ++++++++ .../state/impl/InMemorySessionStore.java | 70 ++++++++++++ .../state/impl/InMemorySignedPreKeyStore.java | 59 ++++++++++ .../libaxolotl/InMemoryAxolotlStore.java | 112 ------------------- .../libaxolotl/InMemoryIdentityKeyStore.java | 51 --------- .../libaxolotl/InMemoryPreKeyStore.java | 41 ------- .../libaxolotl/InMemorySessionStore.java | 67 ------------ .../libaxolotl/InMemorySignedPreKeyStore.java | 58 ---------- .../libaxolotl/SessionBuilderTest.java | 42 ++++---- .../libaxolotl/SessionCipherTest.java | 4 +- .../libaxolotl/SimultaneousInitiateTests.java | 24 ++--- .../libaxolotl/TestInMemoryAxolotlStore.java | 22 ++++ .../libaxolotl/TestInMemoryIdentityKeyStore.java | 23 ++++ 15 files changed, 417 insertions(+), 364 deletions(-) create mode 100644 java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryAxolotlStore.java create mode 100644 java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryIdentityKeyStore.java create mode 100644 java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryPreKeyStore.java create mode 100644 java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemorySessionStore.java create mode 100644 java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemorySignedPreKeyStore.java delete mode 100644 tests/src/test/java/org/whispersystems/libaxolotl/InMemoryAxolotlStore.java delete mode 100644 tests/src/test/java/org/whispersystems/libaxolotl/InMemoryIdentityKeyStore.java delete mode 100644 tests/src/test/java/org/whispersystems/libaxolotl/InMemoryPreKeyStore.java delete mode 100644 tests/src/test/java/org/whispersystems/libaxolotl/InMemorySessionStore.java delete mode 100644 tests/src/test/java/org/whispersystems/libaxolotl/InMemorySignedPreKeyStore.java create mode 100644 tests/src/test/java/org/whispersystems/libaxolotl/TestInMemoryAxolotlStore.java create mode 100644 tests/src/test/java/org/whispersystems/libaxolotl/TestInMemoryIdentityKeyStore.java diff --git a/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryAxolotlStore.java b/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryAxolotlStore.java new file mode 100644 index 00000000..4e3c818d --- /dev/null +++ b/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryAxolotlStore.java @@ -0,0 +1,120 @@ +package org.whispersystems.libaxolotl.state.impl; + +import org.whispersystems.libaxolotl.AxolotlAddress; +import org.whispersystems.libaxolotl.IdentityKey; +import org.whispersystems.libaxolotl.IdentityKeyPair; +import org.whispersystems.libaxolotl.InvalidKeyIdException; +import org.whispersystems.libaxolotl.state.AxolotlStore; +import org.whispersystems.libaxolotl.state.PreKeyRecord; +import org.whispersystems.libaxolotl.state.SessionRecord; +import org.whispersystems.libaxolotl.state.SignedPreKeyRecord; + +import java.util.List; + +public class InMemoryAxolotlStore implements AxolotlStore { + + private final InMemoryPreKeyStore preKeyStore = new InMemoryPreKeyStore(); + private final InMemorySessionStore sessionStore = new InMemorySessionStore(); + private final InMemorySignedPreKeyStore signedPreKeyStore = new InMemorySignedPreKeyStore(); + + private final InMemoryIdentityKeyStore identityKeyStore; + + public InMemoryAxolotlStore(IdentityKeyPair identityKeyPair, int registrationId) { + this.identityKeyStore = new InMemoryIdentityKeyStore(identityKeyPair, registrationId); + } + + @Override + public IdentityKeyPair getIdentityKeyPair() { + return identityKeyStore.getIdentityKeyPair(); + } + + @Override + public int getLocalRegistrationId() { + return identityKeyStore.getLocalRegistrationId(); + } + + @Override + public void saveIdentity(String name, IdentityKey identityKey) { + identityKeyStore.saveIdentity(name, identityKey); + } + + @Override + public boolean isTrustedIdentity(String name, IdentityKey identityKey) { + return identityKeyStore.isTrustedIdentity(name, identityKey); + } + + @Override + public PreKeyRecord loadPreKey(int preKeyId) throws InvalidKeyIdException { + return preKeyStore.loadPreKey(preKeyId); + } + + @Override + public void storePreKey(int preKeyId, PreKeyRecord record) { + preKeyStore.storePreKey(preKeyId, record); + } + + @Override + public boolean containsPreKey(int preKeyId) { + return preKeyStore.containsPreKey(preKeyId); + } + + @Override + public void removePreKey(int preKeyId) { + preKeyStore.removePreKey(preKeyId); + } + + @Override + public SessionRecord loadSession(AxolotlAddress address) { + return sessionStore.loadSession(address); + } + + @Override + public List getSubDeviceSessions(String name) { + return sessionStore.getSubDeviceSessions(name); + } + + @Override + public void storeSession(AxolotlAddress address, SessionRecord record) { + sessionStore.storeSession(address, record); + } + + @Override + public boolean containsSession(AxolotlAddress address) { + return sessionStore.containsSession(address); + } + + @Override + public void deleteSession(AxolotlAddress address) { + sessionStore.deleteSession(address); + } + + @Override + public void deleteAllSessions(String name) { + sessionStore.deleteAllSessions(name); + } + + @Override + public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException { + return signedPreKeyStore.loadSignedPreKey(signedPreKeyId); + } + + @Override + public List loadSignedPreKeys() { + return signedPreKeyStore.loadSignedPreKeys(); + } + + @Override + public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) { + signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record); + } + + @Override + public boolean containsSignedPreKey(int signedPreKeyId) { + return signedPreKeyStore.containsSignedPreKey(signedPreKeyId); + } + + @Override + public void removeSignedPreKey(int signedPreKeyId) { + signedPreKeyStore.removeSignedPreKey(signedPreKeyId); + } +} diff --git a/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryIdentityKeyStore.java b/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryIdentityKeyStore.java new file mode 100644 index 00000000..b1b04e08 --- /dev/null +++ b/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryIdentityKeyStore.java @@ -0,0 +1,46 @@ +package org.whispersystems.libaxolotl.state.impl; + +import org.whispersystems.libaxolotl.IdentityKey; +import org.whispersystems.libaxolotl.IdentityKeyPair; +import org.whispersystems.libaxolotl.ecc.Curve; +import org.whispersystems.libaxolotl.ecc.ECKeyPair; +import org.whispersystems.libaxolotl.state.IdentityKeyStore; + +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.util.HashMap; +import java.util.Map; + +public class InMemoryIdentityKeyStore implements IdentityKeyStore { + + private final Map trustedKeys = new HashMap<>(); + + private final IdentityKeyPair identityKeyPair; + private final int localRegistrationId; + + public InMemoryIdentityKeyStore(IdentityKeyPair identityKeyPair, int localRegistrationId) { + this.identityKeyPair = identityKeyPair; + this.localRegistrationId = localRegistrationId; + } + + @Override + public IdentityKeyPair getIdentityKeyPair() { + return identityKeyPair; + } + + @Override + public int getLocalRegistrationId() { + return localRegistrationId; + } + + @Override + public void saveIdentity(String name, IdentityKey identityKey) { + trustedKeys.put(name, identityKey); + } + + @Override + public boolean isTrustedIdentity(String name, IdentityKey identityKey) { + IdentityKey trusted = trustedKeys.get(name); + return (trusted == null || trusted.equals(identityKey)); + } +} diff --git a/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryPreKeyStore.java b/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryPreKeyStore.java new file mode 100644 index 00000000..80651f21 --- /dev/null +++ b/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemoryPreKeyStore.java @@ -0,0 +1,42 @@ +package org.whispersystems.libaxolotl.state.impl; + +import org.whispersystems.libaxolotl.InvalidKeyIdException; +import org.whispersystems.libaxolotl.state.PreKeyRecord; +import org.whispersystems.libaxolotl.state.PreKeyStore; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class InMemoryPreKeyStore implements PreKeyStore { + + private final Map store = new HashMap<>(); + + @Override + public PreKeyRecord loadPreKey(int preKeyId) throws InvalidKeyIdException { + try { + if (!store.containsKey(preKeyId)) { + throw new InvalidKeyIdException("No such prekeyrecord!"); + } + + return new PreKeyRecord(store.get(preKeyId)); + } catch (IOException e) { + throw new AssertionError(e); + } + } + + @Override + public void storePreKey(int preKeyId, PreKeyRecord record) { + store.put(preKeyId, record.serialize()); + } + + @Override + public boolean containsPreKey(int preKeyId) { + return store.containsKey(preKeyId); + } + + @Override + public void removePreKey(int preKeyId) { + store.remove(preKeyId); + } +} diff --git a/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemorySessionStore.java b/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemorySessionStore.java new file mode 100644 index 00000000..f5270c7d --- /dev/null +++ b/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemorySessionStore.java @@ -0,0 +1,70 @@ +package org.whispersystems.libaxolotl.state.impl; + +import org.whispersystems.libaxolotl.AxolotlAddress; +import org.whispersystems.libaxolotl.state.SessionRecord; +import org.whispersystems.libaxolotl.state.SessionStore; + +import java.io.IOException; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class InMemorySessionStore implements SessionStore { + + private Map sessions = new HashMap<>(); + + public InMemorySessionStore() {} + + @Override + public synchronized SessionRecord loadSession(AxolotlAddress remoteAddress) { + try { + if (containsSession(remoteAddress)) { + return new SessionRecord(sessions.get(remoteAddress)); + } else { + return new SessionRecord(); + } + } catch (IOException e) { + throw new AssertionError(e); + } + } + + @Override + public synchronized List getSubDeviceSessions(String name) { + List deviceIds = new LinkedList<>(); + + for (AxolotlAddress key : sessions.keySet()) { + if (key.getName().equals(name) && + key.getDeviceId() != 1) + { + deviceIds.add(key.getDeviceId()); + } + } + + return deviceIds; + } + + @Override + public synchronized void storeSession(AxolotlAddress address, SessionRecord record) { + sessions.put(address, record.serialize()); + } + + @Override + public synchronized boolean containsSession(AxolotlAddress address) { + return sessions.containsKey(address); + } + + @Override + public synchronized void deleteSession(AxolotlAddress address) { + sessions.remove(address); + } + + @Override + public synchronized void deleteAllSessions(String name) { + for (AxolotlAddress key : sessions.keySet()) { + if (key.getName().equals(name)) { + sessions.remove(key); + } + } + } +} diff --git a/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemorySignedPreKeyStore.java b/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemorySignedPreKeyStore.java new file mode 100644 index 00000000..bab83137 --- /dev/null +++ b/java/src/main/java/org/whispersystems/libaxolotl/state/impl/InMemorySignedPreKeyStore.java @@ -0,0 +1,59 @@ +package org.whispersystems.libaxolotl.state.impl; + +import org.whispersystems.libaxolotl.InvalidKeyIdException; +import org.whispersystems.libaxolotl.state.SignedPreKeyRecord; +import org.whispersystems.libaxolotl.state.SignedPreKeyStore; + +import java.io.IOException; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class InMemorySignedPreKeyStore implements SignedPreKeyStore { + + private final Map store = new HashMap<>(); + + @Override + public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException { + try { + if (!store.containsKey(signedPreKeyId)) { + throw new InvalidKeyIdException("No such signedprekeyrecord! " + signedPreKeyId); + } + + return new SignedPreKeyRecord(store.get(signedPreKeyId)); + } catch (IOException e) { + throw new AssertionError(e); + } + } + + @Override + public List loadSignedPreKeys() { + try { + List results = new LinkedList<>(); + + for (byte[] serialized : store.values()) { + results.add(new SignedPreKeyRecord(serialized)); + } + + return results; + } catch (IOException e) { + throw new AssertionError(e); + } + } + + @Override + public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) { + store.put(signedPreKeyId, record.serialize()); + } + + @Override + public boolean containsSignedPreKey(int signedPreKeyId) { + return store.containsKey(signedPreKeyId); + } + + @Override + public void removeSignedPreKey(int signedPreKeyId) { + store.remove(signedPreKeyId); + } +} diff --git a/tests/src/test/java/org/whispersystems/libaxolotl/InMemoryAxolotlStore.java b/tests/src/test/java/org/whispersystems/libaxolotl/InMemoryAxolotlStore.java deleted file mode 100644 index f4bf38eb..00000000 --- a/tests/src/test/java/org/whispersystems/libaxolotl/InMemoryAxolotlStore.java +++ /dev/null @@ -1,112 +0,0 @@ -package org.whispersystems.libaxolotl; - -import org.whispersystems.libaxolotl.state.AxolotlStore; -import org.whispersystems.libaxolotl.state.PreKeyRecord; -import org.whispersystems.libaxolotl.state.SessionRecord; -import org.whispersystems.libaxolotl.state.SignedPreKeyRecord; - -import java.util.List; - -public class InMemoryAxolotlStore implements AxolotlStore { - - private final InMemoryIdentityKeyStore identityKeyStore = new InMemoryIdentityKeyStore(); - private final InMemoryPreKeyStore preKeyStore = new InMemoryPreKeyStore(); - private final InMemorySessionStore sessionStore = new InMemorySessionStore(); - private final InMemorySignedPreKeyStore signedPreKeyStore = new InMemorySignedPreKeyStore(); - - - @Override - public IdentityKeyPair getIdentityKeyPair() { - return identityKeyStore.getIdentityKeyPair(); - } - - @Override - public int getLocalRegistrationId() { - return identityKeyStore.getLocalRegistrationId(); - } - - @Override - public void saveIdentity(String name, IdentityKey identityKey) { - identityKeyStore.saveIdentity(name, identityKey); - } - - @Override - public boolean isTrustedIdentity(String name, IdentityKey identityKey) { - return identityKeyStore.isTrustedIdentity(name, identityKey); - } - - @Override - public PreKeyRecord loadPreKey(int preKeyId) throws InvalidKeyIdException { - return preKeyStore.loadPreKey(preKeyId); - } - - @Override - public void storePreKey(int preKeyId, PreKeyRecord record) { - preKeyStore.storePreKey(preKeyId, record); - } - - @Override - public boolean containsPreKey(int preKeyId) { - return preKeyStore.containsPreKey(preKeyId); - } - - @Override - public void removePreKey(int preKeyId) { - preKeyStore.removePreKey(preKeyId); - } - - @Override - public SessionRecord loadSession(AxolotlAddress address) { - return sessionStore.loadSession(address); - } - - @Override - public List getSubDeviceSessions(String name) { - return sessionStore.getSubDeviceSessions(name); - } - - @Override - public void storeSession(AxolotlAddress address, SessionRecord record) { - sessionStore.storeSession(address, record); - } - - @Override - public boolean containsSession(AxolotlAddress address) { - return sessionStore.containsSession(address); - } - - @Override - public void deleteSession(AxolotlAddress address) { - sessionStore.deleteSession(address); - } - - @Override - public void deleteAllSessions(String name) { - sessionStore.deleteAllSessions(name); - } - - @Override - public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException { - return signedPreKeyStore.loadSignedPreKey(signedPreKeyId); - } - - @Override - public List loadSignedPreKeys() { - return signedPreKeyStore.loadSignedPreKeys(); - } - - @Override - public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) { - signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record); - } - - @Override - public boolean containsSignedPreKey(int signedPreKeyId) { - return signedPreKeyStore.containsSignedPreKey(signedPreKeyId); - } - - @Override - public void removeSignedPreKey(int signedPreKeyId) { - signedPreKeyStore.removeSignedPreKey(signedPreKeyId); - } -} diff --git a/tests/src/test/java/org/whispersystems/libaxolotl/InMemoryIdentityKeyStore.java b/tests/src/test/java/org/whispersystems/libaxolotl/InMemoryIdentityKeyStore.java deleted file mode 100644 index bf6dc318..00000000 --- a/tests/src/test/java/org/whispersystems/libaxolotl/InMemoryIdentityKeyStore.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.whispersystems.libaxolotl; - -import org.whispersystems.libaxolotl.ecc.Curve; -import org.whispersystems.libaxolotl.ecc.ECKeyPair; -import org.whispersystems.libaxolotl.state.IdentityKeyStore; - -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.util.HashMap; -import java.util.Map; - -public class InMemoryIdentityKeyStore implements IdentityKeyStore { - - private final Map trustedKeys = new HashMap<>(); - - private final IdentityKeyPair identityKeyPair; - private final int localRegistrationId; - - public InMemoryIdentityKeyStore() { - try { - ECKeyPair identityKeyPairKeys = Curve.generateKeyPair(); - - this.identityKeyPair = new IdentityKeyPair(new IdentityKey(identityKeyPairKeys.getPublicKey()), - identityKeyPairKeys.getPrivateKey()); - this.localRegistrationId = SecureRandom.getInstance("SHA1PRNG").nextInt(16380) + 1; - } catch (NoSuchAlgorithmException e) { - throw new AssertionError(e); - } - } - - @Override - public IdentityKeyPair getIdentityKeyPair() { - return identityKeyPair; - } - - @Override - public int getLocalRegistrationId() { - return localRegistrationId; - } - - @Override - public void saveIdentity(String name, IdentityKey identityKey) { - trustedKeys.put(name, identityKey); - } - - @Override - public boolean isTrustedIdentity(String name, IdentityKey identityKey) { - IdentityKey trusted = trustedKeys.get(name); - return (trusted == null || trusted.equals(identityKey)); - } -} diff --git a/tests/src/test/java/org/whispersystems/libaxolotl/InMemoryPreKeyStore.java b/tests/src/test/java/org/whispersystems/libaxolotl/InMemoryPreKeyStore.java deleted file mode 100644 index a2ea6811..00000000 --- a/tests/src/test/java/org/whispersystems/libaxolotl/InMemoryPreKeyStore.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.whispersystems.libaxolotl; - -import org.whispersystems.libaxolotl.state.PreKeyRecord; -import org.whispersystems.libaxolotl.state.PreKeyStore; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -public class InMemoryPreKeyStore implements PreKeyStore { - - private final Map store = new HashMap<>(); - - @Override - public PreKeyRecord loadPreKey(int preKeyId) throws InvalidKeyIdException { - try { - if (!store.containsKey(preKeyId)) { - throw new InvalidKeyIdException("No such prekeyrecord!"); - } - - return new PreKeyRecord(store.get(preKeyId)); - } catch (IOException e) { - throw new AssertionError(e); - } - } - - @Override - public void storePreKey(int preKeyId, PreKeyRecord record) { - store.put(preKeyId, record.serialize()); - } - - @Override - public boolean containsPreKey(int preKeyId) { - return store.containsKey(preKeyId); - } - - @Override - public void removePreKey(int preKeyId) { - store.remove(preKeyId); - } -} diff --git a/tests/src/test/java/org/whispersystems/libaxolotl/InMemorySessionStore.java b/tests/src/test/java/org/whispersystems/libaxolotl/InMemorySessionStore.java deleted file mode 100644 index f707773e..00000000 --- a/tests/src/test/java/org/whispersystems/libaxolotl/InMemorySessionStore.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.whispersystems.libaxolotl; - -import org.whispersystems.libaxolotl.state.SessionRecord; -import org.whispersystems.libaxolotl.state.SessionStore; - -import java.io.IOException; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public class InMemorySessionStore implements SessionStore { - - private Map sessions = new HashMap<>(); - - public InMemorySessionStore() {} - - @Override - public synchronized SessionRecord loadSession(AxolotlAddress remoteAddress) { - try { - if (containsSession(remoteAddress)) { - return new SessionRecord(sessions.get(remoteAddress)); - } else { - return new SessionRecord(); - } - } catch (IOException e) { - throw new AssertionError(e); - } - } - - @Override - public synchronized List getSubDeviceSessions(String name) { - List deviceIds = new LinkedList<>(); - - for (AxolotlAddress key : sessions.keySet()) { - if (key.getName().equals(name)) { - deviceIds.add(key.getDeviceId()); - } - } - - return deviceIds; - } - - @Override - public synchronized void storeSession(AxolotlAddress address, SessionRecord record) { - sessions.put(address, record.serialize()); - } - - @Override - public synchronized boolean containsSession(AxolotlAddress address) { - return sessions.containsKey(address); - } - - @Override - public synchronized void deleteSession(AxolotlAddress address) { - sessions.remove(address); - } - - @Override - public synchronized void deleteAllSessions(String name) { - for (AxolotlAddress key : sessions.keySet()) { - if (key.getName().equals(name)) { - sessions.remove(key); - } - } - } -} diff --git a/tests/src/test/java/org/whispersystems/libaxolotl/InMemorySignedPreKeyStore.java b/tests/src/test/java/org/whispersystems/libaxolotl/InMemorySignedPreKeyStore.java deleted file mode 100644 index 9f452d6d..00000000 --- a/tests/src/test/java/org/whispersystems/libaxolotl/InMemorySignedPreKeyStore.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.whispersystems.libaxolotl; - -import org.whispersystems.libaxolotl.state.SignedPreKeyRecord; -import org.whispersystems.libaxolotl.state.SignedPreKeyStore; - -import java.io.IOException; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public class InMemorySignedPreKeyStore implements SignedPreKeyStore { - - private final Map store = new HashMap<>(); - - @Override - public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException { - try { - if (!store.containsKey(signedPreKeyId)) { - throw new InvalidKeyIdException("No such signedprekeyrecord! " + signedPreKeyId); - } - - return new SignedPreKeyRecord(store.get(signedPreKeyId)); - } catch (IOException e) { - throw new AssertionError(e); - } - } - - @Override - public List loadSignedPreKeys() { - try { - List results = new LinkedList<>(); - - for (byte[] serialized : store.values()) { - results.add(new SignedPreKeyRecord(serialized)); - } - - return results; - } catch (IOException e) { - throw new AssertionError(e); - } - } - - @Override - public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) { - store.put(signedPreKeyId, record.serialize()); - } - - @Override - public boolean containsSignedPreKey(int signedPreKeyId) { - return store.containsKey(signedPreKeyId); - } - - @Override - public void removeSignedPreKey(int signedPreKeyId) { - store.remove(signedPreKeyId); - } -} diff --git a/tests/src/test/java/org/whispersystems/libaxolotl/SessionBuilderTest.java b/tests/src/test/java/org/whispersystems/libaxolotl/SessionBuilderTest.java index 539131f0..1105ffc6 100644 --- a/tests/src/test/java/org/whispersystems/libaxolotl/SessionBuilderTest.java +++ b/tests/src/test/java/org/whispersystems/libaxolotl/SessionBuilderTest.java @@ -25,10 +25,10 @@ public class SessionBuilderTest extends TestCase { public void testBasicPreKeyV2() throws InvalidKeyException, InvalidVersionException, InvalidMessageException, InvalidKeyIdException, DuplicateMessageException, LegacyMessageException, UntrustedIdentityException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); ECKeyPair bobPreKeyPair = Curve.generateKeyPair(); PreKeyBundle bobPreKey = new PreKeyBundle(bobStore.getLocalRegistrationId(), 1, 31337, bobPreKeyPair.getPublicKey(), @@ -64,7 +64,7 @@ public class SessionBuilderTest extends TestCase { runInteraction(aliceStore, bobStore); - aliceStore = new InMemoryAxolotlStore(); + aliceStore = new TestInMemoryAxolotlStore(); aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS); @@ -104,10 +104,10 @@ public class SessionBuilderTest extends TestCase { public void testBasicPreKeyV3() throws InvalidKeyException, InvalidVersionException, InvalidMessageException, InvalidKeyIdException, DuplicateMessageException, LegacyMessageException, UntrustedIdentityException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); - final AxolotlStore bobStore = new InMemoryAxolotlStore(); + final AxolotlStore bobStore = new TestInMemoryAxolotlStore(); ECKeyPair bobPreKeyPair = Curve.generateKeyPair(); ECKeyPair bobSignedPreKeyPair = Curve.generateKeyPair(); byte[] bobSignedPreKeySignature = Curve.calculateSignature(bobStore.getIdentityKeyPair().getPrivateKey(), @@ -156,7 +156,7 @@ public class SessionBuilderTest extends TestCase { runInteraction(aliceStore, bobStore); - aliceStore = new InMemoryAxolotlStore(); + aliceStore = new TestInMemoryAxolotlStore(); aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); aliceSessionCipher = new SessionCipher(aliceStore, BOB_ADDRESS); @@ -198,10 +198,10 @@ public class SessionBuilderTest extends TestCase { } public void testBadSignedPreKeySignature() throws InvalidKeyException, UntrustedIdentityException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); - IdentityKeyStore bobIdentityKeyStore = new InMemoryIdentityKeyStore(); + IdentityKeyStore bobIdentityKeyStore = new TestInMemoryIdentityKeyStore(); ECKeyPair bobPreKeyPair = Curve.generateKeyPair(); ECKeyPair bobSignedPreKeyPair = Curve.generateKeyPair(); @@ -237,10 +237,10 @@ public class SessionBuilderTest extends TestCase { } public void testRepeatBundleMessageV2() throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException, InvalidMessageException, InvalidKeyIdException, DuplicateMessageException, LegacyMessageException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); ECKeyPair bobPreKeyPair = Curve.generateKeyPair(); ECKeyPair bobSignedPreKeyPair = Curve.generateKeyPair(); @@ -290,10 +290,10 @@ public class SessionBuilderTest extends TestCase { } public void testRepeatBundleMessageV3() throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException, InvalidMessageException, InvalidKeyIdException, DuplicateMessageException, LegacyMessageException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); ECKeyPair bobPreKeyPair = Curve.generateKeyPair(); ECKeyPair bobSignedPreKeyPair = Curve.generateKeyPair(); @@ -344,10 +344,10 @@ public class SessionBuilderTest extends TestCase { } public void testBadMessageBundle() throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException, InvalidMessageException, DuplicateMessageException, LegacyMessageException, InvalidKeyIdException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); ECKeyPair bobPreKeyPair = Curve.generateKeyPair(); ECKeyPair bobSignedPreKeyPair = Curve.generateKeyPair(); @@ -397,10 +397,10 @@ public class SessionBuilderTest extends TestCase { } public void testBasicKeyExchange() throws InvalidKeyException, LegacyMessageException, InvalidMessageException, DuplicateMessageException, UntrustedIdentityException, StaleKeyExchangeException, InvalidVersionException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); SessionBuilder bobSessionBuilder = new SessionBuilder(bobStore, ALICE_ADDRESS); KeyExchangeMessage aliceKeyExchangeMessage = aliceSessionBuilder.process(); @@ -420,7 +420,7 @@ public class SessionBuilderTest extends TestCase { runInteraction(aliceStore, bobStore); - aliceStore = new InMemoryAxolotlStore(); + aliceStore = new TestInMemoryAxolotlStore(); aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); aliceKeyExchangeMessage = aliceSessionBuilder.process(); @@ -439,10 +439,10 @@ public class SessionBuilderTest extends TestCase { public void testSimultaneousKeyExchange() throws InvalidKeyException, DuplicateMessageException, LegacyMessageException, InvalidMessageException, UntrustedIdentityException, StaleKeyExchangeException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); SessionBuilder bobSessionBuilder = new SessionBuilder(bobStore, ALICE_ADDRESS); KeyExchangeMessage aliceKeyExchange = aliceSessionBuilder.process(); @@ -467,10 +467,10 @@ public class SessionBuilderTest extends TestCase { } public void testOptionalOneTimePreKey() throws Exception { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); ECKeyPair bobPreKeyPair = Curve.generateKeyPair(); ECKeyPair bobSignedPreKeyPair = Curve.generateKeyPair(); diff --git a/tests/src/test/java/org/whispersystems/libaxolotl/SessionCipherTest.java b/tests/src/test/java/org/whispersystems/libaxolotl/SessionCipherTest.java index a3b5db4c..41731982 100644 --- a/tests/src/test/java/org/whispersystems/libaxolotl/SessionCipherTest.java +++ b/tests/src/test/java/org/whispersystems/libaxolotl/SessionCipherTest.java @@ -49,8 +49,8 @@ public class SessionCipherTest extends TestCase { private void runInteraction(SessionRecord aliceSessionRecord, SessionRecord bobSessionRecord) throws DuplicateMessageException, LegacyMessageException, InvalidMessageException, NoSuchAlgorithmException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); aliceStore.storeSession(new AxolotlAddress("+14159999999", 1), aliceSessionRecord); bobStore.storeSession(new AxolotlAddress("+14158888888", 1), bobSessionRecord); diff --git a/tests/src/test/java/org/whispersystems/libaxolotl/SimultaneousInitiateTests.java b/tests/src/test/java/org/whispersystems/libaxolotl/SimultaneousInitiateTests.java index 6e68508c..90e56d94 100644 --- a/tests/src/test/java/org/whispersystems/libaxolotl/SimultaneousInitiateTests.java +++ b/tests/src/test/java/org/whispersystems/libaxolotl/SimultaneousInitiateTests.java @@ -32,8 +32,8 @@ public class SimultaneousInitiateTests extends TestCase { InvalidMessageException, DuplicateMessageException, LegacyMessageException, InvalidKeyIdException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); PreKeyBundle alicePreKeyBundle = createAlicePreKeyBundle(aliceStore); PreKeyBundle bobPreKeyBundle = createBobPreKeyBundle(bobStore); @@ -86,8 +86,8 @@ public class SimultaneousInitiateTests extends TestCase { } public void testLostSimultaneousInitiate() throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException, InvalidMessageException, DuplicateMessageException, LegacyMessageException, InvalidKeyIdException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); PreKeyBundle alicePreKeyBundle = createAlicePreKeyBundle(aliceStore); PreKeyBundle bobPreKeyBundle = createBobPreKeyBundle(bobStore); @@ -138,8 +138,8 @@ public class SimultaneousInitiateTests extends TestCase { InvalidMessageException, DuplicateMessageException, LegacyMessageException, InvalidKeyIdException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); PreKeyBundle alicePreKeyBundle = createAlicePreKeyBundle(aliceStore); PreKeyBundle bobPreKeyBundle = createBobPreKeyBundle(bobStore); @@ -197,8 +197,8 @@ public class SimultaneousInitiateTests extends TestCase { InvalidMessageException, DuplicateMessageException, LegacyMessageException, InvalidKeyIdException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); PreKeyBundle alicePreKeyBundle = createAlicePreKeyBundle(aliceStore); PreKeyBundle bobPreKeyBundle = createBobPreKeyBundle(bobStore); @@ -273,8 +273,8 @@ public class SimultaneousInitiateTests extends TestCase { InvalidMessageException, DuplicateMessageException, LegacyMessageException, InvalidKeyIdException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); @@ -352,8 +352,8 @@ public class SimultaneousInitiateTests extends TestCase { InvalidMessageException, DuplicateMessageException, LegacyMessageException, InvalidKeyIdException, NoSessionException { - AxolotlStore aliceStore = new InMemoryAxolotlStore(); - AxolotlStore bobStore = new InMemoryAxolotlStore(); + AxolotlStore aliceStore = new TestInMemoryAxolotlStore(); + AxolotlStore bobStore = new TestInMemoryAxolotlStore(); SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS); diff --git a/tests/src/test/java/org/whispersystems/libaxolotl/TestInMemoryAxolotlStore.java b/tests/src/test/java/org/whispersystems/libaxolotl/TestInMemoryAxolotlStore.java new file mode 100644 index 00000000..8c4700fc --- /dev/null +++ b/tests/src/test/java/org/whispersystems/libaxolotl/TestInMemoryAxolotlStore.java @@ -0,0 +1,22 @@ +package org.whispersystems.libaxolotl; + +import org.whispersystems.libaxolotl.ecc.Curve; +import org.whispersystems.libaxolotl.ecc.ECKeyPair; +import org.whispersystems.libaxolotl.util.KeyHelper; + +public class TestInMemoryAxolotlStore extends org.whispersystems.libaxolotl.state.impl.InMemoryAxolotlStore { + public TestInMemoryAxolotlStore() { + super(generateIdentityKeyPair(), generateRegistrationId()); + } + + private static IdentityKeyPair generateIdentityKeyPair() { + ECKeyPair identityKeyPairKeys = Curve.generateKeyPair(); + + return new IdentityKeyPair(new IdentityKey(identityKeyPairKeys.getPublicKey()), + identityKeyPairKeys.getPrivateKey()); + } + + private static int generateRegistrationId() { + return KeyHelper.generateRegistrationId(false); + } +} diff --git a/tests/src/test/java/org/whispersystems/libaxolotl/TestInMemoryIdentityKeyStore.java b/tests/src/test/java/org/whispersystems/libaxolotl/TestInMemoryIdentityKeyStore.java new file mode 100644 index 00000000..10a206d3 --- /dev/null +++ b/tests/src/test/java/org/whispersystems/libaxolotl/TestInMemoryIdentityKeyStore.java @@ -0,0 +1,23 @@ +package org.whispersystems.libaxolotl; + +import org.whispersystems.libaxolotl.ecc.Curve; +import org.whispersystems.libaxolotl.ecc.ECKeyPair; +import org.whispersystems.libaxolotl.util.KeyHelper; + +public class TestInMemoryIdentityKeyStore extends org.whispersystems.libaxolotl.state.impl.InMemoryIdentityKeyStore { + public TestInMemoryIdentityKeyStore() { + super(generateIdentityKeyPair(), generateRegistrationId()); + } + + private static IdentityKeyPair generateIdentityKeyPair() { + ECKeyPair identityKeyPairKeys = Curve.generateKeyPair(); + + return new IdentityKeyPair(new IdentityKey(identityKeyPairKeys.getPublicKey()), + identityKeyPairKeys.getPrivateKey()); + } + + private static int generateRegistrationId() { + return KeyHelper.generateRegistrationId(false); + } + +} -- cgit v1.2.3