[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.63

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

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