releasing/cbrtools/perl/Crypt/GPG.pm
author jjkang
Fri, 25 Jun 2010 20:58:33 +0800
changeset 605 122d2b873fd1
parent 602 3145852acc89
permissions -rw-r--r--
Minor changes: rofsbuild
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     1
# Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     2
# All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     3
# This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     4
# under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     5
# which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     6
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     7
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     8
# Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     9
# Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    10
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    11
# Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    12
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    13
# Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    14
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    15
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    16
# Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    17
# Crypt::GPG.pm
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    18
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    19
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    20
package Crypt::GPG;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    21
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    22
use strict;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    23
use File::Basename;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    24
use IPC::Open2;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    25
use IO::Handle;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    26
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    27
use Crypt;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    28
use vars qw(@ISA);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    29
@ISA=("Crypt");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    30
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    31
# Overidden methods from Crypt.pm
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    32
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    33
sub Initialize {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    34
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    35
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    36
  #check to see if the pgp executable exists
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    37
  grep {-x "$_/gpg.exe"} split /;/, $ENV{PATH}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    38
    or die "Error: The PGP executable \"gpg.exe\" does not exist in users path\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    39
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    40
  #call super class method
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    41
  $self->SUPER::Initialize();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    42
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    43
  #check for existence of keyrings and keys
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    44
  $self->CheckKeyRings();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    45
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    46
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    47
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    48
# Implemented abstract methods from Crypt.pm
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    49
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    50
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    51
sub DoEncrypt {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    52
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    53
  my $plainText = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    54
  my $cipherText = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    55
  my @recipientKeys = @{$_[0]};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    56
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    57
  $self->CheckKeyRings();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    58
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    59
  #build options list
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    60
  my @options = qw(--batch --no-tty --yes --always-trust);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    61
  push @options, '--status-fd 1';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    62
  push @options, '-o '.$self->Quoted($cipherText);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    63
  if ($self->DefaultPath()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    64
    push @options, '--homedir '.$self->Quoted($self->DefaultPath());
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    65
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    66
  foreach my $key (@recipientKeys) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    67
    if ($key =~ /^0x([0-9a-fA-F]{8})$/i) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    68
      push @options, '-r '.$1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    69
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    70
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    71
  my @command = '-e '.$self->Quoted($plainText);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    72
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    73
  # Do encryption. This occasionally fails due to GPG failing to read
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    74
  # the random_seed file when we get a return value of 2. Until we get
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    75
  # a later version of gpg checked as compatible, just retry if this happens.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    76
  my $retries = 2;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    77
  my $retval;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    78
  do {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    79
      my $cmd = "gpg @options @command";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    80
      print "Executing command: $cmd\n" if $self->{verbose} > 1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    81
      open GPG, "$cmd 2>&1 |" or die "Error: Encrypt command failed.\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    82
      my $error;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    83
      while (my $line = <GPG>) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    84
        if ($self->{verbose} > 1) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    85
          print "\t$line";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    86
        }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    87
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    88
      close GPG;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    89
      $retval = $? >> 8;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    90
      $retries = 0 unless( $retval == 2 );  # Only retry if retval is 2.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    91
      if( $retval ) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    92
        print "WARNING: GPG failure. Error code $retval. ";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    93
        print "Retrying GPG..." if( $retries > 0 );
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    94
        print "\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    95
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    96
  } while( $retries-- > 0 );
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    97
  die "ERROR: GPG returned error code $retval.\n" if ($retval > 0);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    98
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    99
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   100
sub DoDecrypt {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   101
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   102
  my $cipherText = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   103
  my $plainText = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   104
  my $passPhrase = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   105
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   106
  $self->CheckKeyRings();
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   107
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   108
  #build options list
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   109
  my @options = qw(--batch);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   110
  push @options, '--status-fd 1';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   111
  push @options, '--passphrase-fd 0';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   112
  push @options, '-o '.$self->Quoted($plainText);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   113
  if ($self->DefaultPath()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   114
    push @options, '--homedir '.$self->Quoted($self->DefaultPath());
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   115
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   116
  my @command = '-d '.$self->Quoted($cipherText);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   117
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   118
  #do decryption reading passphrase from STDIN writing output to log file
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   119
  my $gpgOutput = '/gpg_output.log';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   120
  my $cmd = "gpg @options @command";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   121
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   122
  # retry 100 times of GPG and opening GPG output
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   123
  my $retries = 100;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   124
  while ($retries > 0) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   125
    print "Executing command: $cmd\n" if $self->{verbose} > 1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   126
    if (open GPGIN, "| $cmd 2>NUL 1> $gpgOutput") {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   127
      print GPGIN "$passPhrase\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   128
      while (my $line  = <GPGIN>) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   129
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   130
      close GPGIN;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   131
	  
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   132
      #open output of gpg command from file for parsing
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   133
      if (open GPGOUT, "$gpgOutput") {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   134
        #open output of gpg successfully, then jump out and go ahead
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   135
        last;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   136
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   137
      else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   138
        print "Warning: Cannot open gpg output file, $!\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   139
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   140
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   141
    else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   142
      print "Warning: Error: Decrypt command failed, $!\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   143
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   144
    $retries--;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   145
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   146
    # sleep 10 seconds for next try
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   147
    sleep(10);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   148
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   149
  die "Error: Cannot create or open output log file for $cipherText.\n" if ($retries<=0);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   150
  
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   151
  my $badPassPhrase =0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   152
  my %enc_to;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   153
  my %no_seckey;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   154
  my $keyTally = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   155
  my $useKeyTally = 0; # Fallback for if parsing fails
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   156
  while (my $line = <GPGOUT>) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   157
    if ($self->{verbose} > 1) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   158
      print "\t$line";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   159
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   160
    next if ($line =~ /^\s*$/);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   161
    if ($line =~ /BAD_PASSPHRASE/) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   162
      $badPassPhrase = 1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   163
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   164
    elsif ($line =~ /GOOD_PASSPHRASE/) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   165
      $badPassPhrase = 0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   166
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   167
    elsif ($line =~ /ENC_TO/) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   168
      if ($line =~ /ENC_TO\s+([\dA-F]*)/) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   169
        $enc_to{$1} = $1; # Value is unimportant
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   170
      } else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   171
        $useKeyTally = 1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   172
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   173
      --$keyTally;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   174
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   175
    elsif ($line =~ /NO_SECKEY/) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   176
      if ($line =~ /NO_SECKEY\s+([\dA-F]*)/) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   177
        $no_seckey{$1} = $1; # Value is unimportant
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   178
      } else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   179
        $useKeyTally = 1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   180
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   181
      --$keyTally;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   182
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   183
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   184
  close GPGOUT;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   185
  my $retval = $? >> 8;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   186
  unlink $gpgOutput;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   187
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   188
  if (!$useKeyTally) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   189
    foreach my $key (keys(%no_seckey)) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   190
      delete $no_seckey{$key};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   191
      if (exists $enc_to{$key}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   192
        delete $enc_to{$key};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   193
      } else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   194
        die "Error: Parsing of GPG output failed. Got a NO_SECKEY for no corresponding ENC_TO.\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   195
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   196
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   197
    $keyTally = scalar(keys(%enc_to)); # Number of private keys
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   198
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   199
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   200
  #handle specific decryption errors
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   201
  if ($badPassPhrase and $keyTally != 0) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   202
    die "Error: Decryption of $cipherText failed. BAD_PASSPHRASE\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   203
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   204
  elsif ($keyTally == 0) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   205
    die "Error: Decryption of $cipherText failed. No decrypting key available. NO_SECKEY\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   206
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   207
  elsif ($keyTally < 0) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   208
    # Parsing failed, and we got spurious NO_SECKEY messages
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   209
    die "Error: Parsing of GPG output failed. Too many NO_SECKEYs\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   210
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   211
  die "Error code returned by gpg: $retval.\n" if ($retval > 0);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   212
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   213
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   214
sub GetPublicKeyList {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   215
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   216
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   217
  my @options;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   218
  if ($self->DefaultPath()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   219
    push @options, '--homedir '.$self->Quoted($self->DefaultPath());
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   220
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   221
  my @command = qw(--list-keys);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   222
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   223
  #list and extract keyids
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   224
  open GPG, "gpg @options @command 2>&1 |" or die "Error: List keys command failed.\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   225
  my @keys;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   226
  while (my $line = <GPG>) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   227
    if ($line =~ /^pub.*?([0-9a-fA-F]{8})\b/i) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   228
      push @keys, '0x'.$1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   229
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   230
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   231
  close GPG;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   232
  return \@keys;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   233
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   234
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   235
sub GetSecretKeyList {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   236
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   237
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   238
  my @options;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   239
  if ($self->DefaultPath()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   240
    push @options, '--homedir '.$self->Quoted($self->DefaultPath());
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   241
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   242
  my @command = qw(--list-secret-keys);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   243
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   244
  #list and extract keyids
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   245
  open GPG, "gpg @options @command 2>&1 |" or die "Error: List keys command failed.\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   246
  my @keys;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   247
  while (my $line = <GPG>) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   248
    if ($line =~ /^sec.*?([0-9a-fA-F]{8})\b/i) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   249
      push @keys, '0x'.$1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   250
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   251
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   252
  close GPG;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   253
  return \@keys;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   254
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   255
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   256
# Private
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   257
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   258
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   259
sub CheckKeyRings {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   260
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   261
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   262
  if ($self->DefaultPath) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   263
    unless (-e $self->DefaultPath.'/pubring.gpg') {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   264
      die "Error: PGP Public keyring does not exist\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   265
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   266
    unless (-e $self->DefaultPath.'/secring.gpg') {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   267
      die "Error: PGP secret keyring does not exist\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   268
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   269
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   270
  unless (@{$self->PublicKeyList}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   271
    die "Error: PGP public keyring is empty\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   272
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   273
  unless (@{$self->SecretKeyList}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   274
    die "Error: PGP secret keyring is empty\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   275
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   276
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   277
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   278
1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   279
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   280
__END__
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   281
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   282
=head1 NAME
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   283
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   284
Crypt::GPG.pm - A wrapper over the Gnu Privacy Guard command line PGP tool
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   285
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   286
=head1 DESCRIPTION
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   287
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   288
C<Crypt::GPG> is inherited from the abstract base class C<Crypt>, implementing the abstract methods required for PGP encryption, decryption, etc... by calling Gnu Privacy Guard PGP command line tool (tested with version 1.0.6). For this module to work the PGP executable must have the name C<gpg.exe> and exist in one of the directories defined in the users path.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   289
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   290
=head1 KNOWN BUGS
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   291
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   292
None
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   293
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   294
=head1 COPYRIGHT
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   295
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   296
 Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   297
 All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   298
 This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   299
 under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   300
 which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   301
 at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   302
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   303
 Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   304
 Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   305
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   306
 Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   307
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   308
 Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   309
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   310
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   311
=cut