-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add decryption helper #65
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,7 +99,18 @@ def __enter__(self) -> tarfile.TarFile: | |
return self._tar | ||
|
||
# Encrypted/Decrypted Tarfile | ||
self._open_file() | ||
self._setup_cipher() | ||
|
||
self._tar = tarfile.open( | ||
fileobj=self, | ||
mode=self._tar_mode, | ||
dereference=False, | ||
bufsize=self._bufsize, | ||
) | ||
return self._tar | ||
|
||
def _open_file(self) -> None: | ||
if self._fileobj: | ||
# If we have a fileobj, we don't need to open a file | ||
self._file = self._fileobj | ||
|
@@ -113,6 +124,7 @@ def __enter__(self) -> tarfile.TarFile: | |
fd = os.open(self._name, file_mode, 0o666) | ||
self._file = os.fdopen(fd, "rb" if read_mode else "wb") | ||
|
||
def _setup_cipher(self) -> None: | ||
# Extract IV for CBC | ||
if self._mode == MOD_READ: | ||
cbc_rand = self._file.read(16) | ||
|
@@ -131,26 +143,66 @@ def __enter__(self) -> tarfile.TarFile: | |
self._encrypt = self._aes.encryptor() | ||
self._padder = padding.PKCS7(BLOCK_SIZE_BITS).padder() | ||
|
||
self._tar = tarfile.open( | ||
fileobj=self, | ||
mode=self._tar_mode, | ||
dereference=False, | ||
bufsize=self._bufsize, | ||
) | ||
return self._tar | ||
|
||
def __exit__(self, exc_type, exc_value, traceback) -> None: | ||
"""Close file.""" | ||
if self._tar: | ||
self._tar.close() | ||
self._tar = None | ||
self._close_file() | ||
|
||
def _close_file(self) -> None: | ||
"""Close file.""" | ||
if self._file: | ||
if not self._mode.startswith("r"): | ||
self._file.write(self._encrypt.update(self._padder.finalize())) | ||
if not self._fileobj: | ||
self._file.close() | ||
self._file = None | ||
|
||
@contextmanager | ||
def decrypt(self, tarinfo: tarfile.TarInfo) -> Generator[BinaryIO, None, None]: | ||
"""Decrypt inner tar. | ||
|
||
This is a helper to decrypt data and discard the padding. | ||
""" | ||
|
||
class DecryptInnerTar: | ||
"""Decrypt inner tar file.""" | ||
|
||
def __init__(self, parent: SecureTarFile) -> None: | ||
"""Initialize.""" | ||
self._parent = parent | ||
self._pos = 0 | ||
self._size = tarinfo.size | ||
self._tail = b"" | ||
|
||
def read(self, size: int = 0) -> bytes: | ||
"""Read data.""" | ||
if self._tail: | ||
# Finish reading tail | ||
data = self._tail[:size] | ||
self._tail = self._tail[size:] | ||
return data | ||
|
||
data = self._parent.read(size) | ||
self._pos += len(data) | ||
if not data or self._size - self._pos > 16: | ||
return data | ||
|
||
# Last block, read tail and discard padding | ||
data += self._parent.read(self._size - self._pos) | ||
padding_len = data[-1] | ||
data = data[:-padding_len] | ||
self._tail = data[size:] | ||
Comment on lines
+195
to
+196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could check the padding is as expected and raise |
||
return data[:size] | ||
|
||
try: | ||
self._open_file() | ||
self._setup_cipher() | ||
yield DecryptInnerTar(self) | ||
finally: | ||
self._close_file() | ||
|
||
def write(self, data: bytes) -> None: | ||
"""Write data.""" | ||
data = self._padder.update(data) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should maybe raise if this method is called when key is not set?
Maybe we should add error handling in a follow-up?