-
Notifications
You must be signed in to change notification settings - Fork 1
/
serverapi.py
286 lines (225 loc) · 9.82 KB
/
serverapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import requests
import os
from random import SystemRandom
import base64
import json
import hashlib
import time
import ssl
from keyutils import PreKeyUtil, IdentityKeyUtil, RecipientPreKeyUtil
import config
class ServerError(Exception):
def __init__(self , code, message):
self.code = code
self.message = message
def __str__(self):
return self.message
headers = {'Content-type': 'application/json'}
HOST = 'textsecure-service.whispersystems.org'
URL_BASE = "https://" + HOST
#URL_BASE = "http://127.0.0.1:8080"
# bugs.python.org/issue18293
# Monkey patching certificate pinning
with open('certs/' + HOST + '.der', 'rb') as f:
valid_cert = f.read()
def cert_verify(self, conn, url, verify, cert):
# skip built in certificate verification for our host
if not conn.host == HOST:
orig_cert_verify(self, conn, url, verify, cert)
return
def do_handshake(self, block=False):
orig_do_handshake(self, block=False)
if self.server_hostname == HOST:
# Ensure certificate is the pinned one
if self.getpeercert(True) != valid_cert:
raise ssl.SSLError('Did not match pinned certificate')
orig_do_handshake = ssl.SSLSocket.do_handshake
ssl.SSLSocket.do_handshake = do_handshake
orig_cert_verify = requests.adapters.HTTPAdapter.cert_verify
requests.adapters.HTTPAdapter.cert_verify = cert_verify
def requestVerificationCode(phone_number):
r = requests.get( URL_BASE + '/v1/accounts/sms/code/' + phone_number)
print(r.status_code)
if r.status_code in (400 , 404):
raise ServerError(r.status_code, 'The phone number was badly formatted.')
elif r.status_code == 413:
raise ServerError(r.status_code, 'Rate limit exceeded.')
elif r.status_code == 415:
raise ServerError(r.status_code, 'Invalid transport.')
elif r.status_code == 200:
return
def confirmVerificationCode(phone_number, token):
password = base64.b64encode(os.urandom(16))[:-2].decode('utf-8')
signalingKey = os.urandom(52)
registrationId = SystemRandom().randint(1, 16380 + 1)
payload = {
"signalingKey" : base64.b64encode(signalingKey).decode('utf-8'),
"supportsSms" : False,
"fetchesMessages" : False,
"registrationId" : registrationId
}
r = requests.put( URL_BASE + '/v1/accounts/code/' + token,
headers=headers,
auth=(phone_number, password),
data=json.dumps(payload) )
if r.status_code in (400 , 404):
raise ServerError(r.status_code, 'The token was badly formatted.')
elif r.status_code == 401:
raise ServerError(r.status_code, 'Badly formatted basic auth.')
elif r.status_code == 403:
raise ServerError(r.status_code, 'Verification code was incorrect.')
elif r.status_code == 413:
raise ServerError(r.status_code, 'Rate limit exceeded.')
elif r.status_code == 417:
raise ServerError(r.status_code, 'Number already registered.')
elif r.status_code in (200, 204):
config.setConfigOption('phone_number', phone_number)
config.setConfigOption('password', password)
config.setConfigOption('signalingKey', signalingKey)
config.setConfigOption('registrationId', str(registrationId))
return
def registerGCMid():
regid = config.getConfigOption('gcmregid').decode('utf-8')
phone_number = config.getConfigOption('phone_number').decode('utf-8')
password = config.getConfigOption('password').decode('utf-8')
data = { 'gcmRegistrationId' : regid }
r = requests.put( URL_BASE + '/v1/accounts/gcm/',
headers=headers,
auth=(phone_number, password),
data=json.dumps(data)
)
if r.status_code == 401:
raise ServerError(r.status_code, 'Invalid authentication credentials.')
elif r.status_code == 415:
raise ServerError(r.status_code, 'Badly formatted JSON.')
elif r.status_code in (200, 204):
return r.content
def registerPreKeys():
#get auth data
phone_number = config.getConfigOption('phone_number').decode('utf-8')
password = config.getConfigOption('password').decode('utf-8')
#generate keys
PreKeyUtil().generatePreKeys()
PreKeyUtil().generateLastResortKey()
IdentityKeyUtil().generateIdentityKey()
records = PreKeyUtil().getPreKeys()
preKeys = list()
for preKey in records:
preKeys.append(
{
'keyId' : preKey.keyId,
'publicKey' : base64.b64encode(b'\x05' + preKey.publicKey).decode("utf-8"),
'identityKey' : base64.b64encode(b'\x05' + IdentityKeyUtil().getIdentityKey()).decode("utf-8")
}
)
lastResortKey = PreKeyUtil().getLastResortKey()
lastResortKeyDict = {
'keyId' : lastResortKey.keyId,
'publicKey' : base64.b64encode(b'\x05' + lastResortKey.publicKey).decode("utf-8"),
'identityKey' : base64.b64encode(b'\x05' + IdentityKeyUtil().getIdentityKey()).decode("utf-8")
}
data = { 'lastResortKey' : lastResortKeyDict , 'keys' : preKeys}
r = requests.put( URL_BASE + '/v1/keys/',
headers=headers,
auth=(phone_number, password),
data=json.dumps(data) )
if r.status_code == 401:
raise ServerError(r.status_code, 'Invalid authentication credentials.')
elif r.status_code == 415:
raise ServerError(r.status_code, 'Badly formatted JSON.')
elif r.status_code in (200, 204):
return
def getContactIntersection(contacts):
#get auth data
phone_number = config.getConfigOption('phone_number').decode('utf-8')
password = config.getConfigOption('password').decode('utf-8')
contacts = [ '+611231231234', '+611231231235' ]
tokens = []
for contact in contacts:
thash = hashlib.sha1(contact.encode('utf-8')).digest()[0:10]
tokens.append(base64.b64encode(thash).decode('utf-8').rstrip('='))
data = { 'contacts' : tokens }
r = requests.put( URL_BASE + '/v1/directory/tokens',
headers=headers,
auth=(phone_number, password),
data=json.dumps(data) )
if r.status_code == 400:
raise ServerError(r.status_code, 'Badly formatted tokens.')
if r.status_code == 401:
raise ServerError(r.status_code, 'Invalid authentication credentials.')
elif r.status_code == 415:
raise ServerError(r.status_code, 'Badly formatted JSON.')
elif r.status_code in (200, 204):
print(r.content)
return r.content
def getRecipientsPreKeyList(number):
print('Getting recipients prekey')
#get auth data
phone_number = config.getConfigOption('phone_number').decode('utf-8')
password = config.getConfigOption('password').decode('utf-8')
r = requests.get(URL_BASE + '/v1/keys/' + number + '/*',
headers=headers,
auth=(phone_number, password))
if r.status_code == 401:
raise ServerError(r.status_code, 'Invalid authentication credentials.')
elif r.status_code == 404:
raise ServerError(r.status_code, 'Unknown/unregistered number.')
elif r.status_code == 413:
raise ServerError(r.status_code, 'Rate limit exceeded.')
elif r.status_code in (200, 204):
RecipientPreKeyUtil().saveRecipientPreKey(number, json.loads(r.content.decode('utf-8')))
preKeyList = createPreKeyListFromJson(r.content)
return preKeyList
import ratcheting_session
def createPreKeyListFromJson(serialized):
data = json.loads(serialized.decode('utf-8'))
keys = list()
for key in data['keys']:
publicKey = ratcheting_session.Curve().decodePoint( base64.b64decode( key['publicKey'] ) )
identityKey = ratcheting_session.Curve().decodePoint( base64.b64decode( key['identityKey'] ) )
keys.append( PreKeyEntity( key['deviceId'], key['keyId'], publicKey, identityKey, key['registrationId'] ) )
preKeyList = PreKeyList('', keys)
return preKeyList
class PreKeyList:
def __init__(self, lastResortKey, keys):
self.keys = keys
self.lastResortKey = lastResortKey
class PreKeyEntity:
def __init__(self, deviceId, keyId, publicKey, identityKey, registrationId):
self.deviceId = deviceId
self.keyId = keyId
self.publicKey = publicKey
self.identityKey = identityKey
self.registrationId = registrationId
def submitMessage(messages):
#get auth data
phone_number = config.getConfigOption('phone_number').decode('utf-8')
password = config.getConfigOption('password').decode('utf-8')
for message in messages.messages:
data = {
'messages': [{
'type': message.type,
'destinationDeviceId': 1,
'destinationRegistrationId': message.remoteRegistrationId,
'body': base64.b64encode(message.body).decode('utf-8'),
'timestamp': int(time.time())
}]
}
r = requests.put( URL_BASE + '/v1/messages/' + messages.destination,
headers=headers,
auth=(phone_number, password),
data=json.dumps(data) )
if r.status_code == 401:
raise ServerError(r.status_code, 'Invalid authentication credentials.')
elif r.status_code == 409:
#this error returns data
raise ServerError(r.status_code, 'Mismatched devices.')
elif r.status_code == 410:
#this error returns data
raise ServerError(r.status_code, 'Stale devices.')
elif r.status_code == 413:
raise ServerError(r.status_code, 'Rate limit exceeded.')
elif r.status_code == 415:
raise ServerError(r.status_code, 'Badly formatted JSON.')
elif r.status_code in (200, 204):
return r.content