<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[wolfSSL - Embedded SSL Library — PKCS#7 CMS detached signature verification failure (ASN_PARSE_E)]]></title>
		<link>https://www.wolfssl.com/forums/topic1666-pkcs7-cms-detached-signature-verification-failure-asnparsee.html</link>
		<atom:link href="https://www.wolfssl.com/forums/feed-rss-topic1666.xml" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in PKCS#7 CMS detached signature verification failure (ASN_PARSE_E).]]></description>
		<lastBuildDate>Wed, 25 May 2022 22:20:45 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: PKCS#7 CMS detached signature verification failure (ASN_PARSE_E)]]></title>
			<link>https://www.wolfssl.com/forums/post6568.html#p6568</link>
			<description><![CDATA[<p>Hi Beat,</p><p>I&#039;m not sure that I am following the reasoning behind needing to reset &quot;ret = 0&quot; at that location in pkcs7.c.&nbsp; It is expected behavior that &quot;ret&quot; would be set to <strong>ASN_PARSE_E</strong> in the following section of code for your use case, since <strong>tag != ASN_OCTET_STRING</strong>:</p><div class="codebox"><pre><code>            /* get length of content in case of single part */
            if (ret == 0 &amp;&amp; !multiPart) {
                if (tag != ASN_OCTET_STRING)
                    ret = ASN_PARSE_E;

                if (ret == 0 &amp;&amp; GetLength_ex(pkiMsg, &amp;localIdx,
                            &amp;length, pkiMsgSz, NO_USER_CHECK) &lt; 0)
                    ret = ASN_PARSE_E;
            }</code></pre></div><p>Later on, we enter the &quot;else&quot; block like you mentioned above, where &quot;detached&quot; gets set to 1, &quot;length&quot; to 0, and &quot;contentLen&quot; to 0.&nbsp; The next time &quot;ret&quot; is checked is inside the &quot;#ifndef NO_PKCS7_STREAM&quot; section right below that, where:</p><div class="codebox"><pre><code>            /* content expected? */
            if ((ret == 0 &amp;&amp; length &gt; 0) &amp;&amp;
                !(pkiMsg2 &amp;&amp; pkiMsg2Sz &gt; 0 &amp;&amp; hashBuf &amp;&amp; hashSz &gt; 0)) {
                pkcs7-&gt;stream-&gt;expected = length + ASN_TAG_SZ + MAX_LENGTH_SZ;
            }</code></pre></div><p>Here, the value of &quot;ret&quot; won&#039;t matter since &quot;length&quot; has already been set to 0.&nbsp; Then, just a little below that, we reset the value of &quot;ret&quot; with the following call:</p><div class="codebox"><pre><code>            if ((ret = wc_PKCS7_StreamEndCase(pkcs7, &amp;stateIdx, &amp;idx)) != 0) {
                break;
            }</code></pre></div><p>I re-ran your reproducer application against wolfSSL (current master), after making the following changes and verification passed successfully without the additional reset of &quot;ret&quot;.&nbsp; Changes to main.c:</p><p>1.&nbsp; Removed setting &quot;pkcs7.content&quot; and &quot;pkcs7.contentSz&quot; explicitly. No longer needed for this use case after our previous modifications.<br />2.&nbsp; Switched wc_PKCS7_VerifySignedData() to &quot;wc_PKCS7_VerifySignedData_ex(), passing in &quot;shaSumOfBinary&quot;.</p><p>I have re-attached the reproducer bundle I am using here.&nbsp; Steps I am using to compile/test are:</p><div class="codebox"><pre><code>$ git clone https://github.com/wolfSSL/wolfssl.git
$ cd wolfssl
$ ./autogen.sh
$ ./configure --enable-pkcs7
$ make
$ sudo make install

$ unzip signature_and_binary_v2.zip
$ cd signature_and_binary_v2
$ gcc main.c -lwolfssl
$ ./a.out -s 4dda5fe7-55d8-481a-b6eb-cf74b1b58d4d-d319ae60-1645-42b9-a9c0-eca4395587be.bin -b test.bin

calculated SHA256 of test.bin
56 d3 bb f1 e0 a9 27 b6 57 ba 90 ea 7d c0 ec e6 
ba 60 22 39 f0 ac 00 ad 8b 87 96 af ad 20 d1 40 
Sucessfully verified</code></pre></div><p>Let me know if you think I&#039;m still overlooking something, but this looks to be working as expected for this code path to me.</p><p>Thanks,<br />Chris</p>]]></description>
			<author><![CDATA[null@example.com (chrisc)]]></author>
			<pubDate>Wed, 25 May 2022 22:20:45 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post6568.html#p6568</guid>
		</item>
		<item>
			<title><![CDATA[Re: PKCS#7 CMS detached signature verification failure (ASN_PARSE_E)]]></title>
			<link>https://www.wolfssl.com/forums/post6451.html#p6451</link>
			<description><![CDATA[<p>After all this time I had a chance to test your solution. I clone the actual state of the repository, where master was at &quot;8d70594&quot;. However function wc_PKCS7_VerifySignedData_ex didn&#039;t work from the beginning with our detached pre-calculated hash. I had to recompile wolfssl with PKCS7_STREAM disabled. The problem is a missing &quot;ret = 0&quot; here:</p><p>index 3bcbfec6f..f4c7d596e 100644<br />--- a/wolfcrypt/src/pkcs7.c<br />+++ b/wolfcrypt/src/pkcs7.c<br />@@ -4679,7 +4679,7 @@ static int PKCS7_VerifySignedData(PKCS7* pkcs7, const byte* hashBuf,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idx = localIdx;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else {<br />-<br />+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ret = 0;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* If either pkcs7-&gt;content and pkcs7-&gt;contentSz are set<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * (detached signature where user has set content explicitly<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; * into pkcs7-&gt;content/contentSz) OR pkcs7-&gt;hashBuf and</p><p>Because the following code section checks for the exitence of an embedded data length information, which doesn&#039;t exist in the detached case. So the GetLength_Ex will fail, which is okay. But the ret value will be checked again in the next code section, which is guarded with #ifndef NO_PKCS7_STREAM, without beeing updated by a regular function call in between. So, we have to set it by ourselfs.</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* get length of content in case of single part */<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ret == 0 &amp;&amp; !multiPart) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (tag != ASN_OCTET_STRING)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret = ASN_PARSE_E;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ret == 0 &amp;&amp; GetLength_ex(pkiMsg, &amp;localIdx,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;length, pkiMsgSz, NO_USER_CHECK) &lt; 0)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret = ASN_PARSE_E;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><br /><p>Do you agree?</p>]]></description>
			<author><![CDATA[null@example.com (bstraehl)]]></author>
			<pubDate>Thu, 13 Jan 2022 19:27:24 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post6451.html#p6451</guid>
		</item>
		<item>
			<title><![CDATA[Re: PKCS#7 CMS detached signature verification failure (ASN_PARSE_E)]]></title>
			<link>https://www.wolfssl.com/forums/post5976.html#p5976</link>
			<description><![CDATA[<p>Hi Beat,</p><p>I have put up a small fix in the following Pull Request to wolfSSL that should fix your use case:</p><p><a href="https://github.com/wolfSSL/wolfssl/pull/3996">https://github.com/wolfSSL/wolfssl/pull/3996</a></p><p>Testing here with your sample app, with this change, your signature correctly verifies with wc_PKCS7_VerifySignedData_ex().</p><p>Best Regards,<br />Chris</p>]]></description>
			<author><![CDATA[null@example.com (chrisc)]]></author>
			<pubDate>Thu, 29 Apr 2021 22:50:43 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post5976.html#p5976</guid>
		</item>
		<item>
			<title><![CDATA[Re: PKCS#7 CMS detached signature verification failure (ASN_PARSE_E)]]></title>
			<link>https://www.wolfssl.com/forums/post5974.html#p5974</link>
			<description><![CDATA[<p>Hi Beat,</p><p>Thanks for the clarification.&nbsp; I now see your desire/need to pass in only the pre-computed digest of the content rather than the content itself.</p><p>Let me revisit our wc_PKCS7_VerifySignedData_ex() code and I&#039;ll get back to you shortly.</p><p>Thanks,<br />Chris</p>]]></description>
			<author><![CDATA[null@example.com (chrisc)]]></author>
			<pubDate>Thu, 29 Apr 2021 20:58:28 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post5974.html#p5974</guid>
		</item>
		<item>
			<title><![CDATA[Re: PKCS#7 CMS detached signature verification failure (ASN_PARSE_E)]]></title>
			<link>https://www.wolfssl.com/forums/post5899.html#p5899</link>
			<description><![CDATA[<p>Hello Chris,<br />thank you for your information. Unfortunately I don&#039;t think that this is the solution. First of all, the functions wc_PKCS7_VerifySignedData or wc_PKCS7_VerifySignedData_ex respond with an error -140, which is ASN_PARSE_E and which is not correct, because the signature, which is attached to this topic, doesn&#039;t contain ASN1 encoding errors. This can be verified with dumpasn1 or OpenSSL. </p><p>And then in contrast to my example code, our binary to be signed is not guaranteed to be a contiguous object, but can also be a set of data junks and because of this, we sign a sha256 digest derived from those. So I modified what you suggested by replacing these lines:<br /></p><div class="codebox"><pre><code>/* when verifying detached signature, must set content and contentSz */
pkcs7.content = buffer;
pkcs7.contentSz = bytes_read;</code></pre></div><p>with this:<br /></p><div class="codebox"><pre><code>/* when verifying detached signature, must set content and contentSz */
pkcs7.content = shaSumOfBinary;
pkcs7.contentSz = WC_SHA256_DIGEST_SIZE;</code></pre></div><p>and then called wc_PKCS7_VerifySignedData() as you suggested. But besides the already mentionen ASN_PARSE_E, this wouldn&#039;t work, because the function calculates the hash of my hash, which is not what I want. For what I want, namely the verification of a signed hash, I need function wc_PKCS7_VerifySignedData_ex, which allows me to specify the hash that was signed. Please check the comments in the wolfSSL source code for this function:<br /></p><div class="codebox"><pre><code> 5621 /* variant that allows computed data hash and header/foot,
 5622  * which is useful for large data signing */</code></pre></div><p>Best regards,<br />Beat</p>]]></description>
			<author><![CDATA[null@example.com (bstraehl)]]></author>
			<pubDate>Fri, 19 Mar 2021 12:35:01 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post5899.html#p5899</guid>
		</item>
		<item>
			<title><![CDATA[Re: PKCS#7 CMS detached signature verification failure (ASN_PARSE_E)]]></title>
			<link>https://www.wolfssl.com/forums/post5897.html#p5897</link>
			<description><![CDATA[<p>Hi Beat,</p><p>I had a chance to look over your examples and believe you may have just been mis-using the wc_PKCS7_VerifySignedData API for use with detached signatures.</p><p>wc_PKCS7_VerifySignedData() can verify detached signatures, but your application needs to explicitly set the content and content size using the &quot;pkcs7.content&quot; and &quot;pkcs7.contentSz&quot; structure members before doing the verification.</p><p>Internally in pkcs7.c we do some detection of empty content in the PKCS#7/CMS bundle and purposefully set our internal error state to ASN_PARSE_E.&nbsp; But, we recover from that error state later on once we realize the user has set the pkcs7.content and pkcs7.contentSz correctly.</p><p>I have attached a modified version of your main.c (here as main_verifySignedData.c) where I explicitly set these two struct members before doing the verification.&nbsp; Compiling this against the current version of wolfSSL, I am seeing a verification success.</p><p>I did also change wc_PKCS7_VerifySignedData_ex() to the normal wc_PKCS7_VerifySignedData() in your sample application.&nbsp; wc_PKCS7_VerifySignedData_ex() requires as inputs the PKCS#7/CMS &quot;head&quot; and &quot;foot&quot; as inputs that would have been received as outputs from the corresponding call to wc_PKCS7_EncodeSignedData_ex().&nbsp; My guess was that you did not have these available and meant to use wc_PKCS7_VerifySignedData() instead.&nbsp; But, please correct me if I am wrong.&nbsp; wc_PKCS7_VerifySignedData() also handles calculation of the message digest internally, so I removed that logic from your sample code.</p><p>Best Regards,<br />Chris</p>]]></description>
			<author><![CDATA[null@example.com (chrisc)]]></author>
			<pubDate>Thu, 18 Mar 2021 23:39:05 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post5897.html#p5897</guid>
		</item>
		<item>
			<title><![CDATA[Re: PKCS#7 CMS detached signature verification failure (ASN_PARSE_E)]]></title>
			<link>https://www.wolfssl.com/forums/post5772.html#p5772</link>
			<description><![CDATA[<p>Hello Chris</p><p>Are there any news in this concern? If it is of any help, I have a version of the function wc_PKCS7_VerifySignedData that works. I fixed the issue mainly by removing the code, which deals with the interpretation of the embedded signed data.</p><p>Best regards,<br />Beat</p>]]></description>
			<author><![CDATA[null@example.com (bstraehl)]]></author>
			<pubDate>Mon, 08 Feb 2021 06:54:48 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post5772.html#p5772</guid>
		</item>
		<item>
			<title><![CDATA[Re: PKCS#7 CMS detached signature verification failure (ASN_PARSE_E)]]></title>
			<link>https://www.wolfssl.com/forums/post5745.html#p5745</link>
			<description><![CDATA[<p>Hi bstraehl,</p><p>Thanks for attaching your test application.&nbsp; We&#039;ll look into this and get back to you shortly.</p><p>Best Regards,<br />Chris</p>]]></description>
			<author><![CDATA[null@example.com (chrisc)]]></author>
			<pubDate>Fri, 22 Jan 2021 00:01:26 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post5745.html#p5745</guid>
		</item>
		<item>
			<title><![CDATA[Re: PKCS#7 CMS detached signature verification failure (ASN_PARSE_E)]]></title>
			<link>https://www.wolfssl.com/forums/post5729.html#p5729</link>
			<description><![CDATA[<p>I attached my test application here.</p>]]></description>
			<author><![CDATA[null@example.com (bstraehl)]]></author>
			<pubDate>Fri, 15 Jan 2021 15:16:54 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post5729.html#p5729</guid>
		</item>
		<item>
			<title><![CDATA[PKCS#7 CMS detached signature verification failure (ASN_PARSE_E)]]></title>
			<link>https://www.wolfssl.com/forums/post5717.html#p5717</link>
			<description><![CDATA[<p>I try to do a verification of a detached PKCS#7 CMS signature. The verification fails with ASN_PARSE_E (-140). I&#039;m using function wc_PKCS7_VerifySignedData_ex. The reason why it fails is an ASN.1 parsing synchronization error. It happens after the failing attempt to read non-existing signed data (after object 1.2.840.113549.1.7.1) from the signature. Instead on this position the sequence of certificates begins, but wc_PKCS7_VerifySignedData_ex already returned with ASN_PARSE_E. In my opinion, wc_PKCS7_VerifySignedData_ex should be able to handle both situations. Either there is signed data OR an implicit[0] set of certificates. </p><p>Please find attached the binary (its SHA256 was used for signing) and the resulting signature.</p><p>I used wolfSSL from <a href="https://github.com/wolfSSL/wolfssl">https://github.com/wolfSSL/wolfssl</a> commit: c57fee136a40f7dcd2c8315a3c6bfe602ca98b8c</p>]]></description>
			<author><![CDATA[null@example.com (bstraehl)]]></author>
			<pubDate>Wed, 13 Jan 2021 12:50:01 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post5717.html#p5717</guid>
		</item>
	</channel>
</rss>
