2

更新

我的解决方案基于这个这个答案。

背景

我正在尝试读取 DER 格式的证书文件并尝试对其进行验证。

我的证书是DER格式的。我已经通过以下方式确认了这一点:

使用openssl命令行:

  • openssl x509 -text -noout -inform DER -in Cert.cer:显示证书

  • openssl x509 -text -noout -in Cert.cer: 显示unable to load certificate

  • openssl x509 -inform der -in Cert.cer -out Cert.pem: 将 DER 转换为 PEM

我正在使用以下代码阅读:

static std::vector<char> ReadAllBytes(char const* filename)
{
    std::cout << "in ReadAllBytes(" << filename << ")" << std::endl;
    std::ifstream stream(filename, std::ios::in | std::ios::binary);
    std::vector<char> contents((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());

    std::cout << "out ReadAllBytes" << std::endl;

    return contents;
}

int main(int argc, char **argv)
{
    OpenSSL_add_all_algorithms();

    auto readBytes = ReadAllBytes("Cert.cer");
    std::cout << "after ReadAllBytes, read size:" << readBytes.size() << std::endl;
    BIO *bio_mem = BIO_new(BIO_s_mem());
    BIO_puts(bio_mem, readBytes.data());
    X509 * x509 = d2i_X509_bio(bio_mem, NULL);

    // PEM format
    //X509 *x509 = PEM_read_bio_X509(bio_mem, NULL, NULL, NULL);

    if(x509 == NULL){
        unsigned int errCode = ERR_get_error();

        printf("\nError: %s\n", ERR_error_string(errCode, NULL));
        printf("\nLib: %s\n", ERR_lib_error_string(errCode));
        printf("\nFunc: %s\n", ERR_func_error_string(errCode));
        printf("\nReason: %s\n", ERR_reason_error_string(errCode));
    }

    BIO_free(bio_mem);
    X509_free(x509);
}

输出:

in ReadAllBytes(Cert.cer)
out ReadAllBytes
after ReadAllBytes, read size:1033

Error: error:0D06B08E:lib(13):func(107):reason(142)

Lib: (null)

Func: (null)

Reason: (null)

调用后更新输出ERR_load_crypto_strings();

Error: error:0D06B08E:asn1 encoding routines:ASN1_D2I_READ_BIO:not enough data

Lib: asn1 encoding routines

Func: ASN1_D2I_READ_BIO

Reason: not enough data

问题

d2i_X509_bio(bio_mem, NULL)返回NULL

转换后,我已成功读取 PEM 格式的证书:X509 *x509 = PEM_read_bio_X509(bio_mem, NULL, NULL, NULL);

问题

  • 我错过的代码中有什么问题吗?

  • 如何DER使用 openssl 读取格式化的 x509 证书文件?

4

1 回答 1

1

看起来您的问题是您将数据 blob 作为字符串传递。

BIO_puts(put string) 复制到第一个零值字节。奇怪的是,这在您的证书中间的某个地方,这就是为什么您得到“数据不足”的原因(DER 长度值最终大于 BIO 数据的长度)。(如果您的证书没有零,那么它会读得太远并且复制太多;要非常小心地调用带有指针而不是长度的函数)。

BIO_write,另一方面,写入指定数量的数据。

所以不是BIO_puts(bio_mem, readBytes.data())你想要的BIO_write(bio_mem, readBytes.data(), readBytes.size())

从技术上讲,您应该在循环中写入BIO_write,检查返回值(它接受了多少字节进行写入),但BIO_MEM总是在一次调用中严重失败或成功。

(原来BIO_MEM不是一个流(一个有位置的数据段)而是一个管道(一个有读位置和写位置的数据段),所以写入后不需要倒带。 )

于 2019-01-16T15:51:17.467 回答