GPG.pm

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