【问题标题】:x509 certificate verification in CC中的x509证书验证
【发布时间】:2011-02-14 22:33:48
【问题描述】:

我确实有 DER 和 PEM 格式的证书,我的目标是检索颁发者和主题字段并使用 CA 公钥验证证书,同时使用根公钥验证 CA 证书。 我能够检索颁发者和主题的所有详细信息,但无法验证证书。
使用的 API:

x509 = d2i_X509_fp (fp, &x509); //READING DER Format
x509 = PEM_read_X509 (fp, &x509, NULL, NULL); //READING PEM Format
//to retrieve the Subject:
X509_NAME_oneline(X509_get_subject_name(x509), subject, sizeof (subject));
//to retrieve the Issuer:
X509_NAME_oneline(X509_get_issuer_name(x509), issuer, sizeof (issuer));

//To store the CA public key (in unsigned char *key) that will be used to verify the 
//certificate (in my case always sha1WithRSAEncryption):
RSA *x = X509_get_pubkey(x509)->pkey.rsa;
bn = x->n;
//extracts the bytes from public key & convert into unsigned char buffer
buf_len = (size_t) BN_num_bytes (bn);
stored_CA_pubKey = (unsigned char *)malloc (buf_len);
i_n = BN_bn2bin (bn, (unsigned char *)stored_CA_pubKey);
if (i_n != buf_len)
  LOG(ERROR," : key error\n");
if (key[0] & 0x80)
  LOG(DEBUG, "00\n");

stored_CA_pubKeyLen = EVP_PKEY_size(X509_get_pubkey(x509));

为了验证,我采用了不同的方法,但我无法验证:

一)

i_x509_verify = X509_verify(cert_x509, ca_pubkey);

b)

/* verify the signature */
int iRet1, iRet2, iReason;
iRet1 = EVP_VerifyInit(&md_ctx, EVP_sha1());
iRet2 = EVP_VerifyUpdate(&md_ctx, cert_code, cert_code_len);
rv = EVP_VerifyFinal(&md_ctx, (const unsigned char *)stored_CA_pubKey,
     stored_CA_pubKeyLen, cert_pubkey);

注意:cert_code 和 stored_CA_pubKey 是无符号字符缓冲区。

【问题讨论】:

    标签: c openssl


    【解决方案1】:

    我使用以下代码来验证证书

    初始化 CertStore:

    X509_STORE* m_store = X509_STORE_new();
    X509_LOOKUP* m_lookup = X509_STORE_add_lookup(m_store,X509_LOOKUP_file());    
    X509_STORE_load_locations(m_store, "CAFile.pem", NULL);
    X509_STORE_set_default_paths(m_store);
    X509_LOOKUP_load_file(m_lookup,"CAFile.pem",X509_FILETYPE_PEM)
    // alternative lookup by hashdir
    // X509_LOOKUP* m_lookup=X509_STORE_add_lookup(m_store,X509_LOOKUP_hash_dir());
    

    验证证书:

    X509_STORE_CTX *storeCtx = X509_STORE_CTX_new();
    X509_STORE_CTX_init(storeCtx,m_store,cert,NULL);
    X509_STORE_CTX_set_flags(storeCtx, X509_V_FLAG_CB_ISSUER_CHECK);
    if (X509_verify_cert(storeCtx) == 1)
    {
      printf("success");
    }
    else
    {
      printf("Verificatione rror: %s",X509_verify_cert_error_string(storeCtx->error));
    }
    X509_STORE_CTX_free(storeCtx);
    

    你还需要清理 m_store

    if(m_store != NULL)
    {
       X509_STORE_free(m_store);
       m_store = NULL;
    }
    

    【讨论】:

    • 在 init cert store 2 行 llookup 被使用但在第 5 行声明......这段代码是如何工作的......?请给出完整代码
    • thx Balamurugan,我只复制了相关的代码片段,完整的代码会很复杂。 :) 查找的第二个初始化是 ca 文件存储在哈希目录中的替代方法。
    • 你应该使用X509_STORE_CTX_get_error(storeCtx)而不是storeCtx->error;由于 ABI 的变化,后者更加脆弱。
    【解决方案2】:

    【讨论】:

      【解决方案3】:
      X509_STORE* m_store = NULL;
      
      X509_LOOKUP *m_lookup = NULL;
      X509_STORE_CTX *storeCtx = NULL;
      m_store = X509_STORE_new();
      if(NULL == m_store) goto exit;
      m_lookup = X509_STORE_add_lookup(m_store, X509_LOOKUP_file());
      if(NULL == m_lookup) goto exit;
      X509_STORE_load_locations(m_store, CA_CERT_PATH, NULL);
      X509_STORE_set_default_paths(m_store);
      X509_LOOKUP_load_file(m_lookup,CA_CERT_PATH, X509_FILETYPE_ASN1);
      m_lookup = X509_STORE_add_lookup(m_store, X509_LOOKUP_hash_dir());
      if(NULL == m_lookup) goto exit;
      storeCtx = X509_STORE_CTX_new();
      if(NULL == storeCtx) goto exit;
      X509_STORE_CTX_init(storeCtx,m_store,cer_x509,NULL);
      X509_STORE_CTX_set_flags(storeCtx, /*X509_V_FLAG_CHECK_SS_SIGNATURE*/0x4000);
      if (X509_verify_cert(storeCtx) == 1)
      {
      printf("success\n");
      }
      else
      {
      printf("Verification error: %s\n",X509_verify_cert_error_string(storeCtx->error));
      }
      exit:
          if(NULL != storeCtx) X509_STORE_CTX_free(storeCtx);
          if(m_store != NULL)
          {
              X509_STORE_free(m_store);
              m_store = NULL;
          }
      

      执行此操作后,我也无法验证自签名证书

      【讨论】:

      • 我尝试过类似的方法,但自签名证书验证不起作用。甚至使用“管理计算机证书”管理单元在我的机器中手动安装了证书。任何帮助,将不胜感激。 X509_STORE_CTX* ctx; ctx = X509_STORE_CTX_new(); X509_STORE* 存储 = X509_STORE_new(); X509_STORE_add_cert(存储,证书); X509_STORE_CTX_init(ctx, 存储, 证书, NULL); X509_VERIFY_PARAM* 参数 = NULL;参数 = X509_VERIFY_PARAM_new(); if (param) {X509_VERIFY_PARAM_set_flags(param,X509_V_FLAG_CHECK_SS_SIGNATURE);} status = X509_verify_cert(ctx);
      猜你喜欢
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 2011-04-06
      • 1970-01-01
      • 2023-03-03
      • 2011-11-11
      • 2018-06-30
      相关资源
      最近更新 更多