title: How to convert PEM certificate chain to PKCS7 in native Go?
body: <p>I am looking for any practical method for converting a <strong>PEM</strong>-encoded x509 certificate chain into <strong>PKCS7</strong> format in the Go language.</p>
  
  <p>This openssl command line illustrates what I am trying to achieve in native Go.</p>
  
  <pre><code>openssl crl2pkcs7 -nocrl -certfile certificate-chain.pem &gt; pkcs7.pem
  </code></pre>
  
  <p>I am able to run the openssl command line from within a Go program using the <code>exec</code> package, but I am looking for an efficient solution in Go.</p>
  
  <p>The input file contains a sequence of certificates in PEM format, starting with -----BEGIN CERTIFICATE-----\n and <code>base64</code> encoded data.  The desired output needs to be in PKCS7 format, starting with -----BEGIN PKCS7-----.</p>
  
  <p>I am looking for an efficient solution, because I am not actually reading and writing files, but handling lots of certificates as strings in memory.</p>
  
  <p>Any suggestions appreciated.</p>