【问题标题】:Convert X509_STORE_load_locations() call to load data from Memory转换 X509_STORE_load_locations() 调用以从内存中加载数据
【发布时间】:2017-11-30 20:18:41
【问题描述】:

如何转换下面的代码以从内存中加载文件“cabundle.pem”?该代码用于证书验证(Source

    const char ca_bundlestr[] = "./cabundle.pem";
    ret = X509_STORE_load_locations(store, ca_bundlestr, NULL);`

所以我目前所做的是将 cabundle.pem 放在字符串数组中,然后将证书添加到商店,但它不起作用,因为我认为我需要做的是使用 X509_STORE_CTX_trusted_stack 将它添加到商店,但我不知道如何使用 STACK_OF(X509) 我尝试搜索但我找不到任何东西。

    const char ca_bundle[] = "-----BEGIN CERTIFICATE-----\n"
    "MIIImDCCCD2gAwIBAgIRAMch/W0685E2zrArUB/79kUwCgYIKoZIzj0EAwIwgZIx\n"
    "MIIImDCCC...............................................AwIwgZIx\n"
    "CzAJBgNVB..........................................0ZXIxEDAOBgNV\n"
    "-----END CERTIFICATE-----\n";

    BIO *bio;
    X509 *cert;

    bio = BIO_new(BIO_s_mem());
    BIO_puts(bio, ca_bundle);
    cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
    X509_STORE_add_cert(store, cert);

那么如何将我的字符串数组ca_bundle 加载到 STACK_OF(X509) 变量中?或者,如果有其他方法可以做到这一点,请告诉我。谢谢

【问题讨论】:

    标签: c openssl


    【解决方案1】:

    我问得太早了。我刚刚找到了我的问题的答案。由于 cabundle.pem 包含多个证书,您需要循环执行并读取下一个证书,然后将其再次添加到存储中。您不需要X509_STORE_CTX_trusted_stack,因为将所有证书添加到存储区的方式与调用X509_STORE_load_locations 的方式相同

    while (cert = PEM_read_bio_X509(bio, NULL, NULL, NULL)) {
        X509_STORE_add_cert(store, cert);
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      相关资源
      最近更新 更多