releasing/cbrtools/perl/Crypt.pm
changeset 607 378360dbbdba
parent 602 3145852acc89
equal deleted inserted replaced
591:22486c9c7b15 607:378360dbbdba
       
     1 # Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of the License "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 # 
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 # 
       
    11 # Contributors:
       
    12 # 
       
    13 # Description:
       
    14 # 
       
    15 #
       
    16 
       
    17 package Crypt;
       
    18 
       
    19 use strict;
       
    20 
       
    21 #
       
    22 # Constructor
       
    23 #
       
    24 
       
    25 sub New {
       
    26   my $invocant = shift;
       
    27   my $class = ref($invocant) || $invocant;
       
    28   my %args = @_;
       
    29   my $self = {
       
    30 	      defaultPath => $args{default_path},
       
    31 	      verbose => $args{verbose}
       
    32 	     };
       
    33   bless $self, $class;
       
    34   $self->Initialize();
       
    35   return $self;
       
    36 }
       
    37 
       
    38 sub Initialize {
       
    39   my $self = shift;
       
    40   
       
    41   #convert defaultPath attribute to correct format
       
    42   if ($self->{defaultPath}) {
       
    43     $self->DefaultPath($self->{defaultPath});
       
    44   }
       
    45 }
       
    46 
       
    47 #
       
    48 # Public getters/setters
       
    49 #
       
    50 
       
    51 sub DefaultPath {
       
    52   my $self = shift;
       
    53   
       
    54   if (defined $_[0]) {
       
    55     my $defaultPath = shift;
       
    56     $defaultPath =~ s/\\/\//g;  #replace '\'s with / 
       
    57     $defaultPath =~ s/\/+$//;   #remove trailing '/'s  
       
    58     $self->{defaultPath} = $defaultPath;
       
    59     delete $self->{publicKeys};   #new default path implies new keyring files so delete  
       
    60     delete $self->{secretKeys};   #the current key lists
       
    61   }
       
    62   return $self->{defaultPath};
       
    63 }
       
    64 
       
    65 #
       
    66 # Public methods
       
    67 #
       
    68 
       
    69 sub Encrypt {
       
    70   my $self = shift;
       
    71   my $plainText = shift;
       
    72   my $cipherText = shift;
       
    73   my @recipientKeys = @{$_[0]};
       
    74 
       
    75   unless (defined $plainText and defined $cipherText and @recipientKeys) {
       
    76     die "Error: Incorrect arguments for encryption.\n";
       
    77   }
       
    78   $plainText=~ s/\\/\//g;  #replace '\'s with /`s
       
    79   $cipherText=~ s/\\/\//g;
       
    80 
       
    81   if ($self->{verbose} > 1) {
       
    82     print "Encrypting $plainText with key(s) ".join(", ",@recipientKeys)."\n";
       
    83   }
       
    84 
       
    85   unless (-e $plainText) {
       
    86     die "Error: Encryption aborted. $plainText does not exist.\n";
       
    87   }
       
    88   #check to see if all the recipient keys exist on the public keyring
       
    89   foreach my $recipientKey (@recipientKeys) {
       
    90     $self->PublicKeyExists($recipientKey) 
       
    91       or die "Error: Encryption failed. $recipientKey not in keyring.\n";
       
    92   }
       
    93   
       
    94   #call subclass method to actually encrypt file
       
    95   $self->DoEncrypt($plainText, $cipherText, \@recipientKeys);
       
    96   
       
    97   #throw an error if encrypted file not created
       
    98   unless (-e $cipherText) {
       
    99     die "Error: Encryption of $plainText failed.\n";
       
   100   }
       
   101 }
       
   102 
       
   103 sub Decrypt {
       
   104   my $self = shift;
       
   105   my $cipherText = shift;
       
   106   my $plainText = shift;
       
   107   my $passPhrase = shift;
       
   108 
       
   109   unless (defined $plainText and defined $cipherText and defined $passPhrase) {
       
   110     die "Error: Incorrect arguments for decryption.\n";
       
   111   }
       
   112   $plainText=~ s/\\/\//g;  #replace '\'s with /`s
       
   113   $cipherText=~ s/\\/\//g;
       
   114 
       
   115   if ($self->{verbose} > 1) {
       
   116     print "Decrypting $cipherText\n";    
       
   117   }
       
   118 
       
   119   unless (-e $cipherText) {
       
   120     die "Error: Decryption aborted. $cipherText does not exist.\n";
       
   121   }
       
   122   #call subclass method to actually decrypt file
       
   123   $self->DoDecrypt($cipherText, $plainText, $passPhrase);
       
   124   
       
   125   #throw an error if decrypted file not created
       
   126   unless (-e $plainText) {
       
   127     die "Error: Decryption of $cipherText failed.\n";
       
   128   }	
       
   129 }
       
   130 
       
   131 sub PublicKeyList {
       
   132   my $self = shift;
       
   133 
       
   134   unless (exists $self->{publicKeys}) {
       
   135     #call subclass method to get key list
       
   136     foreach my $key (@{$self->GetPublicKeyList()}) {
       
   137       $self->{publicKeys}->{uc($key)} = 1;
       
   138     }	
       
   139   }
       
   140   my @keys = keys %{$self->{publicKeys}};
       
   141   return \@keys;
       
   142 }
       
   143 
       
   144 sub SecretKeyList {
       
   145   my $self = shift;
       
   146 
       
   147   unless (exists $self->{secretKeys}) { 
       
   148     #call subclass method to get key list 
       
   149     foreach my $key (@{$self->GetSecretKeyList()}) {
       
   150       $self->{secretKeys}->{uc($key)} = 1;
       
   151     }
       
   152   }
       
   153   my @keys = keys %{$self->{secretKeys}};
       
   154   return \@keys;
       
   155 }
       
   156 
       
   157 
       
   158 sub PublicKeyExists {
       
   159   my $self = shift;
       
   160   my $requiredKey = shift;
       
   161 
       
   162   unless (exists $self->{publicKeys}) {
       
   163     $self->PublicKeyList();
       
   164   }
       
   165   return ($self->{publicKeys}->{uc($requiredKey)});
       
   166 }
       
   167 
       
   168 sub SecretKeyExists {
       
   169   my $self = shift;
       
   170   my $requiredKey = $_[0];
       
   171 
       
   172   unless (exists $self->{secretKeys}) {
       
   173     $self->SecretKeyList();
       
   174   }
       
   175   return ($self->{secretKeys}->{uc($requiredKey)});
       
   176 }
       
   177 
       
   178 #
       
   179 # Abstract methods (must be implemented in a subclass)
       
   180 #
       
   181 
       
   182 sub DoEncrypt {
       
   183   die "Error: Call to abstract method ".ref($_[0])."::_DoEncrypt.\n";
       
   184 }
       
   185 
       
   186 sub DoDecrypt {
       
   187   die "Error: Call to abstract method ".ref($_[0])."::_DoDecrypt.\n";
       
   188 }
       
   189 
       
   190 sub GetPublicKeyList {
       
   191   die "Error: Call to abstract method ".ref($_[0])."::_GetPublicKeyList.\n";
       
   192 }
       
   193 
       
   194 sub GetSecretKeyList {
       
   195   die "Error: Call to abstract method ".ref($_[0])."::_GetSecretKeyList.\n";
       
   196 }
       
   197 
       
   198 #
       
   199 # Private methods
       
   200 #
       
   201 
       
   202 sub Quoted {
       
   203   my $self = shift;
       
   204   my $string = $_[0];
       
   205   return ($string =~ /^\s*(\".*\")\s*$/) ? $1 : "\"$string\"";
       
   206 }
       
   207 
       
   208 1;
       
   209 
       
   210 =head1 NAME
       
   211 
       
   212 Crypt.pm - Abstract base class to crypt modules.
       
   213 
       
   214 =head1 SYNOPSIS
       
   215 
       
   216  use Crypt::PGP;
       
   217 
       
   218  $crypt = Crypt::PGP->New(default_path => 'somePath/someDir',
       
   219                           verbose => 1);
       
   220 
       
   221  $crypt->DefaultPath('somedir/anotherdir');
       
   222  $defaultpath = $crypt->DefaultPath();
       
   223 
       
   224  @publickeys = @{$crypt->PublicKeyList()};
       
   225  @secretkeys = @{$crypt->SecretKeyList()};
       
   226 
       
   227  $crypt->Encrypt('somefile.txt', 'somefile.pgp', ['0x24534213', '0x1EA3B4DC', '0x8721DACE']);
       
   228  $crypt->Decrypt('somefile.pgp', 'somefile.txt', 'mypassphrase');
       
   229 
       
   230 
       
   231 =head1 DESCRIPTION
       
   232 
       
   233 C<Crypt> is the abstract base class to a family of modules of the form C<Crypt::>F<PGPTool> which are simple wrappers over PGP command line tools. Each module in the C<Crypt> directory must implement the following abstract interface...
       
   234 
       
   235 =over 4
       
   236 
       
   237 =item * DoEncrypt($plainText, $cipherText, \@recipientKeys)
       
   238 
       
   239 Should encrypt the C<$plainText> file with the public keys C<@recipientKeys> and store the result in the C<$cipherText> file.
       
   240 
       
   241 =item * DoDecrypt($cipherText, $plainText, $passPhrase)
       
   242 
       
   243 Should decrypt the C<$cipherText> file using the secret key with pass phrase C<$passPhrase> and store the result in the C<$plainText> file. Must die with C<"BAD_PASSPHRASE"> if passphrase incorrect and C<"NO_SECKEY"> if secret key not available for decrypting file.
       
   244 
       
   245 =item * array_ref GetPublicKeyList( )
       
   246 
       
   247 Should return the list of keyids stored on the public keyring.
       
   248 
       
   249 =item * array_ref GetSecretKeyList( )
       
   250 
       
   251 Should return the list of keyids stored on the secret keyring.
       
   252 
       
   253 =back
       
   254 
       
   255 B<NOTE:> A key id is an 8 digit hexadecimal number preceeded by a zero and an x (or X) e.g 0x12345678, 0X3eDC2A82
       
   256 
       
   257 
       
   258 =head1 INTERFACE
       
   259 
       
   260 =head2 New
       
   261 
       
   262 Passed an argument list in the form of hash key value pairs. The supported arguments are...
       
   263 
       
   264   default_path  => $path_string
       
   265   verbose       => $verbosity_integer
       
   266 
       
   267 Returns a reference to an object derived from C<Crypt> (C<Crypt> is abstract so cannot be instantiated)
       
   268 
       
   269 =head2 DefaultPath
       
   270 
       
   271 Returns the current value of the C<defaultPath> attribute which stores the path to the users configuration and keyring files. If the C<defaultPath> is undefined then the tools default path is used. If passed a path as an argument sets the C<defaultPath> attribute to this value and updates the public and secret keyring file names. 
       
   272 
       
   273 =head2 Encrypt
       
   274 
       
   275 Passed a plain text file name, a cipher text file name and a reference to an array of recipients pgp keyids. Encrypts the plain text file with the recipients keys. Outputs the result to the cipher text file.
       
   276 
       
   277 =head2 Decrypt
       
   278 
       
   279 Passed a cipher text file name, a plain text file name and the users private key pass phrase. Decrypts the cipher text file with the users private key and outputs the result to the plain text file.
       
   280 
       
   281 =head2 PublicKeyList
       
   282 
       
   283 Returns a reference to an array of keyids for keys stored in the public keyring
       
   284 
       
   285 =head2 SecretKeyList
       
   286 
       
   287 Returns a reference to an array of keyids for keys stored in the secret keyring
       
   288 
       
   289 =head2 PublicKeyExists
       
   290 
       
   291 Passed a public key id. Returns true if the key exists in the public keyring
       
   292 
       
   293 =head2 SecretKeyExists
       
   294 
       
   295 Passed a secret key id. Returns true if the key exists in the secret keyring
       
   296 
       
   297 =head1 KNOWN BUGS
       
   298 
       
   299 None
       
   300 
       
   301 =head1 COPYRIGHT
       
   302 
       
   303  Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
   304  All rights reserved.
       
   305  This component and the accompanying materials are made available
       
   306  under the terms of the License "Eclipse Public License v1.0"
       
   307  which accompanies this distribution, and is available
       
   308  at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
   309  
       
   310  Initial Contributors:
       
   311  Nokia Corporation - initial contribution.
       
   312  
       
   313  Contributors:
       
   314  
       
   315  Description:
       
   316  
       
   317 
       
   318 =cut