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
.guava
.Optional
;
28 import org
.whispersystems
.textsecure
.api
.TextSecureAccountManager
;
29 import org
.whispersystems
.textsecure
.api
.TextSecureMessagePipe
;
30 import org
.whispersystems
.textsecure
.api
.TextSecureMessageReceiver
;
31 import org
.whispersystems
.textsecure
.api
.TextSecureMessageSender
;
32 import org
.whispersystems
.textsecure
.api
.crypto
.TextSecureCipher
;
33 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureContent
;
34 import org
.whispersystems
.textsecure
.api
.messages
.TextSecureEnvelope
;
35 import org
.whispersystems
.textsecure
.api
.push
.TextSecureAddress
;
36 import org
.whispersystems
.textsecure
.api
.push
.TrustStore
;
39 import java
.util
.List
;
40 import java
.util
.concurrent
.TimeUnit
;
41 import java
.util
.concurrent
.TimeoutException
;
43 public class Manager
{
44 private final static String URL
= "https://textsecure-service.whispersystems.org";
45 private final static TrustStore TRUST_STORE
= new WhisperTrustStore();
47 private final static String settingsPath
= System
.getProperty("user.home") + "/.config/textsecure";
49 private String username
;
50 private String password
;
51 private String signalingKey
;
53 private boolean registered
= false;
55 private JsonAxolotlStore axolotlStore
;
56 TextSecureAccountManager accountManager
;
58 public Manager(String username
) {
59 this.username
= username
;
62 private String
getFileName() {
63 String path
= settingsPath
+ "/data";
64 new File(path
).mkdirs();
65 return path
+ "/" + username
;
68 public boolean userExists() {
69 File f
= new File(getFileName());
70 if (!f
.exists() || f
.isDirectory()) {
76 public boolean userHasKeys() {
77 return axolotlStore
!= null;
80 public void load() throws IOException
, InvalidKeyException
{
81 JSONObject
in = new JSONObject(IOUtils
.toString(new FileInputStream(getFileName())));
82 username
= in.getString("username");
83 password
= in.getString("password");
84 if (in.has("signalingKey")) {
85 signalingKey
= in.getString("signalingKey");
87 axolotlStore
= new JsonAxolotlStore(in.getJSONObject("axolotlStore"));
88 registered
= in.getBoolean("registered");
89 accountManager
= new TextSecureAccountManager(URL
, TRUST_STORE
, username
, password
);
93 String out
= new JSONObject().put("username", username
)
94 .put("password", password
)
95 .put("signalingKey", signalingKey
)
96 .put("axolotlStore", axolotlStore
.getJson())
97 .put("registered", registered
).toString();
99 OutputStreamWriter writer
= new OutputStreamWriter(new FileOutputStream(getFileName()));
103 } catch (Exception e
) {
104 System
.out
.println("Saving file error: " + e
.getMessage());
109 public void createNewIdentity() {
110 IdentityKeyPair identityKey
= KeyHelper
.generateIdentityKeyPair();
111 int registrationId
= KeyHelper
.generateRegistrationId(false);
112 axolotlStore
= new JsonAxolotlStore(identityKey
, registrationId
);
116 public boolean isRegistered() {
120 public void register() throws IOException
{
121 password
= Util
.getSecret(18);
123 accountManager
= new TextSecureAccountManager(URL
, TRUST_STORE
, username
, password
);
125 accountManager
.requestSmsVerificationCode();
129 public void verifyAccount(String verificationCode
) throws IOException
{
130 verificationCode
= verificationCode
.replace("-", "");
131 signalingKey
= Util
.getSecret(52);
132 accountManager
.verifyAccount(verificationCode
, signalingKey
, false, axolotlStore
.getLocalRegistrationId());
134 //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
137 List
<PreKeyRecord
> oneTimePreKeys
= KeyHelper
.generatePreKeys(start
, 100);
138 PreKeyRecord lastResortKey
= KeyHelper
.generateLastResortPreKey();
139 int signedPreKeyId
= 0;
140 SignedPreKeyRecord signedPreKeyRecord
;
142 signedPreKeyRecord
= KeyHelper
.generateSignedPreKey(axolotlStore
.getIdentityKeyPair(), signedPreKeyId
);
143 } catch (InvalidKeyException e
) {
144 // Should really not happen
145 System
.out
.println("invalid key");
148 accountManager
.setPreKeys(axolotlStore
.getIdentityKeyPair().getPublicKey(), lastResortKey
, signedPreKeyRecord
, oneTimePreKeys
);
151 public TextSecureMessageSender
getMessageSender() {
152 return new TextSecureMessageSender(URL
, TRUST_STORE
, username
, password
,
153 axolotlStore
, Optional
.<TextSecureMessageSender
.EventListener
>absent());
156 public TextSecureContent
receiveMessage() throws IOException
, InvalidVersionException
{
157 TextSecureMessageReceiver messageReceiver
= new TextSecureMessageReceiver(URL
, TRUST_STORE
, username
, password
, signalingKey
);
158 TextSecureMessagePipe messagePipe
= null;
161 messagePipe
= messageReceiver
.createMessagePipe();
163 TextSecureEnvelope envelope
;
165 envelope
= messagePipe
.read(5, TimeUnit
.SECONDS
);
166 } catch (TimeoutException e
) {
169 TextSecureCipher cipher
= new TextSecureCipher(new TextSecureAddress(username
), axolotlStore
);
170 TextSecureContent message
= null;
172 message
= cipher
.decrypt(envelope
);
173 } catch (Exception e
) {
174 // TODO handle all exceptions
179 if (messagePipe
!= null)
180 messagePipe
.shutdown();