title: p12 (PKCS#12) certificat convert to DER fails using OpenSSL
body: <p>Dose somebody had a luck converting p12 crt to DER? What I am doing wrong?</p>
  
  <p>Convert p12 to DER I am using <strong>i2d_PKCS12_bio</strong>. To get ref to certificate I am using <strong>SecCertificateCreateWithData()</strong> method which returns nil. This method is taking only DER data as written in the Apple doc.</p>
  
  <p>In <a href="http://www.opensource.apple.com/source/OpenSSL/OpenSSL-7.1/openssl/doc/openssl.txt" rel="nofollow">OpenSSL documentations</a> is written:</p>
  
  <blockquote>
    <p><strong>i2d</strong>       is the standard ASN1 function that converts the internal
          structure into the DER representation: for example
          i2d_ASN1_IA5STRING().</p>
  </blockquote>
  
  <pre><code>PKCS12 *p12; // p12 was created ok.
  char *data_p12 = NULL;
  BIO *mem = BIO_new(BIO_s_mem());
  i2d_PKCS12_bio(mem, p12); // In the documentation is written that data should be converted to DER.
  int _flush __attribute__((unused)) = BIO_flush(mem);
  long size = BIO_get_mem_data(mem, &amp;data_p12);
  NSData *retData = [[NSData alloc] initWithBytes:data_p12 length:size];
  
  CFDataRef dataRef = CFDataCreate(nil, retData.bytes, retData.length);
  SecCertificateRef secRef = SecCertificateCreateWithData(nil, dataRef);
  // secRef is nil. FAIL!
  </code></pre>
  
  <p>PS: I did save p12 data as file and it work as p12 file.</p>