1 ;;; epa.el --- the EasyPG Assistant -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006-2018 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 ;; In the doc string below, we say "symbol `error'" to avoid producing
60 ;; a hyperlink for `error' the function.
61 (defcustom epa-pinentry-mode nil
64 GnuPG 2.1 or later has an option to control the behavior of
65 Pinentry invocation. The value should be the symbol `error',
66 `ask', `cancel', or `loopback'. See the GnuPG manual for the
69 In epa commands, a particularly useful mode is `loopback', which
70 redirects all Pinentry queries to the caller, so Emacs can query
71 passphrase through the minibuffer, instead of external Pinentry
73 :type
'(choice (const nil
)
81 (defgroup epa-faces nil
86 (defcustom epa-mail-aliases nil
87 "Alist of aliases of email addresses that stand for encryption keys.
88 Each element is a list of email addresses (ALIAS EXPANSIONS...).
89 When one of the recipients of a message being encrypted is ALIAS,
90 instead of encrypting it for ALIAS, encrypt it for EXPANSIONS...
92 If EXPANSIONS is empty, ignore ALIAS as regards encryption.
93 This is a handy way to avoid warnings about addresses that you don't
96 The command `epa-mail-encrypt' uses this."
97 :type
'(repeat (cons (string :tag
"Alias") (repeat (string :tag
"Expansion"))))
101 (defface epa-validity-high
102 '((default :weight bold
)
103 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
104 "Face for high validity EPA information."
107 (defface epa-validity-medium
108 '((default :slant italic
)
109 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
110 "Face for medium validity EPA information."
113 (defface epa-validity-low
115 "Face used for displaying the low validity."
118 (defface epa-validity-disabled
119 '((t :slant italic
:inverse-video t
))
120 "Face used for displaying the disabled validity."
124 '((((class color
) (background dark
))
125 :foreground
"lightyellow")
126 (((class color
) (background light
))
127 :foreground
"blue4"))
128 "Face used for displaying the string."
132 '((default :weight bold
)
133 (((class color
) (background dark
)) :foreground
"orange")
134 (((class color
) (background light
)) :foreground
"red"))
135 "Face used for displaying the high validity."
138 (defface epa-field-name
139 '((default :weight bold
)
140 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
141 "Face for the name of the attribute field."
144 (defface epa-field-body
145 '((default :slant italic
)
146 (((class color
) (background dark
)) :foreground
"turquoise"))
147 "Face for the body of the attribute field."
150 (defcustom epa-validity-face-alist
151 '((unknown . epa-validity-disabled
)
152 (invalid . epa-validity-disabled
)
153 (disabled . epa-validity-disabled
)
154 (revoked . epa-validity-disabled
)
155 (expired . epa-validity-disabled
)
156 (none . epa-validity-low
)
157 (undefined . epa-validity-low
)
158 (never . epa-validity-low
)
159 (marginal . epa-validity-medium
)
160 (full . epa-validity-high
)
161 (ultimate . epa-validity-high
))
162 "An alist mapping validity values to faces."
163 :type
'(repeat (cons symbol face
))
166 (defvar epa-font-lock-keywords
169 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
171 (2 'epa-field-body
)))
172 "Default expressions to addon in epa-mode.")
174 (defconst epa-pubkey-algorithm-letter-alist
182 (defvar epa-protocol
'OpenPGP
183 "The default protocol.
184 The value can be either OpenPGP or CMS.
186 You should bind this variable with `let', but do not set it globally.")
188 (defvar epa-armor nil
189 "If non-nil, epa commands create ASCII armored output.
191 You should bind this variable with `let', but do not set it globally.")
193 (defvar epa-textmode nil
194 "If non-nil, epa commands treat input files as text.
196 You should bind this variable with `let', but do not set it globally.")
198 (defvar epa-keys-buffer nil
)
199 (defvar epa-key-buffer-alist nil
)
201 (defvar epa-list-keys-arguments nil
)
202 (defvar epa-info-buffer nil
)
203 (defvar epa-error-buffer nil
)
204 (defvar epa-last-coding-system-specified nil
)
206 (defvar epa-key-list-mode-map
207 (let ((keymap (make-sparse-keymap))
208 (menu-map (make-sparse-keymap)))
209 (define-key keymap
"m" 'epa-mark-key
)
210 (define-key keymap
"u" 'epa-unmark-key
)
211 (define-key keymap
"d" 'epa-decrypt-file
)
212 (define-key keymap
"v" 'epa-verify-file
)
213 (define-key keymap
"s" 'epa-sign-file
)
214 (define-key keymap
"e" 'epa-encrypt-file
)
215 (define-key keymap
"r" 'epa-delete-keys
)
216 (define-key keymap
"i" 'epa-import-keys
)
217 (define-key keymap
"o" 'epa-export-keys
)
218 (define-key keymap
"g" 'revert-buffer
)
219 (define-key keymap
"n" 'next-line
)
220 (define-key keymap
"p" 'previous-line
)
221 (define-key keymap
" " 'scroll-up-command
)
222 (define-key keymap
[?\S-\
] 'scroll-down-command
)
223 (define-key keymap
[delete] 'scroll-down-command)
224 (define-key keymap "q" 'epa-exit-buffer)
225 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
226 (define-key menu-map [epa-key-list-unmark-key]
227 '(menu-item "Unmark Key" epa-unmark-key
228 :help "Unmark a key"))
229 (define-key menu-map [epa-key-list-mark-key]
230 '(menu-item "Mark Key" epa-mark-key
232 (define-key menu-map [separator-epa-file] '(menu-item "--"))
233 (define-key menu-map [epa-verify-file]
234 '(menu-item "Verify File..." epa-verify-file
235 :help "Verify FILE"))
236 (define-key menu-map [epa-sign-file]
237 '(menu-item "Sign File..." epa-sign-file
238 :help "Sign FILE by SIGNERS keys selected"))
239 (define-key menu-map [epa-decrypt-file]
240 '(menu-item "Decrypt File..." epa-decrypt-file
241 :help "Decrypt FILE"))
242 (define-key menu-map [epa-encrypt-file]
243 '(menu-item "Encrypt File..." epa-encrypt-file
244 :help "Encrypt FILE for RECIPIENTS"))
245 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
246 (define-key menu-map [epa-key-list-delete-keys]
247 '(menu-item "Delete Keys" epa-delete-keys
248 :help "Delete Marked Keys"))
249 (define-key menu-map [epa-key-list-import-keys]
250 '(menu-item "Import Keys" epa-import-keys
251 :help "Import keys from a file"))
252 (define-key menu-map [epa-key-list-export-keys]
253 '(menu-item "Export Keys" epa-export-keys
254 :help "Export marked keys to a file"))
257 (defvar epa-key-mode-map
258 (let ((keymap (make-sparse-keymap)))
259 (define-key keymap "q" 'epa-exit-buffer)
262 (defvar epa-info-mode-map
263 (let ((keymap (make-sparse-keymap)))
264 (define-key keymap "q" 'delete-window)
267 (defvar epa-exit-buffer-function #'quit-window)
269 (define-widget 'epa-key 'push-button
270 "Button for representing an epg-key object."
272 :button-face-get 'epa--key-widget-button-face-get
273 :value-create 'epa--key-widget-value-create
274 :action 'epa--key-widget-action
275 :help-echo 'epa--key-widget-help-echo)
277 (defun epa--key-widget-action (widget &optional _event)
278 (save-selected-window
279 (epa--show-key (widget-get widget :value))))
281 (defun epa--key-widget-value-create (widget)
282 (let* ((key (widget-get widget :value))
283 (primary-sub-key (car (epg-key-sub-key-list key)))
284 (primary-user-id (car (epg-key-user-id-list key))))
285 (insert (format "%c "
286 (if (epg-sub-key-validity primary-sub-key)
287 (car (rassq (epg-sub-key-validity primary-sub-key)
288 epg-key-validity-alist))
290 (epg-sub-key-id primary-sub-key)
293 (if (stringp (epg-user-id-string primary-user-id))
294 (epg-user-id-string primary-user-id)
295 (epg-decode-dn (epg-user-id-string primary-user-id)))
298 (defun epa--key-widget-button-face-get (widget)
299 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
300 (widget-get widget :value))))))
302 (cdr (assq validity epa-validity-face-alist))
305 (defun epa--key-widget-help-echo (widget)
307 (epg-sub-key-id (car (epg-key-sub-key-list
308 (widget-get widget :value))))))
310 (defalias 'epa--encode-coding-string
311 (if (fboundp 'encode-coding-string) #'encode-coding-string #'identity))
313 (defalias 'epa--decode-coding-string
314 (if (fboundp 'decode-coding-string) #'decode-coding-string #'identity))
316 (define-derived-mode epa-key-list-mode special-mode "Keys"
317 "Major mode for `epa-list-keys'."
318 (buffer-disable-undo)
319 (setq truncate-lines t
321 (setq-local font-lock-defaults '(epa-font-lock-keywords t))
322 ;; In XEmacs, auto-initialization of font-lock is not effective
323 ;; if buffer-file-name is not set.
324 (font-lock-set-defaults)
325 (make-local-variable 'epa-exit-buffer-function)
326 (setq-local revert-buffer-function #'epa--key-list-revert-buffer))
328 (define-derived-mode epa-key-mode special-mode "Key"
329 "Major mode for a key description."
330 (buffer-disable-undo)
331 (setq truncate-lines t
333 (setq-local font-lock-defaults '(epa-font-lock-keywords t))
334 ;; In XEmacs, auto-initialization of font-lock is not effective
335 ;; if buffer-file-name is not set.
336 (font-lock-set-defaults)
337 (make-local-variable 'epa-exit-buffer-function))
339 (define-derived-mode epa-info-mode special-mode "Info"
340 "Major mode for `epa-info-buffer'."
341 (buffer-disable-undo)
342 (setq truncate-lines t
345 (defun epa-mark-key (&optional arg)
346 "Mark a key on the current line.
347 If ARG is non-nil, unmark the key."
349 (let ((inhibit-read-only t)
353 (unless (get-text-property (point) 'epa-key)
354 (error "No key on this line"))
355 (setq properties (text-properties-at (point)))
357 (insert (if arg " " "*"))
358 (set-text-properties (1- (point)) (point) properties)
361 (defun epa-unmark-key (&optional arg)
362 "Unmark a key on the current line.
363 If ARG is non-nil, mark the key."
365 (epa-mark-key (not arg)))
367 (defun epa-exit-buffer ()
368 "Exit the current buffer.
369 `epa-exit-buffer-function' is called if it is set."
371 (funcall epa-exit-buffer-function))
373 (defun epa--insert-keys (keys)
376 (narrow-to-region (point) (point))
381 (add-text-properties point (point)
382 (list 'epa-key (car keys)
387 (widget-create 'epa-key :value (car keys))
389 (setq keys (cdr keys))))
390 (add-text-properties (point-min) (point-max)
391 (list 'epa-list-keys t
397 (defun epa--list-keys (name secret)
398 (unless (and epa-keys-buffer
399 (buffer-live-p epa-keys-buffer))
400 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
401 (set-buffer epa-keys-buffer)
403 (let ((inhibit-read-only t)
406 (context (epg-make-context epa-protocol)))
407 (unless (get-text-property point 'epa-list-keys)
408 (setq point (next-single-property-change point 'epa-list-keys)))
411 (or (next-single-property-change point 'epa-list-keys)
414 (epa--insert-keys (epg-list-keys context name secret))
416 (set-keymap-parent (current-local-map) widget-keymap))
417 (make-local-variable 'epa-list-keys-arguments)
418 (setq epa-list-keys-arguments (list name secret))
419 (goto-char (point-min))
420 (pop-to-buffer (current-buffer)))
423 (defun epa-list-keys (&optional name)
424 "List all keys matched with NAME from the public keyring."
426 (if current-prefix-arg
427 (let ((name (read-string "Pattern: "
428 (if epa-list-keys-arguments
429 (car epa-list-keys-arguments)))))
430 (list (if (equal name "") nil name)))
432 (epa--list-keys name nil))
435 (defun epa-list-secret-keys (&optional name)
436 "List all keys matched with NAME from the private keyring."
438 (if current-prefix-arg
439 (let ((name (read-string "Pattern: "
440 (if epa-list-keys-arguments
441 (car epa-list-keys-arguments)))))
442 (list (if (equal name "") nil name)))
444 (epa--list-keys name t))
446 (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm)
447 (apply #'epa--list-keys epa-list-keys-arguments))
449 (defun epa--marked-keys ()
450 (or (with-current-buffer epa-keys-buffer
451 (goto-char (point-min))
453 (while (re-search-forward "^\\*" nil t)
454 (if (setq key (get-text-property (match-beginning 0)
456 (setq keys (cons key keys))))
458 (let ((key (get-text-property (point-at-bol) 'epa-key)))
462 (defun epa--select-keys (prompt keys)
463 (unless (and epa-keys-buffer
464 (buffer-live-p epa-keys-buffer))
465 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
466 (with-current-buffer epa-keys-buffer
468 ;; C-c C-c is the usual way to finish the selection (bug#11159).
469 (define-key (current-local-map) "\C-c\C-c" 'exit-recursive-edit)
470 (let ((inhibit-read-only t)
474 (substitute-command-keys "\
475 - `\\[epa-mark-key]' to mark a key on the line
476 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
478 :notify (lambda (&rest _ignore) (abort-recursive-edit))
480 "Click here or \\[abort-recursive-edit] to cancel"
483 :notify (lambda (&rest _ignore) (exit-recursive-edit))
485 "Click here or \\[exit-recursive-edit] to finish"
488 (epa--insert-keys keys)
490 (set-keymap-parent (current-local-map) widget-keymap)
491 (setq epa-exit-buffer-function #'abort-recursive-edit)
492 (goto-char (point-min))
493 (let ((display-buffer-mark-dedicated 'soft))
494 (pop-to-buffer (current-buffer))))
499 (kill-buffer epa-keys-buffer))))
502 (defun epa-select-keys (context prompt &optional names secret)
503 "Display a user's keyring and ask him to select keys.
504 CONTEXT is an epg-context.
505 PROMPT is a string to prompt with.
506 NAMES is a list of strings to be matched with keys. If it is nil, all
508 If SECRET is non-nil, list secret keys instead of public keys."
509 (let ((keys (epg-list-keys context names secret)))
510 (epa--select-keys prompt keys)))
512 (defun epa--show-key (key)
513 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
514 (entry (assoc (epg-sub-key-id primary-sub-key)
515 epa-key-buffer-alist))
516 (inhibit-read-only t)
520 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
521 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
522 (unless (and (cdr entry)
523 (buffer-live-p (cdr entry)))
524 (setcdr entry (generate-new-buffer
525 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
526 (set-buffer (cdr entry))
528 (make-local-variable 'epa-key)
531 (setq pointer (epg-key-user-id-list key))
535 (if (epg-user-id-validity (car pointer))
537 (car (rassq (epg-user-id-validity (car pointer))
538 epg-key-validity-alist)))
541 (if (stringp (epg-user-id-string (car pointer)))
542 (epg-user-id-string (car pointer))
543 (epg-decode-dn (epg-user-id-string (car pointer))))
545 (setq pointer (cdr pointer)))
546 (setq pointer (epg-key-sub-key-list key))
549 (if (epg-sub-key-validity (car pointer))
551 (car (rassq (epg-sub-key-validity (car pointer))
552 epg-key-validity-alist)))
555 (epg-sub-key-id (car pointer))
558 (epg-sub-key-length (car pointer)))
560 (cdr (assq (epg-sub-key-algorithm (car pointer))
561 epg-pubkey-algorithm-alist))
564 (format-time-string "%Y-%m-%d"
565 (epg-sub-key-creation-time (car pointer)))
566 (error "????-??-??"))
567 (if (epg-sub-key-expiration-time (car pointer))
568 (format (if (time-less-p (current-time)
569 (epg-sub-key-expiration-time
574 (format-time-string "%Y-%m-%d"
575 (epg-sub-key-expiration-time
577 (error "????-??-??")))
580 (mapconcat #'symbol-name
581 (epg-sub-key-capability (car pointer))
584 (epg-sub-key-fingerprint (car pointer))
586 (setq pointer (cdr pointer)))
587 (goto-char (point-min))
588 (pop-to-buffer (current-buffer))))
590 (defun epa-display-info (info)
591 (if epa-popup-info-window
592 (save-selected-window
593 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
594 (setq epa-info-buffer (generate-new-buffer "*Info*")))
595 (if (get-buffer-window epa-info-buffer)
596 (delete-window (get-buffer-window epa-info-buffer)))
597 (with-current-buffer epa-info-buffer
598 (let ((inhibit-read-only t)
603 (goto-char (point-min)))
604 (if (> (window-height)
605 epa-info-window-height)
606 (set-window-buffer (split-window nil (- (window-height)
607 epa-info-window-height))
609 (pop-to-buffer epa-info-buffer)
610 (if (> (window-height) epa-info-window-height)
611 (shrink-window (- (window-height) epa-info-window-height)))))
612 (message "%s" info)))
614 (defun epa-display-error (context)
615 (unless (equal (epg-context-error-output context) "")
616 (let ((buffer (get-buffer-create "*Error*")))
617 (save-selected-window
618 (unless (and epa-error-buffer (buffer-live-p epa-error-buffer))
619 (setq epa-error-buffer (generate-new-buffer "*Error*")))
620 (if (get-buffer-window epa-error-buffer)
621 (delete-window (get-buffer-window epa-error-buffer)))
622 (with-current-buffer buffer
623 (let ((inhibit-read-only t)
627 (pcase (epg-context-operation context)
628 (`decrypt "Error while decrypting with \"%s\":")
629 (`verify "Error while verifying with \"%s\":")
630 (`sign "Error while signing with \"%s\":")
631 (`encrypt "Error while encrypting with \"%s\":")
632 (`import-keys "Error while importing keys with \"%s\":")
633 (`export-keys "Error while exporting keys with \"%s\":")
634 (_ "Error while executing \"%s\":\n\n"))
635 (epg-context-program context))
637 (epg-context-error-output context)))
639 (goto-char (point-min)))
640 (display-buffer buffer)))))
642 (defun epa-display-verify-result (verify-result)
643 (declare (obsolete epa-display-info "23.1"))
644 (epa-display-info (epg-verify-result-to-string verify-result)))
646 (defun epa-passphrase-callback-function (context key-id handback)
649 (format "Passphrase for symmetric encryption%s: "
650 ;; Add the file name to the prompt, if any.
651 (if (stringp handback)
652 (format " for %s" handback)
654 (eq (epg-context-operation context) 'encrypt))
657 "Passphrase for PIN: "
658 (let ((entry (assoc key-id epg-user-id-alist)))
660 (format "Passphrase for %s %s: " key-id (cdr entry))
661 (format "Passphrase for %s: " key-id)))))))
663 (defun epa-progress-callback-function (_context what _char current total
665 (let ((prompt (or handback
666 (format "Processing %s: " what))))
667 ;; According to gnupg/doc/DETAIL: a "total" of 0 indicates that
668 ;; the total amount is not known. The condition TOTAL && CUR ==
669 ;; TOTAL may be used to detect the end of an operation.
671 (if (= current total)
672 (message "%s...done" prompt)
673 (message "%s...%d%%" prompt
674 (floor (* 100.0 current) total)))
675 (message "%s..." prompt))))
677 (defun epa-read-file-name (input)
678 "Interactively read an output file name based on INPUT file name."
679 (setq input (file-name-sans-extension (expand-file-name input)))
682 (concat "To file (default " (file-name-nondirectory input) ") ")
683 (file-name-directory input)
687 (defun epa-decrypt-file (decrypt-file &optional plain-file)
688 "Decrypt DECRYPT-FILE into PLAIN-FILE.
689 If you do not specify PLAIN-FILE, this functions prompts for the value to use."
691 (let* ((file (read-file-name "File to decrypt: "))
692 (plain (epa-read-file-name file)))
694 (or plain-file (setq plain-file (epa-read-file-name decrypt-file)))
695 (setq decrypt-file (expand-file-name decrypt-file))
696 (let ((context (epg-make-context epa-protocol)))
697 (epg-context-set-passphrase-callback context
698 #'epa-passphrase-callback-function)
699 (epg-context-set-progress-callback context
701 #'epa-progress-callback-function
702 (format "Decrypting %s..."
703 (file-name-nondirectory decrypt-file))))
704 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
705 (message "Decrypting %s..." (file-name-nondirectory decrypt-file))
706 (condition-case error
707 (epg-decrypt-file context decrypt-file plain-file)
709 (epa-display-error context)
710 (signal (car error) (cdr error))))
711 (message "Decrypting %s...wrote %s" (file-name-nondirectory decrypt-file)
712 (file-name-nondirectory plain-file))
713 (if (epg-context-result-for context 'verify)
714 (epa-display-info (epg-verify-result-to-string
715 (epg-context-result-for context 'verify))))))
718 (defun epa-verify-file (file)
720 (interactive "fFile: ")
721 (setq file (expand-file-name file))
722 (let* ((context (epg-make-context epa-protocol))
723 (plain (if (equal (file-name-extension file) "sig")
724 (file-name-sans-extension file))))
725 (epg-context-set-progress-callback context
727 #'epa-progress-callback-function
728 (format "Verifying %s..."
729 (file-name-nondirectory file))))
730 (message "Verifying %s..." (file-name-nondirectory file))
731 (condition-case error
732 (epg-verify-file context file plain)
734 (epa-display-error context)
735 (signal (car error) (cdr error))))
736 (message "Verifying %s...done" (file-name-nondirectory file))
737 (if (epg-context-result-for context 'verify)
738 (epa-display-info (epg-verify-result-to-string
739 (epg-context-result-for context 'verify))))))
741 (defun epa--read-signature-type ()
744 (message "Signature type (n,c,d,?) ")
749 (setq type 'detached))
751 (with-output-to-temp-buffer "*Help*"
752 (with-current-buffer standard-output
754 n - Create a normal signature
755 c - Create a cleartext signature
756 d - Create a detached signature
760 (setq type 'normal))))
764 (defun epa-sign-file (file signers mode)
765 "Sign FILE by SIGNERS keys selected."
767 (let ((verbose current-prefix-arg))
768 (list (expand-file-name (read-file-name "File: "))
770 (epa-select-keys (epg-make-context epa-protocol)
771 "Select keys for signing.
772 If no one is selected, default secret key is used. "
775 (epa--read-signature-type)
777 (let ((signature (concat file
778 (if (eq epa-protocol 'OpenPGP)
781 '(nil t normal detached))))
783 (if (memq mode '(t detached))
786 (if (memq mode '(t detached))
789 (context (epg-make-context epa-protocol)))
790 (setf (epg-context-armor context) epa-armor)
791 (setf (epg-context-textmode context) epa-textmode)
792 (setf (epg-context-signers context) signers)
793 (epg-context-set-passphrase-callback context
794 #'epa-passphrase-callback-function)
795 (epg-context-set-progress-callback context
797 #'epa-progress-callback-function
798 (format "Signing %s..."
799 (file-name-nondirectory file))))
800 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
801 (message "Signing %s..." (file-name-nondirectory file))
802 (condition-case error
803 (epg-sign-file context file signature mode)
805 (epa-display-error context)
806 (signal (car error) (cdr error))))
807 (message "Signing %s...wrote %s" (file-name-nondirectory file)
808 (file-name-nondirectory signature))))
811 (defun epa-encrypt-file (file recipients)
812 "Encrypt FILE for RECIPIENTS."
814 (list (expand-file-name (read-file-name "File: "))
815 (epa-select-keys (epg-make-context epa-protocol)
816 "Select recipients for encryption.
817 If no one is selected, symmetric encryption will be performed. ")))
818 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
819 (if epa-armor ".asc" ".gpg")
821 (context (epg-make-context epa-protocol)))
822 (setf (epg-context-armor context) epa-armor)
823 (setf (epg-context-textmode context) epa-textmode)
824 (epg-context-set-passphrase-callback context
825 #'epa-passphrase-callback-function)
826 (epg-context-set-progress-callback context
828 #'epa-progress-callback-function
829 (format "Encrypting %s..."
830 (file-name-nondirectory file))))
831 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
832 (message "Encrypting %s..." (file-name-nondirectory file))
833 (condition-case error
834 (epg-encrypt-file context file recipients cipher)
836 (epa-display-error context)
837 (signal (car error) (cdr error))))
838 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
839 (file-name-nondirectory cipher))))
842 (defun epa-decrypt-region (start end &optional make-buffer-function)
843 "Decrypt the current region between START and END.
845 If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
846 It should return that buffer. If it copies the input, it should
847 delete the text now being decrypted. It should leave point at the
848 proper place to insert the plaintext.
850 Be careful about using this command in Lisp programs!
851 Since this function operates on regions, it does some tricks such
852 as coding-system detection and unibyte/multibyte conversion. If
853 you are sure how the data in the region should be treated, you
854 should consider using the string based counterpart
855 `epg-decrypt-string', or the file based counterpart
856 `epg-decrypt-file' instead.
860 \(let ((context (epg-make-context \\='OpenPGP)))
861 (decode-coding-string
862 (epg-decrypt-string context (buffer-substring start end))
866 (let ((context (epg-make-context epa-protocol))
868 (epg-context-set-passphrase-callback context
869 #'epa-passphrase-callback-function)
870 (epg-context-set-progress-callback context
872 #'epa-progress-callback-function
874 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
875 (message "Decrypting...")
876 (condition-case error
877 (setq plain (epg-decrypt-string context (buffer-substring start end)))
879 (epa-display-error context)
880 (signal (car error) (cdr error))))
881 (message "Decrypting...done")
882 (setq plain (epa--decode-coding-string
884 (or coding-system-for-read
885 (get-text-property start 'epa-coding-system-used)
887 (if make-buffer-function
888 (with-current-buffer (funcall make-buffer-function)
889 (let ((inhibit-read-only t))
891 (if (or (eq epa-replace-original-text t)
892 (and epa-replace-original-text
893 (y-or-n-p "Replace the original text? ")))
894 (let ((inhibit-read-only t))
895 (delete-region start end)
898 (with-output-to-temp-buffer "*Temp*"
899 (set-buffer standard-output)
902 (if (epg-context-result-for context 'verify)
903 (epa-display-info (epg-verify-result-to-string
904 (epg-context-result-for context 'verify)))))))
906 (defun epa--find-coding-system-for-mime-charset (mime-charset)
907 (if (featurep 'xemacs)
908 (if (fboundp 'find-coding-system)
909 (find-coding-system mime-charset))
910 ;; Find the first coding system which corresponds to MIME-CHARSET.
911 (let ((pointer (coding-system-list)))
913 (not (eq (coding-system-get (car pointer) 'mime-charset)
915 (setq pointer (cdr pointer)))
919 (defun epa-decrypt-armor-in-region (start end)
920 "Decrypt OpenPGP armors in the current region between START and END.
922 Don't use this command in Lisp programs!
923 See the reason described in the `epa-decrypt-region' documentation."
924 (declare (interactive-only t))
928 (narrow-to-region start end)
930 (let (armor-start armor-end)
931 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
932 (setq armor-start (match-beginning 0)
933 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
936 (error "Encryption armor beginning has no matching end"))
937 (goto-char armor-start)
938 (let ((coding-system-for-read
939 (or coding-system-for-read
940 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
941 (epa--find-coding-system-for-mime-charset
942 (intern (downcase (match-string 1))))))))
943 (goto-char armor-end)
944 (epa-decrypt-region armor-start armor-end)))))))
947 (defun epa-verify-region (start end)
948 "Verify the current region between START and END.
950 Don't use this command in Lisp programs!
951 Since this function operates on regions, it does some tricks such
952 as coding-system detection and unibyte/multibyte conversion. If
953 you are sure how the data in the region should be treated, you
954 should consider using the string based counterpart
955 `epg-verify-string', or the file based counterpart
956 `epg-verify-file' instead.
960 \(let ((context (epg-make-context \\='OpenPGP)))
961 (decode-coding-string
962 (epg-verify-string context (buffer-substring start end))
964 (declare (interactive-only t))
966 (let ((context (epg-make-context epa-protocol))
968 (setf (epg-context-progress-callback context)
970 #'epa-progress-callback-function
972 (message "Verifying...")
973 (condition-case error
974 (setq plain (epg-verify-string
976 (epa--encode-coding-string
977 (buffer-substring start end)
978 (or coding-system-for-write
979 (get-text-property start 'epa-coding-system-used)))))
981 (epa-display-error context)
982 (signal (car error) (cdr error))))
983 (message "Verifying...done")
984 (setq plain (epa--decode-coding-string
986 (or coding-system-for-read
987 (get-text-property start 'epa-coding-system-used)
989 (if (or (eq epa-replace-original-text t)
990 (and epa-replace-original-text
991 (y-or-n-p "Replace the original text? ")))
992 (let ((inhibit-read-only t)
994 (delete-region start end)
997 (with-output-to-temp-buffer "*Temp*"
998 (set-buffer standard-output)
1001 (if (epg-context-result-for context 'verify)
1002 (epa-display-info (epg-verify-result-to-string
1003 (epg-context-result-for context 'verify))))))
1006 (defun epa-verify-cleartext-in-region (start end)
1007 "Verify OpenPGP cleartext signed messages in the current region
1008 between START and END.
1010 Don't use this command in Lisp programs!
1011 See the reason described in the `epa-verify-region' documentation."
1012 (declare (interactive-only t))
1016 (narrow-to-region start end)
1018 (let (cleartext-start cleartext-end)
1019 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
1021 (setq cleartext-start (match-beginning 0))
1022 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
1024 (error "Invalid cleartext signed message"))
1025 (setq cleartext-end (re-search-forward
1026 "^-----END PGP SIGNATURE-----$"
1028 (unless cleartext-end
1029 (error "No cleartext tail"))
1030 (epa-verify-region cleartext-start cleartext-end))))))
1032 (defalias 'epa--select-safe-coding-system
1033 (if (fboundp 'select-safe-coding-system)
1034 #'select-safe-coding-system
1036 buffer-file-coding-system)))
1039 (defun epa-sign-region (start end signers mode)
1040 "Sign the current region between START and END by SIGNERS keys selected.
1042 Don't use this command in Lisp programs!
1043 Since this function operates on regions, it does some tricks such
1044 as coding-system detection and unibyte/multibyte conversion. If
1045 you are sure how the data should be treated, you should consider
1046 using the string based counterpart `epg-sign-string', or the file
1047 based counterpart `epg-sign-file' instead.
1051 \(let ((context (epg-make-context \\='OpenPGP)))
1054 (encode-coding-string (buffer-substring start end) \\='utf-8)))"
1055 (declare (interactive-only t))
1057 (let ((verbose current-prefix-arg))
1058 (setq epa-last-coding-system-specified
1059 (or coding-system-for-write
1060 (epa--select-safe-coding-system
1061 (region-beginning) (region-end))))
1062 (list (region-beginning) (region-end)
1064 (epa-select-keys (epg-make-context epa-protocol)
1065 "Select keys for signing.
1066 If no one is selected, default secret key is used. "
1069 (epa--read-signature-type)
1072 (let ((context (epg-make-context epa-protocol))
1074 ;;(setf (epg-context-armor context) epa-armor)
1075 (setf (epg-context-armor context) t)
1076 ;;(setf (epg-context-textmode context) epa-textmode)
1077 (setf (epg-context-textmode context) t)
1078 (setf (epg-context-signers context) signers)
1079 (epg-context-set-passphrase-callback context
1080 #'epa-passphrase-callback-function)
1081 (epg-context-set-progress-callback context
1083 #'epa-progress-callback-function
1085 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1086 (message "Signing...")
1087 (condition-case error
1088 (setq signature (epg-sign-string context
1089 (epa--encode-coding-string
1090 (buffer-substring start end)
1091 epa-last-coding-system-specified)
1094 (epa-display-error context)
1095 (signal (car error) (cdr error))))
1096 (message "Signing...done")
1097 (delete-region start end)
1099 (add-text-properties (point)
1101 (insert (epa--decode-coding-string
1103 (or coding-system-for-read
1104 epa-last-coding-system-specified)))
1106 (list 'epa-coding-system-used
1107 epa-last-coding-system-specified
1113 (defalias 'epa--derived-mode-p
1114 (if (fboundp 'derived-mode-p)
1116 (lambda (&rest modes)
1117 "Non-nil if the current major mode is derived from one of MODES.
1118 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1119 (let ((parent major-mode))
1120 (while (and (not (memq parent modes))
1121 (setq parent (get parent 'derived-mode-parent))))
1125 (defun epa-encrypt-region (start end recipients sign signers)
1126 "Encrypt the current region between START and END for RECIPIENTS.
1128 Don't use this command in Lisp programs!
1129 Since this function operates on regions, it does some tricks such
1130 as coding-system detection and unibyte/multibyte conversion. If
1131 you are sure how the data should be treated, you should consider
1132 using the string based counterpart `epg-encrypt-string', or the
1133 file based counterpart `epg-encrypt-file' instead.
1137 \(let ((context (epg-make-context \\='OpenPGP)))
1140 (encode-coding-string (buffer-substring start end) \\='utf-8)
1142 (declare (interactive-only t))
1144 (let ((verbose current-prefix-arg)
1145 (context (epg-make-context epa-protocol))
1147 (setq epa-last-coding-system-specified
1148 (or coding-system-for-write
1149 (epa--select-safe-coding-system
1150 (region-beginning) (region-end))))
1151 (list (region-beginning) (region-end)
1152 (epa-select-keys context
1153 "Select recipients for encryption.
1154 If no one is selected, symmetric encryption will be performed. ")
1155 (setq sign (if verbose (y-or-n-p "Sign? ")))
1157 (epa-select-keys context
1158 "Select keys for signing. ")))))
1160 (let ((context (epg-make-context epa-protocol))
1162 ;;(setf (epg-context-armor context) epa-armor)
1163 (setf (epg-context-armor context) t)
1164 ;;(setf (epg-context-textmode context) epa-textmode)
1165 (setf (epg-context-textmode context) t)
1167 (setf (epg-context-signers context) signers))
1168 (epg-context-set-passphrase-callback context
1169 #'epa-passphrase-callback-function)
1170 (epg-context-set-progress-callback context
1172 #'epa-progress-callback-function
1174 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1175 (message "Encrypting...")
1176 (condition-case error
1177 (setq cipher (epg-encrypt-string context
1178 (epa--encode-coding-string
1179 (buffer-substring start end)
1180 epa-last-coding-system-specified)
1184 (epa-display-error context)
1185 (signal (car error) (cdr error))))
1186 (message "Encrypting...done")
1187 (delete-region start end)
1189 (add-text-properties (point)
1193 (list 'epa-coding-system-used
1194 epa-last-coding-system-specified
1201 (defun epa-delete-keys (keys &optional allow-secret)
1202 "Delete selected KEYS."
1204 (let ((keys (epa--marked-keys)))
1206 (error "No keys selected"))
1208 (eq (nth 1 epa-list-keys-arguments) t))))
1209 (let ((context (epg-make-context epa-protocol)))
1210 (message "Deleting...")
1211 (condition-case error
1212 (epg-delete-keys context keys allow-secret)
1214 (epa-display-error context)
1215 (signal (car error) (cdr error))))
1216 (message "Deleting...done")
1217 (apply #'epa--list-keys epa-list-keys-arguments)))
1220 (defun epa-import-keys (file)
1221 "Import keys from FILE."
1222 (interactive "fFile: ")
1223 (setq file (expand-file-name file))
1224 (let ((context (epg-make-context epa-protocol)))
1225 (message "Importing %s..." (file-name-nondirectory file))
1228 (epg-import-keys-from-file context file)
1229 (message "Importing %s...done" (file-name-nondirectory file)))
1231 (epa-display-error context)
1232 (message "Importing %s...failed" (file-name-nondirectory file))))
1233 (if (epg-context-result-for context 'import)
1234 (epa-display-info (epg-import-result-to-string
1235 (epg-context-result-for context 'import))))
1236 ;; FIXME: Why not use the (otherwise unused) epa--derived-mode-p?
1237 (if (eq major-mode 'epa-key-list-mode)
1238 (apply #'epa--list-keys epa-list-keys-arguments))))
1241 (defun epa-import-keys-region (start end)
1242 "Import keys from the region."
1244 (let ((context (epg-make-context epa-protocol)))
1245 (message "Importing...")
1248 (epg-import-keys-from-string context (buffer-substring start end))
1249 (message "Importing...done"))
1251 (epa-display-error context)
1252 (message "Importing...failed")))
1253 (if (epg-context-result-for context 'import)
1254 (epa-display-info (epg-import-result-to-string
1255 (epg-context-result-for context 'import))))))
1258 (defun epa-import-armor-in-region (start end)
1259 "Import keys in the OpenPGP armor format in the current region
1260 between START and END."
1264 (narrow-to-region start end)
1266 (let (armor-start armor-end)
1267 (while (re-search-forward
1268 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1270 (setq armor-start (match-beginning 0)
1271 armor-end (re-search-forward
1272 (concat "^-----END " (match-string 1) "-----$")
1275 (error "No armor tail"))
1276 (epa-import-keys-region armor-start armor-end))))))
1279 (defun epa-export-keys (keys file)
1280 "Export selected KEYS to FILE."
1282 (let ((keys (epa--marked-keys))
1285 (error "No keys selected"))
1288 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1289 (if epa-armor ".asc" ".gpg"))
1294 (concat "To file (default "
1295 (file-name-nondirectory default-name)
1297 (file-name-directory default-name)
1299 (let ((context (epg-make-context epa-protocol)))
1300 (setf (epg-context-armor context) epa-armor)
1301 (message "Exporting to %s..." (file-name-nondirectory file))
1302 (condition-case error
1303 (epg-export-keys-to-file context keys file)
1305 (epa-display-error context)
1306 (signal (car error) (cdr error))))
1307 (message "Exporting to %s...done" (file-name-nondirectory file))))
1310 (defun epa-insert-keys (keys)
1311 "Insert selected KEYS after the point."
1313 (list (epa-select-keys (epg-make-context epa-protocol)
1314 "Select keys to export.
1315 If no one is selected, default public key is exported. ")))
1316 (let ((context (epg-make-context epa-protocol)))
1317 ;;(setf (epg-context-armor context) epa-armor)
1318 (setf (epg-context-armor context) t)
1319 (condition-case error
1320 (insert (epg-export-keys-to-string context keys))
1322 (epa-display-error context)
1323 (signal (car error) (cdr error))))))
1325 ;; (defun epa-sign-keys (keys &optional local)
1326 ;; "Sign selected KEYS.
1327 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1329 ;; Don't use this command in Lisp programs!"
1330 ;; (declare (interactive-only t))
1332 ;; (let ((keys (epa--marked-keys)))
1334 ;; (error "No keys selected"))
1335 ;; (list keys current-prefix-arg)))
1336 ;; (let ((context (epg-make-context epa-protocol)))
1337 ;; (epg-context-set-passphrase-callback context
1338 ;; #'epa-passphrase-callback-function)
1339 ;; (epg-context-set-progress-callback context
1341 ;; #'epa-progress-callback-function
1342 ;; "Signing keys..."))
1343 ;; (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1344 ;; (message "Signing keys...")
1345 ;; (epg-sign-keys context keys local)
1346 ;; (message "Signing keys...done")))
1347 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1351 ;;; epa.el ends here