line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::PGP2;
|
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
661
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
4
|
1
|
|
|
1
|
|
999
|
use diagnostics;
|
|
1
|
|
|
|
|
213998
|
|
|
1
|
|
|
|
|
109
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require Exporter;
|
7
|
1
|
|
|
1
|
|
1260
|
use AutoLoader qw(AUTOLOAD);
|
|
1
|
|
|
|
|
1711
|
|
|
1
|
|
|
|
|
6
|
|
8
|
1
|
|
|
1
|
|
48
|
use vars qw / $VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK /;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
119
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
@ISA = qw(Exporter);
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
%EXPORT_TAGS = ( );
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
@EXPORT = qw ( encrypt PGP_ERR_SUCCESS PGP_ERR_FAIL PGP_ERR_BAD_OPTIONS PGP_ERR_MISSING_KEY PGP_ERR_MISSING_TEXT );
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
@EXPORT_OK = ();
|
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
903
|
use IPC::Open3;
|
|
1
|
|
|
|
|
4612
|
|
|
1
|
|
|
|
|
448
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
$VERSION = '0.03';
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
1;
|
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
0
|
0
|
|
sub PGP_ERR_SUCCESS { 0 }
|
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
0
|
0
|
|
sub PGP_ERR_FAIL { 1 }
|
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
0
|
0
|
|
sub PGP_ERR_BAD_OPTIONS { 2 }
|
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
0
|
0
|
|
sub PGP_ERR_MISSING_KEY { 3 }
|
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
0
|
0
|
|
sub PGP_ERR_MISSING_TEXT { 4 }
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# Program: encrypt
|
35
|
|
|
|
|
|
|
# Author : James Briggs
|
36
|
|
|
|
|
|
|
# Date : 2001 01 22
|
37
|
|
|
|
|
|
|
# Version: see $VERSION
|
38
|
|
|
|
|
|
|
# Purpose: generate PGP ciphertext using external pgp utility
|
39
|
|
|
|
|
|
|
# Env : Perl5 and IPC::Open3
|
40
|
|
|
|
|
|
|
# Usage : my ($ciphertext, $msg, $error) = Crypt::PGP2::encrypt($plaintext,'my secret text','at');
|
41
|
|
|
|
|
|
|
# Returns: list with 3 elements (see POD for details)
|
42
|
|
|
|
|
|
|
# Notes : see the POD documentation also
|
43
|
|
|
|
|
|
|
# - Perl signals should not be used to monitor the pipes as they are unsafe
|
44
|
|
|
|
|
|
|
# However, the $msg return will give the pgp status code, if available.
|
45
|
|
|
|
|
|
|
# - Only 3 files are needed to encrypt a file with a public key:
|
46
|
|
|
|
|
|
|
# pubring.pgp, randseed.bin, and config.txt (chmod 400 *) ?
|
47
|
|
|
|
|
|
|
# - permissions on tmp, .pgp must be set correctly (chmod 100 .pgp) ?
|
48
|
|
|
|
|
|
|
# - PGP generates temp files. The names of these files can be seen when +verbose=3
|
49
|
|
|
|
|
|
|
# - You must use more than 512 bit keys to be secure.
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub encrypt {
|
52
|
|
|
|
|
|
|
# retrieve arguments
|
53
|
0
|
|
|
0
|
0
|
|
my ($plaintext, $key, $options) = @_;
|
54
|
|
|
|
|
|
|
|
55
|
0
|
0
|
0
|
|
|
|
return ('', '', PGP_ERR_MISSING_KEY) if not defined $key or $key eq '';
|
56
|
0
|
0
|
0
|
|
|
|
return ('', '', PGP_ERR_MISSING_TEXT) if not defined $plaintext or $plaintext eq '';
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# set explicit path to PGP binary
|
59
|
0
|
|
|
|
|
|
my $pgp = '/usr/local/bin/pgp';
|
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
$ENV{'PGPPATH'} = '/.pgp';
|
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
my $ciphertext = '';
|
64
|
0
|
|
|
|
|
|
my $msg = '';
|
65
|
0
|
|
|
|
|
|
my $error = '';
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# assign defaults if blank options
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# -a means ASCII armour
|
70
|
|
|
|
|
|
|
# -t means portable text newlines
|
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
0
|
|
|
|
$options = 'at' if not defined $options or $options eq '';
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# only allow certain pgp options
|
75
|
0
|
0
|
|
|
|
|
return ('', '', PGP_ERR_BAD_OPTIONS) if $options !~ /^[at]+$/;
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# this module needs leading '-' and pgp filter option 'fe'
|
78
|
0
|
|
|
|
|
|
$options = '-fe' . $options;
|
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $pid = open3 \*WRITE, \*READ, \*ERROR, $pgp, $options, $key;
|
81
|
|
|
|
|
|
|
|
82
|
0
|
0
|
|
|
|
|
return ('', '', PGP_ERR_FAIL) if ! $pid;
|
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
print WRITE $plaintext;
|
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
close WRITE;
|
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
$ciphertext = join '', ;
|
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
close READ;
|
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
$msg = "$pgp $options $key\n";
|
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
$msg .= join '', ;
|
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
close ERROR;
|
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
return ($ciphertext, $msg, PGP_ERR_SUCCESS);
|
99
|
|
|
|
|
|
|
}
|
100
|
|
|
|
|
|
|
__END__
|