| 1 | | In order to install this module, first run |
|---|
| 2 | | |
|---|
| 3 | | perl Makefile.PL |
|---|
| 4 | | |
|---|
| 5 | | If this command says that some prerequisite modules are not present, |
|---|
| 6 | | you need to install these modules; each required one should |
|---|
| 7 | | be available from CPAN at http://www.cpan.org/ |
|---|
| 8 | | |
|---|
| 9 | | make |
|---|
| 10 | | make test |
|---|
| 11 | | make install |
|---|
| 12 | | |
|---|
| 13 | | This module depends on: |
|---|
| 14 | | |
|---|
| 15 | | Perl 5.005 |
|---|
| 16 | | Class::MethodMaker 1.00 or later, available from CPAN; http://www.cpan.org/ |
|---|
| 17 | | GnuPG available from http://www.gnupg.org/ |
|---|
| 18 | | |
|---|
| 19 | | |
|---|
| 20 | | ----------------------------------------------------------------------- |
|---|
| 21 | | |
|---|
| 22 | | NAME |
|---|
| 23 | | GnuPG::Interface - Perl interface to GnuPG |
|---|
| 24 | | |
|---|
| 25 | | SYNOPSIS |
|---|
| 26 | | # A simple example |
|---|
| 27 | | use IO::Handle; |
|---|
| 28 | | use GnuPG::Interface; |
|---|
| 29 | | |
|---|
| 30 | | # settting up the situation |
|---|
| 31 | | my $gnupg = GnuPG::Interface->new(); |
|---|
| 32 | | $gnupg->options->hash_init( armor => 1, |
|---|
| 33 | | homedir => '/home/foobar' ); |
|---|
| 34 | | |
|---|
| 35 | | # Note you can set the recipients even if you aren't encrypting! |
|---|
| 36 | | $gnupg->options->push_recipients( 'ftobin@cpan.org' ); |
|---|
| 37 | | $gnupg->options->meta_interactive( 0 ); |
|---|
| 38 | | |
|---|
| 39 | | # how we create some handles to interact with GnuPG |
|---|
| 40 | | my $input = IO::Handle->new(); |
|---|
| 41 | | my $output = IO::Handle->new(); |
|---|
| 42 | | my $handles = GnuPG::Handles->new( stdin => $input, |
|---|
| 43 | | stdout => $output ); |
|---|
| 44 | | |
|---|
| 45 | | # Now we'll go about encrypting with the options already set |
|---|
| 46 | | my @plaintext = ( 'foobar' ); |
|---|
| 47 | | my $pid = $gnupg->encrypt( handles => $handles ); |
|---|
| 48 | | |
|---|
| 49 | | # Now we write to the input of GnuPG |
|---|
| 50 | | print $input @plaintext; |
|---|
| 51 | | close $input; |
|---|
| 52 | | |
|---|
| 53 | | # now we read the output |
|---|
| 54 | | my @ciphertext = <$output>; |
|---|
| 55 | | close $output; |
|---|
| 56 | | |
|---|
| 57 | | waitpid $pid, 0; |
|---|
| 58 | | |
|---|
| 59 | | DESCRIPTION |
|---|
| 60 | | GnuPG::Interface and its associated modules are designed to provide an |
|---|
| 61 | | object-oriented method for interacting with GnuPG, being able to perform |
|---|
| 62 | | functions such as but not limited to encrypting, signing, decryption, |
|---|
| 63 | | verification, and key-listing parsing. |
|---|
| 64 | | |
|---|
| 65 | | How Data Member Accessor Methods are Created |
|---|
| 66 | | |
|---|
| 67 | | Each module in the GnuPG::Interface bundle relies on Class::MethodMaker |
|---|
| 68 | | to generate the get/set methods used to set the object's data members. |
|---|
| 69 | | *This is very important to realize.* This means that any data member |
|---|
| 70 | | which is a list has special methods assigned to it for pushing, popping, |
|---|
| 71 | | and clearing the list. |
|---|
| 72 | | |
|---|
| 73 | | Understanding Bidirectional Communication |
|---|
| 74 | | |
|---|
| 75 | | It is also imperative to realize that this package uses interprocess |
|---|
| 76 | | communication methods similar to those used in the IPC::Open3 manpage |
|---|
| 77 | | and the section on "Bidirectional Communication with Another Process" in |
|---|
| 78 | | the perlipc manpage, and that users of this package need to understand |
|---|
| 79 | | how to use this method because this package does not abstract these |
|---|
| 80 | | methods for the user greatly. This package is not designed to abstract |
|---|
| 81 | | this away entirely (partly for security purposes), but rather to simply |
|---|
| 82 | | help create 'proper', clean calls to GnuPG, and to implement key-listing |
|---|
| 83 | | parsing. Please see the section on "Bidirectional Communication with |
|---|
| 84 | | Another Process" in the perlipc manpage to learn how to deal with these |
|---|
| 85 | | methods. |
|---|
| 86 | | |
|---|
| 87 | | Using this package to do message processing generally invovlves creating |
|---|
| 88 | | a GnuPG::Interface object, creating a GnuPG::Handles object, setting |
|---|
| 89 | | some options in its options data member, and then calling a method which |
|---|
| 90 | | invokes GnuPG, such as clearsign. One then interacts with with the |
|---|
| 91 | | handles appropriately, as described in the section on "Bidirectional |
|---|
| 92 | | Communication with Another Process" in the perlipc manpage. |
|---|
| 93 | | |
|---|
| 94 | | OBJECT METHODS |
|---|
| 95 | | Initialization Methods |
|---|
| 96 | | |
|---|
| 97 | | new( *%initialization_args* ) |
|---|
| 98 | | This methods creates a new object. The optional arguments are |
|---|
| 99 | | initialization of data members; the initialization is done in a |
|---|
| 100 | | manner according to the method created as described in the section |
|---|
| 101 | | on "new_hash_init" in the Class::MethodMaker manpage. |
|---|
| 102 | | |
|---|
| 103 | | hash_init( *%args* ). |
|---|
| 104 | | This methods work as described in the section on "new_hash_init" in |
|---|
| 105 | | the Class::MethodMaker manpage. |
|---|
| 106 | | |
|---|
| 107 | | Object Methods which use a GnuPG::Handles Object |
|---|
| 108 | | |
|---|
| 109 | | list_public_keys( % ) |
|---|
| 110 | | list_sigs( % ) |
|---|
| 111 | | list_secret_keys( % ) |
|---|
| 112 | | encrypt( % ) |
|---|
| 113 | | encrypt_symmetrically( % ) |
|---|
| 114 | | sign( % ) |
|---|
| 115 | | clearsign( % ) |
|---|
| 116 | | detach_sign( % ) |
|---|
| 117 | | sign_and_encrypt( % ) |
|---|
| 118 | | decrypt( % ) |
|---|
| 119 | | verify( % ) |
|---|
| 120 | | import_keys( % ) |
|---|
| 121 | | export_keys( % ) |
|---|
| 122 | | recv_keys( % ) |
|---|
| 123 | | send_keys( % ) |
|---|
| 124 | | These methods each correspond directly to or are very similar to a |
|---|
| 125 | | GnuPG command described in the gpg manpage. Each of these methods |
|---|
| 126 | | takes a hash, which currently must contain a key of handles which |
|---|
| 127 | | has the value of a GnuPG::Handles object. Another optional key is |
|---|
| 128 | | command_args which should have the value of an array reference; |
|---|
| 129 | | these arguments will be passed to GnuPG as command arguments. These |
|---|
| 130 | | command arguments are used for such things as determining the keys |
|---|
| 131 | | to list in the export_keys method. *Please note that GnuPG command |
|---|
| 132 | | arguments are not the same as GnuPG options*. To understand what are |
|---|
| 133 | | options and what are command arguments please read the section on |
|---|
| 134 | | "COMMANDS" in the gpg manpage and the section on "OPTIONS" in the |
|---|
| 135 | | gpg manpage. |
|---|
| 136 | | |
|---|
| 137 | | Each of these calls returns the PID for the resulting GnuPG process. |
|---|
| 138 | | One can use this PID in a `waitpid' call instead of a `wait' call if |
|---|
| 139 | | more precise process reaping is needed. |
|---|
| 140 | | |
|---|
| 141 | | These methods will attach the handles specified in the handles |
|---|
| 142 | | object to the running GnuPG object, so that bidirectional |
|---|
| 143 | | communication can be established. That is, the optionally-defined |
|---|
| 144 | | stdin, stdout, stderr, status, logger, and passphrase handles will |
|---|
| 145 | | be attached to GnuPG's input, output, standard error, the handle |
|---|
| 146 | | created by setting status-fd, the handle created by setting logger- |
|---|
| 147 | | fd, and the handle created by setting passphrase-fd respectively. |
|---|
| 148 | | This tying of handles of similar to the process done in |
|---|
| 149 | | *IPC::Open3*. |
|---|
| 150 | | |
|---|
| 151 | | If you want the GnuPG process to read or write directly to an |
|---|
| 152 | | already-opened filehandle, you cannot do this via the normal |
|---|
| 153 | | *IPC::Open3* mechanisms. In order to accomplish this, set the |
|---|
| 154 | | appropriate handles data member to the already-opened filehandle, |
|---|
| 155 | | and then set the option direct to be true for that handle, as |
|---|
| 156 | | described in the "options" entry in the GnuPG::Handles manpage. For |
|---|
| 157 | | example, to have GnuPG read from the file input.txt and write to |
|---|
| 158 | | output.txt, the following snippet may do: |
|---|
| 159 | | |
|---|
| 160 | | my $infile = IO::File->new( 'input.txt.' ); |
|---|
| 161 | | my $outfile = IO::File->new( 'output.txt' ); |
|---|
| 162 | | my $handles = GnuPG::Handles->new( stdin => $infile, |
|---|
| 163 | | stdout => $outfile, |
|---|
| 164 | | ); |
|---|
| 165 | | $handles->options( 'stdin' )->{direct} = 1; |
|---|
| 166 | | $handles->options( 'stdout' )->{direct} = 1; |
|---|
| 167 | | |
|---|
| 168 | | If any handle in the handles object is not defined, GnuPG's input, |
|---|
| 169 | | output, and standard error will be tied to the running program's |
|---|
| 170 | | standard error, standard output, or standard error. If the status or |
|---|
| 171 | | logger handle is not defined, this channel of communication is never |
|---|
| 172 | | established with GnuPG, and so this information is not generated and |
|---|
| 173 | | does not come into play. If the passphrase data member handle of the |
|---|
| 174 | | handles object is not defined, but the the passphrase data member |
|---|
| 175 | | handle of GnuPG::Interface object is, GnuPG::Interface will handle |
|---|
| 176 | | passing this information into GnuPG for the user as a convience. |
|---|
| 177 | | Note that this will result in GnuPG::Interface storing the |
|---|
| 178 | | passphrase in memory, instead of having it simply 'pass-through' to |
|---|
| 179 | | GnuPG via a handle. |
|---|
| 180 | | |
|---|
| 181 | | Other Methods |
|---|
| 182 | | |
|---|
| 183 | | get_public_keys( @search_strings ) |
|---|
| 184 | | get_secret_keys( @search_strings ) |
|---|
| 185 | | get_public_keys_with_sigs( @search_strings ) |
|---|
| 186 | | These methods create and return objects of the type GnuPG::PublicKey |
|---|
| 187 | | or GnuPG::SecretKey respectively. This is done by parsing the output |
|---|
| 188 | | of GnuPG with the option with-colons enabled. The objects created do |
|---|
| 189 | | or do not have signature information stored in them, depending if |
|---|
| 190 | | the method ends in *_sigs*; this separation of functionality is |
|---|
| 191 | | there because of performance hits when listing information with |
|---|
| 192 | | signatures. |
|---|
| 193 | | |
|---|
| 194 | | test_default_key_passphrase() |
|---|
| 195 | | This method will return a true or false value, depending on whether |
|---|
| 196 | | GnuPG reports a good passphrase was entered while signing a short |
|---|
| 197 | | message using the values of the passphrase data member, and the |
|---|
| 198 | | default key specified in the options data member. |
|---|
| 199 | | |
|---|
| 200 | | Invoking GnuPG with a custom call |
|---|
| 201 | | GnuPG::Interface attempts to cover a lot of the commands of GnuPG that |
|---|
| 202 | | one would want to perform; however, there may be a lot more calls that |
|---|
| 203 | | GnuPG is and will be capable of, so a generic command interface is |
|---|
| 204 | | provided, `wrap_call'. |
|---|
| 205 | | |
|---|
| 206 | | wrap_call( %args ) |
|---|
| 207 | | Call GnuPG with a custom command. The %args hash must contain at |
|---|
| 208 | | least the following keys: |
|---|
| 209 | | |
|---|
| 210 | | commands |
|---|
| 211 | | The value of this key in the hash must be a reference to a a |
|---|
| 212 | | list of commands for GnuPG, such as `[ qw( --encrypt --sign ) |
|---|
| 213 | | ]'. |
|---|
| 214 | | |
|---|
| 215 | | handles As with most other GnuPG::Interface methods, handles must be a |
|---|
| 216 | | GnuPG::Handles object. |
|---|
| 217 | | |
|---|
| 218 | | The following keys are optional. |
|---|
| 219 | | |
|---|
| 220 | | command_args |
|---|
| 221 | | As with other GnuPG::Interface methods, the value in hash for |
|---|
| 222 | | this key must be a reference to a list of arguments to be passed |
|---|
| 223 | | to the GnuPG command, such as which keys to list in a key- |
|---|
| 224 | | listing. |
|---|
| 225 | | |
|---|
| 226 | | OBJECT DATA MEMBERS |
|---|
| 227 | | Note that these data members are interacted with via object methods |
|---|
| 228 | | created using the methods described in the section on "get_set" in the |
|---|
| 229 | | Class::MethodMaker manpage, or the section on "object" in the |
|---|
| 230 | | Class::MethodMaker manpage. Please read there for more information. |
|---|
| 231 | | |
|---|
| 232 | | call |
|---|
| 233 | | This defines the call made to invoke GnuPG. Defaults to 'gpg'; this |
|---|
| 234 | | should be changed if 'gpg' is not in your path, or there is a |
|---|
| 235 | | different name for the binary on your system. |
|---|
| 236 | | |
|---|
| 237 | | passphrase |
|---|
| 238 | | In order to lessen the burden of using handles by the user of this |
|---|
| 239 | | package, setting this option to one's passphrase for a secret key |
|---|
| 240 | | will allow the package to enter the passphrase via a handle to GnuPG |
|---|
| 241 | | by itself instead of leaving this to the user. See also the |
|---|
| 242 | | "passphrase" entry in the GnuPG::Handles manpage. |
|---|
| 243 | | |
|---|
| 244 | | options |
|---|
| 245 | | This data member, of the type GnuPG::Options; the setting stored in |
|---|
| 246 | | this data member are used to determine the options used when calling |
|---|
| 247 | | GnuPG via *any* of the object methods described in this package. See |
|---|
| 248 | | the GnuPG::Options manpage for more information. |
|---|
| 249 | | |
|---|
| 250 | | EXAMPLES |
|---|
| 251 | | The following setup can be done before any of the following examples: |
|---|
| 252 | | |
|---|
| 253 | | use IO::Handle; |
|---|
| 254 | | use GnuPG::Interface; |
|---|
| 255 | | |
|---|
| 256 | | my @original_plaintext = ( "How do you doo?" ); |
|---|
| 257 | | my $passphrsae = "Three Little Pigs"; |
|---|
| 258 | | |
|---|
| 259 | | my $gnupg = GnuPG::Interface->new(); |
|---|
| 260 | | |
|---|
| 261 | | $gnupg->options->hash_init( armor => 1, |
|---|
| 262 | | recipients => [ 'ftobin@uiuc.edu', |
|---|
| 263 | | '0xABCD1234' ], |
|---|
| 264 | | meta_interactive( 0 ), |
|---|
| 265 | | ); |
|---|
| 266 | | |
|---|
| 267 | | Encrypting |
|---|
| 268 | | |
|---|
| 269 | | # We'll let the standard error of GnuPG pass through |
|---|
| 270 | | # to our own standard error, by not creating |
|---|
| 271 | | # a stderr-part of the $handles object. |
|---|
| 272 | | my ( $input, $output ) = ( IO::Handle->new(), |
|---|
| 273 | | IO::Handle->new() ); |
|---|
| 274 | | |
|---|
| 275 | | my $handles = GnuPG::Handles->new( stdin => $input, |
|---|
| 276 | | stdout => $output ); |
|---|
| 277 | | |
|---|
| 278 | | # this sets up the communication |
|---|
| 279 | | # Note that the recipients were specified earlier |
|---|
| 280 | | # in the 'options' data member of the $gnupg object. |
|---|
| 281 | | my $pid = $gnupg->encrypt( handles => $handles ); |
|---|
| 282 | | |
|---|
| 283 | | # this passes in the plaintext |
|---|
| 284 | | print $input @original_plaintext; |
|---|
| 285 | | |
|---|
| 286 | | # this closes the communication channel, |
|---|
| 287 | | # indicating we are done |
|---|
| 288 | | close $input; |
|---|
| 289 | | |
|---|
| 290 | | my @ciphertext = <$output>; # reading the output |
|---|
| 291 | | |
|---|
| 292 | | waitpid $pid, 0; # clean up the finished GnuPG process |
|---|
| 293 | | |
|---|
| 294 | | Signing |
|---|
| 295 | | |
|---|
| 296 | | # This time we'll catch the standard error for our perusing |
|---|
| 297 | | my ( $input, $output, $error ) = ( IO::Handle->new(), |
|---|
| 298 | | IO::Handle->new(), |
|---|
| 299 | | IO::Handle->new(), |
|---|
| 300 | | ); |
|---|
| 301 | | |
|---|
| 302 | | my $handles = GnuPG::Handles->new( stdin => $input, |
|---|
| 303 | | stdout => $output, |
|---|
| 304 | | stderr => $error, |
|---|
| 305 | | ); |
|---|
| 306 | | |
|---|
| 307 | | # indicate our pasphrase through the |
|---|
| 308 | | # convience method |
|---|
| 309 | | $gnupg->passphrase( $passphrase ); |
|---|
| 310 | | |
|---|
| 311 | | # this sets up the communication |
|---|
| 312 | | my $pid = $gnupg->sign( handles => $handles ); |
|---|
| 313 | | |
|---|
| 314 | | # this passes in the plaintext |
|---|
| 315 | | print $input @original_plaintext; |
|---|
| 316 | | |
|---|
| 317 | | # this closes the communication channel, |
|---|
| 318 | | # indicating we are done |
|---|
| 319 | | close $input; |
|---|
| 320 | | |
|---|
| 321 | | my @ciphertext = <$output>; # reading the output |
|---|
| 322 | | my @error_output = <$error>; # reading the error |
|---|
| 323 | | |
|---|
| 324 | | close $output; |
|---|
| 325 | | close $error; |
|---|
| 326 | | |
|---|
| 327 | | waitpid $pid, 0; # clean up the finished GnuPG process |
|---|
| 328 | | |
|---|
| 329 | | Decryption |
|---|
| 330 | | |
|---|
| 331 | | # This time we'll catch the standard error for our perusing |
|---|
| 332 | | # as well as passing in the passphrase manually |
|---|
| 333 | | # as well as the status information given by GnuPG |
|---|
| 334 | | my ( $input, $output, $error, $passphrase_fh, $status_fh ) |
|---|
| 335 | | = ( IO::Handle->new(), |
|---|
| 336 | | IO::Handle->new(), |
|---|
| 337 | | IO::Handle->new(), |
|---|
| 338 | | IO::Handle->new(), |
|---|
| 339 | | IO::Handle->new(), |
|---|
| 340 | | ); |
|---|
| 341 | | |
|---|
| 342 | | my $handles = GnuPG::Handles->new( stdin => $input, |
|---|
| 343 | | stdout => $output, |
|---|
| 344 | | stderr => $error, |
|---|
| 345 | | passphrase => $passphrase_fh, |
|---|
| 346 | | status => $status_fh, |
|---|
| 347 | | ); |
|---|
| 348 | | |
|---|
| 349 | | # this time we'll also demonstrate decrypting |
|---|
| 350 | | # a file written to disk |
|---|
| 351 | | # Make sure you "use IO::File" if you use this module! |
|---|
| 352 | | my $cipher_file = IO::File->new( 'encrypted.gpg' ); |
|---|
| 353 | | |
|---|
| 354 | | # this sets up the communication |
|---|
| 355 | | my $pid = $gnupg->decrypt( handles => $handles ); |
|---|
| 356 | | |
|---|
| 357 | | # This passes in the passphrase |
|---|
| 358 | | print $passphrase_fd $passphrase; |
|---|
| 359 | | close $passphrase_fd; |
|---|
| 360 | | |
|---|
| 361 | | # this passes in the plaintext |
|---|
| 362 | | print $input $_ while <$cipher_file> |
|---|
| 363 | | |
|---|
| 364 | | # this closes the communication channel, |
|---|
| 365 | | # indicating we are done |
|---|
| 366 | | close $input; |
|---|
| 367 | | close $cipher_file; |
|---|
| 368 | | |
|---|
| 369 | | my @plaintext = <$output>; # reading the output |
|---|
| 370 | | my @error_output = <$error>; # reading the error |
|---|
| 371 | | my @status_info = <$status_fh> # read the status info |
|---|
| 372 | | |
|---|
| 373 | | # clean up... |
|---|
| 374 | | close $output; |
|---|
| 375 | | close $error; |
|---|
| 376 | | close $status_fh; |
|---|
| 377 | | |
|---|
| 378 | | waitpid $pid, 0; # clean up the finished GnuPG process |
|---|
| 379 | | |
|---|
| 380 | | Printing Keys |
|---|
| 381 | | |
|---|
| 382 | | # This time we'll just let GnuPG print to our own output |
|---|
| 383 | | # and read from our input, because no input is needed! |
|---|
| 384 | | my $handles = GnuPG::Handles->new(); |
|---|
| 385 | | |
|---|
| 386 | | my @ids = [ 'ftobin', '0xABCD1234' ]; |
|---|
| 387 | | |
|---|
| 388 | | # this time we need to specify something for |
|---|
| 389 | | # command_args because --list-public-keys takes |
|---|
| 390 | | # search ids as arguments |
|---|
| 391 | | my $pid = $gnupg->list_public_keys( handles => $handles, |
|---|
| 392 | | command_args => [ @ids ] ); |
|---|
| 393 | | |
|---|
| 394 | | waitpid $pid, 0; |
|---|
| 395 | | |
|---|
| 396 | | Creating GnuPG::PublicKey Objects |
|---|
| 397 | | |
|---|
| 398 | | my @ids = [ 'ftobin', '0xABCD1234' ]; |
|---|
| 399 | | |
|---|
| 400 | | my @keys = $gnupg->get_public_keys( @ids ); |
|---|
| 401 | | |
|---|
| 402 | | # no wait is required this time; it's handled internally |
|---|
| 403 | | # since the entire call is encapsulated |
|---|
| 404 | | |
|---|
| 405 | | Custom GnuPG call |
|---|
| 406 | | |
|---|
| 407 | | # assuming $handles is a GnuPG::Handles object |
|---|
| 408 | | my $pid = $gnupg->wrap_call |
|---|
| 409 | | ( commands => [ qw( --list-packets ) ], |
|---|
| 410 | | command_args => [ qw( test/key.1.asc ) ], |
|---|
| 411 | | handles => $handles, |
|---|
| 412 | | ); |
|---|
| 413 | | |
|---|
| 414 | | my @out = <$handles->stdout()>; |
|---|
| 415 | | waitpid $pid, 0; |
|---|
| 416 | | |
|---|
| 417 | | FAQ |
|---|
| 418 | | How do I get GnuPG::Interface to read/write directly from |
|---|
| 419 | | a filehandle? |
|---|
| 420 | | You need to set GnuPG::Handles direct option to be true for the |
|---|
| 421 | | filehandles in concern. See the "options" entry in the |
|---|
| 422 | | GnuPG::Handles manpage and the section on "Object Methods which use |
|---|
| 423 | | a GnuPG::Handles Object" for more information. |
|---|
| 424 | | |
|---|
| 425 | | Why do you make it so difficult to get GnuPG to write/read |
|---|
| 426 | | from a filehandle? In the shell, I can just call GnuPG |
|---|
| 427 | | with the --outfile option! |
|---|
| 428 | | There are lots of issues when trying to tell GnuPG to read/write |
|---|
| 429 | | directly from a file, such as if the file isn't there, or there is a |
|---|
| 430 | | file, and you want to write over it! What do you want to happen |
|---|
| 431 | | then? Having the user of this module handle these questions |
|---|
| 432 | | beforehand by opening up filehandles to GnuPG lets the user know |
|---|
| 433 | | fully what is going to happen in these circumstances, and makes the |
|---|
| 434 | | module less error-prone. |
|---|
| 435 | | |
|---|
| 436 | | When having GnuPG process a large message, sometimes it just |
|---|
| 437 | | hanges there. |
|---|
| 438 | | Your problem may be due to buffering issues; when GnuPG reads/writes |
|---|
| 439 | | to non-direct filehandles (those that are sent to filehandles which |
|---|
| 440 | | you read to from into memory, not that those access the disk), |
|---|
| 441 | | buffering issues can mess things up. I recommend looking into the |
|---|
| 442 | | "options" entry in the GnuPG::Handles manpage. |
|---|
| 443 | | |
|---|
| 444 | | NOTES |
|---|
| 445 | | This package is the successor to PGP::GPG::MessageProcessor, which I |
|---|
| 446 | | found to be too inextensible to carry on further. A total redesign was |
|---|
| 447 | | needed, and this is the resulting work. |
|---|
| 448 | | |
|---|
| 449 | | After any call to a GnuPG-command method of GnuPG::Interface in which |
|---|
| 450 | | one passes in the handles, one should all wait to clean up GnuPG from |
|---|
| 451 | | the process table. |
|---|
| 452 | | |
|---|
| 453 | | BUGS |
|---|
| 454 | | Currently there are problems when transmitting large quantities of |
|---|
| 455 | | information over handles; I'm guessing this is due to buffering issues. |
|---|
| 456 | | This bug does not seem specific to this package; IPC::Open3 also appears |
|---|
| 457 | | affected. |
|---|
| 458 | | |
|---|
| 459 | | I don't know yet how well this modules handles parsing OpenPGP v3 keys. |
|---|
| 460 | | |
|---|
| 461 | | SEE ALSO |
|---|
| 462 | | the GnuPG::Options manpage, the GnuPG::Handles manpage, the |
|---|
| 463 | | GnuPG::PublicKey manpage, the GnuPG::SecretKey manpage, the gpg manpage, |
|---|
| 464 | | the Class::MethodMaker manpage, the section on "Bidirectional |
|---|
| 465 | | Communication with Another Process" in the perlipc manpage |
|---|
| 466 | | |
|---|
| 467 | | AUTHOR |
|---|
| 468 | | Frank J. Tobin, ftobin@cpan.org |
|---|
| 469 | | |
|---|
| 470 | | PACKAGE UPDATES |
|---|
| 471 | | Package updates may be found on http://GnuPG-Interface.sourceforge.net/ |
|---|
| 472 | | or CPAN, http://www.cpan.org/. |
|---|
| 473 | | |
|---|
| | 1 | GnuPG::Interface(3) User Contributed Perl Documentation GnuPG::Interface(3) |
|---|
| | 2 | |
|---|
| | 3 | |
|---|
| | 4 | |
|---|
| | 5 | NNAAMMEE |
|---|
| | 6 | GnuPG::Interface â Perl interface to GnuPG |
|---|
| | 7 | |
|---|
| | 8 | SSYYNNOOPPSSIISS |
|---|
| | 9 | # A simple example |
|---|
| | 10 | use IO::Handle; |
|---|
| | 11 | use GnuPG::Interface; |
|---|
| | 12 | |
|---|
| | 13 | # settting up the situation |
|---|
| | 14 | my $gnupg = GnuPG::Interfaceâ>new(); |
|---|
| | 15 | $gnupgâ>optionsâ>hash_init( armor => 1, |
|---|
| | 16 | homedir => â/home/foobarâ ); |
|---|
| | 17 | |
|---|
| | 18 | # Note you can set the recipients even if you arenât encrypting! |
|---|
| | 19 | $gnupgâ>optionsâ>push_recipients( âftobin@cpan.orgâ ); |
|---|
| | 20 | $gnupgâ>optionsâ>meta_interactive( 0 ); |
|---|
| | 21 | |
|---|
| | 22 | # how we create some handles to interact with GnuPG |
|---|
| | 23 | my $input = IO::Handleâ>new(); |
|---|
| | 24 | my $output = IO::Handleâ>new(); |
|---|
| | 25 | my $handles = GnuPG::Handlesâ>new( stdin => $input, |
|---|
| | 26 | stdout => $output ); |
|---|
| | 27 | |
|---|
| | 28 | # Now weâll go about encrypting with the options already set |
|---|
| | 29 | my @plaintext = ( âfoobarâ ); |
|---|
| | 30 | my $pid = $gnupgâ>encrypt( handles => $handles ); |
|---|
| | 31 | |
|---|
| | 32 | # Now we write to the input of GnuPG |
|---|
| | 33 | print $input @plaintext; |
|---|
| | 34 | close $input; |
|---|
| | 35 | |
|---|
| | 36 | # now we read the output |
|---|
| | 37 | my @ciphertext = <$output>; |
|---|
| | 38 | close $output; |
|---|
| | 39 | |
|---|
| | 40 | waitpid $pid, 0; |
|---|
| | 41 | |
|---|
| | 42 | DDEESSCCRRIIPPTTIIOONN |
|---|
| | 43 | GnuPG::Interface and its associated modules are designed to provide an |
|---|
| | 44 | objectâoriented method for interacting with GnuPG, being able to perâ |
|---|
| | 45 | form functions such as but not limited to encrypting, signing, decrypâ |
|---|
| | 46 | tion, verification, and keyâlisting parsing. |
|---|
| | 47 | |
|---|
| | 48 | HHooww DDaattaa MMeemmbbeerr AAcccceessssoorr MMeetthhooddss aarree CCrreeaatteedd |
|---|
| | 49 | |
|---|
| | 50 | Each module in the GnuPG::Interface bundle relies on Class::MethodMaker |
|---|
| | 51 | to generate the get/set methods used to set the objectâs data members. |
|---|
| | 52 | _T_h_i_s _i_s _v_e_r_y _i_m_p_o_r_t_a_n_t _t_o _r_e_a_l_i_z_e_. This means that any data member |
|---|
| | 53 | which is a list has special methods assigned to it for pushing, popâ |
|---|
| | 54 | ping, and clearing the list. |
|---|
| | 55 | |
|---|
| | 56 | UUnnddeerrssttaannddiinngg BBiiddiirreeccttiioonnaall CCoommmmuunniiccaattiioonn |
|---|
| | 57 | |
|---|
| | 58 | It is also imperative to realize that this package uses interprocess |
|---|
| | 59 | communication methods similar to those used in IPC::Open3 and "Bidirecâ |
|---|
| | 60 | tional Communication with Another Process" in perlipc, and that users |
|---|
| | 61 | of this package need to understand how to use this method because this |
|---|
| | 62 | package does not abstract these methods for the user greatly. This |
|---|
| | 63 | package is not designed to abstract this away entirely (partly for |
|---|
| | 64 | security purposes), but rather to simply help create âproperâ, clean |
|---|
| | 65 | calls to GnuPG, and to implement keyâlisting parsing. Please see |
|---|
| | 66 | "Bidirectional Communication with Another Process" in perlipc to learn |
|---|
| | 67 | how to deal with these methods. |
|---|
| | 68 | |
|---|
| | 69 | Using this package to do message processing generally invovlves creatâ |
|---|
| | 70 | ing a GnuPG::Interface object, creating a GnuPG::Handles object, setâ |
|---|
| | 71 | ting some options in its ooppttiioonnss data member, and then calling a method |
|---|
| | 72 | which invokes GnuPG, such as cclleeaarrssiiggnn. One then interacts with with |
|---|
| | 73 | the handles appropriately, as described in "Bidirectional Communication |
|---|
| | 74 | with Another Process" in perlipc. |
|---|
| | 75 | |
|---|
| | 76 | OOBBJJEECCTT MMEETTHHOODDSS |
|---|
| | 77 | IInniittiiaalliizzaattiioonn MMeetthhooddss |
|---|
| | 78 | |
|---|
| | 79 | |
|---|
| | 80 | new( _%_i_n_i_t_i_a_l_i_z_a_t_i_o_n___a_r_g_s ) |
|---|
| | 81 | This methods creates a new object. The optional arguments are iniâ |
|---|
| | 82 | tialization of data members; the initialization is done in a manner |
|---|
| | 83 | according to the method created as described in "new_hash_init" in |
|---|
| | 84 | Class::MethodMaker. |
|---|
| | 85 | |
|---|
| | 86 | hash_init( _%_a_r_g_s ). |
|---|
| | 87 | This methods work as described in "new_hash_init" in Class::Methodâ |
|---|
| | 88 | Maker. |
|---|
| | 89 | |
|---|
| | 90 | OObbjjeecctt MMeetthhooddss wwhhiicchh uussee aa GGnnuuPPGG::::HHaannddlleess OObbjjeecctt |
|---|
| | 91 | |
|---|
| | 92 | |
|---|
| | 93 | list_public_keys( % ) |
|---|
| | 94 | list_sigs( % ) |
|---|
| | 95 | list_secret_keys( % ) |
|---|
| | 96 | encrypt( % ) |
|---|
| | 97 | encrypt_symmetrically( % ) |
|---|
| | 98 | sign( % ) |
|---|
| | 99 | clearsign( % ) |
|---|
| | 100 | detach_sign( % ) |
|---|
| | 101 | sign_and_encrypt( % ) |
|---|
| | 102 | decrypt( % ) |
|---|
| | 103 | verify( % ) |
|---|
| | 104 | import_keys( % ) |
|---|
| | 105 | export_keys( % ) |
|---|
| | 106 | recv_keys( % ) |
|---|
| | 107 | send_keys( % ) |
|---|
| | 108 | These methods each correspond directly to or are very similar to a |
|---|
| | 109 | GnuPG command described in gpg. Each of these methods takes a |
|---|
| | 110 | hash, which currently must contain a key of hhaannddlleess which has the |
|---|
| | 111 | value of a GnuPG::Handles object. Another optional key is ccoommââ |
|---|
| | 112 | mmaanndd__aarrggss which should have the value of an array reference; these |
|---|
| | 113 | arguments will be passed to GnuPG as command arguments. These comâ |
|---|
| | 114 | mand arguments are used for such things as determining the keys to |
|---|
| | 115 | list in the eexxppoorrtt__kkeeyyss method. _P_l_e_a_s_e _n_o_t_e _t_h_a_t _G_n_u_P_G _c_o_m_m_a_n_d |
|---|
| | 116 | _a_r_g_u_m_e_n_t_s _a_r_e _n_o_t _t_h_e _s_a_m_e _a_s _G_n_u_P_G _o_p_t_i_o_n_s. To understand what |
|---|
| | 117 | are options and what are command arguments please read "COMMANDS" |
|---|
| | 118 | in gpg and "OPTIONS" in gpg. |
|---|
| | 119 | |
|---|
| | 120 | Each of these calls returns the PID for the resulting GnuPG |
|---|
| | 121 | process. One can use this PID in a "waitpid" call instead of a |
|---|
| | 122 | "wait" call if more precise process reaping is needed. |
|---|
| | 123 | |
|---|
| | 124 | These methods will attach the handles specified in the hhaannddlleess |
|---|
| | 125 | object to the running GnuPG object, so that bidirectional communiâ |
|---|
| | 126 | cation can be established. That is, the optionallyâdefined ssttddiinn, |
|---|
| | 127 | ssttddoouutt, ssttddeerrrr, ssttaattuuss, llooggggeerr, and ppaasssspphhrraassee handles will be |
|---|
| | 128 | attached to GnuPGâs input, output, standard error, the handle creâ |
|---|
| | 129 | ated by setting ssttaattuussââffdd, the handle created by setting llooggggeerrââffdd, |
|---|
| | 130 | and the handle created by setting ppaasssspphhrraasseeââffdd respectively. This |
|---|
| | 131 | tying of handles of similar to the process done in _I_P_C_:_:_O_p_e_n_3. |
|---|
| | 132 | |
|---|
| | 133 | If you want the GnuPG process to read or write directly to an |
|---|
| | 134 | alreadyâopened filehandle, you cannot do this via the normal |
|---|
| | 135 | _I_P_C_:_:_O_p_e_n_3 mechanisms. In order to accomplish this, set the approâ |
|---|
| | 136 | priate hhaannddlleess data member to the alreadyâopened filehandle, and |
|---|
| | 137 | then set the option ddiirreecctt to be true for that handle, as described |
|---|
| | 138 | in "options" in GnuPG::Handles. For example, to have GnuPG read |
|---|
| | 139 | from the file _i_n_p_u_t_._t_x_t and write to _o_u_t_p_u_t_._t_x_t, the following |
|---|
| | 140 | snippet may do: |
|---|
| | 141 | |
|---|
| | 142 | my $infile = IO::Fileâ>new( âinput.txtâ ); |
|---|
| | 143 | my $outfile = IO::Fileâ>new( â>output.txtâ ); |
|---|
| | 144 | my $handles = GnuPG::Handlesâ>new( stdin => $infile, |
|---|
| | 145 | stdout => $outfile, |
|---|
| | 146 | ); |
|---|
| | 147 | $handlesâ>options( âstdinâ )â>{direct} = 1; |
|---|
| | 148 | $handlesâ>options( âstdoutâ )â>{direct} = 1; |
|---|
| | 149 | |
|---|
| | 150 | If any handle in the hhaannddlleess object is not defined, GnuPGâs input, |
|---|
| | 151 | output, and standard error will be tied to the running programâs |
|---|
| | 152 | standard error, standard output, or standard error. If the ssttaattuuss |
|---|
| | 153 | or llooggggeerr handle is not defined, this channel of communication is |
|---|
| | 154 | never established with GnuPG, and so this information is not generâ |
|---|
| | 155 | ated and does not come into play. If the ppaasssspphhrraassee data member |
|---|
| | 156 | handle of the hhaannddlleess object is not defined, but the the ppaasssspphhrraassee |
|---|
| | 157 | data member handle of GnuPG::Interface object is, GnuPG::Interface |
|---|
| | 158 | will handle passing this information into GnuPG for the user as a |
|---|
| | 159 | convience. Note that this will result in GnuPG::Interface storing |
|---|
| | 160 | the passphrase in memory, instead of having it simply |
|---|
| | 161 | âpassâthroughâ to GnuPG via a handle. |
|---|
| | 162 | |
|---|
| | 163 | OOtthheerr MMeetthhooddss |
|---|
| | 164 | |
|---|
| | 165 | |
|---|
| | 166 | get_public_keys( @search_strings ) |
|---|
| | 167 | get_secret_keys( @search_strings ) |
|---|
| | 168 | get_public_keys_with_sigs( @search_strings ) |
|---|
| | 169 | These methods create and return objects of the type GnuPG::Pubâ |
|---|
| | 170 | licKey or GnuPG::SecretKey respectively. This is done by parsing |
|---|
| | 171 | the output of GnuPG with the option wwiitthhââccoolloonnss enabled. The |
|---|
| | 172 | objects created do or do not have signature information stored in |
|---|
| | 173 | them, depending if the method ends in ___s_i_g_s; this separation of |
|---|
| | 174 | functionality is there because of performance hits when listing |
|---|
| | 175 | information with signatures. |
|---|
| | 176 | |
|---|
| | 177 | _t_e_s_t___d_e_f_a_u_l_t___k_e_y___p_a_s_s_p_h_r_a_s_e_(_) |
|---|
| | 178 | This method will return a true or false value, depending on whether |
|---|
| | 179 | GnuPG reports a good passphrase was entered while signing a short |
|---|
| | 180 | message using the values of the ppaasssspphhrraassee data member, and the |
|---|
| | 181 | default key specified in the ooppttiioonnss data member. |
|---|
| | 182 | |
|---|
| | 183 | IInnvvookkiinngg GGnnuuPPGG wwiitthh aa ccuussttoomm ccaallll |
|---|
| | 184 | GnuPG::Interface attempts to cover a lot of the commands of GnuPG that |
|---|
| | 185 | one would want to perform; however, there may be a lot more calls that |
|---|
| | 186 | GnuPG is and will be capable of, so a generic command interface is proâ |
|---|
| | 187 | vided, "wrap_call". |
|---|
| | 188 | |
|---|
| | 189 | wrap_call( %args ) |
|---|
| | 190 | Call GnuPG with a custom command. The %args hash must contain at |
|---|
| | 191 | least the following keys: |
|---|
| | 192 | |
|---|
| | 193 | commands |
|---|
| | 194 | The value of this key in the hash must be a reference to a a |
|---|
| | 195 | list of commands for GnuPG, such as "[ qw( ââencrypt ââsign ) |
|---|
| | 196 | ]". |
|---|
| | 197 | |
|---|
| | 198 | handles |
|---|
| | 199 | As with most other GnuPG::Interface methods, hhaannddlleess must be a |
|---|
| | 200 | GnuPG::Handles object. |
|---|
| | 201 | |
|---|
| | 202 | The following keys are optional. |
|---|
| | 203 | |
|---|
| | 204 | command_args |
|---|
| | 205 | As with other GnuPG::Interface methods, the value in hash for |
|---|
| | 206 | this key must be a reference to a list of arguments to be |
|---|
| | 207 | passed to the GnuPG command, such as which keys to list in a |
|---|
| | 208 | keyâlisting. |
|---|
| | 209 | |
|---|
| | 210 | OOBBJJEECCTT DDAATTAA MMEEMMBBEERRSS |
|---|
| | 211 | Note that these data members are interacted with via object methods |
|---|
| | 212 | created using the methods described in "get_set" in Class::MethodMaker, |
|---|
| | 213 | or "object" in Class::MethodMaker. Please read there for more informaâ |
|---|
| | 214 | tion. |
|---|
| | 215 | |
|---|
| | 216 | call |
|---|
| | 217 | This defines the call made to invoke GnuPG. Defaults to âgpgâ; |
|---|
| | 218 | this should be changed if âgpgâ is not in your path, or there is a |
|---|
| | 219 | different name for the binary on your system. |
|---|
| | 220 | |
|---|
| | 221 | passphrase |
|---|
| | 222 | In order to lessen the burden of using handles by the user of this |
|---|
| | 223 | package, setting this option to oneâs passphrase for a secret key |
|---|
| | 224 | will allow the package to enter the passphrase via a handle to |
|---|
| | 225 | GnuPG by itself instead of leaving this to the user. See also |
|---|
| | 226 | "passphrase" in GnuPG::Handles. |
|---|
| | 227 | |
|---|
| | 228 | options |
|---|
| | 229 | This data member, of the type GnuPG::Options; the setting stored in |
|---|
| | 230 | this data member are used to determine the options used when callâ |
|---|
| | 231 | ing GnuPG via _a_n_y of the object methods described in this package. |
|---|
| | 232 | See GnuPG::Options for more information. |
|---|
| | 233 | |
|---|
| | 234 | EEXXAAMMPPLLEESS |
|---|
| | 235 | The following setup can be done before any of the following examples: |
|---|
| | 236 | |
|---|
| | 237 | use IO::Handle; |
|---|
| | 238 | use GnuPG::Interface; |
|---|
| | 239 | |
|---|
| | 240 | my @original_plaintext = ( "How do you doo?" ); |
|---|
| | 241 | my $passphrase = "Three Little Pigs"; |
|---|
| | 242 | |
|---|
| | 243 | my $gnupg = GnuPG::Interfaceâ>new(); |
|---|
| | 244 | |
|---|
| | 245 | $gnupgâ>optionsâ>hash_init( armor => 1, |
|---|
| | 246 | recipients => [ âftobin@uiuc.eduâ, |
|---|
| | 247 | â0xABCD1234â ], |
|---|
| | 248 | meta_interactive( 0 ), |
|---|
| | 249 | ); |
|---|
| | 250 | |
|---|
| | 251 | EEnnccrryyppttiinngg |
|---|
| | 252 | |
|---|
| | 253 | # Weâll let the standard error of GnuPG pass through |
|---|
| | 254 | # to our own standard error, by not creating |
|---|
| | 255 | # a stderrâpart of the $handles object. |
|---|
| | 256 | my ( $input, $output ) = ( IO::Handleâ>new(), |
|---|
| | 257 | IO::Handleâ>new() ); |
|---|
| | 258 | |
|---|
| | 259 | my $handles = GnuPG::Handlesâ>new( stdin => $input, |
|---|
| | 260 | stdout => $output ); |
|---|
| | 261 | |
|---|
| | 262 | # this sets up the communication |
|---|
| | 263 | # Note that the recipients were specified earlier |
|---|
| | 264 | # in the âoptionsâ data member of the $gnupg object. |
|---|
| | 265 | my $pid = $gnupgâ>encrypt( handles => $handles ); |
|---|
| | 266 | |
|---|
| | 267 | # this passes in the plaintext |
|---|
| | 268 | print $input @original_plaintext; |
|---|
| | 269 | |
|---|
| | 270 | # this closes the communication channel, |
|---|
| | 271 | # indicating we are done |
|---|
| | 272 | close $input; |
|---|
| | 273 | |
|---|
| | 274 | my @ciphertext = <$output>; # reading the output |
|---|
| | 275 | |
|---|
| | 276 | waitpid $pid, 0; # clean up the finished GnuPG process |
|---|
| | 277 | |
|---|
| | 278 | SSiiggnniinngg |
|---|
| | 279 | |
|---|
| | 280 | # This time weâll catch the standard error for our perusing |
|---|
| | 281 | my ( $input, $output, $error ) = ( IO::Handleâ>new(), |
|---|
| | 282 | IO::Handleâ>new(), |
|---|
| | 283 | IO::Handleâ>new(), |
|---|
| | 284 | ); |
|---|
| | 285 | |
|---|
| | 286 | my $handles = GnuPG::Handlesâ>new( stdin => $input, |
|---|
| | 287 | stdout => $output, |
|---|
| | 288 | stderr => $error, |
|---|
| | 289 | ); |
|---|
| | 290 | |
|---|
| | 291 | # indicate our pasphrase through the |
|---|
| | 292 | # convience method |
|---|
| | 293 | $gnupgâ>passphrase( $passphrase ); |
|---|
| | 294 | |
|---|
| | 295 | # this sets up the communication |
|---|
| | 296 | my $pid = $gnupgâ>sign( handles => $handles ); |
|---|
| | 297 | |
|---|
| | 298 | # this passes in the plaintext |
|---|
| | 299 | print $input @original_plaintext; |
|---|
| | 300 | |
|---|
| | 301 | # this closes the communication channel, |
|---|
| | 302 | # indicating we are done |
|---|
| | 303 | close $input; |
|---|
| | 304 | |
|---|
| | 305 | my @ciphertext = <$output>; # reading the output |
|---|
| | 306 | my @error_output = <$error>; # reading the error |
|---|
| | 307 | |
|---|
| | 308 | close $output; |
|---|
| | 309 | close $error; |
|---|
| | 310 | |
|---|
| | 311 | waitpid $pid, 0; # clean up the finished GnuPG process |
|---|
| | 312 | |
|---|
| | 313 | DDeeccrryyppttiioonn |
|---|
| | 314 | |
|---|
| | 315 | # This time weâll catch the standard error for our perusing |
|---|
| | 316 | # as well as passing in the passphrase manually |
|---|
| | 317 | # as well as the status information given by GnuPG |
|---|
| | 318 | my ( $input, $output, $error, $passphrase_fh, $status_fh ) |
|---|
| | 319 | = ( IO::Handleâ>new(), |
|---|
| | 320 | IO::Handleâ>new(), |
|---|
| | 321 | IO::Handleâ>new(), |
|---|
| | 322 | IO::Handleâ>new(), |
|---|
| | 323 | IO::Handleâ>new(), |
|---|
| | 324 | ); |
|---|
| | 325 | |
|---|
| | 326 | my $handles = GnuPG::Handlesâ>new( stdin => $input, |
|---|
| | 327 | stdout => $output, |
|---|
| | 328 | stderr => $error, |
|---|
| | 329 | passphrase => $passphrase_fh, |
|---|
| | 330 | status => $status_fh, |
|---|
| | 331 | ); |
|---|
| | 332 | |
|---|
| | 333 | |
|---|