@@ -175,6 +175,13 @@ def deserialize_header(stream):
175
175
header ['header_iv_length' ] = iv_length
176
176
177
177
(frame_length ,) = unpack_values ('>I' , stream )
178
+ if content_type == ContentType .FRAMED_DATA and frame_length > aws_encryption_sdk .internal .defaults .MAX_FRAME_SIZE :
179
+ raise SerializationError ('Specified frame length larger than allowed maximum: {found} > {max}' .format (
180
+ found = frame_length ,
181
+ max = aws_encryption_sdk .internal .defaults .MAX_FRAME_SIZE
182
+ ))
183
+ elif content_type == ContentType .NO_FRAMING and frame_length != 0 :
184
+ raise SerializationError ('Non-zero frame length found for non-framed message' )
178
185
header ['frame_length' ] = frame_length
179
186
180
187
return MessageHeader (** header )
@@ -200,8 +207,8 @@ def deserialize_header_auth(stream, algorithm, verifier=None):
200
207
return MessageHeaderAuthentication (* unpack_values (format_string , stream , verifier ))
201
208
202
209
203
- def deserialize_single_block_values (stream , header , verifier = None ):
204
- """Deserializes the IV and Tag from a single block stream.
210
+ def deserialize_non_framed_values (stream , header , verifier = None ):
211
+ """Deserializes the IV and Tag from a non-framed stream.
205
212
206
213
:param stream: Source data stream
207
214
:type stream: io.BytesIO
@@ -212,7 +219,7 @@ def deserialize_single_block_values(stream, header, verifier=None):
212
219
:returns: IV, Tag, and Data Length values for body
213
220
:rtype: tuple of str, str, and int
214
221
"""
215
- _LOGGER .debug ('Starting single block body iv/tag deserialization' )
222
+ _LOGGER .debug ('Starting non-framed body iv/tag deserialization' )
216
223
(data_iv , data_length ) = unpack_values (
217
224
'>{}sQ' .format (header .algorithm .iv_len ),
218
225
stream ,
@@ -233,7 +240,7 @@ def update_verifier_with_tag(stream, header, verifier):
233
240
"""Updates verifier with data for authentication tag.
234
241
235
242
.. note::
236
- This is meant to be used in conjunction with deserialize_single_block_values
243
+ This is meant to be used in conjunction with deserialize_non_framed_values
237
244
to update the verifier over information which has already been retrieved.
238
245
239
246
:param stream: Source data stream
@@ -284,6 +291,11 @@ def deserialize_frame(stream, header, verifier=None):
284
291
frame_data ['iv' ] = frame_iv
285
292
if final_frame is True :
286
293
(content_length ,) = unpack_values ('>I' , stream , verifier )
294
+ if content_length >= header .frame_length :
295
+ raise SerializationError ('Invalid final frame length: {final} >= {normal}' .format (
296
+ final = content_length ,
297
+ normal = header .frame_length
298
+ ))
287
299
else :
288
300
content_length = header .frame_length
289
301
(frame_content , frame_tag ) = unpack_values (
@@ -312,19 +324,19 @@ def deserialize_footer(stream, verifier=None):
312
324
"""
313
325
_LOGGER .debug ('Starting footer deserialization' )
314
326
signature = b''
327
+ if verifier is None :
328
+ return MessageFooter (signature = signature )
315
329
try :
316
330
(sig_len ,) = unpack_values ('>H' , stream )
317
331
(signature ,) = unpack_values (
318
332
'>{sig_len}s' .format (sig_len = sig_len ),
319
333
stream
320
334
)
321
- if verifier :
322
- verifier .set_signature (signature )
323
- verifier .verify ()
324
- except struct .error :
325
- # If there is a struct error, assume that there is no footer to read.
326
- if verifier :
327
- raise SerializationError ('No signature found in message' )
335
+ except SerializationError :
336
+ raise SerializationError ('No signature found in message' )
337
+ if verifier :
338
+ verifier .set_signature (signature )
339
+ verifier .verify ()
328
340
return MessageFooter (signature = signature )
329
341
330
342
@@ -340,10 +352,14 @@ def unpack_values(format_string, stream, verifier=None):
340
352
:returns: Unpacked values
341
353
:rtype: tuple
342
354
"""
343
- message_bytes = stream .read (struct .calcsize (format_string ))
344
- if verifier :
345
- verifier .update (message_bytes )
346
- return struct .unpack (format_string , message_bytes )
355
+ try :
356
+ message_bytes = stream .read (struct .calcsize (format_string ))
357
+ if verifier :
358
+ verifier .update (message_bytes )
359
+ values = struct .unpack (format_string , message_bytes )
360
+ except struct .error as e :
361
+ raise SerializationError ('Unexpected deserialization error' , type (e ), e .args )
362
+ return values
347
363
348
364
349
365
def deserialize_wrapped_key (wrapping_algorithm , wrapping_key_id , wrapped_encrypted_key ):
0 commit comments