<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rimaz Saththar</title>
    <description>The latest articles on DEV Community by Rimaz Saththar (@rimaz17).</description>
    <link>https://dev.to/rimaz17</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4272471%2F95a3648e-5b0a-45c6-a0b4-55e7fddff92d.jpeg</url>
      <title>DEV Community: Rimaz Saththar</title>
      <link>https://dev.to/rimaz17</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rimaz17"/>
    <language>en</language>
    <item>
      <title>The OpenSSL Bug That Hid for 11 Years Behind an Ignored Return Value</title>
      <dc:creator>Rimaz Saththar</dc:creator>
      <pubDate>Sun, 29 Nov 2026 19:18:51 +0000</pubDate>
      <link>https://dev.to/rimaz17/the-openssl-bug-that-hid-for-11-years-behind-an-ignored-return-value-2np2</link>
      <guid>https://dev.to/rimaz17/the-openssl-bug-that-hid-for-11-years-behind-an-ignored-return-value-2np2</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Since 2015, OpenSSL couldn't print ASN.1 ENUMERATED fields. The output stopped mid-line, with no error and exit code 0.&lt;/li&gt;
&lt;li&gt;Cause: a 2015 refactor made &lt;code&gt;ASN1_INTEGER_to_BN()&lt;/code&gt; reject anything that isn't an INTEGER, but the printer still sent ENUMERATED values through it.&lt;/li&gt;
&lt;li&gt;Fix: use &lt;code&gt;i2s_ASN1_ENUMERATED()&lt;/code&gt; for ENUMERATED values. Four lines of C plus a regression test.&lt;/li&gt;
&lt;li&gt;Status: approved by an OpenSSL committer and labelled for backport to 3.5, 4.0 and 4.1 (&lt;a href="https://github.com/openssl/openssl/pull/33632" rel="noopener noreferrer"&gt;PR #33632&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;The bug report I picked up started with five bytes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;30 03 0a 01 01

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a complete, valid OCSP response. &lt;code&gt;30 03&lt;/code&gt; is a SEQUENCE that's 3 bytes long, and &lt;code&gt;0a 01 01&lt;/code&gt; is an ENUMERATED with the value 1. That one field is &lt;code&gt;responseStatus&lt;/code&gt;. When you ask OpenSSL to print it, you get this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;OCSP_RESPONSE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;responseStatus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rc=0&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;rc=0&lt;/code&gt; is from my shell, not OpenSSL. OpenSSL printed &lt;code&gt;responseStatus:&lt;/code&gt; and then stopped, in the middle of the line, and exited as if everything had gone fine.&lt;/p&gt;

&lt;p&gt;I'd been looking for an issue to work on in OpenSSL, and this one, &lt;a href="https://github.com/openssl/openssl/issues/33494" rel="noopener noreferrer"&gt;#33494&lt;/a&gt;, had been triaged by the maintainers and labelled &lt;em&gt;help wanted&lt;/em&gt;. It looked small. It turned out to be a regression that had been sitting in every OpenSSL release since 1.1.0, and tracking it down taught me a lot about how bugs survive in mature code.&lt;/p&gt;

&lt;h2&gt;
  
  
  A failure that doesn't look like one
&lt;/h2&gt;

&lt;p&gt;The first thing I did was reproduce it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'\x30\x03\x0a\x01\x01'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; ocspresp.der
openssl asn1parse -inform DER -in ocspresp.der -item OCSP_RESPONSE&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"rc=$?"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same result: half a line of output, nothing on stderr, exit code 0. The correct output should have been:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;OCSP_RESPONSE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;responseStatus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="na"&gt;responseBytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;ABSENT&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exit code was the first clue to why this had gone unnoticed. In &lt;code&gt;apps/asn1parse.c&lt;/code&gt;, the call that does the printing throws its result away:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="nc"&gt;ASN1_item_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bio_out&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;ASN1_item_free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ASN1_item_print()&lt;/code&gt; was returning 0 to say it had failed. Nobody was listening. So any script, CI job or person running this command would see success unless they read the output closely and noticed it was incomplete.&lt;/p&gt;

&lt;p&gt;Without &lt;code&gt;-item&lt;/code&gt;, OpenSSL's generic parser printed the same field correctly as &lt;code&gt;ENUMERATED :01&lt;/code&gt;. So the data was fine. Only the structured printer was broken, and only for one type. I tried an attribute certificate (&lt;code&gt;X509_ACERT&lt;/code&gt;), which also contains an ENUMERATED field, and it failed the same way. Everything after the first ENUMERATED field just vanished.&lt;/p&gt;

&lt;h2&gt;
  
  
  Following the number down
&lt;/h2&gt;

&lt;p&gt;ASN.1 has two types for whole numbers: INTEGER and ENUMERATED. They're encoded identically. The only difference is the tag byte (&lt;code&gt;0x02&lt;/code&gt; for INTEGER, &lt;code&gt;0x0a&lt;/code&gt; for ENUMERATED) and what they mean. An ENUMERATED is a number picked from a fixed list, like a status code. Inside OpenSSL, both are stored in the same &lt;code&gt;ASN1_STRING&lt;/code&gt; struct, and the &lt;code&gt;type&lt;/code&gt; field tells you which one you have: &lt;code&gt;V_ASN1_INTEGER&lt;/code&gt; or &lt;code&gt;V_ASN1_ENUMERATED&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;crypto/asn1/tasn_prn.c&lt;/code&gt;, the printer treats them as the same thing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;V_ASN1_INTEGER&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;V_ASN1_ENUMERATED&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;asn1_print_integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;asn1_print_integer()&lt;/code&gt; converts the value to a decimal string with &lt;code&gt;i2s_ASN1_INTEGER()&lt;/code&gt;. That function turns the value into a &lt;code&gt;BIGNUM&lt;/code&gt; first, using &lt;code&gt;ASN1_INTEGER_to_BN()&lt;/code&gt;. And that's where it stops:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="no"&gt;BIGNUM&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;asn1_string_to_bn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;ASN1_INTEGER&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ai&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BIGNUM&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;bn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;itype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ai&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="no"&gt;V_ASN1_NEG&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;itype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ERR_raise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ERR_LIB_ASN1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ASN1_R_WRONG_INTEGER_TYPE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="no"&gt;BIGNUM&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;ASN1_INTEGER_to_BN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;ASN1_INTEGER&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ai&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BIGNUM&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;bn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;asn1_string_to_bn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ai&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;V_ASN1_INTEGER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ASN1_INTEGER_to_BN()&lt;/code&gt; passes &lt;code&gt;V_ASN1_INTEGER&lt;/code&gt; as the type it expects. Our value's type is &lt;code&gt;V_ASN1_ENUMERATED&lt;/code&gt;, so the check fails, an error is raised, and &lt;code&gt;NULL&lt;/code&gt; comes back up the chain.&lt;/p&gt;

&lt;p&gt;The whole path looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnifbw93yuiycb5dx2ge8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnifbw93yuiycb5dx2ge8.png" alt="Call chain from asn1_primitive_print() down to ASN1_INTEGER_to_BN(), where ENUMERATED values are rejected" width="799" height="513"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One layer says the two types are interchangeable. A layer three calls below says they aren't. An ENUMERATED value gets sent down the INTEGER road and is turned away at the end of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Was it always like this?
&lt;/h2&gt;

&lt;p&gt;That contradiction bothered me. Code that has been printing certificates and OCSP responses for decades doesn't usually have a whole data type that just doesn't work. Either nobody had ever printed an ENUMERATED field with this function, or something had changed underneath it.&lt;/p&gt;

&lt;p&gt;The strict check raises a specific error code, &lt;code&gt;ASN1_R_WRONG_INTEGER_TYPE&lt;/code&gt;, so I asked git when that error code first appeared in the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git log -S"ASN1_R_WRONG_INTEGER_TYPE" --format='%h %ad %s' --date=short -- crypto/asn1/a_int.c


6c5b6cb035 2015-05-15 ASN1 INTEGER refactor.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One commit, from May 2015. Here's what &lt;code&gt;ASN1_INTEGER_to_BN()&lt;/code&gt; looked like just before it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="no"&gt;BIGNUM&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;ASN1_INTEGER_to_BN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;ASN1_INTEGER&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ai&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;BIGNUM&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;bn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;BIGNUM&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BN_bin2bn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ai&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ai&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nc"&gt;ASN1err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ASN1_F_ASN1_INTEGER_TO_BN&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;ASN1_R_BN_LIB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ai&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;V_ASN1_NEG_INTEGER&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nc"&gt;BN_set_negative&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No type check at all. It converted whatever bytes it was given. The printer had been quietly relying on that since the printing code was written in 2005. The 2015 refactor tightened the rule, which was a reasonable thing to do, and the printer broke without anyone noticing. OpenSSL 1.1.0, released in 2016, was the first release with the bug. Because the failure was silent, it stayed that way through 3.x and into the 4.x branches.&lt;/p&gt;

&lt;p&gt;I want to be fair to everyone involved here. Neither change was careless. The type check is a good idea, and the printer's shortcut was harmless when it was written. The bug only existed in the gap between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;I had two options:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Why I did or didn't choose it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Loosen the type check in &lt;code&gt;asn1_string_to_bn()&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Undoes a deliberate safety improvement and affects every caller in the library&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Make the printer use the right conversion&lt;/td&gt;
&lt;td&gt;Only touches the code that made the wrong assumption&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I went with the second. &lt;code&gt;i2s_ASN1_ENUMERATED()&lt;/code&gt; already existed, so I didn't need any new conversion code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;asn1_print_integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;BIO&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;ASN1_INTEGER&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;i2s_ASN1_INTEGER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="no"&gt;V_ASN1_NEG&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;V_ASN1_ENUMERATED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;i2s_ASN1_ENUMERATED&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;i2s_ASN1_INTEGER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;&amp;amp; ~V_ASN1_NEG&lt;/code&gt; part matters. OpenSSL doesn't store the sign of a number separately. It sets a &lt;code&gt;V_ASN1_NEG&lt;/code&gt; flag inside the type, so a negative ENUMERATED has the type &lt;code&gt;V_ASN1_NEG_ENUMERATED&lt;/code&gt;, not &lt;code&gt;V_ASN1_ENUMERATED&lt;/code&gt;. Without the mask, a negative value would fail the comparison and still go down the wrong path.&lt;/p&gt;

&lt;p&gt;After the fix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;OCSP_RESPONSE&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;responseStatus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="na"&gt;responseBytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;ABSENT&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing it
&lt;/h2&gt;

&lt;p&gt;I added a regression test to &lt;code&gt;test/recipes/04-test_asn1_parse.t&lt;/code&gt; that runs &lt;code&gt;asn1parse&lt;/code&gt; on the 5-byte response and checks for &lt;strong&gt;both&lt;/strong&gt; output lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight perl"&gt;&lt;code&gt;&lt;span class="nf"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;grep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/^&lt;/span&gt;\&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;responseStatus&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;$&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;grep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/^&lt;/span&gt;\&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;responseBytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;ABSENT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;$&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="s2"&gt;"asn1parse -item prints an ENUMERATED field"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Checking only &lt;code&gt;responseStatus: 1&lt;/code&gt; wouldn't be enough. The original bug cut off everything after the ENUMERATED field, so a test that ignored the next line could pass while the rest of the structure was still missing.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What I checked&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;New test, without the fix&lt;/td&gt;
&lt;td&gt;Fails&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;New test, with the fix&lt;/td&gt;
&lt;td&gt;Passes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Full &lt;code&gt;make test&lt;/code&gt; with ASan and UBSan&lt;/td&gt;
&lt;td&gt;Passes, no sanitizer reports&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Negative ENUMERATED (&lt;code&gt;-1&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Prints &lt;code&gt;-1&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Existing &lt;code&gt;ocsp-response.der&lt;/code&gt; in the test suite&lt;/td&gt;
&lt;td&gt;Prints correctly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;INTEGER fields (e.g. certificate serial numbers)&lt;/td&gt;
&lt;td&gt;Unchanged&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What happened next
&lt;/h2&gt;

&lt;p&gt;I opened &lt;a href="https://github.com/openssl/openssl/pull/33632" rel="noopener noreferrer"&gt;PR #33632&lt;/a&gt;. An OpenSSL committer reviewed and approved it, and it was labelled for backport to the 3.5, 4.0 and 4.1 branches. At the time of writing it's waiting on a second approval before it's merged.&lt;/p&gt;

&lt;p&gt;The review was useful in its own right. The reviewer said they'd personally prefer a separate &lt;code&gt;asn1_print_enumerate()&lt;/code&gt; function, but that it probably wasn't worth the churn. I think that's the right call for a fix heading into three stable branches: a smaller diff is easier to review and backport. They also pointed out that the test could feed the bytes through stdin instead of writing a file. Both were fair points, and I learned more from those two comments about how OpenSSL is maintained than from reading the code.&lt;/p&gt;

&lt;p&gt;There's one related change I left out on purpose. &lt;code&gt;asn1parse&lt;/code&gt; still ignores the return value of &lt;code&gt;ASN1_item_print()&lt;/code&gt;. Fixing that would change how the command behaves, so I mentioned it in the PR rather than slipping it into a bug fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'm taking away
&lt;/h2&gt;

&lt;p&gt;The fix was four lines. Finding out why those four lines were needed was the actual work, and a few things stuck with me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ignored return values hide bugs for years.&lt;/strong&gt; If &lt;code&gt;asn1parse&lt;/code&gt; had checked one return value, this would most likely have been caught years earlier.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Making a check stricter can break code far away.&lt;/strong&gt; When I tighten validation now, I look for callers that might depend on the looser behaviour.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git log -S&lt;/code&gt; &lt;strong&gt;answers "when did this start?"&lt;/strong&gt; Searching for when a specific error code appeared took me straight to the commit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix where the mistake is, not where it hurts.&lt;/strong&gt; The failure showed up deep in a conversion function, but the real problem was the printer's assumption.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you've been wanting to contribute to a big open-source project, look for a &lt;em&gt;help wanted&lt;/em&gt; label, comment before you start, and keep your first PR small. Mine started with five bytes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Links:&lt;/strong&gt; &lt;a href="https://github.com/openssl/openssl/issues/33494" rel="noopener noreferrer"&gt;Issue #33494&lt;/a&gt; · &lt;a href="https://github.com/openssl/openssl/pull/33632" rel="noopener noreferrer"&gt;PR #33632&lt;/a&gt;&lt;/p&gt;

</description>
      <category>openssl</category>
      <category>c</category>
      <category>opensource</category>
      <category>debugging</category>
    </item>
  </channel>
</rss>
