title: how to pass PEM certificate as first arg of i2d_X509
body: <p>I'm creating self-signed certificate by:</p>
  
  <pre><code>openssl req -new -x509 -key privkey.pem -out cert.pem -days 1095
  </code></pre>
  
  <p>How do I pass cert.pem to i2d_X509?
  I need something like:</p>
  
  <pre><code> len = i2d_X509(".\cert.pem", &amp;buf);
  </code></pre>
  
  <p>but my certificate in a PEM file..</p>
  
  <p>Here is my code: (I used the example in <a href="https://www.openssl.org/docs/crypto/d2i_X509.html" rel="nofollow">https://www.openssl.org/docs/crypto/d2i_X509.html</a>)</p>
  
  <pre><code>#include &lt;openssl/x509.h&gt;
  #include &lt;stdio.h&gt;
  
  int main(void)
  {
      int len,i;
      unsigned char *buf;
  
      buf = NULL;
      len = i2d_X509((X509*)".\cert.pem", &amp;buf);
  
      if (len &lt; 0){
          printf("error len &lt; 0");
          return -1;
       }
  
      printf("buf:");
      for (i = 0; i &lt; len ; i++)
          printf("0x%02X", *(buf+i));
  
      return 0;
  }
  </code></pre>