Mojo :: UserAgent TLS / SSL证书身份验证

时间:2013-03-20 21:14:23

标签: perl mojolicious

我是Mojolicious的新手,我正在尝试使用Mojo :: UserAgent脚本同时使用TLS证书颁发机构和TLS证书文件,但证书文件受密码保护。我在传递密码时遇到问题,因此可以打开客户端证书。

我有以下代码:


#!/usr/bin/env perl

use Modern::Perl;
use Mojo::UserAgent;

IO::Socket::SSL::set_defaults(SSL_passwd_cb => sub {return "password";});
my $ua = Mojo::UserAgent->new;
my $base_dir = '/path/to/certs/';
$ua->ca($base_dir . 'ca-cert.crt');
$ua->cert($base_dir . 'clientcert.crt');
my $tx = $ua->build_tx(POST => '/POST HTTP/1.1');
$tx->req->url->parse('https://example.com:12345');
$ua->start($tx);

if(my $res = $tx->success) {
  say $res->body;
  print Dumper($tx);
} else {
  my ($err, $code) = $tx->error;
  say $code ? "$code response: $err" : "Connection error: $err";
}

我已使用以下内容验证了证书:

openssl s_client -connect host:port -CApath /path/to/cert - CAfile ca-cert.crt -cert clientcert.crt

我收到提示:

Enter pass phrase for clientcert.crt:

我输入密码并正确验证。

那么,如何获取IO :: Socket :: SSL的密码?

1 个答案:

答案 0 :(得分:3)

我找到了解决方案(并报告了问题),所以现在您可以通过以下方式解决此问题。

在Mojo :: IOLoop :: Client中更改了以下内容:

SSL_key       => $args->{tls_key},
#SSL_key_file       => $args->{tls_key},

然后,您可以使用以下代码进行身份验证:

#!/usr/bin/env perl

use Modern::Perl;
use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;
my $base_dir = '/path/to/certs/';
$ua->ca($base_dir . 'ca-cert.crt');
$ua->cert($base_dir . 'clientcert.crt');

my $bio = Net::SSLeay::BIO_new_file($base_dir . 'clientcert.crt', 'r');
my $privkey = Net::SSLeay::PEM_read_bio_PrivateKey($bio, undef, 'password');
$ua->key($privkey);

my $tx = $ua->post('https://example.com:12345');

if(my $res = $tx->success) {
  say $res->body;
  print Dumper($tx);
} else {
  my ($err, $code) = $tx->error;
  say $code ? "$code response: $err" : "Connection error: $err";
}
相关问题