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 / Cipher.pm < prev    next >
Encoding:
Perl POD Document  |  2001-04-24  |  5.6 KB  |  241 lines

  1. # $Id: Cipher.pm,v 1.10 2001/04/17 18:27:45 btrott Exp $
  2.  
  3. package Net::SSH::Perl::Cipher;
  4.  
  5. use strict;
  6. use Carp qw( croak );
  7.  
  8. use vars qw( %CIPHERS %CIPHERS_SSH2 %CIPH_REVERSE %SUPPORTED );
  9. BEGIN {
  10.     %CIPHERS = (
  11.         None => 0,
  12.         IDEA => 1,
  13.         DES  => 2,
  14.         DES3 => 3,
  15.         RC4 => 5,
  16.         Blowfish => 6,
  17.     );
  18.     %CIPHERS_SSH2 = (
  19.         '3des-cbc' => 'DES3',
  20.         'blowfish-cbc' => 'Blowfish',
  21.         'arcfour' => 'RC4',
  22.     );
  23.     %CIPH_REVERSE = reverse %CIPHERS;
  24. }
  25.  
  26. sub _determine_supported {
  27.     for my $ciph (keys %CIPHERS) {
  28.         my $pack = sprintf "%s::%s", __PACKAGE__, $ciph;
  29.         eval "use $pack";
  30.         $SUPPORTED{$CIPHERS{$ciph}}++ unless $@;
  31.     }
  32. }
  33.  
  34. sub new {
  35.     my $class = shift;
  36.     my $type = shift;
  37.     my($ciph);
  38.     unless ($type eq "None") {
  39.         $type = $CIPHERS_SSH2{$type} || $type;
  40.         my $ciph_class = join '::', __PACKAGE__, $type;
  41.         (my $lib = $ciph_class . ".pm") =~ s!::!/!g;
  42.         require $lib;
  43.         $ciph = $ciph_class->new(@_);
  44.     }
  45.     else {
  46.         $ciph = bless { }, __PACKAGE__;
  47.     }
  48.     $ciph;
  49. }
  50.  
  51. sub new_from_key_str {
  52.     my $class = shift;
  53.     eval "use Digest::MD5 qw( md5 );";
  54.     defined $_[1] ?
  55.         $class->new($_[0], md5($_[1])) :
  56.         $class->new(@_);
  57. }
  58.  
  59. sub enabled { $_[0]->{enabled} }
  60. sub enable { $_[0]->{enabled} = 1 }
  61.  
  62. sub id {
  63.     my $this = shift;
  64.     my $type;
  65.     if (my $class = ref $this) {
  66.         my $pack = __PACKAGE__;
  67.         ($type = $class) =~ s/^${pack}:://;
  68.     }
  69.     else {
  70.         $type = $this;
  71.     }
  72.     $CIPHERS{$type};
  73. }
  74.  
  75. sub name {
  76.     my $this = shift;
  77.     my $name;
  78.     if (my $class = ref $this) {
  79.         my $pack = __PACKAGE__;
  80.         ($name = $class) =~ s/^${pack}:://;
  81.     }
  82.     else {
  83.         $name = $CIPH_REVERSE{$this};
  84.     }
  85.     $name;
  86. }
  87.  
  88. sub mask {
  89.     my $mask = 0;
  90.     $mask |= (1<<$_) for keys %SUPPORTED;
  91.     $mask;
  92. }
  93.  
  94. sub supported {
  95.     unless (keys %SUPPORTED) {
  96.         _determine_supported();
  97.     }
  98.     return [ keys %SUPPORTED ] unless @_;
  99.     my $id = ref $_[0] ? shift->id : shift;
  100.     return $id == 0 || exists $SUPPORTED{$id} unless @_;
  101.     my $ssupp = shift;
  102.     mask() & $ssupp & (1 << $id);
  103. }
  104.  
  105. sub encrypt { $_[1] }
  106.  
  107. sub decrypt { $_[1] }
  108.  
  109. 1;
  110. __END__
  111.  
  112. =head1 NAME
  113.  
  114. Net::SSH::Perl::Cipher - Base cipher class, plus utility methods
  115.  
  116. =head1 SYNOPSIS
  117.  
  118.     use Net::SSH::Perl::Cipher;
  119.  
  120.     # Get list of supported cipher IDs.
  121.     my $supported = Net::SSH::Perl::Cipher::supported();
  122.  
  123.     # Translate a cipher name into an ID.
  124.     my $id = Net::SSH::Perl::Cipher::id($name);
  125.  
  126.     # Translate a cipher ID into a name.
  127.     my $name = Net::SSH::Perl::Cipher::name($id);
  128.  
  129. =head1 DESCRIPTION
  130.  
  131. I<Net::SSH::Perl::Cipher> provides a base class for each of
  132. the encryption cipher classes. In addition, it defines
  133. a set of utility methods that can be called either as
  134. functions or object methods.
  135.  
  136. =head1 UTILITY METHODS
  137.  
  138. =head2 supported( [ $ciph_id [, $server_supports ] ])
  139.  
  140. Without arguments, returns a reference to an array of
  141. ciphers supported by I<Net::SSH::Perl>. These are ciphers
  142. that have working Net::SSH::Perl::Cipher:: implementations,
  143. essentially.
  144.  
  145. With one argument I<$ciph_id>, returns a true value if
  146. that cipher is supported by I<Net::SSH::Perl>, and false
  147. otherwise.
  148.  
  149. With two arguments, I<$ciph_id> and I<$server_supports>,
  150. returns true if the cipher represented by I<$ciph_id>
  151. is supported both by I<Net::SSH::Perl> and by the sshd
  152. server. The list of ciphers supported by the server
  153. should be in I<$server_supports>, a bit mask sent
  154. from the server during the session identification
  155. phase.
  156.  
  157. Can be called either as a non-exported function, i.e.
  158.  
  159.     my $i_support = Net::SSH::Perl::Cipher::supported();
  160.  
  161. or as an object method of a I<Net::SSH::Perl::Cipher>
  162. object, or an object of a subclass:
  163.  
  164.     if ($ciph->supported($server_supports)) {
  165.         print "Server supports cipher $ciph";
  166.     }
  167.  
  168. =head2 id( [ $cipher_name ] )
  169.  
  170. Translates a cipher name into a cipher ID.
  171.  
  172. If given I<$cipher_name> translates that name into
  173. the corresponding ID. If called as an object method,
  174. translates the object's cipher class name into the
  175. ID.
  176.  
  177. =head2 name( [ $cipher_id ] )
  178.  
  179. Translates a cipher ID into a cipher name.
  180.  
  181. If given I<$cipher_id> translates that ID into the
  182. corresponding name. If called as an object method,
  183. returns the (stripped) object's cipher class name;
  184. for example, if the object were of type
  185. I<Net::SSH::Perl::Cipher::IDEA>, I<name> would return
  186. I<IDEA>.
  187.  
  188. =head1 CIPHER USAGE
  189.  
  190. =head2 Net::SSH::Perl::Cipher->new($cipher_name, $key)
  191.  
  192. Instantiates a new cipher object of the type
  193. I<$cipher_name> with the key I<$key>; returns
  194. the cipher object, which will be blessed into the
  195. actual cipher subclass.
  196.  
  197. If I<$cipher_name> is the special type I<'None'>
  198. (no encryption cipher), the object will actually
  199. be blessed directly into the base class, and
  200. text to be encrypted and decrypted will be passed
  201. through without change.
  202.  
  203. =head2 $cipher->encrypt($text)
  204.  
  205. Encrypts I<$text> and returns the encrypted string.
  206.  
  207. =head2 $cipher->decrypt($text)
  208.  
  209. Decrypts I<$text> and returns the decrypted string.
  210.  
  211. =head1 CIPHER DEVELOPMENT
  212.  
  213. Classes implementing an encryption cipher must
  214. implement the following three methods:
  215.  
  216. =over 4
  217.  
  218. =item * $class->new($key)
  219.  
  220. Given a key I<$key>, should construct a new cipher
  221. object and bless it into I<$class>, presumably.
  222.  
  223. =item * $cipher->encrypt($text)
  224.  
  225. Given plain text I<$text>, should encrypt the text
  226. and return the encrypted string.
  227.  
  228. =item * $cipher->decrypt($text)
  229.  
  230. Given encrypted text I<$text>, should decrypt the
  231. text and return the decrypted string.
  232.  
  233. =back
  234.  
  235. =head1 AUTHOR & COPYRIGHTS
  236.  
  237. Please see the Net::SSH::Perl manpage for author, copyright,
  238. and license information.
  239.  
  240. =cut
  241.