|
NAMEGnuPG::Interface - Perl interface to GnuPGSYNOPSIS# A simple example use IO::Handle; use GnuPG::Interface; # setting 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@cpan.org' ); $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' ); my $pid = $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; waitpid $pid, 0; DESCRIPTIONGnuPG::Interface and its 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 CreatedEach module in the GnuPG::Interface bundle relies on Moo 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 CommunicationIt 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. This package is not designed to abstract this away entirely (partly for security purposes), but rather to simply help create 'proper', clean calls 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. GnuPG VersionsAs of this version of GnuPG::Interface, there are two supported versions of GnuPG: 1.4.x and 2.2.x. The GnuPG download page <https://gnupg.org/download/index.html> has updated information on the currently supported versions.GnuPG released 2.0 and 2.1 versions in the past and some packaging systems may still provide these if you install the default "gpg", "gnupg", "gnupg2", etc. packages. This modules supports only version 2.2.x, so you may need to find additional package repositories or build from source to get the updated version. OBJECT METHODSInitialization Methods
Object Methods which use a GnuPG::Handles Object
Other Methods
Invoking GnuPG with a custom callGnuPG::Interface attempts to cover a lot of the commands of GnuPG that one would want to perform; however, there may be a lot more calls that GnuPG is and will be capable of, so a generic command interface is provided, "wrap_call".
The following keys are optional.
OBJECT DATA MEMBERS
EXAMPLESThe 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 $passphrase = "Three Little Pigs"; my $gnupg = GnuPG::Interface->new(); $gnupg->options->hash_init( armor => 1, recipients => [ 'ftobin@uiuc.edu', '0xABCD1234ABCD1234ABCD1234ABCD1234ABCD1234' ], meta_interactive => 0 , ); $gnupg->options->debug_level(4); $gnupg->options->logger_file("/tmp/gnupg-$$-decrypt-".time().".log"); Encrypting# We'll let the standard error of GnuPG pass through # to our own standard error, by not creating # a stderr-part of the $handles object. 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. my $pid = $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 waitpid $pid, 0; # 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 # convenience method $gnupg->passphrase( $passphrase ); # this sets up the communication my $pid = $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; waitpid $pid, 0; # 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 my $pid = $gnupg->decrypt( handles => $handles ); # This passes in the passphrase print $passphrase_fh $passphrase; close $passphrase_fh; # 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; waitpid $pid, 0; # 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', '0xABCD1234ABCD1234ABCD1234ABCD1234ABCD1234' ); # this time we need to specify something for # command_args because --list-public-keys takes # search ids as arguments my $pid = $gnupg->list_public_keys( handles => $handles, command_args => [ @ids ] ); waitpid $pid, 0; Creating GnuPG::PublicKey Objectsmy @ids = [ 'ftobin', '0xABCD1234ABCD1234ABCD1234ABCD1234ABCD1234' ]; my @keys = $gnupg->get_public_keys( @ids ); # no wait is required this time; it's handled internally # since the entire call is encapsulated Custom GnuPG call# assuming $handles is a GnuPG::Handles object my $pid = $gnupg->wrap_call ( commands => [ qw( --list-packets ) ], command_args => [ qw( test/key.1.asc ) ], handles => $handles, ); my @out = <$handles->stdout()>; waitpid $pid, 0; FAQ
NOTESThis 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. BUGSLarge Amounts of DataCurrently 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.OpenPGP v3 KeysI don't know yet how well this module handles parsing OpenPGP v3 keys.RHEL 7 Test FailuresTesting with the updates for version 1.00 we saw intermittent test failures on RHEL 7 with GnuPG version 2.2.20. In some cases the tests would all pass for several runs, then one would fail. We're unable to reliably reproduce this so we would be interested in feedback from other users.SEE ALSOGnuPG::Options, GnuPG::Handles, GnuPG::PublicKey, GnuPG::SecretKey, gpg, "Bidirectional Communication with Another Process" in perlipcLICENSEThis module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.AUTHORGnuPG::Interface is currently maintained by Best Practical Solutions <BPS@cpan.org>.Frank J. Tobin, ftobin@cpan.org was the original author of the package.
Visit the GSP FreeBSD Man Page Interface. |