KDBX/Loader/V3.pm

filter: Statementstmt 95.3% 82 / 86 Branchbran 73.0% 38 / 52 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 85.1% 132 / 155 CC 29 SCAR 28.3
CC
29
Cov
85%
_read_header
28.4
_read_body
10.1
BEGIN
1.0
CRAP 17.0
« KDBX/Dumper/V4.pm ↑ Summary KDBX/Dumper/V3.pm »
1 package File::KDBX::Loader::V3;
2 # ABSTRACT: Load KDBX3 files
3  
4 # magic
5 # headers
6 # body
7 # CRYPT(
8 # start bytes
9 # HASH(
10 # COMPRESS(
11 # xml
12 # )
13 # )
14 # )
15  
16 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 ▶use Crypt::Digest qw(digest_data);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use File::KDBX::Constants qw(:header :compression :kdf);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use File::KDBX::IO::Crypt;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use File::KDBX::IO::HashBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use File::KDBX::Util qw(:class :int :io :load erase_scoped);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 6 extends 'File::KDBX::Loader';
29  
30 6 our $VERSION = '0.905'; # VERSION
31  
32 sub _read_header {
33 160 ▶ my $self = shift;
Subroutine _read_header: called CC=20 SCAR=33.5
34 160 my $fh = shift;
35  
36 160 ▶ read_all $fh, my $buf, 3 or throw 'Malformed header field, expected header type and size';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $buf, 3) or throw("Malformed header field, expected header type and size") 0 160 160
37 160 my ($type, $size) = unpack('C S<', $buf);
38  
39 160 my $val;
40 160 ▶ if (0 < $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $size) 160 0 160
41 160 ▶ 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 160 160
42 160 $buf .= $val;
43 }
44  
45 160 $type = to_header_constant($type);
46 160 ▶ if ($type == HEADER_END) {
Branch: 12 branches
BranchTrueFalseTotal
if ($type == 0) { } 16 144 160
elsif ($type == 1) { } 0 144 144
elsif ($type == 2) { } 16 128 144
elsif ($type == 3) { } 16 112 128
elsif ($type == 4) { } 16 96 112
elsif ($type == 5) { } 16 80 96
elsif ($type == 6) { } 16 64 80
elsif ($type == 7) { } 16 48 64
elsif ($type == 8) { } 16 32 48
elsif ($type == 9) { } 16 16 32
elsif ($type == 10) { } 16 0 16
elsif ($type == 11 or $type == 12) { } 0 0 0
47 # done
48 }
49 elsif ($type == HEADER_COMMENT) {
50 0 $val = decode('UTF-8', $val);
51 }
52 elsif ($type == HEADER_CIPHER_ID) {
53 16 ▶ $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 16 16
54 }
55 elsif ($type == HEADER_COMPRESSION_FLAGS) {
56 16 $val = unpack('L<', $val);
57 }
58 elsif ($type == HEADER_MASTER_SEED) {
59 16 ▶ $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 16 16
60 }
61 elsif ($type == HEADER_TRANSFORM_SEED) {
62 # nothing
63 }
64 elsif ($type == HEADER_TRANSFORM_ROUNDS) {
65 16 ($val) = unpack_Ql($val);
66 }
67 elsif ($type == HEADER_ENCRYPTION_IV) {
68 # nothing
69 }
70 elsif ($type == HEADER_INNER_RANDOM_STREAM_KEY) {
71 # nothing
72 }
73 elsif ($type == HEADER_STREAM_START_BYTES) {
74 # nothing
75 }
76 elsif ($type == HEADER_INNER_RANDOM_STREAM_ID) {
77 16 ($val) = unpack('L<', $val);
78 }
79 ▶ elsif ($type == HEADER_KDF_PARAMETERS ||
Condition0%0/3
$type == "kdf_parameters" or $type == "public_custom_data"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type == "kdf_parameters" or $type == "public_custom_data"
$type == "kdf_parameters" $type == "public_custom_data"
Truth table0%0/3
A or B
A= $type == "kdf_parameters"B= $type == "public_custom_data"
ABresult
1X1
011
000
80 $type == HEADER_PUBLIC_CUSTOM_DATA) {
81 0 throw "Unexpected KDBX4 header: $type", type => $type;
82 }
83 else {
84 0 alert "Unknown header: $type", type => $type;
85 }
86  
87 160 ▶ return wantarray ? ($type => $val, $buf) : $buf;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 160 0 160
88 }
89  
90 sub _read_body {
91 16 ▶ my $self = shift;
Subroutine _read_body: called CC=10 SCAR=23.1
92 16 my $fh = shift;
93 16 my $key = shift;
94 16 my $header_data = shift;
95 16 my $kdbx = $self->kdbx;
96  
97 # assert all required headers present
98 16 for my $field (
99 HEADER_CIPHER_ID,
100 HEADER_ENCRYPTION_IV,
101 HEADER_MASTER_SEED,
102 HEADER_INNER_RANDOM_STREAM_KEY,
103 HEADER_STREAM_START_BYTES,
104 ) {
105 80 ▶ defined $kdbx->headers->{$field} or throw "Missing $field";
Branch: 1 branch
BranchTrueFalseTotal
defined $kdbx->headers->{$field} or throw("Missing $field") 0 80 80
106 }
107  
108 $kdbx->kdf_parameters({
109 KDF_PARAM_UUID() => KDF_UUID_AES,
110 KDF_PARAM_AES_ROUNDS() => delete $kdbx->headers->{+HEADER_TRANSFORM_ROUNDS},
111 16 KDF_PARAM_AES_SEED() => delete $kdbx->headers->{+HEADER_TRANSFORM_SEED},
112 });
113  
114 16 my $master_seed = $kdbx->headers->{+HEADER_MASTER_SEED};
115  
116 16 my @cleanup;
117 16 $key = $kdbx->composite_key($key);
118  
119 16 my $response = $key->challenge($master_seed);
120 16 push @cleanup, erase_scoped $response;
121  
122 16 my $transformed_key = $kdbx->kdf->transform($key);
123 16 push @cleanup, erase_scoped $transformed_key;
124  
125 16 my $final_key = digest_data('SHA256', $master_seed, $response, $transformed_key);
126 16 push @cleanup, erase_scoped $final_key;
127  
128 16 my $cipher = $kdbx->cipher(key => $final_key);
129 16 $fh = File::KDBX::IO::Crypt->new($fh, cipher => $cipher);
130  
131 16 ▶ read_all $fh, my $start_bytes, 32 or throw 'Failed to read starting bytes';
Branch: 1 branch
BranchTrueFalseTotal
&read_all($fh, my $start_bytes, 32) or throw("Failed to read starting bytes") 0 16 16
132  
133 16 my $expected_start_bytes = $kdbx->headers->{stream_start_bytes};
134 16 ▶ $start_bytes eq $expected_start_bytes
Branch: 1 branch
BranchTrueFalseTotal
$start_bytes eq $expected_start_bytes or throw("Invalid credentials or data is corrupt (wrong starting bytes)\n", "got", $start_bytes, "expected", $expected_start_bytes, "headers", $kdbx->headers) 3 13 16
135 or throw "Invalid credentials or data is corrupt (wrong starting bytes)\n",
136 got => $start_bytes, expected => $expected_start_bytes, headers => $kdbx->headers;
137  
138 13 $kdbx->key($key);
139  
140 13 $fh = File::KDBX::IO::HashBlock->new($fh);
141  
142 13 my $compress = $kdbx->headers->{+HEADER_COMPRESSION_FLAGS};
143 13 ▶ if ($compress == COMPRESSION_GZIP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($compress == 1) { } 12 1 13
elsif ($compress != 0) { } 0 1 1
144 12 load_optional('IO::Uncompress::Gunzip');
145 12 ▶ $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 12 12
146 or throw "Failed to initialize compression library: $IO::Uncompress::Gunzip::GunzipError",
147 error => $IO::Uncompress::Gunzip::GunzipError;
148 }
149 elsif ($compress != COMPRESSION_NONE) {
150 0 throw "Unsupported compression ($compress)\n", compression_flags => $compress;
151 }
152  
153 13 $self->_read_inner_body($fh);
154 13 close($fh);
155  
156 13 ▶ if (my $header_hash = $kdbx->meta->{header_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $header_hash = $kdbx->meta->{"header_hash"}) 11 2 13
157 11 my $got_header_hash = digest_data('SHA256', $header_data);
158 11 ▶ $header_hash eq $got_header_hash
Branch: 1 branch
BranchTrueFalseTotal
$header_hash eq $got_header_hash or throw("Header hash does not match", "got", $got_header_hash, "expected", $header_hash) 1 10 11
159 or throw 'Header hash does not match', got => $got_header_hash, expected => $header_hash;
160 }
161 }
162  
163 6 1;
164  
165 __END__
« KDBX/Dumper/V4.pm ↑ Summary KDBX/Dumper/V3.pm »