blib/lib/Validate/Yubikey.pm

filter: Statementstmt 40.0% 36 / 90 Branchbran 23.3% 7 / 30 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 66.6% 6 / 9 Podpod 40.0% 2 / 5 totaltotal 36.2% 54 / 149 CC 22 SCAR 37.1
CC
22
Cov
36%
validate
86.6
yubicrc
20.0
new
12.9
CRAP 41.0
↑ Summary Build »
1 package Validate::Yubikey;
2  
3 1 our $VERSION = '0.01';
4  
5 =head1 NAME
6  
7 Validate::Yubikey - Validate Yubikey OTPs
8  
9 =head1 SYNOPSIS
10  
11 use Validate::Yubikey;
12  
13 sub validate_callback {
14 my $public_id = shift;
15  
16 return {
17 iid => $iid,
18 key => $key,
19 count => $count,
20 use => $use,
21 lastuse => $lastuse,
22 lastts => $lastts,
23 };
24 }
25  
26 sub update_callback {
27 my ($public_id, $data) = @_;
28 }
29  
30 sub log_message {
31 print shift, "\n";
32 }
33  
34 my $yubi = Validate::Yubikey->new(
35 callback => \&validate_callback,
36 update_callback => \&update_callback,
37 log_callback => \&log_message,
38 );
39  
40 my $otp_valid = $yubi->validate("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
41  
42 =head1 DESCRIPTION
43  
44 The Yubikey is a hardware OTP token produced by Yubico (L<http://www.yubico.com>).
45  
46 This module provides validation of Yubikey OTPs. It relies on you to specify
47 callback functions that handle retrieving token information from somewhere and
48 updating the persistent information associated with each token.
49  
50 =cut
51  
52 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use Crypt::ECB;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use Crypt::Rijndael;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 sub hex2modhex {
57 0 ▶ my $s = shift;
Subroutine hex2modhex: not called CC=1 SCAR=6.9
Pod undocumented
58 0 $s =~ tr/0123456789abcdef/cbdefghijklnrtuv/;
59 0 return $s;
60 }
61  
62 sub modhex2hex {
63 1 ▶ my $s = shift;
Subroutine modhex2hex: called CC=1 SCAR=0.0
Pod undocumented
64 1 $s =~ tr/cbdefghijklnrtuv/0123456789abcdef/;
65 1 return $s;
66 }
67  
68 sub yubicrc {
69 0 ▶ my $data = shift;
Subroutine yubicrc: not called CC=4 SCAR=30.0
Pod undocumented
70 0 my $crc = 0xffff;
71  
72 0 foreach my $h (unpack('H2' x 16, $data)) {
73 0 my $d = hex($h);
74 0 $crc = $crc ^ ($d & 0xff);
75 0 for (1..8) {
76 0 my $n = $crc & 1;
77 0 $crc = $crc >> 1;
78 0 ▶ if ($n != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n != 0) 0 0 0
79 0 $crc = $crc ^ 0x8408;
80 }
81 }
82 }
83
84 0 return $crc;
85 }
86  
87 =head1 METHODS
88  
89 =head2 new
90  
91 Create a new Validate::Yubikey instance.
92  
93 =over 4
94  
95 =item callback
96  
97 Required.
98  
99 =item update_callback
100  
101 Required.
102  
103 =item log_callback
104  
105 Optional.
106  
107 =back
108  
109 =cut
110  
111 sub new {
112 1 ▶ my ($class, %data) = @_;
Subroutine new: called CC=8 SCAR=25.6
Pod documented
113 1 my $self = {};
114 1 bless $self, $class;
115  
116 1 ▶ if (exists $data{callback} && ref($data{callback}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data{'callback'} and ref $data{'callback'} eq 'CODE') { } 1 0 1
Condition33%1/3
exists $data{"callback"} and ref $data{"callback"} eq "CODE"
!ll&&!rl&&r
001
MC/DC0%0/2
exists $data{"callback"} and ref $data{"callback"} eq "CODE"
exists $data{"callback"} ref $data{"callback"} eq "CODE"
Truth table33%1/3
A and B
A= exists $data{"callback"}B= ref $data{"callback"} eq "CODE"
ABresult
0X0
100
111
117 1 $self->{callback} = $data{callback};
118 } else {
119 0 croak __PACKAGE__, '->new called without callback';
120 }
121  
122 1 ▶ if (exists $data{update_callback} && ref($data{update_callback}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data{'update_callback'} and ref $data{'update_callback'} eq 'CODE') { } 1 0 1
Condition33%1/3
exists $data{"update_callback"} and ref $data{"update_callback"} eq "CODE"
!ll&&!rl&&r
001
MC/DC0%0/2
exists $data{"update_callback"} and ref $data{"update_callback"} eq "CODE"
exists $data{"update_callback"} ref $data{"update_callback"} eq "CODE"
Truth table33%1/3
A and B
A= exists $data{"update_callback"}B= ref $data{"update_callback"} eq "CODE"
ABresult
0X0
100
111
123 1 $self->{update_callback} = $data{update_callback};
124 } else {
125 0 croak __PACKAGE__, '->new called without update_callback';
126 }
127  
128 1 ▶ if (exists $data{log_callback} && ref($data{log_callback}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data{'log_callback'} and ref $data{'log_callback'} eq 'CODE') { } 1 0 1
Condition33%1/3
exists $data{"log_callback"} and ref $data{"log_callback"} eq "CODE"
!ll&&!rl&&r
001
MC/DC0%0/2
exists $data{"log_callback"} and ref $data{"log_callback"} eq "CODE"
exists $data{"log_callback"} ref $data{"log_callback"} eq "CODE"
Truth table33%1/3
A and B
A= exists $data{"log_callback"}B= ref $data{"log_callback"} eq "CODE"
ABresult
0X0
100
111
129 1 $self->{log_callback} = $data{log_callback};
130 } else {
131 0 ▶ $self->{log_callback} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 }
133  
134 1 ▶ if ($data{max_age}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{'max_age'}) { } 0 1 1
135 0 $self->{max_age} = $data{max_age};
136 } else {
137 1 $self->{max_age} = 60;
138 }
139  
140 1 return $self;
141 }
142  
143 =head2 validate
144  
145 =over 4
146  
147 =item Arguments: $otp, @callback_args
148  
149 =item Return Value: $success
150  
151 =back
152  
153 Validate an OTP.
154  
155 =cut
156  
157 sub validate {
158 1 ▶ my ($self, $otp, @cbargs) = @_;
Subroutine validate: called CC=12 SCAR=44.6
Pod documented
159  
160 1 ▶ if ($otp =~ /^([cbdefghijklnrtuv]{0,16})([cbdefghijklnrtuv]{32})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($otp =~ /^([cbdefghijklnrtuv]{0,16})([cbdefghijklnrtuv]{32})$/) { } 1 0 1
161 1 my ($public_id, $cipher) = ($1, $2);
162  
163 1 my $token = $self->{callback}->($public_id, @cbargs);
164  
165 1 ▶ if (!$token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token) 0 1 1
166 0 $self->{log_callback}->(sprintf('callback returned no token for pid %s', $public_id));
167 0 return 0;
168 }
169  
170 1 foreach my $k (qw/key count iid lastuse lastts use/) {
171 6 ▶ if (!defined($token->{$k})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $token->{$k}) 0 6 6
172 0 carp "callback didn't return $k";
173 0 return 0;
174 }
175 }
176  
177 1 $cipher = &modhex2hex($cipher);
178  
179 1 my $crypt = Crypt::ECB->new;
180 1 $crypt->start('decrypt');
181 0 $crypt->cipher('Rijndael');
182 0 $crypt->key(pack('H*', $token->{key}));
183 0 my $plaintext = $crypt->decrypt(pack('H*', $cipher));
184 0 my $plainhex = unpack('H*', $plaintext);
185  
186 0 ▶ if (substr($plainhex, 0, length($token->{iid})) eq $token->{iid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($plainhex, 0, length $token->{'iid'}) eq $token->{'iid'}) { } 0 0 0
187 0 my $crc = &yubicrc($plaintext);
188  
189 0 ▶ if ($crc == 0xf0b8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crc == 61624) { } 0 0 0
190 0 my $count = hex(substr($plainhex, 14, 2).substr($plainhex, 12, 2));
191 0 my $use = hex(substr($plainhex, 22, 2));
192 0 my $low = substr($plainhex, 18, 2).substr($plainhex, 16, 2);
193 0 my $high = substr($plainhex, 20, 2);
194 0 my $ts = ((hex($high) << 16) + hex($low)) / 8; # XXX magic
195  
196 0 my $tinfo = sprintf('iid=%s, count=%d, use=%d, ts=%d', $token->{iid}, $count, $use, $ts);
197 0 my $tsnow = $token->{lastts} + (time() - $token->{lastuse});
198 0 my $tsage = $tsnow - $ts;
199 0 $self->{update_callback}->($public_id, { lastuse => time(), lastts => $ts });
200  
201 0 ▶ if ($count < $token->{count}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($count < $token->{'count'}) { } 0 0 0
elsif ($count == $token->{'count'}) { } 0 0 0
elsif ($count > $token->{'count'}) { } 0 0 0
202 0 $self->{log_callback}->(sprintf('token %s failed: duplicate otp, count (%s)', $public_id, $tinfo));
203 } elsif ($count == $token->{count}) {
204 0 ▶ if ($use <= $token->{use}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($use <= $token->{'use'}) { } 0 0 0
elsif ($tsage > $self->{'max_age'}) { } 0 0 0
205 0 $self->{log_callback}->(sprintf('token %s failed: duplicate otp in same session (%s)', $public_id, $tinfo));
206 } elsif ($tsage > $self->{max_age}) {
207 0 $self->{log_callback}->(sprintf('token %s failed: expired otp is %d seconds old (%s)', $public_id, $tsage, $tinfo));
208 } else {
209 0 $self->{log_callback}->(sprintf('token %s ok, same session (%s)', $public_id, $tinfo));
210 0 $self->{update_callback}->($public_id, { count => $count, use => $use });
211 0 return 1;
212 }
213 } elsif ($count > $token->{count}) {
214 0 $self->{log_callback}->(sprintf('token %s ok (%s)', $public_id, $tinfo));
215 0 $self->{update_callback}->($public_id, { count => $count, use => $use });
216 0 return 1;
217 } else {
218 0 $self->{log_callback}->(sprintf('something bad with token %s (%s)', $public_id, $tinfo));
219 }
220 } else {
221 0 $self->{log_callback}->(sprintf('token %s failed: corrupt otp (crc)', $public_id));
222 }
223 } else {
224 0 $self->{log_callback}->(sprintf('token %s failed: corrupt otp (internal id)', $public_id));
225 }
226 } else {
227 0 $self->{log_callback}->(sprintf('token %s failed: invalid otp', $public_id));
228 }
229  
230 0 return 0;
231 }
232  
233 =head1 CALLBACKS
234  
235 =head2 callback
236  
237 =over 4
238  
239 =item Receives: $public_id, @callback_args
240  
241 =item Returns: \%token_data
242  
243 =back
244  
245 Called during validation when information about the token is required.
246 Receives the public ID of the Yubikey. It's expected that your subroutine
247 returns a hash reference containing the following keys:
248  
249 =over 4
250  
251 =item iid - Internal ID
252  
253 =item key - Secret key
254  
255 =back
256  
257 Plus the four values stored by the L<update_callback>.
258  
259 =head2 update_callback
260  
261 =over 4
262  
263 =item Receives: $public_id, \%token_data, @callback_args
264  
265 =item Returns: nothing
266  
267 =back
268  
269 Called to update the persistent storage of token parameters that enable replay
270 protection. C<%token_data> will contain one or more of the following keys,
271 which should be associated with the supplied C<$public_id>:
272  
273 =over 4
274  
275 =item count
276  
277 =item use
278  
279 =item lastuse
280  
281 =item lastts
282  
283 =back
284  
285 These should all be integers.
286  
287 =head2 log_callback
288  
289 =over 4
290  
291 =item Receives: $log_message
292  
293 =item Returns: nothing
294  
295 =back
296  
297 Called with messages produced during validation. If not supplied to L<new>,
298 logging will disabled.
299  
300 =head1 EXAMPLE
301  
302 Here's a simple program that uses L<DBIx::Class> to store token information.
303  
304 package YKKSM::DB::Token;
305 use base qw/DBIx::Class/;
306
307 __PACKAGE__->load_components(qw/PK::Auto Core/);
308 __PACKAGE__->table('token');
309 __PACKAGE__->add_columns(qw/uid pid iid key count use lastuse lastts/);
310 __PACKAGE__->set_primary_key('uid');
311
312 package YKKSM::DB;
313 use base qw/DBIx::Class::Schema/;
314
315 __PACKAGE__->load_classes(qw/Token/);
316
317 package YKTest;
318 use Validate::Yubikey;
319
320 my $schema = YKKSM::DB->connect("dbi:SQLite:dbname=yktest.db");
321
322 my $yk = Validate::Yubikey->new(
323 callback => sub {
324 my $pid = shift;
325 my $token = $schema->resultset('Token')->find({ pid => $pid });
326
327 if ($token) {
328 return {
329 iid => $token->iid,
330 key => $token->key,
331 count => $token->count,
332 use => $token->use,
333 lastuse => $token->lastuse,
334 lastts => $token->lastts,
335 };
336 } else {
337 return undef;
338 }
339 },
340 update_callback => sub {
341 my ($pid, $data) = @_;
342 my $token = $schema->resultset('Token')->find({ pid => $pid });
343 if ($token) {
344 $token->update($data);
345 } else {
346 die "asked to update nonexistent token $pid";
347 }
348 },
349 log_callback => sub {
350 print shift, "\n";
351 },
352 );
353
354 if ($yk->validate($ARGV[0])) {
355 print "success!\n";
356 } else {
357 print "failure 8(\n";
358 }
359  
360 =head1 AUTHOR
361  
362 Ben Wilber <ben@desync.com>
363  
364 But most of this module was derived from Yubico's PHP stuff.
365  
366 =head1 LICENSE
367  
368 This library is free software and may be distributed under the same terms
369 as perl itself.
370  
371 =cut
372  
373 1 1;
↑ Summary Build »