| 1 | package Mail::GPG; | ||
| 2 | |||
| 3 | $VERSION = "1.0.13"; | ||
| 4 | |||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use IO::Handle; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Data::Dumper (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use GnuPG::Interface; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use MIME::Parser; | ||
| 11 | use Mail::GPG::Result; | ||
| 12 | use Mail::Address; | ||
| 13 | use File::Temp; | ||
| 14 | use List::MoreUtils (); | ||
| 15 | |||
| 16 | use EV; | ||
| 17 | use AnyEvent; | ||
| 18 | |||
| 19 | #-- control debug output on i/o multiplexing | ||
| 20 | use constant IODEBUG => 0; | ||
| 21 | |||
| 22 | sub get_default_key_id { shift->{default_key_id} } | ||
| 23 | sub get_default_passphrase { shift->{default_passphrase} } | ||
| 24 | sub get_debug { shift->{debug} } | ||
| 25 | sub get_debug_dir { shift->{debug_dir} } | ||
| 26 | sub get_gnupg_hash_init { shift->{gnupg_hash_init} } | ||
| 27 | sub get_digest { shift->{digest} } | ||
| 28 | sub get_default_key_encrypt { shift->{default_key_encrypt} } | ||
| 29 | sub get_gpg_call { shift->{gpg_call} } | ||
| 30 | sub get_no_strict_7bit_encoding { shift->{no_strict_7bit_encoding} } | ||
| 31 | sub get_use_long_key_ids { shift->{use_long_key_ids} } | ||
| 32 | |||
| 33 | sub set_default_key_id { shift->{default_key_id} = $_[1] } | ||
| 34 | sub set_default_passphrase { shift->{default_passphrase} = $_[1] } | ||
| 35 | sub set_debug { shift->{debug} = $_[1] } | ||
| 36 | sub set_debug_dir { shift->{debug_dir} = $_[1] } | ||
| 37 | sub set_gnupg_hash_init { shift->{gnupg_hash_init} = $_[1] } | ||
| 38 | sub set_digest { shift->{digest} = $_[1] } | ||
| 39 | sub set_default_key_encrypt { shift->{default_key_encrypt} = $_[1] } | ||
| 40 | sub set_gpg_call { shift->{gpg_call} = $_[1] } | ||
| 41 | sub set_no_strict_7bit_encoding { shift->{no_strict_7bit_encoding}=$_[1]} | ||
| 42 | sub set_use_long_key_ids { shift->{use_long_key_ids} = $_[1] } | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my $class = shift; | ||
| 46 | my %par = @_; | ||
| 47 | my ($default_key_id, $default_passphrase, $debug, $debug_dir) = | ||
| 48 | @par{'default_key_id','default_passphrase','debug','debug_dir'}; | ||
| 49 | my ($gnupg_hash_init, $digest, $gpg_call, $default_key_encrypt) = | ||
| 50 | @par{'gnupg_hash_init','digest','gpg_call','default_key_encrypt'}; | ||
| 51 | my ($no_strict_7bit_encoding, $use_long_key_ids) = | ||
| 52 | @par{'no_strict_7bit_encoding','use_long_key_ids'}; | ||
| 53 | |||
| 54 | $debug_dir ||= $ENV{DUMPDIR} || File::Spec->tmpdir . '/mail-gpg-test'; | ||
| 55 | $gnupg_hash_init ||= {}; | ||
| 56 | $digest ||= "RIPEMD160"; | ||
| 57 | $gpg_call ||= "gpg"; | ||
| 58 | $no_strict_7bit_encoding ||= 0; | ||
| 59 | $use_long_key_ids ||= 0; | ||
| 60 | |||
| 61 | my $self = bless { | ||
| 62 | default_key_id => $default_key_id, | ||
| 63 | default_passphrase => $default_passphrase, | ||
| 64 | debug => $debug, | ||
| 65 | debug_dir => $debug_dir, | ||
| 66 | gnupg_hash_init => $gnupg_hash_init, | ||
| 67 | digest => $digest, | ||
| 68 | default_key_encrypt => $default_key_encrypt, | ||
| 69 | gpg_call => $gpg_call, | ||
| 70 | no_strict_7bit_encoding => $no_strict_7bit_encoding, | ||
| 71 | use_long_key_ids => $use_long_key_ids, | ||
| 72 | }, $class; | ||
| 73 | |||
| 74 | return $self; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub new_gpg_interface { | ||
| 78 | my $self = shift; | ||
| 79 | my %par = @_; | ||
| 80 | my ($options, $passphrase) = @par{'options','passphrase'}; | ||
| 81 | |||
| 82 | my $gpg = GnuPG::Interface->new; | ||
| 83 | |||
| 84 | $gpg->passphrase($passphrase) if defined $passphrase; | ||
| 85 | $gpg->call( $self->get_gpg_call ) if $self->get_gpg_call ne ''; | ||
| 86 | |||
| 87 | my $gnupg_hash_init = $self->get_gnupg_hash_init; | ||
| 88 | |||
| 89 | if ($options) { | ||
| 90 | $gpg->options->hash_init( %{$options}, %{$gnupg_hash_init} ); | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | $gpg->options->hash_init( %{$gnupg_hash_init} ); | ||
| 94 | } | ||
| 95 | |||
| 96 | $gpg->options->push_extra_args( '--digest', $self->get_digest ); | ||
| 97 | $gpg->options->meta_interactive(0); | ||
| 98 | |||
| 99 | return $gpg; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub save_debug_file { | ||
| 103 | my $self = shift; | ||
| 104 | my %par = @_; | ||
| 105 | my ($name, $data, $data_fh ) = @par{'name','data','data_fh' }; | ||
| 106 | |||
| 107 | $name = $self->get_debug_dir . "/mgpg-" . $name; | ||
| 108 | |||
| 109 | open( DBG, ">$name" ) or die "can't write $name"; | ||
| 110 | if ($data_fh) { | ||
| 111 | seek $data_fh, 0, 0; | ||
| 112 | print DBG $_ while <$data_fh>; | ||
| 113 | } | ||
| 114 | elsif ( ref $data ) { | ||
| 115 | print DBG $$data; | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | print DBG $data; | ||
| 119 | } | ||
| 120 | close DBG; | ||
| 121 | |||
| 122 | 1; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub check_7bit_encoding_of_all_parts { | ||
| 126 | my $self = shift; | ||
| 127 | my %par = @_; | ||
| 128 | my ($entity) = $par{'entity'}; | ||
| 129 | |||
| 130 | #-- skip if no strict encoding check should be applied | ||
| 131 | return 1 if $self->get_no_strict_7bit_encoding; | ||
| 132 | |||
| 133 | #-- first the primary entity | ||
| 134 | my $encoding = $entity->head->get("content-transfer-encoding"); | ||
| 135 | die "Content transfer encoding '$encoding' is not 7bit safe" | ||
| 136 | unless not defined $encoding | ||
| 137 | or $encoding =~ /^(quoted-printable|base64|7bit)\s*$/i; | ||
| 138 | |||
| 139 | #-- now all parts | ||
| 140 | return 1 if not $entity->parts; | ||
| 141 | |||
| 142 | #-- recursively | ||
| 143 | my $parts = $entity->parts; | ||
| 144 | for ( my $i = 0; $i < $parts; ++$i ) { | ||
| 145 | $self->check_7bit_encoding_of_all_parts( entity => $entity->parts($i), | ||
| 146 | ); | ||
| 147 | } | ||
| 148 | |||
| 149 | return 1; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub check_encryption { | ||
| 153 | my $self = shift; | ||
| 154 | my %par = @_; | ||
| 155 | my ($entity, $encrypted_text_sref) = | ||
| 156 | @par{'entity','encrypted_text_sref'}; | ||
| 157 | |||
| 158 | my $is_armor; | ||
| 159 | if ( $entity->effective_type =~ m!multipart/encrypted!i ) { | ||
| 160 | |||
| 161 | #-- is this a valid RFC 3156 multipart/encrypted entity? | ||
| 162 | die "Entity must have two parts" | ||
| 163 | if $entity->parts != 2; | ||
| 164 | die "Entity is not OpenPGP encrypted" | ||
| 165 | unless $entity->parts(0)->effective_type | ||
| 166 | =~ m!application/pgp-encrypted!i; | ||
| 167 | $$encrypted_text_sref = $entity->parts(1)->body_as_string; | ||
| 168 | |||
| 169 | } | ||
| 170 | elsif ( $entity->bodyhandle ) { | ||
| 171 | |||
| 172 | #-- probably an ASCII armor encrypted entity | ||
| 173 | #-- (we need the *decoded* data here - hopefully the | ||
| 174 | #-- MIME::Parser had decode_body(1) set). | ||
| 175 | $$encrypted_text_sref = $entity->bodyhandle->as_string; | ||
| 176 | die "Entity is not OpenPGP encrypted" | ||
| 177 | unless $$encrypted_text_sref =~ /^-----BEGIN PGP MESSAGE-----/m; | ||
| 178 | $is_armor = 1; | ||
| 179 | } | ||
| 180 | else { | ||
| 181 | die "Entity is not multipart/encrypted and has no body"; | ||
| 182 | } | ||
| 183 | |||
| 184 | return $is_armor; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub perform_multiplexed_gpg_io { | ||
| 188 | my $self = shift; | ||
| 189 | my %par = @_; | ||
| 190 | my ($data_fh, $data_canonify, $stdin_fh, $stderr_fh) = | ||
| 191 | @par{'data_fh','data_canonify','stdin_fh','stderr_fh'}; | ||
| 192 | my ($stdout_fh, $status_fh, $stderr_sref, $stdout_sref) = | ||
| 193 | @par{'stdout_fh','status_fh','stderr_sref','stdout_sref'}; | ||
| 194 | my ($status_sref) = | ||
| 195 | $par{'status_sref'}; | ||
| 196 | |||
| 197 | #-- Debug stuff | ||
| 198 | IODEBUG && print "anyevent\n"; | ||
| 199 | my $nl = "\n"; | ||
| 200 | my $r = 0; | ||
| 201 | my $w = 0; | ||
| 202 | |||
| 203 | #-- I/O buffer size | ||
| 204 | my $bsize = 8192; | ||
| 205 | |||
| 206 | #-- perl < 5.6 compatibility: seek() and read() work | ||
| 207 | #-- on native GLOB filehandle only, so dertmine type | ||
| 208 | #-- of filehandle here | ||
| 209 | my $data_fh_glob = ref $data_fh eq 'GLOB'; | ||
| 210 | |||
| 211 | #-- rewind the data filehandle | ||
| 212 | if ($data_fh_glob) { | ||
| 213 | seek $data_fh, 0, 0; | ||
| 214 | } | ||
| 215 | else { | ||
| 216 | $data_fh->seek( 0, 0 ); | ||
| 217 | } | ||
| 218 | |||
| 219 | #-- Watchers for various handles | ||
| 220 | my ($data_watcher, $stdin_watcher, $stderr_watcher, | ||
| 221 | $stdout_watcher, $status_watcher); | ||
| 222 | |||
| 223 | #-- Buffer for data reading and stdin writing | ||
| 224 | my $data_buffer = ""; | ||
| 225 | |||
| 226 | #-- Mainloop condvar | ||
| 227 | my $done = AnyEvent->condvar; | ||
| 228 | |||
| 229 | IODEBUG && print "dw $nl"; | ||
| 230 | |||
| 231 | #-- watch for incoming data | ||
| 232 | $data_watcher = AnyEvent->io ( | ||
| 233 | fh => $data_fh, | ||
| 234 | poll => "r", | ||
| 235 | cb => sub { | ||
| 236 | IODEBUG && print "d"; | ||
| 237 | |||
| 238 | my $rc = sysread($data_fh, my $buffer, $bsize); | ||
| 239 | |||
| 240 | IODEBUG && ( $r += $rc ); | ||
| 241 | IODEBUG && print "($rc->$r) $nl"; | ||
| 242 | |||
| 243 | #-- on eof stop watcher and close filehandle | ||
| 244 | if ( !$rc ) { | ||
| 245 | IODEBUG && print "de $nl"; | ||
| 246 | $data_watcher = undef; | ||
| 247 | close $data_fh; | ||
| 248 | return; | ||
| 249 | } | ||
| 250 | |||
| 251 | if ( $data_canonify ) { | ||
| 252 | #-- canonify it if requested | ||
| 253 | $buffer =~ s/\x0A/\x0D\x0A/g; | ||
| 254 | $buffer =~ s/\x0D\x0D\x0A/\x0D\x0A/g; | ||
| 255 | } | ||
| 256 | |||
| 257 | $data_buffer .= $buffer; | ||
| 258 | |||
| 259 | #-- start stdin watcher if there is no watcher yet | ||
| 260 | if ( not $stdin_watcher ) { | ||
| 261 | IODEBUG && print "iw $nl"; | ||
| 262 | |||
| 263 | $stdin_watcher = AnyEvent->io ( | ||
| 264 | fh => $stdin_fh, | ||
| 265 | poll => 'w', | ||
| 266 | cb => sub { | ||
| 267 | IODEBUG && print "i"; | ||
| 268 | |||
| 269 | my $written = syswrite($stdin_fh, $data_buffer, $bsize); | ||
| 270 | |||
| 271 | $w += $written; | ||
| 272 | IODEBUG && print "($written->$w) $nl"; | ||
| 273 | |||
| 274 | #-- if we couldn't write anything, stop watcher | ||
| 275 | if ( not defined $written ) { | ||
| 276 | $stdin_watcher = undef; | ||
| 277 | IODEBUG && print "ie $nl"; | ||
| 278 | return; | ||
| 279 | } | ||
| 280 | |||
| 281 | #-- cut off i/o buffer | ||
| 282 | $data_buffer = substr($data_buffer, $written) if $written; | ||
| 283 | |||
| 284 | #-- on empty buffer there is no need to watch | ||
| 285 | #-- for writing into the stdin handle | ||
| 286 | $stdin_watcher = undef if $data_buffer eq ""; | ||
| 287 | |||
| 288 | IODEBUG && print "is $nl" unless $stdin_watcher; | ||
| 289 | |||
| 290 | #-- if read and write watchers are off, we | ||
| 291 | #-- are done with feeding and close stdin | ||
| 292 | #-- filehandle to signal that to gpg | ||
| 293 | if ( not $stdin_watcher and not $data_watcher ) { | ||
| 294 | IODEBUG && print "ic $nl"; | ||
| 295 | close $stdin_fh; | ||
| 296 | } | ||
| 297 | }, | ||
| 298 | ); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | ); | ||
| 302 | |||
| 303 | IODEBUG && print "ew $nl"; | ||
| 304 | |||
| 305 | #-- grab stderr | ||
| 306 | $stderr_watcher = AnyEvent->io ( | ||
| 307 | fh => $stderr_fh, | ||
| 308 | poll => 'r', | ||
| 309 | cb => sub { | ||
| 310 | IODEBUG && print "e"; | ||
| 311 | |||
| 312 | my $rc = sysread( | ||
| 313 | $stderr_fh, | ||
| 314 | ${$stderr_sref}, | ||
| 315 | $bsize, | ||
| 316 | length(${$stderr_sref}) | ||
| 317 | ); | ||
| 318 | |||
| 319 | IODEBUG && print "($rc) $nl"; | ||
| 320 | |||
| 321 | $stderr_watcher = undef unless $rc; | ||
| 322 | }, | ||
| 323 | ); | ||
| 324 | |||
| 325 | #-- grab stdout and stop loop on eof | ||
| 326 | $stdout_watcher = AnyEvent->io ( | ||
| 327 | fh => $stdout_fh, | ||
| 328 | poll => 'r', | ||
| 329 | cb => sub { | ||
| 330 | IODEBUG && print "o"; | ||
| 331 | |||
| 332 | my $rc = sysread( | ||
| 333 | $stdout_fh, | ||
| 334 | ${$stdout_sref}, | ||
| 335 | $bsize, | ||
| 336 | length(${$stdout_sref}) | ||
| 337 | ); | ||
| 338 | |||
| 339 | IODEBUG && print "($rc) $nl"; | ||
| 340 | |||
| 341 | #-- if gpg stdout is eof we're done | ||
| 342 | if ( not $rc ) { | ||
| 343 | $stdout_watcher = undef; | ||
| 344 | $done->send; | ||
| 345 | } | ||
| 346 | }, | ||
| 347 | ); | ||
| 348 | |||
| 349 | #-- grab status if requested | ||
| 350 | if ( $status_fh ) { | ||
| 351 | IODEBUG && print "sw $nl"; | ||
| 352 | |||
| 353 | $status_watcher = AnyEvent->io ( | ||
| 354 | fh => $status_fh, | ||
| 355 | poll => 'r', | ||
| 356 | cb => sub { | ||
| 357 | IODEBUG && print "s"; | ||
| 358 | |||
| 359 | my $rc = sysread( | ||
| 360 | $status_fh, | ||
| 361 | ${$status_sref}, | ||
| 362 | $bsize, | ||
| 363 | length(${$status_sref}) | ||
| 364 | ); | ||
| 365 | |||
| 366 | IODEBUG && print "($rc) $nl"; | ||
| 367 | |||
| 368 | $status_watcher = undef unless $rc; | ||
| 369 | }, | ||
| 370 | ); | ||
| 371 | } | ||
| 372 | |||
| 373 | IODEBUG && print "loop $nl"; | ||
| 374 | |||
| 375 | #-- enter mainloop until eof of gpg stdout | ||
| 376 | $done->recv; | ||
| 377 | |||
| 378 | 1; | ||
| 379 | } | ||
| 380 | |||
| 381 | #-- Addresses bug: https://rt.cpan.org/Public/Bug/Display.html?id=103828 (here) | ||
| 382 | #-- Possibly also: https://rt.cpan.org/Public/Bug/Display.html?id=81082 (here) | ||
| 383 | # and https://rt.cpan.org/Public/Bug/Display.html?id=80500 (in Makefile.PL) | ||
| 384 | |||
| 385 | sub _parse_key_list { | ||
| 386 | my $self = shift; | ||
| 387 | my ($output_stdout, %par) = @_; | ||
| 388 | my ($coerce, $debug, $verbose) = | ||
| 389 | @par{'coerce','debug','verbose'}; | ||
| 390 | |||
| 391 | #-- grab key ID's and emails from output (backward compatible to gpg 1.x) | ||
| 392 | # | ||
| 393 | # Example: | ||
| 394 | # search request: "--list-keys --with-colons 6C187D0F196ED9E3" | ||
| 395 | # format : see /usr/share/doc/packages/gpg2/DETAILS | ||
| 396 | # | ||
| 397 | #--OLD sample: gpg (GnuPG) 1.4.11 | ||
| 398 | # tru:t:1:1431088683:0:3:1:5 | ||
| 399 | # pub:-:1024:17:062F00DAE20F5035:2004-02-10:::-:Jörn Reder Mail\x3a\x3aGPG Test Key <mailgpg@localdomain>::scaESCA: | ||
| 400 | # sub:-:1024:16:6C187D0F196ED9E3:2004-02-10::::::e: | ||
| 401 | # | ||
| 402 | # expected output: (062F00DAE20F5035, 'Jörn Reder Mail::GPG Test Key <mailgpg@localdomain>') | ||
| 403 | # | ||
| 404 | #--NEW sample: gpg (GnuPG) 2.0.22 | ||
| 405 | # tru:t:1:1429473192:0:3:1:5 | ||
| 406 | # pub:-:1024:17:062F00DAE20F5035:1076425915:::-:::scaESCA: | ||
| 407 | # uid:-::::1076425915::588869ADE077B8FB05788A99565AEED15AED8231::Jörn Reder Mail\x3a\x3aGPG Test Key <mailgpg@localdomain>: | ||
| 408 | # sub:-:1024:16:6C187D0F196ED9E3:1076425917::::::e: | ||
| 409 | # | ||
| 410 | # expected output: (062F00DAE20F5035, 'Jörn Reder Mail::GPG Test Key <mailgpg@localdomain>', | ||
| 411 | # 6C187D0F196ED9E3, 'Jörn Reder Mail::GPG Test Key <mailgpg@localdomain>') | ||
| 412 | # | ||
| 413 | # PERLBOTIX<ätt>cpan.org / May, 2015 | ||
| 414 | |||
| 415 | my @result; | ||
| 416 | |||
| 417 | #-- needed for utf8 handling | ||
| 418 | require Encode if $] >= 5.008; | ||
| 419 | |||
| 420 | $output_stdout .= "\nFLUSH\n"; #-- we add this token to trigger flushing of the last record | ||
| 421 | |||
| 422 | #-- these values are valid per "paragraph" (from pub: to pub:) | ||
| 423 | my @ids; #-- list of potential IDs (pub, sub) | ||
| 424 | my @emails; #-- list of potential email adresses (uid) | ||
| 425 | my $gpg2_mode = 0; #-- auto-detect OLD/NEW format: true if NEW (gpg2) format | ||
| 426 | |||
| 427 | #-- parse output line by line | ||
| 428 | #-- OLD-format (gpg 1.x): simulate old behaviour | ||
| 429 | #-- NEW-format (gpg 2.x): create all combinations of valid key-IDs and emails; return (pub) key-ID first, | ||
| 430 | # so the result stays backward compatible | ||
| 431 | |||
| 432 | while ( $output_stdout =~ m!^((\w+):?.*?)[\r\n]+!mg ) { | ||
| 433 | |||
| 434 | my ($line, $tag) = ($1, $2); | ||
| 435 | my @fields = split /:/, $line, -1; | ||
| 436 | |||
| 437 | warn("\nFields: ", join(", ", map { "($_)" } @fields)) if $debug; | ||
| 438 | |||
| 439 | #-- skip entries that are expired or otherwise invalid | ||
| 440 | if ( ( $fields[1] and $fields[1] =~ /^([eird])$/ ) | ||
| 441 | or ( $fields[11] and $fields[11] =~ /([D])/ ) | ||
| 442 | ) { #-- cannot use key (invalid/expired/revoked/[dD]isabled) | ||
| 443 | carp "Skipping key invalidated by '$1'-flag: ($line)" if $verbose or $debug; | ||
| 444 | next; | ||
| 445 | } | ||
| 446 | |||
| 447 | #-- a further 'pub' marks a new paragraph or the end of the current paragraph | ||
| 448 | #-- a 'FLUSH' marks the end of the the whole text | ||
| 449 | if ( $tag eq 'pub' or $tag eq 'FLUSH' ) { | ||
| 450 | |||
| 451 | #-- flush intermediate or final results | ||
| 452 | if ( @ids and @emails ) { | ||
| 453 | |||
| 454 | @emails = List::MoreUtils::uniq @emails; #-- uniq preserves order | ||
| 455 | |||
| 456 | for ( @emails ) { | ||
| 457 | #-- $key_mail is quoted C-style (e.g. \x3a is a colon) | ||
| 458 | s/\\x(..)/chr(hex($1))/eg; | ||
| 459 | |||
| 460 | #-- tell Perl that this variable is utf8 encoded | ||
| 461 | #-- (if Perl version is 5.8.0 or greater) | ||
| 462 | eval { $_ = Encode::decode("utf-8", $_, Encode::FB_CROAK) } | ||
| 463 | if $] >= 5.008; | ||
| 464 | } | ||
| 465 | |||
| 466 | @ids = List::MoreUtils::uniq @ids; | ||
| 467 | if ( !$self->get_use_long_key_ids ) { | ||
| 468 | $_ = substr($_,-8) for ( @ids ); | ||
| 469 | } | ||
| 470 | |||
| 471 | #-- finally, fill result array with all combinations of key-ids and emails (legacy), found in the previous paragraph | ||
| 472 | for my $id ( @ids ) { | ||
| 473 | |||
| 474 | if ( $coerce ) { | ||
| 475 | push @result, $id, [ @emails ]; #-- "modern" interface | ||
| 476 | } | ||
| 477 | else { | ||
| 478 | for my $m ( @emails ) { | ||
| 479 | push @result, ($id, $m); #-- legacy | ||
| 480 | } | ||
| 481 | } | ||
| 482 | } | ||
| 483 | |||
| 484 | } elsif ( @ids or @emails ) { | ||
| 485 | carp "Incomplete key data - key is probably invalid? (@ids) x (@emails)!"; | ||
| 486 | } | ||
| 487 | |||
| 488 | @ids = (); | ||
| 489 | @emails = (); | ||
| 490 | last if defined $fields[1] and $fields[1] eq 'FLUSH'; | ||
| 491 | |||
| 492 | |||
| 493 | #-- parse 'new' format (pub:..; next: uid:..., uid:..., sub:... ) | ||
| 494 | if ( not defined $fields[9] or $fields[9] =~ /^\s*$/ ) { | ||
| 495 | $gpg2_mode = 1; | ||
| 496 | push @ids, $fields[4]; | ||
| 497 | } | ||
| 498 | #-- parse 'old' format (pub:) | ||
| 499 | elsif ( $fields[9] =~ /<[^>]+>/ ) { | ||
| 500 | $gpg2_mode = 0; | ||
| 501 | push @ids, $fields[4]; | ||
| 502 | push @emails, $fields[9]; | ||
| 503 | } | ||
| 504 | else { | ||
| 505 | die "Cannot parse: ($line)"; | ||
| 506 | } | ||
| 507 | |||
| 508 | } #-- 'pub' & 'FLUSH' handled obove | ||
| 509 | |||
| 510 | #-- handle 'sub' entries / extract key-id | ||
| 511 | elsif ( $tag eq 'sub' and $gpg2_mode and $fields[4]) { | ||
| 512 | push @ids, $fields[4]; | ||
| 513 | } | ||
| 514 | |||
| 515 | #-- handle 'uid' entries / extract email | ||
| 516 | elsif ( $tag eq 'uid' and $gpg2_mode and $fields[9] =~ /<[^>]+>/ ) { | ||
| 517 | push @emails, $fields[9]; | ||
| 518 | } | ||
| 519 | |||
| 520 | #-- ignore anything else | ||
| 521 | else { | ||
| 522 | warn "Ignoring line [gpg2_mode=$gpg2_mode] -- '$line'" if $debug; | ||
| 523 | } | ||
| 524 | |||
| 525 | } #-- loop over output_stdout | ||
| 526 | |||
| 527 | warn Data::Dumper->Dump( [ \@result], [qw(RESULT_AS_REF)] ) if $debug; | ||
| 528 | |||
| 529 | return @result; | ||
| 530 | } | ||
| 531 | |||
| 532 | |||
| 533 | sub query_keyring { | ||
| 534 | my $self = shift; | ||
| 535 | my %par = @_; | ||
| 536 | my ($search, $debug) = @par{'search','debug'}; | ||
| 537 | |||
| 538 | #-- ignore any PIPE signals, in case of gpg exited | ||
| 539 | #-- early before we fed our data into it. | ||
| 540 | local $SIG{PIPE} = 'IGNORE'; | ||
| 541 | |||
| 542 | #-- we parse gpg's output and rely on english | ||
| 543 | local $ENV{LC_ALL} = "C"; | ||
| 544 | |||
| 545 | #-- get a GnuPG::Interface | ||
| 546 | my $gpg = $self->new_gpg_interface; | ||
| 547 | |||
| 548 | #-- initialize Handles | ||
| 549 | my $stdout = IO::Handle->new; | ||
| 550 | my $stderr = IO::Handle->new; | ||
| 551 | my $handles = GnuPG::Handles->new( | ||
| 552 | stdout => $stdout, | ||
| 553 | stderr => $stderr, | ||
| 554 | ); | ||
| 555 | |||
| 556 | #-- execute gpg --list-public-keys | ||
| 557 | my $pid = $gpg->wrap_call( | ||
| 558 | handles => $handles, | ||
| 559 | commands => [ "--list-keys", "--with-colons" ], | ||
| 560 | command_args => [$search], | ||
| 561 | ); | ||
| 562 | |||
| 563 | #-- fetch gpg's STDERR | ||
| 564 | my $output_stderr; | ||
| 565 | $output_stderr .= $_ while <$stderr>; | ||
| 566 | close $stderr; | ||
| 567 | |||
| 568 | #-- fetch gpg's STDOUT | ||
| 569 | my $output_stdout; | ||
| 570 | $output_stdout .= $_ while <$stdout>; | ||
| 571 | close $stdout; | ||
| 572 | |||
| 573 | #-- wait on gpg exit | ||
| 574 | waitpid $pid, 0; | ||
| 575 | |||
| 576 | if ( $debug ) { | ||
| 577 | warn "LIST_KEYS(CMD) -- --list-keys --with-colons $search\n"; | ||
| 578 | warn "LIST_KEYS(STDERR) -- search for ($search):\n$output_stderr\n"; | ||
| 579 | warn "LIST_KEYS(STDOUT) -- search for ($search):\n$output_stdout\n"; | ||
| 580 | } | ||
| 581 | |||
| 582 | my @result = $self->_parse_key_list( $output_stdout , %par); | ||
| 583 | |||
| 584 | #-- return result: undef if nothing found, first key-id if | ||
| 585 | #-- a scalar is requested, all entries suitable for a hash | ||
| 586 | #-- slurp if an array is requested | ||
| 587 | |||
| 588 | # Compatibility note: The first id is always the id of the 'pub' entry, even for a subkey-hit. | ||
| 589 | # gpgv2: The result should not be used to initialise a hash, since some emails will be clobbered. | ||
| 590 | # We need another interface here. Curently Mail::GPG uses only the first entry... | ||
| 591 | return if not @result; | ||
| 592 | return $result[0] if not wantarray; | ||
| 593 | return @result; | ||
| 594 | } | ||
| 595 | |||
| 596 | sub build_rfc3156_multipart_entity { | ||
| 597 | my $self = shift; | ||
| 598 | my %par = @_; | ||
| 599 | my ($entity, $method) = @par{'entity','method'}; | ||
| 600 | |||
| 601 | #-- check, if content-transfer-encoding follows the | ||
| 602 | #-- RFC 3156 requirement of being 7bit safe | ||
| 603 | $self->check_7bit_encoding_of_all_parts( entity => $entity ); | ||
| 604 | |||
| 605 | #-- build entity for signed/encrypted version; first make | ||
| 606 | #-- a copy of the given entity (deep copy of body | ||
| 607 | #-- files isn't necessary, body data isn't modified | ||
| 608 | #-- here). | ||
| 609 | my $rfc_entity = $entity->dup; | ||
| 610 | |||
| 611 | #-- determine the part, which is to be signed/encrypted | ||
| 612 | my ( $work_part, $multipart ); | ||
| 613 | if ( $rfc_entity->parts > 1 ) { | ||
| 614 | |||
| 615 | #-- the entity is multipart, so we need to build | ||
| 616 | #-- a new version of it with all parts, but without | ||
| 617 | #-- the rfc822 mail headers of the original entity | ||
| 618 | #-- (according RFC 3156 the signed/encrypted parts | ||
| 619 | #-- need MIME content headers only) | ||
| 620 | $work_part = MIME::Entity->build( Type => "multipart/mixed" ); | ||
| 621 | $work_part->add_part($_) for $rfc_entity->parts; | ||
| 622 | $rfc_entity->parts( [] ); | ||
| 623 | $multipart = 1; | ||
| 624 | } | ||
| 625 | else { | ||
| 626 | |||
| 627 | #-- the entity is single part, so just make it | ||
| 628 | #-- multipart and take the first (and only) part | ||
| 629 | $rfc_entity->make_multipart; | ||
| 630 | $work_part = $rfc_entity->parts(0); | ||
| 631 | $multipart = 0; | ||
| 632 | } | ||
| 633 | |||
| 634 | #-- configure headers and add first part to the entity | ||
| 635 | if ( $method eq 'sign' ) { | ||
| 636 | |||
| 637 | #-- set correct MIME OpenPGP header für multipart/signed | ||
| 638 | $rfc_entity->head->mime_attr( "Content-Type", "multipart/signed" ); | ||
| 639 | $rfc_entity->head->mime_attr( "Content-Type.protocol", | ||
| 640 | "application/pgp-signature" ); | ||
| 641 | $rfc_entity->head->mime_attr( "Content-Type.micalg", | ||
| 642 | "pgp-" . lc( $self->get_digest ) ); | ||
| 643 | |||
| 644 | #-- add content part as first part | ||
| 645 | $rfc_entity->add_part($work_part) if $multipart; | ||
| 646 | } | ||
| 647 | else { | ||
| 648 | |||
| 649 | #-- set correct MIME OpenPGP header für multipart/encrypted | ||
| 650 | $rfc_entity->head->mime_attr( "Content-Type", "multipart/encrypted" ); | ||
| 651 | $rfc_entity->head->mime_attr( "Content-Type.protocol", | ||
| 652 | "application/pgp-encrypted" ); | ||
| 653 | |||
| 654 | #-- remove all parts | ||
| 655 | $rfc_entity->parts( [] ); | ||
| 656 | |||
| 657 | #-- and add OpenPGP version part as first part | ||
| 658 | $rfc_entity->attach( | ||
| 659 | Type => "application/pgp-encrypted", | ||
| 660 | Disposition => "inline", | ||
| 661 | Data => ["Version: 1\n"], | ||
| 662 | Encoding => "7bit", | ||
| 663 | ); | ||
| 664 | } | ||
| 665 | |||
| 666 | #-- return the newly created entitiy and the part to work on | ||
| 667 | return ( $rfc_entity, $work_part ); | ||
| 668 | } | ||
| 669 | |||
| 670 | sub mime_sign { | ||
| 671 | my $self = shift; | ||
| 672 | my %par = @_; | ||
| 673 | my ($key_id, $passphrase, $entity) = | ||
| 674 | @par{'key_id','passphrase','entity'}; | ||
| 675 | |||
| 676 | #-- ignore any PIPE signals, in case of gpg exited | ||
| 677 | #-- early before we fed our data into it. | ||
| 678 | local $SIG{PIPE} = 'IGNORE'; | ||
| 679 | |||
| 680 | #-- we parse gpg's output and rely on english | ||
| 681 | local $ENV{LC_ALL} = "C"; | ||
| 682 | |||
| 683 | #-- get default key ID and passphrase, if not given | ||
| 684 | $key_id = $self->get_default_key_id if not defined $key_id; | ||
| 685 | $passphrase = $self->get_default_passphrase if not defined $passphrase; | ||
| 686 | |||
| 687 | #-- check parameters | ||
| 688 | die "No key_id set" if $key_id eq ''; | ||
| 689 | die "No passphrase set" if not defined $passphrase; | ||
| 690 | |||
| 691 | #-- build entity for signed version | ||
| 692 | #-- (only the 2nd part with the signature data | ||
| 693 | #-- needs to be added later) | ||
| 694 | my ( $signed_entity, $sign_part ) = $self->build_rfc3156_multipart_entity( | ||
| 695 | entity => $entity, | ||
| 696 | method => "sign", | ||
| 697 | ); | ||
| 698 | |||
| 699 | #-- get a GnuPG::Interface | ||
| 700 | my $gpg = $self->new_gpg_interface( | ||
| 701 | options => { | ||
| 702 | armor => 1, | ||
| 703 | default_key => $key_id, | ||
| 704 | }, | ||
| 705 | passphrase => $passphrase, | ||
| 706 | ); | ||
| 707 | |||
| 708 | #-- initialize handles | ||
| 709 | my $stdin = IO::Handle->new; | ||
| 710 | my $stdout = IO::Handle->new; | ||
| 711 | my $stderr = IO::Handle->new; | ||
| 712 | my $handles = GnuPG::Handles->new( | ||
| 713 | stdin => $stdin, | ||
| 714 | stdout => $stdout, | ||
| 715 | stderr => $stderr, | ||
| 716 | ); | ||
| 717 | |||
| 718 | #-- execute gpg for signing | ||
| 719 | my $pid = $gpg->detach_sign( handles => $handles ); | ||
| 720 | |||
| 721 | #-- put encoded entity data into temporary file | ||
| 722 | #-- (faster than in-memory operation) | ||
| 723 | my ( $data_fh, $data_file ) = File::Temp::tempfile(); | ||
| 724 | unlink $data_file; | ||
| 725 | $sign_part->print($data_fh); | ||
| 726 | |||
| 727 | #-- perform I/O (multiplexed to prevent blocking) | ||
| 728 | my ( $output_stdout, $output_stderr ) = ("", ""); | ||
| 729 | $self->perform_multiplexed_gpg_io( | ||
| 730 | data_fh => $data_fh, | ||
| 731 | data_canonify => 1, | ||
| 732 | stdin_fh => $stdin, | ||
| 733 | stderr_fh => $stderr, | ||
| 734 | stdout_fh => $stdout, | ||
| 735 | stderr_sref => \$output_stderr, | ||
| 736 | stdout_sref => \$output_stdout, | ||
| 737 | ); | ||
| 738 | |||
| 739 | #-- close reader filehandles (stdin was closed | ||
| 740 | #-- by perform_multiplexed_gpg_io()) | ||
| 741 | close $stdout; | ||
| 742 | close $stderr; | ||
| 743 | |||
| 744 | #-- fetch zombie | ||
| 745 | waitpid $pid, 0; | ||
| 746 | die $output_stderr if $?; | ||
| 747 | |||
| 748 | #-- attach OpenPGP signature as second part | ||
| 749 | $signed_entity->attach( | ||
| 750 | Type => "application/pgp-signature", | ||
| 751 | Disposition => "inline", | ||
| 752 | Data => [$output_stdout], | ||
| 753 | Encoding => "7bit", | ||
| 754 | ); | ||
| 755 | |||
| 756 | #-- debugging: create file with signed data | ||
| 757 | if ( $self->get_debug ) { | ||
| 758 | $self->save_debug_file( | ||
| 759 | name => "mime-sign-data.txt", | ||
| 760 | data_fh => $data_fh, | ||
| 761 | ); | ||
| 762 | $self->save_debug_file( | ||
| 763 | name => "mime-sign-entity.txt", | ||
| 764 | data => \$signed_entity->as_string, | ||
| 765 | ); | ||
| 766 | } | ||
| 767 | |||
| 768 | #-- close temporary data filehandle | ||
| 769 | close $data_fh; | ||
| 770 | |||
| 771 | #-- return signed entity | ||
| 772 | return $signed_entity; | ||
| 773 | } | ||
| 774 | |||
| 775 | sub mime_encrypt { | ||
| 776 | my $self = shift; | ||
| 777 | my %par = @_; | ||
| 778 | my ($entity, $recipients) = @par{'entity','recipients'}; | ||
| 779 | |||
| 780 | #-- call mime_sign_encrypt() with no_sign option | ||
| 781 | return $self->mime_sign_encrypt( | ||
| 782 | entity => $entity, | ||
| 783 | recipients => $recipients, | ||
| 784 | _no_sign => 1, | ||
| 785 | ); | ||
| 786 | } | ||
| 787 | |||
| 788 | sub mime_sign_encrypt { | ||
| 789 | my $self = shift; | ||
| 790 | my %par = @_; | ||
| 791 | my ($key_id, $passphrase, $entity, $recipients, $_no_sign) = | ||
| 792 | @par{'key_id','passphrase','entity','recipients','_no_sign'}; | ||
| 793 | |||
| 794 | #-- ignore any PIPE signals, in case of gpg exited | ||
| 795 | #-- early before we fed our data into it. | ||
| 796 | local $SIG{PIPE} = 'IGNORE'; | ||
| 797 | |||
| 798 | #-- we parse gpg's output and rely on english | ||
| 799 | local $ENV{LC_ALL} = "C"; | ||
| 800 | |||
| 801 | #-- get default key ID and passphrase, if not given | ||
| 802 | $key_id = $self->get_default_key_id if not defined $key_id; | ||
| 803 | $passphrase = $self->get_default_passphrase if not defined $passphrase; | ||
| 804 | |||
| 805 | #-- check parameters | ||
| 806 | die "No key_id set" if not $_no_sign and $key_id eq ''; | ||
| 807 | die "No passphrase set" if not $_no_sign and not defined $passphrase; | ||
| 808 | |||
| 809 | #-- build entity for encrypted version | ||
| 810 | #-- (only the 2nd part with the encrypted data | ||
| 811 | #-- needs to be added later) | ||
| 812 | my ( $encrypted_entity, $encrypt_part ) | ||
| 813 | = $self->build_rfc3156_multipart_entity( | ||
| 814 | entity => $entity, | ||
| 815 | method => "encrypt", | ||
| 816 | ); | ||
| 817 | |||
| 818 | #-- get a GnuPG::Interface | ||
| 819 | my $gpg = $self->new_gpg_interface( | ||
| 820 | options => { | ||
| 821 | armor => 1, | ||
| 822 | default_key => $key_id, | ||
| 823 | }, | ||
| 824 | passphrase => $passphrase, | ||
| 825 | ); | ||
| 826 | |||
| 827 | #-- add recipients, but first extract the mail-adress | ||
| 828 | #-- part, otherwise gpg couldn't find keys for adresses | ||
| 829 | #-- with quoted printable encodings in the name part- | ||
| 830 | $recipients = $self->extract_mail_address( recipients => $recipients, ); | ||
| 831 | $gpg->options->push_recipients($_) for @{$recipients}; | ||
| 832 | |||
| 833 | #-- add default key to recipients if requested | ||
| 834 | $gpg->options->push_recipients( $self->get_default_key_id ) | ||
| 835 | if $self->get_default_key_encrypt | ||
| 836 | and $self->get_default_key_id; | ||
| 837 | |||
| 838 | #-- initialize handles | ||
| 839 | my $stdin = IO::Handle->new; | ||
| 840 | my $stdout = IO::Handle->new; | ||
| 841 | my $stderr = IO::Handle->new; | ||
| 842 | my $handles = GnuPG::Handles->new( | ||
| 843 | stdin => $stdin, | ||
| 844 | stdout => $stdout, | ||
| 845 | stderr => $stderr, | ||
| 846 | ); | ||
| 847 | |||
| 848 | #-- execute gpg for encryption | ||
| 849 | my $pid; | ||
| 850 | if ($_no_sign) { | ||
| 851 | $pid = $gpg->encrypt( handles => $handles ); | ||
| 852 | } | ||
| 853 | else { | ||
| 854 | $pid = $gpg->sign_and_encrypt( handles => $handles ); | ||
| 855 | } | ||
| 856 | |||
| 857 | #-- put encoded entity data into temporary file | ||
| 858 | #-- (faster than in-memory operation) | ||
| 859 | my ( $data_fh, $data_file ) = File::Temp::tempfile(); | ||
| 860 | unlink $data_file; | ||
| 861 | $encrypt_part->print($data_fh); | ||
| 862 | |||
| 863 | #-- perform I/O (multiplexed to prevent blocking) | ||
| 864 | my ( $output_stdout, $output_stderr ) = ("", ""); | ||
| 865 | $self->perform_multiplexed_gpg_io( | ||
| 866 | data_fh => $data_fh, | ||
| 867 | data_canonify => 1, | ||
| 868 | stdin_fh => $stdin, | ||
| 869 | stderr_fh => $stderr, | ||
| 870 | stdout_fh => $stdout, | ||
| 871 | stderr_sref => \$output_stderr, | ||
| 872 | stdout_sref => \$output_stdout, | ||
| 873 | ); | ||
| 874 | |||
| 875 | #-- close reader filehandles (stdin was closed | ||
| 876 | #-- by perform_multiplexed_gpg_io()) | ||
| 877 | close $stdout; | ||
| 878 | close $stderr; | ||
| 879 | |||
| 880 | #-- fetch zombie | ||
| 881 | waitpid $pid, 0; | ||
| 882 | die $output_stderr if $?; | ||
| 883 | |||
| 884 | #-- attach second part with the encrytped text | ||
| 885 | $encrypted_entity->attach( | ||
| 886 | Type => "application/octet-stream", | ||
| 887 | Disposition => "inline", | ||
| 888 | Data => [$output_stdout], | ||
| 889 | Encoding => "7bit", | ||
| 890 | ); | ||
| 891 | |||
| 892 | #-- debugging: create file with encrypted data | ||
| 893 | if ( $self->get_debug ) { | ||
| 894 | $self->save_debug_file( | ||
| 895 | name => "mime-enc-data.txt", | ||
| 896 | data_fh => $data_fh, | ||
| 897 | ); | ||
| 898 | $self->save_debug_file( | ||
| 899 | name => "mime-enc-entity.txt", | ||
| 900 | data => \$encrypted_entity->as_string, | ||
| 901 | ); | ||
| 902 | } | ||
| 903 | |||
| 904 | #-- close temporary data filehandle | ||
| 905 | close $data_fh; | ||
| 906 | |||
| 907 | #-- return encrytped entity | ||
| 908 | return $encrypted_entity; | ||
| 909 | } | ||
| 910 | |||
| 911 | sub armor_sign { | ||
| 912 | my $self = shift; | ||
| 913 | my %par = @_; | ||
| 914 | my ($key_id, $passphrase, $entity) = | ||
| 915 | @par{'key_id','passphrase','entity'}; | ||
| 916 | |||
| 917 | #-- ignore any PIPE signals, in case of gpg exited | ||
| 918 | #-- early before we fed our data into it. | ||
| 919 | local $SIG{PIPE} = 'IGNORE'; | ||
| 920 | |||
| 921 | #-- we parse gpg's output and rely on english | ||
| 922 | local $ENV{LC_ALL} = "C"; | ||
| 923 | |||
| 924 | #-- get default key ID and passphrase, if not given | ||
| 925 | $key_id = $self->get_default_key_id if not defined $key_id; | ||
| 926 | $passphrase = $self->get_default_passphrase if not defined $passphrase; | ||
| 927 | |||
| 928 | #-- check parameters | ||
| 929 | die "No key_id set" if $key_id eq ''; | ||
| 930 | die "No passphrase set" if not defined $passphrase; | ||
| 931 | die "Entity has no body" if not $entity->bodyhandle; | ||
| 932 | |||
| 933 | #-- check, if body content-transfer-encoding is 7bit safe | ||
| 934 | if ( not $self->get_no_strict_7bit_encoding ) { | ||
| 935 | my $encoding = $entity->head->get("content-transfer-encoding"); | ||
| 936 | die "Content transfer encoding '$encoding' is not 7bit safe" | ||
| 937 | unless $encoding =~ /^(quoted-printable|base64|7bit)\s*$/i; | ||
| 938 | } | ||
| 939 | |||
| 940 | #-- get a GnuPG::Interface, with ASCII armor enabled | ||
| 941 | my $gpg = $self->new_gpg_interface( | ||
| 942 | options => { | ||
| 943 | armor => 1, | ||
| 944 | default_key => $key_id, | ||
| 945 | }, | ||
| 946 | passphrase => $passphrase, | ||
| 947 | ); | ||
| 948 | |||
| 949 | #-- initialize handles | ||
| 950 | my $stdin = IO::Handle->new; | ||
| 951 | my $stdout = IO::Handle->new; | ||
| 952 | my $stderr = IO::Handle->new; | ||
| 953 | my $handles = GnuPG::Handles->new( | ||
| 954 | stdin => $stdin, | ||
| 955 | stdout => $stdout, | ||
| 956 | stderr => $stderr, | ||
| 957 | ); | ||
| 958 | |||
| 959 | #-- execute gpg for signing | ||
| 960 | my $pid = $gpg->clearsign( handles => $handles ); | ||
| 961 | |||
| 962 | #-- put encoded entity data into temporary file | ||
| 963 | #-- (faster than in-memory operation) | ||
| 964 | my ( $data_fh, $data_file ) = File::Temp::tempfile(); | ||
| 965 | unlink $data_file; | ||
| 966 | $entity->print($data_fh); | ||
| 967 | |||
| 968 | #-- perform I/O (multiplexed to prevent blocking) | ||
| 969 | my ( $output_stdout, $output_stderr ) = ("", ""); | ||
| 970 | $self->perform_multiplexed_gpg_io( | ||
| 971 | data_fh => $data_fh, | ||
| 972 | data_canonify => 1, | ||
| 973 | stdin_fh => $stdin, | ||
| 974 | stderr_fh => $stderr, | ||
| 975 | stdout_fh => $stdout, | ||
| 976 | stderr_sref => \$output_stderr, | ||
| 977 | stdout_sref => \$output_stdout, | ||
| 978 | ); | ||
| 979 | |||
| 980 | #-- close reader filehandles (stdin was closed | ||
| 981 | #-- by perform_multiplexed_gpg_io()) | ||
| 982 | close $stdout; | ||
| 983 | close $stderr; | ||
| 984 | |||
| 985 | #-- fetch zombie | ||
| 986 | waitpid $pid, 0; | ||
| 987 | die $output_stderr if $?; | ||
| 988 | |||
| 989 | #-- build entity for encrypted version | ||
| 990 | my $signed_entity = MIME::Entity->build( Data => [$output_stdout], ); | ||
| 991 | |||
| 992 | #-- copy all header fields from original entity | ||
| 993 | foreach my $tag ( $entity->head->tags ) { | ||
| 994 | my @values = $entity->head->get($tag); | ||
| 995 | for ( my $i = 0; $i < @values; ++$i ) { | ||
| 996 | $signed_entity->head->replace( $tag, $values[$i], $i ); | ||
| 997 | } | ||
| 998 | } | ||
| 999 | |||
| 1000 | #-- debugging: create file with signed data | ||
| 1001 | if ( $self->get_debug ) { | ||
| 1002 | $self->save_debug_file( | ||
| 1003 | name => "armor-sign-data.txt", | ||
| 1004 | data => \$entity->bodyhandle->as_string, | ||
| 1005 | ); | ||
| 1006 | $self->save_debug_file( | ||
| 1007 | name => "armor-sign-entity.txt", | ||
| 1008 | data => \$signed_entity->as_string, | ||
| 1009 | ); | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | #-- return the signed entity | ||
| 1013 | return $signed_entity; | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | sub armor_encrypt { | ||
| 1017 | my $self = shift; | ||
| 1018 | my %par = @_; | ||
| 1019 | my ($entity, $recipients) = @par{'entity','recipients'}; | ||
| 1020 | |||
| 1021 | #-- call armor_sign_encrypt() with no_sign option | ||
| 1022 | return $self->armor_sign_encrypt( | ||
| 1023 | entity => $entity, | ||
| 1024 | recipients => $recipients, | ||
| 1025 | _no_sign => 1, | ||
| 1026 | ); | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | sub armor_sign_encrypt { | ||
| 1030 | my $self = shift; | ||
| 1031 | my %par = @_; | ||
| 1032 | my ($key_id, $passphrase, $entity, $recipients, $_no_sign) = | ||
| 1033 | @par{'key_id','passphrase','entity','recipients','_no_sign'}; | ||
| 1034 | |||
| 1035 | #-- ignore any PIPE signals, in case of gpg exited | ||
| 1036 | #-- early before we fed our data into it. | ||
| 1037 | local $SIG{PIPE} = 'IGNORE'; | ||
| 1038 | |||
| 1039 | #-- we parse gpg's output and rely on english | ||
| 1040 | local $ENV{LC_ALL} = "C"; | ||
| 1041 | |||
| 1042 | #-- get default key ID and passphrase, if not given | ||
| 1043 | if ( not $_no_sign ) { | ||
| 1044 | $key_id = $self->get_default_key_id if not defined $key_id; | ||
| 1045 | $passphrase = $self->get_default_passphrase | ||
| 1046 | if not defined $passphrase; | ||
| 1047 | |||
| 1048 | #-- check parameters | ||
| 1049 | die "No key_id set" if $key_id eq ''; | ||
| 1050 | die "No passphrase set" if not defined $passphrase; | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | #-- check parameters | ||
| 1054 | die "Entity has no body" if not $entity->bodyhandle; | ||
| 1055 | |||
| 1056 | #-- get a GnuPG::Interface, with ASCII armor enabled | ||
| 1057 | my $gpg = $self->new_gpg_interface( | ||
| 1058 | options => { | ||
| 1059 | armor => 1, | ||
| 1060 | default_key => $key_id, | ||
| 1061 | }, | ||
| 1062 | passphrase => $passphrase, | ||
| 1063 | ); | ||
| 1064 | |||
| 1065 | #-- add recipients, but first extract the mail-adress | ||
| 1066 | #-- part, otherwise gpg couldn't find keys for adresses | ||
| 1067 | #-- with quoted printable encodings in the name part- | ||
| 1068 | $recipients = $self->extract_mail_address( recipients => $recipients, ); | ||
| 1069 | $gpg->options->push_recipients($_) for @{$recipients}; | ||
| 1070 | |||
| 1071 | #-- add default key to recipients if requested | ||
| 1072 | $gpg->options->push_recipients( $self->get_default_key_id ) | ||
| 1073 | if $self->get_default_key_encrypt | ||
| 1074 | and $self->get_default_key_id; | ||
| 1075 | |||
| 1076 | #-- initialize handles | ||
| 1077 | my $stdin = IO::Handle->new; | ||
| 1078 | my $stdout = IO::Handle->new; | ||
| 1079 | my $stderr = IO::Handle->new; | ||
| 1080 | my $handles = GnuPG::Handles->new( | ||
| 1081 | stdin => $stdin, | ||
| 1082 | stdout => $stdout, | ||
| 1083 | stderr => $stderr, | ||
| 1084 | ); | ||
| 1085 | |||
| 1086 | #-- execute gpg for encryption | ||
| 1087 | my $pid; | ||
| 1088 | if ($_no_sign) { | ||
| 1089 | $pid = $gpg->encrypt( handles => $handles ); | ||
| 1090 | } | ||
| 1091 | else { | ||
| 1092 | $pid = $gpg->sign_and_encrypt( handles => $handles ); | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | #-- put encoded entity data into temporary file | ||
| 1096 | #-- (faster than in-memory operation) | ||
| 1097 | my ( $data_fh, $data_file ) = File::Temp::tempfile(); | ||
| 1098 | unlink $data_file; | ||
| 1099 | $entity->print($data_fh); | ||
| 1100 | |||
| 1101 | #-- perform I/O (multiplexed to prevent blocking) | ||
| 1102 | my ( $output_stdout, $output_stderr ) = ("", ""); | ||
| 1103 | $self->perform_multiplexed_gpg_io( | ||
| 1104 | data_fh => $data_fh, | ||
| 1105 | data_canonify => 0, | ||
| 1106 | stdin_fh => $stdin, | ||
| 1107 | stderr_fh => $stderr, | ||
| 1108 | stdout_fh => $stdout, | ||
| 1109 | stderr_sref => \$output_stderr, | ||
| 1110 | stdout_sref => \$output_stdout, | ||
| 1111 | ); | ||
| 1112 | |||
| 1113 | #-- close reader filehandles (stdin was closed | ||
| 1114 | #-- by perform_multiplexed_gpg_io()) | ||
| 1115 | close $stdout; | ||
| 1116 | close $stderr; | ||
| 1117 | |||
| 1118 | #-- fetch zombie | ||
| 1119 | waitpid $pid, 0; | ||
| 1120 | die $output_stderr if $?; | ||
| 1121 | |||
| 1122 | #-- build entity for encrypted version | ||
| 1123 | my $encrypted_entity = MIME::Entity->build( | ||
| 1124 | Type => "text/plain", | ||
| 1125 | Encoding => "7bit", | ||
| 1126 | Data => [$output_stdout], | ||
| 1127 | ); | ||
| 1128 | |||
| 1129 | #-- copy header fields from original entity | ||
| 1130 | foreach my $tag ( $entity->head->tags ) { | ||
| 1131 | next if $tag =~ /^content/i; | ||
| 1132 | my @values = $entity->head->get($tag); | ||
| 1133 | for ( my $i = 0; $i < @values; ++$i ) { | ||
| 1134 | $encrypted_entity->head->replace( $tag, $values[$i], $i ); | ||
| 1135 | } | ||
| 1136 | } | ||
| 1137 | |||
| 1138 | #-- debugging: create file with signed data | ||
| 1139 | if ( $self->get_debug ) { | ||
| 1140 | $self->save_debug_file( | ||
| 1141 | name => "armor-enc-data.txt", | ||
| 1142 | data => \$entity->bodyhandle->as_string, | ||
| 1143 | ); | ||
| 1144 | $self->save_debug_file( | ||
| 1145 | name => "armor-enc-entity.txt", | ||
| 1146 | data => \$encrypted_entity->as_string, | ||
| 1147 | ); | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | #-- return the signed entity | ||
| 1151 | return $encrypted_entity; | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | sub decrypt { | ||
| 1155 | my $self = shift; | ||
| 1156 | my %par = @_; | ||
| 1157 | my ($entity, $passphrase) = @par{'entity','passphrase'}; | ||
| 1158 | |||
| 1159 | #-- ignore any PIPE signals, in case of gpg exited | ||
| 1160 | #-- early before we fed our data into it. | ||
| 1161 | local $SIG{PIPE} = 'IGNORE'; | ||
| 1162 | |||
| 1163 | #-- we parse gpg's output and rely on english | ||
| 1164 | local $ENV{LC_ALL} = "C"; | ||
| 1165 | |||
| 1166 | #-- get default passphrase, if not given | ||
| 1167 | $passphrase = $self->get_default_passphrase if not defined $passphrase; | ||
| 1168 | |||
| 1169 | #-- check if the entity is encrypted at all | ||
| 1170 | #-- (dies if not) | ||
| 1171 | my $encrypted_text; | ||
| 1172 | my $is_armor = $self->check_encryption( | ||
| 1173 | entity => $entity, | ||
| 1174 | encrypted_text_sref => \$encrypted_text, | ||
| 1175 | ); | ||
| 1176 | |||
| 1177 | #-- get a GnuPG::Interface | ||
| 1178 | my $gpg = $self->new_gpg_interface( passphrase => $passphrase, ); | ||
| 1179 | |||
| 1180 | #-- initialize handles | ||
| 1181 | my $stdin = IO::Handle->new; | ||
| 1182 | my $stdout = IO::Handle->new; | ||
| 1183 | my $stderr = IO::Handle->new; | ||
| 1184 | my $status = IO::Handle->new; | ||
| 1185 | |||
| 1186 | my $handles = GnuPG::Handles->new( | ||
| 1187 | stdin => $stdin, | ||
| 1188 | stdout => $stdout, | ||
| 1189 | stderr => $stderr, | ||
| 1190 | status => $status, | ||
| 1191 | ); | ||
| 1192 | |||
| 1193 | #-- start gpg for decryption | ||
| 1194 | my $pid = $gpg->decrypt( handles => $handles ); | ||
| 1195 | |||
| 1196 | #-- put encoded entity data into temporary file | ||
| 1197 | #-- (faster than in-memory operation) | ||
| 1198 | my ( $data_fh, $data_file ) = File::Temp::tempfile(); | ||
| 1199 | unlink $data_file; | ||
| 1200 | print $data_fh $encrypted_text; | ||
| 1201 | |||
| 1202 | #-- perform I/O (multiplexed to prevent blocking) | ||
| 1203 | my ( $output_stdout, $output_stderr, $output_status ) = ( "", "", "" ); | ||
| 1204 | |||
| 1205 | $self->perform_multiplexed_gpg_io( | ||
| 1206 | data_fh => $data_fh, | ||
| 1207 | data_canonify => 1, | ||
| 1208 | stdin_fh => $stdin, | ||
| 1209 | stderr_fh => $stderr, | ||
| 1210 | stdout_fh => $stdout, | ||
| 1211 | status_fh => $status, | ||
| 1212 | stderr_sref => \$output_stderr, | ||
| 1213 | stdout_sref => \$output_stdout, | ||
| 1214 | status_sref => \$output_status, | ||
| 1215 | ); | ||
| 1216 | |||
| 1217 | #-- close reader filehandles (stdin was closed | ||
| 1218 | #-- by perform_multiplexed_gpg_io()) | ||
| 1219 | close $stdout; | ||
| 1220 | close $stderr; | ||
| 1221 | |||
| 1222 | #-- fetch zombie | ||
| 1223 | waitpid $pid, 0; | ||
| 1224 | my $rc = $? >> 8; | ||
| 1225 | |||
| 1226 | #-- don't die here for return values != 0, because | ||
| 1227 | #-- this also happens for encrypted+signed mails, | ||
| 1228 | #-- where the public key is missing for verification | ||
| 1229 | #-- and that's not intended here. | ||
| 1230 | |||
| 1231 | #-- parse decrypted text | ||
| 1232 | my $parser = new MIME::Parser; | ||
| 1233 | $parser->output_to_core(1); | ||
| 1234 | |||
| 1235 | # for armor message (which usually contain no MIME entity) | ||
| 1236 | # and if the first line seems to be no header, add an empty | ||
| 1237 | # line at the top, otherwise the first line of a text message | ||
| 1238 | # will be removed by the parser. | ||
| 1239 | if ( $is_armor and $output_stdout !~ /^[\w-]+:/ ) { | ||
| 1240 | $output_stdout = "\n" . $output_stdout; | ||
| 1241 | } | ||
| 1242 | |||
| 1243 | my $dec_entity = $parser->parse_data( $output_stdout ); | ||
| 1244 | |||
| 1245 | #-- Add headers from original entity | ||
| 1246 | if ( $dec_entity->head->as_string eq '' ) { | ||
| 1247 | $dec_entity->head( $entity->head->dup ); | ||
| 1248 | } | ||
| 1249 | else { | ||
| 1250 | |||
| 1251 | #-- copy header fields from original entity | ||
| 1252 | foreach my $tag ( $entity->head->tags ) { | ||
| 1253 | next if $tag =~ /^content/i; | ||
| 1254 | my @values = $entity->head->get($tag); | ||
| 1255 | for ( my $i = 0; $i < @values; ++$i ) { | ||
| 1256 | $dec_entity->head->replace( $tag, $values[$i], $i ); | ||
| 1257 | } | ||
| 1258 | } | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | #-- debugging: create file with encrypted data | ||
| 1262 | if ( $self->get_debug ) { | ||
| 1263 | $self->save_debug_file( | ||
| 1264 | name => "dec-data.txt", | ||
| 1265 | data => $dec_entity->as_string, | ||
| 1266 | ); | ||
| 1267 | } | ||
| 1268 | |||
| 1269 | #-- fetch information from gpg's stderr output | ||
| 1270 | #-- and construct a Mail::GPG::Result object from it | ||
| 1271 | my $result = Mail::GPG::Result->new( | ||
| 1272 | mail_gpg => $self, | ||
| 1273 | gpg_stdout => \$output_stdout, | ||
| 1274 | gpg_stderr => \$output_stderr, | ||
| 1275 | gpg_status => \$output_status, | ||
| 1276 | gpg_rc => $rc, | ||
| 1277 | ); | ||
| 1278 | |||
| 1279 | #-- return decrypted entity and result object | ||
| 1280 | return $dec_entity if not wantarray; | ||
| 1281 | return ( $dec_entity, $result ); | ||
| 1282 | } | ||
| 1283 | |||
| 1284 | sub verify { | ||
| 1285 | my $self = shift; | ||
| 1286 | my %par = @_; | ||
| 1287 | my ($entity) = $par{'entity'}; | ||
| 1288 | |||
| 1289 | #-- ignore any PIPE signals, in case of gpg exited | ||
| 1290 | #-- early before we fed our data into it. | ||
| 1291 | local $SIG{PIPE} = 'IGNORE'; | ||
| 1292 | |||
| 1293 | #-- we parse gpg's output and rely on english | ||
| 1294 | local $ENV{LC_ALL} = "C"; | ||
| 1295 | |||
| 1296 | #-- check if the entity is signed | ||
| 1297 | my ( $signed_text, $signature_text ); | ||
| 1298 | |||
| 1299 | if ( $entity->effective_type =~ m!multipart/signed!i ) { | ||
| 1300 | |||
| 1301 | #-- is this a valid RFC 3156 multipart/signed entity? | ||
| 1302 | die "Entity must have two parts" | ||
| 1303 | if $entity->parts != 2; | ||
| 1304 | die "Entity is not OpenPGP signed" | ||
| 1305 | unless $entity->parts(1)->effective_type | ||
| 1306 | =~ m!application/pgp-signature!i; | ||
| 1307 | |||
| 1308 | #-- hopefully the $entity was parsed with | ||
| 1309 | #-- decode_bodies(0), otherwise this would | ||
| 1310 | #-- return decoded data, but the signature | ||
| 1311 | #-- is calculated on the *encoded* version. | ||
| 1312 | $signed_text = $entity->parts(0)->as_string; | ||
| 1313 | $signature_text = $entity->parts(1)->body_as_string; | ||
| 1314 | |||
| 1315 | } | ||
| 1316 | elsif ( $entity->bodyhandle ) { | ||
| 1317 | |||
| 1318 | #-- probably an ASCII armor signed entity | ||
| 1319 | #-- in that case we need the *decoded* data | ||
| 1320 | $signed_text = $entity->bodyhandle->as_string; | ||
| 1321 | die "Entity is not OpenPGP signed" | ||
| 1322 | unless $signed_text =~ /^-----BEGIN PGP SIGNED MESSAGE-----/m; | ||
| 1323 | } | ||
| 1324 | else { | ||
| 1325 | die "Entity is not multipart/signed and has no body"; | ||
| 1326 | } | ||
| 1327 | |||
| 1328 | #-- get a GnuPG::Interface | ||
| 1329 | my $gpg = $self->new_gpg_interface; | ||
| 1330 | |||
| 1331 | #-- initialize handles | ||
| 1332 | my $stdin = IO::Handle->new; | ||
| 1333 | my $stdout = IO::Handle->new; | ||
| 1334 | my $stderr = IO::Handle->new; | ||
| 1335 | my $status = IO::Handle->new; | ||
| 1336 | |||
| 1337 | my $handles = GnuPG::Handles->new( | ||
| 1338 | stdin => $stdin, | ||
| 1339 | stdout => $stdout, | ||
| 1340 | stderr => $stderr, | ||
| 1341 | status => $status, | ||
| 1342 | ); | ||
| 1343 | |||
| 1344 | #-- distinguish between ascii amor embedded signature | ||
| 1345 | #-- and detached signature (RFC 3156) | ||
| 1346 | my ( $pid, $sign_file, $sign_fh ); | ||
| 1347 | if ($signature_text) { | ||
| 1348 | |||
| 1349 | #-- signature is detached, save it to a temp file | ||
| 1350 | ( $sign_fh, $sign_file ) = File::Temp::tempfile(); | ||
| 1351 | print $sign_fh $signature_text; | ||
| 1352 | close $sign_fh; | ||
| 1353 | |||
| 1354 | #-- pass signature filename to gpg | ||
| 1355 | $pid = $gpg->verify( | ||
| 1356 | handles => $handles, | ||
| 1357 | command_args => [ $sign_file, "-" ], | ||
| 1358 | ); | ||
| 1359 | |||
| 1360 | } | ||
| 1361 | else { | ||
| 1362 | |||
| 1363 | #-- ASCII armor message with embedded signature | ||
| 1364 | $pid = $gpg->verify( handles => $handles, ); | ||
| 1365 | } | ||
| 1366 | |||
| 1367 | #-- put encoded entity data into temporary file | ||
| 1368 | #-- (faster than in-memory operation) | ||
| 1369 | my ( $data_fh, $data_file ) = File::Temp::tempfile(); | ||
| 1370 | unlink $data_file; | ||
| 1371 | print $data_fh $signed_text; | ||
| 1372 | |||
| 1373 | #-- perform I/O (multiplexed to prevent blocking) | ||
| 1374 | my ( $output_stdout, $output_stderr, $output_status ) = ( "", "", "" ); | ||
| 1375 | $self->perform_multiplexed_gpg_io( | ||
| 1376 | data_fh => $data_fh, | ||
| 1377 | data_canonify => 1, | ||
| 1378 | stdin_fh => $stdin, | ||
| 1379 | stderr_fh => $stderr, | ||
| 1380 | stdout_fh => $stdout, | ||
| 1381 | status_fh => $status, | ||
| 1382 | stderr_sref => \$output_stderr, | ||
| 1383 | stdout_sref => \$output_stdout, | ||
| 1384 | status_sref => \$output_status, | ||
| 1385 | ); | ||
| 1386 | |||
| 1387 | #-- close reader filehandles (stdin was closed | ||
| 1388 | #-- by perform_multiplexed_gpg_io()) | ||
| 1389 | close $stdout; | ||
| 1390 | close $stderr; | ||
| 1391 | |||
| 1392 | #-- fetch zombie | ||
| 1393 | waitpid $pid, 0; | ||
| 1394 | my $rc = $? >> 8; | ||
| 1395 | |||
| 1396 | #-- remove detached signature file | ||
| 1397 | unlink $sign_file if defined $sign_file; | ||
| 1398 | |||
| 1399 | #-- debugging: create file with verified data | ||
| 1400 | if ( $self->get_debug ) { | ||
| 1401 | $self->save_debug_file( | ||
| 1402 | name => "verify-data.txt", | ||
| 1403 | data => \$signed_text, | ||
| 1404 | ); | ||
| 1405 | } | ||
| 1406 | |||
| 1407 | #-- construct a Mail::GPG::Result object from | ||
| 1408 | #-- gpg's stderr output | ||
| 1409 | my $result = Mail::GPG::Result->new( | ||
| 1410 | mail_gpg => $self, | ||
| 1411 | gpg_stdout => \$output_stdout, | ||
| 1412 | gpg_stderr => \$output_stderr, | ||
| 1413 | gpg_status => \$output_status, | ||
| 1414 | gpg_rc => $rc, | ||
| 1415 | ); | ||
| 1416 | |||
| 1417 | #-- return result object | ||
| 1418 | return $result; | ||
| 1419 | } | ||
| 1420 | |||
| 1421 | sub is_encrypted { | ||
| 1422 | my $self = shift; | ||
| 1423 | my %par = @_; | ||
| 1424 | my ($entity) = $par{'entity'}; | ||
| 1425 | |||
| 1426 | if ( $entity->effective_type =~ m!multipart/encrypted!i ) { | ||
| 1427 | |||
| 1428 | #-- is this a valid RFC 3156 multipart/encrypted entity? | ||
| 1429 | return 0 if $entity->parts != 2; | ||
| 1430 | return 0 | ||
| 1431 | unless $entity->parts(0)->effective_type | ||
| 1432 | =~ m!application/pgp-encrypted!i; | ||
| 1433 | |||
| 1434 | } | ||
| 1435 | elsif ( $entity->bodyhandle ) { | ||
| 1436 | |||
| 1437 | #-- probably an ASCII armor encrypted entity | ||
| 1438 | #-- check the decoded body for a PGP message | ||
| 1439 | return 0 | ||
| 1440 | unless $entity->bodyhandle->as_string | ||
| 1441 | =~ /^-----BEGIN PGP MESSAGE-----/m; | ||
| 1442 | } | ||
| 1443 | else { | ||
| 1444 | return 0; | ||
| 1445 | } | ||
| 1446 | |||
| 1447 | return 1; | ||
| 1448 | } | ||
| 1449 | |||
| 1450 | sub is_signed { | ||
| 1451 | my $self = shift; | ||
| 1452 | my %par = @_; | ||
| 1453 | my ($entity) = $par{'entity'}; | ||
| 1454 | |||
| 1455 | if ( $entity->effective_type =~ m!multipart/signed!i ) { | ||
| 1456 | |||
| 1457 | #-- is this a valid RFC 3156 multipart/signed entity? | ||
| 1458 | return 0 if $entity->parts != 2; | ||
| 1459 | return 0 | ||
| 1460 | unless $entity->parts(1)->effective_type | ||
| 1461 | =~ m!application/pgp-signature!i; | ||
| 1462 | |||
| 1463 | } | ||
| 1464 | elsif ( $entity->bodyhandle ) { | ||
| 1465 | |||
| 1466 | #-- probably an ASCII armor signed entity, | ||
| 1467 | #-- check the decoded body for a PGP message | ||
| 1468 | return 0 | ||
| 1469 | unless $entity->bodyhandle->as_string | ||
| 1470 | =~ /^-----BEGIN PGP SIGNED MESSAGE-----/m; | ||
| 1471 | } | ||
| 1472 | else { | ||
| 1473 | return 0; | ||
| 1474 | } | ||
| 1475 | |||
| 1476 | return 1; | ||
| 1477 | } | ||
| 1478 | |||
| 1479 | sub is_signed_quick { | ||
| 1480 | my $self = shift; | ||
| 1481 | my %par = @_; | ||
| 1482 | my ($mail_fh, $mail_sref) = @par{'mail_fh', 'mail_sref'}; | ||
| 1483 | |||
| 1484 | croak "Specify mail_fh xor mail_sref" | ||
| 1485 | unless $mail_fh xor $mail_sref; | ||
| 1486 | |||
| 1487 | if ( defined $mail_fh ) { | ||
| 1488 | |||
| 1489 | #-- rewind filehandle | ||
| 1490 | seek( $mail_fh, 0, 0 ); | ||
| 1491 | |||
| 1492 | #-- read filehandle and do rough checks | ||
| 1493 | local ($_); | ||
| 1494 | my $is_signed = 0; | ||
| 1495 | while (<$mail_fh>) { | ||
| 1496 | if (m!application/pgp-signature!i) { | ||
| 1497 | $is_signed = 1; | ||
| 1498 | last; | ||
| 1499 | } | ||
| 1500 | if (/^-----BEGIN PGP SIGNED MESSAGE-----/) { | ||
| 1501 | $is_signed = 1; | ||
| 1502 | last; | ||
| 1503 | } | ||
| 1504 | } | ||
| 1505 | |||
| 1506 | #-- rewind filehandle again | ||
| 1507 | seek( $mail_fh, 0, 0 ); | ||
| 1508 | |||
| 1509 | #-- return sign status | ||
| 1510 | return $is_signed; | ||
| 1511 | |||
| 1512 | } | ||
| 1513 | elsif ( defined $mail_sref ) { | ||
| 1514 | |||
| 1515 | #-- looks like a RFC 3156 multipart/signed entity? | ||
| 1516 | return 1 if $$mail_sref =~ m!application/pgp-signature!i; | ||
| 1517 | |||
| 1518 | #-- or ASCII armor signed? | ||
| 1519 | return 1 if $$mail_sref =~ m!^-----BEGIN PGP SIGNED MESSAGE-----!m; | ||
| 1520 | |||
| 1521 | #-- not signed at all | ||
| 1522 | return 0,; | ||
| 1523 | } | ||
| 1524 | |||
| 1525 | return 1; | ||
| 1526 | } | ||
| 1527 | |||
| 1528 | sub get_decrypt_key { | ||
| 1529 | my $self = shift; | ||
| 1530 | my %par = @_; | ||
| 1531 | my ($entity) = $par{'entity'}; | ||
| 1532 | |||
| 1533 | #-- ignore any PIPE signals, in case of gpg exited | ||
| 1534 | #-- early before we fed our data into it. | ||
| 1535 | local $SIG{PIPE} = 'IGNORE'; | ||
| 1536 | |||
| 1537 | #-- we parse gpg's output and rely on english | ||
| 1538 | local $ENV{LC_ALL} = "C"; | ||
| 1539 | |||
| 1540 | #-- check if the entity is encrypted at all | ||
| 1541 | #-- (dies if not) | ||
| 1542 | my $encrypted_text; | ||
| 1543 | my $is_armor = $self->check_encryption( | ||
| 1544 | entity => $entity, | ||
| 1545 | encrypted_text_sref => \$encrypted_text, | ||
| 1546 | ); | ||
| 1547 | |||
| 1548 | #-- get a GnuPG::Interface | ||
| 1549 | my $gpg = $self->new_gpg_interface; | ||
| 1550 | |||
| 1551 | #-- initialize handles | ||
| 1552 | my $stdin = IO::Handle->new; | ||
| 1553 | my $stdout = IO::Handle->new; | ||
| 1554 | my $stderr = IO::Handle->new; | ||
| 1555 | my $handles = GnuPG::Handles->new( | ||
| 1556 | stdin => $stdin, | ||
| 1557 | stdout => $stdout, | ||
| 1558 | stderr => $stderr, | ||
| 1559 | ); | ||
| 1560 | |||
| 1561 | #-- start gpg for decryption | ||
| 1562 | my $pid = $gpg->wrap_call( | ||
| 1563 | handles => $handles, | ||
| 1564 | commands => | ||
| 1565 | [ "--decrypt", "--batch", "--list-only", "--status-fd", "1" ], | ||
| 1566 | ); | ||
| 1567 | |||
| 1568 | #-- put encoded entity data into temporary file | ||
| 1569 | #-- (faster than in-memory operation) | ||
| 1570 | my ( $data_fh, $data_file ) = File::Temp::tempfile(); | ||
| 1571 | unlink $data_file; | ||
| 1572 | print $data_fh $encrypted_text; | ||
| 1573 | |||
| 1574 | #-- perform I/O (multiplexed to prevent blocking) | ||
| 1575 | my ( $output_stdout, $output_stderr ) = ( "", "" ); | ||
| 1576 | $self->perform_multiplexed_gpg_io( | ||
| 1577 | data_fh => $data_fh, | ||
| 1578 | data_canonify => 1, | ||
| 1579 | stdin_fh => $stdin, | ||
| 1580 | stderr_fh => $stderr, | ||
| 1581 | stdout_fh => $stdout, | ||
| 1582 | stderr_sref => \$output_stderr, | ||
| 1583 | stdout_sref => \$output_stdout, | ||
| 1584 | ); | ||
| 1585 | |||
| 1586 | #-- close reader filehandles (stdin was closed | ||
| 1587 | #-- by perform_multiplexed_gpg_io()) | ||
| 1588 | close $stdout; | ||
| 1589 | close $stderr; | ||
| 1590 | |||
| 1591 | #-- fetch zombie | ||
| 1592 | waitpid $pid, 0; | ||
| 1593 | my $rc = $? >> 8; | ||
| 1594 | |||
| 1595 | #-- grep ENC_TO and NO_SECKEY items | ||
| 1596 | my ( @enc_to_keys, %no_sec_keys, $line ); | ||
| 1597 | while ( $output_stdout =~ /^(.*)$/mg ) { | ||
| 1598 | $line = $1; | ||
| 1599 | push @enc_to_keys, $1 if $line =~ /ENC_TO\s+([^\s]+)/; | ||
| 1600 | $no_sec_keys{$1} = 1 if $line =~ /NO_SECKEY\s+([^\s]+)/; | ||
| 1601 | } | ||
| 1602 | |||
| 1603 | #-- find first key we have the secret portion of | ||
| 1604 | my $key_id; | ||
| 1605 | foreach my $k (@enc_to_keys) { | ||
| 1606 | if ( not exists $no_sec_keys{$k} ) { | ||
| 1607 | $key_id = $k; | ||
| 1608 | last; | ||
| 1609 | } | ||
| 1610 | } | ||
| 1611 | |||
| 1612 | #-- get mail address of this key | ||
| 1613 | my $key_mail; | ||
| 1614 | ( $key_id, $key_mail ) = $self->query_keyring( search => $key_id ); | ||
| 1615 | |||
| 1616 | return $key_id if not wantarray; | ||
| 1617 | return ( $key_id, $key_mail ); | ||
| 1618 | } | ||
| 1619 | |||
| 1620 | sub extract_mail_address { | ||
| 1621 | my $self = shift; | ||
| 1622 | my %par = @_; | ||
| 1623 | my ($recipients) = $par{'recipients'}; | ||
| 1624 | |||
| 1625 | my @recipients; | ||
| 1626 | |||
| 1627 | my $address; | ||
| 1628 | foreach my $r ( @{$recipients} ) { | ||
| 1629 | ($address) = Mail::Address->parse($r); | ||
| 1630 | push @recipients, $address | ||
| 1631 | ? $address->address | ||
| 1632 | : $r; | ||
| 1633 | } | ||
| 1634 | return \@recipients; | ||
| 1635 | } | ||
| 1636 | |||
| 1637 | sub parse { | ||
| 1638 | my $thing = shift; | ||
| 1639 | my %par = @_; | ||
| 1640 | my ($mail_fh, $mail_sref) = @par{'mail_fh','mail_sref'}; | ||
| 1641 | |||
| 1642 | croak "Specify mail_fh xor mail_sref" | ||
| 1643 | unless $mail_fh xor $mail_sref; | ||
| 1644 | |||
| 1645 | require MIME::Parser; | ||
| 1646 | my ( $parser, $entity ); | ||
| 1647 | |||
| 1648 | #-- First parse without body decoding, which is correct | ||
| 1649 | #-- for MIME messages | ||
| 1650 | $parser = MIME::Parser->new; | ||
| 1651 | $parser->output_to_core(1); | ||
| 1652 | $parser->decode_bodies(0); | ||
| 1653 | $entity = $mail_fh | ||
| 1654 | ? $parser->parse($mail_fh) | ||
| 1655 | : $parser->parse_data($$mail_sref); | ||
| 1656 | |||
| 1657 | #-- Ok, if this is a MIME message | ||
| 1658 | return $entity | ||
| 1659 | if $entity->effective_type eq 'multipart/signed' | ||
| 1660 | or $entity->effective_type eq 'multipart/encrypted'; | ||
| 1661 | |||
| 1662 | #-- Now with body decoding, which is MIME::Parser's default | ||
| 1663 | #-- and correct for OpenPGP armor message. Probably this | ||
| 1664 | #-- isn't an OpenPGP message at all. But also in that case | ||
| 1665 | #-- it's the best to return a decoded entity, as MIME::Parser | ||
| 1666 | #-- usually does. | ||
| 1667 | seek( $mail_fh, 0, 0 ) if $mail_fh; | ||
| 1668 | $parser->decode_bodies(1); | ||
| 1669 | $entity = $mail_fh | ||
| 1670 | ? $parser->parse($mail_fh) | ||
| 1671 | : $parser->parse_data($$mail_sref); | ||
| 1672 | |||
| 1673 | return $entity; | ||
| 1674 | } | ||
| 1675 | |||
| 1676 | sub get_key_trust { | ||
| 1677 | my $self = shift; | ||
| 1678 | my %par = @_; | ||
| 1679 | my ($key_id) = $par{'key_id'}; | ||
| 1680 | |||
| 1681 | # Suppress warnings about unknown record type 'tru' | ||
| 1682 | # in GnuPG::Interface | ||
| 1683 | local $SIG{__WARN__} = sub {1}; | ||
| 1684 | |||
| 1685 | my $gpg = $self->new_gpg_interface; | ||
| 1686 | my @keys = $gpg->get_public_keys($key_id); | ||
| 1687 | |||
| 1688 | croak "Request for key ID '$key_id' got multiple result" | ||
| 1689 | if @keys > 1; | ||
| 1690 | |||
| 1691 | return "" unless $keys[0]; | ||
| 1692 | return $keys[0]->owner_trust; | ||
| 1693 | } | ||
| 1694 | |||
| 1695 | 1; | ||
| 1696 | |||
| 1697 | __END__ | ||