OpenSSL "ans1parse" - ASN.1 INTEGER Field Type

Q

How to how extra an ASN.1 child structure using the OpenSSL "ans1parse" command?

✍: FYIcenter.com

A

ASN.1 INTEGER field is a primitive field that can be used to store signed integers of any size. To use ASN.1 INTEGER field in OpenSSL "asn1parse" command, you need to remember the following rules:

  • ASN.1 INTEGER type tag is 0x02.
  • ASN.1 INTEGER type code for "asn1parse" command is INTEGER or INT.
  • ASN.1 INTEGER value literal can be a signed integer in decimal or hexadecimal digits of any size. For example, 1, -1, 128, -128, 0x01, and -0x01 are all valid INTEGER values.
  • ASN.1 INTEGER value is stored with DER encoding in signed binary integer format. For example, 1, -1, 128, and -128 are stored in DER format as 0x01, 0xff, 0x0080, and 0x80 Note that in signed binary number format, integer 128 must stored with 2 bytes as 0x0080 to keep the sign bit (the leading bit) as 0.

Here are examples of using INTEGER with the OpenSSL "asn1parse" command:

C:\Users\fyicenter>\local\openssl\openssl.exe

OpenSSL> asn1parse -genstr INTEGER:1 -out p1.der
    0:d=0  hl=2 l=   1 prim: INTEGER           :01

OpenSSL> asn1parse -genstr INTEGER:-1 -out n1.der
    0:d=0  hl=2 l=   1 prim: INTEGER           :-01

OpenSSL> asn1parse -genstr INTEGER:128 -out p128.der
    0:d=0  hl=2 l=   2 prim: INTEGER           :80

OpenSSL> asn1parse -genstr INTEGER:-128 -out n128.der
    0:d=0  hl=2 l=   1 prim: INTEGER           :-80
    
OpenSSL> asn1parse -genstr INTEGER:0x01 -out p1x.der
    0:d=0  hl=2 l=   1 prim: INTEGER           :01

OpenSSL> asn1parse -genstr INTEGER:-0x01 -out n1x.der
    0:d=0  hl=2 l=   1 prim: INTEGER           :-01

OpenSSL> exit

C:\Users\fyicenter>bin2hex p1.der
020101

C:\Users\fyicenter>bin2hex n1.der
0201ff

C:\Users\fyicenter>bin2hex p128.der
02020080

C:\Users\fyicenter>bin2hex n128.der
020180

C:\Users\fyicenter>bin2hex p1x.der
020101

C:\Users\fyicenter>bin2hex n1x.der
0201ff

Note that "bin2hex" is not a Windows command, You need to create your own tool to dump a binary file in hex format.

 

OpenSSL "ans1parse" Command

⇒⇒OpenSSL Tutorials

2016-09-30, 5088👍, 0💬