<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Konstantin Shemyak - Tech Notes (Posts about pkcs#7)</title><link>https://technotes.shemyak.com/</link><description></description><atom:link href="https://technotes.shemyak.com/categories/pkcs7.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2021 &lt;a href="mailto:konstantin@shemyak.com"&gt;Konstantin Shemyak&lt;/a&gt; </copyright><lastBuildDate>Fri, 02 Apr 2021 15:51:34 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>OpenSSL PKCS7 verification and certificate "Extended Key Usage" extension</title><link>https://technotes.shemyak.com/posts/smimesign-extended-key-usage-extension-for-openssl-pkcs7-verification/</link><dc:creator>Konstantin Shemyak</dc:creator><description>&lt;div&gt;&lt;h3&gt;Problem&lt;/h3&gt;
&lt;p&gt;You verify a signature of &lt;strong&gt;PKCS#7&lt;/strong&gt; structure with OpenSSL and get error&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;  &lt;span class="n"&gt;unsupported&lt;/span&gt; &lt;span class="n"&gt;certificate&lt;/span&gt; &lt;span class="n"&gt;purpose&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;This post explains the reason for this error and ways to proceed.&lt;/p&gt;
&lt;h3&gt;Background&lt;/h3&gt;
&lt;p&gt;By "verify a signature", one probably means that:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The signature itself (e.g. an RSA block) taken over the corresponding
   data (or its digest) validates against the &lt;strong&gt;signing certificate&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Two sets of certificates are available, which we'd call
   "&lt;strong&gt;trusted certificates&lt;/strong&gt;" and "&lt;strong&gt;chaining certificates&lt;/strong&gt;".
   A chain from the &lt;strong&gt;signing certificate&lt;/strong&gt; up to at
   least one of the &lt;strong&gt;trusted certificates&lt;/strong&gt; can be built with the
   &lt;strong&gt;chaining certificates&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;All certificates in this chain have "acceptable" X.509v3 extensions.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first requirement is clear.&lt;/p&gt;
&lt;p&gt;The second one is clear when the sets are defined.
OpenSSL API requires them to be passed as parameters for the
verification.&lt;/p&gt;
&lt;p&gt;The last requirement relies on X.509v3 extensions, which are 
a &lt;a href="https://cyberside.net.ee/docs/T2a_X509_Certs.pdf"&gt;terrible mess&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It's hard to provide a non-messy solution for a messy specification.
Section &lt;code&gt;CERTIFICATE EXTENSIONS&lt;/code&gt; in the OpenSSL manual for &lt;code&gt;x509&lt;/code&gt;
subcommand has this passage:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;The&lt;/span&gt; &lt;span class="nv"&gt;actual&lt;/span&gt; &lt;span class="nv"&gt;checks&lt;/span&gt; &lt;span class="nv"&gt;done&lt;/span&gt; &lt;span class="nv"&gt;are&lt;/span&gt; &lt;span class="nv"&gt;rather&lt;/span&gt; &lt;span class="nv"&gt;complex&lt;/span&gt; &lt;span class="nv"&gt;and&lt;/span&gt; &lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="nv"&gt;various&lt;/span&gt; &lt;span class="nv"&gt;hacks&lt;/span&gt; &lt;span class="nv"&gt;and&lt;/span&gt;
&lt;span class="nv"&gt;workarounds&lt;/span&gt; &lt;span class="nv"&gt;to&lt;/span&gt; &lt;span class="nv"&gt;handle&lt;/span&gt; &lt;span class="nv"&gt;broken&lt;/span&gt; &lt;span class="nv"&gt;certificates&lt;/span&gt; &lt;span class="nv"&gt;and&lt;/span&gt; &lt;span class="nv"&gt;software&lt;/span&gt;.
&lt;/pre&gt;


&lt;p&gt;It looks like PKCS7 verification fell victim of these "hacks and workarounds".&lt;/p&gt;
&lt;h3&gt;OpenSSL certificate verification and X.509v3 extensions&lt;/h3&gt;
&lt;p&gt;Before getting to the topic (verifying PKCS#7 structures), look at
how OpenSSL verifies &lt;strong&gt;certificates&lt;/strong&gt;. Both command-line &lt;code&gt;openssl verify&lt;/code&gt;
and C API &lt;code&gt;X509_verify_cert()&lt;/code&gt; have a notion of &lt;strong&gt;purpose&lt;/strong&gt;, explained
in the section &lt;code&gt;CERTIFICATE EXTENSIONS&lt;/code&gt; of &lt;code&gt;man x509&lt;/code&gt;.
This notion seems to be particular to OpenSSL.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If the &lt;strong&gt;purpose&lt;/strong&gt; is not specified, then OpenSSL does not check the
  certificate extensions at all.&lt;/li&gt;
&lt;li&gt;Otherwise, for each &lt;strong&gt;purpose&lt;/strong&gt;, OpenSSL allows certain combinations
  of the extensions. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The correspondence between OpenSSL's &lt;strong&gt;purpose&lt;/strong&gt; and X.509v3 extensions
is nothing like one-to-one.
For example, purpose &lt;code&gt;S/MIME Signing&lt;/code&gt; (or in short variant &lt;code&gt;smimesign&lt;/code&gt;)
requires that:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;"Common S/MIME Client Tests" pass (description of how they translate
   to X.509v3 extension takes a long paragraph in &lt;code&gt;man x509&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Either &lt;code&gt;KeyUsage&lt;/code&gt; extension is &lt;strong&gt;not present&lt;/strong&gt;, or it is present and
   contains at least one of &lt;code&gt;digigalSignature&lt;/code&gt; and &lt;code&gt;nonRepudiation&lt;/code&gt; flags.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For another example, there seems to be no OpenSSL command-line option
for &lt;code&gt;verify&lt;/code&gt; to require presense of Extended Key Usage bits like
&lt;code&gt;codeSigning&lt;/code&gt;.
For that, one must use C API to separately check every extension bit. &lt;/p&gt;
&lt;p&gt;So far, this sounds about as logical as it could be
to somehow handle The Terrible Mess of X.509v3 extensions.
OpenSSL CLI seems to have made an attempt to compose some "frequently used
combinations" of the extensions and call them with own term "purpose".&lt;/p&gt;
&lt;h3&gt;OpenSSL PKCS#7 verification and X.509v3 extensions&lt;/h3&gt;
&lt;p&gt;By reason unknown yet to the author, OpenSSL uses a &lt;em&gt;different&lt;/em&gt; strategy
when verifying PKCS#7.&lt;/p&gt;
&lt;h4&gt;Command-line&lt;/h4&gt;
&lt;p&gt;There are two command-line utilities which can do that:
&lt;code&gt;openssl smime -verify&lt;/code&gt; and &lt;code&gt;openssl cms -verify&lt;/code&gt; 
(S/MIME and CMS are both PKCS#7).
Both accept &lt;code&gt;-purpose&lt;/code&gt; option, which according to manual pages
has the same meaning as for certificate verification.
&lt;strong&gt;But it does not.&lt;/strong&gt; These are the differences:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If no &lt;code&gt;-purpose&lt;/code&gt; option is passed, both commands behave as though
   they received &lt;code&gt;-purpose smimesign&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It is possible to disable this &lt;code&gt;smimesign&lt;/code&gt; purpose checking by passing
   &lt;code&gt;-purpose any&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;C API&lt;/h4&gt;
&lt;p&gt;On the C API side, one is supposed to use &lt;code&gt;PKCS7_verify()&lt;/code&gt; for PKCS#7
verification. This function also behaves as though it verifies with
&lt;code&gt;smimesign&lt;/code&gt; purpose. 
(see setting &lt;code&gt;X509_PURPOSE_SMIME_SIGN&lt;/code&gt; in &lt;code&gt;pk7_doit.c:919&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Similarly as with the command-line, it is possible to disable checking
the extensions, although with more typing.&lt;/p&gt;
&lt;p&gt;In the C API, the verification "purpose" is a property of &lt;code&gt;X509_STORE&lt;/code&gt;,
passed to &lt;code&gt;PKCS7_verify()&lt;/code&gt;, which plays the role of the 
&lt;strong&gt;trusted certificate set&lt;/strong&gt;. &lt;/p&gt;
&lt;p&gt;Side note: manipulation of the parameters directly
on the store was added only to OpenSSL 1.1.0 
with &lt;code&gt;X509_STORE_get0_param(X509_STORE *store)&lt;/code&gt;.
In earlier versions, an &lt;code&gt;X509_STORE_CTX&lt;/code&gt; must have been created from
the store and parameters manipulates with &lt;code&gt;X509_STORE_CTX_get0_param()&lt;/code&gt;.
BTW support for OpenSSL v1.0.1 has ended just on the day of this writing.&lt;/p&gt;
&lt;h4&gt;Possible reasoning&lt;/h4&gt;
&lt;p&gt;One might imagine reasoning like this: for &lt;code&gt;openssl smime&lt;/code&gt;, &lt;code&gt;smimesign&lt;/code&gt; is
kind of "default purpose" and thus is implicitly required; and
&lt;code&gt;openssl cms&lt;/code&gt; is in fact an attempt to rewrite &lt;code&gt;openssl smime&lt;/code&gt;, thus
behaving in the same way.&lt;/p&gt;
&lt;p&gt;Such behavior is fine for S/MIME, and is not what you would expect for
anything else packed into PKCS#7.&lt;/p&gt;
&lt;p&gt;Translating from OpenSSL's "purpose" to X.509v3 extensions, verification
fails unless your &lt;strong&gt;signing certificate&lt;/strong&gt; satisfies the two conditions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;If&lt;/strong&gt; the &lt;code&gt;Key Usage&lt;/code&gt; extension is present, &lt;strong&gt;then&lt;/strong&gt; it must include
   the &lt;code&gt;digitalSignature&lt;/code&gt; bit or the &lt;code&gt;nonRepudiation&lt;/code&gt; bit.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;If&lt;/strong&gt; the &lt;code&gt;Extended Key Usage&lt;/code&gt; extension is present, &lt;strong&gt;then&lt;/strong&gt; it
   must include &lt;code&gt;email protection&lt;/code&gt; OID.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In fact, the first condition is "reasonable": RFC5280 states in 
&lt;a href="https://tools.ietf.org/html/rfc5280#section-4.2.1.3"&gt;section "Key Usage"&lt;/a&gt;
that&lt;/p&gt;
&lt;p&gt;For example, when an RSA key should be used only to verify signatures on
   objects other than public key certificates and CRLs, the
   digitalSignature and/or nonRepudiation bits would be asserted.&lt;/p&gt;
&lt;p&gt;PKCS#7 qualifies as "object other than public key certificates and CRLs".
But the second condition is not relevant for anything else than S/MIME.
(Of course, in the end it is your certificate practice policy which
determines, what is accepted and what not; the above is just "common sense").&lt;/p&gt;
&lt;h3&gt;Demo&lt;/h3&gt;
&lt;h4&gt;Prepare the files&lt;/h4&gt;
&lt;p&gt;Create a chain of certificates: self-signed "&lt;strong&gt;root&lt;/strong&gt;",
then an "&lt;strong&gt;intermediate&lt;/strong&gt;" signed by the root,
then a "&lt;strong&gt;signing&lt;/strong&gt;" signed by the intermediate.
Add some &lt;code&gt;extendedKeyUsage&lt;/code&gt; extension to the &lt;strong&gt;signing&lt;/strong&gt;, but do not add &lt;code&gt;emailProtection&lt;/code&gt;.
For example, add &lt;code&gt;codeSigning&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Create appropriate OpenSSL config files, as explained in
&lt;a href="https://technotes.shemyak.com/posts/min-openssl-cnf"&gt;"minimum openssl.cnf" post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Create requests for all the three:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;  $ openssl req -config openssl-CA.cnf -new -x509 -nodes -outform pem -out root.pem -keyout root-key.pem
  $ openssl req -config openssl-CA.cnf -new -nodes -out intermediate.csr -keyout intermediate-key.pem
  $ openssl req -config openssl-signing.cnf -new -nodes -outform pem -out signing.csr -keyout signing-key.pem
&lt;/pre&gt;


&lt;p&gt;Sign the &lt;strong&gt;intermediate&lt;/strong&gt; and the &lt;strong&gt;signing&lt;/strong&gt; certificates:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;  $ mkdir -p demoCA/newcerts
  $ touch demoCA/index.txt
  $ &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'01'&lt;/span&gt; &amp;gt; demoCA/serial
  $ openssl ca -config openssl-CA.cnf -in intermediate.csr -out intermediate.pem -keyfile root-key.pem -cert root.pem
  $ openssl ca -config openssl-signing.cnf -in signing.csr -out signing.pem -keyfile intermediate-key.pem -cert intermediate.pem
&lt;/pre&gt;


&lt;p&gt;Create some PKCS7 structure, signed with the &lt;strong&gt;signing&lt;/strong&gt; certificate. 
The &lt;strong&gt;chain certificates&lt;/strong&gt; must be provided during the verification, or
embedded into the signature. Let's embed the intermediate certificate.
(If there had been more than one certificate in the chain, they would
need to be simply placed in one &lt;code&gt;.pem&lt;/code&gt; file):&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;  $ &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'Hello, world!'&lt;/span&gt; &amp;gt; data.txt
  $ openssl smime -sign -in data.txt -inkey signing-crlsign-key.pem -signer signing-crlsign.pem -certfile intermediate.pem -nodetach &amp;gt; signed-crlsign.pkcs7
&lt;/pre&gt;


&lt;p&gt;We have everything ready for verifying.&lt;/p&gt;
&lt;h4&gt;Verification with command-line OpenSSL tools&lt;/h4&gt;
&lt;p&gt;Attempt to verify it:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;  $ openssl smime -verify -CAfile root.pem -in signed-crlsign.pkcs7 -out /dev/null -signer signing-crlsign.pem 
  Verification failure
  &lt;span class="m"&gt;139944505955992&lt;/span&gt;:error:21075075:PKCS7 routines:PKCS7_verify:certificate verify error:pk7_smime.c:336:Verify error:unsupported certificate purpose
&lt;/pre&gt;


&lt;p&gt;Attempt to verify, skipping extension checks:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;  $ openssl smime -verify -CAfile root.pem -in signed-crlsign.pkcs7 -out /dev/null -signer signing-crlsign.pem -purpose any
  Verification successful
&lt;/pre&gt;


&lt;p&gt;Attempt to verify it, specifying the OpenSSL "purpose" which the signing certificate satisfies:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;  $ openssl smime -verify -CAfile root.pem -in signed-crlsign.pkcs7 -out /dev/null -signer signing-crlsign.pem -purpose crlsign
  Verification successful
&lt;/pre&gt;


&lt;h4&gt;Verification with the C OpenSSL API&lt;/h4&gt;
&lt;p&gt;The code below is "demo", any real application would have at least to
check return codes of all system calls and free any allocated resources.
But it shows how the verification of PKCS#7 structure (unexpectedly)
fails, and succeeds after setting the "purpose" which the signing
certificate satisfies:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;
    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;
    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;fcntl.h&amp;gt;              /* open() */&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;

    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;openssl/bio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;
    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;openssl/err.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;
    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;openssl/ssl.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;
    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;openssl/pkcs7.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;
    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;openssl/safestack.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;
    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;openssl/x509.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;
    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;openssl/x509v3.h&amp;gt;     /* X509_PURPOSE_ANY */&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;
    &lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;openssl/x509_vfy.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&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;argc&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;argv&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;X509_STORE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;trusted_store&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;X509_STORE_CTX&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;STACK_OF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X509&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cert_chain&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;X509&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;intermediate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;signing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;BIO&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;in&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;purpose&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="n"&gt;X509_VERIFY_PARAM&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;verify_params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;PKCS7&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kt"&gt;FILE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;fp&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;fd&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="n"&gt;SSL_library_init&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="n"&gt;SSL_load_error_strings&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

      &lt;span class="n"&gt;fd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"signed-ext-no-smimesign.pkcs7"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;O_RDONLY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BIO_new_fd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BIO_NOCLOSE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;p7&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SMIME_read_PKCS7&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="n"&gt;cert_chain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sk_X509_new_null&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

      &lt;span class="n"&gt;fp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"root.pem"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"r"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PEM_read_X509&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;sk_X509_push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cert_chain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="n"&gt;fp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"intermediate.pem"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"r"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;intermediate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PEM_read_X509&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;sk_X509_push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cert_chain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;intermediate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="n"&gt;trusted_store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;X509_STORE_new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="n"&gt;X509_STORE_add_cert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trusted_store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="n"&gt;fp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"signing-ext-no-smimesign.pem"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"r"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;signing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PEM_read_X509&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&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="n"&gt;PKCS7_verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cert_chain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;trusted_store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&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;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Verification without specifying params: %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&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="s"&gt;"OK"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"failure"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="cm"&gt;/* Now set a suitable OpenSSL's "purpose", or disable its checking.&lt;/span&gt;
&lt;span class="cm"&gt;       * Note: since OpenSSL 1.1.0, we'd not need `ctx`, but could just use:&lt;/span&gt;
&lt;span class="cm"&gt;       * verify_params = X509_STORE_get0_param(trusted_store); */&lt;/span&gt;

      &lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;X509_STORE_CTX_new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="n"&gt;X509_STORE_CTX_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;trusted_store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cert_chain&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;verify_params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;X509_STORE_CTX_get0_param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;purpose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;X509_PURPOSE_get_by_sname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"crlsign"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="cm"&gt;/* Or: purpose = X509_PURPOSE_ANY */&lt;/span&gt;
      &lt;span class="n"&gt;X509_VERIFY_PARAM_set_purpose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;verify_params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;purpose&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;X509_STORE_set1_param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trusted_store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;verify_params&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="n"&gt;PKCS7_verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cert_chain&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;trusted_store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&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;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Verification with 'crlsign' purpose: %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&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="s"&gt;"OK"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"failure"&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;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;If our policy requires &lt;code&gt;crlSign&lt;/code&gt; Key Usage, then we can
use this example code. What if the policy needs some extension
combination for which there is no suitable OpenSSL "purpose" - for example,
&lt;code&gt;CodeSigning&lt;/code&gt; Extended Key Usage? In that case it would not be possible
to do it with just one call to &lt;code&gt;PKCS7_verify&lt;/code&gt;, but the extensions
need to be checked separately.&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;If you use OpenSSL for verifying PKCS#7 signatures, you should check
whether either the following holds:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Your signing certificate has &lt;code&gt;Extended Key Usage&lt;/code&gt; extension,
   but no &lt;code&gt;emailProtection&lt;/code&gt; bit.&lt;/li&gt;
&lt;li&gt;Your signing certificate has &lt;code&gt;KeyUsage&lt;/code&gt; extension, but no
   &lt;code&gt;digitalSignature&lt;/code&gt; neither &lt;code&gt;nonRepudiation&lt;/code&gt; OID.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If this is the case, then verification with OpenSSL fails even if your
signature "should" verify correctly.&lt;/p&gt;
&lt;p&gt;For checking signatures with command-line &lt;code&gt;openssl smime -verify&lt;/code&gt;,
a partial workaround can be adding option &lt;code&gt;-purpose any&lt;/code&gt;.
In this case OpenSSL will not check Extended Key Usage extensions at all.
This can be acceptable or not by your verification policy.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;-purpose&lt;/code&gt; option allows to check only for certain
(although probably common) x509v3 extension combinations.
OpenSSL defines a number of what it calls "purposes".
If you need to check a combination which does not correspond to any
of these "purposes", it must be done in a separate operation.&lt;/p&gt;
&lt;p&gt;For checking signatures with C API &lt;code&gt;PKCS7_verify()&lt;/code&gt;, the algorithm
can be the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If your policy does not care about X.509v3 extensions, set your
   verification parameters to &lt;code&gt;X509_PURPOSE_ANY&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Otherwise, check X509v3 extensions of the signing certificate as required
   by your policy
   (&lt;a href="https://zakird.com/2013/10/13/certificate-parsing-with-openssl#other-x509-extensions"&gt;example&lt;/a&gt;).
   Set a custom verification callback with &lt;code&gt;X509_STORE_CTX_set_verify_cb()&lt;/code&gt;,
   which might either ignore the "unsupported certificate purpose" error, i.e.
   &lt;code&gt;X509_V_ERR_INVALID_PURPOSE&lt;/code&gt;, or have some more complicated logic.&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;</description><category>openssl</category><category>pkcs#7</category><category>signature</category><category>x.509</category><guid>https://technotes.shemyak.com/posts/smimesign-extended-key-usage-extension-for-openssl-pkcs7-verification/</guid><pubDate>Sat, 31 Dec 2016 20:34:22 GMT</pubDate></item><item><title>JAR signature block file format</title><link>https://technotes.shemyak.com/posts/jar-signature-block-file-format/</link><dc:creator>Konstantin Shemyak</dc:creator><description>&lt;div&gt;&lt;p&gt;&lt;strong&gt;Summary:&lt;/strong&gt; this post explains the content of the &lt;strong&gt;JAR signature block file&lt;/strong&gt; - 
that is, the file &lt;code&gt;META-INF/*.RSA&lt;/code&gt;, &lt;code&gt;META-INF/*.DSA&lt;/code&gt;, &lt;code&gt;META-INF/*.EC&lt;/code&gt;
or &lt;code&gt;SIG-*&lt;/code&gt; inside the JAR.&lt;/p&gt;
&lt;h3&gt;Oracle does not document it&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Signed JAR file&lt;/strong&gt; contains the following additions over a non-signed JAR:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Checksums over the JAR content, stored in text files
  &lt;code&gt;META-INF/MANIFEST.MF&lt;/code&gt; and &lt;code&gt;META-INF/*.SF&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The actual cryptographic signature (created with the private key
  of the signer) over the checksums in a binary &lt;strong&gt;signature block file&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Surprisingly, format of the latter does not seem to be documented by Oracle. &lt;a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#Digital_Signatures"&gt;JAR file specification&lt;/a&gt;
provides only a useful knowledge that
&lt;em&gt;"These are binary files not intended to be interpreted by humans"&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Here, the content of this "signature block file" is explained.
We show how it can be created and verified with non-Java tool: OpenSSL.&lt;/p&gt;
&lt;h3&gt;Create a sample signature block file&lt;/h3&gt;
&lt;p&gt;For our investigation, generate such file by signing some data with &lt;strong&gt;jarsigner&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Make an RSA private key (and store it unencrypted), corresponding
  self-signed certificate, pack them in a format jarsigner understands:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;openssl genrsa -out key.pem
openssl req -x509 -new -key key.pem -out cert.pem -subj &lt;span class="s1"&gt;'/CN=foo'&lt;/span&gt;
openssl pkcs12 -export -in cert.pem -inkey key.pem -out keystore.pfx -passout pass:123456 -name SEC_PAD
&lt;/pre&gt;


&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;openssl req&lt;/code&gt; command reads the default OpenSSL configuration file.
If you care about exact content of your certificate, you should
not rely on the system default but write your own (you can start with a
&lt;a href="https://technotes.shemyak.com/posts/min-openssl-cnf"&gt;minimal example&lt;/a&gt;).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create the data, jar it, sign the JAR, and unpack the "META-INF" directory:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'Hello, world!'&lt;/span&gt; &amp;gt; data
jar cf data.jar data
jarsigner -keystore keystore.pfx -storetype PKCS12 -storepass &lt;span class="m"&gt;123456&lt;/span&gt; data.jar SEC_PAD
unzip data.jar META-INF/*
&lt;/pre&gt;


&lt;p&gt;The "signature block file" is &lt;code&gt;META-INF/SEC_PAD.RSA&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;What does this block contain&lt;/h3&gt;
&lt;p&gt;The file appears to be a &lt;a href="http://www.herongyang.com/Cryptography/Certificate-Format-DER-Distinguished-Encoding-Rules.html"&gt;DER-encoded&lt;/a&gt;
&lt;a href="https://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One"&gt;ASN.1&lt;/a&gt;
&lt;a href="https://tools.ietf.org/html/rfc2315"&gt;PKCS#7&lt;/a&gt; data structure.
DER-encoded ASN.1 file can be examined with &lt;code&gt;asn1parse&lt;/code&gt; subcommand of the OpenSSL:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;openssl asn1parse -in META-INF/SEC_PAD.RSA -inform der -i &amp;gt; jarsigner.txt
&lt;/pre&gt;


&lt;p&gt;For more verbosity, you may use some ASN.1 decoder such as one at
&lt;a href="http://lapo.it/asn1js/"&gt;lapo.it&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You'll see that the two top-level components are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The certificate.&lt;/li&gt;
&lt;li&gt;256-byte RSA signature.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can extract the signature bytes from the binary data and
&lt;a href="http://qistoph.blogspot.com/2012/01/manual-verify-pkcs7-signed-data-with.html"&gt;verify&lt;/a&gt; (=decrypt with the public key) them with &lt;code&gt;openssl rsautl&lt;/code&gt;.
That includes some "low-level" operations and brings you one more step down 
to understanding the file's content.
A simple "high-level" verification command, not involving manual byte
manipulation, would be:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;openssl cms -verify -noverify -content META-INF/SEC_PAD.SF -in META-INF/SEC_PAD.RSA -inform der
&lt;/pre&gt;


&lt;p&gt;This command tells: &lt;em&gt;"Check that the CMS structure in &lt;code&gt;META-INF/SEC_PAD.RSA&lt;/code&gt;
is really a signature of &lt;code&gt;META-INF/SEC_PAD.SF&lt;/code&gt;; do not attempt to validate
the certificate"&lt;/em&gt;. Congratulations, we have verified the JAR signature
without Java tools.&lt;/p&gt;
&lt;h3&gt;Creating the signature block file with OpenSSL&lt;/h3&gt;
&lt;p&gt;For this example, we created the signature block file with &lt;strong&gt;jarsigner&lt;/strong&gt;. 
There are at least two OpenSSL commands which can produce similar 
structures: &lt;code&gt;openssl cms&lt;/code&gt; and &lt;code&gt;openssl smime&lt;/code&gt;, with the options given below:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;openssl cms -sign -binary -noattr -in META-INF/SEC_PAD.SF -outform der -out openssl-cms.der -signer cert.pem -inkey key.pem -md sha256
openssl smime -sign -noattr -in META-INF/SEC_PAD.SF -outform der -out openssl-smime.der -signer cert.pem -inkey key.pem -md sha256
&lt;/pre&gt;


&lt;p&gt;Let's decode the created files and compare them to what has been produced
with &lt;code&gt;jarsigner&lt;/code&gt;:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;openssl asn1parse -inform der -in openssl-cms.der -i &amp;gt; openssl-cms.txt
openssl asn1parse -inform der -in openssl-smime.der -i &amp;gt; openssl-smime.txt
&lt;/pre&gt;


&lt;h3&gt;Testing the "DIY signature"&lt;/h3&gt;
&lt;p&gt;Underlying ASN.1 structures are, in both &lt;strong&gt;cms&lt;/strong&gt; and &lt;strong&gt;smime&lt;/strong&gt; cases,
very close but not identical to those made by &lt;code&gt;jarsigner&lt;/code&gt;. 
As the format of the signature block file is not specified,
we can only do tests to have some ground to say that "it works".
Just replace the original signature block file with our signature
created by OpenSSL:&lt;/p&gt;
&lt;pre class="code literal-block"&gt;&lt;span&gt;&lt;/span&gt;cp openssl-cms.der META-INF/SEC_PAD.RSA
zip -u data.jar META-INF/SEC_PAD.RSA
jarsigner -verify -keystore keystore.pfx -storetype PKCS12 -storepass &lt;span class="m"&gt;123456&lt;/span&gt; data.jar SEC_PAD
&lt;/pre&gt;


&lt;p&gt;Lucky strike: a signature produced by &lt;code&gt;openssl cms&lt;/code&gt; is recognized by
&lt;code&gt;jarsigner&lt;/code&gt; (that is, at least "it worked for me").&lt;/p&gt;
&lt;p&gt;Note that the &lt;strong&gt;data&lt;/strong&gt; which is signed is &lt;code&gt;SEC_PAD.SF&lt;/code&gt;, and it was 
itself created by jarsigner. If not using the latter, you'll need to
produce that file in some way.&lt;/p&gt;
&lt;h3&gt;What's the use for this knowledge?&lt;/h3&gt;
&lt;p&gt;Besides better understanding your data, one can think of at least two
reasons to sign JARs with non-native tools. Both are somewhat untypical,
but not completely irrelevant:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;The signature must be produced in a system, where native Java tools are not available. 
Such system must have access to private key, and security administrators
may like the idea of not having such overbloated software as JRE in a
tightly controlled environment.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The signature must be produced or verified in a system, where available tools do not support the required signature algorithm.
Examples "why" include compliance with regulations or compatibility with
legacy systems. There are systems where &lt;a href="https://technotes.shemyak.com/posts/support-for-elliptic-curves-by-jarsigner/"&gt;testing which elliptic curves are supported by jarsigner&lt;/a&gt;
reveals just three curves (which is not much).&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Summary (again)&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;JAR signature block file&lt;/strong&gt; is a DER-encoded PKCS#7 structure.&lt;/li&gt;
&lt;li&gt;Its exact content can be viewed with any ASN.1 decoder, e.g. with &lt;code&gt;openssl asn1parse&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;OpenSSL can verify signatures in signature block files and create almost
  identical structures, which have been reported to be accepted by Java
  tools.&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;</description><category>jar</category><category>jarsigner</category><category>openssl</category><category>pkcs#7</category><category>signature</category><guid>https://technotes.shemyak.com/posts/jar-signature-block-file-format/</guid><pubDate>Mon, 08 Dec 2014 12:18:00 GMT</pubDate></item></channel></rss>