1 ;;; epg.el --- EasyPG, yet another GnuPG interface.
2 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
3 ;; 2005, 2006 Free Software Foundation, Inc.
4 ;; Copyright (C) 2006 Daiki Ueno
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, GnuPG
9 ;; This file is part of EasyPG.
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)
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.
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.
29 "EasyPG, yet another GnuPG interface.")
31 (defcustom epg-gpg-program "gpg"
32 "The `gpg' executable."
36 (defvar epg-user-id nil
37 "GnuPG ID of your default identity.")
39 (defvar epg-user-id-alist nil
40 "An alist mapping from key ID to user ID.")
42 (defvar epg-read-point nil)
43 (defvar epg-pending-status-list nil)
44 (defvar epg-key-id nil)
45 (defvar epg-context nil)
46 (defvar epg-debug nil)
48 (defvar epg-colons-pub-spec
50 (length "[0-9]+" 0 string-to-number)
51 (algorithm "[0-9]+" 0 string-to-number)
53 (creation-date "[0-9]+")
54 (expiration-date "[0-9]+")
59 (capability "[escaESCA]*"))
60 "The schema of keylisting output whose type is \"pub\".
61 This is used by `epg-list-keys'.")
63 (defvar epg-colons-sec-spec
65 (length "[0-9]+" 0 string-to-number)
66 (algorithm "[0-9]+" 0 string-to-number)
68 (creation-date "[0-9]+")
69 (expiration-date "[0-9]+")
72 "The schema of keylisting output whose type is \"sec\".
73 This is used by `epg-list-keys'.")
75 (defvar epg-colons-uid-spec
80 (creation-date "[0-9]+")
81 (expiration-date "[0-9]+")
85 "The schema of keylisting output whose type is \"uid\".
86 This is used by `epg-list-keys'.")
88 (defconst epg-cipher-algorithm-alist
100 (defconst epg-pubkey-algorithm-alist
108 (defconst epg-digest-algorithm-alist
116 (defconst epg-compress-algorithm-alist
122 (defvar epg-prompt-alist nil)
124 (defun epg-make-data-from-file (file)
125 "Make a data object from FILE."
128 (defun epg-make-data-from-string (string)
129 "Make a data object from STRING."
132 (defun epg-data-file (data)
133 "Return the file of DATA."
136 (defun epg-data-string (data)
137 "Return the string of DATA."
140 (defun epg-make-context (&optional protocol armor textmode include-certs)
141 "Return a context object."
142 (vector protocol armor textmode include-certs
143 #'epg-passphrase-callback-function
144 #'epg-progress-callback-function
147 (defun epg-context-protocol (context)
148 "Return the protocol used within the context."
151 (defun epg-context-armor (context)
152 "Return t if the output shouled be ASCII armored in the CONTEXT context."
155 (defun epg-context-textmode (context)
156 "Return t if canonical text mode should be used in the CONTEXT context."
159 (defun epg-context-include-certs (context)
160 "Return how many certificates should be included in an S/MIME signed
164 (defun epg-context-passphrase-callback (context)
165 "Return the function used to query passphrase."
168 (defun epg-context-progress-callback (context)
169 "Return the function which handles progress update."
172 (defun epg-context-signers (context)
173 "Return the list of key-id for singning."
176 (defun epg-context-process (context)
177 "Return the process object of `epg-gpg-program'.
178 This function is for internal use only."
181 (defun epg-context-output-file (context)
182 "Return the output file of `epg-gpg-program'.
183 This function is for internal use only."
186 (defun epg-context-result (context)
187 "Return the result of the previous cryptographic operation."
190 (defun epg-context-set-protocol (context protocol)
191 "Set the protocol used within the context."
192 (aset context 0 protocol))
194 (defun epg-context-set-armor (context armor)
195 "Specify if the output shouled be ASCII armored in the CONTEXT context."
196 (aset context 1 armor))
198 (defun epg-context-set-textmode (context textmode)
199 "Specify if canonical text mode should be used in the CONTEXT context."
200 (aset context 2 textmode))
202 (defun epg-context-set-include-certs (context include-certs)
203 "Set how many certificates should be included in an S/MIME signed message."
204 (aset context 3 include-certs))
206 (defun epg-context-set-passphrase-callback (context
208 "Set the function used to query passphrase."
209 (aset context 4 passphrase-callback))
211 (defun epg-context-set-progress-callback (context progress-callback)
212 "Set the function which handles progress update."
213 (aset context 5 progress-callback))
215 (defun epg-context-set-signers (context signers)
216 "Set the list of key-id for singning."
217 (aset context 6 signers))
219 (defun epg-context-set-process (context process)
220 "Set the process object of `epg-gpg-program'.
221 This function is for internal use only."
222 (aset context 7 process))
224 (defun epg-context-set-output-file (context output-file)
225 "Set the output file of `epg-gpg-program'.
226 This function is for internal use only."
227 (aset context 8 output-file))
229 (defun epg-context-set-result (context result)
230 "Set the result of the previous cryptographic operation."
231 (aset context 9 result))
233 (defun epg-make-signature (status key-id user-id)
234 "Return a signature object."
235 (vector status key-id user-id nil nil))
237 (defun epg-signature-status (signature)
238 "Return the status code of SIGNATURE."
241 (defun epg-signature-key-id (signature)
242 "Return the key-id of SIGNATURE."
245 (defun epg-signature-user-id (signature)
246 "Return the user-id of SIGNATURE."
249 (defun epg-signature-validity (signature)
250 "Return the validity of SIGNATURE."
253 (defun epg-signature-fingerprint (signature)
254 "Return the fingerprint of SIGNATURE."
257 (defun epg-signature-set-status (signature status)
258 "Set the status code of SIGNATURE."
259 (aset signature 0 status))
261 (defun epg-signature-set-key-id (signature key-id)
262 "Set the key-id of SIGNATURE."
263 (aset signature 1 key-id))
265 (defun epg-signature-set-user-id (signature user-id)
266 "Set the user-id of SIGNATURE."
267 (aset signature 2 user-id))
269 (defun epg-signature-set-validity (signature validity)
270 "Set the validity of SIGNATURE."
271 (aset signature 3 validity))
273 (defun epg-signature-set-fingerprint (signature fingerprint)
274 "Set the fingerprint of SIGNATURE."
275 (aset signature 4 fingerprint))
277 (defun epg-context-result-for (context name)
278 (cdr (assq name (epg-context-result context))))
280 (defun epg-context-set-result-for (context name value)
281 (let* ((result (epg-context-result context))
282 (entry (assq name result)))
285 (epg-context-set-result context (cons (cons name value) result)))))
287 (defun epg-start (context args)
288 "Start `epg-gpg-program' in a subprocess with given ARGS."
289 (let* ((args (append (list "--no-tty"
293 (if (epg-context-armor context) '("--armor"))
294 (if (epg-context-textmode context) '("--textmode"))
295 (if (epg-context-output-file context)
296 (list "--output" (epg-context-output-file context)))
298 (coding-system-for-write 'binary)
299 process-connection-type
300 (orig-mode (default-file-modes))
301 (buffer (generate-new-buffer " *epg*"))
303 (with-current-buffer buffer
304 (make-local-variable 'epg-read-point)
305 (setq epg-read-point (point-min))
306 (make-local-variable 'epg-pending-status-list)
307 (setq epg-pending-status-list nil)
308 (make-local-variable 'epg-key-id)
309 (setq epg-key-id nil)
310 (make-local-variable 'epg-context)
311 (setq epg-context context))
314 (set-default-file-modes 448)
316 (apply #'start-process "epg" buffer epg-gpg-program args)))
317 (set-default-file-modes orig-mode))
318 (set-process-filter process #'epg-process-filter)
319 (epg-context-set-process context process)))
321 (defun epg-process-filter (process input)
324 (set-buffer (get-buffer-create " *epg-debug*"))
325 (goto-char (point-max))
327 (if (buffer-live-p (process-buffer process))
329 (set-buffer (process-buffer process))
330 (goto-char (point-max))
332 (goto-char epg-read-point)
334 (while (looking-at ".*\n") ;the input line is finished
336 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
337 (let* ((status (match-string 1))
338 (string (match-string 2))
339 (symbol (intern-soft (concat "epg-status-" status))))
340 (if (member status epg-pending-status-list)
341 (setq epg-pending-status-list nil))
344 (funcall symbol process string)))))
346 (setq epg-read-point (point)))))
348 (defun epg-read-output (context)
350 (if (fboundp 'set-buffer-multibyte)
351 (set-buffer-multibyte nil))
352 (if (file-exists-p (epg-context-output-file context))
353 (let ((coding-system-for-read (if (epg-context-textmode context)
356 (insert-file-contents (epg-context-output-file context))
359 (defun epg-wait-for-status (context status-list)
360 (with-current-buffer (process-buffer (epg-context-process context))
361 (setq epg-pending-status-list status-list)
362 (while (and (eq (process-status (epg-context-process context)) 'run)
363 epg-pending-status-list)
364 (accept-process-output (epg-context-process context) 1))))
366 (defun epg-wait-for-completion (context)
367 (if (eq (process-status (epg-context-process context)) 'run)
368 (process-send-eof (epg-context-process context)))
369 (while (eq (process-status (epg-context-process context)) 'run)
370 ;; We can't use accept-process-output instead of sit-for here
371 ;; because it may cause an interrupt during the sentinel execution.
374 (defun epg-reset (context)
375 (if (and (epg-context-process context)
376 (buffer-live-p (process-buffer (epg-context-process context))))
377 (kill-buffer (process-buffer (epg-context-process context))))
378 (epg-context-set-process context nil))
380 (defun epg-delete-output-file (context)
381 (if (and (epg-context-output-file context)
382 (file-exists-p (epg-context-output-file context)))
383 (delete-file (epg-context-output-file context))))
385 (defun epg-status-USERID_HINT (process string)
386 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
387 (let* ((key-id (match-string 1 string))
388 (user-id (match-string 2 string))
389 (entry (assoc key-id epg-user-id-alist)))
391 (setcdr entry user-id)
392 (setq epg-user-id-alist (cons (cons key-id user-id)
393 epg-user-id-alist))))))
395 (defun epg-status-NEED_PASSPHRASE (process string)
396 (if (string-match "\\`\\([^ ]+\\)" string)
397 (setq epg-key-id (match-string 1 string))))
399 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
400 (setq epg-key-id 'SYM))
402 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
403 (setq epg-key-id 'PIN))
405 (defun epg-status-GET_HIDDEN (process string)
407 (funcall (if (consp (epg-context-passphrase-callback epg-context))
408 (car (epg-context-passphrase-callback epg-context))
409 (epg-context-passphrase-callback epg-context))
411 (if (consp (epg-context-passphrase-callback epg-context))
412 (cdr (epg-context-passphrase-callback epg-context)))))
417 (setq string (concat passphrase "\n"))
418 (fillarray passphrase 0)
419 (setq passphrase nil)
420 (process-send-string process string))
422 (fillarray string 0))))))
424 (defun epg-status-GET_BOOL (process string)
425 (let ((entry (assoc string epg-prompt-alist)))
426 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
427 (process-send-string process "y\n")
428 (process-send-string process "n\n"))))
430 (defun epg-status-GET_LINE (process string)
431 (let* ((entry (assoc string epg-prompt-alist))
432 (string (read-string (if entry (cdr entry) (concat string ": ")))))
433 (process-send-string process (concat string "\n"))))
435 (defun epg-status-GOODSIG (process string)
436 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
437 (epg-context-set-result-for
440 (cons (epg-make-signature 'good
441 (match-string 1 string)
442 (match-string 2 string))
443 (epg-context-result-for epg-context 'verify)))))
445 (defun epg-status-EXPSIG (process string)
446 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
447 (epg-context-set-result-for
450 (cons (epg-make-signature 'expired
451 (match-string 1 string)
452 (match-string 2 string))
453 (epg-context-result-for epg-context 'verify)))))
455 (defun epg-status-EXPKEYSIG (process string)
456 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
457 (epg-context-set-result-for
460 (cons (epg-make-signature 'expired-key
461 (match-string 1 string)
462 (match-string 2 string))
463 (epg-context-result-for epg-context 'verify)))))
465 (defun epg-status-REVKEYSIG (process string)
466 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
467 (epg-context-set-result-for
470 (cons (epg-make-signature 'revoked-key
471 (match-string 1 string)
472 (match-string 2 string))
473 (epg-context-result-for epg-context 'verify)))))
475 (defun epg-status-BADSIG (process string)
476 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
477 (epg-context-set-result-for
480 (cons (epg-make-signature 'bad
481 (match-string 1 string)
482 (match-string 2 string))
483 (epg-context-result-for epg-context 'verify)))))
485 (defun epg-status-VALIDSIG (process string)
486 (let ((signature (car (epg-context-result-for epg-context 'verify))))
488 (eq (epg-signature-status signature) 'good)
489 (string-match "\\`\\([^ ]+\\) " string))
490 (epg-signature-set-fingerprint signature (match-string 1 string)))))
492 (defun epg-status-TRUST_UNDEFINED (process string)
493 (let ((signature (car (epg-context-result-for epg-context 'verify))))
495 (eq (epg-signature-status signature) 'good))
496 (epg-signature-set-validity signature 'undefined))))
498 (defun epg-status-TRUST_NEVER (process string)
499 (let ((signature (car (epg-context-result-for epg-context 'verify))))
501 (eq (epg-signature-status signature) 'good))
502 (epg-signature-set-validity signature 'never))))
504 (defun epg-status-TRUST_MARGINAL (process string)
505 (let ((signature (car (epg-context-result-for epg-context 'verify))))
507 (eq (epg-signature-status signature) 'marginal))
508 (epg-signature-set-validity signature 'marginal))))
510 (defun epg-status-TRUST_FULLY (process string)
511 (let ((signature (car (epg-context-result-for epg-context 'verify))))
513 (eq (epg-signature-status signature) 'good))
514 (epg-signature-set-validity signature 'fully))))
516 (defun epg-status-TRUST_ULTIMATE (process string)
517 (let ((signature (car (epg-context-result-for epg-context 'verify))))
519 (eq (epg-signature-status signature) 'good))
520 (epg-signature-set-validity signature 'ultimate))))
522 (defun epg-status-PROGRESS (process string)
523 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
525 (funcall (if (consp (epg-context-progress-callback epg-context))
526 (car (epg-context-progress-callback epg-context))
527 (epg-context-progress-callback epg-context))
528 (match-string 1 string)
529 (match-string 2 string)
530 (string-to-number (match-string 3 string))
531 (string-to-number (match-string 4 string))
532 (if (consp (epg-context-progress-callback epg-context))
533 (cdr (epg-context-progress-callback epg-context))))))
535 (defun epg-status-DECRYPTION_FAILED (process string)
536 (epg-context-set-result-for
538 (cons 'decryption-failed
539 (epg-context-result-for epg-context 'error))))
541 (defun epg-status-NODATA (process string)
542 (epg-context-set-result-for
544 (cons (cons 'no-data (string-to-number string))
545 (epg-context-result-for epg-context 'error))))
547 (defun epg-status-UNEXPECTED (process string)
548 (epg-context-set-result-for
550 (cons (cons 'unexpected (string-to-number string))
551 (epg-context-result-for epg-context 'error))))
553 (defun epg-status-KEYEXPIRED (process string)
554 (epg-context-set-result-for
556 (cons (cons 'key-expired string)
557 (epg-context-result-for epg-context 'error))))
559 (defun epg-status-KEYREVOKED (process string)
560 (epg-context-set-result-for
563 (epg-context-result-for epg-context 'error))))
565 (defun epg-status-BADARMOR (process string)
566 (epg-context-set-result-for
569 (epg-context-result-for epg-context 'error))))
571 (defun epg-passphrase-callback-function (key-id handback)
574 "Passphrase for symmetric encryption: "
576 "Passphrase for PIN: "
577 (let ((entry (assoc key-id epg-user-id-alist)))
579 (format "Passphrase for %s %s: " key-id (cdr entry))
580 (format "Passphrase for %s: " key-id)))))))
582 (defun epg-progress-callback-function (what char current total handback)
583 (message "%s: %d%%/%d%%" what current total))
585 (defun epg-configuration ()
586 "Return a list of internal configuration parameters of `epg-gpg-program'."
587 (let (configuration type)
589 (apply #'call-process epg-gpg-program nil (list t nil) nil
590 '("--with-colons" "--list-config"))
591 (goto-char (point-min))
592 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
593 (setq type (intern (match-string 1))
594 config (cons (cons type
596 '(pubkey cipher digest compress))
597 (mapcar #'string-to-number
598 (delete "" (split-string
605 (defun epg-list-keys (name &optional secret)
606 "List keys associated with STRING."
607 (let ((args (list "--with-colons" "--no-greeting" "--batch"
609 (if secret "--list-secret-keys" "--list-keys")
611 keys type symbol pointer)
613 (apply #'call-process epg-gpg-program nil (list t nil) nil args)
614 (goto-char (point-min))
615 (while (re-search-forward "^\\([a-z][a-z][a-z]\\):\\(.*\\)" nil t)
616 (setq type (match-string 1)
617 symbol (intern-soft (format "epg-colons-%s-spec" type)))
618 (if (member type '("pub" "sec"))
619 (setq keys (cons nil keys)))
622 (setcar keys (cons (cons (intern type)
624 (symbol-value symbol)
630 (setcar pointer (nreverse (car pointer)))
631 (setq pointer (cdr pointer)))
634 (defun epg-parse-colons (alist string)
638 (or (null (car alist))
641 (concat "\\(" (nth 1 (car alist)) "\\)?:")
645 (setq index (match-end 0))
646 (if (match-beginning 1)
648 (cons (cons (car (car alist))
649 (funcall (or (nth 3 (car alist)) #'identity)
651 (1+ (or (nth 2 (car alist)) 0))
654 (setq index (1+ index)))
655 (setq alist (cdr alist)))
658 (if (fboundp 'make-temp-file)
659 (defalias 'epg-make-temp-file 'make-temp-file)
660 ;; stolen from poe.el.
661 (defun epg-make-temp-file (prefix)
662 "Create a temporary file.
663 The returned file name (created by appending some random characters at the end
664 of PREFIX, and expanding against `temporary-file-directory' if necessary),
665 is guaranteed to point to a newly created empty file.
666 You can then use `write-region' to write new data into the file."
667 (let (tempdir tempfile)
670 ;; First, create a temporary directory.
671 (while (condition-case ()
673 (setq tempdir (make-temp-name
675 (file-name-directory prefix)
677 ;; return nil or signal an error.
678 (make-directory tempdir))
680 (file-already-exists t)))
681 (set-file-modes tempdir 448)
682 ;; Second, create a temporary file in the tempdir.
683 ;; There *is* a race condition between `make-temp-name'
684 ;; and `write-region', but we don't care it since we are
685 ;; in a private directory now.
686 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
687 (write-region "" nil tempfile nil 'silent)
688 (set-file-modes tempfile 384)
689 ;; Finally, make a hard-link from the tempfile.
690 (while (condition-case ()
692 (setq file (make-temp-name prefix))
693 ;; return nil or signal an error.
694 (add-name-to-file tempfile file))
696 (file-already-exists t)))
698 ;; Cleanup the tempfile.
700 (file-exists-p tempfile)
701 (delete-file tempfile))
702 ;; Cleanup the tempdir.
704 (file-directory-p tempdir)
705 (delete-directory tempdir))))))
708 (defun epg-start-decrypt (context cipher)
709 "Initiate a decrypt operation on CIPHER.
710 CIPHER is a data object.
712 If you use this function, you will need to wait for the completion of
713 `epg-gpg-program' by using `epg-wait-for-completion' and call
714 `epg-reset' to clear a temporaly output file.
715 If you are unsure, use synchronous version of this function
716 `epg-decrypt-file' or `epg-decrypt-string' instead."
717 (unless (epg-data-file cipher)
718 (error "Not a file"))
719 (epg-context-set-result context nil)
720 (epg-start context (list "--decrypt" (epg-data-file cipher)))
721 (epg-wait-for-status context '("BEGIN_DECRYPTION")))
724 (defun epg-decrypt-file (context cipher plain)
725 "Decrypt a file CIPHER and store the result to a file PLAIN.
726 If PLAIN is nil, it returns the result as a string."
730 (epg-context-set-output-file context plain)
731 (epg-context-set-output-file context
732 (epg-make-temp-file "epg-output")))
733 (epg-start-decrypt context (epg-make-data-from-file cipher))
734 (epg-wait-for-completion context)
735 (if (epg-context-result-for context 'error)
736 (error "Decryption failed"))
738 (epg-read-output context)))
740 (epg-delete-output-file context))
741 (epg-reset context)))
744 (defun epg-decrypt-string (context cipher)
745 "Decrypt a string CIPHER and return the plain text."
746 (let ((input-file (epg-make-temp-file "epg-input"))
747 (coding-system-for-write 'binary))
750 (write-region cipher nil input-file)
751 (epg-context-set-output-file context
752 (epg-make-temp-file "epg-output"))
753 (epg-start-decrypt context (epg-make-data-from-file input-file))
754 (epg-wait-for-completion context)
755 (if (epg-context-result-for context 'error)
756 (error "Decryption failed"))
757 (epg-read-output context))
758 (epg-delete-output-file context)
759 (if (file-exists-p input-file)
760 (delete-file input-file))
761 (epg-reset context))))
764 (defun epg-start-verify (context signature &optional signed-text)
765 "Initiate a verify operation on SIGNATURE.
766 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
768 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
769 For a normal or a clear text signature, SIGNED-TEXT should be nil.
771 If you use this function, you will need to wait for the completion of
772 `epg-gpg-program' by using `epg-wait-for-completion' and call
773 `epg-reset' to clear a temporaly output file.
774 If you are unsure, use synchronous version of this function
775 `epg-verify-file' or `epg-verify-string' instead."
776 (epg-context-set-result context nil)
778 ;; Detached signature.
779 (if (epg-data-file signed-text)
780 (epg-start context (list "--verify" (epg-data-file signature)
781 (epg-data-file signed-text)))
782 (epg-start context (list "--verify" (epg-data-file signature) "-"))
783 (if (eq (process-status (epg-context-process context)) 'run)
784 (process-send-string (epg-context-process context)
785 (epg-data-string signed-text))))
786 ;; Normal (or cleartext) signature.
787 (if (epg-data-file signature)
788 (epg-start context (list "--verify" (epg-data-file signature)))
789 (epg-start context (list "--verify"))
790 (if (eq (process-status (epg-context-process context)) 'run)
791 (process-send-string (epg-context-process context)
792 (epg-data-string signature))))))
795 (defun epg-verify-file (context signature &optional signed-text plain)
796 "Verify a file SIGNATURE.
797 SIGNED-TEXT and PLAIN are also a file if they are specified.
799 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
800 For a normal or a clear text signature, SIGNED-TEXT should be nil."
804 (epg-context-set-output-file context plain)
805 (epg-context-set-output-file context
806 (epg-make-temp-file "epg-output")))
808 (epg-start-verify context
809 (epg-make-data-from-file signature)
810 (epg-make-data-from-file signed-text))
811 (epg-start-verify context
812 (epg-make-data-from-file signature)))
813 (epg-wait-for-completion context)
815 (epg-read-output context)))
817 (epg-delete-output-file context))
818 (epg-reset context)))
821 (defun epg-verify-string (context signature &optional signed-text)
822 "Verify a string SIGNATURE.
823 SIGNED-TEXT is a string if it is specified.
825 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
826 For a normal or a clear text signature, SIGNED-TEXT should be nil."
827 (let ((coding-system-for-write 'binary)
831 (epg-context-set-output-file context
832 (epg-make-temp-file "epg-output"))
835 (setq input-file (epg-make-temp-file "epg-signature"))
836 (write-region signature nil input-file)
837 (epg-start-verify context
838 (epg-make-data-from-file input-file)
839 (epg-make-data-from-string signed-text)))
840 (epg-start-verify context (epg-make-data-from-string signature)))
841 (epg-wait-for-completion context)
842 (epg-read-output context))
843 (epg-delete-output-file context)
845 (file-exists-p input-file))
846 (delete-file input-file))
847 (epg-reset context))))
850 (defun epg-start-sign (context plain &optional mode)
851 "Initiate a sign operation on PLAIN.
852 PLAIN is a data object.
854 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
855 If MODE is t or 'detached, it makes a detached signature.
856 Otherwise, it makes a normal signature.
858 If you use this function, you will need to wait for the completion of
859 `epg-gpg-program' by using `epg-wait-for-completion' and call
860 `epg-reset' to clear a temporaly output file.
861 If you are unsure, use synchronous version of this function
862 `epg-sign-file' or `epg-sign-string' instead."
863 (epg-context-set-result context nil)
865 (append (list (if (eq mode 'clearsign)
867 (if (or (eq mode t) (eq mode 'detached))
871 (mapcar (lambda (signer)
873 (epg-context-signers context)))
874 (if (epg-data-file plain)
875 (list (epg-data-file plain)))))
876 (epg-wait-for-status context '("BEGIN_SIGNING"))
877 (if (and (epg-data-string plain)
878 (eq (process-status (epg-context-process context)) 'run))
879 (process-send-string (epg-context-process context)
880 (epg-data-string plain))))
883 (defun epg-sign-file (context plain signature &optional mode)
884 "Sign a file PLAIN and store the result to a file SIGNATURE.
885 If SIGNATURE is nil, it returns the result as a string.
886 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
887 If MODE is t or 'detached, it makes a detached signature.
888 Otherwise, it makes a normal signature."
892 (epg-context-set-output-file context signature)
893 (epg-context-set-output-file context
894 (epg-make-temp-file "epg-output")))
895 (epg-start-sign context (epg-make-data-from-file plain) mode)
896 (epg-wait-for-completion context)
897 (if (epg-context-result-for context 'error)
898 (error "Sign failed"))
900 (epg-read-output context)))
902 (epg-delete-output-file context))
903 (epg-reset context)))
906 (defun epg-sign-string (context plain &optional mode)
907 "Sign a string PLAIN and return the output as string.
908 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
909 If MODE is t or 'detached, it makes a detached signature.
910 Otherwise, it makes a normal signature."
913 (epg-context-set-output-file context
914 (epg-make-temp-file "epg-output"))
915 (epg-start-sign context (epg-make-data-from-string plain) mode)
916 (epg-wait-for-completion context)
917 (if (epg-context-result-for context 'error)
918 (error "Sign failed"))
919 (epg-read-output context))
920 (epg-delete-output-file context)
921 (epg-reset context)))
924 (defun epg-start-encrypt (context plain recipients
925 &optional sign always-trust)
926 "Initiate an encrypt operation on PLAIN.
927 PLAIN is a data object.
928 If RECIPIENTS is nil, it performs symmetric encryption.
930 If you use this function, you will need to wait for the completion of
931 `epg-gpg-program' by using `epg-wait-for-completion' and call
932 `epg-reset' to clear a temporaly output file.
933 If you are unsure, use synchronous version of this function
934 `epg-encrypt-file' or `epg-encrypt-string' instead."
935 (epg-context-set-result context nil)
937 (append (if always-trust '("--always-trust"))
938 (if recipients '("--encrypt") '("--symmetric"))
942 (mapcar (lambda (signer)
944 (epg-context-signers context)))))
946 (mapcar (lambda (recipient)
947 (list "-r" recipient))
949 (if (epg-data-file plain)
950 (list (epg-data-file plain)))))
952 (epg-wait-for-status context '("BEGIN_SIGNING"))
953 (if (null recipients)
954 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
955 (if (and (epg-data-string plain)
956 (eq (process-status (epg-context-process context)) 'run))
957 (process-send-string (epg-context-process context)
958 (epg-data-string plain))))
961 (defun epg-encrypt-file (context plain recipients
962 cipher &optional sign always-trust)
963 "Encrypt a file PLAIN and store the result to a file CIPHER.
964 If CIPHER is nil, it returns the result as a string.
965 If RECIPIENTS is nil, it performs symmetric encryption."
969 (epg-context-set-output-file context cipher)
970 (epg-context-set-output-file context
971 (epg-make-temp-file "epg-output")))
972 (epg-start-encrypt context (epg-make-data-from-file plain)
973 recipients sign always-trust)
974 (epg-wait-for-completion context)
975 (if (epg-context-result-for context 'error)
976 (error "Encrypt failed"))
978 (epg-read-output context)))
980 (epg-delete-output-file context))
981 (epg-reset context)))
984 (defun epg-encrypt-string (context plain recipients
985 &optional sign always-trust)
986 "Encrypt a string PLAIN.
987 If RECIPIENTS is nil, it performs symmetric encryption."
990 (epg-context-set-output-file context
991 (epg-make-temp-file "epg-output"))
992 (epg-start-encrypt context (epg-make-data-from-string plain)
993 recipients sign always-trust)
994 (epg-wait-for-completion context)
995 (if (epg-context-result-for context 'error)
996 (error "Encrypt failed"))
997 (epg-read-output context))
998 (epg-delete-output-file context)
999 (epg-reset context)))
1002 (defun epg-start-export-keys (context pattern)
1003 "Initiate an export keys operation.
1005 If you use this function, you will need to wait for the completion of
1006 `epg-gpg-program' by using `epg-wait-for-completion' and call
1007 `epg-reset' to clear a temporaly output file.
1008 If you are unsure, use synchronous version of this function
1009 `epg-export-keys' instead."
1010 (epg-context-set-result context nil)
1011 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1012 (epg-start context (list "--export" pattern)))
1015 (defun epg-export-keys (context pattern)
1016 "Extract public keys matched with PATTERN and return them."
1019 (epg-start-export-keys context pattern)
1020 (epg-wait-for-completion context)
1021 (if (epg-context-result-for context 'error)
1022 (error "Export keys failed"))
1023 (epg-read-output context))
1024 (epg-reset context)))
1027 (defun epg-start-import-keys (context keys)
1028 "Initiate an import keys operation.
1029 KEYS is a data object.
1031 If you use this function, you will need to wait for the completion of
1032 `epg-gpg-program' by using `epg-wait-for-completion' and call
1033 `epg-reset' to clear a temporaly output file.
1034 If you are unsure, use synchronous version of this function
1035 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1036 (epg-context-set-result context nil)
1037 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1038 (epg-start context (append (list "--import") (epg-data-file keys)))
1039 (if (and (epg-data-string keys)
1040 (eq (process-status (epg-context-process context)) 'run))
1041 (process-send-string (epg-context-process context)
1042 (epg-data-string keys))))
1044 (defun epg-import-keys-1 (context keys)
1047 (epg-start-import-keys context keys)
1048 (epg-wait-for-completion context)
1049 (if (epg-context-result-for context 'error)
1050 (error "Import keys failed"))
1051 (epg-read-output context))
1052 (epg-reset context)))
1055 (defun epg-import-keys-from-file (context keys)
1056 "Add keys from a file KEYS."
1057 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1060 (defun epg-import-keys-from-string (context keys)
1061 "Add keys from a string KEYS."
1062 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1066 ;;; epg.el ends here