* epg.el (epg-make-context): New slot "operation".
[elisp/epg.git] / epg.el
diff --git a/epg.el b/epg.el
index bcf3675..18ef4b9 100644 (file)
--- a/epg.el
+++ b/epg.el
@@ -91,7 +91,7 @@
     (2 . "ZLIB")
     (3 . "BZIP2")))
 
-(defconst epg-invalid-recipients-alist
+(defconst epg-invalid-recipients-reason-alist
   '((0 . "No specific reason given")
     (1 . "Not Found")
     (2 . "Ambigious specification")
     (9 . "Not a secret key")
     (10 . "Key not trusted")))
 
-(defconst epg-delete-problem-alist
+(defconst epg-delete-problem-reason-alist
   '((1 . "No such key")
     (2 . "Must delete secret key first")
     (3 . "Ambigious specification")))
 
+(defconst epg-import-ok-reason-alist
+  '((0 . "Not actually changed")
+    (1 . "Entirely new key")
+    (2 . "New user IDs")
+    (4 . "New signatures")
+    (8 . "New subkeys")
+    (16 . "Contains private key")))
+
+(defconst epg-import-problem-reason-alist
+  '((0 . "No specific reason given")
+    (1 . "Invalid Certificate")
+    (2 . "Issuer Certificate missing")
+    (3 . "Certificate Chain too long")
+    (4 . "Error storing certificate")))
+
+(defconst epg-no-data-reason-alist
+  '((1 . "No armored data")
+    (2 . "Expected a packet but did not found one")
+    (3 . "Invalid packet found, this may indicate a non OpenPGP message")
+    (4 . "Signature expected but not found")))
+
+(defconst epg-unexpected-reason-alist nil)
+
 (defvar epg-key-validity-alist
   '((?o . unknown)
     (?i . invalid)
     (?c . certify)
     (?a . authentication)))
 
+(defvar epg-new-signature-type-alist
+  '((?D . detached)
+    (?C . clear)
+    (?S . normal)))
+
 (defvar epg-dn-type-alist
   '(("1.2.840.113549.1.9.1" . "EMail")
     ("2.5.4.12" . "T")
                cipher-algorithm digest-algorithm compress-algorithm
                #'epg-passphrase-callback-function
                #'epg-progress-callback-function
-               nil nil nil nil)))
+               nil nil nil nil nil)))
 
 (defun epg-context-protocol (context)
   "Return the protocol used within CONTEXT."
@@ -255,6 +283,12 @@ This function is for internal use only."
     (signal 'wrong-type-argument (list 'epg-context-p context)))
   (aref (cdr context) 12))
 
+(defun epg-context-operation (context)
+  "Return the name of the current cryptographic operation."
+  (unless (eq (car context) 'epg-context)
+    (signal 'wrong-type-argument (list 'epg-context-p context)))
+  (aref (cdr context) 13))
+
 (defun epg-context-set-protocol (context protocol)
   "Set the protocol used within CONTEXT."
   (unless (eq (car context) 'epg-context)
@@ -336,9 +370,15 @@ This function is for internal use only."
     (signal 'wrong-type-argument (list 'epg-context-p context)))
   (aset (cdr context) 12 result))
 
+(defun epg-context-set-operation (context operation)
+  "Set the name of the current cryptographic operation."
+  (unless (eq (car context) 'epg-context)
+    (signal 'wrong-type-argument (list 'epg-context-p context)))
+  (aset (cdr context) 13 operation))
+
 (defun epg-make-signature (status &optional key-id)
   "Return a signature object."
-  (cons 'epg-signature (vector status key-id nil nil nil nil nil nil)))
+  (cons 'epg-signature (vector status key-id nil nil nil nil nil nil nil nil)))
 
 (defun epg-signature-status (signature)
   "Return the status code of SIGNATURE."
@@ -388,6 +428,18 @@ This function is for internal use only."
     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
   (aref (cdr signature) 7))
 
+(defun epg-signature-class (signature)
+  "Return the class of SIGNATURE."
+  (unless (eq (car signature) 'epg-signature)
+    (signal 'wrong-type-argument (list 'epg-signature-p signature)))
+  (aref (cdr signature) 8))
+
+(defun epg-signature-version (signature)
+  "Return the version of SIGNATURE."
+  (unless (eq (car signature) 'epg-signature)
+    (signal 'wrong-type-argument (list 'epg-signature-p signature)))
+  (aref (cdr signature) 9))
+
 (defun epg-signature-set-status (signature status)
  "Set the status code of SIGNATURE."
   (unless (eq (car signature) 'epg-signature)
@@ -436,6 +488,60 @@ This function is for internal use only."
     (signal 'wrong-type-argument (list 'epg-signature-p signature)))
   (aset (cdr signature) 7 digest-algorithm))
 
+(defun epg-signature-set-class (signature class)
+  "Set the class of SIGNATURE."
+  (unless (eq (car signature) 'epg-signature)
+    (signal 'wrong-type-argument (list 'epg-signature-p signature)))
+  (aset (cdr signature) 8 class))
+
+(defun epg-signature-set-version (signature version)
+  "Set the version of SIGNATURE."
+  (unless (eq (car signature) 'epg-signature)
+    (signal 'wrong-type-argument (list 'epg-signature-p signature)))
+  (aset (cdr signature) 9 version))
+
+(defun epg-make-new-signature (type pubkey-algorithm digest-algorithm
+                                   class creation-time fingerprint)
+  "Return a new signature object."
+  (cons 'epg-new-signature (vector type pubkey-algorithm digest-algorithm
+                                  class creation-time fingerprint)))
+
+(defun epg-new-signature-type (new-signature)
+  "Return the type of NEW-SIGNATURE."
+  (unless (eq (car new-signature) 'epg-new-signature)
+    (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
+  (aref (cdr new-signature) 0))
+
+(defun epg-new-signature-pubkey-algorithm (new-signature)
+  "Return the public key algorithm of NEW-SIGNATURE."
+  (unless (eq (car new-signature) 'epg-new-signature)
+    (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
+  (aref (cdr new-signature) 1))
+
+(defun epg-new-signature-digest-algorithm (new-signature)
+  "Return the digest algorithm of NEW-SIGNATURE."
+  (unless (eq (car new-signature) 'epg-new-signature)
+    (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
+  (aref (cdr new-signature) 2))
+
+(defun epg-new-signature-class (new-signature)
+  "Return the class of NEW-SIGNATURE."
+  (unless (eq (car new-signature) 'epg-new-signature)
+    (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
+  (aref (cdr new-signature) 3))
+
+(defun epg-new-signature-creation-time (new-signature)
+  "Return the creation time of NEW-SIGNATURE."
+  (unless (eq (car new-signature) 'epg-new-signature)
+    (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
+  (aref (cdr new-signature) 4))
+
+(defun epg-new-signature-fingerprint (new-signature)
+  "Return the fingerprint of NEW-SIGNATURE."
+  (unless (eq (car new-signature) 'epg-new-signature)
+    (signal 'wrong-type-argument (list 'epg-new-signature-p new-signature)))
+  (aref (cdr new-signature) 5))
+
 (defun epg-make-key (owner-trust)
   "Return a key object."
   (cons 'epg-key (vector owner-trust nil nil)))
@@ -470,11 +576,11 @@ This function is for internal use only."
     (signal 'wrong-type-argument (list 'epg-key-p key)))
   (aset (cdr key) 2 user-id-list))
 
-(defun epg-make-sub-key (validity capability secret algorithm length id
+(defun epg-make-sub-key (validity capability secret-p algorithm length id
                                  creation-time expiration-time)
   "Return a sub key object."
   (cons 'epg-sub-key
-       (vector validity capability secret algorithm length id creation-time
+       (vector validity capability secret-p algorithm length id creation-time
                expiration-time nil)))
 
 (defun epg-sub-key-validity (sub-key)
@@ -489,7 +595,7 @@ This function is for internal use only."
     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
   (aref (cdr sub-key) 1))
 
-(defun epg-sub-key-secret (sub-key)
+(defun epg-sub-key-secret-p (sub-key)
   "Return non-nil if SUB-KEY is a secret key."
   (unless (eq (car sub-key) 'epg-sub-key)
     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
@@ -538,9 +644,9 @@ This function is for internal use only."
     (signal 'wrong-type-argument (list 'epg-sub-key-p sub-key)))
   (aset (cdr sub-key) 8 fingerprint))
 
-(defun epg-make-user-id (validity name)
+(defun epg-make-user-id (validity string)
   "Return a user ID object."
-  (cons 'epg-user-id (vector validity name nil)))
+  (cons 'epg-user-id (vector validity string nil)))
 
 (defun epg-user-id-validity (user-id)
   "Return the validity of USER-ID."
@@ -548,7 +654,7 @@ This function is for internal use only."
     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
   (aref (cdr user-id) 0))
 
-(defun epg-user-id-name (user-id)
+(defun epg-user-id-string (user-id)
   "Return the name of USER-ID."
   (unless (eq (car user-id) 'epg-user-id)
     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
@@ -566,10 +672,68 @@ This function is for internal use only."
     (signal 'wrong-type-argument (list 'epg-user-id-p user-id)))
   (aset (cdr user-id) 2 signature-list))
 
+(defun epg-make-key-signature (validity pubkey-algorithm key-id creation-time
+                                       expiration-time user-id class
+                                       exportable-p)
+  "Return a key signature object."
+  (cons 'epg-key-signature
+       (vector validity pubkey-algorithm key-id creation-time expiration-time
+               user-id class exportable-p)))
+
+(defun epg-key-signature-validity (key-signature)
+  "Return the validity of KEY-SIGNATURE."
+  (unless (eq (car key-signature) 'epg-key-signature)
+    (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
+  (aref (cdr key-signature) 0))
+
+(defun epg-key-signature-pubkey-algorithm (key-signature)
+  "Return the public key algorithm of KEY-SIGNATURE."
+  (unless (eq (car key-signature) 'epg-key-signature)
+    (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
+  (aref (cdr key-signature) 1))
+
+(defun epg-key-signature-key-id (key-signature)
+  "Return the key-id of KEY-SIGNATURE."
+  (unless (eq (car key-signature) 'epg-key-signature)
+    (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
+  (aref (cdr key-signature) 2))
+
+(defun epg-key-signature-creation-time (key-signature)
+  "Return the creation time of KEY-SIGNATURE."
+  (unless (eq (car key-signature) 'epg-key-signature)
+    (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
+  (aref (cdr key-signature) 3))
+
+(defun epg-key-signature-expiration-time (key-signature)
+  "Return the expiration time of KEY-SIGNATURE."
+  (unless (eq (car key-signature) 'epg-key-signature)
+    (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
+  (aref (cdr key-signature) 4))
+
+(defun epg-key-signature-user-id (key-signature)
+  "Return the user-id of KEY-SIGNATURE."
+  (unless (eq (car key-signature) 'epg-key-signature)
+    (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
+  (aref (cdr key-signature) 5))
+
+(defun epg-key-signature-class (key-signature)
+  "Return the class of KEY-SIGNATURE."
+  (unless (eq (car key-signature) 'epg-key-signature)
+    (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
+  (aref (cdr key-signature) 6))
+
+(defun epg-key-signature-exportable-p (key-signature)
+  "Return t if KEY-SIGNATURE is exportable."
+  (unless (eq (car key-signature) 'epg-key-signature)
+    (signal 'wrong-type-argument (list 'epg-key-signature-p key-signature)))
+  (aref (cdr key-signature) 7))
+
 (defun epg-context-result-for (context name)
+  "Return the result of CONTEXT associated with NAME."
   (cdr (assq name (epg-context-result context))))
 
 (defun epg-context-set-result-for (context name value)
+  "Set the result of CONTEXT associated with NAME to VALUE."
   (let* ((result (epg-context-result context))
         (entry (assq name result)))
     (if entry
@@ -577,15 +741,16 @@ This function is for internal use only."
       (epg-context-set-result context (cons (cons name value) result)))))
 
 (defun epg-signature-to-string (signature)
+  "Convert SIGNATURE to a human readable string."
   (let ((user-id (cdr (assoc (epg-signature-key-id signature)
                             epg-user-id-alist))))
     (concat
      (cond ((eq (epg-signature-status signature) 'good)
-           "Good signature ")
+           "Good signature from ")
           ((eq (epg-signature-status signature) 'bad)
-           "Bad signature ")
+           "Bad signature from ")
           ((eq (epg-signature-status signature) 'expired)
-           "Expired signature ")
+           "Expired signature from ")
           ((eq (epg-signature-status signature) 'expired-key)
            "Signature made by expired key ")
           ((eq (epg-signature-status signature) 'revoked-key)
@@ -593,9 +758,9 @@ This function is for internal use only."
           ((eq (epg-signature-status signature) 'no-pubkey)
            "No public key for "))
      (epg-signature-key-id signature)
+     " "
      (if user-id
-        (concat " from "
-                (if (stringp user-id)
+        (concat (if (stringp user-id)
                     user-id
                   (epg-decode-dn user-id))
                 " ")
@@ -605,10 +770,35 @@ This function is for internal use only."
        ""))))
 
 (defun epg-verify-result-to-string (verify-result)
+  "Convert VERIFY-RESULT to a human readable string."
   (mapconcat #'epg-signature-to-string verify-result "\n"))
 
+(defun epg-new-signature-to-string (new-signature)
+  "Convert NEW-SIGNATURE to a human readable string."
+  (concat
+   (cond ((eq (epg-new-signature-type new-signature) 'detached)
+         "Detached signature ")
+        ((eq (epg-new-signature-type new-signature) 'clear)
+         "Clear text signature ")
+        (t
+         "Signature "))
+   (cdr (assq (epg-new-signature-pubkey-algorithm new-signature)
+             epg-pubkey-algorithm-alist))
+   "/"
+   (cdr (assq (epg-new-signature-digest-algorithm new-signature)
+             epg-digest-algorithm-alist))
+   " "
+   (format "%02X " (epg-new-signature-class new-signature))
+   (epg-new-signature-fingerprint new-signature)))
+
 (defun epg-start (context args)
   "Start `epg-gpg-program' in a subprocess with given ARGS."
+  (if (and (epg-context-process context)
+          (eq (process-status (epg-context-process context)) 'run))
+      (error "%s is already running in this context"
+            (if (eq (epg-context-protocol context) 'CMS)
+                epg-gpgsm-program
+              epg-gpg-program)))
   (let* ((args (append (list "--no-tty"
                             "--status-fd" "1"
                             "--yes")
@@ -687,17 +877,17 @@ This function is for internal use only."
        (setq epg-read-point (point)))))
 
 (defun epg-read-output (context)
+  "Read the output file CONTEXT and return the content as a string."
   (with-temp-buffer
     (if (fboundp 'set-buffer-multibyte)
        (set-buffer-multibyte nil))
     (if (file-exists-p (epg-context-output-file context))
-       (let ((coding-system-for-read (if (epg-context-textmode context)
-                                         'raw-text
-                                       'binary)))
+       (let ((coding-system-for-read 'binary))
          (insert-file-contents (epg-context-output-file context))
          (buffer-string)))))
 
 (defun epg-wait-for-status (context status-list)
+  "Wait until one of elements in STATUS-LIST arrives."
   (with-current-buffer (process-buffer (epg-context-process context))
     (setq epg-pending-status-list status-list)
     (while (and (eq (process-status (epg-context-process context)) 'run)
@@ -705,22 +895,19 @@ This function is for internal use only."
       (accept-process-output (epg-context-process context) 1))))
 
 (defun epg-wait-for-completion (context)
+  "Wait until the `epg-gpg-program' process completes."
   (while (eq (process-status (epg-context-process context)) 'run)
-    ;; We can't use accept-process-output instead of sit-for here
-    ;; because it may cause an interrupt during the sentinel execution.
-    (sit-for 0.1)))
-
-(defun epg-flush (context)
-  (if (eq (process-status (epg-context-process context)) 'run)
-      (process-send-eof (epg-context-process context))))
+    (accept-process-output (epg-context-process context) 1)))
 
 (defun epg-reset (context)
+  "Reset the CONTEXT."
   (if (and (epg-context-process context)
           (buffer-live-p (process-buffer (epg-context-process context))))
       (kill-buffer (process-buffer (epg-context-process context))))
   (epg-context-set-process context nil))
 
 (defun epg-delete-output-file (context)
+  "Delete the output file of CONTEXT."
   (if (and (epg-context-output-file context)
           (file-exists-p (epg-context-output-file context)))
       (delete-file (epg-context-output-file context))))
@@ -775,7 +962,7 @@ This function is for internal use only."
              (quit
               (epg-context-set-result-for
                epg-context 'error
-               (cons 'quit
+               (cons '(quit)
                      (epg-context-result-for epg-context 'error)))
               (delete-process process)))
          (if passphrase
@@ -793,7 +980,7 @@ This function is for internal use only."
       (quit
        (epg-context-set-result-for
        epg-context 'error
-       (cons 'quit
+       (cons '(quit)
              (epg-context-result-for epg-context 'error)))
        (delete-process process)))))
 
@@ -808,11 +995,11 @@ This function is for internal use only."
       (quit
        (epg-context-set-result-for
        epg-context 'error
-       (cons 'quit
+       (cons '(quit)
              (epg-context-result-for epg-context 'error)))
        (delete-process process)))))
 
-(defun epg-signature-status-internal (status string)
+(defun epg-status-*SIG (status string)
   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
       (let* ((key-id (match-string 1 string))
             (user-id (match-string 2 string))
@@ -837,19 +1024,19 @@ This function is for internal use only."
           (epg-context-result-for epg-context 'verify)))))
 
 (defun epg-status-GOODSIG (process string)
-  (epg-signature-status-internal 'good string))
+  (epg-status-*SIG 'good string))
 
 (defun epg-status-EXPSIG (process string)
-  (epg-signature-status-internal 'expired string))
+  (epg-status-*SIG 'expired string))
 
 (defun epg-status-EXPKEYSIG (process string)
-  (epg-signature-status-internal 'expired-key string))
+  (epg-status-*SIG 'expired-key string))
 
 (defun epg-status-REVKEYSIG (process string)
-  (epg-signature-status-internal 'revoked-key string))
+  (epg-status-*SIG 'revoked-key string))
 
 (defun epg-status-BADSIG (process string)
-  (epg-signature-status-internal 'bad string))
+  (epg-status-*SIG 'bad string))
 
 (defun epg-status-NO_PUBKEY (process string)
   (epg-context-set-result-for
@@ -859,7 +1046,7 @@ This function is for internal use only."
           (epg-context-result-for epg-context 'verify))))
 
 (defun epg-status-ERRSIG (process string)
-  (let ((signatures (car (epg-context-result-for epg-context 'verify))))
+  (let ((signatures (epg-context-result-for epg-context 'verify)))
     (unless signatures
       (setq signatures (list (epg-make-signature 'error)))
       (epg-context-set-result-for epg-context 'verify signatures))
@@ -876,9 +1063,9 @@ This function is for internal use only."
       (epg-signature-set-digest-algorithm
        (car signatures)
        (string-to-number (match-string 3 string)))
-;      (epg-signature-set-class
-;       (car signatures)
-;       (string-to-number (match-string 4 string) 16))
+      (epg-signature-set-class
+       (car signatures)
+       (string-to-number (match-string 4 string) 16))
       (epg-signature-set-creation-time
        (car signatures)
        (match-string 5 string)))))
@@ -900,19 +1087,18 @@ This function is for internal use only."
       (epg-signature-set-expiration-time
        signature
        (match-string 3 string))
-;      (epg-signature-set-version
-;       signature
-;       (string-to-number (match-string 4 string)))
+      (epg-signature-set-version
+       signature
+       (string-to-number (match-string 4 string)))
       (epg-signature-set-pubkey-algorithm
        signature 
        (string-to-number (match-string 5 string)))
       (epg-signature-set-digest-algorithm
        signature
        (string-to-number (match-string 6 string)))
-;      (epg-signature-set-class
-;       signature
-;       (string-to-number (match-string 7 string) 16))
-      )))
+      (epg-signature-set-class
+       signature
+       (string-to-number (match-string 7 string) 16)))))
 
 (defun epg-status-TRUST_UNDEFINED (process string)
   (let ((signature (car (epg-context-result-for epg-context 'verify))))
@@ -961,37 +1147,37 @@ This function is for internal use only."
 (defun epg-status-DECRYPTION_FAILED (process string)
   (epg-context-set-result-for
    epg-context 'error
-   (cons 'decryption-failed
+   (cons '(decryption-failed)
         (epg-context-result-for epg-context 'error))))
 
 (defun epg-status-NODATA (process string)
   (epg-context-set-result-for
    epg-context 'error
-   (cons (cons 'no-data (string-to-number string))
+   (cons (list 'no-data (cons 'reason (string-to-number string)))
         (epg-context-result-for epg-context 'error))))
 
 (defun epg-status-UNEXPECTED (process string)
   (epg-context-set-result-for
    epg-context 'error
-   (cons (cons 'unexpected (string-to-number string))
+   (cons (list 'unexpected (cons 'reason (string-to-number string)))
         (epg-context-result-for epg-context 'error))))
 
 (defun epg-status-KEYEXPIRED (process string)
   (epg-context-set-result-for
    epg-context 'error
-   (cons (cons 'key-expired string)
+   (cons (list 'key-expired (cons 'expiration-time string))
         (epg-context-result-for epg-context 'error))))
 
 (defun epg-status-KEYREVOKED (process string)
   (epg-context-set-result-for
    epg-context 'error
-   (cons 'key-revoked
+   (cons '(key-revoked)
         (epg-context-result-for epg-context 'error))))
 
 (defun epg-status-BADARMOR (process string)
   (epg-context-set-result-for
    epg-context 'error
-   (cons 'bad-armor
+   (cons '(bad-armor)
         (epg-context-result-for epg-context 'error))))
 
 (defun epg-status-INV_RECP (process string)
@@ -999,21 +1185,24 @@ This function is for internal use only."
       (epg-context-set-result-for
        epg-context 'error
        (cons (list 'invalid-recipient
-                  (string-to-number (match-string 1 string))
-                  (match-string 2 string))
+                  (cons 'reason
+                        (string-to-number (match-string 1 string)))
+                  (cons 'requested-recipient
+                        (match-string 2 string)))
             (epg-context-result-for epg-context 'error)))))
 
 (defun epg-status-NO_RECP (process string)
   (epg-context-set-result-for
    epg-context 'error
-   (cons 'no-recipients
+   (cons '(no-recipients)
         (epg-context-result-for epg-context 'error))))
 
 (defun epg-status-DELETE_PROBLEM (process string)
   (if (string-match "\\`\\([0-9]+\\)" string)
       (epg-context-set-result-for
        epg-context 'error
-       (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
+       (cons (list 'delete-problem
+                  (cons 'reason (string-to-number (match-string 1 string))))
             (epg-context-result-for epg-context 'error)))))
 
 (defun epg-status-SIG_CREATED (process string)
@@ -1021,22 +1210,78 @@ This function is for internal use only."
 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
       (epg-context-set-result-for
        epg-context 'sign
-       (cons (list (cons 'type (string-to-char (match-string 1 string)))
-                  (cons 'pubkey-algorithm
-                        (string-to-number (match-string 2 string)))
-                  (cons 'digest-algorithm
-                        (string-to-number (match-string 3 string)))
-                  (cons 'class (string-to-number (match-string 4 string) 16))
-                  (cons 'creation-time (match-string 5 string))
-                  (cons 'fingerprint (substring string (match-end 0))))
+       (cons (epg-make-new-signature
+             (cdr (assq (aref (match-string 1 string) 0)
+                        epg-new-signature-type-alist))
+             (string-to-number (match-string 2 string))
+             (string-to-number (match-string 3 string))
+             (string-to-number (match-string 4 string) 16)
+             (match-string 5 string)
+             (substring string (match-end 0)))
             (epg-context-result-for epg-context 'sign)))))
 
+(defun epg-status-KEY_CREATED (process string)
+  (if (string-match "\\`\\([BPS]\\) \\([^ ]+\\)" string)
+      (epg-context-set-result-for
+       epg-context 'generate-key
+       (cons (list (cons 'type (string-to-char (match-string 1 string)))
+                  (cons 'fingerprint (match-string 2 string)))
+            (epg-context-result-for epg-context 'generate-key)))))
+
+(defun epg-status-KEY_NOT_CREATED (process string)
+  (epg-context-set-result-for
+   epg-context 'error
+   (cons '(key-not-created)
+        (epg-context-result-for epg-context 'error))))
+
+(defun epg-status-IMPORTED (process string)
+  (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
+      (let* ((key-id (match-string 1 string))
+            (user-id (match-string 2 string))
+            (entry (assoc key-id epg-user-id-alist)))
+       (if entry
+           (setcdr entry user-id)
+         (setq epg-user-id-alist (cons (cons key-id user-id)
+                                       epg-user-id-alist)))
+       (epg-context-set-result-for
+        epg-context 'import
+        (cons (list (cons 'key-id key-id)
+                    (cons 'user-id user-id))
+              (epg-context-result-for epg-context 'import))))))
+
+(defun epg-status-IMPORT_OK (process string)
+  (let ((result (epg-context-result-for epg-context 'import)))
+    (if (and result
+            (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string))
+       (setcar result
+               (append (list (cons 'reason
+                                   (string-to-number
+                                    (match-string 1 string))))
+                       (if (match-beginning 2)
+                           (list (cons 'fingerprint
+                                       (match-string 3 string))))
+                       (car result))))))
+
+(defun epg-status-IMPORT_PROBLEM (process string)
+  (if (string-match "\\`\\([0-9]+\\)\\( \\(.+\\)\\)?" string)
+      (epg-context-set-result-for
+       epg-context 'error
+       (cons (cons 'import-problem
+                  (append (list (cons 'reason
+                                      (string-to-number
+                                       (match-string 1 string))))
+                          (if (match-beginning 2)
+                              (list (cons 'fingerprint
+                                          (match-string 3 string))))))
+            (epg-context-result-for epg-context 'error)))))
+
 (defun epg-passphrase-callback-function (context key-id handback)
-  (read-passwd
-   (if (eq key-id 'SYM)
-       "Passphrase for symmetric encryption: "
+  (if (eq key-id 'SYM)
+      (read-passwd "Passphrase for symmetric encryption: "
+                  (eq (epg-context-operation context) 'encrypt))
+    (read-passwd
      (if (eq key-id 'PIN)
-        "Passphrase for PIN: "
+       "Passphrase for PIN: "
        (let ((entry (assoc key-id epg-user-id-alist)))
         (if entry
             (format "Passphrase for %s %s: " key-id (cdr entry))
@@ -1070,11 +1315,11 @@ This function is for internal use only."
   (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
                            "--with-fingerprint"
                            "--with-fingerprint"
-                           (if (or (eq mode t) (eq mode 'secret))
+                           (if (memq mode '(t secret))
                                "--list-secret-keys"
-                             (if mode
-                                 "--list-sigs"
-                               "--list-keys")))
+                             (if (memq mode '(nil public))
+                                 "--list-keys"
+                               "--list-sigs")))
                      (unless (eq (epg-context-protocol context) 'CMS)
                        '("--fixed-list-mode"))
                      (if name (list name))))
@@ -1112,24 +1357,18 @@ This function is for internal use only."
    (aref line 5)
    (aref line 6)))
 
+;;;###autoload
 (defun epg-list-keys (context &optional name mode)
   "Return a list of epg-key objects matched with NAME.
-If MODE is nil, only public keyring should be searched.
+If MODE is nil or 'public, only public keyring should be searched.
 If MODE is t or 'secret, only secret keyring should be searched. 
 Otherwise, only public keyring should be searched and the key
 signatures should be included."
   (let ((lines (epg-list-keys-1 context name mode))
-       keys cert)
+       keys cert pointer pointer-1)
     (while lines
       (cond
        ((member (aref (car lines) 0) '("pub" "sec" "crt" "crs"))
-       (when (car keys)
-         (epg-key-set-sub-key-list
-          (car keys)
-          (nreverse (epg-key-sub-key-list (car keys))))
-         (epg-key-set-user-id-list
-          (car keys)
-          (nreverse (epg-key-user-id-list (car keys)))))
        (setq cert (member (aref (car lines) 0) '("crt" "crs"))
              keys (cons (epg-make-key
                          (if (aref (car lines) 8)
@@ -1160,19 +1399,45 @@ signatures should be included."
               (epg-key-user-id-list (car keys)))))
        ((equal (aref (car lines) 0) "fpr")
        (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
-                                    (aref (car lines) 9))))
+                                    (aref (car lines) 9)))
+       ((equal (aref (car lines) 0) "sig")
+       (epg-user-id-set-signature-list
+        (car (epg-key-user-id-list (car keys)))
+        (cons
+         (epg-make-key-signature
+          (if (aref (car lines) 1)
+              (cdr (assq (string-to-char (aref (car lines) 1))
+                         epg-key-validity-alist)))
+          (string-to-number (aref (car lines) 3))
+          (aref (car lines) 4)
+          (aref (car lines) 5)
+          (aref (car lines) 6)
+          (aref (car lines) 9)
+          (string-to-number (aref (car lines) 10) 16)
+          (eq (aref (aref (car lines) 10) 2) ?x))
+         (epg-user-id-signature-list
+          (car (epg-key-user-id-list (car keys))))))))
       (setq lines (cdr lines)))
-    (when (car keys)
+    (setq keys (nreverse keys)
+         pointer keys)
+    (while pointer
       (epg-key-set-sub-key-list
-       (car keys)
-       (nreverse (epg-key-sub-key-list (car keys))))
-      (epg-key-set-user-id-list
-       (car keys)
-       (nreverse (epg-key-user-id-list (car keys)))))
-    (nreverse keys)))
+       (car pointer)
+       (nreverse (epg-key-sub-key-list (car pointer))))
+      (setq pointer-1 (epg-key-set-user-id-list
+                         (car pointer)
+                         (nreverse (epg-key-user-id-list (car pointer)))))
+      (while pointer-1
+       (epg-user-id-set-signature-list
+        (car pointer-1)
+        (nreverse (epg-user-id-signature-list (car pointer-1))))
+       (setq pointer-1 (cdr pointer-1)))
+      (setq pointer (cdr pointer)))
+    keys))
 
 (if (fboundp 'make-temp-file)
     (defalias 'epg-make-temp-file 'make-temp-file)
+  (defvar temporary-file-directory)
   ;; stolen from poe.el.
   (defun epg-make-temp-file (prefix)
     "Create a temporary file.
@@ -1181,6 +1446,10 @@ of PREFIX, and expanding against `temporary-file-directory' if necessary),
 is guaranteed to point to a newly created empty file.
 You can then use `write-region' to write new data into the file."
     (let (tempdir tempfile)
+      (setq prefix (expand-file-name prefix
+                                    (if (featurep 'xemacs)
+                                        (temp-directory)
+                                      temporary-file-directory)))
       (unwind-protect
          (let (file)
            ;; First, create a temporary directory.
@@ -1222,6 +1491,13 @@ You can then use `write-region' to write new data into the file."
 
 ;;;###autoload
 (defun epg-cancel (context)
+  (if (buffer-live-p (process-buffer (epg-context-process context)))
+      (save-excursion
+       (set-buffer (process-buffer (epg-context-process context)))
+       (epg-context-set-result-for
+        epg-context 'error
+        (cons '(quit)
+              (epg-context-result-for epg-context 'error)))))
   (if (eq (process-status (epg-context-process context)) 'run)
       (delete-process (epg-context-process context))))
   
@@ -1237,6 +1513,7 @@ If you are unsure, use synchronous version of this function
 `epg-decrypt-file' or `epg-decrypt-string' instead."
   (unless (epg-data-file cipher)
     (error "Not a file"))
+  (epg-context-set-operation context 'decrypt)
   (epg-context-set-result context nil)
   (epg-start context (list "--decrypt" (epg-data-file cipher)))
   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
@@ -1275,7 +1552,6 @@ If PLAIN is nil, it returns the result as a string."
          (epg-context-set-output-file context
                                       (epg-make-temp-file "epg-output"))
          (epg-start-decrypt context (epg-make-data-from-file input-file))
-         (epg-flush context)
          (epg-wait-for-completion context)
          (if (epg-context-result-for context 'error)
              (error "Decrypt failed: %S"
@@ -1299,6 +1575,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-verify-file' or `epg-verify-string' instead."
+  (epg-context-set-operation context 'verify)
   (epg-context-set-result context nil)
   (if signed-text
       ;; Detached signature.
@@ -1308,14 +1585,18 @@ If you are unsure, use synchronous version of this function
        (epg-start context (list "--verify" (epg-data-file signature) "-"))
        (if (eq (process-status (epg-context-process context)) 'run)
            (process-send-string (epg-context-process context)
-                                (epg-data-string signed-text))))
+                                (epg-data-string signed-text)))
+       (if (eq (process-status (epg-context-process context)) 'run)
+           (process-send-eof (epg-context-process context))))
     ;; Normal (or cleartext) signature.
     (if (epg-data-file signature)
        (epg-start context (list "--verify" (epg-data-file signature)))
       (epg-start context (list "--verify"))
       (if (eq (process-status (epg-context-process context)) 'run)
          (process-send-string (epg-context-process context)
-                              (epg-data-string signature))))))
+                              (epg-data-string signature)))
+      (if (eq (process-status (epg-context-process context)) 'run)
+         (process-send-eof (epg-context-process context))))))
 
 ;;;###autoload
 (defun epg-verify-file (context signature &optional signed-text plain)
@@ -1367,7 +1648,6 @@ For a normal or a clear text signature, SIGNED-TEXT should be nil."
                                  (epg-make-data-from-file input-file)
                                  (epg-make-data-from-string signed-text)))
            (epg-start-verify context (epg-make-data-from-string signature)))
-         (epg-flush context)
          (epg-wait-for-completion context)
 ;        (if (epg-context-result-for context 'error)
 ;            (error "Verify failed: %S"
@@ -1384,22 +1664,23 @@ For a normal or a clear text signature, SIGNED-TEXT should be nil."
   "Initiate a sign operation on PLAIN.
 PLAIN is a data object.
 
-If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
-If MODE is t or 'detached, it makes a detached signature.
-Otherwise, it makes a normal signature.
+If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
+If it is nil or 'normal, it makes a normal signature.
+Otherwise, it makes a clear text signature.
 
 If you use this function, you will need to wait for the completion of
 `epg-gpg-program' by using `epg-wait-for-completion' and call
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-sign-file' or `epg-sign-string' instead."
+  (epg-context-set-operation context 'sign)
   (epg-context-set-result context nil)
   (epg-start context
-            (append (list (if (eq mode 'clearsign)
-                              "--clearsign"
-                            (if (or (eq mode t) (eq mode 'detached))
-                                "--detach-sign"
-                              "--sign")))
+            (append (list (if (memq mode '(t detached))
+                              "--detach-sign"
+                            (if (memq mode '(nil normal))
+                                "--sign"
+                              "--clearsign")))
                     (apply #'nconc
                            (mapcar
                             (lambda (signer)
@@ -1412,18 +1693,20 @@ If you are unsure, use synchronous version of this function
   ;; `gpgsm' does not read passphrase from stdin, so waiting is not needed.
   (unless (eq (epg-context-protocol context) 'CMS)
     (epg-wait-for-status context '("BEGIN_SIGNING")))
-  (if (and (epg-data-string plain)
-          (eq (process-status (epg-context-process context)) 'run))
-      (process-send-string (epg-context-process context)
-                          (epg-data-string plain))))
+  (when (epg-data-string plain)
+    (if (eq (process-status (epg-context-process context)) 'run)
+       (process-send-string (epg-context-process context)
+                            (epg-data-string plain)))
+    (if (eq (process-status (epg-context-process context)) 'run)
+       (process-send-eof (epg-context-process context)))))
 
 ;;;###autoload
 (defun epg-sign-file (context plain signature &optional mode)
   "Sign a file PLAIN and store the result to a file SIGNATURE.
 If SIGNATURE is nil, it returns the result as a string.
-If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
-If MODE is t or 'detached, it makes a detached signature.
-Otherwise, it makes a normal signature."
+If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
+If it is nil or 'normal, it makes a normal signature.
+Otherwise, it makes a clear text signature."
   (unwind-protect
       (progn
        (if signature
@@ -1432,10 +1715,7 @@ Otherwise, it makes a normal signature."
                                       (epg-make-temp-file "epg-output")))
        (epg-start-sign context (epg-make-data-from-file plain) mode)
        (epg-wait-for-completion context)
-       (if (epg-context-result-for context 'sign)
-           (if (epg-context-result-for context 'error)
-               (message "Sign warning: %S"
-                        (epg-context-result-for context 'error)))
+       (unless (epg-context-result-for context 'sign)
          (if (epg-context-result-for context 'error)
              (error "Sign failed: %S"
                     (epg-context-result-for context 'error))
@@ -1449,15 +1729,14 @@ Otherwise, it makes a normal signature."
 ;;;###autoload
 (defun epg-sign-string (context plain &optional mode)
   "Sign a string PLAIN and return the output as string.
-If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
-If MODE is t or 'detached, it makes a detached signature.
-Otherwise, it makes a normal signature."
+If optional 3rd argument MODE is t or 'detached, it makes a detached signature.
+If it is nil or 'normal, it makes a normal signature.
+Otherwise, it makes a clear text signature."
   (unwind-protect
       (progn
        (epg-context-set-output-file context
                                     (epg-make-temp-file "epg-output"))
        (epg-start-sign context (epg-make-data-from-string plain) mode)
-       (epg-flush context)
        (epg-wait-for-completion context)
        (unless (epg-context-result-for context 'sign)
          (if (epg-context-result-for context 'error)
@@ -1480,6 +1759,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-encrypt-file' or `epg-encrypt-string' instead."
+  (epg-context-set-operation context 'encrypt)
   (epg-context-set-result context nil)
   (epg-start context
             (append (if always-trust '("--always-trust"))
@@ -1504,10 +1784,12 @@ If you are unsure, use synchronous version of this function
     (if sign
        (epg-wait-for-status context '("BEGIN_SIGNING"))
       (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
-  (if (and (epg-data-string plain)
-          (eq (process-status (epg-context-process context)) 'run))
-      (process-send-string (epg-context-process context)
-                          (epg-data-string plain))))
+  (when (epg-data-string plain)
+    (if (eq (process-status (epg-context-process context)) 'run)
+       (process-send-string (epg-context-process context)
+                            (epg-data-string plain)))
+    (if (eq (process-status (epg-context-process context)) 'run)
+       (process-send-eof (epg-context-process context)))))
 
 ;;;###autoload
 (defun epg-encrypt-file (context plain recipients
@@ -1550,7 +1832,6 @@ If RECIPIENTS is nil, it performs symmetric encryption."
                                     (epg-make-temp-file "epg-output"))
        (epg-start-encrypt context (epg-make-data-from-string plain)
                           recipients sign always-trust)
-       (epg-flush context)
        (epg-wait-for-completion context)
        (if (and sign
                 (not (epg-context-result-for context 'sign)))
@@ -1574,6 +1855,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
+  (epg-context-set-operation context 'export-keys)
   (epg-context-set-result context nil)
   (epg-start context (cons "--export"
                           (mapcar
@@ -1617,25 +1899,24 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
+  (epg-context-set-operation context 'import-keys)
   (epg-context-set-result context nil)
-  (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
   (epg-start context (list "--import" (epg-data-file keys)))
-  (if (and (epg-data-string keys)
-          (eq (process-status (epg-context-process context)) 'run))
-      (process-send-string (epg-context-process context)
-                          (epg-data-string keys))))
-  
+  (when (epg-data-string keys)
+    (if (eq (process-status (epg-context-process context)) 'run)
+       (process-send-string (epg-context-process context)
+                            (epg-data-string keys)))
+    (if (eq (process-status (epg-context-process context)) 'run)
+       (process-send-eof (epg-context-process context)))))
+
 (defun epg-import-keys-1 (context keys)
   (unwind-protect
       (progn
        (epg-start-import-keys context keys)
-       (if (epg-data-file keys)
-           (epg-flush context))
        (epg-wait-for-completion context)
        (if (epg-context-result-for context 'error)
            (error "Import keys failed: %S"
-                  (epg-context-result-for context 'error)))
-       (epg-read-output context))
+                  (epg-context-result-for context 'error))))
     (epg-reset context)))
 
 ;;;###autoload
@@ -1649,6 +1930,36 @@ If you are unsure, use synchronous version of this function
   (epg-import-keys-1 context (epg-make-data-from-string keys)))
 
 ;;;###autoload
+(defun epg-start-receive-keys (context key-id-list)
+  "Initiate a receive key operation.
+KEY-ID-LIST is a list of key IDs.
+
+If you use this function, you will need to wait for the completion of
+`epg-gpg-program' by using `epg-wait-for-completion' and call
+`epg-reset' to clear a temporaly output file.
+If you are unsure, use synchronous version of this function
+`epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
+  (epg-context-set-operation context 'receive-keys)
+  (epg-context-set-result context nil)
+  (epg-start context (cons "--recv-keys" key-id-list)))
+
+;;;###autoload
+(defun epg-receive-keys (context keys)
+  "Add keys from server.
+KEYS is a list of key IDs"
+  (unwind-protect
+      (progn
+       (epg-start-receive-keys context keys)
+       (epg-wait-for-completion context)
+       (if (epg-context-result-for context 'error)
+           (error "Receive keys failed: %S"
+                  (epg-context-result-for context 'error))))
+    (epg-reset context)))
+
+;;;###autoload
+(defalias 'epg-import-keys-from-server 'epg-receive-keys)
+
+;;;###autoload
 (defun epg-start-delete-keys (context keys &optional allow-secret)
   "Initiate an delete keys operation.
 
@@ -1657,6 +1968,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-delete-keys' instead."
+  (epg-context-set-operation context 'delete-keys)
   (epg-context-set-result context nil)
   (epg-start context (cons (if allow-secret
                               "--delete-secret-key"
@@ -1688,6 +2000,7 @@ If you use this function, you will need to wait for the completion of
 `epg-reset' to clear a temporaly output file.
 If you are unsure, use synchronous version of this function
 `epg-sign-keys' instead."
+  (epg-context-set-operation context 'sign-keys)
   (epg-context-set-result context nil)
   (epg-start context (cons (if local
                               "--lsign-key"
@@ -1710,6 +2023,54 @@ If you are unsure, use synchronous version of this function
                   (epg-context-result-for context 'error))))
     (epg-reset context)))
 
+;;;###autoload
+(defun epg-start-generate-key (context parameters)
+  "Initiate a key generation.
+PARAMETERS specifies parameters for the key.
+
+If you use this function, you will need to wait for the completion of
+`epg-gpg-program' by using `epg-wait-for-completion' and call
+`epg-reset' to clear a temporaly output file.
+If you are unsure, use synchronous version of this function
+`epg-generate-key-from-file' or `epg-generate-key-from-string' instead."
+  (epg-context-set-operation context 'generate-key)
+  (epg-context-set-result context nil)
+  (if (epg-data-file parameters)
+      (epg-start context (list "--batch" "--genkey"
+                              (epg-data-file parameters)))
+    (epg-start context '("--batch" "--genkey"))
+    (if (eq (process-status (epg-context-process context)) 'run)
+       (process-send-string (epg-context-process context)
+                            (epg-data-string parameters)))
+    (if (eq (process-status (epg-context-process context)) 'run)
+       (process-send-eof (epg-context-process context)))))
+
+;;;###autoload
+(defun epg-generate-key-from-file (context parameters)
+  "Generate a new key pair.
+PARAMETERS is a file which tells how to create the key."
+  (unwind-protect
+      (progn
+       (epg-start-generate-key context (epg-make-data-from-file parameters))
+       (epg-wait-for-completion context)
+       (if (epg-context-result-for context 'error)
+           (error "Generate key failed: %S"
+                  (epg-context-result-for context 'error))))
+    (epg-reset context)))
+
+;;;###autoload
+(defun epg-generate-key-from-string (context parameters)
+  "Generate a new key pair.
+PARAMETERS is a string which tells how to create the key."
+  (unwind-protect
+      (progn
+       (epg-start-generate-key context (epg-make-data-from-string parameters))
+       (epg-wait-for-completion context)
+       (if (epg-context-result-for context 'error)
+           (error "Generate key failed: %S"
+                  (epg-context-result-for context 'error))))
+    (epg-reset context)))
+
 (defun epg-decode-hexstring (string)
   (let ((index 0))
     (while (eq index (string-match "[0-9A-Fa-f][0-9A-Fa-f]" string index))