KDBX/Dumper/V3.pm

filter: Statementstmt 94.0% 94 / 100 Branchbran 63.0% 29 / 46 Conditioncond 40.0% 2 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 83.3% 140 / 168 SCAR 34.7
CC
28
Cov
83%
_write_header
26.2
_write_body
7.3
_write_headers
4.1
CRAP 32.0
« KDBX/IO/Crypt.pm ↑ Summary KDBX/Loader/V3.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.904'; # VERSION
20  
21 sub _write_headers {
22 8 ▶ my $self = shift;
Subroutine _write_headers: called CC=4 SCAR=14.0
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 ▶ if (nonempty (my $comment = $headers->{+HEADER_COMMENT})) {
Branch: 1 branch
BranchTrueFalseTotal
if (nonempty(my $comment = $headers->{"comment"})) 0 8 8
35 0 $buf .= $self->_write_header($fh, HEADER_COMMENT, $comment);
36 }
37 8 for my $type (
38 HEADER_CIPHER_ID,
39 HEADER_COMPRESSION_FLAGS,
40 HEADER_MASTER_SEED,
41 HEADER_TRANSFORM_SEED,
42 HEADER_TRANSFORM_ROUNDS,
43 HEADER_ENCRYPTION_IV,
44 HEADER_INNER_RANDOM_STREAM_KEY,
45 HEADER_STREAM_START_BYTES,
46 HEADER_INNER_RANDOM_STREAM_ID,
47 ) {
48 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) 0 72 72
49 72 $buf .= $self->_write_header($fh, $type, $headers->{$type});
50 }
51 8 $buf .= $self->_write_header($fh, HEADER_END);
52  
53 8 return $buf;
54 }
55  
56 sub _write_header {
57 80 ▶ my $self = shift;
Subroutine _write_header: called CC=19 SCAR=32.7
58 80 my $fh = shift;
59 80 my $type = shift;
60 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
61  
62 80 $type = to_header_constant($type);
63 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
64 8 $val = "\r\n\r\n";
65 }
66 elsif ($type == HEADER_COMMENT) {
67 0 $val = encode('UTF-8', $val);
68 }
69 elsif ($type == HEADER_CIPHER_ID) {
70 8 my $size = length($val);
71 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) 0 8 8
72 }
73 elsif ($type == HEADER_COMPRESSION_FLAGS) {
74 8 $val = pack('L<', $val);
75 }
76 elsif ($type == HEADER_MASTER_SEED) {
77 8 my $size = length($val);
78 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) 0 8 8
79 }
80 elsif ($type == HEADER_TRANSFORM_SEED) {
81 # nothing
82 }
83 elsif ($type == HEADER_TRANSFORM_ROUNDS) {
84 8 $val = pack_Ql($val);
85 }
86 elsif ($type == HEADER_ENCRYPTION_IV) {
87 # nothing
88 }
89 elsif ($type == HEADER_INNER_RANDOM_STREAM_KEY) {
90 # nothing
91 }
92 elsif ($type == HEADER_STREAM_START_BYTES) {
93 # nothing
94 }
95 elsif ($type == HEADER_INNER_RANDOM_STREAM_ID) {
96 8 $val = pack('L<', $val);
97 }
98 ▶ 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
99 $type == HEADER_PUBLIC_CUSTOM_DATA) {
100 0 throw "Unexpected KDBX4 header: $type", type => $type;
101 }
102 elsif ($type == HEADER_COMMENT) {
103 0 throw "Unexpected KDB header: $type", type => $type;
104 }
105 else {
106 0 alert "Unknown header: $type", type => $type;
107 }
108  
109 80 my $size = length($val);
110 80 my $buf = pack('C S<', 0+$type, $size);
111  
112 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") 0 80 80
113  
114 80 return "$buf$val";
115 }
116  
117 sub _write_body {
118 8 ▶ my $self = shift;
Subroutine _write_body: called CC=7 SCAR=19.9
119 8 my $fh = shift;
120 8 my $key = shift;
121 8 my $header_data = shift;
122 8 my $kdbx = $self->kdbx;
123  
124 # assert all required headers present
125 8 for my $field (
126 HEADER_CIPHER_ID,
127 HEADER_ENCRYPTION_IV,
128 HEADER_MASTER_SEED,
129 HEADER_INNER_RANDOM_STREAM_KEY,
130 HEADER_STREAM_START_BYTES,
131 ) {
132 40 ▶ defined $kdbx->headers->{$field} or throw "Missing $field";
Branch: 1 branch
BranchTrueFalseTotal
defined $kdbx->headers->{$field} or throw("Missing $field") 0 40 40
133 }
134  
135 8 my $master_seed = $kdbx->headers->{+HEADER_MASTER_SEED};
136  
137 8 my @cleanup;
138 8 $key = $kdbx->composite_key($key);
139  
140 8 my $response = $key->challenge($master_seed);
141 8 push @cleanup, erase_scoped $response;
142  
143 8 my $transformed_key = $kdbx->kdf->transform($key);
144 8 push @cleanup, erase_scoped $transformed_key;
145  
146 8 my $final_key = digest_data('SHA256', $master_seed, $response, $transformed_key);
147 8 push @cleanup, erase_scoped $final_key;
148  
149 8 my $cipher = $kdbx->cipher(key => $final_key);
150 8 $fh = File::KDBX::IO::Crypt->new($fh, cipher => $cipher);
151  
152 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") 0 8 8
153 or throw 'Failed to write start bytes';
154  
155 8 $kdbx->key($key);
156  
157 8 $fh = File::KDBX::IO::HashBlock->new($fh);
158  
159 8 my $compress = $kdbx->headers->{+HEADER_COMPRESSION_FLAGS};
160 8 ▶ if ($compress == COMPRESSION_GZIP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($compress == 1) { } 8 0 8
elsif ($compress != 0) { } 0 0 0
161 8 load_optional('IO::Compress::Gzip');
162 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) 0 8 8
163 -Level => IO::Compress::Gzip::Z_BEST_COMPRESSION(),
164 -TextFlag => 1,
165 ) or throw "Failed to initialize compression library: $IO::Compress::Gzip::GzipError",
166 error => $IO::Compress::Gzip::GzipError;
167 }
168 elsif ($compress != COMPRESSION_NONE) {
169 0 throw "Unsupported compression ($compress)\n", compression_flags => $compress;
170 }
171  
172 8 my $header_hash = digest_data('SHA256', $header_data);
173 8 $self->_write_inner_body($fh, $header_hash);
174 }
175  
176 6 1;
177  
178 __END__
« KDBX/IO/Crypt.pm ↑ Summary KDBX/Loader/V3.pm »