Dpkg/OpenPGP/Backend/Sequoia.pm

filter: Statementstmt 25.4% 26 / 102 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 52.6% 10 / 19 Podpod 0.0% 0 / 8 totaltotal 17.9% 36 / 201 CC 31 SCAR 35.7
CC
31
Cov
16%
inline_verify
90.0
verify
56.0
inline_sign
42.0
CRAP 35.4
« Dpkg/OpenPGP/Backend/GnuPG.pm ↑ Summary Dpkg/Control.pm »
1 # Copyright © 2021-2025 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::Backend::Sequoia - OpenPGP backend for Sequoia
21  
22 =head1 DESCRIPTION
23  
24 This module provides a class that implements the OpenPGP backend
25 for Sequoia-PGP.
26  
27 B<Note>: This is a private module, its API can change at any time.
28  
29 =cut
30  
31 package Dpkg::OpenPGP::Backend::Sequoia 0.01;
32  
33 6 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 6 ▶use POSIX qw(:sys_wait_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 6 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 6 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 6 ▶use Dpkg::IPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use Dpkg::OpenPGP::ErrorCodes;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 6 ▶use parent qw(Dpkg::OpenPGP::Backend);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 7 ▶sub DEFAULT_CMDV {
Subroutine DEFAULT_CMDV: called CC=1 SCAR=0.0
Pod undocumented
46 7 return [ qw(sqv) ];
47 }
48  
49 6 ▶sub DEFAULT_CMD {
Subroutine DEFAULT_CMD: called CC=1 SCAR=0.0
Pod undocumented
50 6 return [ qw(sq) ];
51 }
52  
53 0 ▶sub has_keystore {
Subroutine has_keystore: not called CC=5 SCAR=34.0
Pod undocumented
54 0 my $self = shift;
55  
56 0 ▶ return 1 if $ENV{HOME} && -e "$ENV{HOME}/.local/share/sequoia/keystore";
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HOME"} and -e qq[$ENV{"HOME"}/.local/share/sequoia/keystore] 0 0 0
Condition0%0/3
$ENV{"HOME"} and -e qq[$ENV{"HOME"}/.local/share/sequoia/keystore]
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"HOME"} and -e qq[$ENV{"HOME"}/.local/share/sequoia/keystore]
$ENV{"HOME"} -e qq[$ENV{"HOME"}/.local/share/sequoia/keystore]
Truth table0%0/3
A and B
A= $ENV{"HOME"}B= -e qq[$ENV{"HOME"}/.local/share/sequoia/keystore]
ABresult
0X0
100
111
57 0 ▶ return 1 if $ENV{HOME} && -e "$ENV{HOME}/.gnupg";
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HOME"} and -e qq[$ENV{"HOME"}/.gnupg] 0 0 0
Condition0%0/3
$ENV{"HOME"} and -e qq[$ENV{"HOME"}/.gnupg]
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"HOME"} and -e qq[$ENV{"HOME"}/.gnupg]
$ENV{"HOME"} -e qq[$ENV{"HOME"}/.gnupg]
Truth table0%0/3
A and B
A= $ENV{"HOME"}B= -e qq[$ENV{"HOME"}/.gnupg]
ABresult
0X0
100
111
58 0 return 0;
59 }
60  
61 sub _sq_exec
62 0 ▶{
Subroutine _sq_exec: not called CC=3 SCAR=24.8
63 0 my ($self, $cmd, @exec) = @_;
64  
65 0 my ($stdout, $stderr);
66 0 spawn(
67 exec => [ $cmd, @exec ],
68 wait_child => 1,
69 no_check => 1,
70 timeout => 10,
71 to_string => \$stdout,
72 error_to_string => \$stderr,
73 );
74 0 ▶ if (WIFEXITED($?)) {
Branch: 1 branch
BranchTrueFalseTotal
if (WIFEXITED($?)) { } 0 0 0
75 0 my $status = WEXITSTATUS($?);
76 0 ▶ print { *STDERR } "$stdout$stderr" if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 0 0
77 0 return $status;
78 } else {
79 0 subprocerr("$cmd @exec");
80 }
81 }
82  
83 sub _sq_exec_cmdv
84 0 ▶{
Subroutine _sq_exec_cmdv: not called CC=2 SCAR=17.9
85 0 my ($self, @exec) = @_;
86  
87 0 ▶ if ($self->{cmdv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cmdv'}) { } 0 0 0
88 0 return $self->_sq_exec($self->{cmdv}, @exec);
89 } else {
90 0 return $self->_sq_exec($self->{cmd}, qw(verify), @exec);
91 }
92 }
93  
94 sub _sq_exec_cmd
95 0 ▶{
Subroutine _sq_exec_cmd: not called CC=1 SCAR=6.9
96 0 my ($self, @exec) = @_;
97  
98 0 return $self->_sq_exec($self->{cmd}, @exec);
99 }
100  
101 sub armor
102 0 ▶{
Subroutine armor: not called CC=3 SCAR=24.8
Pod undocumented
103 0 my ($self, $type, $in, $out) = @_;
104  
105 0 ▶ return $self->SUPER::armor($type, $in, $out) unless $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
106  
107 # We ignore the $type, and let "sq" handle this automatically.
108 0 my $rc = $self->_sq_exec_cmd(qw(packet armor --output), $out, $in);
109 0 ▶ return OPENPGP_BAD_DATA if $rc;
Branch: 1 branch
BranchTrueFalseTotal
if $rc 0 0 0
110 0 return OPENPGP_OK;
111 }
112  
113 sub dearmor
114 0 ▶{
Subroutine dearmor: not called CC=3 SCAR=24.8
Pod undocumented
115 0 my ($self, $type, $in, $out) = @_;
116  
117 0 ▶ return $self->SUPER::dearmor($type, $in, $out) unless $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
118  
119 # We ignore the $type, and let "sq" handle this automatically.
120 0 my $rc = $self->_sq_exec_cmd(qw(packet dearmor --output), $out, $in);
121 0 ▶ return OPENPGP_BAD_DATA if $rc;
Branch: 1 branch
BranchTrueFalseTotal
if $rc 0 0 0
122 0 return OPENPGP_OK;
123 }
124  
125 sub inline_verify
126 0 ▶{
Subroutine inline_verify: not called CC=9 SCAR=45.0
Pod undocumented
127 0 my ($self, $inlinesigned, $data, @certs) = @_;
128  
129 0 ▶ return OPENPGP_MISSING_CMD unless ($self->{cmdv} || $self->{cmd});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmdv"} or $self->{"cmd"} 0 0 0
Condition0%0/3
$self->{"cmdv"} or $self->{"cmd"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"cmdv"} or $self->{"cmd"}
$self->{"cmdv"} $self->{"cmd"}
Truth table0%0/3
A or B
A= $self->{"cmdv"}B= $self->{"cmd"}
ABresult
1X1
011
000
130 0 ▶ return OPENPGP_MISSING_KEYRINGS if @certs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @certs == 0 0 0 0
131  
132 # XXX: sqv does not support --signer-file. See:
133 # <https://gitlab.com/sequoia-pgp/sequoia-sqv/-/issues/11>.
134 0 ▶ my $keyring_opt = $self->{cmdv} ? '--keyring' : '--signer-file';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cmdv'} ? : 0 0 0
135  
136 0 my @opts;
137 # Select stateless mode for sq.
138 0 ▶ push @opts, '--home=none' unless $self->{cmdv};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmdv"} 0 0 0
139 0 push @opts, '--cleartext';
140 0 push @opts, map { ($keyring_opt, $_) } @certs;
141 0 my $tmpdir;
142 0 ▶ if (not defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
143 # XXX: For sqv the --output option is mandatory. See:
144 # <https://gitlab.com/sequoia-pgp/sequoia-sqv/-/issues/12>.
145 # XXX: For sqv the --output option does not accept «-» as stdout,
146 # which we would discard at spawn() time, and would then not need
147 # to pass /dev/null or use a temporary file. See:
148 # <https://gitlab.com/sequoia-pgp/sequoia-sqv/-/issues/15>.
149 # XXX: We need a temporary file because we cannot pass /dev/null. See:
150 # <https://gitlab.com/sequoia-pgp/sequoia-sq/-/issues/561> and
151 # <https://gitlab.com/sequoia-pgp/sequoia-sqv/-/issues/13>.
152 0 ▶ if ($self->{cmdv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cmdv'}) { } 0 0 0
153 0 $tmpdir = File::Temp->newdir(
154 TEMPLATE => 'dpkg-openpgp-backend-sq-verify-XXXXXX',
155 TMPDIR => 1,
156 );
157 0 $data = "$tmpdir/output";
158 } else {
159 0 $data = '-';
160 }
161 }
162 0 push @opts, '--output', $data;
163  
164 0 my $rc = $self->_sq_exec_cmdv(@opts, $inlinesigned);
165 0 ▶ return OPENPGP_NO_SIG if $rc;
Branch: 1 branch
BranchTrueFalseTotal
if $rc 0 0 0
166 0 return OPENPGP_OK;
167 }
168  
169 sub verify
170 0 ▶{
Subroutine verify: not called CC=7 SCAR=40.3
Pod undocumented
171 0 my ($self, $data, $sig, @certs) = @_;
172  
173 0 ▶ return OPENPGP_MISSING_CMD unless ($self->{cmdv} || $self->{cmd});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmdv"} or $self->{"cmd"} 0 0 0
Condition0%0/3
$self->{"cmdv"} or $self->{"cmd"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"cmdv"} or $self->{"cmd"}
$self->{"cmdv"} $self->{"cmd"}
Truth table0%0/3
A or B
A= $self->{"cmdv"}B= $self->{"cmd"}
ABresult
1X1
011
000
174 0 ▶ return OPENPGP_MISSING_KEYRINGS if @certs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @certs == 0 0 0 0
175  
176 # XXX: sqv does not support --signer-file. See:
177 # <https://gitlab.com/sequoia-pgp/sequoia-sqv/-/issues/11>.
178 0 ▶ my $keyring_opt = $self->{cmdv} ? '--keyring' : '--signer-file';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cmdv'} ? : 0 0 0
179  
180 0 my @opts;
181 # Select stateless mode for sq.
182 0 ▶ push @opts, '--home=none' unless $self->{cmdv};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmdv"} 0 0 0
183 0 push @opts, map { ($keyring_opt, $_) } @certs;
184 0 push @opts, '--signature-file', $sig;
185  
186 0 my $rc = $self->_sq_exec_cmdv(@opts, $data);
187 0 ▶ return OPENPGP_NO_SIG if $rc;
Branch: 1 branch
BranchTrueFalseTotal
if $rc 0 0 0
188 0 return OPENPGP_OK;
189 }
190  
191 sub inline_sign
192 0 ▶{
Subroutine inline_sign: not called CC=6 SCAR=37.4
Pod undocumented
193 0 my ($self, $data, $inlinesigned, $key) = @_;
194  
195 0 ▶ return OPENPGP_MISSING_CMD unless $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
196 0 ▶ return OPENPGP_NEEDS_KEYSTORE if ! $self->can_use_key($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can_use_key($key) 0 0 0
197  
198 0 my @opts;
199 0 push @opts, '--cleartext';
200 0 ▶ if ($key->type eq 'keyfile') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key->type eq 'keyfile') { } 0 0 0
elsif ($key->type eq 'userid') { } 0 0 0
201 0 push @opts, '--signer-file', $key->handle;
202 } elsif ($key->type eq 'userid') {
203 0 push @opts, '--signer-userid', $key->handle;
204 } else {
205 0 push @opts, '--signer', $key->handle;
206 }
207 0 push @opts, '--output', $inlinesigned;
208  
209 0 my $rc = $self->_sq_exec_cmd('sign', @opts, $data);
210 0 ▶ if ($rc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 0 0 0
211 # XXX: Ideally sq would emit this kind of hint itself when it knows it
212 # might apply, but for now this is probably better than nothing.
213 0 hint(g_('imported own keys might be missing ownership information, try:'));
214 0 hint(g_(' sq pki link authorize --cert FINGERPRINT --all --unconstrained'));
215 0 return OPENPGP_KEY_CANNOT_SIGN;
216 }
217 0 return OPENPGP_OK;
218 }
219  
220 =head1 CHANGES
221  
222 =head2 Version 0.xx
223  
224 This is a private module.
225  
226 =cut
227  
228 6 1;
« Dpkg/OpenPGP/Backend/GnuPG.pm ↑ Summary Dpkg/Control.pm »