1 ;;; epa.el --- the EasyPG Assistant -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006-2014 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"
34 :link
'(custom-manual "(epa) Top")
37 (defcustom epa-popup-info-window t
38 "If non-nil, display status information from epa commands in another window."
42 (defcustom epa-info-window-height
5
43 "Number of lines used to display status information."
47 (defgroup epa-faces nil
52 (defcustom epa-mail-aliases nil
53 "Alist of aliases of email addresses that stand for encryption keys.
54 Each element is a list of email addresses (ALIAS EXPANSIONS...).
55 When one of the recipients of a message being encrypted is ALIAS,
56 instead of encrypting it for ALIAS, encrypt it for EXPANSIONS...
58 If EXPANSIONS is empty, ignore ALIAS as regards encryption.
59 This is a handy way to avoid warnings about addresses that you don't
62 The command `epa-mail-encrypt' uses this."
63 :type
'(repeat (cons (string :tag
"Alias") (repeat (string :tag
"Expansion"))))
67 (defface epa-validity-high
68 '((default :weight bold
)
69 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
70 "Face for high validity EPA information."
73 (defface epa-validity-medium
74 '((default :slant italic
)
75 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
76 "Face for medium validity EPA information."
79 (defface epa-validity-low
81 "Face used for displaying the low validity."
84 (defface epa-validity-disabled
85 '((t :slant italic
:inverse-video t
))
86 "Face used for displaying the disabled validity."
90 '((((class color
) (background dark
))
91 :foreground
"lightyellow")
92 (((class color
) (background light
))
94 "Face used for displaying the string."
98 '((default :weight bold
)
99 (((class color
) (background dark
)) :foreground
"orange")
100 (((class color
) (background light
)) :foreground
"red"))
101 "Face used for displaying the high validity."
104 (defface epa-field-name
105 '((default :weight bold
)
106 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
107 "Face for the name of the attribute field."
110 (defface epa-field-body
111 '((default :slant italic
)
112 (((class color
) (background dark
)) :foreground
"turquoise"))
113 "Face for the body of the attribute field."
116 (defcustom epa-validity-face-alist
117 '((unknown . epa-validity-disabled
)
118 (invalid . epa-validity-disabled
)
119 (disabled . epa-validity-disabled
)
120 (revoked . epa-validity-disabled
)
121 (expired . epa-validity-disabled
)
122 (none . epa-validity-low
)
123 (undefined . epa-validity-low
)
124 (never . epa-validity-low
)
125 (marginal . epa-validity-medium
)
126 (full . epa-validity-high
)
127 (ultimate . epa-validity-high
))
128 "An alist mapping validity values to faces."
129 :type
'(repeat (cons symbol face
))
132 (defvar epa-font-lock-keywords
135 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
137 (2 'epa-field-body
)))
138 "Default expressions to addon in epa-mode.")
140 (defconst epa-pubkey-algorithm-letter-alist
148 (defvar epa-protocol
'OpenPGP
149 "The default protocol.
150 The value can be either OpenPGP or CMS.
152 You should bind this variable with `let', but do not set it globally.")
154 (defvar epa-armor nil
155 "If non-nil, epa commands create ASCII armored output.
157 You should bind this variable with `let', but do not set it globally.")
159 (defvar epa-textmode nil
160 "If non-nil, epa commands treat input files as text.
162 You should bind this variable with `let', but do not set it globally.")
164 (defvar epa-keys-buffer nil
)
165 (defvar epa-key-buffer-alist nil
)
167 (defvar epa-list-keys-arguments nil
)
168 (defvar epa-info-buffer nil
)
169 (defvar epa-last-coding-system-specified nil
)
171 (defvar epa-key-list-mode-map
172 (let ((keymap (make-sparse-keymap))
173 (menu-map (make-sparse-keymap)))
174 (define-key keymap
"m" 'epa-mark-key
)
175 (define-key keymap
"u" 'epa-unmark-key
)
176 (define-key keymap
"d" 'epa-decrypt-file
)
177 (define-key keymap
"v" 'epa-verify-file
)
178 (define-key keymap
"s" 'epa-sign-file
)
179 (define-key keymap
"e" 'epa-encrypt-file
)
180 (define-key keymap
"r" 'epa-delete-keys
)
181 (define-key keymap
"i" 'epa-import-keys
)
182 (define-key keymap
"o" 'epa-export-keys
)
183 (define-key keymap
"g" 'revert-buffer
)
184 (define-key keymap
"n" 'next-line
)
185 (define-key keymap
"p" 'previous-line
)
186 (define-key keymap
" " 'scroll-up-command
)
187 (define-key keymap
[?\S-\
] 'scroll-down-command
)
188 (define-key keymap
[delete] 'scroll-down-command)
189 (define-key keymap "q" 'epa-exit-buffer)
190 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
191 (define-key menu-map [epa-key-list-unmark-key]
192 '(menu-item "Unmark Key" epa-unmark-key
193 :help "Unmark a key"))
194 (define-key menu-map [epa-key-list-mark-key]
195 '(menu-item "Mark Key" epa-mark-key
197 (define-key menu-map [separator-epa-file] '(menu-item "--"))
198 (define-key menu-map [epa-verify-file]
199 '(menu-item "Verify File..." epa-verify-file
200 :help "Verify FILE"))
201 (define-key menu-map [epa-sign-file]
202 '(menu-item "Sign File..." epa-sign-file
203 :help "Sign FILE by SIGNERS keys selected"))
204 (define-key menu-map [epa-decrypt-file]
205 '(menu-item "Decrypt File..." epa-decrypt-file
206 :help "Decrypt FILE"))
207 (define-key menu-map [epa-encrypt-file]
208 '(menu-item "Encrypt File..." epa-encrypt-file
209 :help "Encrypt FILE for RECIPIENTS"))
210 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
211 (define-key menu-map [epa-key-list-delete-keys]
212 '(menu-item "Delete Keys" epa-delete-keys
213 :help "Delete Marked Keys"))
214 (define-key menu-map [epa-key-list-import-keys]
215 '(menu-item "Import Keys" epa-import-keys
216 :help "Import keys from a file"))
217 (define-key menu-map [epa-key-list-export-keys]
218 '(menu-item "Export Keys" epa-export-keys
219 :help "Export marked keys to a file"))
222 (defvar epa-key-mode-map
223 (let ((keymap (make-sparse-keymap)))
224 (define-key keymap "q" 'epa-exit-buffer)
227 (defvar epa-info-mode-map
228 (let ((keymap (make-sparse-keymap)))
229 (define-key keymap "q" 'delete-window)
232 (defvar epa-exit-buffer-function #'bury-buffer)
234 (define-widget 'epa-key 'push-button
235 "Button for representing a epg-key object."
237 :button-face-get 'epa--key-widget-button-face-get
238 :value-create 'epa--key-widget-value-create
239 :action 'epa--key-widget-action
240 :help-echo 'epa--key-widget-help-echo)
242 (defun epa--key-widget-action (widget &optional _event)
243 (save-selected-window
244 (epa--show-key (widget-get widget :value))))
246 (defun epa--key-widget-value-create (widget)
247 (let* ((key (widget-get widget :value))
248 (primary-sub-key (car (epg-key-sub-key-list key)))
249 (primary-user-id (car (epg-key-user-id-list key))))
250 (insert (format "%c "
251 (if (epg-sub-key-validity primary-sub-key)
252 (car (rassq (epg-sub-key-validity primary-sub-key)
253 epg-key-validity-alist))
255 (epg-sub-key-id primary-sub-key)
258 (if (stringp (epg-user-id-string primary-user-id))
259 (epg-user-id-string primary-user-id)
260 (epg-decode-dn (epg-user-id-string primary-user-id)))
263 (defun epa--key-widget-button-face-get (widget)
264 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
265 (widget-get widget :value))))))
267 (cdr (assq validity epa-validity-face-alist))
270 (defun epa--key-widget-help-echo (widget)
272 (epg-sub-key-id (car (epg-key-sub-key-list
273 (widget-get widget :value))))))
275 (defalias 'epa--encode-coding-string
276 (if (fboundp 'encode-coding-string) #'encode-coding-string #'identity))
278 (defalias 'epa--decode-coding-string
279 (if (fboundp 'decode-coding-string) #'decode-coding-string #'identity))
281 (define-derived-mode epa-key-list-mode special-mode "Keys"
282 "Major mode for `epa-list-keys'."
283 (buffer-disable-undo)
284 (setq truncate-lines t
286 (setq-local font-lock-defaults '(epa-font-lock-keywords t))
287 ;; In XEmacs, auto-initialization of font-lock is not effective
288 ;; if buffer-file-name is not set.
289 (font-lock-set-defaults)
290 (make-local-variable 'epa-exit-buffer-function)
291 (setq-local revert-buffer-function #'epa--key-list-revert-buffer))
293 (define-derived-mode epa-key-mode special-mode "Key"
294 "Major mode for a key description."
295 (buffer-disable-undo)
296 (setq truncate-lines t
298 (setq-local font-lock-defaults '(epa-font-lock-keywords t))
299 ;; In XEmacs, auto-initialization of font-lock is not effective
300 ;; if buffer-file-name is not set.
301 (font-lock-set-defaults)
302 (make-local-variable 'epa-exit-buffer-function))
304 (define-derived-mode epa-info-mode special-mode "Info"
305 "Major mode for `epa-info-buffer'."
306 (buffer-disable-undo)
307 (setq truncate-lines t
310 (defun epa-mark-key (&optional arg)
311 "Mark a key on the current line.
312 If ARG is non-nil, unmark the key."
314 (let ((inhibit-read-only t)
318 (unless (get-text-property (point) 'epa-key)
319 (error "No key on this line"))
320 (setq properties (text-properties-at (point)))
322 (insert (if arg " " "*"))
323 (set-text-properties (1- (point)) (point) properties)
326 (defun epa-unmark-key (&optional arg)
327 "Unmark a key on the current line.
328 If ARG is non-nil, mark the key."
330 (epa-mark-key (not arg)))
332 (defun epa-exit-buffer ()
333 "Exit the current buffer.
334 `epa-exit-buffer-function' is called if it is set."
336 (funcall epa-exit-buffer-function))
338 (defun epa--insert-keys (keys)
341 (narrow-to-region (point) (point))
346 (add-text-properties point (point)
347 (list 'epa-key (car keys)
352 (widget-create 'epa-key :value (car keys))
354 (setq keys (cdr keys))))
355 (add-text-properties (point-min) (point-max)
356 (list 'epa-list-keys t
362 (defun epa--list-keys (name secret)
363 (unless (and epa-keys-buffer
364 (buffer-live-p epa-keys-buffer))
365 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
366 (set-buffer epa-keys-buffer)
368 (let ((inhibit-read-only t)
371 (context (epg-make-context epa-protocol)))
372 (unless (get-text-property point 'epa-list-keys)
373 (setq point (next-single-property-change point 'epa-list-keys)))
376 (or (next-single-property-change point 'epa-list-keys)
379 (epa--insert-keys (epg-list-keys context name secret))
381 (set-keymap-parent (current-local-map) widget-keymap))
382 (make-local-variable 'epa-list-keys-arguments)
383 (setq epa-list-keys-arguments (list name secret))
384 (goto-char (point-min))
385 (pop-to-buffer (current-buffer)))
388 (defun epa-list-keys (&optional name)
389 "List all keys matched with NAME from the public keyring."
391 (if current-prefix-arg
392 (let ((name (read-string "Pattern: "
393 (if epa-list-keys-arguments
394 (car epa-list-keys-arguments)))))
395 (list (if (equal name "") nil name)))
397 (epa--list-keys name nil))
400 (defun epa-list-secret-keys (&optional name)
401 "List all keys matched with NAME from the private keyring."
403 (if current-prefix-arg
404 (let ((name (read-string "Pattern: "
405 (if epa-list-keys-arguments
406 (car epa-list-keys-arguments)))))
407 (list (if (equal name "") nil name)))
409 (epa--list-keys name t))
411 (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm)
412 (apply #'epa--list-keys epa-list-keys-arguments))
414 (defun epa--marked-keys ()
415 (or (with-current-buffer epa-keys-buffer
416 (goto-char (point-min))
418 (while (re-search-forward "^\\*" nil t)
419 (if (setq key (get-text-property (match-beginning 0)
421 (setq keys (cons key keys))))
423 (let ((key (get-text-property (point-at-bol) 'epa-key)))
427 (defun epa--select-keys (prompt keys)
428 (unless (and epa-keys-buffer
429 (buffer-live-p epa-keys-buffer))
430 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
431 (with-current-buffer epa-keys-buffer
433 ;; C-c C-c is the usual way to finish the selection (bug#11159).
434 (define-key (current-local-map) "\C-c\C-c" 'exit-recursive-edit)
435 (let ((inhibit-read-only t)
439 (substitute-command-keys "\
440 - `\\[epa-mark-key]' to mark a key on the line
441 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
443 :notify (lambda (&rest _ignore) (abort-recursive-edit))
445 (substitute-command-keys
446 "Click here or \\[abort-recursive-edit] to cancel")
449 :notify (lambda (&rest _ignore) (exit-recursive-edit))
451 (substitute-command-keys
452 "Click here or \\[exit-recursive-edit] to finish")
455 (epa--insert-keys keys)
457 (set-keymap-parent (current-local-map) widget-keymap)
458 (setq epa-exit-buffer-function #'abort-recursive-edit)
459 (goto-char (point-min))
460 (let ((display-buffer-mark-dedicated 'soft))
461 (pop-to-buffer (current-buffer))))
466 (kill-buffer epa-keys-buffer))))
469 (defun epa-select-keys (context prompt &optional names secret)
470 "Display a user's keyring and ask him to select keys.
471 CONTEXT is an epg-context.
472 PROMPT is a string to prompt with.
473 NAMES is a list of strings to be matched with keys. If it is nil, all
475 If SECRET is non-nil, list secret keys instead of public keys."
476 (let ((keys (epg-list-keys context names secret)))
477 (epa--select-keys prompt keys)))
479 (defun epa--show-key (key)
480 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
481 (entry (assoc (epg-sub-key-id primary-sub-key)
482 epa-key-buffer-alist))
483 (inhibit-read-only t)
487 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
488 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
489 (unless (and (cdr entry)
490 (buffer-live-p (cdr entry)))
491 (setcdr entry (generate-new-buffer
492 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
493 (set-buffer (cdr entry))
495 (make-local-variable 'epa-key)
498 (setq pointer (epg-key-user-id-list key))
502 (if (epg-user-id-validity (car pointer))
504 (car (rassq (epg-user-id-validity (car pointer))
505 epg-key-validity-alist)))
508 (if (stringp (epg-user-id-string (car pointer)))
509 (epg-user-id-string (car pointer))
510 (epg-decode-dn (epg-user-id-string (car pointer))))
512 (setq pointer (cdr pointer)))
513 (setq pointer (epg-key-sub-key-list key))
516 (if (epg-sub-key-validity (car pointer))
518 (car (rassq (epg-sub-key-validity (car pointer))
519 epg-key-validity-alist)))
522 (epg-sub-key-id (car pointer))
525 (epg-sub-key-length (car pointer)))
527 (cdr (assq (epg-sub-key-algorithm (car pointer))
528 epg-pubkey-algorithm-alist))
531 (format-time-string "%Y-%m-%d"
532 (epg-sub-key-creation-time (car pointer)))
533 (error "????-??-??"))
534 (if (epg-sub-key-expiration-time (car pointer))
535 (format (if (time-less-p (current-time)
536 (epg-sub-key-expiration-time
541 (format-time-string "%Y-%m-%d"
542 (epg-sub-key-expiration-time
544 (error "????-??-??")))
547 (mapconcat #'symbol-name
548 (epg-sub-key-capability (car pointer))
551 (epg-sub-key-fingerprint (car pointer))
553 (setq pointer (cdr pointer)))
554 (goto-char (point-min))
555 (pop-to-buffer (current-buffer))))
557 (defun epa-display-info (info)
558 (if epa-popup-info-window
559 (save-selected-window
560 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
561 (setq epa-info-buffer (generate-new-buffer "*Info*")))
562 (if (get-buffer-window epa-info-buffer)
563 (delete-window (get-buffer-window epa-info-buffer)))
564 (with-current-buffer epa-info-buffer
565 (let ((inhibit-read-only t)
570 (goto-char (point-min)))
571 (if (> (window-height)
572 epa-info-window-height)
573 (set-window-buffer (split-window nil (- (window-height)
574 epa-info-window-height))
576 (pop-to-buffer epa-info-buffer)
577 (if (> (window-height) epa-info-window-height)
578 (shrink-window (- (window-height) epa-info-window-height)))))
579 (message "%s" info)))
581 (defun epa-display-verify-result (verify-result)
582 (declare (obsolete epa-display-info "23.1"))
583 (epa-display-info (epg-verify-result-to-string verify-result)))
585 (defun epa-passphrase-callback-function (context key-id handback)
588 (format "Passphrase for symmetric encryption%s: "
589 ;; Add the file name to the prompt, if any.
590 (if (stringp handback)
591 (format " for %s" handback)
593 (eq (epg-context-operation context) 'encrypt))
596 "Passphrase for PIN: "
597 (let ((entry (assoc key-id epg-user-id-alist)))
599 (format "Passphrase for %s %s: " key-id (cdr entry))
600 (format "Passphrase for %s: " key-id)))))))
602 (defun epa-progress-callback-function (_context what _char current total
604 (let ((prompt (or handback
605 (format "Processing %s: " what))))
606 ;; According to gnupg/doc/DETAIL: a "total" of 0 indicates that
607 ;; the total amount is not known. The condition TOTAL && CUR ==
608 ;; TOTAL may be used to detect the end of an operation.
610 (if (= current total)
611 (message "%s...done" prompt)
612 (message "%s...%d%%" prompt
613 (floor (* (/ current (float total)) 100))))
614 (message "%s..." prompt))))
616 (defun epa-read-file-name (input)
617 "Interactively read an output file name based on INPUT file name."
618 (setq input (file-name-sans-extension (expand-file-name input)))
621 (concat "To file (default " (file-name-nondirectory input) ") ")
622 (file-name-directory input)
626 (defun epa-decrypt-file (decrypt-file &optional plain-file)
627 "Decrypt DECRYPT-FILE into PLAIN-FILE.
628 If you do not specify PLAIN-FILE, this functions prompts for the value to use."
630 (let* ((file (read-file-name "File to decrypt: "))
631 (plain (epa-read-file-name file)))
633 (or plain-file (setq plain-file (epa-read-file-name decrypt-file)))
634 (setq decrypt-file (expand-file-name decrypt-file))
635 (let ((context (epg-make-context epa-protocol)))
636 (epg-context-set-passphrase-callback context
637 #'epa-passphrase-callback-function)
638 (epg-context-set-progress-callback context
640 #'epa-progress-callback-function
641 (format "Decrypting %s..."
642 (file-name-nondirectory decrypt-file))))
643 (message "Decrypting %s..." (file-name-nondirectory decrypt-file))
644 (epg-decrypt-file context decrypt-file plain-file)
645 (message "Decrypting %s...wrote %s" (file-name-nondirectory decrypt-file)
646 (file-name-nondirectory plain-file))
647 (if (epg-context-result-for context 'verify)
648 (epa-display-info (epg-verify-result-to-string
649 (epg-context-result-for context 'verify))))))
652 (defun epa-verify-file (file)
654 (interactive "fFile: ")
655 (setq file (expand-file-name file))
656 (let* ((context (epg-make-context epa-protocol))
657 (plain (if (equal (file-name-extension file) "sig")
658 (file-name-sans-extension file))))
659 (epg-context-set-progress-callback context
661 #'epa-progress-callback-function
662 (format "Verifying %s..."
663 (file-name-nondirectory file))))
664 (message "Verifying %s..." (file-name-nondirectory file))
665 (epg-verify-file context file plain)
666 (message "Verifying %s...done" (file-name-nondirectory file))
667 (if (epg-context-result-for context 'verify)
668 (epa-display-info (epg-verify-result-to-string
669 (epg-context-result-for context 'verify))))))
671 (defun epa--read-signature-type ()
674 (message "Signature type (n,c,d,?) ")
679 (setq type 'detached))
681 (with-output-to-temp-buffer "*Help*"
682 (with-current-buffer standard-output
684 n - Create a normal signature
685 c - Create a cleartext signature
686 d - Create a detached signature
690 (setq type 'normal))))
694 (defun epa-sign-file (file signers mode)
695 "Sign FILE by SIGNERS keys selected."
697 (let ((verbose current-prefix-arg))
698 (list (expand-file-name (read-file-name "File: "))
700 (epa-select-keys (epg-make-context epa-protocol)
701 "Select keys for signing.
702 If no one is selected, default secret key is used. "
705 (epa--read-signature-type)
707 (let ((signature (concat file
708 (if (eq epa-protocol 'OpenPGP)
711 '(nil t normal detached))))
713 (if (memq mode '(t detached))
716 (if (memq mode '(t detached))
719 (context (epg-make-context epa-protocol)))
720 (epg-context-set-armor context epa-armor)
721 (epg-context-set-textmode context epa-textmode)
722 (epg-context-set-signers context signers)
723 (epg-context-set-passphrase-callback context
724 #'epa-passphrase-callback-function)
725 (epg-context-set-progress-callback context
727 #'epa-progress-callback-function
728 (format "Signing %s..."
729 (file-name-nondirectory file))))
730 (message "Signing %s..." (file-name-nondirectory file))
731 (epg-sign-file context file signature mode)
732 (message "Signing %s...wrote %s" (file-name-nondirectory file)
733 (file-name-nondirectory signature))))
736 (defun epa-encrypt-file (file recipients)
737 "Encrypt FILE for RECIPIENTS."
739 (list (expand-file-name (read-file-name "File: "))
740 (epa-select-keys (epg-make-context epa-protocol)
741 "Select recipients for encryption.
742 If no one is selected, symmetric encryption will be performed. ")))
743 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
744 (if epa-armor ".asc" ".gpg")
746 (context (epg-make-context epa-protocol)))
747 (epg-context-set-armor context epa-armor)
748 (epg-context-set-textmode context epa-textmode)
749 (epg-context-set-passphrase-callback context
750 #'epa-passphrase-callback-function)
751 (epg-context-set-progress-callback context
753 #'epa-progress-callback-function
754 (format "Encrypting %s..."
755 (file-name-nondirectory file))))
756 (message "Encrypting %s..." (file-name-nondirectory file))
757 (epg-encrypt-file context file recipients cipher)
758 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
759 (file-name-nondirectory cipher))))
762 (defun epa-decrypt-region (start end &optional make-buffer-function)
763 "Decrypt the current region between START and END.
765 If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
766 It should return that buffer. If it copies the input, it should
767 delete the text now being decrypted. It should leave point at the
768 proper place to insert the plaintext.
770 Be careful about using this command in Lisp programs!
771 Since this function operates on regions, it does some tricks such
772 as coding-system detection and unibyte/multibyte conversion. If
773 you are sure how the data in the region should be treated, you
774 should consider using the string based counterpart
775 `epg-decrypt-string', or the file based counterpart
776 `epg-decrypt-file' instead.
780 \(let ((context (epg-make-context 'OpenPGP)))
781 (decode-coding-string
782 (epg-decrypt-string context (buffer-substring start end))
786 (let ((context (epg-make-context epa-protocol))
788 (epg-context-set-passphrase-callback context
789 #'epa-passphrase-callback-function)
790 (epg-context-set-progress-callback context
792 #'epa-progress-callback-function
794 (message "Decrypting...")
795 (setq plain (epg-decrypt-string context (buffer-substring start end)))
796 (message "Decrypting...done")
797 (setq plain (epa--decode-coding-string
799 (or coding-system-for-read
800 (get-text-property start 'epa-coding-system-used)
802 (if make-buffer-function
803 (with-current-buffer (funcall make-buffer-function)
804 (let ((inhibit-read-only t))
806 (if (y-or-n-p "Replace the original text? ")
807 (let ((inhibit-read-only t))
808 (delete-region start end)
811 (with-output-to-temp-buffer "*Temp*"
812 (set-buffer standard-output)
815 (if (epg-context-result-for context 'verify)
816 (epa-display-info (epg-verify-result-to-string
817 (epg-context-result-for context 'verify)))))))
819 (defun epa--find-coding-system-for-mime-charset (mime-charset)
820 (if (featurep 'xemacs)
821 (if (fboundp 'find-coding-system)
822 (find-coding-system mime-charset))
823 ;; Find the first coding system which corresponds to MIME-CHARSET.
824 (let ((pointer (coding-system-list)))
826 (not (eq (coding-system-get (car pointer) 'mime-charset)
828 (setq pointer (cdr pointer)))
832 (defun epa-decrypt-armor-in-region (start end)
833 "Decrypt OpenPGP armors in the current region between START and END.
835 Don't use this command in Lisp programs!
836 See the reason described in the `epa-decrypt-region' documentation."
840 (narrow-to-region start end)
842 (let (armor-start armor-end)
843 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
844 (setq armor-start (match-beginning 0)
845 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
848 (error "Encryption armor beginning has no matching end"))
849 (goto-char armor-start)
850 (let ((coding-system-for-read
851 (or coding-system-for-read
852 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
853 (epa--find-coding-system-for-mime-charset
854 (intern (downcase (match-string 1))))))))
855 (goto-char armor-end)
856 (epa-decrypt-region armor-start armor-end)))))))
859 (defun epa-verify-region (start end)
860 "Verify the current region between START and END.
862 Don't use this command in Lisp programs!
863 Since this function operates on regions, it does some tricks such
864 as coding-system detection and unibyte/multibyte conversion. If
865 you are sure how the data in the region should be treated, you
866 should consider using the string based counterpart
867 `epg-verify-string', or the file based counterpart
868 `epg-verify-file' instead.
872 \(let ((context (epg-make-context 'OpenPGP)))
873 (decode-coding-string
874 (epg-verify-string context (buffer-substring start end))
877 (let ((context (epg-make-context epa-protocol))
879 (epg-context-set-progress-callback context
881 #'epa-progress-callback-function
883 (message "Verifying...")
884 (setq plain (epg-verify-string
886 (epa--encode-coding-string
887 (buffer-substring start end)
888 (or coding-system-for-write
889 (get-text-property start 'epa-coding-system-used)))))
890 (message "Verifying...done")
891 (setq plain (epa--decode-coding-string
893 (or coding-system-for-read
894 (get-text-property start 'epa-coding-system-used)
896 (if (y-or-n-p "Replace the original text? ")
897 (let ((inhibit-read-only t)
899 (delete-region start end)
902 (with-output-to-temp-buffer "*Temp*"
903 (set-buffer standard-output)
906 (if (epg-context-result-for context 'verify)
907 (epa-display-info (epg-verify-result-to-string
908 (epg-context-result-for context 'verify))))))
911 (defun epa-verify-cleartext-in-region (start end)
912 "Verify OpenPGP cleartext signed messages in the current region
913 between START and END.
915 Don't use this command in Lisp programs!
916 See the reason described in the `epa-verify-region' documentation."
920 (narrow-to-region start end)
922 (let (cleartext-start cleartext-end)
923 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
925 (setq cleartext-start (match-beginning 0))
926 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
928 (error "Invalid cleartext signed message"))
929 (setq cleartext-end (re-search-forward
930 "^-----END PGP SIGNATURE-----$"
932 (unless cleartext-end
933 (error "No cleartext tail"))
934 (epa-verify-region cleartext-start cleartext-end))))))
936 (defalias 'epa--select-safe-coding-system
937 (if (fboundp 'select-safe-coding-system)
938 #'select-safe-coding-system
940 buffer-file-coding-system)))
943 (defun epa-sign-region (start end signers mode)
944 "Sign the current region between START and END by SIGNERS keys selected.
946 Don't use this command in Lisp programs!
947 Since this function operates on regions, it does some tricks such
948 as coding-system detection and unibyte/multibyte conversion. If
949 you are sure how the data should be treated, you should consider
950 using the string based counterpart `epg-sign-string', or the file
951 based counterpart `epg-sign-file' instead.
955 \(let ((context (epg-make-context 'OpenPGP)))
958 (encode-coding-string (buffer-substring start end) 'utf-8)))"
960 (let ((verbose current-prefix-arg))
961 (setq epa-last-coding-system-specified
962 (or coding-system-for-write
963 (epa--select-safe-coding-system
964 (region-beginning) (region-end))))
965 (list (region-beginning) (region-end)
967 (epa-select-keys (epg-make-context epa-protocol)
968 "Select keys for signing.
969 If no one is selected, default secret key is used. "
972 (epa--read-signature-type)
975 (let ((context (epg-make-context epa-protocol))
977 ;;(epg-context-set-armor context epa-armor)
978 (epg-context-set-armor context t)
979 ;;(epg-context-set-textmode context epa-textmode)
980 (epg-context-set-textmode context t)
981 (epg-context-set-signers context signers)
982 (epg-context-set-passphrase-callback context
983 #'epa-passphrase-callback-function)
984 (epg-context-set-progress-callback context
986 #'epa-progress-callback-function
988 (message "Signing...")
989 (setq signature (epg-sign-string context
990 (epa--encode-coding-string
991 (buffer-substring start end)
992 epa-last-coding-system-specified)
994 (message "Signing...done")
995 (delete-region start end)
997 (add-text-properties (point)
999 (insert (epa--decode-coding-string
1001 (or coding-system-for-read
1002 epa-last-coding-system-specified)))
1004 (list 'epa-coding-system-used
1005 epa-last-coding-system-specified
1011 (defalias 'epa--derived-mode-p
1012 (if (fboundp 'derived-mode-p)
1014 (lambda (&rest modes)
1015 "Non-nil if the current major mode is derived from one of MODES.
1016 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1017 (let ((parent major-mode))
1018 (while (and (not (memq parent modes))
1019 (setq parent (get parent 'derived-mode-parent))))
1023 (defun epa-encrypt-region (start end recipients sign signers)
1024 "Encrypt the current region between START and END for RECIPIENTS.
1026 Don't use this command in Lisp programs!
1027 Since this function operates on regions, it does some tricks such
1028 as coding-system detection and unibyte/multibyte conversion. If
1029 you are sure how the data should be treated, you should consider
1030 using the string based counterpart `epg-encrypt-string', or the
1031 file based counterpart `epg-encrypt-file' instead.
1035 \(let ((context (epg-make-context 'OpenPGP)))
1038 (encode-coding-string (buffer-substring start end) 'utf-8)
1041 (let ((verbose current-prefix-arg)
1042 (context (epg-make-context epa-protocol))
1044 (setq epa-last-coding-system-specified
1045 (or coding-system-for-write
1046 (epa--select-safe-coding-system
1047 (region-beginning) (region-end))))
1048 (list (region-beginning) (region-end)
1049 (epa-select-keys context
1050 "Select recipients for encryption.
1051 If no one is selected, symmetric encryption will be performed. ")
1052 (setq sign (if verbose (y-or-n-p "Sign? ")))
1054 (epa-select-keys context
1055 "Select keys for signing. ")))))
1057 (let ((context (epg-make-context epa-protocol))
1059 ;;(epg-context-set-armor context epa-armor)
1060 (epg-context-set-armor context t)
1061 ;;(epg-context-set-textmode context epa-textmode)
1062 (epg-context-set-textmode context t)
1064 (epg-context-set-signers context signers))
1065 (epg-context-set-passphrase-callback context
1066 #'epa-passphrase-callback-function)
1067 (epg-context-set-progress-callback context
1069 #'epa-progress-callback-function
1071 (message "Encrypting...")
1072 (setq cipher (epg-encrypt-string context
1073 (epa--encode-coding-string
1074 (buffer-substring start end)
1075 epa-last-coding-system-specified)
1078 (message "Encrypting...done")
1079 (delete-region start end)
1081 (add-text-properties (point)
1085 (list 'epa-coding-system-used
1086 epa-last-coding-system-specified
1093 (defun epa-delete-keys (keys &optional allow-secret)
1094 "Delete selected KEYS."
1096 (let ((keys (epa--marked-keys)))
1098 (error "No keys selected"))
1100 (eq (nth 1 epa-list-keys-arguments) t))))
1101 (let ((context (epg-make-context epa-protocol)))
1102 (message "Deleting...")
1103 (epg-delete-keys context keys allow-secret)
1104 (message "Deleting...done")
1105 (apply #'epa--list-keys epa-list-keys-arguments)))
1108 (defun epa-import-keys (file)
1109 "Import keys from FILE."
1110 (interactive "fFile: ")
1111 (setq file (expand-file-name file))
1112 (let ((context (epg-make-context epa-protocol)))
1113 (message "Importing %s..." (file-name-nondirectory file))
1116 (epg-import-keys-from-file context file)
1117 (message "Importing %s...done" (file-name-nondirectory file)))
1119 (message "Importing %s...failed" (file-name-nondirectory file))))
1120 (if (epg-context-result-for context 'import)
1121 (epa-display-info (epg-import-result-to-string
1122 (epg-context-result-for context 'import))))
1123 ;; FIXME: Why not use the (otherwise unused) epa--derived-mode-p?
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