1 ;;; epa.el --- the EasyPG Assistant
2 ;; Copyright (C) 2006, 2007, 2008, 2009 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 of the License, or
12 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'wid-edit
))
31 "The EasyPG Assistant"
35 (defcustom epa-popup-info-window t
36 "If non-nil, status information from epa commands is displayed on
41 (defcustom epa-info-window-height
5
42 "Number of lines used to display status information."
46 (defgroup epa-faces nil
51 (defface epa-validity-high
52 `((((class color
) (background dark
))
53 (:foreground
"PaleTurquoise"
54 ,@(if (assq ':weight custom-face-attributes
)
58 (,@(if (assq ':weight custom-face-attributes
)
61 "Face used for displaying the high validity."
64 (defface epa-validity-medium
65 `((((class color
) (background dark
))
66 (:foreground
"PaleTurquoise"
67 ,@(if (assq ':slant custom-face-attributes
)
71 (,@(if (assq ':slant custom-face-attributes
)
74 "Face used for displaying the medium validity."
77 (defface epa-validity-low
79 (,@(if (assq ':slant custom-face-attributes
)
82 "Face used for displaying the low validity."
85 (defface epa-validity-disabled
87 (,@(if (assq ':slant custom-face-attributes
)
91 "Face used for displaying the disabled validity."
95 '((((class color
) (background dark
))
96 (:foreground
"lightyellow"))
97 (((class color
) (background light
))
98 (:foreground
"blue4")))
99 "Face used for displaying the string."
103 `((((class color
) (background dark
))
104 (:foreground
"orange"
105 ,@(if (assq ':weight custom-face-attributes
)
108 (((class color
) (background light
))
110 ,@(if (assq ':weight custom-face-attributes
)
114 (,@(if (assq ':weight custom-face-attributes
)
117 "Face used for displaying the high validity."
120 (defface epa-field-name
121 `((((class color
) (background dark
))
122 (:foreground
"PaleTurquoise"
123 ,@(if (assq ':weight custom-face-attributes
)
127 (,@(if (assq ':weight custom-face-attributes
)
130 "Face for the name of the attribute field."
133 (defface epa-field-body
134 `((((class color
) (background dark
))
135 (:foreground
"turquoise"
136 ,@(if (assq ':slant custom-face-attributes
)
140 (,@(if (assq ':slant custom-face-attributes
)
143 "Face for the body of the attribute field."
146 (defcustom epa-validity-face-alist
147 '((unknown . epa-validity-disabled
)
148 (invalid . epa-validity-disabled
)
149 (disabled . epa-validity-disabled
)
150 (revoked . epa-validity-disabled
)
151 (expired . epa-validity-disabled
)
152 (none . epa-validity-low
)
153 (undefined . epa-validity-low
)
154 (never . epa-validity-low
)
155 (marginal . epa-validity-medium
)
156 (full . epa-validity-high
)
157 (ultimate . epa-validity-high
))
158 "An alist mapping validity values to faces."
159 :type
'(repeat (cons symbol face
))
162 (defvar epa-font-lock-keywords
165 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
167 (2 'epa-field-body
)))
168 "Default expressions to addon in epa-mode.")
170 (defconst epa-pubkey-algorithm-letter-alist
178 (defvar epa-protocol
'OpenPGP
179 "*The default protocol.
180 The value can be either OpenPGP or CMS.
182 You should bind this variable with `let', but do not set it globally.")
184 (defvar epa-armor nil
185 "*If non-nil, epa commands create ASCII armored output.
187 You should bind this variable with `let', but do not set it globally.")
189 (defvar epa-textmode nil
190 "*If non-nil, epa commands treat input files as text.
192 You should bind this variable with `let', but do not set it globally.")
194 (defvar epa-keys-buffer nil
)
195 (defvar epa-key-buffer-alist nil
)
197 (defvar epa-list-keys-arguments nil
)
198 (defvar epa-info-buffer nil
)
199 (defvar epa-last-coding-system-specified nil
)
201 (defvar epa-key-list-mode-map
202 (let ((keymap (make-sparse-keymap))
203 (menu-map (make-sparse-keymap)))
204 (define-key keymap
"m" 'epa-mark-key
)
205 (define-key keymap
"u" 'epa-unmark-key
)
206 (define-key keymap
"d" 'epa-decrypt-file
)
207 (define-key keymap
"v" 'epa-verify-file
)
208 (define-key keymap
"s" 'epa-sign-file
)
209 (define-key keymap
"e" 'epa-encrypt-file
)
210 (define-key keymap
"r" 'epa-delete-keys
)
211 (define-key keymap
"i" 'epa-import-keys
)
212 (define-key keymap
"o" 'epa-export-keys
)
213 (define-key keymap
"g" 'revert-buffer
)
214 (define-key keymap
"n" 'next-line
)
215 (define-key keymap
"p" 'previous-line
)
216 (define-key keymap
" " 'scroll-up
)
217 (define-key keymap
[delete] 'scroll-down)
218 (define-key keymap "q" 'epa-exit-buffer)
219 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
220 (define-key menu-map [epa-key-list-unmark-key]
221 '(menu-item "Unmark Key" epa-unmark-key
222 :help "Unmark a key"))
223 (define-key menu-map [epa-key-list-mark-key]
224 '(menu-item "Mark Key" epa-mark-key
226 (define-key menu-map [separator-epa-file] '(menu-item "--"))
227 (define-key menu-map [epa-verify-file]
228 '(menu-item "Verify File..." epa-verify-file
229 :help "Verify FILE"))
230 (define-key menu-map [epa-sign-file]
231 '(menu-item "Sign File..." epa-sign-file
232 :help "Sign FILE by SIGNERS keys selected"))
233 (define-key menu-map [epa-decrypt-file]
234 '(menu-item "Decrypt File..." epa-decrypt-file
235 :help "Decrypt FILE"))
236 (define-key menu-map [epa-encrypt-file]
237 '(menu-item "Encrypt File.." epa-encrypt-file
238 :help "Encrypt FILE for RECIPIENTS"))
239 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
240 (define-key menu-map [epa-key-list-delete-keys]
241 '(menu-item "Delete keys" epa-delete-keys
242 :help "Delete Marked Keys"))
243 (define-key menu-map [epa-key-list-import-keys]
244 '(menu-item "Import Keys" epa-import-keys
245 :help "Import keys from a file"))
246 (define-key menu-map [epa-key-list-export-keys]
247 '(menu-item "Export Keys" epa-export-keys
248 :help "Export marked keys to a file"))
251 (defvar epa-key-mode-map
252 (let ((keymap (make-sparse-keymap)))
253 (define-key keymap "q" 'epa-exit-buffer)
256 (defvar epa-info-mode-map
257 (let ((keymap (make-sparse-keymap)))
258 (define-key keymap "q" 'delete-window)
261 (defvar epa-exit-buffer-function #'bury-buffer)
263 (define-widget 'epa-key 'push-button
264 "Button for representing a epg-key object."
266 :button-face-get 'epa--key-widget-button-face-get
267 :value-create 'epa--key-widget-value-create
268 :action 'epa--key-widget-action
269 :help-echo 'epa--key-widget-help-echo)
271 (defun epa--key-widget-action (widget &optional event)
272 (save-selected-window
273 (epa--show-key (widget-get widget :value))))
275 (defun epa--key-widget-value-create (widget)
276 (let* ((key (widget-get widget :value))
277 (primary-sub-key (car (epg-key-sub-key-list key)))
278 (primary-user-id (car (epg-key-user-id-list key))))
279 (insert (format "%c "
280 (if (epg-sub-key-validity primary-sub-key)
281 (car (rassq (epg-sub-key-validity primary-sub-key)
282 epg-key-validity-alist))
284 (epg-sub-key-id primary-sub-key)
287 (if (stringp (epg-user-id-string primary-user-id))
288 (epg-user-id-string primary-user-id)
289 (epg-decode-dn (epg-user-id-string primary-user-id)))
292 (defun epa--key-widget-button-face-get (widget)
293 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
294 (widget-get widget :value))))))
296 (cdr (assq validity epa-validity-face-alist))
299 (defun epa--key-widget-help-echo (widget)
301 (epg-sub-key-id (car (epg-key-sub-key-list
302 (widget-get widget :value))))))
305 (if (fboundp 'encode-coding-string)
306 (defalias 'epa--encode-coding-string 'encode-coding-string)
307 (defalias 'epa--encode-coding-string 'identity)))
310 (if (fboundp 'decode-coding-string)
311 (defalias 'epa--decode-coding-string 'decode-coding-string)
312 (defalias 'epa--decode-coding-string 'identity)))
314 (defun epa-key-list-mode ()
315 "Major mode for `epa-list-keys'."
316 (kill-all-local-variables)
317 (buffer-disable-undo)
318 (setq major-mode 'epa-key-list-mode
322 (use-local-map epa-key-list-mode-map)
323 (make-local-variable 'font-lock-defaults)
324 (setq font-lock-defaults '(epa-font-lock-keywords t))
325 ;; In XEmacs, auto-initialization of font-lock is not effective
326 ;; if buffer-file-name is not set.
327 (font-lock-set-defaults)
328 (make-local-variable 'epa-exit-buffer-function)
329 (make-local-variable 'revert-buffer-function)
330 (setq revert-buffer-function 'epa--key-list-revert-buffer)
331 (run-mode-hooks 'epa-key-list-mode-hook))
333 (defun epa-key-mode ()
334 "Major mode for a key description."
335 (kill-all-local-variables)
336 (buffer-disable-undo)
337 (setq major-mode 'epa-key-mode
341 (use-local-map epa-key-mode-map)
342 (make-local-variable 'font-lock-defaults)
343 (setq font-lock-defaults '(epa-font-lock-keywords t))
344 ;; In XEmacs, auto-initialization of font-lock is not effective
345 ;; if buffer-file-name is not set.
346 (font-lock-set-defaults)
347 (make-local-variable 'epa-exit-buffer-function)
348 (run-mode-hooks 'epa-key-mode-hook))
350 (defun epa-info-mode ()
351 "Major mode for `epa-info-buffer'."
352 (kill-all-local-variables)
353 (buffer-disable-undo)
354 (setq major-mode 'epa-info-mode
358 (use-local-map epa-info-mode-map)
359 (run-mode-hooks 'epa-info-mode-hook))
361 (defun epa-mark-key (&optional arg)
362 "Mark a key on the current line.
363 If ARG is non-nil, unmark the key."
365 (let ((inhibit-read-only t)
369 (unless (get-text-property (point) 'epa-key)
370 (error "No key on this line"))
371 (setq properties (text-properties-at (point)))
373 (insert (if arg " " "*"))
374 (set-text-properties (1- (point)) (point) properties)
377 (defun epa-unmark-key (&optional arg)
378 "Unmark a key on the current line.
379 If ARG is non-nil, mark the key."
381 (epa-mark-key (not arg)))
383 (defun epa-exit-buffer ()
384 "Exit the current buffer.
385 `epa-exit-buffer-function' is called if it is set."
387 (funcall epa-exit-buffer-function))
389 (defun epa--insert-keys (keys)
392 (narrow-to-region (point) (point))
397 (add-text-properties point (point)
398 (list 'epa-key (car keys)
403 (widget-create 'epa-key :value (car keys))
405 (setq keys (cdr keys))))
406 (add-text-properties (point-min) (point-max)
407 (list 'epa-list-keys t
413 (defun epa--list-keys (name secret)
414 (unless (and epa-keys-buffer
415 (buffer-live-p epa-keys-buffer))
416 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
417 (set-buffer epa-keys-buffer)
419 (let ((inhibit-read-only t)
422 (context (epg-make-context epa-protocol)))
423 (unless (get-text-property point 'epa-list-keys)
424 (setq point (next-single-property-change point 'epa-list-keys)))
427 (or (next-single-property-change point 'epa-list-keys)
430 (epa--insert-keys (epg-list-keys context name secret))
432 (set-keymap-parent (current-local-map) widget-keymap))
433 (make-local-variable 'epa-list-keys-arguments)
434 (setq epa-list-keys-arguments (list name secret))
435 (goto-char (point-min))
436 (pop-to-buffer (current-buffer)))
439 (defun epa-list-keys (&optional name)
440 "List all keys matched with NAME from the public keyring."
442 (if current-prefix-arg
443 (let ((name (read-string "Pattern: "
444 (if epa-list-keys-arguments
445 (car epa-list-keys-arguments)))))
446 (list (if (equal name "") nil name)))
448 (epa--list-keys name nil))
451 (defun epa-list-secret-keys (&optional name)
452 "List all keys matched with NAME from the private keyring."
454 (if current-prefix-arg
455 (let ((name (read-string "Pattern: "
456 (if epa-list-keys-arguments
457 (car epa-list-keys-arguments)))))
458 (list (if (equal name "") nil name)))
460 (epa--list-keys name t))
462 (defun epa--key-list-revert-buffer (&optional ignore-auto noconfirm)
463 (apply #'epa--list-keys epa-list-keys-arguments))
465 (defun epa--marked-keys ()
467 (set-buffer epa-keys-buffer)
468 (goto-char (point-min))
470 (while (re-search-forward "^\\*" nil t)
471 (if (setq key (get-text-property (match-beginning 0)
473 (setq keys (cons key keys))))
477 (let ((key (get-text-property (point) 'epa-key)))
481 (defun epa--select-keys (prompt keys)
483 (unless (and epa-keys-buffer
484 (buffer-live-p epa-keys-buffer))
485 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
486 (set-buffer epa-keys-buffer)
488 (let ((inhibit-read-only t)
492 (substitute-command-keys "\
493 - `\\[epa-mark-key]' to mark a key on the line
494 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
496 :notify (lambda (&rest ignore) (abort-recursive-edit))
498 (substitute-command-keys
499 "Click here or \\[abort-recursive-edit] to cancel")
502 :notify (lambda (&rest ignore) (exit-recursive-edit))
504 (substitute-command-keys
505 "Click here or \\[exit-recursive-edit] to finish")
508 (epa--insert-keys keys)
510 (set-keymap-parent (current-local-map) widget-keymap)
511 (setq epa-exit-buffer-function #'abort-recursive-edit)
512 (goto-char (point-min))
513 (pop-to-buffer (current-buffer)))
518 (if (get-buffer-window epa-keys-buffer)
519 (delete-window (get-buffer-window epa-keys-buffer)))
520 (kill-buffer epa-keys-buffer))))
523 (defun epa-select-keys (context prompt &optional names secret)
524 "Display a user's keyring and ask him to select keys.
525 CONTEXT is an epg-context.
526 PROMPT is a string to prompt with.
527 NAMES is a list of strings to be matched with keys. If it is nil, all
529 If SECRET is non-nil, list secret keys instead of public keys."
530 (let ((keys (epg-list-keys context names secret)))
531 (epa--select-keys prompt keys)))
533 (defun epa--show-key (key)
534 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
535 (entry (assoc (epg-sub-key-id primary-sub-key)
536 epa-key-buffer-alist))
537 (inhibit-read-only t)
541 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
542 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
543 (unless (and (cdr entry)
544 (buffer-live-p (cdr entry)))
545 (setcdr entry (generate-new-buffer
546 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
547 (set-buffer (cdr entry))
549 (make-local-variable 'epa-key)
552 (setq pointer (epg-key-user-id-list key))
556 (if (epg-user-id-validity (car pointer))
558 (car (rassq (epg-user-id-validity (car pointer))
559 epg-key-validity-alist)))
562 (if (stringp (epg-user-id-string (car pointer)))
563 (epg-user-id-string (car pointer))
564 (epg-decode-dn (epg-user-id-string (car pointer))))
566 (setq pointer (cdr pointer)))
567 (setq pointer (epg-key-sub-key-list key))
570 (if (epg-sub-key-validity (car pointer))
572 (car (rassq (epg-sub-key-validity (car pointer))
573 epg-key-validity-alist)))
576 (epg-sub-key-id (car pointer))
579 (epg-sub-key-length (car pointer)))
581 (cdr (assq (epg-sub-key-algorithm (car pointer))
582 epg-pubkey-algorithm-alist))
585 (format-time-string "%Y-%m-%d"
586 (epg-sub-key-creation-time (car pointer)))
587 (error "????-??-??"))
588 (if (epg-sub-key-expiration-time (car pointer))
589 (format (if (time-less-p (current-time)
590 (epg-sub-key-expiration-time
595 (format-time-string "%Y-%m-%d"
596 (epg-sub-key-expiration-time
598 (error "????-??-??")))
601 (mapconcat #'symbol-name
602 (epg-sub-key-capability (car pointer))
605 (epg-sub-key-fingerprint (car pointer))
607 (setq pointer (cdr pointer)))
608 (goto-char (point-min))
609 (pop-to-buffer (current-buffer))))
611 (defun epa-display-info (info)
612 (if epa-popup-info-window
613 (save-selected-window
614 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
615 (setq epa-info-buffer (generate-new-buffer "*Info*")))
616 (if (get-buffer-window epa-info-buffer)
617 (delete-window (get-buffer-window epa-info-buffer)))
619 (set-buffer epa-info-buffer)
620 (let ((inhibit-read-only t)
625 (goto-char (point-min)))
626 (if (> (window-height)
627 epa-info-window-height)
628 (set-window-buffer (split-window nil (- (window-height)
629 epa-info-window-height))
631 (pop-to-buffer epa-info-buffer)
632 (if (> (window-height) epa-info-window-height)
633 (shrink-window (- (window-height) epa-info-window-height)))))
634 (message "%s" info)))
636 (defun epa-display-verify-result (verify-result)
637 (epa-display-info (epg-verify-result-to-string verify-result)))
638 (make-obsolete 'epa-display-verify-result 'epa-display-info)
640 (defun epa-passphrase-callback-function (context key-id handback)
642 (read-passwd "Passphrase for symmetric encryption: "
643 (eq (epg-context-operation context) 'encrypt))
646 "Passphrase for PIN: "
647 (let ((entry (assoc key-id epg-user-id-alist)))
649 (format "Passphrase for %s %s: " key-id (cdr entry))
650 (format "Passphrase for %s: " key-id)))))))
652 (defun epa-progress-callback-function (context what char current total
654 (message "%s%d%% (%d/%d)" (or handback
656 (if (> total 0) (floor (* (/ current (float total)) 100)) 0)
660 (defun epa-decrypt-file (file)
662 (interactive "fFile: ")
663 (setq file (expand-file-name file))
664 (let* ((default-name (file-name-sans-extension file))
665 (plain (expand-file-name
667 (concat "To file (default "
668 (file-name-nondirectory default-name)
670 (file-name-directory default-name)
672 (context (epg-make-context epa-protocol)))
673 (epg-context-set-passphrase-callback context
674 #'epa-passphrase-callback-function)
675 (epg-context-set-progress-callback context
677 #'epa-progress-callback-function
678 (format "Decrypting %s..."
679 (file-name-nondirectory file))))
680 (message "Decrypting %s..." (file-name-nondirectory file))
681 (epg-decrypt-file context file plain)
682 (message "Decrypting %s...wrote %s" (file-name-nondirectory file)
683 (file-name-nondirectory plain))
684 (if (epg-context-result-for context 'verify)
685 (epa-display-info (epg-verify-result-to-string
686 (epg-context-result-for context 'verify))))))
689 (defun epa-verify-file (file)
691 (interactive "fFile: ")
692 (setq file (expand-file-name file))
693 (let* ((context (epg-make-context epa-protocol))
694 (plain (if (equal (file-name-extension file) "sig")
695 (file-name-sans-extension file))))
696 (epg-context-set-progress-callback context
698 #'epa-progress-callback-function
699 (format "Verifying %s..."
700 (file-name-nondirectory file))))
701 (message "Verifying %s..." (file-name-nondirectory file))
702 (epg-verify-file context file plain)
703 (message "Verifying %s...done" (file-name-nondirectory file))
704 (if (epg-context-result-for context 'verify)
705 (epa-display-info (epg-verify-result-to-string
706 (epg-context-result-for context 'verify))))))
708 (defun epa--read-signature-type ()
711 (message "Signature type (n,c,d,?) ")
716 (setq type 'detached))
718 (with-output-to-temp-buffer "*Help*"
720 (set-buffer standard-output)
722 n - Create a normal signature
723 c - Create a cleartext signature
724 d - Create a detached signature
728 (setq type 'normal))))
732 (defun epa-sign-file (file signers mode)
733 "Sign FILE by SIGNERS keys selected."
735 (let ((verbose current-prefix-arg))
736 (list (expand-file-name (read-file-name "File: "))
738 (epa-select-keys (epg-make-context epa-protocol)
739 "Select keys for signing.
740 If no one is selected, default secret key is used. "
743 (epa--read-signature-type)
745 (let ((signature (concat file
746 (if (eq epa-protocol 'OpenPGP)
749 '(nil t normal detached))))
751 (if (memq mode '(t detached))
754 (if (memq mode '(t detached))
757 (context (epg-make-context epa-protocol)))
758 (epg-context-set-armor context epa-armor)
759 (epg-context-set-textmode context epa-textmode)
760 (epg-context-set-signers context signers)
761 (epg-context-set-passphrase-callback context
762 #'epa-passphrase-callback-function)
763 (epg-context-set-progress-callback context
765 #'epa-progress-callback-function
766 (format "Signing %s..."
767 (file-name-nondirectory file))))
768 (message "Signing %s..." (file-name-nondirectory file))
769 (epg-sign-file context file signature mode)
770 (message "Signing %s...wrote %s" (file-name-nondirectory file)
771 (file-name-nondirectory signature))))
774 (defun epa-encrypt-file (file recipients)
775 "Encrypt FILE for RECIPIENTS."
777 (list (expand-file-name (read-file-name "File: "))
778 (epa-select-keys (epg-make-context epa-protocol)
779 "Select recipients for encryption.
780 If no one is selected, symmetric encryption will be performed. ")))
781 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
782 (if epa-armor ".asc" ".gpg")
784 (context (epg-make-context epa-protocol)))
785 (epg-context-set-armor context epa-armor)
786 (epg-context-set-textmode context epa-textmode)
787 (epg-context-set-passphrase-callback context
788 #'epa-passphrase-callback-function)
789 (epg-context-set-progress-callback context
791 #'epa-progress-callback-function
792 (format "Encrypting %s..."
793 (file-name-nondirectory file))))
794 (message "Encrypting %s..." (file-name-nondirectory file))
795 (epg-encrypt-file context file recipients cipher)
796 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
797 (file-name-nondirectory cipher))))
800 (defun epa-decrypt-region (start end)
801 "Decrypt the current region between START and END.
803 Don't use this command in Lisp programs!
804 Since this function operates on regions, it does some tricks such
805 as coding-system detection and unibyte/multibyte conversion. If
806 you are sure how the data in the region should be treated, you
807 should consider using the string based counterpart
808 `epg-decrypt-string', or the file based counterpart
809 `epg-decrypt-file' instead.
813 \(let ((context (epg-make-context 'OpenPGP)))
814 (decode-coding-string
815 (epg-decrypt-string context (buffer-substring start end))
819 (let ((context (epg-make-context epa-protocol))
821 (epg-context-set-passphrase-callback context
822 #'epa-passphrase-callback-function)
823 (epg-context-set-progress-callback context
825 #'epa-progress-callback-function
827 (message "Decrypting...")
828 (setq plain (epg-decrypt-string context (buffer-substring start end)))
829 (message "Decrypting...done")
830 (setq plain (epa--decode-coding-string
832 (or coding-system-for-read
833 (get-text-property start 'epa-coding-system-used))))
834 (if (y-or-n-p "Replace the original text? ")
835 (let ((inhibit-read-only t)
837 (delete-region start end)
840 (with-output-to-temp-buffer "*Temp*"
841 (set-buffer standard-output)
844 (if (epg-context-result-for context 'verify)
845 (epa-display-info (epg-verify-result-to-string
846 (epg-context-result-for context 'verify)))))))
848 (defun epa--find-coding-system-for-mime-charset (mime-charset)
849 (if (featurep 'xemacs)
850 (if (fboundp 'find-coding-system)
851 (find-coding-system mime-charset))
852 (let ((pointer (coding-system-list)))
854 (eq (coding-system-get (car pointer) 'mime-charset)
856 (setq pointer (cdr pointer)))
860 (defun epa-decrypt-armor-in-region (start end)
861 "Decrypt OpenPGP armors in the current region between START and END.
863 Don't use this command in Lisp programs!
864 See the reason described in the `epa-decrypt-region' documentation."
868 (narrow-to-region start end)
870 (let (armor-start armor-end)
871 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
872 (setq armor-start (match-beginning 0)
873 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
876 (error "No armor tail"))
877 (goto-char armor-start)
878 (let ((coding-system-for-read
879 (or coding-system-for-read
880 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
881 (epa--find-coding-system-for-mime-charset
882 (intern (downcase (match-string 1))))))))
883 (goto-char armor-end)
884 (epa-decrypt-region armor-start armor-end)))))))
887 (defun epa-verify-region (start end)
888 "Verify the current region between START and END.
890 Don't use this command in Lisp programs!
891 Since this function operates on regions, it does some tricks such
892 as coding-system detection and unibyte/multibyte conversion. If
893 you are sure how the data in the region should be treated, you
894 should consider using the string based counterpart
895 `epg-verify-string', or the file based counterpart
896 `epg-verify-file' instead.
900 \(let ((context (epg-make-context 'OpenPGP)))
901 (decode-coding-string
902 (epg-verify-string context (buffer-substring start end))
905 (let ((context (epg-make-context epa-protocol))
907 (epg-context-set-progress-callback context
909 #'epa-progress-callback-function
911 (message "Verifying...")
912 (setq plain (epg-verify-string
914 (epa--encode-coding-string
915 (buffer-substring start end)
916 (or coding-system-for-write
917 (get-text-property start 'epa-coding-system-used)))))
918 (message "Verifying...done")
919 (setq plain (epa--decode-coding-string
921 (or coding-system-for-read
922 (get-text-property start 'epa-coding-system-used))))
923 (if (y-or-n-p "Replace the original text? ")
924 (let ((inhibit-read-only t)
926 (delete-region start end)
929 (with-output-to-temp-buffer "*Temp*"
930 (set-buffer standard-output)
933 (if (epg-context-result-for context 'verify)
934 (epa-display-info (epg-verify-result-to-string
935 (epg-context-result-for context 'verify))))))
938 (defun epa-verify-cleartext-in-region (start end)
939 "Verify OpenPGP cleartext signed messages in the current region
940 between START and END.
942 Don't use this command in Lisp programs!
943 See the reason described in the `epa-verify-region' documentation."
947 (narrow-to-region start end)
949 (let (cleartext-start cleartext-end)
950 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
952 (setq cleartext-start (match-beginning 0))
953 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
955 (error "Invalid cleartext signed message"))
956 (setq cleartext-end (re-search-forward
957 "^-----END PGP SIGNATURE-----$"
959 (unless cleartext-end
960 (error "No cleartext tail"))
961 (epa-verify-region cleartext-start cleartext-end))))))
964 (if (fboundp 'select-safe-coding-system)
965 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system)
966 (defun epa--select-safe-coding-system (from to)
967 buffer-file-coding-system)))
970 (defun epa-sign-region (start end signers mode)
971 "Sign the current region between START and END by SIGNERS keys selected.
973 Don't use this command in Lisp programs!
974 Since this function operates on regions, it does some tricks such
975 as coding-system detection and unibyte/multibyte conversion. If
976 you are sure how the data should be treated, you should consider
977 using the string based counterpart `epg-sign-string', or the file
978 based counterpart `epg-sign-file' instead.
982 \(let ((context (epg-make-context 'OpenPGP)))
985 (encode-coding-string (buffer-substring start end) 'utf-8)))"
987 (let ((verbose current-prefix-arg))
988 (setq epa-last-coding-system-specified
989 (or coding-system-for-write
990 (epa--select-safe-coding-system
991 (region-beginning) (region-end))))
992 (list (region-beginning) (region-end)
994 (epa-select-keys (epg-make-context epa-protocol)
995 "Select keys for signing.
996 If no one is selected, default secret key is used. "
999 (epa--read-signature-type)
1002 (let ((context (epg-make-context epa-protocol))
1004 ;;(epg-context-set-armor context epa-armor)
1005 (epg-context-set-armor context t)
1006 ;;(epg-context-set-textmode context epa-textmode)
1007 (epg-context-set-textmode context t)
1008 (epg-context-set-signers context signers)
1009 (epg-context-set-passphrase-callback context
1010 #'epa-passphrase-callback-function)
1011 (epg-context-set-progress-callback context
1013 #'epa-progress-callback-function
1015 (message "Signing...")
1016 (setq signature (epg-sign-string context
1017 (epa--encode-coding-string
1018 (buffer-substring start end)
1019 epa-last-coding-system-specified)
1021 (message "Signing...done")
1022 (delete-region start end)
1024 (add-text-properties (point)
1026 (insert (epa--decode-coding-string
1028 (or coding-system-for-read
1029 epa-last-coding-system-specified)))
1031 (list 'epa-coding-system-used
1032 epa-last-coding-system-specified
1039 (if (fboundp 'derived-mode-p)
1040 (defalias 'epa--derived-mode-p 'derived-mode-p)
1041 (defun epa--derived-mode-p (&rest modes)
1042 "Non-nil if the current major mode is derived from one of MODES.
1043 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1044 (let ((parent major-mode))
1045 (while (and (not (memq parent modes))
1046 (setq parent (get parent 'derived-mode-parent))))
1050 (defun epa-encrypt-region (start end recipients sign signers)
1051 "Encrypt the current region between START and END for RECIPIENTS.
1053 Don't use this command in Lisp programs!
1054 Since this function operates on regions, it does some tricks such
1055 as coding-system detection and unibyte/multibyte conversion. If
1056 you are sure how the data should be treated, you should consider
1057 using the string based counterpart `epg-encrypt-string', or the
1058 file based counterpart `epg-encrypt-file' instead.
1062 \(let ((context (epg-make-context 'OpenPGP)))
1065 (encode-coding-string (buffer-substring start end) 'utf-8)
1068 (let ((verbose current-prefix-arg)
1069 (context (epg-make-context epa-protocol))
1071 (setq epa-last-coding-system-specified
1072 (or coding-system-for-write
1073 (epa--select-safe-coding-system
1074 (region-beginning) (region-end))))
1075 (list (region-beginning) (region-end)
1076 (epa-select-keys context
1077 "Select recipients for encryption.
1078 If no one is selected, symmetric encryption will be performed. ")
1079 (setq sign (if verbose (y-or-n-p "Sign? ")))
1081 (epa-select-keys context
1082 "Select keys for signing. ")))))
1084 (let ((context (epg-make-context epa-protocol))
1086 ;;(epg-context-set-armor context epa-armor)
1087 (epg-context-set-armor context t)
1088 ;;(epg-context-set-textmode context epa-textmode)
1089 (epg-context-set-textmode context t)
1091 (epg-context-set-signers context signers))
1092 (epg-context-set-passphrase-callback context
1093 #'epa-passphrase-callback-function)
1094 (epg-context-set-progress-callback context
1096 #'epa-progress-callback-function
1098 (message "Encrypting...")
1099 (setq cipher (epg-encrypt-string context
1100 (epa--encode-coding-string
1101 (buffer-substring start end)
1102 epa-last-coding-system-specified)
1105 (message "Encrypting...done")
1106 (delete-region start end)
1108 (add-text-properties (point)
1112 (list 'epa-coding-system-used
1113 epa-last-coding-system-specified
1120 (defun epa-delete-keys (keys &optional allow-secret)
1121 "Delete selected KEYS."
1123 (let ((keys (epa--marked-keys)))
1125 (error "No keys selected"))
1127 (eq (nth 1 epa-list-keys-arguments) t))))
1128 (let ((context (epg-make-context epa-protocol)))
1129 (message "Deleting...")
1130 (epg-delete-keys context keys allow-secret)
1131 (message "Deleting...done")
1132 (apply #'epa--list-keys epa-list-keys-arguments)))
1135 (defun epa-import-keys (file)
1136 "Import keys from FILE."
1137 (interactive "fFile: ")
1138 (setq file (expand-file-name file))
1139 (let ((context (epg-make-context epa-protocol)))
1140 (message "Importing %s..." (file-name-nondirectory file))
1143 (epg-import-keys-from-file context file)
1144 (message "Importing %s...done" (file-name-nondirectory file)))
1146 (message "Importing %s...failed" (file-name-nondirectory file))))
1147 (if (epg-context-result-for context 'import)
1148 (epa-display-info (epg-import-result-to-string
1149 (epg-context-result-for context 'import))))
1150 (if (eq major-mode 'epa-key-list-mode)
1151 (apply #'epa--list-keys epa-list-keys-arguments))))
1154 (defun epa-import-keys-region (start end)
1155 "Import keys from the region."
1157 (let ((context (epg-make-context epa-protocol)))
1158 (message "Importing...")
1161 (epg-import-keys-from-string context (buffer-substring start end))
1162 (message "Importing...done"))
1164 (message "Importing...failed")))
1165 (if (epg-context-result-for context 'import)
1166 (epa-display-info (epg-import-result-to-string
1167 (epg-context-result-for context 'import))))))
1170 (defun epa-import-armor-in-region (start end)
1171 "Import keys in the OpenPGP armor format in the current region
1172 between START and END."
1176 (narrow-to-region start end)
1178 (let (armor-start armor-end)
1179 (while (re-search-forward
1180 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1182 (setq armor-start (match-beginning 0)
1183 armor-end (re-search-forward
1184 (concat "^-----END " (match-string 1) "-----$")
1187 (error "No armor tail"))
1188 (epa-import-keys-region armor-start armor-end))))))
1191 (defun epa-export-keys (keys file)
1192 "Export selected KEYS to FILE."
1194 (let ((keys (epa--marked-keys))
1197 (error "No keys selected"))
1200 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1201 (if epa-armor ".asc" ".gpg"))
1206 (concat "To file (default "
1207 (file-name-nondirectory default-name)
1209 (file-name-directory default-name)
1211 (let ((context (epg-make-context epa-protocol)))
1212 (epg-context-set-armor context epa-armor)
1213 (message "Exporting to %s..." (file-name-nondirectory file))
1214 (epg-export-keys-to-file context keys file)
1215 (message "Exporting to %s...done" (file-name-nondirectory file))))
1218 (defun epa-insert-keys (keys)
1219 "Insert selected KEYS after the point."
1221 (list (epa-select-keys (epg-make-context epa-protocol)
1222 "Select keys to export. ")))
1223 (let ((context (epg-make-context epa-protocol)))
1224 ;;(epg-context-set-armor context epa-armor)
1225 (epg-context-set-armor context t)
1226 (insert (epg-export-keys-to-string context keys))))
1228 ;; (defun epa-sign-keys (keys &optional local)
1229 ;; "Sign selected KEYS.
1230 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1232 ;; Don't use this command in Lisp programs!"
1234 ;; (let ((keys (epa--marked-keys)))
1236 ;; (error "No keys selected"))
1237 ;; (list keys current-prefix-arg)))
1238 ;; (let ((context (epg-make-context epa-protocol)))
1239 ;; (epg-context-set-passphrase-callback context
1240 ;; #'epa-passphrase-callback-function)
1241 ;; (epg-context-set-progress-callback context
1243 ;; #'epa-progress-callback-function
1244 ;; "Signing keys..."))
1245 ;; (message "Signing keys...")
1246 ;; (epg-sign-keys context keys local)
1247 ;; (message "Signing keys...done")))
1248 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1252 ;; arch-tag: 38d20ced-20d5-4137-b17a-f206335423d7
1253 ;;; epa.el ends here