1 ;;; epa.el --- the EasyPG Assistant
2 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
29 (eval-when-compile (require 'wid-edit
))
33 "The EasyPG Assistant"
37 (defcustom epa-popup-info-window t
38 "If non-nil, status information from epa commands is displayed on
43 (defcustom epa-info-window-height
5
44 "Number of lines used to display status information."
48 (defgroup epa-faces nil
53 (defface epa-validity-high
54 `((((class color
) (background dark
))
55 (:foreground
"PaleTurquoise"
56 ,@(if (assq ':weight custom-face-attributes
)
60 (,@(if (assq ':weight custom-face-attributes
)
63 "Face used for displaying the high validity."
66 (defface epa-validity-medium
67 `((((class color
) (background dark
))
68 (:foreground
"PaleTurquoise"
69 ,@(if (assq ':slant custom-face-attributes
)
73 (,@(if (assq ':slant custom-face-attributes
)
76 "Face used for displaying the medium validity."
79 (defface epa-validity-low
81 (,@(if (assq ':slant custom-face-attributes
)
84 "Face used for displaying the low validity."
87 (defface epa-validity-disabled
89 (,@(if (assq ':slant custom-face-attributes
)
93 "Face used for displaying the disabled validity."
97 '((((class color
) (background dark
))
98 (:foreground
"lightyellow"))
99 (((class color
) (background light
))
100 (:foreground
"blue4")))
101 "Face used for displaying the string."
105 `((((class color
) (background dark
))
106 (:foreground
"orange"
107 ,@(if (assq ':weight custom-face-attributes
)
110 (((class color
) (background light
))
112 ,@(if (assq ':weight custom-face-attributes
)
116 (,@(if (assq ':weight custom-face-attributes
)
119 "Face used for displaying the high validity."
122 (defface epa-field-name
123 `((((class color
) (background dark
))
124 (:foreground
"PaleTurquoise"
125 ,@(if (assq ':weight custom-face-attributes
)
129 (,@(if (assq ':weight custom-face-attributes
)
132 "Face for the name of the attribute field."
135 (defface epa-field-body
136 `((((class color
) (background dark
))
137 (:foreground
"turquoise"
138 ,@(if (assq ':slant custom-face-attributes
)
142 (,@(if (assq ':slant custom-face-attributes
)
145 "Face for the body of the attribute field."
148 (defcustom epa-validity-face-alist
149 '((unknown . epa-validity-disabled
)
150 (invalid . epa-validity-disabled
)
151 (disabled . epa-validity-disabled
)
152 (revoked . epa-validity-disabled
)
153 (expired . epa-validity-disabled
)
154 (none . epa-validity-low
)
155 (undefined . epa-validity-low
)
156 (never . epa-validity-low
)
157 (marginal . epa-validity-medium
)
158 (full . epa-validity-high
)
159 (ultimate . epa-validity-high
))
160 "An alist mapping validity values to faces."
161 :type
'(repeat (cons symbol face
))
164 (defvar epa-font-lock-keywords
167 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
169 (2 'epa-field-body
)))
170 "Default expressions to addon in epa-mode.")
172 (defconst epa-pubkey-algorithm-letter-alist
180 (defvar epa-protocol
'OpenPGP
181 "*The default protocol.
182 The value can be either OpenPGP or CMS.
184 You should bind this variable with `let', but do not set it globally.")
186 (defvar epa-armor nil
187 "*If non-nil, epa commands create ASCII armored output.
189 You should bind this variable with `let', but do not set it globally.")
191 (defvar epa-textmode nil
192 "*If non-nil, epa commands treat input files as text.
194 You should bind this variable with `let', but do not set it globally.")
196 (defvar epa-keys-buffer nil
)
197 (defvar epa-key-buffer-alist nil
)
199 (defvar epa-list-keys-arguments nil
)
200 (defvar epa-info-buffer nil
)
201 (defvar epa-last-coding-system-specified nil
)
203 (defvar epa-key-list-mode-map
204 (let ((keymap (make-sparse-keymap))
205 (menu-map (make-sparse-keymap)))
206 (define-key keymap
"m" 'epa-mark-key
)
207 (define-key keymap
"u" 'epa-unmark-key
)
208 (define-key keymap
"d" 'epa-decrypt-file
)
209 (define-key keymap
"v" 'epa-verify-file
)
210 (define-key keymap
"s" 'epa-sign-file
)
211 (define-key keymap
"e" 'epa-encrypt-file
)
212 (define-key keymap
"r" 'epa-delete-keys
)
213 (define-key keymap
"i" 'epa-import-keys
)
214 (define-key keymap
"o" 'epa-export-keys
)
215 (define-key keymap
"g" 'revert-buffer
)
216 (define-key keymap
"n" 'next-line
)
217 (define-key keymap
"p" 'previous-line
)
218 (define-key keymap
" " 'scroll-up
)
219 (define-key keymap
[delete] 'scroll-down)
220 (define-key keymap "q" 'epa-exit-buffer)
221 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
222 (define-key menu-map [epa-key-list-unmark-key]
223 '(menu-item "Unmark Key" epa-unmark-key
224 :help "Unmark a key"))
225 (define-key menu-map [epa-key-list-mark-key]
226 '(menu-item "Mark Key" epa-mark-key
228 (define-key menu-map [separator-epa-file] '(menu-item "--"))
229 (define-key menu-map [epa-verify-file]
230 '(menu-item "Verify File..." epa-verify-file
231 :help "Verify FILE"))
232 (define-key menu-map [epa-sign-file]
233 '(menu-item "Sign File..." epa-sign-file
234 :help "Sign FILE by SIGNERS keys selected"))
235 (define-key menu-map [epa-decrypt-file]
236 '(menu-item "Decrypt File..." epa-decrypt-file
237 :help "Decrypt FILE"))
238 (define-key menu-map [epa-encrypt-file]
239 '(menu-item "Encrypt File.." epa-encrypt-file
240 :help "Encrypt FILE for RECIPIENTS"))
241 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
242 (define-key menu-map [epa-key-list-delete-keys]
243 '(menu-item "Delete keys" epa-delete-keys
244 :help "Delete Marked Keys"))
245 (define-key menu-map [epa-key-list-import-keys]
246 '(menu-item "Import Keys" epa-import-keys
247 :help "Import keys from a file"))
248 (define-key menu-map [epa-key-list-export-keys]
249 '(menu-item "Export Keys" epa-export-keys
250 :help "Export marked keys to a file"))
253 (defvar epa-key-mode-map
254 (let ((keymap (make-sparse-keymap)))
255 (define-key keymap "q" 'epa-exit-buffer)
258 (defvar epa-info-mode-map
259 (let ((keymap (make-sparse-keymap)))
260 (define-key keymap "q" 'delete-window)
263 (defvar epa-exit-buffer-function #'bury-buffer)
265 (define-widget 'epa-key 'push-button
266 "Button for representing a epg-key object."
268 :button-face-get 'epa--key-widget-button-face-get
269 :value-create 'epa--key-widget-value-create
270 :action 'epa--key-widget-action
271 :help-echo 'epa--key-widget-help-echo)
273 (defun epa--key-widget-action (widget &optional event)
274 (epa--show-key (widget-get widget :value)))
276 (defun epa--key-widget-value-create (widget)
277 (let* ((key (widget-get widget :value))
278 (primary-sub-key (car (epg-key-sub-key-list key)))
279 (primary-user-id (car (epg-key-user-id-list key))))
280 (insert (format "%c "
281 (if (epg-sub-key-validity primary-sub-key)
282 (car (rassq (epg-sub-key-validity primary-sub-key)
283 epg-key-validity-alist))
285 (epg-sub-key-id primary-sub-key)
288 (if (stringp (epg-user-id-string primary-user-id))
289 (epg-user-id-string primary-user-id)
290 (epg-decode-dn (epg-user-id-string primary-user-id)))
293 (defun epa--key-widget-button-face-get (widget)
294 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
295 (widget-get widget :value))))))
297 (cdr (assq validity epa-validity-face-alist))
300 (defun epa--key-widget-help-echo (widget)
302 (epg-sub-key-id (car (epg-key-sub-key-list
303 (widget-get widget :value))))))
306 (if (fboundp 'encode-coding-string)
307 (defalias 'epa--encode-coding-string 'encode-coding-string)
308 (defalias 'epa--encode-coding-string 'identity)))
311 (if (fboundp 'decode-coding-string)
312 (defalias 'epa--decode-coding-string 'decode-coding-string)
313 (defalias 'epa--decode-coding-string 'identity)))
315 (defun epa-key-list-mode ()
316 "Major mode for `epa-list-keys'."
317 (kill-all-local-variables)
318 (buffer-disable-undo)
319 (setq major-mode 'epa-key-list-mode
323 (use-local-map epa-key-list-mode-map)
324 (make-local-variable 'font-lock-defaults)
325 (setq font-lock-defaults '(epa-font-lock-keywords t))
326 ;; In XEmacs, auto-initialization of font-lock is not effective
327 ;; if buffer-file-name is not set.
328 (font-lock-set-defaults)
329 (make-local-variable 'epa-exit-buffer-function)
330 (make-local-variable 'revert-buffer-function)
331 (setq revert-buffer-function 'epa--key-list-revert-buffer)
332 (run-hooks 'epa-key-list-mode-hook))
334 (defun epa-key-mode ()
335 "Major mode for a key description."
336 (kill-all-local-variables)
337 (buffer-disable-undo)
338 (setq major-mode 'epa-key-mode
342 (use-local-map epa-key-mode-map)
343 (make-local-variable 'font-lock-defaults)
344 (setq font-lock-defaults '(epa-font-lock-keywords t))
345 ;; In XEmacs, auto-initialization of font-lock is not effective
346 ;; if buffer-file-name is not set.
347 (font-lock-set-defaults)
348 (make-local-variable 'epa-exit-buffer-function)
349 (run-hooks 'epa-key-mode-hook))
351 (defun epa-info-mode ()
352 "Major mode for `epa-info-buffer'."
353 (kill-all-local-variables)
354 (buffer-disable-undo)
355 (setq major-mode 'epa-info-mode
359 (use-local-map epa-info-mode-map)
360 (run-hooks 'epa-info-mode-hook))
362 (defun epa-mark-key (&optional arg)
363 "Mark a key on the current line.
364 If ARG is non-nil, unmark the key."
366 (let ((inhibit-read-only t)
370 (unless (get-text-property (point) 'epa-key)
371 (error "No key on this line"))
372 (setq properties (text-properties-at (point)))
374 (insert (if arg " " "*"))
375 (set-text-properties (1- (point)) (point) properties)
378 (defun epa-unmark-key (&optional arg)
379 "Unmark a key on the current line.
380 If ARG is non-nil, mark the key."
382 (epa-mark-key (not arg)))
384 (defun epa-exit-buffer ()
385 "Exit the current buffer.
386 `epa-exit-buffer-function' is called if it is set."
388 (funcall epa-exit-buffer-function))
390 (defun epa--insert-keys (keys)
393 (narrow-to-region (point) (point))
398 (add-text-properties point (point)
399 (list 'epa-key (car keys)
404 (widget-create 'epa-key :value (car keys))
406 (setq keys (cdr keys))))
407 (add-text-properties (point-min) (point-max)
408 (list 'epa-list-keys t
414 (defun epa--list-keys (name secret)
415 (unless (and epa-keys-buffer
416 (buffer-live-p epa-keys-buffer))
417 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
418 (set-buffer epa-keys-buffer)
420 (let ((inhibit-read-only t)
423 (context (epg-make-context epa-protocol)))
424 (unless (get-text-property point 'epa-list-keys)
425 (setq point (next-single-property-change point 'epa-list-keys)))
428 (or (next-single-property-change point 'epa-list-keys)
431 (epa--insert-keys (epg-list-keys context name secret))
433 (set-keymap-parent (current-local-map) widget-keymap))
434 (make-local-variable 'epa-list-keys-arguments)
435 (setq epa-list-keys-arguments (list name secret))
436 (goto-char (point-min))
437 (pop-to-buffer (current-buffer)))
440 (defun epa-list-keys (&optional name)
441 "List all keys matched with NAME from the public keyring."
443 (if current-prefix-arg
444 (let ((name (read-string "Pattern: "
445 (if epa-list-keys-arguments
446 (car epa-list-keys-arguments)))))
447 (list (if (equal name "") nil name)))
449 (epa--list-keys name nil))
452 (defun epa-list-secret-keys (&optional name)
453 "List all keys matched with NAME from the private keyring."
455 (if current-prefix-arg
456 (let ((name (read-string "Pattern: "
457 (if epa-list-keys-arguments
458 (car epa-list-keys-arguments)))))
459 (list (if (equal name "") nil name)))
461 (epa--list-keys name t))
463 (defun epa--key-list-revert-buffer (&optional ignore-auto noconfirm)
464 (apply #'epa--list-keys epa-list-keys-arguments))
466 (defun epa--marked-keys ()
468 (set-buffer epa-keys-buffer)
469 (goto-char (point-min))
471 (while (re-search-forward "^\\*" nil t)
472 (if (setq key (get-text-property (match-beginning 0)
474 (setq keys (cons key keys))))
478 (let ((key (get-text-property (point) 'epa-key)))
482 (defun epa--select-keys (prompt keys)
484 (unless (and epa-keys-buffer
485 (buffer-live-p epa-keys-buffer))
486 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
487 (set-buffer epa-keys-buffer)
489 (let ((inhibit-read-only t)
493 (substitute-command-keys "\
494 - `\\[epa-mark-key]' to mark a key on the line
495 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
497 :notify (lambda (&rest ignore) (abort-recursive-edit))
499 (substitute-command-keys
500 "Click here or \\[abort-recursive-edit] to cancel")
503 :notify (lambda (&rest ignore) (exit-recursive-edit))
505 (substitute-command-keys
506 "Click here or \\[exit-recursive-edit] to finish")
509 (epa--insert-keys keys)
511 (set-keymap-parent (current-local-map) widget-keymap)
512 (setq epa-exit-buffer-function #'abort-recursive-edit)
513 (goto-char (point-min))
514 (pop-to-buffer (current-buffer)))
519 (if (get-buffer-window epa-keys-buffer)
520 (delete-window (get-buffer-window epa-keys-buffer)))
521 (kill-buffer epa-keys-buffer))))
524 (defun epa-select-keys (context prompt &optional names secret)
525 "Display a user's keyring and ask him to select keys.
526 CONTEXT is an epg-context.
527 PROMPT is a string to prompt with.
528 NAMES is a list of strings to be matched with keys. If it is nil, all
530 If SECRET is non-nil, list secret keys instead of public keys."
531 (let ((keys (epg-list-keys context names secret)))
532 (epa--select-keys prompt keys)))
534 (defun epa--show-key (key)
535 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
536 (entry (assoc (epg-sub-key-id primary-sub-key)
537 epa-key-buffer-alist))
538 (inhibit-read-only t)
542 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
543 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
544 (unless (and (cdr entry)
545 (buffer-live-p (cdr entry)))
546 (setcdr entry (generate-new-buffer
547 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
548 (set-buffer (cdr entry))
550 (make-local-variable 'epa-key)
553 (setq pointer (epg-key-user-id-list key))
557 (if (epg-user-id-validity (car pointer))
559 (car (rassq (epg-user-id-validity (car pointer))
560 epg-key-validity-alist)))
563 (if (stringp (epg-user-id-string (car pointer)))
564 (epg-user-id-string (car pointer))
565 (epg-decode-dn (epg-user-id-string (car pointer))))
567 (setq pointer (cdr pointer)))
568 (setq pointer (epg-key-sub-key-list key))
571 (if (epg-sub-key-validity (car pointer))
573 (car (rassq (epg-sub-key-validity (car pointer))
574 epg-key-validity-alist)))
577 (epg-sub-key-id (car pointer))
580 (epg-sub-key-length (car pointer)))
582 (cdr (assq (epg-sub-key-algorithm (car pointer))
583 epg-pubkey-algorithm-alist))
586 (format-time-string "%Y-%m-%d"
587 (epg-sub-key-creation-time (car pointer)))
588 (error "????-??-??"))
589 (if (epg-sub-key-expiration-time (car pointer))
590 (format "\n\tExpires: %s"
592 (format-time-string "%Y-%m-%d"
593 (epg-sub-key-expiration-time
595 (error "????-??-??")))
598 (mapconcat #'symbol-name
599 (epg-sub-key-capability (car pointer))
602 (epg-sub-key-fingerprint (car pointer))
604 (setq pointer (cdr pointer)))
605 (goto-char (point-min))
606 (pop-to-buffer (current-buffer))))
608 (defun epa-display-info (info)
609 (if epa-popup-info-window
610 (save-selected-window
611 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
612 (setq epa-info-buffer (generate-new-buffer "*Info*")))
613 (if (get-buffer-window epa-info-buffer)
614 (delete-window (get-buffer-window epa-info-buffer)))
616 (set-buffer epa-info-buffer)
617 (let ((inhibit-read-only t)
622 (goto-char (point-min)))
623 (if (> (window-height)
624 epa-info-window-height)
625 (set-window-buffer (split-window nil (- (window-height)
626 epa-info-window-height))
628 (pop-to-buffer epa-info-buffer)
629 (if (> (window-height) epa-info-window-height)
630 (shrink-window (- (window-height) epa-info-window-height)))))
631 (message "%s" info)))
633 (defun epa-display-verify-result (verify-result)
634 (epa-display-info (epg-verify-result-to-string verify-result)))
635 (make-obsolete 'epa-display-verify-result 'epa-display-info)
637 (defun epa-passphrase-callback-function (context key-id handback)
639 (read-passwd "Passphrase for symmetric encryption: "
640 (eq (epg-context-operation context) 'encrypt))
643 "Passphrase for PIN: "
644 (let ((entry (assoc key-id epg-user-id-alist)))
646 (format "Passphrase for %s %s: " key-id (cdr entry))
647 (format "Passphrase for %s: " key-id)))))))
649 (defun epa-progress-callback-function (context what char current total
651 (message "%s%d%% (%d/%d)" (or handback
653 (if (> total 0) (floor (* (/ current (float total)) 100)) 0)
657 (defun epa-decrypt-file (file)
659 (interactive "fFile: ")
660 (setq file (expand-file-name file))
661 (let* ((default-name (file-name-sans-extension file))
662 (plain (expand-file-name
664 (concat "To file (default "
665 (file-name-nondirectory default-name)
667 (file-name-directory default-name)
669 (context (epg-make-context epa-protocol)))
670 (epg-context-set-passphrase-callback context
671 #'epa-passphrase-callback-function)
672 (epg-context-set-progress-callback context
674 #'epa-progress-callback-function
675 (format "Decrypting %s..."
676 (file-name-nondirectory file))))
677 (message "Decrypting %s..." (file-name-nondirectory file))
678 (epg-decrypt-file context file plain)
679 (message "Decrypting %s...wrote %s" (file-name-nondirectory file)
680 (file-name-nondirectory plain))
681 (if (epg-context-result-for context 'verify)
682 (epa-display-info (epg-verify-result-to-string
683 (epg-context-result-for context 'verify))))))
686 (defun epa-verify-file (file)
688 (interactive "fFile: ")
689 (setq file (expand-file-name file))
690 (let* ((context (epg-make-context epa-protocol))
691 (plain (if (equal (file-name-extension file) "sig")
692 (file-name-sans-extension file))))
693 (epg-context-set-progress-callback context
695 #'epa-progress-callback-function
696 (format "Verifying %s..."
697 (file-name-nondirectory file))))
698 (message "Verifying %s..." (file-name-nondirectory file))
699 (epg-verify-file context file plain)
700 (message "Verifying %s...done" (file-name-nondirectory file))
701 (if (epg-context-result-for context 'verify)
702 (epa-display-info (epg-verify-result-to-string
703 (epg-context-result-for context 'verify))))))
705 (defun epa--read-signature-type ()
708 (message "Signature type (n,c,d,?) ")
713 (setq type 'detached))
715 (with-output-to-temp-buffer "*Help*"
717 (set-buffer standard-output)
719 n - Create a normal signature
720 c - Create a cleartext signature
721 d - Create a detached signature
725 (setq type 'normal))))))
728 (defun epa-sign-file (file signers mode)
729 "Sign FILE by SIGNERS keys selected."
731 (let ((verbose current-prefix-arg))
732 (list (expand-file-name (read-file-name "File: "))
734 (epa-select-keys (epg-make-context epa-protocol)
735 "Select keys for signing.
736 If no one is selected, default secret key is used. "
739 (epa--read-signature-type)
741 (let ((signature (concat file
742 (if (eq epa-protocol 'OpenPGP)
745 '(nil t normal detached))))
747 (if (memq mode '(t detached))
750 (if (memq mode '(t detached))
753 (context (epg-make-context epa-protocol)))
754 (epg-context-set-armor context epa-armor)
755 (epg-context-set-textmode context epa-textmode)
756 (epg-context-set-signers context signers)
757 (epg-context-set-passphrase-callback context
758 #'epa-passphrase-callback-function)
759 (epg-context-set-progress-callback context
761 #'epa-progress-callback-function
762 (format "Signing %s..."
763 (file-name-nondirectory file))))
764 (message "Signing %s..." (file-name-nondirectory file))
765 (epg-sign-file context file signature mode)
766 (message "Signing %s...wrote %s" (file-name-nondirectory file)
767 (file-name-nondirectory signature))))
770 (defun epa-encrypt-file (file recipients)
771 "Encrypt FILE for RECIPIENTS."
773 (list (expand-file-name (read-file-name "File: "))
774 (epa-select-keys (epg-make-context epa-protocol)
775 "Select recipients for encryption.
776 If no one is selected, symmetric encryption will be performed. ")))
777 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
778 (if epa-armor ".asc" ".gpg")
780 (context (epg-make-context epa-protocol)))
781 (epg-context-set-armor context epa-armor)
782 (epg-context-set-textmode context epa-textmode)
783 (epg-context-set-passphrase-callback context
784 #'epa-passphrase-callback-function)
785 (epg-context-set-progress-callback context
787 #'epa-progress-callback-function
788 (format "Encrypting %s..."
789 (file-name-nondirectory file))))
790 (message "Encrypting %s..." (file-name-nondirectory file))
791 (epg-encrypt-file context file recipients cipher)
792 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
793 (file-name-nondirectory cipher))))
796 (defun epa-decrypt-region (start end)
797 "Decrypt the current region between START and END.
799 Don't use this command in Lisp programs!
800 Since this function operates on regions, it does some tricks such
801 as coding-system detection and unibyte/multibyte conversion. If
802 you are sure how the data in the region should be treated, you
803 should consider using the string based counterpart
804 `epg-decrypt-string', or the file based counterpart
805 `epg-decrypt-file' instead.
809 \(let ((context (epg-make-context 'OpenPGP)))
810 (decode-coding-string
811 (epg-decrypt-string context (buffer-substring start end))
815 (let ((context (epg-make-context epa-protocol))
817 (epg-context-set-passphrase-callback context
818 #'epa-passphrase-callback-function)
819 (epg-context-set-progress-callback context
821 #'epa-progress-callback-function
823 (message "Decrypting...")
824 (setq plain (epg-decrypt-string context (buffer-substring start end)))
825 (message "Decrypting...done")
826 (setq plain (epa--decode-coding-string
828 (or coding-system-for-read
829 (get-text-property start 'epa-coding-system-used))))
830 (if (y-or-n-p "Replace the original text? ")
831 (let ((inhibit-read-only t)
833 (delete-region start end)
836 (with-output-to-temp-buffer "*Temp*"
837 (set-buffer standard-output)
840 (if (epg-context-result-for context 'verify)
841 (epa-display-info (epg-verify-result-to-string
842 (epg-context-result-for context 'verify)))))))
844 (defun epa--find-coding-system-for-mime-charset (mime-charset)
845 (if (featurep 'xemacs)
846 (if (fboundp 'find-coding-system)
847 (find-coding-system mime-charset))
848 (let ((pointer (coding-system-list)))
850 (eq (coding-system-get (car pointer) 'mime-charset)
852 (setq pointer (cdr pointer)))
856 (defun epa-decrypt-armor-in-region (start end)
857 "Decrypt OpenPGP armors in the current region between START and END.
859 Don't use this command in Lisp programs!
860 See the reason described in the `epa-decrypt-region' documentation."
864 (narrow-to-region start end)
866 (let (armor-start armor-end)
867 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
868 (setq armor-start (match-beginning 0)
869 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
872 (error "No armor tail"))
873 (goto-char armor-start)
874 (let ((coding-system-for-read
875 (or coding-system-for-read
876 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
877 (epa--find-coding-system-for-mime-charset
878 (intern (downcase (match-string 1))))))))
879 (goto-char armor-end)
880 (epa-decrypt-region armor-start armor-end)))))))
883 (defun epa-verify-region (start end)
884 "Verify the current region between START and END.
886 Don't use this command in Lisp programs!
887 Since this function operates on regions, it does some tricks such
888 as coding-system detection and unibyte/multibyte conversion. If
889 you are sure how the data in the region should be treated, you
890 should consider using the string based counterpart
891 `epg-verify-string', or the file based counterpart
892 `epg-verify-file' instead.
896 \(let ((context (epg-make-context 'OpenPGP)))
897 (decode-coding-string
898 (epg-verify-string context (buffer-substring start end))
901 (let ((context (epg-make-context epa-protocol))
903 (epg-context-set-progress-callback context
905 #'epa-progress-callback-function
907 (message "Verifying...")
908 (setq plain (epg-verify-string
910 (epa--encode-coding-string
911 (buffer-substring start end)
912 (or coding-system-for-write
913 (get-text-property start 'epa-coding-system-used)))))
914 (message "Verifying...done")
915 (setq plain (epa--decode-coding-string
917 (or coding-system-for-read
918 (get-text-property start 'epa-coding-system-used))))
919 (if (y-or-n-p "Replace the original text? ")
920 (let ((inhibit-read-only t)
922 (delete-region start end)
925 (with-output-to-temp-buffer "*Temp*"
926 (set-buffer standard-output)
929 (if (epg-context-result-for context 'verify)
930 (epa-display-info (epg-verify-result-to-string
931 (epg-context-result-for context 'verify))))))
934 (defun epa-verify-cleartext-in-region (start end)
935 "Verify OpenPGP cleartext signed messages in the current region
936 between START and END.
938 Don't use this command in Lisp programs!
939 See the reason described in the `epa-verify-region' documentation."
943 (narrow-to-region start end)
945 (let (cleartext-start cleartext-end)
946 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
948 (setq cleartext-start (match-beginning 0))
949 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
951 (error "Invalid cleartext signed message"))
952 (setq cleartext-end (re-search-forward
953 "^-----END PGP SIGNATURE-----$"
955 (unless cleartext-end
956 (error "No cleartext tail"))
957 (epa-verify-region cleartext-start cleartext-end))))))
960 (if (fboundp 'select-safe-coding-system)
961 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system)
962 (defun epa--select-safe-coding-system (from to)
963 buffer-file-coding-system)))
966 (defun epa-sign-region (start end signers mode)
967 "Sign the current region between START and END by SIGNERS keys selected.
969 Don't use this command in Lisp programs!
970 Since this function operates on regions, it does some tricks such
971 as coding-system detection and unibyte/multibyte conversion. If
972 you are sure how the data should be treated, you should consider
973 using the string based counterpart `epg-sign-string', or the file
974 based counterpart `epg-sign-file' instead.
978 \(let ((context (epg-make-context 'OpenPGP)))
981 (encode-coding-string (buffer-substring start end) 'utf-8)))"
983 (let ((verbose current-prefix-arg))
984 (setq epa-last-coding-system-specified
985 (or coding-system-for-write
986 (epa--select-safe-coding-system
987 (region-beginning) (region-end))))
988 (list (region-beginning) (region-end)
990 (epa-select-keys (epg-make-context epa-protocol)
991 "Select keys for signing.
992 If no one is selected, default secret key is used. "
995 (epa--read-signature-type)
998 (let ((context (epg-make-context epa-protocol))
1000 ;;(epg-context-set-armor context epa-armor)
1001 (epg-context-set-armor context t)
1002 ;;(epg-context-set-textmode context epa-textmode)
1003 (epg-context-set-textmode context t)
1004 (epg-context-set-signers context signers)
1005 (epg-context-set-passphrase-callback context
1006 #'epa-passphrase-callback-function)
1007 (epg-context-set-progress-callback context
1009 #'epa-progress-callback-function
1011 (message "Signing...")
1012 (setq signature (epg-sign-string context
1013 (epa--encode-coding-string
1014 (buffer-substring start end)
1015 epa-last-coding-system-specified)
1017 (message "Signing...done")
1018 (delete-region start end)
1020 (add-text-properties (point)
1022 (insert (epa--decode-coding-string
1024 (or coding-system-for-read
1025 epa-last-coding-system-specified)))
1027 (list 'epa-coding-system-used
1028 epa-last-coding-system-specified
1035 (if (fboundp 'derived-mode-p)
1036 (defalias 'epa--derived-mode-p 'derived-mode-p)
1037 (defun epa--derived-mode-p (&rest modes)
1038 "Non-nil if the current major mode is derived from one of MODES.
1039 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1040 (let ((parent major-mode))
1041 (while (and (not (memq parent modes))
1042 (setq parent (get parent 'derived-mode-parent))))
1046 (defun epa-encrypt-region (start end recipients sign signers)
1047 "Encrypt the current region between START and END for RECIPIENTS.
1049 Don't use this command in Lisp programs!
1050 Since this function operates on regions, it does some tricks such
1051 as coding-system detection and unibyte/multibyte conversion. If
1052 you are sure how the data should be treated, you should consider
1053 using the string based counterpart `epg-encrypt-string', or the
1054 file based counterpart `epg-encrypt-file' instead.
1058 \(let ((context (epg-make-context 'OpenPGP)))
1061 (encode-coding-string (buffer-substring start end) 'utf-8)
1064 (let ((verbose current-prefix-arg)
1065 (context (epg-make-context epa-protocol))
1067 (setq epa-last-coding-system-specified
1068 (or coding-system-for-write
1069 (epa--select-safe-coding-system
1070 (region-beginning) (region-end))))
1071 (list (region-beginning) (region-end)
1072 (epa-select-keys context
1073 "Select recipients for encryption.
1074 If no one is selected, symmetric encryption will be performed. ")
1075 (setq sign (if verbose (y-or-n-p "Sign? ")))
1077 (epa-select-keys context
1078 "Select keys for signing. ")))))
1080 (let ((context (epg-make-context epa-protocol))
1082 ;;(epg-context-set-armor context epa-armor)
1083 (epg-context-set-armor context t)
1084 ;;(epg-context-set-textmode context epa-textmode)
1085 (epg-context-set-textmode context t)
1087 (epg-context-set-signers context signers))
1088 (epg-context-set-passphrase-callback context
1089 #'epa-passphrase-callback-function)
1090 (epg-context-set-progress-callback context
1092 #'epa-progress-callback-function
1094 (message "Encrypting...")
1095 (setq cipher (epg-encrypt-string context
1096 (epa--encode-coding-string
1097 (buffer-substring start end)
1098 epa-last-coding-system-specified)
1101 (message "Encrypting...done")
1102 (delete-region start end)
1104 (add-text-properties (point)
1108 (list 'epa-coding-system-used
1109 epa-last-coding-system-specified
1116 (defun epa-delete-keys (keys &optional allow-secret)
1117 "Delete selected KEYS."
1119 (let ((keys (epa--marked-keys)))
1121 (error "No keys selected"))
1123 (eq (nth 1 epa-list-keys-arguments) t))))
1124 (let ((context (epg-make-context epa-protocol)))
1125 (message "Deleting...")
1126 (epg-delete-keys context keys allow-secret)
1127 (message "Deleting...done")
1128 (apply #'epa--list-keys epa-list-keys-arguments)))
1131 (defun epa-import-keys (file)
1132 "Import keys from FILE."
1133 (interactive "fFile: ")
1134 (setq file (expand-file-name file))
1135 (let ((context (epg-make-context epa-protocol)))
1136 (message "Importing %s..." (file-name-nondirectory file))
1139 (epg-import-keys-from-file context file)
1140 (message "Importing %s...done" (file-name-nondirectory file)))
1142 (message "Importing %s...failed" (file-name-nondirectory file))))
1143 (if (epg-context-result-for context 'import)
1144 (epa-display-info (epg-import-result-to-string
1145 (epg-context-result-for context 'import))))
1146 (if (eq major-mode 'epa-key-list-mode)
1147 (apply #'epa--list-keys epa-list-keys-arguments))))
1150 (defun epa-import-keys-region (start end)
1151 "Import keys from the region."
1153 (let ((context (epg-make-context epa-protocol)))
1154 (message "Importing...")
1157 (epg-import-keys-from-string context (buffer-substring start end))
1158 (message "Importing...done"))
1160 (message "Importing...failed")))
1161 (if (epg-context-result-for context 'import)
1162 (epa-display-info (epg-import-result-to-string
1163 (epg-context-result-for context 'import))))))
1166 (defun epa-import-armor-in-region (start end)
1167 "Import keys in the OpenPGP armor format in the current region
1168 between START and END."
1172 (narrow-to-region start end)
1174 (let (armor-start armor-end)
1175 (while (re-search-forward
1176 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1178 (setq armor-start (match-beginning 0)
1179 armor-end (re-search-forward
1180 (concat "^-----END " (match-string 1) "-----$")
1183 (error "No armor tail"))
1184 (epa-import-keys-region armor-start armor-end))))))
1187 (defun epa-export-keys (keys file)
1188 "Export selected KEYS to FILE."
1190 (let ((keys (epa--marked-keys))
1193 (error "No keys selected"))
1196 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1197 (if epa-armor ".asc" ".gpg"))
1202 (concat "To file (default "
1203 (file-name-nondirectory default-name)
1205 (file-name-directory default-name)
1207 (let ((context (epg-make-context epa-protocol)))
1208 (epg-context-set-armor context epa-armor)
1209 (message "Exporting to %s..." (file-name-nondirectory file))
1210 (epg-export-keys-to-file context keys file)
1211 (message "Exporting to %s...done" (file-name-nondirectory file))))
1214 (defun epa-insert-keys (keys)
1215 "Insert selected KEYS after the point."
1217 (list (epa-select-keys (epg-make-context epa-protocol)
1218 "Select keys to export. ")))
1219 (let ((context (epg-make-context epa-protocol)))
1220 ;;(epg-context-set-armor context epa-armor)
1221 (epg-context-set-armor context t)
1222 (insert (epg-export-keys-to-string context keys))))
1224 ;; (defun epa-sign-keys (keys &optional local)
1225 ;; "Sign selected KEYS.
1226 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1228 ;; Don't use this command in Lisp programs!"
1230 ;; (let ((keys (epa--marked-keys)))
1232 ;; (error "No keys selected"))
1233 ;; (list keys current-prefix-arg)))
1234 ;; (let ((context (epg-make-context epa-protocol)))
1235 ;; (epg-context-set-passphrase-callback context
1236 ;; #'epa-passphrase-callback-function)
1237 ;; (epg-context-set-progress-callback context
1239 ;; #'epa-progress-callback-function
1240 ;; "Signing keys..."))
1241 ;; (message "Signing keys...")
1242 ;; (epg-sign-keys context keys local)
1243 ;; (message "Signing keys...done")))
1244 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1248 ;; arch-tag: 38d20ced-20d5-4137-b17a-f206335423d7
1249 ;;; epa.el ends here