The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

GnuPG::Interface - Perl interface to GnuPG

SYNOPSIS

  # A simple example
  use IO::Handle;
  use GnuPG::Interface;
  
  # settting up the situation
  my $gnupg = GnuPG::Interface->new();
  $gnupg->options->hash_init( armor   => 1,
                              homedir => '/home/foobar' );

  # Note you can set the recipients even if you aren't encrypting!
  $gnupg->options->push_recipients( 'ftobin@uiuc.edu' );
  $gnupg->options->meta_interactive( 0 );

  # how we create some handles to interact with GnuPG
  my $input   = IO::Handle->new();
  my $output  = IO::Handle->new();
  my $handles = GnuPG::Handles->new( stdin  => $input,
                                     stdout => $output );

  # Now we'll go about encrypting with the options already set
  my @plaintext = ( 'foobar' );
  $gnupg->encrypt( handles => $handles );
  
  # Now we write to the input of GnuPG
  print $input @plaintext;
  close $input;

  # now we read the output
  my @ciphertext = <$output>;
  close $output;

  wait;

DESCRIPTION

GnuPG::Interface and it's associated modules are designed to provide an object-oriented method for interacting with GnuPG, being able to perform functions such as but not limited to encrypting, signing, decryption, verification, and key-listing parsing.

How Data Member Accessor Methods are Created

Each module in the GnuPG::Interface bundle relies on Class::MethodMaker to generate the get/set methods used to set the object's data members. This is very important to realize. This means that any data member which is a list has special methods assigned to it for pushing, popping, and clearing the list.

Understanding Bidirectional Communication

It is also imperative to realize that this package uses interprocess communication methods similar to those used in IPC::Open3 and "Bidirectional Communication with Another Process" in perlipc, and that users of this package need to understand how to use this method because this package does not abstract these methods for the user greatly. processes using these methods. This package is not designed to abstract this away (partly for security purposes) but rather to simply creating a 'proper' call to GnuPG, and to implement key-listing parsing. Please see "Bidirectional Communication with Another Process" in perlipc to learn how to deal with these methods.

Using this package to do message processing generally invovlves creating a GnuPG::Interface object, creating a GnuPG::Handles object, setting some options in its options data member, and then calling a method which invokes GnuPG, such as clearsign. One then interacts with with the handles appropriately, as described in "Bidirectional Communication with Another Process" in perlipc.

OBJECT METHODS

Initialization Methods

new( %initialization_args )

This methods creates a new object. The optional arguments are initialization of data members; the initialization is done in a manner according to the method created as described in "new_hash_init" in Class::MethodMaker.

hash_init( %args ).

This methods work as described in "new_hash_init" in Class::MethodMaker.

Object Methods which use a GnuPG::Handles Object

list_public_keys( % )
list_sigs( % )
list_secret_keys( % )
encrypt( % )
encrypt_symmetrically( % )
sign( % )
clearsign( % )
detach_sign( % )
sign_and_encrypt( % )
decrypt( % )
verify( % )
import_keys( % )
export_keys( % )
recv_keys( % )
send_keys( % )

These methods each correspond directly to or are very similar to a GnuPG command described in gpg. Each of these methods takes a hash, which currently must contain a key of handles which has the value of a GnuPG::Handles object. Another optional key is gnupg_command_args should have the value of an array reference; these arguments will be passed to GnuPG as command arguments. These command arguments are used for such things as determining the keys to list in the export_keys method. Please note that GnuPG command arguments are not the same as GnuPG options. To understand what are options and what are command arguments please read "COMMANDS" in gpg and "OPTIONS" in gpg.

These methods will attach the handles specified in the handles object to the running GnuPG object, so that bidirectional communication can be established. That is, the optionally-defined stdin, stdout, stderr, status, logger, and passphrase handles will be attached to GnuPG's input, output, standard error, the handle created by setting status-fd, the handle created by setting logger-fd, and the handle created by setting passphrase-fd respectively. This tying of handles of similar to the process done in IPC::Open3.

If any handle in the handles object is not defined, GnuPG's input, output, and standard error will be tied to the running program's standard error, standard output, or standard error. If the status or logger handle is not defined, this channel of communication is never established with GnuPG, and so this information is not generated and does not come into play. If the passphrase data member handle of the handles object is not defined, but the the passphrase data member handle of GnuPG::Interface object is, GnupG::Interface will handle passing this information into GnuPG for the user as a convience. Note that this will result in GnuPG::Interface storing the passphrase in memory, instead of having it simply 'pass-through' to GnuPG via a handle.

Other Methods

get_public_keys( @search_strings )
get_secret_keys( @search_strings )
get_public_keys_with_sigs( @search_strings )

These methods create and return objects of the type GnuPG::PublicKey or GnuPG::SecretKey respectively. This is done by parsing the output of GnuPG with the option with-colons enabled. The objects created do or do not have signature information stored in them, depending if the method ends in _sigs; this separation of functionality is there because of performance hits when listing information with signatures.

test_default_key_passphrase()

This method will return a true or false value, depending on whether GnupG reports a good passphrase was entered while signing a short message using the values of the passphrase data member, and the default key specified in the options data member.

OBJECT DATA MEMBERS

Note that these data members are interacted with via object methods created using the methods described in "get_set" in Class::MethodMaker, or "object" in Class::MethodMaker. Please read there for more information.

gnupg_call

This defines the call made to invoke GnuPG. Defaults to 'gpg'; this should be changed if 'gpg' is not in your path, or there is a different name for the binary on your system.

passphrase

In order to lessen the burden of using handles by the user of this package, setting this option to one's passphrase for a secret key will allow the package to enter the passphrase via a handle to GnuPG by itself instead of leaving this to the user. See also "passphrase" in GnuPG::Handles.

options

This data member, of the type GnuPG::Options; the setting stored in this data member are used to determine the options used when calling GnuPG via any of the object methods described in this package. See GnuPG::Options for more information.

EXAMPLES

The following setup can be done before any of the following examples:

  use IO::Handle;
  use GnupG::Interface;

  my @original_plaintext = ( "How do you doo?" );
  my $passphrsae = "Three Little Pigs";

  my $gnupg = GnupG::Interface->new();

  $gnupg->options->hash_init( armor    => 1,
                              recipients => [ 'ftobin@uiuc.edu',
                                              '0xABCD1234' ],
                              meta_interactive( 0 ),
                            );

Encrypting

  # We'll let standard error to to our standard error
  my ( $input, $output ) = ( IO::Handle->new(),
                             IO::Handle->new() );

  my $handles = GnupG::Handles->new( stdin    => $input,
                                     stdout   => $output );
   
  # this sets up the communication
  # Note that the recipients were specified earlier
  # in the 'options' data member of the $gnupg object.
  $gnupg->encrypt( handles => $handles );

  # this passes in the plaintext
  print $input @original_plaintext;

  # this closes the communication channel,
  # indicating we are done
  close $input;

  my @ciphertext = <$output>;  # reading the output

  wait;  # clean up the finished GnuPG process

Signing

  # This time we'll catch the standard error for our perusing
  my ( $input, $output, $error ) = ( IO::Handle->new(),
                                     IO::Handle->new(),
                                     IO::Handle->new() );
  my $handles = GnupG::Handles->new( stdin    => $input,
                                     stdout   => $output,
                                     stderr   => $error,  );

  # indicate our pasphrase through the
  # convience method
  $gnupg->passphrase( $passphrase );

  # this sets up the communication
  $gnupg->sign( handles => $handles );

  # this passes in the plaintext
  print $input @original_plaintext;

  # this closes the communication channel,
  # indicating we are done
  close $input;

  my @ciphertext   = <$output>;  # reading the output
  my @error_output = <$error>;   # reading the error

  close $output;
  close $error;

  wait;  # clean up the finished GnuPG process

Decryption

  # This time we'll catch the standard error for our perusing
  # as well as passing in the passphrase manually
  # as well as the status information given by GnuPG
  my ( $input, $output, $error, $passphrase_fh, $status_fh )
    = ( IO::Handle->new(),
        IO::Handle->new(),
        IO::Handle->new(),
        IO::Handle->new(),
        IO::Handle->new() );
  my $handles = GnupG::Handles->new( stdin    => $input,
                                      stdout   => $output,
                                      stderr   => $error,
                                      passphrase => $passphrase_fh,
                                      status     => $status_fh );

  # this time we'll also demonstrate decrypting
  # a file written to disk
  # Make sure you "use IO::File" if you use this module!
  my $cipher_file = IO::File->new( 'encrypted.gpg' );
   
  # this sets up the communication
  $gnupg->decrypt( handles => $handles );

  # This passes in the passphrase
  print $passphrase_fd $passphrase;
  close $passphrase_fd;

  # this passes in the plaintext
  print $input $_ while <$cipher_file>

  # this closes the communication channel,
  # indicating we are done
  close $input;
  close $cipher_file;

  my @plaintext    = <$output>;   # reading the output
  my @error_output = <$error>;    # reading the error
  my @status_info  = <$status_fh> # read the status info

  # clean up...
  close $output;
  close $error;
  close $status_fh;

  wait;  # clean up the finished GnuPG process

Printing Keys

  # This time we'll just let GnuPG print to our own output
  # and read from our input, because no input is needed!
  my $handles = GnuPG::Handles->new();
  
  my @ids = [ 'ftobin', '0xABCD1234' ];

  # this time we need to specify something for
  # gnupg_command_args because --list-public-keys takes
  # search ids as arguments
  $gnupg->list_public_keys( handles            => $handles,
                            gnupg_command_args => [ @ids ]  );
  
   wait;

Creating GnuPG::PublicKey Objects

  my @ids = [ 'ftobin', '0xABCD1234' ];

  my @keys = $gnupg->get_public_keys( @ids );

  # no wait is required this time; it's handled internally
  # since the entire call is encapsulated

NOTES

This package is the successor to PGP::GPG::MessageProcessor, which I found to be too inextensible to carry on further. A total redesign was needed, and this is the resulting work.

After any call to a GnuPG-command method of GnuPG::Interface in which one passes in the handles, one should all wait to clean up GnuPG from the process table.

BUGS

Currently there are problems when transmitting large quantities of information over handles; I'm guessing this is due to buffering issues. This bug does not seem specific to this package; IPC::Open3 also appears affected.

I don't know yet how well this modules handles parsing OpenPGP v3 keys.

SEE ALSO

See GnuPG::Options, GnuPG::Handles, GnuPG::PublicKey, GnuPG::SecretKey, gpg, Class::MethodMaker, and "Bidirectional Communication with Another Process" in perlipc.

AUTHOR

Frank J. Tobin, ftobin@uiuc.edu

PACKAGE UPDATES

Package updates may be found on http://GnuPG-Interface.sourceforge.net/ or CPAN, http://www.cpan.org/.