【问题标题】:How can i translate SecCertificateRef cert object to openssl's x509 certificate object in C++如何在 C++ 中将 SecCertificateRef 证书对象转换为 openssl 的 x509 证书对象
【发布时间】:2020-11-30 16:00:50
【问题描述】:

我有一个 SecCertificateRef 证书。我需要在 C++ 中从中获取到期日期。我找到了这个SecCertificateRef: How to get the certificate information?,但它似乎可以快速完成。

我认为我可以在 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++ ssl openssl core-foundation secure-transport


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-09
      • 2021-03-28
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多