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))))
623 (defun epa-read-file-name (input)
624 "Interactively read an output file name based on INPUT file name."
625 (setq input (file-name-sans-extension (expand-file-name input)))
628 (concat "To file (default " (file-name-nondirectory input) ") ")
629 (file-name-directory input)
633 (defun epa-decrypt-file (decrypt-file &optional plain-file)
634 "Decrypt DECRYPT-FILE into PLAIN-FILE.
635 If you do not specify PLAIN-FILE, this functions prompts for the value to use."
637 (let* ((file (read-file-name "File to decrypt: "))
638 (plain (epa-read-file-name file)))
640 (or plain-file (setq plain-file (epa-read-file-name decrypt-file)))
641 (setq decrypt-file (expand-file-name decrypt-file))
642 (let ((context (epg-make-context epa-protocol)))
643 (epg-context-set-passphrase-callback context
644 #'epa-passphrase-callback-function)
645 (epg-context-set-progress-callback context
647 #'epa-progress-callback-function
648 (format "Decrypting %s..."
649 (file-name-nondirectory decrypt-file))))
650 (message "Decrypting %s..." (file-name-nondirectory decrypt-file))
651 (epg-decrypt-file context decrypt-file plain-file)
652 (message "Decrypting %s...wrote %s" (file-name-nondirectory decrypt-file)
653 (file-name-nondirectory plain-file))
654 (if (epg-context-result-for context 'verify)
655 (epa-display-info (epg-verify-result-to-string
656 (epg-context-result-for context 'verify))))))
659 (defun epa-verify-file (file)
661 (interactive "fFile: ")
662 (setq file (expand-file-name file))
663 (let* ((context (epg-make-context epa-protocol))
664 (plain (if (equal (file-name-extension file) "sig")
665 (file-name-sans-extension file))))
666 (epg-context-set-progress-callback context
668 #'epa-progress-callback-function
669 (format "Verifying %s..."
670 (file-name-nondirectory file))))
671 (message "Verifying %s..." (file-name-nondirectory file))
672 (epg-verify-file context file plain)
673 (message "Verifying %s...done" (file-name-nondirectory file))
674 (if (epg-context-result-for context 'verify)
675 (epa-display-info (epg-verify-result-to-string
676 (epg-context-result-for context 'verify))))))
678 (defun epa--read-signature-type ()
681 (message "Signature type (n,c,d,?) ")
686 (setq type 'detached))
688 (with-output-to-temp-buffer "*Help*"
689 (with-current-buffer standard-output
691 n - Create a normal signature
692 c - Create a cleartext signature
693 d - Create a detached signature
697 (setq type 'normal))))
701 (defun epa-sign-file (file signers mode)
702 "Sign FILE by SIGNERS keys selected."
704 (let ((verbose current-prefix-arg))
705 (list (expand-file-name (read-file-name "File: "))
707 (epa-select-keys (epg-make-context epa-protocol)
708 "Select keys for signing.
709 If no one is selected, default secret key is used. "
712 (epa--read-signature-type)
714 (let ((signature (concat file
715 (if (eq epa-protocol 'OpenPGP)
718 '(nil t normal detached))))
720 (if (memq mode '(t detached))
723 (if (memq mode '(t detached))
726 (context (epg-make-context epa-protocol)))
727 (epg-context-set-armor context epa-armor)
728 (epg-context-set-textmode context epa-textmode)
729 (epg-context-set-signers context signers)
730 (epg-context-set-passphrase-callback context
731 #'epa-passphrase-callback-function)
732 (epg-context-set-progress-callback context
734 #'epa-progress-callback-function
735 (format "Signing %s..."
736 (file-name-nondirectory file))))
737 (message "Signing %s..." (file-name-nondirectory file))
738 (epg-sign-file context file signature mode)
739 (message "Signing %s...wrote %s" (file-name-nondirectory file)
740 (file-name-nondirectory signature))))
743 (defun epa-encrypt-file (file recipients)
744 "Encrypt FILE for RECIPIENTS."
746 (list (expand-file-name (read-file-name "File: "))
747 (epa-select-keys (epg-make-context epa-protocol)
748 "Select recipients for encryption.
749 If no one is selected, symmetric encryption will be performed. ")))
750 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
751 (if epa-armor ".asc" ".gpg")
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-passphrase-callback context
757 #'epa-passphrase-callback-function)
758 (epg-context-set-progress-callback context
760 #'epa-progress-callback-function
761 (format "Encrypting %s..."
762 (file-name-nondirectory file))))
763 (message "Encrypting %s..." (file-name-nondirectory file))
764 (epg-encrypt-file context file recipients cipher)
765 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
766 (file-name-nondirectory cipher))))
769 (defun epa-decrypt-region (start end &optional make-buffer-function)
770 "Decrypt the current region between START and END.
772 If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
773 It should return that buffer. If it copies the input, it should
774 delete the text now being decrypted. It should leave point at the
775 proper place to insert the plaintext.
777 Be careful about using this command in Lisp programs!
778 Since this function operates on regions, it does some tricks such
779 as coding-system detection and unibyte/multibyte conversion. If
780 you are sure how the data in the region should be treated, you
781 should consider using the string based counterpart
782 `epg-decrypt-string', or the file based counterpart
783 `epg-decrypt-file' instead.
787 \(let ((context (epg-make-context 'OpenPGP)))
788 (decode-coding-string
789 (epg-decrypt-string context (buffer-substring start end))
793 (let ((context (epg-make-context epa-protocol))
795 (epg-context-set-passphrase-callback context
796 #'epa-passphrase-callback-function)
797 (epg-context-set-progress-callback context
799 #'epa-progress-callback-function
801 (message "Decrypting...")
802 (setq plain (epg-decrypt-string context (buffer-substring start end)))
803 (message "Decrypting...done")
804 (setq plain (epa--decode-coding-string
806 (or coding-system-for-read
807 (get-text-property start 'epa-coding-system-used)
809 (if make-buffer-function
810 (with-current-buffer (funcall make-buffer-function)
811 (let ((inhibit-read-only t))
813 (if (y-or-n-p "Replace the original text? ")
814 (let ((inhibit-read-only t))
815 (delete-region start end)
818 (with-output-to-temp-buffer "*Temp*"
819 (set-buffer standard-output)
822 (if (epg-context-result-for context 'verify)
823 (epa-display-info (epg-verify-result-to-string
824 (epg-context-result-for context 'verify)))))))
826 (defun epa--find-coding-system-for-mime-charset (mime-charset)
827 (if (featurep 'xemacs)
828 (if (fboundp 'find-coding-system)
829 (find-coding-system mime-charset))
830 ;; Find the first coding system which corresponds to MIME-CHARSET.
831 (let ((pointer (coding-system-list)))
833 (not (eq (coding-system-get (car pointer) 'mime-charset)
835 (setq pointer (cdr pointer)))
839 (defun epa-decrypt-armor-in-region (start end)
840 "Decrypt OpenPGP armors in the current region between START and END.
842 Don't use this command in Lisp programs!
843 See the reason described in the `epa-decrypt-region' documentation."
847 (narrow-to-region start end)
849 (let (armor-start armor-end)
850 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
851 (setq armor-start (match-beginning 0)
852 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
855 (error "Encryption armor beginning has no matching end"))
856 (goto-char armor-start)
857 (let ((coding-system-for-read
858 (or coding-system-for-read
859 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
860 (epa--find-coding-system-for-mime-charset
861 (intern (downcase (match-string 1))))))))
862 (goto-char armor-end)
863 (epa-decrypt-region armor-start armor-end)))))))
866 (defun epa-verify-region (start end)
867 "Verify the current region between START and END.
869 Don't use this command in Lisp programs!
870 Since this function operates on regions, it does some tricks such
871 as coding-system detection and unibyte/multibyte conversion. If
872 you are sure how the data in the region should be treated, you
873 should consider using the string based counterpart
874 `epg-verify-string', or the file based counterpart
875 `epg-verify-file' instead.
879 \(let ((context (epg-make-context 'OpenPGP)))
880 (decode-coding-string
881 (epg-verify-string context (buffer-substring start end))
884 (let ((context (epg-make-context epa-protocol))
886 (epg-context-set-progress-callback context
888 #'epa-progress-callback-function
890 (message "Verifying...")
891 (setq plain (epg-verify-string
893 (epa--encode-coding-string
894 (buffer-substring start end)
895 (or coding-system-for-write
896 (get-text-property start 'epa-coding-system-used)))))
897 (message "Verifying...done")
898 (setq plain (epa--decode-coding-string
900 (or coding-system-for-read
901 (get-text-property start 'epa-coding-system-used)
903 (if (y-or-n-p "Replace the original text? ")
904 (let ((inhibit-read-only t)
906 (delete-region start end)
909 (with-output-to-temp-buffer "*Temp*"
910 (set-buffer standard-output)
913 (if (epg-context-result-for context 'verify)
914 (epa-display-info (epg-verify-result-to-string
915 (epg-context-result-for context 'verify))))))
918 (defun epa-verify-cleartext-in-region (start end)
919 "Verify OpenPGP cleartext signed messages in the current region
920 between START and END.
922 Don't use this command in Lisp programs!
923 See the reason described in the `epa-verify-region' documentation."
927 (narrow-to-region start end)
929 (let (cleartext-start cleartext-end)
930 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
932 (setq cleartext-start (match-beginning 0))
933 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
935 (error "Invalid cleartext signed message"))
936 (setq cleartext-end (re-search-forward
937 "^-----END PGP SIGNATURE-----$"
939 (unless cleartext-end
940 (error "No cleartext tail"))
941 (epa-verify-region cleartext-start cleartext-end))))))
944 (if (fboundp 'select-safe-coding-system)
945 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system)
946 (defun epa--select-safe-coding-system (_from _to)
947 buffer-file-coding-system)))
950 (defun epa-sign-region (start end signers mode)
951 "Sign the current region between START and END by SIGNERS keys selected.
953 Don't use this command in Lisp programs!
954 Since this function operates on regions, it does some tricks such
955 as coding-system detection and unibyte/multibyte conversion. If
956 you are sure how the data should be treated, you should consider
957 using the string based counterpart `epg-sign-string', or the file
958 based counterpart `epg-sign-file' instead.
962 \(let ((context (epg-make-context 'OpenPGP)))
965 (encode-coding-string (buffer-substring start end) 'utf-8)))"
967 (let ((verbose current-prefix-arg))
968 (setq epa-last-coding-system-specified
969 (or coding-system-for-write
970 (epa--select-safe-coding-system
971 (region-beginning) (region-end))))
972 (list (region-beginning) (region-end)
974 (epa-select-keys (epg-make-context epa-protocol)
975 "Select keys for signing.
976 If no one is selected, default secret key is used. "
979 (epa--read-signature-type)
982 (let ((context (epg-make-context epa-protocol))
984 ;;(epg-context-set-armor context epa-armor)
985 (epg-context-set-armor context t)
986 ;;(epg-context-set-textmode context epa-textmode)
987 (epg-context-set-textmode context t)
988 (epg-context-set-signers context signers)
989 (epg-context-set-passphrase-callback context
990 #'epa-passphrase-callback-function)
991 (epg-context-set-progress-callback context
993 #'epa-progress-callback-function
995 (message "Signing...")
996 (setq signature (epg-sign-string context
997 (epa--encode-coding-string
998 (buffer-substring start end)
999 epa-last-coding-system-specified)
1001 (message "Signing...done")
1002 (delete-region start end)
1004 (add-text-properties (point)
1006 (insert (epa--decode-coding-string
1008 (or coding-system-for-read
1009 epa-last-coding-system-specified)))
1011 (list 'epa-coding-system-used
1012 epa-last-coding-system-specified
1019 (if (fboundp 'derived-mode-p)
1020 (defalias 'epa--derived-mode-p 'derived-mode-p)
1021 (defun epa--derived-mode-p (&rest modes)
1022 "Non-nil if the current major mode is derived from one of MODES.
1023 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1024 (let ((parent major-mode))
1025 (while (and (not (memq parent modes))
1026 (setq parent (get parent 'derived-mode-parent))))
1030 (defun epa-encrypt-region (start end recipients sign signers)
1031 "Encrypt the current region between START and END for RECIPIENTS.
1033 Don't use this command in Lisp programs!
1034 Since this function operates on regions, it does some tricks such
1035 as coding-system detection and unibyte/multibyte conversion. If
1036 you are sure how the data should be treated, you should consider
1037 using the string based counterpart `epg-encrypt-string', or the
1038 file based counterpart `epg-encrypt-file' instead.
1042 \(let ((context (epg-make-context 'OpenPGP)))
1045 (encode-coding-string (buffer-substring start end) 'utf-8)
1048 (let ((verbose current-prefix-arg)
1049 (context (epg-make-context epa-protocol))
1051 (setq epa-last-coding-system-specified
1052 (or coding-system-for-write
1053 (epa--select-safe-coding-system
1054 (region-beginning) (region-end))))
1055 (list (region-beginning) (region-end)
1056 (epa-select-keys context
1057 "Select recipients for encryption.
1058 If no one is selected, symmetric encryption will be performed. ")
1059 (setq sign (if verbose (y-or-n-p "Sign? ")))
1061 (epa-select-keys context
1062 "Select keys for signing. ")))))
1064 (let ((context (epg-make-context epa-protocol))
1066 ;;(epg-context-set-armor context epa-armor)
1067 (epg-context-set-armor context t)
1068 ;;(epg-context-set-textmode context epa-textmode)
1069 (epg-context-set-textmode context t)
1071 (epg-context-set-signers context signers))
1072 (epg-context-set-passphrase-callback context
1073 #'epa-passphrase-callback-function)
1074 (epg-context-set-progress-callback context
1076 #'epa-progress-callback-function
1078 (message "Encrypting...")
1079 (setq cipher (epg-encrypt-string context
1080 (epa--encode-coding-string
1081 (buffer-substring start end)
1082 epa-last-coding-system-specified)
1085 (message "Encrypting...done")
1086 (delete-region start end)
1088 (add-text-properties (point)
1092 (list 'epa-coding-system-used
1093 epa-last-coding-system-specified
1100 (defun epa-delete-keys (keys &optional allow-secret)
1101 "Delete selected KEYS."
1103 (let ((keys (epa--marked-keys)))
1105 (error "No keys selected"))
1107 (eq (nth 1 epa-list-keys-arguments) t))))
1108 (let ((context (epg-make-context epa-protocol)))
1109 (message "Deleting...")
1110 (epg-delete-keys context keys allow-secret)
1111 (message "Deleting...done")
1112 (apply #'epa--list-keys epa-list-keys-arguments)))
1115 (defun epa-import-keys (file)
1116 "Import keys from FILE."
1117 (interactive "fFile: ")
1118 (setq file (expand-file-name file))
1119 (let ((context (epg-make-context epa-protocol)))
1120 (message "Importing %s..." (file-name-nondirectory file))
1123 (epg-import-keys-from-file context file)
1124 (message "Importing %s...done" (file-name-nondirectory file)))
1126 (message "Importing %s...failed" (file-name-nondirectory file))))
1127 (if (epg-context-result-for context 'import)
1128 (epa-display-info (epg-import-result-to-string
1129 (epg-context-result-for context 'import))))
1130 (if (eq major-mode 'epa-key-list-mode)
1131 (apply #'epa--list-keys epa-list-keys-arguments))))
1134 (defun epa-import-keys-region (start end)
1135 "Import keys from the region."
1137 (let ((context (epg-make-context epa-protocol)))
1138 (message "Importing...")
1141 (epg-import-keys-from-string context (buffer-substring start end))
1142 (message "Importing...done"))
1144 (message "Importing...failed")))
1145 (if (epg-context-result-for context 'import)
1146 (epa-display-info (epg-import-result-to-string
1147 (epg-context-result-for context 'import))))))
1150 (defun epa-import-armor-in-region (start end)
1151 "Import keys in the OpenPGP armor format in the current region
1152 between START and END."
1156 (narrow-to-region start end)
1158 (let (armor-start armor-end)
1159 (while (re-search-forward
1160 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1162 (setq armor-start (match-beginning 0)
1163 armor-end (re-search-forward
1164 (concat "^-----END " (match-string 1) "-----$")
1167 (error "No armor tail"))
1168 (epa-import-keys-region armor-start armor-end))))))
1171 (defun epa-export-keys (keys file)
1172 "Export selected KEYS to FILE."
1174 (let ((keys (epa--marked-keys))
1177 (error "No keys selected"))
1180 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1181 (if epa-armor ".asc" ".gpg"))
1186 (concat "To file (default "
1187 (file-name-nondirectory default-name)
1189 (file-name-directory default-name)
1191 (let ((context (epg-make-context epa-protocol)))
1192 (epg-context-set-armor context epa-armor)
1193 (message "Exporting to %s..." (file-name-nondirectory file))
1194 (epg-export-keys-to-file context keys file)
1195 (message "Exporting to %s...done" (file-name-nondirectory file))))
1198 (defun epa-insert-keys (keys)
1199 "Insert selected KEYS after the point."
1201 (list (epa-select-keys (epg-make-context epa-protocol)
1202 "Select keys to export.
1203 If no one is selected, default public key is exported. ")))
1204 (let ((context (epg-make-context epa-protocol)))
1205 ;;(epg-context-set-armor context epa-armor)
1206 (epg-context-set-armor context t)
1207 (insert (epg-export-keys-to-string context keys))))
1209 ;; (defun epa-sign-keys (keys &optional local)
1210 ;; "Sign selected KEYS.
1211 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1213 ;; Don't use this command in Lisp programs!"
1215 ;; (let ((keys (epa--marked-keys)))
1217 ;; (error "No keys selected"))
1218 ;; (list keys current-prefix-arg)))
1219 ;; (let ((context (epg-make-context epa-protocol)))
1220 ;; (epg-context-set-passphrase-callback context
1221 ;; #'epa-passphrase-callback-function)
1222 ;; (epg-context-set-progress-callback context
1224 ;; #'epa-progress-callback-function
1225 ;; "Signing keys..."))
1226 ;; (message "Signing keys...")
1227 ;; (epg-sign-keys context keys local)
1228 ;; (message "Signing keys...done")))
1229 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1233 ;;; epa.el ends here