1 package File
::KDBX
::Loader
::V3
;
2 # ABSTRACT: Load KDBX3 files
19 use Crypt
::Digest
qw(digest_data);
20 use Encode
qw(decode);
21 use File
::KDBX
::Constants
qw(:header :compression :kdf);
22 use File
::KDBX
::Error
;
23 use File
::KDBX
::Util
qw(:io assert_64bit erase_scoped);
24 use PerlIO
::via
::File
::KDBX
::Crypt
;
25 use PerlIO
::via
::File
::KDBX
::HashBlock
;
28 use parent
'File::KDBX::Loader';
30 our $VERSION = '999.999'; # VERSION
36 read_all
$fh, my $buf, 3 or throw
'Malformed header field, expected header type and size';
37 my ($type, $size) = unpack('C S<', $buf);
41 read_all
$fh, $val, $size or throw
'Expected header value', type
=> $type, size
=> $size;
45 $type = KDBX_HEADER
($type);
46 if ($type == HEADER_END
) {
49 elsif ($type == HEADER_COMMENT
) {
50 $val = decode
('UTF-8', $val);
52 elsif ($type == HEADER_CIPHER_ID
) {
53 $size == 16 or throw
'Invalid cipher UUID length', got
=> $size, expected
=> $size;
55 elsif ($type == HEADER_COMPRESSION_FLAGS
) {
56 $val = unpack('L<', $val);
58 elsif ($type == HEADER_MASTER_SEED
) {
59 $size == 32 or throw
'Invalid master seed length', got
=> $size, expected
=> $size;
61 elsif ($type == HEADER_TRANSFORM_SEED
) {
64 elsif ($type == HEADER_TRANSFORM_ROUNDS
) {
66 $val = unpack('Q<', $val);
68 elsif ($type == HEADER_ENCRYPTION_IV
) {
71 elsif ($type == HEADER_INNER_RANDOM_STREAM_KEY
) {
74 elsif ($type == HEADER_STREAM_START_BYTES
) {
77 elsif ($type == HEADER_INNER_RANDOM_STREAM_ID
) {
78 $val = unpack('L<', $val);
80 elsif ($type == HEADER_KDF_PARAMETERS
||
81 $type == HEADER_PUBLIC_CUSTOM_DATA
) {
82 throw
"Unexpected KDBX4 header: $type", type
=> $type;
85 alert
"Unknown header: $type", type
=> $type;
88 return wantarray ? ($type => $val, $buf) : $buf;
95 my $header_data = shift;
96 my $kdbx = $self->kdbx;
98 # assert all required headers present
101 HEADER_ENCRYPTION_IV
,
103 HEADER_INNER_RANDOM_STREAM_KEY
,
104 HEADER_STREAM_START_BYTES
,
106 defined $kdbx->headers->{$field} or throw
"Missing $field";
109 $kdbx->kdf_parameters({
110 KDF_PARAM_UUID
() => KDF_UUID_AES
,
111 KDF_PARAM_AES_ROUNDS
() => delete $kdbx->headers->{+HEADER_TRANSFORM_ROUNDS
},
112 KDF_PARAM_AES_SEED
() => delete $kdbx->headers->{+HEADER_TRANSFORM_SEED
},
115 my $master_seed = $kdbx->headers->{+HEADER_MASTER_SEED
};
118 $key = $kdbx->composite_key($key);
120 my $response = $key->challenge($master_seed);
121 push @cleanup, erase_scoped
$response;
123 my $transformed_key = $kdbx->kdf->transform($key);
124 push @cleanup, erase_scoped
$transformed_key;
126 my $final_key = digest_data
('SHA256', $master_seed, $response, $transformed_key);
127 push @cleanup, erase_scoped
$final_key;
129 my $cipher = $kdbx->cipher(key
=> $final_key);
130 PerlIO
::via
::File
::KDBX
::Crypt-
>push($fh, $cipher);
132 read_all
$fh, my $start_bytes, 32 or throw
'Failed to read starting bytes';
134 my $expected_start_bytes = $kdbx->headers->{stream_start_bytes
};
135 $start_bytes eq $expected_start_bytes
136 or throw
"Invalid credentials or data is corrupt (wrong starting bytes)\n",
137 got
=> $start_bytes, expected
=> $expected_start_bytes, headers
=> $kdbx->headers;
141 PerlIO
::via
::File
::KDBX
::HashBlock-
>push($fh);
143 my $compress = $kdbx->headers->{+HEADER_COMPRESSION_FLAGS
};
144 if ($compress == COMPRESSION_GZIP
) {
145 require PerlIO
::via
::File
::KDBX
::Compression
;
146 PerlIO
::via
::File
::KDBX
::Compression-
>push($fh);
148 elsif ($compress != COMPRESSION_NONE
) {
149 throw
"Unsupported compression ($compress)\n", compression_flags
=> $compress;
152 $self->_read_inner_body($fh);
154 binmode($fh, ':pop') if $compress;
155 binmode($fh, ':pop:pop');
157 if (my $header_hash = $kdbx->meta->{header_hash
}) {
158 my $got_header_hash = digest_data
('SHA256', $header_data);
159 $header_hash eq $got_header_hash
160 or throw
'Header hash does not match', got
=> $got_header_hash, expected
=> $header_hash;