简体   繁体   中英

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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM