From 5e561f410a90fe1de532b9a054554e877782f311 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 24 Oct 2024 17:47:20 +0200 Subject: [PATCH] Migrate from libsignal-protocol-java to libsignal --- smack-omemo-signal/build.gradle | 2 +- .../omemo/signal/SignalCachingOmemoStore.java | 18 ++--- .../signal/SignalFileBasedOmemoStore.java | 18 ++--- .../omemo/signal/SignalOmemoKeyUtil.java | 79 +++++++++++++------ .../omemo/signal/SignalOmemoRatchet.java | 53 +++++++------ .../omemo/signal/SignalOmemoService.java | 26 +++--- .../smackx/omemo/signal/SignalOmemoStore.java | 18 ++--- .../signal/SignalOmemoStoreConnector.java | 35 ++++---- .../omemo/LegacySignalOmemoKeyUtilTest.java | 6 +- .../smackx/omemo/SignalOmemoKeyUtilTest.java | 14 ++-- .../smackx/omemo/SignalOmemoStoreTest.java | 18 ++--- 11 files changed, 170 insertions(+), 117 deletions(-) diff --git a/smack-omemo-signal/build.gradle b/smack-omemo-signal/build.gradle index aa0887f94..dd5fd4602 100644 --- a/smack-omemo-signal/build.gradle +++ b/smack-omemo-signal/build.gradle @@ -11,7 +11,7 @@ dependencies { api project(":smack-im") api project(":smack-extensions") api project(":smack-omemo") - implementation 'org.whispersystems:signal-protocol-java:2.8.1' + implementation 'org.signal:libsignal-client:0.26.0' // TODO: Migrate Junit4 tests to Junit5. testImplementation "org.junit.vintage:junit-vintage-engine:$junitVersion" diff --git a/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalCachingOmemoStore.java b/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalCachingOmemoStore.java index 135bd96fb..eafd0fb2d 100644 --- a/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalCachingOmemoStore.java +++ b/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalCachingOmemoStore.java @@ -23,15 +23,15 @@ package org.jivesoftware.smackx.omemo.signal; import org.jivesoftware.smackx.omemo.CachingOmemoStore; import org.jivesoftware.smackx.omemo.OmemoStore; -import org.whispersystems.libsignal.IdentityKey; -import org.whispersystems.libsignal.IdentityKeyPair; -import org.whispersystems.libsignal.SessionCipher; -import org.whispersystems.libsignal.SignalProtocolAddress; -import org.whispersystems.libsignal.ecc.ECPublicKey; -import org.whispersystems.libsignal.state.PreKeyBundle; -import org.whispersystems.libsignal.state.PreKeyRecord; -import org.whispersystems.libsignal.state.SessionRecord; -import org.whispersystems.libsignal.state.SignedPreKeyRecord; +import org.signal.libsignal.protocol.IdentityKey; +import org.signal.libsignal.protocol.IdentityKeyPair; +import org.signal.libsignal.protocol.SessionCipher; +import org.signal.libsignal.protocol.SignalProtocolAddress; +import org.signal.libsignal.protocol.ecc.ECPublicKey; +import org.signal.libsignal.protocol.state.PreKeyBundle; +import org.signal.libsignal.protocol.state.PreKeyRecord; +import org.signal.libsignal.protocol.state.SessionRecord; +import org.signal.libsignal.protocol.state.SignedPreKeyRecord; /** * Implementation of the CachingOmemoStore for smack-omemo-signal. diff --git a/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalFileBasedOmemoStore.java b/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalFileBasedOmemoStore.java index f474b9b19..9cce37dfb 100644 --- a/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalFileBasedOmemoStore.java +++ b/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalFileBasedOmemoStore.java @@ -25,15 +25,15 @@ import java.io.File; import org.jivesoftware.smackx.omemo.FileBasedOmemoStore; import org.jivesoftware.smackx.omemo.util.OmemoKeyUtil; -import org.whispersystems.libsignal.IdentityKey; -import org.whispersystems.libsignal.IdentityKeyPair; -import org.whispersystems.libsignal.SessionCipher; -import org.whispersystems.libsignal.SignalProtocolAddress; -import org.whispersystems.libsignal.ecc.ECPublicKey; -import org.whispersystems.libsignal.state.PreKeyBundle; -import org.whispersystems.libsignal.state.PreKeyRecord; -import org.whispersystems.libsignal.state.SessionRecord; -import org.whispersystems.libsignal.state.SignedPreKeyRecord; +import org.signal.libsignal.protocol.IdentityKey; +import org.signal.libsignal.protocol.IdentityKeyPair; +import org.signal.libsignal.protocol.SessionCipher; +import org.signal.libsignal.protocol.SignalProtocolAddress; +import org.signal.libsignal.protocol.ecc.ECPublicKey; +import org.signal.libsignal.protocol.state.PreKeyBundle; +import org.signal.libsignal.protocol.state.PreKeyRecord; +import org.signal.libsignal.protocol.state.SessionRecord; +import org.signal.libsignal.protocol.state.SignedPreKeyRecord; /** * Implementation of a FileBasedOmemoStore for the smack-omemo-signal module. diff --git a/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalOmemoKeyUtil.java b/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalOmemoKeyUtil.java index c4e5a78d7..dec7bff16 100644 --- a/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalOmemoKeyUtil.java +++ b/smack-omemo-signal/src/main/java/org/jivesoftware/smackx/omemo/signal/SignalOmemoKeyUtil.java @@ -21,6 +21,7 @@ package org.jivesoftware.smackx.omemo.signal; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.TreeMap; @@ -30,16 +31,18 @@ import org.jivesoftware.smackx.omemo.internal.OmemoDevice; import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint; import org.jivesoftware.smackx.omemo.util.OmemoKeyUtil; -import org.whispersystems.libsignal.IdentityKey; -import org.whispersystems.libsignal.IdentityKeyPair; -import org.whispersystems.libsignal.InvalidKeyException; -import org.whispersystems.libsignal.ecc.Curve; -import org.whispersystems.libsignal.ecc.ECPublicKey; -import org.whispersystems.libsignal.state.PreKeyBundle; -import org.whispersystems.libsignal.state.PreKeyRecord; -import org.whispersystems.libsignal.state.SessionRecord; -import org.whispersystems.libsignal.state.SignedPreKeyRecord; -import org.whispersystems.libsignal.util.KeyHelper; +import org.signal.libsignal.protocol.IdentityKey; +import org.signal.libsignal.protocol.IdentityKeyPair; +import org.signal.libsignal.protocol.InvalidKeyException; +import org.signal.libsignal.protocol.InvalidMessageException; +import org.signal.libsignal.protocol.ecc.Curve; +import org.signal.libsignal.protocol.ecc.ECKeyPair; +import org.signal.libsignal.protocol.ecc.ECPublicKey; +import org.signal.libsignal.protocol.state.PreKeyBundle; +import org.signal.libsignal.protocol.state.PreKeyRecord; +import org.signal.libsignal.protocol.state.SessionRecord; +import org.signal.libsignal.protocol.state.SignedPreKeyRecord; +import org.signal.libsignal.protocol.util.Medium; /** * Concrete implementation of the KeyUtil for an implementation using the Signal library. @@ -51,13 +54,13 @@ public class SignalOmemoKeyUtil extends OmemoKeyUtil generateOmemoPreKeys(int currentPreKeyId, int count) { - List preKeyRecords = KeyHelper.generatePreKeys(currentPreKeyId, count); + List preKeyRecords = generatePreKeys(currentPreKeyId, count); TreeMap map = new TreeMap<>(); for (PreKeyRecord p : preKeyRecords) { map.put(p.getId(), p); @@ -65,20 +68,44 @@ public class SignalOmemoKeyUtil extends OmemoKeyUtil generatePreKeys(int start, int count) { + List results = new ArrayList<>(count); + + start--; + + for (int i = 0; i < count; i++) { + int pkIdx = ((start + i) % (Medium.MAX_VALUE - 1)) + 1; + results.add(new PreKeyRecord(pkIdx, Curve.generateKeyPair())); + } + + return results; + } + @Override public SignedPreKeyRecord generateOmemoSignedPreKey(IdentityKeyPair identityKeyPair, int currentPreKeyId) throws CorruptedOmemoKeyException { try { - return KeyHelper.generateSignedPreKey(identityKeyPair, currentPreKeyId); + return generateSignedPreKey(identityKeyPair, currentPreKeyId); } catch (InvalidKeyException e) { throw new CorruptedOmemoKeyException(e); } } + private static SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair, int signedPreKeyId) throws InvalidKeyException { + ECKeyPair keyPair = Curve.generateKeyPair(); + byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize()); + + return new SignedPreKeyRecord(signedPreKeyId, System.currentTimeMillis(), keyPair, signature); + } + @Override public SignedPreKeyRecord signedPreKeyFromBytes(byte[] data) throws IOException { if (data == null) return null; - return new SignedPreKeyRecord(data); + try { + return new SignedPreKeyRecord(data); + } catch (InvalidMessageException e) { + throw new IOException(e); + } } @Override @@ -89,7 +116,11 @@ public class SignalOmemoKeyUtil extends OmemoKeyUtil loadExistingSessions(List addresses) throws NoSessionException { + throw new RuntimeException("Not implemented."); + } + @Override public List getSubDeviceSessions(String s) { BareJid jid; diff --git a/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/LegacySignalOmemoKeyUtilTest.java b/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/LegacySignalOmemoKeyUtilTest.java index e7b91e0a8..db89d04c1 100644 --- a/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/LegacySignalOmemoKeyUtilTest.java +++ b/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/LegacySignalOmemoKeyUtilTest.java @@ -32,9 +32,9 @@ import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException; import org.jivesoftware.smackx.omemo.signal.SignalOmemoKeyUtil; import org.junit.Test; -import org.whispersystems.libsignal.IdentityKey; -import org.whispersystems.libsignal.IdentityKeyPair; -import org.whispersystems.libsignal.state.SignedPreKeyRecord; +import org.signal.libsignal.protocol.IdentityKey; +import org.signal.libsignal.protocol.IdentityKeyPair; +import org.signal.libsignal.protocol.state.SignedPreKeyRecord; /** * Test SignalOmemoKeyUtil methods. diff --git a/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/SignalOmemoKeyUtilTest.java b/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/SignalOmemoKeyUtilTest.java index d14e08ef5..4dd3e7f44 100644 --- a/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/SignalOmemoKeyUtilTest.java +++ b/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/SignalOmemoKeyUtilTest.java @@ -28,13 +28,13 @@ import org.jivesoftware.smackx.omemo.util.OmemoKeyUtil; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import org.whispersystems.libsignal.IdentityKey; -import org.whispersystems.libsignal.IdentityKeyPair; -import org.whispersystems.libsignal.ecc.ECPublicKey; -import org.whispersystems.libsignal.state.PreKeyBundle; -import org.whispersystems.libsignal.state.PreKeyRecord; -import org.whispersystems.libsignal.state.SessionRecord; -import org.whispersystems.libsignal.state.SignedPreKeyRecord; +import org.signal.libsignal.protocol.IdentityKey; +import org.signal.libsignal.protocol.IdentityKeyPair; +import org.signal.libsignal.protocol.ecc.ECPublicKey; +import org.signal.libsignal.protocol.state.PreKeyBundle; +import org.signal.libsignal.protocol.state.PreKeyRecord; +import org.signal.libsignal.protocol.state.SessionRecord; +import org.signal.libsignal.protocol.state.SignedPreKeyRecord; /** * smack-omemo-signal implementation of {@link OmemoKeyUtilTest}. diff --git a/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/SignalOmemoStoreTest.java b/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/SignalOmemoStoreTest.java index a4a9bedea..ce39a26ad 100644 --- a/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/SignalOmemoStoreTest.java +++ b/smack-omemo-signal/src/test/java/org/jivesoftware/smackx/omemo/SignalOmemoStoreTest.java @@ -35,15 +35,15 @@ import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.jxmpp.stringprep.XmppStringprepException; -import org.whispersystems.libsignal.IdentityKey; -import org.whispersystems.libsignal.IdentityKeyPair; -import org.whispersystems.libsignal.SessionCipher; -import org.whispersystems.libsignal.SignalProtocolAddress; -import org.whispersystems.libsignal.ecc.ECPublicKey; -import org.whispersystems.libsignal.state.PreKeyBundle; -import org.whispersystems.libsignal.state.PreKeyRecord; -import org.whispersystems.libsignal.state.SessionRecord; -import org.whispersystems.libsignal.state.SignedPreKeyRecord; +import org.signal.libsignal.protocol.IdentityKey; +import org.signal.libsignal.protocol.IdentityKeyPair; +import org.signal.libsignal.protocol.SessionCipher; +import org.signal.libsignal.protocol.SignalProtocolAddress; +import org.signal.libsignal.protocol.ecc.ECPublicKey; +import org.signal.libsignal.protocol.state.PreKeyBundle; +import org.signal.libsignal.protocol.state.PreKeyRecord; +import org.signal.libsignal.protocol.state.SessionRecord; +import org.signal.libsignal.protocol.state.SignedPreKeyRecord; /** * smack-omemo-signal implementation of {@link OmemoStoreTest}.