line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::DKIM::PrivateKey; |
2
|
8
|
|
|
8
|
|
52
|
use strict; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
221
|
|
3
|
8
|
|
|
8
|
|
40
|
use warnings; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
338
|
|
4
|
|
|
|
|
|
|
our $VERSION = '1.20230212'; # VERSION |
5
|
|
|
|
|
|
|
# ABSTRACT: a private key loaded in memory for DKIM signing |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# Copyright 2005-2007 Messiah College. All rights reserved. |
8
|
|
|
|
|
|
|
# Jason Long |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# Copyright (c) 2004 Anthony D. Urso. All rights reserved. |
11
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
12
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
8
|
|
|
8
|
|
50
|
use base 'Mail::DKIM::Key'; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
3547
|
|
16
|
8
|
|
|
8
|
|
60
|
use Carp; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
2683
|
|
17
|
|
|
|
|
|
|
*calculate_EM = \&Mail::DKIM::Key::calculate_EM; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub load { |
21
|
43
|
|
|
43
|
1
|
43801
|
my $class = shift; |
22
|
43
|
|
|
|
|
122
|
my %prms = @_; |
23
|
|
|
|
|
|
|
|
24
|
43
|
|
|
|
|
102
|
my $self = bless {}, $class; |
25
|
|
|
|
|
|
|
|
26
|
43
|
|
50
|
|
|
243
|
$self->{'TYPE'} = ( $prms{'Type'} or 'rsa' ); |
27
|
|
|
|
|
|
|
|
28
|
43
|
100
|
|
|
|
136
|
if ( $prms{'Data'} ) { |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
29
|
17
|
|
|
|
|
48
|
$self->{'DATA'} = $prms{'Data'}; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
elsif ( defined $prms{'File'} ) { |
32
|
26
|
|
|
|
|
47
|
my @data; |
33
|
26
|
100
|
|
|
|
1254
|
open my $file, '<', $prms{'File'} |
34
|
|
|
|
|
|
|
or die "Error: cannot read $prms{File}: $!\n"; |
35
|
24
|
|
|
|
|
757
|
while ( my $line = <$file> ) { |
36
|
360
|
|
|
|
|
565
|
chomp $line; |
37
|
360
|
100
|
|
|
|
988
|
next if $line =~ /^---/; |
38
|
312
|
|
|
|
|
725
|
push @data, $line; |
39
|
|
|
|
|
|
|
} |
40
|
24
|
|
|
|
|
186
|
$self->{'DATA'} = join '', @data; |
41
|
24
|
|
|
|
|
407
|
close $file; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
elsif ( $prms{'Cork'} ) { |
44
|
0
|
|
|
|
|
0
|
$self->{'CORK'} = $prms{'Cork'}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
else { |
47
|
0
|
|
|
|
|
0
|
croak 'missing required argument'; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
41
|
|
|
|
|
257
|
return $self; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub convert { |
55
|
8
|
|
|
8
|
|
4200
|
use Crypt::OpenSSL::RSA; |
|
8
|
|
|
|
|
54603
|
|
|
8
|
|
|
|
|
3021
|
|
56
|
|
|
|
|
|
|
|
57
|
37
|
|
|
37
|
0
|
70
|
my $self = shift; |
58
|
|
|
|
|
|
|
|
59
|
37
|
50
|
|
|
|
110
|
$self->data |
60
|
|
|
|
|
|
|
or return; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# have to PKCS1ify the privkey because openssl is too finicky... |
63
|
37
|
|
|
|
|
81
|
my $pkcs = "-----BEGIN RSA PRIVATE KEY-----\n"; |
64
|
|
|
|
|
|
|
|
65
|
37
|
|
|
|
|
106
|
for ( my $i = 0 ; $i < length $self->data ; $i += 64 ) { |
66
|
481
|
|
|
|
|
890
|
$pkcs .= substr $self->data, $i, 64; |
67
|
481
|
|
|
|
|
1016
|
$pkcs .= "\n"; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
37
|
|
|
|
|
87
|
$pkcs .= "-----END RSA PRIVATE KEY-----\n"; |
71
|
|
|
|
|
|
|
|
72
|
37
|
|
|
|
|
60
|
my $cork; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
eval { |
75
|
37
|
|
|
|
|
143
|
local $SIG{__DIE__}; |
76
|
37
|
|
|
|
|
1796
|
$cork = new_private_key Crypt::OpenSSL::RSA($pkcs); |
77
|
37
|
|
|
|
|
243
|
1 |
78
|
37
|
50
|
|
|
|
60
|
} || do { |
79
|
0
|
|
|
|
|
0
|
$self->errorstr($@); |
80
|
0
|
|
|
|
|
0
|
return; |
81
|
|
|
|
|
|
|
}; |
82
|
|
|
|
|
|
|
|
83
|
37
|
50
|
|
|
|
128
|
$cork |
84
|
|
|
|
|
|
|
or return; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# segfaults on my machine |
87
|
|
|
|
|
|
|
# $cork->check_key or |
88
|
|
|
|
|
|
|
# return; |
89
|
|
|
|
|
|
|
|
90
|
37
|
|
|
|
|
146
|
$self->cork($cork); |
91
|
|
|
|
|
|
|
|
92
|
37
|
|
|
|
|
70
|
return 1; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
#deprecated |
96
|
|
|
|
|
|
|
sub sign { |
97
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
98
|
0
|
|
|
|
|
0
|
my $mail = shift; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
0
|
return $self->cork->sign($mail); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
#deprecated- use sign_digest() instead |
104
|
|
|
|
|
|
|
sub sign_sha1_digest { |
105
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
106
|
0
|
|
|
|
|
0
|
my ($digest) = @_; |
107
|
0
|
|
|
|
|
0
|
return $self->sign_digest( 'SHA-1', $digest ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub sign_digest { |
112
|
57
|
|
|
57
|
1
|
98
|
my $self = shift; |
113
|
57
|
|
|
|
|
115
|
my ( $digest_algorithm, $digest ) = @_; |
114
|
|
|
|
|
|
|
|
115
|
57
|
|
|
|
|
241
|
my $rsa_priv = $self->cork; |
116
|
57
|
|
|
|
|
250
|
$rsa_priv->use_no_padding; |
117
|
|
|
|
|
|
|
|
118
|
57
|
|
|
|
|
200
|
my $k = $rsa_priv->size; |
119
|
57
|
|
|
|
|
161
|
my $EM = calculate_EM( $digest_algorithm, $digest, $k ); |
120
|
57
|
|
|
|
|
29737
|
return $rsa_priv->decrypt($EM); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
__END__ |