| 1 | # Copyright © 2017 Guillem Jover <guillem@debian.org> | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||||||||||
| 6 | # (at your option) any later version. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||
| 14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =encoding utf8 | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Dpkg::OpenPGP - multi-backend OpenPGP support | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | This module provides a class for transparent multi-backend OpenPGP support. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | B<Note>: This is a private module, its API can change at any time. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | package Dpkg::OpenPGP 0.01; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 6 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 6 | ▶ | use List::Util qw(none); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 6 | ▶ | use Dpkg::Gettext; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 6 | ▶ | use Dpkg::ErrorHandling; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 6 | ▶ | use Dpkg::IPC; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 6 | ▶ | use Dpkg::Path qw(find_command); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 6 | ▶ | use Dpkg::OpenPGP::ErrorCodes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 6 | my @BACKENDS = qw( | |||||||||||||||||||||||||||
| 43 | sop | ||||||||||||||||||||||||||||
| 44 | sq | ||||||||||||||||||||||||||||
| 45 | gpg | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | 6 | my %BACKEND = ( | |||||||||||||||||||||||||||
| 48 | sop => 'SOP', | ||||||||||||||||||||||||||||
| 49 | sq => 'Sequoia', | ||||||||||||||||||||||||||||
| 50 | gpg => 'GnuPG', | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 11 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=25.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 11 | my ($this, %opts) = @_; | |||||||||||||||||||||||||||
| 55 | 11 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 11 | my $self = {}; | |||||||||||||||||||||||||||
| 58 | 11 | bless $self, $class; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 11 | ▶ | my $backend = $opts{backend} // 'auto'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"backend"} // "auto"
MC/DC100%1/1
$opts{"backend"} // "auto"
$opts{"backend"}
Truth table100%2/2
A
A= $opts{"backend"}
| |||||||||||||||||||||||||||||
| 61 | my %backend_opts = ( | ||||||||||||||||||||||||||||
| 62 | cmdv => $opts{cmdv} // 'auto', | ||||||||||||||||||||||||||||
| 63 | 11 | ▶ | cmd => $opts{cmd} // 'auto', | ||||||||||||||||||||||||||
|
Condition100%4/4
$opts{"cmdv"} // "auto"
$opts{"cmd"} // "auto"
MC/DC100%1/1
$opts{"cmdv"} // "auto"
$opts{"cmdv"}
MC/DC100%1/1
$opts{"cmd"} // "auto"
$opts{"cmd"}
Truth table100%2/2
A
A= $opts{"cmdv"}
Truth table100%2/2
A
A= $opts{"cmd"}
| |||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 11 | ▶ | if ($backend eq 'auto') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 67 | # Defaults for stateless full API auto-detection. | ||||||||||||||||||||||||||||
| 68 | 6 | ▶ | $opts{needs}{api} //= 'full'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"needs"}{"api"} //= "full"
MC/DC100%1/1
$opts{"needs"}{"api"} //= "full"
$opts{"needs"}{"api"}
Truth table100%2/2
A
A= $opts{"needs"}{"api"}
| |||||||||||||||||||||||||||||
| 69 | 6 | ▶ | $opts{needs}{keystore} //= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"needs"}{"keystore"} //= 0
MC/DC0%0/1
$opts{"needs"}{"keystore"} //= 0
$opts{"needs"}{"keystore"}
Truth table50%1/2
A
A= $opts{"needs"}{"keystore"}
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 6 | ▶ | if (none { $opts{needs}{api} eq $_ } qw(full verify)) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | error(g_('unknown OpenPGP api requested %s'), $opts{needs}{api}); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 6 | $self->{backend} = $self->_auto_backend($opts{needs}, %backend_opts); | |||||||||||||||||||||||||||
| 76 | } elsif (exists $BACKEND{$backend}) { | ||||||||||||||||||||||||||||
| 77 | 5 | $self->{backend} = $self->_load_backend($BACKEND{$backend}, %backend_opts); | |||||||||||||||||||||||||||
| 78 | 5 | ▶ | if (! $self->{backend}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | error(g_('cannot load OpenPGP backend %s'), $backend); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 0 | error(g_('unknown OpenPGP backend %s'), $backend); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 11 | return $self; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 23 | ▶ | sub _load_backend { | ||||||||||||||||||||||||||
|
Subroutine
_load_backend: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 89 | 23 | my ($self, $backend, %opts) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 23 | my $module = "Dpkg::OpenPGP::Backend::$backend"; | |||||||||||||||||||||||||||
| 92 | 23 | eval qq{ | |||||||||||||||||||||||||||
| 93 | require $module; | ||||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||
| 95 | 23 | ▶ | return if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 23 | return $module->new(%opts); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 6 | ▶ | sub _auto_backend { | ||||||||||||||||||||||||||
|
Subroutine
_auto_backend: called CC=7 SCAR=20.0
| |||||||||||||||||||||||||||||
| 101 | 6 | my ($self, $needs, %opts) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 6 | foreach my $backend (@BACKENDS) { | |||||||||||||||||||||||||||
| 104 | 18 | my $module = $self->_load_backend($BACKEND{$backend}, %opts); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 18 | ▶ | if ($needs->{api} eq 'verify') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 3 | ▶ | next if ! $module->has_verify_cmd(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||||||||
| 109 | 15 | ▶ | next if ! $module->has_backend_cmd(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 1 | ▶ | next if $needs->{keystore} && ! $module->has_keystore(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$needs->{"keystore"} and not $module->has_keystore
MC/DC0%0/2
$needs->{"keystore"} and not $module->has_keystore
$needs->{"keystore"}
not $module->has_keystore
Truth table33%1/3
A and not(B)
A= $needs->{"keystore"}B= not $module->has_keystore
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | return $module; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Otherwise load a dummy backend. | ||||||||||||||||||||||||||||
| 117 | 5 | return Dpkg::OpenPGP::Backend->new(); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 6 | ▶ | sub can_use_secrets { | ||||||||||||||||||||||||||
|
Subroutine
can_use_secrets: called CC=7 SCAR=36.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | 6 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 6 | ▶ | return 0 unless $self->{backend}->has_backend_cmd(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | return 0 if $key->type eq 'keyfile' && ! -f $key->handle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$key->type eq "keyfile" and not -f $key->handle
MC/DC0%0/2
$key->type eq "keyfile" and not -f $key->handle
$key->type eq "keyfile"
not -f $key->handle
Truth table0%0/3
A and not(B)
A= $key->type eq "keyfile"B= not -f $key->handle
| |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | return 0 if $key->type eq 'keystore' && ! -e $key->handle; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$key->type eq "keystore" and not -e $key->handle
MC/DC0%0/2
$key->type eq "keystore" and not -e $key->handle
$key->type eq "keystore"
not -e $key->handle
Truth table0%0/3
A and not(B)
A= $key->type eq "keystore"B= not -e $key->handle
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | return 0 unless $self->{backend}->can_use_key($key); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | return 1; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub get_trusted_keyrings { | ||||||||||||||||||||||||||
|
Subroutine
get_trusted_keyrings: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 131 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | return $self->{backend}->get_trusted_keyrings(); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | ▶ | sub armor { | ||||||||||||||||||||||||||
|
Subroutine
armor: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 137 | 2 | my ($self, $type, $in, $out) = @_; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 2 | return $self->{backend}->armor($type, $in, $out); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub dearmor { | ||||||||||||||||||||||||||
|
Subroutine
dearmor: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 0 | my ($self, $type, $in, $out) = @_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | return $self->{backend}->dearmor($type, $in, $out); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 9 | ▶ | sub inline_verify { | ||||||||||||||||||||||||||
|
Subroutine
inline_verify: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 149 | 9 | my ($self, $inlinesigned, $data, @certs) = @_; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 9 | ▶ | return OPENPGP_MISSING_KEYRINGS if @certs == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 9 | return $self->{backend}->inline_verify($inlinesigned, $data, @certs); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 5 | ▶ | sub verify { | ||||||||||||||||||||||||||
|
Subroutine
verify: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 157 | 5 | my ($self, $data, $sig, @certs) = @_; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 5 | ▶ | return OPENPGP_MISSING_KEYRINGS if @certs == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 5 | return $self->{backend}->verify($data, $sig, @certs); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | sub inline_sign { | ||||||||||||||||||||||||||
|
Subroutine
inline_sign: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 165 | 0 | my ($self, $data, $inlinesigned, $key) = @_; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | return $self->{backend}->inline_sign($data, $inlinesigned, $key); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 CHANGES | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head2 Version 0.xx | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | This is a private module. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 6 | 1; | |||||||||||||||||||||||||||