KDBX/Dumper/V3.pm

filter: Statementstmt 94.1% 96 / 102 Branchbran 62.5% 30 / 48 Conditioncond 40.0% 2 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 83.1% 143 / 172 CC 29 SCAR 26.4
CC
29
Cov
83%
_write_header
26.2
_write_body
7.3
_write_headers
5.2
CRAP 13.9
« KDBX/Loader/V3.pm ↑ Summary KDBX/Key/YubiKey.pm »
1 package File::KDBX::Dumper::V3;
2 # ABSTRACT: Dump KDBX3 files
3  
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Crypt::Digest qw(digest_data);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use File::KDBX::Constants qw(:header :compression);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use File::KDBX::IO::Crypt;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use File::KDBX::IO::HashBlock;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use File::KDBX::Util qw(:class :empty :int :load erase_scoped);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 extends 'File::KDBX::Dumper';
18  
19 6 our $VERSION = '0.906'; # VERSION
20  
21 8 ▶sub _write_headers {
Subroutine _write_headers: called CC=5 SCAR=16.4
22 8 my $self = shift;
23 8 my $fh = shift;
24  
25 8 my $kdbx = $self->kdbx;
26 8 my $headers = $kdbx->headers;
27 8 my $buf = '';
28  
29 # FIXME kinda janky - maybe add a "prepare" hook to massage the KDBX into the correct shape before we get
30 # this far
31 8 local $headers->{+HEADER_TRANSFORM_SEED} = $kdbx->transform_seed;
32 8 local $headers->{+HEADER_TRANSFORM_ROUNDS} = $kdbx->transform_rounds;
33  
34 8 my $got_iv_size = length($headers->{+HEADER_ENCRYPTION_IV});
35 8 ▶ alert 'Encryption IV should be exactly 16 bytes long',
Branch: 1 branch
BranchTrueFalseTotal
if $got_iv_size != 16 0 8 8
36 got => $got_iv_size,
37 expected => 16 if $got_iv_size != 16;
38  
39 8 ▶ if (nonempty (my $comment = $headers->{+HEADER_COMMENT})) {
Branch: 1 branch
BranchTrueFalseTotal
if (nonempty(my $comment = $headers->{"comment"})) 0 8 8
40 0 $buf .= $self->_write_header($fh, HEADER_COMMENT, $comment);
41 }
42 8 for my $type (
43 HEADER_CIPHER_ID,
44 HEADER_COMPRESSION_FLAGS,
45 HEADER_MASTER_SEED,
46 HEADER_TRANSFORM_SEED,
47 HEADER_TRANSFORM_ROUNDS,
48 HEADER_ENCRYPTION_IV,
49 HEADER_INNER_RANDOM_STREAM_KEY,
50 HEADER_STREAM_START_BYTES,
51 HEADER_INNER_RANDOM_STREAM_ID,
52 ) {
53 72 ▶ defined $headers->{$type} or throw "Missing value for required header: $type", type => $type;
Branch: 1 branch
BranchTrueFalseTotal
defined $headers->{$type} or throw("Missing value for required header: $type", "type", $type) 72 0 72
54 72 $buf .= $self->_write_header($fh, $type, $headers->{$type});
55 }
56 8 $buf .= $self->_write_header($fh, HEADER_END);
57  
58 8 return $buf;
59 }
60  
61 80 ▶sub _write_header {
Subroutine _write_header: called CC=19 SCAR=32.7
62 80 my $self = shift;
63 80 my $fh = shift;
64 80 my $type = shift;
65 80 ▶ my $val = shift // '';
Condition100%2/2
shift() // ""
l!l
728
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
66  
67 80 $type = to_header_constant($type);
68 80 ▶ if ($type == HEADER_END) {
Branch: 13 branches
BranchTrueFalseTotal
if ($type == 0) { } 8 72 80
elsif ($type == 1) { } 0 72 72
elsif ($type == 2) { } 8 64 72
elsif ($type == 3) { } 8 56 64
elsif ($type == 4) { } 8 48 56
elsif ($type == 5) { } 8 40 48
elsif ($type == 6) { } 8 32 40
elsif ($type == 7) { } 8 24 32
elsif ($type == 8) { } 8 16 24
elsif ($type == 9) { } 8 8 16
elsif ($type == 10) { } 8 0 8
elsif ($type == 11 or $type == 12) { } 0 0 0
elsif ($type == 1) { } 0 0 0
69 8 $val = "\r\n\r\n";
70 }
71 elsif ($type == HEADER_COMMENT) {
72 0 $val = encode('UTF-8', $val);
73 }
74 elsif ($type == HEADER_CIPHER_ID) {
75 8 my $size = length($val);
76 8 ▶ $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) 8 0 8
77 }
78 elsif ($type == HEADER_COMPRESSION_FLAGS) {
79 8 $val = pack('L<', $val);
80 }
81 elsif ($type == HEADER_MASTER_SEED) {
82 8 my $size = length($val);
83 8 ▶ $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) 8 0 8
84 }
85 elsif ($type == HEADER_TRANSFORM_SEED) {
86 # nothing
87 }
88 elsif ($type == HEADER_TRANSFORM_ROUNDS) {
89 8 $val = pack_Ql($val);
90 }
91 elsif ($type == HEADER_ENCRYPTION_IV) {
92 # nothing
93 }
94 elsif ($type == HEADER_INNER_RANDOM_STREAM_KEY) {
95 # nothing
96 }
97 elsif ($type == HEADER_STREAM_START_BYTES) {
98 # nothing
99 }
100 elsif ($type == HEADER_INNER_RANDOM_STREAM_ID) {
101 8 $val = pack('L<', $val);
102 }
103 ▶ 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
104 $type == HEADER_PUBLIC_CUSTOM_DATA) {
105 0 throw "Unexpected KDBX4 header: $type", type => $type;
106 }
107 elsif ($type == HEADER_COMMENT) {
108 0 throw "Unexpected KDB header: $type", type => $type;
109 }
110 else {
111 0 alert "Unknown header: $type", type => $type;
112 }
113  
114 80 my $size = length($val);
115 80 my $buf = pack('C S<', 0+$type, $size);
116  
117 80 ▶ $fh->print($buf, $val) or throw 'Failed to write header';
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($buf, $val) or throw("Failed to write header") 80 0 80
118  
119 80 return "$buf$val";
120 }
121  
122 8 ▶sub _write_body {
Subroutine _write_body: called CC=7 SCAR=19.9
123 8 my $self = shift;
124 8 my $fh = shift;
125 8 my $key = shift;
126 8 my $header_data = shift;
127 8 my $kdbx = $self->kdbx;
128  
129 # assert all required headers present
130 8 for my $field (
131 HEADER_CIPHER_ID,
132 HEADER_ENCRYPTION_IV,
133 HEADER_MASTER_SEED,
134 HEADER_INNER_RANDOM_STREAM_KEY,
135 HEADER_STREAM_START_BYTES,
136 ) {
137 40 ▶ defined $kdbx->headers->{$field} or throw "Missing $field";
Branch: 1 branch
BranchTrueFalseTotal
defined $kdbx->headers->{$field} or throw("Missing $field") 40 0 40
138 }
139  
140 8 my $master_seed = $kdbx->headers->{+HEADER_MASTER_SEED};
141  
142 8 my @cleanup;
143 8 $key = $kdbx->composite_key($key);
144  
145 8 my $response = $key->challenge($master_seed);
146 8 push @cleanup, erase_scoped $response;
147  
148 8 my $transformed_key = $kdbx->kdf->transform($key);
149 8 push @cleanup, erase_scoped $transformed_key;
150  
151 8 my $final_key = digest_data('SHA256', $master_seed, $response, $transformed_key);
152 8 push @cleanup, erase_scoped $final_key;
153  
154 8 my $cipher = $kdbx->cipher(key => $final_key);
155 8 $fh = File::KDBX::IO::Crypt->new($fh, cipher => $cipher);
156  
157 8 ▶ $fh->print($kdbx->headers->{+HEADER_STREAM_START_BYTES})
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($kdbx->headers->{"stream_start_bytes"}) or throw("Failed to write start bytes") 8 0 8
158 or throw 'Failed to write start bytes';
159  
160 8 $kdbx->key($key);
161  
162 8 $fh = File::KDBX::IO::HashBlock->new($fh);
163  
164 8 my $compress = $kdbx->headers->{+HEADER_COMPRESSION_FLAGS};
165 8 ▶ if ($compress == COMPRESSION_GZIP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($compress == 1) { } 8 0 8
elsif ($compress != 0) { } 0 0 0
166 8 load_optional('IO::Compress::Gzip');
167 8 ▶ $fh = IO::Compress::Gzip->new($fh,
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::Compress::Gzip"->new($fh, "-Level", IO::Compress::Gzip::Z_BEST_COMPRESSION(), "-TextFlag", 1) or throw("Failed to initialize compression library: $IO::Compress::Gzip::GzipError", "error", $IO::Compress::Gzip::GzipError) 8 0 8
168 -Level => IO::Compress::Gzip::Z_BEST_COMPRESSION(),
169 -TextFlag => 1,
170 ) or throw "Failed to initialize compression library: $IO::Compress::Gzip::GzipError",
171 error => $IO::Compress::Gzip::GzipError;
172 }
173 elsif ($compress != COMPRESSION_NONE) {
174 0 throw "Unsupported compression ($compress)\n", compression_flags => $compress;
175 }
176  
177 8 my $header_hash = digest_data('SHA256', $header_data);
178 8 $self->_write_inner_body($fh, $header_hash);
179 }
180  
181 6 1;
182  
183 __END__
« KDBX/Loader/V3.pm ↑ Summary KDBX/Key/YubiKey.pm »