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 (defconst epg-version-number "0.0.0")
38 (defvar epg-user-id nil
39 "GnuPG ID of your default identity.")
41 (defvar epg-user-id-alist nil
42 "An alist mapping from key ID to user ID.")
44 (defvar epg-read-point nil)
45 (defvar epg-pending-status-list nil)
46 (defvar epg-key-id nil)
47 (defvar epg-context nil)
48 (defvar epg-debug nil)
50 ;; from gnupg/include/cipher.h
51 (defconst epg-cipher-algorithm-alist
63 ;; from gnupg/include/cipher.h
64 (defconst epg-pubkey-algorithm-alist
72 ;; from gnupg/include/cipher.h
73 (defconst epg-digest-algorithm-alist
81 ;; from gnupg/include/cipher.h
82 (defconst epg-compress-algorithm-alist
88 (defconst epg-invalid-recipients-alist
89 '((0 . "No specific reason given")
91 (2 . "Ambigious specification")
92 (3 . "Wrong key usage")
97 (8 . "Policy mismatch")
98 (9 . "Not a secret key")
99 (10 . "Key not trusted")))
101 (defconst epg-delete-problem-alist
102 '((1 . "No such key")
103 (2 . "Must delete secret key first")
104 (3 . "Ambigious specification")))
106 (defvar epg-key-validity-alist
119 (defvar epg-key-capablity-alist
123 (?a . authentication)))
125 (defvar epg-prompt-alist nil)
127 (defun epg-make-data-from-file (file)
128 "Make a data object from FILE."
131 (defun epg-make-data-from-string (string)
132 "Make a data object from STRING."
135 (defun epg-data-file (data)
136 "Return the file of DATA."
139 (defun epg-data-string (data)
140 "Return the string of DATA."
143 (defun epg-make-context (&optional protocol armor textmode include-certs
144 cipher-algorithm digest-algorithm
146 "Return a context object."
147 (vector protocol armor textmode include-certs
148 cipher-algorithm digest-algorithm compress-algorithm
149 #'epg-passphrase-callback-function
150 #'epg-progress-callback-function
153 (defun epg-context-protocol (context)
154 "Return the protocol used within CONTEXT."
157 (defun epg-context-armor (context)
158 "Return t if the output shouled be ASCII armored in CONTEXT."
161 (defun epg-context-textmode (context)
162 "Return t if canonical text mode should be used in CONTEXT."
165 (defun epg-context-include-certs (context)
166 "Return how many certificates should be included in an S/MIME signed
170 (defun epg-context-cipher-algorithm (context)
171 "Return the cipher algorithm in CONTEXT."
174 (defun epg-context-digest-algorithm (context)
175 "Return the digest algorithm in CONTEXT."
178 (defun epg-context-compress-algorithm (context)
179 "Return the compress algorithm in CONTEXT."
182 (defun epg-context-passphrase-callback (context)
183 "Return the function used to query passphrase."
186 (defun epg-context-progress-callback (context)
187 "Return the function which handles progress update."
190 (defun epg-context-signers (context)
191 "Return the list of key-id for singning."
194 (defun epg-context-process (context)
195 "Return the process object of `epg-gpg-program'.
196 This function is for internal use only."
199 (defun epg-context-output-file (context)
200 "Return the output file of `epg-gpg-program'.
201 This function is for internal use only."
204 (defun epg-context-result (context)
205 "Return the result of the previous cryptographic operation."
208 (defun epg-context-set-protocol (context protocol)
209 "Set the protocol used within CONTEXT."
210 (aset context 0 protocol))
212 (defun epg-context-set-armor (context armor)
213 "Specify if the output shouled be ASCII armored in CONTEXT."
214 (aset context 1 armor))
216 (defun epg-context-set-textmode (context textmode)
217 "Specify if canonical text mode should be used in CONTEXT."
218 (aset context 2 textmode))
220 (defun epg-context-set-include-certs (context include-certs)
221 "Set how many certificates should be included in an S/MIME signed message."
222 (aset context 3 include-certs))
224 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
225 "Set the cipher algorithm in CONTEXT."
226 (aset context 4 cipher-algorithm))
228 (defun epg-context-set-digest-algorithm (context digest-algorithm)
229 "Set the digest algorithm in CONTEXT."
230 (aset context 5 digest-algorithm))
232 (defun epg-context-set-compress-algorithm (context compress-algorithm)
233 "Set the compress algorithm in CONTEXT."
234 (aset context 6 compress-algorithm))
236 (defun epg-context-set-passphrase-callback (context
238 "Set the function used to query passphrase."
239 (aset context 7 passphrase-callback))
241 (defun epg-context-set-progress-callback (context progress-callback)
242 "Set the function which handles progress update."
243 (aset context 8 progress-callback))
245 (defun epg-context-set-signers (context signers)
246 "Set the list of key-id for singning."
247 (aset context 9 signers))
249 (defun epg-context-set-process (context process)
250 "Set the process object of `epg-gpg-program'.
251 This function is for internal use only."
252 (aset context 10 process))
254 (defun epg-context-set-output-file (context output-file)
255 "Set the output file of `epg-gpg-program'.
256 This function is for internal use only."
257 (aset context 11 output-file))
259 (defun epg-context-set-result (context result)
260 "Set the result of the previous cryptographic operation."
261 (aset context 12 result))
263 (defun epg-make-signature (status key-id user-id)
264 "Return a signature object."
265 (vector status key-id user-id nil nil))
267 (defun epg-signature-status (signature)
268 "Return the status code of SIGNATURE."
271 (defun epg-signature-key-id (signature)
272 "Return the key-id of SIGNATURE."
275 (defun epg-signature-user-id (signature)
276 "Return the user-id of SIGNATURE."
279 (defun epg-signature-validity (signature)
280 "Return the validity of SIGNATURE."
283 (defun epg-signature-fingerprint (signature)
284 "Return the fingerprint of SIGNATURE."
287 (defun epg-signature-set-status (signature status)
288 "Set the status code of SIGNATURE."
289 (aset signature 0 status))
291 (defun epg-signature-set-key-id (signature key-id)
292 "Set the key-id of SIGNATURE."
293 (aset signature 1 key-id))
295 (defun epg-signature-set-user-id (signature user-id)
296 "Set the user-id of SIGNATURE."
297 (aset signature 2 user-id))
299 (defun epg-signature-set-validity (signature validity)
300 "Set the validity of SIGNATURE."
301 (aset signature 3 validity))
303 (defun epg-signature-set-fingerprint (signature fingerprint)
304 "Set the fingerprint of SIGNATURE."
305 (aset signature 4 fingerprint))
307 (defun epg-make-key (owner-trust)
308 "Return a key object."
309 (vector owner-trust nil nil))
311 (defun epg-key-owner-trust (key)
312 "Return the owner trust of KEY."
315 (defun epg-key-sub-key-list (key)
316 "Return the sub key list of KEY."
319 (defun epg-key-user-id-list (key)
320 "Return the user ID list of KEY."
323 (defun epg-key-set-sub-key-list (key sub-key-list)
324 "Set the sub key list of KEY."
325 (aset key 1 sub-key-list))
327 (defun epg-key-set-user-id-list (key user-id-list)
328 "Set the user ID list of KEY."
329 (aset key 2 user-id-list))
331 (defun epg-make-sub-key (validity capability secret algorithm length id
332 creation-time expiration-time)
333 "Return a sub key object."
334 (vector validity capability secret algorithm length id creation-time
335 expiration-time nil))
337 (defun epg-sub-key-validity (sub-key)
338 "Return the validity of SUB-KEY."
341 (defun epg-sub-key-capability (sub-key)
342 "Return the capability of SUB-KEY."
345 (defun epg-sub-key-secret (sub-key)
346 "Return non-nil if SUB-KEY is a secret key."
349 (defun epg-sub-key-algorithm (sub-key)
350 "Return the algorithm of SUB-KEY."
353 (defun epg-sub-key-length (sub-key)
354 "Return the length of SUB-KEY."
357 (defun epg-sub-key-id (sub-key)
358 "Return the ID of SUB-KEY."
361 (defun epg-sub-key-creation-time (sub-key)
362 "Return the creation time of SUB-KEY."
365 (defun epg-sub-key-expiration-time (sub-key)
366 "Return the expiration time of SUB-KEY."
369 (defun epg-sub-key-fingerprint (sub-key)
370 "Return the fingerprint of SUB-KEY."
373 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
374 "Set the fingerprint of SUB-KEY.
375 This function is for internal use only."
376 (aset sub-key 8 fingerprint))
378 (defun epg-make-user-id (validity name)
379 "Return a user ID object."
380 (vector validity name nil))
382 (defun epg-user-id-validity (user-id)
383 "Return the validity of USER-ID."
386 (defun epg-user-id-name (user-id)
387 "Return the name of USER-ID."
390 (defun epg-user-id-signature-list (user-id)
391 "Return the signature list of USER-ID."
394 (defun epg-user-id-set-signature-list (user-id signature-list)
395 "Set the signature list of USER-ID."
396 (aset user-id 2 signature-list))
398 (defun epg-context-result-for (context name)
399 (cdr (assq name (epg-context-result context))))
401 (defun epg-context-set-result-for (context name value)
402 (let* ((result (epg-context-result context))
403 (entry (assq name result)))
406 (epg-context-set-result context (cons (cons name value) result)))))
408 (defun epg-start (context args)
409 "Start `epg-gpg-program' in a subprocess with given ARGS."
410 (let* ((args (append (list "--no-tty"
413 (if (epg-context-armor context) '("--armor"))
414 (if (epg-context-textmode context) '("--textmode"))
415 (if (epg-context-output-file context)
416 (list "--output" (epg-context-output-file context)))
418 (coding-system-for-write 'binary)
419 process-connection-type
420 (orig-mode (default-file-modes))
421 (buffer (generate-new-buffer " *epg*"))
425 (set-buffer (get-buffer-create " *epg-debug*"))
426 (goto-char (point-max))
427 (insert (format "%s %s\n" epg-gpg-program
428 (mapconcat #'identity args " ")))))
429 (with-current-buffer buffer
430 (make-local-variable 'epg-read-point)
431 (setq epg-read-point (point-min))
432 (make-local-variable 'epg-pending-status-list)
433 (setq epg-pending-status-list nil)
434 (make-local-variable 'epg-key-id)
435 (setq epg-key-id nil)
436 (make-local-variable 'epg-context)
437 (setq epg-context context))
440 (set-default-file-modes 448)
442 (apply #'start-process "epg" buffer epg-gpg-program args)))
443 (set-default-file-modes orig-mode))
444 (set-process-filter process #'epg-process-filter)
445 (epg-context-set-process context process)))
447 (defun epg-process-filter (process input)
450 (set-buffer (get-buffer-create " *epg-debug*"))
451 (goto-char (point-max))
453 (if (buffer-live-p (process-buffer process))
455 (set-buffer (process-buffer process))
456 (goto-char (point-max))
458 (goto-char epg-read-point)
460 (while (looking-at ".*\n") ;the input line is finished
462 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
463 (let* ((status (match-string 1))
464 (string (match-string 2))
465 (symbol (intern-soft (concat "epg-status-" status))))
466 (if (member status epg-pending-status-list)
467 (setq epg-pending-status-list nil))
470 (funcall symbol process string)))))
472 (setq epg-read-point (point)))))
474 (defun epg-read-output (context)
476 (if (fboundp 'set-buffer-multibyte)
477 (set-buffer-multibyte nil))
478 (if (file-exists-p (epg-context-output-file context))
479 (let ((coding-system-for-read (if (epg-context-textmode context)
482 (insert-file-contents (epg-context-output-file context))
485 (defun epg-wait-for-status (context status-list)
486 (with-current-buffer (process-buffer (epg-context-process context))
487 (setq epg-pending-status-list status-list)
488 (while (and (eq (process-status (epg-context-process context)) 'run)
489 epg-pending-status-list)
490 (accept-process-output (epg-context-process context) 1))))
492 (defun epg-wait-for-completion (context &optional no-eof)
493 (if (and (not no-eof)
494 (eq (process-status (epg-context-process context)) 'run))
495 (process-send-eof (epg-context-process context)))
496 (while (eq (process-status (epg-context-process context)) 'run)
497 ;; We can't use accept-process-output instead of sit-for here
498 ;; because it may cause an interrupt during the sentinel execution.
501 (defun epg-reset (context)
502 (if (and (epg-context-process context)
503 (buffer-live-p (process-buffer (epg-context-process context))))
504 (kill-buffer (process-buffer (epg-context-process context))))
505 (epg-context-set-process context nil))
507 (defun epg-delete-output-file (context)
508 (if (and (epg-context-output-file context)
509 (file-exists-p (epg-context-output-file context)))
510 (delete-file (epg-context-output-file context))))
512 (defun epg-status-USERID_HINT (process string)
513 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
514 (let* ((key-id (match-string 1 string))
515 (user-id (match-string 2 string))
516 (entry (assoc key-id epg-user-id-alist)))
518 (setcdr entry user-id)
519 (setq epg-user-id-alist (cons (cons key-id user-id)
520 epg-user-id-alist))))))
522 (defun epg-status-NEED_PASSPHRASE (process string)
523 (if (string-match "\\`\\([^ ]+\\)" string)
524 (setq epg-key-id (match-string 1 string))))
526 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
527 (setq epg-key-id 'SYM))
529 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
530 (setq epg-key-id 'PIN))
532 (defun epg-status-GET_HIDDEN (process string)
534 (funcall (if (consp (epg-context-passphrase-callback epg-context))
535 (car (epg-context-passphrase-callback epg-context))
536 (epg-context-passphrase-callback epg-context))
537 (if (consp (epg-context-passphrase-callback epg-context))
538 (cdr (epg-context-passphrase-callback epg-context)))))
543 (setq string (concat passphrase "\n"))
544 (fillarray passphrase 0)
545 (setq passphrase nil)
546 (process-send-string process string))
548 (fillarray string 0))))))
550 (defun epg-status-GET_BOOL (process string)
551 (let ((entry (assoc string epg-prompt-alist)))
552 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
553 (process-send-string process "y\n")
554 (process-send-string process "n\n"))))
556 (defun epg-status-GET_LINE (process string)
557 (let* ((entry (assoc string epg-prompt-alist))
558 (string (read-string (if entry (cdr entry) (concat string ": ")))))
559 (process-send-string process (concat string "\n"))))
561 (defun epg-status-GOODSIG (process string)
562 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
563 (epg-context-set-result-for
566 (cons (epg-make-signature 'good
567 (match-string 1 string)
568 (match-string 2 string))
569 (epg-context-result-for epg-context 'verify)))))
571 (defun epg-status-EXPSIG (process string)
572 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
573 (epg-context-set-result-for
576 (cons (epg-make-signature 'expired
577 (match-string 1 string)
578 (match-string 2 string))
579 (epg-context-result-for epg-context 'verify)))))
581 (defun epg-status-EXPKEYSIG (process string)
582 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
583 (epg-context-set-result-for
586 (cons (epg-make-signature 'expired-key
587 (match-string 1 string)
588 (match-string 2 string))
589 (epg-context-result-for epg-context 'verify)))))
591 (defun epg-status-REVKEYSIG (process string)
592 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
593 (epg-context-set-result-for
596 (cons (epg-make-signature 'revoked-key
597 (match-string 1 string)
598 (match-string 2 string))
599 (epg-context-result-for epg-context 'verify)))))
601 (defun epg-status-BADSIG (process string)
602 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
603 (epg-context-set-result-for
606 (cons (epg-make-signature 'bad
607 (match-string 1 string)
608 (match-string 2 string))
609 (epg-context-result-for epg-context 'verify)))))
611 (defun epg-status-VALIDSIG (process string)
612 (let ((signature (car (epg-context-result-for epg-context 'verify))))
614 (eq (epg-signature-status signature) 'good)
615 (string-match "\\`\\([^ ]+\\) " string))
616 (epg-signature-set-fingerprint signature (match-string 1 string)))))
618 (defun epg-status-TRUST_UNDEFINED (process string)
619 (let ((signature (car (epg-context-result-for epg-context 'verify))))
621 (eq (epg-signature-status signature) 'good))
622 (epg-signature-set-validity signature 'undefined))))
624 (defun epg-status-TRUST_NEVER (process string)
625 (let ((signature (car (epg-context-result-for epg-context 'verify))))
627 (eq (epg-signature-status signature) 'good))
628 (epg-signature-set-validity signature 'never))))
630 (defun epg-status-TRUST_MARGINAL (process string)
631 (let ((signature (car (epg-context-result-for epg-context 'verify))))
633 (eq (epg-signature-status signature) 'marginal))
634 (epg-signature-set-validity signature 'marginal))))
636 (defun epg-status-TRUST_FULLY (process string)
637 (let ((signature (car (epg-context-result-for epg-context 'verify))))
639 (eq (epg-signature-status signature) 'good))
640 (epg-signature-set-validity signature 'full))))
642 (defun epg-status-TRUST_ULTIMATE (process string)
643 (let ((signature (car (epg-context-result-for epg-context 'verify))))
645 (eq (epg-signature-status signature) 'good))
646 (epg-signature-set-validity signature 'ultimate))))
648 (defun epg-status-PROGRESS (process string)
649 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
651 (funcall (if (consp (epg-context-progress-callback epg-context))
652 (car (epg-context-progress-callback epg-context))
653 (epg-context-progress-callback epg-context))
654 (match-string 1 string)
655 (match-string 2 string)
656 (string-to-number (match-string 3 string))
657 (string-to-number (match-string 4 string))
658 (if (consp (epg-context-progress-callback epg-context))
659 (cdr (epg-context-progress-callback epg-context))))))
661 (defun epg-status-DECRYPTION_FAILED (process string)
662 (epg-context-set-result-for
664 (cons 'decryption-failed
665 (epg-context-result-for epg-context 'error))))
667 (defun epg-status-NODATA (process string)
668 (epg-context-set-result-for
670 (cons (cons 'no-data (string-to-number string))
671 (epg-context-result-for epg-context 'error))))
673 (defun epg-status-UNEXPECTED (process string)
674 (epg-context-set-result-for
676 (cons (cons 'unexpected (string-to-number string))
677 (epg-context-result-for epg-context 'error))))
679 (defun epg-status-KEYEXPIRED (process string)
680 (epg-context-set-result-for
682 (cons (cons 'key-expired string)
683 (epg-context-result-for epg-context 'error))))
685 (defun epg-status-KEYREVOKED (process string)
686 (epg-context-set-result-for
689 (epg-context-result-for epg-context 'error))))
691 (defun epg-status-BADARMOR (process string)
692 (epg-context-set-result-for
695 (epg-context-result-for epg-context 'error))))
697 (defun epg-status-INV_RECP (process string)
698 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
699 (epg-context-set-result-for
701 (cons (list 'invalid-recipient
702 (string-to-number (match-string 1 string))
703 (match-string 2 string))
704 (epg-context-result-for epg-context 'error)))))
706 (defun epg-status-NO_RECP (process string)
707 (epg-context-set-result-for
710 (epg-context-result-for epg-context 'error))))
712 (defun epg-status-DELETE_PROBLEM (process string)
713 (if (string-match "\\`\\([0-9]+\\)" string)
714 (epg-context-set-result-for
716 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
717 (epg-context-result-for epg-context 'error)))))
719 (defun epg-passphrase-callback-function (handback)
721 (if (eq epg-key-id 'SYM)
722 "Passphrase for symmetric encryption: "
723 (if (eq epg-key-id 'PIN)
724 "Passphrase for PIN: "
725 (let ((entry (assoc epg-key-id epg-user-id-alist)))
727 (format "Passphrase for %s %s: " epg-key-id (cdr entry))
728 (format "Passphrase for %s: " epg-key-id)))))))
730 (defun epg-progress-callback-function (what char current total handback)
731 (message "%s: %d%%/%d%%" what current total))
733 (defun epg-configuration ()
734 "Return a list of internal configuration parameters of `epg-gpg-program'."
737 (apply #'call-process epg-gpg-program nil (list t nil) nil
738 '("--with-colons" "--list-config"))
739 (goto-char (point-min))
740 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
741 (setq type (intern (match-string 1))
742 config (cons (cons type
744 '(pubkey cipher digest compress))
745 (mapcar #'string-to-number
746 (delete "" (split-string
753 (defun epg-list-keys-1 (name mode)
754 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
755 "--fixed-list-mode" "--with-fingerprint"
757 (if mode "--list-secret-keys" "--list-keys"))
758 (if name (list name))))
759 keys string field index)
761 (apply #'call-process epg-gpg-program nil (list t nil) nil args)
762 (goto-char (point-min))
763 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
764 (setq keys (cons (make-vector 15 nil) keys)
765 string (match-string 0)
769 (string-match "\\([^:]+\\)?:" string index))
770 (setq index (match-end 0))
771 (aset (car keys) field (match-string 1 string))
772 (setq field (1+ field))))
775 (defun epg-make-sub-key-1 (line)
778 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
780 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
782 (member (aref line 0) '("sec" "ssb"))
783 (string-to-number (aref line 3))
784 (string-to-number (aref line 2))
789 (defun epg-list-keys (&optional name mode)
790 (let ((lines (epg-list-keys-1 name mode))
794 ((member (aref (car lines) 0) '("pub" "sec"))
796 (epg-key-set-sub-key-list
798 (nreverse (epg-key-sub-key-list (car keys))))
799 (epg-key-set-user-id-list
801 (nreverse (epg-key-user-id-list (car keys)))))
802 (setq keys (cons (epg-make-key
803 (if (aref (car lines) 8)
804 (cdr (assq (string-to-char (aref (car lines) 8))
805 epg-key-validity-alist))))
807 (epg-key-set-sub-key-list
809 (cons (epg-make-sub-key-1 (car lines))
810 (epg-key-sub-key-list (car keys)))))
811 ((member (aref (car lines) 0) '("sub" "ssb"))
812 (epg-key-set-sub-key-list
814 (cons (epg-make-sub-key-1 (car lines))
815 (epg-key-sub-key-list (car keys)))))
816 ((equal (aref (car lines) 0) "uid")
817 (epg-key-set-user-id-list
819 (cons (epg-make-user-id
820 (if (aref (car lines) 1)
821 (cdr (assq (string-to-char (aref (car lines) 1))
822 epg-key-validity-alist)))
823 (aref (car lines) 9))
824 (epg-key-user-id-list (car keys)))))
825 ((equal (aref (car lines) 0) "fpr")
826 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
827 (aref (car lines) 9))))
828 (setq lines (cdr lines)))
831 (if (fboundp 'make-temp-file)
832 (defalias 'epg-make-temp-file 'make-temp-file)
833 ;; stolen from poe.el.
834 (defun epg-make-temp-file (prefix)
835 "Create a temporary file.
836 The returned file name (created by appending some random characters at the end
837 of PREFIX, and expanding against `temporary-file-directory' if necessary),
838 is guaranteed to point to a newly created empty file.
839 You can then use `write-region' to write new data into the file."
840 (let (tempdir tempfile)
843 ;; First, create a temporary directory.
844 (while (condition-case ()
846 (setq tempdir (make-temp-name
848 (file-name-directory prefix)
850 ;; return nil or signal an error.
851 (make-directory tempdir))
853 (file-already-exists t)))
854 (set-file-modes tempdir 448)
855 ;; Second, create a temporary file in the tempdir.
856 ;; There *is* a race condition between `make-temp-name'
857 ;; and `write-region', but we don't care it since we are
858 ;; in a private directory now.
859 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
860 (write-region "" nil tempfile nil 'silent)
861 (set-file-modes tempfile 384)
862 ;; Finally, make a hard-link from the tempfile.
863 (while (condition-case ()
865 (setq file (make-temp-name prefix))
866 ;; return nil or signal an error.
867 (add-name-to-file tempfile file))
869 (file-already-exists t)))
871 ;; Cleanup the tempfile.
873 (file-exists-p tempfile)
874 (delete-file tempfile))
875 ;; Cleanup the tempdir.
877 (file-directory-p tempdir)
878 (delete-directory tempdir))))))
881 (defun epg-start-decrypt (context cipher)
882 "Initiate a decrypt operation on CIPHER.
883 CIPHER is a data object.
885 If you use this function, you will need to wait for the completion of
886 `epg-gpg-program' by using `epg-wait-for-completion' and call
887 `epg-reset' to clear a temporaly output file.
888 If you are unsure, use synchronous version of this function
889 `epg-decrypt-file' or `epg-decrypt-string' instead."
890 (unless (epg-data-file cipher)
891 (error "Not a file"))
892 (epg-context-set-result context nil)
893 (epg-start context (list "--decrypt" (epg-data-file cipher)))
894 (epg-wait-for-status context '("BEGIN_DECRYPTION")))
897 (defun epg-decrypt-file (context cipher plain)
898 "Decrypt a file CIPHER and store the result to a file PLAIN.
899 If PLAIN is nil, it returns the result as a string."
903 (epg-context-set-output-file context plain)
904 (epg-context-set-output-file context
905 (epg-make-temp-file "epg-output")))
906 (epg-start-decrypt context (epg-make-data-from-file cipher))
907 (epg-wait-for-completion context t)
908 (if (epg-context-result-for context 'error)
909 (error "Decrypt failed: %S"
910 (epg-context-result-for context 'error)))
912 (epg-read-output context)))
914 (epg-delete-output-file context))
915 (epg-reset context)))
918 (defun epg-decrypt-string (context cipher)
919 "Decrypt a string CIPHER and return the plain text."
920 (let ((input-file (epg-make-temp-file "epg-input"))
921 (coding-system-for-write 'binary))
924 (write-region cipher nil input-file)
925 (epg-context-set-output-file context
926 (epg-make-temp-file "epg-output"))
927 (epg-start-decrypt context (epg-make-data-from-file input-file))
928 (epg-wait-for-completion context)
929 (if (epg-context-result-for context 'error)
930 (error "Decrypt failed: %S"
931 (epg-context-result-for context 'error)))
932 (epg-read-output context))
933 (epg-delete-output-file context)
934 (if (file-exists-p input-file)
935 (delete-file input-file))
936 (epg-reset context))))
939 (defun epg-start-verify (context signature &optional signed-text)
940 "Initiate a verify operation on SIGNATURE.
941 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
943 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
944 For a normal or a clear text signature, SIGNED-TEXT should be nil.
946 If you use this function, you will need to wait for the completion of
947 `epg-gpg-program' by using `epg-wait-for-completion' and call
948 `epg-reset' to clear a temporaly output file.
949 If you are unsure, use synchronous version of this function
950 `epg-verify-file' or `epg-verify-string' instead."
951 (epg-context-set-result context nil)
953 ;; Detached signature.
954 (if (epg-data-file signed-text)
955 (epg-start context (list "--verify" (epg-data-file signature)
956 (epg-data-file signed-text)))
957 (epg-start context (list "--verify" (epg-data-file signature) "-"))
958 (if (eq (process-status (epg-context-process context)) 'run)
959 (process-send-string (epg-context-process context)
960 (epg-data-string signed-text))))
961 ;; Normal (or cleartext) signature.
962 (if (epg-data-file signature)
963 (epg-start context (list "--verify" (epg-data-file signature)))
964 (epg-start context (list "--verify"))
965 (if (eq (process-status (epg-context-process context)) 'run)
966 (process-send-string (epg-context-process context)
967 (epg-data-string signature))))))
970 (defun epg-verify-file (context signature &optional signed-text plain)
971 "Verify a file SIGNATURE.
972 SIGNED-TEXT and PLAIN are also a file if they are specified.
974 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
975 For a normal or a clear text signature, SIGNED-TEXT should be nil."
979 (epg-context-set-output-file context plain)
980 (epg-context-set-output-file context
981 (epg-make-temp-file "epg-output")))
983 (epg-start-verify context
984 (epg-make-data-from-file signature)
985 (epg-make-data-from-file signed-text))
986 (epg-start-verify context
987 (epg-make-data-from-file signature)))
988 (epg-wait-for-completion context t)
990 (epg-read-output context)))
992 (epg-delete-output-file context))
993 (epg-reset context)))
996 (defun epg-verify-string (context signature &optional signed-text)
997 "Verify a string SIGNATURE.
998 SIGNED-TEXT is a string if it is specified.
1000 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1001 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1002 (let ((coding-system-for-write 'binary)
1006 (epg-context-set-output-file context
1007 (epg-make-temp-file "epg-output"))
1010 (setq input-file (epg-make-temp-file "epg-signature"))
1011 (write-region signature nil input-file)
1012 (epg-start-verify context
1013 (epg-make-data-from-file input-file)
1014 (epg-make-data-from-string signed-text)))
1015 (epg-start-verify context (epg-make-data-from-string signature)))
1016 (epg-wait-for-completion context)
1017 (epg-read-output context))
1018 (epg-delete-output-file context)
1020 (file-exists-p input-file))
1021 (delete-file input-file))
1022 (epg-reset context))))
1025 (defun epg-start-sign (context plain &optional mode)
1026 "Initiate a sign operation on PLAIN.
1027 PLAIN is a data object.
1029 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1030 If MODE is t or 'detached, it makes a detached signature.
1031 Otherwise, it makes a normal signature.
1033 If you use this function, you will need to wait for the completion of
1034 `epg-gpg-program' by using `epg-wait-for-completion' and call
1035 `epg-reset' to clear a temporaly output file.
1036 If you are unsure, use synchronous version of this function
1037 `epg-sign-file' or `epg-sign-string' instead."
1038 (epg-context-set-result context nil)
1040 (append (list (if (eq mode 'clearsign)
1042 (if (or (eq mode t) (eq mode 'detached))
1046 (mapcar (lambda (signer)
1048 (epg-context-signers context)))
1049 (if (epg-data-file plain)
1050 (list (epg-data-file plain)))))
1051 (epg-wait-for-status context '("BEGIN_SIGNING"))
1052 (if (and (epg-data-string plain)
1053 (eq (process-status (epg-context-process context)) 'run))
1054 (process-send-string (epg-context-process context)
1055 (epg-data-string plain))))
1058 (defun epg-sign-file (context plain signature &optional mode)
1059 "Sign a file PLAIN and store the result to a file SIGNATURE.
1060 If SIGNATURE is nil, it returns the result as a string.
1061 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1062 If MODE is t or 'detached, it makes a detached signature.
1063 Otherwise, it makes a normal signature."
1067 (epg-context-set-output-file context signature)
1068 (epg-context-set-output-file context
1069 (epg-make-temp-file "epg-output")))
1070 (epg-start-sign context (epg-make-data-from-file plain) mode)
1071 (epg-wait-for-completion context t)
1072 (if (epg-context-result-for context 'error)
1073 (error "Sign failed: %S"
1074 (epg-context-result-for context 'error)))
1076 (epg-read-output context)))
1078 (epg-delete-output-file context))
1079 (epg-reset context)))
1082 (defun epg-sign-string (context plain &optional mode)
1083 "Sign a string PLAIN and return the output as string.
1084 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1085 If MODE is t or 'detached, it makes a detached signature.
1086 Otherwise, it makes a normal signature."
1089 (epg-context-set-output-file context
1090 (epg-make-temp-file "epg-output"))
1091 (epg-start-sign context (epg-make-data-from-string plain) mode)
1092 (epg-wait-for-completion context)
1093 (if (epg-context-result-for context 'error)
1094 (error "Sign failed: %S"
1095 (epg-context-result-for context 'error)))
1096 (epg-read-output context))
1097 (epg-delete-output-file context)
1098 (epg-reset context)))
1101 (defun epg-start-encrypt (context plain recipients
1102 &optional sign always-trust)
1103 "Initiate an encrypt operation on PLAIN.
1104 PLAIN is a data object.
1105 If RECIPIENTS is nil, it performs symmetric encryption.
1107 If you use this function, you will need to wait for the completion of
1108 `epg-gpg-program' by using `epg-wait-for-completion' and call
1109 `epg-reset' to clear a temporaly output file.
1110 If you are unsure, use synchronous version of this function
1111 `epg-encrypt-file' or `epg-encrypt-string' instead."
1112 (epg-context-set-result context nil)
1114 (append (if always-trust '("--always-trust"))
1115 (if recipients '("--encrypt") '("--symmetric"))
1119 (mapcar (lambda (signer)
1121 (epg-context-signers context)))))
1123 (mapcar (lambda (recipient)
1124 (list "-r" recipient))
1126 (if (epg-data-file plain)
1127 (list (epg-data-file plain)))))
1129 (epg-wait-for-status context '("BEGIN_SIGNING")))
1130 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))
1131 (if (and (epg-data-string plain)
1132 (eq (process-status (epg-context-process context)) 'run))
1133 (process-send-string (epg-context-process context)
1134 (epg-data-string plain))))
1137 (defun epg-encrypt-file (context plain recipients
1138 cipher &optional sign always-trust)
1139 "Encrypt a file PLAIN and store the result to a file CIPHER.
1140 If CIPHER is nil, it returns the result as a string.
1141 If RECIPIENTS is nil, it performs symmetric encryption."
1145 (epg-context-set-output-file context cipher)
1146 (epg-context-set-output-file context
1147 (epg-make-temp-file "epg-output")))
1148 (epg-start-encrypt context (epg-make-data-from-file plain)
1149 recipients sign always-trust)
1150 (epg-wait-for-completion context t)
1151 (if (epg-context-result-for context 'error)
1152 (error "Encrypt failed: %S"
1153 (epg-context-result-for context 'error)))
1155 (epg-read-output context)))
1157 (epg-delete-output-file context))
1158 (epg-reset context)))
1161 (defun epg-encrypt-string (context plain recipients
1162 &optional sign always-trust)
1163 "Encrypt a string PLAIN.
1164 If RECIPIENTS is nil, it performs symmetric encryption."
1167 (epg-context-set-output-file context
1168 (epg-make-temp-file "epg-output"))
1169 (epg-start-encrypt context (epg-make-data-from-string plain)
1170 recipients sign always-trust)
1171 (epg-wait-for-completion context)
1172 (if (epg-context-result-for context 'error)
1173 (error "Encrypt failed: %S"
1174 (epg-context-result-for context 'error)))
1175 (epg-read-output context))
1176 (epg-delete-output-file context)
1177 (epg-reset context)))
1180 (defun epg-start-export-keys (context keys)
1181 "Initiate an export keys operation.
1183 If you use this function, you will need to wait for the completion of
1184 `epg-gpg-program' by using `epg-wait-for-completion' and call
1185 `epg-reset' to clear a temporaly output file.
1186 If you are unsure, use synchronous version of this function
1187 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1188 (epg-context-set-result context nil)
1189 (epg-start context (cons "--export"
1193 (car (epg-key-sub-key-list key))))
1197 (defun epg-export-keys-to-file (context keys file)
1198 "Extract public KEYS."
1202 (epg-context-set-output-file context file)
1203 (epg-context-set-output-file context
1204 (epg-make-temp-file "epg-output")))
1205 (epg-start-export-keys context keys)
1206 (epg-wait-for-completion context)
1207 (if (epg-context-result-for context 'error)
1208 (error "Export keys failed"))
1210 (epg-read-output context)))
1212 (epg-delete-output-file context))
1213 (epg-reset context)))
1216 (defun epg-export-keys-to-string (context keys)
1217 "Extract public KEYS and return them as a string."
1218 (epg-export-keys-to-file context keys nil))
1221 (defun epg-start-import-keys (context keys)
1222 "Initiate an import keys operation.
1223 KEYS is a data object.
1225 If you use this function, you will need to wait for the completion of
1226 `epg-gpg-program' by using `epg-wait-for-completion' and call
1227 `epg-reset' to clear a temporaly output file.
1228 If you are unsure, use synchronous version of this function
1229 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1230 (epg-context-set-result context nil)
1231 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1232 (epg-start context (list "--import" (epg-data-file keys)))
1233 (if (and (epg-data-string keys)
1234 (eq (process-status (epg-context-process context)) 'run))
1235 (process-send-string (epg-context-process context)
1236 (epg-data-string keys))))
1238 (defun epg-import-keys-1 (context keys)
1241 (epg-start-import-keys context keys)
1242 (epg-wait-for-completion context (epg-data-file keys))
1243 (if (epg-context-result-for context 'error)
1244 (error "Import keys failed"))
1245 (epg-read-output context))
1246 (epg-reset context)))
1249 (defun epg-import-keys-from-file (context keys)
1250 "Add keys from a file KEYS."
1251 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1254 (defun epg-import-keys-from-string (context keys)
1255 "Add keys from a string KEYS."
1256 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1259 (defun epg-start-delete-keys (context keys &optional allow-secret)
1260 "Initiate an delete keys operation.
1262 If you use this function, you will need to wait for the completion of
1263 `epg-gpg-program' by using `epg-wait-for-completion' and call
1264 `epg-reset' to clear a temporaly output file.
1265 If you are unsure, use synchronous version of this function
1266 `epg-delete-keys' instead."
1267 (epg-context-set-result context nil)
1268 (epg-start context (cons (if allow-secret
1269 "--delete-secret-key"
1274 (car (epg-key-sub-key-list key))))
1278 (defun epg-delete-keys (context keys &optional allow-secret)
1279 "Delete KEYS from the key ring."
1282 (epg-start-delete-keys context keys)
1283 (epg-wait-for-completion context t)
1284 (if (epg-context-result-for context 'error)
1285 (error "Delete key failed")))
1286 (epg-reset context)))
1290 ;;; epg.el ends here