330e7c46926c58c809d3bb1d6de8d2b1b20a02ba
[elisp/epg.git] / epg.el
1 (defgroup epg ()
2   "EasyPG, yet another GnuPG interface.")
3
4 (defcustom epg-gpg-program "gpg"
5   "The `gpg' executable."
6   :group 'epg
7   :type 'string)
8
9 (defvar epg-user-id nil
10   "GnuPG ID of your default identity.")
11
12 (defvar epg-user-id-alist nil
13   "An alist mapping from key ID to user ID.")
14
15 (defvar epg-read-point nil)
16 (defvar epg-pending-status-list nil)
17 (defvar epg-key-id nil)
18 (defvar epg-context nil)
19 (defvar epg-debug nil)
20
21 (defvar epg-colons-pub-spec
22   '((trust "[^:]")
23     (length "[0-9]+" 0 string-to-number)
24     (algorithm "[0-9]+" 0 string-to-number)
25     (key-id "[^:]+")
26     (creation-date "[0-9]+")
27     (expiration-date "[0-9]+")
28     nil
29     (ownertrust "[^:]")
30     nil
31     nil
32     (capability "[escaESCA]*"))
33   "The schema of keylisting output whose type is \"pub\".
34 This is used by `epg-list-keys'.")
35
36 (defvar epg-colons-sec-spec
37   '((trust "[^:]")
38     (length "[0-9]+" 0 string-to-number)
39     (algorithm "[0-9]+" 0 string-to-number)
40     (key-id "[^:]+")
41     (creation-date "[0-9]+")
42     (expiration-date "[0-9]+")
43     nil
44     (ownertrust "[^:]"))
45 "The schema of keylisting output whose type is \"sec\".
46 This is used by `epg-list-keys'.")
47
48 (defvar epg-colons-uid-spec
49   '((trust "[^:]")
50     nil
51     nil
52     nil
53     (creation-date "[0-9]+")
54     (expiration-date "[0-9]+")
55     (hash "[^:]+")
56     nil
57     (user-id "[^:]+"))
58   "The schema of keylisting output whose type is \"uid\".
59 This is used by `epg-list-keys'.")
60     
61 (defun epg-make-context (&optional protocol armor textmode include-certs)
62   "Return a context object."
63   (vector protocol armor textmode include-certs
64           (cons #'epg-passphrase-callback-function nil)
65           (cons #'epg-progress-callback-function nil)
66           nil nil nil nil))
67
68 (defun epg-context-protocol (context)
69   "Return the protocol used within the context."
70   (aref context 0))
71
72 (defun epg-context-armor (context)
73   "Return t if the output shouled be ASCII armored in the CONTEXT context."
74   (aref context 1))
75
76 (defun epg-context-textmode (context)
77   "Return t if canonical text mode should be used in the CONTEXT context."
78   (aref context 2))
79
80 (defun epg-context-include-certs (context)
81   "Return how many certificates should be included in an S/MIME signed
82 message."
83   (aref context 3))
84
85 (defun epg-context-passphrase-callback-info (context)
86   "Return the function used to query passphrase."
87   (aref context 4))
88
89 (defun epg-context-progress-callback-info (context)
90   "Return the function which handles progress update."
91   (aref context 5))
92
93 (defun epg-context-signers (context)
94   "Return the list of key-id for singning."
95   (aref context 6))
96
97 (defun epg-context-process (context)
98   "Return the process object of `epg-gpg-program'.
99 This function is for internal use only."
100   (aref context 7))
101
102 (defun epg-context-output-file (context)
103   "Return the output file of `epg-gpg-program'.
104 This function is for internal use only."
105   (aref context 8))
106
107 (defun epg-context-result (context)
108   "Return the result of the previous cryptographic operation."
109   (aref context 9))
110
111 (defun epg-context-set-protocol (context protocol)
112   "Set the protocol used within the context."
113   (aset context 0 protocol))
114
115 (defun epg-context-set-armor (context armor)
116   "Specify if the output shouled be ASCII armored in the CONTEXT context."
117   (aset context 1 armor))
118
119 (defun epg-context-set-textmode (context textmode)
120   "Specify if canonical text mode should be used in the CONTEXT context."
121   (aset context 2 textmode))
122
123 (defun epg-context-set-include-certs (context include-certs)
124  "Set how many certificates should be included in an S/MIME signed message."
125   (aset context 3 include-certs))
126
127 (defun epg-context-set-passphrase-callback-info (context
128                                                  passphrase-callback-info)
129   "Set the function used to query passphrase."
130   (aset context 4 passphrase-callback-info))
131
132 (defun epg-context-set-progress-callback-info (context progress-callback-info)
133   "Set the function which handles progress update."
134   (aset context 5 progress-callback-info))
135
136 (defun epg-context-set-signers (context signers)
137  "Set the list of key-id for singning."
138   (aset context 6 signers))
139
140 (defun epg-context-set-process (context process)
141   "Set the process object of `epg-gpg-program'.
142 This function is for internal use only."
143   (aset context 7 process))
144
145 (defun epg-context-set-output-file (context output-file)
146   "Set the output file of `epg-gpg-program'.
147 This function is for internal use only."
148   (aset context 8 output-file))
149
150 (defun epg-context-set-result (context result)
151   "Set the result of the previous cryptographic operation."
152   (aset context 9 result))
153
154 (defun epg-make-signature (status key-id user-id)
155   "Return a signature object."
156   (vector status key-id user-id nil))
157
158 (defun epg-signature-status (signature)
159   "Return the status code of SIGNATURE."
160   (aref signature 0))
161
162 (defun epg-signature-key-id (signature)
163   "Return the key-id of SIGNATURE."
164   (aref signature 1))
165
166 (defun epg-signature-user-id (signature)
167   "Return the user-id of SIGNATURE."
168   (aref signature 2))
169   
170 (defun epg-signature-validity (signature)
171   "Return the validity of SIGNATURE."
172   (aref signature 3))
173
174 (defun epg-signature-set-status (signature status)
175  "Set the status code of SIGNATURE."
176   (aset signature 0 status))
177
178 (defun epg-signature-set-key-id (signature key-id)
179  "Set the key-id of SIGNATURE."
180   (aset signature 1 key-id))
181
182 (defun epg-signature-set-user-id (signature user-id)
183  "Set the user-id of SIGNATURE."
184   (aset signature 2 user-id))
185   
186 (defun epg-signature-set-validity (signature validity)
187  "Set the validity of SIGNATURE."
188   (aset signature 3 validity))
189
190 (defun epg-context-result-for (context name)
191   (cdr (assq name (epg-context-result context))))
192
193 (defun epg-context-set-result-for (context name value)
194   (let* ((result (epg-context-result context))
195          (entry (assq name result)))
196     (if entry
197         (setcdr entry value)
198       (epg-context-set-result context (cons (cons name value) result)))))
199
200 (defun epg-start (context args)
201   "Start `epg-gpg-program' in a subprocess with given ARGS."
202   (let* ((args (append (list "--no-tty"
203                              "--status-fd" "1"
204                              "--command-fd" "0"
205                              "--yes") ; overwrite
206                        (if (epg-context-armor context) '("--armor"))
207                        (if (epg-context-textmode context) '("--textmode"))
208                        (if (epg-context-output-file context)
209                            (list "--output" (epg-context-output-file context)))
210                        args))
211          (coding-system-for-write 'binary)
212          process-connection-type
213          (orig-mode (default-file-modes))
214          (buffer (generate-new-buffer " *epg*"))
215          process)
216     (with-current-buffer buffer
217       (make-local-variable 'epg-read-point)
218       (setq epg-read-point (point-min))
219       (make-local-variable 'epg-pending-status-list)
220       (setq epg-pending-status-list nil)
221       (make-local-variable 'epg-key-id)
222       (setq epg-key-id nil)
223       (make-local-variable 'epg-context)
224       (setq epg-context context))
225     (unwind-protect
226         (progn
227           (set-default-file-modes 448)
228           (setq process
229                 (apply #'start-process "epg" buffer epg-gpg-program args)))
230       (set-default-file-modes orig-mode))
231     (set-process-filter process #'epg-process-filter)
232     (epg-context-set-process context process)))
233
234 (defun epg-process-filter (process input)
235   (if epg-debug
236       (save-excursion
237         (set-buffer (get-buffer-create  " *epg-debug*"))
238         (goto-char (point-max))
239         (insert input)))
240   (if (buffer-live-p (process-buffer process))
241       (save-excursion
242         (set-buffer (process-buffer process))
243         (goto-char (point-max))
244         (insert input)
245         (goto-char epg-read-point)
246         (beginning-of-line)
247         (while (looking-at ".*\n")      ;the input line is finished
248           (save-excursion
249             (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
250                 (let* ((status (match-string 1))
251                        (string (match-string 2))
252                        (symbol (intern-soft (concat "epg-status-" status))))
253                   (if (member status epg-pending-status-list)
254                       (setq epg-pending-status-list nil))
255                   (if (and symbol
256                            (fboundp symbol))
257                       (funcall symbol process string)))))
258           (forward-line))
259         (setq epg-read-point (point)))))
260
261 (defun epg-read-output (context)
262   (with-temp-buffer
263     (if (fboundp 'set-buffer-multibyte)
264         (set-buffer-multibyte nil))
265     (if (file-exists-p (epg-context-output-file context))
266         (let ((coding-system-for-read (if (epg-context-textmode context)
267                                           'raw-text
268                                         'binary)))
269           (insert-file-contents (epg-context-output-file context))
270           (buffer-string)))))
271
272 (defun epg-wait-for-status (context status-list)
273   (with-current-buffer (process-buffer (epg-context-process context))
274     (setq epg-pending-status-list status-list)
275     (while (and (eq (process-status (epg-context-process context)) 'run)
276                 epg-pending-status-list)
277       (accept-process-output (epg-context-process context) 1))))
278
279 (defun epg-wait-for-completion (context)
280   (if (eq (process-status (epg-context-process context)) 'run)
281       (process-send-eof (epg-context-process context)))
282   (while (eq (process-status (epg-context-process context)) 'run)
283     ;; We can't use accept-process-output instead of sit-for here
284     ;; because it may cause an interrupt during the sentinel execution.
285     (sit-for 0.1)))
286
287 (defun epg-reset (context)
288   (if (and (epg-context-process context)
289            (buffer-live-p (process-buffer (epg-context-process context))))
290       (kill-buffer (process-buffer (epg-context-process context))))
291   (epg-context-set-process context nil)
292   (if (file-exists-p (epg-context-output-file context))
293       (delete-file (epg-context-output-file context)))
294   (aset context 9 nil))
295
296 (defun epg-status-USERID_HINT (process string)
297   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
298       (let* ((key-id (match-string 1 string))
299              (user-id (match-string 2 string))
300              (entry (assoc key-id epg-user-id-alist)))
301         (if entry
302             (setcdr entry user-id)
303           (setq epg-user-id-alist (cons (cons key-id user-id)
304                                         epg-user-id-alist))))))
305
306 (defun epg-status-NEED_PASSPHRASE (process string)
307   (if (string-match "\\`\\([^ ]+\\)" string)
308       (setq epg-key-id (match-string 1 string))))
309
310 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
311   (setq epg-key-id 'SYM))
312
313 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
314   (setq epg-key-id 'PIN))
315
316 (defun epg-status-GET_HIDDEN (process string)
317   (let ((passphrase
318          (funcall (car (epg-context-passphrase-callback-info epg-context))
319                   epg-key-id
320                   (cdr (epg-context-passphrase-callback-info epg-context))))
321         string)
322     (if passphrase
323         (unwind-protect
324             (progn
325               (setq string (concat passphrase "\n"))
326               (fillarray passphrase 0)
327               (setq passphrase nil)
328               (process-send-string process string))
329           (if string
330               (fillarray string 0))))))
331
332 (defun epg-status-GOODSIG (process string)
333   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
334       (epg-context-set-result-for
335        epg-context
336        'verify
337        (cons (epg-make-signature 'good
338                                  (match-string 1 string)
339                                  (match-string 2 string))
340              (epg-context-result-for epg-context 'verify)))))
341
342 (defun epg-status-EXPSIG (process string)
343   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
344       (epg-context-set-result-for
345        epg-context
346        'verify
347        (cons (epg-make-signature 'expired
348                                  (match-string 1 string)
349                                  (match-string 2 string))
350              (epg-context-result-for epg-context 'verify)))))
351
352 (defun epg-status-EXPKEYSIG (process string)
353   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
354       (epg-context-set-result-for
355        epg-context
356        'verify
357        (cons (epg-make-signature 'expired-key
358                                  (match-string 1 string)
359                                  (match-string 2 string))
360              (epg-context-result-for epg-context 'verify)))))
361
362 (defun epg-status-REVKEYSIG (process string)
363   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
364       (epg-context-set-result-for
365        epg-context
366        'verify
367        (cons (epg-make-signature 'revoked-key
368                                  (match-string 1 string)
369                                  (match-string 2 string))
370              (epg-context-result-for epg-context 'verify)))))
371
372 (defun epg-status-BADSIG (process string)
373   (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
374       (epg-context-set-result-for
375        epg-context
376        'verify
377        (cons (epg-make-signature 'bad
378                                  (match-string 1 string)
379                                  (match-string 2 string))
380              (epg-context-result-for epg-context 'verify)))))
381
382 (defun epg-status-TRUST_UNDEFINED (process string)
383   (let ((signature (car (epg-context-result-for epg-context 'verify))))
384     (if (and signature
385              (eq (epg-signature-status signature) 'good))
386         (epg-signature-set-validity signature 'unknown))))
387
388 (defun epg-status-TRUST_NEVER (process string)
389   (let ((signature (car (epg-context-result-for epg-context 'verify))))
390     (if (and signature
391              (eq (epg-signature-status signature) 'good))
392         (epg-signature-set-validity signature 'never))))
393
394 (defun epg-status-TRUST_MARGINAL (process string)
395   (let ((signature (car (epg-context-result-for epg-context 'verify))))
396     (if (and signature
397              (eq (epg-signature-status signature) 'marginal))
398         (epg-signature-set-validity signature 'marginal))))
399
400 (defun epg-status-TRUST_FULLY (process string)
401   (let ((signature (car (epg-context-result-for epg-context 'verify))))
402     (if (and signature
403              (eq (epg-signature-status signature) 'good))
404         (epg-signature-set-validity signature 'full))))
405
406 (defun epg-status-TRUST_ULTIMATE (process string)
407   (let ((signature (car (epg-context-result-for epg-context 'verify))))
408     (if (and signature
409              (eq (epg-signature-status signature) 'good))
410         (epg-signature-set-validity signature 'full))))
411
412 (defun epg-status-PROGRESS (process string)
413   (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
414                     string)
415       (funcall (car (epg-context-progress-callback-info epg-context))
416                (match-string 1 string)
417                (match-string 2 string)
418                (string-to-number (match-string 3 string))
419                (string-to-number (match-string 4 string))
420                (cdr (epg-context-progress-callback-info epg-context)))))
421
422 (defun epg-status-DECRYPTION_FAILED (process string)
423   (epg-context-set-result-for
424    epg-context 'error
425    (cons 'decryption-failed
426          (epg-context-result-for epg-context 'error))))
427
428 (defun epg-status-NODATA (process string)
429   (epg-context-set-result-for
430    epg-context 'error
431    (cons (cons 'no-data (string-to-number string))
432          (epg-context-result-for epg-context 'error))))
433
434 (defun epg-status-UNEXPECTED (process string)
435   (epg-context-set-result-for
436    epg-context 'error
437    (cons (cons 'unexpected (string-to-number string))
438          (epg-context-result-for epg-context 'error))))
439
440 (defun epg-status-KEYEXPIRED (process string)
441   (epg-context-set-result-for
442    epg-context 'error
443    (cons (cons 'key-expired string)
444          (epg-context-result-for epg-context 'error))))
445
446 (defun epg-status-KEYREVOKED (process string)
447   (epg-context-set-result-for
448    epg-context 'error
449    (cons 'key-revoked
450          (epg-context-result-for epg-context 'error))))
451
452 (defun epg-status-BADARMOR (process string)
453   (epg-context-set-result-for
454    epg-context 'error
455    (cons 'bad-armor
456          (epg-context-result-for epg-context 'error))))
457
458 (defun epg-passphrase-callback-function (key-id handback)
459   (read-passwd
460    (if (eq key-id 'SYM)
461        "Passphrase for symmetric encryption: "
462      (if (eq key-id 'PIN)
463          "Passphrase for PIN: "
464        (format "Passphrase for %s: "
465                (let ((entry (assoc key-id epg-user-id-alist)))
466                  (if entry
467                      (cdr entry)
468                    key-id)))))))
469
470 (defun epg-progress-callback-function (what char current total handback)
471   (message "%s: %d%%/%d%%" what current total))
472
473 (defun epg-list-keys (name &optional secret)
474   "List keys associated with STRING."
475   (let ((args (list "--with-colons" "--no-greeting" "--batch"
476                     "--fixed-list-mode"
477                     (if secret "--list-secret-keys" "--list-keys")
478                     name))
479         keys type symbol pointer)
480     (with-temp-buffer
481       (apply #'call-process epg-gpg-program nil (list t nil) nil args)
482       (goto-char (point-min))
483       (while (looking-at "\\([a-z][a-z][a-z]\\):\\(.*\\)")
484         (setq type (match-string 1)
485               symbol (intern-soft (format "epg-colons-%s-spec" type)))
486         (if (member type '("pub" "sec"))
487             (setq keys (cons nil keys)))
488         (if (and symbol
489                  (boundp symbol))
490             (setcar keys (cons (cons (intern type)
491                                      (epg-parse-colons
492                                       (symbol-value symbol)
493                                       (match-string 2)))
494                                (car keys))))
495         (forward-line)))
496     (setq pointer keys)
497     (while pointer
498       (setcar pointer (nreverse (car pointer)))
499       (setq pointer (cdr pointer)))
500     (nreverse keys)))
501
502 (defun epg-parse-colons (alist string)
503   (let ((index 0)
504         result)
505     (while (and alist
506                 (or (null (car alist))
507                     (eq index
508                         (string-match
509                          (concat "\\(" (nth 1 (car alist)) "\\)?:")
510                          string index))))
511       (if (car alist)
512           (progn
513             (setq index (match-end 0))
514             (if (match-beginning 1)
515                 (setq result
516                       (cons (cons (car (car alist))
517                                   (funcall (or (nth 3 (car alist)) #'identity)
518                                            (match-string
519                                             (1+ (or (nth 2 (car alist)) 0))
520                                             string)))
521                             result))))
522         (setq index (1+ index)))
523       (setq alist (cdr alist)))
524     (nreverse result)))
525
526 (if (fboundp 'make-temp-file)
527     (defalias 'epg-make-temp-file 'make-temp-file)
528   ;; stolen from poe.el.
529   (defun epg-make-temp-file (prefix)
530     "Create a temporary file.
531 The returned file name (created by appending some random characters at the end
532 of PREFIX, and expanding against `temporary-file-directory' if necessary),
533 is guaranteed to point to a newly created empty file.
534 You can then use `write-region' to write new data into the file."
535     (let (tempdir tempfile)
536       (unwind-protect
537           (let (file)
538             ;; First, create a temporary directory.
539             (while (condition-case ()
540                        (progn
541                          (setq tempdir (make-temp-name
542                                         (concat
543                                          (file-name-directory prefix)
544                                          "DIR")))
545                          ;; return nil or signal an error.
546                          (make-directory tempdir))
547                      ;; let's try again.
548                      (file-already-exists t)))
549             (set-file-modes tempdir 448)
550             ;; Second, create a temporary file in the tempdir.
551             ;; There *is* a race condition between `make-temp-name'
552             ;; and `write-region', but we don't care it since we are
553             ;; in a private directory now.
554             (setq tempfile (make-temp-name (concat tempdir "/EMU")))
555             (write-region "" nil tempfile nil 'silent)
556             (set-file-modes tempfile 384)
557             ;; Finally, make a hard-link from the tempfile.
558             (while (condition-case ()
559                        (progn
560                          (setq file (make-temp-name prefix))
561                          ;; return nil or signal an error.
562                          (add-name-to-file tempfile file))
563                      ;; let's try again.
564                      (file-already-exists t)))
565             file)
566         ;; Cleanup the tempfile.
567         (and tempfile
568              (file-exists-p tempfile)
569              (delete-file tempfile))
570         ;; Cleanup the tempdir.
571         (and tempdir
572              (file-directory-p tempdir)
573              (delete-directory tempdir))))))
574
575 ;;;###autoload
576 (defun epg-decrypt-start (context input-file)
577   "Initiate a decrypt operation on INPUT-FILE.
578
579 If you use this function, you will need to wait for the completion of
580 `epg-gpg-program' by using `epg-wait-for-completion' and call
581 `epg-reset' to clear a temporaly output file.
582 If you are unsure, use synchronous version of this function
583 `epg-decrypt-string' instead."
584   (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
585   (epg-start context
586              (list "--decrypt" input-file))
587   (epg-wait-for-status context '("BEGIN_DECRYPTION")))
588
589 ;;;###autoload
590 (defun epg-decrypt-file (context input-file)
591   "Decrypt INPUT-FILE and return the plain text."
592   (unwind-protect
593       (progn
594         (epg-decrypt-start context input-file)
595         (epg-wait-for-completion context)
596         (if (epg-context-result-for context 'error)
597             (error "Decryption failed"))
598         (epg-read-output context))
599     (epg-reset context)))
600
601 ;;;###autoload
602 (defun epg-decrypt-string (context string)
603   "Decrypt STRING and return the plain text."
604   (let ((input-file (epg-make-temp-file "epg-input"))
605         (coding-system-for-write 'binary))
606     (unwind-protect
607         (progn
608           (write-region string nil input-file)
609           (epg-decrypt-file context input-file))
610       (if (file-exists-p input-file)
611           (delete-file input-file)))))
612
613 ;;;###autoload
614 (defun epg-verify-start (context signature &optional string)
615   "Initiate a verify operation on SIGNATURE.
616
617 For a detached signature, both SIGNATURE and STRING should be string.
618 For a normal or a clear text signature, STRING should be nil.
619
620 If you use this function, you will need to wait for the completion of
621 `epg-gpg-program' by using `epg-wait-for-completion' and call
622 `epg-reset' to clear a temporaly output file.
623 If you are unsure, use synchronous version of this function
624 `epg-verify-string' instead."
625   (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
626   (if string
627       ;; Detached signature.
628       (progn
629         (epg-start context
630                    (append (list "--verify")
631                            (list signature "-")))
632         (if (eq (process-status (epg-context-process context)) 'run)
633             (process-send-string (epg-context-process context) string)))
634     ;; Normal (or cleartext) signature.
635     (epg-start context (list "--verify"))
636     (if (eq (process-status (epg-context-process context)) 'run)
637         (process-send-string (epg-context-process context) signature))))
638
639 ;;;###autoload
640 (defun epg-verify-string (context signature &optional string)
641   "Verify SIGNATURE.
642
643 For a detached signature, both SIGNATURE and STRING should be string.
644 For a normal or a clear text signature, STRING should be nil."
645   (let ((input-file (epg-make-temp-file "epg-input"))
646         (coding-system-for-write 'binary))
647     (unwind-protect
648         (progn
649           (if string
650               (write-region signature nil input-file))
651           (epg-verify-start context input-file string)
652           (epg-wait-for-completion context)
653           (epg-context-result-for context 'verify))
654       (epg-reset context)
655       (if (file-exists-p input-file)
656           (delete-file input-file)))))
657
658 ;;;###autoload
659 (defun epg-sign-start (context string &optional mode)
660   "Initiate a sign operation on STRING.
661
662 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
663 If MODE is t or 'detached, it makes a detached signature.
664 Otherwise, it makes a normal signature.
665
666 If you use this function, you will need to wait for the completion of
667 `epg-gpg-program' by using `epg-wait-for-completion' and call
668 `epg-reset' to clear a temporaly output file.
669 If you are unsure, use synchronous version of this function
670 `epg-sign-string' instead."
671   (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
672   (epg-start context
673              (append (list (if (eq mode 'clearsign)
674                                "--clearsign"
675                              (if (or (eq mode t) (eq mode 'detached))
676                                  "--detach-sign"
677                                "--sign")))
678                      (apply #'nconc
679                             (mapcar (lambda (signer)
680                                       (list "-u" signer))
681                                     (epg-context-signers context)))))
682   (epg-wait-for-status context '("BEGIN_SIGNING"))
683   (if (eq (process-status (epg-context-process context)) 'run)
684       (process-send-string (epg-context-process context) string)))
685
686 ;;;###autoload
687 (defun epg-sign-string (context string &optional mode)
688   "Sign STRING and return the output as string.
689 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
690 If MODE is t or 'detached, it makes a detached signature.
691 Otherwise, it makes a normal signature."
692   (unwind-protect
693       (progn
694         (epg-sign-start context string mode)
695         (epg-wait-for-completion context)
696         (if (epg-context-result-for context 'error)
697             (error "Sign failed"))
698         (epg-read-output context))
699     (epg-reset context)))
700
701 ;;;###autoload
702 (defun epg-encrypt-start (context string recipients
703                                   &optional sign always-trust)
704   "Initiate a encrypt operation on STRING.
705 If RECIPIENTS is nil, it performs symmetric encryption.
706
707 If you use this function, you will need to wait for the completion of
708 `epg-gpg-program' by using `epg-wait-for-completion' and call
709 `epg-reset' to clear a temporaly output file.
710 If you are unsure, use synchronous version of this function
711 `epg-encrypt-string' instead."
712   (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
713   (epg-start context
714              (append (if always-trust '("--always-trust"))
715                      (if recipients '("--encrypt") '("--symmetric"))
716                      (if sign
717                          (cons "--sign"
718                                (apply #'nconc
719                                       (mapcar (lambda (signer)
720                                                 (list "-u" signer))
721                                               (epg-context-signers context)))))
722                      (apply #'nconc
723                             (mapcar (lambda (recipient)
724                                       (list "-r" recipient))
725                                     recipients))))
726   (if sign
727       (epg-wait-for-status context '("BEGIN_SIGNING"))
728     (if (null recipients)
729         (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
730   (if (eq (process-status (epg-context-process context)) 'run)
731       (process-send-string (epg-context-process context) string)))
732
733 ;;;###autoload
734 (defun epg-encrypt-string (context string recipients
735                                    &optional sign always-trust)
736   "Encrypt STRING.
737 If RECIPIENTS is nil, it performs symmetric encryption."
738   (unwind-protect
739       (progn
740         (epg-encrypt-start context string recipients sign always-trust)
741         (epg-wait-for-completion context)
742         (if (epg-context-result-for context 'error)
743             (error "Encrypt failed"))
744         (epg-read-output context))
745     (epg-reset context)))
746
747 (provide 'epg)
748
749 ;;; epg.el ends here