KDBX/Loader/V4.pm

filter: Statementstmt 93.7% 135 / 144 Branchbran 65.0% 65 / 100 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 77.0% 218 / 283 SCAR 45.4
CC
57
Cov
78%
_read_header
42.1
_read_variant_dictionary
19.0
_read_inner_header
14.2
CRAP 93.5
« KDBX/Key/File.pm ↑ Summary KDBX/Cipher.pm »
1 package File::KDBX::Loader::V4;
2 # ABSTRACT: Load KDBX4 files
3  
4 # magic
5 # headers
6 # headers checksum
7 # headers hmac
8 # body
9 # HMAC(
10 # CRYPT(
11 # COMPRESS(
12 # xml
13 # )
14 # )
15 # )
16  
17 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use Crypt::Digest qw(digest_data);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Crypt::Mac::HMAC qw(hmac);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use File::KDBX::Constants qw(:header :inner_header :variant_map :compression);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use File::KDBX::Util qw(:class :int :io :load erase_scoped);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use File::KDBX::IO::Crypt;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use File::KDBX::IO::HmacBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 2 extends 'File::KDBX::Loader';
32  
33 2 our $VERSION = '0.904'; # VERSION
34  
35 sub _read_header {
36 87 ▶ my $self = shift;
Subroutine _read_header: called CC=20 SCAR=37.4
37 87 my $fh = shift;
38  
39 87 ▶ read_all $fh, my $buf, 5 or throw 'Malformed header field, expected header type and size';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $buf, 5) or throw("Malformed header field, expected header type and size") 0 87 87
40 87 my ($type, $size) = unpack('C L<', $buf);
41  
42 87 my $val;
43 87 ▶ if (0 < $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $size) 74 13 87
44 74 ▶ read_all $fh, $val, $size or throw 'Expected header value', type => $type, size => $size;
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, $val, $size) or throw("Expected header value", "type", $type, "size", $size) 0 74 74
45 74 $buf .= $val;
46 }
47  
48 87 $type = to_header_constant($type);
49 87 ▶ if ($type == HEADER_END) {
Branch: 9 branches
BranchTrueFalseTotal
if ($type == 0) { } 14 73 87
elsif ($type == 1) { } 0 73 73
elsif ($type == 2) { } 14 59 73
elsif ($type == 3) { } 14 45 59
elsif ($type == 4) { } 14 31 45
elsif ($type == 7) { } 14 17 31
elsif ($type == 11) { } 14 3 17
elsif ($type == 12) { } 3 0 3
elsif ($type == 10 or $type == 8 or $type == 5 or $type == 6 or $type == 9) { } 0 0 0
50 # done
51 }
52 elsif ($type == HEADER_COMMENT) {
53 0 $val = decode('UTF-8', $val);
54 }
55 elsif ($type == HEADER_CIPHER_ID) {
56 14 ▶ $size == 16 or throw 'Invalid cipher UUID length', got => $size, expected => $size;
Branch: 1 branch
BranchTrueFalseTotal
$size == 16 or throw("Invalid cipher UUID length", "got", $size, "expected", $size) 0 14 14
57 }
58 elsif ($type == HEADER_COMPRESSION_FLAGS) {
59 14 $val = unpack('L<', $val);
60 }
61 elsif ($type == HEADER_MASTER_SEED) {
62 14 ▶ $size == 32 or throw 'Invalid master seed length', got => $size, expected => $size;
Branch: 1 branch
BranchTrueFalseTotal
$size == 32 or throw("Invalid master seed length", "got", $size, "expected", $size) 0 14 14
63 }
64 elsif ($type == HEADER_ENCRYPTION_IV) {
65 # nothing
66 }
67 elsif ($type == HEADER_KDF_PARAMETERS) {
68 14 open(my $dict_fh, '<', \$val);
69 14 $val = $self->_read_variant_dictionary($dict_fh);
70 }
71 elsif ($type == HEADER_PUBLIC_CUSTOM_DATA) {
72 3 open(my $dict_fh, '<', \$val);
73 3 $val = $self->_read_variant_dictionary($dict_fh);
74 }
75 ▶ elsif ($type == HEADER_INNER_RANDOM_STREAM_ID ||
Condition0%0/12
$type == "inner_random_stream_id" or $type == "inner_random_stream_key" or $type == "transform_seed" or $type == "transform_rounds" or $type == "stream_start_bytes"
l!l&&r!l&&!r
000
$type == "inner_random_stream_id" or $type == "inner_random_stream_key" or $type == "transform_seed" or $type == "transform_rounds"
l!l&&r!l&&!r
000
$type == "inner_random_stream_id" or $type == "inner_random_stream_key" or $type == "transform_seed"
l!l&&r!l&&!r
000
$type == "inner_random_stream_id" or $type == "inner_random_stream_key"
l!l&&r!l&&!r
000
MC/DC0%0/5
$type == "inner_random_stream_id" or $type == "inner_random_stream_key" or $type == "transform_seed" or $type == "transform_rounds" or $type == "stream_start_bytes"
$type == "inner_random_stream_id" $type == "inner_random_stream_key" $type == "transform_seed" $type == "transform_rounds" $type == "stream_start_bytes"
Truth table0%0/6
A or B or C or D or E
A= $type == "inner_random_stream_id"B= $type == "inner_random_stream_key"C= $type == "transform_seed"D= $type == "transform_rounds"E= $type == "stream_start_bytes"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
76 $type == HEADER_INNER_RANDOM_STREAM_KEY ||
77 $type == HEADER_TRANSFORM_SEED ||
78 $type == HEADER_TRANSFORM_ROUNDS ||
79 $type == HEADER_STREAM_START_BYTES) {
80 0 throw "Unexpected KDBX3 header: $type", type => $type;
81 }
82 else {
83 0 alert "Unknown header: $type", type => $type;
84 }
85  
86 87 ▶ return wantarray ? ($type => $val, $buf) : $buf;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 87 0 87
87 }
88  
89 sub _read_variant_dictionary {
90 17 ▶ my $self = shift;
Subroutine _read_variant_dictionary: called CC=16 SCAR=29.5
91 17 my $fh = shift;
92  
93 17 ▶ read_all $fh, my $buf, 2 or throw 'Failed to read variant dictionary version';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $buf, 2) or throw("Failed to read variant dictionary version") 0 17 17
94 17 my ($version) = unpack('S<', $buf);
95 17 ▶ VMAP_VERSION == ($version & VMAP_VERSION_MAJOR_MASK)
Branch: 1 branch
BranchTrueFalseTotal
256 == ($version & 65280) or throw("Unsupported variant dictionary version", "version", $version) 0 17 17
96 or throw 'Unsupported variant dictionary version', version => $version;
97  
98 17 my %dict;
99  
100 17 while (1) {
101 80 ▶ read_all $fh, $buf, 1 or throw 'Failed to read variant type';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, $buf, 1) or throw("Failed to read variant type") 0 80 80
102 80 my ($type) = unpack('C', $buf);
103 80 ▶ last if $type == VMAP_TYPE_END; # terminating null
Branch: 1 branch
BranchTrueFalseTotal
if $type == 0 17 63 80
104  
105 63 ▶ read_all $fh, $buf, 4 or throw 'Failed to read variant key size';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, $buf, 4) or throw("Failed to read variant key size") 0 63 63
106 63 my ($klen) = unpack('L<', $buf);
107  
108 63 ▶ read_all $fh, my $key, $klen or throw 'Failed to read variant key';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $key, $klen) or throw("Failed to read variant key") 0 63 63
109  
110 63 ▶ read_all $fh, $buf, 4 or throw 'Failed to read variant size';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, $buf, 4) or throw("Failed to read variant size") 0 63 63
111 63 my ($vlen) = unpack('L<', $buf);
112  
113 63 ▶ read_all $fh, my $val, $vlen or throw 'Failed to read variant';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $val, $vlen) or throw("Failed to read variant") 0 63 63
114  
115 63 ▶ if ($type == VMAP_TYPE_UINT32) {
Branch: 7 branches
BranchTrueFalseTotal
if ($type == 4) { } 11 52 63
elsif ($type == 5) { } 19 33 52
elsif ($type == 8) { } 1 32 33
elsif ($type == 12) { } 0 32 32
elsif ($type == 13) { } 0 32 32
elsif ($type == 24) { } 1 31 32
elsif ($type == 66) { } 31 0 31
116 11 ($val) = unpack('L<', $val);
117 }
118 elsif ($type == VMAP_TYPE_UINT64) {
119 19 ($val) = unpack_Ql($val);
120 }
121 elsif ($type == VMAP_TYPE_BOOL) {
122 1 ($val) = unpack('C', $val);
123 1 $val = boolean($val);
124 }
125 elsif ($type == VMAP_TYPE_INT32) {
126 0 ($val) = unpack('l<', $val);
127 }
128 elsif ($type == VMAP_TYPE_INT64) {
129 0 ($val) = unpack_ql($val);
130 }
131 elsif ($type == VMAP_TYPE_STRING) {
132 1 $val = decode('UTF-8', $val);
133 }
134 elsif ($type == VMAP_TYPE_BYTEARRAY) {
135 # nothing
136 }
137 else {
138 0 throw 'Unknown variant type', type => $type;
139 }
140 63 $dict{$key} = $val;
141 }
142  
143 17 return \%dict;
144 }
145  
146 sub _read_body {
147 14 ▶ my $self = shift;
Subroutine _read_body: called CC=10 SCAR=23.7
148 14 my $fh = shift;
149 14 my $key = shift;
150 14 my $header_data = shift;
151 14 my $kdbx = $self->kdbx;
152  
153 # assert all required headers present
154 14 for my $field (
155 HEADER_CIPHER_ID,
156 HEADER_ENCRYPTION_IV,
157 HEADER_MASTER_SEED,
158 ) {
159 42 ▶ defined $kdbx->headers->{$field} or throw "Missing $field";
Branch: 1 branch
BranchTrueFalseTotal
defined $kdbx->headers->{$field} or throw("Missing $field") 0 42 42
160 }
161  
162 14 my @cleanup;
163  
164 # checksum check
165 14 ▶ read_all $fh, my $header_hash, 32 or throw 'Failed to read header hash';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $header_hash, 32) or throw("Failed to read header hash") 0 14 14
166 14 my $got_header_hash = digest_data('SHA256', $header_data);
167 14 ▶ $got_header_hash eq $header_hash
Branch: 1 branch
BranchTrueFalseTotal
$got_header_hash eq $header_hash or throw("Data is corrupt (header checksum mismatch)", "got", $got_header_hash, "expected", $header_hash) 0 14 14
168 or throw 'Data is corrupt (header checksum mismatch)',
169 got => $got_header_hash, expected => $header_hash;
170  
171 14 $key = $kdbx->composite_key($key);
172 14 my $transformed_key = $kdbx->kdf->transform($key);
173 14 push @cleanup, erase_scoped $transformed_key;
174  
175 # authentication check
176 14 ▶ read_all $fh, my $header_hmac, 32 or throw 'Failed to read header HMAC';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $header_hmac, 32) or throw("Failed to read header HMAC") 0 14 14
177 14 my $hmac_key = digest_data('SHA512', $kdbx->headers->{master_seed}, $transformed_key, "\x01");
178 14 push @cleanup, erase_scoped $hmac_key;
179 14 my $got_header_hmac = hmac('SHA256',
180 digest_data('SHA512', "\xff\xff\xff\xff\xff\xff\xff\xff", $hmac_key),
181 $header_data,
182 );
183 14 ▶ $got_header_hmac eq $header_hmac
Branch: 1 branch
BranchTrueFalseTotal
$got_header_hmac eq $header_hmac or throw("Invalid credentials or data is corrupt (header HMAC mismatch)\n", "got", $got_header_hmac, "expected", $header_hmac) 6 8 14
184 or throw "Invalid credentials or data is corrupt (header HMAC mismatch)\n",
185 got => $got_header_hmac, expected => $header_hmac;
186  
187 8 $kdbx->key($key);
188  
189 8 $fh = File::KDBX::IO::HmacBlock->new($fh, key => $hmac_key);
190  
191 8 my $final_key = digest_data('SHA256', $kdbx->headers->{master_seed}, $transformed_key);
192 8 push @cleanup, erase_scoped $final_key;
193  
194 8 my $cipher = $kdbx->cipher(key => $final_key);
195 8 $fh = File::KDBX::IO::Crypt->new($fh, cipher => $cipher);
196  
197 8 my $compress = $kdbx->headers->{+HEADER_COMPRESSION_FLAGS};
198 8 ▶ if ($compress == COMPRESSION_GZIP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($compress == 1) { } 8 0 8
elsif ($compress != 0) { } 0 0 0
199 8 load_optional('IO::Uncompress::Gunzip');
200 8 ▶ $fh = IO::Uncompress::Gunzip->new($fh)
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::Uncompress::Gunzip"->new($fh) or throw("Failed to initialize compression library: $IO::Uncompress::Gunzip::GunzipError", "error", $IO::Uncompress::Gunzip::GunzipError) 0 8 8
201 or throw "Failed to initialize compression library: $IO::Uncompress::Gunzip::GunzipError",
202 error => $IO::Uncompress::Gunzip::GunzipError;
203 }
204 elsif ($compress != COMPRESSION_NONE) {
205 0 throw "Unsupported compression ($compress)\n", compression_flags => $compress;
206 }
207  
208 8 $self->_read_inner_headers($fh);
209 8 $self->_read_inner_body($fh);
210 }
211  
212 sub _read_inner_headers {
213 8 ▶ my $self = shift;
Subroutine _read_inner_headers: called CC=3 SCAR=11.0
214 8 my $fh = shift;
215  
216 8 ▶ while (my ($type, $val) = $self->_read_inner_header($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->_read_inner_header($fh)) 25 0 25
217 25 ▶ last if $type == INNER_HEADER_END;
Branch: 1 branch
BranchTrueFalseTotal
if $type == "end" 8 17 25
218 }
219 }
220  
221 sub _read_inner_header {
222 25 ▶ my $self = shift;
Subroutine _read_inner_header: called CC=12 SCAR=26.6
223 25 my $fh = shift;
224 25 my $kdbx = $self->kdbx;
225  
226 25 ▶ read_all $fh, my $buf, 5 or throw 'Expected inner header type and size';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $buf, 5) or throw("Expected inner header type and size") 0 25 25
227 25 my ($type, $size) = unpack('C L<', $buf);
228  
229 25 my $val;
230 25 ▶ if (0 < $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $size) 17 8 25
231 17 ▶ read_all $fh, $val, $size or throw 'Expected inner header value', type => $type, size => $size;
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, $val, $size) or throw("Expected inner header value", "type", $type, "size", $size) 0 17 17
232 }
233  
234 25 ▶ $type = to_inner_header_constant($type) // $type;
Condition33%1/3
to_inner_header_constant($type) // $type
l!l&&r!l&&!r
2500
MC/DC0%0/2
to_inner_header_constant($type) // $type
to_inner_header_constant($type) $type
Truth table33%1/3
A // B
A= to_inner_header_constant($type)B= $type
ABresult
1X1
011
000
235 25 ▶ if ($type == INNER_HEADER_END) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type == 0) { } 8 17 25
elsif ($type == 1) { } 8 9 17
elsif ($type == 2) { } 8 1 9
elsif ($type == 3) { } 1 0 1
236 # nothing
237 }
238 elsif ($type == INNER_HEADER_INNER_RANDOM_STREAM_ID) {
239 8 $val = unpack('L<', $val);
240 8 $kdbx->inner_headers->{$type} = $val;
241 }
242 elsif ($type == INNER_HEADER_INNER_RANDOM_STREAM_KEY) {
243 8 $kdbx->inner_headers->{$type} = $val;
244 }
245 elsif ($type == INNER_HEADER_BINARY) {
246 1 my $msize = $size - 1;
247 1 my ($flags, $data) = unpack("C a$msize", $val);
248 1 my $id = scalar keys %{$kdbx->binaries};
249 1 ▶ $kdbx->binaries->{$id} = {
Branch: 1 branch
BranchTrueFalseTotal
$flags & 1 ? : 0 1 1
250 value => $data,
251 $flags & INNER_HEADER_BINARY_FLAG_PROTECT ? (protect => true) : (),
252 };
253 }
254 else {
255 0 alert "Ignoring unknown inner header type ($type)", type => $type, size => $size, value => $val;
256 0 ▶ return wantarray ? ($type => $val) : $type;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
257 }
258  
259 25 ▶ return wantarray ? ($type => $val) : $type;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 25 0 25
260 }
261  
262 2 1;
263  
264 __END__
« KDBX/Key/File.pm ↑ Summary KDBX/Cipher.pm »