@@ -47,17 +47,13 @@ def assemble_content_aad(message_id, aad_content_string, seq_num, length):
47
47
return struct .pack (fmt , message_id , aad_content_string .value , seq_num , length )
48
48
49
49
50
- def _key_or_value_too_large (encryption_context_list ):
51
- """Helper method to check whether a key or value are too long."""
52
- longest_key = max (encryption_context_list , key = lambda x : len (x [0 ]))[0 ]
53
- longest_value = max (encryption_context_list , key = lambda x : len (x [1 ]))[1 ]
54
- return len (longest_key ) > 2 ** 16 - 1 or len (longest_value ) > 2 ** 16 - 1
55
-
56
-
57
50
def _serialize_encryption_context_list (encryption_context_list , serialized_context ):
58
51
"""Helper function to serialize the list of encryption context key-value pairs."""
59
52
for key , value in sorted (encryption_context_list , key = lambda x : x [0 ]):
60
53
try :
54
+ if len (key ) > 2 ** 16 - 1 or len (value ) > 2 ** 16 - 1 :
55
+ raise SerializationError ("Key or Value are too large. Maximum length is 65535" )
56
+
61
57
serialized_context .extend (
62
58
struct .pack (
63
59
">H{key_size}sH{value_size}s" .format (key_size = len (key ), value_size = len (value )),
@@ -67,19 +63,19 @@ def _serialize_encryption_context_list(encryption_context_list, serialized_conte
67
63
value ,
68
64
)
69
65
)
70
- # we check to make sure that we return the right type of error message for an overly long key or value
71
66
except struct .error as struct_error :
72
67
message = str (struct_error )
73
- # a key or value were too long and struct gave us the expected error
74
68
if message == "'H' format requires 0 <= number <= 65535" :
75
69
raise SerializationError ("Key or Value are too large. Maximum length is 65535" )
76
- # a key or value were too long and struct's error message is different (maybe an update)
77
- if _key_or_value_too_large (encryption_context_list ):
78
- raise SerializationError ("Key or Value are too large. Maximum length is 65535" )
79
- # else we can just raise the struct error as it was (unknown)
80
- raise SerializationError ("Unknown Error with struct pack. Could not serialize key or value" )
70
+
71
+ # else unknown
72
+ raise SerializationError (
73
+ [SerializationError ("Unknown Error with struct pack. Could not serialize key or value" ), struct_error ]
74
+ )
75
+
81
76
if len (serialized_context ) > aws_encryption_sdk .internal .defaults .MAX_BYTE_ARRAY_SIZE :
82
77
raise SerializationError ("The serialized context is too large." )
78
+
83
79
return bytes (serialized_context )
84
80
85
81
0 commit comments