import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.asamk.TextSecure;
-import org.whispersystems.libaxolotl.*;
-import org.whispersystems.libaxolotl.ecc.Curve;
-import org.whispersystems.libaxolotl.ecc.ECKeyPair;
-import org.whispersystems.libaxolotl.state.PreKeyRecord;
-import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
-import org.whispersystems.libaxolotl.util.KeyHelper;
-import org.whispersystems.libaxolotl.util.Medium;
-import org.whispersystems.libaxolotl.util.guava.Optional;
-import org.whispersystems.textsecure.api.TextSecureAccountManager;
-import org.whispersystems.textsecure.api.TextSecureMessagePipe;
-import org.whispersystems.textsecure.api.TextSecureMessageReceiver;
-import org.whispersystems.textsecure.api.TextSecureMessageSender;
-import org.whispersystems.textsecure.api.crypto.TextSecureCipher;
-import org.whispersystems.textsecure.api.messages.*;
-import org.whispersystems.textsecure.api.push.TextSecureAddress;
-import org.whispersystems.textsecure.api.push.TrustStore;
-import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions;
-import org.whispersystems.textsecure.api.util.InvalidNumberException;
-import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
+import org.whispersystems.libsignal.*;
+import org.whispersystems.libsignal.ecc.Curve;
+import org.whispersystems.libsignal.ecc.ECKeyPair;
+import org.whispersystems.libsignal.state.PreKeyRecord;
+import org.whispersystems.libsignal.state.SignalProtocolStore;
+import org.whispersystems.libsignal.state.SignedPreKeyRecord;
+import org.whispersystems.libsignal.util.KeyHelper;
+import org.whispersystems.libsignal.util.Medium;
+import org.whispersystems.libsignal.util.guava.Optional;
+import org.whispersystems.signalservice.api.SignalServiceAccountManager;
+import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
+import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
+import org.whispersystems.signalservice.api.SignalServiceMessageSender;
+import org.whispersystems.signalservice.api.crypto.SignalServiceCipher;
+import org.whispersystems.signalservice.api.messages.*;
+import org.whispersystems.signalservice.api.push.SignalServiceAddress;
+import org.whispersystems.signalservice.api.push.TrustStore;
+import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
+import org.whispersystems.signalservice.api.util.InvalidNumberException;
+import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
import java.io.*;
import java.nio.file.Files;
import java.util.concurrent.TimeoutException;
class Manager implements TextSecure {
- private final static String URL = "https://textsecure-service.whispersystems.org";
+ private final static String URL = "https://SignalService-service.whispersystems.org";
private final static TrustStore TRUST_STORE = new WhisperTrustStore();
public final static String PROJECT_NAME = Manager.class.getPackage().getImplementationTitle();
public final static String PROJECT_VERSION = Manager.class.getPackage().getImplementationVersion();
private final static String USER_AGENT = PROJECT_NAME + " " + PROJECT_VERSION;
- private final static String settingsPath = System.getProperty("user.home") + "/.config/textsecure";
- private final static String dataPath = settingsPath + "/data";
- private final static String attachmentsPath = settingsPath + "/attachments";
+ private final String settingsPath;
+ private final String dataPath;
+ private final String attachmentsPath;
private final ObjectMapper jsonProcessot = new ObjectMapper();
private String username;
private boolean registered = false;
- private JsonAxolotlStore axolotlStore;
- private TextSecureAccountManager accountManager;
+ private SignalProtocolStore signalProtocolStore;
+ private SignalServiceAccountManager accountManager;
private JsonGroupStore groupStore;
- public Manager(String username) {
+ public Manager(String username, String settingsPath) {
this.username = username;
+ this.settingsPath = settingsPath;
+ this.dataPath = this.settingsPath + "/data";
+ this.attachmentsPath = this.settingsPath + "/attachments";
+
jsonProcessot.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); // disable autodetect
jsonProcessot.enable(SerializationFeature.INDENT_OUTPUT); // for pretty print, you can disable it.
jsonProcessot.enable(SerializationFeature.WRITE_NULL_MAP_VALUES);
}
public boolean userHasKeys() {
- return axolotlStore != null;
+ return signalProtocolStore != null;
}
private JsonNode getNotNullNode(JsonNode parent, String name) throws InvalidObjectException {
} else {
nextSignedPreKeyId = 0;
}
- axolotlStore = jsonProcessot.convertValue(getNotNullNode(rootNode, "axolotlStore"), JsonAxolotlStore.class); //new JsonAxolotlStore(in.getJSONObject("axolotlStore"));
+ signalProtocolStore = jsonProcessot.convertValue(getNotNullNode(rootNode, "axolotlStore"), JsonSignalProtocolStore.class);
registered = getNotNullNode(rootNode, "registered").asBoolean();
JsonNode groupStoreNode = rootNode.get("groupStore");
if (groupStoreNode != null) {
if (groupStore == null) {
groupStore = new JsonGroupStore();
}
- accountManager = new TextSecureAccountManager(URL, TRUST_STORE, username, password, USER_AGENT);
+ accountManager = new SignalServiceAccountManager(URL, TRUST_STORE, username, password, USER_AGENT);
}
private void save() {
.put("preKeyIdOffset", preKeyIdOffset)
.put("nextSignedPreKeyId", nextSignedPreKeyId)
.put("registered", registered)
- .putPOJO("axolotlStore", axolotlStore)
+ .putPOJO("axolotlStore", signalProtocolStore)
.putPOJO("groupStore", groupStore)
;
try {
public void createNewIdentity() {
IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair();
int registrationId = KeyHelper.generateRegistrationId(false);
- axolotlStore = new JsonAxolotlStore(identityKey, registrationId);
+ signalProtocolStore = new JsonSignalProtocolStore(identityKey, registrationId);
groupStore = new JsonGroupStore();
registered = false;
save();
public void register(boolean voiceVerication) throws IOException {
password = Util.getSecret(18);
- accountManager = new TextSecureAccountManager(URL, TRUST_STORE, username, password, USER_AGENT);
+ accountManager = new SignalServiceAccountManager(URL, TRUST_STORE, username, password, USER_AGENT);
if (voiceVerication)
accountManager.requestVoiceVerificationCode();
ECKeyPair keyPair = Curve.generateKeyPair();
PreKeyRecord record = new PreKeyRecord(preKeyId, keyPair);
- axolotlStore.storePreKey(preKeyId, record);
+ signalProtocolStore.storePreKey(preKeyId, record);
records.add(record);
}
}
private PreKeyRecord generateLastResortPreKey() {
- if (axolotlStore.containsPreKey(Medium.MAX_VALUE)) {
+ if (signalProtocolStore.containsPreKey(Medium.MAX_VALUE)) {
try {
- return axolotlStore.loadPreKey(Medium.MAX_VALUE);
+ return signalProtocolStore.loadPreKey(Medium.MAX_VALUE);
} catch (InvalidKeyIdException e) {
- axolotlStore.removePreKey(Medium.MAX_VALUE);
+ signalProtocolStore.removePreKey(Medium.MAX_VALUE);
}
}
ECKeyPair keyPair = Curve.generateKeyPair();
PreKeyRecord record = new PreKeyRecord(Medium.MAX_VALUE, keyPair);
- axolotlStore.storePreKey(Medium.MAX_VALUE, record);
+ signalProtocolStore.storePreKey(Medium.MAX_VALUE, record);
save();
return record;
byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
SignedPreKeyRecord record = new SignedPreKeyRecord(nextSignedPreKeyId, System.currentTimeMillis(), keyPair, signature);
- axolotlStore.storeSignedPreKey(nextSignedPreKeyId, record);
+ signalProtocolStore.storeSignedPreKey(nextSignedPreKeyId, record);
nextSignedPreKeyId = (nextSignedPreKeyId + 1) % Medium.MAX_VALUE;
save();
public void verifyAccount(String verificationCode) throws IOException {
verificationCode = verificationCode.replace("-", "");
signalingKey = Util.getSecret(52);
- accountManager.verifyAccountWithCode(verificationCode, signalingKey, axolotlStore.getLocalRegistrationId(), false);
+ accountManager.verifyAccountWithCode(verificationCode, signalingKey, signalProtocolStore.getLocalRegistrationId(), false, true);
//accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
registered = true;
PreKeyRecord lastResortKey = generateLastResortPreKey();
- SignedPreKeyRecord signedPreKeyRecord = generateSignedPreKey(axolotlStore.getIdentityKeyPair());
+ SignedPreKeyRecord signedPreKeyRecord = generateSignedPreKey(signalProtocolStore.getIdentityKeyPair());
- accountManager.setPreKeys(axolotlStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys);
+ accountManager.setPreKeys(signalProtocolStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys);
save();
}
- private static List<TextSecureAttachment> getTextSecureAttachments(List<String> attachments) throws AttachmentInvalidException {
- List<TextSecureAttachment> textSecureAttachments = null;
+ private static List<SignalServiceAttachment> getSignalServiceAttachments(List<String> attachments) throws AttachmentInvalidException {
+ List<SignalServiceAttachment> SignalServiceAttachments = null;
if (attachments != null) {
- textSecureAttachments = new ArrayList<>(attachments.size());
+ SignalServiceAttachments = new ArrayList<>(attachments.size());
for (String attachment : attachments) {
try {
- textSecureAttachments.add(createAttachment(attachment));
+ SignalServiceAttachments.add(createAttachment(attachment));
} catch (IOException e) {
throw new AttachmentInvalidException(attachment, e);
}
}
}
- return textSecureAttachments;
+ return SignalServiceAttachments;
}
- private static TextSecureAttachmentStream createAttachment(String attachment) throws IOException {
+ private static SignalServiceAttachmentStream createAttachment(String attachment) throws IOException {
File attachmentFile = new File(attachment);
InputStream attachmentStream = new FileInputStream(attachmentFile);
final long attachmentSize = attachmentFile.length();
String mime = Files.probeContentType(Paths.get(attachment));
- return new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null);
+ return new SignalServiceAttachmentStream(attachmentStream, mime, attachmentSize, null);
}
@Override
public void sendGroupMessage(String messageText, List<String> attachments,
byte[] groupId)
throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
- final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
+ final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
if (attachments != null) {
- messageBuilder.withAttachments(getTextSecureAttachments(attachments));
+ messageBuilder.withAttachments(getSignalServiceAttachments(attachments));
}
if (groupId != null) {
- TextSecureGroup group = TextSecureGroup.newBuilder(TextSecureGroup.Type.DELIVER)
+ SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER)
.withId(groupId)
.build();
messageBuilder.asGroupMessage(group);
}
- TextSecureDataMessage message = messageBuilder.build();
+ SignalServiceDataMessage message = messageBuilder.build();
sendMessage(message, groupStore.getGroup(groupId).members);
}
public void sendQuitGroupMessage(byte[] groupId) throws GroupNotFoundException, IOException, EncapsulatedExceptions {
- TextSecureGroup group = TextSecureGroup.newBuilder(TextSecureGroup.Type.QUIT)
+ SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT)
.withId(groupId)
.build();
- TextSecureDataMessage message = TextSecureDataMessage.newBuilder()
+ SignalServiceDataMessage message = SignalServiceDataMessage.newBuilder()
.asGroupMessage(group)
.build();
}
}
- TextSecureGroup.Builder group = TextSecureGroup.newBuilder(TextSecureGroup.Type.UPDATE)
+ SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.UPDATE)
.withId(g.groupId)
.withName(g.name)
.withMembers(new ArrayList<>(g.members));
groupStore.updateGroup(g);
- TextSecureDataMessage message = TextSecureDataMessage.newBuilder()
+ SignalServiceDataMessage message = SignalServiceDataMessage.newBuilder()
.asGroupMessage(group.build())
.build();
public void sendMessage(String messageText, List<String> attachments,
List<String> recipients)
throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
- final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
+ final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
if (attachments != null) {
- messageBuilder.withAttachments(getTextSecureAttachments(attachments));
+ messageBuilder.withAttachments(getSignalServiceAttachments(attachments));
}
- TextSecureDataMessage message = messageBuilder.build();
+ SignalServiceDataMessage message = messageBuilder.build();
sendMessage(message, recipients);
}
@Override
public void sendEndSessionMessage(List<String> recipients) throws IOException, EncapsulatedExceptions {
- TextSecureDataMessage message = TextSecureDataMessage.newBuilder()
+ SignalServiceDataMessage message = SignalServiceDataMessage.newBuilder()
.asEndSessionMessage()
.build();
sendMessage(message, recipients);
}
- private void sendMessage(TextSecureDataMessage message, Collection<String> recipients)
+ private void sendMessage(SignalServiceDataMessage message, Collection<String> recipients)
throws IOException, EncapsulatedExceptions {
- TextSecureMessageSender messageSender = new TextSecureMessageSender(URL, TRUST_STORE, username, password,
- axolotlStore, USER_AGENT, Optional.<TextSecureMessageSender.EventListener>absent());
+ SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password,
+ signalProtocolStore, USER_AGENT, Optional.<SignalServiceMessageSender.EventListener>absent());
- Set<TextSecureAddress> recipientsTS = new HashSet<>(recipients.size());
+ Set<SignalServiceAddress> recipientsTS = new HashSet<>(recipients.size());
for (String recipient : recipients) {
try {
recipientsTS.add(getPushAddress(recipient));
messageSender.sendMessage(new ArrayList<>(recipientsTS), message);
if (message.isEndSession()) {
- for (TextSecureAddress recipient : recipientsTS) {
+ for (SignalServiceAddress recipient : recipientsTS) {
handleEndSession(recipient.getNumber());
}
}
save();
}
- private TextSecureContent decryptMessage(TextSecureEnvelope envelope) {
- TextSecureCipher cipher = new TextSecureCipher(new TextSecureAddress(username), axolotlStore);
+ private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) {
+ SignalServiceCipher cipher = new SignalServiceCipher(new SignalServiceAddress(username), signalProtocolStore);
try {
return cipher.decrypt(envelope);
} catch (Exception e) {
}
private void handleEndSession(String source) {
- axolotlStore.deleteAllSessions(source);
+ signalProtocolStore.deleteAllSessions(source);
}
public interface ReceiveMessageHandler {
- void handleMessage(TextSecureEnvelope envelope, TextSecureContent decryptedContent, GroupInfo group);
+ void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent, GroupInfo group);
}
public void receiveMessages(int timeoutSeconds, boolean returnOnTimeout, ReceiveMessageHandler handler) throws IOException {
- final TextSecureMessageReceiver messageReceiver = new TextSecureMessageReceiver(URL, TRUST_STORE, username, password, signalingKey, USER_AGENT);
- TextSecureMessagePipe messagePipe = null;
+ final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, signalingKey, USER_AGENT);
+ SignalServiceMessagePipe messagePipe = null;
try {
messagePipe = messageReceiver.createMessagePipe();
while (true) {
- TextSecureEnvelope envelope;
- TextSecureContent content = null;
+ SignalServiceEnvelope envelope;
+ SignalServiceContent content = null;
GroupInfo group = null;
try {
envelope = messagePipe.read(timeoutSeconds, TimeUnit.SECONDS);
content = decryptMessage(envelope);
if (content != null) {
if (content.getDataMessage().isPresent()) {
- TextSecureDataMessage message = content.getDataMessage().get();
+ SignalServiceDataMessage message = content.getDataMessage().get();
if (message.getGroupInfo().isPresent()) {
- TextSecureGroup groupInfo = message.getGroupInfo().get();
+ SignalServiceGroup groupInfo = message.getGroupInfo().get();
switch (groupInfo.getType()) {
case UPDATE:
try {
}
if (groupInfo.getAvatar().isPresent()) {
- TextSecureAttachment avatar = groupInfo.getAvatar().get();
+ SignalServiceAttachment avatar = groupInfo.getAvatar().get();
if (avatar.isPointer()) {
long avatarId = avatar.asPointer().getId();
try {
handleEndSession(envelope.getSource());
}
if (message.getAttachments().isPresent()) {
- for (TextSecureAttachment attachment : message.getAttachments().get()) {
+ for (SignalServiceAttachment attachment : message.getAttachments().get()) {
if (attachment.isPointer()) {
try {
retrieveAttachment(attachment.asPointer());
return new File(attachmentsPath, attachmentId + "");
}
- private File retrieveAttachment(TextSecureAttachmentPointer pointer) throws IOException, InvalidMessageException {
- final TextSecureMessageReceiver messageReceiver = new TextSecureMessageReceiver(URL, TRUST_STORE, username, password, signalingKey, USER_AGENT);
+ private File retrieveAttachment(SignalServiceAttachmentPointer pointer) throws IOException, InvalidMessageException {
+ final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, signalingKey, USER_AGENT);
File tmpFile = File.createTempFile("ts_attach_" + pointer.getId(), ".tmp");
InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile);
return PhoneNumberFormatter.formatNumber(number, localNumber);
}
- private TextSecureAddress getPushAddress(String number) throws InvalidNumberException {
+ private SignalServiceAddress getPushAddress(String number) throws InvalidNumberException {
String e164number = canonicalizeNumber(number);
- return new TextSecureAddress(e164number);
+ return new SignalServiceAddress(e164number);
}
@Override