[BACK]Return to Keyring.pm CVS log [TXT][DIR] Up to [local] / palm / Palm-Keyring / lib / Palm

Annotation of palm/Palm-Keyring/lib/Palm/Keyring.pm, Revision 1.61

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>