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

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.5     ! andrew     11: # $Id: Keyring.pm,v 1.4 2006/11/10 04:21:17 andrew Exp $
        !            12: # $RedRiver: Keyring.pm,v 1.4 2006/11/10 04:21:17 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.5     ! andrew     30: $VERSION = do { my @r = (q$Revision: 1.4 $ =~ /\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.1       andrew    131: }
                    132:
                    133: sub Encrypt
                    134: {
                    135:        my $self = shift;
                    136:        my $pass = shift;
                    137:
                    138:        if ($pass) {
                    139:                unless ($self->_keyring_verify($pass) ) {
                    140:                        # This would encrypt with a new password.
                    141:                        # First decrypting everything with the old password of course.
                    142:                        $self->_keyring_update($pass) || return undef;
                    143:                        $self->_keyring_verify($pass) || return undef;
                    144:                }
                    145:        }
                    146:
                    147:        my $seen_enc_pass = 0;
                    148:        foreach my $record (@{ $self->{records} }) {
                    149:                unless ($seen_enc_pass) {
                    150:                        $seen_enc_pass = 1;
                    151:                        next;
                    152:                }
                    153:
                    154:                next unless defined $record->{plaintext};
                    155:
                    156:                my $name        = defined $record->{plaintext}->{name}        ? $record->{plaintext}->{name}        : '';
                    157:                my $account     = defined $record->{plaintext}->{account}     ? $record->{plaintext}->{account}     : '';
                    158:                my $password    = defined $record->{plaintext}->{password}    ? $record->{plaintext}->{password}    : '';
                    159:                my $description = defined $record->{plaintext}->{description} ? $record->{plaintext}->{description} : '';
                    160:                my $extra       = '';
                    161:
1.2       andrew    162:                my $plaintext = join("\000", $account, $password, $description, $extra);
1.1       andrew    163:
1.2       andrew    164:                my $encrypted = $self->_crypt3des($plaintext, ENCRYPT);
1.1       andrew    165:
1.2       andrew    166:                $record->{data} = join("\000", $name, $encrypted);
1.1       andrew    167:        }
                    168:
                    169:        return 1;
                    170: }
                    171:
                    172: sub Decrypt
                    173: {
                    174:        my $self = shift;
                    175:        my $pass = shift;
                    176:
                    177:        if ($pass) {
                    178:                $self->_keyring_verify($pass) || return undef;
                    179:        }
                    180:
                    181:        my $seen_enc_pass = 0;
                    182:        foreach my $record (@{ $self->{records} }) {
                    183:                unless ($seen_enc_pass) {
                    184:                        # need to skip the first record because it is the encrypted password
                    185:                        $seen_enc_pass = 1;
                    186:                        next;
                    187:                }
                    188:
                    189:                next unless defined $record->{data};
                    190:
1.2       andrew    191:                my ($name, $encrypted) = split /\000/, $record->{data}, 2;
1.1       andrew    192:                $record->{plaintext}->{name} = $name;
                    193:
1.2       andrew    194:                my $decrypted = $self->_crypt3des($encrypted, DECRYPT);
1.1       andrew    195:                my ($account, $password, $description, $extra)
1.2       andrew    196:                      = split /\000/, $decrypted, 4;
1.1       andrew    197:
                    198:                $record->{plaintext}->{account}     = defined $account     ? $account     : '';
                    199:                $record->{plaintext}->{password}    = defined $password    ? $password    : '';
                    200:                $record->{plaintext}->{description} = defined $description ? $description : '';
                    201:
1.4       andrew    202:         #print "Name:      '$name'\n";
                    203:         #print "Encrypted: '$encrypted' - Length: " . length($encrypted) . "\n";
1.2       andrew    204:         #print "Hex:       '" . unpack("H*", $encrypted) . "'\n";
                    205:         #print "Binary:    '" . unpack("b*", $encrypted) . "'\n";
1.4       andrew    206:         #print "Decrypted: '$decrypted' - Length: " . length($decrypted) . "\n";
                    207:         #print "Hex:       '" . unpack("H*", $decrypted) . "'\n";
                    208:         #print "Binary:    '" . unpack("b*", $decrypted) . "'\n";
                    209:         #print "\n";
1.1       andrew    210:                #print "Extra: $extra\n";
                    211:                #--------------------------------------------------
                    212:                # print "Account:     $account\n";
                    213:                # print "Password:    $password\n";
                    214:                # print "Description: $description\n";
                    215:                #--------------------------------------------------
                    216:
                    217:        }
                    218:
                    219:        return 1;
                    220: }
                    221:
                    222: sub _calc_keys
                    223: {
                    224:        my $self = shift;
                    225:
                    226:        my $pass = $self->{'password'};
                    227:        die "No password defined!" unless defined $pass;
                    228:
                    229:        my $digest = md5($pass);
                    230:
                    231:        my ($key1, $key2) = unpack('a8a8', $digest);
                    232:        #--------------------------------------------------
                    233:        # print "key1: $key1: ", length $key1, "\n";
                    234:        # print "key2: $key2: ", length $key2, "\n";
                    235:        #--------------------------------------------------
                    236:
                    237:        $digest = unpack('H*', $key1 . $key2 . $key1);
                    238:        #--------------------------------------------------
                    239:        # print "Digest: ", $digest, "\n";
                    240:        # print length $digest, "\n";
                    241:        #--------------------------------------------------
                    242:
                    243:        $self->{digest} = $digest;
                    244:        return $digest;
                    245: }
                    246:
                    247: sub _keyring_verify
                    248: {
                    249:        my $self = shift;
                    250:        my $pass = shift;
                    251:
1.4       andrew    252:        die "No password specified!" unless $pass;
1.1       andrew    253:        $self->{password} = $pass;
                    254:
                    255:        # AFAIK the thing we use to test the password is
                    256:        #     always in the first entry
                    257:        my $data = $self->{records}->[0]->{data};
                    258:        #die "No encrypted password in file!" unless defined $data;
                    259:        return undef unless defined $data;
                    260:
                    261:        $data =~ s/\0$//;
                    262:
                    263:        my $salt = substr($data, 0, $kSaltSize);
                    264:
                    265:        my $msg = $salt . $pass;
                    266:
                    267:        $msg .= "\0" x (MD5_CBLOCK - length($msg));
                    268:
                    269:        my $digest = md5($msg);
                    270:
                    271:        if ($data eq $salt . $digest) {
                    272:                # May as well generate the keys we need now, since we know the password is right
                    273:                if ($self->_calc_keys()) {
                    274:                        return 1;
                    275:                } else {
                    276:                        return undef;
                    277:                }
                    278:        } else {
                    279:                return undef;
                    280:        }
                    281: }
                    282:
                    283: sub _keyring_update
                    284: {
                    285:        # It is very important to Encrypt after calling this
                    286:        #     (Although it is generally only called by Encrypt)
                    287:        # because otherwise the data will be out of sync with the
                    288:        # password, and that would suck!
                    289:        my $self = shift;
                    290:        my $pass = shift;
                    291:
1.4       andrew    292:        die "No password specified!" unless $pass;
1.1       andrew    293:
                    294:        # if the database already has a password in it
                    295:        if ($self->{records}->[0]->{data}) {
                    296:                # Make sure everything is decrypted before we update the keyring
                    297:                $self->Decrypt() || return undef;
                    298:        }
                    299:
                    300:        my $salt;
                    301:        for (1..$kSaltSize) {
                    302:                $salt .= chr(int(rand(255)));
                    303:        }
                    304:
                    305:        my $msg = $salt . $pass;
                    306:
                    307:        $msg .= "\0" x (MD5_CBLOCK - length($msg));
                    308:
                    309:        my $digest = md5($msg);
                    310:
                    311:        my $data = $salt . $digest;# . "\0";
                    312:
                    313:        # AFAIK the thing we use to test the password is
                    314:        #     always in the first entry
                    315:        $self->{records}->[0]->{data} = $data;
                    316:
                    317:        $self->{password} = $pass;
                    318:        $self->_calc_keys();
                    319:
                    320:        return 1;
                    321: }
                    322:
1.2       andrew    323:
                    324: # XXX Have to make this encrypt as well as decrypting, but w00 h00!
                    325: # do null padding on the end of a cleartext if we are going to encrypt it
                    326: sub _crypt3des {
1.4       andrew    327:        my ( $self, $plaintext, $flag ) = @_;
1.2       andrew    328:
                    329:        my $passphrase = $self->{digest} || $self->_calc_keys();
1.4       andrew    330:        $passphrase .= ' ' x (16*3);
                    331:        my $cyphertext = "";
1.2       andrew    332:
                    333:
1.4       andrew    334:        my $size = length ( $plaintext );
                    335:        #print "STRING: '$plaintext' - Length: " . length($plaintext) . "\n";
1.2       andrew    336:
1.4       andrew    337:        # This check should see if it is plaintext first, if it is,
                    338:        #   pad it with \000
                    339:        # if not, then die
                    340:        die "record not 8 byte padded" if (length($plaintext) % 8) && ! $flag;
1.2       andrew    341:
1.5     ! andrew    342:        my @C;
1.4       andrew    343:        for ( 0..2 ) {
1.5     ! andrew    344:                $C[$_] = new Crypt::DES( pack( "H*", substr($passphrase, 16*$_, 16 )));
1.4       andrew    345:        }
                    346:
                    347:        for ( 0 .. (($size)/8) - 1) {
                    348:                my $pt = substr( $plaintext, $_*8, 8 );
                    349:                #print "PT: '$pt' - Length: " . length($pt) . "\n";
                    350:                if (length($pt) < 8) {
                    351:                        my $len = 8 - length($pt);
                    352:                        print "LENGTH: $len\n";
                    353:                        print "Binary:    '" . unpack("b*", $pt) . "'\n";
                    354:                        $pt .= (chr(0) x $len);# . $pt;
                    355:                        print "Binary:    '" . unpack("b*", $pt) . "'\n";
                    356:                        #print "PT: '$pt' - Length: " . length($pt) . "\n";
                    357:                }
1.5     ! andrew    358:                $pt = $C[0]->decrypt( $pt );
        !           359:                $pt = $C[1]->encrypt( $pt );
        !           360:                $pt = $C[2]->decrypt( $pt );
1.4       andrew    361:                #print "PT: '$pt' - Length: " . length($pt) . "\n";
                    362:                $cyphertext .= $pt;
                    363:        }
                    364:
                    365:        return substr ( $cyphertext, 0, $size );
1.2       andrew    366: }
1.1       andrew    367:
                    368: 1;
                    369: __END__
                    370:
                    371: =head1 AUTHOR
                    372:
                    373: Andrew Fresh E<lt>andrew@mad-techies.org<gt>
                    374:
                    375: =head1 SEE ALSO
                    376:
                    377: Palm::PDB(3)
                    378:
                    379: Palm::StdAppInfo(3)
                    380:
                    381: =cut

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