Trust an identity with its scannable safety numbers from the other device

Attention, the scannable fingerprints are asymetric, so the scannable
fingerprints from the local listIdentities command can't be used to trust
an identity. The scannable fingerprint must come from the other device.
This commit is contained in:
AsamK 2021-08-22 19:23:49 +02:00
parent 0a5e836ab6
commit 4f67ac674b
2 changed files with 82 additions and 42 deletions

View file

@ -11,6 +11,7 @@ import org.asamk.signal.manager.Manager;
import org.asamk.signal.util.Hex;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.util.Base64;
import java.util.Locale;
public class TrustCommand implements JsonRpcLocalCommand {
@ -49,44 +50,59 @@ public class TrustCommand implements JsonRpcLocalCommand {
}
} else {
var safetyNumber = ns.getString("verified-safety-number");
if (safetyNumber != null) {
safetyNumber = safetyNumber.replaceAll(" ", "");
if (safetyNumber.length() == 66) {
byte[] fingerprintBytes;
try {
fingerprintBytes = Hex.toByteArray(safetyNumber.toLowerCase(Locale.ROOT));
} catch (Exception e) {
throw new UserErrorException(
"Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters.");
}
boolean res;
try {
res = m.trustIdentityVerified(number, fingerprintBytes);
} catch (InvalidNumberException e) {
throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
}
if (!res) {
throw new UserErrorException(
"Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct.");
}
} else if (safetyNumber.length() == 60) {
boolean res;
try {
res = m.trustIdentityVerifiedSafetyNumber(number, safetyNumber);
} catch (InvalidNumberException e) {
throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
}
if (!res) {
throw new UserErrorException(
"Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
}
} else {
if (safetyNumber == null) {
throw new UserErrorException(
"You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER");
}
safetyNumber = safetyNumber.replaceAll(" ", "");
if (safetyNumber.length() == 66) {
byte[] fingerprintBytes;
try {
fingerprintBytes = Hex.toByteArray(safetyNumber.toLowerCase(Locale.ROOT));
} catch (Exception e) {
throw new UserErrorException(
"Failed to parse the fingerprint, make sure the fingerprint is a correctly encoded hex string without additional characters.");
}
boolean res;
try {
res = m.trustIdentityVerified(number, fingerprintBytes);
} catch (InvalidNumberException e) {
throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
}
if (!res) {
throw new UserErrorException(
"Failed to set the trust for the fingerprint of this number, make sure the number and the fingerprint are correct.");
}
} else if (safetyNumber.length() == 60) {
boolean res;
try {
res = m.trustIdentityVerifiedSafetyNumber(number, safetyNumber);
} catch (InvalidNumberException e) {
throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
}
if (!res) {
throw new UserErrorException(
"Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
}
} else {
final byte[] scannableSafetyNumber;
try {
scannableSafetyNumber = Base64.getDecoder().decode(safetyNumber);
} catch (IllegalArgumentException e) {
throw new UserErrorException(
"Safety number has invalid format, either specify the old hex fingerprint or the new safety number");
}
} else {
throw new UserErrorException(
"You need to specify the fingerprint/safety number you have verified with -v SAFETY_NUMBER");
boolean res;
try {
res = m.trustIdentityVerifiedSafetyNumber(number, scannableSafetyNumber);
} catch (InvalidNumberException e) {
throw new UserErrorException("Failed to parse recipient: " + e.getMessage());
}
if (!res) {
throw new UserErrorException(
"Failed to set the trust for the safety number of this phone number, make sure the phone number and the safety number are correct.");
}
}
}
}