| 1 | package ELF::sign; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Net::SSLeay; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Fcntl qw(SEEK_END); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Digest::SHA qw(sha512); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | $VERSION = '0.07'; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | eval { | |||||||||||||||||||||||||||
| 13 | 2 | require Net::SSLeay; | |||||||||||||||||||||||||||
| 14 | 2 | Net::SSLeay->import( 1.65 ); | |||||||||||||||||||||||||||
| 15 | }; | ||||||||||||||||||||||||||||
| 16 | 2 | Net::SSLeay::load_error_strings(); | |||||||||||||||||||||||||||
| 17 | 2 | Net::SSLeay::SSLeay_add_ssl_algorithms(); | |||||||||||||||||||||||||||
| 18 | 2 | Net::SSLeay::randomize(); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | require XSLoader; | ||||||||||||||||||||||||||||
| 22 | XSLoader::load('ELF::sign', $VERSION); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 25 | 0 | my $type = shift; | |||||||||||||||||||||||||||
| 26 | 0 | my $params = {@_}; | |||||||||||||||||||||||||||
| 27 | 0 | my $self = bless({}, $type); | |||||||||||||||||||||||||||
| 28 | 0 | $self->{debug}++; | |||||||||||||||||||||||||||
| 29 | 0 | return $self; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub dataFile { | ||||||||||||||||||||||||||
|
Subroutine
dataFile: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 35 | 0 | $self->{file} = $file; | |||||||||||||||||||||||||||
| 36 | 0 | $self->{pkcs7} = $self->getFromFile(); | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | $self->{datapostset} = $self->{pkcs7} ? length($self->{pkcs7}) + 8 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | return $self->{pkcs7}; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub data { | ||||||||||||||||||||||||||
|
Subroutine
data: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 42 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 43 | 0 | my $data = shift; | |||||||||||||||||||||||||||
| 44 | 0 | delete $self->{file}; | |||||||||||||||||||||||||||
| 45 | 0 | $self->{data} = $data; | |||||||||||||||||||||||||||
| 46 | 0 | $self->{pkcs7} = $self->getFromData(); | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | $self->{datapostset} = $self->{pkcs7} ? length($self->{pkcs7}) + 8 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | return $self->{pkcs7}; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub crt { | ||||||||||||||||||||||||||
|
Subroutine
crt: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 52 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | 0 | $self->{"crt"} = shift; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub key { | ||||||||||||||||||||||||||
|
Subroutine
key: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 57 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | 0 | $self->{"key"} = shift; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub crtFile { | ||||||||||||||||||||||||||
|
Subroutine
crtFile: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 62 | 0 | return shift->loadFile("crt", @_); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub keyFile { | ||||||||||||||||||||||||||
|
Subroutine
keyFile: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 66 | 0 | return shift->loadFile("key", @_); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 0 | my $name = shift; | |||||||||||||||||||||||||||
| 72 | 0 | my $data = shift; | |||||||||||||||||||||||||||
| 73 | 0 | $self->{$name} = $data; | |||||||||||||||||||||||||||
| 74 | 0 | return undef; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub loadFile { | ||||||||||||||||||||||||||
|
Subroutine
loadFile: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 78 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 79 | 0 | my $name = shift; | |||||||||||||||||||||||||||
| 80 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 81 | 0 | delete $self->{$name}; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | open(IN, "<", $file) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | return "Error opening ".$name.": ".$!; | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | while(<IN>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | $self->{$name} .= $_; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 0 | close(IN); | |||||||||||||||||||||||||||
| 88 | 0 | return undef; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub dataToBio { | ||||||||||||||||||||||||||
|
Subroutine
dataToBio: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 92 | 0 | my $data = shift; | |||||||||||||||||||||||||||
| 93 | #my $self = {}; | ||||||||||||||||||||||||||||
| 94 | 0 | my $bio = Net::SSLeay::BIO_new(Net::SSLeay::BIO_s_mem()); | |||||||||||||||||||||||||||
| 95 | 0 | my $sent = Net::SSLeay::BIO_write($bio, $data); | |||||||||||||||||||||||||||
| 96 | #print "Wrote ".$sent." of ".length($data)." bytes.\n" | ||||||||||||||||||||||||||||
| 97 | # if $self->{debug}; | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | die "Cannot write to bio!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | if (($sent) != length($data)); | ||||||||||||||||||||||||||||
| 100 | 0 | return $bio; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub PEMdataToPKCS7 { | ||||||||||||||||||||||||||
|
Subroutine
PEMdataToPKCS7: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 104 | 0 | my $data = shift; | |||||||||||||||||||||||||||
| 105 | 0 | my $pkcs7 = undef; | |||||||||||||||||||||||||||
| 106 | 0 | my $bio = dataToBio($data); | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | die "Error using pkcs7: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | unless ($pkcs7 = PEM_read_bio_PKCS7($bio, 0, 0, 0)); | ||||||||||||||||||||||||||||
| 109 | 0 | Net::SSLeay::BIO_free($bio); | |||||||||||||||||||||||||||
| 110 | 0 | return $pkcs7; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub PEMdataToX509 { | ||||||||||||||||||||||||||
|
Subroutine
PEMdataToX509: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 114 | 0 | my $x509 = shift; | |||||||||||||||||||||||||||
| 115 | 0 | my $bio = dataToBio($x509); | |||||||||||||||||||||||||||
| 116 | 0 | my $x509result = undef; | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | die "Error using x509: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | unless ($x509result = Net::SSLeay::PEM_read_bio_X509($bio)); | ||||||||||||||||||||||||||||
| 119 | 0 | Net::SSLeay::BIO_free($bio); | |||||||||||||||||||||||||||
| 120 | 0 | return $x509result; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub PEMdataToEVP_PKEY { | ||||||||||||||||||||||||||
|
Subroutine
PEMdataToEVP_PKEY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 124 | 0 | my $crt = shift; | |||||||||||||||||||||||||||
| 125 | 0 | my $bio = dataToBio($crt); | |||||||||||||||||||||||||||
| 126 | 0 | my $evp_pkey = undef; | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | die "Error using cacrt: ".Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | unless ($evp_pkey = Net::SSLeay::PEM_read_bio_PrivateKey($bio)); | ||||||||||||||||||||||||||||
| 129 | 0 | Net::SSLeay::BIO_free($bio); | |||||||||||||||||||||||||||
| 130 | 0 | return $evp_pkey; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub getDigest { | ||||||||||||||||||||||||||
|
Subroutine
getDigest: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 134 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 135 | 0 | my $sha512 = Digest::SHA->new("sha512"); | |||||||||||||||||||||||||||
| 136 | 0 | ▶ | if ($self->{file}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | my $filesize = doFile(sub{ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 138 | 0 | my $buf = shift; | |||||||||||||||||||||||||||
| 139 | 0 | $sha512->add($buf); | |||||||||||||||||||||||||||
| 140 | 0 | }, $self->{file}, $self->{datapostset}); | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | return $filesize | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | unless $filesize; | ||||||||||||||||||||||||||||
| 143 | } elsif(defined($self->{data})) { | ||||||||||||||||||||||||||||
| 144 | 0 | $sha512->add(substr($self->{data}, 0, length($self->{data})-$self->{datapostset})); | |||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||
| 146 | 0 | return undef; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | return $sha512->digest(); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub sign { | ||||||||||||||||||||||||||
|
Subroutine
sign: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 152 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 153 | # TODO:XXX:FIXME: Allow to specify which digest should be used! | ||||||||||||||||||||||||||||
| 154 | # TODO:XXX:FIXME: Allow to specify ca the certificate must match! | ||||||||||||||||||||||||||||
| 155 | # TODO:XXX:FIXME: Verify the validity of the certificate used! | ||||||||||||||||||||||||||||
| 156 | 0 | delete $self->{pkcs7}; | |||||||||||||||||||||||||||
| 157 | 0 | my $digest = $self->getDigest(); | |||||||||||||||||||||||||||
| 158 | 0 | ▶ | return "No digest" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | unless $digest; | ||||||||||||||||||||||||||||
| 160 | return "No Cert" | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | unless $self->{crt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | return "No Key" | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | unless $self->{key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | $self->{pkcs7} = datasign($digest, PEMdataToX509($self->{crt}), PEMdataToEVP_PKEY($self->{key})); | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | return $self->{pkcs7} ? undef : "Signing failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | sub doFile { | ||||||||||||||||||||||||||
|
Subroutine
doFile: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 169 | 0 | my $action = shift; | |||||||||||||||||||||||||||
| 170 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 171 | 0 | my $lenp7 = shift; | |||||||||||||||||||||||||||
| 172 | return undef | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | unless my $size = (stat($file))[7]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | open(IN, "<", $file) || die($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 0 | my $filesize = 0; | |||||||||||||||||||||||||||
| 176 | 0 | while (1) { | |||||||||||||||||||||||||||
| 177 | 0 | my $wread = 16*1024; | |||||||||||||||||||||||||||
| 178 | 0 | ▶ | if ($lenp7 && ($wread+$filesize > ($size-$lenp7))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$lenp7 and $wread + $filesize > $size - $lenp7
MC/DC0%0/2
$lenp7 and $wread + $filesize > $size - $lenp7
$lenp7
$wread + $filesize > $size - $lenp7
Truth table0%0/3
A and B
A= $lenp7B= $wread + $filesize > $size - $lenp7
| |||||||||||||||||||||||||||||
| 179 | last unless | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | $wread = $size-($filesize+$lenp7) > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | #print("Reducing wread=16k -> ".$wread." size=".$size." lenp7=".$lenp7." pos=".$filesize." file=".$file."\n"); | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | if ((my $nread = sysread(IN, my $buf, $wread)) > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | &$action($buf); | |||||||||||||||||||||||||||
| 185 | 0 | $filesize += $nread; | |||||||||||||||||||||||||||
| 186 | } else { | ||||||||||||||||||||||||||||
| 187 | 0 | last; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 0 | close(IN); | |||||||||||||||||||||||||||
| 191 | 0 | return $filesize; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | sub verify { | ||||||||||||||||||||||||||
|
Subroutine
verify: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 195 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 196 | # TODO:XXX:FIXME: Allow to specify which digest should be used! | ||||||||||||||||||||||||||||
| 197 | # TODO:XXX:FIXME: Allow to specify ca the certificate must match! | ||||||||||||||||||||||||||||
| 198 | # TODO:XXX:FIXME: Verify the validity of the certificate used! | ||||||||||||||||||||||||||||
| 199 | 0 | my $digest = $self->getDigest(); | |||||||||||||||||||||||||||
| 200 | 0 | ▶ | return "No digest" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | unless $digest; | ||||||||||||||||||||||||||||
| 202 | return "No PKCS7" | ||||||||||||||||||||||||||||
| 203 | 0 | ▶ | unless $self->{pkcs7}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | return "No Cert" | ||||||||||||||||||||||||||||
| 205 | 0 | ▶ | unless $self->{crt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | my $return = dataverify($digest, PEMdataToX509($self->{crt}), PEMdataToPKCS7($self->{pkcs7})); | |||||||||||||||||||||||||||
| 207 | 0 | return $return; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | ▶ | sub hexdump { join ':', map { sprintf "%02X", $_ } unpack "C*", $_[0]; } | ||||||||||||||||||||||||||
|
Subroutine
hexdump: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub save { | ||||||||||||||||||||||||||
|
Subroutine
save: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 213 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 214 | 0 | my $newfile = shift; | |||||||||||||||||||||||||||
| 215 | 0 | ▶ | my $nopkcs7 = shift || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 216 | return "Unsigned" | ||||||||||||||||||||||||||||
| 217 | 0 | ▶ | unless $self->{pkcs7} || $nopkcs7; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"pkcs7"} or $nopkcs7
MC/DC0%0/2
$self->{"pkcs7"} or $nopkcs7
$self->{"pkcs7"}
$nopkcs7
Truth table0%0/3
A or B
A= $self->{"pkcs7"}B= $nopkcs7
| |||||||||||||||||||||||||||||
| 218 | 0 | ▶ | open(my $outfile, ">", $newfile) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | return "Cannot open file: ".$!; | ||||||||||||||||||||||||||||
| 220 | 0 | ▶ | if ($self->{file}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 221 | 0 | ▶ | my $filesize = doFile(sub{ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 222 | 0 | my $buf = shift; | |||||||||||||||||||||||||||
| 223 | 0 | syswrite($outfile, $buf); | |||||||||||||||||||||||||||
| 224 | 0 | }, $self->{file}, $self->{datapostset}); | |||||||||||||||||||||||||||
| 225 | 0 | ▶ | return "Error reading file" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | unless defined($filesize); | ||||||||||||||||||||||||||||
| 227 | } elsif(defined($self->{data})) { | ||||||||||||||||||||||||||||
| 228 | 0 | syswrite($outfile, substr($self->{data}, 0, length($self->{data})-$self->{datapostset})); | |||||||||||||||||||||||||||
| 229 | } else { | ||||||||||||||||||||||||||||
| 230 | 0 | return "No data"; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | 0 | ▶ | syswrite($outfile, $self->{pkcs7}.pack("Q", length($self->{pkcs7}))) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | unless $nopkcs7; | ||||||||||||||||||||||||||||
| 234 | 0 | close($outfile); | |||||||||||||||||||||||||||
| 235 | 0 | return undef; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 239 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 240 | 0 | ▶ | my $nopkcs7 = shift || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 241 | return undef | ||||||||||||||||||||||||||||
| 242 | 0 | ▶ | unless $self->{pkcs7} || $nopkcs7; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"pkcs7"} or $nopkcs7
MC/DC0%0/2
$self->{"pkcs7"} or $nopkcs7
$self->{"pkcs7"}
$nopkcs7
Truth table0%0/3
A or B
A= $self->{"pkcs7"}B= $nopkcs7
| |||||||||||||||||||||||||||||
| 243 | 0 | my $data = ''; | |||||||||||||||||||||||||||
| 244 | 0 | ▶ | if ($self->{file}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 245 | 0 | ▶ | my $filesize = doFile(sub{ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 246 | 0 | my $buf = shift; | |||||||||||||||||||||||||||
| 247 | 0 | $data .= $buf; | |||||||||||||||||||||||||||
| 248 | 0 | }, $self->{file}, $self->{datapostset}); | |||||||||||||||||||||||||||
| 249 | } elsif(defined($self->{data})) { | ||||||||||||||||||||||||||||
| 250 | 0 | $data = substr($self->{data}, 0, length($self->{data})-$self->{datapostset}); | |||||||||||||||||||||||||||
| 251 | } else { | ||||||||||||||||||||||||||||
| 252 | 0 | return undef; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | 0 | ▶ | $data .= $self->{pkcs7}.pack("Q", length($self->{pkcs7})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | unless $nopkcs7; | ||||||||||||||||||||||||||||
| 256 | 0 | return $data; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 0 | ▶ | sub getFromData { | ||||||||||||||||||||||||||
|
Subroutine
getFromData: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 260 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 261 | 0 | ▶ | if (length($self->{data}) < 8) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | #print "Unable to access 8 bytes! ".length($self->{data})."\n"; | ||||||||||||||||||||||||||||
| 263 | 0 | return 0; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | 0 | my $offset = unpack("Q", substr($self->{data}, length($self->{data})-8, 8)); | |||||||||||||||||||||||||||
| 266 | 0 | ▶ | if ($offset+8 > length($self->{data})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | #print "Offset bigger than data -> Skipping\n"; | ||||||||||||||||||||||||||||
| 268 | 0 | return 0; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if ($offset > 10*1024) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | #print "Offset bigger than one MB -> Skipping\n"; | ||||||||||||||||||||||||||||
| 272 | 0 | return 0; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | 0 | my $wanted = "-----BEGIN PKCS7-----"; | |||||||||||||||||||||||||||
| 275 | 0 | ▶ | if (substr($self->{data}, length($self->{data})-(8+$offset), length($wanted)) ne $wanted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | #print "Not a PKCS7 header!\n"; | ||||||||||||||||||||||||||||
| 277 | 0 | return 0; | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | 0 | return substr($self->{data}, length($self->{data})-(8+$offset), $offset); | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 0 | ▶ | sub getFromFile { | ||||||||||||||||||||||||||
|
Subroutine
getFromFile: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 283 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 284 | 0 | ▶ | unless ($self->{file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | #print "Unable to read 8 bytes!\n"; | ||||||||||||||||||||||||||||
| 286 | 0 | return undef; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | 0 | my $filesize = (stat($self->{file}))[7]; | |||||||||||||||||||||||||||
| 289 | 0 | my $openfile = undef; | |||||||||||||||||||||||||||
| 290 | 0 | ▶ | unless (open($openfile, "<", $self->{file})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | #print "Unable to open file="($self->{file}." error=".$!."\n"; | ||||||||||||||||||||||||||||
| 292 | 0 | return undef; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | 0 | seek($openfile, -8, SEEK_END); | |||||||||||||||||||||||||||
| 295 | 0 | my $data = undef; | |||||||||||||||||||||||||||
| 296 | 0 | ▶ | unless ((sysread($openfile, $data, 8)) == 8) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | #print "Unable to read 8 bytes!\n"; | ||||||||||||||||||||||||||||
| 298 | 0 | return 0; | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | 0 | my $offset = unpack("Q", $data); | |||||||||||||||||||||||||||
| 301 | 0 | ▶ | if ($offset+8 > $filesize) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | #print "Offset bigger than file -> Skipping\n"; | ||||||||||||||||||||||||||||
| 303 | 0 | return 0; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | 0 | ▶ | if ($offset > 1024*1024) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | #print "Offset bigger than one MB -> Skipping\n"; | ||||||||||||||||||||||||||||
| 307 | 0 | return 0; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | #print "Found offset of ".$offset."\n"; | ||||||||||||||||||||||||||||
| 310 | 0 | seek($openfile, -(8+$offset), SEEK_END); | |||||||||||||||||||||||||||
| 311 | 0 | my $wanted = "-----BEGIN PKCS7-----"; | |||||||||||||||||||||||||||
| 312 | 0 | ▶ | unless ((sysread($openfile, $data, length($wanted))) == length($wanted)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | #print "Unable to read ".length($wanted)." bytes!\n"; | ||||||||||||||||||||||||||||
| 314 | 0 | return 0; | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | 0 | ▶ | if ($data ne $wanted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | #print "Not a PKCS7 header!"; | ||||||||||||||||||||||||||||
| 318 | 0 | return 0; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | 0 | seek($openfile, -(8+$offset), SEEK_END); | |||||||||||||||||||||||||||
| 321 | 0 | ▶ | unless ((sysread($openfile, $data, $offset)) == $offset) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | #print "Unable to read $offset bytes!\n"; | ||||||||||||||||||||||||||||
| 323 | 0 | return 0; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | 0 | return $data; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 0 | ▶ | sub pkcs7 { | ||||||||||||||||||||||||||
|
Subroutine
pkcs7: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 329 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 330 | 0 | my $pkcs7 = shift; | |||||||||||||||||||||||||||
| 331 | 0 | ▶ | $self->{pkcs7} = $pkcs7 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | if defined($pkcs7); | ||||||||||||||||||||||||||||
| 333 | 0 | return $self->{pkcs7}; | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 1; | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | __END__ | ||||||||||||||||||||||||||||