以编程方式进行SSL证书验证
|
我知道这将是一个巨大的职位,但我想通过提供所有细节来提出我所面临的问题。
背景
我有一个触发firefox来获取URL数据并显示网页(如Firebug)中所有组件的单个组件加载时间的应用程序。但是,该应用程序不会自动验证ssl证书(即,如果存在错误的证书,它将卡住,因为没有用户手动接受/拒绝证书,并且全部以编程方式完成)。我需要通过在启动Firefox程序之前尝试验证站点的证书来解决此问题。
我的解决方案
我发现了这段C代码,可以在C中以编程方式进行SSL证书的验证。我对此进行了简要概述。这是main()方法:
SSL_library_init();
ERR_load_BIO_strings();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
/* Set up the SSL context */
ctx = SSL_CTX_new(SSLv23_client_method());
/* Load the trust store - in this case, it\'s just a single
* certificate that has been created for testing purposes.
*/
if(! SSL_CTX_load_verify_locations(ctx,\"certificate.pem\",NULL))
{
fprintf(stderr, \"Error loading trust store\\n\");
//ERR_print_errors_fp(stderr);
SSL_CTX_free(ctx);
return 0;
}
/* Setup the connection */
bio = BIO_new_ssl_connect(ctx);
/* Set the SSL_MODE_AUTO_RETRY flag */
BIO_get_ssl(bio, & ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
/* Create and setup the connection */
BIO_set_conn_hostname(bio, \"mail.google.com:https\");
fprintf(stderr, \"Connecting to host ...\\n\");
if(BIO_do_connect(bio) <= 0)
{
fprintf(stderr, \"Error attempting to connect: %d\\n\",BIO_do_connect(bio));
//ERR_print_errors_fp(stderr);
BIO_free_all(bio);
SSL_CTX_free(ctx);
return 0;
}
/* Retrieve the peer certificate */
fprintf(stderr, \"Retrieving peer certificate\\n\");
if(getPeerCert(ssl, & peerCert) != X509_V_OK)
{
/* Can be changed to better handle a suspect certificate. However,
* for the purposes of this demonstration, we\'re aborting.
*/
fprintf(stderr, \"Certificate verification error: %i\\n\",SSL_get_verify_result(ssl));
BIO_free_all(bio);
SSL_CTX_free(ctx);
return 0;
}
我忽略了getPeerCert()方法的定义,因为它获得了对等证书并使用openssl的方法进行了验证。
certificate.pem也是一个pem文件,通过执行此问题的解决步骤而获得。
但是当我尝试运行此命令时
Connecting to host ...
Retrieving peer certificate
Certificate verification error: 20
我无法看到为什么这种情况会发生,因为验证应该成功。我将很感激并很高兴能得到我的任何帮助。
更新1
我尝试使用开放的SSL命令,并尝试从代码中调用该命令,即
opensssl verify -CAfile ./ca-bundle.crt cert1...
但是,我发现它可以验证内部和外部证书,也可以验证实际上应该是错误的(内部)证书(特别是错误的域证书)。我将不胜感激对此有任何见解。
没有找到相关结果
已邀请:
2 个回复
倾坞髓
看到这篇文章,但是我还不知道如何编程。 http://www.herongyang.com/crypto/openssl_verify_2.html \\
蜗仓馈
尝试将gmail发行者而不是gmail证书放入certificate.pem。 另外,请确保您了解Bruno对您的问题的第一条评论。