C语言 使用 SSL 对服务器(对等方)进行身份验证



我希望我的 C/C++ 客户端通过 SSL 对服务器进行身份验证。我首先从服务器
下载了证书文件 openssl s_client -showcerts -connect www.openssl.org:443 </dev/null 2>/dev/null | openssl x509 -outform PEM > mycertfile.pem

然后在我的应用程序中,我执行以下 API 调用(伪代码):


// Register the error strings for libcrypto & libssl
SSL_load_error_strings();
// Register the available ciphers and digests
SSL_library_init();
// New context saying we are a client, and using SSL 2 or 3
ctx = SSL_CTX_new(SSLv23_client_method());
// load the certificate
if(!SSL_CTX_load_verify_locations(ctx, "mycertfile.pem", 0))
  ...
// Create an SSL struct for the connection
ssl = SSL_new(ctx);
// Connect the SSL struct to our pre-existing TCP/IP socket connection
if (!SSL_set_fd(ssl, sd))
  ...
// Initiate SSL handshake
if(SSL_connect(ssl) != 1)
  ...
// form this point onwards the SSL connection is established and works
// perfectly, I would be able to send and receive encrypted data
// **Crucial point now**
// Get certificate (it works)
X509 *cert = SSL_get_peer_certificate(ssl);
if(cert) {
  // the below API returns code 19
  const long cert_res = SSL_get_verify_result(ssl);
  if(cert_res == X509_V_OK) {
    printf("Certificate verified!n");
  }
  X509_free(cert);
}

如果我不介意检查证书并且我只对加密连接感兴趣,上面的代码可以正常工作。
问题是,当我尝试验证服务器的真实性时,我确实SSL_get_peer_certificate那里获得了证书,但是即使我刚刚在 5 分钟前下载了证书,结果验证也不起作用。

我做错了什么?

所有这些都在带有 gcc 和 openssl 的 Ubuntu 12.04.03 x86-64 上实现。

谢谢均线

仅当您拥有比 OpenSSL 已提供的更完整的 CA 证书集,或者您连接到使用非标准 CA 对其证书进行签名的服务器,并且您拥有 CA 证书来验证服务器证书时,才应调用 SSL_CTX_load_verify_locations()。 否则,应改为调用SSL_CTX_set_default_verify_paths()

// load the certificate^H^H^H^H^H^H^H^H^H^H^H^H CA trust-store
if(!SSL_CTX_set_default_verify_paths(ctx))
  ...

附带说明一下,您的程序有另一个错误。您传递了错误的指针到 SSL_get_verify_result() 。与其传入SSL_CTX *,不如传入SSL *。编译器应该警告您此错误。

  const long cert_res = SSL_get_verify_result(ssl);

您刚刚下载的证书应由证书颁发机构 (CA) 签名。您需要加载 CA(或根 CA)的证书,而不是证书本身。由于您将服务器的证书直接加载到SSL_CTX_load_verify_locations中,因此验证例程SSL_get_verify_result返回失败代码。验证码很可能必须是 19 (X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN )。

也就是说,OpenSSL 附带了一组内置的 CA(和根 CA),您可以在客户端应用程序中使用这些 CA。在 Linux 发行版上,这些证书的路径通常是/etc/ssl/certs。因此,您可以尝试更改SSL_CTX_load_verify_locations,如下所示:

if (!SSL_CTX_load_verify_locations(ctx, NULL, "/etc/ssl/certs"))
   ...

当然,这假设/etc/ssl/certs 存在并具有相关的证书(其中一个证书签署了服务器证书)。如果要对已知主机进行身份验证,则很可能会在/etc/ssl/certs 中找到 CA。

最新更新