File Coverage

blib/lib/Crypt/PGPSimple.pm
Criterion Covered Total %
statement 0 103 0.0
branch 0 14 0.0
condition 0 6 0.0
subroutine 0 23 0.0
pod 0 22 0.0
total 0 168 0.0


line stmt bran cond sub pod time code
1             # ----------------------------------------------------------------------------
2             # Crypt::PGPSimple.pm
3             # Copyright (c) 2000 Jason M. Hinkle. All rights reserved. This module is
4             # free software; you may redistribute it and/or modify it under the same
5             # terms as Perl itself.
6             # For more information see: http://www.verysimple.com/scripts/
7             #
8             # LEGAL DISCLAIMER:
9             # This software is provided as-is. Use it at your own risk. The
10             # author takes no responsibility for any damages or losses directly
11             # or indirectly caused by this software.
12             # ----------------------------------------------------------------------------
13             package Crypt::PGPSimple;
14             require 5.000;
15            
16             $Crypt::PGPSimple::VERSION = "0.13";
17             $Crypt::PGPSimple::ID = "Crypt::PGPSimple.pm";
18            
19             =head1 NAME
20            
21             Crypt::PGPSimple - Interface to PGP for Windows and UNIX. No other mods required.
22            
23             =head1 DESCRIPTION
24            
25             Object oriented interface to PGP. Requires PGP installed on the server.
26             Allows Perl scripts to encrypt, decrypt and sign messages using PGP
27             for the encyption. Tested with PGP 2.6.2 and PGP 6.5.8 on UNIX and
28             Windows.
29            
30             =head1 SYNOPSIS
31            
32             use Crypt::PGPSimple;
33             my ($objPGP) = new Crypt::PGPSimple;
34            
35             # tell Crypt::PGPSimple about the PGP executable (these are the defaults, by the way,
36             # so if this matches your system, you don't need to set these.
37             # PgpTempDir needs to be writable by the account running the script.
38             $objPGP->Version(2.6.2); # (not currently used, but might be later)
39             $objPGP->PgpExePath("C:\\Progra~1\\Networ~1\\Pgp\\PGP.exe");
40             $objPGP->PgpKeyPath("C:\\Progra~1\\Networ~1\\Pgp\\PgpKey~1");
41             $objPGP->PgpTempDir("C:\\");
42            
43             # Example 1: Encrypt
44             $objPGP->PublicKey("myfriend\@herhost.com");
45             $objPGP->PlainText($plain_text_message);
46             $objPGP->Encrypt;
47             my ($encrypted_message) = $objPGP->EncryptedText;
48            
49             # Example 2: Decrypt
50             $objPGP->Password("mypassword");
51             $objPGP->EncryptedText($encrypted_message);
52             $objPGP->Decrypt;
53             my ($plain_text_message) = $objPGP->PlainText;
54            
55             # Example 3: EncryptSign
56             $objPGP->PublicKey("myfriend\@herhost.com");
57             $objPGP->PrivateKey("me\@myhost.com");
58             $objPGP->Password("mypassword");
59             $objPGP->PlainText($plain_text_message);
60             $objPGP->EncryptSign;
61             my ($encrypted_signed_message) = $objPGP->EncryptedText;
62            
63             # Example 4: Sign
64             $objPGP->PrivateKey("me\@myhost.com");
65             $objPGP->Password("mypassword");
66             $objPGP->PlainText($plain_text_message);
67             $objPGP->Sign;
68             my ($signed_message) = $objPGP->SignedText;
69            
70             =head1 USAGE
71            
72             See http://www.verysimple.com/scripts/ for more information.
73            
74             =head1 PROPERTIES
75            
76             Calling a property with no arguments will return the current value.
77             Calling a property with an argument will change the current value to
78             the value of the argument supplied and return true (1).
79            
80             EncryptedText
81             ErrDescription
82             Password
83             PgpExePath
84             PgpKeyPath
85             PgpTempDir
86             PgpTimeZone
87             PgpVersion
88             PlainText
89             PrivateKey
90             PublicKey
91             Result
92             SignedText
93             Version
94            
95             =head1 METHODS
96            
97             The PGP-related methods (encrypting, decrypting, etc) will return true (1) if
98             they succeeded or false (0) if not. The PGP result message is available
99             in the Result property. If an error occured, ErrDescription may contain
100             details.
101            
102             Decrypt
103             DoPgpCommand($strPgpCommand, $strArguments)
104             Encrypt
105             EncryptSign
106             ErrClear
107             Reset
108             Sign
109             new
110            
111             =head1 VERSION HISTORY
112            
113             0.13 (11/04/00) Updated documentation only.
114             0.12 (11/03/01) Fixed bug w/ multiple recieients (Thanks Ken Hoover)
115             0.11 (01/09/00) Original Release
116            
117             =head1 BUGS & KNOWN ISSUES
118            
119             This module may not work properly with PGP 5.x. Version 5 used a slightly
120             different command-line syntax which was apparently dropped for version 6.
121             There are no current plans to test or modify this module for use with PGP 5.
122            
123             =head1 AUTHOR
124            
125             Jason M. Hinkle
126            
127             =head1 COPYRIGHT
128            
129             Copyright (c) 2001 Jason M. Hinkle. All rights reserved.
130             This module is free software; you can redistribute it and/or modify
131             it under the same terms as Perl itself.
132            
133             =cut
134            
135             #_____________________________________________________________________________
136             sub new {
137 0     0 0   $|++;
138 0           my $class = shift;
139 0           my $this = {
140             strPgpVersion => 6.5.8,
141             strPgpExePath => "C:\\Progra~1\\Networ~1\\Pgp\\PGP.exe",
142             strPgpKeyPath => "C:\\Progra~1\\Networ~1\\Pgp\\PgpKey~1",
143             strPgpTempDir => "C:\\",
144             strPgpTimeZone => "CST6CDT",
145             strPublicKey => "",
146             strPrivateKey => "",
147             strPassword => "",
148             strPlainText => "",
149             strEncryptedText => "",
150             strSignedText => "",
151             strResult => "",
152             strErrDescription => "",
153             };
154 0           bless $this, $class;
155            
156 0           return $this;
157             }
158            
159             # ###########################################################################
160             # PUBLIC PROPERTIES
161            
162             #_____________________________________________________________________________
163             sub Version {
164 0     0 0   return $VERSION;
165             }
166            
167             #_____________________________________________________________________________
168             sub PgpVersion {
169 0     0 0   return shift->_GetSetProperty("strPgpVersion",shift);
170             }
171            
172             #_____________________________________________________________________________
173             sub PgpExePath {
174 0     0 0   return shift->_GetSetProperty("strPgpExePath",shift);
175             }
176            
177             #_____________________________________________________________________________
178             sub PgpKeyPath {
179 0     0 0   return shift->_GetSetProperty("strPgpKeyPath",shift);
180             }
181            
182             #_____________________________________________________________________________
183             sub PgpTempDir {
184 0     0 0   return shift->_GetSetProperty("strPgpTempDir",shift);
185             }
186            
187             #_____________________________________________________________________________
188             sub PgpTimeZone {
189 0     0 0   return shift->_GetSetProperty("strPgpTimeZone",shift);
190             }
191            
192             #_____________________________________________________________________________
193             sub PublicKey {
194 0     0 0   return shift->_GetSetProperty("strPublicKey",shift);
195             }
196            
197             #_____________________________________________________________________________
198             sub PrivateKey {
199 0     0 0   return shift->_GetSetProperty("strPrivateKey",shift);
200             }
201            
202             #_____________________________________________________________________________
203             sub Password {
204 0     0 0   return shift->_GetSetProperty("strPassword",shift);
205             }
206            
207             #_____________________________________________________________________________
208             sub PlainText {
209 0     0 0   return shift->_GetSetProperty("strPlainText",shift);
210             }
211            
212             #_____________________________________________________________________________
213             sub EncryptedText {
214 0     0 0   return shift->_GetSetProperty("strEncryptedText",shift);
215             }
216            
217             #_____________________________________________________________________________
218             sub SignedText {
219 0     0 0   return shift->_GetSetProperty("strSignedText",shift);
220             }
221            
222             #_____________________________________________________________________________
223             sub Result {
224 0     0 0   return shift->{'strResult'};
225             }
226            
227             #_____________________________________________________________________________
228             sub ErrDescription {
229 0     0 0   return shift->{'strErrDescription'};
230             }
231            
232             # ###########################################################################
233             # PRIVATE PROPERTIES
234            
235             #_____________________________________________________________________________
236             sub _GetSetProperty {
237             # private fuction that is used by all the properties to get/set values
238             # if a parameter is sent in, then the property is set and true is returned.
239             # if no parameter is sent, then the current value is returned
240 0     0     my $this = shift;
241 0           my $fieldName = shift;
242 0           my $newValue = shift;
243 0 0         if (defined($newValue)) {
244 0           $this->{$fieldName} = $newValue;
245             } else {
246 0           return $this->{$fieldName};
247             }
248 0           return 1;
249             }
250            
251             # ###########################################################################
252             # PUBLIC METHODS
253            
254             #_____________________________________________________________________________
255             sub Encrypt {
256 0     0 0   my ($this) = shift;
257            
258 0           my ($return_value) = 0;
259            
260             # generate the command line
261 0           my ($pgp_command) = $this->{'strPgpExePath'}
262             . " -feat +batchmode +force "
263             . $this->{'strPublicKey'};
264            
265 0           $this->{'strEncryptedText'} = $this->DoPgpCommand($pgp_command,$this->{'strPlainText'});
266            
267             # if there were results then everything went as planned
268 0 0         if ($this->{'strEncryptedText'} ne "") {
269 0           $return_value = 1;
270             }
271            
272 0           return $return_value;
273             }
274            
275             #_____________________________________________________________________________
276             sub Decrypt {
277 0     0 0   my ($this) = shift;
278            
279             # assume fail
280 0           my ($return_value) = 0;
281            
282             # generate the command line
283 0           my ($pgp_command) = $this->{'strPgpExePath'}
284             . " -f +batchmode +force";
285            
286 0           $this->{'strPlainText'} = $this->DoPgpCommand($pgp_command,$this->{'strEncryptedText'});
287            
288             # if there were results then everything went as planned
289 0 0         if ($this->{'strPlainText'} ne "") {
290 0           $return_value = 1;
291             }
292            
293 0           return $return_value;
294             }
295            
296             #_____________________________________________________________________________
297             sub EncryptSign {
298 0     0 0   my ($this) = shift;
299            
300 0           my ($return_value) = 0;
301            
302             # generate the command line
303 0           my ($pgp_command) = $this->{'strPgpExePath'}
304             . " -feast +batchmode +force "
305             . $this->{'strPublicKey'}
306             . " -u " . $this->{'strPrivateKey'};
307            
308 0           $this->{'strEncryptedText'} = $this->DoPgpCommand($pgp_command,$this->{'strPlainText'});
309            
310             # if there were results then everything went as planned
311 0 0         if ($this->{'strEncryptedText'} ne "") {
312 0           $return_value = 1;
313             }
314            
315 0           return $return_value;
316             }
317            
318             #_____________________________________________________________________________
319             sub Sign {
320 0     0 0   my ($this) = shift;
321            
322 0           my ($return_value) = 0;
323            
324             # generate the command line
325 0           my ($pgp_command) = $this->{'strPgpExePath'}
326             . " -fts +batchmode +force -u "
327             . $this->{'strPrivateKey'};
328            
329 0           $this->{'strSignedText'} = $this->DoPgpCommand($pgp_command,$this->{'strPlainText'});
330            
331             # if there were results then everything went as planned
332 0 0         if ($this->{'strSignedText'} ne "") {
333 0           $return_value = 1;
334             }
335            
336 0           return $return_value;
337             }
338            
339             #_____________________________________________________________________________
340             sub ErrClear {
341 0     0 0   $strErrDescription = "";
342 0           return 1;
343             }
344            
345             #_____________________________________________________________________________
346             sub Reset {
347 0     0 0   my ($this) = shift;
348 0   0       my ($clear_key_info) = shift || "";
349            
350 0           $this->{'strPlainText'} = "";
351 0           $this->{'strEncryptedText'} = "";
352 0           $this->{'strSignedText'} = "";
353 0           $this->{'strResult'} = "";
354 0           $this->{'strErrDescription'} = "";
355            
356 0 0         if ($clear_key_info) {
357 0           $this->{'strPublicKey'} = "";
358 0           $this->{'strPrivateKey'} = "";
359 0           $this->{'strPassword'} = "";
360             }
361 0           return 1;
362             }
363            
364            
365             #_____________________________________________________________________________
366             sub DoPgpCommand {
367 0     0 0   my ($this) = shift;
368 0   0       my ($pgp_command) = shift || "";
369 0   0       my ($pgp_args) = shift || "";
370            
371 0           my ($return_value) = "";
372            
373             # get the filepath settings and set our temp file paths
374 0           my ($encrypted_file_path) = $this->{'strPgpTempDir'} . $$ . ".pgp";
375 0           my ($stdout_path) = $this->{'strPgpTempDir'} . $$ . ".txt";
376            
377 0           $pgp_command .= " > " . $encrypted_file_path;
378            
379             # UNCOMMENT TO DEBUG
380             # print $encrypted_file_path . "\n";
381             # print $stdout_path . "\n";
382             # print $pgp_command . "\n";
383             # print $pgp_args . "\n";
384            
385             # set the environmental variables
386 0           $ENV{"TZ"} = $this->{'strPgpTimeZone'};
387 0           $ENV{"PGPPATH"} = $this->{'strPgpKeyPath'};
388 0           $ENV{"PGPPASS"} = $this->{'strPassword'};
389            
390             # do our redirection magic. pgp insists on sending text to STDERR and STDOUT
391             # even if you tell it to be quite. this way we can catch them all.
392 0           open (OLDOUT, ">&STDOUT");
393 0           open (OLDERR, ">&STDERR");
394 0           open (STDOUT, ">$stdout_path");
395 0           open (STDERR, ">>&STDOUT");
396            
397             # execute PGP command
398 0           open (PGPCOMMAND, "|$pgp_command");
399 0           print PGPCOMMAND $pgp_args;
400 0           close (PGPCOMMAND);
401            
402             # undo our redirection magic
403 0           close (STDOUT);
404 0           close (STDERR);
405 0           open (STDOUT, ">&OLDOUT");
406 0           open (STDERR, ">&OLDERR");
407            
408             # close these just to avoid Perl warnings
409 0           close (OLDOUT);
410 0           close (OLDERR);
411            
412             # open the encrypted file
413 0           open (ENCRYPTED, "$encrypted_file_path");
414 0           $return_value = join('',);
415 0           close (ENCRYPTED);
416            
417             # open the redirect file to see what pgp sent to STDOUT & STDERR
418 0           open (PGPERROR, "$stdout_path");
419 0           $this->{'strResult'} = join('',);
420 0           close (PGPERROR);
421            
422             # delete the temporary files (COMMENT TO DEBUG)
423 0           unlink($encrypted_file_path);
424 0           unlink($stdout_path);
425            
426             # if there is no encrypted text, then something went wrong
427 0 0         if ($return_value eq "") {
428 0           $this->{'strErrDescription'} = "PGP Command Failed. Check Result Property For Details.";
429             }
430            
431 0           $ENV{"PGPPASS"} = "";
432 0           return $return_value;
433             }
434             1;