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);
}
}
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 {

View file

@ -1199,6 +1199,7 @@ class Manager implements Signal {
if (rm.isContactsRequest()) {
try {
sendContacts();
sendVerifiedMessage();
} catch (UntrustedIdentityException | IOException e) {
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) {
return contactStore.getContact(number);
}
@ -1556,6 +1583,11 @@ class Manager implements Signal {
}
signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
try {
sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
} catch (IOException | UntrustedIdentityException e) {
e.printStackTrace();
}
save();
return true;
}
@ -1579,6 +1611,11 @@ class Manager implements Signal {
}
signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
try {
sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED);
} catch (IOException | UntrustedIdentityException e) {
e.printStackTrace();
}
save();
return true;
}
@ -1598,6 +1635,11 @@ class Manager implements Signal {
for (JsonIdentityKeyStore.Identity id : ids) {
if (id.getTrustLevel() == TrustLevel.UNTRUSTED) {
signalProtocolStore.saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
try {
sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED);
} catch (IOException | UntrustedIdentityException e) {
e.printStackTrace();
}
}
}
save();

View file

@ -1,5 +1,7 @@
package org.asamk.signal;
import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage;
public enum TrustLevel {
UNTRUSTED,
TRUSTED_UNVERIFIED,
@ -13,4 +15,28 @@ public enum TrustLevel {
}
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);
}
}