对于 openssl,Net::SSLeay 无法获得任何结果

Cannot get any results with Net::SSLeay for openssl

我正在尝试创建一个脚本来查看我的 ~/ssl/certs 文件夹中的证书数据并向我显示颁发者信息。

它是用 perl 编写的,简单地说:

$data = `/usr/bin/openssl x509 -in $file -noout -issuer`

然而,这不是很便携。我正在尝试使用 Net::SSLeay 来获得相同的输出,但是我似乎能管理的只是校验和数字,我错过了什么?这是我得到的

#!/usr/bin/perl
use 5.10.1;
use strict;
use warnings;
use Net::SSLeay qw(die_now die_if_ssl_error);
Net::SSLeay::load_error_strings();
Net::SSLeay::SSLeay_add_ssl_algorithms();    # Important!
Net::SSLeay::ENGINE_load_builtin_engines();  # If you want built-in engines
Net::SSLeay::ENGINE_register_all_complete(); # If you want built-in engines
Net::SSLeay::randomize();
Net::SSLeay::library_init();
Net::SSLeay::OpenSSL_add_all_algorithms();
my $file = '~/ssl/certs/certificate.crt';

my $x509 = Net::SSLeay::X509_new(); 
Net::SSLeay::X509_free($x509);
my $type = Net::SSLeay::X509_certificate_type($x509);
my $ctx = Net::SSLeay::CTX_new_with_method(Net::SSLeay::TLSv1_method());
my $test = Net::SSLeay::X509_load_cert_file( $ctx, $file, $type );
my $info = Net::SSLeay::X509_issuer_name_hash($x509);

say "\nInfo = $info \nX509 = $x509\nTest= $test\nType = $type\nCTX = $ctx";



This is my output:
Info = 4003674586 
X509 = 16119648
Test= 0
Type = 0
CTX = 16137888

我已经通读了所有的源代码和文档,none 有任何意义。

这个例子可能会让你入门:

https://metacpan.org/source/MIKEM/Net-SSLeay-1.68/examples/x509_cert_details.pl

我已经从中删除了相关位以获取发行人:

#!/usr/bin/perl

use strict;
use warnings;

use Net::SSLeay qw/XN_FLAG_RFC2253 ASN1_STRFLGS_ESC_MSB/;

Net::SSLeay::randomize();
Net::SSLeay::load_error_strings();
Net::SSLeay::ERR_load_crypto_strings();
Net::SSLeay::SSLeay_add_ssl_algorithms();

my $file = shift;
chomp($file);

my $bio = Net::SSLeay::BIO_new_file($file, 'rb') or die "ERROR: BIO_new_file failed";
my $x509 = Net::SSLeay::PEM_read_bio_X509($bio) or die "ERROR: PEM_read_bio_X509 failed";

my $issuer_name = Net::SSLeay::X509_get_issuer_name($x509);
print Net::SSLeay::X509_NAME_print_ex($issuer_name) . "\n";

那么:

$ perl ssl.pl /usr/share/ca-certificates/mozilla/XRamp_Global_CA_Root.crt
CN=XRamp Global Certification Authority,O=XRamp Security Services Inc,OU=www.xrampsecurity.com,C=US

您不需要所有这些上下文等。初始化 SSL 库后,您可以简单地执行以下操作:

my $bio = Net::SSLeay::BIO_new_file($file,'r') or die $!;
my $cert = Net::SSLeay::PEM_read_bio_X509($bio);
Net::SSLeay::BIO_free($bio);
$cert or die "cannot parse $file as PEM X509 cert: ".
    Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error());
my $issuer = Net::SSLeay::X509_NAME_oneline(
    Net::SSLeay::X509_get_issuer_name($cert));