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 a 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 nil
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 (message "Decrypting %s..." (file-name-nondirectory decrypt-file))
705 (condition-case error
706 (epg-decrypt-file context decrypt-file plain-file)
708 (epa-display-error context)
709 (signal (car error) (cdr error))))
710 (message "Decrypting %s...wrote %s" (file-name-nondirectory decrypt-file)
711 (file-name-nondirectory plain-file))
712 (if (epg-context-result-for context 'verify)
713 (epa-display-info (epg-verify-result-to-string
714 (epg-context-result-for context 'verify))))))
717 (defun epa-verify-file (file)
719 (interactive "fFile: ")
720 (setq file (expand-file-name file))
721 (let* ((context (epg-make-context epa-protocol))
722 (plain (if (equal (file-name-extension file) "sig")
723 (file-name-sans-extension file))))
724 (epg-context-set-progress-callback context
726 #'epa-progress-callback-function
727 (format "Verifying %s..."
728 (file-name-nondirectory file))))
729 (message "Verifying %s..." (file-name-nondirectory file))
730 (condition-case error
731 (epg-verify-file context file plain)
733 (epa-display-error context)
734 (signal (car error) (cdr error))))
735 (message "Verifying %s...done" (file-name-nondirectory file))
736 (if (epg-context-result-for context 'verify)
737 (epa-display-info (epg-verify-result-to-string
738 (epg-context-result-for context 'verify))))))
740 (defun epa--read-signature-type ()
743 (message "Signature type (n,c,d,?) ")
748 (setq type 'detached))
750 (with-output-to-temp-buffer "*Help*"
751 (with-current-buffer standard-output
753 n - Create a normal signature
754 c - Create a cleartext signature
755 d - Create a detached signature
759 (setq type 'normal))))
763 (defun epa-sign-file (file signers mode)
764 "Sign FILE by SIGNERS keys selected."
766 (let ((verbose current-prefix-arg))
767 (list (expand-file-name (read-file-name "File: "))
769 (epa-select-keys (epg-make-context epa-protocol)
770 "Select keys for signing.
771 If no one is selected, default secret key is used. "
774 (epa--read-signature-type)
776 (let ((signature (concat file
777 (if (eq epa-protocol 'OpenPGP)
780 '(nil t normal detached))))
782 (if (memq mode '(t detached))
785 (if (memq mode '(t detached))
788 (context (epg-make-context epa-protocol)))
789 (setf (epg-context-armor context) epa-armor)
790 (setf (epg-context-textmode context) epa-textmode)
791 (setf (epg-context-signers context) signers)
792 (epg-context-set-passphrase-callback context
793 #'epa-passphrase-callback-function)
794 (epg-context-set-progress-callback context
796 #'epa-progress-callback-function
797 (format "Signing %s..."
798 (file-name-nondirectory file))))
799 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
800 (message "Signing %s..." (file-name-nondirectory file))
801 (condition-case error
802 (epg-sign-file context file signature mode)
804 (epa-display-error context)
805 (signal (car error) (cdr error))))
806 (message "Signing %s...wrote %s" (file-name-nondirectory file)
807 (file-name-nondirectory signature))))
810 (defun epa-encrypt-file (file recipients)
811 "Encrypt FILE for RECIPIENTS."
813 (list (expand-file-name (read-file-name "File: "))
814 (epa-select-keys (epg-make-context epa-protocol)
815 "Select recipients for encryption.
816 If no one is selected, symmetric encryption will be performed. ")))
817 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
818 (if epa-armor ".asc" ".gpg")
820 (context (epg-make-context epa-protocol)))
821 (setf (epg-context-armor context) epa-armor)
822 (setf (epg-context-textmode context) epa-textmode)
823 (epg-context-set-passphrase-callback context
824 #'epa-passphrase-callback-function)
825 (epg-context-set-progress-callback context
827 #'epa-progress-callback-function
828 (format "Encrypting %s..."
829 (file-name-nondirectory file))))
830 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
831 (message "Encrypting %s..." (file-name-nondirectory file))
832 (condition-case error
833 (epg-encrypt-file context file recipients cipher)
835 (epa-display-error context)
836 (signal (car error) (cdr error))))
837 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
838 (file-name-nondirectory cipher))))
841 (defun epa-decrypt-region (start end &optional make-buffer-function)
842 "Decrypt the current region between START and END.
844 If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
845 It should return that buffer. If it copies the input, it should
846 delete the text now being decrypted. It should leave point at the
847 proper place to insert the plaintext.
849 Be careful about using this command in Lisp programs!
850 Since this function operates on regions, it does some tricks such
851 as coding-system detection and unibyte/multibyte conversion. If
852 you are sure how the data in the region should be treated, you
853 should consider using the string based counterpart
854 `epg-decrypt-string', or the file based counterpart
855 `epg-decrypt-file' instead.
859 \(let ((context (epg-make-context \\='OpenPGP)))
860 (decode-coding-string
861 (epg-decrypt-string context (buffer-substring start end))
865 (let ((context (epg-make-context epa-protocol))
867 (epg-context-set-passphrase-callback context
868 #'epa-passphrase-callback-function)
869 (epg-context-set-progress-callback context
871 #'epa-progress-callback-function
873 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
874 (message "Decrypting...")
875 (condition-case error
876 (setq plain (epg-decrypt-string context (buffer-substring start end)))
878 (epa-display-error context)
879 (signal (car error) (cdr error))))
880 (message "Decrypting...done")
881 (setq plain (epa--decode-coding-string
883 (or coding-system-for-read
884 (get-text-property start 'epa-coding-system-used)
886 (if make-buffer-function
887 (with-current-buffer (funcall make-buffer-function)
888 (let ((inhibit-read-only t))
890 (if (or (eq epa-replace-original-text t)
891 (and epa-replace-original-text
892 (y-or-n-p "Replace the original text? ")))
893 (let ((inhibit-read-only t))
894 (delete-region start end)
897 (with-output-to-temp-buffer "*Temp*"
898 (set-buffer standard-output)
901 (if (epg-context-result-for context 'verify)
902 (epa-display-info (epg-verify-result-to-string
903 (epg-context-result-for context 'verify)))))))
905 (defun epa--find-coding-system-for-mime-charset (mime-charset)
906 (if (featurep 'xemacs)
907 (if (fboundp 'find-coding-system)
908 (find-coding-system mime-charset))
909 ;; Find the first coding system which corresponds to MIME-CHARSET.
910 (let ((pointer (coding-system-list)))
912 (not (eq (coding-system-get (car pointer) 'mime-charset)
914 (setq pointer (cdr pointer)))
918 (defun epa-decrypt-armor-in-region (start end)
919 "Decrypt OpenPGP armors in the current region between START and END.
921 Don't use this command in Lisp programs!
922 See the reason described in the `epa-decrypt-region' documentation."
923 (declare (interactive-only t))
927 (narrow-to-region start end)
929 (let (armor-start armor-end)
930 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
931 (setq armor-start (match-beginning 0)
932 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
935 (error "Encryption armor beginning has no matching end"))
936 (goto-char armor-start)
937 (let ((coding-system-for-read
938 (or coding-system-for-read
939 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
940 (epa--find-coding-system-for-mime-charset
941 (intern (downcase (match-string 1))))))))
942 (goto-char armor-end)
943 (epa-decrypt-region armor-start armor-end)))))))
946 (defun epa-verify-region (start end)
947 "Verify the current region between START and END.
949 Don't use this command in Lisp programs!
950 Since this function operates on regions, it does some tricks such
951 as coding-system detection and unibyte/multibyte conversion. If
952 you are sure how the data in the region should be treated, you
953 should consider using the string based counterpart
954 `epg-verify-string', or the file based counterpart
955 `epg-verify-file' instead.
959 \(let ((context (epg-make-context \\='OpenPGP)))
960 (decode-coding-string
961 (epg-verify-string context (buffer-substring start end))
963 (declare (interactive-only t))
965 (let ((context (epg-make-context epa-protocol))
967 (setf (epg-context-progress-callback context)
969 #'epa-progress-callback-function
971 (message "Verifying...")
972 (condition-case error
973 (setq plain (epg-verify-string
975 (epa--encode-coding-string
976 (buffer-substring start end)
977 (or coding-system-for-write
978 (get-text-property start 'epa-coding-system-used)))))
980 (epa-display-error context)
981 (signal (car error) (cdr error))))
982 (message "Verifying...done")
983 (setq plain (epa--decode-coding-string
985 (or coding-system-for-read
986 (get-text-property start 'epa-coding-system-used)
988 (if (or (eq epa-replace-original-text t)
989 (and epa-replace-original-text
990 (y-or-n-p "Replace the original text? ")))
991 (let ((inhibit-read-only t)
993 (delete-region start end)
996 (with-output-to-temp-buffer "*Temp*"
997 (set-buffer standard-output)
1000 (if (epg-context-result-for context 'verify)
1001 (epa-display-info (epg-verify-result-to-string
1002 (epg-context-result-for context 'verify))))))
1005 (defun epa-verify-cleartext-in-region (start end)
1006 "Verify OpenPGP cleartext signed messages in the current region
1007 between START and END.
1009 Don't use this command in Lisp programs!
1010 See the reason described in the `epa-verify-region' documentation."
1011 (declare (interactive-only t))
1015 (narrow-to-region start end)
1017 (let (cleartext-start cleartext-end)
1018 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
1020 (setq cleartext-start (match-beginning 0))
1021 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
1023 (error "Invalid cleartext signed message"))
1024 (setq cleartext-end (re-search-forward
1025 "^-----END PGP SIGNATURE-----$"
1027 (unless cleartext-end
1028 (error "No cleartext tail"))
1029 (epa-verify-region cleartext-start cleartext-end))))))
1031 (defalias 'epa--select-safe-coding-system
1032 (if (fboundp 'select-safe-coding-system)
1033 #'select-safe-coding-system
1035 buffer-file-coding-system)))
1038 (defun epa-sign-region (start end signers mode)
1039 "Sign the current region between START and END by SIGNERS keys selected.
1041 Don't use this command in Lisp programs!
1042 Since this function operates on regions, it does some tricks such
1043 as coding-system detection and unibyte/multibyte conversion. If
1044 you are sure how the data should be treated, you should consider
1045 using the string based counterpart `epg-sign-string', or the file
1046 based counterpart `epg-sign-file' instead.
1050 \(let ((context (epg-make-context \\='OpenPGP)))
1053 (encode-coding-string (buffer-substring start end) \\='utf-8)))"
1054 (declare (interactive-only t))
1056 (let ((verbose current-prefix-arg))
1057 (setq epa-last-coding-system-specified
1058 (or coding-system-for-write
1059 (epa--select-safe-coding-system
1060 (region-beginning) (region-end))))
1061 (list (region-beginning) (region-end)
1063 (epa-select-keys (epg-make-context epa-protocol)
1064 "Select keys for signing.
1065 If no one is selected, default secret key is used. "
1068 (epa--read-signature-type)
1071 (let ((context (epg-make-context epa-protocol))
1073 ;;(setf (epg-context-armor context) epa-armor)
1074 (setf (epg-context-armor context) t)
1075 ;;(setf (epg-context-textmode context) epa-textmode)
1076 (setf (epg-context-textmode context) t)
1077 (setf (epg-context-signers context) signers)
1078 (epg-context-set-passphrase-callback context
1079 #'epa-passphrase-callback-function)
1080 (epg-context-set-progress-callback context
1082 #'epa-progress-callback-function
1084 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1085 (message "Signing...")
1086 (condition-case error
1087 (setq signature (epg-sign-string context
1088 (epa--encode-coding-string
1089 (buffer-substring start end)
1090 epa-last-coding-system-specified)
1093 (epa-display-error context)
1094 (signal (car error) (cdr error))))
1095 (message "Signing...done")
1096 (delete-region start end)
1098 (add-text-properties (point)
1100 (insert (epa--decode-coding-string
1102 (or coding-system-for-read
1103 epa-last-coding-system-specified)))
1105 (list 'epa-coding-system-used
1106 epa-last-coding-system-specified
1112 (defalias 'epa--derived-mode-p
1113 (if (fboundp 'derived-mode-p)
1115 (lambda (&rest modes)
1116 "Non-nil if the current major mode is derived from one of MODES.
1117 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1118 (let ((parent major-mode))
1119 (while (and (not (memq parent modes))
1120 (setq parent (get parent 'derived-mode-parent))))
1124 (defun epa-encrypt-region (start end recipients sign signers)
1125 "Encrypt the current region between START and END for RECIPIENTS.
1127 Don't use this command in Lisp programs!
1128 Since this function operates on regions, it does some tricks such
1129 as coding-system detection and unibyte/multibyte conversion. If
1130 you are sure how the data should be treated, you should consider
1131 using the string based counterpart `epg-encrypt-string', or the
1132 file based counterpart `epg-encrypt-file' instead.
1136 \(let ((context (epg-make-context \\='OpenPGP)))
1139 (encode-coding-string (buffer-substring start end) \\='utf-8)
1141 (declare (interactive-only t))
1143 (let ((verbose current-prefix-arg)
1144 (context (epg-make-context epa-protocol))
1146 (setq epa-last-coding-system-specified
1147 (or coding-system-for-write
1148 (epa--select-safe-coding-system
1149 (region-beginning) (region-end))))
1150 (list (region-beginning) (region-end)
1151 (epa-select-keys context
1152 "Select recipients for encryption.
1153 If no one is selected, symmetric encryption will be performed. ")
1154 (setq sign (if verbose (y-or-n-p "Sign? ")))
1156 (epa-select-keys context
1157 "Select keys for signing. ")))))
1159 (let ((context (epg-make-context epa-protocol))
1161 ;;(setf (epg-context-armor context) epa-armor)
1162 (setf (epg-context-armor context) t)
1163 ;;(setf (epg-context-textmode context) epa-textmode)
1164 (setf (epg-context-textmode context) t)
1166 (setf (epg-context-signers context) signers))
1167 (epg-context-set-passphrase-callback context
1168 #'epa-passphrase-callback-function)
1169 (epg-context-set-progress-callback context
1171 #'epa-progress-callback-function
1173 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1174 (message "Encrypting...")
1175 (condition-case error
1176 (setq cipher (epg-encrypt-string context
1177 (epa--encode-coding-string
1178 (buffer-substring start end)
1179 epa-last-coding-system-specified)
1183 (epa-display-error context)
1184 (signal (car error) (cdr error))))
1185 (message "Encrypting...done")
1186 (delete-region start end)
1188 (add-text-properties (point)
1192 (list 'epa-coding-system-used
1193 epa-last-coding-system-specified
1200 (defun epa-delete-keys (keys &optional allow-secret)
1201 "Delete selected KEYS."
1203 (let ((keys (epa--marked-keys)))
1205 (error "No keys selected"))
1207 (eq (nth 1 epa-list-keys-arguments) t))))
1208 (let ((context (epg-make-context epa-protocol)))
1209 (message "Deleting...")
1210 (condition-case error
1211 (epg-delete-keys context keys allow-secret)
1213 (epa-display-error context)
1214 (signal (car error) (cdr error))))
1215 (message "Deleting...done")
1216 (apply #'epa--list-keys epa-list-keys-arguments)))
1219 (defun epa-import-keys (file)
1220 "Import keys from FILE."
1221 (interactive "fFile: ")
1222 (setq file (expand-file-name file))
1223 (let ((context (epg-make-context epa-protocol)))
1224 (message "Importing %s..." (file-name-nondirectory file))
1227 (epg-import-keys-from-file context file)
1228 (message "Importing %s...done" (file-name-nondirectory file)))
1230 (epa-display-error context)
1231 (message "Importing %s...failed" (file-name-nondirectory file))))
1232 (if (epg-context-result-for context 'import)
1233 (epa-display-info (epg-import-result-to-string
1234 (epg-context-result-for context 'import))))
1235 ;; FIXME: Why not use the (otherwise unused) epa--derived-mode-p?
1236 (if (eq major-mode 'epa-key-list-mode)
1237 (apply #'epa--list-keys epa-list-keys-arguments))))
1240 (defun epa-import-keys-region (start end)
1241 "Import keys from the region."
1243 (let ((context (epg-make-context epa-protocol)))
1244 (message "Importing...")
1247 (epg-import-keys-from-string context (buffer-substring start end))
1248 (message "Importing...done"))
1250 (epa-display-error context)
1251 (message "Importing...failed")))
1252 (if (epg-context-result-for context 'import)
1253 (epa-display-info (epg-import-result-to-string
1254 (epg-context-result-for context 'import))))))
1257 (defun epa-import-armor-in-region (start end)
1258 "Import keys in the OpenPGP armor format in the current region
1259 between START and END."
1263 (narrow-to-region start end)
1265 (let (armor-start armor-end)
1266 (while (re-search-forward
1267 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1269 (setq armor-start (match-beginning 0)
1270 armor-end (re-search-forward
1271 (concat "^-----END " (match-string 1) "-----$")
1274 (error "No armor tail"))
1275 (epa-import-keys-region armor-start armor-end))))))
1278 (defun epa-export-keys (keys file)
1279 "Export selected KEYS to FILE."
1281 (let ((keys (epa--marked-keys))
1284 (error "No keys selected"))
1287 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1288 (if epa-armor ".asc" ".gpg"))
1293 (concat "To file (default "
1294 (file-name-nondirectory default-name)
1296 (file-name-directory default-name)
1298 (let ((context (epg-make-context epa-protocol)))
1299 (setf (epg-context-armor context) epa-armor)
1300 (message "Exporting to %s..." (file-name-nondirectory file))
1301 (condition-case error
1302 (epg-export-keys-to-file context keys file)
1304 (epa-display-error context)
1305 (signal (car error) (cdr error))))
1306 (message "Exporting to %s...done" (file-name-nondirectory file))))
1309 (defun epa-insert-keys (keys)
1310 "Insert selected KEYS after the point."
1312 (list (epa-select-keys (epg-make-context epa-protocol)
1313 "Select keys to export.
1314 If no one is selected, default public key is exported. ")))
1315 (let ((context (epg-make-context epa-protocol)))
1316 ;;(setf (epg-context-armor context) epa-armor)
1317 (setf (epg-context-armor context) t)
1318 (condition-case error
1319 (insert (epg-export-keys-to-string context keys))
1321 (epa-display-error context)
1322 (signal (car error) (cdr error))))))
1324 ;; (defun epa-sign-keys (keys &optional local)
1325 ;; "Sign selected KEYS.
1326 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1328 ;; Don't use this command in Lisp programs!"
1329 ;; (declare (interactive-only t))
1331 ;; (let ((keys (epa--marked-keys)))
1333 ;; (error "No keys selected"))
1334 ;; (list keys current-prefix-arg)))
1335 ;; (let ((context (epg-make-context epa-protocol)))
1336 ;; (epg-context-set-passphrase-callback context
1337 ;; #'epa-passphrase-callback-function)
1338 ;; (epg-context-set-progress-callback context
1340 ;; #'epa-progress-callback-function
1341 ;; "Signing keys..."))
1342 ;; (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1343 ;; (message "Signing keys...")
1344 ;; (epg-sign-keys context keys local)
1345 ;; (message "Signing keys...done")))
1346 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1350 ;;; epa.el ends here