| 1 | package PGP::Finger::Key; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | # ABSTRACT: class for holding and parsing pgp keys | ||||||||||
| 6 | 1 | our $VERSION = '1.1'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Digest::SHA qw(sha256_hex sha224_hex); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Digest::CRC qw(crcopenpgparmor_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use MIME::Base64; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | use overload | ||||||||||
| 13 | 0 | ▶ | q{""} => sub { $_[0]->armored }, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 14 | 1 | fallback => 1; | |||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | 1 | has 'data' => ( is => 'ro' ); | |||||||||
| 18 | |||||||||||
| 19 | has 'attributes' => ( is => 'ro', isa => 'HashRef[Str]', lazy => 1, | ||||||||||
| 20 | traits => [ 'Hash' ], | ||||||||||
| 21 | 0 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 22 | 1 | handles => { | |||||||||
| 23 | set_attr => 'set', | ||||||||||
| 24 | get_attr => 'get', | ||||||||||
| 25 | has_attr => 'exists', | ||||||||||
| 26 | has_attrs => 'count', | ||||||||||
| 27 | }, | ||||||||||
| 28 | ); | ||||||||||
| 29 | |||||||||||
| 30 | has '_version' => ( is => 'ro', isa => 'Str', lazy => 1, | ||||||||||
| 31 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=11.6
| |||||||||||
| 32 | 0 | my $version; | |||||||||
| 33 | { | ||||||||||
| 34 | 0 | ▶ | no strict 'vars'; # is only declared in build | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||
| 35 | 0 | ▶ | $version = defined $VERSION ? $VERSION : 'head'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | } | ||||||||||
| 37 | 0 | return 'pgpfinger ('.$version.')'; | |||||||||
| 38 | }, | ||||||||||
| 39 | 1 | ); | |||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | sub merge_key { | ||||||||
|
Subroutine
merge_key: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 42 | 0 | my ( $self, @keys ) = @_; | |||||||||
| 43 | |||||||||||
| 44 | 0 | foreach my $key ( @keys ) { | |||||||||
| 45 | 0 | $self->merge_attributes( $key->attributes ); | |||||||||
| 46 | } | ||||||||||
| 47 | 0 | return; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | sub merge_attributes { | ||||||||
|
Subroutine
merge_attributes: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 51 | 0 | my ( $self, $attr ) = @_; | |||||||||
| 52 | |||||||||||
| 53 | 0 | foreach my $name ( keys %$attr ) { | |||||||||
| 54 | 0 | ▶ | if( ! $self->has_attr( $name ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | $self->set_attr( $name => $attr->{$name} ); | |||||||||
| 56 | } else { | ||||||||||
| 57 | 0 | $self->set_attr( $name => $self->get_attr($name).', '.$attr->{$name} ); | |||||||||
| 58 | } | ||||||||||
| 59 | } | ||||||||||
| 60 | 0 | return; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | sub new_armored { | ||||||||
|
Subroutine
new_armored: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||
| 64 | 0 | my ( $class, %options ) = @_; | |||||||||
| 65 | 0 | my $armored = $options{'data'}; | |||||||||
| 66 | 0 | ▶ | if( ! defined $armored ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 0 | die('new_armored called without data'); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 0 | my $b64 = ''; | |||||||||
| 71 | 0 | my @lines = split( /\r?\n/, $armored ); | |||||||||
| 72 | 0 | my $line; | |||||||||
| 73 | 0 | ▶ | while( $line = shift @lines ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | ▶ | if( $line =~ /^\s*$/ ) { next; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 0 | ▶ | if( $line =~ /^-----BEGIN /) { last; } # here we start | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 0 | die('data before BEGIN line in PEM input'); | |||||||||
| 77 | } | ||||||||||
| 78 | 0 | ▶ | if( ! @lines ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | die('end of PEM before -----BEGIN line has been found'); | |||||||||
| 80 | } | ||||||||||
| 81 | 0 | ▶ | while( $line = shift @lines ) { # get headers if present | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | ▶ | if( $line =~ /:/ ) { next; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 0 | ▶ | if( $line =~ /^\s*$/ ) { next; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | last; | |||||||||
| 85 | } | ||||||||||
| 86 | 0 | ▶ | if( ! @lines ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | die('end of PEM before -----END line has been found'); | |||||||||
| 88 | } | ||||||||||
| 89 | 0 | $b64 .= $line; | |||||||||
| 90 | 0 | ▶ | while( $line = shift @lines ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 0 | ▶ | if( $line =~ /^-----END /) { last; } # END | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | $b64 .= $line; | |||||||||
| 93 | } | ||||||||||
| 94 | 0 | ▶ | if( $b64 eq '') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | die('failed parsing PEM encoded key'); | |||||||||
| 96 | } | ||||||||||
| 97 | 0 | $options{'data'} = decode_base64( $b64 ); | |||||||||
| 98 | |||||||||||
| 99 | 0 | return $class->new( %options ); | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 1 | has 'fingerprint' => ( is => 'ro', isa => 'Str', lazy_build => 1); | |||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | sub _build_fingerprint { | ||||||||
|
Subroutine
_build_fingerprint: not called CC=1 SCAR=6.9
| |||||||||||
| 105 | 0 | my $self = shift; | |||||||||
| 106 | 0 | return sha256_hex( $self->data ); | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 1 | has 'armored' => ( is => 'ro', isa => 'Str', lazy_build => 1); | |||||||||
| 110 | |||||||||||
| 111 | 0 | ▶ | sub _build_armored { | ||||||||
|
Subroutine
_build_armored: not called CC=2 SCAR=17.9
| |||||||||||
| 112 | 0 | my $self = shift; | |||||||||
| 113 | 0 | my $armored = ''; | |||||||||
| 114 | 0 | ▶ | if( $self->has_attrs ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | $armored .= join( "\n", | ||||||||||
| 116 | map { | ||||||||||
| 117 | 0 | '# '.$_.': '.$self->get_attr($_) | |||||||||
| 118 | 0 | } keys %{$self->attributes} ); | |||||||||
| 119 | 0 | $armored .= "\n"; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 0 | my $data = encode_base64( $self->data, '' ); | |||||||||
| 123 | 0 | $data =~ s!(.{1,64})!$1\n!g; | |||||||||
| 124 | |||||||||||
| 125 | 0 | my $crc = crcopenpgparmor_base64( $self->data ); | |||||||||
| 126 | |||||||||||
| 127 | 0 | $armored .= "-----BEGIN PGP PUBLIC KEY BLOCK-----\n"; | |||||||||
| 128 | 0 | $armored .= "Version: ".$self->_version."\n\n"; | |||||||||
| 129 | 0 | $armored .= $data; | |||||||||
| 130 | 0 | $armored .= '='.$crc; | |||||||||
| 131 | 0 | $armored .= "-----END PGP PUBLIC KEY BLOCK-----\n"; | |||||||||
| 132 | 0 | return $armored; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 1 | has 'mail' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||
| 136 | |||||||||||
| 137 | has 'local' => ( is => 'ro', isa => 'Str', lazy => 1, | ||||||||||
| 138 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 139 | 0 | my $self = shift; | |||||||||
| 140 | 0 | my ($local) = split('@', $self->mail, 2); | |||||||||
| 141 | 0 | return( $local ); | |||||||||
| 142 | }, | ||||||||||
| 143 | 1 | ); | |||||||||
| 144 | |||||||||||
| 145 | has 'domain' => ( is => 'ro', isa => 'Str', lazy => 1, | ||||||||||
| 146 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 147 | 0 | my $self = shift; | |||||||||
| 148 | 0 | my (undef, $domain) = split('@', $self->mail, 2); | |||||||||
| 149 | 0 | return( $domain ); | |||||||||
| 150 | }, | ||||||||||
| 151 | 1 | ); | |||||||||
| 152 | |||||||||||
| 153 | has 'dns_record_name' => ( is => 'ro', isa => 'Str', lazy => 1, | ||||||||||
| 154 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 155 | 0 | my $self = shift; | |||||||||
| 156 | 0 | return sha224_hex($self->local).'._openpgpkey.'.$self->domain.'.'; | |||||||||
| 157 | }, | ||||||||||
| 158 | 1 | ); | |||||||||
| 159 | |||||||||||
| 160 | 1 | has 'dns_record_generic' => ( is => 'ro', isa => 'Str', lazy_build => 1); | |||||||||
| 161 | |||||||||||
| 162 | 0 | ▶ | sub _build_dns_record_generic { | ||||||||
|
Subroutine
_build_dns_record_generic: not called CC=1 SCAR=6.9
| |||||||||||
| 163 | 0 | my $self = shift; | |||||||||
| 164 | 0 | my $name = $self->dns_record_name; | |||||||||
| 165 | 0 | my $data = unpack( "H*", $self->data ); | |||||||||
| 166 | 0 | my $num_octets = length( $self->data ); | |||||||||
| 167 | |||||||||||
| 168 | 0 | return join(' ', $name, 'IN', 'TYPE65280', '\#', $num_octets, $data )."\n"; | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | 1 | has 'dns_record_rfc' => ( is => 'ro', isa => 'Str', lazy_build => 1); | |||||||||
| 172 | |||||||||||
| 173 | 0 | ▶ | sub _build_dns_record_rfc { | ||||||||
|
Subroutine
_build_dns_record_rfc: not called CC=1 SCAR=6.9
| |||||||||||
| 174 | 0 | my $self = shift; | |||||||||
| 175 | 0 | my $name = $self->dns_record_name; | |||||||||
| 176 | 0 | my $num_octets = 0; | |||||||||
| 177 | 0 | my $data = encode_base64( $self->data, ''); | |||||||||
| 178 | |||||||||||
| 179 | 0 | return join(' ', $name, 'IN', 'OPENPGPKEY ', $data )."\n"; | |||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | 0 | ▶ | sub clone { | ||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 183 | 0 | my $self = shift; | |||||||||
| 184 | 0 | my $class = ref( $self ); | |||||||||
| 185 | 0 | return $class->new( | |||||||||
| 186 | mail => $self->mail, | ||||||||||
| 187 | data => $self->data, | ||||||||||
| 188 | attributes => $self->attributes, | ||||||||||
| 189 | ); | ||||||||||
| 190 | } | ||||||||||
| 191 | |||||||||||
| 192 | 1 | 1; | |||||||||
| 193 | |||||||||||
| 194 | __END__ | ||||||||||