certificate - 通过 OpenSSL : Unable to get certificate CRL 根据 CRL 验证证书

标签 certificate openssl certificate-authority certificate-revocation

我在根据创建证书的同一 CA 创建的 CRL 验证证书时遇到问题。

我已经创建了自己的证书颁发机构 (CA) 和中间 CA。通过使用这个中间 CA,我创建了几个证书并撤销了其中的一些。撤销证书后,我更新了证书撤销列表 (CRL)。然后,我将 CRL 附加到链证书(根 CA 和中间 CA 证书的串联)。我想用这个文件来检查证书是否被撤销。我正在运行的 C 代码按预期返回已撤销的证书,同时它显示有效证书的意外消息:无法获取证书 CRL。此外,当我删除 CRL 检查时,它会返回预期结果。这可能是什么原因?

我正在运行的代码如下。

#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>

int main() {

  const char ca_bundlestr[] = "./ca-chain.crl.pem";//"./ca-chain.cert.pem";//"./ca-chain.crl.pem";
  const char cert_filestr[] = "./RasPi3B-10.1.1.10.crt.pem";//"./ToBeRevoked3.crt.pem";

  BIO              *certbio = NULL;
  BIO               *outbio = NULL;
  X509          *error_cert = NULL;
  X509                *cert = NULL;
  X509_NAME    *certsubject = NULL;
  X509_STORE         *store = NULL;
  X509_STORE_CTX  *vrfy_ctx = NULL;
  int ret;

  /* ---------------------------------------------------------- *
   * These function calls initialize openssl for correct work.  *
   * ---------------------------------------------------------- */
  OpenSSL_add_all_algorithms();
  ERR_load_BIO_strings();
  ERR_load_crypto_strings();

  X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();

  /* ---------------------------------------------------------- *
   * Create the Input/Output BIO's.                             *
   * ---------------------------------------------------------- */
  certbio = BIO_new(BIO_s_file());
  outbio  = BIO_new_fp(stdout, BIO_NOCLOSE);

  /* ---------------------------------------------------------- *
   * Initialize the global certificate validation store object. *
   * ---------------------------------------------------------- */
  if (!(store=X509_STORE_new()))
     BIO_printf(outbio, "Error creating X509_STORE_CTX object\n");

  /* ---------------------------------------------------------- *
   * Create the context structure for the validation operation. *
   * ---------------------------------------------------------- */
  vrfy_ctx = X509_STORE_CTX_new();

  /* ---------------------------------------------------------- *
   * Load the certificate and cacert chain from file (PEM).     *
   * ---------------------------------------------------------- */
  ret = BIO_read_filename(certbio, cert_filestr);
  if (! (cert = PEM_read_bio_X509(certbio, NULL, 0, NULL))) {
    BIO_printf(outbio, "Error loading cert into memory\n");
    exit(-1);
  }

  ret = X509_STORE_load_locations(store, ca_bundlestr, NULL);
  if (ret != 1)
    BIO_printf(outbio, "Error loading CA cert or chain file\n");

  X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
  //X509_VERIFY_PARAM_set_depth(param, 1);
  //X509_STORE_CTX_set0_param(vrfy_ctx, param);

  /* ---------------------------------------------------------- *
   * Initialize the ctx structure for a verification operation: *
   * Set the trusted cert store, the unvalidated cert, and any  *
   * potential certs that could be needed (here we set it NULL) *
   * ---------------------------------------------------------- */
  X509_STORE_CTX_init(vrfy_ctx, store, cert, NULL);

  /* ---------------------------------------------------------- *
   * Check the complete cert chain can be build and validated.  *
   * Returns 1 on success, 0 on verification failures, and -1   *
   * for trouble with the ctx object (i.e. missing certificate) *
   * ---------------------------------------------------------- */
  ret = X509_verify_cert(vrfy_ctx);
  BIO_printf(outbio, "Verification return code: %d\n", ret);

  if(ret == 0 || ret == 1)
  BIO_printf(outbio, "Verification result text: %s\n",
             X509_verify_cert_error_string(vrfy_ctx->error));

  /* ---------------------------------------------------------- *
   * The error handling below shows how to get failure details  *
   * from the offending certificate.                            *
   * ---------------------------------------------------------- */
  if(ret == 0) {
    /*  get the offending certificate causing the failure */
    error_cert  = X509_STORE_CTX_get_current_cert(vrfy_ctx);
    certsubject = X509_NAME_new();
    certsubject = X509_get_subject_name(error_cert);
    BIO_printf(outbio, "Verification failed cert:\n");
    X509_NAME_print_ex(outbio, certsubject, 0, XN_FLAG_MULTILINE);
    BIO_printf(outbio, "\n");
  }

  /* ---------------------------------------------------------- *
   * Free up all structures                                     *
   * ---------------------------------------------------------- */
  X509_STORE_CTX_free(vrfy_ctx);
  X509_STORE_free(store);
  X509_free(cert);
  BIO_free_all(certbio);
  BIO_free_all(outbio);
  exit(0);
}

最佳答案

如果您有中间 CA,则需要同时提供根 CA 的 CRL 和中间 CA(完整链)的 CRL。您可以通过简单地连接 CRL 来做到这一点。

关于certificate - 通过 OpenSSL : Unable to get certificate CRL 根据 CRL 验证证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45847531/

相关文章:

python - Scrapy 打开 SSl 错误

openssl - 在 CSR 中添加证书策略扩展

ios - 对于主题名称不匹配的自签名 CA,SecTrustEvaluate 因 kSecTrustResultRecoverableTrustFailure 而失败

tomcat - TLS_RSA_WITH_AES_128_CBC_SHA 和 SSL_RSA_WITH_AES_128_CBC_SHA

certificate - ASN1。序列 vs 集合

ssl - 基于操作系统的证书(ca-bundle.crt)是否可以使用 smtp_tls_CAfile 正常工作

ssl - 如何在 Mosquitto 上提供基于 SSL/TLS 的通信?

javascript - SSL UNABLE_TO_VERIFY_LEAF_SIGNATURE 的 Node.js 错误

ssl - CA(证书颁发机构)从 CSR 交付什么?

java - 如何信任 Java 中的证书颁发机构?