| 1 | package Brackup::Decrypt; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Brackup::Util qw(slurp tempfile); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | # Decrypt a dataref into a dataref | ||||||||||||||||||||||||||||||||||||||||||
| 9 | 45 | ▶ | sub decrypt_data { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decrypt_data: called CC=4 SCAR=22.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 45 | my ($dataref, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 45 | my $meta = $opts{meta}; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | # do nothing if the data is not encrypted | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 45 | ▶ | return $dataref unless $meta && $meta->{"GPG-Recipient"}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$meta and $meta->{"GPG-Recipient"}
MC/DC0%0/2
$meta and $meta->{"GPG-Recipient"}
$meta
$meta->{"GPG-Recipient"}
Truth table33%1/3
A and B
A= $metaB= $meta->{"GPG-Recipient"}
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | my $dataref_temp = ( (tempfile())[1] || die ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
(tempfile())[1] || die()
MC/DC0%0/1
(tempfile())[1] || die()
(tempfile())[1]
Truth table0%0/2
A
A= (tempfile())[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | write_to_file($dataref_temp, $dataref); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | my $decrypted_temp = decrypt_file($dataref_temp,%opts); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | unlink($dataref_temp); | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | my $data = slurp($decrypted_temp); | |||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | unlink($decrypted_temp); | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | return \$data; | |||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub write_to_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_to_file: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my ($file, $ref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | open (my $fh, '>', $file) or die "Failed to open $file for writing: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | print $fh $$ref; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | close($fh) or die; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | die "File is not of the correct size" unless -s $file == length $$ref; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 9 | ▶ | sub decrypt_file_if_needed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decrypt_file_if_needed: called CC=3 SCAR=15.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 9 | my ($filename) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 9 | my $meta = slurp($filename, decompress => 1); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 9 | ▶ | if ($meta =~ /[\x00-\x08]/) { # silly is-binary heuristic | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my $new_file = decrypt_file($filename,no_batch => 1); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if (defined $new_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | warn "Decrypted ${filename} to ${new_file}.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | return $new_file; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 9 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | # Decrypt a file into a new file | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # Return the new file's name, or undef. | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 13 | our $warned_about_gpg_agent = 0; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub decrypt_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decrypt_file: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my ($encrypted_file,%opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $no_batch = delete $opts{no_batch}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $meta = delete $opts{meta}; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | croak("Unknown options: " . join(', ', keys %opts)) if %opts; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # find which key we're using to decrypt it | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($meta) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | my $rcpt = $meta->{"GPG-Recipient"} or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | return undef; | ||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | unless ($ENV{'GPG_AGENT_INFO'} || | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$ENV{"GPG_AGENT_INFO"} or @Brackup::GPG_ARGS or $Brackup::Decrypt::warned_about_gpg_agent++
$ENV{"GPG_AGENT_INFO"} or @Brackup::GPG_ARGS
MC/DC0%0/3
$ENV{"GPG_AGENT_INFO"} or @Brackup::GPG_ARGS or $Brackup::Decrypt::warned_about_gpg_agent++
$ENV{"GPG_AGENT_INFO"}
@Brackup::GPG_ARGS
$Brackup::Decrypt::warned_about_gpg_agent++
Truth table0%0/4
A or B or C
A= $ENV{"GPG_AGENT_INFO"}B= @Brackup::GPG_ARGSC= $Brackup::Decrypt::warned_about_gpg_agent++
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | @Brackup::GPG_ARGS || | ||||||||||||||||||||||||||||||||||||||||||
| 72 | $warned_about_gpg_agent++) | ||||||||||||||||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $err = q{ | |||||||||||||||||||||||||||||||||||||||||
| 75 | # | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # WARNING: trying to restore encrypted files, | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # but $ENV{'GPG_AGENT_INFO'} not present. | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # Are you running gpg-agent? | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # | ||||||||||||||||||||||||||||||||||||||||||
| 80 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $err =~ s/^\s+//gm; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | warn $err; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my $output_temp = ( (tempfile())[1] || die ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
(tempfile())[1] || die()
MC/DC0%0/1
(tempfile())[1] || die()
(tempfile())[1]
Truth table0%0/2
A
A= (tempfile())[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | my @list = ("gpg", @Brackup::GPG_ARGS, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | "--use-agent", | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | !$opts{no_batch} ? ("--batch") : (), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | "--trust-model=always", | ||||||||||||||||||||||||||||||||||||||||||
| 91 | "--output", $output_temp, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | "--yes", "--quiet", | ||||||||||||||||||||||||||||||||||||||||||
| 93 | "--decrypt", $encrypted_file); | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | system(@list) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | and die "Failed to decrypt with gpg: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | return $output_temp; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||