| 1 | package PGP::Mail; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 3 | ▶ | use Fcntl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | use IO::Handle; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use GnuPG::Interface; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use MIME::Parser; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | PGP::Mail - Signature checking for PGP-signed mail messages | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use PGP::Mail; | ||
| 17 | my $pgpmail=new PGP::Mail($mail, {default-keyring=>"kr.gpg"}); | ||
| 18 | $status=$pgpmail->status(); | ||
| 19 | $keyid=$pgpmail->keyid(); | ||
| 20 | $data=$pgpmail->data(); | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This module operates on PGP-signed mail messages. It checks the signature of | ||
| 25 | either a standard clearsigned, a signed message or a PGP/MIME style message. | ||
| 26 | |||
| 27 | It returns an object which can be used to check what the signed data was, | ||
| 28 | whether the signature verification succeeded, and what keyid did the | ||
| 29 | signature. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | use vars qw($VERSION); | ||
| 34 | |||
| 35 | $VERSION=('$Revision: 1.7 $'=~/(\d+\.\d+)/)[0]; | ||
| 36 | |||
| 37 | =head2 my I<$pgpmail>=B<new> PGP::Mail(I<$mesg>, I<$args>); | ||
| 38 | |||
| 39 | Creates a new PGP::Mail object using the RFC2822 message specified in | ||
| 40 | I<$mesg>. It will do the signature verification itself. I<$args> is a | ||
| 41 | hashref which gets passed to GnuPG::Interface's options. It is particularly | ||
| 42 | worth looking at L<GnuPG::Options> for this. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my $class=shift; | ||
| 48 | $class = ref($class) || $class; | ||
| 49 | |||
| 50 | my $self=bless{},$class; | ||
| 51 | $self->init(@_); | ||
| 52 | $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | =head2 I<$pgpmail>->B<status>(); | ||
| 56 | |||
| 57 | Returns the status of the signature verification (currently C<good>, C<bad> | ||
| 58 | or C<unverified>). | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub status { | ||
| 63 | my $self=shift; | ||
| 64 | |||
| 65 | return $self->{'status'} | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 I<$pgpmail>->B<keyid>(); | ||
| 69 | |||
| 70 | Returns the keyid of this signature, in the format "0xI<64-bit_key_id>". | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub keyid { | ||
| 75 | my $self=shift; | ||
| 76 | |||
| 77 | return $self->{'keyid'} | ||
| 78 | } | ||
| 79 | |||
| 80 | =head2 I<$pgpmail>->B<data>(); | ||
| 81 | |||
| 82 | Returns the signed data, run through MIME::Parser if necessary. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub data { | ||
| 87 | my $self=shift; | ||
| 88 | |||
| 89 | return $self->{'data'} | ||
| 90 | } | ||
| 91 | |||
| 92 | sub init { | ||
| 93 | my $self = shift; | ||
| 94 | my $data = shift; | ||
| 95 | my $args = shift || {}; | ||
| 96 | |||
| 97 | my @lines=map {$_."\n"} split /\r?\n/, $data; | ||
| 98 | |||
| 99 | my @header=(); | ||
| 100 | my $finished=0; | ||
| 101 | while(!$finished) { | ||
| 102 | my $line=shift(@lines); | ||
| 103 | |||
| 104 | if(!defined $line) { | ||
| 105 | $finished=1; | ||
| 106 | } | ||
| 107 | elsif(defined $line && $line=~/^$/) { | ||
| 108 | $finished=1; | ||
| 109 | } | ||
| 110 | elsif($line=~/^[ \t]+/) { | ||
| 111 | $header[-1].=$line; | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | push(@header,$line); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | # we should now have the header in @header and the body | ||
| 119 | # in @lines | ||
| 120 | |||
| 121 | for my $header (@header) { | ||
| 122 | if($header=~/^content-type:\s+(\S+\/\S+)(;.*)?$/si) { | ||
| 123 | if(lc $1 eq "multipart/signed") { | ||
| 124 | if($header=~/protocol="?application\/pgp/i) { | ||
| 125 | $self->{PGPMIME}=1; | ||
| 126 | $self->{PGPMIMEBOUND} = | ||
| 127 | ($header=~/boundary=\"([^\"]+)\"(;.*)?$/i)[0]; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | if(!$self->{PGPMIME}) { | ||
| 134 | for my $line (@lines) { | ||
| 135 | if($line=~/^-----BEGIN PGP SIGNED MESSAGE-----\s*$/) { | ||
| 136 | $self->{PGPTEXT}=1; | ||
| 137 | last; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | $self->{status}="unverified"; | ||
| 143 | $self->{keyid}="0x0000000000000000"; | ||
| 144 | $self->{data}=join("",@lines); | ||
| 145 | |||
| 146 | if(!$self->{PGPTEXT} && !$self->{PGPMIME}) { | ||
| 147 | return 0; | ||
| 148 | } | ||
| 149 | |||
| 150 | $self->{gpg}=new GnuPG::Interface; | ||
| 151 | $self->{gpg}->options->hash_init( %$args ); | ||
| 152 | $self->{gpg}->options->meta_interactive( 0 ); | ||
| 153 | |||
| 154 | if($self->{PGPTEXT}) { | ||
| 155 | $self->textpgp(\@lines); | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | $self->mimepgp(\@lines, $self->{PGPMIMEBOUND}); | ||
| 159 | } | ||
| 160 | return 1; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub textpgp { | ||
| 164 | my $self=shift; | ||
| 165 | my $data=shift; | ||
| 166 | |||
| 167 | my $input=new IO::Handle; | ||
| 168 | my $output=new IO::Handle; | ||
| 169 | my $error=new IO::Handle; | ||
| 170 | my $status=new IO::Handle; | ||
| 171 | my $pp=new IO::Handle; | ||
| 172 | my $handles=GnuPG::Handles->new( | ||
| 173 | stdin=>$input, | ||
| 174 | stdout=>$output, | ||
| 175 | stderr=>$error, | ||
| 176 | status=>$status, | ||
| 177 | passphrase=>$pp | ||
| 178 | ); | ||
| 179 | my $pid=$self->{gpg}->decrypt(handles=>$handles); | ||
| 180 | close $pp; | ||
| 181 | |||
| 182 | print $input join "",@$data; | ||
| 183 | close $input; | ||
| 184 | |||
| 185 | $self->{data}=join "",<$output>; | ||
| 186 | close $output; | ||
| 187 | |||
| 188 | $self->get_status($status); | ||
| 189 | waitpid $pid, 0; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub get_status { | ||
| 193 | my $self=shift; | ||
| 194 | my $statusfh=shift; | ||
| 195 | |||
| 196 | for my $line (<$statusfh>) { | ||
| 197 | if($line =~ /^\[GNUPG:\] GOODSIG (\w+) /) { | ||
| 198 | $self->{status}="good"; | ||
| 199 | $self->{keyid}="0x$1"; | ||
| 200 | } | ||
| 201 | elsif($line =~ /^\[GNUPG:\] BADSIG (\w+) /) { | ||
| 202 | $self->{status}="bad"; | ||
| 203 | $self->{keyid}="0x$1"; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | sub mimepgp { | ||
| 209 | my $self=shift; | ||
| 210 | my $data=shift; | ||
| 211 | my $bound=shift; | ||
| 212 | |||
| 213 | my $state="before"; | ||
| 214 | my $sigdata=""; | ||
| 215 | my $signature=""; | ||
| 216 | for my $line (@$data) { | ||
| 217 | if($state eq "before" && | ||
| 218 | $line eq "--$bound\n") { | ||
| 219 | $state="data"; | ||
| 220 | next; | ||
| 221 | } | ||
| 222 | elsif($state eq "data" && | ||
| 223 | $line eq "--$bound\n") { | ||
| 224 | $state="sig"; | ||
| 225 | next; | ||
| 226 | } | ||
| 227 | elsif($state eq "sig" && | ||
| 228 | $line eq "--$bound--\n") { | ||
| 229 | $state="finished"; | ||
| 230 | next; | ||
| 231 | } | ||
| 232 | elsif($state eq "data") { | ||
| 233 | my $l=$line; | ||
| 234 | chomp $l; | ||
| 235 | $sigdata.=$l."\r\n"; | ||
| 236 | } | ||
| 237 | elsif($state eq "sig") { | ||
| 238 | $signature.=$line; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | chomp $signature; | ||
| 242 | $sigdata=~s/\r\n$//; | ||
| 243 | |||
| 244 | my $parser=new MIME::Parser; | ||
| 245 | $parser->output_to_core(1); | ||
| 246 | $signature=$parser->parse_data($signature)->bodyhandle->as_string; | ||
| 247 | |||
| 248 | my $fn=""; | ||
| 249 | for my $i (0..3) { | ||
| 250 | if(sysopen(SIGNATURE, | ||
| 251 | $fn="/tmp/file-$$-$i-" . time() . ".dat", | ||
| 252 | O_EXCL | O_RDWR | O_CREAT, 0666)) { | ||
| 253 | last; | ||
| 254 | } | ||
| 255 | else { | ||
| 256 | $fn=""; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | if(!length $fn) { | ||
| 261 | return 0; | ||
| 262 | } | ||
| 263 | |||
| 264 | print SIGNATURE $sigdata; | ||
| 265 | close SIGNATURE; | ||
| 266 | |||
| 267 | my $input=new IO::Handle; | ||
| 268 | my $output=new IO::Handle; | ||
| 269 | my $error=new IO::Handle; | ||
| 270 | my $status=new IO::Handle; | ||
| 271 | my $handles=GnuPG::Handles->new( | ||
| 272 | stdin=>$input, | ||
| 273 | stdout=>$output, | ||
| 274 | stderr=>$error, | ||
| 275 | status=>$status | ||
| 276 | ); | ||
| 277 | my $pid=$self->{gpg}->verify(handles=>$handles, command_args=>["-",$fn]); | ||
| 278 | |||
| 279 | print $input $signature; | ||
| 280 | close $input; | ||
| 281 | |||
| 282 | $parser=new MIME::Parser; | ||
| 283 | $parser->output_to_core(1); | ||
| 284 | $self->{data}=$parser->parse_data($sigdata)->bodyhandle->as_string; | ||
| 285 | |||
| 286 | $self->get_status($status); | ||
| 287 | |||
| 288 | waitpid $pid, 0; | ||
| 289 | unlink $fn; | ||
| 290 | } | ||
| 291 | |||
| 292 | =head1 BUGS | ||
| 293 | |||
| 294 | The style of this module leaves quite a bit to be desired, and it only | ||
| 295 | supports verifying signatures at the moment, rather than the full encryption, | ||
| 296 | decryptions, and creating the messages. | ||
| 297 | |||
| 298 | =head1 AUTHOR | ||
| 299 | |||
| 300 | Matthew Byng-Maddick E<lt>mbm@colondot.netE<gt> | ||
| 301 | |||
| 302 | =head1 SEE ALSO | ||
| 303 | |||
| 304 | L<perl>, L<GnuPG::Interface>, L<GnuPG::Options>, L<MIME::Tools>. | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | 1; | ||