Perl 无法使用Net::SSLeay for openssl获取任何结果

Perl 无法使用Net::SSLeay for openssl获取任何结果,perl,ssl,openssl,x509,Perl,Ssl,Openssl,X509,我正在尝试创建一个脚本,用于查看我的~/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

我正在尝试创建一个脚本,用于查看我的~/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

我已经通读了所有的源代码和文档,但没有一个是有意义的。

这个示例可以帮助您开始:

我截取了其中的相关部分,以获得发行人:

#!/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));

非常感谢!这非常有效,他们真的需要更好地解释他们的文档。@Jeffrey:
Net::SSLeay
是自由软件,根据我的经验,维护人员对改进软件的更改非常开放。因此,请随时提供使文档更好的补丁。是否可以完全解码证书或密钥的base64版本并打印数据?似乎您只能打印主题、发行人和很少的其他内容。我试图比较密钥和证书上的模,以确保它们匹配查看OpenSSL apps/x509.c的源代码。我想说,模仅通过访问内部结构而不是API函数可用。因此,您可能无法使用Net::SSLeay函数获得它。