DKIM/PrivateKey.pm

filter: Statementstmt 83.0% 83 / 100 Branchbran 63.1% 24 / 38 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 86.6% 13 / 15 Podpod 40.0% 2 / 5 totaltotal 77.6% 125 / 161 CC 21 SCAR 14.5
CC
21
Cov
78%
load
8.3
convert
4.6
_convert_ed25519
4.2
CRAP 4.3
« DKIM/Signature.pm ↑ Summary DKIM/Canonicalization/DkimCommon.pm »
1 package Mail::DKIM::PrivateKey;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 our $VERSION = '1.20261116'; # 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 <jlong@messiah.edu>
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 12 ▶use base 'Mail::DKIM::Key';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 *calculate_EM = \&Mail::DKIM::Key::calculate_EM;
18 12 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use Crypt::PK::Ed25519;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 58 ▶sub load {
Subroutine load: called CC=8 SCAR=21.1
Pod documented
23 58 my $class = shift;
24 58 my %prms = @_;
25  
26 58 my $self = bless {}, $class;
27  
28 58 ▶ $self->{'TYPE'} = ( $prms{'Type'} or 'rsa' );
Condition100%2/2
$prms{'Type'} || 'rsa'
l!l
2434
MC/DC100%1/1
$prms{'Type'} || 'rsa'
$prms{'Type'}
Truth table100%2/2
A
A= $prms{'Type'}
Aresult
11
00
29  
30 58 ▶ if ( $prms{'Data'} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($prms{'Data'}) { } 17 41 58
elsif (defined $prms{'File'}) { } 41 0 41
elsif ($prms{'Cork'}) { } 0 0 0
31 17 $self->{'DATA'} = $prms{'Data'};
32 }
33 elsif ( defined $prms{'File'} ) {
34 41 my @data;
35 41 ▶ open my $file, '<', $prms{'File'}
Branch: 1 branch
BranchTrueFalseTotal
open my $file, "<", $prms{"File"} or die qq[Error: cannot read $prms{"File"}: $!\n] 39 2 41
36 or die "Error: cannot read $prms{File}: $!\n";
37 39 ▶ while ( my $line = <$file> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $file)) 561 39 600
38 561 chomp $line;
39 561 ▶ next if $line =~ /^---/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^---/ 78 483 561
40 483 push @data, $line;
41 }
42 39 $self->{'DATA'} = join '', @data;
43 39 close $file;
44 }
45 elsif ( $prms{'Cork'} ) {
46 0 $self->{'CORK'} = $prms{'Cork'};
47 }
48 else {
49 0 croak 'missing required argument';
50 }
51  
52 56 return $self;
53 }
54  
55  
56 41 ▶sub _convert_rsa {
Subroutine _convert_rsa: called CC=4 SCAR=14.1
57 41 my $self = shift;
58  
59 # have to PKCS1ify the privkey because openssl is too finicky...
60 41 my $pkcs = "-----BEGIN RSA PRIVATE KEY-----\n";
61  
62 41 ▶ for ( my $i = 0 ; $i < length $self->data ; $i += 64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $self->data) 533 41 574
63 533 $pkcs .= substr $self->data, $i, 64;
64 533 $pkcs .= "\n";
65 }
66  
67 41 $pkcs .= "-----END RSA PRIVATE KEY-----\n";
68  
69 41 my $cork;
70  
71 eval {
72 41 local $SIG{__DIE__};
73 41 $cork = new_private_key Crypt::OpenSSL::RSA($pkcs);
74 41 1
75 41 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; $cork = "Crypt::OpenSSL::RSA"->new_private_key($pkcs); 1 } } or do { $self->errorstr($@); return } 41 0 41
76 0 $self->errorstr($@);
77 0 return;
78 };
79  
80 41 ▶ $cork
Branch: 1 branch
BranchTrueFalseTotal
$cork or return 41 0 41
81 or return;
82  
83 # segfaults on my machine
84 # $cork->check_key or
85 # return;
86  
87 41 $self->cork($cork);
88 41 return 1;
89 }
90  
91 2 ▶sub _convert_ed25519 {
Subroutine _convert_ed25519: called CC=4 SCAR=14.3
92 2 my $self = shift;
93 2 my $cork;
94  
95 eval {
96 2 local $SIG{__DIE__};
97 2 $cork = new Crypt::PK::Ed25519;
98  
99 # Prepend/append with PEM boilerplate
100 2 my $pem = "-----BEGIN PRIVATE KEY-----\n";
101 2 $pem .= $self->data;
102 2 $pem .= "\n";
103 2 $pem .= "-----END PRIVATE KEY-----\n";
104  
105 # Pass PEM text buffer
106 2 ▶ $cork->import_key(\$pem)
Branch: 1 branch
BranchTrueFalseTotal
$cork->import_key(\$pem) or die "failed to load Ed25519 private key" 2 0 2
107 or die 'failed to load Ed25519 private key';
108  
109 # Alternatively, import_raw_key() could be used,
110 # but requires the 32-byte key, which must be extracted
111 # from the ASN.1 structure first.
112  
113 2 1
114 2 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; $cork = "Crypt::PK::Ed25519"->new; my $pem = "-----BEGIN PRIVATE KEY-----\n"; $pem .= $self->data; $pem .= "\n"; $pem .= "-----END PRIVATE KEY-----\n"; $cork->import_key(\$pem) or die "failed to load Ed25519 private key"; 1 } } or do { $self->errorstr($@); return } 2 0 2
115 0 $self->errorstr($@);
116 0 return;
117 };
118  
119 2 ▶ $cork
Branch: 1 branch
BranchTrueFalseTotal
$cork or return 2 0 2
120 or return;
121  
122 2 $self->cork($cork);
123 2 return 1;
124 }
125  
126 43 ▶sub convert {
Subroutine convert: called CC=4 SCAR=15.2
Pod undocumented
127 43 my $self = shift;
128  
129 43 ▶ $self->data
Branch: 1 branch
BranchTrueFalseTotal
$self->data or return 43 0 43
130 or return;
131  
132 43 ▶ return $self->_convert_rsa if $self->{TYPE} eq 'rsa';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"TYPE"} eq "rsa" 41 2 43
133 2 ▶ return $self->_convert_ed25519 if $self->{TYPE} eq 'ed25519';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"TYPE"} eq "ed25519" 2 0 2
134 0 self->errorstr('unsupported key type');
135 0 return;
136 }
137  
138 #deprecated
139 0 ▶sub sign {
Subroutine sign: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my $self = shift;
141 0 my $mail = shift;
142  
143 0 return $self->cork->sign($mail);
144 }
145  
146 #deprecated- use sign_digest() instead
147 0 ▶sub sign_sha1_digest {
Subroutine sign_sha1_digest: not called CC=1 SCAR=6.9
Pod undocumented
148 0 my $self = shift;
149 0 my ($digest) = @_;
150 0 return $self->sign_digest( 'SHA-1', $digest );
151 }
152  
153  
154 71 ▶sub _sign_digest_rsa {
Subroutine _sign_digest_rsa: called CC=1 SCAR=0.0
155 71 my $self = shift;
156 71 my ( $digest_algorithm, $digest ) = @_;
157  
158 71 my $rsa_priv = $self->cork;
159 71 $rsa_priv->use_no_padding;
160 71 my $k = $rsa_priv->size;
161 71 my $EM = calculate_EM( $digest_algorithm, $digest, $k );
162 71 return $rsa_priv->decrypt($EM);
163 }
164  
165 2 ▶sub _sign_digest_ed25519 {
Subroutine _sign_digest_ed25519: called CC=3 SCAR=13.5
166 2 my $self = shift;
167 2 my ( $digest_algorithm, $digest ) = @_;
168  
169 2 my $ed = $self->cork;
170 2 ▶ if ( !$ed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ed) 0 2 2
171 0 ▶ $@ = $@ ne '' ? "Ed25519 failed: $@" : 'Ed25519 unknown problem';
Branch: 1 branch
BranchTrueFalseTotal
$@ ne '' ? : 0 0 0
172 0 die;
173 }
174 2 return $ed->sign_message($digest);
175 }
176  
177 73 ▶sub sign_digest {
Subroutine sign_digest: called CC=3 SCAR=11.3
Pod documented
178 73 my $self = shift;
179 73 my ( $digest_algorithm, $digest ) = @_;
180  
181 73 ▶ return $self->_sign_digest_rsa($digest_algorithm, $digest) if $self->{TYPE} eq 'rsa';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"TYPE"} eq "rsa" 71 2 73
182 2 ▶ return $self->_sign_digest_ed25519($digest_algorithm, $digest) if $self->{TYPE} eq 'ed25519';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"TYPE"} eq "ed25519" 2 0 2
183 0 die 'unsupported key type';
184 }
185  
186 __END__
« DKIM/Signature.pm ↑ Summary DKIM/Canonicalization/DkimCommon.pm »