| 1 | package File::KDBX::Dumper::KDB; | ||||||||||||||||||||
| 2 | # ABSTRACT: Write KDB files | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use Crypt::PRNG qw(irand); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use Encode qw(encode); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use File::KDBX::Constants qw(:magic); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use File::KDBX::Error; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use File::KDBX::Loader::KDB; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use File::KDBX::Util qw(:class :uuid load_optional); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | extends 'File::KDBX::Dumper'; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 1 | our $VERSION = '0.905'; # VERSION | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 0 | ▶ | sub _write_magic_numbers { '' } | ||||||||||||||||||
|
Subroutine
_write_magic_numbers: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 20 | 0 | ▶ | sub _write_headers { '' } | ||||||||||||||||||
|
Subroutine
_write_headers: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | sub _write_body { | ||||||||||||||||||||
| 23 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_write_body: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 24 | 0 | my $fh = shift; | |||||||||||||||||||
| 25 | 0 | my $key = shift; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 0 | load_optional(qw{File::KeePass File::KeePass::KDBX}); | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | my $k = File::KeePass::KDBX->new($self->kdbx)->to_fkp; | |||||||||||||||||||
| 30 | 0 | $self->_write_custom_icons($self->kdbx, $k); | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | substr($k->header->{seed_rand}, 16) = ''; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | $key = $self->kdbx->composite_key($key, keep_primitive => 1); | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 0 | my $dump = eval { $k->gen_db(File::KDBX::Loader::KDB::_convert_kdbx_to_keepass_master_key($key)) }; | |||||||||||||||||||
| 37 | 0 | ▶ | if (my $err = $@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 0 | throw 'Failed to generate KDB file', error => $err; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | $self->kdbx->key($key); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | print $fh $dump; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | sub _write_custom_icons { | ||||||||||||||||||||
| 47 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_write_custom_icons: not called CC=22 SCAR=62.3
| |||||||||||||||||||||
| 48 | 0 | my $kdbx = shift; | |||||||||||||||||||
| 49 | 0 | my $k = shift; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | ▶ | return if $kdbx->sig2 != KDBX_SIG2_1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 0 | ▶ | return if $k->find_entries({ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | title => 'Meta-Info', | ||||||||||||||||||||
| 54 | username => 'SYSTEM', | ||||||||||||||||||||
| 55 | url => '$', | ||||||||||||||||||||
| 56 | comment => 'KPX_CUSTOM_ICONS_4', | ||||||||||||||||||||
| 57 | }); | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | my @icons; # icon data | |||||||||||||||||||
| 60 | my %icons; # icon uuid -> index | ||||||||||||||||||||
| 61 | 0 | my %entries; # id -> index | |||||||||||||||||||
| 62 | 0 | my %groups; # id -> index | |||||||||||||||||||
| 63 | 0 | my %gid; | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | for my $icon (@{$kdbx->custom_icons}) { | |||||||||||||||||||
| 66 | 0 | my $uuid = $icon->{uuid}; | |||||||||||||||||||
| 67 | 0 | ▶ | my $data = $icon->{data} or next; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 0 | push @icons, $data; | |||||||||||||||||||
| 69 | 0 | $icons{$uuid} = $#icons; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | 0 | for my $entry ($k->find_entries({})) { | |||||||||||||||||||
| 72 | 0 | ▶ | my $icon_uuid = $entry->{custom_icon_uuid} // next; | ||||||||||||||||||
|
Condition0%0/2
$entry->{"custom_icon_uuid"} // (next)
MC/DC0%0/1
$entry->{"custom_icon_uuid"} // (next)
$entry->{"custom_icon_uuid"}
Truth table0%0/2
A
A= $entry->{"custom_icon_uuid"}
| |||||||||||||||||||||
| 73 | 0 | ▶ | my $icon_index = $icons{$icon_uuid} // next; | ||||||||||||||||||
|
Condition0%0/2
$icons{$icon_uuid} // (next)
MC/DC0%0/1
$icons{$icon_uuid} // (next)
$icons{$icon_uuid}
Truth table0%0/2
A
A= $icons{$icon_uuid}
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | ▶ | $entry->{id} //= generate_uuid; | ||||||||||||||||||
|
Condition0%0/3
$entry->{"id"} //= generate_uuid()
MC/DC0%0/2
$entry->{"id"} //= generate_uuid()
$entry->{"id"}
generate_uuid()
Truth table0%0/3
A //= B
A= $entry->{"id"}B= generate_uuid()
| |||||||||||||||||||||
| 76 | 0 | ▶ | next if $entries{$entry->{id}}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | $entries{$entry->{id}} = $icon_index; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | 0 | for my $group ($k->find_groups({})) { | |||||||||||||||||||
| 81 | 0 | ▶ | $gid{$group->{id} || ''}++; | ||||||||||||||||||
|
Condition0%0/2
$group->{'id'} || ''
MC/DC0%0/1
$group->{'id'} || ''
$group->{'id'}
Truth table0%0/2
A
A= $group->{'id'}
| |||||||||||||||||||||
| 82 | 0 | ▶ | my $icon_uuid = $group->{custom_icon_uuid} // next; | ||||||||||||||||||
|
Condition0%0/2
$group->{"custom_icon_uuid"} // (next)
MC/DC0%0/1
$group->{"custom_icon_uuid"} // (next)
$group->{"custom_icon_uuid"}
Truth table0%0/2
A
A= $group->{"custom_icon_uuid"}
| |||||||||||||||||||||
| 83 | 0 | ▶ | my $icon_index = $icons{$icon_uuid} // next; | ||||||||||||||||||
|
Condition0%0/2
$icons{$icon_uuid} // (next)
MC/DC0%0/1
$icons{$icon_uuid} // (next)
$icons{$icon_uuid}
Truth table0%0/2
A
A= $icons{$icon_uuid}
| |||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | ▶ | if ($group->{id} =~ /^[A-Fa-f0-9]{16}$/) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 86 | 0 | $group->{id} = hex($group->{id}); | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | elsif ($group->{id} !~ /^\d+$/) { | ||||||||||||||||||||
| 89 | do { | ||||||||||||||||||||
| 90 | 0 | $group->{id} = irand; | |||||||||||||||||||
| 91 | 0 | ▶ | } while $gid{$group->{id}}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 0 | $gid{$group->{id}}++; | |||||||||||||||||||
| 94 | 0 | ▶ | next if $groups{$group->{id}}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | $groups{$group->{id}} = $icon_index; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | ▶ | return if !@icons; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | my $stream = ''; | |||||||||||||||||||
| 102 | 0 | $stream .= pack('L<3', scalar @icons, scalar keys %entries, scalar keys %groups); | |||||||||||||||||||
| 103 | 0 | ▶ | for (my $i = 0; $i < @icons; ++$i) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 0 | $stream .= pack('L<', length($icons[$i])); | |||||||||||||||||||
| 105 | 0 | $stream .= $icons[$i]; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | 0 | ▶ | while (my ($id, $icon_index) = each %entries) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 0 | $stream .= pack('a16 L<', $id, $icon_index); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | 0 | ▶ | while (my ($id, $icon_index) = each %groups) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 0 | $stream .= pack('L<2', $id, $icon_index); | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | $k->add_entry({ | ||||||||||||||||||||
| 115 | 0 | comment => 'KPX_CUSTOM_ICONS_4', | |||||||||||||||||||
| 116 | title => 'Meta-Info', | ||||||||||||||||||||
| 117 | username => 'SYSTEM', | ||||||||||||||||||||
| 118 | url => '$', | ||||||||||||||||||||
| 119 | id => '0' x 16, | ||||||||||||||||||||
| 120 | icon => 0, | ||||||||||||||||||||
| 121 | binary => {'bin-stream' => $stream}, | ||||||||||||||||||||
| 122 | }); | ||||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 1 | 1; | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||