@@ -37,12 +37,14 @@ impl PasswordAlgorithmValue {
37
37
}
38
38
39
39
fn read ( data : & [ u8 ] ) -> Result < Self , StunParseError > {
40
+ // checked externally that we have at least 4 bytes
40
41
let ty = BigEndian :: read_u16 ( & data[ ..2 ] ) ;
41
42
let len = BigEndian :: read_u16 ( & data[ 2 ..4 ] ) ;
43
+ // all currently know algorithms don't ahve any extra data
42
44
if len != 0 {
43
45
return Err ( StunParseError :: TooLarge {
44
46
expected : 4 ,
45
- actual : data . len ( ) ,
47
+ actual : 4 + len as usize ,
46
48
} ) ;
47
49
}
48
50
Ok ( match ty {
@@ -268,4 +270,35 @@ mod tests {
268
270
Err ( StunParseError :: WrongAttributeImplementation )
269
271
) ) ;
270
272
}
273
+
274
+ #[ test]
275
+ fn password_algorithm_value_too_large ( ) {
276
+ init ( ) ;
277
+ let val = PasswordAlgorithmValue :: SHA256 ;
278
+ let attr = PasswordAlgorithm :: new ( val) ;
279
+ let raw: RawAttribute = attr. into ( ) ;
280
+ let mut data = raw. to_bytes ( ) ;
281
+ data[ 7 ] = 100 ;
282
+ assert ! ( matches!(
283
+ PasswordAlgorithm :: try_from( & RawAttribute :: from_bytes( data. as_ref( ) ) . unwrap( ) ) ,
284
+ Err ( StunParseError :: TooLarge {
285
+ expected: 4 ,
286
+ actual: 104
287
+ } )
288
+ ) ) ;
289
+ }
290
+
291
+ #[ test]
292
+ fn password_algorithm_value_unknown ( ) {
293
+ init ( ) ;
294
+ let val = PasswordAlgorithmValue :: SHA256 ;
295
+ let attr = PasswordAlgorithm :: new ( val) ;
296
+ let raw: RawAttribute = attr. into ( ) ;
297
+ let mut data = raw. to_bytes ( ) ;
298
+ data[ 5 ] = 0x80 ;
299
+ assert ! ( matches!(
300
+ PasswordAlgorithm :: try_from( & RawAttribute :: from_bytes( data. as_ref( ) ) . unwrap( ) ) ,
301
+ Err ( StunParseError :: InvalidAttributeData )
302
+ ) ) ;
303
+ }
271
304
}
0 commit comments