Keyring.pm

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