title: How to extract leaf certificates from PKCS #7 files
body: <p>I have extracted some certificates from windows executable files encoded in DER format and then convert it to readable format using this openssl command:</p>
  
  <pre><code># openssl pkcs7 -inform DER -print_certs -text -in cert.der
  </code></pre>
  
  <p>The output file consist of several certificates. How can I filter out all the certificates except the leaf one. I don't need for example intermediate or TSA ones.</p>
  
  <p>For example this is one of the extracted certificates:</p>
  
  <p><a href="http://%20%20[1]:%20https://pastebin.com/ij2h8i63" rel="nofollow noreferrer">[1]: https://pastebin.com/ij2h8i63</a></p>