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 / RSA1.pm < prev    next >
Encoding:
Perl POD Document  |  2001-06-27  |  3.1 KB  |  141 lines

  1. # $Id: RSA1.pm,v 1.13 2001/06/27 22:49:55 btrott Exp $
  2.  
  3. package Net::SSH::Perl::Key::RSA1;
  4. use strict;
  5.  
  6. use Net::SSH::Perl::Util qw( :ssh1mp :authfile );
  7.  
  8. use Net::SSH::Perl::Key;
  9. use base qw( Net::SSH::Perl::Key );
  10.  
  11. use Carp qw( croak );
  12. use Math::GMP;
  13. use Digest::MD5 qw( md5 );
  14.  
  15. sub init {
  16.     my $key = shift;
  17.     $key->{rsa} = {};
  18.  
  19.     my($blob) = @_;
  20.     return unless $blob;
  21.     my($bits, $e, $n) = split /\s+/, $blob, 3;
  22.     $key->{rsa}{bits} = $bits;
  23.     $key->{rsa}{e} = $e;
  24.     $key->{rsa}{n} = $n;
  25. }
  26.  
  27. sub size { $_[0]->{rsa}{bits} }
  28.  
  29. sub keygen {
  30.     my $class = shift;
  31.     my($bits) = @_;
  32.  
  33.     eval {
  34.         require Crypt::RSA;
  35.         require Crypt::RSA::DataFormat;
  36.         Crypt::RSA::DataFormat->import('bitsize');
  37.     };
  38.     if ($@) {
  39.         croak "rsa1 key generation is unavailable without Crypt::RSA";
  40.     }
  41.     my $gmp = sub { Math::GMP->new("$_[0]") };
  42.  
  43.     my $rsa = Crypt::RSA->new;
  44.     my $key = $class->new;
  45.     my($pub, $priv) = $rsa->keygen(
  46.                      Size      => $bits,
  47.                      Password  => 'ssh',
  48.                      Verbosity => 1,
  49.                      Identity  => 'Net::SSH::Perl',
  50.           );
  51.     die $rsa->errstr unless $pub && $priv;
  52.  
  53.     $key->{rsa}{e} = $gmp->($pub->e);
  54.     $key->{rsa}{n} = $gmp->($pub->n);
  55.     $key->{rsa}{bits} = $gmp->(bitsize($pub->n));
  56.     $key->{rsa}{d} = $gmp->($priv->d);
  57.     $key->{rsa}{u} = $gmp->($priv->u);
  58.     $key->{rsa}{p} = $gmp->($priv->p);
  59.     $key->{rsa}{q} = $gmp->($priv->q);
  60.  
  61.     $key;
  62. }
  63.  
  64. sub read_private {
  65.     my $class = shift;
  66.     my($keyfile, $passphrase) = @_;
  67.     my($key, $comment);
  68.     eval {
  69.         ($key, $comment) = _load_private_key($keyfile, $passphrase);
  70.     };
  71.     if (wantarray) {
  72.         return $key && !$@ ? ($key, $comment) : ();
  73.     }
  74.     else {
  75.         return $key && !$@ ? $key : undef;
  76.     }
  77. }
  78.  
  79. sub write_private {
  80.     my $key = shift;
  81.     my($keyfile, $passphrase, $comment) = @_;
  82.     _save_private_key($keyfile, $key, $passphrase, $comment);
  83. }
  84.  
  85. sub extract_public {
  86.     my $class = shift;
  87.     $class->new(@_);
  88. }
  89.  
  90. sub dump_public { $_[0]->as_blob }
  91.  
  92. sub equal {
  93.     my($keyA, $keyB) = @_;
  94.     $keyA->{rsa} && $keyB->{rsa} &&
  95.     $keyA->{rsa}{bits} == $keyB->{rsa}{bits} &&
  96.     $keyA->{rsa}{n} == $keyB->{rsa}{n} &&
  97.     $keyA->{rsa}{e} == $keyB->{rsa}{e};
  98. }
  99.  
  100. sub as_blob {
  101.     my $key = shift;
  102.     join ' ', $key->{rsa}{bits}, $key->{rsa}{e}, $key->{rsa}{n};
  103. }
  104.  
  105. sub fingerprint_raw {
  106.     my $key = shift;
  107.     _mp_linearize($key->{rsa}->{n}) . _mp_linearize($key->{rsa}->{e});
  108. }
  109.  
  110. 1;
  111. __END__
  112.  
  113. =head1 NAME
  114.  
  115. Net::SSH::Perl::Key::RSA1 - RSA SSH1 key object
  116.  
  117. =head1 SYNOPSIS
  118.  
  119.     use Net::SSH::Perl::Key::RSA1;
  120.     my $key = Net::SSH::Perl::Key::RSA1->new;
  121.  
  122. =head1 DESCRIPTION
  123.  
  124. I<Net::SSH::Perl::Key::RSA1> subclasses I<Net::SSH::Perl::Key>
  125. to implement a key object, SSH style. This object provides
  126. functionality needed by I<Net::SSH::Perl>, ie. for checking
  127. host key files, determining whether keys are equal, generating
  128. key fingerprints, etc.
  129.  
  130. =head1 USAGE
  131.  
  132. I<Net::SSH::Perl::Key::RSA1> implements the interface described in
  133. the documentation for I<Net::SSH::Perl::Key>.
  134.  
  135. =head1 AUTHOR & COPYRIGHTS
  136.  
  137. Please see the Net::SSH::Perl manpage for author, copyright,
  138. and license information.
  139.  
  140. =cut
  141.