OpenSSL 忽略自签名证书错误

发布于 2024-08-21 08:30:25 字数 272 浏览 5 评论 0原文

我正在使用 OpenSSL 库编写一个小程序,该程序应该与 SSLv3 服务器建立连接。此服务器分发自签名证书,这会导致握手失败并显示以下消息:“sslv3 警报握手失败,证书链中的自签名证书。”

有什么办法可以强制连接继续进行吗?我尝试像这样调用 SSL_CTX_set_verify :

SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);

但它似乎没有改变任何东西。

有什么建议吗?

I'm writing a small program with the OpenSSL library that is suppose to establish a connection with an SSLv3 server. This server dispenses a self-signed certificate, which causes the handshake to fail with this message: "sslv3 alert handshake failure, self signed certificate in certificate chain."

Is there a way I can force the connection to proceed? I've tried calling SSL_CTX_set_verify like so:

SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);

But it does not seem to change anything.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

佞臣 2024-08-28 08:30:25

默认情况下,OpenSSL 遍历证书链并尝试验证每个步骤,SSL_set_verify() 不会改变这一点,请参阅手册页。引用它:

实际的验证过程是使用
内置验证程序或使用提供的其他应用程序
使用 SSL_CTX_set_cert_verify_callback(3) 设置验证函数。

因此,解决方案是创建一个简单的回调并设置该回调,以便覆盖所有证书链遍历:

static int always_true_callback(X509_STORE_CTX *ctx, void *arg)
{
    return 1;
}

SSL_CTX_set_cert_verify_callback(CTX, always_true_callback);

By default OpenSSL walks the certificate chain and tries to verify on each step, SSL_set_verify() does not change that, see tha man page. Quoting it:

The actual verification procedure is performed either using the
built-in verification procedure or using another application provided
verification function set with SSL_CTX_set_cert_verify_callback(3).

So the solution is to create a simple callback and set that one, so that you override all certificate-chain walking:

static int always_true_callback(X509_STORE_CTX *ctx, void *arg)
{
    return 1;
}

SSL_CTX_set_cert_verify_callback(CTX, always_true_callback);
久随 2024-08-28 08:30:25

您是否尝试过向您的应用提供服务器的 CA 证书,以便您的应用可以验证证书链?

Have you tried giving your app the server's CA certificate so that your app can verify the certificate chain?

零崎曲识 2024-08-28 08:30:25

检查这些 OpenSSL 示例:http://www.rtfm.com/openssl-examples/

wclient.c 连接到任何 https 页面,例如:

wclient -h www.yahoo.com -p 443

如果您使用默认安装运行该页面,您将收到证书错误(您可以使用 -i 标志来绕过证书检查) )。

要验证证书,您需要下载 CA 证书(Verisign、Thawte、Equifax 等),因此请在 google 上搜索此文件 cacert.pem,下载并将其重命名为 root.pem ,您将能够连接到网络服务器并验证其证书。

Check these OpenSSL Examples: http://www.rtfm.com/openssl-examples/

The wclient.c connects to any https page, for example:

wclient -h www.yahoo.com -p 443

If you run that with the default installation, you'll get a certificate error (you can use the -i flag to bypass the certificate check though).

To verify the certificate, you'll need to download the CA certificates (Verisign, Thawte, Equifax, etc), so google this file cacert.pem, download and rename it to root.pem and you'll be able to connect to a web server and validate its certificate.

柠檬色的秋千 2024-08-28 08:30:25

您是否尝试过设置 SSL_set_verify ?

SSL_set_verify(s, SSL_VERIFY_NONE, NULL);

Have you tried setting SSL_set_verify?

SSL_set_verify(s, SSL_VERIFY_NONE, NULL);
箹锭⒈辈孓 2024-08-28 08:30:25

您可以尝试将自己的回调传递给 SSL_set_verify(),然后进行自己的验证。这不太理想,因为我认为您需要执行所有验证,然后允许忽略自签名错误,但您应该能够从 OpenSSL 源中找出标准验证代码的作用,然后简单地提取它进入您自己的验证回调并允许特定的错误代码...

You could try passing your own callback to SSL_set_verify() and then doing your own verification. It's less than ideal as I think you then need to do all of the verification and then allow the self signed error to be ignored, but you should be able to work out what the standard verify code does from the OpenSSL source and then simply pull it into your own verification callback and allow the specific error code...

执着的年纪 2024-08-28 08:30:25

我的示例客户端代码(链接)可以很好地使用自签名服务器证书。我在 SSL_connect 之后有以下代码,并且可以完全控制客户端中自签名证书的可接受性

SSL_CTX* ctx = SSL_CTX_new(SSLv3_method());

// TCP connection and SSL handshake ...

/* Check the certificate */

rc = SSL_get_verify_result(ssl);
if(rc != X509_V_OK) {
  if (rc == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT || rc == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
    fprintf(stderr, "self signed certificate\n");
  }
  else {
    fprintf(stderr, "Certificate verification error: %ld\n", SSL_get_verify_result(ssl));
    SSL_CTX_free(ctx);
    return 0;
  }
}

My sample client code (link) works fine with self signed server cert. I have the below code after SSL_connect and have full control over self signed certificates acceptability in my client

SSL_CTX* ctx = SSL_CTX_new(SSLv3_method());

// TCP connection and SSL handshake ...

/* Check the certificate */

rc = SSL_get_verify_result(ssl);
if(rc != X509_V_OK) {
  if (rc == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT || rc == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN) {
    fprintf(stderr, "self signed certificate\n");
  }
  else {
    fprintf(stderr, "Certificate verification error: %ld\n", SSL_get_verify_result(ssl));
    SSL_CTX_free(ctx);
    return 0;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文