1 ;;; epa.el --- the EasyPG Assistant -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006-2013 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: PGP, GnuPG
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'wid-edit
))
32 "The EasyPG Assistant"
36 (defcustom epa-popup-info-window t
37 "If non-nil, status information from epa commands is displayed on
42 (defcustom epa-info-window-height
5
43 "Number of lines used to display status information."
47 (defgroup epa-faces nil
52 (defface epa-validity-high
53 '((default :weight bold
)
54 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
55 "Face for high validity EPA information."
58 (defface epa-validity-medium
59 '((default :slant italic
)
60 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
61 "Face for medium validity EPA information."
64 (defface epa-validity-low
66 "Face used for displaying the low validity."
69 (defface epa-validity-disabled
70 '((t :slant italic
:inverse-video t
))
71 "Face used for displaying the disabled validity."
75 '((((class color
) (background dark
))
76 :foreground
"lightyellow")
77 (((class color
) (background light
))
79 "Face used for displaying the string."
83 '((default :weight bold
)
84 (((class color
) (background dark
)) :foreground
"orange")
85 (((class color
) (background light
)) :foreground
"red"))
86 "Face used for displaying the high validity."
89 (defface epa-field-name
90 '((default :weight bold
)
91 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
92 "Face for the name of the attribute field."
95 (defface epa-field-body
96 '((default :slant italic
)
97 (((class color
) (background dark
)) :foreground
"turquoise"))
98 "Face for the body of the attribute field."
101 (defcustom epa-validity-face-alist
102 '((unknown . epa-validity-disabled
)
103 (invalid . epa-validity-disabled
)
104 (disabled . epa-validity-disabled
)
105 (revoked . epa-validity-disabled
)
106 (expired . epa-validity-disabled
)
107 (none . epa-validity-low
)
108 (undefined . epa-validity-low
)
109 (never . epa-validity-low
)
110 (marginal . epa-validity-medium
)
111 (full . epa-validity-high
)
112 (ultimate . epa-validity-high
))
113 "An alist mapping validity values to faces."
114 :type
'(repeat (cons symbol face
))
117 (defvar epa-font-lock-keywords
120 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
122 (2 'epa-field-body
)))
123 "Default expressions to addon in epa-mode.")
125 (defconst epa-pubkey-algorithm-letter-alist
133 (defvar epa-protocol
'OpenPGP
134 "The default protocol.
135 The value can be either OpenPGP or CMS.
137 You should bind this variable with `let', but do not set it globally.")
139 (defvar epa-armor nil
140 "If non-nil, epa commands create ASCII armored output.
142 You should bind this variable with `let', but do not set it globally.")
144 (defvar epa-textmode nil
145 "If non-nil, epa commands treat input files as text.
147 You should bind this variable with `let', but do not set it globally.")
149 (defvar epa-keys-buffer nil
)
150 (defvar epa-key-buffer-alist nil
)
152 (defvar epa-list-keys-arguments nil
)
153 (defvar epa-info-buffer nil
)
154 (defvar epa-last-coding-system-specified nil
)
156 (defvar epa-key-list-mode-map
157 (let ((keymap (make-sparse-keymap))
158 (menu-map (make-sparse-keymap)))
159 (define-key keymap
"m" 'epa-mark-key
)
160 (define-key keymap
"u" 'epa-unmark-key
)
161 (define-key keymap
"d" 'epa-decrypt-file
)
162 (define-key keymap
"v" 'epa-verify-file
)
163 (define-key keymap
"s" 'epa-sign-file
)
164 (define-key keymap
"e" 'epa-encrypt-file
)
165 (define-key keymap
"r" 'epa-delete-keys
)
166 (define-key keymap
"i" 'epa-import-keys
)
167 (define-key keymap
"o" 'epa-export-keys
)
168 (define-key keymap
"g" 'revert-buffer
)
169 (define-key keymap
"n" 'next-line
)
170 (define-key keymap
"p" 'previous-line
)
171 (define-key keymap
" " 'scroll-up-command
)
172 (define-key keymap
[?\S-\
] 'scroll-down-command
)
173 (define-key keymap
[delete] 'scroll-down-command)
174 (define-key keymap "q" 'epa-exit-buffer)
175 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
176 (define-key menu-map [epa-key-list-unmark-key]
177 '(menu-item "Unmark Key" epa-unmark-key
178 :help "Unmark a key"))
179 (define-key menu-map [epa-key-list-mark-key]
180 '(menu-item "Mark Key" epa-mark-key
182 (define-key menu-map [separator-epa-file] '(menu-item "--"))
183 (define-key menu-map [epa-verify-file]
184 '(menu-item "Verify File..." epa-verify-file
185 :help "Verify FILE"))
186 (define-key menu-map [epa-sign-file]
187 '(menu-item "Sign File..." epa-sign-file
188 :help "Sign FILE by SIGNERS keys selected"))
189 (define-key menu-map [epa-decrypt-file]
190 '(menu-item "Decrypt File..." epa-decrypt-file
191 :help "Decrypt FILE"))
192 (define-key menu-map [epa-encrypt-file]
193 '(menu-item "Encrypt File..." epa-encrypt-file
194 :help "Encrypt FILE for RECIPIENTS"))
195 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
196 (define-key menu-map [epa-key-list-delete-keys]
197 '(menu-item "Delete Keys" epa-delete-keys
198 :help "Delete Marked Keys"))
199 (define-key menu-map [epa-key-list-import-keys]
200 '(menu-item "Import Keys" epa-import-keys
201 :help "Import keys from a file"))
202 (define-key menu-map [epa-key-list-export-keys]
203 '(menu-item "Export Keys" epa-export-keys
204 :help "Export marked keys to a file"))
207 (defvar epa-key-mode-map
208 (let ((keymap (make-sparse-keymap)))
209 (define-key keymap "q" 'epa-exit-buffer)
212 (defvar epa-info-mode-map
213 (let ((keymap (make-sparse-keymap)))
214 (define-key keymap "q" 'delete-window)
217 (defvar epa-exit-buffer-function #'bury-buffer)
219 (define-widget 'epa-key 'push-button
220 "Button for representing a epg-key object."
222 :button-face-get 'epa--key-widget-button-face-get
223 :value-create 'epa--key-widget-value-create
224 :action 'epa--key-widget-action
225 :help-echo 'epa--key-widget-help-echo)
227 (defun epa--key-widget-action (widget &optional _event)
228 (save-selected-window
229 (epa--show-key (widget-get widget :value))))
231 (defun epa--key-widget-value-create (widget)
232 (let* ((key (widget-get widget :value))
233 (primary-sub-key (car (epg-key-sub-key-list key)))
234 (primary-user-id (car (epg-key-user-id-list key))))
235 (insert (format "%c "
236 (if (epg-sub-key-validity primary-sub-key)
237 (car (rassq (epg-sub-key-validity primary-sub-key)
238 epg-key-validity-alist))
240 (epg-sub-key-id primary-sub-key)
243 (if (stringp (epg-user-id-string primary-user-id))
244 (epg-user-id-string primary-user-id)
245 (epg-decode-dn (epg-user-id-string primary-user-id)))
248 (defun epa--key-widget-button-face-get (widget)
249 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
250 (widget-get widget :value))))))
252 (cdr (assq validity epa-validity-face-alist))
255 (defun epa--key-widget-help-echo (widget)
257 (epg-sub-key-id (car (epg-key-sub-key-list
258 (widget-get widget :value))))))
261 (if (fboundp 'encode-coding-string)
262 (defalias 'epa--encode-coding-string 'encode-coding-string)
263 (defalias 'epa--encode-coding-string 'identity)))
266 (if (fboundp 'decode-coding-string)
267 (defalias 'epa--decode-coding-string 'decode-coding-string)
268 (defalias 'epa--decode-coding-string 'identity)))
270 (defun epa-key-list-mode ()
271 "Major mode for `epa-list-keys'."
272 (kill-all-local-variables)
273 (buffer-disable-undo)
274 (setq major-mode 'epa-key-list-mode
278 (use-local-map epa-key-list-mode-map)
279 (make-local-variable 'font-lock-defaults)
280 (setq font-lock-defaults '(epa-font-lock-keywords t))
281 ;; In XEmacs, auto-initialization of font-lock is not effective
282 ;; if buffer-file-name is not set.
283 (font-lock-set-defaults)
284 (make-local-variable 'epa-exit-buffer-function)
285 (make-local-variable 'revert-buffer-function)
286 (setq revert-buffer-function 'epa--key-list-revert-buffer)
287 (run-mode-hooks 'epa-key-list-mode-hook))
289 (defun epa-key-mode ()
290 "Major mode for a key description."
291 (kill-all-local-variables)
292 (buffer-disable-undo)
293 (setq major-mode 'epa-key-mode
297 (use-local-map epa-key-mode-map)
298 (make-local-variable 'font-lock-defaults)
299 (setq font-lock-defaults '(epa-font-lock-keywords t))
300 ;; In XEmacs, auto-initialization of font-lock is not effective
301 ;; if buffer-file-name is not set.
302 (font-lock-set-defaults)
303 (make-local-variable 'epa-exit-buffer-function)
304 (run-mode-hooks 'epa-key-mode-hook))
306 (defun epa-info-mode ()
307 "Major mode for `epa-info-buffer'."
308 (kill-all-local-variables)
309 (buffer-disable-undo)
310 (setq major-mode 'epa-info-mode
314 (use-local-map epa-info-mode-map)
315 (run-mode-hooks 'epa-info-mode-hook))
317 (defun epa-mark-key (&optional arg)
318 "Mark a key on the current line.
319 If ARG is non-nil, unmark the key."
321 (let ((inhibit-read-only t)
325 (unless (get-text-property (point) 'epa-key)
326 (error "No key on this line"))
327 (setq properties (text-properties-at (point)))
329 (insert (if arg " " "*"))
330 (set-text-properties (1- (point)) (point) properties)
333 (defun epa-unmark-key (&optional arg)
334 "Unmark a key on the current line.
335 If ARG is non-nil, mark the key."
337 (epa-mark-key (not arg)))
339 (defun epa-exit-buffer ()
340 "Exit the current buffer.
341 `epa-exit-buffer-function' is called if it is set."
343 (funcall epa-exit-buffer-function))
345 (defun epa--insert-keys (keys)
348 (narrow-to-region (point) (point))
353 (add-text-properties point (point)
354 (list 'epa-key (car keys)
359 (widget-create 'epa-key :value (car keys))
361 (setq keys (cdr keys))))
362 (add-text-properties (point-min) (point-max)
363 (list 'epa-list-keys t
369 (defun epa--list-keys (name secret)
370 (unless (and epa-keys-buffer
371 (buffer-live-p epa-keys-buffer))
372 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
373 (set-buffer epa-keys-buffer)
375 (let ((inhibit-read-only t)
378 (context (epg-make-context epa-protocol)))
379 (unless (get-text-property point 'epa-list-keys)
380 (setq point (next-single-property-change point 'epa-list-keys)))
383 (or (next-single-property-change point 'epa-list-keys)
386 (epa--insert-keys (epg-list-keys context name secret))
388 (set-keymap-parent (current-local-map) widget-keymap))
389 (make-local-variable 'epa-list-keys-arguments)
390 (setq epa-list-keys-arguments (list name secret))
391 (goto-char (point-min))
392 (pop-to-buffer (current-buffer)))
395 (defun epa-list-keys (&optional name)
396 "List all keys matched with NAME from the public keyring."
398 (if current-prefix-arg
399 (let ((name (read-string "Pattern: "
400 (if epa-list-keys-arguments
401 (car epa-list-keys-arguments)))))
402 (list (if (equal name "") nil name)))
404 (epa--list-keys name nil))
407 (defun epa-list-secret-keys (&optional name)
408 "List all keys matched with NAME from the private keyring."
410 (if current-prefix-arg
411 (let ((name (read-string "Pattern: "
412 (if epa-list-keys-arguments
413 (car epa-list-keys-arguments)))))
414 (list (if (equal name "") nil name)))
416 (epa--list-keys name t))
418 (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm)
419 (apply #'epa--list-keys epa-list-keys-arguments))
421 (defun epa--marked-keys ()
422 (or (with-current-buffer epa-keys-buffer
423 (goto-char (point-min))
425 (while (re-search-forward "^\\*" nil t)
426 (if (setq key (get-text-property (match-beginning 0)
428 (setq keys (cons key keys))))
430 (let ((key (get-text-property (point-at-bol) 'epa-key)))
434 (defun epa--select-keys (prompt keys)
435 (unless (and epa-keys-buffer
436 (buffer-live-p epa-keys-buffer))
437 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
438 (with-current-buffer epa-keys-buffer
440 ;; C-c C-c is the usual way to finish the selection (bug#11159).
441 (define-key (current-local-map) "\C-c\C-c" 'exit-recursive-edit)
442 (let ((inhibit-read-only t)
446 (substitute-command-keys "\
447 - `\\[epa-mark-key]' to mark a key on the line
448 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
450 :notify (lambda (&rest _ignore) (abort-recursive-edit))
452 (substitute-command-keys
453 "Click here or \\[abort-recursive-edit] to cancel")
456 :notify (lambda (&rest _ignore) (exit-recursive-edit))
458 (substitute-command-keys
459 "Click here or \\[exit-recursive-edit] to finish")
462 (epa--insert-keys keys)
464 (set-keymap-parent (current-local-map) widget-keymap)
465 (setq epa-exit-buffer-function #'abort-recursive-edit)
466 (goto-char (point-min))
467 (let ((display-buffer-mark-dedicated 'soft))
468 (pop-to-buffer (current-buffer))))
473 (kill-buffer epa-keys-buffer))))
476 (defun epa-select-keys (context prompt &optional names secret)
477 "Display a user's keyring and ask him to select keys.
478 CONTEXT is an epg-context.
479 PROMPT is a string to prompt with.
480 NAMES is a list of strings to be matched with keys. If it is nil, all
482 If SECRET is non-nil, list secret keys instead of public keys."
483 (let ((keys (epg-list-keys context names secret)))
484 (epa--select-keys prompt keys)))
486 (defun epa--show-key (key)
487 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
488 (entry (assoc (epg-sub-key-id primary-sub-key)
489 epa-key-buffer-alist))
490 (inhibit-read-only t)
494 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
495 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
496 (unless (and (cdr entry)
497 (buffer-live-p (cdr entry)))
498 (setcdr entry (generate-new-buffer
499 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
500 (set-buffer (cdr entry))
502 (make-local-variable 'epa-key)
505 (setq pointer (epg-key-user-id-list key))
509 (if (epg-user-id-validity (car pointer))
511 (car (rassq (epg-user-id-validity (car pointer))
512 epg-key-validity-alist)))
515 (if (stringp (epg-user-id-string (car pointer)))
516 (epg-user-id-string (car pointer))
517 (epg-decode-dn (epg-user-id-string (car pointer))))
519 (setq pointer (cdr pointer)))
520 (setq pointer (epg-key-sub-key-list key))
523 (if (epg-sub-key-validity (car pointer))
525 (car (rassq (epg-sub-key-validity (car pointer))
526 epg-key-validity-alist)))
529 (epg-sub-key-id (car pointer))
532 (epg-sub-key-length (car pointer)))
534 (cdr (assq (epg-sub-key-algorithm (car pointer))
535 epg-pubkey-algorithm-alist))
538 (format-time-string "%Y-%m-%d"
539 (epg-sub-key-creation-time (car pointer)))
540 (error "????-??-??"))
541 (if (epg-sub-key-expiration-time (car pointer))
542 (format (if (time-less-p (current-time)
543 (epg-sub-key-expiration-time
548 (format-time-string "%Y-%m-%d"
549 (epg-sub-key-expiration-time
551 (error "????-??-??")))
554 (mapconcat #'symbol-name
555 (epg-sub-key-capability (car pointer))
558 (epg-sub-key-fingerprint (car pointer))
560 (setq pointer (cdr pointer)))
561 (goto-char (point-min))
562 (pop-to-buffer (current-buffer))))
564 (defun epa-display-info (info)
565 (if epa-popup-info-window
566 (save-selected-window
567 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
568 (setq epa-info-buffer (generate-new-buffer "*Info*")))
569 (if (get-buffer-window epa-info-buffer)
570 (delete-window (get-buffer-window epa-info-buffer)))
571 (with-current-buffer epa-info-buffer
572 (let ((inhibit-read-only t)
577 (goto-char (point-min)))
578 (if (> (window-height)
579 epa-info-window-height)
580 (set-window-buffer (split-window nil (- (window-height)
581 epa-info-window-height))
583 (pop-to-buffer epa-info-buffer)
584 (if (> (window-height) epa-info-window-height)
585 (shrink-window (- (window-height) epa-info-window-height)))))
586 (message "%s" info)))
588 (defun epa-display-verify-result (verify-result)
589 (declare (obsolete epa-display-info "23.1"))
590 (epa-display-info (epg-verify-result-to-string verify-result)))
592 (defun epa-passphrase-callback-function (context key-id handback)
595 (format "Passphrase for symmetric encryption%s: "
596 ;; Add the file name to the prompt, if any.
597 (if (stringp handback)
598 (format " for %s" handback)
600 (eq (epg-context-operation context) 'encrypt))
603 "Passphrase for PIN: "
604 (let ((entry (assoc key-id epg-user-id-alist)))
606 (format "Passphrase for %s %s: " key-id (cdr entry))
607 (format "Passphrase for %s: " key-id)))))))
609 (defun epa-progress-callback-function (_context what _char current total
611 (let ((prompt (or handback
612 (format "Processing %s: " what))))
613 ;; According to gnupg/doc/DETAIL: a "total" of 0 indicates that
614 ;; the total amount is not known. The condition TOTAL && CUR ==
615 ;; TOTAL may be used to detect the end of an operation.
617 (if (= current total)
618 (message "%s...done" prompt)
619 (message "%s...%d%%" prompt
620 (floor (* (/ current (float total)) 100))))
621 (message "%s..." prompt))))
624 (defun epa-decrypt-file (file)
626 (interactive "fFile: ")
627 (setq file (expand-file-name file))
628 (let* ((default-name (file-name-sans-extension file))
629 (plain (expand-file-name
631 (concat "To file (default "
632 (file-name-nondirectory default-name)
634 (file-name-directory default-name)
636 (context (epg-make-context epa-protocol)))
637 (epg-context-set-passphrase-callback context
638 #'epa-passphrase-callback-function)
639 (epg-context-set-progress-callback context
641 #'epa-progress-callback-function
642 (format "Decrypting %s..."
643 (file-name-nondirectory file))))
644 (message "Decrypting %s..." (file-name-nondirectory file))
645 (epg-decrypt-file context file plain)
646 (message "Decrypting %s...wrote %s" (file-name-nondirectory file)
647 (file-name-nondirectory plain))
648 (if (epg-context-result-for context 'verify)
649 (epa-display-info (epg-verify-result-to-string
650 (epg-context-result-for context 'verify))))))
653 (defun epa-verify-file (file)
655 (interactive "fFile: ")
656 (setq file (expand-file-name file))
657 (let* ((context (epg-make-context epa-protocol))
658 (plain (if (equal (file-name-extension file) "sig")
659 (file-name-sans-extension file))))
660 (epg-context-set-progress-callback context
662 #'epa-progress-callback-function
663 (format "Verifying %s..."
664 (file-name-nondirectory file))))
665 (message "Verifying %s..." (file-name-nondirectory file))
666 (epg-verify-file context file plain)
667 (message "Verifying %s...done" (file-name-nondirectory file))
668 (if (epg-context-result-for context 'verify)
669 (epa-display-info (epg-verify-result-to-string
670 (epg-context-result-for context 'verify))))))
672 (defun epa--read-signature-type ()
675 (message "Signature type (n,c,d,?) ")
680 (setq type 'detached))
682 (with-output-to-temp-buffer "*Help*"
683 (with-current-buffer standard-output
685 n - Create a normal signature
686 c - Create a cleartext signature
687 d - Create a detached signature
691 (setq type 'normal))))
695 (defun epa-sign-file (file signers mode)
696 "Sign FILE by SIGNERS keys selected."
698 (let ((verbose current-prefix-arg))
699 (list (expand-file-name (read-file-name "File: "))
701 (epa-select-keys (epg-make-context epa-protocol)
702 "Select keys for signing.
703 If no one is selected, default secret key is used. "
706 (epa--read-signature-type)
708 (let ((signature (concat file
709 (if (eq epa-protocol 'OpenPGP)
712 '(nil t normal detached))))
714 (if (memq mode '(t detached))
717 (if (memq mode '(t detached))
720 (context (epg-make-context epa-protocol)))
721 (epg-context-set-armor context epa-armor)
722 (epg-context-set-textmode context epa-textmode)
723 (epg-context-set-signers context signers)
724 (epg-context-set-passphrase-callback context
725 #'epa-passphrase-callback-function)
726 (epg-context-set-progress-callback context
728 #'epa-progress-callback-function
729 (format "Signing %s..."
730 (file-name-nondirectory file))))
731 (message "Signing %s..." (file-name-nondirectory file))
732 (epg-sign-file context file signature mode)
733 (message "Signing %s...wrote %s" (file-name-nondirectory file)
734 (file-name-nondirectory signature))))
737 (defun epa-encrypt-file (file recipients)
738 "Encrypt FILE for RECIPIENTS."
740 (list (expand-file-name (read-file-name "File: "))
741 (epa-select-keys (epg-make-context epa-protocol)
742 "Select recipients for encryption.
743 If no one is selected, symmetric encryption will be performed. ")))
744 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
745 (if epa-armor ".asc" ".gpg")
747 (context (epg-make-context epa-protocol)))
748 (epg-context-set-armor context epa-armor)
749 (epg-context-set-textmode context epa-textmode)
750 (epg-context-set-passphrase-callback context
751 #'epa-passphrase-callback-function)
752 (epg-context-set-progress-callback context
754 #'epa-progress-callback-function
755 (format "Encrypting %s..."
756 (file-name-nondirectory file))))
757 (message "Encrypting %s..." (file-name-nondirectory file))
758 (epg-encrypt-file context file recipients cipher)
759 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
760 (file-name-nondirectory cipher))))
763 (defun epa-decrypt-region (start end &optional make-buffer-function)
764 "Decrypt the current region between START and END.
766 If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
767 It should return that buffer. If it copies the input, it should
768 delete the text now being decrypted. It should leave point at the
769 proper place to insert the plaintext.
771 Be careful about using this command in Lisp programs!
772 Since this function operates on regions, it does some tricks such
773 as coding-system detection and unibyte/multibyte conversion. If
774 you are sure how the data in the region should be treated, you
775 should consider using the string based counterpart
776 `epg-decrypt-string', or the file based counterpart
777 `epg-decrypt-file' instead.
781 \(let ((context (epg-make-context 'OpenPGP)))
782 (decode-coding-string
783 (epg-decrypt-string context (buffer-substring start end))
787 (let ((context (epg-make-context epa-protocol))
789 (epg-context-set-passphrase-callback context
790 #'epa-passphrase-callback-function)
791 (epg-context-set-progress-callback context
793 #'epa-progress-callback-function
795 (message "Decrypting...")
796 (setq plain (epg-decrypt-string context (buffer-substring start end)))
797 (message "Decrypting...done")
798 (setq plain (epa--decode-coding-string
800 (or coding-system-for-read
801 (get-text-property start 'epa-coding-system-used)
803 (if make-buffer-function
804 (with-current-buffer (funcall make-buffer-function)
805 (let ((inhibit-read-only t))
807 (if (y-or-n-p "Replace the original text? ")
808 (let ((inhibit-read-only t))
809 (delete-region start end)
812 (with-output-to-temp-buffer "*Temp*"
813 (set-buffer standard-output)
816 (if (epg-context-result-for context 'verify)
817 (epa-display-info (epg-verify-result-to-string
818 (epg-context-result-for context 'verify)))))))
820 (defun epa--find-coding-system-for-mime-charset (mime-charset)
821 (if (featurep 'xemacs)
822 (if (fboundp 'find-coding-system)
823 (find-coding-system mime-charset))
824 ;; Find the first coding system which corresponds to MIME-CHARSET.
825 (let ((pointer (coding-system-list)))
827 (not (eq (coding-system-get (car pointer) 'mime-charset)
829 (setq pointer (cdr pointer)))
833 (defun epa-decrypt-armor-in-region (start end)
834 "Decrypt OpenPGP armors in the current region between START and END.
836 Don't use this command in Lisp programs!
837 See the reason described in the `epa-decrypt-region' documentation."
841 (narrow-to-region start end)
843 (let (armor-start armor-end)
844 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
845 (setq armor-start (match-beginning 0)
846 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
849 (error "Encryption armor beginning has no matching end"))
850 (goto-char armor-start)
851 (let ((coding-system-for-read
852 (or coding-system-for-read
853 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
854 (epa--find-coding-system-for-mime-charset
855 (intern (downcase (match-string 1))))))))
856 (goto-char armor-end)
857 (epa-decrypt-region armor-start armor-end)))))))
860 (defun epa-verify-region (start end)
861 "Verify the current region between START and END.
863 Don't use this command in Lisp programs!
864 Since this function operates on regions, it does some tricks such
865 as coding-system detection and unibyte/multibyte conversion. If
866 you are sure how the data in the region should be treated, you
867 should consider using the string based counterpart
868 `epg-verify-string', or the file based counterpart
869 `epg-verify-file' instead.
873 \(let ((context (epg-make-context 'OpenPGP)))
874 (decode-coding-string
875 (epg-verify-string context (buffer-substring start end))
878 (let ((context (epg-make-context epa-protocol))
880 (epg-context-set-progress-callback context
882 #'epa-progress-callback-function
884 (message "Verifying...")
885 (setq plain (epg-verify-string
887 (epa--encode-coding-string
888 (buffer-substring start end)
889 (or coding-system-for-write
890 (get-text-property start 'epa-coding-system-used)))))
891 (message "Verifying...done")
892 (setq plain (epa--decode-coding-string
894 (or coding-system-for-read
895 (get-text-property start 'epa-coding-system-used)
897 (if (y-or-n-p "Replace the original text? ")
898 (let ((inhibit-read-only t)
900 (delete-region start end)
903 (with-output-to-temp-buffer "*Temp*"
904 (set-buffer standard-output)
907 (if (epg-context-result-for context 'verify)
908 (epa-display-info (epg-verify-result-to-string
909 (epg-context-result-for context 'verify))))))
912 (defun epa-verify-cleartext-in-region (start end)
913 "Verify OpenPGP cleartext signed messages in the current region
914 between START and END.
916 Don't use this command in Lisp programs!
917 See the reason described in the `epa-verify-region' documentation."
921 (narrow-to-region start end)
923 (let (cleartext-start cleartext-end)
924 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
926 (setq cleartext-start (match-beginning 0))
927 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
929 (error "Invalid cleartext signed message"))
930 (setq cleartext-end (re-search-forward
931 "^-----END PGP SIGNATURE-----$"
933 (unless cleartext-end
934 (error "No cleartext tail"))
935 (epa-verify-region cleartext-start cleartext-end))))))
938 (if (fboundp 'select-safe-coding-system)
939 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system)
940 (defun epa--select-safe-coding-system (_from _to)
941 buffer-file-coding-system)))
944 (defun epa-sign-region (start end signers mode)
945 "Sign the current region between START and END by SIGNERS keys selected.
947 Don't use this command in Lisp programs!
948 Since this function operates on regions, it does some tricks such
949 as coding-system detection and unibyte/multibyte conversion. If
950 you are sure how the data should be treated, you should consider
951 using the string based counterpart `epg-sign-string', or the file
952 based counterpart `epg-sign-file' instead.
956 \(let ((context (epg-make-context 'OpenPGP)))
959 (encode-coding-string (buffer-substring start end) 'utf-8)))"
961 (let ((verbose current-prefix-arg))
962 (setq epa-last-coding-system-specified
963 (or coding-system-for-write
964 (epa--select-safe-coding-system
965 (region-beginning) (region-end))))
966 (list (region-beginning) (region-end)
968 (epa-select-keys (epg-make-context epa-protocol)
969 "Select keys for signing.
970 If no one is selected, default secret key is used. "
973 (epa--read-signature-type)
976 (let ((context (epg-make-context epa-protocol))
978 ;;(epg-context-set-armor context epa-armor)
979 (epg-context-set-armor context t)
980 ;;(epg-context-set-textmode context epa-textmode)
981 (epg-context-set-textmode context t)
982 (epg-context-set-signers context signers)
983 (epg-context-set-passphrase-callback context
984 #'epa-passphrase-callback-function)
985 (epg-context-set-progress-callback context
987 #'epa-progress-callback-function
989 (message "Signing...")
990 (setq signature (epg-sign-string context
991 (epa--encode-coding-string
992 (buffer-substring start end)
993 epa-last-coding-system-specified)
995 (message "Signing...done")
996 (delete-region start end)
998 (add-text-properties (point)
1000 (insert (epa--decode-coding-string
1002 (or coding-system-for-read
1003 epa-last-coding-system-specified)))
1005 (list 'epa-coding-system-used
1006 epa-last-coding-system-specified
1013 (if (fboundp 'derived-mode-p)
1014 (defalias 'epa--derived-mode-p 'derived-mode-p)
1015 (defun epa--derived-mode-p (&rest modes)
1016 "Non-nil if the current major mode is derived from one of MODES.
1017 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1018 (let ((parent major-mode))
1019 (while (and (not (memq parent modes))
1020 (setq parent (get parent 'derived-mode-parent))))
1024 (defun epa-encrypt-region (start end recipients sign signers)
1025 "Encrypt the current region between START and END for RECIPIENTS.
1027 Don't use this command in Lisp programs!
1028 Since this function operates on regions, it does some tricks such
1029 as coding-system detection and unibyte/multibyte conversion. If
1030 you are sure how the data should be treated, you should consider
1031 using the string based counterpart `epg-encrypt-string', or the
1032 file based counterpart `epg-encrypt-file' instead.
1036 \(let ((context (epg-make-context 'OpenPGP)))
1039 (encode-coding-string (buffer-substring start end) 'utf-8)
1042 (let ((verbose current-prefix-arg)
1043 (context (epg-make-context epa-protocol))
1045 (setq epa-last-coding-system-specified
1046 (or coding-system-for-write
1047 (epa--select-safe-coding-system
1048 (region-beginning) (region-end))))
1049 (list (region-beginning) (region-end)
1050 (epa-select-keys context
1051 "Select recipients for encryption.
1052 If no one is selected, symmetric encryption will be performed. ")
1053 (setq sign (if verbose (y-or-n-p "Sign? ")))
1055 (epa-select-keys context
1056 "Select keys for signing. ")))))
1058 (let ((context (epg-make-context epa-protocol))
1060 ;;(epg-context-set-armor context epa-armor)
1061 (epg-context-set-armor context t)
1062 ;;(epg-context-set-textmode context epa-textmode)
1063 (epg-context-set-textmode context t)
1065 (epg-context-set-signers context signers))
1066 (epg-context-set-passphrase-callback context
1067 #'epa-passphrase-callback-function)
1068 (epg-context-set-progress-callback context
1070 #'epa-progress-callback-function
1072 (message "Encrypting...")
1073 (setq cipher (epg-encrypt-string context
1074 (epa--encode-coding-string
1075 (buffer-substring start end)
1076 epa-last-coding-system-specified)
1079 (message "Encrypting...done")
1080 (delete-region start end)
1082 (add-text-properties (point)
1086 (list 'epa-coding-system-used
1087 epa-last-coding-system-specified
1094 (defun epa-delete-keys (keys &optional allow-secret)
1095 "Delete selected KEYS."
1097 (let ((keys (epa--marked-keys)))
1099 (error "No keys selected"))
1101 (eq (nth 1 epa-list-keys-arguments) t))))
1102 (let ((context (epg-make-context epa-protocol)))
1103 (message "Deleting...")
1104 (epg-delete-keys context keys allow-secret)
1105 (message "Deleting...done")
1106 (apply #'epa--list-keys epa-list-keys-arguments)))
1109 (defun epa-import-keys (file)
1110 "Import keys from FILE."
1111 (interactive "fFile: ")
1112 (setq file (expand-file-name file))
1113 (let ((context (epg-make-context epa-protocol)))
1114 (message "Importing %s..." (file-name-nondirectory file))
1117 (epg-import-keys-from-file context file)
1118 (message "Importing %s...done" (file-name-nondirectory file)))
1120 (message "Importing %s...failed" (file-name-nondirectory file))))
1121 (if (epg-context-result-for context 'import)
1122 (epa-display-info (epg-import-result-to-string
1123 (epg-context-result-for context 'import))))
1124 (if (eq major-mode 'epa-key-list-mode)
1125 (apply #'epa--list-keys epa-list-keys-arguments))))
1128 (defun epa-import-keys-region (start end)
1129 "Import keys from the region."
1131 (let ((context (epg-make-context epa-protocol)))
1132 (message "Importing...")
1135 (epg-import-keys-from-string context (buffer-substring start end))
1136 (message "Importing...done"))
1138 (message "Importing...failed")))
1139 (if (epg-context-result-for context 'import)
1140 (epa-display-info (epg-import-result-to-string
1141 (epg-context-result-for context 'import))))))
1144 (defun epa-import-armor-in-region (start end)
1145 "Import keys in the OpenPGP armor format in the current region
1146 between START and END."
1150 (narrow-to-region start end)
1152 (let (armor-start armor-end)
1153 (while (re-search-forward
1154 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1156 (setq armor-start (match-beginning 0)
1157 armor-end (re-search-forward
1158 (concat "^-----END " (match-string 1) "-----$")
1161 (error "No armor tail"))
1162 (epa-import-keys-region armor-start armor-end))))))
1165 (defun epa-export-keys (keys file)
1166 "Export selected KEYS to FILE."
1168 (let ((keys (epa--marked-keys))
1171 (error "No keys selected"))
1174 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1175 (if epa-armor ".asc" ".gpg"))
1180 (concat "To file (default "
1181 (file-name-nondirectory default-name)
1183 (file-name-directory default-name)
1185 (let ((context (epg-make-context epa-protocol)))
1186 (epg-context-set-armor context epa-armor)
1187 (message "Exporting to %s..." (file-name-nondirectory file))
1188 (epg-export-keys-to-file context keys file)
1189 (message "Exporting to %s...done" (file-name-nondirectory file))))
1192 (defun epa-insert-keys (keys)
1193 "Insert selected KEYS after the point."
1195 (list (epa-select-keys (epg-make-context epa-protocol)
1196 "Select keys to export.
1197 If no one is selected, default public key is exported. ")))
1198 (let ((context (epg-make-context epa-protocol)))
1199 ;;(epg-context-set-armor context epa-armor)
1200 (epg-context-set-armor context t)
1201 (insert (epg-export-keys-to-string context keys))))
1203 ;; (defun epa-sign-keys (keys &optional local)
1204 ;; "Sign selected KEYS.
1205 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1207 ;; Don't use this command in Lisp programs!"
1209 ;; (let ((keys (epa--marked-keys)))
1211 ;; (error "No keys selected"))
1212 ;; (list keys current-prefix-arg)))
1213 ;; (let ((context (epg-make-context epa-protocol)))
1214 ;; (epg-context-set-passphrase-callback context
1215 ;; #'epa-passphrase-callback-function)
1216 ;; (epg-context-set-progress-callback context
1218 ;; #'epa-progress-callback-function
1219 ;; "Signing keys..."))
1220 ;; (message "Signing keys...")
1221 ;; (epg-sign-keys context keys local)
1222 ;; (message "Signing keys...done")))
1223 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1227 ;;; epa.el ends here