wstrange / asn1lib

Dart ASN1 Encoder / Decoder
BSD 2-Clause "Simplified" License
30 stars 30 forks source link

Problem with the nextObject method of the ASN1Parser class #68

Closed GrzegorzMalina94 closed 4 months ago

GrzegorzMalina94 commented 4 months ago

Description

The method nextObjectof the ASN1Parserclass doesn't work correctly when objects with an extended tag's value are encoded in bytes. I looked at the method code. In my opinion there are two errors there.

  1. The _position field is not updated when an object with an extended tag value is returned.
  2. To the ASN1Object.fromBytes constructor of an object with an extended tag value, all parser bytes (_bytes) are passed. In my opinion _bytes.sublist(_position) should be passed.

To Reproduce

I prepared a small test allowing to reproduce the problem. Test will fail due to malfunction of the nextObject method.

import  'dart:typed_data';   

import  'package:asn1lib/asn1lib.dart';    
import  'package:test/test.dart';      

void  main() {    
final  sampleBytes  =  Uint8List.fromList([    
0x5f,    
0x29,    
0x01,    
0x00,    
0x42,    
0x08,    
0xfd,    
0x45,    
0x43,    
0x20,    
0x01,    
0xff,    
0xff,    
0x01,    
0x5f,    
0x4c,    
0x07,    
0xff,    
0x53,    
0x4d,    
0x52,    
0x44,    
0x54,    
0xd,    
]); 

test(    
"Test ASN1Parser.nextObject method when objects with an extended tag's "    
'value are encoded in bytes. ', () {    
final  parser  =  ASN1Parser(sampleBytes);

final  firstObj  =  parser.nextObject();    
final  secondObj  =  parser.nextObject();    
final  thirdObj  =  parser.nextObject(); 

final  expected1stObjValueBytes  =  Uint8List.fromList([  
0x00,    
]);      

final  expected2ndObjValueBytes  =  Uint8List.fromList([   
0xfd,    
0x45,    
0x43,    
0x20,    
0x01,    
0xff,    
0xff,    
0x01,    
]);

final  expected3rdObjValueBytes  =  Uint8List.fromList([   
0xff,    
0x53,    
0x4d,    
0x52,    
0x44,    
0x54,    
0x0d,   
]);         

expect(firstObj.valueBytes(), orderedEquals(expected1stObjValueBytes));    
expect(secondObj.valueBytes(), orderedEquals(expected2ndObjValueBytes));    
expect(thirdObj.valueBytes(), orderedEquals(expected3rdObjValueBytes));    
    });
}

Relevant Files

asn1Parser.dart

Tasks

They all refer to the nextObject() method of the ASN1Parser class

wstrange commented 4 months ago

Thank you for the detailed bug report!

Are you interested in submitting a PR for this? If not - I can probably take a look at this tomorrow

GrzegorzMalina94 commented 4 months ago

If you can, take a look at it.

wstrange commented 4 months ago

Just pushed 1.5.3 . Let me know if that works for you

GrzegorzMalina94 commented 4 months ago

Yes, problem solved. Thanks.