如何验证中间CA.

时间:2014-12-08 10:05:47

标签: c++ c ssl openssl x509

我有与服务器的SSL连接。我有根CA,它也是服务器证书。我没有客户端证书,当连接启动时,客户端验证服务器证书以及它是否有效连接。现在我已经从我的根CA创建了中间CA,并将其替换为服务器证书,现在我希望客户端验证服务器证书是否受到根CA的信任,我使用openSSL API在C ++中进行验证。我该怎么做?这是我尝试过但需要更多的东西。

  const Char *caCert = "/home/omar/CA/host/host_rsa_key.crt";

      X509_LOOKUP *lookup = X509_STORE_add_lookup(ctx_ ->  cert_store,X509_LOOKUP_file());


      result=X509_LOOKUP_load_file(lookup,caCert,X509_FILETYPE_PEM);
      if(result != 1)
      {
        log()<<ERROR! Cannot load file to lookup structure\n";
        return;
      }


      SSL_CTX_sess_set_new_cb(ctx_, handshakeCallback);

      SSL_CTX_set_verify(ctx_, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
                         verifyCallback);
      SSL_CTX_set_verify_depth(ctx_, 8);

这是我将所有证书加载到ssl_ctx的地方。

我有verifyCallback函数,它在这里调用verifyCertificate()方法

  Int verifyCertificate(Int preverify, X509_STORE_CTX *store)
  {
  X509 *cert = NULL;
 // X509_NAME *name = NULL;

  SSL *ssl = NULL;

 Int certDepth = -1;
 Int certErr   = 0;

 Char *certHost = NULL;
 Char *certHash = NULL;
 Char *certData = NULL;


 cert = X509_STORE_CTX_get_current_cert(store);

 if (cert == NULL)

 log() << " ERROR! Failed to get certificate.\n";


goto VerifyCertificateError;
}



 certDepth = X509_STORE_CTX_get_error_depth(store);
 certErr = X509_STORE_CTX_get_error(store);



ssl = (SSL *) X509_STORE_CTX_get_ex_data(store, SSL_get_ex_data_X509_STORE_CTX_idx());

if (ssl == NULL)
{
 log() <<  ERROR! Failed to get SSL context from store context.\n";

 goto VerifyCertificateError;
}

if (preverify == 0)
{
//
// Self signed certificate allowed, SSH host auth style.
//

if (certErr == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
{

  log() << " Certificate preverify failed on self";

  return 1;
}
else if (certErr == X509_V_ERR_CERT_NOT_YET_VALID)
{

  log() << Allowing not yet valid certificate.\n";

  return 1;
 }
 else if (certErr == X509_V_ERR_CERT_HAS_EXPIRED)
 {

  log() << " Allowing expired certificate.\n";


  return 1;
}
else if(certErr == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT)
{

  log()<<" Unable to get issuer cert.\n"


  return 1;
}


if (certDepth == 0)
{
 if (parseCertificate(store, certHost, certHash, certData) == 0)
 {
   log() << "Encryptable: ERROR! Failed to parse certificate.\n";
  goto VerifyCertificateError;
}


  goto VerifyCertificateError;
}


if (authCallback_(certHost, certHash, certData, certCallback_,
                      callbackParameter_) == 0)
 {

  log() << " ERROR! Failed to authorize "
        << "the server certificate.\n";

  goto VerifyCertificateError;


if (certData != NULL)
{
  delete [] certData;
}

return 0;
}

1 个答案:

答案 0 :(得分:1)

OpenSSL检查中间证书,您无需做任何特殊工作。 当然

  • 你仍然必须指定你的信任锚(你做的)
  • 对等方必须在握手中提供中间证书

您不需要verify_callback,因为默认回调已经进行了验证,即检查整个信任链,包括中间证书。如果你仍然使用一个,preverify包含OpenSSL已经完成的检查结果,你的验证回调应该只返回这个值,除非你有特殊情况需要验证行为不同。