lib/VOMS/Lite/PEMHelper.pm

filter: Statementstmt 69.4% 161 / 232 Branchbran 47.1% 49 / 104 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 10 totaltotal 60.9% 223 / 366 CC 57 SCAR 43.8
CC
57
Cov
62%
readPrivateKey
212.5
readCert
12.9
writeKey
11.1
CRAP 79.9
« lib/VOMS/Lite/CertKeyHelper.pm ↑ Summary lib/VOMS/Lite/RSAKey.pm »
1 package VOMS::Lite::PEMHelper;
2  
3 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MIME::Base64 qw(encode_base64 decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Copy qw(move);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 require Exporter;
9 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 @ISA = qw(Exporter);
11 1 %EXPORT_TAGS = ( );
12 1 @EXPORT_OK = qw( encodeCert writeAC encodeAC readAC readCert decodeCert writeKey writeCert writeCertKey readPrivateKey );
13 1 @EXPORT = ( );
14 1 $VERSION = '0.20';
15  
16 ################################################################
17  
18 1 ▶sub writeAC { #writes a PEM formatted AC
Subroutine writeAC: called CC=3 SCAR=11.6
Pod undocumented
19 # Two arguments (Path to store AC and AC data as a string of chars)
20 1 my ($file,$data)=@_;
21 # my $umasksave=umask(0022); #ACs are not private key material
22 # if ( umask() != 0022 ) { die "Can't umask 0022\n"; }
23 0 ▶ if ( -e $file ) { move($file,"$file.old"); } #move old file away
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 1 1
24 1 ▶ open(AC,">$file") || die "Can't create AC file";
Branch: 1 branch
BranchTrueFalseTotal
open AC, ">$file" or die "Can't create AC file" 1 0 1
25 1 print AC &encodeAC($data);
26 1 close(AC);
27 # umask($umasksave);
28 1 return;
29 }
30  
31 ################################################################
32  
33 2 ▶sub encodeAC {
Subroutine encodeAC: called CC=1 SCAR=0.0
Pod undocumented
34 2 return encodeCert(@_,"ATTRIBUTE CERTIFICATE");
35 }
36  
37 ################################################################
38  
39 0 ▶sub readAC { #Returns BER with AC in it
Subroutine readAC: not called CC=1 SCAR=6.9
Pod undocumented
40 0 my $file=shift;
41 0 return readCert($file,"ATTRIBUTE CERTIFICATE");
42 }
43  
44 ################################################################
45  
46 2 ▶sub readCert { #Returns BERs with CERTs in them
Subroutine readCert: called CC=12 SCAR=25.6
Pod undocumented
47 # One arguement (path to cert file);
48 2 my $file=shift;
49  
50 2 my $type=shift;
51 2 ▶ if ( ! defined($type) ) { $type="CERTIFICATE"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 2 0 2
52 2 $type =~ y/a-z/A-Z/;
53 2 $type =~ s/[^A-Z0-9 ]//g;
54  
55 # Load and parse cert file
56 2 my @myCertData=();
57 2 my $Certnum=-1;
58 2 my $read=0;
59 2 ▶ open(CERT,"<$file") || die "Can't access Public Key file: '$file'";
Branch: 1 branch
BranchTrueFalseTotal
open CERT, "<$file" or die "Can't access Public Key file: '${file}'" 2 0 2
60 2 ▶ while (<CERT>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline CERT)) 29 0 29
61 29 my $line=$_;
62 29 ▶ if ( $line =~ /^-----BEGIN $type-----\r?$/ ) {$read=1; $Certnum++; next;}
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^-----BEGIN $type-----\r?$/) 2 27 29
63 27 ▶ if ( $line =~ /^-----END $type-----\r?$/ ) {$read=0; wantarray ? next : last; }
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^-----END $type-----\r?$/) 2 25 27
wantarray ? : 0 2 2
64 25 ▶ if ( $read==1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read == 1) 25 0 25
65 25 ▶ if ( $line =~ /^([A-Za-z0-9+\/=]+)\r?$/ ) {$myCertData[$Certnum].=$1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^([A-Za-z0-9+/=]+)\r?$]) 25 0 25
66 }
67 }
68 2 close(CERT);
69  
70 0 ▶ if ( $myCertData[0] eq "" ) { die "I didn't understand the format of your $type file:\n$file";}
Branch: 1 branch
BranchTrueFalseTotal
if ($myCertData[0] eq "") 0 2 2
71 2 my @decoded=();
72 2 foreach (@myCertData) { push(@decoded,decode_base64($_)); }
73 2 ▶ return wantarray?@decoded:$decoded[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
74 }
75  
76 ################################################################
77  
78 0 ▶sub decodeCert {
Subroutine decodeCert: not called CC=1 SCAR=6.9
Pod undocumented
79 0 my $type = pop;
80 0 my $pems = join "\n",@_;
81 0 my @ders;
82 0 $pems =~ s|^-----BEGIN $type-----$([a-zA-Z0-9/+=\r\n]+)^-----END $type-----$|push @ders,decode_base64($1)|mge;
83 0 return @ders;
84 }
85  
86 ################################################################
87  
88 5 ▶sub encodeCert {
Subroutine encodeCert: called CC=3 SCAR=11.0
Pod undocumented
89 #my $certstr=shift;
90 5 my $certstr="";
91 5 my $type="CERTIFICATE";
92 5 ▶ if ( $_[-1] !~ /^\x30/ ) { $type=pop; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[-1] =~ /^\x30/) 5 0 5
93
94 5 $type =~ y/a-z/A-Z/;
95 5 $type =~ s/[^A-Z0-9 ]//g;
96 5 foreach (@_) {
97 5 my $OpenSSLCompat=encode_base64($_,'');
98 5 $OpenSSLCompat=~s/(.{1,64})/$&\n/g;
99 5 $certstr .= "-----BEGIN $type-----\n".$OpenSSLCompat."-----END $type-----\n";
100 }
101 5 return $certstr;
102 }
103  
104 ################################################################
105  
106 1 ▶sub writeCertKey {
Subroutine writeCertKey: called CC=6 SCAR=20.1
Pod undocumented
107 # At least 3 arguements (file, public key, private key, [chain of signing certificates]);
108 1 my $file=shift;
109 1 my $pub=shift;
110 1 my $pri=shift;
111  
112 # Place file
113 1 my $umasksave=umask(0077);
114
115 1 ▶ if ( umask() != 0077 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (umask() != 63) 0 1 1
116 0 ▶ if ( $^O =~ /^MSWin/ ) { print STDERR "WARNING: Can't umask 0077 when writing $file\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /^MSWin/) { } 0 0 0
117 0 else { die "Can't umask 0077 when writing $file"; }
118 }
119 0 ▶ if ( -e $file ) { move($file,"$file.old"); } #move old file away
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 1 1
120  
121 1 ▶ open(CERTKEY,">$file") || die "Can't create file to save cert and key to.";
Branch: 1 branch
BranchTrueFalseTotal
open CERTKEY, ">$file" or die "Can't create file to save cert and key to." 1 0 1
122 1 print CERTKEY encodeCert($pub,"CERTIFICATE");
123 1 print CERTKEY encodeCert($pri,"RSA PRIVATE KEY");
124 1 foreach ( @_ ) { print CERTKEY encodeCert($_,"CERTIFICATE"); }
125 1 close(CERTKEY);
126 1 umask($umasksave);
127 1 return;
128 }
129  
130 ################################################################
131  
132 3 ▶sub writeKey {
Subroutine writeKey: called CC=10 SCAR=24.1
Pod undocumented
133 # At least 3 arguements (file, public key, private key, [chain of signing certificates]);
134 3 my $file=shift;
135 3 my $pri=shift;
136 3 my $passwd=shift;
137 3 my $ENCRYPTION="";
138  
139 3 ▶ if ( ! defined $passwd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $passwd) 0 3 3
140 # Prompt for password
141 0 require Term::ReadKey;
142 0 print "I need the passphrase used to encrypt the key in \n$file\nPassphrase: ";
143 0 my $dummy=Term::ReadKey::ReadMode('noecho');
144 0 $passwd = Term::ReadKey::ReadLine(),
145 $dummy=Term::ReadKey::ReadMode('normal');
146 0 chomp $passwd;
147 0 print "\n";
148 }
149  
150 # To encrypt or not to encrypt
151 3 ▶ if ( $passwd ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($passwd ne "") 2 1 3
152  
153 # Spin up the Crypto stuff
154 2 require Digest::MD5;
155 2 require Crypt::DES_EDE3;
156  
157 # Make Initialisation vector
158 2 my $iv="";
159 2 ▶ while (length($iv)<8 ) {$iv.=chr((rand(255)+1));}
Branch: 1 branch
BranchTrueFalseTotal
if (length $iv < 8) 16 2 18
160  
161 # Construct DES Key from password (Munge)
162 2 my $keysize=24;
163 2 my $SALT=$iv;
164 2 my $key=Digest::MD5::md5($passwd,$SALT);
165 2 ▶ while (length($key) < $keysize) { $key .= Digest::MD5::md5($key, $passwd, $SALT);}
Branch: 1 branch
BranchTrueFalseTotal
if (length $key < $keysize) 2 2 4
166 2 $key=substr($key,0,$keysize);
167  
168 # DES Padding Data as per RFC 1423 (not 1851 which adds message payload info)
169 2 my $pad = ( 8 - (length($pri)%8) );
170 2 my $padding=chr($pad) x $pad;
171 2 $pri.=$padding;
172  
173 # Encode Data
174 2 my $DES = Crypt::DES_EDE3->new($key);
175 2 my $cyphertextout="";
176 2 ▶ while ( my $len=length($pri) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = length $pri) 80 2 82
177 80 my $block=substr($pri,0,8);
178 80 $pri=substr($pri,8);
179 80 $block = $SALT ^ $block;
180 80 my $cyphertext=$DES->encrypt($block);
181 80 $SALT=$cyphertext;
182 80 $cyphertextout.=$cyphertext;
183 }
184  
185 # Set PEM encryprion header
186 2 $iv=unpack('H*',$iv);
187 2 $iv =~ y/[a-f]/[A-F]/;
188 2 $ENCRYPTION="Proc-Type: 4,ENCRYPTED\nDEK-Info: DES-EDE3-CBC,$iv\n\n";
189 2 $pri=$cyphertextout;
190 }
191  
192 # Place file
193 3 my $umasksave=umask(0077);
194  
195 3 ▶ if ( umask() != 0077 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (umask() != 63) 0 3 3
196 0 ▶ if ( $^O =~ /^MSWin/ ) { print STDERR "WARNING: Can't umask 0077 when writing $file\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /^MSWin/) { } 0 0 0
197 0 else { die "Can't umask 0077 when writing $file"; }
198 }
199 0 ▶ if ( -e $file ) { move($file,"$file.old"); } #move old file away
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 3 3
200  
201 3 ▶ open(KEY,">$file") || die "Can't create file to save cert and key to.";
Branch: 1 branch
BranchTrueFalseTotal
open KEY, ">$file" or die "Can't create file to save cert and key to." 3 0 3
202 3 my $OpenSSLCompat=encode_base64($pri,'');
203 3 $OpenSSLCompat=~s/(.{1,64})/$&\n/g;
204 3 print KEY "-----BEGIN RSA PRIVATE KEY-----\n$ENCRYPTION".$OpenSSLCompat."-----END RSA PRIVATE KEY-----\n";
205 3 close(KEY);
206 3 umask($umasksave);
207 3 return;
208 }
209  
210 ################################################################
211  
212 3 ▶sub writeCert {
Subroutine writeCert: called CC=4 SCAR=14.1
Pod undocumented
213 # At least 3 arguements (file, public key, private key, [chain of signing certificates]);
214 3 my $file=shift;
215 3 my $pub=shift;
216 3 my $type=shift;
217 3 ▶ if ( ! defined($type) ) { $type="CERTIFICATE"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 3 0 3
218 3 $type =~ y/a-z/A-Z/;
219 3 $type =~ s/[^A-Z0-9 ]//g;
220  
221 # Place file
222 0 ▶ if ( -e $file ) { move($file,"$file.old"); } #move old file away
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 3 3
223 3 ▶ open(CERT,">$file") || die "Can't create file to save cert and key to.";
Branch: 1 branch
BranchTrueFalseTotal
open CERT, ">$file" or die "Can't create file to save cert and key to." 3 0 3
224 3 my $OpenSSLCompat=encode_base64($pub,'');
225 3 $OpenSSLCompat=~s/(.{1,64})/$&\n/g;
226 3 print CERT "-----BEGIN $type-----\n".$OpenSSLCompat."-----END $type-----\n";
227 3 close(CERT);
228 3 return;
229 }
230  
231  
232 ################################################################
233  
234 1 ▶sub readPrivateKey { #Returns BER with Private key in it
Subroutine readPrivateKey: called CC=25 SCAR=53.6
Pod undocumented
235 # Two arguements (path to private key, and optional password);
236 1 my $file=shift;
237 1 my $passwd=shift;
238  
239 # Load and parse private key file
240 1 my ($myKeyData,$PEMV,$PEMType,$PEMEnc,$SALT)=("","","","","");
241 1 my $read=0;
242 1 ▶ open(KEY,"<$file") || die "Can't access Private Key file $file";
Branch: 1 branch
BranchTrueFalseTotal
open KEY, "<$file" or die "Can't access Private Key file $file" 1 0 1
243 1 ▶ while (<KEY>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline KEY)) 9 0 9
244 9 my $line=$_;
245 9 ▶ if ( $line =~ /^-----BEGIN RSA PRIVATE KEY-----$/ ) {$read=1; next;}
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^-----BEGIN RSA PRIVATE KEY-----$/) 1 8 9
246 0 ▶ if ( $line =~ /^-----BEGIN PRIVATE KEY-----$/ ) {$read=2; next;}
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^-----BEGIN PRIVATE KEY-----$/) 0 8 8
247 8 ▶ if ( $line =~ /^-----END RSA PRIVATE KEY-----$/ ) {last;}
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^-----END RSA PRIVATE KEY-----$/) 1 7 8
248 0 ▶ if ( $line =~ /^-----END PRIVATE KEY-----$/ ) {last;}
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^-----END PRIVATE KEY-----$/) 0 7 7
249 7 ▶ if ( $read==1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read == 1) 7 0 7
250 0 ▶ if ( $line =~ /^Proc-Type: ([0-9]+),(ENCRYPTED)$/ ) {$PEMV=$1; $PEMType=$2}
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^Proc-Type: ([0-9]+),(ENCRYPTED)$/) { } 0 7 7
elsif ($line =~ /^DEK-Info: (.*),(.*)$/) { } 0 7 7
elsif ($line =~ m[^([A-Za-z0-9+/=]+)$]) { } 7 0 7
251 0 elsif ( $line =~ /^DEK-Info: (.*),(.*)$/ ) {$PEMEnc=$1; $SALT=$2}
252 7 elsif ( $line =~ /^([A-Za-z0-9+\/=]+)$/ ) {$myKeyData.=$1;}
253 }
254 7 ▶ if ( $read==2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read == 2) 0 7 7
255 0 ▶ if ( $line =~ /^([A-Za-z0-9+\/=]+)$/ ) {$myKeyData.=$1;}
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^([A-Za-z0-9+/=]+)$]) 0 0 0
256 }
257 }
258 1 close(KEY);
259  
260 # Return data if it's not encrypted
261 0 ▶ if ( $myKeyData eq "" ) { die "I didn't understand the format of your key file:\n$file";}
Branch: 1 branch
BranchTrueFalseTotal
if ($myKeyData eq "") 0 1 1
262  
263 # Obtain and check Encryption values
264 1 my $cyphertext=decode_base64($myKeyData);
265  
266 # If "PRIVATE KEY" but not "RSA PRIVATE KEY" Parse into it
267 1 ▶ if ( $read == 2 ) { # Unencrypted pkcs #8
Branch: 1 branch
BranchTrueFalseTotal
if ($read == 2) 0 1 1
268 0 die "I didn't understand the format of your key file:\n$file";
269 }
270  
271 1 ▶ return $cyphertext if ( $PEMType ne "ENCRYPTED" ); # Because actually it's not encrypted.
Branch: 1 branch
BranchTrueFalseTotal
if $PEMType ne "ENCRYPTED" 1 0 1
272 0 ▶ if ( $PEMEnc ne "DES-EDE3-CBC" ) { die "I don't know how to unencrypt your key\n";}
Branch: 1 branch
BranchTrueFalseTotal
if ($PEMEnc ne "DES-EDE3-CBC") 0 0 0
273 0 ▶ if ( $SALT !~ /^[a-fA-F0-9]{16}$/ ) { die "Bad Initilisation Vector (salt)'; I can't unencrypt your key!\n";}
Branch: 1 branch
BranchTrueFalseTotal
unless ($SALT =~ /^[a-fA-F0-9]{16}$/) 0 0 0
274 0 ▶ if ( $PEMV ne "4" ) { print STDERR "Warning: I was expecting a version 4 PEM encrypted file you gave me a Version $PEMV\nFunny things may happen!\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($PEMV ne 4) 0 0 0
275  
276  
277 # Check/get password
278 0 ▶ if ( defined $passwd && $passwd eq "" ) { return undef; } #was expecting no password so abort
Branch: 2 branches
BranchTrueFalseTotal
if (defined $passwd and $passwd eq '') { } 0 0 0
elsif (not defined $passwd) { } 0 0 0
Condition0%0/3
defined $passwd and $passwd eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $passwd and $passwd eq ""
defined $passwd $passwd eq ""
Truth table0%0/3
A and B
A= defined $passwdB= $passwd eq ""
ABresult
0X0
100
111
279 elsif ( ! defined $passwd ) {
280 0 require Term::ReadKey;
281 0 require Digest::MD5;
282 0 print "I need the passphrase used to encrypt the key in \n$file\nPassphrase: ";
283 0 my $dummy=Term::ReadKey::ReadMode('noecho');
284 0 $passwd = Term::ReadKey::ReadLine(),
285 $dummy=Term::ReadKey::ReadMode('normal');
286 0 chomp $passwd;
287 0 print "\n";
288 }
289  
290 # Reconstruct DES Key from password (Munge)
291 0 my $keysize=24;
292 0 $SALT=pack('H*', $SALT);
293 0 my $key=Digest::MD5::md5($passwd,$SALT);
294 0 ▶ while (length($key) < $keysize) { $key .= Digest::MD5::md5($key, $passwd, $SALT);}
Branch: 1 branch
BranchTrueFalseTotal
if (length $key < $keysize) 0 0 0
295 0 $key=substr($key,0,$keysize);
296  
297 # Decode Data
298 0 require Crypt::DES_EDE3;
299 0 my $DES = Crypt::DES_EDE3->new($key);
300 0 my $dataout="";
301 0 ▶ while ( my $len=length($cyphertext) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = length $cyphertext) 0 0 0
302 0 my $block=substr($cyphertext,0,8);
303 0 $cyphertext=substr($cyphertext,8);
304 0 my $data=$SALT ^ $DES->decrypt($block);
305 0 $SALT=$block;
306 0 $dataout.=$data;
307 }
308  
309 # Remove DES Padding
310 0 my $unpad=substr ($dataout,-1);
311 0 ▶ if ( "$unpad" =~ /[\001-\010]/ ) { $dataout=substr($dataout,0,-ord($unpad));}
Branch: 1 branch
BranchTrueFalseTotal
if ("$unpad" =~ /[\001-\010]/) { } 0 0 0
312 0 else { die "Your passphrase didn't do it for me!\n";}
313  
314 0 return $dataout;
315 }
316  
317 1 1;
318 __END__
« lib/VOMS/Lite/CertKeyHelper.pm ↑ Summary lib/VOMS/Lite/RSAKey.pm »