Skip to content

Commit

Permalink
Fixed issue aws#57. Discovered strange behavior with mocks not allowi…
Browse files Browse the repository at this point in the history
…ng for

functions to be run without the mock being invoked (very confused).

Found issue with tox -e py37-manual and will seek to fix it in a future
set of commits.  Flake8 and linters SHOULD be passing after an autoformat
on this commit.
  • Loading branch information
Adriano Hernandez committed Aug 9, 2019
1 parent 6bf21ce commit da31b25
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
7 changes: 6 additions & 1 deletion src/aws_encryption_sdk/internal/formatting/deserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
from aws_encryption_sdk.internal.crypto.encryption import decrypt
from aws_encryption_sdk.internal.defaults import MAX_FRAME_SIZE
# necessary for it to call the correct deserialize_encryption_context
from aws_encryption_sdk.internal.formatting.encryption_context import deserialize_encryption_context
from aws_encryption_sdk.internal.str_ops import to_str
from aws_encryption_sdk.internal.structures import (
Expand Down Expand Up @@ -242,6 +243,7 @@ def deserialize_header(stream):
tee = io.BytesIO()
tee_stream = TeeStream(stream, tee)
version_id, message_type_id = unpack_values(">BB", tee_stream)

header = dict()
header["version"] = _verified_version_from_id(version_id)
header["type"] = _verified_message_type_from_id(message_type_id)
Expand All @@ -251,7 +253,10 @@ def deserialize_header(stream):
header["algorithm"] = _verified_algorithm_from_id(algorithm_id)
header["message_id"] = message_id

header["encryption_context"] = deserialize_encryption_context(tee_stream.read(ser_encryption_context_length))
aad = tee_stream.read(ser_encryption_context_length)
# d_aad = aws_encryption_sdk.internal.formatting.encryption_context.deserialize_encryption_context(aad)
d_aad = deserialize_encryption_context(aad)
header["encryption_context"] = d_aad

header["encrypted_data_keys"] = _deserialize_encrypted_data_keys(tee_stream)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,19 @@ def deserialize_encryption_context(serialized_encryption_context):
:raises SerializationError: if duplicate key found in serialized encryption context
:raises SerializationError: if malformed data found in serialized encryption context
"""
_LOGGER.debug("Deserializing Encryption Context")
if len(serialized_encryption_context) > aws_encryption_sdk.internal.defaults.MAX_BYTE_ARRAY_SIZE:
raise SerializationError("Serialized context is too long.")

if serialized_encryption_context == b"":
_LOGGER.debug("No encryption context data found")
return {}

deserialized_size = 0
encryption_context = {}

dict_size, deserialized_size = read_short(source=serialized_encryption_context, offset=deserialized_size)
_LOGGER.debug("Found %d keys", dict_size)

# either the dict_size is just wrong, or this is malformed
# either the dict_size is just wrong, or this is malformed
# (and we assume the worst case and more common is the latter... former caught later)
if dict_size == 0:
raise SerializationError("Malformed AAD: zero length AAD with non-zero length AAD length field")
Expand Down
30 changes: 26 additions & 4 deletions test/unit/test_deserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Unit test suite for aws_encryption_sdk.deserialize"""
import base64
import io
import struct

Expand Down Expand Up @@ -72,7 +73,7 @@ def apply_fixtures(self):
self.mock_decrypt = self.mock_decrypt_patcher.start()
# Set up encryption_context patch
self.mock_deserialize_ec_patcher = patch(
"aws_encryption_sdk.internal.formatting.deserialize.deserialize_encryption_context"
"aws_encryption_sdk.internal.formatting.encryption_context.deserialize_encryption_context"
)
self.mock_deserialize_ec = self.mock_deserialize_ec_patcher.start()
self.mock_deserialize_ec.return_value = VALUES["updated_encryption_context"]
Expand Down Expand Up @@ -167,10 +168,31 @@ def test_deserialize_header_unknown_content_type(self):
stream = io.BytesIO(VALUES["serialized_header_unknown_content_type"])
aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(stream)
excinfo.match("Unknown content type *")


"""
Decoded looks like this (with important incorrect AAD fields on their own lines):
b'\x01\x80\x00\x14\x9c\r\xf0\x88\x10\xf7\xed,\xc7,\xbb\xfb\xdd\x1c\x08(
\x00\x02
\x00\x00
\x00\x01\x00\x13WEB-CRYPTO-RSA-OAEP\x00(8CED2FD20FC88A9C06EFDB073707EB1EF1655780\x01
\x00V\xc8\x8b\xe8&J\xbb\xde\x8d\xf3\x82\x8d\xb14\xf2\xb4\xd8\x1f)\xb6\xb8\xb5\xac\x8a,\xd0\x1et\xf2
\xdf??U\xe4~\xec\xc2\xaf6\xfe\xd9|\xf7\xf9-~\xe2\xf5\xa9\x98\xc3\x10\x8c!+\xd8\xf8\x10\x9c\x9d\xd2
\xda\x90\x7f\xd1\x945\x1d\x83>\x7fCB\x80\xc6\xb19-~\xc3\x1f\x13\xbf\xc0\xcb\xe2$(\xbe\xa9u\x8co\xeen
\xd2hpz\x94b\x84\xef\xdd\x93\xed\xe4\x1a\xac\x9a.\x19\xc6\xb8\x0e\xb1\xa9\x89\x93\x85IBe\x1eA\xc1\xfc
\x84u\xd4\x9a\x9e\n;\xbaa\x16\xc3V\x10\xf2\xfa$\x8e\xa53\x08\xa4r\x93ui_\x04\x13]\x02\xcb\xac\x1c
\xfbff{\x02|\xe7\xd1*\x1a-\x89<\x0fJ\xcfZ\x12\x87\xbdE\xd0\x0c\x96\x03a\xd4;\xbe\x97\x9d\xc6\x9b\x04
\xe6\xf9b,\x8f|\x08\x07\x08ZYd\xb2\xdfz\xe02\x01\xc0\xa8\x00_\xa2\x1c\xf0\xd8\xfe\xf1>\xd5\xba\xb7J
\xd9!nL~\x0b"\xf9\x1ena\x06\x0b\x19\xe0u\x1a_\xa2\x91\x14\xfd\x17t-\x85\xd9\x8aN\x02\x00\x00\x00\x00
\x0c\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00+\xdb\xcdF\xfc\xa6\x0eJ1;\xa7r\xeb
\xbaC\xb9\xff\xff\xff\xff\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00
\x050*\x9a)P\xad\xa8\x13\x156\x1d?=\xb7\xc3SLG\xe7<S'
"""

def test_deserialize_header_malformed_aad(self):
malformed_header = io.BytesIO(b"AYAAFJwN8IgQ9+0sxyy7+90cCCgAAgAAAAEAE1dFQi1DUllQVE8tUlNBLU9BRVAAKDhD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ragTFTYdPz23w1NMR+c8Uw=="
mdecoded = base64.b64decode(mencoded)
malformed_header = io.BytesIO(mdecoded)

with pytest.raises(SerializationError) as excinfo:
test = aws_encryption_sdk.internal.formatting.deserialize.deserialize_header(malformed_header)
excinfo.match(r"Malformed AAD: zero length AAD with non-zero length AAD length field")
Expand Down
6 changes: 4 additions & 2 deletions test/unit/test_encryption_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ def test_deserialize_encryption_context_empty(self):
serialized_encryption_context=b""
)
assert test == {}

def test_malformed_aad(self):
malformed_aad = b"\x00\x00"
with pytest.raises(SerializationError) as excinfo:
test = aws_encryption_sdk.internal.formatting.encryption_context.deserialize_encryption_context(malformed_aad)
test = aws_encryption_sdk.internal.formatting.encryption_context.deserialize_encryption_context(
malformed_aad
)
excinfo.match(r"Malformed AAD: zero length AAD with non-zero length AAD length field")

0 comments on commit da31b25

Please sign in to comment.