From 0ad5efec388db4f8e9233f4dfd5de716f680549e Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 25 Jun 2019 14:16:25 +0900 Subject: Fix up error handling --- filecrypt.py | 45 +++++++++++++++++++++++++-------------------- server.py | 23 +++++++++++++++-------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/filecrypt.py b/filecrypt.py index ce6e3a1..85d9eea 100644 --- a/filecrypt.py +++ b/filecrypt.py @@ -8,6 +8,7 @@ import os FILE_ID_LENGTH = 22 TOKEN_LENGTH = 22 +HEADER_LENGTH = 56 def encrypt_file(filename_in, chunk_size=1000000//16): file_id = secrets.token_urlsafe(16) @@ -24,6 +25,7 @@ def encrypt_file(filename_in, chunk_size=1000000//16): fout.write(token_tag) # 16 bytes fout.write(auth_secret) # 16 bytes fout.write(data_nonce) # 8 bytes + assert fout.tell() == HEADER_LENGTH cipher = AES.new(key, AES.MODE_CTR, initial_value=struct.pack('Q', seek // token_cipher.block_size), nonce=data_nonce) - print('Seeking to position', seek, 'iv', seek // token_cipher.block_size, 'pos', seek - seek % - cipher.block_size, 'offset', seek % cipher.block_size) - fin.seek(seek - seek % cipher.block_size, 1) - offset = seek % cipher.block_size + def generator(): + with open(filename, 'rb') as fin: + fin.seek(HEADER_LENGTH) + # Use token cipher's block size assuming the two are the same + cipher = AES.new(key, AES.MODE_CTR, + initial_value=struct.pack('>Q', seek // token_cipher.block_size), nonce=data_nonce) + fin.seek(seek - seek % cipher.block_size, 1) + offset = seek % cipher.block_size - to_send = end - seek + 1 if end else None - block = fin.read(cipher.block_size*chunk_size) - while block and (to_send is None or to_send > 0): - data = cipher.decrypt(block) - out = data[offset:to_send] - yield out - if to_send is not None: - to_send -= len(out) - offset = 0 + to_send = end - seek + 1 if end else None block = fin.read(cipher.block_size*chunk_size) + while block and (to_send is None or to_send > 0): + data = cipher.decrypt(block) + out = data[offset:to_send] + yield out + if to_send is not None: + to_send -= len(out) + offset = 0 + block = fin.read(cipher.block_size*chunk_size) + return generator() diff --git a/server.py b/server.py index 9df3546..b084298 100644 --- a/server.py +++ b/server.py @@ -18,25 +18,32 @@ def download(file_id, token, filename): abort(400, 'Invalid token format') path = f'{file_id}.enc' + if not os.path.isfile(path): + abort(403) # forbidden + size = filecrypt.payload_size(path) range_header = re.match('^bytes=([0-9]+)-([0-9]*)$', request.headers.get('Range', '')) if not range_header: - response = Response( - filecrypt.decrypt_generator(path, token), - mimetype='application/octet-stream') + try: + generator = filecrypt.decrypt_generator(path, token) + except ValueError: # MAC check failed + abort(403) # forbidden + + response = Response(generator, mimetype='application/octet-stream') response.headers['Content-Length'] = size else: range_start, range_end = range_header.groups() range_start = int(range_start) range_end = int(range_end) if range_end else size-1 if range_start < 0 or range_end >= size or range_start >= range_end: - abort(416) + abort(416) # range not satisfiable - response = Response( - filecrypt.decrypt_generator(path, token, seek=range_start, end=range_end), - status = 206, - mimetype='application/octet-stream') + try: + generator = filecrypt.decrypt_generator(path, token, seek=range_start, end=range_end) + except ValueError: # MAC check failed + abort(403) # forbidden + response = Response(generator, status=206, mimetype='application/octet-stream') response.headers['Content-Range'] = f'bytes {range_start}-{range_end}/{size}' response.headers['Content-Length'] = range_end - range_start + 1 -- cgit