1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
52 package RT::Crypt::GnuPG;
56 use RT::EmailParser ();
57 use RT::Util 'safe_run_child';
61 RT::Crypt::GnuPG - encrypt/decrypt and sign/verify email messages with the GNU Privacy Guard (GPG)
65 This module provides support for encryption and signing of outgoing messages,
66 as well as the decryption and verification of incoming email.
70 You can control the configuration of this subsystem from RT's configuration file.
71 Some options are available via the web interface, but to enable this functionality, you
72 MUST start in the configuration file.
74 There are two hashes, GnuPG and GnuPGOptions in the configuration file. The
75 first one controls RT specific options. It enables you to enable/disable facility
76 or change the format of messages. The second one is a hash with options for the
77 'gnupg' utility. You can use it to define a keyserver, enable auto-retrieval keys
78 and set almost any option 'gnupg' supports on your system.
84 Set to true value to enable this subsystem:
91 However, note that you B<must> add the 'Auth::GnuPG' email filter to enable
92 the handling of incoming encrypted/signed messages.
94 =head3 Format of outgoing messages
96 Format of outgoing messages can be controlled using the 'OutgoingMessagesFormat'
97 option in the RT config:
100 ... other options ...
101 OutgoingMessagesFormat => 'RFC',
102 ... other options ...
108 ... other options ...
109 OutgoingMessagesFormat => 'Inline',
110 ... other options ...
113 This framework implements two formats of signing and encrypting of email messages:
119 This format is also known as GPG/MIME and described in RFC3156 and RFC1847.
120 Technique described in these RFCs is well supported by many mail user
121 agents (MUA), but some MUAs support only inline signatures and encryption,
122 so it's possible to use inline format (see below).
126 This format doesn't take advantage of MIME, but some mail clients do
127 not support GPG/MIME.
129 We sign text parts using clear signatures. For each attachments another
130 attachment with a signature is added with '.sig' extension.
132 Encryption of text parts is implemented using inline format, other parts
133 are replaced with attachments with the filename extension '.pgp'.
135 This format is discouraged because modern mail clients typically don't support
140 =head3 Encrypting data in the database
142 You can allow users to encrypt data in the database using
143 option C<AllowEncryptDataInDB>. By default it's disabled.
144 Users must have rights to see and modify tickets to use
149 Use this hash to set options of the 'gnupg' program. You can define almost any
150 option you want which gnupg supports, but never try to set options which
151 change output format or gnupg's commands, such as --sign (command),
152 --list-options (option) and other.
154 Some GnuPG options take arguments while others take none. (Such as --use-agent).
155 For options without specific value use C<undef> as hash value.
156 To disable these option just comment them out or delete them from the hash
159 'option-with-value' => 'value',
160 'enabled-option-without-value' => undef,
161 # 'commented-option' => 'value or undef',
164 B<NOTE> that options may contain '-' character and such options B<MUST> be
165 quoted, otherwise you can see quite cryptic error 'gpg: Invalid option "--0"'.
171 The GnuPG home directory, by default it is set to F</opt/rt3/var/data/gpg>.
173 You can manage this data with the 'gpg' commandline utility
174 using the GNUPGHOME environment variable or --homedir option.
175 Other utilities may be used as well.
177 In a standard installation, access to this directory should be granted to
178 the web server user which is running RT's web interface, but if you're running
179 cronjobs or other utilities that access RT directly via API and may generate
180 encrypted/signed notifications then the users you execute these scripts under
181 must have access too.
183 However, granting access to the dir to many users makes your setup less secure,
184 some features, such as auto-import of keys, may not be available if you do not.
185 To enable this features and suppress warnings about permissions on
186 the dir use --no-permission-warning.
190 This option is required in advance when RFC format for outgoing messages is
191 used. We can not get default algorithm from gpg program so RT uses 'SHA1' by
192 default. You may want to override it. You can use MD5, SHA1, RIPEMD160,
193 SHA256 or other, however use `gpg --version` command to get information about
194 supported algorithms by your gpg. These algorithms are listed as hash-functions.
198 This option lets you use GPG Agent to cache the passphrase of RT's key. See
199 L<http://www.gnupg.org/documentation/manuals/gnupg/Invoking-GPG_002dAGENT.html>
200 for information about GPG Agent.
204 This option lets you set the passphrase of RT's key directly. This option is
205 special in that it isn't passed directly to GPG, but is put into a file that
206 GPG then reads (which is more secure). The downside is that anyone who has read
207 access to your RT_SiteConfig.pm file can see the passphrase, thus we recommend
208 the --use-agent option instead.
212 Read `man gpg` to get list of all options this program support.
216 =head2 Per-queue options
218 Using the web interface it's possible to enable signing and/or encrypting by
219 default. As an administrative user of RT, open 'Configuration' then 'Queues',
220 and select a queue. On the page you can see information about the queue's keys
221 at the bottom and two checkboxes to choose default actions.
223 As well, encryption is enabled for autoreplies and other notifications when
224 an encypted message enters system via mailgate interface even if queue's
227 =head2 Handling incoming messages
229 To enable handling of encrypted and signed message in the RT you should add
230 'Auth::GnuPG' mail plugin.
232 Set(@MailPlugins, 'Auth::MailFrom', 'Auth::GnuPG', ...other filter...);
234 See also `perldoc lib/RT/Interface/Email/Auth/GnuPG.pm`.
236 =head2 Errors handling
238 There are several global templates created in the database by default. RT
239 uses these templates to send error messages to users or RT's owner. These
240 templates have 'Error:' or 'Error to RT owner:' prefix in the name. You can
241 adjust the text of the messages using the web interface.
243 Note that C<$TicketObj>, C<$TransactionObj> and other variable usually available
244 in RT's templates are not available in these templates, but each template
245 used for errors reporting has set of available data structures you can use to
246 build better messages. See default templates and descriptions below.
248 As well, you can disable particular notification by deleting content of
249 a template. You can delete a template too, but in this case you'll see
250 error messages in the logs when RT can not load template you've deleted.
252 =head3 Problems with public keys
254 Template 'Error: public key' is used to inform the user that RT has problems with
255 his public key and won't be able to send him encrypted content. There are several
256 reasons why RT can't use a key. However, the actual reason is not sent to the user,
257 but sent to RT owner using 'Error to RT owner: public key'.
259 The possible reasons: "Not Found", "Ambiguous specification", "Wrong
260 key usage", "Key revoked", "Key expired", "No CRL known", "CRL too
261 old", "Policy mismatch", "Not a secret key", "Key not trusted" or
262 "No specific reason given".
264 Due to limitations of GnuPG, it's impossible to encrypt to an untrusted key,
265 unless 'always trust' mode is enabled.
267 In the 'Error: public key' template there are a few additional variables available:
271 =item $Message - user friendly error message
273 =item $Reason - short reason as listed above
275 =item $Recipient - recipient's identification
277 =item $AddressObj - L<Email::Address> object containing recipient's email address
281 A message can have several invalid recipients, to avoid sending many emails
282 to the RT owner the system sends one message to the owner, grouped by
283 recipient. In the 'Error to RT owner: public key' template a C<@BadRecipients>
284 array is available where each element is a hash reference that describes one
285 recipient using the same fields as described above. So it's something like:
288 { Message => '...', Reason => '...', Recipient => '...', ...},
289 { Message => '...', Reason => '...', Recipient => '...', ...},
293 =head3 Private key doesn't exist
295 Template 'Error: no private key' is used to inform the user that
296 he sent an encrypted email, but we have no private key to decrypt
299 In this template C<$Message> object of L<MIME::Entity> class
300 available. It's the message RT received.
304 Template 'Error: bad GnuPG data' used to inform the user that a
305 message he sent has invalid data and can not be handled.
307 There are several reasons for this error, but most of them are data
308 corruption or absence of expected information.
310 In this template C<@Messages> array is available and contains list
313 =head1 FOR DEVELOPERS
315 =head2 Documentation and references
317 * RFC1847 - Security Multiparts for MIME: Multipart/Signed and Multipart/Encrypted.
318 Describes generic MIME security framework, "mulitpart/signed" and "multipart/encrypted"
321 * RFC3156 - MIME Security with Pretty Good Privacy (PGP),
326 # gnupg options supported by GnuPG::Interface
327 # other otions should be handled via extra_args argument
328 my %supported_opt = map { $_ => 1 } qw(
354 our $RE_FILE_EXTENSIONS = qr/pgp|asc/i;
356 # DEV WARNING: always pass all STD* handles to GnuPG interface even if we don't
357 # need them, just pass 'new IO::Handle' and then close it after safe_run_child.
358 # we don't want to leak anything into FCGI/Apache/MP handles, this break things.
359 # So code should look like:
360 # my $handles = GnuPG::Handles->new(
361 # stdin => ($handle{'stdin'} = new IO::Handle),
362 # stdout => ($handle{'stdout'} = new IO::Handle),
363 # stderr => ($handle{'stderr'} = new IO::Handle),
367 =head2 SignEncrypt Entity => MIME::Entity, [ Encrypt => 1, Sign => 1, ... ]
369 Signs and/or encrypts an email message with GnuPG utility.
375 During signing you can pass C<Signer> argument to set key we sign with this option
376 overrides gnupg's C<default-key> option. If C<Signer> argument is not provided
377 then address of a message sender is used.
379 As well you can pass C<Passphrase>, but if value is undefined then L</GetPassphrase>
384 During encryption you can pass a C<Recipients> array, otherwise C<To>, C<Cc> and
385 C<Bcc> fields of the message are used to fetch the list.
389 Returns a hash with the following keys:
402 my $entity = $args{'Entity'};
403 if ( $args{'Sign'} && !defined $args{'Signer'} ) {
404 $args{'Signer'} = UseKeyForSigning()
405 || (Email::Address->parse( $entity->head->get( 'From' ) ))[0]->address;
407 if ( $args{'Encrypt'} && !$args{'Recipients'} ) {
409 $args{'Recipients'} = [
410 grep $_ && !$seen{ $_ }++, map $_->address,
411 map Email::Address->parse( $entity->head->get( $_ ) ),
416 my $format = lc RT->Config->Get('GnuPG')->{'OutgoingMessagesFormat'} || 'RFC';
417 if ( $format eq 'inline' ) {
418 return SignEncryptInline( %args );
420 return SignEncryptRFC3156( %args );
424 sub SignEncryptRFC3156 {
438 my $gnupg = new GnuPG::Interface;
439 my %opt = RT->Config->Get('GnuPGOptions');
441 # handling passphrase in GnuPGOptions
442 $args{'Passphrase'} = delete $opt{'passphrase'}
443 if !defined $args{'Passphrase'};
445 $opt{'digest-algo'} ||= 'SHA1';
446 $opt{'default_key'} = $args{'Signer'}
447 if $args{'Sign'} && $args{'Signer'};
448 $gnupg->options->hash_init(
449 _PrepareGnuPGOptions( %opt ),
451 meta_interactive => 0,
454 my $entity = $args{'Entity'};
456 if ( $args{'Sign'} && !defined $args{'Passphrase'} ) {
457 $args{'Passphrase'} = GetPassphrase( Address => $args{'Signer'} );
461 if ( $args{'Sign'} && !$args{'Encrypt'} ) {
462 # required by RFC3156(Ch. 5) and RFC1847(Ch. 2.1)
463 foreach ( grep !$_->is_multipart, $entity->parts_DFS ) {
464 my $tenc = $_->head->mime_encoding;
465 unless ( $tenc =~ m/^(?:7bit|quoted-printable|base64)$/i ) {
466 $_->head->mime_attr( 'Content-Transfer-Encoding'
467 => $_->effective_type =~ m{^text/}? 'quoted-printable': 'base64'
472 my ($handles, $handle_list) = _make_gpg_handles(stdin =>IO::Handle::CRLF->new );
473 my %handle = %$handle_list;
475 $gnupg->passphrase( $args{'Passphrase'} );
478 local $SIG{'CHLD'} = 'DEFAULT';
479 my $pid = safe_run_child { $gnupg->detach_sign( handles => $handles ) };
480 $entity->make_multipart( 'mixed', Force => 1 );
482 local $SIG{'PIPE'} = 'IGNORE';
483 $entity->parts(0)->print( $handle{'stdin'} );
484 close $handle{'stdin'};
489 my @signature = readline $handle{'stdout'};
490 close $handle{'stdout'};
492 $res{'exit_code'} = $?;
493 foreach ( qw(stderr logger status) ) {
494 $res{$_} = do { local $/; readline $handle{$_} };
495 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
498 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
499 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
500 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
501 if ( $err || $res{'exit_code'} ) {
502 $res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
506 # setup RFC1847(Ch.2.1) requirements
507 my $protocol = 'application/pgp-signature';
508 $entity->head->mime_attr( 'Content-Type' => 'multipart/signed' );
509 $entity->head->mime_attr( 'Content-Type.protocol' => $protocol );
510 $entity->head->mime_attr( 'Content-Type.micalg' => 'pgp-'. lc $opt{'digest-algo'} );
513 Disposition => 'inline',
518 if ( $args{'Encrypt'} ) {
520 $gnupg->options->push_recipients( $_ ) foreach
521 map UseKeyForEncryption($_) || $_,
522 grep !$seen{ $_ }++, map $_->address,
523 map Email::Address->parse( $entity->head->get( $_ ) ),
526 my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
527 binmode $tmp_fh, ':raw';
529 my ($handles, $handle_list) = _make_gpg_handles(stdout => $tmp_fh);
530 my %handle = %$handle_list;
531 $handles->options( 'stdout' )->{'direct'} = 1;
532 $gnupg->passphrase( $args{'Passphrase'} ) if $args{'Sign'};
535 local $SIG{'CHLD'} = 'DEFAULT';
536 my $pid = safe_run_child { $args{'Sign'}
537 ? $gnupg->sign_and_encrypt( handles => $handles )
538 : $gnupg->encrypt( handles => $handles ) };
539 $entity->make_multipart( 'mixed', Force => 1 );
541 local $SIG{'PIPE'} = 'IGNORE';
542 $entity->parts(0)->print( $handle{'stdin'} );
543 close $handle{'stdin'};
548 $res{'exit_code'} = $?;
549 foreach ( qw(stderr logger status) ) {
550 $res{$_} = do { local $/; readline $handle{$_} };
551 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
554 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
555 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
556 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
558 $res{'message'} = $@? $@: "gpg exited with error code ". ($? >> 8);
562 my $protocol = 'application/pgp-encrypted';
564 $entity->head->mime_attr( 'Content-Type' => 'multipart/encrypted' );
565 $entity->head->mime_attr( 'Content-Type.protocol' => $protocol );
568 Disposition => 'inline',
569 Data => ['Version: 1',''],
573 Type => 'application/octet-stream',
574 Disposition => 'inline',
579 $entity->parts(-1)->bodyhandle->{'_dirty_hack_to_save_a_ref_tmp_fh'} = $tmp_fh;
584 sub SignEncryptInline {
587 my $entity = $args{'Entity'};
590 $entity->make_singlepart;
591 if ( $entity->is_multipart ) {
592 foreach ( $entity->parts ) {
593 %res = SignEncryptInline( @_, Entity => $_ );
594 return %res if $res{'exit_code'};
599 return _SignEncryptTextInline( @_ )
600 if $entity->effective_type =~ /^text\//i;
602 return _SignEncryptAttachmentInline( @_ );
605 sub _SignEncryptTextInline {
618 return unless $args{'Sign'} || $args{'Encrypt'};
620 my $gnupg = new GnuPG::Interface;
621 my %opt = RT->Config->Get('GnuPGOptions');
623 # handling passphrase in GnupGOptions
624 $args{'Passphrase'} = delete $opt{'passphrase'}
625 if !defined($args{'Passphrase'});
627 $opt{'digest-algo'} ||= 'SHA1';
628 $opt{'default_key'} = $args{'Signer'}
629 if $args{'Sign'} && $args{'Signer'};
630 $gnupg->options->hash_init(
631 _PrepareGnuPGOptions( %opt ),
633 meta_interactive => 0,
636 if ( $args{'Sign'} && !defined $args{'Passphrase'} ) {
637 $args{'Passphrase'} = GetPassphrase( Address => $args{'Signer'} );
640 if ( $args{'Encrypt'} ) {
641 $gnupg->options->push_recipients( $_ ) foreach
642 map UseKeyForEncryption($_) || $_,
643 @{ $args{'Recipients'} || [] };
648 my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
649 binmode $tmp_fh, ':raw';
651 my ($handles, $handle_list) = _make_gpg_handles(stdout => $tmp_fh);
652 my %handle = %$handle_list;
654 $handles->options( 'stdout' )->{'direct'} = 1;
655 $gnupg->passphrase( $args{'Passphrase'} ) if $args{'Sign'};
657 my $entity = $args{'Entity'};
659 local $SIG{'CHLD'} = 'DEFAULT';
660 my $method = $args{'Sign'} && $args{'Encrypt'}
662 : ($args{'Sign'}? 'clearsign': 'encrypt');
663 my $pid = safe_run_child { $gnupg->$method( handles => $handles ) };
665 local $SIG{'PIPE'} = 'IGNORE';
666 $entity->bodyhandle->print( $handle{'stdin'} );
667 close $handle{'stdin'};
671 $res{'exit_code'} = $?;
674 foreach ( qw(stderr logger status) ) {
675 $res{$_} = do { local $/; readline $handle{$_} };
676 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
679 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
680 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
681 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
682 if ( $err || $res{'exit_code'} ) {
683 $res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
687 $entity->bodyhandle( new MIME::Body::File $tmp_fn );
688 $entity->{'__store_tmp_handle_to_avoid_early_cleanup'} = $tmp_fh;
693 sub _SignEncryptAttachmentInline {
706 return unless $args{'Sign'} || $args{'Encrypt'};
708 my $gnupg = new GnuPG::Interface;
709 my %opt = RT->Config->Get('GnuPGOptions');
711 # handling passphrase in GnupGOptions
712 $args{'Passphrase'} = delete $opt{'passphrase'}
713 if !defined($args{'Passphrase'});
715 $opt{'digest-algo'} ||= 'SHA1';
716 $opt{'default_key'} = $args{'Signer'}
717 if $args{'Sign'} && $args{'Signer'};
718 $gnupg->options->hash_init(
719 _PrepareGnuPGOptions( %opt ),
721 meta_interactive => 0,
724 if ( $args{'Sign'} && !defined $args{'Passphrase'} ) {
725 $args{'Passphrase'} = GetPassphrase( Address => $args{'Signer'} );
728 my $entity = $args{'Entity'};
729 if ( $args{'Encrypt'} ) {
730 $gnupg->options->push_recipients( $_ ) foreach
731 map UseKeyForEncryption($_) || $_,
732 @{ $args{'Recipients'} || [] };
737 my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
738 binmode $tmp_fh, ':raw';
740 my ($handles, $handle_list) = _make_gpg_handles(stdout => $tmp_fh);
741 my %handle = %$handle_list;
742 $handles->options( 'stdout' )->{'direct'} = 1;
743 $gnupg->passphrase( $args{'Passphrase'} ) if $args{'Sign'};
746 local $SIG{'CHLD'} = 'DEFAULT';
747 my $method = $args{'Sign'} && $args{'Encrypt'}
749 : ($args{'Sign'}? 'detach_sign': 'encrypt');
750 my $pid = safe_run_child { $gnupg->$method( handles => $handles ) };
752 local $SIG{'PIPE'} = 'IGNORE';
753 $entity->bodyhandle->print( $handle{'stdin'} );
754 close $handle{'stdin'};
758 $res{'exit_code'} = $?;
761 foreach ( qw(stderr logger status) ) {
762 $res{$_} = do { local $/; readline $handle{$_} };
763 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
766 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
767 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
768 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
769 if ( $err || $res{'exit_code'} ) {
770 $res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
774 my $filename = $entity->head->recommended_filename || 'no_name';
775 if ( $args{'Sign'} && !$args{'Encrypt'} ) {
776 $entity->make_multipart;
778 Type => 'application/octet-stream',
780 Filename => "$filename.sig",
781 Disposition => 'attachment',
784 $entity->bodyhandle( new MIME::Body::File $tmp_fn );
785 $entity->effective_type('application/octet-stream');
786 $entity->head->mime_attr( $_ => "$filename.pgp" )
787 foreach (qw(Content-Type.name Content-Disposition.filename));
790 $entity->{'__store_tmp_handle_to_avoid_early_cleanup'} = $tmp_fh;
795 sub SignEncryptContent {
808 return unless $args{'Sign'} || $args{'Encrypt'};
810 my $gnupg = new GnuPG::Interface;
811 my %opt = RT->Config->Get('GnuPGOptions');
813 # handling passphrase in GnupGOptions
814 $args{'Passphrase'} = delete $opt{'passphrase'}
815 if !defined($args{'Passphrase'});
817 $opt{'digest-algo'} ||= 'SHA1';
818 $opt{'default_key'} = $args{'Signer'}
819 if $args{'Sign'} && $args{'Signer'};
820 $gnupg->options->hash_init(
821 _PrepareGnuPGOptions( %opt ),
823 meta_interactive => 0,
826 if ( $args{'Sign'} && !defined $args{'Passphrase'} ) {
827 $args{'Passphrase'} = GetPassphrase( Address => $args{'Signer'} );
830 if ( $args{'Encrypt'} ) {
831 $gnupg->options->push_recipients( $_ ) foreach
832 map UseKeyForEncryption($_) || $_,
833 @{ $args{'Recipients'} || [] };
838 my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
839 binmode $tmp_fh, ':raw';
841 my ($handles, $handle_list) = _make_gpg_handles(stdout => $tmp_fh);
842 my %handle = %$handle_list;
843 $handles->options( 'stdout' )->{'direct'} = 1;
844 $gnupg->passphrase( $args{'Passphrase'} ) if $args{'Sign'};
847 local $SIG{'CHLD'} = 'DEFAULT';
848 my $method = $args{'Sign'} && $args{'Encrypt'}
850 : ($args{'Sign'}? 'clearsign': 'encrypt');
851 my $pid = safe_run_child { $gnupg->$method( handles => $handles ) };
853 local $SIG{'PIPE'} = 'IGNORE';
854 $handle{'stdin'}->print( ${ $args{'Content'} } );
855 close $handle{'stdin'};
859 $res{'exit_code'} = $?;
862 foreach ( qw(stderr logger status) ) {
863 $res{$_} = do { local $/; readline $handle{$_} };
864 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
867 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
868 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
869 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
870 if ( $err || $res{'exit_code'} ) {
871 $res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
875 ${ $args{'Content'} } = '';
878 my $status = read $tmp_fh, my $buf, 4*1024;
879 unless ( defined $status ) {
880 $RT::Logger->crit( "couldn't read message: $!" );
881 } elsif ( !$status ) {
884 ${ $args{'Content'} } .= $buf;
890 sub FindProtectedParts {
891 my %args = ( Entity => undef, CheckBody => 1, @_ );
892 my $entity = $args{'Entity'};
894 # inline PGP block, only in singlepart
895 unless ( $entity->is_multipart ) {
896 my $file = ($entity->head->recommended_filename||'') =~ /\.${RE_FILE_EXTENSIONS}$/;
898 my $io = $entity->open('r');
900 $RT::Logger->warning( "Entity of type ". $entity->effective_type ." has no body" );
903 while ( defined($_ = $io->getline) ) {
904 next unless /^-----BEGIN PGP (SIGNED )?MESSAGE-----/;
905 my $type = $1? 'signed': 'encrypted';
906 $RT::Logger->debug("Found $type inline part");
909 Format => !$file || $type eq 'signed'? 'Inline' : 'Attachment',
917 # RFC3156, multipart/{signed,encrypted}
918 if ( ( my $type = $entity->effective_type ) =~ /^multipart\/(?:encrypted|signed)$/ ) {
919 unless ( $entity->parts == 2 ) {
920 $RT::Logger->error( "Encrypted or signed entity must has two subparts. Skipped" );
924 my $protocol = $entity->head->mime_attr( 'Content-Type.protocol' );
925 unless ( $protocol ) {
926 $RT::Logger->error( "Entity is '$type', but has no protocol defined. Skipped" );
930 if ( $type eq 'multipart/encrypted' ) {
931 unless ( $protocol eq 'application/pgp-encrypted' ) {
932 $RT::Logger->info( "Skipping protocol '$protocol', only 'application/pgp-encrypted' is supported" );
935 $RT::Logger->debug("Found encrypted according to RFC3156 part");
940 Data => $entity->parts(1),
941 Info => $entity->parts(0),
944 unless ( $protocol eq 'application/pgp-signature' ) {
945 $RT::Logger->info( "Skipping protocol '$protocol', only 'application/pgp-signature' is supported" );
948 $RT::Logger->debug("Found signed according to RFC3156 part");
953 Data => $entity->parts(0),
954 Signature => $entity->parts(1),
959 # attachments signed with signature in another part
961 foreach my $i ( 0 .. $entity->parts - 1 ) {
962 my $part = $entity->parts($i);
964 # we can not associate a signature within an attachment
966 my $fname = $part->head->recommended_filename;
969 if ( $part->effective_type eq 'application/pgp-signature' ) {
970 push @file_indices, $i;
972 elsif ( $fname =~ /\.sig$/i && $part->effective_type eq 'application/octet-stream' ) {
973 push @file_indices, $i;
978 foreach my $i ( @file_indices ) {
979 my $sig_part = $entity->parts($i);
980 $skip{"$sig_part"}++;
981 my $sig_name = $sig_part->head->recommended_filename;
982 my ($file_name) = $sig_name =~ /^(.*?)(?:\.sig)?$/;
984 my ($data_part_idx) =
985 grep $file_name eq ($entity->parts($_)->head->recommended_filename||''),
986 grep $sig_part ne $entity->parts($_),
987 0 .. $entity->parts - 1;
988 unless ( defined $data_part_idx ) {
989 $RT::Logger->error("Found $sig_name attachment, but didn't find $file_name");
992 my $data_part_in = $entity->parts($data_part_idx);
994 $skip{"$data_part_in"}++;
995 $RT::Logger->debug("Found signature (in '$sig_name') of attachment '$file_name'");
998 Format => 'Attachment',
1000 Data => $data_part_in,
1001 Signature => $sig_part,
1005 # attachments with inline encryption
1006 my @encrypted_indices =
1007 grep {($entity->parts($_)->head->recommended_filename || '') =~ /\.${RE_FILE_EXTENSIONS}$/}
1008 0 .. $entity->parts - 1;
1010 foreach my $i ( @encrypted_indices ) {
1011 my $part = $entity->parts($i);
1013 $RT::Logger->debug("Found encrypted attachment '". $part->head->recommended_filename ."'");
1015 Type => 'encrypted',
1016 Format => 'Attachment',
1022 push @res, FindProtectedParts( Entity => $_ )
1023 foreach grep !$skip{"$_"}, $entity->parts;
1028 =head2 VerifyDecrypt Entity => undef, [ Detach => 1, Passphrase => undef, SetStatus => 1 ]
1040 my @protected = FindProtectedParts( Entity => $args{'Entity'} );
1042 # XXX: detaching may brake nested signatures
1043 foreach my $item( grep $_->{'Type'} eq 'signed', @protected ) {
1045 if ( $item->{'Format'} eq 'RFC3156' ) {
1046 push @res, { VerifyRFC3156( %$item, SetStatus => $args{'SetStatus'} ) };
1047 if ( $args{'Detach'} ) {
1048 $item->{'Top'}->parts( [ $item->{'Data'} ] );
1049 $item->{'Top'}->make_singlepart;
1051 $status_on = $item->{'Top'};
1052 } elsif ( $item->{'Format'} eq 'Inline' ) {
1053 push @res, { VerifyInline( %$item ) };
1054 $status_on = $item->{'Data'};
1055 } elsif ( $item->{'Format'} eq 'Attachment' ) {
1056 push @res, { VerifyAttachment( %$item ) };
1057 if ( $args{'Detach'} ) {
1058 $item->{'Top'}->parts( [
1059 grep "$_" ne $item->{'Signature'}, $item->{'Top'}->parts
1061 $item->{'Top'}->make_singlepart;
1063 $status_on = $item->{'Data'};
1065 if ( $args{'SetStatus'} || $args{'AddStatus'} ) {
1066 my $method = $args{'AddStatus'} ? 'add' : 'set';
1067 $status_on->head->$method(
1068 'X-RT-GnuPG-Status' => $res[-1]->{'status'}
1072 foreach my $item( grep $_->{'Type'} eq 'encrypted', @protected ) {
1074 if ( $item->{'Format'} eq 'RFC3156' ) {
1075 push @res, { DecryptRFC3156( %$item ) };
1076 $status_on = $item->{'Top'};
1077 } elsif ( $item->{'Format'} eq 'Inline' ) {
1078 push @res, { DecryptInline( %$item ) };
1079 $status_on = $item->{'Data'};
1080 } elsif ( $item->{'Format'} eq 'Attachment' ) {
1081 push @res, { DecryptAttachment( %$item ) };
1082 $status_on = $item->{'Data'};
1084 if ( $args{'SetStatus'} || $args{'AddStatus'} ) {
1085 my $method = $args{'AddStatus'} ? 'add' : 'set';
1086 $status_on->head->$method(
1087 'X-RT-GnuPG-Status' => $res[-1]->{'status'}
1094 sub VerifyInline { return DecryptInline( @_ ) }
1096 sub VerifyAttachment {
1097 my %args = ( Data => undef, Signature => undef, Top => undef, @_ );
1099 my $gnupg = new GnuPG::Interface;
1100 my %opt = RT->Config->Get('GnuPGOptions');
1101 $opt{'digest-algo'} ||= 'SHA1';
1102 $gnupg->options->hash_init(
1103 _PrepareGnuPGOptions( %opt ),
1104 meta_interactive => 0,
1107 foreach ( $args{'Data'}, $args{'Signature'} ) {
1108 next unless $_->bodyhandle->is_encoded;
1110 require RT::EmailParser;
1111 RT::EmailParser->_DecodeBody($_);
1114 my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
1115 binmode $tmp_fh, ':raw';
1116 $args{'Data'}->bodyhandle->print( $tmp_fh );
1119 my ($handles, $handle_list) = _make_gpg_handles();
1120 my %handle = %$handle_list;
1124 local $SIG{'CHLD'} = 'DEFAULT';
1125 my $pid = safe_run_child { $gnupg->verify(
1126 handles => $handles, command_args => [ '-', $tmp_fn ]
1129 local $SIG{'PIPE'} = 'IGNORE';
1130 $args{'Signature'}->bodyhandle->print( $handle{'stdin'} );
1131 close $handle{'stdin'};
1135 $res{'exit_code'} = $?;
1136 foreach ( qw(stderr logger status) ) {
1137 $res{$_} = do { local $/; readline $handle{$_} };
1138 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
1141 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
1142 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
1143 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
1145 $res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
1151 my %args = ( Data => undef, Signature => undef, Top => undef, @_ );
1153 my $gnupg = new GnuPG::Interface;
1154 my %opt = RT->Config->Get('GnuPGOptions');
1155 $opt{'digest-algo'} ||= 'SHA1';
1156 $gnupg->options->hash_init(
1157 _PrepareGnuPGOptions( %opt ),
1158 meta_interactive => 0,
1161 my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
1162 binmode $tmp_fh, ':raw:eol(CRLF?)';
1163 $args{'Data'}->print( $tmp_fh );
1166 my ($handles, $handle_list) = _make_gpg_handles();
1167 my %handle = %$handle_list;
1171 local $SIG{'CHLD'} = 'DEFAULT';
1172 my $pid = safe_run_child { $gnupg->verify(
1173 handles => $handles, command_args => [ '-', $tmp_fn ]
1176 local $SIG{'PIPE'} = 'IGNORE';
1177 $args{'Signature'}->bodyhandle->print( $handle{'stdin'} );
1178 close $handle{'stdin'};
1182 $res{'exit_code'} = $?;
1183 foreach ( qw(stderr logger status) ) {
1184 $res{$_} = do { local $/; readline $handle{$_} };
1185 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
1188 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
1189 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
1190 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
1192 $res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
1197 sub DecryptRFC3156 {
1202 Passphrase => undef,
1206 my $gnupg = new GnuPG::Interface;
1207 my %opt = RT->Config->Get('GnuPGOptions');
1209 # handling passphrase in GnupGOptions
1210 $args{'Passphrase'} = delete $opt{'passphrase'}
1211 if !defined($args{'Passphrase'});
1213 $opt{'digest-algo'} ||= 'SHA1';
1214 $gnupg->options->hash_init(
1215 _PrepareGnuPGOptions( %opt ),
1216 meta_interactive => 0,
1219 if ( $args{'Data'}->bodyhandle->is_encoded ) {
1220 require RT::EmailParser;
1221 RT::EmailParser->_DecodeBody($args{'Data'});
1224 $args{'Passphrase'} = GetPassphrase()
1225 unless defined $args{'Passphrase'};
1227 my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
1228 binmode $tmp_fh, ':raw';
1230 my ($handles, $handle_list) = _make_gpg_handles(stdout => $tmp_fh);
1231 my %handle = %$handle_list;
1232 $handles->options( 'stdout' )->{'direct'} = 1;
1236 local $SIG{'CHLD'} = 'DEFAULT';
1237 $gnupg->passphrase( $args{'Passphrase'} );
1238 my $pid = safe_run_child { $gnupg->decrypt( handles => $handles ) };
1240 local $SIG{'PIPE'} = 'IGNORE';
1241 $args{'Data'}->bodyhandle->print( $handle{'stdin'} );
1242 close $handle{'stdin'}
1247 $res{'exit_code'} = $?;
1248 foreach ( qw(stderr logger status) ) {
1249 $res{$_} = do { local $/; readline $handle{$_} };
1250 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
1253 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
1254 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
1255 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
1257 # if the decryption is fine but the signature is bad, then without this
1258 # status check we lose the decrypted text
1259 # XXX: add argument to the function to control this check
1260 if ( $res{'status'} !~ /DECRYPTION_OKAY/ ) {
1262 $res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
1268 my $parser = new RT::EmailParser;
1269 my $decrypted = $parser->ParseMIMEEntityFromFileHandle( $tmp_fh, 0 );
1270 $decrypted->{'__store_link_to_object_to_avoid_early_cleanup'} = $parser;
1271 $args{'Top'}->parts( [] );
1272 $args{'Top'}->add_part( $decrypted );
1273 $args{'Top'}->make_singlepart;
1280 Passphrase => undef,
1284 my $gnupg = new GnuPG::Interface;
1285 my %opt = RT->Config->Get('GnuPGOptions');
1287 # handling passphrase in GnuPGOptions
1288 $args{'Passphrase'} = delete $opt{'passphrase'}
1289 if !defined($args{'Passphrase'});
1291 $opt{'digest-algo'} ||= 'SHA1';
1292 $gnupg->options->hash_init(
1293 _PrepareGnuPGOptions( %opt ),
1294 meta_interactive => 0,
1297 if ( $args{'Data'}->bodyhandle->is_encoded ) {
1298 require RT::EmailParser;
1299 RT::EmailParser->_DecodeBody($args{'Data'});
1302 $args{'Passphrase'} = GetPassphrase()
1303 unless defined $args{'Passphrase'};
1305 my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
1306 binmode $tmp_fh, ':raw';
1308 my $io = $args{'Data'}->open('r');
1310 die "Entity has no body, never should happen";
1315 my ($had_literal, $in_block) = ('', 0);
1316 my ($block_fh, $block_fn) = File::Temp::tempfile( UNLINK => 1 );
1317 binmode $block_fh, ':raw';
1319 while ( defined(my $str = $io->getline) ) {
1320 if ( $in_block && $str =~ /^-----END PGP (?:MESSAGE|SIGNATURE)-----/ ) {
1321 print $block_fh $str;
1323 next if $in_block > 0;
1325 seek $block_fh, 0, 0;
1327 my ($res_fh, $res_fn);
1328 ($res_fh, $res_fn, %res) = _DecryptInlineBlock(
1331 BlockHandle => $block_fh,
1333 return %res unless $res_fh;
1335 print $tmp_fh "-----BEGIN OF PGP PROTECTED PART-----\n" if $had_literal;
1336 while (my $buf = <$res_fh> ) {
1339 print $tmp_fh "-----END OF PART-----\n" if $had_literal;
1341 ($block_fh, $block_fn) = File::Temp::tempfile( UNLINK => 1 );
1342 binmode $block_fh, ':raw';
1345 elsif ( $str =~ /^-----BEGIN PGP (SIGNED )?MESSAGE-----/ ) {
1347 print $block_fh $str;
1349 elsif ( $in_block ) {
1350 print $block_fh $str;
1354 $had_literal = 1 if /\S/s;
1360 # we're still in a block, this not bad not good. let's try to
1361 # decrypt what we have, it can be just missing -----END PGP...
1362 seek $block_fh, 0, 0;
1364 my ($res_fh, $res_fn);
1365 ($res_fh, $res_fn, %res) = _DecryptInlineBlock(
1368 BlockHandle => $block_fh,
1370 return %res unless $res_fh;
1372 print $tmp_fh "-----BEGIN OF PGP PROTECTED PART-----\n" if $had_literal;
1373 while (my $buf = <$res_fh> ) {
1376 print $tmp_fh "-----END OF PART-----\n" if $had_literal;
1380 $args{'Data'}->bodyhandle( new MIME::Body::File $tmp_fn );
1381 $args{'Data'}->{'__store_tmp_handle_to_avoid_early_cleanup'} = $tmp_fh;
1385 sub _DecryptInlineBlock {
1388 BlockHandle => undef,
1389 Passphrase => undef,
1392 my $gnupg = $args{'GnuPG'};
1394 my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
1395 binmode $tmp_fh, ':raw';
1397 my ($handles, $handle_list) = _make_gpg_handles(
1398 stdin => $args{'BlockHandle'},
1400 my %handle = %$handle_list;
1401 $handles->options( 'stdout' )->{'direct'} = 1;
1402 $handles->options( 'stdin' )->{'direct'} = 1;
1406 local $SIG{'CHLD'} = 'DEFAULT';
1407 $gnupg->passphrase( $args{'Passphrase'} );
1408 my $pid = safe_run_child { $gnupg->decrypt( handles => $handles ) };
1411 $res{'exit_code'} = $?;
1412 foreach ( qw(stderr logger status) ) {
1413 $res{$_} = do { local $/; readline $handle{$_} };
1414 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
1417 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
1418 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
1419 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
1421 # if the decryption is fine but the signature is bad, then without this
1422 # status check we lose the decrypted text
1423 # XXX: add argument to the function to control this check
1424 if ( $res{'status'} !~ /DECRYPTION_OKAY/ ) {
1426 $res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
1427 return (undef, undef, %res);
1432 return ($tmp_fh, $tmp_fn, %res);
1435 sub DecryptAttachment {
1439 Passphrase => undef,
1443 my $gnupg = new GnuPG::Interface;
1444 my %opt = RT->Config->Get('GnuPGOptions');
1446 # handling passphrase in GnuPGOptions
1447 $args{'Passphrase'} = delete $opt{'passphrase'}
1448 if !defined($args{'Passphrase'});
1450 $opt{'digest-algo'} ||= 'SHA1';
1451 $gnupg->options->hash_init(
1452 _PrepareGnuPGOptions( %opt ),
1453 meta_interactive => 0,
1456 if ( $args{'Data'}->bodyhandle->is_encoded ) {
1457 require RT::EmailParser;
1458 RT::EmailParser->_DecodeBody($args{'Data'});
1461 $args{'Passphrase'} = GetPassphrase()
1462 unless defined $args{'Passphrase'};
1464 my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
1465 binmode $tmp_fh, ':raw';
1466 $args{'Data'}->bodyhandle->print( $tmp_fh );
1469 my ($res_fh, $res_fn, %res) = _DecryptInlineBlock(
1472 BlockHandle => $tmp_fh,
1474 return %res unless $res_fh;
1476 $args{'Data'}->bodyhandle( new MIME::Body::File $res_fn );
1477 $args{'Data'}->{'__store_tmp_handle_to_avoid_early_cleanup'} = $res_fh;
1479 my $head = $args{'Data'}->head;
1481 # we can not trust original content type
1482 # TODO: and don't have way to detect, so we just use octet-stream
1483 # some clients may send .asc files (encryped) as text/plain
1484 $head->mime_attr( "Content-Type" => 'application/octet-stream' );
1486 my $filename = $head->recommended_filename;
1487 $filename =~ s/\.${RE_FILE_EXTENSIONS}$//i;
1488 $head->mime_attr( $_ => $filename )
1489 foreach (qw(Content-Type.name Content-Disposition.filename));
1494 sub DecryptContent {
1497 Passphrase => undef,
1501 my $gnupg = new GnuPG::Interface;
1502 my %opt = RT->Config->Get('GnuPGOptions');
1504 # handling passphrase in GnupGOptions
1505 $args{'Passphrase'} = delete $opt{'passphrase'}
1506 if !defined($args{'Passphrase'});
1508 $opt{'digest-algo'} ||= 'SHA1';
1509 $gnupg->options->hash_init(
1510 _PrepareGnuPGOptions( %opt ),
1511 meta_interactive => 0,
1514 $args{'Passphrase'} = GetPassphrase()
1515 unless defined $args{'Passphrase'};
1517 my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( UNLINK => 1 );
1518 binmode $tmp_fh, ':raw';
1520 my ($handles, $handle_list) = _make_gpg_handles(
1522 my %handle = %$handle_list;
1523 $handles->options( 'stdout' )->{'direct'} = 1;
1527 local $SIG{'CHLD'} = 'DEFAULT';
1528 $gnupg->passphrase( $args{'Passphrase'} );
1529 my $pid = safe_run_child { $gnupg->decrypt( handles => $handles ) };
1531 local $SIG{'PIPE'} = 'IGNORE';
1532 print { $handle{'stdin'} } ${ $args{'Content'} };
1533 close $handle{'stdin'};
1538 $res{'exit_code'} = $?;
1539 foreach ( qw(stderr logger status) ) {
1540 $res{$_} = do { local $/; readline $handle{$_} };
1541 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
1544 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
1545 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
1546 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
1548 # if the decryption is fine but the signature is bad, then without this
1549 # status check we lose the decrypted text
1550 # XXX: add argument to the function to control this check
1551 if ( $res{'status'} !~ /DECRYPTION_OKAY/ ) {
1553 $res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
1558 ${ $args{'Content'} } = '';
1561 my $status = read $tmp_fh, my $buf, 4*1024;
1562 unless ( defined $status ) {
1563 $RT::Logger->crit( "couldn't read message: $!" );
1564 } elsif ( !$status ) {
1567 ${ $args{'Content'} } .= $buf;
1573 =head2 GetPassphrase [ Address => undef ]
1575 Returns passphrase, called whenever it's required with Address as a named argument.
1580 my %args = ( Address => undef, @_ );
1586 Takes a string containing output of gnupg status stream. Parses it and returns
1587 array of hashes. Each element of array is a hash ref and represents line or
1588 group of lines in the status message.
1590 All hashes have Operation, Status and Message elements.
1596 Classification of operations gnupg performs. Now we have support
1597 for Sign, Encrypt, Decrypt, Verify, PassphraseCheck, RecipientsCheck and Data
1602 Informs about success. Value is 'DONE' on success, other values means that
1603 an operation failed, for example 'ERROR', 'BAD', 'MISSING' and may be other.
1607 User friendly message.
1611 This parser is based on information from GnuPG distribution, see also
1612 F<docs/design_docs/gnupg_details_on_output_formats> in the RT distribution.
1616 my %REASON_CODE_TO_TEXT = (
1618 1 => "No armored data",
1619 2 => "Expected a packet, but did not found one",
1620 3 => "Invalid packet found",
1621 4 => "Signature expected, but not found",
1624 0 => "No specific reason given",
1626 2 => "Ambigious specification",
1627 3 => "Wrong key usage",
1630 6 => "No CRL known",
1632 8 => "Policy mismatch",
1633 9 => "Not a secret key",
1634 10 => "Key not trusted",
1637 0 => 'not specified',
1638 4 => 'unknown algorithm',
1639 9 => 'missing public key',
1643 sub ReasonCodeToText {
1644 my $keyword = shift;
1646 return $REASON_CODE_TO_TEXT{ $keyword }{ $code }
1647 if exists $REASON_CODE_TO_TEXT{ $keyword }{ $code };
1651 my %simple_keyword = (
1653 Operation => 'RecipientsCheck',
1655 Message => 'No recipients',
1658 Operation => 'Data',
1660 Message => 'Unexpected data has been encountered',
1663 Operation => 'Data',
1665 Message => 'The ASCII armor is corrupted',
1670 my %parse_keyword = map { $_ => 1 } qw(
1672 SIG_CREATED GOODSIG BADSIG ERRSIG
1674 DECRYPTION_FAILED DECRYPTION_OKAY
1675 BAD_PASSPHRASE GOOD_PASSPHRASE
1677 NO_RECP INV_RECP NODATA UNEXPECTED
1680 # keywords we ignore without any messages as we parse them using other
1681 # keywords as starting point or just ignore as they are useless for us
1682 my %ignore_keyword = map { $_ => 1 } qw(
1683 NEED_PASSPHRASE MISSING_PASSPHRASE BEGIN_SIGNING PLAINTEXT PLAINTEXT_LENGTH
1684 BEGIN_ENCRYPTION SIG_ID VALIDSIG
1685 ENC_TO BEGIN_DECRYPTION END_DECRYPTION GOODMDC
1686 TRUST_UNDEFINED TRUST_NEVER TRUST_MARGINAL TRUST_FULLY TRUST_ULTIMATE
1691 return () unless $status;
1694 while ( $status =~ /\[GNUPG:\]\s*(.*?)(?=\[GNUPG:\]|\z)/igms ) {
1695 push @status, $1; $status[-1] =~ s/\s+/ /g; $status[-1] =~ s/\s+$//;
1697 $status = join "\n", @status;
1701 my (%user_hint, $latest_user_main_key);
1702 for ( my $i = 0; $i < @status; $i++ ) {
1703 my $line = $status[$i];
1704 my ($keyword, $args) = ($line =~ /^(\S+)\s*(.*)$/s);
1705 if ( $simple_keyword{ $keyword } ) {
1706 push @res, $simple_keyword{ $keyword };
1707 $res[-1]->{'Keyword'} = $keyword;
1710 unless ( $parse_keyword{ $keyword } ) {
1711 $RT::Logger->warning("Skipped $keyword") unless $ignore_keyword{ $keyword };
1715 if ( $keyword eq 'USERID_HINT' ) {
1716 my %tmp = _ParseUserHint($status, $line);
1717 $latest_user_main_key = $tmp{'MainKey'};
1718 if ( $user_hint{ $tmp{'MainKey'} } ) {
1719 while ( my ($k, $v) = each %tmp ) {
1720 $user_hint{ $tmp{'MainKey'} }->{$k} = $v;
1723 $user_hint{ $tmp{'MainKey'} } = \%tmp;
1727 elsif ( $keyword eq 'BAD_PASSPHRASE' || $keyword eq 'GOOD_PASSPHRASE' ) {
1730 Operation => 'PassphraseCheck',
1731 Status => $keyword eq 'BAD_PASSPHRASE'? 'BAD' : 'DONE',
1734 $res{'Status'} = 'MISSING' if $status[ $i - 1 ] =~ /^MISSING_PASSPHRASE/;
1735 foreach my $line ( reverse @status[ 0 .. $i-1 ] ) {
1736 next unless $line =~ /^NEED_PASSPHRASE\s+(\S+)\s+(\S+)\s+(\S+)/;
1737 next if $key_id && $2 ne $key_id;
1738 @res{'MainKey', 'Key', 'KeyType'} = ($1, $2, $3);
1741 $res{'Message'} = ucfirst( lc( $res{'Status'} eq 'DONE'? 'GOOD': $res{'Status'} ) ) .' passphrase';
1742 $res{'User'} = ( $user_hint{ $res{'MainKey'} } ||= {} ) if $res{'MainKey'};
1743 if ( exists $res{'User'}->{'EmailAddress'} ) {
1744 $res{'Message'} .= ' for '. $res{'User'}->{'EmailAddress'};
1746 $res{'Message'} .= " for '0x$key_id'";
1750 elsif ( $keyword eq 'END_ENCRYPTION' ) {
1752 Operation => 'Encrypt',
1754 Message => 'Data has been encrypted',
1756 foreach my $line ( reverse @status[ 0 .. $i-1 ] ) {
1757 next unless $line =~ /^BEGIN_ENCRYPTION\s+(\S+)\s+(\S+)/;
1758 @res{'MdcMethod', 'SymAlgo'} = ($1, $2);
1763 elsif ( $keyword eq 'DECRYPTION_FAILED' || $keyword eq 'DECRYPTION_OKAY' ) {
1764 my %res = ( Operation => 'Decrypt' );
1765 @res{'Status', 'Message'} =
1766 $keyword eq 'DECRYPTION_FAILED'
1767 ? ('ERROR', 'Decryption failed')
1768 : ('DONE', 'Decryption process succeeded');
1770 foreach my $line ( reverse @status[ 0 .. $i-1 ] ) {
1771 next unless $line =~ /^ENC_TO\s+(\S+)\s+(\S+)\s+(\S+)/;
1772 my ($key, $alg, $key_length) = ($1, $2, $3);
1774 my %encrypted_to = (
1775 Message => "The message is encrypted to '0x$key'",
1776 User => ( $user_hint{ $key } ||= {} ),
1778 KeyLength => $key_length,
1782 push @{ $res{'EncryptedTo'} ||= [] }, \%encrypted_to;
1787 elsif ( $keyword eq 'NO_SECKEY' || $keyword eq 'NO_PUBKEY' ) {
1788 my ($key) = split /\s+/, $args;
1789 my $type = $keyword eq 'NO_SECKEY'? 'secret': 'public';
1791 Operation => 'KeyCheck',
1792 Status => 'MISSING',
1793 Message => ucfirst( $type ) ." key '0x$key' is not available",
1797 $res{'User'} = ( $user_hint{ $key } ||= {} );
1798 $res{'User'}{ ucfirst( $type ). 'KeyMissing' } = 1;
1801 # GOODSIG, BADSIG, VALIDSIG, TRUST_*
1802 elsif ( $keyword eq 'GOODSIG' ) {
1804 Operation => 'Verify',
1806 Message => 'The signature is good',
1808 @res{qw(Key UserString)} = split /\s+/, $args, 2;
1809 $res{'Message'} .= ', signed by '. $res{'UserString'};
1811 foreach my $line ( @status[ $i .. $#status ] ) {
1812 next unless $line =~ /^TRUST_(\S+)/;
1816 $res{'Message'} .= ', trust level is '. lc( $res{'Trust'} || 'unknown');
1818 foreach my $line ( @status[ $i .. $#status ] ) {
1819 next unless $line =~ /^VALIDSIG\s+(.*)/;
1832 ) } = split /\s+/, $1, 10;
1837 elsif ( $keyword eq 'BADSIG' ) {
1839 Operation => 'Verify',
1841 Message => 'The signature has not been verified okay',
1843 @res{qw(Key UserString)} = split /\s+/, $args, 2;
1846 elsif ( $keyword eq 'ERRSIG' ) {
1848 Operation => 'Verify',
1850 Message => 'Not possible to check the signature',
1852 @res{qw(Key PubkeyAlgo HashAlgo Class Timestamp ReasonCode Other)}
1853 = split /\s+/, $args, 7;
1855 $res{'Reason'} = ReasonCodeToText( $keyword, $res{'ReasonCode'} );
1856 $res{'Message'} .= ", the reason is ". $res{'Reason'};
1860 elsif ( $keyword eq 'SIG_CREATED' ) {
1861 # SIG_CREATED <type> <pubkey algo> <hash algo> <class> <timestamp> <key fpr>
1862 my @props = split /\s+/, $args;
1864 Operation => 'Sign',
1866 Message => "Signed message",
1868 PubKeyAlgo => $props[1],
1869 HashKeyAlgo => $props[2],
1871 Timestamp => $props[4],
1872 KeyFingerprint => $props[5],
1873 User => $user_hint{ $latest_user_main_key },
1875 $res[-1]->{Message} .= ' by '. $user_hint{ $latest_user_main_key }->{'EmailAddress'}
1876 if $user_hint{ $latest_user_main_key };
1878 elsif ( $keyword eq 'INV_RECP' ) {
1879 my ($rcode, $recipient) = split /\s+/, $args, 2;
1880 my $reason = ReasonCodeToText( $keyword, $rcode );
1882 Operation => 'RecipientsCheck',
1884 Message => "Recipient '$recipient' is unusable, the reason is '$reason'",
1885 Recipient => $recipient,
1886 ReasonCode => $rcode,
1890 elsif ( $keyword eq 'NODATA' ) {
1891 my $rcode = (split /\s+/, $args)[0];
1892 my $reason = ReasonCodeToText( $keyword, $rcode );
1894 Operation => 'Data',
1896 Message => "No data has been found. The reason is '$reason'",
1897 ReasonCode => $rcode,
1902 $RT::Logger->warning("Keyword $keyword is unknown");
1905 $res[-1]{'Keyword'} = $keyword if @res && !$res[-1]{'Keyword'};
1910 sub _ParseUserHint {
1911 my ($status, $hint) = (@_);
1912 my ($main_key_id, $user_str) = ($hint =~ /^USERID_HINT\s+(\S+)\s+(.*)$/);
1913 return () unless $main_key_id;
1915 MainKey => $main_key_id,
1916 String => $user_str,
1917 EmailAddress => (map $_->address, Email::Address->parse( $user_str ))[0],
1921 sub _PrepareGnuPGOptions {
1923 my %res = map { lc $_ => $opt{ $_ } } grep $supported_opt{ lc $_ }, keys %opt;
1924 $res{'extra_args'} ||= [];
1925 foreach my $o ( grep !$supported_opt{ lc $_ }, keys %opt ) {
1926 push @{ $res{'extra_args'} }, '--'. lc $o;
1927 push @{ $res{'extra_args'} }, $opt{ $o }
1928 if defined $opt{ $o };
1935 # one arg -> return preferred key
1937 sub UseKeyForEncryption {
1940 } elsif ( @_ > 1 ) {
1942 $key{ lc($_) } = delete $key{ $_ } foreach grep lc ne $_, keys %key;
1944 return $key{ $_[0] };
1949 =head2 UseKeyForSigning
1951 Returns or sets identifier of the key that should be used for signing.
1953 Returns the current value when called without arguments.
1955 Sets new value when called with one argument and unsets if it's undef.
1960 sub UseKeyForSigning {
1967 =head2 GetKeysForEncryption
1969 Takes identifier and returns keys suitable for encryption.
1971 B<Note> that keys for which trust level is not set are
1976 sub GetKeysForEncryption {
1978 my %res = GetKeysInfo( $key_id, 'public', @_ );
1979 return %res if $res{'exit_code'};
1980 return %res unless $res{'info'};
1982 foreach my $key ( splice @{ $res{'info'} } ) {
1983 # skip disabled keys
1984 next if $key->{'Capabilities'} =~ /D/;
1985 # skip keys not suitable for encryption
1986 next unless $key->{'Capabilities'} =~ /e/i;
1987 # skip disabled, expired, revoke and keys with no trust,
1988 # but leave keys with unknown trust level
1989 next if $key->{'TrustLevel'} < 0;
1991 push @{ $res{'info'} }, $key;
1993 delete $res{'info'} unless @{ $res{'info'} };
1997 sub GetKeysForSigning {
1999 return GetKeysInfo( $key_id, 'private', @_ );
2002 sub CheckRecipients {
2003 my @recipients = (@_);
2005 my ($status, @issues) = (1, ());
2008 foreach my $address ( grep !$seen{ lc $_ }++, map $_->address, @recipients ) {
2009 my %res = GetKeysForEncryption( $address );
2010 if ( $res{'info'} && @{ $res{'info'} } == 1 && $res{'info'}[0]{'TrustLevel'} > 0 ) {
2011 # good, one suitable and trusted key
2014 my $user = RT::User->new( $RT::SystemUser );
2015 $user->LoadByEmail( $address );
2016 # it's possible that we have no User record with the email
2017 $user = undef unless $user->id;
2019 if ( my $fpr = UseKeyForEncryption( $address ) ) {
2020 if ( $res{'info'} && @{ $res{'info'} } ) {
2022 grep lc $_->{'Fingerprint'} eq lc $fpr,
2023 grep $_->{'TrustLevel'} > 0,
2029 EmailAddress => $address,
2030 $user? (User => $user) : (),
2033 $issue{'Message'} = "Selected key either is not trusted or doesn't exist anymore."; #loc
2034 push @issues, \%issue;
2039 $prefered_key = $user->PreferredKey if $user;
2040 #XXX: prefered key is not yet implemented...
2045 EmailAddress => $address,
2046 $user? (User => $user) : (),
2050 unless ( $res{'info'} && @{ $res{'info'} } ) {
2052 $issue{'Message'} = "There is no key suitable for encryption."; #loc
2054 elsif ( @{ $res{'info'} } == 1 && !$res{'info'}[0]{'TrustLevel'} ) {
2056 $issue{'Message'} = "There is one suitable key, but trust level is not set."; #loc
2060 $issue{'Message'} = "There are several keys suitable for encryption."; #loc
2062 push @issues, \%issue;
2064 return ($status, @issues);
2067 sub GetPublicKeyInfo {
2068 return GetKeyInfo( shift, 'public', @_ );
2071 sub GetPrivateKeyInfo {
2072 return GetKeyInfo( shift, 'private', @_ );
2076 my %res = GetKeysInfo(@_);
2077 $res{'info'} = $res{'info'}->[0];
2083 my $type = shift || 'public';
2087 return (exit_code => 0) unless $force;
2090 my $gnupg = new GnuPG::Interface;
2091 my %opt = RT->Config->Get('GnuPGOptions');
2092 $opt{'digest-algo'} ||= 'SHA1';
2093 $opt{'with-colons'} = undef; # parseable format
2094 $opt{'fingerprint'} = undef; # show fingerprint
2095 $opt{'fixed-list-mode'} = undef; # don't merge uid with keys
2096 $gnupg->options->hash_init(
2097 _PrepareGnuPGOptions( %opt ),
2099 meta_interactive => 0,
2104 my ($handles, $handle_list) = _make_gpg_handles();
2105 my %handle = %$handle_list;
2108 local $SIG{'CHLD'} = 'DEFAULT';
2109 my $method = $type eq 'private'? 'list_secret_keys': 'list_public_keys';
2110 my $pid = safe_run_child { $gnupg->$method( handles => $handles, $email? (command_args => $email) : () ) };
2111 close $handle{'stdin'};
2115 my @info = readline $handle{'stdout'};
2116 close $handle{'stdout'};
2118 $res{'exit_code'} = $?;
2119 foreach ( qw(stderr logger status) ) {
2120 $res{$_} = do { local $/; readline $handle{$_} };
2121 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
2124 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
2125 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
2126 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
2128 $res{'message'} = $@? $@: "gpg exitted with error code ". ($? >> 8);
2132 @info = ParseKeysInfo( @info );
2133 $res{'info'} = \@info;
2140 my %gpg_opt = RT->Config->Get('GnuPGOptions');
2143 foreach my $line( @lines ) {
2146 ($tag, $line) = split /:/, $line, 2;
2147 if ( $tag eq 'pub' ) {
2150 TrustChar KeyLength Algorithm Key
2151 Created Expire Empty OwnerTrustChar
2152 Empty Empty Capabilities Other
2153 ) } = split /:/, $line, 12;
2155 # workaround gnupg's wierd behaviour, --list-keys command report calculated trust levels
2156 # for any model except 'always', so you can change models and see changes, but not for 'always'
2157 # we try to handle it in a simple way - we set ultimate trust for any key with trust
2158 # level >= 0 if trust model is 'always'
2160 $always_trust = 1 if exists $gpg_opt{'always-trust'};
2161 $always_trust = 1 if exists $gpg_opt{'trust-model'} && $gpg_opt{'trust-model'} eq 'always';
2162 @info{qw(Trust TrustTerse TrustLevel)} =
2163 _ConvertTrustChar( $info{'TrustChar'} );
2164 if ( $always_trust && $info{'TrustLevel'} >= 0 ) {
2165 @info{qw(Trust TrustTerse TrustLevel)} =
2166 _ConvertTrustChar( 'u' );
2169 @info{qw(OwnerTrust OwnerTrustTerse OwnerTrustLevel)} =
2170 _ConvertTrustChar( $info{'OwnerTrustChar'} );
2171 $info{ $_ } = _ParseDate( $info{ $_ } )
2172 foreach qw(Created Expire);
2175 elsif ( $tag eq 'sec' ) {
2178 Empty KeyLength Algorithm Key
2179 Created Expire Empty OwnerTrustChar
2180 Empty Empty Capabilities Other
2181 ) } = split /:/, $line, 12;
2182 @info{qw(OwnerTrust OwnerTrustTerse OwnerTrustLevel)} =
2183 _ConvertTrustChar( $info{'OwnerTrustChar'} );
2184 $info{ $_ } = _ParseDate( $info{ $_ } )
2185 foreach qw(Created Expire);
2188 elsif ( $tag eq 'uid' ) {
2190 @info{ qw(Trust Created Expire String) }
2191 = (split /:/, $line)[0,4,5,8];
2192 $info{ $_ } = _ParseDate( $info{ $_ } )
2193 foreach qw(Created Expire);
2194 push @{ $res[-1]{'User'} ||= [] }, \%info;
2196 elsif ( $tag eq 'fpr' ) {
2197 $res[-1]{'Fingerprint'} = (split /:/, $line, 10)[8];
2207 "The key has been disabled", #loc
2208 "key disabled", #loc
2213 "The key has been revoked", #loc
2218 e => [ "The key has expired", #loc
2223 n => [ "Don't trust this key at all", #loc
2228 #gpupg docs says that '-' and 'q' may safely be treated as the same value
2230 'Unknown (no trust value assigned)', #loc
2235 'Unknown (no trust value assigned)', #loc
2240 'Unknown (this value is new to the system)', #loc
2246 "There is marginal trust in this key", #loc
2251 "The key is fully trusted", #loc
2256 "The key is ultimately trusted", #loc
2262 sub _ConvertTrustChar {
2264 return @{ $verbose{'-'} } unless $value;
2265 $value = substr $value, 0, 1;
2266 return @{ $verbose{ $value } || $verbose{'o'} };
2273 return $value unless $value;
2276 my $obj = RT::Date->new( $RT::SystemUser );
2278 if ( $value =~ /^\d+$/ ) {
2279 $obj->Set( Value => $value );
2281 $obj->Set( Format => 'unknown', Value => $value, Timezone => 'utc' );
2289 my $gnupg = new GnuPG::Interface;
2290 my %opt = RT->Config->Get('GnuPGOptions');
2291 $gnupg->options->hash_init(
2292 _PrepareGnuPGOptions( %opt ),
2293 meta_interactive => 0,
2296 my ($handles, $handle_list) = _make_gpg_handles();
2297 my %handle = %$handle_list;
2300 local $SIG{'CHLD'} = 'DEFAULT';
2301 local @ENV{'LANG', 'LC_ALL'} = ('C', 'C');
2302 my $pid = safe_run_child { $gnupg->wrap_call(
2303 handles => $handles,
2304 commands => ['--delete-secret-and-public-key'],
2305 command_args => [$key],
2307 close $handle{'stdin'};
2308 while ( my $str = readline $handle{'status'} ) {
2309 if ( $str =~ /^\[GNUPG:\]\s*GET_BOOL delete_key\..*/ ) {
2310 print { $handle{'command'} } "y\n";
2316 close $handle{'stdout'};
2319 $res{'exit_code'} = $?;
2320 foreach ( qw(stderr logger status) ) {
2321 $res{$_} = do { local $/; readline $handle{$_} };
2322 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
2325 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
2326 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
2327 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
2328 if ( $err || $res{'exit_code'} ) {
2329 $res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
2337 my $gnupg = new GnuPG::Interface;
2338 my %opt = RT->Config->Get('GnuPGOptions');
2339 $gnupg->options->hash_init(
2340 _PrepareGnuPGOptions( %opt ),
2341 meta_interactive => 0,
2344 my ($handles, $handle_list) = _make_gpg_handles();
2345 my %handle = %$handle_list;
2348 local $SIG{'CHLD'} = 'DEFAULT';
2349 local @ENV{'LANG', 'LC_ALL'} = ('C', 'C');
2350 my $pid = safe_run_child { $gnupg->wrap_call(
2351 handles => $handles,
2352 commands => ['--import'],
2354 print { $handle{'stdin'} } $key;
2355 close $handle{'stdin'};
2359 close $handle{'stdout'};
2362 $res{'exit_code'} = $?;
2363 foreach ( qw(stderr logger status) ) {
2364 $res{$_} = do { local $/; readline $handle{$_} };
2365 delete $res{$_} unless $res{$_} && $res{$_} =~ /\S/s;
2368 $RT::Logger->debug( $res{'status'} ) if $res{'status'};
2369 $RT::Logger->warning( $res{'stderr'} ) if $res{'stderr'};
2370 $RT::Logger->error( $res{'logger'} ) if $res{'logger'} && $?;
2371 if ( $err || $res{'exit_code'} ) {
2372 $res{'message'} = $err? $err : "gpg exitted with error code ". ($res{'exit_code'} >> 8);
2379 Signs a small message with the key, to make sure the key exists and
2380 we have a useable passphrase. The first argument MUST be a key identifier
2381 of the signer: either email address, key id or finger print.
2383 Returns a true value if all went well.
2390 my $mime = MIME::Entity->build(
2391 Type => "text/plain",
2392 From => 'nobody@localhost',
2393 To => 'nobody@localhost',
2394 Subject => "dry sign",
2398 my %res = SignEncrypt(
2405 return $res{exit_code} == 0;
2412 This routine returns true if RT's GnuPG support is configured and working
2413 properly (and false otherwise).
2420 my $gnupg = new GnuPG::Interface;
2421 my %opt = RT->Config->Get('GnuPGOptions');
2422 $gnupg->options->hash_init(
2423 _PrepareGnuPGOptions( %opt ),
2425 meta_interactive => 0,
2428 my ($handles, $handle_list) = _make_gpg_handles();
2429 my %handle = %$handle_list;
2433 local $SIG{'CHLD'} = 'DEFAULT';
2434 my $pid = safe_run_child { $gnupg->wrap_call( commands => ['--version' ], handles => $handles ) };
2435 close $handle{'stdin'};
2440 "Probe for GPG failed."
2441 ." Couldn't run `gpg --version`: ". $@
2446 # on some systems gpg exits with code 2, but still 100% functional,
2447 # it's general error system error or incorrect command, command is correct,
2448 # but there is no way to get actuall error
2449 if ( $? && ($? >> 8) != 2 ) {
2450 my $msg = "Probe for GPG failed."
2451 ." Process exitted with code ". ($? >> 8)
2452 . ($? & 127 ? (" as recieved signal ". ($? & 127)) : '')
2454 foreach ( qw(stderr logger status) ) {
2455 my $tmp = do { local $/; readline $handle{$_} };
2456 next unless $tmp && $tmp =~ /\S/s;
2458 $msg .= "\n$_:\n$tmp\n";
2460 $RT::Logger->debug( $msg );
2467 sub _make_gpg_handles {
2468 my %handle_map = (@_);
2469 $handle_map{$_} = IO::Handle->new
2470 foreach grep !defined $handle_map{$_},
2471 qw(stdin stdout stderr logger status command);
2473 my $handles = GnuPG::Handles->new(%handle_map);
2474 return ($handles, \%handle_map);
2477 RT::Base->_ImportOverlays();
2479 # helper package to avoid using temp file
2480 package IO::Handle::CRLF;
2482 use base qw(IO::Handle);
2485 my ($self, @args) = (@_);
2486 s/\r*\n/\x0D\x0A/g foreach @args;
2487 return $self->SUPER::print( @args );