OpenPGP.pm

filter: Statementstmt 62.2% 274 / 440 Branchbran 32.7% 108 / 330 Conditioncond 32.9% 29 / 88 MC/DCmcdc 5.6% 3 / 53 Subroutinesub 91.1% 31 / 34 Podpod 58.3% 7 / 12 totaltotal 47.2% 452 / 957 CC 208 SCAR 55.3
CC
208
Cov
48%
encrypt
497.4
sign
474.7
decrypt
408.8
CRAP 251.8
↑ Summary OpenPGP/Certificate.pm »
1 package Crypt::OpenPGP;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 11 our $VERSION = '1.21'; # VERSION
6  
7 11 ▶use Crypt::OpenPGP::Constants qw( DEFAULT_CIPHER );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Crypt::OpenPGP::KeyRing;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Crypt::OpenPGP::Plaintext;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Crypt::OpenPGP::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Crypt::OpenPGP::PacketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Crypt::OpenPGP::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Crypt::OpenPGP::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 ▶use parent qw( Crypt::OpenPGP::ErrorHandler );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 11 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 11 our %COMPAT;
21  
22 ## pgp2 and pgp5 do not trim trailing whitespace from "canonical text"
23 ## signatures, only from cleartext signatures.
24 ## See:
25 ## http://cert.uni-stuttgart.de/archive/ietf-openpgp/2000/01/msg00033.html
26 11 $Crypt::OpenPGP::Globals::Trim_trailing_ws = 1;
27  
28 {
29 11 ▶ my $env = sub {
Subroutine __ANON__: called CC=4 SCAR=15.6
30 99 my $dir = shift; my @paths;
31 0 ▶ if (exists $ENV{$dir}) { for (@_) { push @paths, "$ENV{$dir}/$_" } }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{$dir}) 0 99 99
32 99 ▶ return @paths ? @paths : ();
Branch: 1 branch
BranchTrueFalseTotal
@paths ? : 0 99 99
33 11 };
34  
35 99 ▶ my $home = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
36 99 my( @path ) = @_;
37 99 ▶ my $home_dir = File::HomeDir->my_home or return;
Branch: 1 branch
BranchTrueFalseTotal
my $home_dir = "File::HomeDir"->my_home or return 99 0 99
38 99 return File::Spec->catfile( $home_dir, @path );
39 11 };
40  
41 11 %COMPAT = (
42 PGP2 => {
43 'sign' => { Digest => 'MD5', Version => 3 },
44 'encrypt' => { Cipher => 'IDEA', Compress => 'ZIP' },
45 'keygen' => { Type => 'RSA', Cipher => 'IDEA',
46 Version => 3, Digest => 'MD5' },
47 'PubRing' => [
48 $env->('PGPPATH','pubring.pgp'),
49 $home->( '.pgp', 'pubring.pgp' ),
50 ],
51 'SecRing' => [
52 $env->('PGPPATH','secring.pgp'),
53 $home->( '.pgp', 'secring.pgp' ),
54 ],
55 'Config' => [
56 $env->('PGPPATH', 'config.txt'),
57 $home->( '.pgp', 'config.txt' ),
58 ],
59 },
60  
61 PGP5 => {
62 'sign' => { Digest => 'SHA1', Version => 3 },
63 'encrypt' => { Cipher => 'DES3', Compress => 'ZIP' },
64 'keygen' => { Type => 'DSA', Cipher => 'DES3',
65 Version => 4, Digest => 'SHA1' },
66 'PubRing' => [
67 $env->('PGPPATH','pubring.pkr'),
68 $home->( '.pgp', 'pubring.pkr' ),
69 ],
70 'SecRing' => [
71 $env->('PGPPATH','secring.skr'),
72 $home->( '.pgp', 'secring.skr' ),
73 ],
74 'Config' => [
75 $env->('PGPPATH', 'pgp.cfg'),
76 $home->( '.pgp', 'pgp.cfg' ),
77 ],
78 },
79  
80 GnuPG => {
81 'sign' => { Digest => 'SHA256', Version => 4 },
82 'encrypt' => { Cipher => 'Rijndael', Compress => 'Zlib',
83 MDC => 1 },
84 'keygen' => { Type => 'RSA', Cipher => 'Rijndael',
85 Version => 4, Digest => 'SHA256' },
86 'Config' => [
87 $env->('GNUPGHOME', 'options'),
88 $home->( '.gnupg', 'options' ),
89 ],
90 'PubRing' => [
91 $env->('GNUPGHOME', 'pubring.gpg'),
92 $home->( '.gnupg', 'pubring.gpg' ),
93 ],
94 'SecRing' => [
95 $env->('GNUPGHOME', 'secring.gpg'),
96 $home->( '.gnupg', 'secring.gpg' ),
97 ],
98 },
99 );
100 }
101  
102 5 ▶sub version_string {
Subroutine version_string: called CC=1 SCAR=0.0
Pod undocumented
103 11 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 5 __PACKAGE__ . ' ' . __PACKAGE__->VERSION;
105 }
106  
107 2 ▶sub pubrings { $_[0]->{pubrings} }
Subroutine pubrings: called CC=1 SCAR=0.0
Pod undocumented
108 3 ▶sub secrings { $_[0]->{secrings} }
Subroutine secrings: called CC=1 SCAR=0.0
Pod undocumented
109  
110 11 ▶use constant PUBLIC => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 11 ▶use constant SECRET => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 15 ▶sub add_ring {
Subroutine add_ring: called CC=4 SCAR=13.9
Pod undocumented
114 15 my $pgp = shift;
115 15 my($type, $ring) = @_;
116 15 ▶ unless (ref($ring) eq 'Crypt::OpenPGP::KeyRing') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ring eq "Crypt::OpenPGP::KeyRing") 15 0 15
117 15 ▶ $ring = Crypt::OpenPGP::KeyRing->new( Filename => $ring )
Branch: 1 branch
BranchTrueFalseTotal
$ring = "Crypt::OpenPGP::KeyRing"->new("Filename", $ring) or return "Crypt::OpenPGP::KeyRing"->errstr 14 1 15
118 or return Crypt::OpenPGP::KeyRing->errstr;
119 }
120 14 ▶ if ($type == SECRET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 2) { } 7 7 14
121 7 push @{ $pgp->{secrings} }, $ring;
122 } else {
123 7 push @{ $pgp->{pubrings} }, $ring;
124 }
125 14 $ring;
126 }
127  
128 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
129 10 my $class = shift;
130 10 my $pgp = bless { }, $class;
131 10 $pgp->init(@_);
132 }
133  
134 9 ▶sub _first_exists {
Subroutine _first_exists: called CC=4 SCAR=14.5
135 9 my($list) = @_;
136 9 for my $f (@$list) {
137 9 ▶ next unless $f;
Branch: 1 branch
BranchTrueFalseTotal
unless $f 0 9 9
138 9 ▶ return $f if -e $f;
Branch: 1 branch
BranchTrueFalseTotal
if -e $f 0 9 9
139 }
140 }
141  
142 10 ▶sub init {
Subroutine init: called CC=12 SCAR=25.1
Pod undocumented
143 10 my $pgp = shift;
144 10 $pgp->{pubrings} = [];
145 10 $pgp->{secrings} = [];
146 10 my %param = @_;
147 10 my $cfg_file = delete $param{ConfigFile};
148 10 ▶ my $cfg = $pgp->{cfg} = Crypt::OpenPGP::Config->new(%param) or
Branch: 1 branch
BranchTrueFalseTotal
my $cfg = $pgp->{"cfg"} = "Crypt::OpenPGP::Config"->new(%param) or return "Crypt::OpenPGP::Config"->errstr 10 0 10
149 return Crypt::OpenPGP::Config->errstr;
150 10 ▶ if (!$cfg_file && (my $compat = $cfg->get('Compat'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cfg_file and my $compat = $cfg->get("Compat")) 0 10 10
Condition66%2/3
not $cfg_file and my $compat = $cfg->get("Compat")
!ll&&!rl&&r
280
MC/DC0%0/2
not $cfg_file and my $compat = $cfg->get("Compat")
not $cfg_file my $compat = $cfg->get("Compat")
Truth table66%2/3
not(A) and B
A= not $cfg_fileB= my $compat = $cfg->get("Compat")
ABresult
0X0
100
111
151 0 $cfg_file = _first_exists($COMPAT{$compat}{Config});
152 }
153 10 ▶ if ($cfg_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg_file) 2 8 10
154 2 $cfg->read_config($param{Compat}, $cfg_file);
155 }
156 ## Load public and secret keyrings.
157 10 for my $s (qw( PubRing SecRing )) {
158 20 ▶ unless (defined $cfg->get($s)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cfg->get($s)) 5 15 20
159 5 ▶ my @compats = $param{Compat} ? ($param{Compat}) : keys %COMPAT;
Branch: 1 branch
BranchTrueFalseTotal
$param{'Compat'} ? : 3 2 5
160 5 for my $compat (@compats) {
161 9 my $ring = _first_exists($COMPAT{$compat}{$s});
162 9 ▶ $cfg->set($s, $ring), last if $ring;
Branch: 1 branch
BranchTrueFalseTotal
if $ring 0 9 9
163 }
164 }
165 20 ▶ if (my $ring = $cfg->get($s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ring = $cfg->get($s)) 15 5 20
166 15 ▶ $pgp->add_ring($s eq 'PubRing' ? PUBLIC : SECRET, $ring);
Branch: 1 branch
BranchTrueFalseTotal
$s eq 'PubRing' ? : 8 7 15
167 }
168 }
169 10 $pgp;
170 }
171  
172 1 ▶sub handle {
Subroutine handle: called CC=19 SCAR=42.2
Pod documented
173 1 my $pgp = shift;
174 1 my %param = @_;
175 1 my($data);
176 1 ▶ unless ($data = $param{Data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data = $param{"Data"}) 0 1 1
177 my $file = $param{Filename} or
178 0 ▶ return $pgp->error("Need either 'Data' or 'Filename' to decrypt");
Branch: 1 branch
BranchTrueFalseTotal
my $file = $param{"Filename"} or return $pgp->error("Need either 'Data' or 'Filename' to decrypt") 0 0 0
179 0 ▶ $data = $pgp->_read_files($file) or return $pgp->error($pgp->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$data = $pgp->_read_files($file) or return $pgp->error($pgp->errstr) 0 0 0
180 }
181 1 ▶ my $msg = Crypt::OpenPGP::Message->new( Data => $data ) or
Branch: 1 branch
BranchTrueFalseTotal
my $msg = "Crypt::OpenPGP::Message"->new("Data", $data) or return $pgp->error('Reading data packets failed: ' . "Crypt::OpenPGP::Message"->errstr) 1 0 1
182 return $pgp->error("Reading data packets failed: " .
183 Crypt::OpenPGP::Message->errstr);
184 1 my @pieces = $msg->pieces;
185 1 ▶ return $pgp->error("No packets found in message") unless @pieces;
Branch: 1 branch
BranchTrueFalseTotal
unless @pieces 0 1 1
186 1 ▶ while (ref($pieces[0]) eq 'Crypt::OpenPGP::Marker') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pieces[0] eq "Crypt::OpenPGP::Marker") 0 1 1
187 0 shift @pieces;
188 }
189 1 ▶ if (ref($pieces[0]) eq 'Crypt::OpenPGP::Compressed') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pieces[0] eq "Crypt::OpenPGP::Compressed") 0 1 1
190 0 ▶ $data = $pieces[0]->decompress or
Branch: 1 branch
BranchTrueFalseTotal
$data = $pieces[0]->decompress or return $pgp->error('Decompression error: ' . $pieces[0]->errstr) 0 0 0
191 return $pgp->error("Decompression error: " . $pieces[0]->errstr);
192 0 ▶ $msg = Crypt::OpenPGP::Message->new( Data => $data ) or
Branch: 1 branch
BranchTrueFalseTotal
$msg = "Crypt::OpenPGP::Message"->new("Data", $data) or return $pgp->error('Reading decompressed data failed: ' . "Crypt::OpenPGP::Message"->errstr) 0 0 0
193 return $pgp->error("Reading decompressed data failed: " .
194 Crypt::OpenPGP::Message->errstr);
195 0 @pieces = $msg->pieces;
196 }
197 1 my $class = ref($pieces[0]);
198 1 my(%res);
199 1 ▶ if ($class eq 'Crypt::OpenPGP::OnePassSig' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'Crypt::OpenPGP::OnePassSig' or $class eq 'Crypt::OpenPGP::Signature') { } 0 1 1
Condition33%1/3
$class eq "Crypt::OpenPGP::OnePassSig" or $class eq "Crypt::OpenPGP::Signature"
l!l&&r!l&&!r
001
MC/DC0%0/2
$class eq "Crypt::OpenPGP::OnePassSig" or $class eq "Crypt::OpenPGP::Signature"
$class eq "Crypt::OpenPGP::OnePassSig" $class eq "Crypt::OpenPGP::Signature"
Truth table33%1/3
A or B
A= $class eq "Crypt::OpenPGP::OnePassSig"B= $class eq "Crypt::OpenPGP::Signature"
ABresult
1X1
011
000
200 $class eq 'Crypt::OpenPGP::Signature') {
201 0 my($valid, $sig) = $pgp->verify( Signature => $data );
202 0 ▶ return $pgp->error("Error verifying signature: " . $pgp->errstr)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $valid 0 0 0
203 if !defined $valid;
204 0 $res{Validity} = $valid;
205 0 $res{Signature} = $sig;
206 } else {
207 1 ▶ my $cb = $param{PassphraseCallback} || \&_default_passphrase_cb;
Condition50%1/2
$param{'PassphraseCallback'} || \&Crypt::OpenPGP::_default_passphrase_cb
l!l
10
MC/DC0%0/1
$param{'PassphraseCallback'} || \&Crypt::OpenPGP::_default_passphrase_cb
$param{'PassphraseCallback'}
Truth table50%1/2
A
A= $param{'PassphraseCallback'}
Aresult
11
00
208 1 my($pt, $valid, $sig) = $pgp->decrypt(
209 Data => $data,
210 PassphraseCallback => $cb,
211 );
212 1 ▶ return $pgp->error("Decryption failed: " . $pgp->errstr)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pt 0 1 1
213 unless defined $pt;
214 1 ▶ return $pgp->error("Error verifying signature: " . $pgp->errstr)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $valid or $pgp->errstr =~ /^No Signature/ 0 1 1
Condition33%1/3
defined $valid or $pgp->errstr =~ /^No Signature/
l!l&&r!l&&!r
010
MC/DC0%0/2
defined $valid or $pgp->errstr =~ /^No Signature/
defined $valid $pgp->errstr =~ /^No Signature/
Truth table33%1/3
A or B
A= defined $validB= $pgp->errstr =~ /^No Signature/
ABresult
1X1
011
000
215 if !defined($valid) && $pgp->errstr !~ /^No Signature/;
216 1 $res{Plaintext} = $pt;
217 1 ▶ $res{Validity} = $valid if defined $valid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $valid 0 1 1
218 1 ▶ $res{Signature} = $sig if defined $sig;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sig 0 1 1
219 }
220 1 \%res;
221 }
222  
223 0 ▶sub _default_passphrase_cb {
Subroutine _default_passphrase_cb: not called CC=2 SCAR=17.9
224 0 my($cert) = @_;
225 0 my $prompt;
226 0 ▶ if ($cert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cert) { } 0 0 0
227 0 $prompt = sprintf qq(
228 You need a passphrase to unlock the secret key for
229 user "%s".
230 %d-bit %s key, ID %s
231  
232 Enter passphrase: ), $cert->uid,
233 $cert->key->size,
234 $cert->key->alg,
235 substr($cert->key_id_hex, -8, 8);
236 } else {
237 0 $prompt = "Enter passphrase: ";
238 }
239 0 _prompt($prompt, '', 1);
240 }
241  
242 0 ▶sub _prompt {
Subroutine _prompt: not called CC=4 SCAR=30.0
243 0 my($prompt, $def, $noecho) = @_;
244 0 require Term::ReadKey;
245 0 Term::ReadKey->import;
246 0 ▶ print STDERR $prompt . ($def ? "[$def] " : "");
Branch: 1 branch
BranchTrueFalseTotal
$def ? : 0 0 0
247 0 ▶ if ($noecho) {
Branch: 1 branch
BranchTrueFalseTotal
if ($noecho) 0 0 0
248 0 ReadMode('noecho');
249 }
250 0 chomp(my $ans = ReadLine(0));
251 0 ReadMode('restore');
252 0 print STDERR "\n";
253 0 ▶ $ans ? $ans : $def;
Branch: 1 branch
BranchTrueFalseTotal
$ans ? : 0 0 0
254 }
255  
256 2 ▶sub sign {
Subroutine sign: called CC=27 SCAR=61.6
Pod documented
257 2 my $pgp = shift;
258 2 my %param = @_;
259 2 ▶ $pgp->_merge_compat(\%param, 'sign') or
Branch: 1 branch
BranchTrueFalseTotal
$pgp->_merge_compat(\%param, "sign") or return $pgp->error($pgp->errstr) 2 0 2
260 return $pgp->error( $pgp->errstr );
261 2 my($cert, $data);
262 2 require Crypt::OpenPGP::Signature;
263 2 ▶ unless (defined($data = $param{Data})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($data = $param{"Data"})) 0 2 2
264 my $file = $param{Filename} or
265 0 ▶ return $pgp->error("Need either 'Data' or 'Filename' to sign");
Branch: 1 branch
BranchTrueFalseTotal
my $file = $param{"Filename"} or return $pgp->error("Need either 'Data' or 'Filename' to sign") 0 0 0
266 0 ▶ $data = $pgp->_read_files($file) or return $pgp->error($pgp->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$data = $pgp->_read_files($file) or return $pgp->error($pgp->errstr) 0 0 0
267 }
268 2 ▶ unless ($cert = $param{Key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cert = $param{"Key"}) 2 0 2
269 2 ▶ my $kid = $param{KeyID} or return $pgp->error("No KeyID specified");
Branch: 1 branch
BranchTrueFalseTotal
my $kid = $param{"KeyID"} or return $pgp->error("No KeyID specified") 2 0 2
270 2 ▶ my $ring = $pgp->secrings->[0]
Branch: 1 branch
BranchTrueFalseTotal
my $ring = $pgp->secrings->[0] or return $pgp->error("No secret keyrings") 2 0 2
271 or return $pgp->error("No secret keyrings");
272 2 ▶ my $kb = $ring->find_keyblock_by_keyid(pack 'H*', $kid) or
Branch: 1 branch
BranchTrueFalseTotal
my $kb = $ring->find_keyblock_by_keyid(pack("H*", $kid)) or return $pgp->error("Could not find secret key with KeyID $kid") 0 0 0
273 return $pgp->error("Could not find secret key with KeyID $kid");
274 0 $cert = $kb->signing_key;
275 0 $cert->uid($kb->primary_uid);
276 }
277 0 ▶ if ($cert->is_protected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cert->is_protected) 0 0 0
278 0 my $pass = $param{Passphrase};
279 0 ▶ if (!defined $pass && (my $cb = $param{PassphraseCallback})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pass and my $cb = $param{"PassphraseCallback"}) 0 0 0
Condition0%0/3
not defined $pass and my $cb = $param{"PassphraseCallback"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $pass and my $cb = $param{"PassphraseCallback"}
not defined $pass my $cb = $param{"PassphraseCallback"}
Truth table0%0/3
not(A) and B
A= not defined $passB= my $cb = $param{"PassphraseCallback"}
ABresult
0X0
100
111
280 0 $pass = $cb->($cert);
281 }
282 0 ▶ return $pgp->error("Need passphrase to unlock secret key")
Branch: 1 branch
BranchTrueFalseTotal
unless $pass 0 0 0
283 unless $pass;
284 0 ▶ $cert->unlock($pass) or
Branch: 1 branch
BranchTrueFalseTotal
$cert->unlock($pass) or return $pgp->error('Secret key unlock failed: ' . $cert->errstr) 0 0 0
285 return $pgp->error("Secret key unlock failed: " . $cert->errstr);
286 }
287 0 my @ptarg;
288 0 ▶ push @ptarg, ( Filename => $param{Filename} ) if $param{Filename};
Branch: 1 branch
BranchTrueFalseTotal
if $param{"Filename"} 0 0 0
289 0 ▶ if ($param{Clearsign}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'Clearsign'}) { } 0 0 0
290 0 push @ptarg, ( Mode => 't' );
291 ## In clear-signed messages, the line ending before the signature
292 ## is not considered part of the signed text.
293 0 (my $tmp = $data) =~ s!\r?\n$!!;
294 0 push @ptarg, ( Data => $tmp );
295 } else {
296 0 push @ptarg, ( Data => $data );
297 }
298 0 my $pt = Crypt::OpenPGP::Plaintext->new(@ptarg);
299 0 my @sigarg;
300 0 ▶ if (my $hash_alg = $param{Digest}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hash_alg = $param{"Digest"}) 0 0 0
301 0 ▶ my $dgst = Crypt::OpenPGP::Digest->new($hash_alg) or
Branch: 1 branch
BranchTrueFalseTotal
my $dgst = "Crypt::OpenPGP::Digest"->new($hash_alg) or return $pgp->error("Crypt::OpenPGP::Digest"->errstr) 0 0 0
302 return $pgp->error( Crypt::OpenPGP::Digest->errstr );
303 0 @sigarg = ( Digest => $dgst->alg_id );
304 }
305 0 ▶ push @sigarg, (Type => 0x01) if $param{Clearsign};
Branch: 1 branch
BranchTrueFalseTotal
if $param{"Clearsign"} 0 0 0
306 my $sig = Crypt::OpenPGP::Signature->new(
307 Data => $pt,
308 Key => $cert,
309 Version => $param{Version},
310 0 @sigarg,
311 );
312 0 ▶ if ($param{Clearsign}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"Clearsign"}) 0 0 0
313 0 $param{Armour} = $param{Detach} = 1;
314 }
315 my $sig_data = Crypt::OpenPGP::PacketFactory->save($sig,
316 0 ▶ $param{Detach} ? () : ($pt));
Branch: 1 branch
BranchTrueFalseTotal
$param{'Detach'} ? : 0 0 0
317 0 ▶ if ($param{Armour}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"Armour"}) 0 0 0
318 0 require Crypt::OpenPGP::Armour;
319 $sig_data = Crypt::OpenPGP::Armour->armour(
320 Data => $sig_data,
321 0 ▶ Object => ($param{Detach} ? 'SIGNATURE' : 'MESSAGE'),
Branch: 2 branches
BranchTrueFalseTotal
$sig_data = "Crypt::OpenPGP::Armour"->armour("Data", $sig_data, "Object", $param{"Detach"} ? "SIGNATURE" : "MESSAGE") or return $pgp->error("Crypt::OpenPGP::Armour"->errstr) 0 0 0
$param{'Detach'} ? : 0 0 0
322 ) or return $pgp->error( Crypt::OpenPGP::Armour->errstr );
323 }
324 0 ▶ if ($param{Clearsign}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"Clearsign"}) 0 0 0
325 0 require Crypt::OpenPGP::Util;
326 0 my $hash = Crypt::OpenPGP::Digest->alg($sig->{hash_alg});
327 0 my $data = Crypt::OpenPGP::Util::dash_escape($data);
328 0 ▶ $data .= "\n" unless $data =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $data =~ /\n$/ 0 0 0
329 0 ▶ $sig_data = "-----BEGIN PGP SIGNED MESSAGE-----\n" .
Branch: 1 branch
BranchTrueFalseTotal
$hash eq 'MD5' ? : 0 0 0
330 ($hash eq 'MD5' ? '' : "Hash: $hash\n") .
331 "\n" .
332 $data .
333 $sig_data;
334 }
335 0 $sig_data;
336 }
337  
338 3 ▶sub verify {
Subroutine verify: called CC=30 SCAR=48.9
Pod documented
339 3 my $pgp = shift;
340 3 my %param = @_;
341 3 my $wants_object = wantarray;
342 3 my($data, $sig);
343 3 require Crypt::OpenPGP::Signature;
344 $param{Signature} or $param{SigFile} or
345 3 ▶ return $pgp->error("Need Signature or SigFile to verify");
Branch: 1 branch
BranchTrueFalseTotal
$param{"Signature"} or $param{"SigFile"} or return $pgp->error("Need Signature or SigFile to verify") 3 0 3
Condition66%2/3
$param{"Signature"} or $param{"SigFile"}
l!l&&r!l&&!r
210
MC/DC0%0/2
$param{"Signature"} or $param{"SigFile"}
$param{"Signature"} $param{"SigFile"}
Truth table66%2/3
A or B
A= $param{"Signature"}B= $param{"SigFile"}
ABresult
1X1
011
000
346 my %arg = $param{Signature} ? (Data => $param{Signature}) :
347 3 ▶ (Filename => $param{SigFile});
Branch: 1 branch
BranchTrueFalseTotal
$param{'Signature'} ? : 2 1 3
348 3 ▶ $arg{IsPacketStream} = 1 if $param{IsPacketStream};
Branch: 1 branch
BranchTrueFalseTotal
if $param{"IsPacketStream"} 0 3 3
349 3 ▶ my $msg = Crypt::OpenPGP::Message->new( %arg ) or
Branch: 1 branch
BranchTrueFalseTotal
my $msg = "Crypt::OpenPGP::Message"->new(%arg) or return $pgp->error('Reading signature failed: ' . "Crypt::OpenPGP::Message"->errstr) 1 0 1
350 return $pgp->error("Reading signature failed: " .
351 Crypt::OpenPGP::Message->errstr);
352 1 my @pieces = $msg->pieces;
353 1 ▶ if (ref($pieces[0]) eq 'Crypt::OpenPGP::Compressed') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pieces[0] eq "Crypt::OpenPGP::Compressed") 1 0 1
354 1 ▶ $data = $pieces[0]->decompress or
Branch: 1 branch
BranchTrueFalseTotal
$data = $pieces[0]->decompress or return $pgp->error('Decompression error: ' . $pieces[0]->errstr) 1 0 1
355 return $pgp->error("Decompression error: " . $pieces[0]->errstr);
356 1 ▶ $msg = Crypt::OpenPGP::Message->new( Data => $data ) or
Branch: 1 branch
BranchTrueFalseTotal
$msg = "Crypt::OpenPGP::Message"->new("Data", $data) or return $pgp->error('Reading decompressed data failed: ' . "Crypt::OpenPGP::Message"->errstr) 1 0 1
357 return $pgp->error("Reading decompressed data failed: " .
358 Crypt::OpenPGP::Message->errstr);
359 1 @pieces = $msg->pieces;
360 }
361 1 ▶ if (ref($pieces[0]) eq 'Crypt::OpenPGP::OnePassSig') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pieces[0] eq 'Crypt::OpenPGP::OnePassSig') { } 1 0 1
elsif (ref $pieces[0] eq 'Crypt::OpenPGP::Signature') { } 0 0 0
362 1 ($data, $sig) = @pieces[1,2];
363 } elsif (ref($pieces[0]) eq 'Crypt::OpenPGP::Signature') {
364 0 ($sig, $data) = @pieces[0,1];
365 } else {
366 0 return $pgp->error("SigFile contents are strange");
367 }
368 1 ▶ my @pte = ($sig->{type} == 1 ? (Mode => "t") : () );
Branch: 1 branch
BranchTrueFalseTotal
$sig->{'type'} == 1 ? : 0 1 1
369 1 ▶ unless ($data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 1 1
370 0 ▶ if ($param{Data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'Data'}) { } 0 0 0
371 0 $data = Crypt::OpenPGP::Plaintext->new( Data => $param{Data}, @pte );
372 }
373 else {
374 ## if no Signature or detached sig in SigFile
375 0 my @files = ref($param{Files}) eq 'ARRAY' ? @{ $param{Files} } :
376 0 ▶ $param{Files};
Branch: 1 branch
BranchTrueFalseTotal
ref $param{'Files'} eq 'ARRAY' ? : 0 0 0
377 0 my $fdata = $pgp->_read_files(@files);
378 0 ▶ return $pgp->error("Reading data files failed: " . $pgp->errstr)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fdata 0 0 0
379 unless defined $fdata;
380 0 $data = Crypt::OpenPGP::Plaintext->new( Data => $fdata, @pte );
381 }
382 }
383 1 my($cert, $kb);
384 1 ▶ unless ($cert = $param{Key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cert = $param{"Key"}) 1 0 1
385 1 my $key_id = $sig->key_id;
386 1 my $ring = $pgp->pubrings->[0];
387 1 ▶ unless ($ring && ($kb = $ring->find_keyblock_by_keyid($key_id))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ring and $kb = $ring->find_keyblock_by_keyid($key_id)) 0 1 1
Condition33%1/3
$ring and $kb = $ring->find_keyblock_by_keyid($key_id)
!ll&&!rl&&r
001
MC/DC0%0/2
$ring and $kb = $ring->find_keyblock_by_keyid($key_id)
$ring $kb = $ring->find_keyblock_by_keyid($key_id)
Truth table33%1/3
A and B
A= $ringB= $kb = $ring->find_keyblock_by_keyid($key_id)
ABresult
0X0
100
111
388 0 my $cfg = $pgp->{cfg};
389 0 ▶ if ($cfg->get('AutoKeyRetrieve') && $cfg->get('KeyServer')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->get("AutoKeyRetrieve") and $cfg->get("KeyServer")) 0 0 0
Condition0%0/3
$cfg->get("AutoKeyRetrieve") and $cfg->get("KeyServer")
!ll&&!rl&&r
000
MC/DC0%0/2
$cfg->get("AutoKeyRetrieve") and $cfg->get("KeyServer")
$cfg->get("AutoKeyRetrieve") $cfg->get("KeyServer")
Truth table0%0/3
A and B
A= $cfg->get("AutoKeyRetrieve")B= $cfg->get("KeyServer")
ABresult
0X0
100
111
390 0 require Crypt::OpenPGP::KeyServer;
391 0 my $server = Crypt::OpenPGP::KeyServer->new(
392 Server => $cfg->get('KeyServer'),
393 );
394 0 $kb = $server->find_keyblock_by_keyid($key_id);
395 }
396 0 ▶ return $pgp->error("Could not find public key with KeyID " .
Branch: 1 branch
BranchTrueFalseTotal
unless $kb 0 0 0
397 unpack('H*', $key_id))
398 unless $kb;
399 }
400 1 $cert = $kb->key_by_id($sig->key_id);
401 }
402  
403 ## pgp2 and pgp5 do not trim trailing whitespace from "canonical text"
404 ## signatures, only from cleartext signatures. So we first try to verify
405 ## the signature using proper RFC4880 canonical text, then if that fails,
406 ## retry without trimming trailing whitespace.
407 ## See:
408 ## http://cert.uni-stuttgart.de/archive/ietf-openpgp/2000/01/msg00033.html
409 1 my($dgst, $found);
410 1 for (1, 0) {
411 1 local $Crypt::OpenPGP::Globals::Trim_trailing_ws = $_;
412 1 ▶ $dgst = $sig->hash_data($data) or
Branch: 1 branch
BranchTrueFalseTotal
$dgst = $sig->hash_data($data) or return $pgp->error($sig->errstr) 1 0 1
413 return $pgp->error( $sig->errstr );
414 1 ▶ $found++, last if substr($dgst, 0, 2) eq $sig->{chk};
Branch: 1 branch
BranchTrueFalseTotal
if substr($dgst, 0, 2) eq $sig->{"chk"} 1 0 1
415 }
416 1 ▶ return $pgp->error("Message hash does not match signature checkbytes")
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 1 1
417 unless $found;
418 1 ▶ my $valid = $cert->key->public_key->verify($sig, $dgst) ?
Branch: 2 branches
BranchTrueFalseTotal
$cert->key->public_key->verify($sig, $dgst) ? : 1 0 1
$kb && $kb->primary_uid ? : 1 0 1
Condition33%1/3
$kb and $kb->primary_uid
!ll&&!rl&&r
001
MC/DC0%0/2
$kb and $kb->primary_uid
$kb $kb->primary_uid
Truth table33%1/3
A and B
A= $kbB= $kb->primary_uid
ABresult
0X0
100
111
419 ($kb && $kb->primary_uid ? $kb->primary_uid : 1) : 0;
420  
421 1 ▶ $wants_object ? ($valid, $sig) : $valid;
Branch: 1 branch
BranchTrueFalseTotal
$wants_object ? : 0 1 1
422 }
423  
424 3 ▶sub encrypt {
Subroutine encrypt: called CC=50 SCAR=62.1
Pod documented
425 3 my $pgp = shift;
426 3 my %param = @_;
427 3 ▶ $pgp->_merge_compat(\%param, 'encrypt') or
Branch: 1 branch
BranchTrueFalseTotal
$pgp->_merge_compat(\%param, "encrypt") or return $pgp->error($pgp->errstr) 3 0 3
428 return $pgp->error( $pgp->errstr );
429 3 my($data);
430 3 require Crypt::OpenPGP::Cipher;
431 3 require Crypt::OpenPGP::Ciphertext;
432 3 ▶ unless (defined($data = $param{Data})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($data = $param{"Data"})) 0 3 3
433 my $file = $param{Filename} or
434 0 ▶ return $pgp->error("Need either 'Data' or 'Filename' to encrypt");
Branch: 1 branch
BranchTrueFalseTotal
my $file = $param{"Filename"} or return $pgp->error("Need either 'Data' or 'Filename' to encrypt") 0 0 0
435 0 ▶ $data = $pgp->_read_files($file) or return $pgp->error($pgp->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$data = $pgp->_read_files($file) or return $pgp->error($pgp->errstr) 0 0 0
436 }
437 3 my $ptdata;
438 3 ▶ if ($param{SignKeyID}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'SignKeyID'}) { } 0 3 3
439 $ptdata = $pgp->sign(
440 Data => $data,
441 KeyID => $param{SignKeyID},
442 Compat => $param{Compat},
443 Armour => 0,
444 Passphrase => $param{SignPassphrase},
445 PassphraseCallback => $param{SignPassphraseCallback},
446 )
447 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
$ptdata = $pgp->sign("Data", $data, "KeyID", $param{"SignKeyID"}, "Compat", $param{"Compat"}, "Armour", 0, "Passphrase", $param{"SignPassphrase"}, "PassphraseCallback", $param{"SignPassphraseCallback"}) or return 0 0 0
448 } else {
449 my $pt = Crypt::OpenPGP::Plaintext->new( Data => $data,
450 3 ▶ $param{Filename} ? (Filename => $param{Filename}) : () );
Branch: 1 branch
BranchTrueFalseTotal
$param{'Filename'} ? : 0 3 3
451 3 $ptdata = Crypt::OpenPGP::PacketFactory->save($pt);
452 }
453 3 ▶ if (my $alg = $param{Compress}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $alg = $param{"Compress"}) 0 3 3
454 0 require Crypt::OpenPGP::Compressed;
455 0 $alg = Crypt::OpenPGP::Compressed->alg_id($alg);
456 0 ▶ my $cdata = Crypt::OpenPGP::Compressed->new( Data => $ptdata,
Branch: 1 branch
BranchTrueFalseTotal
my $cdata = "Crypt::OpenPGP::Compressed"->new("Data", $ptdata, "Alg", $alg) or return $pgp->error('Compression error: ' . "Crypt::OpenPGP::Compressed"->errstr) 0 0 0
457 Alg => $alg ) or return $pgp->error("Compression error: " .
458 Crypt::OpenPGP::Compressed->errstr);
459 0 $ptdata = Crypt::OpenPGP::PacketFactory->save($cdata);
460 }
461 3 my $key_data = Crypt::OpenPGP::Util::get_random_bytes(32);
462 my $sym_alg = $param{Cipher} ?
463 3 ▶ Crypt::OpenPGP::Cipher->alg_id($param{Cipher}) : DEFAULT_CIPHER;
Branch: 1 branch
BranchTrueFalseTotal
$param{'Cipher'} ? : 0 3 3
464 3 my(@sym_keys);
465 3 ▶ if ($param{Recipients} && !ref($param{Recipients})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"Recipients"} and not ref $param{"Recipients"}) 0 3 3
Condition33%1/3
$param{"Recipients"} and not ref $param{"Recipients"}
!ll&&!rl&&r
300
MC/DC0%0/2
$param{"Recipients"} and not ref $param{"Recipients"}
$param{"Recipients"} not ref $param{"Recipients"}
Truth table33%1/3
A and not(B)
A= $param{"Recipients"}B= not ref $param{"Recipients"}
ABresult
0X0
100
111
466 0 $param{Recipients} = [ $param{Recipients} ];
467 }
468 3 ▶ if (my $kid = delete $param{KeyID}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $kid = delete $param{"KeyID"}) 1 2 3
469 1 ▶ my @kid = ref $kid eq 'ARRAY' ? @$kid : $kid;
Branch: 1 branch
BranchTrueFalseTotal
ref $kid eq 'ARRAY' ? : 0 1 1
470 1 push @{ $param{Recipients} }, @kid;
471 }
472 3 ▶ if ($param{Key} || $param{Recipients}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param{'Key'} or $param{'Recipients'}) { } 1 2 3
elsif (my $pass = $param{'Passphrase'}) { } 2 0 2
Condition66%2/3
$param{"Key"} or $param{"Recipients"}
l!l&&r!l&&!r
012
MC/DC50%1/2
$param{"Key"} or $param{"Recipients"}
$param{"Key"} $param{"Recipients"}
Truth table66%2/3
A or B
A= $param{"Key"}B= $param{"Recipients"}
ABresult
1X1
011
000
473 1 require Crypt::OpenPGP::SessionKey;
474 1 my @keys;
475 1 ▶ if (my $recips = $param{Recipients}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $recips = $param{"Recipients"}) 1 0 1
476 1 ▶ my @recips = ref $recips eq 'ARRAY' ? @$recips : $recips;
Branch: 1 branch
BranchTrueFalseTotal
ref $recips eq 'ARRAY' ? : 1 0 1
477 1 my $ring = $pgp->pubrings->[0];
478 1 my %seen;
479 my $server;
480 1 my $cfg = $pgp->{cfg};
481 1 ▶ if ($cfg->get('AutoKeyRetrieve') && $cfg->get('KeyServer')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->get("AutoKeyRetrieve") and $cfg->get("KeyServer")) 0 1 1
Condition33%1/3
$cfg->get("AutoKeyRetrieve") and $cfg->get("KeyServer")
!ll&&!rl&&r
100
MC/DC0%0/2
$cfg->get("AutoKeyRetrieve") and $cfg->get("KeyServer")
$cfg->get("AutoKeyRetrieve") $cfg->get("KeyServer")
Truth table33%1/3
A and B
A= $cfg->get("AutoKeyRetrieve")B= $cfg->get("KeyServer")
ABresult
0X0
100
111
482 0 require Crypt::OpenPGP::KeyServer;
483 0 $server = Crypt::OpenPGP::KeyServer->new(
484 Server => $cfg->get('KeyServer'),
485 );
486 }
487 1 for my $r (@recips) {
488 1 my($lr, @kb) = (length($r));
489 1 ▶ if (($lr == 8 || $lr == 16) && $r !~ /[^\da-fA-F]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lr == 8 || $lr == 16 and not $r =~ /[^\da-fA-F]/) { } 1 0 1
Condition33%2/6
$lr == 8 || $lr == 16 and not $r =~ /[^\da-fA-F]/
!ll&&!rl&&r
001
$lr == 8 or $lr == 16
l!l&&r!l&&!r
010
MC/DC0%0/3
$lr == 8 || $lr == 16 and not $r =~ /[^\da-fA-F]/
$lr == 8 $lr == 16 not $r =~ /[^\da-fA-F]/
Truth table20%1/5
A or B and not(C)
A= $lr == 8B= $lr == 16C= not $r =~ /[^\da-fA-F]/
ABCresult
00X0
1X00
0100
1X11
0111
490 1 my $id = pack 'H*', $r;
491 1 ▶ @kb = $ring->find_keyblock_by_keyid($id) if $ring;
Branch: 1 branch
BranchTrueFalseTotal
if $ring 1 0 1
492 0 ▶ @kb = $server->find_keyblock_by_keyid($id)
Branch: 1 branch
BranchTrueFalseTotal
if not @kb and $server 0 0 0
Condition0%0/3
not @kb and $server
!ll&&!rl&&r
000
MC/DC0%0/2
not @kb and $server
not @kb $server
Truth table0%0/3
not(A) and B
A= not @kbB= $server
ABresult
0X0
100
111
493 if !@kb && $server;
494 } else {
495 0 ▶ @kb = $ring->find_keyblock_by_uid($r) if $ring;
Branch: 1 branch
BranchTrueFalseTotal
if $ring 0 0 0
496 0 ▶ @kb = $server->find_keyblock_by_uid($r)
Branch: 1 branch
BranchTrueFalseTotal
if not @kb and $server 0 0 0
Condition0%0/3
not @kb and $server
!ll&&!rl&&r
000
MC/DC0%0/2
not @kb and $server
not @kb $server
Truth table0%0/3
not(A) and B
A= not @kbB= $server
ABresult
0X0
100
111
497 if !@kb && $server;
498 }
499 0 for my $kb (@kb) {
500 0 ▶ next unless my $cert = $kb->encrypting_key;
Branch: 1 branch
BranchTrueFalseTotal
unless my $cert = $kb->encrypting_key 0 0 0
501 0 ▶ next if $seen{ $cert->key_id }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$cert->key_id}++ 0 0 0
502 0 $cert->uid($kb->primary_uid);
503 0 push @keys, $cert;
504 }
505 }
506 0 ▶ if (my $cb = $param{RecipientsCallback}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $param{"RecipientsCallback"}) 0 0 0
507 0 @keys = @{ $cb->(\@keys) };
508 }
509 }
510 0 ▶ if ($param{Key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"Key"}) 0 0 0
511 0 push @keys, ref $param{Key} eq 'ARRAY' ? @{$param{Key}} :
512 0 ▶ $param{Key};
Branch: 1 branch
BranchTrueFalseTotal
ref $param{'Key'} eq 'ARRAY' ? : 0 0 0
513 }
514 0 ▶ return $pgp->error("No known recipients for encryption")
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 0 0
515 unless @keys;
516 0 for my $key (@keys) {
517 0 ▶ push @sym_keys, Crypt::OpenPGP::SessionKey->new(
Branch: 1 branch
BranchTrueFalseTotal
push @sym_keys, "Crypt::OpenPGP::SessionKey"->new("Key", $key, "SymKey", $key_data, "Cipher", $sym_alg) or return $pgp->error("Crypt::OpenPGP::SessionKey"->errstr) 0 0 0
518 Key => $key,
519 SymKey => $key_data,
520 Cipher => $sym_alg,
521 ) or
522 return $pgp->error( Crypt::OpenPGP::SessionKey->errstr );
523 }
524 }
525 elsif (my $pass = $param{Passphrase}) {
526 2 require Crypt::OpenPGP::SKSessionKey;
527 2 require Crypt::OpenPGP::S2k;
528 2 my $s2k;
529 2 ▶ if ($param{Compat} && $param{Compat} eq 'PGP2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'Compat'} and $param{'Compat'} eq 'PGP2') { } 0 2 2
Condition33%1/3
$param{"Compat"} and $param{"Compat"} eq "PGP2"
!ll&&!rl&&r
200
MC/DC0%0/2
$param{"Compat"} and $param{"Compat"} eq "PGP2"
$param{"Compat"} $param{"Compat"} eq "PGP2"
Truth table33%1/3
A and B
A= $param{"Compat"}B= $param{"Compat"} eq "PGP2"
ABresult
0X0
100
111
530 0 $s2k = Crypt::OpenPGP::S2k->new('Simple');
531 0 $s2k->{hash} = Crypt::OpenPGP::Digest->new('MD5');
532 } else {
533 2 $s2k = Crypt::OpenPGP::S2k->new('Salt_Iter');
534 }
535 2 ▶ my $cipher = Crypt::OpenPGP::Cipher->new($sym_alg) or
Branch: 1 branch
BranchTrueFalseTotal
my $cipher = "Crypt::OpenPGP::Cipher"->new($sym_alg) or return $pgp->error("Crypt::OpenPGP::Cipher"->errstr) 2 0 2
536 return $pgp->error( Crypt::OpenPGP::Cipher->errstr );
537 2 my $keysize = $cipher->keysize;
538 2 $key_data = $s2k->generate($pass, $keysize);
539 2 ▶ push @sym_keys, Crypt::OpenPGP::SKSessionKey->new(
Branch: 1 branch
BranchTrueFalseTotal
push @sym_keys, "Crypt::OpenPGP::SKSessionKey"->new("Passphrase", $pass, "SymKey", $key_data, "Cipher", $sym_alg, "S2k", $s2k) or return $pgp->error("Crypt::OpenPGP::SKSessionKey"->errstr) 2 0 2
540 Passphrase => $pass,
541 SymKey => $key_data,
542 Cipher => $sym_alg,
543 S2k => $s2k,
544 ) or
545 return $pgp->error( Crypt::OpenPGP::SKSessionKey->errstr );
546 } else {
547 0 return $pgp->error("Need something to encrypt with");
548 }
549 my $enc = Crypt::OpenPGP::Ciphertext->new(
550 MDC => $param{MDC},
551 2 SymKey => $key_data,
552 Data => $ptdata,
553 Cipher => $sym_alg,
554 );
555 my $enc_data = Crypt::OpenPGP::PacketFactory->save(
556 2 ▶ $param{Passphrase} && $param{Compat} && $param{Compat} eq 'PGP2' ?
Branch: 1 branch
BranchTrueFalseTotal
$param{'Passphrase'} && $param{'Compat'} && $param{'Compat'} eq 'PGP2' ? : 0 2 2
Condition33%2/6
$param{"Passphrase"} and $param{"Compat"} and $param{"Compat"} eq "PGP2"
!ll&&!rl&&r
200
$param{"Passphrase"} and $param{"Compat"}
!ll&&!rl&&r
020
MC/DC0%0/3
$param{"Passphrase"} and $param{"Compat"} and $param{"Compat"} eq "PGP2"
$param{"Passphrase"} $param{"Compat"} $param{"Compat"} eq "PGP2"
Truth table25%1/4
A and B and C
A= $param{"Passphrase"}B= $param{"Compat"}C= $param{"Compat"} eq "PGP2"
ABCresult
0XX0
10X0
1100
1111
557 $enc : (@sym_keys, $enc)
558 );
559 2 ▶ if ($param{Armour}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{"Armour"}) 0 2 2
560 0 require Crypt::OpenPGP::Armour;
561 0 ▶ $enc_data = Crypt::OpenPGP::Armour->armour(
Branch: 1 branch
BranchTrueFalseTotal
$enc_data = "Crypt::OpenPGP::Armour"->armour("Data", $enc_data, "Object", "MESSAGE") or return $pgp->error("Crypt::OpenPGP::Armour"->errstr) 0 0 0
562 Data => $enc_data,
563 Object => 'MESSAGE',
564 ) or return $pgp->error( Crypt::OpenPGP::Armour->errstr );
565 }
566 2 $enc_data;
567 }
568  
569 3 ▶sub decrypt {
Subroutine decrypt: called CC=41 SCAR=60.1
Pod documented
570 3 my $pgp = shift;
571 3 my %param = @_;
572 3 my $wants_verify = wantarray;
573 3 my($data);
574 3 ▶ unless ($data = $param{Data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data = $param{"Data"}) 0 3 3
575 my $file = $param{Filename} or
576 0 ▶ return $pgp->error("Need either 'Data' or 'Filename' to decrypt");
Branch: 1 branch
BranchTrueFalseTotal
my $file = $param{"Filename"} or return $pgp->error("Need either 'Data' or 'Filename' to decrypt") 0 0 0
577 0 ▶ $data = $pgp->_read_files($file) or return $pgp->error($pgp->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$data = $pgp->_read_files($file) or return $pgp->error($pgp->errstr) 0 0 0
578 }
579 3 ▶ my $msg = Crypt::OpenPGP::Message->new( Data => $data ) or
Branch: 1 branch
BranchTrueFalseTotal
my $msg = "Crypt::OpenPGP::Message"->new("Data", $data) or return $pgp->error('Reading data packets failed: ' . "Crypt::OpenPGP::Message"->errstr) 3 0 3
580 return $pgp->error("Reading data packets failed: " .
581 Crypt::OpenPGP::Message->errstr);
582 3 my @pieces = $msg->pieces;
583 3 ▶ return $pgp->error("No packets found in message") unless @pieces;
Branch: 1 branch
BranchTrueFalseTotal
unless @pieces 0 3 3
584 3 ▶ while (ref($pieces[0]) eq 'Crypt::OpenPGP::Marker') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pieces[0] eq "Crypt::OpenPGP::Marker") 0 3 3
585 0 shift @pieces;
586 }
587 3 my($key, $alg);
588 3 ▶ if (ref($pieces[0]) eq 'Crypt::OpenPGP::SessionKey') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pieces[0] eq 'Crypt::OpenPGP::SessionKey') { } 1 2 3
elsif (ref $pieces[0] eq 'Crypt::OpenPGP::SKSessionKey') { } 2 0 2
589 1 my($sym_key, $cert, $ring) = (shift @pieces);
590 1 ▶ unless ($cert = $param{Key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cert = $param{"Key"}) 1 0 1
591 1 ▶ $ring = $pgp->secrings->[0]
Branch: 1 branch
BranchTrueFalseTotal
$ring = $pgp->secrings->[0] or return $pgp->error("No secret keyrings") 1 0 1
592 or return $pgp->error("No secret keyrings");
593 }
594 1 my($kb);
595 1 ▶ while (ref($sym_key) eq 'Crypt::OpenPGP::SessionKey') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sym_key eq "Crypt::OpenPGP::SessionKey") 1 0 1
596 1 ▶ if ($cert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cert) { } 0 1 1
597 0 ▶ if ($cert->key_id eq $sym_key->key_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cert->key_id eq $sym_key->key_id) 0 0 0
598 shift @pieces
599 0 ▶ while ref($pieces[0]) eq 'Crypt::OpenPGP::SessionKey';
Branch: 1 branch
BranchTrueFalseTotal
ref $pieces[0] eq "Crypt::OpenPGP::SessionKey" and do { shift @pieces } 0 0 0
600 0 last;
601 }
602 } else {
603 1 ▶ if ($kb = $ring->find_keyblock_by_keyid($sym_key->key_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kb = $ring->find_keyblock_by_keyid($sym_key->key_id)) 0 0 0
604 shift @pieces
605 0 ▶ while ref($pieces[0]) eq 'Crypt::OpenPGP::SessionKey';
Branch: 1 branch
BranchTrueFalseTotal
ref $pieces[0] eq "Crypt::OpenPGP::SessionKey" and do { shift @pieces } 0 0 0
606 0 last;
607 }
608 }
609 0 $sym_key = shift @pieces;
610 }
611 0 ▶ return $pgp->error("Can't find a secret key to decrypt message")
Branch: 1 branch
BranchTrueFalseTotal
unless $kb or $cert 0 0 0
Condition0%0/3
$kb or $cert
l!l&&r!l&&!r
000
MC/DC0%0/2
$kb or $cert
$kb $cert
Truth table0%0/3
A or B
A= $kbB= $cert
ABresult
1X1
011
000
612 unless $kb || $cert;
613 0 ▶ if ($kb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kb) 0 0 0
614 0 $cert = $kb->key_by_id($sym_key->key_id);
615 0 $cert->uid($kb->primary_uid);
616 }
617 0 ▶ if ($cert->is_protected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cert->is_protected) 0 0 0
618 0 my $pass = $param{Passphrase};
619 0 ▶ if (!defined $pass && (my $cb = $param{PassphraseCallback})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pass and my $cb = $param{"PassphraseCallback"}) 0 0 0
Condition0%0/3
not defined $pass and my $cb = $param{"PassphraseCallback"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $pass and my $cb = $param{"PassphraseCallback"}
not defined $pass my $cb = $param{"PassphraseCallback"}
Truth table0%0/3
not(A) and B
A= not defined $passB= my $cb = $param{"PassphraseCallback"}
ABresult
0X0
100
111
620 0 $pass = $cb->($cert);
621 }
622 0 ▶ return $pgp->error("Need passphrase to unlock secret key")
Branch: 1 branch
BranchTrueFalseTotal
unless $pass 0 0 0
623 unless $pass;
624 0 ▶ $cert->unlock($pass) or
Branch: 1 branch
BranchTrueFalseTotal
$cert->unlock($pass) or return $pgp->error('Seckey unlock failed: ' . $cert->errstr) 0 0 0
625 return $pgp->error("Seckey unlock failed: " . $cert->errstr);
626 }
627 0 ▶ ($key, $alg) = $sym_key->decrypt($cert) or
Branch: 1 branch
BranchTrueFalseTotal
() = $sym_key->decrypt($cert) or return $pgp->error('Symkey decrypt failed: ' . $sym_key->errstr) 0 0 0
628 return $pgp->error("Symkey decrypt failed: " . $sym_key->errstr);
629 }
630 elsif (ref($pieces[0]) eq 'Crypt::OpenPGP::SKSessionKey') {
631 2 my $sym_key = shift @pieces;
632 2 my $pass = $param{Passphrase};
633 2 ▶ if (!defined $pass && (my $cb = $param{PassphraseCallback})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pass and my $cb = $param{"PassphraseCallback"}) 1 1 2
Condition66%2/3
not defined $pass and my $cb = $param{"PassphraseCallback"}
!ll&&!rl&&r
101
MC/DC50%1/2
not defined $pass and my $cb = $param{"PassphraseCallback"}
not defined $pass my $cb = $param{"PassphraseCallback"}
Truth table66%2/3
not(A) and B
A= not defined $passB= my $cb = $param{"PassphraseCallback"}
ABresult
0X0
100
111
634 1 $pass = $cb->();
635 }
636 2 ▶ return $pgp->error("Need passphrase to decrypt session key")
Branch: 1 branch
BranchTrueFalseTotal
unless $pass 0 2 2
637 unless $pass;
638 2 ▶ ($key, $alg) = $sym_key->decrypt($pass) or
Branch: 1 branch
BranchTrueFalseTotal
() = $sym_key->decrypt($pass) or return $pgp->error('Symkey decrypt failed: ' . $sym_key->errstr) 2 0 2
639 return $pgp->error("Symkey decrypt failed: " . $sym_key->errstr);
640 }
641 2 my $enc = $pieces[0];
642  
643 ## If there is still no symkey and symmetric algorithm, *and* the
644 ## first packet is a Crypt::OpenPGP::Ciphertext packet, assume that
645 ## the packet is encrypted using a symmetric key, using a 'Simple' s2k.
646 2 ▶ if (!$key && !$alg && ref($enc) eq 'Crypt::OpenPGP::Ciphertext') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $key || $alg and ref $enc eq "Crypt::OpenPGP::Ciphertext") 0 2 2
Condition33%2/6
not $key || $alg and ref $enc eq "Crypt::OpenPGP::Ciphertext"
!ll&&!rl&&r
200
$key || $alg
l!l&&r!l&&!r
200
MC/DC0%0/3
not $key || $alg and ref $enc eq "Crypt::OpenPGP::Ciphertext"
$key $alg ref $enc eq "Crypt::OpenPGP::Ciphertext"
Truth table25%1/4
not(A || B) and C
A= $keyB= $algC= ref $enc eq "Crypt::OpenPGP::Ciphertext"
ABCresult
1XX0
01X0
0000
0011
647 my $pass = $param{Passphrase} or
648 0 ▶ return $pgp->error("Need passphrase to decrypt session key");
Branch: 1 branch
BranchTrueFalseTotal
my $pass = $param{"Passphrase"} or return $pgp->error("Need passphrase to decrypt session key") 0 0 0
649 0 require Crypt::OpenPGP::Cipher;
650 0 require Crypt::OpenPGP::S2k;
651 0 my $ciph = Crypt::OpenPGP::Cipher->new('IDEA');
652 0 my $s2k = Crypt::OpenPGP::S2k->new('Simple');
653 0 $s2k->{hash} = Crypt::OpenPGP::Digest->new('MD5');
654 0 $key = $s2k->generate($pass, $ciph->keysize);
655 0 $alg = $ciph->alg_id;
656 }
657  
658 2 ▶ $data = $enc->decrypt($key, $alg) or
Branch: 1 branch
BranchTrueFalseTotal
$data = $enc->decrypt($key, $alg) or return $pgp->error('Ciphertext decrypt failed: ' . $enc->errstr) 2 0 2
659 return $pgp->error("Ciphertext decrypt failed: " . $enc->errstr);
660  
661 ## This is a special hack: if decrypt gets a signed, encrypted message,
662 ## it needs to be able to pass back the decrypted text *and* a flag
663 ## saying whether the signature is valid or not. But in some cases,
664 ## you don't know ahead of time if there is a signature at all--and if
665 ## there isn't, there is no way of knowing whether the signature is valid,
666 ## or if there isn't a signature at all. So this prepopulates the internal
667 ## errstr with the string "No Signature\n"--if there is a signature, and
668 ## there is an error during verification, the second return value will be
669 ## undef, and the errstr will contain the error that occurred. If there is
670 ## *not* a signature, the second return value will still be undef, but
671 ## the errstr is guaranteed to be "No Signature\n".
672 2 $pgp->error("No Signature");
673  
674 2 my($valid, $sig);
675 2 $msg = Crypt::OpenPGP::Message->new( Data => $data,
676 IsPacketStream => 1 );
677 2 @pieces = $msg->pieces;
678  
679 ## If the first packet in the decrypted data is compressed,
680 ## decompress it and set the list of packets to the result.
681 2 ▶ if (ref($pieces[0]) eq 'Crypt::OpenPGP::Compressed') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pieces[0] eq "Crypt::OpenPGP::Compressed") 0 2 2
682 0 ▶ $data = $pieces[0]->decompress or
Branch: 1 branch
BranchTrueFalseTotal
$data = $pieces[0]->decompress or return $pgp->error('Decompression error: ' . $pieces[0]->errstr) 0 0 0
683 return $pgp->error("Decompression error: " . $pieces[0]->errstr);
684 0 $msg = Crypt::OpenPGP::Message->new( Data => $data,
685 IsPacketStream => 1 );
686 0 @pieces = $msg->pieces;
687 }
688  
689 2 my($pt);
690 2 ▶ if (ref($pieces[0]) eq 'Crypt::OpenPGP::OnePassSig' ||
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pieces[0] eq 'Crypt::OpenPGP::OnePassSig' or ref $pieces[0] eq 'Crypt::OpenPGP::Signature') { } 0 2 2
Condition33%1/3
ref $pieces[0] eq "Crypt::OpenPGP::OnePassSig" or ref $pieces[0] eq "Crypt::OpenPGP::Signature"
l!l&&r!l&&!r
002
MC/DC0%0/2
ref $pieces[0] eq "Crypt::OpenPGP::OnePassSig" or ref $pieces[0] eq "Crypt::OpenPGP::Signature"
ref $pieces[0] eq "Crypt::OpenPGP::OnePassSig" ref $pieces[0] eq "Crypt::OpenPGP::Signature"
Truth table33%1/3
A or B
A= ref $pieces[0] eq "Crypt::OpenPGP::OnePassSig"B= ref $pieces[0] eq "Crypt::OpenPGP::Signature"
ABresult
1X1
011
000
691 ref($pieces[0]) eq 'Crypt::OpenPGP::Signature') {
692 0 $pt = $pieces[1];
693 0 ▶ if ($wants_verify) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wants_verify) 0 0 0
694 0 ($valid, $sig) =
695 $pgp->verify( Signature => $data, IsPacketStream => 1 );
696 }
697 } else {
698 2 $pt = $pieces[0];
699 }
700  
701 2 ▶ $wants_verify ? ($pt->data, $valid, $sig) : $pt->data;
Branch: 1 branch
BranchTrueFalseTotal
$wants_verify ? : 1 1 2
702 }
703  
704 2 ▶sub keygen {
Subroutine keygen: called CC=9 SCAR=22.2
Pod documented
705 2 my $pgp = shift;
706 2 my %param = @_;
707 2 require Crypt::OpenPGP::Certificate;
708 2 require Crypt::OpenPGP::Key;
709 2 require Crypt::OpenPGP::KeyBlock;
710 2 require Crypt::OpenPGP::Signature;
711 2 require Crypt::OpenPGP::UserID;
712  
713 $param{Type} or
714 2 ▶ return $pgp->error("Need a Type of key to generate");
Branch: 1 branch
BranchTrueFalseTotal
$param{"Type"} or return $pgp->error("Need a Type of key to generate") 2 0 2
715 2 ▶ $param{Size} ||= 1024;
Condition50%1/2
$param{"Size"} ||= 1024
l!l
20
MC/DC0%0/1
$param{"Size"} ||= 1024
$param{"Size"}
Truth table50%1/2
A
A= $param{"Size"}
Aresult
11
00
716 2 ▶ $param{Version} ||= 4;
Condition50%1/2
$param{"Version"} ||= 4
l!l
02
MC/DC0%0/1
$param{"Version"} ||= 4
$param{"Version"}
Truth table50%1/2
A
A= $param{"Version"}
Aresult
11
00
717 2 ▶ $param{Version} = 3 if $param{Type} eq 'RSA';
Branch: 1 branch
BranchTrueFalseTotal
if $param{"Type"} eq "RSA" 1 1 2
718  
719 2 my $kb_pub = Crypt::OpenPGP::KeyBlock->new;
720 2 my $kb_sec = Crypt::OpenPGP::KeyBlock->new;
721  
722 2 my($pub, $sec) = Crypt::OpenPGP::Key->keygen($param{Type}, %param);
723 2 ▶ die Crypt::OpenPGP::Key->errstr unless $pub && $sec;
Branch: 1 branch
BranchTrueFalseTotal
unless $pub and $sec 1 1 2
Condition66%2/3
$pub and $sec
!ll&&!rl&&r
101
MC/DC50%1/2
$pub and $sec
$pub $sec
Truth table66%2/3
A and B
A= $pubB= $sec
ABresult
0X0
100
111
724 my $pubcert = Crypt::OpenPGP::Certificate->new(
725 Key => $pub,
726 Version => $param{Version}
727 1 ▶ ) or
Branch: 1 branch
BranchTrueFalseTotal
my $pubcert = "Crypt::OpenPGP::Certificate"->new("Key", $pub, "Version", $param{"Version"}) or die "Crypt::OpenPGP::Certificate"->errstr 1 0 1
728 die Crypt::OpenPGP::Certificate->errstr;
729 my $seccert = Crypt::OpenPGP::Certificate->new(
730 Key => $sec,
731 Passphrase => $param{Passphrase},
732 Version => $param{Version}
733 1 ▶ ) or
Branch: 1 branch
BranchTrueFalseTotal
my $seccert = "Crypt::OpenPGP::Certificate"->new("Key", $sec, "Passphrase", $param{"Passphrase"}, "Version", $param{"Version"}) or die "Crypt::OpenPGP::Certificate"->errstr 1 0 1
734 die Crypt::OpenPGP::Certificate->errstr;
735 1 $kb_pub->add($pubcert);
736 1 $kb_sec->add($seccert);
737  
738 1 my $id = Crypt::OpenPGP::UserID->new( Identity => $param{Identity} );
739 1 $kb_pub->add($id);
740 1 $kb_sec->add($id);
741  
742 my $sig = Crypt::OpenPGP::Signature->new(
743 Data => [ $pubcert, $id ],
744 Key => $seccert,
745 Version => $param{Version},
746 1 Type => 0x13,
747 );
748 1 $kb_pub->add($sig);
749 1 $kb_sec->add($sig);
750  
751 1 ($kb_pub, $kb_sec);
752 }
753  
754 0 ▶sub _read_files {
Subroutine _read_files: not called CC=6 SCAR=37.4
755 0 my $pgp = shift;
756 0 ▶ return $pgp->error("No files specified") unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
757 0 my @files = @_;
758 0 my $data = '';
759 0 for my $file (@files) {
760 0 ▶ $file ||= '';
Condition0%0/2
$file ||= ""
l!l
00
MC/DC0%0/1
$file ||= ""
$file
Truth table0%0/2
A
A= $file
Aresult
11
00
761 0 ▶ open (my $fh, '<', $file) or return $pgp->error("Error opening $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return $pgp->error("Error opening ${file}: $!") 0 0 0
762 0 binmode $fh;
763 0 { local $/; $data .= <$fh> }
764 0 ▶ close $fh or warn "Warning: Got error closing $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or warn "Warning: Got error closing ${file}: $!" 0 0 0
765 }
766 0 $data;
767 }
768  
769 {
770 11 my @MERGE_CONFIG = qw( Cipher Armour Digest );
771 5 ▶ sub _merge_compat {
Subroutine _merge_compat: called CC=8 SCAR=35.6
772 5 my $pgp = shift;
773 5 my($param, $meth) = @_;
774 5 ▶ my $compat = $param->{Compat} || $pgp->{cfg}->get('Compat') || return 1;
Condition40%2/5
$param->{'Compat'} || $pgp->{'cfg'}->get('Compat') || (return 1)
l!l
05
$param->{"Compat"} or $pgp->{"cfg"}->get("Compat")
l!l&&r!l&&!r
005
MC/DC0%0/2
$param->{'Compat'} || $pgp->{'cfg'}->get('Compat') || (return 1)
$param->{"Compat"} $pgp->{"cfg"}->get("Compat")
Truth table33%1/3
A or B
A= $param->{"Compat"}B= $pgp->{"cfg"}->get("Compat")
ABresult
1X1
011
000
775 0 ▶ my $ref = $COMPAT{$compat}{$meth} or
Branch: 1 branch
BranchTrueFalseTotal
my $ref = $Crypt::OpenPGP::COMPAT{$compat}{$meth} or return $pgp->error("No settings for Compat class '${compat}'") 0 0 0
776 return $pgp->error("No settings for Compat class '$compat'");
777 0 for my $arg (keys %$ref) {
778 0 ▶ $param->{$arg} = $ref->{$arg} unless exists $param->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param->{$arg} 0 0 0
779 }
780 0 for my $key (@MERGE_CONFIG) {
781 $param->{$key} = $pgp->{cfg}->get($key)
782 0 ▶ unless exists $param->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param->{$key} 0 0 0
783 }
784 0 1;
785 }
786 }
787  
788 11 1;
789  
790 __END__
↑ Summary OpenPGP/Certificate.pm »