简体   繁体   English

Mojo::UserAgent 证书验证失败

[英]Mojo::UserAgent certificate verify failed

I want to access a website where the certificate cannot be verified (hostname not correct and I cannot change/update the certificate on the server which my application point).我想访问一个无法验证证书的网站(主机名不正确,我无法更改/更新我的应用程序指向的服务器上的证书)。 I'm using Mojo::UserAgent to get request.我正在使用Mojo::UserAgent来获取请求。 So how would go about ignoring this and continues to connect to the website?那么如何忽略这一点并继续连接到该网站呢? I've seen that there is not an option.我已经看到没有选择。

I don't want to use LWP::UserAgent .我不想使用LWP::UserAgent

I've done it using WWW::Curl and WWW::Curl::Easy but I want to clean the code using Mojo::UserAgent (as used in my entire application).我已经使用WWW::CurlWWW::Curl::Easy完成了它,但我想使用Mojo::UserAgent (在我的整个应用程序中使用)清理代码。

hostname not correct ... So how would go about ignoring this and continues to connect to the website?主机名不正确......那么如何忽略这一点并继续连接到网站?

It is a very bad idea just to abandon any kind of validation just because the hostname does not match the certificate.仅仅因为主机名与证书不匹配而放弃任何类型的验证是一个非常糟糕的主意。 Why do you use TLS at all?你为什么要使用 TLS?

A much better way is to know up front which certificate you expect and verify that you exactly get this one.更好的方法是预先知道您期望哪种证书并验证您是否确实获得了该证书。 This can easily be done with the option SSL_fingerprint .这可以通过选项SSL_fingerprint轻松完成。 Unfortunately Mojo::UserAgent does not offer a way to set connection specific arguments, so you need to set it immediately before the connection and back before you do other connections:不幸的是,Mojo::UserAgent 不提供设置连接特定参数的方法,因此您需要在连接之前立即设置它并在执行其他连接之前返回:

use IO::Socket::SSL 1.980;
IO::Socket::SSL::set_client_defaults(
    SSL_fingerprint => "sha256$55a5dfaaf..."
);
... use Mojo::UserAgent to connect ..
IO::Socket::SSL::set_client_defaults(); # set back

For more information about to use this option and how to get the fingerprint see Certificate error in Perl .有关使用此选项以及如何获取指纹的更多信息,请参阅Perl 中的证书错误

Another way in case only the hostname is bad would be to use the SSL_verifycn_name option to specify the hostname you expect inside the certificate.如果只有主机名不好,另一种方法是使用SSL_verifycn_name选项来指定您期望在证书中使用的主机名。

IO::Socket::SSL::set_client_defaults(
    SSL_verifycn_name => 'foo.example.com',
);

Another way could be done with the set_args_filter_hack function which is intended to deal with modules which set strange defaults or which don't let the user set its own values:另一种方法可以使用set_args_filter_hack函数来完成,该函数旨在处理设置奇怪默认值或不允许用户设置自己值的模块:

my $hostname = undef;
IO::Socket::SSL::set_args_filter_hack(
    sub {
        my ($is_server,$args) = @_;
        $args->{SSL_verifycn_name} = $hostname if $hostname;
    }
);
...
$hostname = 'foo.example.com';
... do something with Mojo::UserAgent ...
$hostname = undef;

This way you can adapt the settings for each SSL handshake.通过这种方式,您可以调整每次 SSL 握手的设置。

For more information see the documentation of IO::Socket::SSL , especially the part about the common usage errors.更多信息请参见IO::Socket::SSL文档,特别是关于常见使用错误的部分。 This part also documents what you should do instead of disabling any kind of validation if some part of the certificate is wrong.这部分还记录了您应该做什么,而不是在证书的某些部分错误时禁用任何类型的验证。


 'SSL connect attempt failed error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol' curl ... SSL connection using TLS_RSA_WITH_RC4_128_MD5

My guess is what you are facing here is unrelated to the certificate validation.我的猜测是您在这里面临的问题与证书验证无关。 Given that this server is using a very old cipher RC4-MD5 I will assume that the server can only handle SSL 3.0.鉴于该服务器使用的是非常旧的密码 RC4-MD5,我将假设该服务器只能处理 SSL 3.0。 This version is disabled since a while for security reasons in IO::Socket::SSL.由于 IO::Socket::SSL 中的安全原因,此版本已被禁用一段时间。 To explicitly use this insecure version temporarily:要暂时明确使用此不安全版本:

IO::Socket::SSL::set_client_defaults(
    SSL_version => 'SSLv3'
);

Mojo::UserAgent uses IO::Socket::SSL for SSL/TLS support, so you can disable server certificate verification using Mojo::UserAgent 使用 IO::Socket::SSL 来支持 SSL/TLS,因此您可以使用以下命令禁用服务器证书验证

IO::Socket::SSL::set_defaults(
    SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
);

This is an old question, but Mojolicious is alive and kicking.这是一个古老的问题,但 Mojolicious 还活着并且在踢。 As such I've battled with this recently.因此,我最近一直在与这个作斗争。 Directly from the documents:直接从文档:

my $bool = $ua->insecure;
$ua      = $ua->insecure($bool);

Do not require a valid TLS certificate to access HTTPS/WSS sites, defaults to the value of the MOJO_INSECURE environment variable.不需要有效的 TLS 证书来访问 HTTPS/WSS 站点,默认为 MOJO_INSECURE 环境变量的值。

# Disable TLS certificate verification for testing
say $ua->insecure(1)->get('https://127.0.0.1:3000')->result->code;

In my application $bool is set from a configuration file, so I can switch it back on, where we need it.在我的应用程序中, $bool是从配置文件设置的,因此我可以在需要的地方重新打开它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM