blib/lib/Mail/Milter/Authentication/Handler/SMIME.pm

filter: Statementstmt 93.9% 31 / 33 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.4% 42 / 44 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Mail::Milter::Authentication::Handler::SMIME;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mail::Milter::Authentication 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 our $VERSION = '2.20180318'; # VERSION
7 # ABSTRACT: Authentication Milter Module for validation of SMIME
8 1 ▶use English qw{ -no_match_vars };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Sys::Syslog qw{:standard :macros};
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Mail::AuthenticationResults::Header::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mail::AuthenticationResults::Header::SubEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Mail::AuthenticationResults::Header::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Convert::X509;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Crypt::SMIME;
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use Email::MIME;
17  
18 sub default_config {
19 return {
20 'hide_none' => 0,
21 'pki_store' => '/etc/ssl/certs',
22 };
23 }
24  
25 sub grafana_rows {
26 my ( $self ) = @_;
27 my @rows;
28 push @rows, $self->get_json( 'SMIME_metrics' );
29 return \@rows;
30 }
31  
32 sub register_metrics {
33 return {
34 'smime_total' => 'The number of emails processed for SMIME',
35 };
36 }
37  
38 sub envfrom_callback {
39 my ($self) = @_;
40 $self->{'data'} = [];
41 $self->{'found'} = 0;
42 $self->{'added'} = 0;
43 $self->{'metric_result'} = 'unknown';
44 return;
45 }
46  
47 sub header_callback {
48 my ( $self, $header, $value ) = @_;
49 push @{$self->{'data'}} , $header . ': ' . $value . "\n";
50 return;
51 }
52  
53 sub eoh_callback {
54 my ( $self ) = @_;
55 push @{$self->{'data'}} , "\n";
56 return;
57 }
58  
59 sub body_callback {
60 my ( $self, $chunk ) = @_;
61 push @{$self->{'data'}} , $chunk;
62 return;
63 }
64  
65 sub eom_callback {
66 my ( $self ) = @_;
67  
68 my $config = $self->handler_config();
69  
70 my $data = join( q{}, @{ $self->{'data'} } );
71 $data =~ s/\r//g;
72 # my $EOL = "\015\012";
73 # $data =~ s/\015?\012/$EOL/g;
74  
75 eval {
76 my $parsed = Email::MIME->new( $data );
77 $self->_parse_mime( $parsed, q{} );
78  
79 if ( $self->{'found'} == 0 ) {
80 if ( !( $config->{'hide_none'} ) ) {
81 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'smime' )->set_value( 'none' );
82 $self->add_auth_header( $header );
83 }
84 $self->{'metric_result'} = 'none';
85 }
86 elsif ( $self->{'added'} == 0 ) {
87 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'smime' )->set_value( 'temperror' );
88 $self->add_auth_header( $header );
89 $self->{'metric_result'} = 'error';
90 }
91 };
92 if ( my $error = $@ ) {
93 $self->log_error( 'SMIME Execution Error ' . $error );
94 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'smime' )->set_value( 'temperror' );
95 $self->add_auth_header( $header );
96 $self->{'metric_result'} = 'error';
97 }
98  
99 $self->metric_count( 'smime_total', { 'result' => $self->{'metric_result'} } );
100  
101 return;
102 }
103  
104  
105 sub _parse_mime {
106 my ( $self, $mime, $part_id ) = @_;
107  
108 $part_id =~ s/TEXT\.// ;
109  
110 my $content_type = $mime->content_type() || q{};
111 #$self->{'thischild'}->loginfo( 'SMIME Parse Type ' . $content_type );
112  
113 my $protocol = q{};
114 if ( $content_type . ';' =~ /protocol=.*;/ ) {
115 ( $protocol ) = $content_type =~ /protocol=([^;]*);/;
116 $protocol =~ s/"//g if $protocol;
117 }
118  
119 my $smime_type = q{};
120 if ( $content_type . ';' =~ /smime-type=.*;/ ) {
121 ( $smime_type ) = $content_type =~ /smime-type=([^;]*);/;
122 $smime_type =~ s/"//g if $smime_type;
123 }
124  
125 $content_type =~ s/;.*//;
126  
127 if ( $content_type eq 'message/rfc822' ) {
128 my $new_part = $part_id;
129 if ( $new_part ne q{} ) {
130 $new_part .= '.';
131 }
132 my $parsed = Email::MIME->new( $mime->body_raw() );
133 $self->_parse_mime( $parsed, $new_part . 'TEXT' );
134 }
135  
136 if ( $content_type eq 'multipart/signed' ) {
137 $self->{'thischild'}->loginfo( 'SMIME found ' . $content_type );
138 $self->{'thischild'}->loginfo( 'SMIME Protocol ' . $protocol );
139 if ( $protocol eq 'application/pkcs7-signature' || $protocol eq 'application/x-pkcs7-signature' || $protocol eq q{} ) {
140 my $header = $mime->{'header'}->as_string();
141 my $body = $mime->body_raw();
142 $self->_check_mime( $header . "\r\n" . $body, $part_id );
143 }
144 }
145  
146 if ( $content_type eq 'application/pkcs7-mime' ) {
147 $self->{'thischild'}->loginfo( 'SMIME found ' . $content_type );
148 $self->{'thischild'}->loginfo( 'SMIME Type ' . $smime_type );
149 if ( $smime_type eq 'signed-data' || $smime_type eq q{} ) {
150 # See rfc5751 3.4
151 my $header = $mime->{'header'}->as_string();
152 my $body = $mime->body_raw();
153 $self->_check_mime( $header . "\r\n" . $body, $part_id );
154 }
155 }
156  
157 my @parts = $mime->subparts();
158 #$self->{'thischild'}->loginfo( 'SMIME Has Subparts ' . scalar @parts );
159  
160 my $i = 1;
161 my $new_part = $part_id;
162 if ( $new_part ne q{} ) {
163 $new_part .= '.';
164 }
165 foreach my $part ( @parts ) {
166 $self->_parse_mime( $part, $new_part . $i++ );
167 }
168  
169 return;
170 }
171  
172 sub close_callback {
173 my ( $self ) = @_;
174 delete $self->{'metric_result'};
175 delete $self->{'added'};
176 delete $self->{'found'};
177 delete $self->{'data'};
178 return;
179 }
180  
181 sub _check_mime {
182 my ( $self, $data, $part_id ) = @_;
183  
184 if ( $part_id eq q{} ) {
185 $part_id = 'TEXT';
186 }
187  
188 $self->{'found'} = 1;
189  
190 my $smime = Crypt::SMIME->new();
191 my $config = $self->handler_config();
192 $smime->setPublicKeyStore( $config->{'pki_store'} );
193  
194 my $is_signed;
195 $is_signed = eval{ $smime->isSigned( $data ); };
196 if ( my $error = $@ ) {
197 $self->log_error( 'SMIME isSigned Error ' . $error );
198 }
199  
200 if ( $is_signed ) {
201  
202 my $source;
203 eval {
204 $source = $smime->check( $data );
205 };
206 if ( my $error = $@ ) {
207 $self->log_error( 'SMIME check Error ' . $error );
208 my $signatures = Crypt::SMIME::getSigners( $data );
209 my $all_certs = Crypt::SMIME::extractCertificates( $data );
210 $self->_decode_certs( 'fail', $signatures, $all_certs, $part_id );
211 ## ToDo extract the reason for failure and add as header comment
212 if ( $self->{'added'} == 0 ) {
213 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'smime' )->set_value( 'fail' );
214 $self->add_auth_header( $header );
215 $self->{'metric_result'} = 'fail';
216 $self->{'added'} = 1;
217 }
218 }
219 else {
220 my $signatures = Crypt::SMIME::getSigners( $data );
221 my $all_certs = Crypt::SMIME::extractCertificates( $data );
222 $self->_decode_certs( 'pass', $signatures, $all_certs, $part_id );
223 }
224 }
225  
226 return;
227 }
228  
229 sub _decode_certs {
230 my ( $self, $passfail, $signatures, $all_certs, $part_id ) = @_;
231  
232 my $seen = {};
233  
234 SIGNATURE:
235 foreach my $cert ( @{$signatures} ) {
236  
237  
238 my $cert_info = Convert::X509::Certificate->new( $cert );
239  
240 my $subject = $cert_info->subject();
241 my $issuer = $cert_info->issuer();
242 my $from = $cert_info->from();
243 my $to = $cert_info->to();
244 my $eku = $cert_info->eku();
245 my $serial = $cert_info->serial();
246 my @aia = $cert_info->aia();
247  
248 $from = 'TEST' if exists $self->handler_config()->{ 'TEST_DATE' };
249 $to = 'TEST' if exists $self->handler_config()->{ 'TEST_DATE' };
250  
251 next SIGNATURE if $seen->{ $serial };
252 $seen->{ $serial } = 1;
253  
254 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'smime' )->safe_set_value( $passfail );
255  
256 my $header_id = Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'body.smime-identifier' )->safe_set_value( $subject->{'E'}[0] );
257 $header_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( $subject->{'CN'}[0] ) );
258 $header->add_child( $header_id );
259  
260 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'body.smime-part' )->safe_set_value( $part_id ) );
261 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'body.smime-serial' )->safe_set_value( $serial ) );
262  
263 my $issuer_text = join( ',', map{ $_ . '=' . $issuer->{$_}[0] } sort keys (%{$issuer}) );
264 $issuer_text =~ s/\"/ /g;
265  
266 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'body.smime-issuer' )->safe_set_value( $issuer_text ) );
267 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-smime-valid-from' )->safe_set_value( $from ) );
268 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-smime-valid-to' )->safe_set_value( $to ) );
269  
270 $self->add_auth_header($header);
271  
272 $self->{'metric_result'} = $passfail;
273 $self->{'added'} = 1;
274 }
275  
276 # Non standard
277 CERT:
278 foreach my $cert ( @{$all_certs} ) {
279  
280 my $cert_info = Convert::X509::Certificate->new( $cert );
281  
282 my $subject = $cert_info->subject();
283 my $issuer = $cert_info->issuer();
284 my $from = $cert_info->from();
285 my $to = $cert_info->to();
286 my $eku = $cert_info->eku();
287 my $serial = $cert_info->serial();
288 my @aia = $cert_info->aia();
289  
290 $from = 'TEST' if exists $self->handler_config()->{ 'TEST_DATE' };
291 $to = 'TEST' if exists $self->handler_config()->{ 'TEST_DATE' };
292  
293 next CERT if $seen->{ $serial };
294 $seen->{ $serial } = 1;
295  
296 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-smime-chain' )->safe_set_value( 'info' );
297  
298 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'body.smime-part' )->safe_set_value( $part_id ) );
299 my $chain_id_value = $subject->{'E'}[0];
300 my $chain_id_comment = $subject->{'CN'}[0];
301 $chain_id_value = 'null' if ! defined $chain_id_value;
302 $chain_id_comment = 'null' if ! defined $chain_id_comment;
303 my $chain_id = Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-smime-chain-identifier' )->safe_set_value( $chain_id_value );
304 $chain_id->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( $chain_id_comment ) );
305 $header->add_child( $chain_id );
306 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-smime-chain-serial' )->safe_set_value( $serial ) );
307 my $issuer_text = join( ',', map{ $_ . '=' . $issuer->{$_}[0] } sort keys (%{$issuer}) );
308 $issuer_text =~ s/\"/ /g;
309 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-smime-chain-issuer' )->safe_set_value( $issuer_text ) );
310 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-smime-chain-valid-from' )->safe_set_value( $from ) );
311 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'x-smime-chain-valid-to' )->safe_set_value( $to ) );
312 $self->add_auth_header( $header );
313 $self->{'added'} = 1;
314 }
315  
316 return;
317 }
318  
319 1;
320  
321 __END__
322  
323 =pod
324  
325 =encoding UTF-8
326  
327 =head1 NAME
328  
329 Mail::Milter::Authentication::Handler::SMIME - Authentication Milter Module for validation of SMIME
330  
331 =head1 VERSION
332  
333 version 2.20180318
334  
335 =head1 DESCRIPTION
336  
337 Check SMIME signed email for validity.
338  
339 =head1 CONFIGURATION
340  
341 "SMIME" : {
342 "hide_none" : 0,
343 "pki_store" : "/etc/ssl/certs"
344 },
345  
346 =head2 CONFIG
347  
348 Add a block to the handlers section of your config as follows.
349  
350 "SMIME" : {
351 "hide_none" : 0, | Hide auth line if the result is 'none'
352 "pki_store" : "/etc/ssl/certs" | The location of your trusted root certs
353 },
354  
355 =head1 AUTHOR
356  
357 Marc Bradshaw <marc@marcbradshaw.net>
358  
359 =head1 COPYRIGHT AND LICENSE
360  
361 This software is copyright (c) 2018 by Marc Bradshaw.
362  
363 This is free software; you can redistribute it and/or modify it under
364 the same terms as the Perl 5 programming language system itself.
365  
366 =cut
↑ Summary