| 1 | package Net::SAML2::Role::VerifyXML; | ||||||||||||||||||||||||||||
| 2 | 33 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 33 | our $VERSION = '0.89'; # VERSION | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 33 | ▶ | use Net::SAML2::XML::Sig; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 33 | ▶ | use Crypt::OpenSSL::Verify; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 33 | ▶ | use Crypt::OpenSSL::X509; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 33 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 33 | ▶ | use List::Util qw(none); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 33 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # ABSTRACT: A role to verify the SAML response XML | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 17 | ▶ | sub verify_xml { | ||||||||||||||||||||||||||
|
Subroutine
verify_xml: called CC=11 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 19 | 17 | my $xml = shift; | |||||||||||||||||||||||||||
| 20 | 17 | my %args = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 17 | my $cacert = delete $args{cacert}; | |||||||||||||||||||||||||||
| 23 | 17 | my $anchors = delete $args{anchors}; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 17 | my $x = Net::SAML2::XML::Sig->new({ | |||||||||||||||||||||||||||
| 26 | x509 => 1, | ||||||||||||||||||||||||||||
| 27 | %args, | ||||||||||||||||||||||||||||
| 28 | }); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 17 | ▶ | croak("XML signature check failed") unless $x->verify($xml); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 15 | ▶ | return if !$anchors && !$cacert; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$anchors or $cacert
MC/DC100%2/2
$anchors or $cacert
$anchors
$cacert
Truth table100%3/3
A or B
A= $anchorsB= $cacert
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 12 | ▶ | my $cert = $x->signer_cert | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | or die "Certificate not provided in SAML Response, cannot validate\n"; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 12 | ▶ | if ($cacert) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 7 | my $ca = Crypt::OpenSSL::Verify->new($cacert, { strict_certs => 0 }); | |||||||||||||||||||||||||||
| 39 | 7 | ▶ | try { $ca->verify($cert) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 41 | 0 | croak("Could not verify CA certificate: $_"); | |||||||||||||||||||||||||||
| 42 | 7 | }; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 12 | ▶ | return if !$anchors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 5 | ▶ | if (ref $anchors ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | croak("Unable to verify anchor trust"); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 5 | my ($key) = keys %$anchors; | |||||||||||||||||||||||||||
| 52 | 5 | ▶ | if (none { $key eq $_ } qw(subject issuer issuer_hash)) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | croak("Unable to verify anchor trust, requires subject, issuer or issuer_hash"); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 5 | my $got = $cert->$key; | |||||||||||||||||||||||||||
| 57 | 5 | my $want = $anchors->{$key}; | |||||||||||||||||||||||||||
| 58 | 5 | ▶ | if (!ref $want) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 3 | $want = [ $want ]; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 5 | ▶ | if (none { $_ eq $got } @$want) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | croak("Could not verify trust anchors of certificate!"); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 4 | return; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 33 | 1; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | __END__ | ||||||||||||||||||||||||||||