2 * Copyright (C) 2015 AsamK
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org
.asamk
.signal
;
19 import com
.fasterxml
.jackson
.annotation
.JsonAutoDetect
;
20 import com
.fasterxml
.jackson
.annotation
.PropertyAccessor
;
21 import com
.fasterxml
.jackson
.databind
.DeserializationFeature
;
22 import com
.fasterxml
.jackson
.databind
.JsonNode
;
23 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
24 import com
.fasterxml
.jackson
.databind
.SerializationFeature
;
25 import com
.fasterxml
.jackson
.databind
.node
.ObjectNode
;
26 import org
.asamk
.Signal
;
27 import org
.whispersystems
.libsignal
.*;
28 import org
.whispersystems
.libsignal
.ecc
.Curve
;
29 import org
.whispersystems
.libsignal
.ecc
.ECKeyPair
;
30 import org
.whispersystems
.libsignal
.state
.PreKeyRecord
;
31 import org
.whispersystems
.libsignal
.state
.SignalProtocolStore
;
32 import org
.whispersystems
.libsignal
.state
.SignedPreKeyRecord
;
33 import org
.whispersystems
.libsignal
.util
.KeyHelper
;
34 import org
.whispersystems
.libsignal
.util
.Medium
;
35 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
36 import org
.whispersystems
.signalservice
.api
.SignalServiceAccountManager
;
37 import org
.whispersystems
.signalservice
.api
.SignalServiceMessagePipe
;
38 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageReceiver
;
39 import org
.whispersystems
.signalservice
.api
.SignalServiceMessageSender
;
40 import org
.whispersystems
.signalservice
.api
.crypto
.*;
41 import org
.whispersystems
.signalservice
.api
.crypto
.UntrustedIdentityException
;
42 import org
.whispersystems
.signalservice
.api
.messages
.*;
43 import org
.whispersystems
.signalservice
.api
.messages
.multidevice
.SignalServiceSyncMessage
;
44 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
45 import org
.whispersystems
.signalservice
.api
.push
.TrustStore
;
46 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.AuthorizationFailedException
;
47 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.EncapsulatedExceptions
;
48 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
49 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
52 import java
.nio
.file
.Files
;
53 import java
.nio
.file
.Paths
;
55 import java
.util
.concurrent
.TimeUnit
;
56 import java
.util
.concurrent
.TimeoutException
;
58 class Manager
implements Signal
{
59 private final static String URL
= "https://textsecure-service.whispersystems.org";
60 private final static TrustStore TRUST_STORE
= new WhisperTrustStore();
62 public final static String PROJECT_NAME
= Manager
.class.getPackage().getImplementationTitle();
63 public final static String PROJECT_VERSION
= Manager
.class.getPackage().getImplementationVersion();
64 private final static String USER_AGENT
= PROJECT_NAME
== null ?
null : PROJECT_NAME
+ " " + PROJECT_VERSION
;
66 private final static int PREKEY_MINIMUM_COUNT
= 20;
67 private static final int PREKEY_BATCH_SIZE
= 100;
69 private final String settingsPath
;
70 private final String dataPath
;
71 private final String attachmentsPath
;
73 private final ObjectMapper jsonProcessot
= new ObjectMapper();
74 private String username
;
75 private String password
;
76 private String signalingKey
;
77 private int preKeyIdOffset
;
78 private int nextSignedPreKeyId
;
80 private boolean registered
= false;
82 private SignalProtocolStore signalProtocolStore
;
83 private SignalServiceAccountManager accountManager
;
84 private JsonGroupStore groupStore
;
86 public Manager(String username
, String settingsPath
) {
87 this.username
= username
;
88 this.settingsPath
= settingsPath
;
89 this.dataPath
= this.settingsPath
+ "/data";
90 this.attachmentsPath
= this.settingsPath
+ "/attachments";
92 jsonProcessot
.setVisibility(PropertyAccessor
.ALL
, JsonAutoDetect
.Visibility
.NONE
); // disable autodetect
93 jsonProcessot
.enable(SerializationFeature
.INDENT_OUTPUT
); // for pretty print, you can disable it.
94 jsonProcessot
.enable(SerializationFeature
.WRITE_NULL_MAP_VALUES
);
95 jsonProcessot
.disable(DeserializationFeature
.FAIL_ON_UNKNOWN_PROPERTIES
);
98 public String
getFileName() {
99 new File(dataPath
).mkdirs();
100 return dataPath
+ "/" + username
;
103 public boolean userExists() {
104 File f
= new File(getFileName());
105 return !(!f
.exists() || f
.isDirectory());
108 public boolean userHasKeys() {
109 return signalProtocolStore
!= null;
112 private JsonNode
getNotNullNode(JsonNode parent
, String name
) throws InvalidObjectException
{
113 JsonNode node
= parent
.get(name
);
115 throw new InvalidObjectException(String
.format("Incorrect file format: expected parameter %s not found ", name
));
121 public void load() throws IOException
, InvalidKeyException
{
122 JsonNode rootNode
= jsonProcessot
.readTree(new File(getFileName()));
124 username
= getNotNullNode(rootNode
, "username").asText();
125 password
= getNotNullNode(rootNode
, "password").asText();
126 if (rootNode
.has("signalingKey")) {
127 signalingKey
= getNotNullNode(rootNode
, "signalingKey").asText();
129 if (rootNode
.has("preKeyIdOffset")) {
130 preKeyIdOffset
= getNotNullNode(rootNode
, "preKeyIdOffset").asInt(0);
134 if (rootNode
.has("nextSignedPreKeyId")) {
135 nextSignedPreKeyId
= getNotNullNode(rootNode
, "nextSignedPreKeyId").asInt();
137 nextSignedPreKeyId
= 0;
139 signalProtocolStore
= jsonProcessot
.convertValue(getNotNullNode(rootNode
, "axolotlStore"), JsonSignalProtocolStore
.class);
140 registered
= getNotNullNode(rootNode
, "registered").asBoolean();
141 JsonNode groupStoreNode
= rootNode
.get("groupStore");
142 if (groupStoreNode
!= null) {
143 groupStore
= jsonProcessot
.convertValue(groupStoreNode
, JsonGroupStore
.class);
145 if (groupStore
== null) {
146 groupStore
= new JsonGroupStore();
148 accountManager
= new SignalServiceAccountManager(URL
, TRUST_STORE
, username
, password
, USER_AGENT
);
150 if (registered
&& accountManager
.getPreKeysCount() < PREKEY_MINIMUM_COUNT
) {
154 } catch (AuthorizationFailedException e
) {
155 System
.err
.println("Authorization failed, was the number registered elsewhere?");
159 private void save() {
160 ObjectNode rootNode
= jsonProcessot
.createObjectNode();
161 rootNode
.put("username", username
)
162 .put("password", password
)
163 .put("signalingKey", signalingKey
)
164 .put("preKeyIdOffset", preKeyIdOffset
)
165 .put("nextSignedPreKeyId", nextSignedPreKeyId
)
166 .put("registered", registered
)
167 .putPOJO("axolotlStore", signalProtocolStore
)
168 .putPOJO("groupStore", groupStore
)
171 jsonProcessot
.writeValue(new File(getFileName()), rootNode
);
172 } catch (Exception e
) {
173 System
.err
.println(String
.format("Error saving file: %s", e
.getMessage()));
177 public void createNewIdentity() {
178 IdentityKeyPair identityKey
= KeyHelper
.generateIdentityKeyPair();
179 int registrationId
= KeyHelper
.generateRegistrationId(false);
180 signalProtocolStore
= new JsonSignalProtocolStore(identityKey
, registrationId
);
181 groupStore
= new JsonGroupStore();
186 public boolean isRegistered() {
190 public void register(boolean voiceVerication
) throws IOException
{
191 password
= Util
.getSecret(18);
193 accountManager
= new SignalServiceAccountManager(URL
, TRUST_STORE
, username
, password
, USER_AGENT
);
196 accountManager
.requestVoiceVerificationCode();
198 accountManager
.requestSmsVerificationCode();
204 private List
<PreKeyRecord
> generatePreKeys() {
205 List
<PreKeyRecord
> records
= new LinkedList
<>();
207 for (int i
= 0; i
< PREKEY_BATCH_SIZE
; i
++) {
208 int preKeyId
= (preKeyIdOffset
+ i
) % Medium
.MAX_VALUE
;
209 ECKeyPair keyPair
= Curve
.generateKeyPair();
210 PreKeyRecord
record = new PreKeyRecord(preKeyId
, keyPair
);
212 signalProtocolStore
.storePreKey(preKeyId
, record);
216 preKeyIdOffset
= (preKeyIdOffset
+ PREKEY_BATCH_SIZE
+ 1) % Medium
.MAX_VALUE
;
222 private PreKeyRecord
getOrGenerateLastResortPreKey() {
223 if (signalProtocolStore
.containsPreKey(Medium
.MAX_VALUE
)) {
225 return signalProtocolStore
.loadPreKey(Medium
.MAX_VALUE
);
226 } catch (InvalidKeyIdException e
) {
227 signalProtocolStore
.removePreKey(Medium
.MAX_VALUE
);
231 ECKeyPair keyPair
= Curve
.generateKeyPair();
232 PreKeyRecord
record = new PreKeyRecord(Medium
.MAX_VALUE
, keyPair
);
234 signalProtocolStore
.storePreKey(Medium
.MAX_VALUE
, record);
240 private SignedPreKeyRecord
generateSignedPreKey(IdentityKeyPair identityKeyPair
) {
242 ECKeyPair keyPair
= Curve
.generateKeyPair();
243 byte[] signature
= Curve
.calculateSignature(identityKeyPair
.getPrivateKey(), keyPair
.getPublicKey().serialize());
244 SignedPreKeyRecord
record = new SignedPreKeyRecord(nextSignedPreKeyId
, System
.currentTimeMillis(), keyPair
, signature
);
246 signalProtocolStore
.storeSignedPreKey(nextSignedPreKeyId
, record);
247 nextSignedPreKeyId
= (nextSignedPreKeyId
+ 1) % Medium
.MAX_VALUE
;
251 } catch (InvalidKeyException e
) {
252 throw new AssertionError(e
);
256 public void verifyAccount(String verificationCode
) throws IOException
{
257 verificationCode
= verificationCode
.replace("-", "");
258 signalingKey
= Util
.getSecret(52);
259 accountManager
.verifyAccountWithCode(verificationCode
, signalingKey
, signalProtocolStore
.getLocalRegistrationId(), false, true);
261 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
268 private void refreshPreKeys() throws IOException
{
269 List
<PreKeyRecord
> oneTimePreKeys
= generatePreKeys();
270 PreKeyRecord lastResortKey
= getOrGenerateLastResortPreKey();
271 SignedPreKeyRecord signedPreKeyRecord
= generateSignedPreKey(signalProtocolStore
.getIdentityKeyPair());
273 accountManager
.setPreKeys(signalProtocolStore
.getIdentityKeyPair().getPublicKey(), lastResortKey
, signedPreKeyRecord
, oneTimePreKeys
);
277 private static List
<SignalServiceAttachment
> getSignalServiceAttachments(List
<String
> attachments
) throws AttachmentInvalidException
{
278 List
<SignalServiceAttachment
> SignalServiceAttachments
= null;
279 if (attachments
!= null) {
280 SignalServiceAttachments
= new ArrayList
<>(attachments
.size());
281 for (String attachment
: attachments
) {
283 SignalServiceAttachments
.add(createAttachment(attachment
));
284 } catch (IOException e
) {
285 throw new AttachmentInvalidException(attachment
, e
);
289 return SignalServiceAttachments
;
292 private static SignalServiceAttachmentStream
createAttachment(String attachment
) throws IOException
{
293 File attachmentFile
= new File(attachment
);
294 InputStream attachmentStream
= new FileInputStream(attachmentFile
);
295 final long attachmentSize
= attachmentFile
.length();
296 String mime
= Files
.probeContentType(Paths
.get(attachment
));
297 return new SignalServiceAttachmentStream(attachmentStream
, mime
, attachmentSize
, null);
301 public void sendGroupMessage(String messageText
, List
<String
> attachments
,
303 throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
, UntrustedIdentityException
{
304 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
305 if (attachments
!= null) {
306 messageBuilder
.withAttachments(getSignalServiceAttachments(attachments
));
308 if (groupId
!= null) {
309 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.DELIVER
)
312 messageBuilder
.asGroupMessage(group
);
314 SignalServiceDataMessage message
= messageBuilder
.build();
316 sendMessage(message
, groupStore
.getGroup(groupId
).members
);
319 public void sendQuitGroupMessage(byte[] groupId
) throws GroupNotFoundException
, IOException
, EncapsulatedExceptions
, UntrustedIdentityException
{
320 SignalServiceGroup group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.QUIT
)
324 SignalServiceDataMessage message
= SignalServiceDataMessage
.newBuilder()
325 .asGroupMessage(group
)
328 sendMessage(message
, groupStore
.getGroup(groupId
).members
);
331 public byte[] sendUpdateGroupMessage(byte[] groupId
, String name
, Collection
<String
> members
, String avatarFile
) throws IOException
, EncapsulatedExceptions
, GroupNotFoundException
, AttachmentInvalidException
, UntrustedIdentityException
{
333 if (groupId
== null) {
335 g
= new GroupInfo(Util
.getSecretBytes(16));
336 g
.members
.add(username
);
338 g
= groupStore
.getGroup(groupId
);
345 if (members
!= null) {
346 for (String member
: members
) {
348 g
.members
.add(canonicalizeNumber(member
));
349 } catch (InvalidNumberException e
) {
350 System
.err
.println("Failed to add member \"" + member
+ "\" to group: " + e
.getMessage());
351 System
.err
.println("Aborting…");
357 SignalServiceGroup
.Builder group
= SignalServiceGroup
.newBuilder(SignalServiceGroup
.Type
.UPDATE
)
360 .withMembers(new ArrayList
<>(g
.members
));
362 if (avatarFile
!= null) {
364 group
.withAvatar(createAttachment(avatarFile
));
367 } catch (IOException e
) {
368 throw new AttachmentInvalidException(avatarFile
, e
);
372 groupStore
.updateGroup(g
);
374 SignalServiceDataMessage message
= SignalServiceDataMessage
.newBuilder()
375 .asGroupMessage(group
.build())
378 sendMessage(message
, g
.members
);
383 public void sendMessage(String message
, List
<String
> attachments
, String recipient
)
384 throws EncapsulatedExceptions
, AttachmentInvalidException
, IOException
, UntrustedIdentityException
{
385 List
<String
> recipients
= new ArrayList
<>(1);
386 recipients
.add(recipient
);
387 sendMessage(message
, attachments
, recipients
);
391 public void sendMessage(String messageText
, List
<String
> attachments
,
392 List
<String
> recipients
)
393 throws IOException
, EncapsulatedExceptions
, AttachmentInvalidException
, UntrustedIdentityException
{
394 final SignalServiceDataMessage
.Builder messageBuilder
= SignalServiceDataMessage
.newBuilder().withBody(messageText
);
395 if (attachments
!= null) {
396 messageBuilder
.withAttachments(getSignalServiceAttachments(attachments
));
398 SignalServiceDataMessage message
= messageBuilder
.build();
400 sendMessage(message
, recipients
);
404 public void sendEndSessionMessage(List
<String
> recipients
) throws IOException
, EncapsulatedExceptions
, UntrustedIdentityException
{
405 SignalServiceDataMessage message
= SignalServiceDataMessage
.newBuilder()
406 .asEndSessionMessage()
409 sendMessage(message
, recipients
);
412 private void sendMessage(SignalServiceDataMessage message
, Collection
<String
> recipients
)
413 throws IOException
, EncapsulatedExceptions
, UntrustedIdentityException
{
414 SignalServiceMessageSender messageSender
= new SignalServiceMessageSender(URL
, TRUST_STORE
, username
, password
,
415 signalProtocolStore
, USER_AGENT
, Optional
.<SignalServiceMessageSender
.EventListener
>absent());
417 Set
<SignalServiceAddress
> recipientsTS
= new HashSet
<>(recipients
.size());
418 for (String recipient
: recipients
) {
420 recipientsTS
.add(getPushAddress(recipient
));
421 } catch (InvalidNumberException e
) {
422 System
.err
.println("Failed to add recipient \"" + recipient
+ "\": " + e
.getMessage());
423 System
.err
.println("Aborting sending.");
429 if (message
.getGroupInfo().isPresent()) {
430 messageSender
.sendMessage(new ArrayList
<>(recipientsTS
), message
);
432 // Send to all individually, so sync messages are sent correctly
433 for (SignalServiceAddress address
: recipientsTS
) {
434 messageSender
.sendMessage(address
, message
);
438 if (message
.isEndSession()) {
439 for (SignalServiceAddress recipient
: recipientsTS
) {
440 handleEndSession(recipient
.getNumber());
446 private SignalServiceContent
decryptMessage(SignalServiceEnvelope envelope
) {
447 SignalServiceCipher cipher
= new SignalServiceCipher(new SignalServiceAddress(username
), signalProtocolStore
);
449 return cipher
.decrypt(envelope
);
450 } catch (Exception e
) {
451 // TODO handle all exceptions
457 private void handleEndSession(String source
) {
458 signalProtocolStore
.deleteAllSessions(source
);
461 public interface ReceiveMessageHandler
{
462 void handleMessage(SignalServiceEnvelope envelope
, SignalServiceContent decryptedContent
, GroupInfo group
);
465 private GroupInfo
handleSignalServiceDataMessage(SignalServiceDataMessage message
, boolean isSync
, String source
, String destination
) {
466 GroupInfo group
= null;
467 if (message
.getGroupInfo().isPresent()) {
468 SignalServiceGroup groupInfo
= message
.getGroupInfo().get();
469 switch (groupInfo
.getType()) {
472 group
= groupStore
.getGroup(groupInfo
.getGroupId());
473 } catch (GroupNotFoundException e
) {
474 group
= new GroupInfo(groupInfo
.getGroupId());
477 if (groupInfo
.getAvatar().isPresent()) {
478 SignalServiceAttachment avatar
= groupInfo
.getAvatar().get();
479 if (avatar
.isPointer()) {
480 long avatarId
= avatar
.asPointer().getId();
482 retrieveAttachment(avatar
.asPointer());
483 group
.avatarId
= avatarId
;
484 } catch (IOException
| InvalidMessageException e
) {
485 System
.err
.println("Failed to retrieve group avatar (" + avatarId
+ "): " + e
.getMessage());
490 if (groupInfo
.getName().isPresent()) {
491 group
.name
= groupInfo
.getName().get();
494 if (groupInfo
.getMembers().isPresent()) {
495 group
.members
.addAll(groupInfo
.getMembers().get());
498 groupStore
.updateGroup(group
);
502 group
= groupStore
.getGroup(groupInfo
.getGroupId());
503 } catch (GroupNotFoundException e
) {
508 group
= groupStore
.getGroup(groupInfo
.getGroupId());
509 group
.members
.remove(source
);
510 } catch (GroupNotFoundException e
) {
515 if (message
.isEndSession()) {
516 handleEndSession(isSync ? destination
: source
);
518 if (message
.getAttachments().isPresent()) {
519 for (SignalServiceAttachment attachment
: message
.getAttachments().get()) {
520 if (attachment
.isPointer()) {
522 retrieveAttachment(attachment
.asPointer());
523 } catch (IOException
| InvalidMessageException e
) {
524 System
.err
.println("Failed to retrieve attachment (" + attachment
.asPointer().getId() + "): " + e
.getMessage());
532 public void receiveMessages(int timeoutSeconds
, boolean returnOnTimeout
, ReceiveMessageHandler handler
) throws IOException
{
533 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(URL
, TRUST_STORE
, username
, password
, signalingKey
, USER_AGENT
);
534 SignalServiceMessagePipe messagePipe
= null;
537 messagePipe
= messageReceiver
.createMessagePipe();
540 SignalServiceEnvelope envelope
;
541 SignalServiceContent content
= null;
542 GroupInfo group
= null;
544 envelope
= messagePipe
.read(timeoutSeconds
, TimeUnit
.SECONDS
);
545 if (!envelope
.isReceipt()) {
546 content
= decryptMessage(envelope
);
547 if (content
!= null) {
548 if (content
.getDataMessage().isPresent()) {
549 SignalServiceDataMessage message
= content
.getDataMessage().get();
550 group
= handleSignalServiceDataMessage(message
, false, envelope
.getSource(), username
);
552 if (content
.getSyncMessage().isPresent()) {
553 SignalServiceSyncMessage syncMessage
= content
.getSyncMessage().get();
554 if (syncMessage
.getSent().isPresent()) {
555 SignalServiceDataMessage message
= syncMessage
.getSent().get().getMessage();
556 group
= handleSignalServiceDataMessage(message
, true, envelope
.getSource(), syncMessage
.getSent().get().getDestination().get());
558 if (syncMessage
.getRequest().isPresent()) {
561 if (syncMessage
.getGroups().isPresent()) {
568 handler
.handleMessage(envelope
, content
, group
);
569 } catch (TimeoutException e
) {
572 } catch (InvalidVersionException e
) {
573 System
.err
.println("Ignoring error: " + e
.getMessage());
577 if (messagePipe
!= null)
578 messagePipe
.shutdown();
582 public File
getAttachmentFile(long attachmentId
) {
583 return new File(attachmentsPath
, attachmentId
+ "");
586 private File
retrieveAttachment(SignalServiceAttachmentPointer pointer
) throws IOException
, InvalidMessageException
{
587 final SignalServiceMessageReceiver messageReceiver
= new SignalServiceMessageReceiver(URL
, TRUST_STORE
, username
, password
, signalingKey
, USER_AGENT
);
589 File tmpFile
= File
.createTempFile("ts_attach_" + pointer
.getId(), ".tmp");
590 InputStream input
= messageReceiver
.retrieveAttachment(pointer
, tmpFile
);
592 new File(attachmentsPath
).mkdirs();
593 File outputFile
= getAttachmentFile(pointer
.getId());
594 OutputStream output
= null;
596 output
= new FileOutputStream(outputFile
);
597 byte[] buffer
= new byte[4096];
600 while ((read
= input
.read(buffer
)) != -1) {
601 output
.write(buffer
, 0, read
);
603 } catch (FileNotFoundException e
) {
607 if (output
!= null) {
611 if (!tmpFile
.delete()) {
612 System
.err
.println("Failed to delete temp file: " + tmpFile
);
615 if (pointer
.getPreview().isPresent()) {
616 File previewFile
= new File(outputFile
+ ".preview");
618 output
= new FileOutputStream(previewFile
);
619 byte[] preview
= pointer
.getPreview().get();
620 output
.write(preview
, 0, preview
.length
);
621 } catch (FileNotFoundException e
) {
625 if (output
!= null) {
633 private String
canonicalizeNumber(String number
) throws InvalidNumberException
{
634 String localNumber
= username
;
635 return PhoneNumberFormatter
.formatNumber(number
, localNumber
);
638 private SignalServiceAddress
getPushAddress(String number
) throws InvalidNumberException
{
639 String e164number
= canonicalizeNumber(number
);
640 return new SignalServiceAddress(e164number
);
644 public boolean isRemote() {