-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
254 lines (201 loc) · 9.11 KB
/
server.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
import json
from base64 import b64decode, b64encode
from os import mkdir, remove
from random import uniform
from secrets import token_bytes, token_hex
from flask import Flask, request, send_file
from nacl.bindings import crypto_scalarmult
from nacl.encoding import Base64Encoder, HexEncoder
from nacl.public import Box, PrivateKey, PublicKey
from nacl.signing import VerifyKey
from redis import Redis
import commons
import pki
from time import sleep
# bootstrap keys
intermediate_verifying_key = pki.verify_root_intermediate()
redis = Redis()
app = Flask(__name__)
try:
mkdir(f"{commons.UPLOADS}")
except Exception as e:
print(e)
pass
@app.route("/")
def index():
return {"status": "OK"}, 200
@app.route("/journalists", methods=["POST"])
def add_journalist():
content = request.json
try:
assert ("journalist_key" in content)
assert ("journalist_sig" in content)
assert ("journalist_fetching_key" in content)
assert ("journalist_fetching_sig" in content)
except Exception:
return {"status": "KO"}, 400
journalist_verifying_key = VerifyKey(content["journalist_key"], Base64Encoder)
journalist_fetching_public_key = PublicKey(content["journalist_fetching_key"], Base64Encoder)
try:
journalist_sig = pki.verify_key_func(intermediate_verifying_key,
journalist_verifying_key,
None,
content["journalist_sig"])
journalist_fetching_sig = pki.verify_key_func(journalist_verifying_key,
journalist_fetching_public_key,
None,
content["journalist_fetching_sig"])
except Exception:
return {"status": "KO"}, 400
redis.sadd("journalists", json.dumps({"journalist_key": journalist_verifying_key.encode(Base64Encoder).decode("ascii"),
"journalist_sig": journalist_sig,
"journalist_fetching_key": journalist_fetching_public_key.encode(Base64Encoder).decode('ascii'),
"journalist_fetching_sig": journalist_fetching_sig}))
return {"status": "OK"}, 200
@app.route("/journalists", methods=["GET"])
def get_journalists():
journalists_list = []
journalists = redis.smembers("journalists")
for journalist_json in journalists:
journalists_list.append(json.loads(journalist_json.decode("ascii")))
return {"status": "OK", "count": len(journalists), "journalists": journalists_list}, 200
@app.route("/file", methods=["POST"])
def download_file():
try:
assert ('file' in request.files)
file = request.files['file']
assert (len(file.filename) > 0)
except Exception:
return {"status": "KO"}, 400
file_name = token_hex(32)
file_id = token_hex(32)
redis.set(f"file:{file_id}", file_name.encode("ascii"))
file.save(f"{commons.UPLOADS}{file_name}.enc")
return {"status": "OK", "file_id": file_id}, 200
@app.route("/file/<file_id>", methods=["GET"])
def get_file(file_id):
file_name = redis.get(f"file:{file_id}")
if not file_name:
return {"status": "KO"}, 404
else:
file_name = file_name.decode('ascii')
return send_file(f"{commons.UPLOADS}{file_name}.enc")
@app.route("/file/<file_id>", methods=["DELETE"])
def delete_file(file_id):
file = redis.get(f"file:{file_id}")
if not file:
return {"status": "KO"}, 404
else:
file = file.decode('ascii')
redis.delete(f"file:{file_id}")
remove(f"{commons.UPLOADS}{file}.enc")
return {"status": "OK"}, 200
@app.route("/ephemeral_keys", methods=["POST"])
def add_ephemeral_keys():
content = request.json
try:
assert ("journalist_key" in content)
assert ("ephemeral_keys" in content)
except Exception:
return {"status": "KO"}, 400
journalist_key = content["journalist_key"]
journalists = redis.smembers("journalists")
for journalist in journalists:
journalist_dict = json.loads(journalist.decode("ascii"))
if journalist_dict["journalist_key"] == journalist_key:
journalist_verifying_key = VerifyKey(journalist_dict["journalist_key"], Base64Encoder)
ephemeral_keys = content["ephemeral_keys"]
for ephemeral_key_dict in ephemeral_keys:
ephemeral_key_verifying_key = VerifyKey(ephemeral_key_dict["ephemeral_key"], Base64Encoder)
ephemeral_sig = pki.verify_key_func(
journalist_verifying_key,
ephemeral_key_verifying_key,
None,
ephemeral_key_dict["ephemeral_sig"])
redis.sadd(f"journalist:{journalist_verifying_key.encode(HexEncoder).decode('ascii')}",
json.dumps({"ephemeral_key": ephemeral_key_verifying_key.encode(Base64Encoder).decode("ascii"),
"ephemeral_sig": ephemeral_sig}))
return {"status": "OK"}, 200
@app.route("/ephemeral_keys", methods=["GET"])
def get_ephemeral_keys():
journalists = redis.smembers("journalists")
ephemeral_keys = []
for journalist in journalists:
journalist_dict = json.loads(journalist.decode("ascii"))
ephemeral_key_dict = json.loads(redis.spop(f"journalist:{VerifyKey(journalist_dict['journalist_key'], Base64Encoder).encode(HexEncoder).decode('ascii')}").decode("ascii"))
ephemeral_key_dict["journalist_key"] = journalist_dict["journalist_key"]
ephemeral_keys.append(ephemeral_key_dict)
return {"status": "OK", "count": len(ephemeral_keys), "ephemeral_keys": ephemeral_keys}, 200
@app.route("/fetch", methods=["GET"])
def get_fetch():
potential_messages = []
# retrieve all the message keys
message_keys = redis.keys("message:*")
for message_key in message_keys:
# SERVER EPHEMERAL CHALLENGE KEY
request_ephemeral_key = PrivateKey.generate()
message_id = message_key.decode('ascii').split(":")[1]
# retrieve the message and load the json
message_dict = json.loads(redis.get(message_key).decode('ascii'))
message_server_gdh = crypto_scalarmult(request_ephemeral_key.encode(), b64decode(message_dict["message_public_key"]))
# calculate the shared key for message_id encryption
box = Box(request_ephemeral_key, PublicKey(message_dict["message_gdh"], encoder=Base64Encoder))
encrypted_message_id = box.encrypt(message_id.encode('ascii'))
potential_messages.append({"gdh": b64encode(message_server_gdh).decode('ascii'),
"enc": b64encode(encrypted_message_id).decode('ascii')})
# add DECOY potential messages
# TODO: add shuffling of the response dict
for decoy in range(commons.MAX_MESSAGES - len(potential_messages)):
potential_messages.append({
"gdh": PrivateKey.generate().encode(Base64Encoder).decode('ascii'),
# message_id are 32 bytes and encryption overhead is 64 bytes
"enc": b64encode(token_bytes(32+72)).decode('ascii')
}
)
# TODO: add stronger timing attack mitigations (such as a random delay)
sleep(uniform(0, 2.0))
assert (len(potential_messages) == commons.MAX_MESSAGES)
# padding to hide the number of messages to be added later
response_dict = {"status": "OK",
"count": len(potential_messages),
"messages": potential_messages}
return response_dict, 200
@app.route("/message", methods=["POST"])
def send():
content = request.json
try:
assert ("message_ciphertext" in content)
assert ("message_public_key" in content)
assert ("message_gdh" in content)
except Exception:
return {"status": "KO"}, 400
message_dict = {
# encrypted message
"message_ciphertext": request.json["message_ciphertext"],
# gj, public key part of the keypair generated by the sending journalist for every message
"message_public_key": request.json["message_public_key"],
# gkj, public part computed using the source public key and the per-message secret key
"message_gdh": request.json["message_gdh"]
}
# save the journalist to source reply in redis
redis.set(f"message:{token_hex(32)}", json.dumps(message_dict))
return {"status": "OK"}, 200
@app.route("/message/<message_id>", methods=["GET"])
def get_message(message_id):
assert (len(message_id) == 64)
message = redis.get(f"message:{message_id}")
if message is not None:
message_dict = json.loads(message.decode('ascii'))
del message_dict["message_gdh"]
response = {"status": "OK", "message": message_dict}
return response, 200
else:
return {"status": "KO"}, 404
@app.route("/message/<message_id>", methods=["DELETE"])
def delete_message(message_id):
assert (len(message_id) == 64)
res = redis.delete(f"message:{message_id}")
if res > 0:
return {"status": "OK"}, 200
else:
return {"status": "KO"}, 404