Send and receive verified messages

Fixes #85
This commit is contained in:
AsamK 2017-06-11 16:28:03 +02:00
parent 23fcd8a230
commit 4377a2179b
3 changed files with 80 additions and 2 deletions

View file

@ -992,6 +992,16 @@ public class Main {
System.out.println(" - " + number); System.out.println(" - " + number);
} }
} }
if (syncMessage.getVerified().isPresent()) {
System.out.println("Received sync message with verified identities:");
final List<VerifiedMessage> verifiedList = syncMessage.getVerified().get();
for (VerifiedMessage v : verifiedList) {
System.out.println(" - " + v.getDestination() + ": " + v.getVerified());
String safetyNumber = formatSafetyNumber(m.computeSafetyNumber(v.getDestination(), v.getIdentityKey()));
System.out.println(" " + safetyNumber);
}
}
} }
} }
} else { } else {

View file

@ -96,7 +96,7 @@ class Manager implements Signal {
private final static int PREKEY_MINIMUM_COUNT = 20; private final static int PREKEY_MINIMUM_COUNT = 20;
private static final int PREKEY_BATCH_SIZE = 100; private static final int PREKEY_BATCH_SIZE = 100;
private static final int MAX_ATTACHMENT_SIZE = 150 * 1024 * 1024; private static final int MAX_ATTACHMENT_SIZE = 150 * 1024 * 1024;
private final String settingsPath; private final String settingsPath;
private final String dataPath; private final String dataPath;
@ -559,7 +559,7 @@ class Manager implements Signal {
mime = "application/octet-stream"; mime = "application/octet-stream";
} }
// TODO mabybe add a parameter to set the voiceNote and preview option // TODO mabybe add a parameter to set the voiceNote and preview option
return new SignalServiceAttachmentStream(attachmentStream, mime, attachmentSize, Optional.of(attachmentFile.getName()), false, Optional.<byte[]>absent(),null); return new SignalServiceAttachmentStream(attachmentStream, mime, attachmentSize, Optional.of(attachmentFile.getName()), false, Optional.<byte[]>absent(), null);
} }
private Optional<SignalServiceAttachmentStream> createGroupAvatarAttachment(byte[] groupId) throws IOException { private Optional<SignalServiceAttachmentStream> createGroupAvatarAttachment(byte[] groupId) throws IOException {
@ -1199,6 +1199,7 @@ class Manager implements Signal {
if (rm.isContactsRequest()) { if (rm.isContactsRequest()) {
try { try {
sendContacts(); sendContacts();
sendVerifiedMessage();
} catch (UntrustedIdentityException | IOException e) { } catch (UntrustedIdentityException | IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -1288,6 +1289,12 @@ class Manager implements Signal {
} }
} }
} }
if (syncMessage.getVerified().isPresent()) {
final List<VerifiedMessage> verifiedList = syncMessage.getVerified().get();
for (VerifiedMessage v : verifiedList) {
signalProtocolStore.saveIdentity(v.getDestination(), v.getIdentityKey(), TrustLevel.fromVerifiedState(v.getVerified()));
}
}
} }
} }
} }
@ -1523,6 +1530,26 @@ class Manager implements Signal {
} }
} }
private void sendVerifiedMessage() throws IOException, UntrustedIdentityException {
List<VerifiedMessage> verifiedMessages = new LinkedList<>();
for (Map.Entry<String, List<JsonIdentityKeyStore.Identity>> x : getIdentities().entrySet()) {
final String name = x.getKey();
for (JsonIdentityKeyStore.Identity id : x.getValue()) {
if (id.getTrustLevel() == TrustLevel.TRUSTED_UNVERIFIED) {
continue;
}
VerifiedMessage verifiedMessage = new VerifiedMessage(name, id.getIdentityKey(), id.getTrustLevel().toVerifiedState());
verifiedMessages.add(verifiedMessage);
}
}
sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessages));
}
private void sendVerifiedMessage(String destination, IdentityKey identityKey, TrustLevel trustLevel) throws IOException, UntrustedIdentityException {
VerifiedMessage verifiedMessage = new VerifiedMessage(destination, identityKey, trustLevel.toVerifiedState());
sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage));
}
public ContactInfo getContact(String number) { public ContactInfo getContact(String number) {
return contactStore.getContact(number); return contactStore.getContact(number);
} }
@ -1556,6 +1583,11 @@ class Manager implements Signal {
} }
signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED); signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
try {
sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
} catch (IOException | UntrustedIdentityException e) {
e.printStackTrace();
}
save(); save();
return true; return true;
} }
@ -1579,6 +1611,11 @@ class Manager implements Signal {
} }
signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED); signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
try {
sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
} catch (IOException | UntrustedIdentityException e) {
e.printStackTrace();
}
save(); save();
return true; return true;
} }
@ -1598,6 +1635,11 @@ class Manager implements Signal {
for (JsonIdentityKeyStore.Identity id : ids) { for (JsonIdentityKeyStore.Identity id : ids) {
if (id.getTrustLevel() == TrustLevel.UNTRUSTED) { if (id.getTrustLevel() == TrustLevel.UNTRUSTED) {
signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED); signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
try {
sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
} catch (IOException | UntrustedIdentityException e) {
e.printStackTrace();
}
} }
} }
save(); save();

View file

@ -1,5 +1,7 @@
package org.asamk.signal; package org.asamk.signal;
import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage;
public enum TrustLevel { public enum TrustLevel {
UNTRUSTED, UNTRUSTED,
TRUSTED_UNVERIFIED, TRUSTED_UNVERIFIED,
@ -13,4 +15,28 @@ public enum TrustLevel {
} }
return TrustLevel.cachedValues[i]; return TrustLevel.cachedValues[i];
} }
public static TrustLevel fromVerifiedState(VerifiedMessage.VerifiedState verifiedState) {
switch (verifiedState) {
case DEFAULT:
return TRUSTED_UNVERIFIED;
case UNVERIFIED:
return UNTRUSTED;
case VERIFIED:
return TRUSTED_VERIFIED;
}
throw new RuntimeException("Unknown verified state: " + verifiedState);
}
public VerifiedMessage.VerifiedState toVerifiedState() {
switch (this) {
case TRUSTED_UNVERIFIED:
return VerifiedMessage.VerifiedState.DEFAULT;
case UNTRUSTED:
return VerifiedMessage.VerifiedState.UNVERIFIED;
case TRUSTED_VERIFIED:
return VerifiedMessage.VerifiedState.VERIFIED;
}
throw new RuntimeException("Unknown verified state: " + this);
}
} }