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.20260302'; # 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 sub DESTROY {
44 0 ▶ my ($self) = @_;
Subroutine DESTROY: not called CC=5 SCAR=34.0
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 ▶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 ▶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 ▶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 ▶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 ▶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 ▶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 ▶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 ▶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 ▶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__
150  
151 =pod
152  
153 =encoding UTF-8
154  
155 =head1 NAME
156  
157 Mail::BIMI::VMC::Cert - Class to model a VMC Cert
158  
159 =head1 VERSION
160  
161 version 3.20260302
162  
163 =head1 DESCRIPTION
164  
165 Class for representing, retrieving, validating, and processing a VMC Certificate
166  
167 =head1 INPUTS
168  
169 These values are used as inputs for lookups and verifications, they are typically set by the caller based on values found in the message being processed
170  
171 =head2 ascii_lines
172  
173 is=rw required
174  
175 Raw data of the Cert contents
176  
177 =head1 ATTRIBUTES
178  
179 These values are derived from lookups and verifications made based upon the input values, it is however possible to override these with other values should you wish to, for example, validate a record before it is published in DNS, or validate an Indicator which is only available locally
180  
181 =head2 chain
182  
183 is=rw required
184  
185 Back reference to the chain
186  
187 =head2 errors
188  
189 is=rw
190  
191 =head2 filename
192  
193 is=rw
194  
195 Filename of temporary file containing the cert
196  
197 =head2 index
198  
199 is=rw required
200  
201 Index of this certificate in the chain
202  
203 =head2 indicator_asn
204  
205 is=rw
206  
207 Parsed ASN data for the embedded Indicator
208  
209 =head2 is_valid
210  
211 is=rw
212  
213 Is this a valid Cert?
214  
215 =head2 is_valid_to_root
216  
217 is=rw
218  
219 Could we validate this certificate to the root certs, set by Mail::BIMI::VMC::Chain->is_valid
220  
221 =head2 validated_by
222  
223 is=rw
224  
225 Root and/or intermediate certificate in the chain used to verify this certificate
226  
227 =head2 validated_by_id
228  
229 is=rw
230  
231 Index of cert which validated this cert
232  
233 =head2 verifier
234  
235 is=rw
236  
237 Crypt::OpenSSL::Verify object for the Certificate
238  
239 =head2 warnings
240  
241 is=rw
242  
243 =head2 x509_object
244  
245 is=rw
246  
247 Crypt::OpenSSL::X509 object for the Certificate
248  
249 =head1 CONSUMES
250  
251 =over 4
252  
253 =item * L<Mail::BIMI::Role::Data>
254  
255 =item * L<Mail::BIMI::Role::Data|Mail::BIMI::Role::HasError>
256  
257 =item * L<Mail::BIMI::Role::HasError>
258  
259 =back
260  
261 =head1 EXTENDS
262  
263 =over 4
264  
265 =item * L<Mail::BIMI::Base>
266  
267 =back
268  
269 =head1 METHODS
270  
271 =head2 I<is_expired()>
272  
273 Return true if this cert has expired
274  
275 =head2 I<is_experimental()>
276  
277 Returns true if the cert is marked as experimental
278  
279 =head2 I<has_valid_usage()>
280  
281 Return true if this VMC has a valid usage extension for BIMI
282  
283 =head2 I<full_chain()>
284  
285 The full chain of this certificate as verified to root
286  
287 =head1 REQUIRES
288  
289 =over 4
290  
291 =item * L<Convert::ASN1|Convert::ASN1>
292  
293 =item * L<Crypt::OpenSSL::Verify|Crypt::OpenSSL::Verify>
294  
295 =item * L<Crypt::OpenSSL::X509|Crypt::OpenSSL::X509>
296  
297 =item * L<File::Slurp|File::Slurp>
298  
299 =item * L<File::Temp|File::Temp>
300  
301 =item * L<Mail::BIMI::Prelude|Mail::BIMI::Prelude>
302  
303 =item * L<Moose|Moose>
304  
305 =back
306  
307 =head1 AUTHOR
308  
309 Marc Bradshaw <marc@marcbradshaw.net>
310  
311 =head1 COPYRIGHT AND LICENSE
312  
313 This software is copyright (c) 2020 by Marc Bradshaw.
314  
315 This is free software; you can redistribute it and/or modify it under
316 the same terms as the Perl 5 programming language system itself.
317  
318 =cut
« BIMI/App/Command/checkdomain.pm ↑ Summary BIMI.pm »