1 package File
::KDBX
::Key
;
2 # ABSTRACT: A credential that can protect a KDBX file
7 use Devel
::GlobalDestruction
;
10 use File
::KDBX
::Util
qw(erase);
12 use Ref
::Util
qw(is_arrayref is_coderef is_hashref is_ref is_scalarref);
13 use Scalar
::Util
qw(blessed openhandle refaddr);
16 our $VERSION = '999.999'; # VERSION
22 $key = File
::KDBX
::Key-
>new({ password
=> $password });
23 $key = File
::KDBX
::Key-
>new($password);
25 $key = File
::KDBX
::Key-
>new({ file
=> $filepath });
26 $key = File
::KDBX
::Key-
>new(\
$file);
27 $key = File
::KDBX
::Key-
>new(\
*FILE
);
29 $key = File
::KDBX
::Key-
>new({ composite
=> [...] });
30 $key = File
::KDBX
::Key-
>new([...]); # composite key
32 $key = File
::KDBX
::Key-
>new({ responder
=> \
&responder
});
33 $key = File
::KDBX
::Key-
>new(\
&responder
); # challenge-response key
37 The primitive used to construct the key
is not saved but
is immediately converted to a raw encryption key
(see
40 A L
<File
::KDBX
::Key
::Composite
> is somewhat special
in that it
does retain a reference to its component
keys,
41 and its raw key
is calculated from its components on demand
.
47 my %args = @_ % 2 == 1 ? (primitive
=> shift, @_) : @_;
49 my $primitive = $args{primitive
};
50 delete $args{primitive
} if !$args{keep_primitive
};
51 return $primitive->hide if blessed
$primitive && $primitive->isa($class);
53 my $self = bless \
%args, $class;
54 return $self->init($primitive) if defined $primitive;
58 sub DESTROY
{ !in_global_destruction
and do { $_[0]->_clear_raw_key; erase \
$_[0]->{primitive
} } }
62 $key = $key->init($primitive);
64 Initialize a L
<File
::KDBX
::Key
> with a new primitive
. Returns itself to allow
method chaining
.
70 my $primitive = shift // throw
'Missing key primitive';
74 if (is_arrayref
($primitive)) {
75 $pkg = __PACKAGE__
.'::Composite';
77 elsif (is_scalarref
($primitive) || openhandle
($primitive)) {
78 $pkg = __PACKAGE__
.'::File';
80 elsif (is_coderef
($primitive)) {
81 $pkg = __PACKAGE__
.'::ChallengeResponse';
83 elsif (!is_ref
($primitive)) {
84 $pkg = __PACKAGE__
.'::Password';
86 elsif (is_hashref
($primitive) && defined $primitive->{composite
}) {
87 $pkg = __PACKAGE__
.'::Composite';
88 $primitive = $primitive->{composite
};
90 elsif (is_hashref
($primitive) && defined $primitive->{password
}) {
91 $pkg = __PACKAGE__
.'::Password';
92 $primitive = $primitive->{password
};
94 elsif (is_hashref
($primitive) && defined $primitive->{file
}) {
95 $pkg = __PACKAGE__
.'::File';
96 $primitive = $primitive->{file
};
98 elsif (is_hashref
($primitive) && defined $primitive->{responder
}) {
99 $pkg = __PACKAGE__
.'::ChallengeResponse';
100 $primitive = $primitive->{responder
};
103 throw
'Invalid key primitive', primitive
=> $primitive;
108 return $self->init($primitive);
115 Reload a key by re-reading the key source
and recalculating the raw key
. Returns itself to allow
method
124 $raw_key = $key->raw_key;
125 $raw_key = $key->raw_key($challenge);
127 Get the raw encryption key
. This
is calculated based on the primitive
(s
). The C
<$challenge> argument
is for
128 challenge-response type
keys and is ignored by other types
.
130 B
<NOTE
:> The raw key
is sensitive information
and so
is memory-protected
while not being accessed
. If you
131 access it
, you should L
<File
::KDBX
::Util
/erase
> it
when you
're done.
137 return $self->{raw_key} if !$self->is_hidden;
138 return $self->_safe->peek(\$self->{raw_key});
143 $self->_clear_raw_key;
144 $self->{raw_key} = shift; # after clear
145 $self->_new_safe->add(\$self->{raw_key}); # auto-hide
150 my $safe = $self->_safe;
151 $safe->clear if $safe;
152 erase \$self->{raw_key};
159 Encrypt the raw key for L<File::KDBX/"Memory Protection>. Returns itself to allow method chaining.
165 $self->_new_safe->add(\$self->{raw_key}) if defined $self->{raw_key};
173 Decrypt the raw key so it can be accessed. Returns itself to allow method chaining.
175 You normally don't need to call this because L
</raw_key
> calls this implicitly
.
181 my $safe = $self->_safe;
182 $safe->unlock if $safe;
186 sub is_hidden
{ !!$SAFE{refaddr
($_[0])} }
190 # require Scope::Guard;
195 sub _safe
{ $SAFE{refaddr
($_[0])} }
196 sub _new_safe
{ $SAFE{refaddr
($_[0])} = File
::KDBX
::Safe-
>new }
203 A master key is one or more credentials that can protect a KDBX database. When you encrypt a database with
204 a master key, you will need the master key to decrypt it. B<Keep your master key safe!> If someone gains
205 access to your master key, they can open your database. If you forget or lose any part of your master key, all
206 data in the database is lost.
208 There are several different types of keys, each implemented as a subclass:
211 * L<File::KDBX::Key::Password> - Password or passphrase, knowledge of a string of characters
212 * L<File::KDBX::Key::File> - Possession of a file ("key file") with a secret.
213 * L<File::KDBX::Key::ChallengeResponse> - Possession of a device that responds correctly when challenged
214 * L<File::KDBX::Key::YubiKey> - Possession of a YubiKey hardware device (a type of challenge-response)
215 * L<File::KDBX::Key::Composite> - One or more keys combined as one
217 A good master key is produced from a high amount of "entropy" (unpredictability). The more entropy the better.
218 Combining multiple keys into a B<Composite> key combines the entropy of each individual key. For example, if
219 you have a weak password and you combine it with other keys, the composite key is stronger than the weak
220 password key by itself. (Of course it's much better to not have any weak components of your master key.)
222 B<COMPATIBILITY NOTE:> Most KeePass implementations are limited in the types and numbers of keys they support.
223 B<Password> keys are pretty much universally supported. B<File> keys are pretty well-supported. Many do not
224 support challenge-response keys. If you are concerned about compatibility, you should stick with one of these
230 * One password and one key file