Keyring.pm

filter: Statementstmt 82.5% 498 / 603 Branchbran 62.0% 155 / 250 Conditioncond 50.3% 78 / 155 MC/DCmcdc 19.3% 18 / 93 Subroutinesub 92.6% 38 / 41 Podpod 100% 13 / 13 totaltotal 69.2% 800 / 1155 CC 193 SCAR 27.4
CC
193
Cov
73%
_encrypt_v5
33.7
_encrypt_v4
29.8
new
24.4
CRAP 15.5
« Keyring/ConfigData.pm ↑ Summary
1 package Palm::Keyring;
2 # $RedRiver: Keyring.pm,v 1.54 2007/12/05 05:42:29 andrew Exp $
3 ########################################################################
4 # Keyring.pm *** Perl class for Keyring for Palm OS databases.
5 #
6 # This started as Memo.pm, I just made it work for Keyring.
7 #
8 # 2006.01.26 #*#*# andrew fresh <andrew@cpan.org>
9 ########################################################################
10 # Copyright (C) 2006, 2007 by Andrew Fresh
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the same terms as Perl itself.
14 ########################################################################
15 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 require 5.006_001;
19  
20 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 ▶use base qw/ Palm::StdAppInfo /;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 4 my $ENCRYPT = 1;
25 4 my $DECRYPT = 0;
26 4 my $MD5_CBLOCK = 64;
27 4 my $kSalt_Size = 4;
28 4 my $EMPTY = q{};
29 4 my $SPACE = q{ };
30 4 my $NULL = chr 0;
31  
32 4 my @CRYPTS = (
33 {
34 alias => 'None',
35 name => 'None',
36 keylen => 8,
37 blocksize => 1,
38 default_iter => 500,
39 },
40 {
41 alias => 'DES-EDE3',
42 name => 'DES_EDE3',
43 keylen => 24,
44 blocksize => 8,
45 DES_odd_parity => 1,
46 default_iter => 1000,
47 },
48 {
49 alias => 'AES128',
50 name => 'Rijndael',
51 keylen => 16,
52 blocksize => 16,
53 default_iter => 100,
54 },
55 {
56 alias => 'AES256',
57 name => 'Rijndael',
58 keylen => 32,
59 blocksize => 16,
60 default_iter => 250,
61 },
62 );
63  
64 4 my %LABELS = (
65 0 => {
66 id => 0,
67 name => 'name',
68 },
69 1 => {
70 id => 1,
71 name => 'account',
72 },
73 2 => {
74 id => 2,
75 name => 'password',
76 },
77 3 => {
78 id => 3,
79 name => 'lastchange',
80 },
81 255 => {
82 id => 255,
83 name => 'notes',
84 },
85 );
86  
87  
88 4 our $VERSION = '0.96_07';
89  
90 sub new
91 2 ▶{
Subroutine new: called CC=13 SCAR=32.0
Pod documented
92 2 my $classname = shift;
93 2 my $options = {};
94  
95 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
96 # hashref arguments
97 2 ▶ if (ref $_[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 2 0 2
elsif ($_[0] =~ /^-[a-zA-Z0-9_]{1,20}$/) { } 0 0 0
98 2 $options = shift;
99 }
100  
101 # CGI style arguments
102 elsif ($_[0] =~ /^-[a-zA-Z0-9_]{1,20}$/) {
103 0 my %tmp = @_;
104 0 ▶ while ( my($key,$value) = each %tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %tmp) 0 0 0
105 0 $key =~ s/^-//;
106 0 $options->{lc $key} = $value;
107 }
108 }
109  
110 else {
111 0 $options->{password} = shift;
112 0 $options->{version} = shift;
113 }
114 }
115  
116 # Create a generic PDB. No need to rebless it, though.
117 2 my $self = $classname->SUPER::new();
118  
119 2 $self->{name} = 'Keys-Gtkr'; # Default
120 2 $self->{creator} = 'Gtkr';
121 2 $self->{type} = 'Gkyr';
122  
123 # The PDB is not a resource database by
124 # default, but it's worth emphasizing,
125 # since MemoDB is explicitly not a PRC.
126 2 $self->{attributes}{resource} = 0;
127  
128 # Set the version
129 2 ▶ $self->{version} = $options->{version} || 4;
Condition50%1/2
$options->{'version'} || 4
l!l
20
MC/DC0%0/1
$options->{'version'} || 4
$options->{'version'}
Truth table50%1/2
A
A= $options->{'version'}
Aresult
11
00
130  
131 # Set options
132 2 $self->{options} = $options;
133  
134 # Set defaults
135 2 ▶ if ($self->{version} == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"version"} == 5) 1 1 2
136 1 ▶ $self->{options}->{cipher} ||= 0; # 'None'
Condition50%1/2
$self->{"options"}{"cipher"} ||= 0
l!l
01
MC/DC0%0/1
$self->{"options"}{"cipher"} ||= 0
$self->{"options"}{"cipher"}
Truth table50%1/2
A
A= $self->{"options"}{"cipher"}
Aresult
11
00
137 my $c = crypts($self->{options}->{cipher})
138 1 ▶ or croak('Unknown cipher ' . $self->{options}->{cipher});
Branch: 1 branch
BranchTrueFalseTotal
my $c = crypts($self->{"options"}{"cipher"}) or croak('Unknown cipher ' . $self->{"options"}{"cipher"}) 1 0 1
139 1 ▶ $self->{options}->{iterations} ||= $c->{default_iter};
Condition33%1/3
$self->{"options"}{"iterations"} ||= $c->{"default_iter"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"options"}{"iterations"} ||= $c->{"default_iter"}
$self->{"options"}{"iterations"} $c->{"default_iter"}
Truth table33%1/3
A ||= B
A= $self->{"options"}{"iterations"}B= $c->{"default_iter"}
ABresult
1X1
011
000
140 1 ▶ $self->{appinfo}->{cipher} ||= $self->{options}->{cipher};
Condition33%1/3
$self->{"appinfo"}{"cipher"} ||= $self->{"options"}{"cipher"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"appinfo"}{"cipher"} ||= $self->{"options"}{"cipher"}
$self->{"appinfo"}{"cipher"} $self->{"options"}{"cipher"}
Truth table33%1/3
A ||= B
A= $self->{"appinfo"}{"cipher"}B= $self->{"options"}{"cipher"}
ABresult
1X1
011
000
141 1 ▶ $self->{appinfo}->{iter} ||= $self->{options}->{iterations};
Condition33%1/3
$self->{"appinfo"}{"iter"} ||= $self->{"options"}{"iterations"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"appinfo"}{"iter"} ||= $self->{"options"}{"iterations"}
$self->{"appinfo"}{"iter"} $self->{"options"}{"iterations"}
Truth table33%1/3
A ||= B
A= $self->{"appinfo"}{"iter"}B= $self->{"options"}{"iterations"}
ABresult
1X1
011
000
142 };
143  
144 2 ▶ if ( defined $options->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"password"}) 2 0 2
145 2 $self->Password($options->{password});
146 }
147  
148 2 return $self;
149 }
150  
151 sub import
152 3 ▶{
Subroutine import: called CC=1 SCAR=0.0
153 3 Palm::PDB::RegisterPDBHandlers( __PACKAGE__, [ 'Gtkr', 'Gkyr' ], );
154 3 return 1;
155 }
156  
157 # Accessors
158  
159 sub crypts
160 64 ▶{
Subroutine crypts: called CC=6 SCAR=27.6
Pod documented
161 64 my $crypt = shift;
162 64 ▶ if ((! defined $crypt) || (! length $crypt)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $crypt && length $crypt) { } 0 64 64
elsif ($crypt =~ /\D/) { } 0 64 64
Condition33%1/3
defined $crypt && length $crypt
!ll&&!rl&&r
0064
MC/DC0%0/2
defined $crypt && length $crypt
defined $crypt length $crypt
Truth table33%1/3
A && B
A= defined $cryptB= length $crypt
ABresult
0X0
100
111
163 0 return;
164 } elsif ($crypt =~ /\D/) {
165 0 foreach my $c (@CRYPTS) {
166 0 ▶ if ($c->{alias} eq $crypt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"alias"} eq $crypt) 0 0 0
167 0 return $c;
168 }
169 }
170 # didn't find it.
171 0 return;
172 } else {
173 64 return $CRYPTS[$crypt];
174 }
175 }
176  
177 sub labels
178 80 ▶{
Subroutine labels: called CC=7 SCAR=23.8
Pod documented
179 80 my $label = shift;
180  
181 80 ▶ if ((! defined $label) || (! length $label)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $label && length $label) { } 0 80 80
elsif (exists $LABELS{$label}) { } 60 20 80
Condition33%1/3
defined $label && length $label
!ll&&!rl&&r
0080
MC/DC0%0/2
defined $label && length $label
defined $label length $label
Truth table33%1/3
A && B
A= defined $labelB= length $label
ABresult
0X0
100
111
182 0 return;
183 } elsif (exists $LABELS{$label}) {
184 60 return $LABELS{$label};
185 } else {
186 20 foreach my $l (keys %LABELS) {
187 60 ▶ if ($LABELS{$l}{name} eq $label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LABELS{$l}{"name"} eq $label) 20 40 60
188 20 return $LABELS{$l};
189 }
190 }
191  
192 # didn't find it, make one.
193 0 ▶ if ($label =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label =~ /^\d+$/) { } 0 0 0
194 return {
195 0 id => $label,
196 name => undef,
197 };
198 } else {
199 0 return;
200 }
201 }
202 }
203  
204 # Write
205  
206 sub Write
207 2 ▶{
Subroutine Write: called CC=4 SCAR=13.9
Pod documented
208 2 my $self = shift;
209  
210 2 ▶ if ($self->{version} == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"version"} == 4) 1 1 2
211 # Give the PDB the first record that will hold the encrypted password
212 1 my $rec = $self->new_Record;
213 1 $rec->{data} = $self->{encpassword};
214  
215 1 ▶ if (ref $self->{records} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'records'} eq 'ARRAY') { } 1 0 1
216 1 unshift @{ $self->{records} }, $rec;
217 } else {
218 0 $self->{records} = [ $rec ];
219 }
220 }
221  
222 2 my $rc = $self->SUPER::Write(@_);
223  
224 2 ▶ if ($self->{version} == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"version"} == 4) 1 1 2
225 1 shift @{ $self->{records} };
226 }
227  
228 2 return $rc;
229 }
230  
231 # ParseRecord
232  
233 sub ParseRecord
234 17 ▶{
Subroutine ParseRecord: called CC=8 SCAR=21.2
Pod documented
235 17 my $self = shift;
236  
237 17 my $rec = $self->SUPER::ParseRecord(@_);
238 17 ▶ return $rec if ! exists $rec->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rec->{"data"} 0 17 17
239  
240 17 ▶ if ($self->{version} == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'version'} == 4) { } 2 15 17
elsif ($self->{'version'} == 5) { } 15 0 15
241 # skip the first record because it contains the password.
242 2 ▶ if (! exists $self->{records}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"records"}) 1 1 2
243 1 $self->{encpassword} = $rec->{data};
244 1 return '__DELETE_ME__';
245 }
246  
247 1 ▶ if ($self->{records}->[0] eq '__DELETE_ME__') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"records"}[0] eq "__DELETE_ME__") 1 0 1
248 1 shift @{ $self->{records} };
249 }
250  
251 1 my ( $name, $encrypted ) = split /$NULL/xm, $rec->{data}, 2;
252  
253 1 ▶ return $rec if ! $encrypted;
Branch: 1 branch
BranchTrueFalseTotal
unless $encrypted 0 1 1
254 1 $rec->{plaintext}->{0} = {
255 label => 'name',
256 label_id => 0,
257 data => $name,
258 font => 0,
259 };
260 1 $rec->{encrypted} = $encrypted;
261 1 delete $rec->{data};
262  
263 } elsif ($self->{version} == 5) {
264 my $c = crypts( $self->{appinfo}->{cipher} )
265 15 ▶ or croak('Unknown cipher ' . $self->{appinfo}->{cipher});
Branch: 1 branch
BranchTrueFalseTotal
my $c = crypts($self->{"appinfo"}{"cipher"}) or croak('Unknown cipher ' . $self->{"appinfo"}{"cipher"}) 15 0 15
266 15 my $blocksize = $c->{blocksize};
267 15 my ($field, $extra) = _parse_field($rec->{data});
268 15 delete $rec->{data};
269  
270 15 $rec->{plaintext}->{0} = $field;
271 15 $rec->{ivec} = substr $extra, 0, $blocksize;
272 15 $rec->{encrypted} = substr $extra, $blocksize;
273  
274 } else {
275 0 croak "Unsupported Version $self->{version}";
276 0 return;
277 }
278  
279 16 return $rec;
280 }
281  
282 # PackRecord
283  
284 sub PackRecord
285 5 ▶{
Subroutine PackRecord: called CC=6 SCAR=18.6
Pod documented
286 5 my $self = shift;
287 5 my $rec = shift;
288  
289 5 ▶ if ($self->{version} == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'version'} == 4) { } 2 3 5
elsif ($self->{'version'} == 5) { } 3 0 3
290 2 ▶ if ($rec->{encrypted}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{"encrypted"}) 1 1 2
291 1 ▶ my $name = $rec->{plaintext}->{0}->{data} || $EMPTY;
Condition33%1/3
$rec->{'plaintext'}{'0'}{'data'} || $EMPTY
l!l&&r!l&&!r
100
MC/DC0%0/2
$rec->{'plaintext'}{'0'}{'data'} || $EMPTY
$rec->{'plaintext'}{'0'}{'data'} $EMPTY
Truth table33%1/3
A || B
A= $rec->{'plaintext'}{'0'}{'data'}B= $EMPTY
ABresult
1X1
011
000
292 1 $rec->{data} = join $NULL, $name, $rec->{encrypted};
293 1 delete $rec->{plaintext};
294 1 delete $rec->{encrypted};
295 }
296  
297 } elsif ($self->{version} == 5) {
298 3 my $field;
299 3 ▶ if ($rec->{plaintext}->{0}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{'plaintext'}{'0'}) { } 3 0 3
300 3 $field = $rec->{plaintext}->{0};
301 } else {
302 0 $field = {
303 'label' => 'name',
304 'label_id' => 0,
305 'data' => $EMPTY,
306 'font' => 0,
307 };
308 }
309 3 my $packed = _pack_field($field);
310  
311 3 $rec->{data} = join $EMPTY, $packed, $rec->{ivec}, $rec->{encrypted};
312  
313 } else {
314 0 croak "Unsupported Version $self->{version}";
315 }
316  
317 5 return $self->SUPER::PackRecord($rec, @_);
318 }
319  
320 # ParseAppInfoBlock
321  
322 sub ParseAppInfoBlock
323 6 ▶{
Subroutine ParseAppInfoBlock: called CC=4 SCAR=14.2
Pod documented
324 6 my $self = shift;
325 6 my $data = shift;
326 6 my $appinfo = {};
327  
328 6 &Palm::StdAppInfo::parse_StdAppInfo($appinfo, $data);
329  
330 # int8/uint8
331 # - Signed or Unsigned Byte (8 bits). C types: char, unsigned char
332 # int16/uint16
333 # - Signed or Unsigned Word (16 bits). C types: short, unsigned short
334 # int32/uint32
335 # - Signed or Unsigned Doubleword (32 bits). C types: int, unsigned int
336 # sz
337 # - Zero-terminated C-style string
338  
339 6 ▶ if ($self->{version} == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'version'} == 4) { } 1 5 6
elsif ($self->{'version'} == 5) { } 5 0 5
340 # Nothing extra for version 4
341  
342 } elsif ($self->{version} == 5) {
343 5 ▶ _parse_appinfo_v5($appinfo) || return;
Branch: 1 branch
BranchTrueFalseTotal
_parse_appinfo_v5($appinfo) or return 5 0 5
344  
345 } else {
346 0 croak "Unsupported Version $self->{version}";
347 }
348  
349 6 return $appinfo;
350 }
351  
352 sub _parse_appinfo_v5
353 5 ▶{
Subroutine _parse_appinfo_v5: called CC=2 SCAR=7.0
354 5 my $appinfo = shift;
355  
356 5 ▶ if (! exists $appinfo->{other}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $appinfo->{"other"}) 0 5 5
357 # XXX Corrupt appinfo?
358 0 return;
359 }
360  
361 5 my $unpackstr
362 = ("C1" x 8) # 8 uint8s in an array for the salt
363 . ("n1" x 2) # the iter (uint16) and the cipher (uint16)
364 . ("C1" x 8); # and finally 8 more uint8s for the hash
365  
366 5 my (@salt, $iter, $cipher, @hash);
367 (@salt[0..7], $iter, $cipher, @hash[0..7])
368 5 = unpack $unpackstr, $appinfo->{other};
369  
370 5 $appinfo->{salt} = sprintf "%02x" x 8, @salt;
371 5 $appinfo->{iter} = $iter;
372 5 $appinfo->{cipher} = $cipher;
373 5 $appinfo->{masterhash} = sprintf "%02x" x 8, @hash;
374 5 delete $appinfo->{other};
375  
376 5 return $appinfo
377 }
378  
379 # PackAppInfoBlock
380  
381 sub PackAppInfoBlock
382 2 ▶{
Subroutine PackAppInfoBlock: called CC=3 SCAR=11.2
Pod documented
383 2 my $self = shift;
384 2 my $retval;
385  
386 2 ▶ if ($self->{version} == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'version'} == 4) { } 1 1 2
elsif ($self->{'version'} == 5) { } 1 0 1
387 # Nothing to do for v4
388  
389 } elsif ($self->{version} == 5) {
390 1 _pack_appinfo_v5($self->{appinfo});
391 } else {
392 0 croak "Unsupported Version $self->{version}";
393 }
394 2 return &Palm::StdAppInfo::pack_StdAppInfo($self->{appinfo});
395 }
396  
397 sub _pack_appinfo_v5
398 1 ▶{
Subroutine _pack_appinfo_v5: called CC=1 SCAR=0.0
399 1 my $appinfo = shift;
400  
401 1 my $packstr
402 = ("C1" x 8) # 8 uint8s in an array for the salt
403 . ("n1" x 2) # the iter (uint16) and the cipher (uint16)
404 . ("C1" x 8); # and finally 8 more uint8s for the hash
405  
406 1 my @salt = map { hex $_ } $appinfo->{salt} =~ /../gxm;
407 1 my @hash = map { hex $_ } $appinfo->{masterhash} =~ /../gxm;
408  
409 my $packed = pack($packstr,
410 @salt,
411 $appinfo->{iter},
412 $appinfo->{cipher},
413 @hash
414 1 );
415  
416 1 $appinfo->{other} = $packed;
417  
418 1 return $appinfo
419 }
420  
421 # Encrypt
422  
423 sub Encrypt
424 10 ▶{
Subroutine Encrypt: called CC=16 SCAR=30.9
Pod documented
425 10 my $self = shift;
426 10 my $rec = shift;
427 10 ▶ my $pass = shift || $self->{password};
Condition100%3/3
shift() || $self->{'password'}
l!l&&r!l&&!r
811
MC/DC100%2/2
shift() || $self->{'password'}
shift() $self->{'password'}
Truth table100%3/3
A || B
A= shift()B= $self->{'password'}
ABresult
1X1
011
000
428 10 ▶ my $data = shift || $rec->{plaintext};
Condition66%2/3
shift() || $rec->{'plaintext'}
l!l&&r!l&&!r
460
MC/DC0%0/2
shift() || $rec->{'plaintext'}
shift() $rec->{'plaintext'}
Truth table66%2/3
A || B
A= shift()B= $rec->{'plaintext'}
ABresult
1X1
011
000
429 10 my $ivec = shift;
430  
431 10 ▶ if ( ! $pass && ! $self->{appinfo}->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass or $self->{"appinfo"}{"key"}) 0 10 10
Condition66%2/3
$pass or $self->{"appinfo"}{"key"}
l!l&&r!l&&!r
910
MC/DC0%0/2
$pass or $self->{"appinfo"}{"key"}
$pass $self->{"appinfo"}{"key"}
Truth table66%2/3
A or B
A= $passB= $self->{"appinfo"}{"key"}
ABresult
1X1
011
000
432 0 croak("password not set!\n");
433 }
434  
435 10 ▶ if ( ! $rec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rec) 0 10 10
436 0 croak("Needed parameter 'record' not passed!\n");
437 }
438  
439 10 ▶ if ( ! $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 10 10
440 0 croak("Needed 'plaintext' not passed!\n");
441 }
442  
443 10 ▶ if ( $pass && ! $self->Password($pass)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass and not $self->Password($pass)) 0 10 10
Condition66%2/3
$pass and not $self->Password($pass)
!ll&&!rl&&r
190
MC/DC0%0/2
$pass and not $self->Password($pass)
$pass not $self->Password($pass)
Truth table66%2/3
A and not(B)
A= $passB= not $self->Password($pass)
ABresult
0X0
100
111
444 0 croak("Incorrect Password!\n");
445 }
446  
447 10 my $acct;
448 10 ▶ if ($rec->{encrypted}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rec->{"encrypted"}) 2 8 10
449 2 $acct = $self->Decrypt($rec, $pass);
450 }
451  
452 10 my $encrypted;
453 10 ▶ if ($self->{version} == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'version'} == 4) { } 3 7 10
elsif ($self->{'version'} == 5) { } 7 0 7
454 3 ▶ $self->{digest} ||= _calc_keys( $pass );
Condition33%1/3
$self->{"digest"} ||= _calc_keys($pass)
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{"digest"} ||= _calc_keys($pass)
$self->{"digest"} _calc_keys($pass)
Truth table33%1/3
A ||= B
A= $self->{"digest"}B= _calc_keys($pass)
ABresult
1X1
011
000
455 my $datav4 = {
456 name => $data->{0}->{data},
457 account => $data->{1}->{data},
458 password => $data->{2}->{data},
459 lastchange => $data->{3}->{data},
460 notes => $data->{255}->{data},
461 3 };
462 my $acctv4 = {
463 name => $acct->{0}->{data},
464 account => $acct->{1}->{data},
465 password => $acct->{2}->{data},
466 lastchange => $acct->{3}->{data},
467 notes => $acct->{255}->{data},
468 3 };
469 3 $encrypted = _encrypt_v4($datav4, $acctv4, $self->{digest});
470  
471 } elsif ($self->{version} == 5) {
472 ($encrypted, $ivec) = _encrypt_v5(
473 $data, $acct,
474 $self->{appinfo}->{key},
475 $self->{appinfo}->{cipher},
476 7 $ivec,
477 );
478 7 ▶ if (defined $ivec) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ivec) 7 0 7
479 7 $rec->{ivec} = $ivec;
480 }
481  
482 } else {
483 0 croak "Unsupported Version $self->{version}";
484 }
485  
486 10 $rec->{plaintext}->{0} = $data->{0};
487  
488 10 ▶ if ($encrypted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encrypted) { } 10 0 10
489 10 ▶ if ($encrypted eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($encrypted eq 1) 0 10 10
490 0 return 1;
491 }
492  
493 10 $rec->{attributes}{Dirty} = 1;
494 10 $rec->{attributes}{dirty} = 1;
495 10 $rec->{encrypted} = $encrypted;
496  
497 10 return 1;
498 } else {
499 0 return;
500 }
501 }
502  
503 sub _encrypt_v4
504 3 ▶{
Subroutine _encrypt_v4: called CC=22 SCAR=34.0
505 3 my $new = shift;
506 3 my $old = shift;
507 3 my $digest = shift;
508  
509 3 ▶ $new->{account} ||= $EMPTY;
Condition33%1/3
$new->{"account"} ||= $EMPTY
l!l&&r!l&&!r
300
MC/DC0%0/2
$new->{"account"} ||= $EMPTY
$new->{"account"} $EMPTY
Truth table33%1/3
A ||= B
A= $new->{"account"}B= $EMPTY
ABresult
1X1
011
000
510 3 ▶ $new->{password} ||= $EMPTY;
Condition33%1/3
$new->{"password"} ||= $EMPTY
l!l&&r!l&&!r
300
MC/DC0%0/2
$new->{"password"} ||= $EMPTY
$new->{"password"} $EMPTY
Truth table33%1/3
A ||= B
A= $new->{"password"}B= $EMPTY
ABresult
1X1
011
000
511 3 ▶ $new->{notes} ||= $EMPTY;
Condition33%1/3
$new->{"notes"} ||= $EMPTY
l!l&&r!l&&!r
300
MC/DC0%0/2
$new->{"notes"} ||= $EMPTY
$new->{"notes"} $EMPTY
Truth table33%1/3
A ||= B
A= $new->{"notes"}B= $EMPTY
ABresult
1X1
011
000
512  
513 3 my $changed = 0;
514 3 my $need_newdate = 0;
515 3 ▶ if ($old && %{ $old }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old and %{$old;}) { } 3 0 3
Condition33%1/3
$old and %{$old;}
!ll&&!rl&&r
003
MC/DC0%0/2
$old and %{$old;}
$old %{$old;}
Truth table33%1/3
A and B
A= $oldB= %{$old;}
ABresult
0X0
100
111
516 4 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
517 3 foreach my $key (keys %{ $new }) {
518 6 ▶ next if $key eq 'lastchange';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "lastchange" 1 5 6
519 5 ▶ if ($new->{$key} ne $old->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new->{$key} ne $old->{$key}) 3 2 5
520 3 $changed = 1;
521 3 last;
522 }
523 }
524 3 ▶ if ( exists $new->{lastchange} && exists $old->{lastchange} && (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $new->{'lastchange'} and exists $old->{'lastchange'} and $new->{'lastchange'}{'day'} != $old->{'lastchange'}{'day'} || $new->{'lastchange'}{'month'} != $old->{'lastchange'}{'month'} || $new->{'lastchange'}{'year'} != $old->{'lastchange'}{'year'}) { } 2 1 3
Condition58%7/12
exists $new->{"lastchange"} and exists $old->{"lastchange"} and $new->{"lastchange"}{"day"} != $old->{"lastchange"}{"day"} || $new->{"lastchange"}{"month"} != $old->{"lastchange"}{"month"} || $new->{"lastchange"}{"year"} != $old->{"lastchange"}{"year"}
!ll&&!rl&&r
012
exists $new->{"lastchange"} and exists $old->{"lastchange"}
!ll&&!rl&&r
003
$new->{"lastchange"}{"day"} != $old->{"lastchange"}{"day"} or $new->{"lastchange"}{"month"} != $old->{"lastchange"}{"month"} or $new->{"lastchange"}{"year"} != $old->{"lastchange"}{"year"}
l!l&&r!l&&!r
201
$new->{"lastchange"}{"day"} != $old->{"lastchange"}{"day"} or $new->{"lastchange"}{"month"} != $old->{"lastchange"}{"month"}
l!l&&r!l&&!r
201
MC/DC20%1/5
exists $new->{"lastchange"} and exists $old->{"lastchange"} and $new->{"lastchange"}{"day"} != $old->{"lastchange"}{"day"} || $new->{"lastchange"}{"month"} != $old->{"lastchange"}{"month"} || $new->{"lastchange"}{"year"} != $old->{"lastchange"}{"year"}
exists $new->{"lastchange"} exists $old->{"lastchange"} $new->{"lastchange"}{"day"} != $old->{"lastchange"}{"day"} $new->{"lastchange"}{"month"} != $old->{"lastchange"}{"month"} $new->{"lastchange"}{"year"} != $old->{"lastchange"}{"year"}
Truth table33%2/6
A and B and C or D or E
A= exists $new->{"lastchange"}B= exists $old->{"lastchange"}C= $new->{"lastchange"}{"day"} != $old->{"lastchange"}{"day"}D= $new->{"lastchange"}{"month"} != $old->{"lastchange"}{"month"}E= $new->{"lastchange"}{"year"} != $old->{"lastchange"}{"year"}
ABCDEresult
0XXXX0
10XXX0
110000
111XX1
1101X1
110011
525 $new->{lastchange}->{day} != $old->{lastchange}->{day} ||
526 $new->{lastchange}->{month} != $old->{lastchange}->{month} ||
527 $new->{lastchange}->{year} != $old->{lastchange}->{year}
528 )) {
529 2 $changed = 1;
530 2 $need_newdate = 0;
531 } else {
532 1 $need_newdate = 1;
533 }
534  
535 } else {
536 0 $changed = 1;
537 }
538  
539 # no need to re-encrypt if it has not changed.
540 3 ▶ return 1 if ! $changed;
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 0 3 3
541  
542 3 my ($day, $month, $year);
543  
544 3 ▶ if ($new->{lastchange} && ! $need_newdate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new->{'lastchange'} and not $need_newdate) { } 2 1 3
Condition66%2/3
$new->{"lastchange"} and not $need_newdate
!ll&&!rl&&r
012
MC/DC50%1/2
$new->{"lastchange"} and not $need_newdate
$new->{"lastchange"} not $need_newdate
Truth table66%2/3
A and not(B)
A= $new->{"lastchange"}B= not $need_newdate
ABresult
0X0
100
111
545 2 ▶ $day = $new->{lastchange}->{day} || 1;
Condition50%1/2
$new->{'lastchange'}{'day'} || 1
l!l
20
MC/DC0%0/1
$new->{'lastchange'}{'day'} || 1
$new->{'lastchange'}{'day'}
Truth table50%1/2
A
A= $new->{'lastchange'}{'day'}
Aresult
11
00
546 2 ▶ $month = $new->{lastchange}->{month} || 0;
Condition50%1/2
$new->{'lastchange'}{'month'} || 0
l!l
20
MC/DC0%0/1
$new->{'lastchange'}{'month'} || 0
$new->{'lastchange'}{'month'}
Truth table50%1/2
A
A= $new->{'lastchange'}{'month'}
Aresult
11
00
547 2 ▶ $year = $new->{lastchange}->{year} || 0;
Condition50%1/2
$new->{'lastchange'}{'year'} || 0
l!l
20
MC/DC0%0/1
$new->{'lastchange'}{'year'} || 0
$new->{'lastchange'}{'year'}
Truth table50%1/2
A
A= $new->{'lastchange'}{'year'}
Aresult
11
00
548  
549 # XXX Need to actually validate the above information somehow
550 2 ▶ if ($year >= 1900) {
Branch: 1 branch
BranchTrueFalseTotal
if ($year >= 1900) 0 2 2
551 0 $year -= 1900;
552 }
553 } else {
554 1 $need_newdate = 1;
555 }
556  
557 3 ▶ if ($need_newdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_newdate) 1 2 3
558 1 ($day, $month, $year) = (localtime)[3,4,5];
559 }
560  
561 3 my $packed_date = _pack_keyring_date( {
562 year => $year,
563 month => $month,
564 day => $day,
565 });
566  
567 my $plaintext = join $NULL,
568 3 $new->{account}, $new->{password}, $new->{notes}, $packed_date;
569  
570 3 return _crypt3des( $plaintext, $digest, $ENCRYPT );
571 }
572  
573 sub _encrypt_v5
574 7 ▶{
Subroutine _encrypt_v5: called CC=21 SCAR=35.2
575 7 my $new = shift;
576 7 my $old = shift;
577 7 my $key = shift;
578 7 my $cipher = shift;
579 7 my $ivec = shift;
580 7 ▶ my $c = crypts($cipher) or croak('Unknown cipher ' . $cipher);
Branch: 1 branch
BranchTrueFalseTotal
my $c = crypts($cipher) or croak('Unknown cipher ' . $cipher) 7 0 7
581  
582 7 ▶ if (! defined $ivec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ivec) 7 0 7
583 7 $ivec = pack("C*",map {rand(256)} 1..$c->{blocksize});
584 }
585  
586 7 my $changed = 0;
587 7 my $need_newdate = 1;
588 7 ▶ if ($new->{3}->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new->{3}{"data"}) 7 0 7
589 7 $need_newdate = 0;
590 }
591 7 foreach my $k (keys %{ $new }) {
592 24 ▶ if (! $old) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $old) { } 20 4 24
elsif ($k == 3) { } 1 3 4
593 20 $changed = 1;
594 } elsif ($k == 3) {
595 1 ▶ if ($old && (
Branch: 1 branch
BranchTrueFalseTotal
if ($old and ($new->{$k}{"data"}{"day"} == $old->{$k}{"data"}{"day"} and $new->{$k}{"data"}{"month"} == $old->{$k}{"data"}{"month"} and $new->{$k}{"data"}{"year"} == $old->{$k}{"data"}{"year"})) 1 0 1
Condition33%3/9
$old and ($new->{$k}{"data"}{"day"} == $old->{$k}{"data"}{"day"} and $new->{$k}{"data"}{"month"} == $old->{$k}{"data"}{"month"} and $new->{$k}{"data"}{"year"} == $old->{$k}{"data"}{"year"})
!ll&&!rl&&r
001
$new->{$k}{"data"}{"day"} == $old->{$k}{"data"}{"day"} and $new->{$k}{"data"}{"month"} == $old->{$k}{"data"}{"month"} and $new->{$k}{"data"}{"year"} == $old->{$k}{"data"}{"year"}
!ll&&!rl&&r
001
$new->{$k}{"data"}{"day"} == $old->{$k}{"data"}{"day"} and $new->{$k}{"data"}{"month"} == $old->{$k}{"data"}{"month"}
!ll&&!rl&&r
001
MC/DC0%0/4
$old and ($new->{$k}{"data"}{"day"} == $old->{$k}{"data"}{"day"} and $new->{$k}{"data"}{"month"} == $old->{$k}{"data"}{"month"} and $new->{$k}{"data"}{"year"} == $old->{$k}{"data"}{"year"})
$old $new->{$k}{"data"}{"day"} == $old->{$k}{"data"}{"day"} $new->{$k}{"data"}{"month"} == $old->{$k}{"data"}{"month"} $new->{$k}{"data"}{"year"} == $old->{$k}{"data"}{"year"}
Truth table20%1/5
A and B and C and D
A= $oldB= $new->{$k}{"data"}{"day"} == $old->{$k}{"data"}{"day"}C= $new->{$k}{"data"}{"month"} == $old->{$k}{"data"}{"month"}D= $new->{$k}{"data"}{"year"} == $old->{$k}{"data"}{"year"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
596 $new->{$k}{data}{day} == $old->{$k}{data}{day} &&
597 $new->{$k}{data}{month} == $old->{$k}{data}{month} &&
598 $new->{$k}{data}{year} == $old->{$k}{data}{year}
599 )) {
600 1 $changed = 1;
601 1 $need_newdate = 1;
602 }
603  
604 } else {
605 3 my $n = join ':', sort %{ $new->{$k} };
606 3 my $o = join ':', sort %{ $old->{$k} };
607 3 ▶ if ($n ne $o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n ne $o) 1 2 3
608 1 $changed = 1;
609 }
610 }
611 }
612  
613 7 ▶ return 1, 0 if $changed == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $changed == 0 0 7 7
614  
615 7 ▶ if ($need_newdate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_newdate) { } 1 6 7
616 1 my ($day, $month, $year) = (localtime)[3,4,5];
617 1 $new->{3} = {
618 label => 'lastchange',
619 label_id => 3,
620 font => 0,
621 data => {
622 year => $year,
623 month => $month,
624 day => $day,
625 },
626 };
627 } else {
628 # XXX Need to actually validate the above information somehow
629 6 ▶ if ($new->{3}->{data}->{year} >= 1900) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new->{3}{"data"}{"year"} >= 1900) 0 6 6
630 0 $new->{3}->{data}->{year} -= 1900;
631 }
632 }
633  
634 7 my $plaintext;
635 7 foreach my $k (keys %{ $new }) {
636 24 ▶ next if $new->{$k}->{label_id} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $new->{$k}{"label_id"} == 0 7 17 24
637 17 $plaintext .= _pack_field($new->{$k});
638 }
639 7 $plaintext .= chr(0xff) x 2;
640  
641 7 my $encrypted;
642 7 ▶ if ($c->{name} eq 'None') {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->{'name'} eq 'None') { } 7 0 7
elsif ($c->{'name'} eq 'DES_EDE3' or $c->{'name'} eq 'Rijndael') { } 0 0 0
643 # do nothing
644 7 $encrypted = $plaintext;
645  
646 ▶ } elsif ($c->{name} eq 'DES_EDE3' or $c->{name} eq 'Rijndael') {
Condition0%0/3
$c->{"name"} eq "DES_EDE3" or $c->{"name"} eq "Rijndael"
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"name"} eq "DES_EDE3" or $c->{"name"} eq "Rijndael"
$c->{"name"} eq "DES_EDE3" $c->{"name"} eq "Rijndael"
Truth table0%0/3
A or B
A= $c->{"name"} eq "DES_EDE3"B= $c->{"name"} eq "Rijndael"
ABresult
1X1
011
000
647 0 require Crypt::CBC;
648 my $cbc = Crypt::CBC->new(
649 -key => $key,
650 -literal_key => 1,
651 -iv => $ivec,
652 -cipher => $c->{name},
653 -keysize => $c->{keylen},
654 -blocksize => $c->{blocksize},
655 0 -header => 'none',
656 -padding => 'oneandzeroes',
657 );
658  
659 0 ▶ if (! $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c) 0 0 0
660 0 croak("Unable to set up encryption!");
661 }
662  
663 0 $encrypted = $cbc->encrypt($plaintext);
664  
665 } else {
666 0 croak "Unsupported Crypt $c->{name}";
667 }
668  
669 7 return $encrypted, $ivec;
670 }
671  
672 # Decrypt
673  
674 sub Decrypt
675 26 ▶{
Subroutine Decrypt: called CC=13 SCAR=28.4
Pod documented
676 26 my $self = shift;
677 26 my $rec = shift;
678 26 ▶ my $pass = shift || $self->{password};
Condition100%3/3
shift() || $self->{'password'}
l!l&&r!l&&!r
5417
MC/DC100%2/2
shift() || $self->{'password'}
shift() $self->{'password'}
Truth table100%3/3
A || B
A= shift()B= $self->{'password'}
ABresult
1X1
011
000
679  
680 26 ▶ if ( ! $pass && ! $self->{appinfo}->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass or $self->{"appinfo"}{"key"}) 2 24 26
Condition100%3/3
$pass or $self->{"appinfo"}{"key"}
l!l&&r!l&&!r
9152
MC/DC100%2/2
$pass or $self->{"appinfo"}{"key"}
$pass $self->{"appinfo"}{"key"}
Truth table100%3/3
A or B
A= $passB= $self->{"appinfo"}{"key"}
ABresult
1X1
011
000
681 2 croak("password not set!\n");
682 }
683  
684 24 ▶ if ( ! $rec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rec) 0 24 24
685 0 croak("Needed parameter 'record' not passed!\n");
686 }
687  
688 24 ▶ if ( $pass && ! $self->Password($pass)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass and not $self->Password($pass)) 0 24 24
Condition66%2/3
$pass and not $self->Password($pass)
!ll&&!rl&&r
1590
MC/DC0%0/2
$pass and not $self->Password($pass)
$pass not $self->Password($pass)
Truth table66%2/3
A and not(B)
A= $passB= not $self->Password($pass)
ABresult
0X0
100
111
689 0 croak("Invalid Password!\n");
690 }
691  
692 24 ▶ if ( ! $rec->{encrypted} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rec->{"encrypted"}) 0 24 24
693 0 croak("No encrypted content!");
694 }
695  
696 24 my $plaintext;
697 24 ▶ if ($self->{version} == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'version'} == 4) { } 6 18 24
elsif ($self->{'version'} == 5) { } 18 0 18
698 6 ▶ $self->{digest} ||= _calc_keys( $pass );
Condition33%1/3
$self->{"digest"} ||= _calc_keys($pass)
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->{"digest"} ||= _calc_keys($pass)
$self->{"digest"} _calc_keys($pass)
Truth table33%1/3
A ||= B
A= $self->{"digest"}B= _calc_keys($pass)
ABresult
1X1
011
000
699 6 my $acct = _decrypt_v4($rec->{encrypted}, $self->{digest});
700 $plaintext = {
701 0 => $rec->{plaintext}->{0},
702 1 => {
703 label => 'account',
704 label_id => 1,
705 font => 0,
706 data => $acct->{account},
707 },
708 2 => {
709 label => 'password',
710 label_id => 2,
711 font => 0,
712 data => $acct->{password},
713 },
714 3 => {
715 label => 'lastchange',
716 label_id => 3,
717 font => 0,
718 data => $acct->{lastchange},
719 },
720 255 => {
721 label => 'notes',
722 label_id => 255,
723 font => 0,
724 data => $acct->{notes},
725 },
726 6 };
727  
728 } elsif ($self->{version} == 5) {
729 $plaintext = _decrypt_v5(
730 $rec->{encrypted}, $self->{appinfo}->{key},
731 $self->{appinfo}->{cipher}, $rec->{ivec},
732 18 );
733 18 ▶ $plaintext->{0} ||= $rec->{plaintext}->{0};
Condition33%1/3
$plaintext->{0} ||= $rec->{"plaintext"}{0}
l!l&&r!l&&!r
0180
MC/DC0%0/2
$plaintext->{0} ||= $rec->{"plaintext"}{0}
$plaintext->{0} $rec->{"plaintext"}{0}
Truth table33%1/3
A ||= B
A= $plaintext->{0}B= $rec->{"plaintext"}{0}
ABresult
1X1
011
000
734  
735 } else {
736 0 croak "Unsupported Version $self->{version}";
737 }
738  
739 24 ▶ if ($plaintext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plaintext) 24 0 24
740 24 $rec->{plaintext} = $plaintext;
741 24 return $plaintext;
742 }
743 0 return;
744 }
745  
746 sub _decrypt_v4
747 6 ▶{
Subroutine _decrypt_v4: called CC=2 SCAR=7.0
748 6 my $encrypted = shift;
749 6 my $digest = shift;
750  
751 6 my $plaintext = _crypt3des( $encrypted, $digest, $DECRYPT );
752 6 my ( $account, $password, $notes, $packed_date )
753 = split /$NULL/xm, $plaintext, 4;
754  
755 6 my $modified;
756 6 ▶ if ($packed_date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packed_date) 6 0 6
757 6 $modified = _parse_keyring_date($packed_date);
758 }
759  
760 return {
761 6 account => $account,
762 password => $password,
763 notes => $notes,
764 lastchange => $modified,
765 };
766 }
767  
768 sub _decrypt_v5
769 18 ▶{
Subroutine _decrypt_v5: called CC=8 SCAR=26.6
770  
771 18 my $encrypted = shift;
772 18 my $key = shift;
773 18 my $cipher = shift;
774 18 my $ivec = shift;
775  
776 18 ▶ my $c = crypts($cipher) or croak('Unknown cipher ' . $cipher);
Branch: 1 branch
BranchTrueFalseTotal
my $c = crypts($cipher) or croak('Unknown cipher ' . $cipher) 18 0 18
777  
778 18 my $plaintext;
779  
780 18 ▶ if ($c->{name} eq 'None') {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->{'name'} eq 'None') { } 18 0 18
elsif ($c->{'name'} eq 'DES_EDE3' or $c->{'name'} eq 'Rijndael') { } 0 0 0
781 # do nothing
782 18 $plaintext = $encrypted;
783  
784 ▶ } elsif ($c->{name} eq 'DES_EDE3' or $c->{name} eq 'Rijndael') {
Condition0%0/3
$c->{"name"} eq "DES_EDE3" or $c->{"name"} eq "Rijndael"
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"name"} eq "DES_EDE3" or $c->{"name"} eq "Rijndael"
$c->{"name"} eq "DES_EDE3" $c->{"name"} eq "Rijndael"
Truth table0%0/3
A or B
A= $c->{"name"} eq "DES_EDE3"B= $c->{"name"} eq "Rijndael"
ABresult
1X1
011
000
785 0 require Crypt::CBC;
786 my $cbc = Crypt::CBC->new(
787 -key => $key,
788 -literal_key => 1,
789 -iv => $ivec,
790 -cipher => $c->{name},
791 -keysize => $c->{keylen},
792 -blocksize => $c->{blocksize},
793 0 -header => 'none',
794 -padding => 'oneandzeroes',
795 );
796  
797 0 ▶ if (! $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c) 0 0 0
798 0 croak("Unable to set up encryption!");
799 }
800 0 my $len = $c->{blocksize} - length($encrypted) % $c->{blocksize};
801 0 $encrypted .= $NULL x $len;
802 0 $plaintext = $cbc->decrypt($encrypted);
803  
804 } else {
805 0 croak "Unsupported Crypt $c->{name}";
806 }
807  
808 18 my %fields;
809 18 ▶ while ($plaintext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plaintext) 63 0 63
810 63 my $field;
811 63 ($field, $plaintext) = _parse_field($plaintext);
812 63 ▶ if (! $field) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field) 18 45 63
813 18 last;
814 }
815 45 $fields{ $field->{label_id} } = $field;
816 }
817  
818 18 return \%fields;
819 }
820  
821 # Password
822  
823 sub Password
824 31 ▶{
Subroutine Password: called CC=17 SCAR=28.9
Pod documented
825 31 my $self = shift;
826 31 my $pass = shift;
827 31 my $new_pass = shift;
828  
829 31 ▶ if (! $pass) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 4 27 31
830 4 delete $self->{password};
831 4 delete $self->{appinfo}->{key};
832 4 return 1;
833 }
834  
835 27 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"version"} == 4 and not exists $self->{"encpassword"} or $self->{"version"} == 5 and not exists $self->{"appinfo"}{"masterhash"}) 2 25 27
Condition100%9/9
$self->{"version"} == 4 and not exists $self->{"encpassword"} or $self->{"version"} == 5 and not exists $self->{"appinfo"}{"masterhash"}
l!l&&r!l&&!r
1125
$self->{"version"} == 4 and not exists $self->{"encpassword"}
!ll&&!rl&&r
14121
$self->{"version"} == 5 and not exists $self->{"appinfo"}{"masterhash"}
!ll&&!rl&&r
12131
MC/DC75%3/4
$self->{"version"} == 4 and not exists $self->{"encpassword"} or $self->{"version"} == 5 and not exists $self->{"appinfo"}{"masterhash"}
$self->{"version"} == 4 not exists $self->{"encpassword"} $self->{"version"} == 5 not exists $self->{"appinfo"}{"masterhash"}
Truth table57%4/7
A and not(B) or C and not(D)
A= $self->{"version"} == 4B= not exists $self->{"encpassword"}C= $self->{"version"} == 5D= not exists $self->{"appinfo"}{"masterhash"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
836 ($self->{version} == 4 && ! exists $self->{encpassword}) ||
837 ($self->{version} == 5 && ! exists $self->{appinfo}->{masterhash})
838 ) {
839 2 return $self->_password_update($pass);
840 }
841  
842 25 ▶ if ($new_pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_pass) 2 23 25
843 2 my @accts = ();
844 2 foreach my $rec (@{ $self->{records} }) {
845 4 my $acct = $self->Decrypt($rec, $pass);
846 4 ▶ if ( ! $acct ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($acct) 0 4 4
847 0 croak("Couldn't decrypt $rec->{plaintext}->{0}->{data}");
848 }
849 4 push @accts, $acct;
850 }
851  
852 2 ▶ if ( ! $self->_password_update($new_pass)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_password_update($new_pass)) 0 2 2
853 0 croak("Couldn't set new password!");
854 }
855 2 $pass = $new_pass;
856  
857 2 foreach my $i (0..$#accts) {
858 4 delete $self->{records}->[$i]->{encrypted};
859 4 $self->{records}->[$i]->{plaintext} = $accts[$i];
860 4 $self->Encrypt($self->{records}->[$i], $pass);
861 }
862 }
863  
864 25 ▶ if (defined $self->{password} && $pass eq $self->{password}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"password"} and $pass eq $self->{"password"}) 10 15 25
Condition66%2/3
defined $self->{"password"} and $pass eq $self->{"password"}
!ll&&!rl&&r
15010
MC/DC50%1/2
defined $self->{"password"} and $pass eq $self->{"password"}
defined $self->{"password"} $pass eq $self->{"password"}
Truth table66%2/3
A and B
A= defined $self->{"password"}B= $pass eq $self->{"password"}
ABresult
0X0
100
111
865 # already verified this password
866 10 return 1;
867 }
868  
869 15 ▶ if ($self->{version} == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'version'} == 4) { } 2 13 15
elsif ($self->{'version'} == 5) { } 13 0 13
870 2 my $valid = _password_verify_v4($pass, $self->{encpassword});
871  
872 # May as well generate the keys we need now,
873 # since we know the password is right
874 2 ▶ if ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid) 2 0 2
875 2 $self->{digest} = _calc_keys($pass);
876 2 ▶ if ($self->{digest} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"digest"}) 2 0 2
877 2 $self->{password} = $pass;
878 2 return 1;
879 }
880 }
881 } elsif ($self->{version} == 5) {
882 13 return _password_verify_v5($self->{appinfo}, $pass);
883 } else {
884 0 croak "Unsupported version $self->{version}";
885 }
886  
887 0 return;
888 }
889  
890 sub _password_verify_v4
891 2 ▶{
Subroutine _password_verify_v4: called CC=4 SCAR=14.6
892 2 require Digest::MD5;
893 2 import Digest::MD5 qw(md5);
894  
895 2 my $pass = shift;
896 2 my $data = shift;
897  
898 0 ▶ if (! $pass) { croak('No password specified!'); };
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass) 0 2 2
899  
900 # XXX die "No encrypted password in file!" unless defined $data;
901 0 ▶ if ( ! defined $data) { return; };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 2 2
902  
903 2 $data =~ s/$NULL$//xm;
904  
905 2 my $salt = substr $data, 0, $kSalt_Size;
906  
907 2 my $msg = $salt . $pass;
908 2 $msg .= "\0" x ( $MD5_CBLOCK - length $msg );
909  
910 2 my $digest = md5($msg);
911  
912 2 ▶ if ($data ne $salt . $digest ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne $salt . $digest) 0 2 2
913 0 return;
914 }
915  
916 2 return 1;
917 }
918  
919 sub _password_verify_v5
920 13 ▶{
Subroutine _password_verify_v5: called CC=3 SCAR=11.3
921 13 my $appinfo = shift;
922 13 my $pass = shift;
923  
924 13 my $salt = pack("H*", $appinfo->{salt});
925  
926 my $c = crypts($appinfo->{cipher})
927 13 ▶ or croak('Unknown cipher ' . $appinfo->{cipher});
Branch: 1 branch
BranchTrueFalseTotal
my $c = crypts($appinfo->{"cipher"}) or croak('Unknown cipher ' . $appinfo->{"cipher"}) 13 0 13
928 my ($key, $hash) = _calc_key_v5(
929 $pass, $salt, $appinfo->{iter},
930 $c->{keylen},
931 $c->{DES_odd_parity},
932 13 );
933  
934 #print "Iter: '" . $appinfo->{iter} . "'\n";
935 #print "Key: '". unpack("H*", $key) . "'\n";
936 #print "Salt: '". unpack("H*", $salt) . "'\n";
937 #print "Hash: '". $hash . "'\n";
938 #print "Hash: '". $appinfo->{masterhash} . "'\n";
939  
940 13 ▶ if ($appinfo->{masterhash} eq $hash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($appinfo->{'masterhash'} eq $hash) { } 13 0 13
941 13 $appinfo->{key} = $key;
942 } else {
943 0 return;
944 }
945  
946 13 return $key;
947 }
948  
949  
950 sub _password_update
951 4 ▶{
Subroutine _password_update: called CC=8 SCAR=24.4
952 # It is very important to Encrypt after calling this
953 # (Although it is generally only called by Encrypt)
954 # because otherwise the data will be out of sync with the
955 # password, and that would suck!
956 4 my $self = shift;
957 4 my $pass = shift;
958  
959 4 ▶ if ($self->{version} == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'version'} == 4) { } 2 2 4
elsif ($self->{'version'} == 5) { } 2 0 2
960 2 my $data = _password_update_v4($pass, @_);
961  
962 2 ▶ if (! $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 2 2
963 0 carp("Failed to update password!");
964 0 return;
965 }
966  
967 # AFAIK the thing we use to test the password is
968 # always in the first entry
969 2 $self->{encpassword} = $data;
970 2 $self->{password} = $pass;
971 2 $self->{digest} = _calc_keys( $self->{password} );
972  
973 2 return 1;
974  
975 } elsif ($self->{version} == 5) {
976 2 ▶ my $cipher = shift || $self->{appinfo}->{cipher};
Condition33%1/3
shift() || $self->{'appinfo'}{'cipher'}
l!l&&r!l&&!r
002
MC/DC0%0/2
shift() || $self->{'appinfo'}{'cipher'}
shift() $self->{'appinfo'}{'cipher'}
Truth table33%1/3
A || B
A= shift()B= $self->{'appinfo'}{'cipher'}
ABresult
1X1
011
000
977 2 ▶ my $iter = shift || $self->{appinfo}->{iter};
Condition33%1/3
shift() || $self->{'appinfo'}{'iter'}
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() || $self->{'appinfo'}{'iter'}
shift() $self->{'appinfo'}{'iter'}
Truth table33%1/3
A || B
A= shift()B= $self->{'appinfo'}{'iter'}
ABresult
1X1
011
000
978 2 ▶ my $salt = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
979  
980 my $hash = _password_update_v5(
981 2 $self->{appinfo}, $pass, $cipher, $iter, $salt
982 );
983  
984 2 ▶ if (! $hash) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash) 0 2 2
985 0 carp("Failed to update password!");
986 0 return;
987 }
988  
989 2 return 1;
990 } else {
991 0 croak("Unsupported version ($self->{version})");
992 }
993  
994 0 return;
995 }
996  
997 sub _password_update_v4
998 2 ▶{
Subroutine _password_update_v4: called CC=3 SCAR=11.1
999 2 require Digest::MD5;
1000 2 import Digest::MD5 qw(md5);
1001  
1002 2 my $pass = shift;
1003
1004 0 ▶ if (! defined $pass) { croak('No password specified!'); };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pass) 0 2 2
1005  
1006 2 my $salt;
1007 2 for ( 1 .. $kSalt_Size ) {
1008 8 $salt .= chr int rand 255;
1009 }
1010  
1011 2 my $msg = $salt . $pass;
1012  
1013 2 $msg .= "\0" x ( $MD5_CBLOCK - length $msg );
1014  
1015 2 my $digest = md5($msg);
1016  
1017 2 my $data = $salt . $digest; # . "\0";
1018  
1019 2 return $data;
1020 }
1021  
1022 sub _password_update_v5
1023 2 ▶{
Subroutine _password_update_v5: called CC=3 SCAR=11.1
1024 2 my $appinfo = shift;
1025 2 my $pass = shift;
1026 2 my $cipher = shift;
1027 2 my $iter = shift;
1028  
1029 # I thought this needed to be 'blocksize', but apparently not.
1030 #my $length = $CRYPTS[ $cipher ]{blocksize};
1031 2 my $length = 8;
1032 2 ▶ my $salt = shift || pack("C*",map {rand(256)} 1..$length);
Condition33%1/3
shift() || pack('C*', map({rand 256;} 1 .. $length))
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() || pack('C*', map({rand 256;} 1 .. $length))
shift() pack('C*', map({rand 256;} 1 .. $length))
Truth table33%1/3
A || B
A= shift()B= pack('C*', map({rand 256;} 1 .. $length))
ABresult
1X1
011
000
1033  
1034 2 ▶ my $c = crypts($cipher) or croak('Unknown cipher ' . $cipher);
Branch: 1 branch
BranchTrueFalseTotal
my $c = crypts($cipher) or croak('Unknown cipher ' . $cipher) 2 0 2
1035 my ($key, $hash) = _calc_key_v5(
1036 $pass, $salt, $iter,
1037 $c->{keylen},
1038 $c->{DES_odd_parity},
1039 2 );
1040  
1041 2 $appinfo->{salt} = unpack "H*", $salt;
1042 2 $appinfo->{iter} = $iter;
1043 2 $appinfo->{cipher} = $cipher;
1044 2 $appinfo->{masterhash} = $hash;
1045 2 $appinfo->{key} = $key;
1046  
1047 2 return $key;
1048 }
1049  
1050 sub Unlock
1051 2 ▶{
Subroutine Unlock: called CC=5 SCAR=18.1
Pod documented
1052 2 my $self = shift;
1053 2 my ($pass) = @_;
1054 2 ▶ $pass ||= $self->{password};
Condition33%1/3
$pass ||= $self->{"password"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$pass ||= $self->{"password"}
$pass $self->{"password"}
Truth table33%1/3
A ||= B
A= $passB= $self->{"password"}
ABresult
1X1
011
000
1055  
1056 2 ▶ if ( $pass && ! $self->Password($pass)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass and not $self->Password($pass)) 0 2 2
Condition33%1/3
$pass and not $self->Password($pass)
!ll&&!rl&&r
020
MC/DC0%0/2
$pass and not $self->Password($pass)
$pass not $self->Password($pass)
Truth table33%1/3
A and not(B)
A= $passB= not $self->Password($pass)
ABresult
0X0
100
111
1057 0 croak("Invalid Password!\n");
1058 }
1059  
1060 2 foreach my $rec (@{ $self->{records} }) {
1061 4 $self->Decrypt($rec);
1062 }
1063  
1064 2 return 1;
1065  
1066 }
1067  
1068 sub Lock
1069 2 ▶{
Subroutine Lock: called CC=2 SCAR=6.9
Pod documented
1070 2 my $self = shift;
1071  
1072 2 $self->Password();
1073  
1074 2 foreach my $rec (@{ $self->{records} }) {
1075 4 my $name = $rec->{plaintext}->{0};
1076 4 delete $rec->{plaintext};
1077 4 $rec->{plaintext}->{0} = $name;
1078 }
1079  
1080 2 return 1;
1081 }
1082  
1083 # Helpers
1084  
1085 sub _calc_keys
1086 4 ▶{
Subroutine _calc_keys: called CC=2 SCAR=7.1
1087 4 my $pass = shift;
1088 0 ▶ if (! defined $pass) { croak('No password defined!'); };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pass) 0 4 4
1089  
1090 4 my $digest = md5($pass);
1091  
1092 4 my ( $key1, $key2 ) = unpack 'a8a8', $digest;
1093  
1094 #--------------------------------------------------
1095 # print "key1: $key1: ", length $key1, "\n";
1096 # print "key2: $key2: ", length $key2, "\n";
1097 #--------------------------------------------------
1098  
1099 4 $digest = unpack 'H*', $key1 . $key2 . $key1;
1100  
1101 #--------------------------------------------------
1102 # print "Digest: ", $digest, "\n";
1103 # print length $digest, "\n";
1104 #--------------------------------------------------
1105  
1106 4 return $digest;
1107 }
1108  
1109 sub _calc_key_v5
1110 15 ▶{
Subroutine _calc_key_v5: called CC=2 SCAR=7.0
1111 15 my ($pass, $salt, $iter, $keylen, $dop) = @_;
1112  
1113 15 require Digest::HMAC_SHA1;
1114 15 import Digest::HMAC_SHA1 qw(hmac_sha1);
1115 15 require Digest::SHA1;
1116 15 import Digest::SHA1 qw(sha1);
1117  
1118 15 my $key = _pbkdf2( $pass, $salt, $iter, $keylen, \&hmac_sha1 );
1119 0 ▶ if ($dop) { $key = _DES_odd_parity($key); }
Branch: 1 branch
BranchTrueFalseTotal
if ($dop) 0 15 15
1120  
1121 15 my $hash = unpack("H*", substr(sha1($key.$salt),0, 8));
1122  
1123 15 return $key, $hash;
1124 }
1125  
1126 sub _crypt3des
1127 9 ▶{
Subroutine _crypt3des: called CC=7 SCAR=19.5
1128 9 require Crypt::DES;
1129  
1130 9 my ( $plaintext, $passphrase, $flag ) = @_;
1131  
1132 9 $passphrase .= $SPACE x ( 16 * 3 );
1133 9 my $cyphertext = $EMPTY;
1134  
1135 9 my $size = length $plaintext;
1136  
1137 #print "STRING: '$plaintext' - Length: " . (length $plaintext) . "\n";
1138  
1139 9 my @C;
1140 9 for ( 0 .. 2 ) {
1141 27 $C[$_] =
1142 new Crypt::DES( pack 'H*', ( substr $passphrase, 16 * $_, 16 ));
1143 }
1144  
1145 9 for ( 0 .. ( ($size) / 8 ) ) {
1146 60 my $pt = substr $plaintext, $_ * 8, 8;
1147  
1148 #print "PT: '$pt' - Length: " . length($pt) . "\n";
1149 60 ▶ if (! length $pt) { next; };
Branch: 1 branch
BranchTrueFalseTotal
unless (length $pt) 6 54 60
1150 54 ▶ if ( (length $pt) < 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pt < 8) 3 51 54
1151 0 ▶ if ($flag == $DECRYPT) { croak('record not 8 byte padded'); };
Branch: 1 branch
BranchTrueFalseTotal
if ($flag == $DECRYPT) 0 3 3
1152 3 my $len = 8 - (length $pt);
1153 3 $pt .= ($NULL x $len);
1154 }
1155 54 ▶ if ( $flag == $ENCRYPT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag == $ENCRYPT) { } 18 36 54
1156 18 $pt = $C[0]->encrypt($pt);
1157 18 $pt = $C[1]->decrypt($pt);
1158 18 $pt = $C[2]->encrypt($pt);
1159 }
1160 else {
1161 36 $pt = $C[0]->decrypt($pt);
1162 36 $pt = $C[1]->encrypt($pt);
1163 36 $pt = $C[2]->decrypt($pt);
1164 }
1165  
1166 #print "PT: '$pt' - Length: " . length($pt) . "\n";
1167 54 $cyphertext .= $pt;
1168 }
1169  
1170 9 $cyphertext =~ s/$NULL+$//xm;
1171  
1172 #print "CT: '$cyphertext' - Length: " . length($cyphertext) . "\n";
1173  
1174 9 return $cyphertext;
1175 }
1176  
1177 sub _parse_field
1178 78 ▶{
Subroutine _parse_field: called CC=7 SCAR=19.5
1179 78 my $field = shift;
1180  
1181 78 my ($len) = unpack "n", $field;
1182 78 ▶ if ($len + 4 > length $field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len + 4 > length $field) 18 60 78
1183 18 return undef, $field;
1184 }
1185 60 my $unpackstr = "x2 C1 C1 A$len";
1186 60 my $offset = 2 +1 +1 +$len;
1187 60 ▶ if ($len % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len % 2) 15 45 60
1188 # trim the 0/1 byte padding for next even address.
1189 15 $offset++;
1190 15 $unpackstr .= ' x'
1191 }
1192  
1193 60 my ($label, $font, $data) = unpack $unpackstr, $field;
1194 60 my $leftover = substr $field, $offset;
1195  
1196 60 my $label_id = $label;
1197 60 my $l = labels($label);
1198 60 ▶ if ($l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l) 60 0 60
1199 60 ▶ $label = $l->{name} || $l->{id};
Condition33%1/3
$l->{'name'} || $l->{'id'}
l!l&&r!l&&!r
6000
MC/DC0%0/2
$l->{'name'} || $l->{'id'}
$l->{'name'} $l->{'id'}
Truth table33%1/3
A || B
A= $l->{'name'}B= $l->{'id'}
ABresult
1X1
011
000
1200 60 $label_id = $l->{id};
1201 }
1202  
1203 60 ▶ if ($label_id && $label_id == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label_id and $label_id == 3) 18 42 60
Condition100%3/3
$label_id and $label_id == 3
!ll&&!rl&&r
152718
MC/DC100%2/2
$label_id and $label_id == 3
$label_id $label_id == 3
Truth table100%3/3
A and B
A= $label_idB= $label_id == 3
ABresult
0X0
100
111
1204 18 ($data) = substr $field, 4, $len;
1205 18 $data = _parse_keyring_date($data);
1206 }
1207 return {
1208 #len => $len,
1209 60 label => $label,
1210 label_id => $label_id,
1211 font => $font,
1212 data => $data,
1213 }, $leftover;
1214 }
1215  
1216 sub _pack_field
1217 20 ▶{
Subroutine _pack_field: called CC=11 SCAR=24.4
1218 20 my $field = shift;
1219  
1220 20 my $packed;
1221 20 ▶ if (defined $field) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) { } 20 0 20
1222 20 ▶ my $label = $field->{label_id} || 0;
Condition100%2/2
$field->{'label_id'} || 0
l!l
173
MC/DC100%1/1
$field->{'label_id'} || 0
$field->{'label_id'}
Truth table100%2/2
A
A= $field->{'label_id'}
Aresult
11
00
1223 20 ▶ if (defined $field->{label} && ! $label) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{"label"} and not $label) 3 17 20
Condition66%2/3
defined $field->{"label"} and not $label
!ll&&!rl&&r
0173
MC/DC50%1/2
defined $field->{"label"} and not $label
defined $field->{"label"} not $label
Truth table66%2/3
A and not(B)
A= defined $field->{"label"}B= not $label
ABresult
0X0
100
111
1224 3 $label = $field->{label};
1225 }
1226  
1227 20 my $l = labels($field->{label});
1228 20 ▶ if ($l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l) 20 0 20
1229 20 $label = $l->{id};
1230 }
1231  
1232 20 ▶ my $font = $field->{font} || 0;
Condition50%1/2
$field->{'font'} || 0
l!l
020
MC/DC0%0/1
$field->{'font'} || 0
$field->{'font'}
Truth table50%1/2
A
A= $field->{'font'}
Aresult
11
00
1233 20 ▶ my $data = defined $field->{data} ? $field->{data} : $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
defined $field->{'data'} ? : 20 0 20
1234  
1235 20 ▶ if ($label && $label == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label and $label == 3) 7 13 20
Condition100%3/3
$label and $label == 3
!ll&&!rl&&r
3107
MC/DC100%2/2
$label and $label == 3
$label $label == 3
Truth table100%3/3
A and B
A= $labelB= $label == 3
ABresult
0X0
100
111
1236 7 $data = _pack_keyring_date($data);
1237 }
1238 20 my $len = length $data;
1239 20 my $packstr = "n1 C1 C1 A*";
1240  
1241 20 $packed = pack $packstr, ($len, $label, $font, $data);
1242  
1243 20 ▶ if ($len % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len % 2) 6 14 20
1244 # add byte padding for next even address.
1245 6 $packed .= $NULL;
1246 }
1247 } else {
1248 0 my $packstr = "n1 C1 C1 x1";
1249 0 $packed = pack $packstr, 0, 0, 0;
1250 }
1251  
1252 20 return $packed;
1253 }
1254  
1255 sub _parse_keyring_date
1256 24 ▶{
Subroutine _parse_keyring_date: called CC=3 SCAR=11.2
1257 24 my $data = shift;
1258  
1259 24 my $u = unpack 'n', $data;
1260 24 my $year = (($u & 0xFE00) >> 9) + 4; # since 1900
1261 24 my $month = (($u & 0x01E0) >> 5) - 1; # 0-11
1262 24 my $day = (($u & 0x001F) >> 0); # 1-31
1263  
1264 return {
1265 24 ▶ year => $year,
Condition50%2/4
$month || 0
l!l
240
$day || 1
l!l
240
MC/DC0%0/1
$month || 0
$month
MC/DC0%0/1
$day || 1
$day
Truth table50%1/2
A
A= $month
Aresult
11
00
Truth table50%1/2
A
A= $day
Aresult
11
00
1266 month => $month || 0,
1267 day => $day || 1,
1268 };
1269 }
1270  
1271 sub _pack_keyring_date
1272 10 ▶{
Subroutine _pack_keyring_date: called CC=1 SCAR=0.0
1273 10 my $d = shift;
1274 10 my $year = $d->{year};
1275 10 my $month = $d->{month};
1276 10 my $day = $d->{day};
1277  
1278 10 $year -= 4;
1279 10 $month++;
1280  
1281 10 return pack 'n*', $day | ($month << 5) | ($year << 9);
1282 }
1283  
1284  
1285 sub _hexdump
1286 0 ▶{
Subroutine _hexdump: not called CC=4 SCAR=30.0
1287 0 my $prefix = shift; # What to print in front of each line
1288 0 my $data = shift; # The data to dump
1289 0 my $maxlines = shift; # Max # of lines to dump
1290 0 my $offset; # Offset of current chunk
1291  
1292 0 ▶ for ($offset = 0; $offset < length($data); $offset += 16)
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $data) 0 0 0
1293 {
1294 0 my $hex; # Hex values of the data
1295 my $ascii; # ASCII values of the data
1296 0 my $chunk; # Current chunk of data
1297  
1298 0 ▶ last if defined($maxlines) && ($offset >= ($maxlines * 16));
Branch: 1 branch
BranchTrueFalseTotal
if defined $maxlines and $offset >= $maxlines * 16 0 0 0
Condition0%0/3
defined $maxlines and $offset >= $maxlines * 16
!ll&&!rl&&r
000
MC/DC0%0/2
defined $maxlines and $offset >= $maxlines * 16
defined $maxlines $offset >= $maxlines * 16
Truth table0%0/3
A and B
A= defined $maxlinesB= $offset >= $maxlines * 16
ABresult
0X0
100
111
1299  
1300 0 $chunk = substr($data, $offset, 16);
1301  
1302 0 ($hex = $chunk) =~ s/./sprintf "%02x ", ord($&)/ges;
1303  
1304 0 ($ascii = $chunk) =~ y/\040-\176/./c;
1305  
1306 0 printf "%s %-48s|%-16s|\n", $prefix, $hex, $ascii;
1307 }
1308 }
1309  
1310 sub _bindump
1311 0 ▶{
Subroutine _bindump: not called CC=4 SCAR=30.0
1312 0 my $prefix = shift; # What to print in front of each line
1313 0 my $data = shift; # The data to dump
1314 0 my $maxlines = shift; # Max # of lines to dump
1315 0 my $offset; # Offset of current chunk
1316  
1317 0 ▶ for ($offset = 0; $offset < length($data); $offset += 8)
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $data) 0 0 0
1318 {
1319 0 my $bin; # binary values of the data
1320 my $ascii; # ASCII values of the data
1321 0 my $chunk; # Current chunk of data
1322  
1323 0 ▶ last if defined($maxlines) && ($offset >= ($maxlines * 8));
Branch: 1 branch
BranchTrueFalseTotal
if defined $maxlines and $offset >= $maxlines * 8 0 0 0
Condition0%0/3
defined $maxlines and $offset >= $maxlines * 8
!ll&&!rl&&r
000
MC/DC0%0/2
defined $maxlines and $offset >= $maxlines * 8
defined $maxlines $offset >= $maxlines * 8
Truth table0%0/3
A and B
A= defined $maxlinesB= $offset >= $maxlines * 8
ABresult
0X0
100
111
1324  
1325 0 $chunk = substr($data, $offset, 8);
1326  
1327 0 ($bin = $chunk) =~ s/./sprintf "%08b ", ord($&)/ges;
1328  
1329 0 ($ascii = $chunk) =~ y/\040-\176/./c;
1330  
1331 0 printf "%s %-72s|%-8s|\n", $prefix, $bin, $ascii;
1332 }
1333 }
1334  
1335 # Thanks to Jochen Hoenicke <hoenicke@gmail.com>
1336 # (one of the authors of Palm Keyring)
1337 # for these next two subs.
1338  
1339 # Usage pbkdf2(password, salt, iter, keylen, prf)
1340 # iter is number of iterations
1341 # keylen is length of generated key in bytes
1342 # prf is the pseudo random function (e.g. hmac_sha1)
1343 # returns the key.
1344 sub _pbkdf2($$$$$)
1345 15 ▶{
Subroutine _pbkdf2: called CC=3 SCAR=11.0
1346 15 my ($password, $salt, $iter, $keylen, $prf) = @_;
1347 15 my ($k, $t, $u, $ui, $i);
1348 15 $t = "";
1349 15 ▶ for ($k = 1; length($t) < $keylen; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $t < $keylen) 15 15 30
1350 15 $u = $ui = &$prf($salt.pack('N', $k), $password);
1351 15 ▶ for ($i = 1; $i < $iter; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $iter) 7485 15 7500
1352 7485 $ui = &$prf($ui, $password);
1353 7485 $u ^= $ui;
1354 }
1355 15 $t .= $u;
1356 }
1357 15 return substr($t, 0, $keylen);
1358 }
1359  
1360 0 ▶sub _DES_odd_parity($) {
Subroutine _DES_odd_parity: not called CC=2 SCAR=17.9
1361 0 my $key = $_[0];
1362 0 my ($r, $i);
1363 0 my @odd_parity = (
1364 1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14,
1365 16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
1366 32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
1367 49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
1368 64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
1369 81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
1370 97, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,
1371 112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
1372 128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
1373 145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
1374 161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
1375 176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
1376 193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
1377 208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
1378 224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
1379 241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254);
1380 0 ▶ for ($i = 0; $i< length($key); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $key) 0 0 0
1381 0 $r .= chr($odd_parity[ord(substr($key, $i, 1))]);
1382 }
1383 0 return $r;
1384 }
1385  
1386 4 1;
1387 __END__
« Keyring/ConfigData.pm ↑ Summary