* epa-file.el (epa-file-write-region): RECIPIENTS is now a list of
[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-signature (status key-id user-id)
265   "Return a signature object."
266   (vector status key-id user-id nil nil))
267
268 (defun epg-signature-status (signature)
269   "Return the status code of SIGNATURE."
270   (aref signature 0))
271
272 (defun epg-signature-key-id (signature)
273   "Return the key-id of SIGNATURE."
274   (aref signature 1))
275
276 (defun epg-signature-user-id (signature)
277   "Return the user-id of SIGNATURE."
278   (aref signature 2))
279   
280 (defun epg-signature-validity (signature)
281   "Return the validity of SIGNATURE."
282   (aref signature 3))
283
284 (defun epg-signature-fingerprint (signature)
285   "Return the fingerprint of SIGNATURE."
286   (aref signature 4))
287
288 (defun epg-signature-set-status (signature status)
289  "Set the status code of SIGNATURE."
290   (aset signature 0 status))
291
292 (defun epg-signature-set-key-id (signature key-id)
293  "Set the key-id of SIGNATURE."
294   (aset signature 1 key-id))
295
296 (defun epg-signature-set-user-id (signature user-id)
297  "Set the user-id of SIGNATURE."
298   (aset signature 2 user-id))
299   
300 (defun epg-signature-set-validity (signature validity)
301  "Set the validity of SIGNATURE."
302   (aset signature 3 validity))
303
304 (defun epg-signature-set-fingerprint (signature fingerprint)
305  "Set the fingerprint of SIGNATURE."
306   (aset signature 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-signature-list (user-id)
392   "Return the signature list of USER-ID."
393   (aref user-id 2))
394
395 (defun epg-user-id-set-signature-list (user-id signature-list)
396   "Set the signature list of USER-ID."
397   (aset user-id 2 signature-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-start (context args)
410   "Start `epg-gpg-program' in a subprocess with given ARGS."
411   (let* ((args (append (list "--no-tty"
412                              "--status-fd" "1"
413                              "--command-fd" "0"
414                              "--yes")
415                        (if (epg-context-armor context) '("--armor"))
416                        (if (epg-context-textmode context) '("--textmode"))
417                        (if (epg-context-output-file context)
418                            (list "--output" (epg-context-output-file context)))
419                        args))
420          (coding-system-for-write 'binary)
421          process-connection-type
422          (orig-mode (default-file-modes))
423          (buffer (generate-new-buffer " *epg*"))
424          process)
425     (if epg-debug
426         (save-excursion
427           (unless epg-debug-buffer
428             (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
429           (set-buffer epg-debug-buffer)
430           (goto-char (point-max))
431           (insert (format "%s %s\n" epg-gpg-program
432                           (mapconcat #'identity args " ")))))
433     (with-current-buffer buffer
434       (make-local-variable 'epg-read-point)
435       (setq epg-read-point (point-min))
436       (make-local-variable 'epg-pending-status-list)
437       (setq epg-pending-status-list nil)
438       (make-local-variable 'epg-key-id)
439       (setq epg-key-id nil)
440       (make-local-variable 'epg-context)
441       (setq epg-context context))
442     (unwind-protect
443         (progn
444           (set-default-file-modes 448)
445           (setq process
446                 (apply #'start-process "epg" buffer epg-gpg-program args)))
447       (set-default-file-modes orig-mode))
448     (set-process-filter process #'epg-process-filter)
449     (epg-context-set-process context process)))
450
451 (defun epg-process-filter (process input)
452   (if epg-debug
453       (save-excursion
454         (unless epg-debug-buffer
455           (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
456         (set-buffer epg-debug-buffer)
457         (goto-char (point-max))
458         (insert input)))
459   (if (buffer-live-p (process-buffer process))
460       (save-excursion
461         (set-buffer (process-buffer process))
462         (goto-char (point-max))
463         (insert input)
464         (goto-char epg-read-point)
465         (beginning-of-line)
466         (while (looking-at ".*\n")      ;the input line finished
467           (save-excursion
468             (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
469                 (let* ((status (match-string 1))
470                        (string (match-string 2))
471                        (symbol (intern-soft (concat "epg-status-" status))))
472                   (if (member status epg-pending-status-list)
473                       (setq epg-pending-status-list nil))
474                   (if (and symbol
475                            (fboundp symbol))
476                       (funcall symbol process string)))))
477           (forward-line))
478         (setq epg-read-point (point)))))
479
480 (defun epg-read-output (context)
481   (with-temp-buffer
482     (if (fboundp 'set-buffer-multibyte)
483         (set-buffer-multibyte nil))
484     (if (file-exists-p (epg-context-output-file context))
485         (let ((coding-system-for-read (if (epg-context-textmode context)
486                                           'raw-text
487                                         'binary)))
488           (insert-file-contents (epg-context-output-file context))
489           (buffer-string)))))
490
491 (defun epg-wait-for-status (context status-list)
492   (with-current-buffer (process-buffer (epg-context-process context))
493     (setq epg-pending-status-list status-list)
494     (while (and (eq (process-status (epg-context-process context)) 'run)
495                 epg-pending-status-list)
496       (accept-process-output (epg-context-process context) 1))))
497
498 (defun epg-wait-for-completion (context)
499   (while (eq (process-status (epg-context-process context)) 'run)
500     ;; We can't use accept-process-output instead of sit-for here
501     ;; because it may cause an interrupt during the sentinel execution.
502     (sit-for 0.1)))
503
504 (defun epg-flush (context)
505   (if (eq (process-status (epg-context-process context)) 'run)
506       (process-send-eof (epg-context-process context))))
507
508 (defun epg-reset (context)
509   (if (and (epg-context-process context)
510            (buffer-live-p (process-buffer (epg-context-process context))))
511       (kill-buffer (process-buffer (epg-context-process context))))
512   (epg-context-set-process context nil))
513
514 (defun epg-delete-output-file (context)
515   (if (and (epg-context-output-file context)
516            (file-exists-p (epg-context-output-file context)))
517       (delete-file (epg-context-output-file context))))
518
519 (defun epg-status-USERID_HINT (process string)
520   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
521       (let* ((key-id (match-string 1 string))
522              (user-id (match-string 2 string))
523              (entry (assoc key-id epg-user-id-alist)))
524         (if entry
525             (setcdr entry user-id)
526           (setq epg-user-id-alist (cons (cons key-id user-id)
527                                         epg-user-id-alist))))))
528
529 (defun epg-status-NEED_PASSPHRASE (process string)
530   (if (string-match "\\`\\([^ ]+\\)" string)
531       (setq epg-key-id (match-string 1 string))))
532
533 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
534   (setq epg-key-id 'SYM))
535
536 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
537   (setq epg-key-id 'PIN))
538
539 (defun epg-status-GET_HIDDEN (process string)
540   (if (and epg-key-id
541            (string-match "\\`passphrase\\." string))
542     (let ((passphrase
543            (funcall
544             (if (consp (epg-context-passphrase-callback epg-context))
545                 (car (epg-context-passphrase-callback epg-context))
546               (epg-context-passphrase-callback epg-context))
547             epg-key-id
548             (if (consp (epg-context-passphrase-callback epg-context))
549                 (cdr (epg-context-passphrase-callback epg-context)))))
550           string)
551       (if passphrase
552           (unwind-protect
553               (progn
554                 (setq string (concat passphrase "\n"))
555                 (fillarray passphrase 0)
556                 (setq passphrase nil)
557                 (process-send-string process string))
558             (if string
559                 (fillarray string 0)))))))
560
561 (defun epg-status-GET_BOOL (process string)
562   (let ((entry (assoc string epg-prompt-alist)))
563     (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
564         (process-send-string process "y\n")
565       (process-send-string process "n\n"))))
566
567 (defun epg-status-GET_LINE (process string)
568   (let* ((entry (assoc string epg-prompt-alist))
569          (string (read-string (if entry (cdr entry) (concat string ": ")))))
570     (process-send-string process (concat string "\n"))))
571
572 (defun epg-status-GOODSIG (process string)
573   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
574       (epg-context-set-result-for
575        epg-context
576        'verify
577        (cons (epg-make-signature 'good
578                                  (match-string 1 string)
579                                  (match-string 2 string))
580              (epg-context-result-for epg-context 'verify)))))
581
582 (defun epg-status-EXPSIG (process string)
583   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
584       (epg-context-set-result-for
585        epg-context
586        'verify
587        (cons (epg-make-signature 'expired
588                                  (match-string 1 string)
589                                  (match-string 2 string))
590              (epg-context-result-for epg-context 'verify)))))
591
592 (defun epg-status-EXPKEYSIG (process string)
593   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
594       (epg-context-set-result-for
595        epg-context
596        'verify
597        (cons (epg-make-signature 'expired-key
598                                  (match-string 1 string)
599                                  (match-string 2 string))
600              (epg-context-result-for epg-context 'verify)))))
601
602 (defun epg-status-REVKEYSIG (process string)
603   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
604       (epg-context-set-result-for
605        epg-context
606        'verify
607        (cons (epg-make-signature 'revoked-key
608                                  (match-string 1 string)
609                                  (match-string 2 string))
610              (epg-context-result-for epg-context 'verify)))))
611
612 (defun epg-status-BADSIG (process string)
613   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
614       (epg-context-set-result-for
615        epg-context
616        'verify
617        (cons (epg-make-signature 'bad
618                                  (match-string 1 string)
619                                  (match-string 2 string))
620              (epg-context-result-for epg-context 'verify)))))
621
622 (defun epg-status-VALIDSIG (process string)
623   (let ((signature (car (epg-context-result-for epg-context 'verify))))
624     (if (and signature
625              (eq (epg-signature-status signature) 'good)
626              (string-match "\\`\\([^ ]+\\) " string))
627         (epg-signature-set-fingerprint signature (match-string 1 string)))))
628
629 (defun epg-status-TRUST_UNDEFINED (process string)
630   (let ((signature (car (epg-context-result-for epg-context 'verify))))
631     (if (and signature
632              (eq (epg-signature-status signature) 'good))
633         (epg-signature-set-validity signature 'undefined))))
634
635 (defun epg-status-TRUST_NEVER (process string)
636   (let ((signature (car (epg-context-result-for epg-context 'verify))))
637     (if (and signature
638              (eq (epg-signature-status signature) 'good))
639         (epg-signature-set-validity signature 'never))))
640
641 (defun epg-status-TRUST_MARGINAL (process string)
642   (let ((signature (car (epg-context-result-for epg-context 'verify))))
643     (if (and signature
644              (eq (epg-signature-status signature) 'marginal))
645         (epg-signature-set-validity signature 'marginal))))
646
647 (defun epg-status-TRUST_FULLY (process string)
648   (let ((signature (car (epg-context-result-for epg-context 'verify))))
649     (if (and signature
650              (eq (epg-signature-status signature) 'good))
651         (epg-signature-set-validity signature 'full))))
652
653 (defun epg-status-TRUST_ULTIMATE (process string)
654   (let ((signature (car (epg-context-result-for epg-context 'verify))))
655     (if (and signature
656              (eq (epg-signature-status signature) 'good))
657         (epg-signature-set-validity signature 'ultimate))))
658
659 (defun epg-status-PROGRESS (process string)
660   (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
661                     string)
662       (funcall (if (consp (epg-context-progress-callback epg-context))
663                    (car (epg-context-progress-callback epg-context))
664                  (epg-context-progress-callback epg-context))
665                (match-string 1 string)
666                (match-string 2 string)
667                (string-to-number (match-string 3 string))
668                (string-to-number (match-string 4 string))
669                (if (consp (epg-context-progress-callback epg-context))
670                    (cdr (epg-context-progress-callback epg-context))))))
671
672 (defun epg-status-DECRYPTION_FAILED (process string)
673   (epg-context-set-result-for
674    epg-context 'error
675    (cons 'decryption-failed
676          (epg-context-result-for epg-context 'error))))
677
678 (defun epg-status-NODATA (process string)
679   (epg-context-set-result-for
680    epg-context 'error
681    (cons (cons 'no-data (string-to-number string))
682          (epg-context-result-for epg-context 'error))))
683
684 (defun epg-status-UNEXPECTED (process string)
685   (epg-context-set-result-for
686    epg-context 'error
687    (cons (cons 'unexpected (string-to-number string))
688          (epg-context-result-for epg-context 'error))))
689
690 (defun epg-status-KEYEXPIRED (process string)
691   (epg-context-set-result-for
692    epg-context 'error
693    (cons (cons 'key-expired string)
694          (epg-context-result-for epg-context 'error))))
695
696 (defun epg-status-KEYREVOKED (process string)
697   (epg-context-set-result-for
698    epg-context 'error
699    (cons 'key-revoked
700          (epg-context-result-for epg-context 'error))))
701
702 (defun epg-status-BADARMOR (process string)
703   (epg-context-set-result-for
704    epg-context 'error
705    (cons 'bad-armor
706          (epg-context-result-for epg-context 'error))))
707
708 (defun epg-status-INV_RECP (process string)
709   (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
710       (epg-context-set-result-for
711        epg-context 'error
712        (cons (list 'invalid-recipient
713                    (string-to-number (match-string 1 string))
714                    (match-string 2 string))
715              (epg-context-result-for epg-context 'error)))))
716
717 (defun epg-status-NO_RECP (process string)
718   (epg-context-set-result-for
719    epg-context 'error
720    (cons 'no-recipients
721          (epg-context-result-for epg-context 'error))))
722
723 (defun epg-status-DELETE_PROBLEM (process string)
724   (if (string-match "\\`\\([0-9]+\\)" string)
725       (epg-context-set-result-for
726        epg-context 'error
727        (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
728              (epg-context-result-for epg-context 'error)))))
729
730 (defun epg-status-SIG_CREATED (process string)
731   (if (string-match "\\`\\([DCS]\\) \\([0-9]+\\) \\([0-9]+\\) \
732 \\([0-9A-Fa-F][0-9A-Fa-F]\\) \\(.*\\) " string)
733       (epg-context-set-result-for
734        epg-context 'sign
735        (cons (list (cons 'type (string-to-char (match-string 1 string)))
736                    (cons 'pubkey-algorithm
737                          (string-to-number (match-string 2 string)))
738                    (cons 'digest-algorithm
739                          (string-to-number (match-string 3 string)))
740                    (cons 'class (string-to-number (match-string 4 string) 16))
741                    (cons 'creation-time (match-string 5 string))
742                    (cons 'fingerprint (substring string (match-end 0))))
743              (epg-context-result-for epg-context 'sign)))))
744
745 (defun epg-passphrase-callback-function (key-id handback)
746   (read-passwd
747    (if (eq key-id 'SYM)
748        "Passphrase for symmetric encryption: "
749      (if (eq key-id 'PIN)
750          "Passphrase for PIN: "
751        (let ((entry (assoc key-id epg-user-id-alist)))
752          (if entry
753              (format "Passphrase for %s %s: " key-id (cdr entry))
754            (format "Passphrase for %s: " key-id)))))))
755
756 (defun epg-progress-callback-function (what char current total handback)
757   (message "%s: %d%%/%d%%" what current total))
758
759 (defun epg-configuration ()
760   "Return a list of internal configuration parameters of `epg-gpg-program'."
761   (let (config type)
762     (with-temp-buffer
763       (apply #'call-process epg-gpg-program nil (list t nil) nil
764              '("--with-colons" "--list-config"))
765       (goto-char (point-min))
766       (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
767         (setq type (intern (match-string 1))
768               config (cons (cons type
769                                  (if (memq type
770                                            '(pubkey cipher digest compress))
771                                      (mapcar #'string-to-number
772                                              (delete "" (split-string
773                                                          (match-string 2)
774                                                          ";")))
775                                    (match-string 2)))
776                            config))))
777     config))
778
779 (defun epg-list-keys-1 (name mode)
780   (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
781                             "--fixed-list-mode" "--with-fingerprint"
782                             "--with-fingerprint"
783                             (if mode "--list-secret-keys" "--list-keys"))
784                       (if name (list name))))
785         keys string field index)
786     (with-temp-buffer
787       (apply #'call-process epg-gpg-program nil (list t nil) nil args)
788       (goto-char (point-min))
789       (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
790         (setq keys (cons (make-vector 15 nil) keys)
791               string (match-string 0)
792               index 0
793               field 0)
794         (while (eq index
795                    (string-match "\\([^:]+\\)?:" string index))
796           (setq index (match-end 0))
797           (aset (car keys) field (match-string 1 string))
798           (setq field (1+ field))))
799       (nreverse keys))))
800
801 (defun epg-make-sub-key-1 (line)
802   (epg-make-sub-key
803    (if (aref line 1)
804        (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
805    (delq nil
806          (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
807                  (aref line 11)))
808    (member (aref line 0) '("sec" "ssb"))
809    (string-to-number (aref line 3))
810    (string-to-number (aref line 2))
811    (aref line 4)
812    (aref line 5)
813    (aref line 6)))
814
815 (defun epg-list-keys (&optional name mode)
816   (let ((lines (epg-list-keys-1 name mode))
817         keys)
818     (while lines
819       (cond
820        ((member (aref (car lines) 0) '("pub" "sec"))
821         (when (car keys)
822           (epg-key-set-sub-key-list
823            (car keys)
824            (nreverse (epg-key-sub-key-list (car keys))))
825           (epg-key-set-user-id-list
826            (car keys)
827            (nreverse (epg-key-user-id-list (car keys)))))
828         (setq keys (cons (epg-make-key
829                           (if (aref (car lines) 8)
830                               (cdr (assq (string-to-char (aref (car lines) 8))
831                                          epg-key-validity-alist))))
832                          keys))
833         (epg-key-set-sub-key-list
834          (car keys)
835          (cons (epg-make-sub-key-1 (car lines))
836                (epg-key-sub-key-list (car keys)))))
837        ((member (aref (car lines) 0) '("sub" "ssb"))
838         (epg-key-set-sub-key-list
839          (car keys)
840          (cons (epg-make-sub-key-1 (car lines))
841                (epg-key-sub-key-list (car keys)))))
842        ((equal (aref (car lines) 0) "uid")
843         (epg-key-set-user-id-list
844          (car keys)
845          (cons (epg-make-user-id
846                 (if (aref (car lines) 1)
847                     (cdr (assq (string-to-char (aref (car lines) 1))
848                                epg-key-validity-alist)))
849                 (aref (car lines) 9))
850                (epg-key-user-id-list (car keys)))))
851        ((equal (aref (car lines) 0) "fpr")
852         (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
853                                      (aref (car lines) 9))))
854       (setq lines (cdr lines)))
855     (nreverse keys)))
856
857 (if (fboundp 'make-temp-file)
858     (defalias 'epg-make-temp-file 'make-temp-file)
859   ;; stolen from poe.el.
860   (defun epg-make-temp-file (prefix)
861     "Create a temporary file.
862 The returned file name (created by appending some random characters at the end
863 of PREFIX, and expanding against `temporary-file-directory' if necessary),
864 is guaranteed to point to a newly created empty file.
865 You can then use `write-region' to write new data into the file."
866     (let (tempdir tempfile)
867       (unwind-protect
868           (let (file)
869             ;; First, create a temporary directory.
870             (while (condition-case ()
871                        (progn
872                          (setq tempdir (make-temp-name
873                                         (concat
874                                          (file-name-directory prefix)
875                                          "DIR")))
876                          ;; return nil or signal an error.
877                          (make-directory tempdir))
878                      ;; let's try again.
879                      (file-already-exists t)))
880             (set-file-modes tempdir 448)
881             ;; Second, create a temporary file in the tempdir.
882             ;; There *is* a race condition between `make-temp-name'
883             ;; and `write-region', but we don't care it since we are
884             ;; in a private directory now.
885             (setq tempfile (make-temp-name (concat tempdir "/EMU")))
886             (write-region "" nil tempfile nil 'silent)
887             (set-file-modes tempfile 384)
888             ;; Finally, make a hard-link from the tempfile.
889             (while (condition-case ()
890                        (progn
891                          (setq file (make-temp-name prefix))
892                          ;; return nil or signal an error.
893                          (add-name-to-file tempfile file))
894                      ;; let's try again.
895                      (file-already-exists t)))
896             file)
897         ;; Cleanup the tempfile.
898         (and tempfile
899              (file-exists-p tempfile)
900              (delete-file tempfile))
901         ;; Cleanup the tempdir.
902         (and tempdir
903              (file-directory-p tempdir)
904              (delete-directory tempdir))))))
905
906 ;;;###autoload
907 (defun epg-start-decrypt (context cipher)
908   "Initiate a decrypt operation on CIPHER.
909 CIPHER is a data object.
910
911 If you use this function, you will need to wait for the completion of
912 `epg-gpg-program' by using `epg-wait-for-completion' and call
913 `epg-reset' to clear a temporaly output file.
914 If you are unsure, use synchronous version of this function
915 `epg-decrypt-file' or `epg-decrypt-string' instead."
916   (unless (epg-data-file cipher)
917     (error "Not a file"))
918   (epg-context-set-result context nil)
919   (epg-start context (list "--decrypt" (epg-data-file cipher)))
920   (epg-wait-for-status context '("BEGIN_DECRYPTION")))
921
922 ;;;###autoload
923 (defun epg-decrypt-file (context cipher plain)
924   "Decrypt a file CIPHER and store the result to a file PLAIN.
925 If PLAIN is nil, it returns the result as a string."
926   (unwind-protect
927       (progn
928         (if plain
929             (epg-context-set-output-file context plain)
930           (epg-context-set-output-file context
931                                        (epg-make-temp-file "epg-output")))
932         (epg-start-decrypt context (epg-make-data-from-file cipher))
933         (epg-wait-for-completion context)
934         (if (epg-context-result-for context 'error)
935             (error "Decrypt failed: %S"
936                    (epg-context-result-for context 'error)))
937         (unless plain
938           (epg-read-output context)))
939     (unless plain
940       (epg-delete-output-file context))
941     (epg-reset context)))
942
943 ;;;###autoload
944 (defun epg-decrypt-string (context cipher)
945   "Decrypt a string CIPHER and return the plain text."
946   (let ((input-file (epg-make-temp-file "epg-input"))
947         (coding-system-for-write 'binary))
948     (unwind-protect
949         (progn
950           (write-region cipher nil input-file)
951           (epg-context-set-output-file context
952                                        (epg-make-temp-file "epg-output"))
953           (epg-start-decrypt context (epg-make-data-from-file input-file))
954           (epg-flush context)
955           (epg-wait-for-completion context)
956           (if (epg-context-result-for context 'error)
957               (error "Decrypt failed: %S"
958                      (epg-context-result-for context 'error)))
959           (epg-read-output context))
960       (epg-delete-output-file context)
961       (if (file-exists-p input-file)
962           (delete-file input-file))
963       (epg-reset context))))
964
965 ;;;###autoload
966 (defun epg-start-verify (context signature &optional signed-text)
967   "Initiate a verify operation on SIGNATURE.
968 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
969
970 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
971 For a normal or a clear text signature, SIGNED-TEXT should be nil.
972
973 If you use this function, you will need to wait for the completion of
974 `epg-gpg-program' by using `epg-wait-for-completion' and call
975 `epg-reset' to clear a temporaly output file.
976 If you are unsure, use synchronous version of this function
977 `epg-verify-file' or `epg-verify-string' instead."
978   (epg-context-set-result context nil)
979   (if signed-text
980       ;; Detached signature.
981       (if (epg-data-file signed-text)
982           (epg-start context (list "--verify" (epg-data-file signature)
983                                    (epg-data-file signed-text)))
984         (epg-start context (list "--verify" (epg-data-file signature) "-"))
985         (if (eq (process-status (epg-context-process context)) 'run)
986             (process-send-string (epg-context-process context)
987                                  (epg-data-string signed-text))))
988     ;; Normal (or cleartext) signature.
989     (if (epg-data-file signature)
990         (epg-start context (list "--verify" (epg-data-file signature)))
991       (epg-start context (list "--verify"))
992       (if (eq (process-status (epg-context-process context)) 'run)
993           (process-send-string (epg-context-process context)
994                                (epg-data-string signature))))))
995
996 ;;;###autoload
997 (defun epg-verify-file (context signature &optional signed-text plain)
998   "Verify a file SIGNATURE.
999 SIGNED-TEXT and PLAIN are also a file if they are specified.
1000
1001 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1002 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1003   (unwind-protect
1004       (progn
1005         (if plain
1006             (epg-context-set-output-file context plain)
1007           (epg-context-set-output-file context
1008                                        (epg-make-temp-file "epg-output")))
1009         (if signed-text
1010             (epg-start-verify context
1011                               (epg-make-data-from-file signature)
1012                               (epg-make-data-from-file signed-text))
1013           (epg-start-verify context
1014                             (epg-make-data-from-file signature)))
1015         (epg-wait-for-completion context)
1016         (unless plain
1017           (epg-read-output context)))
1018     (unless plain
1019       (epg-delete-output-file context))
1020     (epg-reset context)))
1021
1022 ;;;###autoload
1023 (defun epg-verify-string (context signature &optional signed-text)
1024   "Verify a string SIGNATURE.
1025 SIGNED-TEXT is a string if it is specified.
1026
1027 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1028 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1029   (let ((coding-system-for-write 'binary)
1030         input-file)
1031     (unwind-protect
1032         (progn
1033           (epg-context-set-output-file context
1034                                        (epg-make-temp-file "epg-output"))
1035           (if signed-text
1036               (progn
1037                 (setq input-file (epg-make-temp-file "epg-signature"))
1038                 (write-region signature nil input-file)
1039                 (epg-start-verify context
1040                                   (epg-make-data-from-file input-file)
1041                                   (epg-make-data-from-string signed-text)))
1042             (epg-start-verify context (epg-make-data-from-string signature)))
1043           (epg-flush context)
1044           (epg-wait-for-completion context)
1045           (epg-read-output context))
1046       (epg-delete-output-file context)
1047       (if (and input-file
1048                (file-exists-p input-file))
1049           (delete-file input-file))
1050       (epg-reset context))))
1051
1052 ;;;###autoload
1053 (defun epg-start-sign (context plain &optional mode)
1054   "Initiate a sign operation on PLAIN.
1055 PLAIN is a data object.
1056
1057 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1058 If MODE is t or 'detached, it makes a detached signature.
1059 Otherwise, it makes a normal signature.
1060
1061 If you use this function, you will need to wait for the completion of
1062 `epg-gpg-program' by using `epg-wait-for-completion' and call
1063 `epg-reset' to clear a temporaly output file.
1064 If you are unsure, use synchronous version of this function
1065 `epg-sign-file' or `epg-sign-string' instead."
1066   (epg-context-set-result context nil)
1067   (epg-start context
1068              (append (list (if (eq mode 'clearsign)
1069                                "--clearsign"
1070                              (if (or (eq mode t) (eq mode 'detached))
1071                                  "--detach-sign"
1072                                "--sign")))
1073                      (apply #'nconc
1074                             (mapcar
1075                              (lambda (signer)
1076                                (list "-u"
1077                                      (epg-sub-key-id
1078                                       (car (epg-key-sub-key-list signer)))))
1079                              (epg-context-signers context)))
1080                      (if (epg-data-file plain)
1081                          (list (epg-data-file plain)))))
1082   (epg-wait-for-status context '("BEGIN_SIGNING"))
1083   (if (and (epg-data-string plain)
1084            (eq (process-status (epg-context-process context)) 'run))
1085       (process-send-string (epg-context-process context)
1086                            (epg-data-string plain))))
1087
1088 ;;;###autoload
1089 (defun epg-sign-file (context plain signature &optional mode)
1090   "Sign a file PLAIN and store the result to a file SIGNATURE.
1091 If SIGNATURE is nil, it returns the result as a string.
1092 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1093 If MODE is t or 'detached, it makes a detached signature.
1094 Otherwise, it makes a normal signature."
1095   (unwind-protect
1096       (progn
1097         (if signature
1098             (epg-context-set-output-file context signature)
1099           (epg-context-set-output-file context
1100                                        (epg-make-temp-file "epg-output")))
1101         (epg-start-sign context (epg-make-data-from-file plain) mode)
1102         (epg-wait-for-completion context)
1103         (if (epg-context-result-for context 'error)
1104             (error "Sign failed: %S"
1105                    (epg-context-result-for context 'error)))
1106         (unless signature
1107           (epg-read-output context)))
1108     (unless signature
1109       (epg-delete-output-file context))
1110     (epg-reset context)))
1111
1112 ;;;###autoload
1113 (defun epg-sign-string (context plain &optional mode)
1114   "Sign a string PLAIN and return the output as string.
1115 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1116 If MODE is t or 'detached, it makes a detached signature.
1117 Otherwise, it makes a normal signature."
1118   (unwind-protect
1119       (progn
1120         (epg-context-set-output-file context
1121                                      (epg-make-temp-file "epg-output"))
1122         (epg-start-sign context (epg-make-data-from-string plain) mode)
1123         (epg-flush context)
1124         (epg-wait-for-completion context)
1125         (if (epg-context-result-for context 'error)
1126             (error "Sign failed: %S"
1127                    (epg-context-result-for context 'error)))
1128         (epg-read-output context))
1129     (epg-delete-output-file context)
1130     (epg-reset context)))
1131
1132 ;;;###autoload
1133 (defun epg-start-encrypt (context plain recipients
1134                                   &optional sign always-trust)
1135   "Initiate an encrypt operation on PLAIN.
1136 PLAIN is a data object.
1137 If RECIPIENTS is nil, it performs symmetric encryption.
1138
1139 If you use this function, you will need to wait for the completion of
1140 `epg-gpg-program' by using `epg-wait-for-completion' and call
1141 `epg-reset' to clear a temporaly output file.
1142 If you are unsure, use synchronous version of this function
1143 `epg-encrypt-file' or `epg-encrypt-string' instead."
1144   (epg-context-set-result context nil)
1145   (epg-start context
1146              (append (if always-trust '("--always-trust"))
1147                      (if recipients '("--encrypt") '("--symmetric"))
1148                      (if sign
1149                          (cons "--sign"
1150                                (apply #'nconc
1151                                       (mapcar (lambda (signer)
1152                                                 (list "-u" signer))
1153                                               (epg-context-signers context)))))
1154                      (apply #'nconc
1155                             (mapcar
1156                              (lambda (recipient)
1157                                (list "-r"
1158                                      (epg-sub-key-id
1159                                       (car (epg-key-sub-key-list recipient)))))
1160                              recipients))
1161                      (if (epg-data-file plain)
1162                          (list (epg-data-file plain)))))
1163   (if sign
1164       (epg-wait-for-status context '("BEGIN_SIGNING")))
1165   (epg-wait-for-status context '("BEGIN_ENCRYPTION"))
1166   (if (and (epg-data-string plain)
1167            (eq (process-status (epg-context-process context)) 'run))
1168       (process-send-string (epg-context-process context)
1169                            (epg-data-string plain))))
1170
1171 ;;;###autoload
1172 (defun epg-encrypt-file (context plain recipients
1173                                  cipher &optional sign always-trust)
1174   "Encrypt a file PLAIN and store the result to a file CIPHER.
1175 If CIPHER is nil, it returns the result as a string.
1176 If RECIPIENTS is nil, it performs symmetric encryption."
1177   (unwind-protect
1178       (progn
1179         (if cipher
1180             (epg-context-set-output-file context cipher)
1181           (epg-context-set-output-file context
1182                                        (epg-make-temp-file "epg-output")))
1183         (epg-start-encrypt context (epg-make-data-from-file plain)
1184                            recipients sign always-trust)
1185         (epg-wait-for-completion context)
1186         (if (epg-context-result-for context 'error)
1187             (error "Encrypt failed: %S"
1188                    (epg-context-result-for context 'error)))
1189         (unless cipher
1190           (epg-read-output context)))
1191     (unless cipher
1192       (epg-delete-output-file context))
1193     (epg-reset context)))
1194
1195 ;;;###autoload
1196 (defun epg-encrypt-string (context plain recipients
1197                                    &optional sign always-trust)
1198   "Encrypt a string PLAIN.
1199 If RECIPIENTS is nil, it performs symmetric encryption."
1200   (unwind-protect
1201       (progn
1202         (epg-context-set-output-file context
1203                                      (epg-make-temp-file "epg-output"))
1204         (epg-start-encrypt context (epg-make-data-from-string plain)
1205                            recipients sign always-trust)
1206         (epg-flush context)
1207         (epg-wait-for-completion context)
1208         (if (epg-context-result-for context 'error)
1209             (error "Encrypt failed: %S"
1210                    (epg-context-result-for context 'error)))
1211         (epg-read-output context))
1212     (epg-delete-output-file context)
1213     (epg-reset context)))
1214
1215 ;;;###autoload
1216 (defun epg-start-export-keys (context keys)
1217   "Initiate an export keys operation.
1218
1219 If you use this function, you will need to wait for the completion of
1220 `epg-gpg-program' by using `epg-wait-for-completion' and call
1221 `epg-reset' to clear a temporaly output file.
1222 If you are unsure, use synchronous version of this function
1223 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1224   (epg-context-set-result context nil)
1225   (epg-start context (cons "--export"
1226                            (mapcar
1227                             (lambda (key)
1228                               (epg-sub-key-id
1229                                (car (epg-key-sub-key-list key))))
1230                             keys))))
1231
1232 ;;;###autoload
1233 (defun epg-export-keys-to-file (context keys file)
1234   "Extract public KEYS."
1235   (unwind-protect
1236       (progn
1237         (if keys
1238             (epg-context-set-output-file context file)
1239           (epg-context-set-output-file context
1240                                        (epg-make-temp-file "epg-output")))
1241         (epg-start-export-keys context keys)
1242         (epg-wait-for-completion context)
1243         (if (epg-context-result-for context 'error)
1244             (error "Export keys failed"))
1245         (unless file
1246           (epg-read-output context)))
1247     (unless file
1248       (epg-delete-output-file context))
1249     (epg-reset context)))
1250
1251 ;;;###autoload
1252 (defun epg-export-keys-to-string (context keys)
1253   "Extract public KEYS and return them as a string."
1254   (epg-export-keys-to-file context keys nil))
1255
1256 ;;;###autoload
1257 (defun epg-start-import-keys (context keys)
1258   "Initiate an import keys operation.
1259 KEYS is a data object.
1260
1261 If you use this function, you will need to wait for the completion of
1262 `epg-gpg-program' by using `epg-wait-for-completion' and call
1263 `epg-reset' to clear a temporaly output file.
1264 If you are unsure, use synchronous version of this function
1265 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1266   (epg-context-set-result context nil)
1267   (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1268   (epg-start context (list "--import" (epg-data-file keys)))
1269   (if (and (epg-data-string keys)
1270            (eq (process-status (epg-context-process context)) 'run))
1271       (process-send-string (epg-context-process context)
1272                            (epg-data-string keys))))
1273   
1274 (defun epg-import-keys-1 (context keys)
1275   (unwind-protect
1276       (progn
1277         (epg-start-import-keys context keys)
1278         (if (epg-data-file keys)
1279             (epg-flush context))
1280         (epg-wait-for-completion context)
1281         (if (epg-context-result-for context 'error)
1282             (error "Import keys failed"))
1283         (epg-read-output context))
1284     (epg-reset context)))
1285
1286 ;;;###autoload
1287 (defun epg-import-keys-from-file (context keys)
1288   "Add keys from a file KEYS."
1289   (epg-import-keys-1 context (epg-make-data-from-file keys)))
1290
1291 ;;;###autoload
1292 (defun epg-import-keys-from-string (context keys)
1293   "Add keys from a string KEYS."
1294   (epg-import-keys-1 context (epg-make-data-from-string keys)))
1295
1296 ;;;###autoload
1297 (defun epg-start-delete-keys (context keys &optional allow-secret)
1298   "Initiate an delete keys operation.
1299
1300 If you use this function, you will need to wait for the completion of
1301 `epg-gpg-program' by using `epg-wait-for-completion' and call
1302 `epg-reset' to clear a temporaly output file.
1303 If you are unsure, use synchronous version of this function
1304 `epg-delete-keys' instead."
1305   (epg-context-set-result context nil)
1306   (epg-start context (cons (if allow-secret
1307                                "--delete-secret-key"
1308                              "--delete-key")
1309                            (mapcar
1310                             (lambda (key)
1311                               (epg-sub-key-id
1312                                (car (epg-key-sub-key-list key))))
1313                             keys))))
1314
1315 ;;;###autoload
1316 (defun epg-delete-keys (context keys &optional allow-secret)
1317   "Delete KEYS from the key ring."
1318   (unwind-protect
1319       (progn
1320         (epg-start-delete-keys context keys)
1321         (epg-wait-for-completion context)
1322         (if (epg-context-result-for context 'error)
1323             (error "Delete key failed")))
1324     (epg-reset context)))
1325
1326 (provide 'epg)
1327
1328 ;;; epg.el ends here