我如何在 C++ 中将 SecCertificateRef 证书对象转换为 openssl 的 x509 证书对象

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

我有一个 SecCertificateRef 证书。我需要在 C++ 中从中获取到期日期。我找到了这个 SecCertificateRef: How to get the certificate information? 但它似乎适用于 swift.

我认为我可以用 C++ 做的最接近的等价物是:

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

但这不起作用。

我可以

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

也不行:(

如何将 SecCertificateRef 证书对象翻译成 X509 *。曾经,我 X509 *certificateX509,我可以使用 openssl 的 X509_get_notAfter api 来获取到期日期。

在 C++ 中,您可以从 Apple 的 API 获取字节指针并将其传递给 d2i_X509 ,

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