This file describes PGG, an Emacs interface to various PGP implementations.

Copyright © 2001, 2003, 2004, 2005, 2006, 2007  Free Software Foundation, Inc.

     Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts.  A copy of the license is included in the section entitled ``GNU Free Documentation License.''

PGG
***

This manual describes PGG.  PGG is an interface library between Emacs and various tools for secure communication.  PGG also provides a simple user interface to encrypt, decrypt, sign, and verify MIME messages.

Overview
********

PGG is an interface library between Emacs and various tools for secure communication.  Even though Mailcrypt has similar feature, it does not deal with detached PGP messages, normally used in PGP/MIME infrastructure.  This was the main reason why I wrote the new library.

PGP/MIME is an application of MIME Object Security Services (RFC1848). The standard is documented in RFC2015.

Prerequisites
*************

PGG requires at least one implementation of privacy guard system. This document assumes that you have already obtained and installed them and that you are familiar with its basic functions.

By default, PGG uses GnuPG.  If you are new to such a system, I recommend that you should look over the GNU Privacy Handbook (GPH) which is available at http://www.gnupg.org/documentation/.

When using GnuPG, we recommend the use of the gpg-agent program, which is distributed with versions 2.0 and later of GnuPG. This is a daemon to manage private keys independently from any protocol, and provides the most secure way to input and cache your passphrases (see Caching passphrase).  By default, PGG will attempt to use gpg-agent if it is running.  See Invoking GPG-AGENT (Using the GNU Privacy Guard).

PGG also supports Pretty Good Privacy version 2 or version 5.

How to use
**********

The toplevel interface of this library is quite simple, and only intended to use with public-key cryptographic operation.

To use PGG, evaluate following expression at the beginning of your application program.

     (require 'pgg)

If you want to check existence of pgg.el at runtime, instead you can list autoload setting for desired functions as follows.

     (autoload 'pgg-encrypt-region "pgg"
       "Encrypt the current region." t)
     (autoload 'pgg-encrypt-symmetric-region "pgg"
       "Encrypt the current region with symmetric algorithm." t)
     (autoload 'pgg-decrypt-region "pgg"
       "Decrypt the current region." t)
     (autoload 'pgg-sign-region "pgg"
       "Sign the current region." t)
     (autoload 'pgg-verify-region "pgg"
       "Verify the current region." t)
     (autoload 'pgg-insert-key "pgg"
       "Insert the ASCII armored public key." t)
     (autoload 'pgg-snarf-keys-region "pgg"
       "Import public keys in the current region." t)

User Commands
=============

At this time you can use some cryptographic commands.  The behavior of these commands relies on a fashion of invocation because they are also intended to be used as library functions.  In case you don't have the signer's public key, for example, the function pgg-verify-region fails immediately, but if the function had been called interactively, it would ask you to retrieve the signer's public key from the server.

 -- Command: pgg-encrypt-region start end recipients &optional sign passphrase
     Encrypt the current region between START and END for RECIPIENTS.  When the function were called interactively, you would be asked about the recipients.

     If encryption is successful, it replaces the current region contents (in the accessible portion) with the resulting data.

     If optional argument SIGN is non-nil, the function is request to do a combined sign and encrypt.  This currently is confirmed to work with GnuPG, but might not work with PGP or PGP5.

     If optional PASSPHRASE is nil, the passphrase will be obtained from the passphrase cache or user.

 -- Command: pgg-encrypt-symmetric-region &optional start end passphrase
     Encrypt the current region between START and END using a symmetric cipher.  After invocation you are asked for a passphrase.

     If optional PASSPHRASE is nil, the passphrase will be obtained from the passphrase cache or user.

     symmetric-cipher encryption is currently only implemented for GnuPG.

 -- Command: pgg-decrypt-region start end &optional passphrase
     Decrypt the current region between START and END.  If decryption is successful, it replaces the current region contents (in the accessible portion) with the resulting data.

     If optional PASSPHRASE is nil, the passphrase will be obtained from the passphrase cache or user.

 -- Command: pgg-sign-region start end &optional cleartext passphrase
     Make the signature from text between START and END.  If the optional third argument CLEARTEXT is non-nil, or the function is called interactively, it does not create a detached signature.  In such a case, it replaces the current region contents (in the accessible portion) with the resulting data.

     If optional PASSPHRASE is nil, the passphrase will be obtained from the passphrase cache or user.

 -- Command: pgg-verify-region start end &optional signature fetch
     Verify the current region between START and END.  If the optional third argument SIGNATURE is non-nil, it is treated as the detached signature file of the current region.

     If the optional 4th argument FETCH is non-nil, or the function is called interactively, we attempt to fetch the signer's public key from the key server.

 -- Command: pgg-insert-key
     Retrieve the user's public key and insert it as ASCII-armored format.

 -- Command: pgg-snarf-keys-region start end
     Collect public keys in the current region between START and END, and add them into the user's keyring.

Selecting an implementation
===========================

Since PGP has a long history and there are a number of PGP implementations available today, the function which each one has differs considerably.  For example, if you are using GnuPG, you know you can select cipher algorithm from 3DES, CAST5, BLOWFISH, and so on, but on the other hand the version 2 of PGP only supports IDEA.

Which implementation is used is controlled by the pgg-scheme variable.  If it is nil (the default), the value of the pgg-default-scheme variable will be used instead.

 -- Variable: pgg-scheme
     Force specify the scheme of PGP implementation.  The value can be set to gpg, pgp, and pgp5.  The default is nil.

 -- Variable: pgg-default-scheme
     The default scheme of PGP implementation.  The value should be one of gpg, pgp, and pgp5.  The default is gpg.

Caching passphrase
==================

When using GnuPG (gpg) as the PGP scheme, we recommend using a program called gpg-agent for entering and caching passphrases (Actually, gpg-agent does not cache passphrases but private keys.  On the other hand, from a user's point of view, this technical difference isn't visible.).

 -- Variable: pgg-gpg-use-agent
     If non-nil, attempt to use gpg-agent whenever possible. The default is t.  If gpg-agent is not running, or GnuPG is not the current PGP scheme, PGG's own passphrase-caching mechanism is used (see below).

To use gpg-agent with PGG, you must first ensure that gpg-agent is running.  For example, if you are running in the X Window System, you can do this by putting the following line in your .xsession file:

     eval "$(gpg-agent --daemon)"

For more details on invoking gpg-agent, see Invoking GPG-AGENT (Using the GNU Privacy Guard).

Whenever you perform a PGG operation that requires a GnuPG passphrase, GnuPG will contact gpg-agent, which prompts you for the passphrase.  Furthermore, gpg-agent ``caches'' the result, so that subsequent uses will not require you to enter the passphrase again.  (This cache usually expires after a certain time has passed; you can change this using the --default-cache-ttl option when invoking gpg-agent.)

If you are running in a X Window System environment, gpg-agent prompts for a passphrase by opening a graphical window.  However, if you are running Emacs on a text terminal, gpg-agent has trouble receiving input from the terminal, since it is being sent to Emacs. One workaround for this problem is to run gpg-agent on a different terminal from Emacs, with the --keep-tty option; this tells gpg-agent use its own terminal to prompt for passphrases.

When gpg-agent is not being used, PGG prompts for a passphrase through Emacs.  It also has its own passphrase caching mechanism, which is controlled by the variable pgg-cache-passphrase (see below).

There is a security risk in handling passphrases through PGG rather than gpg-agent.  When you enter your passphrase into an Emacs prompt, it is temporarily stored as a cleartext string in the memory of the Emacs executable.  If the executable memory is swapped to disk, the root user can, in theory, extract the passphrase from the swapfile.  Furthermore, the swapfile containing the cleartext passphrase might remain on the disk after the system is discarded or stolen.  gpg-agent avoids this problem by using certain tricks, such as memory locking, which have not been implemented in Emacs.

 -- Variable: pgg-cache-passphrase
     If non-nil, store passphrases.  The default value of this variable is t.  If you are worried about security issues, however, you could stop the caching of passphrases by setting this variable to nil.

 -- Variable: pgg-passphrase-cache-expiry
     Elapsed time for expiration in seconds.

If your passphrase contains non-ASCII characters, you might need to specify the coding system to be used to encode your passphrases, since GnuPG treats them as a byte sequence, not as a character sequence.

 -- Variable: pgg-passphrase-coding-system
     Coding system used to encode passphrase.

Default user identity
=====================

The PGP implementation is usually able to select the proper key to use for signing and decryption, but if you have more than one key, you may need to specify the key id to use.

 -- Variable: pgg-default-user-id
     User ID of your default identity.  It defaults to the value returned by (user-login-name).  You can customize this variable.

 -- Variable: pgg-gpg-user-id
     User ID of the GnuPG default identity.  It defaults to nil. This overrides pgg-default-user-id.  You can customize this variable.

 -- Variable: pgg-pgp-user-id
     User ID of the PGP 2.x/6.x default identity.  It defaults to nil.  This overrides pgg-default-user-id.  You can customize this variable.

 -- Variable: pgg-pgp5-user-id
     User ID of the PGP 5.x default identity.  It defaults to nil. This overrides pgg-default-user-id.  You can customize this variable.

Architecture
************

PGG introduces the notion of a "scheme of PGP implementation" (used interchangeably with "scheme" in this document).  This term refers to a singleton object wrapped with the luna object system.

Since PGG was designed for accessing and developing PGP functionality, the architecture had to be designed not just for interoperability but also for extensiblity.  In this chapter we explore the architecture while finding out how to write the PGG back end.

Initializing
============

A scheme must be initialized before it is used. It had better guarantee to keep only one instance of a scheme.

The following code is snipped out of pgg-gpg.el.  Once an instance of pgg-gpg scheme is initialized, it's stored to the variable pgg-scheme-gpg-instance and will be reused from now on.

     (defvar pgg-scheme-gpg-instance nil)

     (defun pgg-make-scheme-gpg ()
       (or pgg-scheme-gpg-instance
           (setq pgg-scheme-gpg-instance
     	    (luna-make-entity 'pgg-scheme-gpg))))

The name of the function must follow the regulation---pgg-make-scheme- follows the back end name.

Back end methods
================

In each back end, these methods must be present.  The output of these methods is stored in special buffers (Getting output), so that these methods must tell the status of the execution.

 -- Method: pgg-scheme-lookup-key scheme string &optional type
     Return keys associated with STRING.  If the optional third argument TYPE is non-nil, it searches from the secret keyrings.

 -- Method: pgg-scheme-encrypt-region scheme start end recipients &optional sign passphrase
     Encrypt the current region between START and END for RECIPIENTS.  If SIGN is non-nil, do a combined sign and encrypt.  If encryption is successful, it returns t, otherwise nil.

 -- Method: pgg-scheme-encrypt-symmetric-region scheme start end &optional passphrase
     Encrypt the current region between START and END using a symmetric cipher and a passphrases.  If encryption is successful, it returns t, otherwise nil.  This function is currently only implemented for GnuPG.

 -- Method: pgg-scheme-decrypt-region scheme start end &optional passphrase
     Decrypt the current region between START and END.  If decryption is successful, it returns t, otherwise nil.

 -- Method: pgg-scheme-sign-region scheme start end &optional cleartext passphrase
     Make the signature from text between START and END.  If the optional third argument CLEARTEXT is non-nil, it does not create a detached signature.  If signing is successful, it returns t, otherwise nil.

 -- Method: pgg-scheme-verify-region scheme start end &optional signature
     Verify the current region between START and END.  If the optional third argument SIGNATURE is non-nil, it is treated as the detached signature of the current region.  If the signature is successfully verified, it returns t, otherwise nil.

 -- Method: pgg-scheme-insert-key scheme
     Retrieve the user's public key and insert it as ASCII-armored format. On success, it returns t, otherwise nil.

 -- Method: pgg-scheme-snarf-keys-region scheme start end
     Collect public keys in the current region between START and END, and add them into the user's keyring. On success, it returns t, otherwise nil.

Getting output
==============

The output of the back end methods (Back end methods) is stored in special buffers, so that these methods must tell the status of the execution.

 -- Variable: pgg-errors-buffer
     The standard error output of the execution of the PGP command is stored here.

 -- Variable: pgg-output-buffer
     The standard output of the execution of the PGP command is stored here.

 -- Variable: pgg-status-buffer
     The rest of status information of the execution of the PGP command is stored here.

Parsing OpenPGP packets
***********************

The format of OpenPGP messages is maintained in order to publish all necessary information needed to develop interoperable applications. The standard is documented in RFC 2440.

PGG has its own parser for the OpenPGP packets.

 -- Function: pgg-parse-armor string
     List the sequence of packets in STRING.

 -- Function: pgg-parse-armor-region start end
     List the sequence of packets in the current region between START and END.

 -- Variable: pgg-ignore-packet-checksum
     If non-nil, don't check the checksum of the packets.