wstrange / asn1lib

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

ANS1ObjectIdentifier SHA-256 #70

Closed ysimonx closed 1 week ago

ysimonx commented 2 weeks ago

Flutter

asn1lib: ^1.5.3

2024-09-04

SHA-256 is "2.16.840.1.101.3.4.2.1" ( thanks to https://oid-rep.orange-labs.fr/get/2.16.840.1.101.3.4.2.1 )

ASN1ObjectIdentifier sha256OidHS = ASN1ObjectIdentifier([
      2,
      16,
      840,
      1,
      101,
      3,
      4,
      2,
      1
    ]); // SHA-256 OID sous forme de liste d'entiers

    ASN1Sequence seq = ASN1Sequence();
    seq.add(sha256OidHS);
    var hexseq = seq.encodedBytes
        .map((e) => "${e.toRadixString(16).padLeft(2, '0')} ")
        .join();
    print(hexseq);

produces

"30 0b 06 09 60 86 48 01 65 03 04 02 01"

However, it should be

"30 0d 06 09 60 86 48 01 65 03 04 02 01"

note the 0b -> 0d

May be a issue ???

wstrange commented 1 week ago

Hmmm. I did not write this particular class - so I always need to look up the ASN1 docs. Need your help here.

0x30 is a sequence (which looks correct), and 0x0b is the length of the encoded sequence - which in this case is 11 bytes - which from the hex dump also looks ok.

However, I see there are values in that OID (840) that cant be encoded in a byte - so the class is probably not doing the correct thing.

I'd try using the string constructor, just to see if it works - then take a look at the primary constructor and the _encode method.

If you can create a PR (along with a test case ) that would be really appreciated

wstrange commented 1 week ago

Adding this as a reference:OID

wstrange commented 1 week ago

If I paste that OID into: https://misc.daniel-marschall.de/asn.1/oid-converter/online.php

2.16.840.1.101.3.4.2.1

I get: 06 09 60 86 48 01 65 03 04 02 01

Which are the 11 bytes of the OID. Youv'e wrapped this in a outer sequence - but I think this looks correct?

Are you expecting something different?

ysimonx commented 1 week ago

@wstrange

thank you

With your commets, I made some additional researchs .... and I managed to fix my issue

based on

    var algorithmSeq = new ASN1Sequence();
    var algorithmAsn1Obj = new ASN1Object.fromBytes(Uint8List.fromList([0x6, 0x9, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0xd, 0x1, 0x1, 0x1]));
    var paramsAsn1Obj = new ASN1Object.fromBytes(Uint8List.fromList([0x5, 0x0]));
    algorithmSeq.add(algorithmAsn1Obj);
    algorithmSeq.add(paramsAsn1Obj);

I found at https://gist.github.com/hnvn/38ef37566471f1135773b5426fb73011 I realize that I forgot to add 0x5, 0x0 at the sequence ...

I tried to add these two values, and now, I have the correct bytes sequence

thank you so much !