| 1 | package File::KDBX::Key::File; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: A file key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Crypt::Digest qw(digest_data); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Crypt::Misc 0.029 qw(decode_b64 encode_b64); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Crypt::PRNG qw(random_bytes); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use File::KDBX::Constants qw(:key_file); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use File::KDBX::Error; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use File::KDBX::Util qw(:class :erase trim); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Ref::Util qw(is_ref is_scalarref); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Scalar::Util qw(openhandle); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use XML::LibXML::Reader; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | extends 'File::KDBX::Key'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 21 | ▶ | our $VERSION = '0.904'; # VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
type: called CC=3 SCAR=11.8
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 9 | ▶ | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
version: called CC=3 SCAR=11.3
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 21 | ▶ | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filepath: called CC=3 SCAR=11.1
Pod
documented
Branch: 1 branch
Condition50%1/2
$_[0]{"type"} //= undef
MC/DC0%0/1
$_[0]{"type"} //= undef
$_[0]{"type"}
Truth table50%1/2
A
A= $_[0]{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 9 | ▶ | has 'type', is => 'ro'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_[0]{"version"} //= undef
MC/DC100%1/1
$_[0]{"version"} //= undef
$_[0]{"version"}
Truth table100%2/2
A
A= $_[0]{"version"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 7 | ▶ | has 'version', is => 'ro'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_[0]{"filepath"} //= undef
MC/DC100%1/1
$_[0]{"filepath"} //= undef
$_[0]{"filepath"}
Truth table100%2/2
A
A= $_[0]{"filepath"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | has 'filepath', is => 'ro'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 37 | ▶ | sub init { shift->load(@_) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | sub load { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 37 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 37 | ▶ | my $primitive = shift // throw 'Missing key primitive'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
shift() // throw("Missing key primitive")
MC/DC0%0/2
shift() // throw("Missing key primitive")
shift()
throw("Missing key primitive")
Truth table33%1/3
A // B
A= shift()B= throw("Missing key primitive")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 36 | my $data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | my $cleanup; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 36 | ▶ | if (openhandle($primitive)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | seek $primitive, 0, 0; # not using ->seek method so it works on perl 5.10 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | my $buf = do { local $/; <$primitive> }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | $data = \$buf; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | $cleanup = erase_scoped $data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | elsif (is_scalarref($primitive)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 20 | $data = $primitive; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | ▶ | elsif (defined $primitive && !is_ref($primitive)) { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $primitive and not Ref::Util::XS::is_ref($primitive)
MC/DC50%1/2
defined $primitive and not Ref::Util::XS::is_ref($primitive)
defined $primitive
not Ref::Util::XS::is_ref($primitive)
Truth table66%2/3
A and not(B)
A= defined $primitiveB= not Ref::Util::XS::is_ref($primitive)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 14 | ▶ | open(my $fh, '<:raw', $primitive) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | or throw "Failed to open key file ($primitive)", filepath => $primitive; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 13 | my $buf = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 13 | $data = \$buf; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 13 | $cleanup = erase_scoped $data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 13 | $self->{filepath} = $primitive; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | throw 'Unexpected primitive type', type => ref $primitive; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 34 | my $raw_key; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 34 | ▶ | if (substr($$data, 0, 120) =~ /<KeyFile>/ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
substr($$data, 0, 120) =~ /<KeyFile>/ and () = $self->_load_xml($data, \$raw_key)
MC/DC50%1/2
substr($$data, 0, 120) =~ /<KeyFile>/ and () = $self->_load_xml($data, \$raw_key)
substr($$data, 0, 120) =~ /<KeyFile>/
() = $self->_load_xml($data, \$raw_key)
Truth table66%2/3
A and B
A= substr($$data, 0, 120) =~ /<KeyFile>/B= () = $self->_load_xml($data, \$raw_key)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | and my ($type, $version) = $self->_load_xml($data, \$raw_key)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 6 | $self->{type} = $type; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 6 | $self->{version} = $version; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 6 | $self->_set_raw_key($raw_key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | elsif (length($$data) == 32) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | $self->{type} = KEY_FILE_TYPE_BINARY; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 4 | $self->_set_raw_key($$data); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | elsif ($$data =~ /^[A-Fa-f0-9]{64}$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | $self->{type} = KEY_FILE_TYPE_HEX; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | $self->_set_raw_key(pack('H64', $$data)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 22 | $self->{type} = KEY_FILE_TYPE_HASHED; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 22 | $self->_set_raw_key(digest_data('SHA256', $$data)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 34 | return $self->hide; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | sub reload { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reload: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | $self->init($self->{filepath}) if defined $self->{filepath}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | sub save { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=30 SCAR=41.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 7 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 7 | my @cleanup; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 7 | ▶ | my $raw_key = $args{raw_key} // $self->raw_key // random_bytes(32); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
($args{"raw_key"} // $self->raw_key) // &random_bytes(32)
$args{"raw_key"} // $self->raw_key
MC/DC0%0/3
($args{"raw_key"} // $self->raw_key) // &random_bytes(32)
$args{"raw_key"}
$self->raw_key
&random_bytes(32)
Truth table25%1/4
A // B // C
A= $args{"raw_key"}B= $self->raw_keyC= &random_bytes(32)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 7 | push @cleanup, erase_scoped $raw_key; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 7 | ▶ | length($raw_key) == 32 or throw 'Raw key must be exactly 256 bits (32 bytes)', length => length($raw_key); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 7 | ▶ | my $type = $args{type} // $self->type // KEY_FILE_TYPE_XML; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition60%3/5
($args{"type"} // $self->type) // "xml"
$args{"type"} // $self->type
MC/DC0%0/2
($args{"type"} // $self->type) // "xml"
$args{"type"}
$self->type
Truth table66%2/3
A // B
A= $args{"type"}B= $self->type
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 7 | ▶ | my $version = $args{version} // $self->version // 2; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
($args{"version"} // $self->version) // 2
$args{"version"} // $self->version
MC/DC50%1/2
($args{"version"} // $self->version) // 2
$args{"version"}
$self->version
Truth table66%2/3
A // B
A= $args{"version"}B= $self->version
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 7 | ▶ | my $filepath = $args{filepath} // $self->filepath; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$args{"filepath"} // $self->filepath
MC/DC100%2/2
$args{"filepath"} // $self->filepath
$args{"filepath"}
$self->filepath
Truth table100%3/3
A // B
A= $args{"filepath"}B= $self->filepath
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 7 | my $fh = $args{fh}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 7 | ▶ | my $atomic = $args{atomic} // 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"atomic"} // 1
MC/DC0%0/1
$args{"atomic"} // 1
$args{"atomic"}
Truth table50%1/2
A
A= $args{"atomic"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 7 | my $filepath_temp; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 7 | ▶ | if (!openhandle($fh)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 6 | ▶ | $filepath or throw 'Must specify where to safe the key file to'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 6 | ▶ | if ($atomic) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 6 | require File::Temp; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 6 | ($fh, $filepath_temp) = eval { File::Temp::tempfile("${filepath}-XXXXXX", UNLINK => 1) }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 6 | ▶ | if (!$fh or my $err = $@) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $fh or my $err = $@
MC/DC0%0/2
not $fh or my $err = $@
not $fh
my $err = $@
Truth table33%1/3
not(A) or B
A= not $fhB= my $err = $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $err //= 'Unknown error'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$err //= "Unknown error"
MC/DC0%0/1
$err //= "Unknown error"
$err
Truth table0%0/2
A
A= $err
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | throw sprintf('Open file failed (%s): %s', $filepath_temp, $err), | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | error => $err, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | filepath => $filepath_temp; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | open($fh, '>:raw', $filepath) or throw "Open file failed ($filepath): $!", filepath => $filepath; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 7 | ▶ | if ($type == KEY_FILE_TYPE_XML) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 3 | $self->_save_xml($fh, $raw_key, $version); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | elsif ($type == KEY_FILE_TYPE_BINARY) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 2 | print $fh $raw_key; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | elsif ($type == KEY_FILE_TYPE_HEX) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | my $hex = uc(unpack('H*', $raw_key)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | push @cleanup, erase_scoped $hex; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | print $fh $hex; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | throw "Cannot save $type key file (invalid type)", type => $type; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 6 | close($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 6 | ▶ | if ($filepath_temp) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 5 | my ($file_mode, $file_uid, $file_gid) = (stat($filepath))[2, 4, 5]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 5 | ▶ | my $mode = $args{mode} // $file_mode // do { my $m = umask; defined $m ? oct(666) &~ $m : undef }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
($args{"mode"} // $file_mode) // do {
my $m = umask;
defined $m ? 438 & ~$m : undef
}
$args{"mode"} // $file_mode
MC/DC0%0/3
($args{"mode"} // $file_mode) // do {
my $m = umask;
defined $m ? 438 & ~$m : undef
}
$args{"mode"}
$file_mode
do {
my $m = umask;
defined $m ? 438 & ~$m : undef
}
Truth table25%1/4
A // B // C
A= $args{"mode"}B= $file_modeC= do {
my $m = umask;
defined $m ? 438 & ~$m : undef
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 5 | ▶ | my $uid = $args{uid} // $file_uid // -1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
($args{"uid"} // $file_uid) // -1
$args{"uid"} // $file_uid
MC/DC0%0/2
($args{"uid"} // $file_uid) // -1
$args{"uid"}
$file_uid
Truth table33%1/3
A // B
A= $args{"uid"}B= $file_uid
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 5 | ▶ | my $gid = $args{gid} // $file_gid // -1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
($args{"gid"} // $file_gid) // -1
$args{"gid"} // $file_gid
MC/DC0%0/2
($args{"gid"} // $file_gid) // -1
$args{"gid"}
$file_gid
Truth table33%1/3
A // B
A= $args{"gid"}B= $file_gid
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 5 | ▶ | chmod($mode, $filepath_temp) if defined $mode; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 5 | chown($uid, $gid, $filepath_temp); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 5 | ▶ | rename($filepath_temp, $filepath) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | or throw "Failed to write file ($filepath): $!", filepath => $filepath; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | sub _load_xml { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_xml: called CC=22 SCAR=34.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 6 | my $buf = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 6 | my $out = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 6 | my ($version, $hash, $data); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 6 | my $reader = XML::LibXML::Reader->new(string => $$buf); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 6 | my $pattern = XML::LibXML::Pattern->new('/KeyFile/Meta/Version|/KeyFile/Key/Data'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 6 | ▶ | while ($reader->nextPatternMatch($pattern) == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 24 | ▶ | next if $reader->nodeType != XML_READER_TYPE_ELEMENT; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 12 | my $name = $reader->localName; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 12 | ▶ | if ($name eq 'Version') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 6 | ▶ | $reader->read if !$reader->isEmptyElement; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 6 | ▶ | $reader->nodeType == XML_READER_TYPE_TEXT | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | or alert 'Expected text node with version', line => $reader->lineNumber; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 6 | my $val = trim($reader->value); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 6 | ▶ | defined $version | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | and alert 'Overwriting version', previous => $version, new => $val, line => $reader->lineNumber; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 6 | $version = $val; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | elsif ($name eq 'Data') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 6 | ▶ | $hash = trim($reader->getAttribute('Hash')) if $reader->hasAttributes; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 6 | ▶ | $reader->read if !$reader->isEmptyElement; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 6 | ▶ | $reader->nodeType == XML_READER_TYPE_TEXT | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | or alert 'Expected text node with data', line => $reader->lineNumber; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 6 | $data = $reader->value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 6 | ▶ | $data =~ s/\s+//g if defined $data; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 6 | ▶ | return if !defined $version || !defined $data; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $version and defined $data
MC/DC0%0/2
defined $version and defined $data
defined $version
defined $data
Truth table33%1/3
A and B
A= defined $versionB= defined $data
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 6 | ▶ | if ($version =~ /^1\.0/ && $data =~ /^[A-Za-z0-9+\/=]+$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$version =~ /^1\.0/ and $data =~ m[^[A-Za-z0-9+/=]+$]
MC/DC50%1/2
$version =~ /^1\.0/ and $data =~ m[^[A-Za-z0-9+/=]+$]
$version =~ /^1\.0/
$data =~ m[^[A-Za-z0-9+/=]+$]
Truth table66%2/3
A and B
A= $version =~ /^1\.0/B= $data =~ m[^[A-Za-z0-9+/=]+$]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 2 | $$out = eval { decode_b64($data) }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 2 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | throw 'Failed to decode key in key file', version => $version, data => $data, error => $err; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | return (KEY_FILE_TYPE_XML, $version); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | ▶ | elsif ($version =~ /^2\.0/ && $data =~ /^[A-Fa-f0-9]+$/ && defined $hash && $hash =~ /^[A-Fa-f0-9]+$/) { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%3/9
$version =~ /^2\.0/ and $data =~ /^[A-Fa-f0-9]+$/ and defined $hash and $hash =~ /^[A-Fa-f0-9]+$/
$version =~ /^2\.0/ and $data =~ /^[A-Fa-f0-9]+$/ and defined $hash
$version =~ /^2\.0/ and $data =~ /^[A-Fa-f0-9]+$/
MC/DC0%0/4
$version =~ /^2\.0/ and $data =~ /^[A-Fa-f0-9]+$/ and defined $hash and $hash =~ /^[A-Fa-f0-9]+$/
$version =~ /^2\.0/
$data =~ /^[A-Fa-f0-9]+$/
defined $hash
$hash =~ /^[A-Fa-f0-9]+$/
Truth table20%1/5
A and B and C and D
A= $version =~ /^2\.0/B= $data =~ /^[A-Fa-f0-9]+$/C= defined $hashD= $hash =~ /^[A-Fa-f0-9]+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 4 | $$out = pack('H*', $data); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 4 | $hash = pack('H*', $hash); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 4 | my $got_hash = digest_data('SHA256', $$out); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 4 | ▶ | $hash eq substr($got_hash, 0, length($hash)) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | or throw 'Checksum mismatch', got => $got_hash, expected => $hash; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 4 | return (KEY_FILE_TYPE_XML, $version); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | throw 'Unexpected data in key file', version => $version, data => $data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | sub _save_xml { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_save_xml: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 3 | my $fh = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 3 | my $raw_key = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 3 | ▶ | my $version = shift // 2; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() // 2
MC/DC0%0/1
shift() // 2
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 3 | my @cleanup; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 3 | my $dom = XML::LibXML::Document->new('1.0', 'UTF-8'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 3 | my $doc = XML::LibXML::Element->new('KeyFile'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 3 | $dom->setDocumentElement($doc); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 3 | my $meta_node = XML::LibXML::Element->new('Meta'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 3 | $doc->appendChild($meta_node); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 3 | my $version_node = XML::LibXML::Element->new('Version'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 3 | $version_node->appendText(sprintf('%.1f', $version)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 3 | $meta_node->appendChild($version_node); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 3 | my $key_node = XML::LibXML::Element->new('Key'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 3 | $doc->appendChild($key_node); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 3 | my $data_node = XML::LibXML::Element->new('Data'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 3 | $key_node->appendChild($data_node); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 3 | ▶ | if (int($version) == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 1 | my $b64 = encode_b64($raw_key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | push @cleanup, erase_scoped $b64; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 1 | $data_node->appendText($b64); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | elsif (int($version) == 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 2 | my @hex = unpack('(H8)8', $raw_key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 2 | my $hex = uc(sprintf("\n %s\n %s\n ", join(' ', @hex[0..3]), join(' ', @hex[4..7]))); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 2 | push @cleanup, erase_scoped $hex, @hex; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 2 | $data_node->appendText($hex); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 2 | my $hash = digest_data('SHA256', $raw_key); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 2 | substr($hash, 4) = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 2 | $hash = uc(unpack('H*', $hash)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 2 | $data_node->setAttribute('Hash', $hash); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | throw 'Failed to save unsupported key file version', version => $version; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 3 | $dom->toFH($fh, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||