title: Store a private key and a certificate in C++/OpenSSL
body: <p>I need to store certificates and their private key in memory.
  Certificates can be in the 4 following formats : PEM, PKCS12, PKCS7, DER.
  I'ill need to write them back as PEM later.</p>
  
  <p>All the snippets i see are storing only the public certificate in a <code>X509</code> struct.
  What about the private part ??</p>
  
  <p>I've found a way using <code>X509_INFO</code>, but i got a major problem with it :
  I haven't find a way to get a <code>X509_INFO</code> from DER/PKCS7/PKCS12 files</p>
  
  <p>For the moment i got the following code : </p>
  
  <pre><code>    QList&lt;X509_INFO*&gt;* Certificat::stringPEMToX509_INFO(QString stringPem)
  {
      QList &lt;X509_INFO*&gt;* liste_certificats = new QList&lt;X509_INFO*&gt;;
      STACK_OF(X509_INFO)* pile_certificats = NULL;
      X509_INFO* certificat;
  
      BIO* bio = BIO_new(BIO_s_mem());
  
      const char* pem = stringPem.toAscii().constData();
      BIO_puts(bio, pem);
  
      //https://github.com/openssl/openssl/blob/master/crypto/pem/pem_info.c
      pile_certificats = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
  
      for (int i = 0; i &lt; sk_X509_INFO_num(pile_certificats); i++)
      {
          certificat = sk_X509_INFO_value(pile_certificats, i);
          liste_certificats-&gt;push_back(certificat);
      }
      sk_X509_INFO_pop_free(pile_certificats, X509_INFO_free);
      BIO_free_all(bio);
  
      return liste_certificats;
  
  }
  </code></pre>
  
  <p>My goal would be to have the same function but for DER, PKCS12 and PKCS7.</p>
  
  <p>I tried to get a X509_INFO from a DER like this : </p>
  
  <pre><code>p12 = d2i_PKCS12_bio(bio, NULL);
  certificat = X509_INFO_new();
  certificat-&gt;x509 = cert;
  certificat-&gt;x_pkey = pkey;
  </code></pre>
  
  <p>But x_pkey is a <code>X509_PKEY</code> and pkey an <code>EVP_PKEY</code>...</p>
  
  <p>If there is no way to store it as a single struct, would it be possible to store my certificates as <code>X509</code> + a <code>EVP_PKEY</code> for the private key, and still output both private and public part in a <code>PEM</code> ?</p>