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

时间:2015-03-24 18:35:14

标签: 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::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

我已经阅读了所有源代码和文档,但没有任何意义。

2 个答案:

答案 0 :(得分:1)

您不需要所有这些上下文等。初始化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));

答案 1 :(得分:0)

此示例可能可以帮助您入门:

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