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 cipher-algorithm digest-algorithm
139 "Return a context object."
140 (vector protocol armor textmode include-certs
141 cipher-algorithm digest-algorithm compress-algorithm
142 #'epg-passphrase-callback-function
143 #'epg-progress-callback-function
146 (defun epg-context-protocol (context)
147 "Return the protocol used within CONTEXT."
150 (defun epg-context-armor (context)
151 "Return t if the output shouled be ASCII armored in CONTEXT."
154 (defun epg-context-textmode (context)
155 "Return t if canonical text mode should be used in CONTEXT."
158 (defun epg-context-include-certs (context)
159 "Return how many certificates should be included in an S/MIME signed
163 (defun epg-context-cipher-algorithm (context)
164 "Return the cipher algorithm in CONTEXT."
167 (defun epg-context-digest-algorithm (context)
168 "Return the digest algorithm in CONTEXT."
171 (defun epg-context-compress-algorithm (context)
172 "Return the compress algorithm in CONTEXT."
175 (defun epg-context-passphrase-callback (context)
176 "Return the function used to query passphrase."
179 (defun epg-context-progress-callback (context)
180 "Return the function which handles progress update."
183 (defun epg-context-signers (context)
184 "Return the list of key-id for singning."
187 (defun epg-context-process (context)
188 "Return the process object of `epg-gpg-program'.
189 This function is for internal use only."
192 (defun epg-context-output-file (context)
193 "Return the output file of `epg-gpg-program'.
194 This function is for internal use only."
197 (defun epg-context-result (context)
198 "Return the result of the previous cryptographic operation."
201 (defun epg-context-set-protocol (context protocol)
202 "Set the protocol used within CONTEXT."
203 (aset context 0 protocol))
205 (defun epg-context-set-armor (context armor)
206 "Specify if the output shouled be ASCII armored in CONTEXT."
207 (aset context 1 armor))
209 (defun epg-context-set-textmode (context textmode)
210 "Specify if canonical text mode should be used in CONTEXT."
211 (aset context 2 textmode))
213 (defun epg-context-set-include-certs (context include-certs)
214 "Set how many certificates should be included in an S/MIME signed message."
215 (aset context 3 include-certs))
217 (defun epg-context-set-cipher-algorithm (context cipher-algorithm)
218 "Set the cipher algorithm in CONTEXT."
219 (aset context 4 cipher-algorithm))
221 (defun epg-context-set-digest-algorithm (context digest-algorithm)
222 "Set the digest algorithm in CONTEXT."
223 (aset context 5 digest-algorithm))
225 (defun epg-context-set-compress-algorithm (context compress-algorithm)
226 "Set the compress algorithm in CONTEXT."
227 (aset context 6 compress-algorithm))
229 (defun epg-context-set-passphrase-callback (context
231 "Set the function used to query passphrase."
232 (aset context 7 passphrase-callback))
234 (defun epg-context-set-progress-callback (context progress-callback)
235 "Set the function which handles progress update."
236 (aset context 8 progress-callback))
238 (defun epg-context-set-signers (context signers)
239 "Set the list of key-id for singning."
240 (aset context 9 signers))
242 (defun epg-context-set-process (context process)
243 "Set the process object of `epg-gpg-program'.
244 This function is for internal use only."
245 (aset context 10 process))
247 (defun epg-context-set-output-file (context output-file)
248 "Set the output file of `epg-gpg-program'.
249 This function is for internal use only."
250 (aset context 11 output-file))
252 (defun epg-context-set-result (context result)
253 "Set the result of the previous cryptographic operation."
254 (aset context 12 result))
256 (defun epg-make-signature (status key-id user-id)
257 "Return a signature object."
258 (vector status key-id user-id nil nil))
260 (defun epg-signature-status (signature)
261 "Return the status code of SIGNATURE."
264 (defun epg-signature-key-id (signature)
265 "Return the key-id of SIGNATURE."
268 (defun epg-signature-user-id (signature)
269 "Return the user-id of SIGNATURE."
272 (defun epg-signature-validity (signature)
273 "Return the validity of SIGNATURE."
276 (defun epg-signature-fingerprint (signature)
277 "Return the fingerprint of SIGNATURE."
280 (defun epg-signature-set-status (signature status)
281 "Set the status code of SIGNATURE."
282 (aset signature 0 status))
284 (defun epg-signature-set-key-id (signature key-id)
285 "Set the key-id of SIGNATURE."
286 (aset signature 1 key-id))
288 (defun epg-signature-set-user-id (signature user-id)
289 "Set the user-id of SIGNATURE."
290 (aset signature 2 user-id))
292 (defun epg-signature-set-validity (signature validity)
293 "Set the validity of SIGNATURE."
294 (aset signature 3 validity))
296 (defun epg-signature-set-fingerprint (signature fingerprint)
297 "Set the fingerprint of SIGNATURE."
298 (aset signature 4 fingerprint))
300 (defun epg-make-key (owner-trust)
301 "Return a key object."
302 (vector owner-trust nil nil))
304 (defun epg-key-owner-trust (key)
305 "Return the owner trust of KEY."
308 (defun epg-key-sub-key-list (key)
309 "Return the sub key list of KEY."
312 (defun epg-key-user-id-list (key)
313 "Return the user ID list of KEY."
316 (defun epg-key-set-sub-key-list (key sub-key-list)
317 "Set the sub key list of KEY."
318 (aset key 1 sub-key-list))
320 (defun epg-key-set-user-id-list (key user-id-list)
321 "Set the user ID list of KEY."
322 (aset key 2 user-id-list))
324 (defun epg-make-sub-key (validity capability secret algorithm length id
325 creation-time expiration-time)
326 "Return a sub key object."
327 (vector validity capability secret algorithm length id creation-time
328 expiration-time nil))
330 (defun epg-sub-key-validity (sub-key)
331 "Return the validity of SUB-KEY."
334 (defun epg-sub-key-capability (sub-key)
335 "Return the capability of SUB-KEY."
338 (defun epg-sub-key-secret (sub-key)
339 "Return non-nil if SUB-KEY is a secret key."
342 (defun epg-sub-key-algorithm (sub-key)
343 "Return the algorithm of SUB-KEY."
346 (defun epg-sub-key-length (sub-key)
347 "Return the length of SUB-KEY."
350 (defun epg-sub-key-id (sub-key)
351 "Return the ID of SUB-KEY."
354 (defun epg-sub-key-creation-time (sub-key)
355 "Return the creation time of SUB-KEY."
358 (defun epg-sub-key-expiration-time (sub-key)
359 "Return the expiration time of SUB-KEY."
362 (defun epg-sub-key-fingerprint (sub-key)
363 "Return the fingerprint of SUB-KEY."
366 (defun epg-sub-key-set-fingerprint (sub-key fingerprint)
367 "Set the fingerprint of SUB-KEY.
368 This function is for internal use only."
369 (aset sub-key 8 fingerprint))
371 (defun epg-make-user-id (validity name)
372 "Return a user ID object."
373 (vector validity name nil))
375 (defun epg-user-id-validity (user-id)
376 "Return the validity of USER-ID."
379 (defun epg-user-id-name (user-id)
380 "Return the name of USER-ID."
383 (defun epg-user-id-signature-list (user-id)
384 "Return the signature list of USER-ID."
387 (defun epg-user-id-set-signature-list (user-id signature-list)
388 "Set the signature list of USER-ID."
389 (aset user-id 2 signature-list))
391 (defun epg-context-result-for (context name)
392 (cdr (assq name (epg-context-result context))))
394 (defun epg-context-set-result-for (context name value)
395 (let* ((result (epg-context-result context))
396 (entry (assq name result)))
399 (epg-context-set-result context (cons (cons name value) result)))))
401 (defun epg-start (context args)
402 "Start `epg-gpg-program' in a subprocess with given ARGS."
403 (let* ((args (append (list "--no-tty"
407 (if (epg-context-armor context) '("--armor"))
408 (if (epg-context-textmode context) '("--textmode"))
409 (if (epg-context-output-file context)
410 (list "--output" (epg-context-output-file context)))
412 (coding-system-for-write 'binary)
413 process-connection-type
414 (orig-mode (default-file-modes))
415 (buffer (generate-new-buffer " *epg*"))
417 (with-current-buffer buffer
418 (make-local-variable 'epg-read-point)
419 (setq epg-read-point (point-min))
420 (make-local-variable 'epg-pending-status-list)
421 (setq epg-pending-status-list nil)
422 (make-local-variable 'epg-key-id)
423 (setq epg-key-id nil)
424 (make-local-variable 'epg-context)
425 (setq epg-context context))
428 (set-default-file-modes 448)
430 (apply #'start-process "epg" buffer epg-gpg-program args)))
431 (set-default-file-modes orig-mode))
432 (set-process-filter process #'epg-process-filter)
433 (epg-context-set-process context process)))
435 (defun epg-process-filter (process input)
438 (set-buffer (get-buffer-create " *epg-debug*"))
439 (goto-char (point-max))
441 (if (buffer-live-p (process-buffer process))
443 (set-buffer (process-buffer process))
444 (goto-char (point-max))
446 (goto-char epg-read-point)
448 (while (looking-at ".*\n") ;the input line is finished
450 (if (looking-at "\\[GNUPG:] \\([A-Z_]+\\) ?\\(.*\\)")
451 (let* ((status (match-string 1))
452 (string (match-string 2))
453 (symbol (intern-soft (concat "epg-status-" status))))
454 (if (member status epg-pending-status-list)
455 (setq epg-pending-status-list nil))
458 (funcall symbol process string)))))
460 (setq epg-read-point (point)))))
462 (defun epg-read-output (context)
464 (if (fboundp 'set-buffer-multibyte)
465 (set-buffer-multibyte nil))
466 (if (file-exists-p (epg-context-output-file context))
467 (let ((coding-system-for-read (if (epg-context-textmode context)
470 (insert-file-contents (epg-context-output-file context))
473 (defun epg-wait-for-status (context status-list)
474 (with-current-buffer (process-buffer (epg-context-process context))
475 (setq epg-pending-status-list status-list)
476 (while (and (eq (process-status (epg-context-process context)) 'run)
477 epg-pending-status-list)
478 (accept-process-output (epg-context-process context) 1))))
480 (defun epg-wait-for-completion (context)
481 (if (eq (process-status (epg-context-process context)) 'run)
482 (process-send-eof (epg-context-process context)))
483 (while (eq (process-status (epg-context-process context)) 'run)
484 ;; We can't use accept-process-output instead of sit-for here
485 ;; because it may cause an interrupt during the sentinel execution.
488 (defun epg-reset (context)
489 (if (and (epg-context-process context)
490 (buffer-live-p (process-buffer (epg-context-process context))))
491 (kill-buffer (process-buffer (epg-context-process context))))
492 (epg-context-set-process context nil))
494 (defun epg-delete-output-file (context)
495 (if (and (epg-context-output-file context)
496 (file-exists-p (epg-context-output-file context)))
497 (delete-file (epg-context-output-file context))))
499 (defun epg-status-USERID_HINT (process string)
500 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
501 (let* ((key-id (match-string 1 string))
502 (user-id (match-string 2 string))
503 (entry (assoc key-id epg-user-id-alist)))
505 (setcdr entry user-id)
506 (setq epg-user-id-alist (cons (cons key-id user-id)
507 epg-user-id-alist))))))
509 (defun epg-status-NEED_PASSPHRASE (process string)
510 (if (string-match "\\`\\([^ ]+\\)" string)
511 (setq epg-key-id (match-string 1 string))))
513 (defun epg-status-NEED_PASSPHRASE_SYM (process string)
514 (setq epg-key-id 'SYM))
516 (defun epg-status-NEED_PASSPHRASE_PIN (process string)
517 (setq epg-key-id 'PIN))
519 (defun epg-status-GET_HIDDEN (process string)
521 (funcall (if (consp (epg-context-passphrase-callback epg-context))
522 (car (epg-context-passphrase-callback epg-context))
523 (epg-context-passphrase-callback epg-context))
525 (if (consp (epg-context-passphrase-callback epg-context))
526 (cdr (epg-context-passphrase-callback epg-context)))))
531 (setq string (concat passphrase "\n"))
532 (fillarray passphrase 0)
533 (setq passphrase nil)
534 (process-send-string process string))
536 (fillarray string 0))))))
538 (defun epg-status-GET_BOOL (process string)
539 (let ((entry (assoc string epg-prompt-alist)))
540 (if (y-or-n-p (if entry (cdr entry) (concat string "? ")))
541 (process-send-string process "y\n")
542 (process-send-string process "n\n"))))
544 (defun epg-status-GET_LINE (process string)
545 (let* ((entry (assoc string epg-prompt-alist))
546 (string (read-string (if entry (cdr entry) (concat string ": ")))))
547 (process-send-string process (concat string "\n"))))
549 (defun epg-status-GOODSIG (process string)
550 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
551 (epg-context-set-result-for
554 (cons (epg-make-signature 'good
555 (match-string 1 string)
556 (match-string 2 string))
557 (epg-context-result-for epg-context 'verify)))))
559 (defun epg-status-EXPSIG (process string)
560 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
561 (epg-context-set-result-for
564 (cons (epg-make-signature 'expired
565 (match-string 1 string)
566 (match-string 2 string))
567 (epg-context-result-for epg-context 'verify)))))
569 (defun epg-status-EXPKEYSIG (process string)
570 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
571 (epg-context-set-result-for
574 (cons (epg-make-signature 'expired-key
575 (match-string 1 string)
576 (match-string 2 string))
577 (epg-context-result-for epg-context 'verify)))))
579 (defun epg-status-REVKEYSIG (process string)
580 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
581 (epg-context-set-result-for
584 (cons (epg-make-signature 'revoked-key
585 (match-string 1 string)
586 (match-string 2 string))
587 (epg-context-result-for epg-context 'verify)))))
589 (defun epg-status-BADSIG (process string)
590 (if (string-match "\\`\\([^ ]+\\) \\(.*\\)" string)
591 (epg-context-set-result-for
594 (cons (epg-make-signature 'bad
595 (match-string 1 string)
596 (match-string 2 string))
597 (epg-context-result-for epg-context 'verify)))))
599 (defun epg-status-VALIDSIG (process string)
600 (let ((signature (car (epg-context-result-for epg-context 'verify))))
602 (eq (epg-signature-status signature) 'good)
603 (string-match "\\`\\([^ ]+\\) " string))
604 (epg-signature-set-fingerprint signature (match-string 1 string)))))
606 (defun epg-status-TRUST_UNDEFINED (process string)
607 (let ((signature (car (epg-context-result-for epg-context 'verify))))
609 (eq (epg-signature-status signature) 'good))
610 (epg-signature-set-validity signature 'undefined))))
612 (defun epg-status-TRUST_NEVER (process string)
613 (let ((signature (car (epg-context-result-for epg-context 'verify))))
615 (eq (epg-signature-status signature) 'good))
616 (epg-signature-set-validity signature 'never))))
618 (defun epg-status-TRUST_MARGINAL (process string)
619 (let ((signature (car (epg-context-result-for epg-context 'verify))))
621 (eq (epg-signature-status signature) 'marginal))
622 (epg-signature-set-validity signature 'marginal))))
624 (defun epg-status-TRUST_FULLY (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 'full))))
630 (defun epg-status-TRUST_ULTIMATE (process string)
631 (let ((signature (car (epg-context-result-for epg-context 'verify))))
633 (eq (epg-signature-status signature) 'good))
634 (epg-signature-set-validity signature 'ultimate))))
636 (defun epg-status-PROGRESS (process string)
637 (if (string-match "\\`\\([^ ]+\\) \\([^ ]\\) \\([0-9]+\\) \\([0-9]+\\)"
639 (funcall (if (consp (epg-context-progress-callback epg-context))
640 (car (epg-context-progress-callback epg-context))
641 (epg-context-progress-callback epg-context))
642 (match-string 1 string)
643 (match-string 2 string)
644 (string-to-number (match-string 3 string))
645 (string-to-number (match-string 4 string))
646 (if (consp (epg-context-progress-callback epg-context))
647 (cdr (epg-context-progress-callback epg-context))))))
649 (defun epg-status-DECRYPTION_FAILED (process string)
650 (epg-context-set-result-for
652 (cons 'decryption-failed
653 (epg-context-result-for epg-context 'error))))
655 (defun epg-status-NODATA (process string)
656 (epg-context-set-result-for
658 (cons (cons 'no-data (string-to-number string))
659 (epg-context-result-for epg-context 'error))))
661 (defun epg-status-UNEXPECTED (process string)
662 (epg-context-set-result-for
664 (cons (cons 'unexpected (string-to-number string))
665 (epg-context-result-for epg-context 'error))))
667 (defun epg-status-KEYEXPIRED (process string)
668 (epg-context-set-result-for
670 (cons (cons 'key-expired string)
671 (epg-context-result-for epg-context 'error))))
673 (defun epg-status-KEYREVOKED (process string)
674 (epg-context-set-result-for
677 (epg-context-result-for epg-context 'error))))
679 (defun epg-status-BADARMOR (process string)
680 (epg-context-set-result-for
683 (epg-context-result-for epg-context 'error))))
685 (defun epg-status-INV_RECP (process string)
686 (if (string-match "\\`\\([0-9]+\\) \\(.*\\)" string)
687 (epg-context-set-result-for
689 (cons (list 'invalid-recipient
690 (string-to-number (match-string 1 string))
691 (match-string 2 string))
692 (epg-context-result-for epg-context 'error)))))
694 (defun epg-status-NO_RECP (process string)
695 (epg-context-set-result-for
698 (epg-context-result-for epg-context 'error))))
700 (defun epg-passphrase-callback-function (key-id handback)
703 "Passphrase for symmetric encryption: "
705 "Passphrase for PIN: "
706 (let ((entry (assoc key-id epg-user-id-alist)))
708 (format "Passphrase for %s %s: " key-id (cdr entry))
709 (format "Passphrase for %s: " key-id)))))))
711 (defun epg-progress-callback-function (what char current total handback)
712 (message "%s: %d%%/%d%%" what current total))
714 (defun epg-configuration ()
715 "Return a list of internal configuration parameters of `epg-gpg-program'."
718 (apply #'call-process epg-gpg-program nil (list t nil) nil
719 '("--with-colons" "--list-config"))
720 (goto-char (point-min))
721 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
722 (setq type (intern (match-string 1))
723 config (cons (cons type
725 '(pubkey cipher digest compress))
726 (mapcar #'string-to-number
727 (delete "" (split-string
734 (defun epg-list-keys-1 (name mode)
735 (let ((args (append (list "--with-colons" "--no-greeting" "--batch"
736 "--fixed-list-mode" "--with-fingerprint"
738 (if mode "--list-secret-keys" "--list-keys"))
739 (if name (list name))))
740 keys string field index)
742 (apply #'call-process epg-gpg-program nil (list t nil) nil args)
743 (goto-char (point-min))
744 (while (re-search-forward "^[a-z][a-z][a-z]:.*" nil t)
745 (setq keys (cons (make-vector 15 nil) keys)
746 string (match-string 0)
750 (string-match "\\([^:]+\\)?:" string index))
751 (setq index (match-end 0))
752 (aset (car keys) field (match-string 1 string))
753 (setq field (1+ field))))
756 (defun epg-make-sub-key-1 (line)
758 (cdr (assq (string-to-char (aref line 1)) epg-key-validity-alist))
760 (mapcar (lambda (char) (cdr (assq char epg-key-capablity-alist)))
762 (member (aref line 0) '("sec" "ssb"))
763 (string-to-number (aref line 3))
764 (string-to-number (aref line 2))
769 (defun epg-list-keys (&optional name mode)
770 (let ((lines (epg-list-keys-1 name mode))
774 ((member (aref (car lines) 0) '("pub" "sec"))
776 (epg-key-set-sub-key-list
778 (nreverse (epg-key-sub-key-list (car keys))))
779 (epg-key-set-user-id-list
781 (nreverse (epg-key-user-id-list (car keys)))))
782 (setq keys (cons (epg-make-key
783 (cdr (assq (string-to-char (aref (car lines) 8))
784 epg-key-validity-alist)))
786 (epg-key-set-sub-key-list
788 (cons (epg-make-sub-key-1 (car lines))
789 (epg-key-sub-key-list (car keys)))))
790 ((member (aref (car lines) 0) '("sub" "ssb"))
791 (epg-key-set-sub-key-list
793 (cons (epg-make-sub-key-1 (car lines))
794 (epg-key-sub-key-list (car keys)))))
795 ((equal (aref (car lines) 0) "uid")
796 (epg-key-set-user-id-list
798 (cons (epg-make-user-id
799 (cdr (assq (string-to-char (aref (car lines) 1))
800 epg-key-validity-alist))
801 (aref (car lines) 9))
802 (epg-key-user-id-list (car keys)))))
803 ((equal (aref (car lines) 0) "fpr")
804 (epg-sub-key-set-fingerprint (car (epg-key-sub-key-list (car keys)))
805 (aref (car lines) 9))))
806 (setq lines (cdr lines)))
809 (if (fboundp 'make-temp-file)
810 (defalias 'epg-make-temp-file 'make-temp-file)
811 ;; stolen from poe.el.
812 (defun epg-make-temp-file (prefix)
813 "Create a temporary file.
814 The returned file name (created by appending some random characters at the end
815 of PREFIX, and expanding against `temporary-file-directory' if necessary),
816 is guaranteed to point to a newly created empty file.
817 You can then use `write-region' to write new data into the file."
818 (let (tempdir tempfile)
821 ;; First, create a temporary directory.
822 (while (condition-case ()
824 (setq tempdir (make-temp-name
826 (file-name-directory prefix)
828 ;; return nil or signal an error.
829 (make-directory tempdir))
831 (file-already-exists t)))
832 (set-file-modes tempdir 448)
833 ;; Second, create a temporary file in the tempdir.
834 ;; There *is* a race condition between `make-temp-name'
835 ;; and `write-region', but we don't care it since we are
836 ;; in a private directory now.
837 (setq tempfile (make-temp-name (concat tempdir "/EMU")))
838 (write-region "" nil tempfile nil 'silent)
839 (set-file-modes tempfile 384)
840 ;; Finally, make a hard-link from the tempfile.
841 (while (condition-case ()
843 (setq file (make-temp-name prefix))
844 ;; return nil or signal an error.
845 (add-name-to-file tempfile file))
847 (file-already-exists t)))
849 ;; Cleanup the tempfile.
851 (file-exists-p tempfile)
852 (delete-file tempfile))
853 ;; Cleanup the tempdir.
855 (file-directory-p tempdir)
856 (delete-directory tempdir))))))
859 (defun epg-start-decrypt (context cipher)
860 "Initiate a decrypt operation on CIPHER.
861 CIPHER is a data object.
863 If you use this function, you will need to wait for the completion of
864 `epg-gpg-program' by using `epg-wait-for-completion' and call
865 `epg-reset' to clear a temporaly output file.
866 If you are unsure, use synchronous version of this function
867 `epg-decrypt-file' or `epg-decrypt-string' instead."
868 (unless (epg-data-file cipher)
869 (error "Not a file"))
870 (epg-context-set-result context nil)
871 (epg-start context (list "--decrypt" (epg-data-file cipher)))
872 (epg-wait-for-status context '("BEGIN_DECRYPTION")))
875 (defun epg-decrypt-file (context cipher plain)
876 "Decrypt a file CIPHER and store the result to a file PLAIN.
877 If PLAIN is nil, it returns the result as a string."
881 (epg-context-set-output-file context plain)
882 (epg-context-set-output-file context
883 (epg-make-temp-file "epg-output")))
884 (epg-start-decrypt context (epg-make-data-from-file cipher))
885 (epg-wait-for-completion context)
886 (if (epg-context-result-for context 'error)
887 (error "Decrypt failed: %S"
888 (epg-context-result-for context 'error)))
890 (epg-read-output context)))
892 (epg-delete-output-file context))
893 (epg-reset context)))
896 (defun epg-decrypt-string (context cipher)
897 "Decrypt a string CIPHER and return the plain text."
898 (let ((input-file (epg-make-temp-file "epg-input"))
899 (coding-system-for-write 'binary))
902 (write-region cipher nil input-file)
903 (epg-context-set-output-file context
904 (epg-make-temp-file "epg-output"))
905 (epg-start-decrypt context (epg-make-data-from-file input-file))
906 (epg-wait-for-completion context)
907 (if (epg-context-result-for context 'error)
908 (error "Decrypt failed: %S"
909 (epg-context-result-for context 'error)))
910 (epg-read-output context))
911 (epg-delete-output-file context)
912 (if (file-exists-p input-file)
913 (delete-file input-file))
914 (epg-reset context))))
917 (defun epg-start-verify (context signature &optional signed-text)
918 "Initiate a verify operation on SIGNATURE.
919 SIGNATURE and SIGNED-TEXT are a data object if they are specified.
921 For a detached signature, both SIGNATURE and SIGNED-TEXT should be set.
922 For a normal or a clear text signature, SIGNED-TEXT should be nil.
924 If you use this function, you will need to wait for the completion of
925 `epg-gpg-program' by using `epg-wait-for-completion' and call
926 `epg-reset' to clear a temporaly output file.
927 If you are unsure, use synchronous version of this function
928 `epg-verify-file' or `epg-verify-string' instead."
929 (epg-context-set-result context nil)
931 ;; Detached signature.
932 (if (epg-data-file signed-text)
933 (epg-start context (list "--verify" (epg-data-file signature)
934 (epg-data-file signed-text)))
935 (epg-start context (list "--verify" (epg-data-file signature) "-"))
936 (if (eq (process-status (epg-context-process context)) 'run)
937 (process-send-string (epg-context-process context)
938 (epg-data-string signed-text))))
939 ;; Normal (or cleartext) signature.
940 (if (epg-data-file signature)
941 (epg-start context (list "--verify" (epg-data-file signature)))
942 (epg-start context (list "--verify"))
943 (if (eq (process-status (epg-context-process context)) 'run)
944 (process-send-string (epg-context-process context)
945 (epg-data-string signature))))))
948 (defun epg-verify-file (context signature &optional signed-text plain)
949 "Verify a file SIGNATURE.
950 SIGNED-TEXT and PLAIN are also a file if they are specified.
952 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
953 For a normal or a clear text signature, SIGNED-TEXT should be nil."
957 (epg-context-set-output-file context plain)
958 (epg-context-set-output-file context
959 (epg-make-temp-file "epg-output")))
961 (epg-start-verify context
962 (epg-make-data-from-file signature)
963 (epg-make-data-from-file signed-text))
964 (epg-start-verify context
965 (epg-make-data-from-file signature)))
966 (epg-wait-for-completion context)
968 (epg-read-output context)))
970 (epg-delete-output-file context))
971 (epg-reset context)))
974 (defun epg-verify-string (context signature &optional signed-text)
975 "Verify a string SIGNATURE.
976 SIGNED-TEXT is a string if it is specified.
978 For a detached signature, both SIGNATURE and SIGNED-TEXT should be string.
979 For a normal or a clear text signature, SIGNED-TEXT should be nil."
980 (let ((coding-system-for-write 'binary)
984 (epg-context-set-output-file context
985 (epg-make-temp-file "epg-output"))
988 (setq input-file (epg-make-temp-file "epg-signature"))
989 (write-region signature nil input-file)
990 (epg-start-verify context
991 (epg-make-data-from-file input-file)
992 (epg-make-data-from-string signed-text)))
993 (epg-start-verify context (epg-make-data-from-string signature)))
994 (epg-wait-for-completion context)
995 (epg-read-output context))
996 (epg-delete-output-file context)
998 (file-exists-p input-file))
999 (delete-file input-file))
1000 (epg-reset context))))
1003 (defun epg-start-sign (context plain &optional mode)
1004 "Initiate a sign operation on PLAIN.
1005 PLAIN is a data object.
1007 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1008 If MODE is t or 'detached, it makes a detached signature.
1009 Otherwise, it makes a normal signature.
1011 If you use this function, you will need to wait for the completion of
1012 `epg-gpg-program' by using `epg-wait-for-completion' and call
1013 `epg-reset' to clear a temporaly output file.
1014 If you are unsure, use synchronous version of this function
1015 `epg-sign-file' or `epg-sign-string' instead."
1016 (epg-context-set-result context nil)
1018 (append (list (if (eq mode 'clearsign)
1020 (if (or (eq mode t) (eq mode 'detached))
1024 (mapcar (lambda (signer)
1026 (epg-context-signers context)))
1027 (if (epg-data-file plain)
1028 (list (epg-data-file plain)))))
1029 (epg-wait-for-status context '("BEGIN_SIGNING"))
1030 (if (and (epg-data-string plain)
1031 (eq (process-status (epg-context-process context)) 'run))
1032 (process-send-string (epg-context-process context)
1033 (epg-data-string plain))))
1036 (defun epg-sign-file (context plain signature &optional mode)
1037 "Sign a file PLAIN and store the result to a file SIGNATURE.
1038 If SIGNATURE is nil, it returns the result as a string.
1039 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1040 If MODE is t or 'detached, it makes a detached signature.
1041 Otherwise, it makes a normal signature."
1045 (epg-context-set-output-file context signature)
1046 (epg-context-set-output-file context
1047 (epg-make-temp-file "epg-output")))
1048 (epg-start-sign context (epg-make-data-from-file plain) mode)
1049 (epg-wait-for-completion context)
1050 (if (epg-context-result-for context 'error)
1051 (error "Sign failed: %S"
1052 (epg-context-result-for context 'error)))
1054 (epg-read-output context)))
1056 (epg-delete-output-file context))
1057 (epg-reset context)))
1060 (defun epg-sign-string (context plain &optional mode)
1061 "Sign a string PLAIN and return the output as string.
1062 If optional 3rd argument MODE is 'clearsign, it makes a clear text signature.
1063 If MODE is t or 'detached, it makes a detached signature.
1064 Otherwise, it makes a normal signature."
1067 (epg-context-set-output-file context
1068 (epg-make-temp-file "epg-output"))
1069 (epg-start-sign context (epg-make-data-from-string plain) mode)
1070 (epg-wait-for-completion context)
1071 (if (epg-context-result-for context 'error)
1072 (error "Sign failed: %S"
1073 (epg-context-result-for context 'error)))
1074 (epg-read-output context))
1075 (epg-delete-output-file context)
1076 (epg-reset context)))
1079 (defun epg-start-encrypt (context plain recipients
1080 &optional sign always-trust)
1081 "Initiate an encrypt operation on PLAIN.
1082 PLAIN is a data object.
1083 If RECIPIENTS is nil, it performs symmetric encryption.
1085 If you use this function, you will need to wait for the completion of
1086 `epg-gpg-program' by using `epg-wait-for-completion' and call
1087 `epg-reset' to clear a temporaly output file.
1088 If you are unsure, use synchronous version of this function
1089 `epg-encrypt-file' or `epg-encrypt-string' instead."
1090 (epg-context-set-result context nil)
1092 (append (if always-trust '("--always-trust"))
1093 (if recipients '("--encrypt") '("--symmetric"))
1097 (mapcar (lambda (signer)
1099 (epg-context-signers context)))))
1101 (mapcar (lambda (recipient)
1102 (list "-r" recipient))
1104 (if (epg-data-file plain)
1105 (list (epg-data-file plain)))))
1107 (epg-wait-for-status context '("BEGIN_SIGNING"))
1108 (if (null recipients)
1109 (epg-wait-for-status context '("BEGIN_ENCRYPTION"))))
1110 (if (and (epg-data-string plain)
1111 (eq (process-status (epg-context-process context)) 'run))
1112 (process-send-string (epg-context-process context)
1113 (epg-data-string plain))))
1116 (defun epg-encrypt-file (context plain recipients
1117 cipher &optional sign always-trust)
1118 "Encrypt a file PLAIN and store the result to a file CIPHER.
1119 If CIPHER is nil, it returns the result as a string.
1120 If RECIPIENTS is nil, it performs symmetric encryption."
1124 (epg-context-set-output-file context cipher)
1125 (epg-context-set-output-file context
1126 (epg-make-temp-file "epg-output")))
1127 (epg-start-encrypt context (epg-make-data-from-file plain)
1128 recipients sign always-trust)
1129 (epg-wait-for-completion context)
1130 (if (epg-context-result-for context 'error)
1131 (error "Encrypt failed: %S"
1132 (epg-context-result-for context 'error)))
1134 (epg-read-output context)))
1136 (epg-delete-output-file context))
1137 (epg-reset context)))
1140 (defun epg-encrypt-string (context plain recipients
1141 &optional sign always-trust)
1142 "Encrypt a string PLAIN.
1143 If RECIPIENTS is nil, it performs symmetric encryption."
1146 (epg-context-set-output-file context
1147 (epg-make-temp-file "epg-output"))
1148 (epg-start-encrypt context (epg-make-data-from-string plain)
1149 recipients sign always-trust)
1150 (epg-wait-for-completion context)
1151 (if (epg-context-result-for context 'error)
1152 (error "Encrypt failed: %S"
1153 (epg-context-result-for context 'error)))
1154 (epg-read-output context))
1155 (epg-delete-output-file context)
1156 (epg-reset context)))
1159 (defun epg-start-export-keys (context pattern)
1160 "Initiate an export keys operation.
1162 If you use this function, you will need to wait for the completion of
1163 `epg-gpg-program' by using `epg-wait-for-completion' and call
1164 `epg-reset' to clear a temporaly output file.
1165 If you are unsure, use synchronous version of this function
1166 `epg-export-keys' instead."
1167 (epg-context-set-result context nil)
1168 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1169 (epg-start context (list "--export" pattern)))
1172 (defun epg-export-keys (context pattern)
1173 "Extract public keys matched with PATTERN and return them."
1176 (epg-start-export-keys context pattern)
1177 (epg-wait-for-completion context)
1178 (if (epg-context-result-for context 'error)
1179 (error "Export keys failed"))
1180 (epg-read-output context))
1181 (epg-reset context)))
1184 (defun epg-start-import-keys (context keys)
1185 "Initiate an import keys operation.
1186 KEYS is a data object.
1188 If you use this function, you will need to wait for the completion of
1189 `epg-gpg-program' by using `epg-wait-for-completion' and call
1190 `epg-reset' to clear a temporaly output file.
1191 If you are unsure, use synchronous version of this function
1192 `epg-import-keys-from-file' or `epg-import-keys-from-string' instead."
1193 (epg-context-set-result context nil)
1194 (epg-context-set-output-file context (epg-make-temp-file "epg-output"))
1195 (epg-start context (append (list "--import") (epg-data-file keys)))
1196 (if (and (epg-data-string keys)
1197 (eq (process-status (epg-context-process context)) 'run))
1198 (process-send-string (epg-context-process context)
1199 (epg-data-string keys))))
1201 (defun epg-import-keys-1 (context keys)
1204 (epg-start-import-keys context keys)
1205 (epg-wait-for-completion context)
1206 (if (epg-context-result-for context 'error)
1207 (error "Import keys failed"))
1208 (epg-read-output context))
1209 (epg-reset context)))
1212 (defun epg-import-keys-from-file (context keys)
1213 "Add keys from a file KEYS."
1214 (epg-import-keys-1 context (epg-make-data-from-file keys)))
1217 (defun epg-import-keys-from-string (context keys)
1218 "Add keys from a string KEYS."
1219 (epg-import-keys-1 context (epg-make-data-from-string keys)))
1223 ;;; epg.el ends here