-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add trusted certificate check #11
Open
timlegge
wants to merge
1
commit into
master
Choose a base branch
from
new-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use strict; | ||
use warnings; | ||
use Crypt::OpenSSL::Verify; | ||
use Crypt::OpenSSL::X509; | ||
use IO::Socket::SSL; | ||
use Net::SSLeay; | ||
use Data::Dumper; | ||
use File::Slurp qw{ write_file }; | ||
use Test::More; | ||
|
||
my $openssl_version = `openssl version`; | ||
$openssl_version =~ /OpenSSL ([\d\.]+)/; | ||
$openssl_version = $1; | ||
|
||
my %chain = (); | ||
my $inter_cnt = 1; | ||
|
||
my $server_name = 'www.google.com'; | ||
|
||
sub verify_callback { | ||
my $cert = $_[4]; | ||
my $subject = Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_subject_name($cert)); | ||
my $issuer = Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_issuer_name($cert)); | ||
|
||
$subject =~ /CN=(.*$)/; | ||
$subject = $1; | ||
$issuer =~ /CN=(.*$)/; | ||
$issuer = $1; | ||
if ( $subject eq $server_name ) { | ||
$chain{'server'} = { name => $subject, x509 => Net::SSLeay::PEM_get_string_X509($cert), }; | ||
} elsif ( $subject ne $issuer ) { | ||
my $int = 'intermediate' . $inter_cnt; | ||
$chain{'intermediates'} = $inter_cnt; | ||
$chain{$int} = { 'name' => $subject, 'x509' => Net::SSLeay::PEM_get_string_X509($cert), }; | ||
$inter_cnt++; | ||
} elsif ( $subject eq $issuer ) { | ||
$chain{'root'} = { 'name' => $subject, 'x509' => Net::SSLeay::PEM_get_string_X509($cert), }; | ||
} | ||
return 1; | ||
} | ||
|
||
sub get_cert_chain { | ||
my $peer = shift; | ||
IO::Socket::SSL->new( | ||
PeerHost => $peer . ":443", | ||
SSL_verify_callback => \&verify_callback | ||
) or die $SSL_ERROR||$!; | ||
} | ||
|
||
get_cert_chain($server_name); | ||
|
||
my $cert = $chain{'server'}{'x509'}; | ||
|
||
my $intermediate = ''; | ||
for ( my $i = 1; $i <= $chain{intermediates}; $i++ ) { | ||
$intermediate = $intermediate . $chain{"intermediate$i"}{'x509'} ."\n"; | ||
} | ||
|
||
write_file('intermediate.pem', $intermediate); | ||
write_file('cert.pem', $cert); | ||
|
||
my $ret; | ||
|
||
SKIP: { | ||
skip "openSSL not installed", 1 unless `which openssl`; | ||
|
||
#say 'OpenSSL verification:'; | ||
eval { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps use Test::Exception: |
||
$ret = `openssl verify -CAfile intermediate.pem cert.pem`; | ||
}; | ||
like($ret, qr/OK/, "OpenSSL verification - OK"); | ||
} | ||
|
||
#say 'Crypt::OpenSSL::Verify verification:'; | ||
my $verifier = Crypt::OpenSSL::Verify->new('intermediate.pem',{strict_certs=>0}); | ||
my $cert_object = Crypt::OpenSSL::X509->new_from_string($cert); | ||
my $verify = $verifier->verify($cert_object); | ||
ok($verify, "Crypt::OpenSSL::Verify verification - OK"); | ||
|
||
$verifier = Crypt::OpenSSL::Verify->new('intermediate.pem',{strict_certs=>1}); | ||
$cert_object = Crypt::OpenSSL::X509->new_from_string($cert); | ||
$verify = $verifier->verify($cert_object); | ||
ok($verify, "Crypt::OpenSSL::Verify strict verification - OK"); | ||
|
||
SKIP: { | ||
skip "Incorrect/missing version of openSSL", 2 unless (($openssl_version ge '1.1.1') and (`which openssl`)); | ||
#say 'OpenSSL verification - noCApath:'; | ||
eval { | ||
$ret = `openssl verify -no-CApath -CAfile intermediate.pem cert.pem 2>&1`; | ||
}; | ||
like ($ret, qr/error 2 at 1 depth lookup: .* issuer certificate/s, "OpenSSL verification no-CApath - OK"); | ||
|
||
$verifier = Crypt::OpenSSL::Verify->new('intermediate.pem', {noCApath =>1, strict_certs=>1}); | ||
$cert_object = Crypt::OpenSSL::X509->new_from_string($cert); | ||
eval { | ||
$ret = $verifier->verify($cert_object); | ||
}; | ||
like($ret, qr/error 2 at 1 depth lookup: .* issuer certificate/s, "Crypt::OpenSSL::Verify - noCApath failed to find root - OK"); | ||
} | ||
|
||
SKIP: { | ||
skip "Incorrect/missing version of openSSL", 2 unless (($openssl_version ge '1.1.1') and (`which openssl`));; | ||
#say 'OpenSSL verification intermediate:'; | ||
eval { | ||
$ret = `openssl verify intermediate.pem`; | ||
}; | ||
like ($ret, qr/intermediate.pem: OK/s, "OpenSSL verification intermediate - OK"); | ||
} | ||
|
||
$verifier = Crypt::OpenSSL::Verify->new('', { strict_certs=>1}); | ||
$cert_object = Crypt::OpenSSL::X509->new_from_string($intermediate); | ||
eval { | ||
$ret = $verifier->verify($cert_object); | ||
}; | ||
ok($ret, "Crypt::OpenSSL::Verify intermediate - OK"); | ||
|
||
SKIP: { | ||
skip "Incorrect/missing version of openSSL", 2 unless (($openssl_version ge '1.1.1') and (`which openssl`));; | ||
#say 'OpenSSL verification intermediate - noCAfile & noCApath:'; | ||
eval { | ||
$ret = `openssl verify -no-CApath -no-CAfile intermediate.pem 2>&1`; | ||
}; | ||
like ($ret, qr/error 20 at 0 depth lookup: unable to get local issuer certificate/s, "OpenSSL verification intermediate no-CAfile & no-CApath - OK"); | ||
|
||
$verifier = Crypt::OpenSSL::Verify->new('', {noCAfile =>1, noCApath =>1, strict_certs=>1}); | ||
$cert_object = Crypt::OpenSSL::X509->new_from_string($intermediate); | ||
eval { | ||
$ret = $verifier->verify($cert_object); | ||
}; | ||
like($ret, qr/error 20 at 0 depth lookup: unable to get local issuer certificate/s, "Crypt::OpenSSL::Verify intermediate - noCAfile & noCApath failed to find root - OK"); | ||
} | ||
|
||
done_testing; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would do this in a skip_all like situation.
in the top of the testfile