Search code examples
asn.1ber

ASN.1 BER Encode Integer 0x‭10000000FFFFFFFC Indefinite Length Encoded


I am implementing BER according to spec and I am asking myself the question: How would I encode an Integer 0x‭10000000FFFFFFFC as BER Integer (Tag 0x02) when I use the indefinite length encoding?

I did not find any escaping characters in the spec so far, so I would assume, that I cannot encode such a number and therefore would have to rely on the sender to know that as well and send the Integer with the Short Form Length Encoding. But in BitStrings, the same problem arises.


Solution

  • You NEVER use indefinite length encoding with an integer ...

    It's written in the X.690 (08/2015) chapter 8.3 (Encoding of an integer value):

    The encoding of an integer value shall be primitive
    

    Indefinite length is used for constructed types (SEQUENCE, SEQUENCE OF ...) and for basic types that can contain large values (String types, BITSTRING, OCTET STRING ...). In this case the spec will say:

    The encoding of a bitstring value shall be either primitive or constructed at the option of the sender
    

    The CER encoding rules (chapter 9) give you an idea of what large value meant at the time of the writing:

     Bitstring, octetstring, and restricted character string values shall be encoded with a primitive encoding if they would require no more than 1000 contents octets, and as a constructed encoding otherwise
    

    So you see that even a gigantic integer will always be less that 1000 bytes when encoded: hence the choice of never using the indefinite length form for an integer