1
1
# -*- coding: utf-8 -*-
2
2
#
3
3
# Handlers dealing with tip interface for receivers (rtip)
4
- import base64
5
4
import copy
6
5
import json
7
6
import os
10
9
11
10
from datetime import datetime , timedelta
12
11
12
+ from nacl .encoding import Base64Encoder
13
13
from twisted .internet .threads import deferToThread
14
14
from twisted .internet .defer import inlineCallbacks , returnValue
15
15
@@ -90,14 +90,14 @@ def db_grant_tip_access(session, tid, user_id, user_cc, itip, rtip, receiver_id)
90
90
91
91
_tip_key = b''
92
92
if itip .crypto_tip_pub_key :
93
- _tip_key = GCE .asymmetric_decrypt (user_cc , base64 . b64decode (rtip .crypto_tip_prv_key ))
93
+ _tip_key = GCE .asymmetric_decrypt (user_cc , Base64Encoder . decode (rtip .crypto_tip_prv_key ))
94
94
_tip_key = GCE .asymmetric_encrypt (new_receiver .crypto_pub_key , _tip_key )
95
95
96
96
new_rtip = db_create_receivertip (session , new_receiver , itip , _tip_key )
97
97
new_rtip .new = False
98
98
if itip .deprecated_crypto_files_pub_key :
99
- _files_key = GCE .asymmetric_decrypt (user_cc , base64 . b64decode (rtip .deprecated_crypto_files_prv_key ))
100
- new_rtip .deprecated_crypto_files_prv_key = base64 . b64encode (
99
+ _files_key = GCE .asymmetric_decrypt (user_cc , Base64Encoder . decode (rtip .deprecated_crypto_files_prv_key ))
100
+ new_rtip .deprecated_crypto_files_prv_key = Base64Encoder . encode (
101
101
GCE .asymmetric_encrypt (new_receiver .crypto_pub_key , _files_key ))
102
102
103
103
wbfiles = session .query (models .WhistleblowerFile ) \
@@ -431,7 +431,7 @@ def db_redact_comment(session, tid, user_id, itip_id, redaction, redaction_data,
431
431
content = redact_content (currentMaskedContent .get ('content' ), new_permanent_redaction )
432
432
433
433
comment = session .query (models .Comment ).get (redaction_data ['reference_id' ])
434
- comment .content = base64 . b64encode (GCE .asymmetric_encrypt (itip_id .crypto_tip_pub_key , content )).decode ()
434
+ comment .content = Base64Encoder . encode (GCE .asymmetric_encrypt (itip_id .crypto_tip_pub_key , content )).decode ()
435
435
436
436
437
437
def db_redact_answers (answers , redaction ):
@@ -484,7 +484,7 @@ def db_redact_answers_recursively(session, tid, user_id, itip_id, redaction, red
484
484
_content = answers
485
485
486
486
if itip_id .crypto_tip_pub_key :
487
- _content = base64 . b64encode (
487
+ _content = Base64Encoder . encode (
488
488
GCE .asymmetric_encrypt (itip_id .crypto_tip_pub_key , json .dumps (_content , cls = JSONEncoder ).encode ())).decode ()
489
489
490
490
itip_answers = session .query (models .InternalTipAnswers ) \
@@ -514,7 +514,7 @@ def db_redact_whistleblower_identity(session, tid, user_id, itip_id, redaction,
514
514
515
515
_content = whistleblower_identity
516
516
if itip_id .crypto_tip_pub_key :
517
- _content = base64 . b64encode (
517
+ _content = Base64Encoder . encode (
518
518
GCE .asymmetric_encrypt (itip_id .crypto_tip_pub_key , json .dumps (_content , cls = JSONEncoder ).encode ())).decode ()
519
519
520
520
itip_whistleblower_identity = session .query (models .InternalTipData ) \
@@ -617,7 +617,7 @@ def register_rfile_on_db(session, tid, user_id, itip_id, uploaded_file):
617
617
for k in ['name' , 'description' , 'type' , 'size' ]:
618
618
if k == 'size' :
619
619
uploaded_file [k ] = str (uploaded_file [k ])
620
- uploaded_file [k ] = base64 . b64encode (GCE .asymmetric_encrypt (itip .crypto_tip_pub_key , uploaded_file [k ]))
620
+ uploaded_file [k ] = Base64Encoder . encode (GCE .asymmetric_encrypt (itip .crypto_tip_pub_key , uploaded_file [k ]))
621
621
622
622
new_file = models .ReceiverFile ()
623
623
new_file .id = uploaded_file ['filename' ]
@@ -660,7 +660,7 @@ def db_get_rtip(session, tid, user_id, itip_id, language):
660
660
661
661
db_log (session , tid = tid , type = 'access_report' , user_id = user_id , object_id = itip .id )
662
662
663
- return serializers .serialize_rtip (session , itip , rtip , language ), base64 . b64decode (rtip .crypto_tip_prv_key )
663
+ return serializers .serialize_rtip (session , itip , rtip , language ), Base64Encoder . decode (rtip .crypto_tip_prv_key )
664
664
665
665
666
666
@transact
@@ -876,7 +876,7 @@ def set_internaltip_variable(session, tid, user_id, itip_id, key, value):
876
876
_ , _ , itip = db_access_rtip (session , tid , user_id , itip_id )
877
877
878
878
if itip .crypto_tip_pub_key and value and key in ['label' ]:
879
- value = base64 . b64encode (GCE .asymmetric_encrypt (itip .crypto_tip_pub_key , value ))
879
+ value = Base64Encoder . encode (GCE .asymmetric_encrypt (itip .crypto_tip_pub_key , value ))
880
880
881
881
setattr (itip , key , value )
882
882
@@ -947,12 +947,12 @@ def create_identityaccessrequest(session, tid, user_id, user_cc, itip_id, reques
947
947
"""
948
948
user , rtip , itip = db_access_rtip (session , tid , user_id , itip_id )
949
949
950
- crypto_tip_prv_key = GCE .asymmetric_decrypt (user_cc , base64 . b64decode (rtip .crypto_tip_prv_key ))
950
+ crypto_tip_prv_key = GCE .asymmetric_decrypt (user_cc , Base64Encoder . decode (rtip .crypto_tip_prv_key ))
951
951
952
952
iar = models .IdentityAccessRequest ()
953
953
iar .internaltip_id = itip .id
954
954
iar .request_user_id = user .id
955
- iar .request_motivation = base64 . b64encode (
955
+ iar .request_motivation = Base64Encoder . encode (
956
956
GCE .asymmetric_encrypt (itip .crypto_tip_pub_key , request ['request_motivation' ]))
957
957
session .add (iar )
958
958
session .flush ()
@@ -962,7 +962,7 @@ def create_identityaccessrequest(session, tid, user_id, user_cc, itip_id, reques
962
962
iarc = models .IdentityAccessRequestCustodian ()
963
963
iarc .identityaccessrequest_id = iar .id
964
964
iarc .custodian_id = custodian .id
965
- iarc .crypto_tip_prv_key = base64 . b64encode (GCE .asymmetric_encrypt (custodian .crypto_pub_key , crypto_tip_prv_key ))
965
+ iarc .crypto_tip_prv_key = Base64Encoder . encode (GCE .asymmetric_encrypt (custodian .crypto_pub_key , crypto_tip_prv_key ))
966
966
session .add (iarc )
967
967
custodians += 1
968
968
@@ -996,7 +996,7 @@ def create_comment(session, tid, user_id, itip_id, content, visibility='public')
996
996
997
997
_content = content
998
998
if itip .crypto_tip_pub_key :
999
- _content = base64 . b64encode (GCE .asymmetric_encrypt (itip .crypto_tip_pub_key , content )).decode ()
999
+ _content = Base64Encoder . encode (GCE .asymmetric_encrypt (itip .crypto_tip_pub_key , content )).decode ()
1000
1000
1001
1001
comment = models .Comment ()
1002
1002
comment .internaltip_id = itip .id
@@ -1028,7 +1028,7 @@ def create_redaction(session, tid, user_id, data):
1028
1028
else :
1029
1029
content_str = data .get ('content' , str (data ))
1030
1030
content_bytes = content_str .encode ()
1031
- mask_content = base64 . b64encode (GCE .asymmetric_encrypt (itip .crypto_tip_pub_key , content_bytes )).decode ()
1031
+ mask_content = Base64Encoder . encode (GCE .asymmetric_encrypt (itip .crypto_tip_pub_key , content_bytes )).decode ()
1032
1032
1033
1033
redaction = models .Redaction ()
1034
1034
redaction .id = data .get ('id' )
@@ -1257,8 +1257,8 @@ def get(self, wbfile_id):
1257
1257
self .check_file_presence (filelocation )
1258
1258
1259
1259
if tip_prv_key :
1260
- tip_prv_key = GCE .asymmetric_decrypt (self .session .cc , base64 . b64decode (tip_prv_key ))
1261
- name = GCE .asymmetric_decrypt (tip_prv_key , base64 . b64decode (name .encode ())).decode ()
1260
+ tip_prv_key = GCE .asymmetric_decrypt (self .session .cc , Base64Encoder . decode (tip_prv_key ))
1261
+ name = GCE .asymmetric_decrypt (tip_prv_key , Base64Encoder . decode (name .encode ())).decode ()
1262
1262
1263
1263
try :
1264
1264
# First attempt
@@ -1268,7 +1268,7 @@ def get(self, wbfile_id):
1268
1268
if not tip_prv_key2 :
1269
1269
raise
1270
1270
1271
- files_prv_key2 = GCE .asymmetric_decrypt (self .session .cc , base64 . b64decode (tip_prv_key2 ))
1271
+ files_prv_key2 = GCE .asymmetric_decrypt (self .session .cc , Base64Encoder . decode (tip_prv_key2 ))
1272
1272
filelocation = GCE .streaming_encryption_open ('DECRYPT' , files_prv_key2 , filelocation )
1273
1273
1274
1274
yield self .write_file_as_download (name , filelocation , pgp_key )
@@ -1307,7 +1307,7 @@ def download_rfile(self, session, tid, user_id, file_id):
1307
1307
except :
1308
1308
raise errors .ResourceNotFound
1309
1309
else :
1310
- return rfile .name , rfile .id , base64 . b64decode (rtip .crypto_tip_prv_key ), pgp_key
1310
+ return rfile .name , rfile .id , Base64Encoder . decode (rtip .crypto_tip_prv_key ), pgp_key
1311
1311
1312
1312
@inlineCallbacks
1313
1313
def get (self , rfile_id ):
@@ -1324,7 +1324,7 @@ def get(self, rfile_id):
1324
1324
1325
1325
if tip_prv_key :
1326
1326
tip_prv_key = GCE .asymmetric_decrypt (self .session .cc , tip_prv_key )
1327
- name = GCE .asymmetric_decrypt (tip_prv_key , base64 . b64decode (name .encode ())).decode ()
1327
+ name = GCE .asymmetric_decrypt (tip_prv_key , Base64Encoder . decode (name .encode ())).decode ()
1328
1328
filelocation = GCE .streaming_encryption_open ('DECRYPT' , tip_prv_key , filelocation )
1329
1329
1330
1330
yield self .write_file_as_download (name , filelocation , pgp_key )
0 commit comments