Search code examples
c++sslopensslcore-foundationsecure-transport

How can i translate SecCertificateRef cert object to openssl's x509 certificate object in C++


i've a SecCertificateRef cert. I need to get Expiry date from it in C++. I've found this SecCertificateRef: How to get the certificate information? but it seems like it does it for swift.

Closest equivalent I thought I can do in c++ is :

CFDataRef data = SecCertificateCopyData(cert);
const unsigned char *certificateDataBytes = (const unsigned char *)data;
X509 *certificateX509 = d2i_X509(NULL, &certificateDataBytes, sizeof(certificateDataBytes));

but this does not work.

OR

I can do

    CFDataRef data = SecCertificateCopyData(cert);
    unsigned char* imageBuffer = (unsigned char*) malloc(CFDataGetLength(data));
    imageBuffer = static_cast<unsigned char *> (memcpy(imageBuffer, data, CFDataGetLength(data)));
    int length = sizeof(imageBuffer);
    const unsigned char* i = (const unsigned char*) imageBuffer;
    X509 *certificateX509 = d2i_X509(NULL, &i, length);

doesn't work either :(

How can i translate SecCertificateRef cert object to X509 * . once, I've X509 *certificateX509, i can use openssl's X509_get_notAfter api to get expiry date.


Solution

  • in C++ you can get byte pointer from Apple's API and pass it d2i_X509 ,

    CFDataRef data = SecCertificateCopyData(cert);
    auto dataBufferPointer = CFDataGetBytePtr(data);
    X509 *certificateX509 = d2i_X509(NULL, &dataBufferPointer, CFDataGetLength(data));