Skip to content

Conversation

@JesusMcCloud
Copy link
Collaborator

  • Completely revamped ASN.1 Tag Handling
    • Properly handle multi-byte tags
    • Introduce a new data structure TLV.Tag with an accompanying TagClass enum and a constructed flag to accurately represent arbitrary tags up to ULong.MAX_VALUE
    • Make all tag parameters ULong to reflect support for multi-byte tags
    • Remove DERTags
    • Revamp implicit tagging (there is still work to be done, but at least it supports CONSTRUCTED ASN.1 elements)
  • Refactor Int.Companion.decodeFromDer() -> Int.Companion.decodeFromDerValue()
  • Refactor Long.Companion.decodeFromDer() -> Long.Companion.decodeFromDerValue()
  • Introduce ULong.toAsn1VarInt() to encode ULongs into ASN.1 unsigned VarInts (not to be confused with
    multi^2_base'sUVarInt!
    )
  • Introduce decodeAsn1VarULong() and decodeAsn1VarUInt() which can handle overlong inputs, as long as they start with a valid unsigned number encoding.
    • Comes in three ULong flavours:
      • Iterator<Byte>.decodeAsn1VarULong()
      • Iterable<Byte>.decodeAsn1VarULong()
      • ByteArray.decodeAsn1VarULong()
    • and three UInt flavours:
      • Iterator<Byte>.decodeAsn1VarUInt()
      • Iterable<Byte>.decodeAsn1VarUInt()
      • ByteArray.decodeAsn1VarUInt()

@nodh
Copy link
Collaborator

nodh commented Sep 11, 2024

This encoding test fails for me:

"fails" {
        val it = 2204309167L
        val bytes = (it).toTwosComplementByteArray()
        val fromBC = ASN1Integer(it).encoded
        val long = Long.decodeFromDerValue(bytes)
        val encoded = Asn1Primitive(Asn1Element.Tag.INT, bytes).derEncoded
        encoded shouldBe fromBC
        long shouldBe it
    }

with

Expected :[2, 5, 0, -125, 99, 22, -81]
Actual   :[2, 6, 0, 0, -125, 99, 22, -81]

@JesusMcCloud
Copy link
Collaborator Author

This encoding test fails for me:

"fails" {
        val it = 2204309167L
        val bytes = (it).toTwosComplementByteArray()
        val fromBC = ASN1Integer(it).encoded
        val long = Long.decodeFromDerValue(bytes)
        val encoded = Asn1Primitive(Asn1Element.Tag.INT, bytes).derEncoded
        encoded shouldBe fromBC
        long shouldBe it
    }

with

Expected :[2, 5, 0, -125, 99, 22, -81]
Actual   :[2, 6, 0, 0, -125, 99, 22, -81]

@iaik-jheher i thought you fixed it?

@JesusMcCloud JesusMcCloud force-pushed the feature/asn1ProperTagging branch from a59ff40 to e6fe92b Compare September 12, 2024 15:03
@JesusMcCloud
Copy link
Collaborator Author

JesusMcCloud commented Sep 16, 2024

decodeTag is broken!. encoding works perfectly fine though, so debugging should be easy

@JesusMcCloud
Copy link
Collaborator Author

decodeTag is broken!. encoding works perfectly fine though, so debugging should be easy

was varint decoding bug. fixed now

@JesusMcCloud JesusMcCloud force-pushed the feature/asn1ProperTagging branch from 2d52fc2 to ea4b1e3 Compare September 16, 2024 07:07
@iaik-jheher
Copy link
Collaborator

decodeTag is broken!. encoding works perfectly fine though, so debugging should be easy

was varint decoding bug. fixed now

did you add a test case that covers this, since it was not caught before?

@JesusMcCloud
Copy link
Collaborator Author

decodeTag is broken!. encoding works perfectly fine though, so debugging should be easy

was varint decoding bug. fixed now

did you add a test case that covers this, since it was not caught before?

totally forgot do test for trainlign bytes. now there's a testcase for longs and ints and both caught the error

@JesusMcCloud JesusMcCloud force-pushed the feature/asn1ProperTagging branch from ea4b1e3 to 885c2d4 Compare September 16, 2024 09:24
@JesusMcCloud JesusMcCloud merged commit 6bd3ae3 into development Sep 16, 2024
iaik-jheher pushed a commit that referenced this pull request Sep 16, 2024
* Completely revamped ASN.1 Tag Handling
  * Properly handle multi-byte tags
  * Introduce a new data structure `TLV.Tag` with an accompanying `TagClass` enum and a `constructed` flag to accurately represent arbitrary tags up to `ULong.MAX_VALUE`
  * Make all `tag` parameters `ULong` to reflect support for multi-byte tags
  * Remove `DERTags`
  * Revamp implicit tagging (there is still work to be done, but at least it supports CONSTRUCTED ASN.1 elements)
* Refactor `Int.Companion.decodeFromDer()` -> `Int.Companion.decodeFromDerValue()`
* Refactor `Long.Companion.decodeFromDer()` -> `Long.Companion.decodeFromDerValue()`
* Introduce `ULong.toAsn1VarInt()` to encode ULongs into ASN.1 unsigned VarInts (**not to be confused with
  multi^2_base's`UVarInt`!**)
* Introduce `decodeAsn1VarULong()` and `decodeAsn1VarUInt()` which can handle overlong inputs, as long as they start with a valid unsigned number encoding.
  * Comes in three ULong flavours:
    * `Iterator<Byte>.decodeAsn1VarULong()`
    * `Iterable<Byte>.decodeAsn1VarULong()`
    * `ByteArray.decodeAsn1VarULong()`
  * and three UInt flavours:
      * `Iterator<Byte>.decodeAsn1VarUInt()`
      * `Iterable<Byte>.decodeAsn1VarUInt()`
      * `ByteArray.decodeAsn1VarUInt()`
JesusMcCloud added a commit that referenced this pull request Sep 20, 2024
* Completely revamped ASN.1 Tag Handling
  * Properly handle multi-byte tags
  * Introduce a new data structure `TLV.Tag` with an accompanying `TagClass` enum and a `constructed` flag to accurately represent arbitrary tags up to `ULong.MAX_VALUE`
  * Make all `tag` parameters `ULong` to reflect support for multi-byte tags
  * Remove `DERTags`
  * Revamp implicit tagging (there is still work to be done, but at least it supports CONSTRUCTED ASN.1 elements)
* Refactor `Int.Companion.decodeFromDer()` -> `Int.Companion.decodeFromDerValue()`
* Refactor `Long.Companion.decodeFromDer()` -> `Long.Companion.decodeFromDerValue()`
* Introduce `ULong.toAsn1VarInt()` to encode ULongs into ASN.1 unsigned VarInts (**not to be confused with
  multi^2_base's`UVarInt`!**)
* Introduce `decodeAsn1VarULong()` and `decodeAsn1VarUInt()` which can handle overlong inputs, as long as they start with a valid unsigned number encoding.
  * Comes in three ULong flavours:
    * `Iterator<Byte>.decodeAsn1VarULong()`
    * `Iterable<Byte>.decodeAsn1VarULong()`
    * `ByteArray.decodeAsn1VarULong()`
  * and three UInt flavours:
      * `Iterator<Byte>.decodeAsn1VarUInt()`
      * `Iterable<Byte>.decodeAsn1VarUInt()`
      * `ByteArray.decodeAsn1VarUInt()`
@JesusMcCloud JesusMcCloud deleted the feature/asn1ProperTagging branch November 14, 2024 16:21
StjepanovicSrdjan pushed a commit to StjepanovicSrdjan/signum that referenced this pull request Jun 18, 2025
* Completely revamped ASN.1 Tag Handling
  * Properly handle multi-byte tags
  * Introduce a new data structure `TLV.Tag` with an accompanying `TagClass` enum and a `constructed` flag to accurately represent arbitrary tags up to `ULong.MAX_VALUE`
  * Make all `tag` parameters `ULong` to reflect support for multi-byte tags
  * Remove `DERTags`
  * Revamp implicit tagging (there is still work to be done, but at least it supports CONSTRUCTED ASN.1 elements)
* Refactor `Int.Companion.decodeFromDer()` -> `Int.Companion.decodeFromDerValue()`
* Refactor `Long.Companion.decodeFromDer()` -> `Long.Companion.decodeFromDerValue()`
* Introduce `ULong.toAsn1VarInt()` to encode ULongs into ASN.1 unsigned VarInts (**not to be confused with
  multi^2_base's`UVarInt`!**)
* Introduce `decodeAsn1VarULong()` and `decodeAsn1VarUInt()` which can handle overlong inputs, as long as they start with a valid unsigned number encoding.
  * Comes in three ULong flavours:
    * `Iterator<Byte>.decodeAsn1VarULong()`
    * `Iterable<Byte>.decodeAsn1VarULong()`
    * `ByteArray.decodeAsn1VarULong()`
  * and three UInt flavours:
      * `Iterator<Byte>.decodeAsn1VarUInt()`
      * `Iterable<Byte>.decodeAsn1VarUInt()`
      * `ByteArray.decodeAsn1VarUInt()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants