| 1 | package Crypt::OpenPGP::Certificate; | ||||||||||||||||||||||||||||
| 2 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 8 | our $VERSION = '1.21'; # VERSION | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Crypt::OpenPGP::S2k; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Crypt::OpenPGP::Key::Public; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Crypt::OpenPGP::Key::Secret; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Crypt::OpenPGP::Buffer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Crypt::OpenPGP::Util qw( mp2bin bin2mp bitsize ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use Crypt::OpenPGP::Constants qw( DEFAULT_CIPHER | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | PGP_PKT_PUBLIC_KEY | ||||||||||||||||||||||||||||
| 14 | PGP_PKT_PUBLIC_SUBKEY | ||||||||||||||||||||||||||||
| 15 | PGP_PKT_SECRET_KEY | ||||||||||||||||||||||||||||
| 16 | 8 | PGP_PKT_SECRET_SUBKEY ); | |||||||||||||||||||||||||||
| 17 | 8 | ▶ | use Crypt::OpenPGP::Cipher; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 8 | ▶ | use parent qw( Crypt::OpenPGP::ErrorHandler ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | { | ||||||||||||||||||||||||||||
| 21 | 8 | our %KEY_ALG = ( | |||||||||||||||||||||||||||
| 22 | 1 => 'RSA', | ||||||||||||||||||||||||||||
| 23 | 16 => 'ElGamal', | ||||||||||||||||||||||||||||
| 24 | 17 => 'DSA', | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | 8 | my @PKT_TYPES = ( | |||||||||||||||||||||||||||
| 27 | PGP_PKT_PUBLIC_KEY, | ||||||||||||||||||||||||||||
| 28 | PGP_PKT_PUBLIC_SUBKEY, | ||||||||||||||||||||||||||||
| 29 | PGP_PKT_SECRET_KEY, | ||||||||||||||||||||||||||||
| 30 | PGP_PKT_SECRET_SUBKEY | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | 2 | ▶ | sub pkt_type { | ||||||||||||||||||||||||||
|
Subroutine
pkt_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 2 | my $cert = shift; | |||||||||||||||||||||||||||
| 34 | 2 | $PKT_TYPES[ ($cert->{is_secret} << 1) | $cert->{is_subkey} ]; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 8 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 13 | my $class = shift; | |||||||||||||||||||||||||||
| 40 | 13 | my $cert = bless { }, $class; | |||||||||||||||||||||||||||
| 41 | 13 | $cert->init(@_); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 13 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=11 SCAR=25.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 13 | my $cert = shift; | |||||||||||||||||||||||||||
| 46 | 13 | my %param = @_; | |||||||||||||||||||||||||||
| 47 | 13 | ▶ | if (my $key = $param{Key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | $cert->{version} = $param{Version} || 4; | ||||||||||||||||||||||||||
|
Condition50%1/2
$param{'Version'} || 4
MC/DC0%0/1
$param{'Version'} || 4
$param{'Version'}
Truth table50%1/2
A
A= $param{'Version'}
| |||||||||||||||||||||||||||||
| 49 | 2 | $cert->{key} = $key; | |||||||||||||||||||||||||||
| 50 | 2 | our %KEY_ALG; | |||||||||||||||||||||||||||
| 51 | 2 | ▶ | eval require Crypt::DSA::GMP if $KEY_ALG{$key->alg_id} eq 'DSA'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 2 | $cert->{is_secret} = $key->is_secret; | |||||||||||||||||||||||||||
| 53 | 2 | ▶ | $cert->{is_subkey} = $param{Subkey} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$param{'Subkey'} || 0
MC/DC0%0/1
$param{'Subkey'} || 0
$param{'Subkey'}
Truth table50%1/2
A
A= $param{'Subkey'}
| |||||||||||||||||||||||||||||
| 54 | 2 | $cert->{timestamp} = time; | |||||||||||||||||||||||||||
| 55 | 2 | $cert->{pk_alg} = $key->alg_id; | |||||||||||||||||||||||||||
| 56 | 2 | ▶ | if ($cert->{version} < 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 2 | ▶ | $cert->{validity} = $param{Validity} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$param{'Validity'} || 0
MC/DC0%0/1
$param{'Validity'} || 0
$param{'Validity'}
Truth table50%1/2
A
A= $param{'Validity'}
| |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | $key->alg eq 'RSA' or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | return (ref $cert)->error("Version 3 keys must be RSA"); | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 2 | $cert->{s2k} = Crypt::OpenPGP::S2k->new('Salt_Iter'); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | ▶ | if ($cert->{is_secret}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | $param{Passphrase} or | ||||||||||||||||||||||||||||
| 65 | 1 | ▶ | return (ref $cert)->error("Need a Passphrase to lock key"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 1 | ▶ | $cert->{cipher} = $param{Cipher} || DEFAULT_CIPHER; | ||||||||||||||||||||||||||
|
Condition33%1/3
$param{'Cipher'} || &DEFAULT_CIPHER()
MC/DC0%0/2
$param{'Cipher'} || &DEFAULT_CIPHER()
$param{'Cipher'}
&DEFAULT_CIPHER()
Truth table33%1/3
A || B
A= $param{'Cipher'}B= &DEFAULT_CIPHER()
| |||||||||||||||||||||||||||||
| 67 | 1 | $cert->lock($param{Passphrase}); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 13 | $cert; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub type { $_[0]->{type} } | ||||||||||||||||||||||||||
|
Subroutine
type: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub version { $_[0]->{version} } | ||||||||||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub timestamp { $_[0]->{timestamp} } | ||||||||||||||||||||||||||
|
Subroutine
timestamp: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub validity { $_[0]->{validity} } | ||||||||||||||||||||||||||
|
Subroutine
validity: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 8 | ▶ | sub pk_alg { $_[0]->{pk_alg} } | ||||||||||||||||||||||||||
|
Subroutine
pk_alg: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 8 | ▶ | sub key { $_[0]->{key} } | ||||||||||||||||||||||||||
|
Subroutine
key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | sub is_secret { $_[0]->{key}->is_secret } | ||||||||||||||||||||||||||
|
Subroutine
is_secret: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 9 | ▶ | sub is_subkey { $_[0]->{is_subkey} } | ||||||||||||||||||||||||||
|
Subroutine
is_subkey: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | sub is_protected { $_[0]->{is_protected} } | ||||||||||||||||||||||||||
|
Subroutine
is_protected: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub can_encrypt { $_[0]->{key}->can_encrypt } | ||||||||||||||||||||||||||
|
Subroutine
can_encrypt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub can_sign { $_[0]->{key}->can_sign } | ||||||||||||||||||||||||||
|
Subroutine
can_sign: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub uid { | ||||||||||||||||||||||||||
|
Subroutine
uid: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 0 | my $cert = shift; | |||||||||||||||||||||||||||
| 86 | 0 | ▶ | $cert->{_uid} = shift if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | $cert->{_uid}; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 8 | ▶ | sub public_cert { | ||||||||||||||||||||||||||
|
Subroutine
public_cert: called CC=5 SCAR=22.5
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 8 | my $cert = shift; | |||||||||||||||||||||||||||
| 92 | 8 | our %KEY_ALG; | |||||||||||||||||||||||||||
| 93 | 8 | ▶ | eval require Crypt::DSA::GMP if $KEY_ALG{$cert->pk_alg} eq 'DSA'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | return $cert unless $cert->is_secret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | my $pub = (ref $cert)->new; | |||||||||||||||||||||||||||
| 96 | 0 | for my $f (qw( version timestamp pk_alg is_subkey )) { | |||||||||||||||||||||||||||
| 97 | 0 | $pub->{$f} = $cert->{$f}; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $pub->{validity} = $cert->{validity} if $cert->{version} < 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $pub->{key} = $cert->{key}->public_key; | |||||||||||||||||||||||||||
| 101 | 0 | $pub; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 18 | ▶ | sub key_id { | ||||||||||||||||||||||||||
|
Subroutine
key_id: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 18 | my $cert = shift; | |||||||||||||||||||||||||||
| 106 | 18 | ▶ | unless ($cert->{key_id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 13 | ▶ | if ($cert->{version} < 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 3 | $cert->{key_id} = substr(mp2bin($cert->{key}->n), -8); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||
| 111 | 10 | $cert->{key_id} = substr($cert->fingerprint, -8); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 12 | $cert->{key_id}; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub key_id_hex { uc unpack 'H*', $_[0]->key_id } | ||||||||||||||||||||||||||
|
Subroutine
key_id_hex: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 10 | ▶ | sub fingerprint { | ||||||||||||||||||||||||||
|
Subroutine
fingerprint: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 10 | my $cert = shift; | |||||||||||||||||||||||||||
| 121 | 10 | ▶ | unless ($cert->{fingerprint}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 6 | ▶ | if ($cert->{version} < 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | my $dgst = Crypt::OpenPGP::Digest->new('MD5'); | |||||||||||||||||||||||||||
| 124 | $cert->{fingerprint} = | ||||||||||||||||||||||||||||
| 125 | 0 | $dgst->hash(mp2bin($cert->{key}->n) . mp2bin($cert->{key}->e)); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | else { | ||||||||||||||||||||||||||||
| 128 | 6 | my $data = $cert->public_cert->save; | |||||||||||||||||||||||||||
| 129 | 0 | $cert->{fingerprint} = _gen_v4_fingerprint($data); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 4 | $cert->{fingerprint}; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub fingerprint_hex { uc unpack 'H*', $_[0]->fingerprint } | ||||||||||||||||||||||||||
|
Subroutine
fingerprint_hex: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub fingerprint_words { | ||||||||||||||||||||||||||
|
Subroutine
fingerprint_words: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 0 | require Crypt::OpenPGP::Words; | |||||||||||||||||||||||||||
| 139 | 0 | Crypt::OpenPGP::Words->encode($_[0]->fingerprint); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 4 | ▶ | sub _gen_v4_fingerprint { | ||||||||||||||||||||||||||
|
Subroutine
_gen_v4_fingerprint: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 4 | my($data) = @_; | |||||||||||||||||||||||||||
| 144 | 4 | my $buf = Crypt::OpenPGP::Buffer->new; | |||||||||||||||||||||||||||
| 145 | 4 | $buf->put_int8(0x99); | |||||||||||||||||||||||||||
| 146 | 4 | $buf->put_int16(length $data); | |||||||||||||||||||||||||||
| 147 | 4 | $buf->put_bytes($data); | |||||||||||||||||||||||||||
| 148 | 4 | my $dgst = Crypt::OpenPGP::Digest->new('SHA1'); | |||||||||||||||||||||||||||
| 149 | 4 | $dgst->hash($buf->bytes); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 11 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=14 SCAR=28.5
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 11 | my $class = shift; | |||||||||||||||||||||||||||
| 154 | 11 | my($buf, $secret, $subkey) = @_; | |||||||||||||||||||||||||||
| 155 | 11 | my $cert = $class->new; | |||||||||||||||||||||||||||
| 156 | 11 | $cert->{is_secret} = $secret; | |||||||||||||||||||||||||||
| 157 | 11 | $cert->{is_subkey} = $subkey; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 11 | $cert->{version} = $buf->get_int8; | |||||||||||||||||||||||||||
| 160 | 11 | $cert->{timestamp} = $buf->get_int32; | |||||||||||||||||||||||||||
| 161 | 11 | ▶ | if ($cert->{version} < 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 1 | $cert->{validity} = $buf->get_int16; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 11 | $cert->{pk_alg} = $buf->get_int8; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 11 | ▶ | my $key_class = 'Crypt::OpenPGP::Key::' . ($secret ? 'Secret' : 'Public'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 11 | my $key = $cert->{key} = $key_class->new($cert->{pk_alg}); | |||||||||||||||||||||||||||
| 168 | 11 | ▶ | if( ! defined $key ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 6 | $cert->{unparsed_key} = $buf->get_bytes( $buf->length - $buf->offset ); | |||||||||||||||||||||||||||
| 170 | 6 | return $cert; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 5 | my @pub = $key->public_props; | |||||||||||||||||||||||||||
| 174 | 5 | for my $e (@pub) { | |||||||||||||||||||||||||||
| 175 | 10 | $key->$e($buf->get_mp_int); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 5 | ▶ | if ($cert->{version} >= 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 4 | my $data = $buf->bytes(0, $buf->offset); | |||||||||||||||||||||||||||
| 180 | 4 | $cert->{fingerprint} = _gen_v4_fingerprint($data); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 5 | ▶ | if ($secret) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 2 | $cert->{cipher} = $buf->get_int8; | |||||||||||||||||||||||||||
| 185 | 2 | ▶ | if ($cert->{cipher}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 2 | $cert->{is_protected} = 1; | |||||||||||||||||||||||||||
| 187 | 2 | ▶ | if ($cert->{cipher} == 255 || $cert->{cipher} == 254) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cert->{"cipher"} == 255 or $cert->{"cipher"} == 254
MC/DC0%0/2
$cert->{"cipher"} == 255 or $cert->{"cipher"} == 254
$cert->{"cipher"} == 255
$cert->{"cipher"} == 254
Truth table33%1/3
A or B
A= $cert->{"cipher"} == 255B= $cert->{"cipher"} == 254
| |||||||||||||||||||||||||||||
| 188 | 2 | $cert->{sha1check} = $cert->{cipher} == 254; | |||||||||||||||||||||||||||
| 189 | 2 | $cert->{cipher} = $buf->get_int8; | |||||||||||||||||||||||||||
| 190 | 2 | $cert->{s2k} = Crypt::OpenPGP::S2k->parse($buf); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | else { | ||||||||||||||||||||||||||||
| 193 | 0 | $cert->{s2k} = Crypt::OpenPGP::S2k->new('Simple'); | |||||||||||||||||||||||||||
| 194 | 0 | $cert->{s2k}->set_hash('MD5'); | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 2 | $cert->{iv} = $buf->get_bytes(8); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 2 | ▶ | if ($cert->{is_protected}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 2 | ▶ | if ($cert->{version} < 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 0 | $cert->{encrypted} = {}; | |||||||||||||||||||||||||||
| 203 | 0 | my @sec = $key->secret_props; | |||||||||||||||||||||||||||
| 204 | 0 | for my $e (@sec) { | |||||||||||||||||||||||||||
| 205 | 0 | my $h = $cert->{encrypted}{"${e}h"} = $buf->get_bytes(2); | |||||||||||||||||||||||||||
| 206 | 0 | $cert->{encrypted}{"${e}b"} = | |||||||||||||||||||||||||||
| 207 | $buf->get_bytes(int((unpack('n', $h)+7)/8)); | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 0 | $cert->{csum} = $buf->get_int16; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | else { | ||||||||||||||||||||||||||||
| 212 | $cert->{encrypted} = | ||||||||||||||||||||||||||||
| 213 | 2 | $buf->get_bytes($buf->length - $buf->offset); | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | else { | ||||||||||||||||||||||||||||
| 217 | 0 | my @sec = $key->secret_props; | |||||||||||||||||||||||||||
| 218 | 0 | for my $e (@sec) { | |||||||||||||||||||||||||||
| 219 | 0 | $key->$e($buf->get_mp_int); | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 5 | $cert; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 4 | ▶ | sub save { | ||||||||||||||||||||||||||
|
Subroutine
save: called CC=10 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | 4 | my $cert = shift; | |||||||||||||||||||||||||||
| 229 | 4 | my $buf = Crypt::OpenPGP::Buffer->new; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 4 | $buf->put_int8($cert->{version}); | |||||||||||||||||||||||||||
| 232 | 4 | $buf->put_int32($cert->{timestamp}); | |||||||||||||||||||||||||||
| 233 | 4 | ▶ | if ($cert->{version} < 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 3 | $buf->put_int16($cert->{validity}); | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | 4 | $buf->put_int8($cert->{pk_alg}); | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 4 | my $key = $cert->{key}; | |||||||||||||||||||||||||||
| 239 | 4 | ▶ | if( ! defined $key && defined $cert->{unparsed_key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $key and defined $cert->{"unparsed_key"}
MC/DC50%1/2
not defined $key and defined $cert->{"unparsed_key"}
not defined $key
defined $cert->{"unparsed_key"}
Truth table66%2/3
not(A) and B
A= not defined $keyB= defined $cert->{"unparsed_key"}
| |||||||||||||||||||||||||||||
| 240 | 1 | $buf->put_bytes($cert->{unparsed_key}); | |||||||||||||||||||||||||||
| 241 | 1 | return $buf->bytes; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 3 | my @pub = $key->public_props; | |||||||||||||||||||||||||||
| 245 | 3 | for my $e (@pub) { | |||||||||||||||||||||||||||
| 246 | 6 | $buf->put_mp_int($key->$e()); | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 3 | ▶ | if ($cert->{key}->is_secret) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 0 | ▶ | if ($cert->{cipher}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 0 | $buf->put_int8(255); | |||||||||||||||||||||||||||
| 252 | 0 | $buf->put_int8($cert->{cipher}); | |||||||||||||||||||||||||||
| 253 | 0 | $buf->append($cert->{s2k}->save); | |||||||||||||||||||||||||||
| 254 | 0 | $buf->put_bytes($cert->{iv}); | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | ▶ | if ($cert->{version} < 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 0 | my @sec = $key->secret_props; | |||||||||||||||||||||||||||
| 258 | 0 | for my $e (@sec) { | |||||||||||||||||||||||||||
| 259 | 0 | $buf->put_bytes($cert->{encrypted}{"${e}h"}); | |||||||||||||||||||||||||||
| 260 | 0 | $buf->put_bytes($cert->{encrypted}{"${e}b"}); | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | 0 | $buf->put_int16($cert->{csum}); | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | else { | ||||||||||||||||||||||||||||
| 265 | 0 | $buf->put_bytes($cert->{encrypted}); | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | else { | ||||||||||||||||||||||||||||
| 269 | 0 | my @sec = $key->secret_props; | |||||||||||||||||||||||||||
| 270 | 0 | for my $e (@sec) { | |||||||||||||||||||||||||||
| 271 | 0 | $key->$e($buf->get_mp_int); | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | 3 | $buf->bytes; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 1 | ▶ | sub v3_checksum { | ||||||||||||||||||||||||||
|
Subroutine
v3_checksum: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 279 | 1 | my $cert = shift; | |||||||||||||||||||||||||||
| 280 | 1 | my $k = $cert->{encrypted}; | |||||||||||||||||||||||||||
| 281 | 1 | my $sum = 0; | |||||||||||||||||||||||||||
| 282 | 1 | my @sec = $cert->{key}->secret_props; | |||||||||||||||||||||||||||
| 283 | 1 | for my $e (@sec) { | |||||||||||||||||||||||||||
| 284 | 4 | $sum += unpack '%16C*', $k->{"${e}h"}; | |||||||||||||||||||||||||||
| 285 | 4 | $sum += unpack '%16C*', $k->{"${e}b"}; | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | 1 | $sum & 0xFFFF; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 0 | ▶ | sub unlock { | ||||||||||||||||||||||||||
|
Subroutine
unlock: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||
| 291 | 0 | my $cert = shift; | |||||||||||||||||||||||||||
| 292 | 0 | ▶ | return 1 unless $cert->{is_secret} && $cert->{is_protected}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cert->{"is_secret"} and $cert->{"is_protected"}
MC/DC0%0/2
$cert->{"is_secret"} and $cert->{"is_protected"}
$cert->{"is_secret"}
$cert->{"is_protected"}
Truth table0%0/3
A and B
A= $cert->{"is_secret"}B= $cert->{"is_protected"}
| |||||||||||||||||||||||||||||
| 293 | 0 | my($passphrase) = @_; | |||||||||||||||||||||||||||
| 294 | 0 | ▶ | my $cipher = Crypt::OpenPGP::Cipher->new($cert->{cipher}) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | return $cert->error( Crypt::OpenPGP::Cipher->errstr ); | ||||||||||||||||||||||||||||
| 296 | 0 | my $key = $cert->{s2k}->generate($passphrase, $cipher->keysize); | |||||||||||||||||||||||||||
| 297 | 0 | $cipher->init($key, $cert->{iv}); | |||||||||||||||||||||||||||
| 298 | 0 | my @sec = $cert->{key}->secret_props; | |||||||||||||||||||||||||||
| 299 | 0 | ▶ | if ($cert->{version} < 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 0 | my $k = $cert->{encrypted}; | |||||||||||||||||||||||||||
| 301 | 0 | my $r = {}; | |||||||||||||||||||||||||||
| 302 | 0 | for my $e (@sec) { | |||||||||||||||||||||||||||
| 303 | 0 | $r->{$e} = $k->{"${e}b"}; | |||||||||||||||||||||||||||
| 304 | 0 | $k->{"${e}b"} = $cipher->decrypt($r->{$e}); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | 0 | ▶ | unless ($cert->{csum} == $cert->v3_checksum) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 0 | $k->{"${_}b"} = $r->{$_} for @sec; | |||||||||||||||||||||||||||
| 308 | 0 | return $cert->error("Bad checksum"); | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | 0 | for my $e (@sec) { | |||||||||||||||||||||||||||
| 311 | 0 | $cert->{key}->$e(bin2mp($k->{"${e}b"})); | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | 0 | ▶ | unless ($cert->{key}->check) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | 0 | $k->{"${_}b"} = $r->{$_} for @sec; | |||||||||||||||||||||||||||
| 315 | 0 | return $cert->error("p*q != n"); | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | else { | ||||||||||||||||||||||||||||
| 319 | 0 | my $decrypted = $cipher->decrypt($cert->{encrypted}); | |||||||||||||||||||||||||||
| 320 | 0 | ▶ | if ($cert->{sha1check}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | 0 | my $dgst = Crypt::OpenPGP::Digest->new('SHA1'); | |||||||||||||||||||||||||||
| 322 | 0 | my $csum = substr $decrypted, -20, 20, ''; | |||||||||||||||||||||||||||
| 323 | 0 | ▶ | unless ($dgst->hash($decrypted) eq $csum) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 0 | return $cert->error("Bad SHA-1 hash"); | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | } else { | ||||||||||||||||||||||||||||
| 327 | 0 | my $csum = unpack "n", substr $decrypted, -2, 2, ''; | |||||||||||||||||||||||||||
| 328 | 0 | my $gen_csum = unpack '%16C*', $decrypted; | |||||||||||||||||||||||||||
| 329 | 0 | ▶ | unless ($csum == $gen_csum) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 0 | return $cert->error("Bad simple checksum"); | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | 0 | my $buf = Crypt::OpenPGP::Buffer->new; | |||||||||||||||||||||||||||
| 334 | 0 | $buf->append($decrypted); | |||||||||||||||||||||||||||
| 335 | 0 | for my $e (@sec) { | |||||||||||||||||||||||||||
| 336 | 0 | $cert->{key}->$e( $buf->get_mp_int ); | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 0 | $cert->{is_protected} = 0; | |||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 0 | 1; | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 1 | ▶ | sub lock { | ||||||||||||||||||||||||||
|
Subroutine
lock: called CC=7 SCAR=21.4
Pod
documented
| |||||||||||||||||||||||||||||
| 346 | 1 | my $cert = shift; | |||||||||||||||||||||||||||
| 347 | 1 | ▶ | return if !$cert->{is_secret} || $cert->{is_protected}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $cert->{"is_secret"} or $cert->{"is_protected"}
MC/DC0%0/2
not $cert->{"is_secret"} or $cert->{"is_protected"}
not $cert->{"is_secret"}
$cert->{"is_protected"}
Truth table33%1/3
not(A) or B
A= not $cert->{"is_secret"}B= $cert->{"is_protected"}
| |||||||||||||||||||||||||||||
| 348 | 1 | my($passphrase) = @_; | |||||||||||||||||||||||||||
| 349 | 1 | my $cipher = Crypt::OpenPGP::Cipher->new($cert->{cipher}); | |||||||||||||||||||||||||||
| 350 | 1 | my $sym_key = $cert->{s2k}->generate($passphrase, $cipher->keysize); | |||||||||||||||||||||||||||
| 351 | 1 | $cert->{iv} = Crypt::OpenPGP::Util::get_random_bytes(8); | |||||||||||||||||||||||||||
| 352 | 1 | $cipher->init($sym_key, $cert->{iv}); | |||||||||||||||||||||||||||
| 353 | 1 | my @sec = $cert->{key}->secret_props; | |||||||||||||||||||||||||||
| 354 | 1 | ▶ | if ($cert->{version} < 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 1 | my $k = $cert->{encrypted} = {}; | |||||||||||||||||||||||||||
| 356 | 1 | my $key = $cert->key; | |||||||||||||||||||||||||||
| 357 | 1 | for my $e (@sec) { | |||||||||||||||||||||||||||
| 358 | 4 | $k->{"${e}b"} = mp2bin($key->$e()); | |||||||||||||||||||||||||||
| 359 | 4 | $k->{"${e}h"} = pack 'n', bitsize($key->$e()); | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | 1 | $cert->{csum} = $cert->v3_checksum; | |||||||||||||||||||||||||||
| 362 | 1 | for my $e (@sec) { | |||||||||||||||||||||||||||
| 363 | 4 | $k->{"${e}b"} = $cipher->encrypt( $k->{"${e}b"} ); | |||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | else { | ||||||||||||||||||||||||||||
| 367 | 0 | my $buf = Crypt::OpenPGP::Buffer->new; | |||||||||||||||||||||||||||
| 368 | 0 | for my $e (@sec) { | |||||||||||||||||||||||||||
| 369 | 0 | $buf->put_mp_int($cert->{key}->$e()); | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | 0 | my $cnt = $buf->bytes; | |||||||||||||||||||||||||||
| 372 | 0 | $cnt .= pack 'n', unpack '%16C*', $cnt; | |||||||||||||||||||||||||||
| 373 | 0 | $cert->{encrypted} = $cipher->encrypt($cnt); | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 1 | $cert->{is_protected} = 1; | |||||||||||||||||||||||||||
| 377 | 1 | 1; | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 8 | 1; | |||||||||||||||||||||||||||
| 381 | __END__ | ||||||||||||||||||||||||||||