* epa.el (epa-select-keys): Added "Cancel" button.
[elisp/epg.git] / epg.el
1 ;;; epg.el --- the EasyPG Library
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;;   2005, 2006 Free Software Foundation, Inc.
4 ;; Copyright (C) 2006 Daiki Ueno
5
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, GnuPG
8
9 ;; This file is part of EasyPG.
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Code:
27
28 (defgroup epg ()
29   "The EasyPG Library")
30
31 (defcustom epg-gpg-program "gpg"
32   "The `gpg' executable."
33   :group 'epg
34   :type 'string)
35
36 (defconst epg-version-number "0.0.0")
37
38 (defvar epg-user-id nil
39   "GnuPG ID of your default identity.")
40
41 (defvar epg-user-id-alist nil
42   "An alist mapping from key ID to user ID.")
43
44 (defvar epg-read-point nil)
45 (defvar epg-pending-status-list nil)
46 (defvar epg-key-id nil)
47 (defvar epg-context nil)
48 (defvar epg-debug nil)
49 (defvar epg-debug-buffer nil)
50
51 ;; from gnupg/include/cipher.h
52 (defconst epg-cipher-algorithm-alist
53   '((0 . "NONE")
54     (1 . "IDEA")
55     (2 . "3DES")
56     (3 . "CAST5")
57     (4 . "BLOWFISH")
58     (7 . "AES")
59     (8 . "AES192")
60     (9 . "AES256")
61     (10 . "TWOFISH")
62     (110 . "DUMMY")))
63
64 ;; from gnupg/include/cipher.h
65 (defconst epg-pubkey-algorithm-alist
66   '((1 . "RSA")
67     (2 . "RSA_E")
68     (3 . "RSA_S")
69     (16 . "ELGAMAL_E")
70     (17 . "DSA")
71     (20 . "ELGAMAL")))
72
73 ;; from gnupg/include/cipher.h
74 (defconst epg-digest-algorithm-alist
75   '((1 . "MD5")
76     (2 . "SHA1")
77     (3 . "RMD160")
78     (8 . "SHA256")
79     (9 . "SHA384")
80     (10 . "SHA512")))
81
82 ;; from gnupg/include/cipher.h
83 (defconst epg-compress-algorithm-alist
84   '((0 . "NONE")
85     (1 . "ZIP")
86     (2 . "ZLIB")
87     (3 . "BZIP2")))
88
89 (defconst epg-invalid-recipients-alist
90   '((0 . "No specific reason given")
91     (1 . "Not Found")
92     (2 . "Ambigious specification")
93     (3 . "Wrong key usage")
94     (4 . "Key revoked")
95     (5 . "Key expired")
96     (6 . "No CRL known")
97     (7 . "CRL too old")
98     (8 . "Policy mismatch")
99     (9 . "Not a secret key")
100     (10 . "Key not trusted")))
101
102 (defconst epg-delete-problem-alist
103   '((1 . "No such key")
104     (2 . "Must delete secret key first")
105     (3 . "Ambigious specification")))
106
107 (defvar epg-key-validity-alist
108   '((?o . unknown)
109     (?i . invalid)
110     (?d . disabled)
111     (?r . revoked)
112     (?e . expired)
113     (?- . none)
114     (?q . undefined)
115     (?n . never)
116     (?m . marginal)
117     (?f . full)
118     (?u . ultimate)))
119
120 (defvar epg-key-capablity-alist
121   '((?e . encrypt)
122     (?s . sign)
123     (?c . certify)
124     (?a . authentication)))
125
126 (defvar epg-prompt-alist nil)
127
128 (defun epg-make-data-from-file (file)
129   "Make a data object from FILE."
130   (vector file nil))
131
132 (defun epg-make-data-from-string (string)
133   "Make a data object from STRING."
134   (vector nil string))
135
136 (defun epg-data-file (data)
137   "Return the file of DATA."
138   (aref data 0))
139
140 (defun epg-data-string (data)
141   "Return the string of DATA."
142   (aref data 1))
143
144 (defun epg-make-context (&optional protocol armor textmode include-certs
145                                    cipher-algorithm digest-algorithm
146                                    compress-algorithm)
147   "Return a context object."
148   (vector protocol armor textmode include-certs
149           cipher-algorithm digest-algorithm compress-algorithm
150           #'epg-passphrase-callback-function
151           #'epg-progress-callback-function
152           nil nil nil nil))
153
154 (defun epg-context-protocol (context)
155   "Return the protocol used within CONTEXT."
156   (aref context 0))
157
158 (defun epg-context-armor (context)
159   "Return t if the output shouled be ASCII armored in CONTEXT."
160   (aref context 1))
161
162 (defun epg-context-textmode (context)
163   "Return t if canonical text mode should be used in CONTEXT."
164   (aref context 2))
165
166 (defun epg-context-include-certs (context)
167   "Return how many certificates should be included in an S/MIME signed
168 message."
169   (aref context 3))
170
171 (defun epg-context-cipher-algorithm (context)
172   "Return the cipher algorithm in CONTEXT."
173   (aref context 4))
174
175 (defun epg-context-digest-algorithm (context)
176   "Return the digest algorithm in CONTEXT."
177   (aref context 5))
178
179 (defun epg-context-compress-algorithm (context)
180   "Return the compress algorithm in CONTEXT."
181   (aref context 6))
182
183 (defun epg-context-passphrase-callback (context)
184   "Return the function used to query passphrase."
185   (aref context 7))
186
187 (defun epg-context-progress-callback (context)
188   "Return the function which handles progress update."
189   (aref context 8))
190
191 (defun epg-context-signers (context)
192   "Return the list of key-id for singning."
193   (aref context 9))
194
195 (defun epg-context-process (context)
196   "Return the process object of `epg-gpg-program'.
197 This function is for internal use only."
198   (aref context 10))
199
200 (defun epg-context-output-file (context)
201   "Return the output file of `epg-gpg-program'.
202 This function is for internal use only."
203   (aref context 11))
204
205 (defun epg-context-result (context)
206   "Return the result of the previous cryptographic operation."
207   (aref context 12))
208
209 (defun epg-context-set-protocol (context protocol)
210   "Set the protocol used within CONTEXT."
211   (aset context 0 protocol))
212
213 (defun epg-context-set-armor (context armor)
214   "Specify if the output shouled be ASCII armored in CONTEXT."
215   (aset context 1 armor))
216
217 (defun epg-context-set-textmode (context textmode)
218   "Specify if canonical text mode should be used in CONTEXT."
219   (aset context 2 textmode))
220
221 (defun epg-context-set-include-certs (context include-certs)
222  "Set how many certificates should be included in an S/MIME signed message."
223   (aset context 3 include-certs))
224
225 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
226  "Set the cipher algorithm in CONTEXT."
227   (aset context 4 cipher-algorithm))
228
229 (defun epg-context-set-digest-algorithm (context digest-algorithm)
230  "Set the digest algorithm in CONTEXT."
231   (aset context 5 digest-algorithm))
232
233 (defun epg-context-set-compress-algorithm (context compress-algorithm)
234  "Set the compress algorithm in CONTEXT."
235   (aset context 6 compress-algorithm))
236
237 (defun epg-context-set-passphrase-callback (context
238                                                  passphrase-callback)
239   "Set the function used to query passphrase."
240   (aset context 7 passphrase-callback))
241
242 (defun epg-context-set-progress-callback (context progress-callback)
243   "Set the function which handles progress update."
244   (aset context 8 progress-callback))
245
246 (defun epg-context-set-signers (context signers)
247  "Set the list of key-id for singning."
248   (aset context 9 signers))
249
250 (defun epg-context-set-process (context process)
251   "Set the process object of `epg-gpg-program'.
252 This function is for internal use only."
253   (aset context 10 process))
254
255 (defun epg-context-set-output-file (context output-file)
256   "Set the output file of `epg-gpg-program'.
257 This function is for internal use only."
258   (aset context 11 output-file))
259
260 (defun epg-context-set-result (context result)
261   "Set the result of the previous cryptographic operation."
262   (aset context 12 result))
263
264 (defun epg-make-verify-result (status key-id user-id)
265   "Return a verify-result object."
266   (vector status key-id user-id nil nil))
267
268 (defun epg-verify-result-status (verify-result)
269   "Return the status code of VERIFY-RESULT."
270   (aref verify-result 0))
271
272 (defun epg-verify-result-key-id (verify-result)
273   "Return the key-id of VERIFY-RESULT."
274   (aref verify-result 1))
275
276 (defun epg-verify-result-user-id (verify-result)
277   "Return the user-id of VERIFY-RESULT."
278   (aref verify-result 2))
279   
280 (defun epg-verify-result-validity (verify-result)
281   "Return the validity of VERIFY-RESULT."
282   (aref verify-result 3))
283
284 (defun epg-verify-result-fingerprint (verify-result)
285   "Return the fingerprint of VERIFY-RESULT."
286   (aref verify-result 4))
287
288 (defun epg-verify-result-set-status (verify-result status)
289  "Set the status code of VERIFY-RESULT."
290   (aset verify-result 0 status))
291
292 (defun epg-verify-result-set-key-id (verify-result key-id)
293  "Set the key-id of VERIFY-RESULT."
294   (aset verify-result 1 key-id))
295
296 (defun epg-verify-result-set-user-id (verify-result user-id)
297  "Set the user-id of VERIFY-RESULT."
298   (aset verify-result 2 user-id))
299   
300 (defun epg-verify-result-set-validity (verify-result validity)
301  "Set the validity of VERIFY-RESULT."
302   (aset verify-result 3 validity))
303
304 (defun epg-verify-result-set-fingerprint (verify-result fingerprint)
305  "Set the fingerprint of VERIFY-RESULT."
306   (aset verify-result 4 fingerprint))
307
308 (defun epg-make-key (owner-trust)
309   "Return a key object."
310   (vector owner-trust nil nil))
311
312 (defun epg-key-owner-trust (key)
313   "Return the owner trust of KEY."
314   (aref key 0))
315
316 (defun epg-key-sub-key-list (key)
317   "Return the sub key list of KEY."
318   (aref key 1))
319
320 (defun epg-key-user-id-list (key)
321   "Return the user ID list of KEY."
322   (aref key 2))
323
324 (defun epg-key-set-sub-key-list (key sub-key-list)
325   "Set the sub key list of KEY."
326   (aset key 1 sub-key-list))
327
328 (defun epg-key-set-user-id-list (key user-id-list)
329   "Set the user ID list of KEY."
330   (aset key 2 user-id-list))
331
332 (defun epg-make-sub-key (validity capability secret algorithm length id
333                                   creation-time expiration-time)
334   "Return a sub key object."
335   (vector validity capability secret algorithm length id creation-time
336           expiration-time nil))
337
338 (defun epg-sub-key-validity (sub-key)
339   "Return the validity of SUB-KEY."
340   (aref sub-key 0))
341
342 (defun epg-sub-key-capability (sub-key)
343   "Return the capability of SUB-KEY."
344   (aref sub-key 1))
345
346 (defun epg-sub-key-secret (sub-key)
347   "Return non-nil if SUB-KEY is a secret key."
348   (aref sub-key 2))
349
350 (defun epg-sub-key-algorithm (sub-key)
351   "Return the algorithm of SUB-KEY."
352   (aref sub-key 3))
353
354 (defun epg-sub-key-length (sub-key)
355   "Return the length of SUB-KEY."
356   (aref sub-key 4))
357
358 (defun epg-sub-key-id (sub-key)
359   "Return the ID of SUB-KEY."
360   (aref sub-key 5))
361
362 (defun epg-sub-key-creation-time (sub-key)
363   "Return the creation time of SUB-KEY."
364   (aref sub-key 6))
365
366 (defun epg-sub-key-expiration-time (sub-key)
367   "Return the expiration time of SUB-KEY."
368   (aref sub-key 7))
369
370 (defun epg-sub-key-fingerprint (sub-key)
371   "Return the fingerprint of SUB-KEY."
372   (aref sub-key 8))
373
374 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
375   "Set the fingerprint of SUB-KEY.
376 This function is for internal use only."
377   (aset sub-key 8 fingerprint))
378
379 (defun epg-make-user-id (validity name)
380   "Return a user ID object."
381   (vector validity name nil))
382
383 (defun epg-user-id-validity (user-id)
384   "Return the validity of USER-ID."
385   (aref user-id 0))
386
387 (defun epg-user-id-name (user-id)
388   "Return the name of USER-ID."
389   (aref user-id 1))
390
391 (defun epg-user-id-verify-result-list (user-id)
392   "Return the verify-result list of USER-ID."
393   (aref user-id 2))
394
395 (defun epg-user-id-set-verify-result-list (user-id verify-result-list)
396   "Set the verify-result list of USER-ID."
397   (aset user-id 2 verify-result-list))
398
399 (defun epg-context-result-for (context name)
400   (cdr (assq name (epg-context-result context))))
401
402 (defun epg-context-set-result-for (context name value)
403   (let* ((result (epg-context-result context))
404          (entry (assq name result)))
405     (if entry
406         (setcdr entry value)
407       (epg-context-set-result context (cons (cons name value) result)))))
408
409 (defun epg-verify-result-to-string (verify-result)
410   (format "%s verify-result from %s %s%s"
411           (capitalize (symbol-name (epg-verify-result-status verify-result)))
412           (epg-verify-result-key-id verify-result)
413           (epg-verify-result-user-id verify-result)
414           (if (epg-verify-result-validity verify-result)
415               (format " (trust %s)"
416                       (epg-verify-result-validity verify-result))
417             "")))
418
419 (defun epg-start (context args)
420   "Start `epg-gpg-program' in a subprocess with given ARGS."
421   (let* ((args (append (list "--no-tty"
422                              "--status-fd" "1"
423                              "--command-fd" "0"
424                              "--yes")
425                        (if (epg-context-armor context) '("--armor"))
426                        (if (epg-context-textmode context) '("--textmode"))
427                        (if (epg-context-output-file context)
428                            (list "--output" (epg-context-output-file context)))
429                        args))
430          (coding-system-for-write 'binary)
431          process-connection-type
432          (orig-mode (default-file-modes))
433          (buffer (generate-new-buffer " *epg*"))
434          process)
435     (if epg-debug
436         (save-excursion
437           (unless epg-debug-buffer
438             (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
439           (set-buffer epg-debug-buffer)
440           (goto-char (point-max))
441           (insert (format "%s %s\n" epg-gpg-program
442                           (mapconcat #'identity args " ")))))
443     (with-current-buffer buffer
444       (make-local-variable 'epg-read-point)
445       (setq epg-read-point (point-min))
446       (make-local-variable 'epg-pending-status-list)
447       (setq epg-pending-status-list nil)
448       (make-local-variable 'epg-key-id)
449       (setq epg-key-id nil)
450       (make-local-variable 'epg-context)
451       (setq epg-context context))
452     (unwind-protect
453         (progn
454           (set-default-file-modes 448)
455           (setq process
456                 (apply #'start-process "epg" buffer epg-gpg-program args)))
457       (set-default-file-modes orig-mode))
458     (set-process-filter process #'epg-process-filter)
459     (epg-context-set-process context process)))
460
461 (defun epg-process-filter (process input)
462   (if epg-debug
463       (save-excursion
464         (unless epg-debug-buffer
465           (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
466         (set-buffer epg-debug-buffer)
467         (goto-char (point-max))
468         (insert input)))
469   (if (buffer-live-p (process-buffer process))
470       (save-excursion
471         (set-buffer (process-buffer process))
472         (goto-char (point-max))
473         (insert input)
474         (goto-char epg-read-point)
475         (beginning-of-line)
476         (while (looking-at ".*\n")      ;the input line finished
477           (save-excursion
478             (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
479                 (let* ((status (match-string 1))
480                        (string (match-string 2))
481                        (symbol (intern-soft (concat "epg-status-" status))))
482                   (if (member status epg-pending-status-list)
483                       (setq epg-pending-status-list nil))
484                   (if (and symbol
485                            (fboundp symbol))
486                       (funcall symbol process string)))))
487           (forward-line))
488         (setq epg-read-point (point)))))
489
490 (defun epg-read-output (context)
491   (with-temp-buffer
492     (if (fboundp 'set-buffer-multibyte)
493         (set-buffer-multibyte nil))
494     (if (file-exists-p (epg-context-output-file context))
495         (let ((coding-system-for-read (if (epg-context-textmode context)
496                                           'raw-text
497                                         'binary)))
498           (insert-file-contents (epg-context-output-file context))
499           (buffer-string)))))
500
501 (defun epg-wait-for-status (context status-list)
502   (with-current-buffer (process-buffer (epg-context-process context))
503     (setq epg-pending-status-list status-list)
504     (while (and (eq (process-status (epg-context-process context)) 'run)
505                 epg-pending-status-list)
506       (accept-process-output (epg-context-process context) 1))))
507
508 (defun epg-wait-for-completion (context)
509   (while (eq (process-status (epg-context-process context)) 'run)
510     ;; We can't use accept-process-output instead of sit-for here
511     ;; because it may cause an interrupt during the sentinel execution.
512     (sit-for 0.1)))
513
514 (defun epg-flush (context)
515   (if (eq (process-status (epg-context-process context)) 'run)
516       (process-send-eof (epg-context-process context))))
517
518 (defun epg-reset (context)
519   (if (and (epg-context-process context)
520            (buffer-live-p (process-buffer (epg-context-process context))))
521       (kill-buffer (process-buffer (epg-context-process context))))
522   (epg-context-set-process context nil))
523
524 (defun epg-delete-output-file (context)
525   (if (and (epg-context-output-file context)
526            (file-exists-p (epg-context-output-file context)))
527       (delete-file (epg-context-output-file context))))
528
529 (defun epg-status-USERID_HINT (process string)
530   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
531       (let* ((key-id (match-string 1 string))
532              (user-id (match-string 2 string))
533              (entry (assoc key-id epg-user-id-alist)))
534         (if entry
535             (setcdr entry user-id)
536           (setq epg-user-id-alist (cons (cons key-id user-id)
537                                         epg-user-id-alist))))))
538
539 (defun epg-status-NEED_PASSPHRASE (process string)
540   (if (string-match "\\`\\([^ ]+\\)" string)
541       (setq epg-key-id (match-string 1 string))))
542
543 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
544   (setq epg-key-id 'SYM))
545
546 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
547   (setq epg-key-id 'PIN))
548
549 (defun epg-status-GET_HIDDEN (process string)
550   (if (and epg-key-id
551            (string-match "\\`passphrase\\." string))
552     (let ((passphrase
553            (funcall
554             (if (consp (epg-context-passphrase-callback epg-context))
555                 (car (epg-context-passphrase-callback epg-context))
556               (epg-context-passphrase-callback epg-context))
557             epg-key-id
558             (if (consp (epg-context-passphrase-callback epg-context))
559                 (cdr (epg-context-passphrase-callback epg-context)))))
560           string)
561       (if passphrase
562           (unwind-protect
563               (progn
564                 (setq string (concat passphrase "\n"))
565                 (fillarray passphrase 0)
566                 (setq passphrase nil)
567                 (process-send-string process string))
568             (if string
569                 (fillarray string 0)))))))
570
571 (defun epg-status-GET_BOOL (process string)
572   (let ((entry (assoc string epg-prompt-alist)))
573     (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
574         (process-send-string process "y\n")
575       (process-send-string process "n\n"))))
576
577 (defun epg-status-GET_LINE (process string)
578   (let* ((entry (assoc string epg-prompt-alist))
579          (string (read-string (if entry (cdr entry) (concat string ": ")))))
580     (process-send-string process (concat string "\n"))))
581
582 (defun epg-status-GOODSIG (process string)
583   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
584       (epg-context-set-result-for
585        epg-context
586        'verify
587        (cons (epg-make-verify-result 'good
588                                  (match-string 1 string)
589                                  (match-string 2 string))
590              (epg-context-result-for epg-context 'verify)))))
591
592 (defun epg-status-EXPSIG (process string)
593   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
594       (epg-context-set-result-for
595        epg-context
596        'verify
597        (cons (epg-make-verify-result 'expired
598                                  (match-string 1 string)
599                                  (match-string 2 string))
600              (epg-context-result-for epg-context 'verify)))))
601
602 (defun epg-status-EXPKEYSIG (process string)
603   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
604       (epg-context-set-result-for
605        epg-context
606        'verify
607        (cons (epg-make-verify-result 'expired-key
608                                  (match-string 1 string)
609                                  (match-string 2 string))
610              (epg-context-result-for epg-context 'verify)))))
611
612 (defun epg-status-REVKEYSIG (process string)
613   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
614       (epg-context-set-result-for
615        epg-context
616        'verify
617        (cons (epg-make-verify-result 'revoked-key
618                                  (match-string 1 string)
619                                  (match-string 2 string))
620              (epg-context-result-for epg-context 'verify)))))
621
622 (defun epg-status-BADSIG (process string)
623   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
624       (epg-context-set-result-for
625        epg-context
626        'verify
627        (cons (epg-make-verify-result 'bad
628                                  (match-string 1 string)
629                                  (match-string 2 string))
630              (epg-context-result-for epg-context 'verify)))))
631
632 (defun epg-status-VALIDSIG (process string)
633   (let ((verify-result (car (epg-context-result-for epg-context 'verify))))
634     (if (and verify-result
635              (eq (epg-verify-result-status verify-result) 'good)
636              (string-match "\\`\\([^ ]+\\) " string))
637         (epg-verify-result-set-fingerprint verify-result (match-string 1 string)))))
638
639 (defun epg-status-TRUST_UNDEFINED (process string)
640   (let ((verify-result (car (epg-context-result-for epg-context 'verify))))
641     (if (and verify-result
642              (eq (epg-verify-result-status verify-result) 'good))
643         (epg-verify-result-set-validity verify-result 'undefined))))
644
645 (defun epg-status-TRUST_NEVER (process string)
646   (let ((verify-result (car (epg-context-result-for epg-context 'verify))))
647     (if (and verify-result
648              (eq (epg-verify-result-status verify-result) 'good))
649         (epg-verify-result-set-validity verify-result 'never))))
650
651 (defun epg-status-TRUST_MARGINAL (process string)
652   (let ((verify-result (car (epg-context-result-for epg-context 'verify))))
653     (if (and verify-result
654              (eq (epg-verify-result-status verify-result) 'marginal))
655         (epg-verify-result-set-validity verify-result 'marginal))))
656
657 (defun epg-status-TRUST_FULLY (process string)
658   (let ((verify-result (car (epg-context-result-for epg-context 'verify))))
659     (if (and verify-result
660              (eq (epg-verify-result-status verify-result) 'good))
661         (epg-verify-result-set-validity verify-result 'full))))
662
663 (defun epg-status-TRUST_ULTIMATE (process string)
664   (let ((verify-result (car (epg-context-result-for epg-context 'verify))))
665     (if (and verify-result
666              (eq (epg-verify-result-status verify-result) 'good))
667         (epg-verify-result-set-validity verify-result 'ultimate))))
668
669 (defun epg-status-PROGRESS (process string)
670   (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
671                     string)
672       (funcall (if (consp (epg-context-progress-callback epg-context))
673                    (car (epg-context-progress-callback epg-context))
674                  (epg-context-progress-callback epg-context))
675                (match-string 1 string)
676                (match-string 2 string)
677                (string-to-number (match-string 3 string))
678                (string-to-number (match-string 4 string))
679                (if (consp (epg-context-progress-callback epg-context))
680                    (cdr (epg-context-progress-callback epg-context))))))
681
682 (defun epg-status-DECRYPTION_FAILED (process string)
683   (epg-context-set-result-for
684    epg-context 'error
685    (cons 'decryption-failed
686          (epg-context-result-for epg-context 'error))))
687
688 (defun epg-status-NODATA (process string)
689   (epg-context-set-result-for
690    epg-context 'error
691    (cons (cons 'no-data (string-to-number string))
692          (epg-context-result-for epg-context 'error))))
693
694 (defun epg-status-UNEXPECTED (process string)
695   (epg-context-set-result-for
696    epg-context 'error
697    (cons (cons 'unexpected (string-to-number string))
698          (epg-context-result-for epg-context 'error))))
699
700 (defun epg-status-KEYEXPIRED (process string)
701   (epg-context-set-result-for
702    epg-context 'error
703    (cons (cons 'key-expired string)
704          (epg-context-result-for epg-context 'error))))
705
706 (defun epg-status-KEYREVOKED (process string)
707   (epg-context-set-result-for
708    epg-context 'error
709    (cons 'key-revoked
710          (epg-context-result-for epg-context 'error))))
711
712 (defun epg-status-BADARMOR (process string)
713   (epg-context-set-result-for
714    epg-context 'error
715    (cons 'bad-armor
716          (epg-context-result-for epg-context 'error))))
717
718 (defun epg-status-INV_RECP (process string)
719   (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
720       (epg-context-set-result-for
721        epg-context 'error
722        (cons (list 'invalid-recipient
723                    (string-to-number (match-string 1 string))
724                    (match-string 2 string))
725              (epg-context-result-for epg-context 'error)))))
726
727 (defun epg-status-NO_RECP (process string)
728   (epg-context-set-result-for
729    epg-context 'error
730    (cons 'no-recipients
731          (epg-context-result-for epg-context 'error))))
732
733 (defun epg-status-DELETE_PROBLEM (process string)
734   (if (string-match "\\`\\([0-9]+\\)" string)
735       (epg-context-set-result-for
736        epg-context 'error
737        (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
738              (epg-context-result-for epg-context 'error)))))
739
740 (defun epg-status-SIG_CREATED (process string)
741   (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
742 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
743       (epg-context-set-result-for
744        epg-context 'sign
745        (cons (list (cons 'type (string-to-char (match-string 1 string)))
746                    (cons 'pubkey-algorithm
747                          (string-to-number (match-string 2 string)))
748                    (cons 'digest-algorithm
749                          (string-to-number (match-string 3 string)))
750                    (cons 'class (string-to-number (match-string 4 string) 16))
751                    (cons 'creation-time (match-string 5 string))
752                    (cons 'fingerprint (substring string (match-end 0))))
753              (epg-context-result-for epg-context 'sign)))))
754
755 (defun epg-passphrase-callback-function (key-id handback)
756   (read-passwd
757    (if (eq key-id 'SYM)
758        "Passphrase for symmetric encryption: "
759      (if (eq key-id 'PIN)
760          "Passphrase for PIN: "
761        (let ((entry (assoc key-id epg-user-id-alist)))
762          (if entry
763              (format "Passphrase for %s %s: " key-id (cdr entry))
764            (format "Passphrase for %s: " key-id)))))))
765
766 (defun epg-progress-callback-function (what char current total handback)
767   (message "%s: %d%%/%d%%" what current total))
768
769 (defun epg-configuration ()
770   "Return a list of internal configuration parameters of `epg-gpg-program'."
771   (let (config type)
772     (with-temp-buffer
773       (apply #'call-process epg-gpg-program nil (list t nil) nil
774              '("--with-colons" "--list-config"))
775       (goto-char (point-min))
776       (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
777         (setq type (intern (match-string 1))
778               config (cons (cons type
779                                  (if (memq type
780                                            '(pubkey cipher digest compress))
781                                      (mapcar #'string-to-number
782                                              (delete "" (split-string
783                                                          (match-string 2)
784                                                          ";")))
785                                    (match-string 2)))
786                            config))))
787     config))
788
789 (defun epg-list-keys-1 (name mode)
790   (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
791                             "--fixed-list-mode" "--with-fingerprint"
792                             "--with-fingerprint"
793                             (if mode "--list-secret-keys" "--list-keys"))
794                       (if name (list name))))
795         keys string field index)
796     (with-temp-buffer
797       (apply #'call-process epg-gpg-program nil (list t nil) nil args)
798       (goto-char (point-min))
799       (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
800         (setq keys (cons (make-vector 15 nil) keys)
801               string (match-string 0)
802               index 0
803               field 0)
804         (while (eq index
805                    (string-match "\\([^:]+\\)?:" string index))
806           (setq index (match-end 0))
807           (aset (car keys) field (match-string 1 string))
808           (setq field (1+ field))))
809       (nreverse keys))))
810
811 (defun epg-make-sub-key-1 (line)
812   (epg-make-sub-key
813    (if (aref line 1)
814        (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
815    (delq nil
816          (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
817                  (aref line 11)))
818    (member (aref line 0) '("sec" "ssb"))
819    (string-to-number (aref line 3))
820    (string-to-number (aref line 2))
821    (aref line 4)
822    (aref line 5)
823    (aref line 6)))
824
825 (defun epg-list-keys (&optional name mode)
826   (let ((lines (epg-list-keys-1 name mode))
827         keys)
828     (while lines
829       (cond
830        ((member (aref (car lines) 0) '("pub" "sec"))
831         (when (car keys)
832           (epg-key-set-sub-key-list
833            (car keys)
834            (nreverse (epg-key-sub-key-list (car keys))))
835           (epg-key-set-user-id-list
836            (car keys)
837            (nreverse (epg-key-user-id-list (car keys)))))
838         (setq keys (cons (epg-make-key
839                           (if (aref (car lines) 8)
840                               (cdr (assq (string-to-char (aref (car lines) 8))
841                                          epg-key-validity-alist))))
842                          keys))
843         (epg-key-set-sub-key-list
844          (car keys)
845          (cons (epg-make-sub-key-1 (car lines))
846                (epg-key-sub-key-list (car keys)))))
847        ((member (aref (car lines) 0) '("sub" "ssb"))
848         (epg-key-set-sub-key-list
849          (car keys)
850          (cons (epg-make-sub-key-1 (car lines))
851                (epg-key-sub-key-list (car keys)))))
852        ((equal (aref (car lines) 0) "uid")
853         (epg-key-set-user-id-list
854          (car keys)
855          (cons (epg-make-user-id
856                 (if (aref (car lines) 1)
857                     (cdr (assq (string-to-char (aref (car lines) 1))
858                                epg-key-validity-alist)))
859                 (aref (car lines) 9))
860                (epg-key-user-id-list (car keys)))))
861        ((equal (aref (car lines) 0) "fpr")
862         (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
863                                      (aref (car lines) 9))))
864       (setq lines (cdr lines)))
865     (nreverse keys)))
866
867 (if (fboundp 'make-temp-file)
868     (defalias 'epg-make-temp-file 'make-temp-file)
869   ;; stolen from poe.el.
870   (defun epg-make-temp-file (prefix)
871     "Create a temporary file.
872 The returned file name (created by appending some random characters at the end
873 of PREFIX, and expanding against `temporary-file-directory' if necessary),
874 is guaranteed to point to a newly created empty file.
875 You can then use `write-region' to write new data into the file."
876     (let (tempdir tempfile)
877       (unwind-protect
878           (let (file)
879             ;; First, create a temporary directory.
880             (while (condition-case ()
881                        (progn
882                          (setq tempdir (make-temp-name
883                                         (concat
884                                          (file-name-directory prefix)
885                                          "DIR")))
886                          ;; return nil or signal an error.
887                          (make-directory tempdir))
888                      ;; let's try again.
889                      (file-already-exists t)))
890             (set-file-modes tempdir 448)
891             ;; Second, create a temporary file in the tempdir.
892             ;; There *is* a race condition between `make-temp-name'
893             ;; and `write-region', but we don't care it since we are
894             ;; in a private directory now.
895             (setq tempfile (make-temp-name (concat tempdir "/EMU")))
896             (write-region "" nil tempfile nil 'silent)
897             (set-file-modes tempfile 384)
898             ;; Finally, make a hard-link from the tempfile.
899             (while (condition-case ()
900                        (progn
901                          (setq file (make-temp-name prefix))
902                          ;; return nil or signal an error.
903                          (add-name-to-file tempfile file))
904                      ;; let's try again.
905                      (file-already-exists t)))
906             file)
907         ;; Cleanup the tempfile.
908         (and tempfile
909              (file-exists-p tempfile)
910              (delete-file tempfile))
911         ;; Cleanup the tempdir.
912         (and tempdir
913              (file-directory-p tempdir)
914              (delete-directory tempdir))))))
915
916 ;;;###autoload
917 (defun epg-start-decrypt (context cipher)
918   "Initiate a decrypt operation on CIPHER.
919 CIPHER is a data object.
920
921 If you use this function, you will need to wait for the completion of
922 `epg-gpg-program' by using `epg-wait-for-completion' and call
923 `epg-reset' to clear a temporaly output file.
924 If you are unsure, use synchronous version of this function
925 `epg-decrypt-file' or `epg-decrypt-string' instead."
926   (unless (epg-data-file cipher)
927     (error "Not a file"))
928   (epg-context-set-result context nil)
929   (epg-start context (list "--decrypt" (epg-data-file cipher)))
930   (epg-wait-for-status context '("BEGIN_DECRYPTION")))
931
932 ;;;###autoload
933 (defun epg-decrypt-file (context cipher plain)
934   "Decrypt a file CIPHER and store the result to a file PLAIN.
935 If PLAIN is nil, it returns the result as a string."
936   (unwind-protect
937       (progn
938         (if plain
939             (epg-context-set-output-file context plain)
940           (epg-context-set-output-file context
941                                        (epg-make-temp-file "epg-output")))
942         (epg-start-decrypt context (epg-make-data-from-file cipher))
943         (epg-wait-for-completion context)
944         (if (epg-context-result-for context 'error)
945             (error "Decrypt failed: %S"
946                    (epg-context-result-for context 'error)))
947         (unless plain
948           (epg-read-output context)))
949     (unless plain
950       (epg-delete-output-file context))
951     (epg-reset context)))
952
953 ;;;###autoload
954 (defun epg-decrypt-string (context cipher)
955   "Decrypt a string CIPHER and return the plain text."
956   (let ((input-file (epg-make-temp-file "epg-input"))
957         (coding-system-for-write 'binary))
958     (unwind-protect
959         (progn
960           (write-region cipher nil input-file)
961           (epg-context-set-output-file context
962                                        (epg-make-temp-file "epg-output"))
963           (epg-start-decrypt context (epg-make-data-from-file input-file))
964           (epg-flush context)
965           (epg-wait-for-completion context)
966           (if (epg-context-result-for context 'error)
967               (error "Decrypt failed: %S"
968                      (epg-context-result-for context 'error)))
969           (epg-read-output context))
970       (epg-delete-output-file context)
971       (if (file-exists-p input-file)
972           (delete-file input-file))
973       (epg-reset context))))
974
975 ;;;###autoload
976 (defun epg-start-verify (context signature &optional signed-text)
977   "Initiate a verify operation on SIGNATURE.
978 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
979
980 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
981 For a normal or a clear text signature, SIGNED-TEXT should be nil.
982
983 If you use this function, you will need to wait for the completion of
984 `epg-gpg-program' by using `epg-wait-for-completion' and call
985 `epg-reset' to clear a temporaly output file.
986 If you are unsure, use synchronous version of this function
987 `epg-verify-file' or `epg-verify-string' instead."
988   (epg-context-set-result context nil)
989   (if signed-text
990       ;; Detached signature.
991       (if (epg-data-file signed-text)
992           (epg-start context (list "--verify" (epg-data-file signature)
993                                    (epg-data-file signed-text)))
994         (epg-start context (list "--verify" (epg-data-file signature) "-"))
995         (if (eq (process-status (epg-context-process context)) 'run)
996             (process-send-string (epg-context-process context)
997                                  (epg-data-string signed-text))))
998     ;; Normal (or cleartext) signature.
999     (if (epg-data-file signature)
1000         (epg-start context (list "--verify" (epg-data-file signature)))
1001       (epg-start context (list "--verify"))
1002       (if (eq (process-status (epg-context-process context)) 'run)
1003           (process-send-string (epg-context-process context)
1004                                (epg-data-string signature))))))
1005
1006 ;;;###autoload
1007 (defun epg-verify-file (context signature &optional signed-text plain)
1008   "Verify a file SIGNATURE.
1009 SIGNED-TEXT and PLAIN are also a file if they are specified.
1010
1011 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1012 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1013   (unwind-protect
1014       (progn
1015         (if plain
1016             (epg-context-set-output-file context plain)
1017           (epg-context-set-output-file context
1018                                        (epg-make-temp-file "epg-output")))
1019         (if signed-text
1020             (epg-start-verify context
1021                               (epg-make-data-from-file signature)
1022                               (epg-make-data-from-file signed-text))
1023           (epg-start-verify context
1024                             (epg-make-data-from-file signature)))
1025         (epg-wait-for-completion context)
1026         (unless plain
1027           (epg-read-output context)))
1028     (unless plain
1029       (epg-delete-output-file context))
1030     (epg-reset context)))
1031
1032 ;;;###autoload
1033 (defun epg-verify-string (context signature &optional signed-text)
1034   "Verify a string SIGNATURE.
1035 SIGNED-TEXT is a string if it is specified.
1036
1037 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1038 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1039   (let ((coding-system-for-write 'binary)
1040         input-file)
1041     (unwind-protect
1042         (progn
1043           (epg-context-set-output-file context
1044                                        (epg-make-temp-file "epg-output"))
1045           (if signed-text
1046               (progn
1047                 (setq input-file (epg-make-temp-file "epg-signature"))
1048                 (write-region signature nil input-file)
1049                 (epg-start-verify context
1050                                   (epg-make-data-from-file input-file)
1051                                   (epg-make-data-from-string signed-text)))
1052             (epg-start-verify context (epg-make-data-from-string signature)))
1053           (epg-flush context)
1054           (epg-wait-for-completion context)
1055           (epg-read-output context))
1056       (epg-delete-output-file context)
1057       (if (and input-file
1058                (file-exists-p input-file))
1059           (delete-file input-file))
1060       (epg-reset context))))
1061
1062 ;;;###autoload
1063 (defun epg-start-sign (context plain &optional mode)
1064   "Initiate a sign operation on PLAIN.
1065 PLAIN is a data object.
1066
1067 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1068 If MODE is t or 'detached, it makes a detached signature.
1069 Otherwise, it makes a normal signature.
1070
1071 If you use this function, you will need to wait for the completion of
1072 `epg-gpg-program' by using `epg-wait-for-completion' and call
1073 `epg-reset' to clear a temporaly output file.
1074 If you are unsure, use synchronous version of this function
1075 `epg-sign-file' or `epg-sign-string' instead."
1076   (epg-context-set-result context nil)
1077   (epg-start context
1078              (append (list (if (eq mode 'clearsign)
1079                                "--clearsign"
1080                              (if (or (eq mode t) (eq mode 'detached))
1081                                  "--detach-sign"
1082                                "--sign")))
1083                      (apply #'nconc
1084                             (mapcar
1085                              (lambda (signer)
1086                                (list "-u"
1087                                      (epg-sub-key-id
1088                                       (car (epg-key-sub-key-list signer)))))
1089                              (epg-context-signers context)))
1090                      (if (epg-data-file plain)
1091                          (list (epg-data-file plain)))))
1092   (epg-wait-for-status context '("BEGIN_SIGNING"))
1093   (if (and (epg-data-string plain)
1094            (eq (process-status (epg-context-process context)) 'run))
1095       (process-send-string (epg-context-process context)
1096                            (epg-data-string plain))))
1097
1098 ;;;###autoload
1099 (defun epg-sign-file (context plain signature &optional mode)
1100   "Sign a file PLAIN and store the result to a file SIGNATURE.
1101 If SIGNATURE is nil, it returns the result as a string.
1102 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1103 If MODE is t or 'detached, it makes a detached signature.
1104 Otherwise, it makes a normal signature."
1105   (unwind-protect
1106       (progn
1107         (if signature
1108             (epg-context-set-output-file context signature)
1109           (epg-context-set-output-file context
1110                                        (epg-make-temp-file "epg-output")))
1111         (epg-start-sign context (epg-make-data-from-file plain) mode)
1112         (epg-wait-for-completion context)
1113         (if (epg-context-result-for context 'error)
1114             (error "Sign failed: %S"
1115                    (epg-context-result-for context 'error)))
1116         (unless signature
1117           (epg-read-output context)))
1118     (unless signature
1119       (epg-delete-output-file context))
1120     (epg-reset context)))
1121
1122 ;;;###autoload
1123 (defun epg-sign-string (context plain &optional mode)
1124   "Sign a string PLAIN and return the output as string.
1125 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1126 If MODE is t or 'detached, it makes a detached signature.
1127 Otherwise, it makes a normal signature."
1128   (unwind-protect
1129       (progn
1130         (epg-context-set-output-file context
1131                                      (epg-make-temp-file "epg-output"))
1132         (epg-start-sign context (epg-make-data-from-string plain) mode)
1133         (epg-flush context)
1134         (epg-wait-for-completion context)
1135         (if (epg-context-result-for context 'error)
1136             (error "Sign failed: %S"
1137                    (epg-context-result-for context 'error)))
1138         (epg-read-output context))
1139     (epg-delete-output-file context)
1140     (epg-reset context)))
1141
1142 ;;;###autoload
1143 (defun epg-start-encrypt (context plain recipients
1144                                   &optional sign always-trust)
1145   "Initiate an encrypt operation on PLAIN.
1146 PLAIN is a data object.
1147 If RECIPIENTS is nil, it performs symmetric encryption.
1148
1149 If you use this function, you will need to wait for the completion of
1150 `epg-gpg-program' by using `epg-wait-for-completion' and call
1151 `epg-reset' to clear a temporaly output file.
1152 If you are unsure, use synchronous version of this function
1153 `epg-encrypt-file' or `epg-encrypt-string' instead."
1154   (epg-context-set-result context nil)
1155   (epg-start context
1156              (append (if always-trust '("--always-trust"))
1157                      (if recipients '("--encrypt") '("--symmetric"))
1158                      (if sign
1159                          (cons "--sign"
1160                                (apply #'nconc
1161                                       (mapcar (lambda (signer)
1162                                                 (list "-u" signer))
1163                                               (epg-context-signers context)))))
1164                      (apply #'nconc
1165                             (mapcar
1166                              (lambda (recipient)
1167                                (list "-r"
1168                                      (epg-sub-key-id
1169                                       (car (epg-key-sub-key-list recipient)))))
1170                              recipients))
1171                      (if (epg-data-file plain)
1172                          (list (epg-data-file plain)))))
1173   (if sign
1174       (epg-wait-for-status context '("BEGIN_SIGNING")))
1175   (epg-wait-for-status context '("BEGIN_ENCRYPTION"))
1176   (if (and (epg-data-string plain)
1177            (eq (process-status (epg-context-process context)) 'run))
1178       (process-send-string (epg-context-process context)
1179                            (epg-data-string plain))))
1180
1181 ;;;###autoload
1182 (defun epg-encrypt-file (context plain recipients
1183                                  cipher &optional sign always-trust)
1184   "Encrypt a file PLAIN and store the result to a file CIPHER.
1185 If CIPHER is nil, it returns the result as a string.
1186 If RECIPIENTS is nil, it performs symmetric encryption."
1187   (unwind-protect
1188       (progn
1189         (if cipher
1190             (epg-context-set-output-file context cipher)
1191           (epg-context-set-output-file context
1192                                        (epg-make-temp-file "epg-output")))
1193         (epg-start-encrypt context (epg-make-data-from-file plain)
1194                            recipients sign always-trust)
1195         (epg-wait-for-completion context)
1196         (if (epg-context-result-for context 'error)
1197             (error "Encrypt failed: %S"
1198                    (epg-context-result-for context 'error)))
1199         (unless cipher
1200           (epg-read-output context)))
1201     (unless cipher
1202       (epg-delete-output-file context))
1203     (epg-reset context)))
1204
1205 ;;;###autoload
1206 (defun epg-encrypt-string (context plain recipients
1207                                    &optional sign always-trust)
1208   "Encrypt a string PLAIN.
1209 If RECIPIENTS is nil, it performs symmetric encryption."
1210   (unwind-protect
1211       (progn
1212         (epg-context-set-output-file context
1213                                      (epg-make-temp-file "epg-output"))
1214         (epg-start-encrypt context (epg-make-data-from-string plain)
1215                            recipients sign always-trust)
1216         (epg-flush context)
1217         (epg-wait-for-completion context)
1218         (if (epg-context-result-for context 'error)
1219             (error "Encrypt failed: %S"
1220                    (epg-context-result-for context 'error)))
1221         (epg-read-output context))
1222     (epg-delete-output-file context)
1223     (epg-reset context)))
1224
1225 ;;;###autoload
1226 (defun epg-start-export-keys (context keys)
1227   "Initiate an export keys operation.
1228
1229 If you use this function, you will need to wait for the completion of
1230 `epg-gpg-program' by using `epg-wait-for-completion' and call
1231 `epg-reset' to clear a temporaly output file.
1232 If you are unsure, use synchronous version of this function
1233 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1234   (epg-context-set-result context nil)
1235   (epg-start context (cons "--export"
1236                            (mapcar
1237                             (lambda (key)
1238                               (epg-sub-key-id
1239                                (car (epg-key-sub-key-list key))))
1240                             keys))))
1241
1242 ;;;###autoload
1243 (defun epg-export-keys-to-file (context keys file)
1244   "Extract public KEYS."
1245   (unwind-protect
1246       (progn
1247         (if keys
1248             (epg-context-set-output-file context file)
1249           (epg-context-set-output-file context
1250                                        (epg-make-temp-file "epg-output")))
1251         (epg-start-export-keys context keys)
1252         (epg-wait-for-completion context)
1253         (if (epg-context-result-for context 'error)
1254             (error "Export keys failed"))
1255         (unless file
1256           (epg-read-output context)))
1257     (unless file
1258       (epg-delete-output-file context))
1259     (epg-reset context)))
1260
1261 ;;;###autoload
1262 (defun epg-export-keys-to-string (context keys)
1263   "Extract public KEYS and return them as a string."
1264   (epg-export-keys-to-file context keys nil))
1265
1266 ;;;###autoload
1267 (defun epg-start-import-keys (context keys)
1268   "Initiate an import keys operation.
1269 KEYS is a data object.
1270
1271 If you use this function, you will need to wait for the completion of
1272 `epg-gpg-program' by using `epg-wait-for-completion' and call
1273 `epg-reset' to clear a temporaly output file.
1274 If you are unsure, use synchronous version of this function
1275 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1276   (epg-context-set-result context nil)
1277   (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1278   (epg-start context (list "--import" (epg-data-file keys)))
1279   (if (and (epg-data-string keys)
1280            (eq (process-status (epg-context-process context)) 'run))
1281       (process-send-string (epg-context-process context)
1282                            (epg-data-string keys))))
1283   
1284 (defun epg-import-keys-1 (context keys)
1285   (unwind-protect
1286       (progn
1287         (epg-start-import-keys context keys)
1288         (if (epg-data-file keys)
1289             (epg-flush context))
1290         (epg-wait-for-completion context)
1291         (if (epg-context-result-for context 'error)
1292             (error "Import keys failed"))
1293         (epg-read-output context))
1294     (epg-reset context)))
1295
1296 ;;;###autoload
1297 (defun epg-import-keys-from-file (context keys)
1298   "Add keys from a file KEYS."
1299   (epg-import-keys-1 context (epg-make-data-from-file keys)))
1300
1301 ;;;###autoload
1302 (defun epg-import-keys-from-string (context keys)
1303   "Add keys from a string KEYS."
1304   (epg-import-keys-1 context (epg-make-data-from-string keys)))
1305
1306 ;;;###autoload
1307 (defun epg-start-delete-keys (context keys &optional allow-secret)
1308   "Initiate an delete keys operation.
1309
1310 If you use this function, you will need to wait for the completion of
1311 `epg-gpg-program' by using `epg-wait-for-completion' and call
1312 `epg-reset' to clear a temporaly output file.
1313 If you are unsure, use synchronous version of this function
1314 `epg-delete-keys' instead."
1315   (epg-context-set-result context nil)
1316   (epg-start context (cons (if allow-secret
1317                                "--delete-secret-key"
1318                              "--delete-key")
1319                            (mapcar
1320                             (lambda (key)
1321                               (epg-sub-key-id
1322                                (car (epg-key-sub-key-list key))))
1323                             keys))))
1324
1325 ;;;###autoload
1326 (defun epg-delete-keys (context keys &optional allow-secret)
1327   "Delete KEYS from the key ring."
1328   (unwind-protect
1329       (progn
1330         (epg-start-delete-keys context keys)
1331         (epg-wait-for-completion context)
1332         (if (epg-context-result-for context 'error)
1333             (error "Delete key failed")))
1334     (epg-reset context)))
1335
1336 (provide 'epg)
1337
1338 ;;; epg.el ends here