title: Reading raw SSL/TLS Certificate from memory using OpenSSL
body: <p>I'm having trouble using OpenSSL to parse certificates from bytes acquired from a pcap file. The data is coming directly from <code>ssl.handshake.certificate</code> field in Wireshark (when observing the pcap).</p>
  
  <p>The raw data is from a pcap file i found <a href="https://git.lekensteyn.nl/peter/wireshark-notes/commit/tls/mysql-ssl.pcapng?id=8cfd2f667e796e4c0e3bdbe117e515206346f74a" rel="nofollow noreferrer">from Wireshark</a>. This is just the 10th packet and is a certificate with a commonName 'Ubuntu'. I believe my problem is that i'm giving the OpenSSL function data that is incorrectly formatted but i wouldve thought that it could handle network order bytes (as i have directly from a pcap).</p>
  
  <pre><code>const unsigned int raw[] = {
    0x30, 0x82, 0x02, 0xb2, 0x30, 0x82, 0x01, 0x9a,
    0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
    0xcd, 0xdb, 0x2b, 0xb3, 0xe2, 0xa7, 0x1b, 0x88,
    0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
    0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30,
    0x11, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
    0x04, 0x03, 0x13, 0x06, 0x75, 0x62, 0x75, 0x6e,
    0x74, 0x75, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x35,
    0x30, 0x31, 0x32, 0x37, 0x31, 0x36, 0x35, 0x35,
    0x30, 0x31, 0x5a, 0x17, 0x0d, 0x32, 0x35, 0x30,
    0x31, 0x32, 0x34, 0x31, 0x36, 0x35, 0x35, 0x30,
    0x31, 0x5a, 0x30, 0x11, 0x31, 0x0f, 0x30, 0x0d,
    0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x06, 0x75,
    0x62, 0x75, 0x6e, 0x74, 0x75, 0x30, 0x82, 0x01,
    0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
    0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
    0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01,
    0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xf1, 0x85,
    0x02, 0x0b, 0x26, 0x3d, 0x2d, 0xcd, 0xcc, 0x75,
    0xc2, 0x1d, 0x51, 0x19, 0x7c, 0x1d, 0xbc, 0x1e,
    0x14, 0xe5, 0x71, 0x6f, 0xfb, 0xaa, 0x2a, 0x8b,
    0x2f, 0x69, 0xe3, 0xe7, 0xc7, 0xa4, 0xc4, 0x79,
    0xef, 0x17, 0x0e, 0x4b, 0x37, 0xd8, 0x68, 0x00,
    0x81, 0x6a, 0x62, 0x55, 0x79, 0x1d, 0x1d, 0x73,
    0x6d, 0xbb, 0x20, 0x43, 0x4e, 0x21, 0x8b, 0xe8,
    0x67, 0xb6, 0x0e, 0xfd, 0xde, 0x15, 0x18, 0xff,
    0x9f, 0x0d, 0x2b, 0xf6, 0x61, 0xd5, 0x51, 0x1c,
    0xfe, 0x5e, 0xca, 0x7f, 0xdc, 0x16, 0xc8, 0x4f,
    0x36, 0x3d, 0x17, 0x4b, 0x85, 0x53, 0x2e, 0x85,
    0x04, 0x7c, 0xa3, 0x02, 0xdd, 0xf2, 0x9b, 0x86,
    0xcd, 0x98, 0x64, 0xd4, 0xcd, 0x10, 0x25, 0x79,
    0xd6, 0x81, 0x36, 0x40, 0x72, 0xd3, 0xfd, 0x06,
    0xff, 0x55, 0x79, 0x0b, 0xc3, 0x07, 0x7a, 0x04,
    0xd3, 0x9a, 0x80, 0x92, 0xd2, 0x33, 0x82, 0x63,
    0x87, 0x1f, 0xae, 0x9f, 0xb3, 0x9e, 0x01, 0x41,
    0x07, 0x42, 0x02, 0x25, 0x9d, 0x29, 0xb8, 0x67,
    0xa0, 0x02, 0xf5, 0x57, 0x63, 0xd6, 0xc1, 0x41,
    0x5c, 0x5d, 0x74, 0x5d, 0x75, 0x17, 0x3a, 0xce,
    0xd8, 0xb2, 0x9f, 0x4a, 0x66, 0x6b, 0x4c, 0x4e,
    0x28, 0xf1, 0x2f, 0x74, 0x86, 0xdf, 0xe3, 0x9a,
    0x3d, 0x8e, 0xbd, 0x7e, 0x03, 0xf9, 0x8d, 0x42,
    0xa1, 0xe9, 0xa3, 0x58, 0xca, 0x26, 0xdb, 0xae,
    0xbb, 0x47, 0x4a, 0x81, 0x98, 0x80, 0xee, 0x54,
    0x04, 0x93, 0x22, 0xa8, 0x6a, 0xb1, 0xb8, 0x13,
    0x14, 0xa4, 0x90, 0x99, 0x51, 0x3d, 0x30, 0xd3,
    0x9a, 0xd1, 0x46, 0xc4, 0x79, 0x74, 0xe5, 0x6d,
    0x4f, 0xde, 0xb8, 0xa6, 0x90, 0xb1, 0x75, 0x67,
    0x16, 0xf0, 0x0e, 0x4b, 0x15, 0xfa, 0x9f, 0xbf,
    0x65, 0x48, 0x2d, 0x27, 0x00, 0x3a, 0x86, 0x73,
    0x49, 0xa5, 0xd9, 0x01, 0x56, 0x5f, 0x02, 0x03,
    0x01, 0x00, 0x01, 0xa3, 0x0d, 0x30, 0x0b, 0x30,
    0x09, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02,
    0x30, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
    0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
    0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x1d, 0x14,
    0x29, 0xa8, 0x5f, 0x81, 0x2b, 0xae, 0x61, 0x36,
    0x7b, 0x44, 0xbb, 0xc1, 0xa2, 0x11, 0x23, 0x21,
    0x43, 0x98, 0xee, 0x42, 0xfa, 0x90, 0x01, 0x5a,
    0xde, 0xd6, 0x80, 0x1f, 0xa4, 0x49, 0x4b, 0xaf,
    0xf8, 0x9d, 0xb8, 0xa3, 0xc8, 0xf2, 0x0f, 0xdb,
    0x33, 0x6d, 0xd7, 0x29, 0x6f, 0xda, 0xe3, 0x06,
    0x51, 0xab, 0xbf, 0xd2, 0xb1, 0xee, 0x89, 0x24,
    0x2c, 0x00, 0x5f, 0x71, 0x0a, 0xc9, 0xba, 0xde,
    0x61, 0xa0, 0x82, 0x7e, 0x40, 0xb8, 0xbf, 0xd3,
    0x75, 0x9a, 0xab, 0x96, 0x77, 0xb1, 0x8e, 0x1c,
    0xad, 0xf7, 0x36, 0xbb, 0xfc, 0xc3, 0x30, 0x58,
    0x38, 0x19, 0x19, 0x3a, 0xdd, 0xdb, 0x03, 0xed,
    0x5a, 0x31, 0xb9, 0x74, 0x06, 0x63, 0x95, 0x41,
    0x48, 0x66, 0x41, 0x63, 0xf5, 0x0b, 0x5d, 0x82,
    0xe4, 0xaf, 0xfc, 0x47, 0x17, 0xed, 0x4e, 0xe3,
    0x8d, 0x62, 0xf9, 0x6a, 0xec, 0xcb, 0xbb, 0x2c,
    0xe7, 0x8c, 0x30, 0x49, 0xfa, 0x5f, 0xda, 0xdc,
    0x18, 0x14, 0x9a, 0x9a, 0x7a, 0x0f, 0xbf, 0x47,
    0x22, 0x46, 0x00, 0x7b, 0xf9, 0xed, 0xd8, 0x52,
    0x48, 0x32, 0x84, 0x93, 0x41, 0x50, 0x2f, 0x31,
    0xf6, 0x1d, 0xfc, 0x69, 0x0d, 0x30, 0xd0, 0xda,
    0x44, 0x65, 0x85, 0xde, 0x0c, 0x2c, 0x92, 0x9c,
    0x42, 0xd7, 0x40, 0xe2, 0xbc, 0x7d, 0xd3, 0x93,
    0xad, 0x63, 0x42, 0xe3, 0x95, 0xc4, 0xaf, 0xa2,
    0x3d, 0xd6, 0x02, 0x1c, 0xf9, 0x28, 0xd2, 0xe8,
    0x18, 0xa0, 0x6f, 0xb0, 0x42, 0x4c, 0x9a, 0x75,
    0xca, 0x49, 0xb6, 0xa2, 0x66, 0x3d, 0xa2, 0x21,
    0x76, 0xfa, 0xbd, 0xe6, 0x66, 0x73, 0x04, 0x35,
    0xde, 0x6b, 0x1a, 0xb9, 0x3f, 0xa0, 0x06, 0xd5,
    0x39, 0x0e, 0xc3, 0xfa, 0xe2, 0x6f, 0x7f, 0xb3,
    0x4d, 0xee, 0x54, 0x38, 0x78, 0x3b, 0x1b, 0x2e,
    0x71, 0xc2, 0xc5, 0x2d, 0xca, 0xf1
  };
  
  const unsigned char* data = reinterpret_cast&lt;const unsigned char*&gt;(raw);
  
  size_t len = sizeof(raw)/sizeof(raw[0]);
  
  X509 *cert = d2i_X509(NULL, &amp;data, len);
  </code></pre>
  
  <p>This will always return <code>cert</code> as NULL and i'm stuck because this seemed like a simple procedure.</p>