| 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
| |||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | open(AC,">$file") || die "Can't create AC file"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | while (<CERT>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | 29 | my $line=$_; | |||||||||||||||||||||||||||||||
| 62 | 29 | ▶ | if ( $line =~ /^-----BEGIN $type-----\r?$/ ) {$read=1; $Certnum++; next;} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | 27 | ▶ | if ( $line =~ /^-----END $type-----\r?$/ ) {$read=0; wantarray ? next : last; } | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 64 | 25 | ▶ | if ( $read==1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 65 | 25 | ▶ | if ( $line =~ /^([A-Za-z0-9+\/=]+)\r?$/ ) {$myCertData[$Certnum].=$1;} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 71 | 2 | my @decoded=(); | |||||||||||||||||||||||||||||||
| 72 | 2 | foreach (@myCertData) { push(@decoded,decode_base64($_)); } | |||||||||||||||||||||||||||||||
| 73 | 2 | ▶ | return wantarray?@decoded:$decoded[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ( $^O =~ /^MSWin/ ) { print STDERR "WARNING: Can't umask 0077 when writing $file\n"; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | open(CERTKEY,">$file") || die "Can't create file to save cert and key to."; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | if ( $^O =~ /^MSWin/ ) { print STDERR "WARNING: Can't umask 0077 when writing $file\n"; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | 3 | ▶ | open(KEY,">$file") || die "Can't create file to save cert and key to."; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 223 | 3 | ▶ | open(CERT,">$file") || die "Can't create file to save cert and key to."; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 243 | 1 | ▶ | while (<KEY>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 244 | 9 | my $line=$_; | |||||||||||||||||||||||||||||||
| 245 | 9 | ▶ | if ( $line =~ /^-----BEGIN RSA PRIVATE KEY-----$/ ) {$read=1; next;} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | if ( $line =~ /^-----BEGIN PRIVATE KEY-----$/ ) {$read=2; next;} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 247 | 8 | ▶ | if ( $line =~ /^-----END RSA PRIVATE KEY-----$/ ) {last;} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | if ( $line =~ /^-----END PRIVATE KEY-----$/ ) {last;} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 249 | 7 | ▶ | if ( $read==1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | if ( $line =~ /^Proc-Type: ([0-9]+),(ENCRYPTED)$/ ) {$PEMV=$1; $PEMType=$2} | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 255 | 0 | ▶ | if ( $line =~ /^([A-Za-z0-9+\/=]+)$/ ) {$myKeyData.=$1;} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | if ( $PEMEnc ne "DES-EDE3-CBC" ) { die "I don't know how to unencrypt your key\n";} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | if ( $SALT !~ /^[a-fA-F0-9]{16}$/ ) { die "Bad Initilisation Vector (salt)'; I can't unencrypt your key!\n";} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||
| 277 | # Check/get password | ||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | if ( defined $passwd && $passwd eq "" ) { return undef; } #was expecting no password so abort | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
defined $passwd and $passwd eq ""
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 ""
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||