home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / PEM.pm < prev    next >
Encoding:
Perl POD Document  |  2001-04-24  |  2.3 KB  |  97 lines

  1. # $Id: PEM.pm,v 1.6 2001/04/22 08:03:04 btrott Exp $
  2.  
  3. package Crypt::DSA::Key::PEM;
  4. use strict;
  5.  
  6. use Convert::PEM;
  7.  
  8. use Crypt::DSA::Key;
  9. use base qw( Crypt::DSA::Key );
  10.  
  11. sub deserialize {
  12.     my $key = shift;
  13.     my %param = @_;
  14.  
  15.     my $pem = $key->_pem;
  16.     my $pkey = $pem->decode( Content  => $param{Content},
  17.                              Password => $param{Password} );
  18.     return unless $pkey;
  19.  
  20.     for my $m (qw( p q g pub_key priv_key )) {
  21.         $key->$m( $pkey->{DSAPrivateKey}{$m} );
  22.     }
  23.     $key;
  24. }
  25.  
  26. sub serialize {
  27.     my $key = shift;
  28.     my %param = @_;
  29.  
  30.     my $pkey = { DSAPrivateKey => { version => 0 } };
  31.     for my $m (qw( p q g pub_key priv_key )) {
  32.         $pkey->{DSAPrivateKey}{$m} = $key->$m();
  33.     }
  34.  
  35.     my $pem = $key->_pem;
  36.     my $buf = $pem->encode(
  37.             Content  => $pkey,
  38.             Password => $param{Password}
  39.         ) or croak $pem->errstr;
  40.     $buf;
  41. }
  42.  
  43. sub _pem {
  44.     my $key = shift;
  45.     unless (defined $key->{__pem}) {
  46.         my $pem = Convert::PEM->new(
  47.              Name => "DSA PRIVATE KEY",
  48.              ASN  => qq(
  49.                  DSAPrivateKey SEQUENCE {
  50.                      version INTEGER,
  51.                      p INTEGER,
  52.                      q INTEGER,
  53.                      g INTEGER,
  54.                      pub_key INTEGER,
  55.                      priv_key INTEGER
  56.                  }
  57.            ));
  58.         $pem->asn->configure( decode => { bigint => 'Math::Pari' },
  59.                               encode => { bigint => 'Math::Pari' } );
  60.         $key->{__pem} = $pem;
  61.     }
  62.     $key->{__pem};
  63. }
  64.  
  65. 1;
  66. __END__
  67.  
  68. =head1 NAME
  69.  
  70. Crypt::DSA::Key::PEM - Read/write DSA PEM files
  71.  
  72. =head1 SYNOPSIS
  73.  
  74.     use Crypt::DSA::Key;
  75.     my $key = Crypt::DSA::Key->new( Type => 'PEM', ...);
  76.     $key->write( Type => 'PEM', ...);
  77.  
  78. =head1 DESCRIPTION
  79.  
  80. I<Crypt::DSA::Key::PEM> provides an interface to reading and
  81. writing DSA PEM files, using I<Convert::PEM>. The files are
  82. ASN.1-encoded and optionally encrypted.
  83.  
  84. You shouldn't use this module directly. As the SYNOPSIS above
  85. suggests, this module should be considered a plugin for
  86. I<Crypt::DSA::Key>, and all access to PEM files (reading DSA
  87. keys from disk, etc.) should be done through that module.
  88.  
  89. Read the I<Crypt::DSA::Key> documentation for more details.
  90.  
  91. =head1 AUTHOR & COPYRIGHTS
  92.  
  93. Please see the Crypt::DSA manpage for author, copyright,
  94. and license information.
  95.  
  96. =cut
  97.