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/>.
19 import org
.apache
.commons
.io
.IOUtils
;
20 import org
.json
.JSONObject
;
21 import org
.whispersystems
.libaxolotl
.IdentityKeyPair
;
22 import org
.whispersystems
.libaxolotl
.InvalidKeyException
;
23 import org
.whispersystems
.libaxolotl
.InvalidVersionException
;
24 import org
.whispersystems
.libaxolotl
.state
.PreKeyRecord
;
25 import org
.whispersystems
.libaxolotl
.state
.SignedPreKeyRecord
;
26 import org
.whispersystems
.libaxolotl
.util
.KeyHelper
;
27 import org
.whispersystems
.libaxolotl
.util
.Medium
;
28 import org
.whispersystems
.libaxolotl
.util
.guava
.Optional
;
29 import org
.whispersystems
.textsecure
.api
.TextSecureAccountManager
;
30 import org
.whispersystems
.textsecure
.api
.TextSecureMessagePipe
;
31 import org
.whispersystems
.textsecure
.api
.TextSecureMessageReceiver
;
32 import org
.whispersystems
.textsecure
.api
.TextSecureMessageSender
;
33 import org
.whispersystems
.textsecure
.api
.crypto
.TextSecureCipher
;
34 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureContent
;
35 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureEnvelope
;
36 import org
.whispersystems
.textsecure
.api
.push
.TextSecureAddress
;
37 import org
.whispersystems
.textsecure
.api
.push
.TrustStore
;
38 import org
.whispersystems
.textsecure
.api
.util
.InvalidNumberException
;
39 import org
.whispersystems
.textsecure
.api
.util
.PhoneNumberFormatter
;
42 import java
.util
.List
;
43 import java
.util
.concurrent
.TimeUnit
;
44 import java
.util
.concurrent
.TimeoutException
;
46 public class Manager
{
47 private final static String URL
= "https://textsecure-service.whispersystems.org";
48 private final static TrustStore TRUST_STORE
= new WhisperTrustStore();
50 private final static String settingsPath
= System
.getProperty("user.home") + "/.config/textsecure";
52 private String username
;
53 private String password
;
54 private String signalingKey
;
56 private boolean registered
= false;
58 private JsonAxolotlStore axolotlStore
;
59 TextSecureAccountManager accountManager
;
61 public Manager(String username
) {
62 this.username
= username
;
65 private String
getFileName() {
66 String path
= settingsPath
+ "/data";
67 new File(path
).mkdirs();
68 return path
+ "/" + username
;
71 public boolean userExists() {
72 File f
= new File(getFileName());
73 if (!f
.exists() || f
.isDirectory()) {
79 public boolean userHasKeys() {
80 return axolotlStore
!= null;
83 public void load() throws IOException
, InvalidKeyException
{
84 JSONObject
in = new JSONObject(IOUtils
.toString(new FileInputStream(getFileName())));
85 username
= in.getString("username");
86 password
= in.getString("password");
87 if (in.has("signalingKey")) {
88 signalingKey
= in.getString("signalingKey");
90 axolotlStore
= new JsonAxolotlStore(in.getJSONObject("axolotlStore"));
91 registered
= in.getBoolean("registered");
92 accountManager
= new TextSecureAccountManager(URL
, TRUST_STORE
, username
, password
);
96 String out
= new JSONObject().put("username", username
)
97 .put("password", password
)
98 .put("signalingKey", signalingKey
)
99 .put("axolotlStore", axolotlStore
.getJson())
100 .put("registered", registered
).toString();
102 OutputStreamWriter writer
= new OutputStreamWriter(new FileOutputStream(getFileName()));
106 } catch (Exception e
) {
107 System
.out
.println("Saving file error: " + e
.getMessage());
112 public void createNewIdentity() {
113 IdentityKeyPair identityKey
= KeyHelper
.generateIdentityKeyPair();
114 int registrationId
= KeyHelper
.generateRegistrationId(false);
115 axolotlStore
= new JsonAxolotlStore(identityKey
, registrationId
);
119 public boolean isRegistered() {
123 public void register(boolean voiceVerication
) throws IOException
{
124 password
= Util
.getSecret(18);
126 accountManager
= new TextSecureAccountManager(URL
, TRUST_STORE
, username
, password
);
129 accountManager
.requestVoiceVerificationCode();
131 accountManager
.requestSmsVerificationCode();
136 public void verifyAccount(String verificationCode
) throws IOException
{
137 verificationCode
= verificationCode
.replace("-", "");
138 signalingKey
= Util
.getSecret(52);
139 accountManager
.verifyAccount(verificationCode
, signalingKey
, false, axolotlStore
.getLocalRegistrationId());
141 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
145 List
<PreKeyRecord
> oneTimePreKeys
= KeyHelper
.generatePreKeys(start
, 100);
146 for (int i
= start
; i
< oneTimePreKeys
.size(); i
++) {
147 axolotlStore
.storePreKey(i
, oneTimePreKeys
.get(i
));
150 PreKeyRecord lastResortKey
= KeyHelper
.generateLastResortPreKey();
151 axolotlStore
.storePreKey(Medium
.MAX_VALUE
, lastResortKey
);
153 int signedPreKeyId
= 0;
154 SignedPreKeyRecord signedPreKeyRecord
;
156 signedPreKeyRecord
= KeyHelper
.generateSignedPreKey(axolotlStore
.getIdentityKeyPair(), signedPreKeyId
);
157 axolotlStore
.storeSignedPreKey(signedPreKeyId
, signedPreKeyRecord
);
158 } catch (InvalidKeyException e
) {
159 // Should really not happen
160 System
.out
.println("invalid key");
163 accountManager
.setPreKeys(axolotlStore
.getIdentityKeyPair().getPublicKey(), lastResortKey
, signedPreKeyRecord
, oneTimePreKeys
);
166 public TextSecureMessageSender
getMessageSender() {
167 return new TextSecureMessageSender(URL
, TRUST_STORE
, username
, password
,
168 axolotlStore
, Optional
.<TextSecureMessageSender
.EventListener
>absent());
171 public TextSecureContent
decryptMessage(TextSecureEnvelope envelope
) {
172 TextSecureCipher cipher
= new TextSecureCipher(new TextSecureAddress(username
), axolotlStore
);
174 return cipher
.decrypt(envelope
);
175 } catch (Exception e
) {
176 // TODO handle all exceptions
182 public void handleEndSession(String source
) {
183 axolotlStore
.deleteAllSessions(source
);
186 public interface ReceiveMessageHandler
{
187 void handleMessage(TextSecureEnvelope envelope
);
190 public void receiveMessages(int timeoutSeconds
, boolean returnOnTimeout
, ReceiveMessageHandler handler
) throws IOException
{
191 TextSecureMessageReceiver messageReceiver
= new TextSecureMessageReceiver(URL
, TRUST_STORE
, username
, password
, signalingKey
);
192 TextSecureMessagePipe messagePipe
= null;
195 messagePipe
= messageReceiver
.createMessagePipe();
198 TextSecureEnvelope envelope
;
200 envelope
= messagePipe
.read(timeoutSeconds
, TimeUnit
.SECONDS
);
201 handler
.handleMessage(envelope
);
202 } catch (TimeoutException e
) {
205 } catch (InvalidVersionException e
) {
206 System
.out
.println("Ignoring error: " + e
.getMessage());
211 if (messagePipe
!= null)
212 messagePipe
.shutdown();
216 public String
canonicalizeNumber(String number
) throws InvalidNumberException
{
217 String localNumber
= username
;
218 return PhoneNumberFormatter
.formatNumber(number
, localNumber
);
221 protected TextSecureAddress
getPushAddress(String number
) throws InvalidNumberException
{
222 String e164number
= canonicalizeNumber(number
);
223 return new TextSecureAddress(e164number
);