Dpkg/OpenPGP/Backend/GnuPG.pm

filter: Statementstmt 53.0% 95 / 179 Branchbran 23.7% 19 / 80 Conditioncond 4.7% 1 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 83.3% 25 / 30 Podpod 0.0% 0 / 9 totaltotal 42.3% 140 / 331 CC 50 SCAR 37.4
CC
50
Cov
41%
_file_is_librepgp
163.2
get_trusted_keyrings
56.0
has_keystore
42.0
CRAP 42.1
« Dpkg/Vendor/Devuan.pm ↑ Summary Dpkg/OpenPGP/Backend/Sequoia.pm »
1 # Copyright © 2007, 2022 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::GnuPG - OpenPGP backend for GnuPG
21  
22 =head1 DESCRIPTION
23  
24 This module provides a class that implements the OpenPGP backend
25 for GnuPG.
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::GnuPG 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::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 6 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 6 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 6 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶use Dpkg::IPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 6 ▶use Dpkg::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 6 ▶use Dpkg::Path qw(find_command);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 6 ▶use Dpkg::OpenPGP::ErrorCodes;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 6 ▶use parent qw(Dpkg::OpenPGP::Backend);
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 31 ▶sub DEFAULT_CMDV {
Subroutine DEFAULT_CMDV: called CC=1 SCAR=0.0
Pod undocumented
50 31 return [ qw(gpgv-sq gpgv) ];
51 }
52  
53 11 ▶sub DEFAULT_CMDSTORE {
Subroutine DEFAULT_CMDSTORE: called CC=1 SCAR=0.0
Pod undocumented
54 11 return [ qw(gpg-agent) ];
55 }
56  
57 11 ▶sub DEFAULT_CMD {
Subroutine DEFAULT_CMD: called CC=1 SCAR=0.0
Pod undocumented
58 11 return [ qw(gpg-sq gpg) ];
59 }
60  
61 0 ▶sub has_keystore {
Subroutine has_keystore: not called CC=6 SCAR=37.4
Pod undocumented
62 0 my $self = shift;
63  
64 0 ▶ return 0 if not defined $self->{cmdstore};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"cmdstore"} 0 0 0
65 return 1 if ($ENV{GNUPGHOME} && -e $ENV{GNUPGHOME}) ||
66 0 ▶ ($ENV{HOME} && -e "$ENV{HOME}/.gnupg");
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GNUPGHOME"} and -e $ENV{"GNUPGHOME"} or $ENV{"HOME"} and -e qq[$ENV{"HOME"}/.gnupg] 0 0 0
Condition0%0/9
$ENV{"GNUPGHOME"} and -e $ENV{"GNUPGHOME"} or $ENV{"HOME"} and -e qq[$ENV{"HOME"}/.gnupg]
l!l&&r!l&&!r
000
$ENV{"GNUPGHOME"} and -e $ENV{"GNUPGHOME"}
!ll&&!rl&&r
000
$ENV{"HOME"} and -e qq[$ENV{"HOME"}/.gnupg]
!ll&&!rl&&r
000
MC/DC0%0/4
$ENV{"GNUPGHOME"} and -e $ENV{"GNUPGHOME"} or $ENV{"HOME"} and -e qq[$ENV{"HOME"}/.gnupg]
$ENV{"GNUPGHOME"} -e $ENV{"GNUPGHOME"} $ENV{"HOME"} -e qq[$ENV{"HOME"}/.gnupg]
Truth table0%0/7
A and B or C and D
A= $ENV{"GNUPGHOME"}B= -e $ENV{"GNUPGHOME"}C= $ENV{"HOME"}D= -e qq[$ENV{"HOME"}/.gnupg]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
67 0 return 0;
68 }
69  
70 0 ▶sub can_use_key {
Subroutine can_use_key: not called CC=1 SCAR=6.9
Pod undocumented
71 0 my ($self, $key) = @_;
72  
73 # With gpg, a secret key always requires gpg-agent (the key store).
74 0 return $self->has_keystore();
75 }
76  
77 0 ▶sub get_trusted_keyrings {
Subroutine get_trusted_keyrings: not called CC=7 SCAR=40.3
Pod undocumented
78 0 my $self = shift;
79  
80 0 my $keystore;
81 0 ▶ if ($ENV{GNUPGHOME} && -e $ENV{GNUPGHOME}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'GNUPGHOME'} and -e $ENV{'GNUPGHOME'}) { } 0 0 0
elsif ($ENV{'HOME'} and -e "$ENV{'HOME'}/.gnupg") { } 0 0 0
Condition0%0/3
$ENV{"GNUPGHOME"} and -e $ENV{"GNUPGHOME"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"GNUPGHOME"} and -e $ENV{"GNUPGHOME"}
$ENV{"GNUPGHOME"} -e $ENV{"GNUPGHOME"}
Truth table0%0/3
A and B
A= $ENV{"GNUPGHOME"}B= -e $ENV{"GNUPGHOME"}
ABresult
0X0
100
111
82 0 $keystore = $ENV{GNUPGHOME};
83 ▶ } elsif ($ENV{HOME} && -e "$ENV{HOME}/.gnupg") {
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
84 0 $keystore = "$ENV{HOME}/.gnupg";
85 } else {
86 0 return;
87 }
88  
89 0 my @keyrings;
90 0 foreach my $keyring (qw(trustedkeys.gpg trustedkeys.kbx)) {
91 0 ▶ push @keyrings, "$keystore/$keyring" if -r "$keystore/$keyring";
Branch: 1 branch
BranchTrueFalseTotal
if -r "$keystore/$keyring" 0 0 0
92 }
93 0 return @keyrings;
94 }
95  
96 sub _gpg_exec
97 14 ▶{
Subroutine _gpg_exec: called CC=3 SCAR=11.8
98 14 my ($self, @exec) = @_;
99  
100 14 my ($stdout, $stderr);
101 14 spawn(
102 exec => \@exec,
103 wait_child => 1,
104 no_check => 1,
105 timeout => 10,
106 to_string => \$stdout,
107 error_to_string => \$stderr,
108 );
109 10 ▶ if (WIFEXITED($?)) {
Branch: 1 branch
BranchTrueFalseTotal
if (WIFEXITED($?)) { } 10 0 10
110 10 my $status = WEXITSTATUS($?);
111 0 ▶ print { *STDERR } "$stdout$stderr" if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 10 10
112 10 return $status;
113 } else {
114 0 subprocerr("@exec");
115 }
116 }
117  
118 14 ▶sub _gpg_options_weak_digests {
Subroutine _gpg_options_weak_digests: called CC=1 SCAR=0.0
119 my @gpg_weak_digests = map {
120 14 (qw(--weak-digest), $_)
121 } qw(SHA1 RIPEMD160);
122  
123 14 return @gpg_weak_digests;
124 }
125  
126 14 ▶sub _file_read_header($file, $size)
Subroutine _file_read_header: called CC=5 SCAR=19.1
127 {
128 14 my $header;
129  
130 14 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or syserr(g_("cannot open %s"), $file) 14 0 14
131 or syserr(g_('cannot open %s'), $file);
132 14 my $rc = read $fh, $header, $size;
133 14 ▶ if (! defined $rc) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $rc) { } 0 14 14
elsif ($rc == 0) { } 0 14 14
elsif ($rc != $size) { } 0 14 14
134 0 syserr(g_('cannot read %s'), $file);
135 } elsif ($rc == 0) {
136 0 error(g_('unexpected empty file %s'), $file);
137 } elsif ($rc != $size) {
138 0 error(g_('partial read %s (%d bytes of %d total)'), $file, $rc, $size);
139 }
140 14 close $fh;
141  
142 14 return $header;
143 }
144  
145 14 ▶sub _file_is_keybox($file)
Subroutine _file_is_keybox: called CC=1 SCAR=0.0
146 {
147 14 my $header = _file_read_header($file, 32);
148  
149 14 my ($lead, $magic) = unpack 'a8a4', $header;
150  
151 14 return $magic eq 'KBXf';
152 }
153  
154 # https://www.rfc-editor.org/rfc/rfc9580.html#name-packet-headers
155 6 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
156 6 PKT_FORMAT_MASK => 0b11000000,
157 PKT_FORMAT_NEW => 0b11000000,
158 PKT_FORMAT_OLD => 0b10000000,
159  
160 PKT_OLD_LEN_TYPE_MASK => 0b11,
161 6 };
162  
163 # https://www.rfc-editor.org/rfc/rfc9580.html#name-packet-types
164 6 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
165 6 PKT_ID_SECKEY => 5,
166 PKT_ID_PUBKEY => 6,
167 6 };
168  
169 14 ▶sub _file_is_librepgp($file)
Subroutine _file_is_librepgp: called CC=13 SCAR=51.0
170 {
171 14 ▶ return 0 if -z $file;
Branch: 1 branch
BranchTrueFalseTotal
if -z $file 14 0 14
172  
173 0 my $header = _file_read_header($file, 32);
174  
175 0 my ($ctb, @bytes) = unpack 'C8', $header;
176 0 my ($length, $version);
177  
178 0 my $format = $ctb & PKT_FORMAT_MASK;
179 0 my $type_id;
180 0 ▶ if ($format == PKT_FORMAT_NEW) {
Branch: 2 branches
BranchTrueFalseTotal
if ($format == 192) { } 0 0 0
elsif ($format == 128) { } 0 0 0
181 0 $type_id = $ctb ^ $format;
182 0 ▶ if ($bytes[0] < 192) {
Branch: 4 branches
BranchTrueFalseTotal
if ($bytes[0] < 192) { } 0 0 0
elsif ($bytes[0] < 224) { } 0 0 0
elsif ($bytes[0] < 255) { } 0 0 0
elsif ($bytes[0] == 255) { } 0 0 0
183 0 $length = 1;
184 } elsif ($bytes[0] < 224) {
185 0 $length = 2;
186 } elsif ($bytes[0] < 255) {
187 0 $length = 1;
188 } elsif ($bytes[0] == 255) {
189 0 $length = 5;
190 }
191 } elsif ($format == PKT_FORMAT_OLD) {
192 0 $type_id = ($ctb ^ $format) >> 2;
193 0 $length = $ctb & PKT_OLD_LEN_TYPE_MASK;
194 0 ▶ if ($length == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($length == 0) { } 0 0 0
elsif ($length == 1) { } 0 0 0
elsif ($length == 2) { } 0 0 0
195 0 $length = 1;
196 } elsif ($length == 1) {
197 0 $length = 2;
198 } elsif ($length == 2) {
199 0 $length = 4;
200 } else {
201 0 warning(g_('old OpenPGP packet in %s with indeterminate length'),
202 $file);
203 0 return 0;
204 }
205 }
206  
207 0 ▶ if ($type_id == PKT_ID_SECKEY || $type_id == PKT_ID_PUBKEY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_id == 5 or $type_id == 6) { } 0 0 0
Condition0%0/3
$type_id == 5 or $type_id == 6
l!l&&r!l&&!r
000
MC/DC0%0/2
$type_id == 5 or $type_id == 6
$type_id == 5 $type_id == 6
Truth table0%0/3
A or B
A= $type_id == 5B= $type_id == 6
ABresult
1X1
011
000
208 0 $version = $bytes[$length];
209 } else {
210 0 warning(g_('unknown OpenPGP packet type %0x in %s'), $ctb, $file);
211 0 return 0;
212 }
213  
214 0 debug(1, 'pgp-packet file=%s ctb=%0x format=%0b id=%d len=%0x version=%0x',
215 $file, $ctb, $format, $type_id, $length, $version);
216  
217 0 return $version == 5;
218 }
219  
220 14 ▶sub _gpg_verify {
Subroutine _gpg_verify: called CC=12 SCAR=28.0
221 14 my ($self, $signeddata, $sig, $data, @certs) = @_;
222  
223 14 ▶ return OPENPGP_MISSING_CMD if ! $self->has_verify_cmd();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_verify_cmd 0 14 14
224  
225 14 my $gpg_home = File::Temp->newdir(
226 TEMPLATE => 'dpkg-gpg-verify.XXXXXXXX',
227 TMPDIR => 1,
228 );
229  
230 14 my @exec;
231 14 ▶ if ($self->{cmdv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cmdv'}) { } 14 0 14
232 14 push @exec, $self->{cmdv};
233 # We need to touch the trustedkeys.gpg keyring, otherwise gpgv will
234 # emit an error about the trustedkeys.kbx file being of unknown type.
235 14 file_touch("$gpg_home/trustedkeys.gpg");
236 } else {
237 0 push @exec, $self->{cmd};
238 0 push @exec, qw(--no-options --no-default-keyring --batch --quiet);
239 }
240 14 push @exec, _gpg_options_weak_digests();
241 14 push @exec, '--homedir', $gpg_home;
242 14 foreach my $cert (@certs) {
243 14 my $certring = File::Temp->new(
244 UNLINK => 1,
245 SUFFIX => '.pgp',
246 );
247 14 my $rc;
248 14 ▶ if ($cert =~ m{\.kbx$} || _file_is_keybox($cert)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cert =~ /\.kbx$/u or _file_is_keybox($cert)) { } 0 14 14
Condition33%1/3
$cert =~ /\.kbx$/u or _file_is_keybox($cert)
l!l&&r!l&&!r
0014
MC/DC0%0/2
$cert =~ /\.kbx$/u or _file_is_keybox($cert)
$cert =~ /\.kbx$/u _file_is_keybox($cert)
Truth table33%1/3
A or B
A= $cert =~ /\.kbx$/uB= _file_is_keybox($cert)
ABresult
1X1
011
000
249 # Accept GnuPG apparent or real keybox-format keyrings as-is, but
250 # warn that they are deprecated.
251 0 $rc = 1;
252 0 warning(g_('using GnuPG specific KeyBox formatted keyring %s is deprecated; ' .
253 'use an OpenPGP formatted keyring instead'),
254 $cert);
255 } else {
256 # Note that these _pgp_* functions are only necessary while
257 # relying on gpgv, and gpgv itself does not verify multiple
258 # signatures correctly (see https://bugs.debian.org/1010955).
259 14 $rc = $self->dearmor('PUBLIC KEY BLOCK', $cert, $certring);
260  
261 14 ▶ if (_file_is_librepgp($certring)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_file_is_librepgp($certring)) 0 14 14
262 0 warning(g_('found non-interoperable LibrePGP artifact in %s, ' .
263 'only OpenPGP is supported'), $cert);
264 }
265 }
266 14 ▶ $certring = $cert if $rc;
Branch: 1 branch
BranchTrueFalseTotal
if $rc 0 14 14
267 14 push @exec, '--keyring', $certring;
268 }
269 14 ▶ push @exec, '--output', $data if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 14 14
270 14 ▶ if (! $self->{cmdv}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"cmdv"}) 0 14 14
271 0 push @exec, '--verify';
272 }
273 14 ▶ push @exec, $sig if defined $sig;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sig 5 9 14
274 14 push @exec, $signeddata;
275  
276 14 my $rc = $self->_gpg_exec(@exec);
277 10 ▶ return OPENPGP_NO_SIG if $rc;
Branch: 1 branch
BranchTrueFalseTotal
if $rc 0 10 10
278 10 return OPENPGP_OK;
279 }
280  
281 9 ▶sub inline_verify {
Subroutine inline_verify: called CC=2 SCAR=7.1
Pod undocumented
282 9 my ($self, $inlinesigned, $data, @certs) = @_;
283  
284 9 ▶ return OPENPGP_MISSING_KEYRINGS if @certs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @certs == 0 0 9 9
285  
286 9 return $self->_gpg_verify($inlinesigned, undef, $data, @certs);
287 }
288  
289 5 ▶sub verify {
Subroutine verify: called CC=2 SCAR=7.1
Pod undocumented
290 5 my ($self, $data, $sig, @certs) = @_;
291  
292 5 ▶ return OPENPGP_MISSING_KEYRINGS if @certs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @certs == 0 0 5 5
293  
294 5 return $self->_gpg_verify($data, $sig, undef, @certs);
295 }
296  
297 0 ▶sub _gpg_fixup_newline {
Subroutine _gpg_fixup_newline: not called CC=3 SCAR=24.8
298 0 my $origfile = shift;
299  
300 0 my $signdir = File::Temp->newdir(
301 TEMPLATE => 'dpkg-sign.XXXXXXXX',
302 TMPDIR => 1,
303 );
304 0 my $signfile = $signdir . q(/) . basename($origfile);
305  
306 0 copy($origfile, $signfile);
307  
308 # Make sure the file to sign ends with a newline, as GnuPG does not adhere
309 # to the OpenPGP specification (see <https://dev.gnupg.org/T7106>).
310 0 ▶ open my $signfh, '>>', $signfile
Branch: 1 branch
BranchTrueFalseTotal
open my $signfh, ">>", $signfile or syserr(g_("cannot open %s"), $signfile) 0 0 0
311 or syserr(g_('cannot open %s'), $signfile);
312 0 print { $signfh } "\n";
313 0 ▶ close $signfh
Branch: 1 branch
BranchTrueFalseTotal
close $signfh or syserr(g_("cannot close %s"), $signfile) 0 0 0
314 or syserr(g_('cannot close %s'), $signfile);
315  
316 # Return the dir object so that it is kept in scope in the caller, and
317 # thus not cleaned up within this function.
318 0 return ($signdir, $signfile);
319 }
320  
321 0 ▶sub inline_sign {
Subroutine inline_sign: not called CC=6 SCAR=37.4
Pod undocumented
322 0 my ($self, $data, $inlinesigned, $key) = @_;
323  
324 0 ▶ return OPENPGP_MISSING_CMD if ! $self->has_backend_cmd();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_backend_cmd 0 0 0
325  
326 0 my ($signdir, $signfile);
327 0 ▶ if ($self->{cmd} !~ m{/gpg-sq$}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'cmd'} =~ m[/gpg-sq$]u) { } 0 0 0
328 0 ($signdir, $signfile) = _gpg_fixup_newline($data);
329 } else {
330 0 $signfile = $data;
331 }
332  
333 0 my @exec = ($self->{cmd});
334 0 push @exec, _gpg_options_weak_digests();
335 0 push @exec, qw(--utf8-strings --textmode --armor);
336 # Set conformance level.
337 0 push @exec, '--openpgp';
338 # Set secure algorithm preferences.
339 0 push @exec, '--personal-digest-preferences', 'SHA512 SHA384 SHA256 SHA224';
340 0 ▶ if ($key->type eq 'keyfile') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key->type eq 'keyfile') { } 0 0 0
elsif ($key->type eq 'keystore') { } 0 0 0
341 # Promote the keyfile keyhandle to a keystore, this way we share the
342 # same gpg-agent and can get any password cached.
343 0 my $gpg_home = File::Temp->newdir(
344 TEMPLATE => 'dpkg-sign.XXXXXXXX',
345 TMPDIR => 1,
346 );
347  
348 0 push @exec, '--homedir', $gpg_home;
349 0 $self->_gpg_exec(@exec, qw(--quiet --no-tty --batch --import), $key->handle);
350 0 $key->set('keystore', $gpg_home);
351 } elsif ($key->type eq 'keystore') {
352 0 push @exec, '--homedir', $key->handle;
353 } else {
354 0 push @exec, '--local-user', $key->handle;
355 }
356 0 push @exec, '--output', $inlinesigned;
357  
358 0 my $rc = $self->_gpg_exec(@exec, '--clearsign', $signfile);
359 0 ▶ return OPENPGP_CMD_CANNOT_SIGN if $rc;
Branch: 1 branch
BranchTrueFalseTotal
if $rc 0 0 0
360 0 return OPENPGP_OK;
361 }
362  
363 =head1 CHANGES
364  
365 =head2 Version 0.xx
366  
367 This is a private module.
368  
369 =cut
370  
371 6 1;
« Dpkg/Vendor/Devuan.pm ↑ Summary Dpkg/OpenPGP/Backend/Sequoia.pm »