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

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