-
Notifications
You must be signed in to change notification settings - Fork 1
/
Iterator.pm
1218 lines (1017 loc) · 33.9 KB
/
Iterator.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Mail::DKIM::Iterator;
use v5.10.0;
our $VERSION = '1.001';
use strict;
use warnings;
use Crypt::OpenSSL::RSA;
use Scalar::Util 'dualvar';
# critical header fields which should be well protected
my @critical_headers = qw(from subject content-type content-transfer-encoding);
my $critical_headers_rx = do {
my $rx = join("|",@critical_headers);
qr{$rx}i;
};
# all header fields which should be included in the signature
my @sign_headers = (@critical_headers, 'to', 'cc');
use Exporter 'import';
our @EXPORT =qw(
DKIM_INVALID_HDR
DKIM_TEMPFAIL
DKIM_SOFTFAIL
DKIM_PERMFAIL
DKIM_SUCCESS
);
use constant {
DKIM_INVALID_HDR => dualvar(-3,'invalid-header'),
DKIM_SOFTFAIL => dualvar(-2,'soft-fail'),
DKIM_TEMPFAIL => dualvar(-1,'temp-fail'),
DKIM_PERMFAIL => dualvar( 0,'perm-fail'),
DKIM_SUCCESS => dualvar( 1,'valid'),
};
# create new object
sub new {
my ($class,%args) = @_;
my $self = bless {
records => $args{dns} || {}, # mapping (dnsname,dkim_key)
extract_sig => 1, # extract signatures from mail header
filter => $args{filter}, # filter which signatures are relevant
# sig => [...], # list of signatures from header or to sign
_hdrbuf => '', # used while collecting the mail header
}, $class;
if (my $sig = delete $args{sign}) {
# signatures given for signing, either as [{},...] or {}
# add to self.sig
$sig = [$sig] if ref($sig) ne 'ARRAY';
$self->{extract_sig} = delete $args{sign_and_verify};
my $error;
for(@$sig) {
$_->{h} //= 'from' if ref($_); # minimal
my $s = parse_signature($_,\$error,1);
die "bad signature '$_': $error" if !$s;
$s->{h_auto} //= 1; # secure version will be detected based on mail
push @{$self->{sig}}, $s
}
}
return $self;
}
# Iterator: feed object with information and get back what to do next
sub next {
my $self = shift;
my $rv;
while (@_) {
my $arg = shift;
if (ref($arg)) {
# ref: mapping (host,dkim_key)
while (my ($k,$v) = each %$arg) {
$self->{records}{$k} = $v;
}
$rv = _compute_result($self);
} else {
# string: append data from mail
if (defined $self->{_hdrbuf}) {
# header not fully read: append and try to find end of header
$self->{_hdrbuf} .= $arg;
$self->{_hdrbuf} =~m{(\r?\n)\1}g or last; # no end of header
# Extract header into self.header and look for DKIM signatures
# inside. The rest of _hdrbuf will be appended as part of the
# body and the attribute _hdrbuf itself is no longer needed
$self->{header} = substr($self->{_hdrbuf},0,$+[0],'');
if ($self->{extract_sig}
and my @sig = _parse_header($self->{header})) {
if (my $f = $self->{filter}) {
@sig = grep { $f->($_,$self->{header}) } @sig;
}
push @{$self->{sig} ||= []}, @sig if @sig;
}
$arg = delete $self->{_hdrbuf};
_append_body($self,$arg) if $arg ne '';
} else {
# header already read: append as part of body
_append_body($self,$arg);
}
if (!$self->{sig}) {
# No signatures found in body -> empty return list
$rv = [];
} else {
$rv = _compute_result($self);
}
}
}
$rv = _compute_result($self) if ! @_;
# If we have no results yet just return that we need more data
$rv or return ([],\'');
# Extract the DNS names for the partial results where the DKIM key is needed
# and return the as todo. If the body hash could not yet computed for a
# signature mark also that we need more data
my (%need_dns,$need_more_data);
for(@$rv) {
$_->status and next;
my $sig = $_->sig;
# Need more data to compute the body hash?
$need_more_data = 1 if !$sig->{'bh:computed'};
# Need to get DKIM key to validate signature?
# Only if we have sig.b, i.e. an extracted signature from the header.
if ($sig->{b}) {
my $name = $_->dnsname;
$need_dns{$name}++ if ! $self->{records}{$name};
}
}
# return preliminary results and @todo
return ($rv,$need_more_data ? (\''):(),sort keys %need_dns);
}
sub filter {
my ($self,$filter) = @_;
$self->{filter} = $filter;
@{$self->{sig}} = grep { $filter->($_,$self->{header}) } @{$self->{sig}}
if $self->{header} && $self->{sig};
}
sub result {
my $self = shift;
return $self->{_last_result};
}
sub authentication_results {
return join(";\n",map { " ".$_->authentication_results } @{shift->result || []});
}
# Compute result based on current data.
# This might add more DKIM records to validate signatures.
sub _compute_result {
my $self = shift;
return if defined $self->{_hdrbuf}; # need more header
return [] if !$self->{sig}; # nothing to verify
my @rv;
for my $sig (@{$self->{sig}}) {
# use final result if we have one already
if ($sig->{':result'}) {
push @rv, $sig->{':result'};
next;
}
if (!$sig->{b}) {
# sig is not for verification but for signing
if (!$sig->{'bh:computed'}) {
# incomplete: still need more data to compute signature
push @rv, Mail::DKIM::Iterator::SignRecord->new($sig);
} else {
# complete: compute signature and save it in :result
my $err;
my $dkim_sig = sign($sig,$sig->{':key'},$self->{header},\$err);
push @rv, $sig->{':result'} =
Mail::DKIM::Iterator::SignRecord->new(
$dkim_sig ? ($sig,$dkim_sig,DKIM_SUCCESS)
: ($sig,undef,DKIM_PERMFAIL,$err)
);
}
next;
}
if ($sig->{error}) {
# something wrong with the DKIM-Signature header, return error
push @rv, $sig->{':result'} =
Mail::DKIM::Iterator::VerifyRecord->new(
$sig,
($sig->{s}//'UNKNOWN')."_domainkey".($sig->{d}//'UNKNOWN'),
DKIM_INVALID_HDR,
$sig->{error}
);
next;
}
my $dns = "$sig->{s}._domainkey.$sig->{d}";
if ($sig->{x} && $sig->{x} < time()) {
push @rv, $sig->{':result'} = Mail::DKIM::Iterator::VerifyRecord
->new($sig,$dns, DKIM_SOFTFAIL, "signature e[x]pired");
next;
}
if (my $txt = $self->{records}{$dns}) {
if (!ref($txt) || ref($txt) eq 'ARRAY') {
# Take the first syntactically valid DKIM key from the list of
# TXT records.
my $error = "no TXT records";
for(ref($txt) ? @$txt:$txt) {
if (my $r = parse_dkimkey($_,\$error)) {
$self->{records}{$dns} = $txt = $r;
$error = undef;
last;
}
}
if ($error) {
$self->{records}{$dns} = $txt = { permfail => $error };
}
}
my @v = _verify_sig($sig,$txt);
push @rv, Mail::DKIM::Iterator::VerifyRecord->new($sig,$dns,@v);
$sig->{':result'} = $rv[-1] if @v; # final result
} elsif (exists $self->{records}{$dns}) {
# cannot get DKIM record
push @rv, $sig->{':result'} = Mail::DKIM::Iterator::VerifyRecord
->new($sig,$dns, DKIM_TEMPFAIL, "dns lookup failed");
} else {
# no DKIM record yet known for $dns - preliminary result
push @rv, Mail::DKIM::Iterator::VerifyRecord->new($sig,$dns);
}
}
return ($self->{_last_result} = \@rv);
}
# Parse DKIM-Signature value into hash and fill in necessary default values.
# Input can be string or hash.
sub parse_signature {
my ($v,$error,$for_signing) = @_;
$v = parse_taglist($v,$error) or return if !ref($v);
if ($for_signing) {
# some defaults
$v->{v} //= '1';
}
if (($v->{v}//'') ne '1') {
$$error = "bad DKIM signature version: ".($v->{v}||'<undef>');
} elsif (!$v->{d}) {
$$error = "required [d]omain not given";
} elsif (!$v->{s}) {
$$error = "required [s]elector not given";
} elsif (!$v->{h}) {
$$error = "required [h]eader fields not given";
} elsif ($v->{l} && $v->{l} !~m{^\d{1,76}\z}) {
$$error = "invalid body [l]ength";
} elsif (do {
$v->{q} = lc($v->{q}//'dns/txt');
$v->{q} ne 'dns/txt'
}) {
$$error = "unsupported query method $v->{q}";
}
return if $$error;
$v->{d} = lc($v->{d});
$v->{a} = lc($v->{a}//'rsa-sha256');
$v->{c} = lc($v->{c}//'simple/simple');
my @h = split(/\s*:\s*/,lc($v->{h}));
$$error = "'from' missing from [h]eader fields" if ! grep { $_ eq 'from' } @h;
$v->{'h:list'} = \@h;
if ($for_signing) {
delete $v->{b};
delete $v->{bh};
$v->{t} = undef if exists $v->{t};
if (defined $v->{x} && $v->{x} !~m{^\+?\d{1,12}\z}) {
$$error = "invalid e[x]piration time";
}
} else {
if (!$v->{b} or not $v->{'b:bin'} = _decode64($v->{b})) {
$$error = "invalid body signature: ".($v->{b}||'<undef>');
} elsif (!$v->{bh} or not $v->{'bh:bin'} = _decode64($v->{bh})) {
$$error = "invalid header signature: ".($v->{bh}||'<undef>');
} elsif ($v->{t} && $v->{t} !~m{^\d{1,12}\z}) {
$$error = "invalid [t]imestamp";
} elsif ($v->{x}) {
if ($v->{x} !~m{^\d{1,12}\z}) {
$$error = "invalid e[x]piration time";
} elsif ($v->{t} && $v->{x} < $v->{t}) {
$$error = "expiration precedes timestamp";
}
}
if ($v->{i}) {
$v->{i} = _decodeQP($v->{i});
if (lc($v->{i}) =~m{\@([^@]+)$}) {
$v->{'i:domain'} = $1;
$$error ||= "[i]dentity does not match [d]omain"
if $v->{'i:domain'} !~m{^(.+\.)?\Q$v->{d}\E\z};
} else {
$$error = "no domain in identity";
}
} else {
$v->{i} = '@'.$v->{d};
}
}
my ($hdrc,$bodyc) = $v->{c}
=~m{^(relaxed|simple)(?:/(relaxed|simple))?$} or do {
$$error ||= "invalid canonicalization $v->{c}";
};
$bodyc ||= 'simple';
my ($kalgo,$halgo) = $v->{a} =~m{^(rsa)-(sha(?:1|256))$} or do {
$$error ||= "unsupported algorithm $v->{a}";
};
return if $$error;
$v->{'c:hdr'} = $hdrc;
$v->{'c:body'} = $bodyc;
$v->{'a:key'} = $kalgo;
$v->{'a:hash'} = $halgo;
# ignore: z
return $v;
}
# Parse DKIM key into hash and fill in necessary default values.
# Input can be string or hash.
sub parse_dkimkey {
my ($v,$error) = @_;
$v = parse_taglist($v,$error) or return if !ref($v);
if (!$v || !%$v) {
$$error = "invalid or empty DKIM record";
return;
}
if (($v->{v}||='DKIM1') ne 'DKIM1') {
$$error = "bad DKIM record version: $v->{v}";
} elsif (($v->{k}//='rsa') ne 'rsa') {
$$error = "unsupported key type $v->{k}";
} else {
if (exists $v->{g}) {
# g is deprecated in RFC 6376
if (1) {
delete $v->{g}
} else {
$v->{g} = ($v->{g}//'') =~m{^(.*)\*(.*)$}
? qr{^\Q$1\E.*\Q$2\E\@[^@]+\z}
: qr{^\Q$v->{g}\E\@[^@]+\z};
}
}
$v->{t} = { map { $_ => 1 } split(':',lc($v->{t} || '')) };
$v->{h} = { map { $_ => 1 } split(':',lc($v->{h} || 'sha1:sha256')) };
$v->{s} = { map { $_ => 1 } split(':',lc($v->{s} || '*')) };
if (!$v->{s}{'*'} && !$v->{s}{email}) {
$$error = "service type " . join(':',keys %{$v->{s}})
. " does not match";
return;
}
return $v;
}
return;
}
# Finalize signature, i.e add the 'b' parameter.
# Input is signature (hash or string), the private key (PEM or
# Crypto::OpenSSL::RSA object) and the mail header.
# Output is "DKIM-Signature: .... " string with proper line length so that it
# can be inserted into the mail header.
sub sign {
my ($sig,$key,$hdr,$error) = @_;
if (ref($sig) && $sig->{h_auto}) {
# add a useful default based on the header which makes sure that no all
# relevant headers are covered and no additional important headers can
# be added
my (%oh,@nh);
$oh{lc($_)}++ for split(':',$sig->{h} ||'');
for my $k (@sign_headers) {
for($hdr =~m{^($k):}mgi) {
push @nh,$k; # cover each instance in header
}
push @nh,$k; # cover non-existance so that no instance can be added
delete $oh{$k} if exists $oh{$k} and --$oh{$k} == 0;
}
push @nh,($_) x $oh{$_} for keys %oh;
$sig->{h} = join(':',@nh);
}
$sig = parse_signature($sig,$error,1) or return;
my %sig = %$sig;
$sig{t} = time() if !$sig{t} && exists $sig{t};
$sig{x} = ($sig{t} || time()) + $1
if $sig{x} && $sig{x} =~m{^\+(\d+)$};
$sig{'a:key'} eq 'rsa' or do {
$$error = "unsupported algorithm ".$sig{'a:key'};
return;
};
delete $sig{b};
$sig{i} = _encodeQP($sig{':i'}) if $sig{':i'};
$sig{z} = _encodeQP($sig{':z'}) if $sig{':z'};
$sig{bh} = _encode64($sig{'bh:computed'} || $sig{'bh:bin'});
$sig{h} = join(':',@{$sig{'h:list'}});
my @v;
for (qw(v a c d q s t x h l i z bh)) {
my $v = delete $sig{$_} // next;
push @v, "$_=$v"
}
for(sort keys %sig) {
m{:} and next;
my $v = _encodeQP(delete $sig{$_} // next);
push @v, "$_=$v"
}
my @lines = shift(@v);
for(@v,"b=") {
$lines[-1] .= ';';
my $append = " $_";
my $x80 = (@lines == 1 ? 64 : 80) - length($lines[-1]);
if (length($append)<=$x80) {
$lines[-1] .= $append;
} elsif (length($append)<=80) {
push @lines,$append;
} else {
while (1) {
if ( $x80>10) {
$lines[-1] .= substr($append,0,$x80,'');
$append eq '' and last;
}
push @lines,' ';
$x80 = 80;
}
}
}
my $dkh = 'DKIM-Signature: '.join("\r\n",@lines);
$sig->{'a:key'} eq 'rsa' or do {
$$error = "unsupported signature algorithm $sig->{'a:key'}";
return;
};
my $hash = _compute_hdrhash($hdr,
$sig{'h:list'},$sig->{'a:hash'},$sig->{'c:hdr'},$dkh);
my $priv = ref($key) ? $key : Crypt::OpenSSL::RSA->new_private_key($key);
$priv or do {
$$error = "using private key failed";
return;
};
$priv->use_no_padding;
my $data = _encode64($priv->decrypt(
_emsa_pkcs1_v15($sig->{'a:hash'},$hash,$priv->size)));
my $x80 = 80 - ($dkh =~m{\n([^\n]+)\z} && length($1));
while ($data ne '') {
$dkh .= substr($data,0,$x80,'') if $x80>10;
$dkh .= "\r\n " if $data ne '';
$x80 = 80;
}
$dkh .= "\r\n";
return $dkh;
}
# Verify a DKIM signature (hash from parse_signature) using a DKIM key (hash
# from parse_dkimkey). Output is (error_code,error_string) or simply
# (DKIM_SUCCESS) in case of no error.
sub _verify_sig {
my ($sig,$param) = @_;
return (DKIM_PERMFAIL,"none or invalid dkim record") if ! %$param;
return (DKIM_TEMPFAIL,$param->{tempfail}) if $param->{tempfail};
return (DKIM_PERMFAIL,$param->{permfail}) if $param->{permfail};
my $FAIL = $param->{t}{y} ? DKIM_SOFTFAIL : DKIM_PERMFAIL;
return ($FAIL,"key revoked") if ! $param->{p};
return ($FAIL,"hash algorithm not allowed")
if ! $param->{h}{$sig->{'a:hash'}};
return ($FAIL,"identity does not match domain") if $param->{t}{s}
&& $sig->{'i:domain'} && $sig->{'i:domain'} ne $sig->{d};
return ($FAIL,"identity does not match granularity")
if $param->{g} && $sig->{i} !~ $param->{g};
# pre-computed hash over body
return if ! defined $sig->{'bh:computed'}; # not yet computed
if ($sig->{'bh:computed'} ne $sig->{'bh:bin'}) {
return ($FAIL,'body hash mismatch');
}
my $rsa = Crypt::OpenSSL::RSA->new_public_key(do {
local $_ = $param->{p};
s{\s+}{}g;
s{(.{1,64})}{$1\n}g;
"-----BEGIN PUBLIC KEY-----\n$_-----END PUBLIC KEY-----\n";
});
$rsa or return ($FAIL,"using public key failed");
$rsa->use_no_padding;
my $bencrypt = $rsa->encrypt($sig->{'b:bin'});
my $expect = _emsa_pkcs1_v15(
$sig->{'a:hash'},$sig->{'h:hash'},$rsa->size);
if ($expect ne $bencrypt) {
# warn "expect= "._encode64($expect)."\n";
# warn "encrypt="._encode64($bencrypt)."\n";
return ($FAIL,'header sig mismatch');
}
return (DKIM_SUCCESS, join(' + ', @{$sig->{':warning'} || []}));
}
# parse the header and extract
sub _parse_header {
my $hdr = shift;
my %all_critical = map { $_ => 0 } @critical_headers;
$all_critical{lc($_)}-- for $hdr =~m{^($critical_headers_rx):}mig;
my @sig;
while ( $hdr =~m{^(DKIM-Signature:\s*(.*\n(?:[ \t].*\n)*))}mig ) {
my $dkh = $1; # original value to exclude it when computing hash
my $error;
my $sig = parse_signature($2,\$error);
if ($sig) {
$sig->{'h:hash'} = _compute_hdrhash($hdr,
$sig->{'h:list'},$sig->{'a:hash'},$sig->{'c:hdr'},$dkh);
my %critical = %all_critical;
$critical{$_}++ for @{$sig->{'h:list'}};
if (my @h = grep { $critical{$_} < 0 } keys %critical) {
push @{$sig->{':warning'}},
"unprotected critical header ".join(",",sort @h);
}
} else {
$sig = { error => "invalid DKIM-Signature header: $error" };
}
push @sig,$sig;
}
return @sig;
}
{
# EMSA-PKCS1-v1_5 encapsulation, see RFC 3447 9.2
my %sig_prefix = (
'sha1' => pack("H*","3021300906052B0E03021A05000414"),
'sha256' => pack("H*","3031300d060960864801650304020105000420"),
);
sub _emsa_pkcs1_v15 {
my ($algo,$hash,$len) = @_;
my $t = ($sig_prefix{$algo} || die "unsupport digest $algo") . $hash;
my $pad = $len - length($t) -3;
$pad < 8 and die;
return "\x00\x01" . ("\xff" x $pad) . "\x00" . $t;
}
}
{
# simple header canonicalization:
my $simple_hdrc = sub {
my $line = shift;
$line =~s{(?<!\r)\n}{\r\n}g; # normalize line end
return $line;
};
# relaxed header canonicalization:
my $relaxed_hdrc = sub {
my ($k,$v) = shift() =~m{\A([^:]+:[ \t]*)?(.*)\z}s;
$v =~s{\r?\n([ \t])}{$1}g; # unfold lines
$v =~s{[ \t]+}{ }g; # WSP+ -> SP
$v =~s{\s+\z}{\r\n}; # eliminate all WS from end, normalize line end
$k = lc($k||''); # lower case key
$k=~s{[ \t]*:[ \t]*}{:}; # remove white-space around colon
return $k.$v;
};
my %hdrc = (
simple => $simple_hdrc,
relaxed => $relaxed_hdrc,
);
use Digest::SHA;
my %digest = (
sha1 => sub { Digest::SHA->new(1) },
sha256 => sub { Digest::SHA->new(256) },
);
# compute the hash over the header
sub _compute_hdrhash {
my ($hdr,$headers,$hash,$canon,$dkh) = @_;
#warn "XXX $hash | $canon";
$hash = $digest{$hash}();
$canon = $hdrc{$canon};
my @hdr;
my %kv;
for my $k (@$headers) {
if ($k eq 'dkim-signature') {
for($hdr =~m{^($k:[^\n]*\n(?:[ \t][^\n]*\n)*)}mig) {
$_ eq $dkh and next;
push @hdr,$_;
}
} else {
my $v = $kv{$k} ||=
[ $hdr =~m{^($k:[^\n]*\n(?:[ \t][^\n]*\n)*)}mig ];
# take last matching kv in mail header
push @hdr, pop(@$v) // '';
}
}
$dkh =~s{([ \t;:]b=)([a-zA-Z0-9/+= \t\r\n]+)}{$1};
$dkh =~s{[\r\n]+\z}{};
push @hdr,$dkh;
$_ = $canon->($_) for (@hdr);
#warn Dumper(\@hdr); use Data::Dumper;
$hash->add(@hdr);
return $hash->digest;
}
# simple body canonicalization:
# - normalize to \r\n line end
# - remove all empty lines at the end
# - make sure that body consists at least of a single empty line
# relaxed body canonicalization:
# - like simple, but additionally...
# - remove any white-space at the end of a line (excluding \r\n)
# - compact any white-space inside the line to a single space
my $bodyc = sub {
my $relaxed = shift;
my $empty = my $no_line_yet = '';
my $realdata;
sub {
my $data = shift;
if ($data eq '') {
return $no_line_yet if $realdata;
return "\r\n";
}
my $nl = rindex($data,"\n");
if ($nl == -1) {
$no_line_yet .= $data;
return '';
}
if ($nl == length($data)-1) {
# newline at end of data
$data = $no_line_yet . $data if $no_line_yet ne '';
$no_line_yet = '';
} else {
# newline somewhere inside
$no_line_yet .= substr($data,0,$nl+1,'');
($data,$no_line_yet) = ($no_line_yet,$data);
}
$data =~s{(?<!\r)\n}{\r\n}g; # normalize line ends
if ($relaxed) {
$data =~s{[ \t]+}{ }g; # compact WSP+ to SP
$data =~s{ \r\n}{\r\n}g; # remove WSP+ at eol
}
if ($data =~m{(^|\n)(?:\r\n)+\z}) {
if (!$+[1]) {
# everything empty
$empty .= $data;
return '';
} else {
# part empty
$empty .= substr($data,0,$+[1],'');
($empty,$data) = ($data,$empty);
}
} else {
# nothing empty
if ($empty ne '') {
$data = $empty . $data;
$empty = '';
}
}
$realdata = 1;
return $data;
};
};
my %bodyc = (
simple => sub { $bodyc->(0) },
relaxed => sub { $bodyc->(1) },
);
# add data to the body
sub _append_body {
my ($self,$buf) = @_;
my $bh = $self->{_bodyhash} ||= do {
my @bh;
for(@{$self->{sig}}) {
if (!$_->{error} and
my $digest = $digest{$_->{'a:hash'}}() and
my $transform = $bodyc{$_->{'c:body'}}()
) {
push @bh, {
digest => $digest,
transform => $transform,
$_->{l} ? (l => $_->{l}) :
defined($_->{l}) ? (l => \$_->{l}) : # capture l
(),
};
} else {
push @bh, { done => 1 };
}
}
\@bh;
};
my $i=-1;
for(@$bh) {
$i++;
$_->{done} and next;
if ($buf eq '') {
$_->{done} = 1;
goto compute_signature;
}
my $tbuf = $_->{transform}($buf);
$tbuf eq '' and next;
{
defined $_->{l} or last;
if (ref $_->{l}) {
${$_->{l}} += length($tbuf);
next;
}
if ($_->{l} > 0) {
last if ($_->{l} -= length($tbuf))>0;
$_->{_data_after_l} ||=
substr($tbuf,$_->{l},-$_->{l},'') =~m{\S} & 1;
$_->{l} = 0;
} else {
$_->{_data_after_l} ||= $tbuf =~m{\S} & 1;
$tbuf = '';
}
$_->{done} = 1;
}
$_->{digest}->add($tbuf) if $tbuf ne '';
$_->{done} or next;
compute_signature:
$self->{sig}[$i]{'bh:computed'} = $_->{digest}->digest;
push @{$self->{sig}[$i]{':warning'}}, 'data after signed body'
if $_->{_data_after_l};
}
delete $self->{_bodyhash}
if @$bh == grep { $_->{done} } @$bh; # done with all
}
}
{
# parse_taglist($val,\$error)
# Parse a tag-list, like in the DKIM signature and in the DKIM key.
# Returns a hash of the parsed list. If error occur $error will be set and
# undef will be returned.
my $fws = qr{
[ \t]+ (?:\r?\n[ \t]+)? |
\r?\n[ \t]+
}x;
my $tagname = qr{[a-z]\w*}i;
my $tval = qr{[\x21-\x3a\x3c-\x7e]+};
my $tagval = qr{$tval(?:$fws$tval)*};
my $end = qr{(?:\r?\n)?\z};
my $delim_or_end = qr{ $fws? (?: $end | ; (?: $fws?$end|)) }x;
sub parse_taglist {
my ($v,$error) = @_;
my %v;
while ( $v =~m{\G $fws? (?:
($tagname) $fws?=$fws? ($tagval?) $delim_or_end |
| (.+)
)}xgcs) {
if (defined $3) {
$$error = "invalid data at end: '$3'";
return;
}
last if ! defined $1;
exists($v{$1}) && do {
$$error = "duplicate key $1";
return;
};
$v{$1} = $2;
}
#warn Dumper(\%v); use Data::Dumper;
return \%v;
}
}
sub _encode64 {
my $data = shift;
my $pad = ( 3 - length($data) % 3 ) % 3;
$data = pack('u',$data);
$data =~s{(^.|\n)}{}mg;
$data =~tr{` -_}{AA-Za-z0-9+/};
substr($data,-$pad) = '=' x $pad if $pad;
return $data;
}
sub _decode64 {
my $data = shift;
$data =~s{\s+}{}g;
$data =~s{=+$}{};
$data =~tr{A-Za-z0-9+/}{`!-_};
$data =~s{(.{1,60})}{ chr(32 + length($1)*3/4) . $1 . "\n" }eg;
return unpack("u",$data);
}
sub _encodeQP {
(my $data = shift)
=~s{([^\x21-\x3a\x3c\x3e-\x7e])}{ sprintf('=%02X',ord($1)) }esg;
return $data;
}
sub _decodeQP {
my $data = shift;
$data =~s{\s+}{}g;
$data =~s{=([0-9A-F][0-9A-F])}{ chr(hex($1)) }esg;
return $data;
}
# ResultRecord for verification.
package Mail::DKIM::Iterator::VerifyRecord;
sub new {
my $class = shift;
bless [@_],$class;
}
sub sig { shift->[0] }
sub domain { shift->[0]{d} }
sub dnsname { shift->[1] }
sub status { shift->[2] }
sub error { $_[0]->[2] >0 ? undef : $_[0]->[3] }
sub warning { $_[0]->[2] >0 ? $_[0]->[3] : undef }
sub authentication_results {
my $self = shift;
my $ar = "dkim=$self->[2]";
$ar .= " ($self->[3])" if defined $self->[3] and $self->[3] ne '';
$ar .= " header.d=".$self->[0]{d};
return $ar;
}
# ResultRecord for signing.
package Mail::DKIM::Iterator::SignRecord;
sub new {
my $class = shift;
bless [@_],$class;
}
sub sig { shift->[0] }
sub domain { shift->[0]{d} }
sub dnsname {
my $sig = shift->[0];
return ($sig->{s} || 'UNKNOWN').'_domainkey'.($sig->{d} || 'UNKNOWN');
}
sub signature { shift->[1] }
sub status { shift->[2] }
sub error { shift->[3] }
1;
__END__
=head1 NAME Mail::DKIM::Iterator
Iterativ validation of DKIM records or DKIM signing of mails.
=head1 SYNOPSIS
# ---- Verify all DKIM signature headers found within a mail --------------
my $mailfile = $ARGV[0];
use Mail::DKIM::Iterator;
use Net::DNS;
my %dnscache;
my $res = Net::DNS::Resolver->new;
# Create a new Mail::DKIM::Iterator object.
# Feed parts from the mail and results from DNS lookups into the object
# until we have the final result.
open( my $fh,'<',$mailfile) or die $!;
my $dkim = Mail::DKIM::Iterator->new(dns => \%dnscache);
my $rv;
my @todo = \'';
while (@todo) {
my $todo = shift(@todo);
if (ref($todo)) {
# need more data from mail
if (read($fh,$buf,8192)) {
($rv,@todo) = $dkim->next($buf);
} else {
($rv,@todo) = $dkim->next('');
}
} else {
# need a DNS lookup
if (my $q = $res->query($todo,'TXT')) {
# successful lookup
($rv,@todo) = $dkim->next({
$todo => [
map { $_->type eq 'TXT' ? ($_->txtdata) : () }
$q->answer
]
});
} else {
# failed lookup
($rv,@todo) = $dkim->next({ $todo => undef });
}
}
}
# This final result consists of a VerifyRecord for each DKIM signature
# in the header, which provides access to the status. Status is one of
# of DKIM_SUCCESS, DKIM_PERMFAIL, DKIM_TEMPFAIL, DKIM_SOFTFAIL or
# DKIM_INVALID_HDR. In case of error $record->error contains a string
# representation of the error.
for(@$rv) {
my $status = $_->status;
my $name = $_->domain;
if (!defined $status) {
print STDERR "$mailfile: $name UNKNOWN\n";
} elsif ($status == DKIM_SUCCESS) {
# fully validated
print STDERR "$mailfile: $name OK ".$_->warning".\n";
} elsif ($status == DKIM_PERMFAIL) {
# hard error
print STDERR "$mailfile: $name FAIL ".$_->error."\n";
} else {
# soft-fail, temp-fail, invalid-header
print STDERR "$mailfile: $name $status ".$_->error."\n";
}
}
# ---- Create signature for a mail ----------------------------------------
my $mailfile = $ARGV[0];
use Mail::DKIM::Iterator;
my $dkim = Mail::DKIM::Iterator->new(sign => {
c => 'relaxed/relaxed',
a => 'rsa-sha1',
d => 'example.com',
s => 'foobar',
':key' => ... PEM string for private key or Crypt::OpenSSL::RSA object
});
open(my $fh,'<',$mailfile) or die $!;
my $rv;
my @todo = \'';
while (@todo) {
my $todo = shift @todo;
die "DNS lookups should not be needed here" if !ref($todo);
# need more data from mail
if (read($fh,$buf,8192)) {
($rv,@todo) = $dkim->next($buf);
} else {
($rv,@todo) = $dkim->next('');
}
}
for(@$rv) {
my $status = $_->status;
my $name = $_->domain;
if (!defined $status) {
print STDERR "$mailfile: $name UNKNOWN\n";
} elsif (status != DKIM_SUCCESS) {
print STDERR "$mailfile: $name $status - ".$_->error."\n";
} else {
# show signature
print $_->signature;
}
}
=head1 DESCRIPTION
With this module one can validate DKIM Signatures in mails and also create DKIM