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)
49 (defvar epg-debug-buffer nil)
51 ;; from gnupg/include/cipher.h
52 (defconst epg-cipher-algorithm-alist
64 ;; from gnupg/include/cipher.h
65 (defconst epg-pubkey-algorithm-alist
73 ;; from gnupg/include/cipher.h
74 (defconst epg-digest-algorithm-alist
82 ;; from gnupg/include/cipher.h
83 (defconst epg-compress-algorithm-alist
89 (defconst epg-invalid-recipients-alist
90 '((0 . "No specific reason given")
92 (2 . "Ambigious specification")
93 (3 . "Wrong key usage")
98 (8 . "Policy mismatch")
99 (9 . "Not a secret key")
100 (10 . "Key not trusted")))
102 (defconst epg-delete-problem-alist
103 '((1 . "No such key")
104 (2 . "Must delete secret key first")
105 (3 . "Ambigious specification")))
107 (defvar epg-key-validity-alist
120 (defvar epg-key-capablity-alist
124 (?a . authentication)))
126 (defvar epg-prompt-alist nil)
128 (defun epg-make-data-from-file (file)
129 "Make a data object from FILE."
132 (defun epg-make-data-from-string (string)
133 "Make a data object from STRING."
136 (defun epg-data-file (data)
137 "Return the file of DATA."
140 (defun epg-data-string (data)
141 "Return the string of DATA."
144 (defun epg-make-context (&optional protocol armor textmode include-certs
145 cipher-algorithm digest-algorithm
147 "Return a context object."
148 (vector protocol armor textmode include-certs
149 cipher-algorithm digest-algorithm compress-algorithm
150 #'epg-passphrase-callback-function
151 #'epg-progress-callback-function
154 (defun epg-context-protocol (context)
155 "Return the protocol used within CONTEXT."
158 (defun epg-context-armor (context)
159 "Return t if the output shouled be ASCII armored in CONTEXT."
162 (defun epg-context-textmode (context)
163 "Return t if canonical text mode should be used in CONTEXT."
166 (defun epg-context-include-certs (context)
167 "Return how many certificates should be included in an S/MIME signed
171 (defun epg-context-cipher-algorithm (context)
172 "Return the cipher algorithm in CONTEXT."
175 (defun epg-context-digest-algorithm (context)
176 "Return the digest algorithm in CONTEXT."
179 (defun epg-context-compress-algorithm (context)
180 "Return the compress algorithm in CONTEXT."
183 (defun epg-context-passphrase-callback (context)
184 "Return the function used to query passphrase."
187 (defun epg-context-progress-callback (context)
188 "Return the function which handles progress update."
191 (defun epg-context-signers (context)
192 "Return the list of key-id for singning."
195 (defun epg-context-process (context)
196 "Return the process object of `epg-gpg-program'.
197 This function is for internal use only."
200 (defun epg-context-output-file (context)
201 "Return the output file of `epg-gpg-program'.
202 This function is for internal use only."
205 (defun epg-context-result (context)
206 "Return the result of the previous cryptographic operation."
209 (defun epg-context-set-protocol (context protocol)
210 "Set the protocol used within CONTEXT."
211 (aset context 0 protocol))
213 (defun epg-context-set-armor (context armor)
214 "Specify if the output shouled be ASCII armored in CONTEXT."
215 (aset context 1 armor))
217 (defun epg-context-set-textmode (context textmode)
218 "Specify if canonical text mode should be used in CONTEXT."
219 (aset context 2 textmode))
221 (defun epg-context-set-include-certs (context include-certs)
222 "Set how many certificates should be included in an S/MIME signed message."
223 (aset context 3 include-certs))
225 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
226 "Set the cipher algorithm in CONTEXT."
227 (aset context 4 cipher-algorithm))
229 (defun epg-context-set-digest-algorithm (context digest-algorithm)
230 "Set the digest algorithm in CONTEXT."
231 (aset context 5 digest-algorithm))
233 (defun epg-context-set-compress-algorithm (context compress-algorithm)
234 "Set the compress algorithm in CONTEXT."
235 (aset context 6 compress-algorithm))
237 (defun epg-context-set-passphrase-callback (context
239 "Set the function used to query passphrase."
240 (aset context 7 passphrase-callback))
242 (defun epg-context-set-progress-callback (context progress-callback)
243 "Set the function which handles progress update."
244 (aset context 8 progress-callback))
246 (defun epg-context-set-signers (context signers)
247 "Set the list of key-id for singning."
248 (aset context 9 signers))
250 (defun epg-context-set-process (context process)
251 "Set the process object of `epg-gpg-program'.
252 This function is for internal use only."
253 (aset context 10 process))
255 (defun epg-context-set-output-file (context output-file)
256 "Set the output file of `epg-gpg-program'.
257 This function is for internal use only."
258 (aset context 11 output-file))
260 (defun epg-context-set-result (context result)
261 "Set the result of the previous cryptographic operation."
262 (aset context 12 result))
264 (defun epg-make-signature (status key-id user-id)
265 "Return a signature object."
266 (vector status key-id user-id nil nil))
268 (defun epg-signature-status (signature)
269 "Return the status code of SIGNATURE."
272 (defun epg-signature-key-id (signature)
273 "Return the key-id of SIGNATURE."
276 (defun epg-signature-user-id (signature)
277 "Return the user-id of SIGNATURE."
280 (defun epg-signature-validity (signature)
281 "Return the validity of SIGNATURE."
284 (defun epg-signature-fingerprint (signature)
285 "Return the fingerprint of SIGNATURE."
288 (defun epg-signature-set-status (signature status)
289 "Set the status code of SIGNATURE."
290 (aset signature 0 status))
292 (defun epg-signature-set-key-id (signature key-id)
293 "Set the key-id of SIGNATURE."
294 (aset signature 1 key-id))
296 (defun epg-signature-set-user-id (signature user-id)
297 "Set the user-id of SIGNATURE."
298 (aset signature 2 user-id))
300 (defun epg-signature-set-validity (signature validity)
301 "Set the validity of SIGNATURE."
302 (aset signature 3 validity))
304 (defun epg-signature-set-fingerprint (signature fingerprint)
305 "Set the fingerprint of SIGNATURE."
306 (aset signature 4 fingerprint))
308 (defun epg-make-key (owner-trust)
309 "Return a key object."
310 (vector owner-trust nil nil))
312 (defun epg-key-owner-trust (key)
313 "Return the owner trust of KEY."
316 (defun epg-key-sub-key-list (key)
317 "Return the sub key list of KEY."
320 (defun epg-key-user-id-list (key)
321 "Return the user ID list of KEY."
324 (defun epg-key-set-sub-key-list (key sub-key-list)
325 "Set the sub key list of KEY."
326 (aset key 1 sub-key-list))
328 (defun epg-key-set-user-id-list (key user-id-list)
329 "Set the user ID list of KEY."
330 (aset key 2 user-id-list))
332 (defun epg-make-sub-key (validity capability secret algorithm length id
333 creation-time expiration-time)
334 "Return a sub key object."
335 (vector validity capability secret algorithm length id creation-time
336 expiration-time nil))
338 (defun epg-sub-key-validity (sub-key)
339 "Return the validity of SUB-KEY."
342 (defun epg-sub-key-capability (sub-key)
343 "Return the capability of SUB-KEY."
346 (defun epg-sub-key-secret (sub-key)
347 "Return non-nil if SUB-KEY is a secret key."
350 (defun epg-sub-key-algorithm (sub-key)
351 "Return the algorithm of SUB-KEY."
354 (defun epg-sub-key-length (sub-key)
355 "Return the length of SUB-KEY."
358 (defun epg-sub-key-id (sub-key)
359 "Return the ID of SUB-KEY."
362 (defun epg-sub-key-creation-time (sub-key)
363 "Return the creation time of SUB-KEY."
366 (defun epg-sub-key-expiration-time (sub-key)
367 "Return the expiration time of SUB-KEY."
370 (defun epg-sub-key-fingerprint (sub-key)
371 "Return the fingerprint of SUB-KEY."
374 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
375 "Set the fingerprint of SUB-KEY.
376 This function is for internal use only."
377 (aset sub-key 8 fingerprint))
379 (defun epg-make-user-id (validity name)
380 "Return a user ID object."
381 (vector validity name nil))
383 (defun epg-user-id-validity (user-id)
384 "Return the validity of USER-ID."
387 (defun epg-user-id-name (user-id)
388 "Return the name of USER-ID."
391 (defun epg-user-id-signature-list (user-id)
392 "Return the signature list of USER-ID."
395 (defun epg-user-id-set-signature-list (user-id signature-list)
396 "Set the signature list of USER-ID."
397 (aset user-id 2 signature-list))
399 (defun epg-context-result-for (context name)
400 (cdr (assq name (epg-context-result context))))
402 (defun epg-context-set-result-for (context name value)
403 (let* ((result (epg-context-result context))
404 (entry (assq name result)))
407 (epg-context-set-result context (cons (cons name value) result)))))
409 (defun epg-start (context args)
410 "Start `epg-gpg-program' in a subprocess with given ARGS."
411 (let* ((args (append (list "--no-tty"
415 (if (epg-context-armor context) '("--armor"))
416 (if (epg-context-textmode context) '("--textmode"))
417 (if (epg-context-output-file context)
418 (list "--output" (epg-context-output-file context)))
420 (coding-system-for-write 'binary)
421 process-connection-type
422 (orig-mode (default-file-modes))
423 (buffer (generate-new-buffer " *epg*"))
427 (unless epg-debug-buffer
428 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
429 (set-buffer epg-debug-buffer)
430 (goto-char (point-max))
431 (insert (format "%s %s\n" epg-gpg-program
432 (mapconcat #'identity args " ")))))
433 (with-current-buffer buffer
434 (make-local-variable 'epg-read-point)
435 (setq epg-read-point (point-min))
436 (make-local-variable 'epg-pending-status-list)
437 (setq epg-pending-status-list nil)
438 (make-local-variable 'epg-key-id)
439 (setq epg-key-id nil)
440 (make-local-variable 'epg-context)
441 (setq epg-context context))
444 (set-default-file-modes 448)
446 (apply #'start-process "epg" buffer epg-gpg-program args)))
447 (set-default-file-modes orig-mode))
448 (set-process-filter process #'epg-process-filter)
449 (epg-context-set-process context process)))
451 (defun epg-process-filter (process input)
454 (unless epg-debug-buffer
455 (setq epg-debug-buffer (generate-new-buffer " *epg-debug*")))
456 (set-buffer epg-debug-buffer)
457 (goto-char (point-max))
459 (if (buffer-live-p (process-buffer process))
461 (set-buffer (process-buffer process))
462 (goto-char (point-max))
464 (goto-char epg-read-point)
466 (while (looking-at ".*\n") ;the input line finished
468 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
469 (let* ((status (match-string 1))
470 (string (match-string 2))
471 (symbol (intern-soft (concat "epg-status-" status))))
472 (if (member status epg-pending-status-list)
473 (setq epg-pending-status-list nil))
476 (funcall symbol process string)))))
478 (setq epg-read-point (point)))))
480 (defun epg-read-output (context)
482 (if (fboundp 'set-buffer-multibyte)
483 (set-buffer-multibyte nil))
484 (if (file-exists-p (epg-context-output-file context))
485 (let ((coding-system-for-read (if (epg-context-textmode context)
488 (insert-file-contents (epg-context-output-file context))
491 (defun epg-wait-for-status (context status-list)
492 (with-current-buffer (process-buffer (epg-context-process context))
493 (setq epg-pending-status-list status-list)
494 (while (and (eq (process-status (epg-context-process context)) 'run)
495 epg-pending-status-list)
496 (accept-process-output (epg-context-process context) 1))))
498 (defun epg-wait-for-completion (context)
499 (while (eq (process-status (epg-context-process context)) 'run)
500 ;; We can't use accept-process-output instead of sit-for here
501 ;; because it may cause an interrupt during the sentinel execution.
504 (defun epg-flush (context)
505 (if (eq (process-status (epg-context-process context)) 'run)
506 (process-send-eof (epg-context-process context))))
508 (defun epg-reset (context)
509 (if (and (epg-context-process context)
510 (buffer-live-p (process-buffer (epg-context-process context))))
511 (kill-buffer (process-buffer (epg-context-process context))))
512 (epg-context-set-process context nil))
514 (defun epg-delete-output-file (context)
515 (if (and (epg-context-output-file context)
516 (file-exists-p (epg-context-output-file context)))
517 (delete-file (epg-context-output-file context))))
519 (defun epg-status-USERID_HINT (process string)
520 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
521 (let* ((key-id (match-string 1 string))
522 (user-id (match-string 2 string))
523 (entry (assoc key-id epg-user-id-alist)))
525 (setcdr entry user-id)
526 (setq epg-user-id-alist (cons (cons key-id user-id)
527 epg-user-id-alist))))))
529 (defun epg-status-NEED_PASSPHRASE (process string)
530 (if (string-match "\\`\\([^ ]+\\)" string)
531 (setq epg-key-id (match-string 1 string))))
533 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
534 (setq epg-key-id 'SYM))
536 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
537 (setq epg-key-id 'PIN))
539 (defun epg-status-GET_HIDDEN (process string)
541 (string-match "\\`passphrase\\." string))
544 (if (consp (epg-context-passphrase-callback epg-context))
545 (car (epg-context-passphrase-callback epg-context))
546 (epg-context-passphrase-callback epg-context))
548 (if (consp (epg-context-passphrase-callback epg-context))
549 (cdr (epg-context-passphrase-callback epg-context)))))
554 (setq string (concat passphrase "\n"))
555 (fillarray passphrase 0)
556 (setq passphrase nil)
557 (process-send-string process string))
559 (fillarray string 0)))))))
561 (defun epg-status-GET_BOOL (process string)
562 (let ((entry (assoc string epg-prompt-alist)))
563 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
564 (process-send-string process "y\n")
565 (process-send-string process "n\n"))))
567 (defun epg-status-GET_LINE (process string)
568 (let* ((entry (assoc string epg-prompt-alist))
569 (string (read-string (if entry (cdr entry) (concat string ": ")))))
570 (process-send-string process (concat string "\n"))))
572 (defun epg-status-GOODSIG (process string)
573 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
574 (epg-context-set-result-for
577 (cons (epg-make-signature 'good
578 (match-string 1 string)
579 (match-string 2 string))
580 (epg-context-result-for epg-context 'verify)))))
582 (defun epg-status-EXPSIG (process string)
583 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
584 (epg-context-set-result-for
587 (cons (epg-make-signature 'expired
588 (match-string 1 string)
589 (match-string 2 string))
590 (epg-context-result-for epg-context 'verify)))))
592 (defun epg-status-EXPKEYSIG (process string)
593 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
594 (epg-context-set-result-for
597 (cons (epg-make-signature 'expired-key
598 (match-string 1 string)
599 (match-string 2 string))
600 (epg-context-result-for epg-context 'verify)))))
602 (defun epg-status-REVKEYSIG (process string)
603 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
604 (epg-context-set-result-for
607 (cons (epg-make-signature 'revoked-key
608 (match-string 1 string)
609 (match-string 2 string))
610 (epg-context-result-for epg-context 'verify)))))
612 (defun epg-status-BADSIG (process string)
613 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
614 (epg-context-set-result-for
617 (cons (epg-make-signature 'bad
618 (match-string 1 string)
619 (match-string 2 string))
620 (epg-context-result-for epg-context 'verify)))))
622 (defun epg-status-VALIDSIG (process string)
623 (let ((signature (car (epg-context-result-for epg-context 'verify))))
625 (eq (epg-signature-status signature) 'good)
626 (string-match "\\`\\([^ ]+\\) " string))
627 (epg-signature-set-fingerprint signature (match-string 1 string)))))
629 (defun epg-status-TRUST_UNDEFINED (process string)
630 (let ((signature (car (epg-context-result-for epg-context 'verify))))
632 (eq (epg-signature-status signature) 'good))
633 (epg-signature-set-validity signature 'undefined))))
635 (defun epg-status-TRUST_NEVER (process string)
636 (let ((signature (car (epg-context-result-for epg-context 'verify))))
638 (eq (epg-signature-status signature) 'good))
639 (epg-signature-set-validity signature 'never))))
641 (defun epg-status-TRUST_MARGINAL (process string)
642 (let ((signature (car (epg-context-result-for epg-context 'verify))))
644 (eq (epg-signature-status signature) 'marginal))
645 (epg-signature-set-validity signature 'marginal))))
647 (defun epg-status-TRUST_FULLY (process string)
648 (let ((signature (car (epg-context-result-for epg-context 'verify))))
650 (eq (epg-signature-status signature) 'good))
651 (epg-signature-set-validity signature 'full))))
653 (defun epg-status-TRUST_ULTIMATE (process string)
654 (let ((signature (car (epg-context-result-for epg-context 'verify))))
656 (eq (epg-signature-status signature) 'good))
657 (epg-signature-set-validity signature 'ultimate))))
659 (defun epg-status-PROGRESS (process string)
660 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
662 (funcall (if (consp (epg-context-progress-callback epg-context))
663 (car (epg-context-progress-callback epg-context))
664 (epg-context-progress-callback epg-context))
665 (match-string 1 string)
666 (match-string 2 string)
667 (string-to-number (match-string 3 string))
668 (string-to-number (match-string 4 string))
669 (if (consp (epg-context-progress-callback epg-context))
670 (cdr (epg-context-progress-callback epg-context))))))
672 (defun epg-status-DECRYPTION_FAILED (process string)
673 (epg-context-set-result-for
675 (cons 'decryption-failed
676 (epg-context-result-for epg-context 'error))))
678 (defun epg-status-NODATA (process string)
679 (epg-context-set-result-for
681 (cons (cons 'no-data (string-to-number string))
682 (epg-context-result-for epg-context 'error))))
684 (defun epg-status-UNEXPECTED (process string)
685 (epg-context-set-result-for
687 (cons (cons 'unexpected (string-to-number string))
688 (epg-context-result-for epg-context 'error))))
690 (defun epg-status-KEYEXPIRED (process string)
691 (epg-context-set-result-for
693 (cons (cons 'key-expired string)
694 (epg-context-result-for epg-context 'error))))
696 (defun epg-status-KEYREVOKED (process string)
697 (epg-context-set-result-for
700 (epg-context-result-for epg-context 'error))))
702 (defun epg-status-BADARMOR (process string)
703 (epg-context-set-result-for
706 (epg-context-result-for epg-context 'error))))
708 (defun epg-status-INV_RECP (process string)
709 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
710 (epg-context-set-result-for
712 (cons (list 'invalid-recipient
713 (string-to-number (match-string 1 string))
714 (match-string 2 string))
715 (epg-context-result-for epg-context 'error)))))
717 (defun epg-status-NO_RECP (process string)
718 (epg-context-set-result-for
721 (epg-context-result-for epg-context 'error))))
723 (defun epg-status-DELETE_PROBLEM (process string)
724 (if (string-match "\\`\\([0-9]+\\)" string)
725 (epg-context-set-result-for
727 (cons (cons 'delete-problem (string-to-number (match-string 1 string)))
728 (epg-context-result-for epg-context 'error)))))
730 (defun epg-passphrase-callback-function (key-id handback)
733 "Passphrase for symmetric encryption: "
735 "Passphrase for PIN: "
736 (let ((entry (assoc key-id epg-user-id-alist)))
738 (format "Passphrase for %s %s: " key-id (cdr entry))
739 (format "Passphrase for %s: " key-id)))))))
741 (defun epg-progress-callback-function (what char current total handback)
742 (message "%s: %d%%/%d%%" what current total))
744 (defun epg-configuration ()
745 "Return a list of internal configuration parameters of `epg-gpg-program'."
748 (apply #'call-process epg-gpg-program nil (list t nil) nil
749 '("--with-colons" "--list-config"))
750 (goto-char (point-min))
751 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
752 (setq type (intern (match-string 1))
753 config (cons (cons type
755 '(pubkey cipher digest compress))
756 (mapcar #'string-to-number
757 (delete "" (split-string
764 (defun epg-list-keys-1 (name mode)
765 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
766 "--fixed-list-mode" "--with-fingerprint"
768 (if mode "--list-secret-keys" "--list-keys"))
769 (if name (list name))))
770 keys string field index)
772 (apply #'call-process epg-gpg-program nil (list t nil) nil args)
773 (goto-char (point-min))
774 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
775 (setq keys (cons (make-vector 15 nil) keys)
776 string (match-string 0)
780 (string-match "\\([^:]+\\)?:" string index))
781 (setq index (match-end 0))
782 (aset (car keys) field (match-string 1 string))
783 (setq field (1+ field))))
786 (defun epg-make-sub-key-1 (line)
789 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist)))
791 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
793 (member (aref line 0) '("sec" "ssb"))
794 (string-to-number (aref line 3))
795 (string-to-number (aref line 2))
800 (defun epg-list-keys (&optional name mode)
801 (let ((lines (epg-list-keys-1 name mode))
805 ((member (aref (car lines) 0) '("pub" "sec"))
807 (epg-key-set-sub-key-list
809 (nreverse (epg-key-sub-key-list (car keys))))
810 (epg-key-set-user-id-list
812 (nreverse (epg-key-user-id-list (car keys)))))
813 (setq keys (cons (epg-make-key
814 (if (aref (car lines) 8)
815 (cdr (assq (string-to-char (aref (car lines) 8))
816 epg-key-validity-alist))))
818 (epg-key-set-sub-key-list
820 (cons (epg-make-sub-key-1 (car lines))
821 (epg-key-sub-key-list (car keys)))))
822 ((member (aref (car lines) 0) '("sub" "ssb"))
823 (epg-key-set-sub-key-list
825 (cons (epg-make-sub-key-1 (car lines))
826 (epg-key-sub-key-list (car keys)))))
827 ((equal (aref (car lines) 0) "uid")
828 (epg-key-set-user-id-list
830 (cons (epg-make-user-id
831 (if (aref (car lines) 1)
832 (cdr (assq (string-to-char (aref (car lines) 1))
833 epg-key-validity-alist)))
834 (aref (car lines) 9))
835 (epg-key-user-id-list (car keys)))))
836 ((equal (aref (car lines) 0) "fpr")
837 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
838 (aref (car lines) 9))))
839 (setq lines (cdr lines)))
842 (if (fboundp 'make-temp-file)
843 (defalias 'epg-make-temp-file 'make-temp-file)
844 ;; stolen from poe.el.
845 (defun epg-make-temp-file (prefix)
846 "Create a temporary file.
847 The returned file name (created by appending some random characters at the end
848 of PREFIX, and expanding against `temporary-file-directory' if necessary),
849 is guaranteed to point to a newly created empty file.
850 You can then use `write-region' to write new data into the file."
851 (let (tempdir tempfile)
854 ;; First, create a temporary directory.
855 (while (condition-case ()
857 (setq tempdir (make-temp-name
859 (file-name-directory prefix)
861 ;; return nil or signal an error.
862 (make-directory tempdir))
864 (file-already-exists t)))
865 (set-file-modes tempdir 448)
866 ;; Second, create a temporary file in the tempdir.
867 ;; There *is* a race condition between `make-temp-name'
868 ;; and `write-region', but we don't care it since we are
869 ;; in a private directory now.
870 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
871 (write-region "" nil tempfile nil 'silent)
872 (set-file-modes tempfile 384)
873 ;; Finally, make a hard-link from the tempfile.
874 (while (condition-case ()
876 (setq file (make-temp-name prefix))
877 ;; return nil or signal an error.
878 (add-name-to-file tempfile file))
880 (file-already-exists t)))
882 ;; Cleanup the tempfile.
884 (file-exists-p tempfile)
885 (delete-file tempfile))
886 ;; Cleanup the tempdir.
888 (file-directory-p tempdir)
889 (delete-directory tempdir))))))
892 (defun epg-start-decrypt (context cipher)
893 "Initiate a decrypt operation on CIPHER.
894 CIPHER is a data object.
896 If you use this function, you will need to wait for the completion of
897 `epg-gpg-program' by using `epg-wait-for-completion' and call
898 `epg-reset' to clear a temporaly output file.
899 If you are unsure, use synchronous version of this function
900 `epg-decrypt-file' or `epg-decrypt-string' instead."
901 (unless (epg-data-file cipher)
902 (error "Not a file"))
903 (epg-context-set-result context nil)
904 (epg-start context (list "--decrypt" (epg-data-file cipher)))
905 (epg-wait-for-status context '("BEGIN_DECRYPTION")))
908 (defun epg-decrypt-file (context cipher plain)
909 "Decrypt a file CIPHER and store the result to a file PLAIN.
910 If PLAIN is nil, it returns the result as a string."
914 (epg-context-set-output-file context plain)
915 (epg-context-set-output-file context
916 (epg-make-temp-file "epg-output")))
917 (epg-start-decrypt context (epg-make-data-from-file cipher))
918 (epg-wait-for-completion context)
919 (if (epg-context-result-for context 'error)
920 (error "Decrypt failed: %S"
921 (epg-context-result-for context 'error)))
923 (epg-read-output context)))
925 (epg-delete-output-file context))
926 (epg-reset context)))
929 (defun epg-decrypt-string (context cipher)
930 "Decrypt a string CIPHER and return the plain text."
931 (let ((input-file (epg-make-temp-file "epg-input"))
932 (coding-system-for-write 'binary))
935 (write-region cipher nil input-file)
936 (epg-context-set-output-file context
937 (epg-make-temp-file "epg-output"))
938 (epg-start-decrypt context (epg-make-data-from-file input-file))
940 (epg-wait-for-completion context)
941 (if (epg-context-result-for context 'error)
942 (error "Decrypt failed: %S"
943 (epg-context-result-for context 'error)))
944 (epg-read-output context))
945 (epg-delete-output-file context)
946 (if (file-exists-p input-file)
947 (delete-file input-file))
948 (epg-reset context))))
951 (defun epg-start-verify (context signature &optional signed-text)
952 "Initiate a verify operation on SIGNATURE.
953 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
955 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
956 For a normal or a clear text signature, SIGNED-TEXT should be nil.
958 If you use this function, you will need to wait for the completion of
959 `epg-gpg-program' by using `epg-wait-for-completion' and call
960 `epg-reset' to clear a temporaly output file.
961 If you are unsure, use synchronous version of this function
962 `epg-verify-file' or `epg-verify-string' instead."
963 (epg-context-set-result context nil)
965 ;; Detached signature.
966 (if (epg-data-file signed-text)
967 (epg-start context (list "--verify" (epg-data-file signature)
968 (epg-data-file signed-text)))
969 (epg-start context (list "--verify" (epg-data-file signature) "-"))
970 (if (eq (process-status (epg-context-process context)) 'run)
971 (process-send-string (epg-context-process context)
972 (epg-data-string signed-text))))
973 ;; Normal (or cleartext) signature.
974 (if (epg-data-file signature)
975 (epg-start context (list "--verify" (epg-data-file signature)))
976 (epg-start context (list "--verify"))
977 (if (eq (process-status (epg-context-process context)) 'run)
978 (process-send-string (epg-context-process context)
979 (epg-data-string signature))))))
982 (defun epg-verify-file (context signature &optional signed-text plain)
983 "Verify a file SIGNATURE.
984 SIGNED-TEXT and PLAIN are also a file if they are specified.
986 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
987 For a normal or a clear text signature, SIGNED-TEXT should be nil."
991 (epg-context-set-output-file context plain)
992 (epg-context-set-output-file context
993 (epg-make-temp-file "epg-output")))
995 (epg-start-verify context
996 (epg-make-data-from-file signature)
997 (epg-make-data-from-file signed-text))
998 (epg-start-verify context
999 (epg-make-data-from-file signature)))
1000 (epg-wait-for-completion context)
1002 (epg-read-output context)))
1004 (epg-delete-output-file context))
1005 (epg-reset context)))
1008 (defun epg-verify-string (context signature &optional signed-text)
1009 "Verify a string SIGNATURE.
1010 SIGNED-TEXT is a string if it is specified.
1012 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
1013 For a normal or a clear text signature, SIGNED-TEXT should be nil."
1014 (let ((coding-system-for-write 'binary)
1018 (epg-context-set-output-file context
1019 (epg-make-temp-file "epg-output"))
1022 (setq input-file (epg-make-temp-file "epg-signature"))
1023 (write-region signature nil input-file)
1024 (epg-start-verify context
1025 (epg-make-data-from-file input-file)
1026 (epg-make-data-from-string signed-text)))
1027 (epg-start-verify context (epg-make-data-from-string signature)))
1029 (epg-wait-for-completion context)
1030 (epg-read-output context))
1031 (epg-delete-output-file context)
1033 (file-exists-p input-file))
1034 (delete-file input-file))
1035 (epg-reset context))))
1038 (defun epg-start-sign (context plain &optional mode)
1039 "Initiate a sign operation on PLAIN.
1040 PLAIN is a data object.
1042 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1043 If MODE is t or 'detached, it makes a detached signature.
1044 Otherwise, it makes a normal signature.
1046 If you use this function, you will need to wait for the completion of
1047 `epg-gpg-program' by using `epg-wait-for-completion' and call
1048 `epg-reset' to clear a temporaly output file.
1049 If you are unsure, use synchronous version of this function
1050 `epg-sign-file' or `epg-sign-string' instead."
1051 (epg-context-set-result context nil)
1053 (append (list (if (eq mode 'clearsign)
1055 (if (or (eq mode t) (eq mode 'detached))
1059 (mapcar (lambda (signer)
1061 (epg-context-signers context)))
1062 (if (epg-data-file plain)
1063 (list (epg-data-file plain)))))
1064 (epg-wait-for-status context '("BEGIN_SIGNING"))
1065 (if (and (epg-data-string plain)
1066 (eq (process-status (epg-context-process context)) 'run))
1067 (process-send-string (epg-context-process context)
1068 (epg-data-string plain))))
1071 (defun epg-sign-file (context plain signature &optional mode)
1072 "Sign a file PLAIN and store the result to a file SIGNATURE.
1073 If SIGNATURE is nil, it returns the result as a string.
1074 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1075 If MODE is t or 'detached, it makes a detached signature.
1076 Otherwise, it makes a normal signature."
1080 (epg-context-set-output-file context signature)
1081 (epg-context-set-output-file context
1082 (epg-make-temp-file "epg-output")))
1083 (epg-start-sign context (epg-make-data-from-file plain) mode)
1084 (epg-wait-for-completion context)
1085 (if (epg-context-result-for context 'error)
1086 (error "Sign failed: %S"
1087 (epg-context-result-for context 'error)))
1089 (epg-read-output context)))
1091 (epg-delete-output-file context))
1092 (epg-reset context)))
1095 (defun epg-sign-string (context plain &optional mode)
1096 "Sign a string PLAIN and return the output as string.
1097 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1098 If MODE is t or 'detached, it makes a detached signature.
1099 Otherwise, it makes a normal signature."
1102 (epg-context-set-output-file context
1103 (epg-make-temp-file "epg-output"))
1104 (epg-start-sign context (epg-make-data-from-string plain) mode)
1106 (epg-wait-for-completion context)
1107 (if (epg-context-result-for context 'error)
1108 (error "Sign failed: %S"
1109 (epg-context-result-for context 'error)))
1110 (epg-read-output context))
1111 (epg-delete-output-file context)
1112 (epg-reset context)))
1115 (defun epg-start-encrypt (context plain recipients
1116 &optional sign always-trust)
1117 "Initiate an encrypt operation on PLAIN.
1118 PLAIN is a data object.
1119 If RECIPIENTS is nil, it performs symmetric encryption.
1121 If you use this function, you will need to wait for the completion of
1122 `epg-gpg-program' by using `epg-wait-for-completion' and call
1123 `epg-reset' to clear a temporaly output file.
1124 If you are unsure, use synchronous version of this function
1125 `epg-encrypt-file' or `epg-encrypt-string' instead."
1126 (epg-context-set-result context nil)
1128 (append (if always-trust '("--always-trust"))
1129 (if recipients '("--encrypt") '("--symmetric"))
1133 (mapcar (lambda (signer)
1135 (epg-context-signers context)))))
1137 (mapcar (lambda (recipient)
1138 (list "-r" recipient))
1140 (if (epg-data-file plain)
1141 (list (epg-data-file plain)))))
1143 (epg-wait-for-status context '("BEGIN_SIGNING")))
1144 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))
1145 (if (and (epg-data-string plain)
1146 (eq (process-status (epg-context-process context)) 'run))
1147 (process-send-string (epg-context-process context)
1148 (epg-data-string plain))))
1151 (defun epg-encrypt-file (context plain recipients
1152 cipher &optional sign always-trust)
1153 "Encrypt a file PLAIN and store the result to a file CIPHER.
1154 If CIPHER is nil, it returns the result as a string.
1155 If RECIPIENTS is nil, it performs symmetric encryption."
1159 (epg-context-set-output-file context cipher)
1160 (epg-context-set-output-file context
1161 (epg-make-temp-file "epg-output")))
1162 (epg-start-encrypt context (epg-make-data-from-file plain)
1163 recipients sign always-trust)
1164 (epg-wait-for-completion context)
1165 (if (epg-context-result-for context 'error)
1166 (error "Encrypt failed: %S"
1167 (epg-context-result-for context 'error)))
1169 (epg-read-output context)))
1171 (epg-delete-output-file context))
1172 (epg-reset context)))
1175 (defun epg-encrypt-string (context plain recipients
1176 &optional sign always-trust)
1177 "Encrypt a string PLAIN.
1178 If RECIPIENTS is nil, it performs symmetric encryption."
1181 (epg-context-set-output-file context
1182 (epg-make-temp-file "epg-output"))
1183 (epg-start-encrypt context (epg-make-data-from-string plain)
1184 recipients sign always-trust)
1186 (epg-wait-for-completion context)
1187 (if (epg-context-result-for context 'error)
1188 (error "Encrypt failed: %S"
1189 (epg-context-result-for context 'error)))
1190 (epg-read-output context))
1191 (epg-delete-output-file context)
1192 (epg-reset context)))
1195 (defun epg-start-export-keys (context keys)
1196 "Initiate an export keys operation.
1198 If you use this function, you will need to wait for the completion of
1199 `epg-gpg-program' by using `epg-wait-for-completion' and call
1200 `epg-reset' to clear a temporaly output file.
1201 If you are unsure, use synchronous version of this function
1202 `epg-export-keys-to-file' or `epg-export-keys-to-string' instead."
1203 (epg-context-set-result context nil)
1204 (epg-start context (cons "--export"
1208 (car (epg-key-sub-key-list key))))
1212 (defun epg-export-keys-to-file (context keys file)
1213 "Extract public KEYS."
1217 (epg-context-set-output-file context file)
1218 (epg-context-set-output-file context
1219 (epg-make-temp-file "epg-output")))
1220 (epg-start-export-keys context keys)
1221 (epg-wait-for-completion context)
1222 (if (epg-context-result-for context 'error)
1223 (error "Export keys failed"))
1225 (epg-read-output context)))
1227 (epg-delete-output-file context))
1228 (epg-reset context)))
1231 (defun epg-export-keys-to-string (context keys)
1232 "Extract public KEYS and return them as a string."
1233 (epg-export-keys-to-file context keys nil))
1236 (defun epg-start-import-keys (context keys)
1237 "Initiate an import keys operation.
1238 KEYS is a data object.
1240 If you use this function, you will need to wait for the completion of
1241 `epg-gpg-program' by using `epg-wait-for-completion' and call
1242 `epg-reset' to clear a temporaly output file.
1243 If you are unsure, use synchronous version of this function
1244 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1245 (epg-context-set-result context nil)
1246 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1247 (epg-start context (list "--import" (epg-data-file keys)))
1248 (if (and (epg-data-string keys)
1249 (eq (process-status (epg-context-process context)) 'run))
1250 (process-send-string (epg-context-process context)
1251 (epg-data-string keys))))
1253 (defun epg-import-keys-1 (context keys)
1256 (epg-start-import-keys context keys)
1257 (if (epg-data-file keys)
1258 (epg-flush context))
1259 (epg-wait-for-completion context)
1260 (if (epg-context-result-for context 'error)
1261 (error "Import keys failed"))
1262 (epg-read-output context))
1263 (epg-reset context)))
1266 (defun epg-import-keys-from-file (context keys)
1267 "Add keys from a file KEYS."
1268 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1271 (defun epg-import-keys-from-string (context keys)
1272 "Add keys from a string KEYS."
1273 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1276 (defun epg-start-delete-keys (context keys &optional allow-secret)
1277 "Initiate an delete keys operation.
1279 If you use this function, you will need to wait for the completion of
1280 `epg-gpg-program' by using `epg-wait-for-completion' and call
1281 `epg-reset' to clear a temporaly output file.
1282 If you are unsure, use synchronous version of this function
1283 `epg-delete-keys' instead."
1284 (epg-context-set-result context nil)
1285 (epg-start context (cons (if allow-secret
1286 "--delete-secret-key"
1291 (car (epg-key-sub-key-list key))))
1295 (defun epg-delete-keys (context keys &optional allow-secret)
1296 "Delete KEYS from the key ring."
1299 (epg-start-delete-keys context keys)
1300 (epg-wait-for-completion context)
1301 (if (epg-context-result-for context 'error)
1302 (error "Delete key failed")))
1303 (epg-reset context)))
1307 ;;; epg.el ends here