Annotation of palm/Palm-Keyring/lib/Palm/Keyring.pm, Revision 1.62
1.14 andrew 1: package Palm::Keyring;
1.62 ! andrew 2: # $RedRiver: Keyring.pm,v 1.61 2008/09/19 05:55:35 andrew Exp $
1.27 andrew 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.
1.1 andrew 7: #
1.27 andrew 8: # 2006.01.26 #*#*# andrew fresh <andrew@cpan.org>
9: ########################################################################
10: # Copyright (C) 2006, 2007 by Andrew Fresh
1.1 andrew 11: #
1.27 andrew 12: # This program is free software; you can redistribute it and/or modify
13: # it under the same terms as Perl itself.
14: ########################################################################
1.1 andrew 15: use strict;
1.14 andrew 16: use warnings;
1.27 andrew 17:
1.54 andrew 18: require 5.006_001;
19:
1.14 andrew 20: use Carp;
21:
22: use base qw/ Palm::StdAppInfo /;
1.1 andrew 23:
1.24 andrew 24: my $ENCRYPT = 1;
25: my $DECRYPT = 0;
26: my $MD5_CBLOCK = 64;
27: my $kSalt_Size = 4;
28: my $EMPTY = q{};
29: my $SPACE = q{ };
30: my $NULL = chr 0;
1.14 andrew 31:
1.28 andrew 32: my @CRYPTS = (
1.34 andrew 33: {
34: alias => 'None',
1.28 andrew 35: name => 'None',
36: keylen => 8,
37: blocksize => 1,
1.29 andrew 38: default_iter => 500,
1.28 andrew 39: },
1.34 andrew 40: {
41: alias => 'DES-EDE3',
1.28 andrew 42: name => 'DES_EDE3',
43: keylen => 24,
44: blocksize => 8,
45: DES_odd_parity => 1,
1.29 andrew 46: default_iter => 1000,
1.28 andrew 47: },
1.34 andrew 48: {
49: alias => 'AES128',
1.28 andrew 50: name => 'Rijndael',
51: keylen => 16,
52: blocksize => 16,
1.29 andrew 53: default_iter => 100,
1.28 andrew 54: },
1.34 andrew 55: {
56: alias => 'AES256',
1.28 andrew 57: name => 'Rijndael',
58: keylen => 32,
59: blocksize => 16,
1.29 andrew 60: default_iter => 250,
1.28 andrew 61: },
62: );
63:
1.46 andrew 64: 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:
1.1 andrew 87:
1.54 andrew 88: our $VERSION = '0.96_07';
1.28 andrew 89:
90: sub new
91: {
1.14 andrew 92: my $classname = shift;
1.28 andrew 93: my $options = {};
94:
1.46 andrew 95: if (@_) {
96: # hashref arguments
97: if (ref $_[0] eq 'HASH') {
98: $options = shift;
99: }
100:
101: # CGI style arguments
102: elsif ($_[0] =~ /^-[a-zA-Z0-9_]{1,20}$/) {
103: my %tmp = @_;
104: while ( my($key,$value) = each %tmp) {
105: $key =~ s/^-//;
106: $options->{lc $key} = $value;
107: }
108: }
109:
110: else {
111: $options->{password} = shift;
112: $options->{version} = shift;
1.56 andrew 113: $options->{cipher} = shift;
1.46 andrew 114: }
1.28 andrew 115: }
1.1 andrew 116:
1.14 andrew 117: # Create a generic PDB. No need to rebless it, though.
1.28 andrew 118: my $self = $classname->SUPER::new();
1.1 andrew 119:
1.28 andrew 120: $self->{name} = 'Keys-Gtkr'; # Default
121: $self->{creator} = 'Gtkr';
122: $self->{type} = 'Gkyr';
1.14 andrew 123:
124: # The PDB is not a resource database by
125: # default, but it's worth emphasizing,
126: # since MemoDB is explicitly not a PRC.
1.28 andrew 127: $self->{attributes}{resource} = 0;
1.1 andrew 128:
1.28 andrew 129: # Set the version
130: $self->{version} = $options->{version} || 4;
1.1 andrew 131:
1.28 andrew 132: # Set options
133: $self->{options} = $options;
1.1 andrew 134:
1.29 andrew 135: # Set defaults
136: if ($self->{version} == 5) {
137: $self->{options}->{cipher} ||= 0; # 'None'
1.39 andrew 138: my $c = crypts($self->{options}->{cipher})
139: or croak('Unknown cipher ' . $self->{options}->{cipher});
140: $self->{options}->{iterations} ||= $c->{default_iter};
141: $self->{appinfo}->{cipher} ||= $self->{options}->{cipher};
142: $self->{appinfo}->{iter} ||= $self->{options}->{iterations};
1.29 andrew 143: };
144:
1.56 andrew 145: if ( defined $options->{file} ) {
146: $self->Load($options->{file});
147: }
148:
1.28 andrew 149: if ( defined $options->{password} ) {
150: $self->Password($options->{password});
1.14 andrew 151: }
1.1 andrew 152:
1.14 andrew 153: return $self;
154: }
1.1 andrew 155:
1.28 andrew 156: sub import
157: {
1.14 andrew 158: Palm::PDB::RegisterPDBHandlers( __PACKAGE__, [ 'Gtkr', 'Gkyr' ], );
159: return 1;
160: }
1.1 andrew 161:
1.34 andrew 162: # Accessors
163:
164: sub crypts
165: {
166: my $crypt = shift;
1.46 andrew 167: if ((! defined $crypt) || (! length $crypt)) {
1.39 andrew 168: return;
169: } elsif ($crypt =~ /\D/) {
1.34 andrew 170: foreach my $c (@CRYPTS) {
171: if ($c->{alias} eq $crypt) {
172: return $c;
173: }
174: }
175: # didn't find it.
176: return;
177: } else {
178: return $CRYPTS[$crypt];
179: }
180: }
181:
1.46 andrew 182: sub labels
183: {
184: my $label = shift;
185:
186: if ((! defined $label) || (! length $label)) {
187: return;
188: } elsif (exists $LABELS{$label}) {
189: return $LABELS{$label};
190: } else {
191: foreach my $l (keys %LABELS) {
192: if ($LABELS{$l}{name} eq $label) {
193: return $LABELS{$l};
194: }
195: }
196:
197: # didn't find it, make one.
198: if ($label =~ /^\d+$/) {
199: return {
200: id => $label,
201: name => undef,
202: };
203: } else {
204: return;
205: }
206: }
207: }
208:
209: # Write
210:
211: sub Write
212: {
213: my $self = shift;
214:
215: if ($self->{version} == 4) {
216: # Give the PDB the first record that will hold the encrypted password
217: my $rec = $self->new_Record;
218: $rec->{data} = $self->{encpassword};
219:
220: if (ref $self->{records} eq 'ARRAY') {
221: unshift @{ $self->{records} }, $rec;
222: } else {
223: $self->{records} = [ $rec ];
224: }
225: }
226:
1.56 andrew 227: my @rc = $self->SUPER::Write(@_);
1.46 andrew 228:
229: if ($self->{version} == 4) {
230: shift @{ $self->{records} };
231: }
232:
1.56 andrew 233: return @rc;
1.46 andrew 234: }
235:
1.29 andrew 236: # ParseRecord
1.28 andrew 237:
238: sub ParseRecord
239: {
1.14 andrew 240: my $self = shift;
241:
1.16 andrew 242: my $rec = $self->SUPER::ParseRecord(@_);
1.56 andrew 243: return $rec if !(defined $rec->{data} && length $rec->{data} );
1.28 andrew 244:
245: if ($self->{version} == 4) {
246: # skip the first record because it contains the password.
1.46 andrew 247: if (! exists $self->{records}) {
248: $self->{encpassword} = $rec->{data};
249: return '__DELETE_ME__';
250: }
1.56 andrew 251:
1.46 andrew 252: if ($self->{records}->[0] eq '__DELETE_ME__') {
253: shift @{ $self->{records} };
254: }
1.28 andrew 255:
256: my ( $name, $encrypted ) = split /$NULL/xm, $rec->{data}, 2;
257:
258: return $rec if ! $encrypted;
1.48 andrew 259: $rec->{plaintext}->{0} = {
1.46 andrew 260: label => 'name',
261: label_id => 0,
262: data => $name,
263: font => 0,
264: };
1.28 andrew 265: $rec->{encrypted} = $encrypted;
266: delete $rec->{data};
267:
268: } elsif ($self->{version} == 5) {
1.39 andrew 269: my $c = crypts( $self->{appinfo}->{cipher} )
270: or croak('Unknown cipher ' . $self->{appinfo}->{cipher});
271: my $blocksize = $c->{blocksize};
1.28 andrew 272: my ($field, $extra) = _parse_field($rec->{data});
1.37 andrew 273: delete $rec->{data};
1.16 andrew 274:
1.48 andrew 275: $rec->{plaintext}->{0} = $field;
1.37 andrew 276: $rec->{ivec} = substr $extra, 0, $blocksize;
277: $rec->{encrypted} = substr $extra, $blocksize;
1.28 andrew 278:
279: } else {
1.56 andrew 280: # XXX Can never get here to test, ParseAppInfoBlock is always run
281: # XXX first by Load().
1.46 andrew 282: croak "Unsupported Version $self->{version}";
1.28 andrew 283: }
1.12 andrew 284:
1.16 andrew 285: return $rec;
1.14 andrew 286: }
1.11 andrew 287:
1.28 andrew 288: # PackRecord
289:
290: sub PackRecord
291: {
1.16 andrew 292: my $self = shift;
293: my $rec = shift;
294:
1.28 andrew 295: if ($self->{version} == 4) {
296: if ($rec->{encrypted}) {
1.48 andrew 297: my $name = $rec->{plaintext}->{0}->{data} || $EMPTY;
1.46 andrew 298: $rec->{data} = join $NULL, $name, $rec->{encrypted};
1.28 andrew 299: }
1.29 andrew 300:
1.28 andrew 301: } elsif ($self->{version} == 5) {
1.58 andrew 302: croak 'No encrypted data in record' if !defined $rec->{encrypted};
303: croak 'No ivec!' if !$rec->{ivec};
1.56 andrew 304:
1.37 andrew 305: my $field;
1.48 andrew 306: if ($rec->{plaintext}->{0}) {
307: $field = $rec->{plaintext}->{0};
1.37 andrew 308: } else {
309: $field = {
1.46 andrew 310: 'label' => 'name',
311: 'label_id' => 0,
1.37 andrew 312: 'data' => $EMPTY,
313: 'font' => 0,
314: };
315: }
316: my $packed = _pack_field($field);
1.29 andrew 317:
1.46 andrew 318: $rec->{data} = join $EMPTY, $packed, $rec->{ivec}, $rec->{encrypted};
1.29 andrew 319:
1.28 andrew 320: } else {
1.46 andrew 321: croak "Unsupported Version $self->{version}";
1.16 andrew 322: }
1.56 andrew 323: # XXX Should I?
324: delete $rec->{plaintext};
325: delete $rec->{encrypted};
1.58 andrew 326:
327: croak 'No data in record to pack' if !$rec->{data};
1.1 andrew 328:
1.16 andrew 329: return $self->SUPER::PackRecord($rec, @_);
1.14 andrew 330: }
1.1 andrew 331:
1.28 andrew 332: # ParseAppInfoBlock
333:
334: sub ParseAppInfoBlock
335: {
336: my $self = shift;
337: my $data = shift;
338: my $appinfo = {};
339:
340: &Palm::StdAppInfo::parse_StdAppInfo($appinfo, $data);
341:
342: # int8/uint8
343: # - Signed or Unsigned Byte (8 bits). C types: char, unsigned char
344: # int16/uint16
345: # - Signed or Unsigned Word (16 bits). C types: short, unsigned short
346: # int32/uint32
347: # - Signed or Unsigned Doubleword (32 bits). C types: int, unsigned int
348: # sz
349: # - Zero-terminated C-style string
350:
351: if ($self->{version} == 4) {
352: # Nothing extra for version 4
353:
354: } elsif ($self->{version} == 5) {
1.59 andrew 355: _parse_appinfo_v5($appinfo);
1.28 andrew 356:
357: } else {
1.46 andrew 358: croak "Unsupported Version $self->{version}";
1.28 andrew 359: }
360:
361: return $appinfo;
362: }
363:
364: sub _parse_appinfo_v5
365: {
366: my $appinfo = shift;
367:
1.59 andrew 368: croak 'Corrupt appinfo? no {other}' if ! $appinfo->{other};
1.28 andrew 369:
370: my $unpackstr
371: = ("C1" x 8) # 8 uint8s in an array for the salt
1.35 andrew 372: . ("n1" x 2) # the iter (uint16) and the cipher (uint16)
1.28 andrew 373: . ("C1" x 8); # and finally 8 more uint8s for the hash
374:
375: my (@salt, $iter, $cipher, @hash);
376: (@salt[0..7], $iter, $cipher, @hash[0..7])
377: = unpack $unpackstr, $appinfo->{other};
378:
379: $appinfo->{salt} = sprintf "%02x" x 8, @salt;
380: $appinfo->{iter} = $iter;
381: $appinfo->{cipher} = $cipher;
382: $appinfo->{masterhash} = sprintf "%02x" x 8, @hash;
383: delete $appinfo->{other};
384:
385: return $appinfo
386: }
387:
388: # PackAppInfoBlock
389:
390: sub PackAppInfoBlock
391: {
392: my $self = shift;
393: my $retval;
394:
395: if ($self->{version} == 4) {
396: # Nothing to do for v4
397:
398: } elsif ($self->{version} == 5) {
1.29 andrew 399: _pack_appinfo_v5($self->{appinfo});
1.28 andrew 400: } else {
1.46 andrew 401: croak "Unsupported Version $self->{version}";
1.28 andrew 402: }
403: return &Palm::StdAppInfo::pack_StdAppInfo($self->{appinfo});
404: }
405:
1.29 andrew 406: sub _pack_appinfo_v5
407: {
408: my $appinfo = shift;
409:
410: my $packstr
411: = ("C1" x 8) # 8 uint8s in an array for the salt
1.35 andrew 412: . ("n1" x 2) # the iter (uint16) and the cipher (uint16)
1.29 andrew 413: . ("C1" x 8); # and finally 8 more uint8s for the hash
414:
415: my @salt = map { hex $_ } $appinfo->{salt} =~ /../gxm;
416: my @hash = map { hex $_ } $appinfo->{masterhash} =~ /../gxm;
417:
418: my $packed = pack($packstr,
419: @salt,
420: $appinfo->{iter},
421: $appinfo->{cipher},
422: @hash
423: );
424:
425: $appinfo->{other} = $packed;
426:
427: return $appinfo
428: }
429:
1.28 andrew 430: # Encrypt
431:
432: sub Encrypt
433: {
1.14 andrew 434: my $self = shift;
1.16 andrew 435: my $rec = shift;
1.28 andrew 436: my $pass = shift || $self->{password};
1.59 andrew 437:
438: if ( !$rec ) {
439: croak('Needed parameter [record] not passed!');
440: }
441:
1.48 andrew 442: my $data = shift || $rec->{plaintext};
1.34 andrew 443: my $ivec = shift;
1.16 andrew 444:
1.59 andrew 445:
1.29 andrew 446: if ( ! $pass && ! $self->{appinfo}->{key}) {
1.59 andrew 447: croak('password not set!');
1.16 andrew 448: }
1.14 andrew 449:
1.16 andrew 450: if ( ! $data) {
1.59 andrew 451: croak('Needed parameter [plaintext] not passed!');
1.14 andrew 452: }
453:
1.29 andrew 454: if ( $pass && ! $self->Password($pass)) {
1.59 andrew 455: croak('Incorrect Password!');
1.16 andrew 456: }
1.14 andrew 457:
1.29 andrew 458: my $acct;
459: if ($rec->{encrypted}) {
460: $acct = $self->Decrypt($rec, $pass);
461: }
462:
463: my $encrypted;
1.28 andrew 464: if ($self->{version} == 4) {
465: $self->{digest} ||= _calc_keys( $pass );
1.46 andrew 466: my $datav4 = {
467: name => $data->{0}->{data},
468: account => $data->{1}->{data},
469: password => $data->{2}->{data},
470: lastchange => $data->{3}->{data},
471: notes => $data->{255}->{data},
472: };
473: my $acctv4 = {
474: name => $acct->{0}->{data},
475: account => $acct->{1}->{data},
476: password => $acct->{2}->{data},
477: lastchange => $acct->{3}->{data},
478: notes => $acct->{255}->{data},
479: };
480: $encrypted = _encrypt_v4($datav4, $acctv4, $self->{digest});
1.29 andrew 481:
482: } elsif ($self->{version} == 5) {
1.61 andrew 483: ($encrypted, $rec->{ivec}) = _encrypt_v5(
1.46 andrew 484: $data, $acct,
1.29 andrew 485: $self->{appinfo}->{key},
486: $self->{appinfo}->{cipher},
1.34 andrew 487: $ivec,
1.29 andrew 488: );
489:
490: } else {
1.60 andrew 491: croak "Unsupported version $self->{version}";
1.29 andrew 492: }
493:
1.48 andrew 494: $rec->{plaintext}->{0} = $data->{0};
1.46 andrew 495:
1.62 ! andrew 496: if ($encrypted ne '1') {
1.29 andrew 497: $rec->{attributes}{Dirty} = 1;
498: $rec->{attributes}{dirty} = 1;
499: $rec->{encrypted} = $encrypted;
1.62 ! andrew 500: }
1.29 andrew 501:
1.62 ! andrew 502: return 1;
1.28 andrew 503: }
1.14 andrew 504:
1.28 andrew 505: sub _encrypt_v4
506: {
1.29 andrew 507: my $new = shift;
508: my $old = shift;
1.28 andrew 509: my $digest = shift;
510:
1.29 andrew 511: $new->{account} ||= $EMPTY;
512: $new->{password} ||= $EMPTY;
513: $new->{notes} ||= $EMPTY;
1.1 andrew 514:
1.22 andrew 515: my $changed = 0;
516: my $need_newdate = 0;
1.29 andrew 517: if ($old && %{ $old }) {
1.46 andrew 518: no warnings 'uninitialized';
1.29 andrew 519: foreach my $key (keys %{ $new }) {
1.22 andrew 520: next if $key eq 'lastchange';
1.29 andrew 521: if ($new->{$key} ne $old->{$key}) {
1.22 andrew 522: $changed = 1;
523: last;
524: }
525: }
1.29 andrew 526: if ( exists $new->{lastchange} && exists $old->{lastchange} && (
527: $new->{lastchange}->{day} != $old->{lastchange}->{day} ||
528: $new->{lastchange}->{month} != $old->{lastchange}->{month} ||
529: $new->{lastchange}->{year} != $old->{lastchange}->{year}
1.22 andrew 530: )) {
531: $changed = 1;
532: $need_newdate = 0;
533: } else {
534: $need_newdate = 1;
535: }
536:
537: } else {
538: $changed = 1;
539: }
540:
541: # no need to re-encrypt if it has not changed.
542: return 1 if ! $changed;
543:
1.21 andrew 544: my ($day, $month, $year);
545:
1.29 andrew 546: if ($new->{lastchange} && ! $need_newdate ) {
547: $day = $new->{lastchange}->{day} || 1;
548: $month = $new->{lastchange}->{month} || 0;
549: $year = $new->{lastchange}->{year} || 0;
1.22 andrew 550:
551: # XXX Need to actually validate the above information somehow
552: if ($year >= 1900) {
553: $year -= 1900;
554: }
555: } else {
556: $need_newdate = 1;
557: }
558:
559: if ($need_newdate) {
1.21 andrew 560: ($day, $month, $year) = (localtime)[3,4,5];
561: }
1.22 andrew 562:
1.29 andrew 563: my $packed_date = _pack_keyring_date( {
1.28 andrew 564: year => $year,
565: month => $month,
566: day => $day,
567: });
1.19 andrew 568:
1.16 andrew 569: my $plaintext = join $NULL,
1.29 andrew 570: $new->{account}, $new->{password}, $new->{notes}, $packed_date;
1.1 andrew 571:
1.28 andrew 572: return _crypt3des( $plaintext, $digest, $ENCRYPT );
573: }
1.11 andrew 574:
1.29 andrew 575: sub _encrypt_v5
576: {
577: my $new = shift;
578: my $old = shift;
579: my $key = shift;
580: my $cipher = shift;
1.34 andrew 581: my $ivec = shift;
1.39 andrew 582: my $c = crypts($cipher) or croak('Unknown cipher ' . $cipher);
1.29 andrew 583:
1.34 andrew 584: if (! defined $ivec) {
1.59 andrew 585: while (! $ivec) {
586: $ivec = pack("C*",map {rand(256)} 1..$c->{blocksize});
587: }
1.34 andrew 588: }
589:
1.29 andrew 590: my $changed = 0;
591: my $need_newdate = 1;
1.46 andrew 592: if ($new->{3}->{data}) {
593: $need_newdate = 0;
594: }
595: foreach my $k (keys %{ $new }) {
596: if (! $old) {
597: $changed = 1;
598: } elsif ($k == 3) {
599: if ($old && (
600: $new->{$k}{data}{day} == $old->{$k}{data}{day} &&
601: $new->{$k}{data}{month} == $old->{$k}{data}{month} &&
602: $new->{$k}{data}{year} == $old->{$k}{data}{year}
1.29 andrew 603: )) {
604: $changed = 1;
1.46 andrew 605: $need_newdate = 1;
1.29 andrew 606: }
607:
1.46 andrew 608: } else {
609: my $n = join ':', sort %{ $new->{$k} };
610: my $o = join ':', sort %{ $old->{$k} };
1.29 andrew 611: if ($n ne $o) {
612: $changed = 1;
613: }
614: }
615: }
616:
1.60 andrew 617: return (1, $ivec) if $changed == 0;
1.29 andrew 618:
1.46 andrew 619: if ($need_newdate) {
1.29 andrew 620: my ($day, $month, $year) = (localtime)[3,4,5];
1.46 andrew 621: $new->{3} = {
622: label => 'lastchange',
623: label_id => 3,
624: font => 0,
625: data => {
626: year => $year,
627: month => $month,
628: day => $day,
1.47 andrew 629: },
1.29 andrew 630: };
631: } else {
632: # XXX Need to actually validate the above information somehow
1.46 andrew 633: if ($new->{3}->{data}->{year} >= 1900) {
634: $new->{3}->{data}->{year} -= 1900;
1.29 andrew 635: }
636: }
637:
1.48 andrew 638: my $plaintext;
1.46 andrew 639: foreach my $k (keys %{ $new }) {
1.52 andrew 640: next if $new->{$k}->{label_id} == 0;
1.48 andrew 641: $plaintext .= _pack_field($new->{$k});
1.29 andrew 642: }
1.54 andrew 643: $plaintext .= chr(0xff) x 2;
1.46 andrew 644:
1.29 andrew 645: my $encrypted;
1.39 andrew 646: if ($c->{name} eq 'None') {
1.29 andrew 647: # do nothing
1.48 andrew 648: $encrypted = $plaintext;
1.29 andrew 649:
1.39 andrew 650: } elsif ($c->{name} eq 'DES_EDE3' or $c->{name} eq 'Rijndael') {
1.35 andrew 651: require Crypt::CBC;
1.39 andrew 652: my $cbc = Crypt::CBC->new(
1.35 andrew 653: -key => $key,
1.29 andrew 654: -literal_key => 1,
655: -iv => $ivec,
1.39 andrew 656: -cipher => $c->{name},
657: -keysize => $c->{keylen},
658: -blocksize => $c->{blocksize},
1.29 andrew 659: -header => 'none',
660: -padding => 'oneandzeroes',
661: );
662:
663: if (! $c) {
664: croak("Unable to set up encryption!");
665: }
666:
1.48 andrew 667: $encrypted = $cbc->encrypt($plaintext);
1.29 andrew 668:
669: } else {
1.46 andrew 670: croak "Unsupported Crypt $c->{name}";
1.29 andrew 671: }
672:
673: return $encrypted, $ivec;
674: }
675:
1.28 andrew 676: # Decrypt
1.1 andrew 677:
1.31 andrew 678: sub Decrypt
1.28 andrew 679: {
1.14 andrew 680: my $self = shift;
1.16 andrew 681: my $rec = shift;
1.28 andrew 682: my $pass = shift || $self->{password};
1.16 andrew 683:
1.29 andrew 684: if ( ! $pass && ! $self->{appinfo}->{key}) {
1.28 andrew 685: croak("password not set!\n");
1.16 andrew 686: }
687:
688: if ( ! $rec) {
1.19 andrew 689: croak("Needed parameter 'record' not passed!\n");
1.16 andrew 690: }
1.14 andrew 691:
1.30 andrew 692: if ( $pass && ! $self->Password($pass)) {
1.16 andrew 693: croak("Invalid Password!\n");
1.14 andrew 694: }
695:
1.28 andrew 696: if ( ! $rec->{encrypted} ) {
1.16 andrew 697: croak("No encrypted content!");
698: }
1.14 andrew 699:
1.48 andrew 700: my $plaintext;
1.28 andrew 701: if ($self->{version} == 4) {
702: $self->{digest} ||= _calc_keys( $pass );
703: my $acct = _decrypt_v4($rec->{encrypted}, $self->{digest});
1.48 andrew 704: $plaintext = {
705: 0 => $rec->{plaintext}->{0},
1.46 andrew 706: 1 => {
707: label => 'account',
708: label_id => 1,
709: font => 0,
710: data => $acct->{account},
711: },
712: 2 => {
713: label => 'password',
714: label_id => 2,
715: font => 0,
716: data => $acct->{password},
717: },
718: 3 => {
719: label => 'lastchange',
720: label_id => 3,
721: font => 0,
722: data => $acct->{lastchange},
723: },
724: 255 => {
725: label => 'notes',
726: label_id => 255,
727: font => 0,
728: data => $acct->{notes},
729: },
730: };
1.29 andrew 731:
1.28 andrew 732: } elsif ($self->{version} == 5) {
1.48 andrew 733: $plaintext = _decrypt_v5(
1.29 andrew 734: $rec->{encrypted}, $self->{appinfo}->{key},
735: $self->{appinfo}->{cipher}, $rec->{ivec},
1.28 andrew 736: );
1.48 andrew 737: $plaintext->{0} ||= $rec->{plaintext}->{0};
1.29 andrew 738:
1.28 andrew 739: } else {
1.46 andrew 740: croak "Unsupported Version $self->{version}";
1.28 andrew 741: }
1.48 andrew 742:
743: if ($plaintext) {
744: $rec->{plaintext} = $plaintext;
745: return $plaintext;
746: }
1.28 andrew 747: return;
748: }
1.14 andrew 749:
1.28 andrew 750: sub _decrypt_v4
751: {
752: my $encrypted = shift;
753: my $digest = shift;
754:
1.48 andrew 755: my $plaintext = _crypt3des( $encrypted, $digest, $DECRYPT );
1.29 andrew 756: my ( $account, $password, $notes, $packed_date )
1.48 andrew 757: = split /$NULL/xm, $plaintext, 4;
1.14 andrew 758:
1.28 andrew 759: my $modified;
1.29 andrew 760: if ($packed_date) {
761: $modified = _parse_keyring_date($packed_date);
1.19 andrew 762: }
763:
1.16 andrew 764: return {
1.20 andrew 765: account => $account,
766: password => $password,
767: notes => $notes,
1.28 andrew 768: lastchange => $modified,
1.16 andrew 769: };
770: }
1.14 andrew 771:
1.28 andrew 772: sub _decrypt_v5
773: {
1.34 andrew 774:
1.28 andrew 775: my $encrypted = shift;
776: my $key = shift;
777: my $cipher = shift;
1.29 andrew 778: my $ivec = shift;
779:
1.39 andrew 780: my $c = crypts($cipher) or croak('Unknown cipher ' . $cipher);
1.28 andrew 781:
1.48 andrew 782: my $plaintext;
1.28 andrew 783:
1.39 andrew 784: if ($c->{name} eq 'None') {
1.28 andrew 785: # do nothing
1.48 andrew 786: $plaintext = $encrypted;
1.28 andrew 787:
1.39 andrew 788: } elsif ($c->{name} eq 'DES_EDE3' or $c->{name} eq 'Rijndael') {
1.35 andrew 789: require Crypt::CBC;
1.39 andrew 790: my $cbc = Crypt::CBC->new(
1.35 andrew 791: -key => $key,
1.29 andrew 792: -literal_key => 1,
793: -iv => $ivec,
1.39 andrew 794: -cipher => $c->{name},
795: -keysize => $c->{keylen},
796: -blocksize => $c->{blocksize},
1.29 andrew 797: -header => 'none',
798: -padding => 'oneandzeroes',
799: );
800:
1.28 andrew 801: if (! $c) {
802: croak("Unable to set up encryption!");
803: }
1.39 andrew 804: my $len = $c->{blocksize} - length($encrypted) % $c->{blocksize};
1.34 andrew 805: $encrypted .= $NULL x $len;
1.48 andrew 806: $plaintext = $cbc->decrypt($encrypted);
1.28 andrew 807:
808: } else {
1.46 andrew 809: croak "Unsupported Crypt $c->{name}";
1.28 andrew 810: }
811:
1.46 andrew 812: my %fields;
1.48 andrew 813: while ($plaintext) {
1.28 andrew 814: my $field;
1.48 andrew 815: ($field, $plaintext) = _parse_field($plaintext);
1.28 andrew 816: if (! $field) {
817: last;
818: }
1.46 andrew 819: $fields{ $field->{label_id} } = $field;
1.28 andrew 820: }
821:
1.46 andrew 822: return \%fields;
1.28 andrew 823: }
824:
825: # Password
826:
827: sub Password
828: {
1.16 andrew 829: my $self = shift;
1.24 andrew 830: my $pass = shift;
1.16 andrew 831: my $new_pass = shift;
1.14 andrew 832:
1.24 andrew 833: if (! $pass) {
834: delete $self->{password};
1.30 andrew 835: delete $self->{appinfo}->{key};
1.28 andrew 836: return 1;
1.24 andrew 837: }
838:
1.29 andrew 839: if (
1.46 andrew 840: ($self->{version} == 4 && ! exists $self->{encpassword}) ||
1.29 andrew 841: ($self->{version} == 5 && ! exists $self->{appinfo}->{masterhash})
842: ) {
1.16 andrew 843: return $self->_password_update($pass);
844: }
845:
846: if ($new_pass) {
847: my @accts = ();
1.46 andrew 848: foreach my $rec (@{ $self->{records} }) {
849: my $acct = $self->Decrypt($rec, $pass);
1.16 andrew 850: if ( ! $acct ) {
1.48 andrew 851: croak("Couldn't decrypt $rec->{plaintext}->{0}->{data}");
1.16 andrew 852: }
853: push @accts, $acct;
854: }
1.14 andrew 855:
1.16 andrew 856: if ( ! $self->_password_update($new_pass)) {
857: croak("Couldn't set new password!");
858: }
859: $pass = $new_pass;
1.1 andrew 860:
1.16 andrew 861: foreach my $i (0..$#accts) {
1.28 andrew 862: delete $self->{records}->[$i]->{encrypted};
1.48 andrew 863: $self->{records}->[$i]->{plaintext} = $accts[$i];
864: $self->Encrypt($self->{records}->[$i], $pass);
1.16 andrew 865: }
1.14 andrew 866: }
1.1 andrew 867:
1.28 andrew 868: if (defined $self->{password} && $pass eq $self->{password}) {
869: # already verified this password
870: return 1;
871: }
872:
873: if ($self->{version} == 4) {
1.46 andrew 874: my $valid = _password_verify_v4($pass, $self->{encpassword});
1.28 andrew 875:
1.46 andrew 876: # May as well generate the keys we need now,
877: # since we know the password is right
1.28 andrew 878: if ($valid) {
879: $self->{digest} = _calc_keys($pass);
880: if ($self->{digest} ) {
881: $self->{password} = $pass;
882: return 1;
883: }
884: }
885: } elsif ($self->{version} == 5) {
1.35 andrew 886: return _password_verify_v5($self->{appinfo}, $pass);
1.28 andrew 887: } else {
1.46 andrew 888: croak "Unsupported version $self->{version}";
1.28 andrew 889: }
890:
891: return;
892: }
893:
894: sub _password_verify_v4
895: {
1.32 andrew 896: require Digest::MD5;
897: import Digest::MD5 qw(md5);
898:
1.28 andrew 899: my $pass = shift;
900: my $data = shift;
901:
902: if (! $pass) { croak('No password specified!'); };
903:
904: # XXX die "No encrypted password in file!" unless defined $data;
905: if ( ! defined $data) { return; };
906:
907: $data =~ s/$NULL$//xm;
908:
909: my $salt = substr $data, 0, $kSalt_Size;
910:
911: my $msg = $salt . $pass;
912: $msg .= "\0" x ( $MD5_CBLOCK - length $msg );
913:
914: my $digest = md5($msg);
915:
1.33 andrew 916: if ($data ne $salt . $digest ) {
1.28 andrew 917: return;
918: }
919:
920: return 1;
921: }
922:
923: sub _password_verify_v5
924: {
1.35 andrew 925: my $appinfo = shift;
1.28 andrew 926: my $pass = shift;
927:
928: my $salt = pack("H*", $appinfo->{salt});
929:
1.39 andrew 930: my $c = crypts($appinfo->{cipher})
931: or croak('Unknown cipher ' . $appinfo->{cipher});
1.29 andrew 932: my ($key, $hash) = _calc_key_v5(
933: $pass, $salt, $appinfo->{iter},
1.39 andrew 934: $c->{keylen},
935: $c->{DES_odd_parity},
1.28 andrew 936: );
937:
1.35 andrew 938: #print "Iter: '" . $appinfo->{iter} . "'\n";
1.28 andrew 939: #print "Key: '". unpack("H*", $key) . "'\n";
1.35 andrew 940: #print "Salt: '". unpack("H*", $salt) . "'\n";
1.29 andrew 941: #print "Hash: '". $hash . "'\n";
1.28 andrew 942: #print "Hash: '". $appinfo->{masterhash} . "'\n";
943:
1.29 andrew 944: if ($appinfo->{masterhash} eq $hash) {
1.28 andrew 945: $appinfo->{key} = $key;
946: } else {
947: return;
948: }
1.29 andrew 949:
950: return $key;
951: }
952:
953:
954: sub _password_update
955: {
956: # It is very important to Encrypt after calling this
957: # (Although it is generally only called by Encrypt)
958: # because otherwise the data will be out of sync with the
959: # password, and that would suck!
960: my $self = shift;
961: my $pass = shift;
962:
963: if ($self->{version} == 4) {
964: my $data = _password_update_v4($pass, @_);
965:
966: if (! $data) {
967: carp("Failed to update password!");
968: return;
969: }
970:
971: # AFAIK the thing we use to test the password is
972: # always in the first entry
1.46 andrew 973: $self->{encpassword} = $data;
1.29 andrew 974: $self->{password} = $pass;
975: $self->{digest} = _calc_keys( $self->{password} );
976:
977: return 1;
978:
979: } elsif ($self->{version} == 5) {
980: my $cipher = shift || $self->{appinfo}->{cipher};
981: my $iter = shift || $self->{appinfo}->{iter};
982: my $salt = shift || 0;
983:
984: my $hash = _password_update_v5(
985: $self->{appinfo}, $pass, $cipher, $iter, $salt
986: );
987:
988: if (! $hash) {
989: carp("Failed to update password!");
990: return;
991: }
992:
993: return 1;
994: } else {
995: croak("Unsupported version ($self->{version})");
996: }
997:
998: return;
999: }
1000:
1001: sub _password_update_v4
1002: {
1.32 andrew 1003: require Digest::MD5;
1004: import Digest::MD5 qw(md5);
1005:
1.29 andrew 1006: my $pass = shift;
1007:
1008: if (! defined $pass) { croak('No password specified!'); };
1009:
1010: my $salt;
1011: for ( 1 .. $kSalt_Size ) {
1012: $salt .= chr int rand 255;
1013: }
1014:
1015: my $msg = $salt . $pass;
1016:
1017: $msg .= "\0" x ( $MD5_CBLOCK - length $msg );
1018:
1019: my $digest = md5($msg);
1020:
1021: my $data = $salt . $digest; # . "\0";
1022:
1023: return $data;
1024: }
1025:
1026: sub _password_update_v5
1027: {
1028: my $appinfo = shift;
1029: my $pass = shift;
1030: my $cipher = shift;
1031: my $iter = shift;
1032:
1033: # I thought this needed to be 'blocksize', but apparently not.
1034: #my $length = $CRYPTS[ $cipher ]{blocksize};
1035: my $length = 8;
1036: my $salt = shift || pack("C*",map {rand(256)} 1..$length);
1037:
1.39 andrew 1038: my $c = crypts($cipher) or croak('Unknown cipher ' . $cipher);
1.29 andrew 1039: my ($key, $hash) = _calc_key_v5(
1040: $pass, $salt, $iter,
1.39 andrew 1041: $c->{keylen},
1042: $c->{DES_odd_parity},
1.29 andrew 1043: );
1044:
1045: $appinfo->{salt} = unpack "H*", $salt;
1046: $appinfo->{iter} = $iter;
1047: $appinfo->{cipher} = $cipher;
1.39 andrew 1048: $appinfo->{masterhash} = $hash;
1.29 andrew 1049: $appinfo->{key} = $key;
1050:
1.28 andrew 1051: return $key;
1.1 andrew 1052: }
1053:
1.48 andrew 1054: sub Unlock
1055: {
1056: my $self = shift;
1057: my ($pass) = @_;
1058: $pass ||= $self->{password};
1059:
1060: if ( $pass && ! $self->Password($pass)) {
1061: croak("Invalid Password!\n");
1062: }
1063:
1064: foreach my $rec (@{ $self->{records} }) {
1065: $self->Decrypt($rec);
1066: }
1067:
1068: return 1;
1069:
1070: }
1071:
1072: sub Lock
1073: {
1074: my $self = shift;
1075:
1076: $self->Password();
1077:
1078: foreach my $rec (@{ $self->{records} }) {
1079: my $name = $rec->{plaintext}->{0};
1080: delete $rec->{plaintext};
1081: $rec->{plaintext}->{0} = $name;
1082: }
1083:
1084: return 1;
1085: }
1086:
1.34 andrew 1087: # Helpers
1.28 andrew 1088:
1089: sub _calc_keys
1090: {
1.14 andrew 1091: my $pass = shift;
1092: if (! defined $pass) { croak('No password defined!'); };
1093:
1094: my $digest = md5($pass);
1095:
1096: my ( $key1, $key2 ) = unpack 'a8a8', $digest;
1097:
1098: #--------------------------------------------------
1099: # print "key1: $key1: ", length $key1, "\n";
1100: # print "key2: $key2: ", length $key2, "\n";
1101: #--------------------------------------------------
1102:
1103: $digest = unpack 'H*', $key1 . $key2 . $key1;
1104:
1105: #--------------------------------------------------
1106: # print "Digest: ", $digest, "\n";
1107: # print length $digest, "\n";
1108: #--------------------------------------------------
1109:
1110: return $digest;
1.3 andrew 1111: }
1112:
1.29 andrew 1113: sub _calc_key_v5
1114: {
1115: my ($pass, $salt, $iter, $keylen, $dop) = @_;
1116:
1.32 andrew 1117: require Digest::HMAC_SHA1;
1118: import Digest::HMAC_SHA1 qw(hmac_sha1);
1119: require Digest::SHA1;
1120: import Digest::SHA1 qw(sha1);
1121:
1.29 andrew 1122: my $key = _pbkdf2( $pass, $salt, $iter, $keylen, \&hmac_sha1 );
1.43 andrew 1123: if ($dop) { $key = _DES_odd_parity($key); }
1.29 andrew 1124:
1125: my $hash = unpack("H*", substr(sha1($key.$salt),0, 8));
1126:
1127: return $key, $hash;
1128: }
1129:
1.28 andrew 1130: sub _crypt3des
1131: {
1.32 andrew 1132: require Crypt::DES;
1133:
1.28 andrew 1134: my ( $plaintext, $passphrase, $flag ) = @_;
1135:
1136: $passphrase .= $SPACE x ( 16 * 3 );
1137: my $cyphertext = $EMPTY;
1138:
1139: my $size = length $plaintext;
1.14 andrew 1140:
1.28 andrew 1141: #print "STRING: '$plaintext' - Length: " . (length $plaintext) . "\n";
1.11 andrew 1142:
1.28 andrew 1143: my @C;
1144: for ( 0 .. 2 ) {
1145: $C[$_] =
1146: new Crypt::DES( pack 'H*', ( substr $passphrase, 16 * $_, 16 ));
1.16 andrew 1147: }
1148:
1.28 andrew 1149: for ( 0 .. ( ($size) / 8 ) ) {
1150: my $pt = substr $plaintext, $_ * 8, 8;
1151:
1152: #print "PT: '$pt' - Length: " . length($pt) . "\n";
1153: if (! length $pt) { next; };
1154: if ( (length $pt) < 8 ) {
1155: if ($flag == $DECRYPT) { croak('record not 8 byte padded'); };
1156: my $len = 8 - (length $pt);
1157: $pt .= ($NULL x $len);
1158: }
1159: if ( $flag == $ENCRYPT ) {
1160: $pt = $C[0]->encrypt($pt);
1161: $pt = $C[1]->decrypt($pt);
1162: $pt = $C[2]->encrypt($pt);
1163: }
1164: else {
1165: $pt = $C[0]->decrypt($pt);
1166: $pt = $C[1]->encrypt($pt);
1167: $pt = $C[2]->decrypt($pt);
1168: }
1169:
1170: #print "PT: '$pt' - Length: " . length($pt) . "\n";
1171: $cyphertext .= $pt;
1172: }
1.11 andrew 1173:
1.28 andrew 1174: $cyphertext =~ s/$NULL+$//xm;
1.11 andrew 1175:
1.28 andrew 1176: #print "CT: '$cyphertext' - Length: " . length($cyphertext) . "\n";
1.11 andrew 1177:
1.28 andrew 1178: return $cyphertext;
1179: }
1.11 andrew 1180:
1.28 andrew 1181: sub _parse_field
1182: {
1183: my $field = shift;
1184:
1.46 andrew 1185: my ($len) = unpack "n", $field;
1.28 andrew 1186: if ($len + 4 > length $field) {
1.55 andrew 1187: return (undef, $field);
1.28 andrew 1188: }
1.34 andrew 1189: my $unpackstr = "x2 C1 C1 A$len";
1190: my $offset = 2 +1 +1 +$len;
1.46 andrew 1191: if ($len % 2) {
1.28 andrew 1192: # trim the 0/1 byte padding for next even address.
1.34 andrew 1193: $offset++;
1.28 andrew 1194: $unpackstr .= ' x'
1195: }
1.11 andrew 1196:
1.34 andrew 1197: my ($label, $font, $data) = unpack $unpackstr, $field;
1198: my $leftover = substr $field, $offset;
1.11 andrew 1199:
1.46 andrew 1200: my $label_id = $label;
1201: my $l = labels($label);
1202: if ($l) {
1203: $label = $l->{name} || $l->{id};
1204: $label_id = $l->{id};
1205: }
1206:
1207: if ($label_id && $label_id == 3) {
1208: ($data) = substr $field, 4, $len;
1.28 andrew 1209: $data = _parse_keyring_date($data);
1.14 andrew 1210: }
1.28 andrew 1211: return {
1212: #len => $len,
1.46 andrew 1213: label => $label,
1214: label_id => $label_id,
1.28 andrew 1215: font => $font,
1216: data => $data,
1217: }, $leftover;
1.6 andrew 1218: }
1219:
1.29 andrew 1220: sub _pack_field
1221: {
1222: my $field = shift;
1.28 andrew 1223:
1.37 andrew 1224: my $packed;
1225: if (defined $field) {
1226: my $label = $field->{label_id} || 0;
1227: if (defined $field->{label} && ! $label) {
1.46 andrew 1228: $label = $field->{label};
1229: }
1230:
1231: my $l = labels($field->{label});
1232: if ($l) {
1233: $label = $l->{id};
1.37 andrew 1234: }
1.46 andrew 1235:
1.37 andrew 1236: my $font = $field->{font} || 0;
1237: my $data = defined $field->{data} ? $field->{data} : $EMPTY;
1238:
1239: if ($label && $label == 3) {
1240: $data = _pack_keyring_date($data);
1241: }
1242: my $len = length $data;
1243: my $packstr = "n1 C1 C1 A*";
1244:
1245: $packed = pack $packstr, ($len, $label, $font, $data);
1246:
1247: if ($len % 2) {
1248: # add byte padding for next even address.
1249: $packed .= $NULL;
1250: }
1251: } else {
1.38 andrew 1252: my $packstr = "n1 C1 C1 x1";
1.37 andrew 1253: $packed = pack $packstr, 0, 0, 0;
1.14 andrew 1254: }
1255:
1.29 andrew 1256: return $packed;
1257: }
1.11 andrew 1258:
1.29 andrew 1259: sub _parse_keyring_date
1260: {
1261: my $data = shift;
1.11 andrew 1262:
1.29 andrew 1263: my $u = unpack 'n', $data;
1264: my $year = (($u & 0xFE00) >> 9) + 4; # since 1900
1265: my $month = (($u & 0x01E0) >> 5) - 1; # 0-11
1266: my $day = (($u & 0x001F) >> 0); # 1-31
1.11 andrew 1267:
1.29 andrew 1268: return {
1269: year => $year,
1270: month => $month || 0,
1271: day => $day || 1,
1272: };
1273: }
1.11 andrew 1274:
1.29 andrew 1275: sub _pack_keyring_date
1276: {
1277: my $d = shift;
1278: my $year = $d->{year};
1279: my $month = $d->{month};
1280: my $day = $d->{day};
1.11 andrew 1281:
1.29 andrew 1282: $year -= 4;
1283: $month++;
1.11 andrew 1284:
1.46 andrew 1285: return pack 'n*', $day | ($month << 5) | ($year << 9);
1.1 andrew 1286: }
1.29 andrew 1287:
1.1 andrew 1288:
1.28 andrew 1289: sub _hexdump
1290: {
1291: my $prefix = shift; # What to print in front of each line
1292: my $data = shift; # The data to dump
1293: my $maxlines = shift; # Max # of lines to dump
1294: my $offset; # Offset of current chunk
1295:
1.55 andrew 1296: my @lines;
1297:
1.28 andrew 1298: for ($offset = 0; $offset < length($data); $offset += 16)
1299: {
1300: my $hex; # Hex values of the data
1301: my $ascii; # ASCII values of the data
1302: my $chunk; # Current chunk of data
1303:
1304: last if defined($maxlines) && ($offset >= ($maxlines * 16));
1.14 andrew 1305:
1.28 andrew 1306: $chunk = substr($data, $offset, 16);
1.14 andrew 1307:
1.28 andrew 1308: ($hex = $chunk) =~ s/./sprintf "%02x ", ord($&)/ges;
1.11 andrew 1309:
1.28 andrew 1310: ($ascii = $chunk) =~ y/\040-\176/./c;
1.14 andrew 1311:
1.55 andrew 1312: push @lines, sprintf "%s %-48s|%-16s|\n", $prefix, $hex, $ascii;
1.14 andrew 1313: }
1.55 andrew 1314: return wantarray ? @lines : \@lines;
1.28 andrew 1315: }
1316:
1317: sub _bindump
1318: {
1319: my $prefix = shift; # What to print in front of each line
1320: my $data = shift; # The data to dump
1321: my $maxlines = shift; # Max # of lines to dump
1322: my $offset; # Offset of current chunk
1323:
1.55 andrew 1324: my @lines;
1325:
1.28 andrew 1326: for ($offset = 0; $offset < length($data); $offset += 8)
1327: {
1328: my $bin; # binary values of the data
1329: my $ascii; # ASCII values of the data
1330: my $chunk; # Current chunk of data
1.14 andrew 1331:
1.28 andrew 1332: last if defined($maxlines) && ($offset >= ($maxlines * 8));
1.14 andrew 1333:
1.28 andrew 1334: $chunk = substr($data, $offset, 8);
1.14 andrew 1335:
1.28 andrew 1336: ($bin = $chunk) =~ s/./sprintf "%08b ", ord($&)/ges;
1.14 andrew 1337:
1.28 andrew 1338: ($ascii = $chunk) =~ y/\040-\176/./c;
1.14 andrew 1339:
1.55 andrew 1340: push @lines, sprintf "%s %-72s|%-8s|\n", $prefix, $bin, $ascii;
1.14 andrew 1341: }
1.55 andrew 1342: return wantarray ? @lines : \@lines;
1.28 andrew 1343: }
1.14 andrew 1344:
1.28 andrew 1345: # Thanks to Jochen Hoenicke <hoenicke@gmail.com>
1346: # (one of the authors of Palm Keyring)
1347: # for these next two subs.
1348:
1349: # Usage pbkdf2(password, salt, iter, keylen, prf)
1350: # iter is number of iterations
1351: # keylen is length of generated key in bytes
1352: # prf is the pseudo random function (e.g. hmac_sha1)
1353: # returns the key.
1.55 andrew 1354: sub _pbkdf2
1.28 andrew 1355: {
1356: my ($password, $salt, $iter, $keylen, $prf) = @_;
1357: my ($k, $t, $u, $ui, $i);
1358: $t = "";
1359: for ($k = 1; length($t) < $keylen; $k++) {
1360: $u = $ui = &$prf($salt.pack('N', $k), $password);
1361: for ($i = 1; $i < $iter; $i++) {
1362: $ui = &$prf($ui, $password);
1363: $u ^= $ui;
1364: }
1365: $t .= $u;
1366: }
1367: return substr($t, 0, $keylen);
1368: }
1.11 andrew 1369:
1.55 andrew 1370: sub _DES_odd_parity {
1.28 andrew 1371: my $key = $_[0];
1372: my ($r, $i);
1373: my @odd_parity = (
1374: 1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14,
1375: 16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
1376: 32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
1377: 49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
1378: 64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
1379: 81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
1380: 97, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,
1381: 112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
1382: 128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
1383: 145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
1384: 161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
1385: 176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
1386: 193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
1387: 208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
1388: 224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
1389: 241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254);
1390: for ($i = 0; $i< length($key); $i++) {
1391: $r .= chr($odd_parity[ord(substr($key, $i, 1))]);
1392: }
1393: return $r;
1.14 andrew 1394: }
1.11 andrew 1395:
1.14 andrew 1396: 1;
1397: __END__
1398: =head1 NAME
1.11 andrew 1399:
1.14 andrew 1400: Palm::Keyring - Handler for Palm Keyring databases.
1.1 andrew 1401:
1.14 andrew 1402: =head1 DESCRIPTION
1.7 andrew 1403:
1.14 andrew 1404: The Keyring PDB handler is a helper class for the Palm::PDB package. It
1405: parses Keyring for Palm OS databases. See
1406: L<http://gnukeyring.sourceforge.net/>.
1.1 andrew 1407:
1.49 andrew 1408: It has the standard Palm::PDB methods with 4 additional public methods.
1409: Unlock, Lock, Decrypt and Encrypt.
1.1 andrew 1410:
1.37 andrew 1411: It currently supports the v4 Keyring databases as well as
1.49 andrew 1412: the pre-release v5 databases.
1.1 andrew 1413:
1.14 andrew 1414: =head1 SYNOPSIS
1.1 andrew 1415:
1.16 andrew 1416: use Palm::PDB;
1417: use Palm::Keyring;
1.17 andrew 1418:
1419: my $pass = 'password';
1.18 andrew 1420: my $file = 'Keys-Gtkr.pdb';
1421: my $pdb = new Palm::PDB;
1.16 andrew 1422: $pdb->Load($file);
1.17 andrew 1423:
1.49 andrew 1424: $pdb->Unlock($pass);
1.46 andrew 1425: foreach my $rec (@{ $pdb->{records} }) {
1.49 andrew 1426: print $rec->{plaintext}->{0}->{data}, ' - ',
1427: $rec->{plaintext}->{1}->{data}, "\n";
1.16 andrew 1428: }
1.49 andrew 1429: $pdb->Lock();
1.1 andrew 1430:
1.14 andrew 1431: =head1 SUBROUTINES/METHODS
1.1 andrew 1432:
1.14 andrew 1433: =head2 new
1.11 andrew 1434:
1.56 andrew 1435: $pdb = new Palm::Keyring([$password[, $version[, $cipher]]]);
1.11 andrew 1436:
1.14 andrew 1437: Create a new PDB, initialized with the various Palm::Keyring fields
1438: and an empty record list.
1.11 andrew 1439:
1.14 andrew 1440: Use this method if you're creating a Keyring PDB from scratch otherwise you
1.16 andrew 1441: can just use Palm::PDB::new() before calling Load().
1.11 andrew 1442:
1.49 andrew 1443: If you pass in a password, it will initalize the database with the encrypted
1.24 andrew 1444: password.
1445:
1.31 andrew 1446: new() now also takes options in other formats
1447:
1448: $pdb = new Palm::Keyring({ key1 => value1, key2 => value2 });
1449: $pdb = new Palm::Keyring( -key1 => value1, -key2 => value2);
1450:
1.38 andrew 1451: =over
1452:
1453: =item Supported options
1.31 andrew 1454:
1455: =over
1456:
1457: =item password
1458:
1459: The password used to initialize the database
1460:
1461: =item version
1462:
1463: The version of database to create. Accepts either 4 or 5. Currently defaults to 4.
1464:
1465: =item cipher
1466:
1.49 andrew 1467: The cipher to use. Either the number or the name. Only used by v5 datbases.
1.31 andrew 1468:
1469: 0 => None
1470: 1 => DES_EDE3
1471: 2 => AES128
1472: 3 => AES256
1473:
1474: =item iterations
1475:
1.49 andrew 1476: The number of iterations to encrypt with. Only used by somy crypts in v5 databases.
1.56 andrew 1477:
1478: =item file
1479:
1480: The name of a file to Load(). This will override many of the other options.
1.37 andrew 1481:
1.31 andrew 1482: =back
1483:
1.38 andrew 1484: =back
1485:
1.36 andrew 1486: For v5 databases there are some additional appinfo fields set.
1.40 andrew 1487: These are set either on new() or Load().
1.36 andrew 1488:
1.37 andrew 1489: $pdb->{appinfo} = {
1490: # normal appinfo stuff described in L<Palm::StdAppInfo>
1491: cipher => The index number of the cipher being used
1492: iter => Number of iterations for the cipher
1493: };
1.36 andrew 1494:
1.43 andrew 1495: =head2 crypts
1.34 andrew 1496:
1497: Pass in the alias of the crypt to use, or the index.
1498:
1.38 andrew 1499: These only make sense for v5 databases.
1500:
1.34 andrew 1501: This is a function, not a method.
1.40 andrew 1502:
1.38 andrew 1503: $cipher can be 0, 1, 2, 3, None, DES_EDE3, AES128 or AES256.
1.34 andrew 1504:
1505: my $c = Palm::Keyring::crypt($cipher);
1506:
1507: $c is now:
1508:
1509: $c = {
1510: alias => (None|DES_EDE3|AES128|AES256),
1511: name => (None|DES_EDE3|Rijndael),
1.44 andrew 1512: keylen => <key length of the cipher>,
1.34 andrew 1513: blocksize => <block size of the cipher>,
1514: default_iter => <default iterations for the cipher>,
1515: };
1516:
1.46 andrew 1517: If it is unable to find the crypt it will return undef.
1518:
1519: =head2 labels
1520:
1.49 andrew 1521: Pass in the id or the name of the label. The label id is used as a key
1522: to the different parts of the records.
1523: See Encrypt() for details on where the label is used.
1.46 andrew 1524:
1525: This is a function, not a method.
1526:
1527: my $l = Palm::Keyring::labels($label);
1528:
1529: $l is now:
1530:
1531: $l = {
1532: id => 0,
1533: name => 'name',
1534: };
1535:
1536: If what you passed in was a number that doesn't have a name, it will return:
1537:
1538: $l => {
1539: id => $num_passed_in,
1540: name => undef,
1541: }
1542:
1543: If you pass in a name that it can't find, then it returns undef.
1544:
1.16 andrew 1545: =head2 Encrypt
1.11 andrew 1546:
1.49 andrew 1547: =head3 B<!!! IMPORTANT !!!> The order of the arguments to Encrypt has
1548: changed. $password and $plaintext used to be swapped. They changed
1549: because you can now set $rec->{plaintext} and not pass in $plaintext so
1550: $password is more important.
1551:
1.48 andrew 1552: $pdb->Encrypt($rec[, $password[, $plaintext[, $ivec]]]);
1.11 andrew 1553:
1.16 andrew 1554: Encrypts an account into a record, either with the password previously
1555: used, or with a password that is passed.
1.34 andrew 1556:
1557: $ivec is the initialization vector to use to encrypt the record. This is
1558: not used by v4 databases. Normally this is not passed and is generated
1559: randomly.
1.1 andrew 1560:
1.28 andrew 1561: $rec is a record from $pdb->{records} or a new_Record().
1.48 andrew 1562: $rec->{plaintext} is a hashref in the format below.
1.1 andrew 1563:
1.48 andrew 1564: $plaintext = {
1.46 andrew 1565: 0 => {
1566: label => 'name',
1567: label_id => 0,
1568: font => 0,
1569: data => $name,
1570: 1 => {
1571: label => 'account',
1572: label_id => 1,
1573: font => 0,
1574: data => $account,
1575: },
1576: 2 => {
1577: label => 'password',
1578: label_id => 2,
1579: font => 0,
1580: data => $password,
1.20 andrew 1581: },
1.46 andrew 1582: 3 => {
1583: label => 'lastchange',
1584: label_id => 3,
1585: font => 0,
1.49 andrew 1586: data => {
1587: year => $year, # usually the year - 1900
1588: mon => $mon, # range 0-11
1589: day => $day, # range 1-31
1590: },
1.31 andrew 1591: },
1.46 andrew 1592: 255 => {
1593: label => 'notes',
1594: label_id => 255,
1595: font => 0,
1596: data => $notes,
1.31 andrew 1597: },
1.46 andrew 1598: };
1.31 andrew 1599:
1.49 andrew 1600: The account name is stored in $rec->{plaintext}->{0}->{data} for both v4
1601: and v5 databases even when the record has not been Decrypt()ed.
1.31 andrew 1602:
1.48 andrew 1603: $rec->{plaintext}->{0} => {
1.47 andrew 1604: label => 'name',
1605: label_id => 0,
1606: font => 0,
1607: data => 'account name',
1.46 andrew 1608: };
1.31 andrew 1609:
1.22 andrew 1610: If you have changed anything other than the lastchange, or don't pass in a
1.24 andrew 1611: lastchange key, Encrypt() will generate a new lastchange date for you.
1.22 andrew 1612:
1613: If you pass in a lastchange field that is different than the one in the
1614: record, it will honor what you passed in.
1615:
1.48 andrew 1616: You can either set $rec->{plaintext} or pass in $plaintext. $plaintext
1617: is used over anything in $rec->{plaintext}.
1618:
1.22 andrew 1619:
1.16 andrew 1620: =head2 Decrypt
1.1 andrew 1621:
1.48 andrew 1622: my $plaintext = $pdb->Decrypt($rec[, $password]);
1.1 andrew 1623:
1.48 andrew 1624: Decrypts the record and returns a reference for the plaintext account as
1.49 andrew 1625: described under Encrypt().
1.48 andrew 1626: Also sets $rec->{plaintext} with the same information as $plaintext as
1.49 andrew 1627: described in Encrypt().
1.1 andrew 1628:
1.46 andrew 1629: foreach my $rec (@{ $pdb->{records} }) {
1.48 andrew 1630: my $plaintext = $pdb->Decrypt($rec);
1631: # do something with $plaintext
1.16 andrew 1632: }
1.1 andrew 1633:
1.31 andrew 1634:
1.16 andrew 1635: =head2 Password
1.1 andrew 1636:
1.16 andrew 1637: $pdb->Password([$password[, $new_password]]);
1.1 andrew 1638:
1.16 andrew 1639: Either sets the password to be used to crypt, or if you pass $new_password,
1640: changes the password on the database.
1.1 andrew 1641:
1.16 andrew 1642: If you have created a new $pdb, and you didn't set a password when you
1643: called new(), you only need to pass one password and it will set that as
1644: the password.
1.1 andrew 1645:
1.24 andrew 1646: If nothing is passed, it forgets the password that it was remembering.
1.36 andrew 1647:
1648: After a successful password verification the following fields are set
1649:
1650: For v4
1651:
1.37 andrew 1652: $pdb->{digest} = the calculated digest used from the key;
1653: $pdb->{password} = the password that was passed in;
1.46 andrew 1654: $pdb->{encpassword} = the password as stored in the pdb;
1.36 andrew 1655:
1656: For v5
1657:
1.37 andrew 1658: $pdb->{appinfo} = {
1659: # As described under new() with these additional fields
1660: cipher => The index number of the cipher being used
1661: iter => Number of iterations for the cipher
1662: key => The key that is calculated from the password
1663: and salt and is used to decrypt the records.
1664: masterhash => the hash of the key that is stored in the
1665: database. Either set when Loading the database
1666: or when setting a new password.
1667: salt => the salt that is either read out of the database
1668: or calculated when setting a new password.
1669: };
1.1 andrew 1670:
1.48 andrew 1671: =head2 Unlock
1672:
1673: $pdb->Unlock([$password]);
1674:
1675: Decrypts all the records. Sets $rec->{plaintext} for all records.
1676:
1677: This makes it easy to show all decrypted information.
1678:
1679: my $pdb = Palm::KeyRing->new();
1680: $pdb->Load($keyring_file);
1681: $pdb->Unlock($password);
1682: foreach my $plaintext (map { $_->{plaintext} } @{ $pdb->{records} }) {
1683: # Do something like display the account.
1684: }
1685: $pdb->Lock();
1686:
1687: =head2 Lock
1688:
1689: $pdb->Lock();
1690:
1691: Unsets $rec->{plaintext} for all records and unsets the saved password.
1692:
1.49 andrew 1693: This does NOT Encrypt() any of the records before clearing them, so if
1.48 andrew 1694: you are not careful you will lose information.
1695:
1696: B<CAVEAT!> This only does "delete $rec->{plaintext}" and the same for the
1697: password. If someone knows of a cross platform reliable way to make
1698: sure that the information is actually cleared from memory I would
1699: appreciate it. Also, if someone knows how to make sure that the stuff
1700: in $rec->{plaintext} is not written to swap, that would be very handy as
1701: well.
1702:
1.43 andrew 1703: =head2 Other overridden subroutines/methods
1704:
1705: =over
1706:
1707: =item ParseAppInfoBlock
1708:
1709: Converts the extra returned by Palm::StdAppInfo::ParseAppInfoBlock() into
1710: the following additions to $pdb->{appinfo}
1711:
1712: $pdb->{appinfo} = {
1713: cipher => The index number of the cipher being used (Not v4)
1714: iter => Number of iterations for the cipher (Not v4)
1715: };
1716:
1717: =item PackAppInfoBlock
1718:
1719: Reverses ParseAppInfoBlock before
1720: sending it on to Palm::StdAppInfo::PackAppInfoBlock()
1721:
1722: =item ParseRecord
1723:
1724: Adds some fields to a record from Palm::StdAppInfo::ParseRecord()
1725:
1726: $rec = {
1727: name => Account name
1728: ivec => The IV for the encrypted record. (Not v4)
1729: encrypted => the encrypted information
1730: };
1.46 andrew 1731:
1732: For v4 databases it also removes record 0 and moves the encrypted password
1733: to $self->{encpassword}.
1.43 andrew 1734:
1735: =item PackRecord
1736:
1737: Reverses ParseRecord and then sends it through Palm::StdAppInfo::PackRecord()
1738:
1.47 andrew 1739: =item Write
1740:
1741: For v4 databases it puts back the record 0 for the encrypted password before
1742: writing it.
1743:
1.43 andrew 1744: =back
1745:
1.14 andrew 1746: =head1 DEPENDENCIES
1.1 andrew 1747:
1.14 andrew 1748: Palm::StdAppInfo
1.1 andrew 1749:
1.41 andrew 1750: B<For v4 databases>
1751:
1.14 andrew 1752: Digest::MD5
1.9 andrew 1753:
1.14 andrew 1754: Crypt::DES
1.4 andrew 1755:
1.41 andrew 1756: B<For v5 databases>
1757:
1758: Digest::HMAC_SHA1
1759:
1760: Digest::SHA1
1761:
1762: Depending on how the database is encrypted
1763:
1764: Crypt::CBC - For any encryption but None
1765:
1.43 andrew 1766: Crypt::DES_EDE3 - DES_EDE3 encryption
1.41 andrew 1767:
1.43 andrew 1768: Crytp::Rijndael - AES encryption schemes
1.10 andrew 1769:
1.24 andrew 1770: =head1 THANKS
1771:
1.47 andrew 1772: I would like to thank the helpful Perlmonk shigetsu who gave me some great
1773: advice and helped me get my first module posted.
1774: L<http://perlmonks.org/?node_id=596998>
1.24 andrew 1775:
1776: I would also like to thank
1777: Johan Vromans
1778: E<lt>jvromans@squirrel.nlE<gt> --
1779: L<http://www.squirrel.nl/people/jvromans>.
1780: He had his own Palm::KeyRing module that he posted a couple of days before
1781: mine was ready and he was kind enough to let me have the namespace as well
1782: as giving me some very helpful hints about doing a few things that I was
1783: unsure of. He is really great.
1.42 andrew 1784:
1785: And finally,
1786: thanks to Jochen Hoenicke E<lt>hoenicke@gmail.comE<gt>
1787: (one of the authors of Palm Keyring)
1788: for getting me started on the v5 support as well as providing help
1789: and some subroutines.
1.24 andrew 1790:
1.14 andrew 1791: =head1 BUGS AND LIMITATIONS
1.43 andrew 1792:
1793: I am sure there are problems with this module. For example, I have
1794: not done very extensive testing of the v5 databases.
1.45 andrew 1795:
1796: I am not sure I am 'require module' the best way, but I don't want to
1797: depend on modules that you don't need to use.
1.43 andrew 1798:
1799: The date validation for packing new dates is very poor.
1800:
1801: I have not gone through and standardized on how the module fails. Some
1802: things fail with croak, some return undef, some may even fail silently.
1.49 andrew 1803: Nothing initializes a lasterr method or anything like that.
1804:
1805: This module does not do anything special with the plaintext data. It SHOULD
1806: treat it somehow special so that it can't be found in RAM or in a swap file
1807: anywhere. I don't have a clue how to do this.
1808:
1809: I need to fix all this before it is a 1.0 candidate.
1.1 andrew 1810:
1.14 andrew 1811: Please report any bugs or feature requests to
1812: C<bug-palm-keyring at rt.cpan.org>, or through the web interface at
1813: L<http://rt.cpan.org>. I will be notified, and then you'll automatically be
1814: notified of progress on your bug as I make changes.
1.1 andrew 1815:
1816: =head1 AUTHOR
1817:
1.27 andrew 1818: Andrew Fresh E<lt>andrew@cpan.orgE<gt>
1.1 andrew 1819:
1.14 andrew 1820: =head1 LICENSE AND COPYRIGHT
1821:
1822: Copyright 2004, 2005, 2006, 2007 Andrew Fresh, All Rights Reserved.
1823:
1.15 andrew 1824: This program is free software; you can redistribute it and/or
1825: modify it under the same terms as Perl itself.
1.14 andrew 1826:
1.1 andrew 1827: =head1 SEE ALSO
1828:
1829: Palm::PDB(3)
1830:
1831: Palm::StdAppInfo(3)
1.11 andrew 1832:
1833: The Keyring for Palm OS website:
1834: L<http://gnukeyring.sourceforge.net/>
1.31 andrew 1835:
1836: The HACKING guide for palm keyring databases:
1837: L<http://gnukeyring.cvs.sourceforge.net/*checkout*/gnukeyring/keyring/HACKING>
1.24 andrew 1838:
1839: Johan Vromans also has a wxkeyring app that now uses this module, available
1.27 andrew 1840: from his website at L<http://www.vromans.org/johan/software/sw_palmkeyring.html>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>