1 ;;; epa.el --- the EasyPG Assistant -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006-2017 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 <https://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'wid-edit
))
32 "The EasyPG Assistant"
34 :link
'(custom-manual "(epa) Top")
37 (defcustom epa-replace-original-text
'ask
38 "Whether the original text shall be replaced by the decrypted.
40 If t, replace the original text without any confirmation.
41 If nil, don't replace the original text and show the result in a new buffer.
42 If neither t nor nil, ask user for confirmation."
44 :type
'(choice (const :tag
"Never" nil
)
45 (const :tag
"Ask the user" ask
)
46 (const :tag
"Always" t
))
49 (defcustom epa-popup-info-window t
50 "If non-nil, display status information from epa commands in another window."
54 (defcustom epa-info-window-height
5
55 "Number of lines used to display status information."
59 (defcustom epa-pinentry-mode nil
62 GnuPG 2.1 or later has an option to control the behavior of
63 Pinentry invocation. Possible modes are: `ask', `cancel',
64 `error', and `loopback'. See the GnuPG manual for the meanings.
66 In epa commands, a particularly useful mode is `loopback', which
67 redirects all Pinentry queries to the caller, so Emacs can query
68 passphrase through the minibuffer, instead of external Pinentry
70 :type
'(choice (const nil
)
78 (defgroup epa-faces nil
83 (defcustom epa-mail-aliases nil
84 "Alist of aliases of email addresses that stand for encryption keys.
85 Each element is a list of email addresses (ALIAS EXPANSIONS...).
86 When one of the recipients of a message being encrypted is ALIAS,
87 instead of encrypting it for ALIAS, encrypt it for EXPANSIONS...
89 If EXPANSIONS is empty, ignore ALIAS as regards encryption.
90 This is a handy way to avoid warnings about addresses that you don't
93 The command `epa-mail-encrypt' uses this."
94 :type
'(repeat (cons (string :tag
"Alias") (repeat (string :tag
"Expansion"))))
98 (defface epa-validity-high
99 '((default :weight bold
)
100 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
101 "Face for high validity EPA information."
104 (defface epa-validity-medium
105 '((default :slant italic
)
106 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
107 "Face for medium validity EPA information."
110 (defface epa-validity-low
112 "Face used for displaying the low validity."
115 (defface epa-validity-disabled
116 '((t :slant italic
:inverse-video t
))
117 "Face used for displaying the disabled validity."
121 '((((class color
) (background dark
))
122 :foreground
"lightyellow")
123 (((class color
) (background light
))
124 :foreground
"blue4"))
125 "Face used for displaying the string."
129 '((default :weight bold
)
130 (((class color
) (background dark
)) :foreground
"orange")
131 (((class color
) (background light
)) :foreground
"red"))
132 "Face used for displaying the high validity."
135 (defface epa-field-name
136 '((default :weight bold
)
137 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
138 "Face for the name of the attribute field."
141 (defface epa-field-body
142 '((default :slant italic
)
143 (((class color
) (background dark
)) :foreground
"turquoise"))
144 "Face for the body of the attribute field."
147 (defcustom epa-validity-face-alist
148 '((unknown . epa-validity-disabled
)
149 (invalid . epa-validity-disabled
)
150 (disabled . epa-validity-disabled
)
151 (revoked . epa-validity-disabled
)
152 (expired . epa-validity-disabled
)
153 (none . epa-validity-low
)
154 (undefined . epa-validity-low
)
155 (never . epa-validity-low
)
156 (marginal . epa-validity-medium
)
157 (full . epa-validity-high
)
158 (ultimate . epa-validity-high
))
159 "An alist mapping validity values to faces."
160 :type
'(repeat (cons symbol face
))
163 (defvar epa-font-lock-keywords
166 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
168 (2 'epa-field-body
)))
169 "Default expressions to addon in epa-mode.")
171 (defconst epa-pubkey-algorithm-letter-alist
179 (defvar epa-protocol
'OpenPGP
180 "The default protocol.
181 The value can be either OpenPGP or CMS.
183 You should bind this variable with `let', but do not set it globally.")
185 (defvar epa-armor nil
186 "If non-nil, epa commands create ASCII armored output.
188 You should bind this variable with `let', but do not set it globally.")
190 (defvar epa-textmode nil
191 "If non-nil, epa commands treat input files as text.
193 You should bind this variable with `let', but do not set it globally.")
195 (defvar epa-keys-buffer nil
)
196 (defvar epa-key-buffer-alist nil
)
198 (defvar epa-list-keys-arguments nil
)
199 (defvar epa-info-buffer nil
)
200 (defvar epa-error-buffer nil
)
201 (defvar epa-last-coding-system-specified nil
)
203 (defvar epa-key-list-mode-map
204 (let ((keymap (make-sparse-keymap))
205 (menu-map (make-sparse-keymap)))
206 (define-key keymap
"m" 'epa-mark-key
)
207 (define-key keymap
"u" 'epa-unmark-key
)
208 (define-key keymap
"d" 'epa-decrypt-file
)
209 (define-key keymap
"v" 'epa-verify-file
)
210 (define-key keymap
"s" 'epa-sign-file
)
211 (define-key keymap
"e" 'epa-encrypt-file
)
212 (define-key keymap
"r" 'epa-delete-keys
)
213 (define-key keymap
"i" 'epa-import-keys
)
214 (define-key keymap
"o" 'epa-export-keys
)
215 (define-key keymap
"g" 'revert-buffer
)
216 (define-key keymap
"n" 'next-line
)
217 (define-key keymap
"p" 'previous-line
)
218 (define-key keymap
" " 'scroll-up-command
)
219 (define-key keymap
[?\S-\
] 'scroll-down-command
)
220 (define-key keymap
[delete] 'scroll-down-command)
221 (define-key keymap "q" 'epa-exit-buffer)
222 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
223 (define-key menu-map [epa-key-list-unmark-key]
224 '(menu-item "Unmark Key" epa-unmark-key
225 :help "Unmark a key"))
226 (define-key menu-map [epa-key-list-mark-key]
227 '(menu-item "Mark Key" epa-mark-key
229 (define-key menu-map [separator-epa-file] '(menu-item "--"))
230 (define-key menu-map [epa-verify-file]
231 '(menu-item "Verify File..." epa-verify-file
232 :help "Verify FILE"))
233 (define-key menu-map [epa-sign-file]
234 '(menu-item "Sign File..." epa-sign-file
235 :help "Sign FILE by SIGNERS keys selected"))
236 (define-key menu-map [epa-decrypt-file]
237 '(menu-item "Decrypt File..." epa-decrypt-file
238 :help "Decrypt FILE"))
239 (define-key menu-map [epa-encrypt-file]
240 '(menu-item "Encrypt File..." epa-encrypt-file
241 :help "Encrypt FILE for RECIPIENTS"))
242 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
243 (define-key menu-map [epa-key-list-delete-keys]
244 '(menu-item "Delete Keys" epa-delete-keys
245 :help "Delete Marked Keys"))
246 (define-key menu-map [epa-key-list-import-keys]
247 '(menu-item "Import Keys" epa-import-keys
248 :help "Import keys from a file"))
249 (define-key menu-map [epa-key-list-export-keys]
250 '(menu-item "Export Keys" epa-export-keys
251 :help "Export marked keys to a file"))
254 (defvar epa-key-mode-map
255 (let ((keymap (make-sparse-keymap)))
256 (define-key keymap "q" 'epa-exit-buffer)
259 (defvar epa-info-mode-map
260 (let ((keymap (make-sparse-keymap)))
261 (define-key keymap "q" 'delete-window)
264 (defvar epa-exit-buffer-function #'quit-window)
266 (define-widget 'epa-key 'push-button
267 "Button for representing a epg-key object."
269 :button-face-get 'epa--key-widget-button-face-get
270 :value-create 'epa--key-widget-value-create
271 :action 'epa--key-widget-action
272 :help-echo 'epa--key-widget-help-echo)
274 (defun epa--key-widget-action (widget &optional _event)
275 (save-selected-window
276 (epa--show-key (widget-get widget :value))))
278 (defun epa--key-widget-value-create (widget)
279 (let* ((key (widget-get widget :value))
280 (primary-sub-key (car (epg-key-sub-key-list key)))
281 (primary-user-id (car (epg-key-user-id-list key))))
282 (insert (format "%c "
283 (if (epg-sub-key-validity primary-sub-key)
284 (car (rassq (epg-sub-key-validity primary-sub-key)
285 epg-key-validity-alist))
287 (epg-sub-key-id primary-sub-key)
290 (if (stringp (epg-user-id-string primary-user-id))
291 (epg-user-id-string primary-user-id)
292 (epg-decode-dn (epg-user-id-string primary-user-id)))
295 (defun epa--key-widget-button-face-get (widget)
296 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
297 (widget-get widget :value))))))
299 (cdr (assq validity epa-validity-face-alist))
302 (defun epa--key-widget-help-echo (widget)
304 (epg-sub-key-id (car (epg-key-sub-key-list
305 (widget-get widget :value))))))
307 (defalias 'epa--encode-coding-string
308 (if (fboundp 'encode-coding-string) #'encode-coding-string #'identity))
310 (defalias 'epa--decode-coding-string
311 (if (fboundp 'decode-coding-string) #'decode-coding-string #'identity))
313 (define-derived-mode epa-key-list-mode special-mode "Keys"
314 "Major mode for `epa-list-keys'."
315 (buffer-disable-undo)
316 (setq truncate-lines t
318 (setq-local font-lock-defaults '(epa-font-lock-keywords t))
319 ;; In XEmacs, auto-initialization of font-lock is not effective
320 ;; if buffer-file-name is not set.
321 (font-lock-set-defaults)
322 (make-local-variable 'epa-exit-buffer-function)
323 (setq-local revert-buffer-function #'epa--key-list-revert-buffer))
325 (define-derived-mode epa-key-mode special-mode "Key"
326 "Major mode for a key description."
327 (buffer-disable-undo)
328 (setq truncate-lines t
330 (setq-local font-lock-defaults '(epa-font-lock-keywords t))
331 ;; In XEmacs, auto-initialization of font-lock is not effective
332 ;; if buffer-file-name is not set.
333 (font-lock-set-defaults)
334 (make-local-variable 'epa-exit-buffer-function))
336 (define-derived-mode epa-info-mode special-mode "Info"
337 "Major mode for `epa-info-buffer'."
338 (buffer-disable-undo)
339 (setq truncate-lines t
342 (defun epa-mark-key (&optional arg)
343 "Mark a key on the current line.
344 If ARG is non-nil, unmark the key."
346 (let ((inhibit-read-only t)
350 (unless (get-text-property (point) 'epa-key)
351 (error "No key on this line"))
352 (setq properties (text-properties-at (point)))
354 (insert (if arg " " "*"))
355 (set-text-properties (1- (point)) (point) properties)
358 (defun epa-unmark-key (&optional arg)
359 "Unmark a key on the current line.
360 If ARG is non-nil, mark the key."
362 (epa-mark-key (not arg)))
364 (defun epa-exit-buffer ()
365 "Exit the current buffer.
366 `epa-exit-buffer-function' is called if it is set."
368 (funcall epa-exit-buffer-function))
370 (defun epa--insert-keys (keys)
373 (narrow-to-region (point) (point))
378 (add-text-properties point (point)
379 (list 'epa-key (car keys)
384 (widget-create 'epa-key :value (car keys))
386 (setq keys (cdr keys))))
387 (add-text-properties (point-min) (point-max)
388 (list 'epa-list-keys t
394 (defun epa--list-keys (name secret)
395 (unless (and epa-keys-buffer
396 (buffer-live-p epa-keys-buffer))
397 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
398 (set-buffer epa-keys-buffer)
400 (let ((inhibit-read-only t)
403 (context (epg-make-context epa-protocol)))
404 (unless (get-text-property point 'epa-list-keys)
405 (setq point (next-single-property-change point 'epa-list-keys)))
408 (or (next-single-property-change point 'epa-list-keys)
411 (epa--insert-keys (epg-list-keys context name secret))
413 (set-keymap-parent (current-local-map) widget-keymap))
414 (make-local-variable 'epa-list-keys-arguments)
415 (setq epa-list-keys-arguments (list name secret))
416 (goto-char (point-min))
417 (pop-to-buffer (current-buffer)))
420 (defun epa-list-keys (&optional name)
421 "List all keys matched with NAME from the public keyring."
423 (if current-prefix-arg
424 (let ((name (read-string "Pattern: "
425 (if epa-list-keys-arguments
426 (car epa-list-keys-arguments)))))
427 (list (if (equal name "") nil name)))
429 (epa--list-keys name nil))
432 (defun epa-list-secret-keys (&optional name)
433 "List all keys matched with NAME from the private keyring."
435 (if current-prefix-arg
436 (let ((name (read-string "Pattern: "
437 (if epa-list-keys-arguments
438 (car epa-list-keys-arguments)))))
439 (list (if (equal name "") nil name)))
441 (epa--list-keys name t))
443 (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm)
444 (apply #'epa--list-keys epa-list-keys-arguments))
446 (defun epa--marked-keys ()
447 (or (with-current-buffer epa-keys-buffer
448 (goto-char (point-min))
450 (while (re-search-forward "^\\*" nil t)
451 (if (setq key (get-text-property (match-beginning 0)
453 (setq keys (cons key keys))))
455 (let ((key (get-text-property (point-at-bol) 'epa-key)))
459 (defun epa--select-keys (prompt keys)
460 (unless (and epa-keys-buffer
461 (buffer-live-p epa-keys-buffer))
462 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
463 (with-current-buffer epa-keys-buffer
465 ;; C-c C-c is the usual way to finish the selection (bug#11159).
466 (define-key (current-local-map) "\C-c\C-c" 'exit-recursive-edit)
467 (let ((inhibit-read-only t)
471 (substitute-command-keys "\
472 - `\\[epa-mark-key]' to mark a key on the line
473 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
475 :notify (lambda (&rest _ignore) (abort-recursive-edit))
477 "Click here or \\[abort-recursive-edit] to cancel"
480 :notify (lambda (&rest _ignore) (exit-recursive-edit))
482 "Click here or \\[exit-recursive-edit] to finish"
485 (epa--insert-keys keys)
487 (set-keymap-parent (current-local-map) widget-keymap)
488 (setq epa-exit-buffer-function #'abort-recursive-edit)
489 (goto-char (point-min))
490 (let ((display-buffer-mark-dedicated 'soft))
491 (pop-to-buffer (current-buffer))))
496 (kill-buffer epa-keys-buffer))))
499 (defun epa-select-keys (context prompt &optional names secret)
500 "Display a user's keyring and ask him to select keys.
501 CONTEXT is an epg-context.
502 PROMPT is a string to prompt with.
503 NAMES is a list of strings to be matched with keys. If it is nil, all
505 If SECRET is non-nil, list secret keys instead of public keys."
506 (let ((keys (epg-list-keys context names secret)))
507 (epa--select-keys prompt keys)))
509 (defun epa--show-key (key)
510 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
511 (entry (assoc (epg-sub-key-id primary-sub-key)
512 epa-key-buffer-alist))
513 (inhibit-read-only t)
517 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
518 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
519 (unless (and (cdr entry)
520 (buffer-live-p (cdr entry)))
521 (setcdr entry (generate-new-buffer
522 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
523 (set-buffer (cdr entry))
525 (make-local-variable 'epa-key)
528 (setq pointer (epg-key-user-id-list key))
532 (if (epg-user-id-validity (car pointer))
534 (car (rassq (epg-user-id-validity (car pointer))
535 epg-key-validity-alist)))
538 (if (stringp (epg-user-id-string (car pointer)))
539 (epg-user-id-string (car pointer))
540 (epg-decode-dn (epg-user-id-string (car pointer))))
542 (setq pointer (cdr pointer)))
543 (setq pointer (epg-key-sub-key-list key))
546 (if (epg-sub-key-validity (car pointer))
548 (car (rassq (epg-sub-key-validity (car pointer))
549 epg-key-validity-alist)))
552 (epg-sub-key-id (car pointer))
555 (epg-sub-key-length (car pointer)))
557 (cdr (assq (epg-sub-key-algorithm (car pointer))
558 epg-pubkey-algorithm-alist))
561 (format-time-string "%Y-%m-%d"
562 (epg-sub-key-creation-time (car pointer)))
563 (error "????-??-??"))
564 (if (epg-sub-key-expiration-time (car pointer))
565 (format (if (time-less-p (current-time)
566 (epg-sub-key-expiration-time
571 (format-time-string "%Y-%m-%d"
572 (epg-sub-key-expiration-time
574 (error "????-??-??")))
577 (mapconcat #'symbol-name
578 (epg-sub-key-capability (car pointer))
581 (epg-sub-key-fingerprint (car pointer))
583 (setq pointer (cdr pointer)))
584 (goto-char (point-min))
585 (pop-to-buffer (current-buffer))))
587 (defun epa-display-info (info)
588 (if epa-popup-info-window
589 (save-selected-window
590 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
591 (setq epa-info-buffer (generate-new-buffer "*Info*")))
592 (if (get-buffer-window epa-info-buffer)
593 (delete-window (get-buffer-window epa-info-buffer)))
594 (with-current-buffer epa-info-buffer
595 (let ((inhibit-read-only t)
600 (goto-char (point-min)))
601 (if (> (window-height)
602 epa-info-window-height)
603 (set-window-buffer (split-window nil (- (window-height)
604 epa-info-window-height))
606 (pop-to-buffer epa-info-buffer)
607 (if (> (window-height) epa-info-window-height)
608 (shrink-window (- (window-height) epa-info-window-height)))))
609 (message "%s" info)))
611 (defun epa-display-error (context)
612 (unless (equal (epg-context-error-output context) "")
613 (let ((buffer (get-buffer-create "*Error*")))
614 (save-selected-window
615 (unless (and epa-error-buffer (buffer-live-p epa-error-buffer))
616 (setq epa-error-buffer (generate-new-buffer "*Error*")))
617 (if (get-buffer-window epa-error-buffer)
618 (delete-window (get-buffer-window epa-error-buffer)))
619 (with-current-buffer buffer
620 (let ((inhibit-read-only t)
624 (pcase (epg-context-operation context)
625 (`decrypt "Error while decrypting with \"%s\":")
626 (`verify "Error while verifying with \"%s\":")
627 (`sign "Error while signing with \"%s\":")
628 (`encrypt "Error while encrypting with \"%s\":")
629 (`import-keys "Error while importing keys with \"%s\":")
630 (`export-keys "Error while exporting keys with \"%s\":")
631 (_ "Error while executing \"%s\":\n\n"))
632 (epg-context-program context))
634 (epg-context-error-output context)))
636 (goto-char (point-min)))
637 (display-buffer buffer)))))
639 (defun epa-display-verify-result (verify-result)
640 (declare (obsolete epa-display-info "23.1"))
641 (epa-display-info (epg-verify-result-to-string verify-result)))
643 (defun epa-passphrase-callback-function (context key-id handback)
646 (format "Passphrase for symmetric encryption%s: "
647 ;; Add the file name to the prompt, if any.
648 (if (stringp handback)
649 (format " for %s" handback)
651 (eq (epg-context-operation context) 'encrypt))
654 "Passphrase for PIN: "
655 (let ((entry (assoc key-id epg-user-id-alist)))
657 (format "Passphrase for %s %s: " key-id (cdr entry))
658 (format "Passphrase for %s: " key-id)))))))
660 (defun epa-progress-callback-function (_context what _char current total
662 (let ((prompt (or handback
663 (format "Processing %s: " what))))
664 ;; According to gnupg/doc/DETAIL: a "total" of 0 indicates that
665 ;; the total amount is not known. The condition TOTAL && CUR ==
666 ;; TOTAL may be used to detect the end of an operation.
668 (if (= current total)
669 (message "%s...done" prompt)
670 (message "%s...%d%%" prompt
671 (floor (* 100.0 current) total)))
672 (message "%s..." prompt))))
674 (defun epa-read-file-name (input)
675 "Interactively read an output file name based on INPUT file name."
676 (setq input (file-name-sans-extension (expand-file-name input)))
679 (concat "To file (default " (file-name-nondirectory input) ") ")
680 (file-name-directory input)
684 (defun epa-decrypt-file (decrypt-file &optional plain-file)
685 "Decrypt DECRYPT-FILE into PLAIN-FILE.
686 If you do not specify PLAIN-FILE, this functions prompts for the value to use."
688 (let* ((file (read-file-name "File to decrypt: "))
689 (plain (epa-read-file-name file)))
691 (or plain-file (setq plain-file (epa-read-file-name decrypt-file)))
692 (setq decrypt-file (expand-file-name decrypt-file))
693 (let ((context (epg-make-context epa-protocol)))
694 (epg-context-set-passphrase-callback context
695 #'epa-passphrase-callback-function)
696 (epg-context-set-progress-callback context
698 #'epa-progress-callback-function
699 (format "Decrypting %s..."
700 (file-name-nondirectory decrypt-file))))
701 (message "Decrypting %s..." (file-name-nondirectory decrypt-file))
702 (condition-case error
703 (epg-decrypt-file context decrypt-file plain-file)
705 (epa-display-error context)
706 (signal (car error) (cdr error))))
707 (message "Decrypting %s...wrote %s" (file-name-nondirectory decrypt-file)
708 (file-name-nondirectory plain-file))
709 (if (epg-context-result-for context 'verify)
710 (epa-display-info (epg-verify-result-to-string
711 (epg-context-result-for context 'verify))))))
714 (defun epa-verify-file (file)
716 (interactive "fFile: ")
717 (setq file (expand-file-name file))
718 (let* ((context (epg-make-context epa-protocol))
719 (plain (if (equal (file-name-extension file) "sig")
720 (file-name-sans-extension file))))
721 (epg-context-set-progress-callback context
723 #'epa-progress-callback-function
724 (format "Verifying %s..."
725 (file-name-nondirectory file))))
726 (message "Verifying %s..." (file-name-nondirectory file))
727 (condition-case error
728 (epg-verify-file context file plain)
730 (epa-display-error context)
731 (signal (car error) (cdr error))))
732 (message "Verifying %s...done" (file-name-nondirectory file))
733 (if (epg-context-result-for context 'verify)
734 (epa-display-info (epg-verify-result-to-string
735 (epg-context-result-for context 'verify))))))
737 (defun epa--read-signature-type ()
740 (message "Signature type (n,c,d,?) ")
745 (setq type 'detached))
747 (with-output-to-temp-buffer "*Help*"
748 (with-current-buffer standard-output
750 n - Create a normal signature
751 c - Create a cleartext signature
752 d - Create a detached signature
756 (setq type 'normal))))
760 (defun epa-sign-file (file signers mode)
761 "Sign FILE by SIGNERS keys selected."
763 (let ((verbose current-prefix-arg))
764 (list (expand-file-name (read-file-name "File: "))
766 (epa-select-keys (epg-make-context epa-protocol)
767 "Select keys for signing.
768 If no one is selected, default secret key is used. "
771 (epa--read-signature-type)
773 (let ((signature (concat file
774 (if (eq epa-protocol 'OpenPGP)
777 '(nil t normal detached))))
779 (if (memq mode '(t detached))
782 (if (memq mode '(t detached))
785 (context (epg-make-context epa-protocol)))
786 (setf (epg-context-armor context) epa-armor)
787 (setf (epg-context-textmode context) epa-textmode)
788 (setf (epg-context-signers context) signers)
789 (epg-context-set-passphrase-callback context
790 #'epa-passphrase-callback-function)
791 (epg-context-set-progress-callback context
793 #'epa-progress-callback-function
794 (format "Signing %s..."
795 (file-name-nondirectory file))))
796 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
797 (message "Signing %s..." (file-name-nondirectory file))
798 (condition-case error
799 (epg-sign-file context file signature mode)
801 (epa-display-error context)
802 (signal (car error) (cdr error))))
803 (message "Signing %s...wrote %s" (file-name-nondirectory file)
804 (file-name-nondirectory signature))))
807 (defun epa-encrypt-file (file recipients)
808 "Encrypt FILE for RECIPIENTS."
810 (list (expand-file-name (read-file-name "File: "))
811 (epa-select-keys (epg-make-context epa-protocol)
812 "Select recipients for encryption.
813 If no one is selected, symmetric encryption will be performed. ")))
814 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
815 (if epa-armor ".asc" ".gpg")
817 (context (epg-make-context epa-protocol)))
818 (setf (epg-context-armor context) epa-armor)
819 (setf (epg-context-textmode context) epa-textmode)
820 (epg-context-set-passphrase-callback context
821 #'epa-passphrase-callback-function)
822 (epg-context-set-progress-callback context
824 #'epa-progress-callback-function
825 (format "Encrypting %s..."
826 (file-name-nondirectory file))))
827 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
828 (message "Encrypting %s..." (file-name-nondirectory file))
829 (condition-case error
830 (epg-encrypt-file context file recipients cipher)
832 (epa-display-error context)
833 (signal (car error) (cdr error))))
834 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
835 (file-name-nondirectory cipher))))
838 (defun epa-decrypt-region (start end &optional make-buffer-function)
839 "Decrypt the current region between START and END.
841 If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
842 It should return that buffer. If it copies the input, it should
843 delete the text now being decrypted. It should leave point at the
844 proper place to insert the plaintext.
846 Be careful about using this command in Lisp programs!
847 Since this function operates on regions, it does some tricks such
848 as coding-system detection and unibyte/multibyte conversion. If
849 you are sure how the data in the region should be treated, you
850 should consider using the string based counterpart
851 `epg-decrypt-string', or the file based counterpart
852 `epg-decrypt-file' instead.
856 \(let ((context (epg-make-context \\='OpenPGP)))
857 (decode-coding-string
858 (epg-decrypt-string context (buffer-substring start end))
862 (let ((context (epg-make-context epa-protocol))
864 (epg-context-set-passphrase-callback context
865 #'epa-passphrase-callback-function)
866 (epg-context-set-progress-callback context
868 #'epa-progress-callback-function
870 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
871 (message "Decrypting...")
872 (condition-case error
873 (setq plain (epg-decrypt-string context (buffer-substring start end)))
875 (epa-display-error context)
876 (signal (car error) (cdr error))))
877 (message "Decrypting...done")
878 (setq plain (epa--decode-coding-string
880 (or coding-system-for-read
881 (get-text-property start 'epa-coding-system-used)
883 (if make-buffer-function
884 (with-current-buffer (funcall make-buffer-function)
885 (let ((inhibit-read-only t))
887 (if (or (eq epa-replace-original-text t)
888 (and epa-replace-original-text
889 (y-or-n-p "Replace the original text? ")))
890 (let ((inhibit-read-only t))
891 (delete-region start end)
894 (with-output-to-temp-buffer "*Temp*"
895 (set-buffer standard-output)
898 (if (epg-context-result-for context 'verify)
899 (epa-display-info (epg-verify-result-to-string
900 (epg-context-result-for context 'verify)))))))
902 (defun epa--find-coding-system-for-mime-charset (mime-charset)
903 (if (featurep 'xemacs)
904 (if (fboundp 'find-coding-system)
905 (find-coding-system mime-charset))
906 ;; Find the first coding system which corresponds to MIME-CHARSET.
907 (let ((pointer (coding-system-list)))
909 (not (eq (coding-system-get (car pointer) 'mime-charset)
911 (setq pointer (cdr pointer)))
915 (defun epa-decrypt-armor-in-region (start end)
916 "Decrypt OpenPGP armors in the current region between START and END.
918 Don't use this command in Lisp programs!
919 See the reason described in the `epa-decrypt-region' documentation."
920 (declare (interactive-only t))
924 (narrow-to-region start end)
926 (let (armor-start armor-end)
927 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
928 (setq armor-start (match-beginning 0)
929 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
932 (error "Encryption armor beginning has no matching end"))
933 (goto-char armor-start)
934 (let ((coding-system-for-read
935 (or coding-system-for-read
936 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
937 (epa--find-coding-system-for-mime-charset
938 (intern (downcase (match-string 1))))))))
939 (goto-char armor-end)
940 (epa-decrypt-region armor-start armor-end)))))))
943 (defun epa-verify-region (start end)
944 "Verify the current region between START and END.
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 in the region should be treated, you
950 should consider using the string based counterpart
951 `epg-verify-string', or the file based counterpart
952 `epg-verify-file' instead.
956 \(let ((context (epg-make-context \\='OpenPGP)))
957 (decode-coding-string
958 (epg-verify-string context (buffer-substring start end))
960 (declare (interactive-only t))
962 (let ((context (epg-make-context epa-protocol))
964 (setf (epg-context-progress-callback context)
966 #'epa-progress-callback-function
968 (message "Verifying...")
969 (condition-case error
970 (setq plain (epg-verify-string
972 (epa--encode-coding-string
973 (buffer-substring start end)
974 (or coding-system-for-write
975 (get-text-property start 'epa-coding-system-used)))))
977 (epa-display-error context)
978 (signal (car error) (cdr error))))
979 (message "Verifying...done")
980 (setq plain (epa--decode-coding-string
982 (or coding-system-for-read
983 (get-text-property start 'epa-coding-system-used)
985 (if (or (eq epa-replace-original-text t)
986 (and epa-replace-original-text
987 (y-or-n-p "Replace the original text? ")))
988 (let ((inhibit-read-only t)
990 (delete-region start end)
993 (with-output-to-temp-buffer "*Temp*"
994 (set-buffer standard-output)
997 (if (epg-context-result-for context 'verify)
998 (epa-display-info (epg-verify-result-to-string
999 (epg-context-result-for context 'verify))))))
1002 (defun epa-verify-cleartext-in-region (start end)
1003 "Verify OpenPGP cleartext signed messages in the current region
1004 between START and END.
1006 Don't use this command in Lisp programs!
1007 See the reason described in the `epa-verify-region' documentation."
1008 (declare (interactive-only t))
1012 (narrow-to-region start end)
1014 (let (cleartext-start cleartext-end)
1015 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
1017 (setq cleartext-start (match-beginning 0))
1018 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
1020 (error "Invalid cleartext signed message"))
1021 (setq cleartext-end (re-search-forward
1022 "^-----END PGP SIGNATURE-----$"
1024 (unless cleartext-end
1025 (error "No cleartext tail"))
1026 (epa-verify-region cleartext-start cleartext-end))))))
1028 (defalias 'epa--select-safe-coding-system
1029 (if (fboundp 'select-safe-coding-system)
1030 #'select-safe-coding-system
1032 buffer-file-coding-system)))
1035 (defun epa-sign-region (start end signers mode)
1036 "Sign the current region between START and END by SIGNERS keys selected.
1038 Don't use this command in Lisp programs!
1039 Since this function operates on regions, it does some tricks such
1040 as coding-system detection and unibyte/multibyte conversion. If
1041 you are sure how the data should be treated, you should consider
1042 using the string based counterpart `epg-sign-string', or the file
1043 based counterpart `epg-sign-file' instead.
1047 \(let ((context (epg-make-context \\='OpenPGP)))
1050 (encode-coding-string (buffer-substring start end) \\='utf-8)))"
1051 (declare (interactive-only t))
1053 (let ((verbose current-prefix-arg))
1054 (setq epa-last-coding-system-specified
1055 (or coding-system-for-write
1056 (epa--select-safe-coding-system
1057 (region-beginning) (region-end))))
1058 (list (region-beginning) (region-end)
1060 (epa-select-keys (epg-make-context epa-protocol)
1061 "Select keys for signing.
1062 If no one is selected, default secret key is used. "
1065 (epa--read-signature-type)
1068 (let ((context (epg-make-context epa-protocol))
1070 ;;(setf (epg-context-armor context) epa-armor)
1071 (setf (epg-context-armor context) t)
1072 ;;(setf (epg-context-textmode context) epa-textmode)
1073 (setf (epg-context-textmode context) t)
1074 (setf (epg-context-signers context) signers)
1075 (epg-context-set-passphrase-callback context
1076 #'epa-passphrase-callback-function)
1077 (epg-context-set-progress-callback context
1079 #'epa-progress-callback-function
1081 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1082 (message "Signing...")
1083 (condition-case error
1084 (setq signature (epg-sign-string context
1085 (epa--encode-coding-string
1086 (buffer-substring start end)
1087 epa-last-coding-system-specified)
1090 (epa-display-error context)
1091 (signal (car error) (cdr error))))
1092 (message "Signing...done")
1093 (delete-region start end)
1095 (add-text-properties (point)
1097 (insert (epa--decode-coding-string
1099 (or coding-system-for-read
1100 epa-last-coding-system-specified)))
1102 (list 'epa-coding-system-used
1103 epa-last-coding-system-specified
1109 (defalias 'epa--derived-mode-p
1110 (if (fboundp 'derived-mode-p)
1112 (lambda (&rest modes)
1113 "Non-nil if the current major mode is derived from one of MODES.
1114 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1115 (let ((parent major-mode))
1116 (while (and (not (memq parent modes))
1117 (setq parent (get parent 'derived-mode-parent))))
1121 (defun epa-encrypt-region (start end recipients sign signers)
1122 "Encrypt the current region between START and END for RECIPIENTS.
1124 Don't use this command in Lisp programs!
1125 Since this function operates on regions, it does some tricks such
1126 as coding-system detection and unibyte/multibyte conversion. If
1127 you are sure how the data should be treated, you should consider
1128 using the string based counterpart `epg-encrypt-string', or the
1129 file based counterpart `epg-encrypt-file' instead.
1133 \(let ((context (epg-make-context \\='OpenPGP)))
1136 (encode-coding-string (buffer-substring start end) \\='utf-8)
1138 (declare (interactive-only t))
1140 (let ((verbose current-prefix-arg)
1141 (context (epg-make-context epa-protocol))
1143 (setq epa-last-coding-system-specified
1144 (or coding-system-for-write
1145 (epa--select-safe-coding-system
1146 (region-beginning) (region-end))))
1147 (list (region-beginning) (region-end)
1148 (epa-select-keys context
1149 "Select recipients for encryption.
1150 If no one is selected, symmetric encryption will be performed. ")
1151 (setq sign (if verbose (y-or-n-p "Sign? ")))
1153 (epa-select-keys context
1154 "Select keys for signing. ")))))
1156 (let ((context (epg-make-context epa-protocol))
1158 ;;(setf (epg-context-armor context) epa-armor)
1159 (setf (epg-context-armor context) t)
1160 ;;(setf (epg-context-textmode context) epa-textmode)
1161 (setf (epg-context-textmode context) t)
1163 (setf (epg-context-signers context) signers))
1164 (epg-context-set-passphrase-callback context
1165 #'epa-passphrase-callback-function)
1166 (epg-context-set-progress-callback context
1168 #'epa-progress-callback-function
1170 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1171 (message "Encrypting...")
1172 (condition-case error
1173 (setq cipher (epg-encrypt-string context
1174 (epa--encode-coding-string
1175 (buffer-substring start end)
1176 epa-last-coding-system-specified)
1180 (epa-display-error context)
1181 (signal (car error) (cdr error))))
1182 (message "Encrypting...done")
1183 (delete-region start end)
1185 (add-text-properties (point)
1189 (list 'epa-coding-system-used
1190 epa-last-coding-system-specified
1197 (defun epa-delete-keys (keys &optional allow-secret)
1198 "Delete selected KEYS."
1200 (let ((keys (epa--marked-keys)))
1202 (error "No keys selected"))
1204 (eq (nth 1 epa-list-keys-arguments) t))))
1205 (let ((context (epg-make-context epa-protocol)))
1206 (message "Deleting...")
1207 (condition-case error
1208 (epg-delete-keys context keys allow-secret)
1210 (epa-display-error context)
1211 (signal (car error) (cdr error))))
1212 (message "Deleting...done")
1213 (apply #'epa--list-keys epa-list-keys-arguments)))
1216 (defun epa-import-keys (file)
1217 "Import keys from FILE."
1218 (interactive "fFile: ")
1219 (setq file (expand-file-name file))
1220 (let ((context (epg-make-context epa-protocol)))
1221 (message "Importing %s..." (file-name-nondirectory file))
1224 (epg-import-keys-from-file context file)
1225 (message "Importing %s...done" (file-name-nondirectory file)))
1227 (epa-display-error context)
1228 (message "Importing %s...failed" (file-name-nondirectory file))))
1229 (if (epg-context-result-for context 'import)
1230 (epa-display-info (epg-import-result-to-string
1231 (epg-context-result-for context 'import))))
1232 ;; FIXME: Why not use the (otherwise unused) epa--derived-mode-p?
1233 (if (eq major-mode 'epa-key-list-mode)
1234 (apply #'epa--list-keys epa-list-keys-arguments))))
1237 (defun epa-import-keys-region (start end)
1238 "Import keys from the region."
1240 (let ((context (epg-make-context epa-protocol)))
1241 (message "Importing...")
1244 (epg-import-keys-from-string context (buffer-substring start end))
1245 (message "Importing...done"))
1247 (epa-display-error context)
1248 (message "Importing...failed")))
1249 (if (epg-context-result-for context 'import)
1250 (epa-display-info (epg-import-result-to-string
1251 (epg-context-result-for context 'import))))))
1254 (defun epa-import-armor-in-region (start end)
1255 "Import keys in the OpenPGP armor format in the current region
1256 between START and END."
1260 (narrow-to-region start end)
1262 (let (armor-start armor-end)
1263 (while (re-search-forward
1264 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1266 (setq armor-start (match-beginning 0)
1267 armor-end (re-search-forward
1268 (concat "^-----END " (match-string 1) "-----$")
1271 (error "No armor tail"))
1272 (epa-import-keys-region armor-start armor-end))))))
1275 (defun epa-export-keys (keys file)
1276 "Export selected KEYS to FILE."
1278 (let ((keys (epa--marked-keys))
1281 (error "No keys selected"))
1284 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1285 (if epa-armor ".asc" ".gpg"))
1290 (concat "To file (default "
1291 (file-name-nondirectory default-name)
1293 (file-name-directory default-name)
1295 (let ((context (epg-make-context epa-protocol)))
1296 (setf (epg-context-armor context) epa-armor)
1297 (message "Exporting to %s..." (file-name-nondirectory file))
1298 (condition-case error
1299 (epg-export-keys-to-file context keys file)
1301 (epa-display-error context)
1302 (signal (car error) (cdr error))))
1303 (message "Exporting to %s...done" (file-name-nondirectory file))))
1306 (defun epa-insert-keys (keys)
1307 "Insert selected KEYS after the point."
1309 (list (epa-select-keys (epg-make-context epa-protocol)
1310 "Select keys to export.
1311 If no one is selected, default public key is exported. ")))
1312 (let ((context (epg-make-context epa-protocol)))
1313 ;;(setf (epg-context-armor context) epa-armor)
1314 (setf (epg-context-armor context) t)
1315 (condition-case error
1316 (insert (epg-export-keys-to-string context keys))
1318 (epa-display-error context)
1319 (signal (car error) (cdr error))))))
1321 ;; (defun epa-sign-keys (keys &optional local)
1322 ;; "Sign selected KEYS.
1323 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1325 ;; Don't use this command in Lisp programs!"
1326 ;; (declare (interactive-only t))
1328 ;; (let ((keys (epa--marked-keys)))
1330 ;; (error "No keys selected"))
1331 ;; (list keys current-prefix-arg)))
1332 ;; (let ((context (epg-make-context epa-protocol)))
1333 ;; (epg-context-set-passphrase-callback context
1334 ;; #'epa-passphrase-callback-function)
1335 ;; (epg-context-set-progress-callback context
1337 ;; #'epa-progress-callback-function
1338 ;; "Signing keys..."))
1339 ;; (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1340 ;; (message "Signing keys...")
1341 ;; (epg-sign-keys context keys local)
1342 ;; (message "Signing keys...done")))
1343 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1347 ;;; epa.el ends here