Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

openssl asn1parse doesnt display all characters in serial number:#23244

Answeredbymattcaswell
ajuliansr asked this question inQ&A
Discussion options

openssl version

OpenSSL 3.0.12 24 Oct 2023 (Library: OpenSSL 3.0.12 24 Oct 2023)
openssl asn1parse doesnt display all characters in serial number:
From the following the field starting at offset 13 is 17 characters long, but only 16 of the characters are displayed:

openssl asn1parse -in Sectigo-cert.cer```    0:d=0  hl=4 l=2070 cons: SEQUENCE    4:d=1  hl=4 l=1790 cons:  SEQUENCE    8:d=2  hl=2 l=   3 cons:   cont [ 0 ]   10:d=3  hl=2 l=   1 prim:    INTEGER           :02   13:d=2  hl=2 l=  17 prim:   INTEGER           :C9F97F52C320766D6DF0D55DD229559E   32:d=2  hl=2 l=  13 cons:   SEQUENCE   34:d=3  hl=2 l=   9 prim:    OBJECT            :sha256WithRSAEncryption   45:d=3  hl=2 l=   0 prim:    NULL[Sectigo-cert.zip](https://github.com/openssl/openssl/files/13866077/Sectigo-cert.zip)
You must be logged in to vote

Point is that asn1parse is supposed to show what it parsed rather than a somewhat frivolous interpretation.

When asn1parse parses a signed number, I expect it to show me a signed number, so either C9F97F52C320766D6DF0D55DD229559E or -360680AD3CDF8992920F2AA22DD6AA62 as appropriate. In this case it is clearly encoded as a positive integer in the DER, so the correct presentation is C9F97F52C320766D6DF0D55DD229559E. This is completely unambiguous and printing the leading 0 is incorrect IMO.

This is not a bug.

Replies: 14 comments 7 replies

Comment options

openssl x509 -noout -serial
returns only 16 characters also.

You must be logged in to vote
0 replies
Comment options

Here is a hex dump of the first bytes of that cert, ending immediately after the encoding of the serial number:

30820816308206fea00302010202 1100c9f97f52c320766d6df0d55dd229559e

I agree that the serial number is encoded using 17 octets.

However, the fact that only 16 octets are displayed by the command-line utility is intentional.

When displaying an asn1 INTEGER in printable hex, if the leading octet is zero, then it is not displayed.

This behaviour can be surprising, and we have had some internal discussions on it (I will link to the related github issue if I can find it).

You must be logged in to vote
0 replies
Comment options

converting this to der format and dumping it out in hexedit shows this:

00000000   30 82 08 16  30 82 06 FE  A0 03 02 01  02 02 11 00  C9 F9 7F 52  C3 20 76 6D  6D F0 D5 5D  0...0..............R. vmm..]

Byte 0 (0x30) is the start of the sequence in asn1parse
Byte 4 (0x30) is the start of the next sequence in asn1parse
Byte 8 (0xA0) is the cons (0) value from asn1parse
Byte 10 (0x2) is the first integer reported in asn1parse (with a value of 2)
Byte 13 (0x2) is the serial number that is output (purportedly with a length of 17)

However, if you look at byte 15 which is the first byte of the serial number, its zero (0x0). Since the value is encoded as an integer, I would imagine that the decode is truncating the leading zero, as a zero padded integer is just the integer

However

https://www.itu.int/ITU-T/studygroups/com10/languages/X.690_1297.pdf

Indicates that when encoding an integer the bits of the first octet and bit 8 of the second octect:

  1. shall not all be ones
    and
  2. shall not all be zero

To ensure minimal space during encoding.

But bit 8 of the second octet (0xc9) is 1 meaning both of those requirements would be met, and truncating the leading zero would result in a sign change. The truncation would have been ok if bit 8 of the second octet were zero, but since, its not that would lead to a sign change, which definately seems like a bug to me

You must be logged in to vote
0 replies
Comment options

Here is a related PR (unmerged):

#8136

Above, I said:

When displaying an asn1 INTEGER in printable hex, if the leading octet is zero, then it is not displayed.

After re-reading the discussion in that PR, I see that my comment isn't completely accurate. There is some inconsistency in how openssl treats leading zero octets when displaying an asn1 INTEGER.

You must be logged in to vote
0 replies
Comment options

It seems to me that the leading zero in this case has to be included as truncating it results in the sign of the represented integer being flipped.

You must be logged in to vote
0 replies
Comment options

t8m
Jan 9, 2024
Maintainer

The leading zero is just a DER encoding artifact. The serial number is a number, thus the leading zero would not be displayed. This is not a bug.

You must be logged in to vote
0 replies
Comment options

The serial number isn'tjust a number... it's a signed number.asn1parse doesn't have any information on the number's signedness, though, it just parses DER (not ASN.1 😆), so it's difficult to know whether C9F97F52C320766D6DF0D55DD229559E is meant to be interpreted as C9F97F52C320766D6DF0D55DD229559E or as -360680AD3CDF8992920F2AA22DD6AA62.

Point is thatasn1parse is supposed to show what it parsed rather than a somewhat frivolous interpretation. Since a (positive) C9F97F52C320766D6DF0D55DD229559E is encoded 00C9F97F52C320766D6DF0D55DD229559E, I can understand why the lack of that leading 00 can be a bit... surprising, for those who pay attention to these details.

I'd regard this as a bug, but perhaps not a terribly important one

You must be logged in to vote
1 reply
@levitte
Comment options

Changing my mind on this because it was shown that negative numbers are shown as actual negative numbers (with a minus sign).

Not a bug

Comment options

Github seems to be having issues at the moment, but IIRC#8136 indicated that openssl x509 -text was showing a simmilar issue

You must be logged in to vote
0 replies
Comment options

Point is that asn1parse is supposed to show what it parsed rather than a somewhat frivolous interpretation.

When asn1parse parses a signed number, I expect it to show me a signed number, so either C9F97F52C320766D6DF0D55DD229559E or -360680AD3CDF8992920F2AA22DD6AA62 as appropriate. In this case it is clearly encoded as a positive integer in the DER, so the correct presentation is C9F97F52C320766D6DF0D55DD229559E. This is completely unambiguous and printing the leading 0 is incorrect IMO.

This is not a bug.

You must be logged in to vote
0 replies
Answer selected byt8m
Comment options

Ok, I see now, DER encoded the a 16 byte integer using a minimum of 17 bytes because the DER encoding has to preserve the sign of the integer being encoded. Whatever did the encoding interpreted the serial number as positive, so the DER encoding added an extra zero pad to record the fact that the value was positive (rather than negative as would have been the interpretation if the zero pad wasn't there). asn1parse is smart enough to prepend a '-' (negative) notation in front of a number that it parses from DER when the leading bit is zero, so we can correctly interpret a printed integer value (even one in hex with the MSB set), as positive or negative based on the presence or lack of '-'.

In fact editing the provided file to make the uppper octet in DER 0xFF results in this print out:

openssl asn1parse -in ./cert.der -inform DER    0:d=0  hl=4 l=2070 cons: SEQUENCE              4:d=1  hl=4 l=1790 cons: SEQUENCE              8:d=2  hl=2 l=   3 cons: cont [ 0 ]           10:d=3  hl=2 l=   1 prim: INTEGER           :02   13:d=2  hl=2 l=  17 prim: INTEGER           :-7F360680AD3CDF8992920F2AA22DD6AA62   32:d=2  hl=2 l=  13 cons: SEQUENCE

So we can see how we handle +/- integers, and that the encoding size doesn't necessecarily match the number of octets printed because of said padding.

I agree now, this isn't a bug

You must be logged in to vote
1 reply
@levitte
Comment options

Oh! I hadn't caught on to this display of negative numbers in asn1parse... Ok then

Comment options

Thanks to all for the discussion/explanation. Now I have to convince a vendor that their display of the 00 is wrong.

You must be logged in to vote
2 replies
@levitte
Comment options

Their display isn't necessarily wrong, just a different design choice.

@DDvO
Comment options

DDvONov 9, 2024
Collaborator

Thanks to all for the discussion/explanation. Now I have to convince a vendor that their display of the 00 is wrong.

Would be great if you could also convince Mozilla.
They didn't get it and refused to fix their inconsistencies.
https://bugzilla.mozilla.org/show_bug.cgi?id=1520923

Comment options

Now I have to convince a vendor that their display of the 00 is wrong.

before you commit too fiercely to one side of the discussion, please note that other tools for displaying ASN1 data in a human-readable form have made different design choices thanopenssl asn1parse.

e.g.dumpasn1 shows the leading00:

$ openssl x509 -in Sectigo-cert.cer -outform DER -out foo.der$ dumpasn1 foo.der | head   0 2070: SEQUENCE {   4 1790:   SEQUENCE {   8    3:     [0] {  10    1:       INTEGER 2         :       }  13   17:     INTEGER 00 C9 F9 7F 52 C3 20 76 6D 6D F0 D5 5D D2 29 55 9E  32   13:     SEQUENCE {  34    9:       OBJECT IDENTIFIER         :         sha256WithRSAEncryption (1 2 840 113549 1 1 11)  45    0:       NULL
You must be logged in to vote
1 reply
@DDvO
Comment options

DDvONov 9, 2024
Collaborator

Thedumpasn1 tool simply has the same bug - as pointed out before,
the leading00 for unsigned integers having a highest byte >= 0x80
is nothing but an internal DER encoding artifact.

Outputting it as if it was part of the number is confusing and inconsistent.
This is way I also still pursue#8136.

Comment options

The sad part is to "add insult to injury" the vendor's code renders the serial number as a set of 16 hex bytes, adding a newline before the 17th byte e.g. Serial Number: 00 B6 94 C3 AE A4 A3 58 E5 5F 7F 03 F9 F4 18 55 which means that you are unable to grep the output of the tool for "Serial Number" and get the full serial number.
This makes the extraction of the serial number (from 100+ hosts) for comparison to the list of issued certificate serial numbers somewhat more of a challenge.
Thanks to all who participated in this discussion. Too bad their isnt a 'standard'.

You must be logged in to vote
1 reply
@DDvO
Comment options

DDvONov 9, 2024
Collaborator

Yeah, just another sad example of needless hickups incurred by
wrongly taking that leading internal(!) 00 byte as part of the value string.

Comment options

DDvO
Nov 9, 2024
Collaborator

Unfortunately, not so many people these days have a sufficient understanding of ASN.1 BER encoding (including its variant DER).

You must be logged in to vote
1 reply
@vdukhovni
Comment options

This is much too rigid a view of the design space.
Implementations that don't output leading "-" signs are perfectly justified in displaying the leading "00".
This is essentially either debugging output or in any case a close rendering of the encoding and the "00:" prefix is a legitimate design choice, just like not displaying it is, provided a "-" signs is prefixed to the first nibble.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
8 participants
@ajuliansr@levitte@nhorman@vdukhovni@DDvO@jamuir@t8m@mattcaswell
Converted from issue

This discussion was converted from issue #23236 on January 09, 2024 19:23.


[8]ページ先頭

©2009-2025 Movatter.jp