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
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.
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 ;; from gnupg/include/cipher.h
49 (defconst epg-cipher-algorithm-alist
61 ;; from gnupg/include/cipher.h
62 (defconst epg-pubkey-algorithm-alist
70 ;; from gnupg/include/cipher.h
71 (defconst epg-digest-algorithm-alist
79 ;; from gnupg/include/cipher.h
80 (defconst epg-compress-algorithm-alist
86 (defconst epg-invalid-recipients-alist
87 '((0 . "No specific reason given")
89 (2 . "Ambigious specification")
90 (3 . "Wrong key usage")
95 (8 . "Policy mismatch")
96 (9 . "Not a secret key")
97 (10 . "Key not trusted")))
99 (defvar epg-key-validity-alist
112 (defvar epg-key-capablity-alist
116 (?a . authentication)))
118 (defvar epg-prompt-alist nil)
120 (defun epg-make-data-from-file (file)
121 "Make a data object from FILE."
124 (defun epg-make-data-from-string (string)
125 "Make a data object from STRING."
128 (defun epg-data-file (data)
129 "Return the file of DATA."
132 (defun epg-data-string (data)
133 "Return the string of DATA."
136 (defun epg-make-context (&optional protocol armor textmode include-certs)
137 "Return a context object."
138 (vector protocol armor textmode include-certs
139 #'epg-passphrase-callback-function
140 #'epg-progress-callback-function
143 (defun epg-context-protocol (context)
144 "Return the protocol used within the context."
147 (defun epg-context-armor (context)
148 "Return t if the output shouled be ASCII armored in the CONTEXT context."
151 (defun epg-context-textmode (context)
152 "Return t if canonical text mode should be used in the CONTEXT context."
155 (defun epg-context-include-certs (context)
156 "Return how many certificates should be included in an S/MIME signed
160 (defun epg-context-passphrase-callback (context)
161 "Return the function used to query passphrase."
164 (defun epg-context-progress-callback (context)
165 "Return the function which handles progress update."
168 (defun epg-context-signers (context)
169 "Return the list of key-id for singning."
172 (defun epg-context-process (context)
173 "Return the process object of `epg-gpg-program'.
174 This function is for internal use only."
177 (defun epg-context-output-file (context)
178 "Return the output file of `epg-gpg-program'.
179 This function is for internal use only."
182 (defun epg-context-result (context)
183 "Return the result of the previous cryptographic operation."
186 (defun epg-context-set-protocol (context protocol)
187 "Set the protocol used within the context."
188 (aset context 0 protocol))
190 (defun epg-context-set-armor (context armor)
191 "Specify if the output shouled be ASCII armored in the CONTEXT context."
192 (aset context 1 armor))
194 (defun epg-context-set-textmode (context textmode)
195 "Specify if canonical text mode should be used in the CONTEXT context."
196 (aset context 2 textmode))
198 (defun epg-context-set-include-certs (context include-certs)
199 "Set how many certificates should be included in an S/MIME signed message."
200 (aset context 3 include-certs))
202 (defun epg-context-set-passphrase-callback (context
204 "Set the function used to query passphrase."
205 (aset context 4 passphrase-callback))
207 (defun epg-context-set-progress-callback (context progress-callback)
208 "Set the function which handles progress update."
209 (aset context 5 progress-callback))
211 (defun epg-context-set-signers (context signers)
212 "Set the list of key-id for singning."
213 (aset context 6 signers))
215 (defun epg-context-set-process (context process)
216 "Set the process object of `epg-gpg-program'.
217 This function is for internal use only."
218 (aset context 7 process))
220 (defun epg-context-set-output-file (context output-file)
221 "Set the output file of `epg-gpg-program'.
222 This function is for internal use only."
223 (aset context 8 output-file))
225 (defun epg-context-set-result (context result)
226 "Set the result of the previous cryptographic operation."
227 (aset context 9 result))
229 (defun epg-make-signature (status key-id user-id)
230 "Return a signature object."
231 (vector status key-id user-id nil nil))
233 (defun epg-signature-status (signature)
234 "Return the status code of SIGNATURE."
237 (defun epg-signature-key-id (signature)
238 "Return the key-id of SIGNATURE."
241 (defun epg-signature-user-id (signature)
242 "Return the user-id of SIGNATURE."
245 (defun epg-signature-validity (signature)
246 "Return the validity of SIGNATURE."
249 (defun epg-signature-fingerprint (signature)
250 "Return the fingerprint of SIGNATURE."
253 (defun epg-signature-set-status (signature status)
254 "Set the status code of SIGNATURE."
255 (aset signature 0 status))
257 (defun epg-signature-set-key-id (signature key-id)
258 "Set the key-id of SIGNATURE."
259 (aset signature 1 key-id))
261 (defun epg-signature-set-user-id (signature user-id)
262 "Set the user-id of SIGNATURE."
263 (aset signature 2 user-id))
265 (defun epg-signature-set-validity (signature validity)
266 "Set the validity of SIGNATURE."
267 (aset signature 3 validity))
269 (defun epg-signature-set-fingerprint (signature fingerprint)
270 "Set the fingerprint of SIGNATURE."
271 (aset signature 4 fingerprint))
273 (defun epg-make-key (owner-trust)
274 "Return a key object."
275 (vector owner-trust nil nil))
277 (defun epg-key-owner-trust (key)
278 "Return the owner trust of KEY."
281 (defun epg-key-sub-key-list (key)
282 "Return the sub key list of KEY."
285 (defun epg-key-user-id-list (key)
286 "Return the user ID list of KEY."
289 (defun epg-key-set-sub-key-list (key sub-key-list)
290 "Set the sub key list of KEY."
291 (aset key 1 sub-key-list))
293 (defun epg-key-set-user-id-list (key user-id-list)
294 "Set the user ID list of KEY."
295 (aset key 2 user-id-list))
297 (defun epg-make-sub-key (validity capability secret algorithm length id
298 creation-time expiration-time)
299 "Return a sub key object."
300 (vector validity capability secret algorithm length id creation-time
301 expiration-time nil))
303 (defun epg-sub-key-validity (sub-key)
304 "Return the validity of SUB-KEY."
307 (defun epg-sub-key-capability (sub-key)
308 "Return the capability of SUB-KEY."
311 (defun epg-sub-key-secret (sub-key)
312 "Return non-nil if SUB-KEY is a secret key."
315 (defun epg-sub-key-algorithm (sub-key)
316 "Return the algorithm of SUB-KEY."
319 (defun epg-sub-key-length (sub-key)
320 "Return the length of SUB-KEY."
323 (defun epg-sub-key-id (sub-key)
324 "Return the ID of SUB-KEY."
327 (defun epg-sub-key-creation-time (sub-key)
328 "Return the creation time of SUB-KEY."
331 (defun epg-sub-key-expiration-time (sub-key)
332 "Return the expiration time of SUB-KEY."
335 (defun epg-sub-key-fingerprint (sub-key)
336 "Return the fingerprint of SUB-KEY."
339 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
340 "Set the fingerprint of SUB-KEY.
341 This function is for internal use only."
342 (aset sub-key 8 fingerprint))
344 (defun epg-make-user-id (validity name)
345 "Return a user ID object."
346 (vector validity name nil))
348 (defun epg-user-id-validity (user-id)
349 "Return the validity of USER-ID."
352 (defun epg-user-id-name (user-id)
353 "Return the name of USER-ID."
356 (defun epg-user-id-signature-list (user-id)
357 "Return the signature list of USER-ID."
360 (defun epg-user-id-set-signature-list (user-id signature-list)
361 "Set the signature list of USER-ID."
362 (aset user-id 2 signature-list))
364 (defun epg-context-result-for (context name)
365 (cdr (assq name (epg-context-result context))))
367 (defun epg-context-set-result-for (context name value)
368 (let* ((result (epg-context-result context))
369 (entry (assq name result)))
372 (epg-context-set-result context (cons (cons name value) result)))))
374 (defun epg-start (context args)
375 "Start `epg-gpg-program' in a subprocess with given ARGS."
376 (let* ((args (append (list "--no-tty"
379 (if (epg-context-armor context) '("--armor"))
380 (if (epg-context-textmode context) '("--textmode"))
381 (if (epg-context-output-file context)
382 (list "--output" (epg-context-output-file context)))
384 (coding-system-for-write 'binary)
385 process-connection-type
386 (orig-mode (default-file-modes))
387 (buffer (generate-new-buffer " *epg*"))
389 (with-current-buffer buffer
390 (make-local-variable 'epg-read-point)
391 (setq epg-read-point (point-min))
392 (make-local-variable 'epg-pending-status-list)
393 (setq epg-pending-status-list nil)
394 (make-local-variable 'epg-key-id)
395 (setq epg-key-id nil)
396 (make-local-variable 'epg-context)
397 (setq epg-context context))
400 (set-default-file-modes 448)
402 (apply #'start-process "epg" buffer epg-gpg-program args)))
403 (set-default-file-modes orig-mode))
404 (set-process-filter process #'epg-process-filter)
405 (epg-context-set-process context process)))
407 (defun epg-process-filter (process input)
410 (set-buffer (get-buffer-create " *epg-debug*"))
411 (goto-char (point-max))
413 (if (buffer-live-p (process-buffer process))
415 (set-buffer (process-buffer process))
416 (goto-char (point-max))
418 (goto-char epg-read-point)
420 (while (looking-at ".*\n") ;the input line is finished
422 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
423 (let* ((status (match-string 1))
424 (string (match-string 2))
425 (symbol (intern-soft (concat "epg-status-" status))))
426 (if (member status epg-pending-status-list)
427 (setq epg-pending-status-list nil))
430 (funcall symbol process string)))))
432 (setq epg-read-point (point)))))
434 (defun epg-read-output (context)
436 (if (fboundp 'set-buffer-multibyte)
437 (set-buffer-multibyte nil))
438 (if (file-exists-p (epg-context-output-file context))
439 (let ((coding-system-for-read (if (epg-context-textmode context)
442 (insert-file-contents (epg-context-output-file context))
445 (defun epg-wait-for-status (context status-list)
446 (with-current-buffer (process-buffer (epg-context-process context))
447 (setq epg-pending-status-list status-list)
448 (while (and (eq (process-status (epg-context-process context)) 'run)
449 epg-pending-status-list)
450 (accept-process-output (epg-context-process context) 1))))
452 (defun epg-wait-for-completion (context)
453 (if (eq (process-status (epg-context-process context)) 'run)
454 (process-send-eof (epg-context-process context)))
455 (while (eq (process-status (epg-context-process context)) 'run)
456 ;; We can't use accept-process-output instead of sit-for here
457 ;; because it may cause an interrupt during the sentinel execution.
460 (defun epg-reset (context)
461 (if (and (epg-context-process context)
462 (buffer-live-p (process-buffer (epg-context-process context))))
463 (kill-buffer (process-buffer (epg-context-process context))))
464 (epg-context-set-process context nil))
466 (defun epg-delete-output-file (context)
467 (if (and (epg-context-output-file context)
468 (file-exists-p (epg-context-output-file context)))
469 (delete-file (epg-context-output-file context))))
471 (defun epg-status-USERID_HINT (process string)
472 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
473 (let* ((key-id (match-string 1 string))
474 (user-id (match-string 2 string))
475 (entry (assoc key-id epg-user-id-alist)))
477 (setcdr entry user-id)
478 (setq epg-user-id-alist (cons (cons key-id user-id)
479 epg-user-id-alist))))))
481 (defun epg-status-NEED_PASSPHRASE (process string)
482 (if (string-match "\\`\\([^ ]+\\)" string)
483 (setq epg-key-id (match-string 1 string))))
485 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
486 (setq epg-key-id 'SYM))
488 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
489 (setq epg-key-id 'PIN))
491 (defun epg-status-GET_HIDDEN (process string)
493 (funcall (if (consp (epg-context-passphrase-callback epg-context))
494 (car (epg-context-passphrase-callback epg-context))
495 (epg-context-passphrase-callback epg-context))
497 (if (consp (epg-context-passphrase-callback epg-context))
498 (cdr (epg-context-passphrase-callback epg-context)))))
503 (setq string (concat passphrase "\n"))
504 (fillarray passphrase 0)
505 (setq passphrase nil)
506 (process-send-string process string))
508 (fillarray string 0))))))
510 (defun epg-status-GET_BOOL (process string)
511 (let ((entry (assoc string epg-prompt-alist)))
512 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
513 (process-send-string process "y\n")
514 (process-send-string process "n\n"))))
516 (defun epg-status-GET_LINE (process string)
517 (let* ((entry (assoc string epg-prompt-alist))
518 (string (read-string (if entry (cdr entry) (concat string ": ")))))
519 (process-send-string process (concat string "\n"))))
521 (defun epg-status-GOODSIG (process string)
522 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
523 (epg-context-set-result-for
526 (cons (epg-make-signature 'good
527 (match-string 1 string)
528 (match-string 2 string))
529 (epg-context-result-for epg-context 'verify)))))
531 (defun epg-status-EXPSIG (process string)
532 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
533 (epg-context-set-result-for
536 (cons (epg-make-signature 'expired
537 (match-string 1 string)
538 (match-string 2 string))
539 (epg-context-result-for epg-context 'verify)))))
541 (defun epg-status-EXPKEYSIG (process string)
542 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
543 (epg-context-set-result-for
546 (cons (epg-make-signature 'expired-key
547 (match-string 1 string)
548 (match-string 2 string))
549 (epg-context-result-for epg-context 'verify)))))
551 (defun epg-status-REVKEYSIG (process string)
552 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
553 (epg-context-set-result-for
556 (cons (epg-make-signature 'revoked-key
557 (match-string 1 string)
558 (match-string 2 string))
559 (epg-context-result-for epg-context 'verify)))))
561 (defun epg-status-BADSIG (process string)
562 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
563 (epg-context-set-result-for
566 (cons (epg-make-signature 'bad
567 (match-string 1 string)
568 (match-string 2 string))
569 (epg-context-result-for epg-context 'verify)))))
571 (defun epg-status-VALIDSIG (process string)
572 (let ((signature (car (epg-context-result-for epg-context 'verify))))
574 (eq (epg-signature-status signature) 'good)
575 (string-match "\\`\\([^ ]+\\) " string))
576 (epg-signature-set-fingerprint signature (match-string 1 string)))))
578 (defun epg-status-TRUST_UNDEFINED (process string)
579 (let ((signature (car (epg-context-result-for epg-context 'verify))))
581 (eq (epg-signature-status signature) 'good))
582 (epg-signature-set-validity signature 'undefined))))
584 (defun epg-status-TRUST_NEVER (process string)
585 (let ((signature (car (epg-context-result-for epg-context 'verify))))
587 (eq (epg-signature-status signature) 'good))
588 (epg-signature-set-validity signature 'never))))
590 (defun epg-status-TRUST_MARGINAL (process string)
591 (let ((signature (car (epg-context-result-for epg-context 'verify))))
593 (eq (epg-signature-status signature) 'marginal))
594 (epg-signature-set-validity signature 'marginal))))
596 (defun epg-status-TRUST_FULLY (process string)
597 (let ((signature (car (epg-context-result-for epg-context 'verify))))
599 (eq (epg-signature-status signature) 'good))
600 (epg-signature-set-validity signature 'full))))
602 (defun epg-status-TRUST_ULTIMATE (process string)
603 (let ((signature (car (epg-context-result-for epg-context 'verify))))
605 (eq (epg-signature-status signature) 'good))
606 (epg-signature-set-validity signature 'ultimate))))
608 (defun epg-status-PROGRESS (process string)
609 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
611 (funcall (if (consp (epg-context-progress-callback epg-context))
612 (car (epg-context-progress-callback epg-context))
613 (epg-context-progress-callback epg-context))
614 (match-string 1 string)
615 (match-string 2 string)
616 (string-to-number (match-string 3 string))
617 (string-to-number (match-string 4 string))
618 (if (consp (epg-context-progress-callback epg-context))
619 (cdr (epg-context-progress-callback epg-context))))))
621 (defun epg-status-DECRYPTION_FAILED (process string)
622 (epg-context-set-result-for
624 (cons 'decryption-failed
625 (epg-context-result-for epg-context 'error))))
627 (defun epg-status-NODATA (process string)
628 (epg-context-set-result-for
630 (cons (cons 'no-data (string-to-number string))
631 (epg-context-result-for epg-context 'error))))
633 (defun epg-status-UNEXPECTED (process string)
634 (epg-context-set-result-for
636 (cons (cons 'unexpected (string-to-number string))
637 (epg-context-result-for epg-context 'error))))
639 (defun epg-status-KEYEXPIRED (process string)
640 (epg-context-set-result-for
642 (cons (cons 'key-expired string)
643 (epg-context-result-for epg-context 'error))))
645 (defun epg-status-KEYREVOKED (process string)
646 (epg-context-set-result-for
649 (epg-context-result-for epg-context 'error))))
651 (defun epg-status-BADARMOR (process string)
652 (epg-context-set-result-for
655 (epg-context-result-for epg-context 'error))))
657 (defun epg-status-INV_RECP (process string)
658 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
659 (epg-context-set-result-for
661 (cons (cons 'invalid-recipient
662 (string-to-number (match-string 1 string)))
663 (epg-context-result-for epg-context 'error)))))
665 (defun epg-status-NO_RECP (process string)
666 (epg-context-set-result-for
669 (epg-context-result-for epg-context 'error))))
671 (defun epg-passphrase-callback-function (key-id handback)
674 "Passphrase for symmetric encryption: "
676 "Passphrase for PIN: "
677 (let ((entry (assoc key-id epg-user-id-alist)))
679 (format "Passphrase for %s %s: " key-id (cdr entry))
680 (format "Passphrase for %s: " key-id)))))))
682 (defun epg-progress-callback-function (what char current total handback)
683 (message "%s: %d%%/%d%%" what current total))
685 (defun epg-configuration ()
686 "Return a list of internal configuration parameters of `epg-gpg-program'."
689 (apply #'call-process epg-gpg-program nil (list t nil) nil
690 '("--with-colons" "--list-config"))
691 (goto-char (point-min))
692 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
693 (setq type (intern (match-string 1))
694 config (cons (cons type
696 '(pubkey cipher digest compress))
697 (mapcar #'string-to-number
698 (delete "" (split-string
705 (defun epg-list-keys-1 (name mode)
706 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
707 "--fixed-list-mode" "--with-fingerprint"
709 (if mode "--list-secret-keys" "--list-keys"))
710 (if name (list name))))
711 keys string field index)
713 (apply #'call-process epg-gpg-program nil (list t nil) nil args)
714 (goto-char (point-min))
715 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
716 (setq keys (cons (make-vector 15 nil) keys)
717 string (match-string 0)
721 (string-match "\\([^:]+\\)?:" string index))
722 (setq index (match-end 0))
723 (aset (car keys) field (match-string 1 string))
724 (setq field (1+ field))))
727 (defun epg-make-sub-key-1 (line)
729 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist))
731 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
733 (member (aref line 0) '("sec" "ssb"))
734 (string-to-number (aref line 3))
735 (string-to-number (aref line 2))
740 (defun epg-list-keys (&optional name mode)
741 (let ((lines (epg-list-keys-1 name mode))
745 ((member (aref (car lines) 0) '("pub" "sec"))
747 (epg-key-set-sub-key-list
749 (nreverse (epg-key-sub-key-list (car keys))))
750 (epg-key-set-user-id-list
752 (nreverse (epg-key-user-id-list (car keys)))))
753 (setq keys (cons (epg-make-key
754 (cdr (assq (string-to-char (aref (car lines) 8))
755 epg-key-validity-alist)))
757 (epg-key-set-sub-key-list
759 (cons (epg-make-sub-key-1 (car lines))
760 (epg-key-sub-key-list (car keys)))))
761 ((member (aref (car lines) 0) '("sub" "ssb"))
762 (epg-key-set-sub-key-list
764 (cons (epg-make-sub-key-1 (car lines))
765 (epg-key-sub-key-list (car keys)))))
766 ((equal (aref (car lines) 0) "uid")
767 (epg-key-set-user-id-list
769 (cons (epg-make-user-id
770 (cdr (assq (string-to-char (aref (car lines) 1))
771 epg-key-validity-alist))
772 (aref (car lines) 9))
773 (epg-key-user-id-list (car keys)))))
774 ((equal (aref (car lines) 0) "fpr")
775 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
776 (aref (car lines) 9))))
777 (setq lines (cdr lines)))
780 (if (fboundp 'make-temp-file)
781 (defalias 'epg-make-temp-file 'make-temp-file)
782 ;; stolen from poe.el.
783 (defun epg-make-temp-file (prefix)
784 "Create a temporary file.
785 The returned file name (created by appending some random characters at the end
786 of PREFIX, and expanding against `temporary-file-directory' if necessary),
787 is guaranteed to point to a newly created empty file.
788 You can then use `write-region' to write new data into the file."
789 (let (tempdir tempfile)
792 ;; First, create a temporary directory.
793 (while (condition-case ()
795 (setq tempdir (make-temp-name
797 (file-name-directory prefix)
799 ;; return nil or signal an error.
800 (make-directory tempdir))
802 (file-already-exists t)))
803 (set-file-modes tempdir 448)
804 ;; Second, create a temporary file in the tempdir.
805 ;; There *is* a race condition between `make-temp-name'
806 ;; and `write-region', but we don't care it since we are
807 ;; in a private directory now.
808 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
809 (write-region "" nil tempfile nil 'silent)
810 (set-file-modes tempfile 384)
811 ;; Finally, make a hard-link from the tempfile.
812 (while (condition-case ()
814 (setq file (make-temp-name prefix))
815 ;; return nil or signal an error.
816 (add-name-to-file tempfile file))
818 (file-already-exists t)))
820 ;; Cleanup the tempfile.
822 (file-exists-p tempfile)
823 (delete-file tempfile))
824 ;; Cleanup the tempdir.
826 (file-directory-p tempdir)
827 (delete-directory tempdir))))))
830 (defun epg-start-decrypt (context cipher)
831 "Initiate a decrypt operation on CIPHER.
832 CIPHER is a data object.
834 If you use this function, you will need to wait for the completion of
835 `epg-gpg-program' by using `epg-wait-for-completion' and call
836 `epg-reset' to clear a temporaly output file.
837 If you are unsure, use synchronous version of this function
838 `epg-decrypt-file' or `epg-decrypt-string' instead."
839 (unless (epg-data-file cipher)
840 (error "Not a file"))
841 (epg-context-set-result context nil)
842 (epg-start context (list "--decrypt" (epg-data-file cipher)))
843 (epg-wait-for-status context '("BEGIN_DECRYPTION")))
846 (defun epg-decrypt-file (context cipher plain)
847 "Decrypt a file CIPHER and store the result to a file PLAIN.
848 If PLAIN is nil, it returns the result as a string."
852 (epg-context-set-output-file context plain)
853 (epg-context-set-output-file context
854 (epg-make-temp-file "epg-output")))
855 (epg-start-decrypt context (epg-make-data-from-file cipher))
856 (epg-wait-for-completion context)
857 (if (epg-context-result-for context 'error)
858 (error "Decryption failed"))
860 (epg-read-output context)))
862 (epg-delete-output-file context))
863 (epg-reset context)))
866 (defun epg-decrypt-string (context cipher)
867 "Decrypt a string CIPHER and return the plain text."
868 (let ((input-file (epg-make-temp-file "epg-input"))
869 (coding-system-for-write 'binary))
872 (write-region cipher nil input-file)
873 (epg-context-set-output-file context
874 (epg-make-temp-file "epg-output"))
875 (epg-start-decrypt context (epg-make-data-from-file input-file))
876 (epg-wait-for-completion context)
877 (if (epg-context-result-for context 'error)
878 (error "Decryption failed"))
879 (epg-read-output context))
880 (epg-delete-output-file context)
881 (if (file-exists-p input-file)
882 (delete-file input-file))
883 (epg-reset context))))
886 (defun epg-start-verify (context signature &optional signed-text)
887 "Initiate a verify operation on SIGNATURE.
888 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
890 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
891 For a normal or a clear text signature, SIGNED-TEXT should be nil.
893 If you use this function, you will need to wait for the completion of
894 `epg-gpg-program' by using `epg-wait-for-completion' and call
895 `epg-reset' to clear a temporaly output file.
896 If you are unsure, use synchronous version of this function
897 `epg-verify-file' or `epg-verify-string' instead."
898 (epg-context-set-result context nil)
900 ;; Detached signature.
901 (if (epg-data-file signed-text)
902 (epg-start context (list "--verify" (epg-data-file signature)
903 (epg-data-file signed-text)))
904 (epg-start context (list "--verify" (epg-data-file signature) "-"))
905 (if (eq (process-status (epg-context-process context)) 'run)
906 (process-send-string (epg-context-process context)
907 (epg-data-string signed-text))))
908 ;; Normal (or cleartext) signature.
909 (if (epg-data-file signature)
910 (epg-start context (list "--verify" (epg-data-file signature)))
911 (epg-start context (list "--verify"))
912 (if (eq (process-status (epg-context-process context)) 'run)
913 (process-send-string (epg-context-process context)
914 (epg-data-string signature))))))
917 (defun epg-verify-file (context signature &optional signed-text plain)
918 "Verify a file SIGNATURE.
919 SIGNED-TEXT and PLAIN are also a file if they are specified.
921 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
922 For a normal or a clear text signature, SIGNED-TEXT should be nil."
926 (epg-context-set-output-file context plain)
927 (epg-context-set-output-file context
928 (epg-make-temp-file "epg-output")))
930 (epg-start-verify context
931 (epg-make-data-from-file signature)
932 (epg-make-data-from-file signed-text))
933 (epg-start-verify context
934 (epg-make-data-from-file signature)))
935 (epg-wait-for-completion context)
937 (epg-read-output context)))
939 (epg-delete-output-file context))
940 (epg-reset context)))
943 (defun epg-verify-string (context signature &optional signed-text)
944 "Verify a string SIGNATURE.
945 SIGNED-TEXT is a string if it is specified.
947 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
948 For a normal or a clear text signature, SIGNED-TEXT should be nil."
949 (let ((coding-system-for-write 'binary)
953 (epg-context-set-output-file context
954 (epg-make-temp-file "epg-output"))
957 (setq input-file (epg-make-temp-file "epg-signature"))
958 (write-region signature nil input-file)
959 (epg-start-verify context
960 (epg-make-data-from-file input-file)
961 (epg-make-data-from-string signed-text)))
962 (epg-start-verify context (epg-make-data-from-string signature)))
963 (epg-wait-for-completion context)
964 (epg-read-output context))
965 (epg-delete-output-file context)
967 (file-exists-p input-file))
968 (delete-file input-file))
969 (epg-reset context))))
972 (defun epg-start-sign (context plain &optional mode)
973 "Initiate a sign operation on PLAIN.
974 PLAIN is a data object.
976 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
977 If MODE is t or 'detached, it makes a detached signature.
978 Otherwise, it makes a normal signature.
980 If you use this function, you will need to wait for the completion of
981 `epg-gpg-program' by using `epg-wait-for-completion' and call
982 `epg-reset' to clear a temporaly output file.
983 If you are unsure, use synchronous version of this function
984 `epg-sign-file' or `epg-sign-string' instead."
985 (epg-context-set-result context nil)
987 (append (list (if (eq mode 'clearsign)
989 (if (or (eq mode t) (eq mode 'detached))
993 (mapcar (lambda (signer)
995 (epg-context-signers context)))
996 (if (epg-data-file plain)
997 (list (epg-data-file plain)))))
998 (epg-wait-for-status context '("BEGIN_SIGNING"))
999 (if (and (epg-data-string plain)
1000 (eq (process-status (epg-context-process context)) 'run))
1001 (process-send-string (epg-context-process context)
1002 (epg-data-string plain))))
1005 (defun epg-sign-file (context plain signature &optional mode)
1006 "Sign a file PLAIN and store the result to a file SIGNATURE.
1007 If SIGNATURE is nil, it returns the result as a string.
1008 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1009 If MODE is t or 'detached, it makes a detached signature.
1010 Otherwise, it makes a normal signature."
1014 (epg-context-set-output-file context signature)
1015 (epg-context-set-output-file context
1016 (epg-make-temp-file "epg-output")))
1017 (epg-start-sign context (epg-make-data-from-file plain) mode)
1018 (epg-wait-for-completion context)
1019 (if (epg-context-result-for context 'error)
1020 (error "Sign failed"))
1022 (epg-read-output context)))
1024 (epg-delete-output-file context))
1025 (epg-reset context)))
1028 (defun epg-sign-string (context plain &optional mode)
1029 "Sign a string PLAIN and return the output as string.
1030 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1031 If MODE is t or 'detached, it makes a detached signature.
1032 Otherwise, it makes a normal signature."
1035 (epg-context-set-output-file context
1036 (epg-make-temp-file "epg-output"))
1037 (epg-start-sign context (epg-make-data-from-string plain) mode)
1038 (epg-wait-for-completion context)
1039 (if (epg-context-result-for context 'error)
1040 (error "Sign failed"))
1041 (epg-read-output context))
1042 (epg-delete-output-file context)
1043 (epg-reset context)))
1046 (defun epg-start-encrypt (context plain recipients
1047 &optional sign always-trust)
1048 "Initiate an encrypt operation on PLAIN.
1049 PLAIN is a data object.
1050 If RECIPIENTS is nil, it performs symmetric encryption.
1052 If you use this function, you will need to wait for the completion of
1053 `epg-gpg-program' by using `epg-wait-for-completion' and call
1054 `epg-reset' to clear a temporaly output file.
1055 If you are unsure, use synchronous version of this function
1056 `epg-encrypt-file' or `epg-encrypt-string' instead."
1057 (epg-context-set-result context nil)
1059 (append (if always-trust '("--always-trust"))
1060 (if recipients '("--encrypt") '("--symmetric"))
1064 (mapcar (lambda (signer)
1066 (epg-context-signers context)))))
1068 (mapcar (lambda (recipient)
1069 (list "-r" recipient))
1071 (if (epg-data-file plain)
1072 (list (epg-data-file plain)))))
1074 (epg-wait-for-status context '("BEGIN_SIGNING"))
1075 (if (null recipients)
1076 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1077 (if (and (epg-data-string plain)
1078 (eq (process-status (epg-context-process context)) 'run))
1079 (process-send-string (epg-context-process context)
1080 (epg-data-string plain))))
1083 (defun epg-encrypt-file (context plain recipients
1084 cipher &optional sign always-trust)
1085 "Encrypt a file PLAIN and store the result to a file CIPHER.
1086 If CIPHER is nil, it returns the result as a string.
1087 If RECIPIENTS is nil, it performs symmetric encryption."
1091 (epg-context-set-output-file context cipher)
1092 (epg-context-set-output-file context
1093 (epg-make-temp-file "epg-output")))
1094 (epg-start-encrypt context (epg-make-data-from-file plain)
1095 recipients sign always-trust)
1096 (epg-wait-for-completion context)
1097 (if (epg-context-result-for context 'error)
1098 (error "Encrypt failed"))
1100 (epg-read-output context)))
1102 (epg-delete-output-file context))
1103 (epg-reset context)))
1106 (defun epg-encrypt-string (context plain recipients
1107 &optional sign always-trust)
1108 "Encrypt a string PLAIN.
1109 If RECIPIENTS is nil, it performs symmetric encryption."
1112 (epg-context-set-output-file context
1113 (epg-make-temp-file "epg-output"))
1114 (epg-start-encrypt context (epg-make-data-from-string plain)
1115 recipients sign always-trust)
1116 (epg-wait-for-completion context)
1117 (if (epg-context-result-for context 'error)
1118 (error "Encrypt failed"))
1119 (epg-read-output context))
1120 (epg-delete-output-file context)
1121 (epg-reset context)))
1124 (defun epg-start-export-keys (context pattern)
1125 "Initiate an export keys operation.
1127 If you use this function, you will need to wait for the completion of
1128 `epg-gpg-program' by using `epg-wait-for-completion' and call
1129 `epg-reset' to clear a temporaly output file.
1130 If you are unsure, use synchronous version of this function
1131 `epg-export-keys' instead."
1132 (epg-context-set-result context nil)
1133 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1134 (epg-start context (list "--export" pattern)))
1137 (defun epg-export-keys (context pattern)
1138 "Extract public keys matched with PATTERN and return them."
1141 (epg-start-export-keys context pattern)
1142 (epg-wait-for-completion context)
1143 (if (epg-context-result-for context 'error)
1144 (error "Export keys failed"))
1145 (epg-read-output context))
1146 (epg-reset context)))
1149 (defun epg-start-import-keys (context keys)
1150 "Initiate an import keys operation.
1151 KEYS is a data object.
1153 If you use this function, you will need to wait for the completion of
1154 `epg-gpg-program' by using `epg-wait-for-completion' and call
1155 `epg-reset' to clear a temporaly output file.
1156 If you are unsure, use synchronous version of this function
1157 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1158 (epg-context-set-result context nil)
1159 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1160 (epg-start context (append (list "--import") (epg-data-file keys)))
1161 (if (and (epg-data-string keys)
1162 (eq (process-status (epg-context-process context)) 'run))
1163 (process-send-string (epg-context-process context)
1164 (epg-data-string keys))))
1166 (defun epg-import-keys-1 (context keys)
1169 (epg-start-import-keys context keys)
1170 (epg-wait-for-completion context)
1171 (if (epg-context-result-for context 'error)
1172 (error "Import keys failed"))
1173 (epg-read-output context))
1174 (epg-reset context)))
1177 (defun epg-import-keys-from-file (context keys)
1178 "Add keys from a file KEYS."
1179 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1182 (defun epg-import-keys-from-string (context keys)
1183 "Add keys from a string KEYS."
1184 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1188 ;;; epg.el ends here