BIMI/VMC/Cert.pm

filter: Statementstmt 37.5% 39 / 104 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 42.1% 8 / 19 Podpod 100% 4 / 4 totaltotal 29.6% 51 / 172 CC 22 SCAR 30.1
CC
22
Cov
27%
_build_indicator_asn
42.0
has_valid_usage
42.0
DESTROY
30.0
CRAP 20.2
« BIMI/App/Command/checkdomain.pm ↑ Summary BIMI.pm »
1 package Mail::BIMI::VMC::Cert;
2 # ABSTRACT: Class to model a VMC Cert
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 Convert::ASN1;
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  
13 29 extends 'Mail::BIMI::Base';
14 29 with(
15 'Mail::BIMI::Role::Data',
16 'Mail::BIMI::Role::HasError',
17 );
18 29 has chain => ( is => 'rw', isa => 'Mail::BIMI::VMC::Chain', required => 1, weak_ref => 1,
19 documentation => 'Back reference to the chain' );
20 29 has ascii_lines => ( is => 'rw', isa => 'ArrayRef', required => 1,
21 documentation => 'inputs: Raw data of the Cert contents', );
22 29 has x509_object => ( is => 'rw', isa => 'Maybe[Crypt::OpenSSL::X509]', lazy => 1, builder => '_build_x509_object',
23 documentation => 'Crypt::OpenSSL::X509 object for the Certificate' );
24 29 has verifier => ( is => 'rw', isa => 'Crypt::OpenSSL::Verify', lazy => 1, builder => '_build_verifier',
25 documentation => 'Crypt::OpenSSL::Verify object for the Certificate' );
26 29 has is_valid_to_root => ( is => 'rw',
27 documentation => 'Could we validate this certificate to the root certs, set by Mail::BIMI::VMC::Chain->is_valid' );
28 29 has filename => ( is => 'rw', lazy => 1, builder => '_build_filename',
29 documentation => 'Filename of temporary file containing the cert' );
30 0 ▶has _delete_file_on_destroy => ( is => 'rw', lazy => 1, default => sub{return 0} );
Subroutine __ANON__: not called CC=1 SCAR=1.2
31 29 has is_valid => ( is => 'rw', lazy => 1, builder => '_build_is_valid',
32 documentation => 'Is this a valid Cert?' );
33 29 has indicator_asn => ( is => 'rw', lazy => 1, builder => '_build_indicator_asn',
34 documentation => 'Parsed ASN data for the embedded Indicator' );
35 29 has index => ( is => 'rw', required => 1,
36 documentation => 'Index of this certificate in the chain' );
37 29 has validated_by => ( is => 'rw',
38 documentation => 'Root and/or intermediate certificate in the chain used to verify this certificate' );
39 29 has validated_by_id => ( is => 'rw',
40 documentation => 'Index of cert which validated this cert' );
41  
42  
43 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=5 SCAR=34.0
44 0 my ($self) = @_;
45 0 ▶ return unless $self->{_delete_file_on_destroy};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_delete_file_on_destroy"} 0 0 0
46 0 ▶ if ( $self->{filename} && -f $self->{filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filename"} and -f $self->{"filename"}) 0 0 0
Condition0%0/3
$self->{"filename"} and -f $self->{"filename"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"filename"} and -f $self->{"filename"}
$self->{"filename"} -f $self->{"filename"}
Truth table0%0/3
A and B
A= $self->{"filename"}B= -f $self->{"filename"}
ABresult
0X0
100
111
47 0 ▶ unlink $self->{filename} or warn "Unable to unlink temporary cert file: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->{"filename"} or warn "Unable to unlink temporary cert file: $!" 0 0 0
48 }
49 }
50  
51 0 ▶sub _build_is_valid($self) {
Subroutine _build_is_valid: not called CC=2 SCAR=17.9
52 0 $self->x509_object; # trigger object parse
53 0 ▶ return 0 if $self->errors->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->errors} 0 0 0
54 0 return 1;
55 }
56  
57 0 ▶sub _build_indicator_asn($self) {
Subroutine _build_indicator_asn: not called CC=6 SCAR=37.4
58 0 ▶ return if !$self->x509_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->x509_object 0 0 0
59 0 my $exts = eval{ $self->x509_object->extensions_by_oid() };
60 0 ▶ return if !$exts;
Branch: 1 branch
BranchTrueFalseTotal
unless $exts 0 0 0
61 0 ▶ return if !exists $exts->{&LOGOTYPE_OID()};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$exts{&LOGOTYPE_OID()} 0 0 0
62 0 my $indhex = $exts->{&LOGOTYPE_OID()}->value;
63 0 $indhex =~ s/^#//;
64 0 my $indicator = pack("H*",$indhex);
65 0 my $asn = Convert::ASN1->new;
66 0 $asn->prepare_file($self->get_file_name('asn1.txt'));
67 0 my $decoder = $asn->find('LogotypeExtn');
68 0 ▶ die $asn->error if $asn->error;
Branch: 1 branch
BranchTrueFalseTotal
if $asn->error 0 0 0
69 0 my $decoded = $decoder->decode($indicator);
70 0 ▶ if ( $decoder->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoder->error) 0 0 0
71 0 $self->add_error('VMC_PARSE_ERROR',$decoder->error);
72 0 return;
73 }
74  
75 #my $image_details = $decoded->{subjectLogo}->{direct}->{image}->[0]->{imageDetails};
76 #my $mime_type = $image_details->{mediaType};
77 #my $logo_hash = $image_details->{logotypeHash}->[0];
78 0 return $decoded;
79 }
80  
81 0 ▶sub _build_x509_object($self) {
Subroutine _build_x509_object: not called CC=2 SCAR=17.9
82 0 my $cert;
83 eval{
84 0 $cert = Crypt::OpenSSL::X509->new_from_string(join("\n",$self->ascii_lines->@*));
85 0 1;
86 0 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $cert = "Crypt::OpenSSL::X509"->new_from_string(join("\n", @{$self->ascii_lines})); 1 } } or do { my $error = $@; chomp $error; $error =~ s/\. at .*$//u; $self->add_error("VMC_PARSE_ERROR", $error); return } 0 0 0
87 0 my $error = $@;
88 0 chomp $error;
89 0 $error =~ s/\. at .*$//;
90 0 $self->add_error('VMC_PARSE_ERROR',$error);
91 0 return;
92 };
93 0 return $cert;
94 }
95  
96 0 ▶sub _build_verifier($self) {
Subroutine _build_verifier: not called CC=1 SCAR=6.9
97 0 return Crypt::OpenSSL::Verify->new($self->filename,{noCApath=>1});
98 }
99  
100  
101 0 ▶sub is_expired($self) {
Subroutine is_expired: not called CC=3 SCAR=24.8
Pod documented
102 0 ▶ return 0 if !$self->x509_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->x509_object 0 0 0
103 0 my $seconds = 0;
104 0 ▶ if ($self->x509_object->checkend($seconds)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->x509_object->checkend($seconds)) 0 0 0
105 0 return 1;
106 }
107 0 return 0;
108 }
109  
110  
111 0 ▶sub is_experimental($self) {
Subroutine is_experimental: not called CC=4 SCAR=30.0
Pod documented
112 0 ▶ return if !$self->x509_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->x509_object 0 0 0
113 0 my $exts = eval{ $self->x509_object->extensions_by_oid() };
114 0 ▶ return if !$exts;
Branch: 1 branch
BranchTrueFalseTotal
unless $exts 0 0 0
115 0 my $key = IS_EXPERIMENTAL_OID;
116 0 ▶ return if !exists $exts->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $exts->{$key} 0 0 0
117 0 return 1;
118 }
119  
120  
121 0 ▶sub has_valid_usage($self) {
Subroutine has_valid_usage: not called CC=6 SCAR=37.4
Pod documented
122 0 ▶ return if !$self->x509_object;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->x509_object 0 0 0
123 0 my $exts = eval{ $self->x509_object->extensions_by_oid() };
124 0 ▶ return if !$exts;
Branch: 1 branch
BranchTrueFalseTotal
unless $exts 0 0 0
125 0 my $extended_usage = $exts->{'2.5.29.37'};
126 0 ▶ return if !$extended_usage;
Branch: 1 branch
BranchTrueFalseTotal
unless $extended_usage 0 0 0
127 0 my $extended_usage_string = $extended_usage->to_string;
128 0 ▶ return 1 if $extended_usage_string eq USAGE_OID;
Branch: 1 branch
BranchTrueFalseTotal
if $extended_usage_string eq "1.3.6.1.5.5.7.3.31" 0 0 0
129 0 ▶ return 1 if $extended_usage_string eq 'Brand Indicator for Message Identification';
Branch: 1 branch
BranchTrueFalseTotal
if $extended_usage_string eq "Brand Indicator for Message Identification" 0 0 0
130 0 return 0;
131 }
132  
133  
134 0 ▶sub full_chain($self) {
Subroutine full_chain: not called CC=1 SCAR=6.9
Pod documented
135 0 return join("\n",$self->ascii_lines->@*,$self->validated_by);
136 }
137  
138 0 ▶sub _build_filename($self) {
Subroutine _build_filename: not called CC=1 SCAR=6.9
139 0 my $temp_fh = File::Temp->new(UNLINK=>0);
140 0 my $temp_name = $temp_fh->filename;
141 0 close $temp_fh;
142 0 write_file($temp_name,$self->full_chain);
143 0 $self->_delete_file_on_destroy(1);
144 0 return $temp_name;
145 }
146  
147 29 1;
148  
149 __END__
« BIMI/App/Command/checkdomain.pm ↑ Summary BIMI.pm »