KDBX/Key/YubiKey.pm

filter: Statementstmt 85.0% 199 / 234 Branchbran 55.7% 78 / 140 Conditioncond 52.8% 47 / 89 MC/DCmcdc 20.8% 10 / 48 Subroutinesub 84.2% 32 / 38 Podpod 100% 16 / 16 totaltotal 67.6% 382 / 565 CC 107 SCAR 25.4
CC
107
Cov
70%
_yk_errno
34.0
scan
26.0
_get_yubikey_info
20.0
CRAP 12.7
« KDBX/Dumper.pm ↑ Summary KDBX/Key/ChallengeResponse.pm »
1 package File::KDBX::Key::YubiKey;
2 # ABSTRACT: A Yubico challenge-response key
3  
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use File::KDBX::Constants qw(:yubikey);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::KDBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::KDBX::Util qw(:class :io pad_pkcs7);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use IPC::Cmd 0.84 qw(run_forked);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Ref::Util qw(is_arrayref);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 extends 'File::KDBX::Key::ChallengeResponse';
16  
17 2 our $VERSION = '0.905'; # VERSION
18  
19 # It can take some time for the USB device to be ready again, so we can retry a few times.
20 2 our $RETRY_COUNT = 5;
21 2 our $RETRY_INTERVAL = 0.1;
22  
23 2 my @CONFIG_VALID = (0, CONFIG1_VALID, CONFIG2_VALID);
24 2 my @CONFIG_TOUCH = (0, CONFIG1_TOUCH, CONFIG2_TOUCH);
25  
26 sub challenge {
27 8 ▶ my $self = shift;
Subroutine challenge: called CC=18 SCAR=29.6
Pod documented
28 8 my $challenge = shift;
29 8 my %args = @_;
30  
31 8 ▶ my $device = $args{device} // $self->device;
Condition66%2/3
$args{"device"} // $self->device
l!l&&r!l&&!r
044
MC/DC50%1/2
$args{"device"} // $self->device
$args{"device"} $self->device
Truth table66%2/3
A // B
A= $args{"device"}B= $self->device
ABresult
1X1
011
000
32 8 ▶ my $slot = $args{slot} // $self->slot;
Condition33%1/3
$args{"slot"} // $self->slot
l!l&&r!l&&!r
080
MC/DC0%0/2
$args{"slot"} // $self->slot
$args{"slot"} $self->slot
Truth table33%1/3
A // B
A= $args{"slot"}B= $self->slot
ABresult
1X1
011
000
33 8 ▶ my $timeout = $args{timeout} // $self->timeout;
Condition100%3/3
$args{"timeout"} // $self->timeout
l!l&&r!l&&!r
161
MC/DC100%2/2
$args{"timeout"} // $self->timeout
$args{"timeout"} $self->timeout
Truth table100%3/3
A // B
A= $args{"timeout"}B= $self->timeout
ABresult
1X1
011
000
34 8 local $self->{device} = $device;
35 8 local $self->{slot} = $slot;
36 8 local $self->{timeout} = $timeout;
37  
38 8 my $hooks = $challenge ne 'test';
39 8 ▶ if ($hooks and my $hook = $self->{pre_challenge}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hooks and my $hook = $self->{"pre_challenge"}) 1 7 8
Condition100%3/3
$hooks and my $hook = $self->{"pre_challenge"}
!ll&&!rl&&r
161
MC/DC100%2/2
$hooks and my $hook = $self->{"pre_challenge"}
$hooks my $hook = $self->{"pre_challenge"}
Truth table100%3/3
A and B
A= $hooksB= my $hook = $self->{"pre_challenge"}
ABresult
0X0
100
111
40 1 $hook->($self, $challenge);
41 }
42  
43 8 ▶ my @cmd = ($self->_program('ykchalresp'), "-n$device", "-$slot", qw{-H -i-}, $timeout == 0 ? '-N' : ());
Branch: 1 branch
BranchTrueFalseTotal
$timeout == 0 ? : 2 6 8
44  
45 8 my $r;
46 8 my $try = 0;
47 TRY:
48 {
49 8 ▶ $r = $self->_run_ykpers(\@cmd, {
Branch: 1 branch
BranchTrueFalseTotal
0 < $timeout ? : 5 3 8
50 (0 < $timeout ? (timeout => $timeout) : ()),
51 child_stdin => pad_pkcs7($challenge, 64),
52 terminate_on_parent_sudden_death => 1,
53 });
54  
55 7 ▶ if (my $t = $r->{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $r->{"timeout"}) 1 6 7
56 1 throw 'Timed out while waiting for challenge response',
57 command => \@cmd,
58 challenge => $challenge,
59 timeout => $t,
60 result => $r;
61 }
62  
63 6 my $exit_code = $r->{exit_code};
64 6 ▶ if ($exit_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code != 0) 3 3 6
65 3 my $err = $r->{stderr};
66 3 chomp $err;
67 3 my $yk_errno = _yk_errno($err);
68 3 ▶ if ($yk_errno == YK_EUSBERR && $err =~ /resource busy/i && ++$try <= $RETRY_COUNT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yk_errno == "USB error" and $err =~ /resource busy/i and ++$try <= $File::KDBX::Key::YubiKey::RETRY_COUNT) 0 3 3
Condition50%3/6
$yk_errno == "USB error" and $err =~ /resource busy/i and ++$try <= $File::KDBX::Key::YubiKey::RETRY_COUNT
!ll&&!rl&&r
300
$yk_errno == "USB error" and $err =~ /resource busy/i
!ll&&!rl&&r
210
MC/DC0%0/3
$yk_errno == "USB error" and $err =~ /resource busy/i and ++$try <= $File::KDBX::Key::YubiKey::RETRY_COUNT
$yk_errno == "USB error" $err =~ /resource busy/i ++$try <= $File::KDBX::Key::YubiKey::RETRY_COUNT
Truth table50%2/4
A and B and C
A= $yk_errno == "USB error"B= $err =~ /resource busy/iC= ++$try <= $File::KDBX::Key::YubiKey::RETRY_COUNT
ABCresult
0XX0
10X0
1100
1111
69 0 sleep $RETRY_INTERVAL;
70 0 goto TRY;
71 }
72 3 ▶ throw 'Failed to receive challenge response: ' . ($err ? $err : 'Something happened'),
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 3 0 3
Condition50%1/2
$yk_errno || 0
l!l
30
MC/DC0%0/1
$yk_errno || 0
$yk_errno
Truth table50%1/2
A
A= $yk_errno
Aresult
11
00
73 error => $err,
74 yk_errno => $yk_errno || 0;
75 }
76 }
77  
78 3 my $resp = $r->{stdout};
79 3 chomp $resp;
80 3 ▶ $resp =~ /^[A-Fa-f0-9]+$/ or throw 'Unexpected response from challenge', response => $resp, result => $r;
Branch: 1 branch
BranchTrueFalseTotal
$resp =~ /^[A-Fa-f0-9]+$/ or throw("Unexpected response from challenge", "response", $resp, "result", $r) 0 3 3
81 3 $resp = pack('H*', $resp);
82  
83 # HMAC-SHA1 response is only 20 bytes
84 3 substr($resp, 20) = '';
85  
86 3 ▶ if ($hooks and my $hook = $self->{post_challenge}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hooks and my $hook = $self->{"post_challenge"}) 1 2 3
Condition100%3/3
$hooks and my $hook = $self->{"post_challenge"}
!ll&&!rl&&r
111
MC/DC100%2/2
$hooks and my $hook = $self->{"post_challenge"}
$hooks my $hook = $self->{"post_challenge"}
Truth table100%3/3
A and B
A= $hooksB= my $hook = $self->{"post_challenge"}
ABresult
0X0
100
111
87 1 $hook->($self, $challenge, $resp);
88 }
89  
90 3 return $resp;
91 }
92  
93  
94 sub scan {
95 1 ▶ my $self = shift;
Subroutine scan: called CC=13 SCAR=32.6
Pod documented
96 1 my %args = @_;
97  
98 1 ▶ my $limit = delete $args{limit} // 4;
Condition50%1/2
delete $args{"limit"} // 4
l!l
01
MC/DC0%0/1
delete $args{"limit"} // 4
delete $args{"limit"}
Truth table50%1/2
A
A= delete $args{"limit"}
Aresult
11
00
99  
100 1 my @keys;
101 1 ▶ for (my $device = 0; $device < $limit; ++$device) {
Branch: 1 branch
BranchTrueFalseTotal
if ($device < $limit) 3 0 3
102 3 ▶ my %info = $self->_get_yubikey_info($device) or last;
Branch: 1 branch
BranchTrueFalseTotal
my(%info) = $self->_get_yubikey_info($device) or last 1 2 3
103  
104 2 ▶ for (my $slot = 1; $slot <= 2; ++$slot) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot <= 2) 4 2 6
105 4 ▶ my $config = $CONFIG_VALID[$slot] // next;
Condition50%1/2
$CONFIG_VALID[$slot] // (next)
l!l
40
MC/DC0%0/1
$CONFIG_VALID[$slot] // (next)
$CONFIG_VALID[$slot]
Truth table50%1/2
A
A= $CONFIG_VALID[$slot]
Aresult
11
00
106 4 ▶ next unless $info{touch_level} & $config;
Branch: 1 branch
BranchTrueFalseTotal
unless $info{"touch_level"} & $config 3 1 4
107  
108 1 my $key = $self->new(%args, device => $device, slot => $slot, %info);
109 1 ▶ if ($info{product_id} <= NEO_OTP_U2F_CCID_PID) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info{'product_id'} <= 278) { } 0 1 1
110 # NEO and earlier always require touch, so forego testing
111 0 $key->touch_level($info{touch_level} | $CONFIG_TOUCH[$slot]);
112 0 push @keys, $key;
113 }
114 else {
115 1 eval { $key->challenge('test', timeout => 0) };
116 1 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 1 1
117 0 ▶ my $yk_errno = ref $err && $err->details->{yk_errno} || 0;
Condition0%0/5
ref $err && $err->details->{'yk_errno'} || 0
l!l
00
ref $err and $err->details->{"yk_errno"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $err && $err->details->{'yk_errno'} || 0
ref $err $err->details->{"yk_errno"}
Truth table0%0/3
A and B
A= ref $errB= $err->details->{"yk_errno"}
ABresult
111
0X0
100
118 0 ▶ if ($yk_errno == YK_EWOULDBLOCK) {
Branch: 2 branches
BranchTrueFalseTotal
if ($yk_errno == 11) { } 0 0 0
elsif ($yk_errno != 0) { } 0 0 0
119 0 $key->touch_level($info{touch_level} | $CONFIG_TOUCH[$slot]);
120 }
121 elsif ($yk_errno != 0) {
122 # alert $err;
123 0 next;
124 }
125 }
126 1 push @keys, $key;
127 }
128 }
129 }
130  
131 1 return @keys;
132 9 ▶}
Subroutine device: called CC=4 SCAR=14.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) 0 9 9
133 9 ▶ 
Subroutine slot: called CC=4 SCAR=14.9
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 9 9
unless builtin::blessed($_[0]) 0 12 12
134 9 ▶ 
Subroutine timeout: called CC=4 SCAR=16.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 12 12
unless builtin::blessed($_[0]) 3 10 13
Condition66%2/3
$_[0]{"device"} //= $default
l!l&&r!l&&!r
504
MC/DC50%1/2
$_[0]{"device"} //= $default
$_[0]{"device"} $default
Truth table66%2/3
A //= B
A= $_[0]{"device"}B= $default
ABresult
1X1
011
000
135 12 ▶has device => 0;
Subroutine pre_challenge: not called CC=4 SCAR=19.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 2 8 10
unless builtin::blessed($_[0]) 0 0 0
Condition66%2/3
$_[0]{"slot"} //= $default
l!l&&r!l&&!r
840
MC/DC0%0/2
$_[0]{"slot"} //= $default
$_[0]{"slot"} $default
Truth table66%2/3
A //= B
A= $_[0]{"slot"}B= $default
ABresult
1X1
011
000
136 10 ▶has slot => 1;
Subroutine post_challenge: not called CC=4 SCAR=19.6
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 0 0
unless builtin::blessed($_[0]) 0 0 0
Condition66%2/3
$_[0]{"timeout"} //= $default
l!l&&r!l&&!r
550
MC/DC0%0/2
$_[0]{"timeout"} //= $default
$_[0]{"timeout"} $default
Truth table66%2/3
A //= B
A= $_[0]{"timeout"}B= $default
ABresult
1X1
011
000
137 0 ▶has timeout => 10;
Subroutine ykchalresp: called CC=4 SCAR=16.9
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 0 0
unless builtin::blessed($_[0]) 0 8 8
Condition0%0/2
$_[0]{"pre_challenge"} //= undef
l!l
00
MC/DC0%0/1
$_[0]{"pre_challenge"} //= undef
$_[0]{"pre_challenge"}
Truth table0%0/2
A
A= $_[0]{"pre_challenge"}
Aresult
11
00
138 0 ▶has pre_challenge => undef;
Subroutine ykinfo: called CC=4 SCAR=14.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 8 8
unless builtin::blessed($_[0]) 3 1 4
Condition0%0/2
$_[0]{"post_challenge"} //= undef
l!l
00
MC/DC0%0/1
$_[0]{"post_challenge"} //= undef
$_[0]{"post_challenge"}
Truth table0%0/2
A
A= $_[0]{"post_challenge"}
Aresult
11
00
139 8 ▶has post_challenge => undef;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ 0 1 1
Condition100%2/2
$_[0]{"ykchalresp"} //= scalar &$default($_[0])
l!l
26
MC/DC100%1/1
$_[0]{"ykchalresp"} //= scalar &$default($_[0])
$_[0]{"ykchalresp"}
Truth table100%2/2
A
A= $_[0]{"ykchalresp"}
Aresult
11
00
140 1 ▶has ykchalresp => sub { $ENV{YKCHALRESP} || 'ykchalresp' };
Subroutine __ANON__: called CC=2 SCAR=7.4
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"YKCHALRESP"} or "ykchalresp" 0 6 6
Condition50%1/2
$_[0]{"ykinfo"} //= scalar &$default($_[0])
l!l
01
MC/DC0%0/1
$_[0]{"ykinfo"} //= scalar &$default($_[0])
$_[0]{"ykinfo"}
Truth table50%1/2
A
A= $_[0]{"ykinfo"}
Aresult
11
00
141 4 ▶has ykinfo => sub { $ENV{YKINFO} || 'ykinfo' };
Subroutine __ANON__: called CC=2 SCAR=7.4
Subroutine serial: called CC=4 SCAR=15.1
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
$ENV{"YKINFO"} or "ykinfo" 0 4 4
unless builtin::blessed($_[0]) 0 4 4
142 4 ▶ 
Subroutine version: called CC=4 SCAR=15.7
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 4 4
unless builtin::blessed($_[0]) 0 2 2
143 4 ▶ 
Subroutine touch_level: called CC=4 SCAR=16.1
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 2 2
unless builtin::blessed($_[0]) 0 2 2
Condition50%1/2
$_[0]{"serial"} //= scalar &$default($_[0])
l!l
40
MC/DC0%0/1
$_[0]{"serial"} //= scalar &$default($_[0])
$_[0]{"serial"}
Truth table50%1/2
A
A= $_[0]{"serial"}
Aresult
11
00
144 2 ▶has serial => sub { $_[0]->_set_yubikey_info; $_[0]->{serial} };
Subroutine __ANON__: not called CC=1 SCAR=0.7
Subroutine vendor_id: called CC=4 SCAR=16.1
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 2 2
unless builtin::blessed($_[0]) 0 2 2
Condition50%1/2
$_[0]{"version"} //= scalar &$default($_[0])
l!l
20
MC/DC0%0/1
$_[0]{"version"} //= scalar &$default($_[0])
$_[0]{"version"}
Truth table50%1/2
A
A= $_[0]{"version"}
Aresult
11
00
145 2 ▶has version => sub { $_[0]->_set_yubikey_info; $_[0]->{version} };
Subroutine __ANON__: not called CC=1 SCAR=0.7
Subroutine product_id: called CC=4 SCAR=15.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $#_ 0 2 2
unless builtin::blessed($_[0]) 0 2 2
Condition50%1/2
$_[0]{"touch_level"} //= scalar &$default($_[0])
l!l
20
MC/DC0%0/1
$_[0]{"touch_level"} //= scalar &$default($_[0])
$_[0]{"touch_level"}
Truth table50%1/2
A
A= $_[0]{"touch_level"}
Aresult
11
00
146 2 ▶has touch_level => sub { $_[0]->_set_yubikey_info; $_[0]->{touch_level} };
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
if $#_ 0 2 2
Condition100%2/2
$_[0]{"vendor_id"} //= scalar &$default($_[0])
l!l
11
MC/DC100%1/1
$_[0]{"vendor_id"} //= scalar &$default($_[0])
$_[0]{"vendor_id"}
Truth table100%2/2
A
A= $_[0]{"vendor_id"}
Aresult
11
00
147 2 ▶has vendor_id => sub { $_[0]->_set_yubikey_info; $_[0]->{vendor_id} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition50%1/2
$_[0]{"product_id"} //= scalar &$default($_[0])
l!l
20
MC/DC0%0/1
$_[0]{"product_id"} //= scalar &$default($_[0])
$_[0]{"product_id"}
Truth table50%1/2
A
A= $_[0]{"product_id"}
Aresult
11
00
148 0 ▶has product_id => sub { $_[0]->_set_yubikey_info; $_[0]->{product_id} };
Subroutine __ANON__: not called CC=1 SCAR=2.6
149  
150  
151 sub name {
152 2 ▶ my $self = shift;
Subroutine name: called CC=4 SCAR=14.0
Pod documented
153 2 ▶ my $name = _product_name($self->vendor_id, $self->product_id // return);
Condition50%1/2
$self->product_id // (return)
l!l
20
MC/DC0%0/1
$self->product_id // (return)
$self->product_id
Truth table50%1/2
A
A= $self->product_id
Aresult
11
00
154 2 my $serial = $self->serial;
155 2 ▶ my $version = $self->version || '?';
Condition50%1/2
$self->version || '?'
l!l
20
MC/DC0%0/1
$self->version || '?'
$self->version
Truth table50%1/2
A
A= $self->version
Aresult
11
00
156 2 my $slot = $self->slot;
157 2 ▶ my $touch = $self->requires_interaction ? ' - Interaction required' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->requires_interaction ? : 1 1 2
158 2 return sprintf('%s v%s [%d] (slot #%d)', $name, $version, $serial, $slot);
159 }
160  
161  
162 sub requires_interaction {
163 2 ▶ my $self = shift;
Subroutine requires_interaction: called CC=2 SCAR=7.1
Pod documented
164 2 ▶ my $touch = $self->touch_level // return;
Condition50%1/2
$self->touch_level // (return)
l!l
20
MC/DC0%0/1
$self->touch_level // (return)
$self->touch_level
Truth table50%1/2
A
A= $self->touch_level
Aresult
11
00
165 2 return $touch & $CONFIG_TOUCH[$self->slot];
166 }
167  
168 ##############################################################################
169  
170 ### Call ykinfo to get some information about a YubiKey
171 sub _get_yubikey_info {
172 4 ▶ my $self = shift;
Subroutine _get_yubikey_info: called CC=11 SCAR=29.9
173 4 my $device = shift;
174  
175 4 my $timeout = $self->timeout;
176 4 my @cmd = ($self->_program('ykinfo'), "-n$device", qw{-a});
177  
178 4 my $r;
179 4 my $try = 0;
180 TRY:
181 {
182 4 ▶ $r = $self->_run_ykpers(\@cmd, {
Branch: 1 branch
BranchTrueFalseTotal
0 < $timeout ? : 4 0 4
183 (0 < $timeout ? (timeout => $timeout) : ()),
184 terminate_on_parent_sudden_death => 1,
185 });
186  
187 4 my $exit_code = $r->{exit_code};
188 4 ▶ if ($exit_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code != 0) 1 3 4
189 1 my $err = $r->{stderr};
190 1 chomp $err;
191 1 my $yk_errno = _yk_errno($err);
192 1 ▶ return if $yk_errno == YK_ENOKEY;
Branch: 1 branch
BranchTrueFalseTotal
if $yk_errno == "no yubikey present" 1 0 1
193 0 ▶ if ($yk_errno == YK_EWOULDBLOCK && ++$try <= $RETRY_COUNT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($yk_errno == "operation would block" and ++$try <= $File::KDBX::Key::YubiKey::RETRY_COUNT) 0 0 0
Condition0%0/3
$yk_errno == "operation would block" and ++$try <= $File::KDBX::Key::YubiKey::RETRY_COUNT
!ll&&!rl&&r
000
MC/DC0%0/2
$yk_errno == "operation would block" and ++$try <= $File::KDBX::Key::YubiKey::RETRY_COUNT
$yk_errno == "operation would block" ++$try <= $File::KDBX::Key::YubiKey::RETRY_COUNT
Truth table0%0/3
A and B
A= $yk_errno == "operation would block"B= ++$try <= $File::KDBX::Key::YubiKey::RETRY_COUNT
ABresult
0X0
100
111
194 0 sleep $RETRY_INTERVAL;
195 0 goto TRY;
196 }
197 0 ▶ alert 'Failed to get YubiKey device info: ' . ($err ? $err : 'Something happened'),
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 0 0
Condition0%0/2
$yk_errno || 0
l!l
00
MC/DC0%0/1
$yk_errno || 0
$yk_errno
Truth table0%0/2
A
A= $yk_errno
Aresult
11
00
198 error => $err,
199 yk_errno => $yk_errno || 0;
200 0 return;
201 }
202 }
203  
204 3 my $out = $r->{stdout};
205 3 chomp $out;
206 3 ▶ if (!$out) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out) 0 3 3
207 0 alert 'Failed to get YubiKey device info: no output';
208 0 return;
209 }
210  
211 3 my %info = map { $_ => ($out =~ /^\Q$_\E: (.+)$/m)[0] }
212 qw(serial version touch_level vendor_id product_id);
213 3 ▶ $info{vendor_id} = hex($info{vendor_id}) if defined $info{vendor_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info{"vendor_id"} 3 0 3
214 3 ▶ $info{product_id} = hex($info{product_id}) if defined $info{product_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info{"product_id"} 3 0 3
215  
216 3 return %info;
217 }
218  
219 ### Set the YubiKey information as attributes of a Key object
220 sub _set_yubikey_info {
221 1 ▶ my $self = shift;
Subroutine _set_yubikey_info: called CC=1 SCAR=0.0
222 1 my %info = $self->_get_yubikey_info($self->device);
223 1 @$self{keys %info} = values %info;
224 }
225  
226 sub _program {
227 12 ▶ my $self = shift;
Subroutine _program: called CC=3 SCAR=11.1
228 12 my $name = shift;
229 12 ▶ my @cmd = $self->$name // $name;
Condition33%1/3
$self->$name // $name
l!l&&r!l&&!r
1200
MC/DC0%0/2
$self->$name // $name
$self->$name $name
Truth table33%1/3
A // B
A= $self->$nameB= $name
ABresult
1X1
011
000
230 12 my $name_uc = uc($name);
231 12 my $flags = $ENV{"${name_uc}_FLAGS"};
232 12 ▶ push @cmd, split(/\h+/, $flags) if $flags;
Branch: 1 branch
BranchTrueFalseTotal
if $flags 11 1 12
233 12 return @cmd;
234 }
235  
236 sub _run_ykpers {
237 12 ▶ my $self = shift;
Subroutine _run_ykpers: called CC=8 SCAR=22.1
238 12 my $ppid = $$;
239 12 my $r = eval { run_forked(@_) };
240 12 my $err = $@;
241 12 ▶ if ($$ != $ppid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ != $ppid) 0 12 12
242 # Work around IPC::Cmd bug where child can return from run_forked.
243 # https://rt.cpan.org/Public/Bug/Display.html?id=127372
244 0 require POSIX;
245 0 POSIX::_exit(0);
246 }
247 12 ▶ if ($err || ($r->{exit_code} == 0 && $r->{err_msg} eq '' && $r->{stdout} eq '' && $r->{stderr} eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err or $r->{"exit_code"} == 0 and $r->{"err_msg"} eq "" and $r->{"stdout"} eq "" and $r->{"stderr"} eq "") 1 11 12
Condition66%8/12
$err or $r->{"exit_code"} == 0 and $r->{"err_msg"} eq "" and $r->{"stdout"} eq "" and $r->{"stderr"} eq ""
l!l&&r!l&&!r
0120
$r->{"exit_code"} == 0 and $r->{"err_msg"} eq "" and $r->{"stdout"} eq "" and $r->{"stderr"} eq ""
!ll&&!rl&&r
1101
$r->{"exit_code"} == 0 and $r->{"err_msg"} eq "" and $r->{"stdout"} eq ""
!ll&&!rl&&r
1101
$r->{"exit_code"} == 0 and $r->{"err_msg"} eq ""
!ll&&!rl&&r
471
MC/DC0%0/5
$err or $r->{"exit_code"} == 0 and $r->{"err_msg"} eq "" and $r->{"stdout"} eq "" and $r->{"stderr"} eq ""
$err $r->{"exit_code"} == 0 $r->{"err_msg"} eq "" $r->{"stdout"} eq "" $r->{"stderr"} eq ""
Truth table16%1/6
A or B and C and D and E
A= $errB= $r->{"exit_code"} == 0C= $r->{"err_msg"} eq ""D= $r->{"stdout"} eq ""E= $r->{"stderr"} eq ""
ABCDEresult
1XXXX1
011111
00XXX0
010XX0
0110X0
011100
248 1 ▶ $err //= 'No output';
Condition50%1/2
$err //= "No output"
l!l
10
MC/DC0%0/1
$err //= "No output"
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
249 1 my $prog = $_[0][0];
250 1 throw "Failed to run $prog - Make sure you have the YubiKey Personalization Tool (CLI) package installed.\n",
251 error => $err;
252 }
253 11 return $r;
254 }
255  
256 sub _yk_errno {
257 4 ▶ local $_ = shift or return 0;
Subroutine _yk_errno: called CC=16 SCAR=35.3
Branch: 1 branch
BranchTrueFalseTotal
local $_ = shift() or return 0 0 4 4
258 4 ▶ return YK_EUSBERR if $_ =~ YK_EUSBERR;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /USB error/ 1 3 4
259 3 ▶ return YK_EWRONGSIZ if $_ =~ YK_EWRONGSIZ;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /wrong size/ 0 3 3
260 3 ▶ return YK_EWRITEERR if $_ =~ YK_EWRITEERR;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /write error/ 0 3 3
261 3 ▶ return YK_ETIMEOUT if $_ =~ YK_ETIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /timeout/ 0 3 3
262 3 ▶ return YK_ENOKEY if $_ =~ YK_ENOKEY;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /no yubikey present/ 1 2 3
263 2 ▶ return YK_EFIRMWARE if $_ =~ YK_EFIRMWARE;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /unsupported firmware version/ 0 2 2
264 2 ▶ return YK_ENOMEM if $_ =~ YK_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /out of memory/ 0 2 2
265 2 ▶ return YK_ENOSTATUS if $_ =~ YK_ENOSTATUS;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /no status structure given/ 0 2 2
266 2 ▶ return YK_ENOTYETIMPL if $_ =~ YK_ENOTYETIMPL;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /not yet implemented/ 1 1 2
267 1 ▶ return YK_ECHECKSUM if $_ =~ YK_ECHECKSUM;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /checksum mismatch/ 0 1 1
268 1 ▶ return YK_EWOULDBLOCK if $_ =~ YK_EWOULDBLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /operation would block/ 1 0 1
269 0 ▶ return YK_EINVALIDCMD if $_ =~ YK_EINVALIDCMD;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /invalid command for operation/ 0 0 0
270 0 ▶ return YK_EMORETHANONE if $_ =~ YK_EMORETHANONE;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /expected only one YubiKey but serveral present/ 0 0 0
271 0 ▶ return YK_ENODATA if $_ =~ YK_ENODATA;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /no data returned from device/ 0 0 0
272 0 return -1;
273 }
274  
275 2 my %PIDS;
276 2 for my $pid (
277 YUBIKEY_PID, NEO_OTP_PID, NEO_OTP_CCID_PID, NEO_CCID_PID, NEO_U2F_PID, NEO_OTP_U2F_PID, NEO_U2F_CCID_PID,
278 NEO_OTP_U2F_CCID_PID, YK4_OTP_PID, YK4_U2F_PID, YK4_OTP_U2F_PID, YK4_CCID_PID, YK4_OTP_CCID_PID,
279 YK4_U2F_CCID_PID, YK4_OTP_U2F_CCID_PID, PLUS_U2F_OTP_PID, ONLYKEY_PID,
280 ) {
281 34 $PIDS{$pid} = $PIDS{0+$pid} = $pid;
282 }
283 2 ▶sub _product_name { $PIDS{$_[1]} // 'Unknown' }
Subroutine _product_name: called CC=2 SCAR=7.6
Condition50%1/2
$PIDS{$_[1]} // "Unknown"
l!l
20
MC/DC0%0/1
$PIDS{$_[1]} // "Unknown"
$PIDS{$_[1]}
Truth table50%1/2
A
A= $PIDS{$_[1]}
Aresult
11
00
284  
285 2 1;
286  
287 __END__
« KDBX/Dumper.pm ↑ Summary KDBX/Key/ChallengeResponse.pm »