Dpkg/OpenPGP/Backend.pm

filter: Statementstmt 83.3% 90 / 108 Branchbran 54.1% 13 / 24 Conditioncond 75.0% 9 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 77.7% 21 / 27 Podpod 0.0% 0 / 14 totaltotal 70.9% 137 / 193 CC 19 SCAR 10.7
CC
19
Cov
78%
can_use_key
6.0
inline_verify
6.0
verify
6.0
CRAP 2.9
« Dpkg/Package.pm ↑ Summary Dpkg/Shlibs/Cppfilt.pm »
1 # Copyright © 2017, 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 - OpenPGP backend base class
21  
22 =head1 DESCRIPTION
23  
24 This module provides an OpenPGP backend base class that specific
25 implementations should inherit from.
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 0.01;
32  
33 6 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 6 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 6 ▶use MIME::Base64;
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::Path qw(find_command);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use Dpkg::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶use Dpkg::OpenPGP::ErrorCodes;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 10 ▶sub DEFAULT_CMDV {
Subroutine DEFAULT_CMDV: called CC=1 SCAR=0.0
Pod undocumented
45 10 return [];
46 }
47  
48 17 ▶sub DEFAULT_CMDSTORE {
Subroutine DEFAULT_CMDSTORE: called CC=1 SCAR=0.0
Pod undocumented
49 17 return [];
50 }
51  
52 5 ▶sub DEFAULT_CMD {
Subroutine DEFAULT_CMD: called CC=1 SCAR=0.0
Pod undocumented
53 5 return [];
54 }
55  
56 84 ▶sub _detect_cmd {
Subroutine _detect_cmd: called CC=4 SCAR=14.0
57 84 my ($cmd, $default) = @_;
58  
59 84 ▶ if (! defined $cmd || $cmd eq 'auto') {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $cmd or $cmd eq 'auto') { } 79 5 84
elsif ($cmd eq 'none') { } 0 5 5
Condition100%3/3
not defined $cmd or $cmd eq "auto"
l!l&&r!l&&!r
38415
MC/DC100%2/2
not defined $cmd or $cmd eq "auto"
not defined $cmd $cmd eq "auto"
Truth table100%3/3
not(A) or B
A= not defined $cmdB= $cmd eq "auto"
ABresult
1X1
011
000
60 79 ▶ return first { find_command($_) } @{$default};
Subroutine __ANON__: called CC=1 SCAR=0.0
61 } elsif ($cmd eq 'none') {
62 0 return;
63 } else {
64 5 return find_command($cmd);
65 }
66 }
67  
68 28 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
69 28 my ($this, %opts) = @_;
70 28 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0280
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
71  
72 28 my $self = {};
73 28 bless $self, $class;
74  
75 28 $self->{cmdv} = _detect_cmd($opts{cmdv}, $self->DEFAULT_CMDV());
76 28 $self->{cmdstore} = _detect_cmd($opts{cmdstore}, $self->DEFAULT_CMDSTORE());
77 28 $self->{cmd} = _detect_cmd($opts{cmd}, $self->DEFAULT_CMD());
78  
79 28 return $self;
80 }
81  
82 31 ▶sub has_backend_cmd {
Subroutine has_backend_cmd: called CC=1 SCAR=0.0
Pod undocumented
83 31 my $self = shift;
84  
85 31 return defined $self->{cmd};
86 }
87  
88 27 ▶sub has_verify_cmd {
Subroutine has_verify_cmd: called CC=4 SCAR=13.9
Pod undocumented
89 27 my $self = shift;
90  
91 27 ▶ return 1 if @{$self->DEFAULT_CMDV()} && defined $self->{cmdv};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->DEFAULT_CMDV;} and defined $self->{"cmdv"} 20 7 27
Condition100%3/3
@{$self->DEFAULT_CMDV;} and defined $self->{"cmdv"}
!ll&&!rl&&r
5220
MC/DC100%2/2
@{$self->DEFAULT_CMDV;} and defined $self->{"cmdv"}
@{$self->DEFAULT_CMDV;} defined $self->{"cmdv"}
Truth table100%3/3
A and B
A= @{$self->DEFAULT_CMDV;}B= defined $self->{"cmdv"}
ABresult
0X0
100
111
92 7 ▶ return 1 if defined $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"cmd"} 0 7 7
93 7 return 0;
94 }
95  
96 0 ▶sub has_keystore {
Subroutine has_keystore: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my $self = shift;
98  
99 0 return 0;
100 }
101  
102 0 ▶sub can_use_key {
Subroutine can_use_key: not called CC=2 SCAR=17.9
Pod undocumented
103 0 my ($self, $key) = @_;
104  
105 0 ▶ return $self->has_keystore() if $key->needs_keystore();
Branch: 1 branch
BranchTrueFalseTotal
if $key->needs_keystore 0 0 0
106 0 return 1;
107 }
108  
109 0 ▶sub get_trusted_keyrings {
Subroutine get_trusted_keyrings: not called CC=1 SCAR=6.9
Pod undocumented
110 0 my $self = shift;
111  
112 0 return ();
113 }
114  
115 # _pgp_* functions are strictly for applying or removing ASCII armor.
116 # See <https://www.rfc-editor.org/rfc/rfc9580.html#section-6> for more
117 # details.
118  
119 16 ▶sub _pgp_dearmor_data {
Subroutine _pgp_dearmor_data: called CC=3 SCAR=11.2
120 16 my ($type, $data, $filename) = @_;
121  
122 # Note that we ignore an incorrect or absent checksum, following the
123 # guidance of <https://www.rfc-editor.org/rfc/rfc9580.html>.
124 16 my $armor_regex = qr{
125 -----BEGIN\ PGP\ \Q$type\E-----[\r\t ]*\n
126 (?:[^:\n]+:\ [^\n]*[\r\t ]*\n)*
127 [\r\t ]*\n
128 ([a-zA-Z0-9/+\n]+={0,2})[\r\t ]*\n
129 (?:=[a-zA-Z0-9/+]{4}[\r\t ]*\n)?
130 -----END\ PGP\ \Q$type\E-----
131 }xm;
132  
133 16 my $blocks = 0;
134 16 my $binary;
135 16 ▶ while ($data =~ m/$armor_regex/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /$armor_regex/gu) 15 16 31
136 15 $binary .= decode_base64($1);
137 15 $blocks++;
138 }
139 16 ▶ if ($blocks > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocks > 1) 0 16 16
140 0 warning(g_('multiple concatenated ASCII Armor blocks in %s, ' .
141 'which is not an interoperable construct, see <%s>'),
142 $filename,
143 'https://tests.sequoia-pgp.org/results.html#ASCII_Armor');
144 0 hint('sq keyring merge --overwrite --output %s %s',
145 $filename, $filename);
146 }
147 16 return $binary;
148 }
149  
150 2 ▶sub _pgp_armor_checksum {
Subroutine _pgp_armor_checksum: called CC=4 SCAR=13.9
151 2 my ($data) = @_;
152  
153 # From RFC9580 <https://www.rfc-editor.org/rfc/rfc9580.html>.
154 #
155 # The resulting three-octet-wide value then gets base64-encoded into
156 # four base64 ASCII characters.
157  
158 ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
159 2 my $CRC24_INIT = 0xB704CE;
160 2 my $CRC24_GENERATOR = 0x864CFB;
161  
162 2 my @bytes = unpack 'C*', $data;
163 2 my $crc = $CRC24_INIT;
164 2 for my $b (@bytes) {
165 1132 $crc ^= ($b << 16);
166 1132 for (1 .. 8) {
167 9056 $crc <<= 1;
168 9056 ▶ if ($crc & 0x1000000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crc & 16777216) 4554 4502 9056
169 # Clear bit 25 to avoid overflow.
170 4554 $crc &= 0xffffff;
171 4554 $crc ^= $CRC24_GENERATOR;
172 }
173 }
174 }
175 2 my $sum = pack 'CCC', ($crc >> 16) & 0xff, ($crc >> 8) & 0xff, $crc & 0xff;
176 2 return encode_base64($sum, q{});
177 }
178  
179 2 ▶sub _pgp_armor_data {
Subroutine _pgp_armor_data: called CC=1 SCAR=0.0
180 2 my ($type, $data) = @_;
181  
182 2 my $out = encode_base64($data, q{}) =~ s/(.{1,64})/$1\n/gr;
183 2 chomp $out;
184 2 my $crc = _pgp_armor_checksum($data);
185 2 my $armor = <<~"ARMOR";
186 -----BEGIN PGP $type-----
187  
188 $out
189 =$crc
190 -----END PGP $type-----
191 ARMOR
192 2 return $armor;
193 }
194  
195 2 ▶sub armor {
Subroutine armor: called CC=3 SCAR=11.1
Pod undocumented
196 2 my ($self, $type, $in, $out) = @_;
197  
198 2 my $raw_data = file_slurp($in);
199 2 ▶ my $data = _pgp_dearmor_data($type, $raw_data, $in) // $raw_data;
Condition66%2/3
_pgp_dearmor_data($type, $raw_data, $in) // $raw_data
l!l&&r!l&&!r
110
MC/DC0%0/2
_pgp_dearmor_data($type, $raw_data, $in) // $raw_data
_pgp_dearmor_data($type, $raw_data, $in) $raw_data
Truth table66%2/3
A // B
A= _pgp_dearmor_data($type, $raw_data, $in)B= $raw_data
ABresult
1X1
011
000
200 2 my $armor = _pgp_armor_data($type, $data);
201 2 ▶ return OPENPGP_BAD_DATA unless defined $armor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $armor 0 2 2
202 2 file_dump($out, $armor);
203  
204 2 return OPENPGP_OK;
205 }
206  
207 14 ▶sub dearmor {
Subroutine dearmor: called CC=2 SCAR=7.0
Pod undocumented
208 14 my ($self, $type, $in, $out) = @_;
209  
210 14 my $armor = file_slurp($in);
211 14 my $data = _pgp_dearmor_data($type, $armor, $in);
212 14 ▶ return OPENPGP_BAD_DATA unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 14 14
213 14 file_dump($out, $data);
214  
215 14 return OPENPGP_OK;
216 }
217  
218 0 ▶sub inline_verify {
Subroutine inline_verify: not called CC=2 SCAR=17.9
Pod undocumented
219 0 my ($self, $inlinesigned, $data, @certs) = @_;
220  
221 0 ▶ return OPENPGP_MISSING_KEYRINGS if @certs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @certs == 0 0 0 0
222 0 return OPENPGP_UNSUPPORTED_SUBCMD;
223 }
224  
225 0 ▶sub verify {
Subroutine verify: not called CC=2 SCAR=17.9
Pod undocumented
226 0 my ($self, $data, $sig, @certs) = @_;
227  
228 0 ▶ return OPENPGP_MISSING_KEYRINGS if @certs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @certs == 0 0 0 0
229 0 return OPENPGP_UNSUPPORTED_SUBCMD;
230 }
231  
232 0 ▶sub inline_sign {
Subroutine inline_sign: not called CC=1 SCAR=6.9
Pod undocumented
233 0 my ($self, $data, $inlinesigned, $key) = @_;
234  
235 0 return OPENPGP_UNSUPPORTED_SUBCMD;
236 }
237  
238 =head1 CHANGES
239  
240 =head2 Version 0.xx
241  
242 This is a private module.
243  
244 =cut
245  
246 6 1;
« Dpkg/Package.pm ↑ Summary Dpkg/Shlibs/Cppfilt.pm »