BIMI/VMC/Chain.pm

filter: Statementstmt 25.0% 36 / 144 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 66.6% 10 / 15 Podpod 100% 2 / 2 totaltotal 19.2% 48 / 250 CC 47 SCAR 55.8
CC
47
Cov
16%
_build_is_valid
420.0
app_validate
380.0
vmc
72.0
CRAP 266.1
↑ Summary BIMI/Record.pm »
1 package Mail::BIMI::VMC::Chain;
2 # ABSTRACT: Class to model a VMC Chain
3 29 our $VERSION = '3.20261116'; # VERSION
4 29 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use Mail::BIMI::Prelude;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use Mail::BIMI::VMC::Cert;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use Crypt::OpenSSL::X509 1.812;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use Crypt::OpenSSL::Verify 0.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use File::Slurp qw{ read_file write_file };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use Mozilla::CA;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 ▶use Term::ANSIColor qw{ :constants };
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 29 extends 'Mail::BIMI::Base';
16 29 with(
17 'Mail::BIMI::Role::Data',
18 'Mail::BIMI::Role::HasError',
19 );
20 29 has cert_list => ( is => 'rw', isa => 'ArrayRef',
21 documentation => 'ArrayRef of individual Certificates in the chain' );
22 29 has cert_object_list => ( is => 'rw', isa => 'ArrayRef', lazy => 1, builder => '_build_cert_object_list',
23 documentation => 'ArrayRef of Crypt::OpenSSL::X509 objects for the Certificates in the chain' );
24 29 has is_valid => ( is => 'rw', lazy => 1, builder => '_build_is_valid',
25 documentation => 'Does the VMC of this chain validate back to root?' );
26  
27  
28 0 ▶sub _default_root_certs($self) {
Subroutine _default_root_certs: not called CC=2 SCAR=17.9
29 0 my @ca;
30 0 my $manifest = $self->get_data_from_file('CA.manifest');
31 0 for my $ca_file (split "\n", $manifest) {
32 0 push @ca, $self->get_data_from_file("CA/$ca_file");
33 }
34 0 return join "\n", @ca;
35 }
36  
37 0 ▶sub _build_is_valid($self) {
Subroutine _build_is_valid: not called CC=20 SCAR=60.4
38 # Start with root cert validations
39 0 ▶ return 0 if !$self->vmc;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->vmc 0 0 0
40  
41 0 my $ssl_root_cert = $self->bimi_object->options->ssl_root_cert;
42 0 my $unlink_root_cert_file = 0;
43 0 ▶ if ( !$ssl_root_cert ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssl_root_cert) 0 0 0
44 0 my $mozilla_root = scalar read_file Mozilla::CA::SSL_ca_file;
45 0 state $bimi_root = $self->_default_root_certs;
46 0 my $temp_fh = File::Temp->new(UNLINK=>0);
47 0 $ssl_root_cert = $temp_fh->filename;
48 0 $unlink_root_cert_file = 1;
49 0 print $temp_fh join("\n",$mozilla_root,$bimi_root);
50 0 close $temp_fh;
51 }
52  
53 0 my $root_ca = Crypt::OpenSSL::Verify->new($ssl_root_cert,{noCApath=>0,strict_certs=>0});
54 0 my $root_ca_ascii = scalar read_file $ssl_root_cert;
55 0 foreach my $cert ( $self->cert_object_list->@* ) {
56 0 my $i = $cert->index;
57 0 ▶ if ($cert->is_expired) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cert->is_expired) 0 0 0
58 0 $self->log_verbose("Certificate $i is expired");
59 0 next;
60 }
61 0 ▶ if ( !$cert->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cert->is_valid) 0 0 0
62 0 $self->log_verbose("Certificate $i is not valid");
63 0 next;
64 }
65 0 ▶ if ( !$cert->has_valid_usage ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cert->has_valid_usage) 0 0 0
66 0 $self->log_verbose("Certificate $i does not have valid usage flags for BIMI");
67 0 next;
68 }
69 0 my $is_valid = 0;
70 0 eval {
71 0 $root_ca->verify($cert->x509_object);
72 0 $is_valid = 1;
73 };
74 0 ▶ if ( !$is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_valid) { } 0 0 0
75 0 $self->log_verbose("Certificate $i not directly validated to root");
76 # NOP
77 }
78 else {
79 0 $self->log_verbose("Certificate $i directly validated to root");
80 0 $cert->validated_by($root_ca_ascii);
81 0 $cert->validated_by_id(0);
82 0 $cert->is_valid_to_root(1);
83 }
84 }
85  
86 0 my $iteration_did_no_work;
87 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$iteration_did_no_work or do { do { $iteration_did_no_work = 1; VALIDATED_CERT: foreach my $validated_cert (@{$self->cert_object_list}) { next VALIDATED_CERT unless $validated_cert->is_valid_to_root; my $validated_i = $validated_cert->index; VALIDATING_CERT: foreach my $validating_cert (@{$self->cert_object_list}) { next VALIDATING_CERT if $validating_cert->is_valid_to_root; my $validating_i = $validating_cert->index; if ($validating_cert->is_expired) { $self->log_verbose("Certificate $validating_i is expired"); next; } ; unless ($validating_cert->is_valid) { $self->log_verbose("Certificate $validating_i is not valid"); next VALIDATING_CERT; } ; eval { do { $validated_cert->verifier->verify($validating_cert->x509_object); $self->log_verbose("Certificate $validating_i validated to root via certificate $validated_i"); $validating_cert->validated_by($validated_cert->full_chain); $validating_cert->validated_by_id($validated_i); $validating_cert->is_valid_to_root(1); $iteration_did_no_work = 0 } }; } ; } } } 0 0 0
88 0 $iteration_did_no_work = 1;
89 VALIDATED_CERT:
90 0 foreach my $validated_cert ( $self->cert_object_list->@* ) {
91 0 ▶ next VALIDATED_CERT if ! $validated_cert->is_valid_to_root;
Branch: 1 branch
BranchTrueFalseTotal
unless $validated_cert->is_valid_to_root 0 0 0
92 0 my $validated_i = $validated_cert->index;
93 VALIDATING_CERT:
94 0 foreach my $validating_cert ( $self->cert_object_list->@* ) {
95 0 ▶ next VALIDATING_CERT if $validating_cert->is_valid_to_root;
Branch: 1 branch
BranchTrueFalseTotal
if $validating_cert->is_valid_to_root 0 0 0
96 0 my $validating_i = $validating_cert->index;
97 0 ▶ if ($validating_cert->is_expired) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validating_cert->is_expired) 0 0 0
98 0 $self->log_verbose("Certificate $validating_i is expired");
99 0 next;
100 }
101 0 ▶ if ( !$validating_cert->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($validating_cert->is_valid) 0 0 0
102 0 $self->log_verbose("Certificate $validating_i is not valid");
103 0 next VALIDATING_CERT;
104 }
105 0 eval{
106 0 $validated_cert->verifier->verify($validating_cert->x509_object);
107 0 $self->log_verbose("Certificate $validating_i validated to root via certificate $validated_i");
108 0 $validating_cert->validated_by($validated_cert->full_chain);
109 0 $validating_cert->validated_by_id($validated_i);
110 0 $validating_cert->is_valid_to_root(1);
111 0 $iteration_did_no_work = 0;
112 };
113 }
114 }
115 } until $iteration_did_no_work;
116 0 ▶ if ( !$self->vmc->is_valid_to_root ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->vmc->is_valid_to_root) 0 0 0
117 0 $self->add_error('VMC_PARSE_ERROR','Could not verify VMC');
118 }
119  
120 0 ▶ if ( $unlink_root_cert_file && -f $ssl_root_cert ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unlink_root_cert_file and -f $ssl_root_cert) 0 0 0
Condition0%0/3
$unlink_root_cert_file and -f $ssl_root_cert
!ll&&!rl&&r
000
MC/DC0%0/2
$unlink_root_cert_file and -f $ssl_root_cert
$unlink_root_cert_file -f $ssl_root_cert
Truth table0%0/3
A and B
A= $unlink_root_cert_fileB= -f $ssl_root_cert
ABresult
0X0
100
111
121 0 ▶ unlink $ssl_root_cert or warn "Unable to unlink temporary chain file: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $ssl_root_cert or warn "Unable to unlink temporary chain file: $!" 0 0 0
122 }
123  
124 0 ▶ return 0 if $self->errors->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->errors} 0 0 0
125 0 return 1;
126 }
127  
128  
129 0 ▶sub vmc($self) {
Subroutine vmc: not called CC=8 SCAR=42.8
Pod documented
130 0 my $vmc;
131 0 foreach my $cert ( $self->cert_object_list->@* ) {
132 0 my $x509_object = $cert->x509_object;
133 0 ▶ next if !$x509_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $x509_object 0 0 0
134 0 my $exts = eval{ $x509_object->extensions_by_oid() };
135 0 ▶ next if !$exts;
Branch: 1 branch
BranchTrueFalseTotal
unless $exts 0 0 0
136 0 ▶ if ( $cert->has_valid_usage && exists $exts->{&LOGOTYPE_OID()}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cert->has_valid_usage and exists $$exts{&LOGOTYPE_OID()}) 0 0 0
Condition0%0/3
$cert->has_valid_usage and exists $$exts{&LOGOTYPE_OID()}
!ll&&!rl&&r
000
MC/DC0%0/2
$cert->has_valid_usage and exists $$exts{&LOGOTYPE_OID()}
$cert->has_valid_usage exists $$exts{&LOGOTYPE_OID()}
Truth table0%0/3
A and B
A= $cert->has_valid_usageB= exists $$exts{&LOGOTYPE_OID()}
ABresult
0X0
100
111
137 # Has both extended usage and embedded Indicator
138 0 ▶ $self->add_error('VMC_VALIDATION_ERROR','Multiple VMCs found in chain') if $vmc;
Branch: 1 branch
BranchTrueFalseTotal
if $vmc 0 0 0
139 0 $vmc = $cert;
140 }
141 }
142 0 ▶ if ( !$vmc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vmc) 0 0 0
143 0 $self->add_error('VMC_VALIDATION_ERROR','No valid VMC found in chain');
144 }
145 0 return $vmc;
146 }
147  
148 0 ▶sub _build_cert_object_list($self) {
Subroutine _build_cert_object_list: not called CC=2 SCAR=17.9
149 0 my @objects;
150 0 my $i = 1;
151 0 foreach my $cert ( $self->cert_list->@* ) {
152 0 push @objects, Mail::BIMI::VMC::Cert->new(
153 bimi_object => $self->bimi_object,
154 chain => $self,
155 ascii_lines => $cert,
156 index => $i++,
157 );
158 }
159 0 return \@objects;
160 }
161  
162  
163 0 ▶sub app_validate($self) {
Subroutine app_validate: not called CC=19 SCAR=59.4
Pod documented
164 0 ▶ say 'Certificate Chain Returned: '.($self->is_valid ? GREEN."\x{2713}" : BRIGHT_RED."\x{26A0}").RESET;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_valid ? : 0 0 0
165 0 foreach my $cert ( $self->cert_object_list->@* ) {
166 0 my $i = $cert->index;
167 0 my $obj = $cert->x509_object;
168 0 say '';
169 0 ▶ say YELLOW.' Certificate '.$i.WHITE.': '.($cert->is_valid ? GREEN."\x{2713}" : BRIGHT_RED."\x{26A0}").RESET;
Branch: 1 branch
BranchTrueFalseTotal
$cert->is_valid ? : 0 0 0
170 0 ▶ if ( $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj) 0 0 0
171 0 ▶ say YELLOW.' Subject '.WHITE.': '.CYAN.($obj->subject//'-none-').RESET;
Condition0%0/2
$obj->subject // "-none-"
l!l
00
MC/DC0%0/1
$obj->subject // "-none-"
$obj->subject
Truth table0%0/2
A
A= $obj->subject
Aresult
11
00
172 0 ▶ say YELLOW.' Not Before '.WHITE.': '.CYAN.($obj->notBefore//'-none-').RESET;
Condition0%0/2
$obj->notBefore // "-none-"
l!l
00
MC/DC0%0/1
$obj->notBefore // "-none-"
$obj->notBefore
Truth table0%0/2
A
A= $obj->notBefore
Aresult
11
00
173 0 ▶ say YELLOW.' Not After '.WHITE.': '.CYAN.($obj->notAfter//'-none-').RESET;
Condition0%0/2
$obj->notAfter // "-none-"
l!l
00
MC/DC0%0/1
$obj->notAfter // "-none-"
$obj->notAfter
Truth table0%0/2
A
A= $obj->notAfter
Aresult
11
00
174 0 ▶ say YELLOW.' Issuer '.WHITE.': '.CYAN.($obj->issuer//'-none-').RESET;
Condition0%0/2
$obj->issuer // "-none-"
l!l
00
MC/DC0%0/1
$obj->issuer // "-none-"
$obj->issuer
Truth table0%0/2
A
A= $obj->issuer
Aresult
11
00
175 0 ▶ say YELLOW.' Expired '.WHITE.': '.($obj->checkend(0)?BRIGHT_RED.'Yes':GREEN.'No').RESET;
Branch: 1 branch
BranchTrueFalseTotal
$obj->checkend(0) ? : 0 0 0
176 0 ▶ say YELLOW.' Experimental '.WHITE.': '.($cert->is_experimental?BRIGHT_RED.'Yes':GREEN.'No').RESET;
Branch: 1 branch
BranchTrueFalseTotal
$cert->is_experimental ? : 0 0 0
177 0 my $exts = eval{ $obj->extensions_by_oid() };
178 0 ▶ if ( $exts ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exts) { } 0 0 0
179 0 ▶ my $alt_name = exists $exts->{'2.5.29.17'} ? $exts->{'2.5.29.17'}->to_string : '-none-';
Branch: 1 branch
BranchTrueFalseTotal
exists $exts->{'2.5.29.17'} ? : 0 0 0
180 0 ▶ say YELLOW.' Alt Name '.WHITE.': '.CYAN.($alt_name//'-none-').RESET;
Condition0%0/2
$alt_name // "-none-"
l!l
00
MC/DC0%0/1
$alt_name // "-none-"
$alt_name
Truth table0%0/2
A
A= $alt_name
Aresult
11
00
181 0 ▶ say YELLOW.' Has LogotypeExtn '.WHITE.': '.CYAN.(exists($exts->{&LOGOTYPE_OID()})?GREEN.'Yes':BRIGHT_RED.'No').RESET;
Branch: 1 branch
BranchTrueFalseTotal
exists $$exts{&LOGOTYPE_OID()} ? : 0 0 0
182 }
183 else {
184 0 say YELLOW.' Extensions '.WHITE.': '.BRIGHT_RED.'NOT FOUND'.RESET;
185 }
186 0 ▶ say YELLOW.' Has Valid Usage '.WHITE.': '.CYAN.($cert->has_valid_usage?GREEN.'Yes':BRIGHT_RED.'No').RESET;
Branch: 1 branch
BranchTrueFalseTotal
$cert->has_valid_usage ? : 0 0 0
187 }
188 0 ▶ say YELLOW.' Valid to Root '.WHITE.': '.CYAN.($cert->is_valid_to_root?GREEN.($cert->validated_by_id == 0?'Direct':'Via cert '.$cert->validated_by_id):BRIGHT_RED.'No').RESET;
Branch: 2 branches
BranchTrueFalseTotal
$cert->is_valid_to_root ? : 0 0 0
$cert->validated_by_id == 0 ? : 0 0 0
189 0 ▶ say YELLOW.' Is Valid '.WHITE.': '.CYAN.($cert->is_valid?GREEN.'Yes':BRIGHT_RED.'No').RESET;
Branch: 1 branch
BranchTrueFalseTotal
$cert->is_valid ? : 0 0 0
190 }
191 }
192  
193 29 1;
194  
195 __END__
↑ Summary BIMI/Record.pm »