| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | PgpDecryptor - Module for decrypt PGP data | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | my $pgp = App::Greple::PgpDecryptor->new; | ||||||||||
| 8 | |||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||
| 10 | |||||||||||
| 11 | =head2 initialize | ||||||||||
| 12 | |||||||||||
| 13 | Initialize object. | ||||||||||
| 14 | |||||||||||
| 15 | Without parameter, read passphrase from terminal. | ||||||||||
| 16 | |||||||||||
| 17 | $pgp->initialize(); | ||||||||||
| 18 | |||||||||||
| 19 | Provide passphrase string or file descriptor if available. | ||||||||||
| 20 | |||||||||||
| 21 | $pgp->initialize({passphrase => passphrase}); | ||||||||||
| 22 | |||||||||||
| 23 | $pgp->initialize({passphrase_fd => fd}); | ||||||||||
| 24 | |||||||||||
| 25 | =head2 decrypt | ||||||||||
| 26 | |||||||||||
| 27 | Decrypt data. Pass the encrypted data and get the result. | ||||||||||
| 28 | |||||||||||
| 29 | $decrypted = $pgp->decript($encrpted); | ||||||||||
| 30 | |||||||||||
| 31 | =head2 decrypt_comand | ||||||||||
| 32 | |||||||||||
| 33 | Return decrypt command string. You can use this command to decrypt | ||||||||||
| 34 | data. Call B<reset> after command execution. | ||||||||||
| 35 | |||||||||||
| 36 | open(STDIN, '-|') or exec $pgp->decrypt_command; | ||||||||||
| 37 | |||||||||||
| 38 | =head2 reset | ||||||||||
| 39 | |||||||||||
| 40 | Reset internal status. | ||||||||||
| 41 | |||||||||||
| 42 | =cut | ||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | package App::Greple::PgpDecryptor; | ||||||||||
| 46 | |||||||||||
| 47 | 1 | ▶ | use v5.24; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 52 | 0 | my $obj = bless { | |||||||||
| 53 | FH => undef, | ||||||||||
| 54 | }, shift; | ||||||||||
| 55 | 0 | $obj; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub initialize { | ||||||||
|
Subroutine
initialize: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 59 | 0 | my $obj = shift; | |||||||||
| 60 | 0 | ▶ | my $opt = @_ ? shift : {}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | my $passphrase = ""; | |||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | if (my $fd = $opt->{passphrase_fd}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 0 | $obj->fh(_openfh($fd)); | |||||||||
| 65 | } | ||||||||||
| 66 | else { | ||||||||||
| 67 | 0 | ▶ | if (not defined $obj->fh) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | $obj->fh(_openfh()); | |||||||||
| 69 | } | ||||||||||
| 70 | 0 | ▶ | if (defined $opt->{passphrase}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | $passphrase = $opt->{passphrase}; | |||||||||
| 72 | } else { | ||||||||||
| 73 | 0 | _readphrase(\$passphrase); | |||||||||
| 74 | } | ||||||||||
| 75 | 0 | $obj->setphrase(\$passphrase); | |||||||||
| 76 | |||||||||||
| 77 | ## | ||||||||||
| 78 | ## Destroy data as much as possible | ||||||||||
| 79 | ## | ||||||||||
| 80 | 0 | $passphrase =~ s/./\0/g; | |||||||||
| 81 | 0 | $passphrase = ""; | |||||||||
| 82 | 0 | undef $passphrase; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | $obj; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | sub setphrase { | ||||||||
|
Subroutine
setphrase: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 89 | 0 | my $obj = shift; | |||||||||
| 90 | 0 | my $fh = $obj->fh; | |||||||||
| 91 | 0 | my $passphrase_r = shift; | |||||||||
| 92 | |||||||||||
| 93 | 0 | $obj->reset; | |||||||||
| 94 | 0 | $fh->syswrite($$passphrase_r, length($$passphrase_r)); | |||||||||
| 95 | 0 | $obj->reset; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | sub getphrase { | ||||||||
|
Subroutine
getphrase: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 99 | 0 | my $obj = shift; | |||||||||
| 100 | 0 | my $fh = $obj->fh; | |||||||||
| 101 | |||||||||||
| 102 | 0 | $obj->reset; | |||||||||
| 103 | 0 | my $phrase = $fh->getline; | |||||||||
| 104 | 0 | $obj->reset; | |||||||||
| 105 | 0 | $phrase; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | sub fh { | ||||||||
|
Subroutine
fh: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 109 | 0 | my $obj = shift; | |||||||||
| 110 | @_ ? $obj->{FH} = shift | ||||||||||
| 111 | 0 | ▶ | : $obj->{FH}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | sub pgppassfd { | ||||||||
|
Subroutine
pgppassfd: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 115 | 0 | my $obj = shift; | |||||||||
| 116 | 0 | $obj->fh->fileno; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 0 | ▶ | sub decrypt_command { | ||||||||
|
Subroutine
decrypt_command: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 120 | 0 | my $obj = shift; | |||||||||
| 121 | 0 | my $command = "gpg"; | |||||||||
| 122 | 0 | my @option = ( qw(--quiet --batch --decrypt) , | |||||||||
| 123 | qw(--no-tty --no-mdc-warning) ); | ||||||||||
| 124 | 0 | sprintf "$command @option --passphrase-fd %d", $obj->pgppassfd; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 0 | ▶ | sub reset { | ||||||||
|
Subroutine
reset: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 128 | 0 | my $obj = shift; | |||||||||
| 129 | 0 | ▶ | defined $obj->fh or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 0 | ▶ | $obj->fh->sysseek(0, 0) or die $!; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 0 | ▶ | sub _openfh { | ||||||||
|
Subroutine
_openfh: not called CC=4 SCAR=22.9
| |||||||||||
| 134 | 1 | ▶ | use Fcntl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 135 | 1 | ▶ | use IO::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 136 | |||||||||||
| 137 | 0 | my $fd = shift; | |||||||||
| 138 | 0 | my $fh; | |||||||||
| 139 | |||||||||||
| 140 | 0 | ▶ | if (defined $fd) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | $fh = IO::Handle->new; | |||||||||
| 142 | 0 | $fh->fdopen($fd, "w+"); | |||||||||
| 143 | } else { | ||||||||||
| 144 | 0 | $fh = new_tmpfile IO::File; | |||||||||
| 145 | 0 | ▶ | defined $fh or die "new_tmpefile: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | 0 | ▶ | $fh->fcntl(F_SETFD, 0) or die "fcntl F_SETFD failed: $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | |||||||||||
| 150 | 0 | return $fh; | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | 1 | my $noecho; | |||||||||
| 154 | my $restore; | ||||||||||
| 155 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=9.6
| |||||||||||
| 156 | 1 | ($noecho, $restore) = eval { | |||||||||
| 157 | 1 | require Term::ReadKey; | |||||||||
| 158 | 0 | import Term::ReadKey; | |||||||||
| 159 | 0 | ▶ | (sub { ReadMode('noecho', @_) }, sub { ReadMode('restore', @_) }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 160 | }; | ||||||||||
| 161 | 1 | ▶ | if (not defined $noecho) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | 0 | ▶ | $noecho = sub { system 'stty -echo < /dev/tty' }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 163 | 0 | ▶ | $restore = sub { system 'stty echo < /dev/tty' }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 164 | } | ||||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 0 | ▶ | sub _readphrase { | ||||||||
|
Subroutine
_readphrase: not called CC=3 SCAR=24.8
| |||||||||||
| 168 | 0 | my $passphrase_r = shift; | |||||||||
| 169 | |||||||||||
| 170 | 0 | print STDERR "Enter PGP Passphrase> "; | |||||||||
| 171 | 0 | ▶ | open my $tty, '<', '/dev/tty' or die; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 0 | $noecho->($tty); | |||||||||
| 173 | 0 | ▶ | if (defined (my $pass = ReadLine(0, $tty))) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 0 | chomp($$passphrase_r = $pass); | |||||||||
| 175 | } | ||||||||||
| 176 | 0 | $restore->($tty); | |||||||||
| 177 | 0 | close $tty; | |||||||||
| 178 | 0 | print STDERR "\n"; | |||||||||
| 179 | |||||||||||
| 180 | 0 | $passphrase_r; | |||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | 0 | ▶ | sub decrypt { | ||||||||
|
Subroutine
decrypt: not called CC=4 SCAR=27.3
Pod
documented
| |||||||||||
| 184 | 1 | ▶ | use IPC::Open2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 185 | |||||||||||
| 186 | 0 | my $obj = shift; | |||||||||
| 187 | 0 | my $enc_data = shift; | |||||||||
| 188 | |||||||||||
| 189 | 0 | $obj->reset; | |||||||||
| 190 | |||||||||||
| 191 | 0 | my $pid = open2(\*RDRFH, \*WTRFH, $obj->decrypt_command); | |||||||||
| 192 | |||||||||||
| 193 | 0 | ▶ | if (length($enc_data) <= 1024 * 16) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 194 | 0 | print WTRFH $enc_data; | |||||||||
| 195 | } | ||||||||||
| 196 | else { | ||||||||||
| 197 | 0 | my $pid = fork; | |||||||||
| 198 | 0 | ▶ | croak "process fork failed" if not defined $pid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 199 | 0 | ▶ | if ($pid == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 200 | 0 | print WTRFH $enc_data; | |||||||||
| 201 | 0 | close WTRFH; | |||||||||
| 202 | 0 | close RDRFH; | |||||||||
| 203 | 0 | exit; | |||||||||
| 204 | } | ||||||||||
| 205 | } | ||||||||||
| 206 | 0 | close WTRFH; | |||||||||
| 207 | 0 | my $dec_data = do { local $/ ; <RDRFH> }; | |||||||||
| 208 | 0 | close RDRFH; | |||||||||
| 209 | |||||||||||
| 210 | 0 | $dec_data; | |||||||||
| 211 | } | ||||||||||
| 212 | |||||||||||
| 213 | 1 | 1; | |||||||||