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

1.1       andrew      1: # Palm::Keyring.pm
                      2: #
                      3: # Perl class for dealing with Keyring for Palm OS databases.
                      4: #
                      5: #      Copyright (C) 2004, Andrew Fresh
                      6: #      You may distribute this file under the terms of the Artistic
                      7: #      License, as specified in the README file distributed with the p5-Palm distribution.
                      8: #
                      9: #   This started as Memo.pm, I just made it work for Keyring.
                     10: #
1.6     ! andrew     11: # $Id: Keyring.pm,v 1.5 2006/11/10 04:52:27 andrew Exp $
        !            12: # $RedRiver: Keyring.pm,v 1.5 2006/11/10 04:52:27 andrew Exp $
1.1       andrew     13:
                     14: use strict;
                     15: package Palm::Keyring;
                     16: use Palm::Raw();
                     17: use Palm::StdAppInfo();
                     18: use vars qw( $VERSION @ISA );
                     19:
                     20: use Digest::MD5 qw(md5);
1.2       andrew     21: use Crypt::DES;
1.1       andrew     22:
                     23: use constant ENCRYPT    =>  1;
                     24: use constant DECRYPT    =>  0;
                     25: use constant MD5_CBLOCK => 64;
                     26: my $kSaltSize = 4;
                     27:
                     28:
                     29: # One liner, to allow MakeMaker to work.
1.6     ! andrew     30: $VERSION = do { my @r = (q$Revision: 1.5 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
1.1       andrew     31:
                     32: @ISA = qw( Palm::StdAppInfo Palm::Raw );
                     33:
                     34: =head1 NAME
                     35:
                     36: Palm::Keyring - Handler for Palm Keyring databases.
                     37:
                     38: =head1 SYNOPSIS
                     39:
                     40:     use Palm::Keyring;
                     41:        $pdb->Decrypt('mypassword');
                     42:
                     43: =head1 DESCRIPTION
                     44:
                     45: The Keyring PDB handler is a helper class for the Palm::PDB package. It
                     46: parses Keyring databases.  See
                     47: L<http://gnukeyring.sourceforge.net/>.
                     48:
                     49: It is just the standard Palm::Raw with 2 additional public methods.  Decrypt and Encrypt.
                     50:
                     51: =cut
                     52: =head2 new
                     53:
                     54:   $pdb = new Palm::Keyring ('password');
                     55:
                     56: Create a new PDB, initialized with the various Palm::Keyring fields
                     57: and an empty record list.
                     58:
                     59: Use this method if you're creating a Keyring PDB from scratch.
                     60:
                     61: =cut
                     62: #'
                     63: sub new
                     64: {
                     65:        my $classname   = shift;
                     66:        my $pass = shift;
                     67:
1.5       andrew     68:        # Create a generic PDB. No need to rebless it, though.
1.1       andrew     69:        my $self        = $classname->SUPER::new(@_);
                     70:
                     71:        $self->{name} = "Keys-Gtkr";    # Default
                     72:        $self->{creator} = "Gtkr";
                     73:        $self->{type} = "Gkyr";
1.5       andrew     74:        # The PDB is not a resource database by
                     75:        # default, but it's worth emphasizing,
                     76:        # since MemoDB is explicitly not a PRC.
1.1       andrew     77:        $self->{attributes}{resource} = 0;
                     78:
                     79:        # Initialize the AppInfo block
                     80:        $self->{appinfo} = {};
                     81:
                     82:        # Add the standard AppInfo block stuff
                     83:        &Palm::StdAppInfo::seed_StdAppInfo($self->{appinfo});
                     84:
                     85:        # Set the version
                     86:        $self->{version} = 4;
                     87:
                     88:        # Give the PDB the first record that will hold the encrypted password
1.5       andrew     89:        $self->{records} = [ {
                     90:                'category'   => 0,
                     91:                'attributes' => {
                     92:                        'private' => 1,
                     93:                        'Secret'  => 1,
                     94:                        'Dirty'   => 1,
                     95:                        'dirty'   => 1
1.1       andrew     96:                },
1.5       andrew     97:        }, ];
1.1       andrew     98:
                     99:        if ($pass) {
                    100:                $self->Encrypt($pass);
                    101:        }
                    102:
                    103:        return $self;
                    104: }
                    105:
                    106: sub import
                    107: {
                    108:        &Palm::PDB::RegisterPDBHandlers(__PACKAGE__,
                    109:                [ "Gtkr", "Gkyr" ],
                    110:                );
1.3       andrew    111: }
                    112:
                    113: sub Load
                    114: {
                    115:        my $self = shift;
                    116:        $self->SUPER::Load(@_);
                    117:
                    118:        # Skip the first 2 records because they are special
                    119:        # and don't have any plaintext
                    120:        my $skip = 0;
                    121:        foreach my $record (@{ $self->{records} }) {
                    122:                if ($skip < 2) {
                    123:                        $skip++;
                    124:                        next;
                    125:                }
                    126:                my ($name, $encrypted) = split /\000/, $record->{data}, 2;
                    127:                $record->{plaintext}->{name} = $name;
                    128:         $record->{encrypted} = $encrypted;
                    129:        }
                    130:        1;
1.6     ! andrew    131: }
        !           132:
        !           133: sub Write
        !           134: {
        !           135:        my $self = shift;
        !           136:        $self->Encrypt() || return undef;
        !           137:        return $self->SUPER::Load(@_);
1.1       andrew    138: }
                    139:
                    140: sub Encrypt
                    141: {
                    142:        my $self = shift;
                    143:        my $pass = shift;
                    144:
                    145:        if ($pass) {
                    146:                unless ($self->_keyring_verify($pass) ) {
                    147:                        # This would encrypt with a new password.
                    148:                        # First decrypting everything with the old password of course.
                    149:                        $self->_keyring_update($pass) || return undef;
                    150:                        $self->_keyring_verify($pass) || return undef;
                    151:                }
                    152:        }
                    153:
                    154:        my $seen_enc_pass = 0;
                    155:        foreach my $record (@{ $self->{records} }) {
                    156:                unless ($seen_enc_pass) {
                    157:                        $seen_enc_pass = 1;
                    158:                        next;
                    159:                }
                    160:
                    161:                next unless defined $record->{plaintext};
                    162:
                    163:                my $name        = defined $record->{plaintext}->{name}        ? $record->{plaintext}->{name}        : '';
                    164:                my $account     = defined $record->{plaintext}->{account}     ? $record->{plaintext}->{account}     : '';
                    165:                my $password    = defined $record->{plaintext}->{password}    ? $record->{plaintext}->{password}    : '';
                    166:                my $description = defined $record->{plaintext}->{description} ? $record->{plaintext}->{description} : '';
                    167:                my $extra       = '';
                    168:
1.2       andrew    169:                my $plaintext = join("\000", $account, $password, $description, $extra);
1.1       andrew    170:
1.2       andrew    171:                my $encrypted = $self->_crypt3des($plaintext, ENCRYPT);
1.1       andrew    172:
1.2       andrew    173:                $record->{data} = join("\000", $name, $encrypted);
1.1       andrew    174:        }
                    175:
                    176:        return 1;
                    177: }
                    178:
                    179: sub Decrypt
                    180: {
                    181:        my $self = shift;
                    182:        my $pass = shift;
                    183:
                    184:        if ($pass) {
                    185:                $self->_keyring_verify($pass) || return undef;
                    186:        }
                    187:
                    188:        my $seen_enc_pass = 0;
                    189:        foreach my $record (@{ $self->{records} }) {
                    190:                unless ($seen_enc_pass) {
                    191:                        # need to skip the first record because it is the encrypted password
                    192:                        $seen_enc_pass = 1;
                    193:                        next;
                    194:                }
                    195:
                    196:                next unless defined $record->{data};
                    197:
1.2       andrew    198:                my ($name, $encrypted) = split /\000/, $record->{data}, 2;
1.1       andrew    199:                $record->{plaintext}->{name} = $name;
                    200:
1.2       andrew    201:                my $decrypted = $self->_crypt3des($encrypted, DECRYPT);
1.1       andrew    202:                my ($account, $password, $description, $extra)
1.2       andrew    203:                      = split /\000/, $decrypted, 4;
1.1       andrew    204:
                    205:                $record->{plaintext}->{account}     = defined $account     ? $account     : '';
                    206:                $record->{plaintext}->{password}    = defined $password    ? $password    : '';
                    207:                $record->{plaintext}->{description} = defined $description ? $description : '';
                    208:
1.4       andrew    209:         #print "Name:      '$name'\n";
                    210:         #print "Encrypted: '$encrypted' - Length: " . length($encrypted) . "\n";
1.2       andrew    211:         #print "Hex:       '" . unpack("H*", $encrypted) . "'\n";
                    212:         #print "Binary:    '" . unpack("b*", $encrypted) . "'\n";
1.4       andrew    213:         #print "Decrypted: '$decrypted' - Length: " . length($decrypted) . "\n";
                    214:         #print "Hex:       '" . unpack("H*", $decrypted) . "'\n";
                    215:         #print "Binary:    '" . unpack("b*", $decrypted) . "'\n";
                    216:         #print "\n";
1.1       andrew    217:                #print "Extra: $extra\n";
                    218:                #--------------------------------------------------
                    219:                # print "Account:     $account\n";
                    220:                # print "Password:    $password\n";
                    221:                # print "Description: $description\n";
                    222:                #--------------------------------------------------
                    223:
                    224:        }
                    225:
                    226:        return 1;
                    227: }
                    228:
                    229: sub _calc_keys
                    230: {
                    231:        my $self = shift;
                    232:
                    233:        my $pass = $self->{'password'};
                    234:        die "No password defined!" unless defined $pass;
                    235:
                    236:        my $digest = md5($pass);
                    237:
                    238:        my ($key1, $key2) = unpack('a8a8', $digest);
                    239:        #--------------------------------------------------
                    240:        # print "key1: $key1: ", length $key1, "\n";
                    241:        # print "key2: $key2: ", length $key2, "\n";
                    242:        #--------------------------------------------------
                    243:
                    244:        $digest = unpack('H*', $key1 . $key2 . $key1);
                    245:        #--------------------------------------------------
                    246:        # print "Digest: ", $digest, "\n";
                    247:        # print length $digest, "\n";
                    248:        #--------------------------------------------------
                    249:
                    250:        $self->{digest} = $digest;
                    251:        return $digest;
                    252: }
                    253:
                    254: sub _keyring_verify
                    255: {
                    256:        my $self = shift;
                    257:        my $pass = shift;
                    258:
1.4       andrew    259:        die "No password specified!" unless $pass;
1.1       andrew    260:        $self->{password} = $pass;
                    261:
                    262:        # AFAIK the thing we use to test the password is
                    263:        #     always in the first entry
                    264:        my $data = $self->{records}->[0]->{data};
                    265:        #die "No encrypted password in file!" unless defined $data;
                    266:        return undef unless defined $data;
                    267:
                    268:        $data =~ s/\0$//;
                    269:
                    270:        my $salt = substr($data, 0, $kSaltSize);
                    271:
                    272:        my $msg = $salt . $pass;
                    273:
                    274:        $msg .= "\0" x (MD5_CBLOCK - length($msg));
                    275:
                    276:        my $digest = md5($msg);
                    277:
                    278:        if ($data eq $salt . $digest) {
                    279:                # May as well generate the keys we need now, since we know the password is right
                    280:                if ($self->_calc_keys()) {
                    281:                        return 1;
                    282:                } else {
                    283:                        return undef;
                    284:                }
                    285:        } else {
                    286:                return undef;
                    287:        }
                    288: }
                    289:
                    290: sub _keyring_update
                    291: {
                    292:        # It is very important to Encrypt after calling this
                    293:        #     (Although it is generally only called by Encrypt)
                    294:        # because otherwise the data will be out of sync with the
                    295:        # password, and that would suck!
                    296:        my $self = shift;
                    297:        my $pass = shift;
                    298:
1.4       andrew    299:        die "No password specified!" unless $pass;
1.1       andrew    300:
                    301:        # if the database already has a password in it
                    302:        if ($self->{records}->[0]->{data}) {
                    303:                # Make sure everything is decrypted before we update the keyring
                    304:                $self->Decrypt() || return undef;
                    305:        }
                    306:
                    307:        my $salt;
                    308:        for (1..$kSaltSize) {
                    309:                $salt .= chr(int(rand(255)));
                    310:        }
                    311:
                    312:        my $msg = $salt . $pass;
                    313:
                    314:        $msg .= "\0" x (MD5_CBLOCK - length($msg));
                    315:
                    316:        my $digest = md5($msg);
                    317:
                    318:        my $data = $salt . $digest;# . "\0";
                    319:
                    320:        # AFAIK the thing we use to test the password is
                    321:        #     always in the first entry
                    322:        $self->{records}->[0]->{data} = $data;
                    323:
                    324:        $self->{password} = $pass;
                    325:        $self->_calc_keys();
                    326:
                    327:        return 1;
                    328: }
                    329:
1.2       andrew    330:
                    331: # XXX Have to make this encrypt as well as decrypting, but w00 h00!
                    332: # do null padding on the end of a cleartext if we are going to encrypt it
                    333: sub _crypt3des {
1.4       andrew    334:        my ( $self, $plaintext, $flag ) = @_;
1.2       andrew    335:
                    336:        my $passphrase = $self->{digest} || $self->_calc_keys();
1.4       andrew    337:        $passphrase .= ' ' x (16*3);
                    338:        my $cyphertext = "";
1.2       andrew    339:
                    340:
1.4       andrew    341:        my $size = length ( $plaintext );
                    342:        #print "STRING: '$plaintext' - Length: " . length($plaintext) . "\n";
1.2       andrew    343:
1.4       andrew    344:        # This check should see if it is plaintext first, if it is,
                    345:        #   pad it with \000
                    346:        # if not, then die
                    347:        die "record not 8 byte padded" if (length($plaintext) % 8) && ! $flag;
1.2       andrew    348:
1.5       andrew    349:        my @C;
1.4       andrew    350:        for ( 0..2 ) {
1.5       andrew    351:                $C[$_] = new Crypt::DES( pack( "H*", substr($passphrase, 16*$_, 16 )));
1.4       andrew    352:        }
                    353:
                    354:        for ( 0 .. (($size)/8) - 1) {
                    355:                my $pt = substr( $plaintext, $_*8, 8 );
                    356:                #print "PT: '$pt' - Length: " . length($pt) . "\n";
                    357:                if (length($pt) < 8) {
                    358:                        my $len = 8 - length($pt);
                    359:                        print "LENGTH: $len\n";
                    360:                        print "Binary:    '" . unpack("b*", $pt) . "'\n";
                    361:                        $pt .= (chr(0) x $len);# . $pt;
                    362:                        print "Binary:    '" . unpack("b*", $pt) . "'\n";
                    363:                        #print "PT: '$pt' - Length: " . length($pt) . "\n";
                    364:                }
1.5       andrew    365:                $pt = $C[0]->decrypt( $pt );
                    366:                $pt = $C[1]->encrypt( $pt );
                    367:                $pt = $C[2]->decrypt( $pt );
1.4       andrew    368:                #print "PT: '$pt' - Length: " . length($pt) . "\n";
                    369:                $cyphertext .= $pt;
                    370:        }
                    371:
                    372:        return substr ( $cyphertext, 0, $size );
1.2       andrew    373: }
1.1       andrew    374:
                    375: 1;
                    376: __END__
                    377:
                    378: =head1 AUTHOR
                    379:
                    380: Andrew Fresh E<lt>andrew@mad-techies.org<gt>
                    381:
                    382: =head1 SEE ALSO
                    383:
                    384: Palm::PDB(3)
                    385:
                    386: Palm::StdAppInfo(3)
                    387:
                    388: =cut

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