@@ -249,6 +249,7 @@ private async Task<ReadOnlyMemory<byte>> ReceiveResponse(
249249 var initPacket = await _connection . ReceiveAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
250250 while ( IsKeepAlivePacket ( initPacket . Span ) )
251251 {
252+ ValidateInitPacket ( initPacket . Span , channelId ) ;
252253 _logger . LogTrace ( "Received keep-alive, waiting for response" ) ;
253254 initPacket = await _connection . ReceiveAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
254255 }
@@ -263,16 +264,11 @@ private async Task<ReadOnlyMemory<byte>> ReceiveResponse(
263264 var responseData = new byte [ responseLength ] ;
264265 var initDataLength = Math . Min ( responseLength , CtapConstants . InitDataSize ) ;
265266
266- // Ensure we don't try to read more data than the packet contains
267- var availableDataInPacket = Math . Min ( initDataLength , initPacket . Length - CtapConstants . InitHeaderSize ) ;
268- if ( availableDataInPacket < 0 )
269- availableDataInPacket = 0 ;
270-
271- initPacket . Span . Slice ( CtapConstants . InitHeaderSize , availableDataInPacket )
267+ initPacket . Span . Slice ( CtapConstants . InitHeaderSize , initDataLength )
272268 . CopyTo ( responseData ) ;
273269
274270 // Receive continuation packets if needed
275- var bytesReceived = availableDataInPacket ;
271+ var bytesReceived = initDataLength ;
276272 byte expectedSequence = 0 ;
277273 while ( bytesReceived < responseLength )
278274 {
@@ -282,15 +278,10 @@ private async Task<ReadOnlyMemory<byte>> ReceiveResponse(
282278 responseLength - bytesReceived ,
283279 CtapConstants . ContinuationDataSize ) ;
284280
285- // Ensure we don't try to read more data than the packet contains
286- var availableContData = Math . Min ( contDataLength , contPacket . Length - CtapConstants . ContinuationHeaderSize ) ;
287- if ( availableContData < 0 )
288- availableContData = 0 ;
289-
290- contPacket . Span . Slice ( CtapConstants . ContinuationHeaderSize , availableContData )
281+ contPacket . Span . Slice ( CtapConstants . ContinuationHeaderSize , contDataLength )
291282 . CopyTo ( responseData . AsSpan ( bytesReceived ) ) ;
292283
293- bytesReceived += availableContData ;
284+ bytesReceived += contDataLength ;
294285 expectedSequence ++ ;
295286 }
296287
@@ -355,8 +346,8 @@ private static bool IsKeepAlivePacket(ReadOnlySpan<byte> packet) =>
355346
356347 private static void ValidateInitPacket ( ReadOnlySpan < byte > packet , uint channelId )
357348 {
358- if ( packet . Length < CtapConstants . InitHeaderSize )
359- throw new InvalidOperationException ( "CTAP HID init packet is too short " ) ;
349+ if ( packet . Length != CtapConstants . PacketSize )
350+ throw new InvalidOperationException ( "CTAP HID init packet must be exactly 64 bytes " ) ;
360351
361352 var packetChannelId = BinaryPrimitives . ReadUInt32BigEndian ( packet ) ;
362353 if ( packetChannelId != channelId )
@@ -368,8 +359,8 @@ private static void ValidateInitPacket(ReadOnlySpan<byte> packet, uint channelId
368359
369360 private static void ValidateContinuationPacket ( ReadOnlySpan < byte > packet , uint channelId , byte expectedSequence )
370361 {
371- if ( packet . Length < CtapConstants . ContinuationHeaderSize )
372- throw new InvalidOperationException ( "CTAP HID continuation packet is too short " ) ;
362+ if ( packet . Length != CtapConstants . PacketSize )
363+ throw new InvalidOperationException ( "CTAP HID continuation packet must be exactly 64 bytes " ) ;
373364
374365 var packetChannelId = BinaryPrimitives . ReadUInt32BigEndian ( packet ) ;
375366 if ( packetChannelId != channelId )
@@ -440,4 +431,4 @@ public void Dispose()
440431 _connection . Dispose ( ) ;
441432 _disposed = true ;
442433 }
443- }
434+ }
0 commit comments