1 ;;; epa.el --- the EasyPG Assistant -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006-2016 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: PGP, GnuPG
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'wid-edit
))
32 "The EasyPG Assistant"
34 :link
'(custom-manual "(epa) Top")
37 (defcustom epa-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."
43 :type
'(choice (const :tag
"Never" nil
)
44 (const :tag
"Ask the user" ask
)
45 (const :tag
"Always" t
))
48 (defcustom epa-popup-info-window t
49 "If non-nil, display status information from epa commands in another window."
53 (defcustom epa-info-window-height
5
54 "Number of lines used to display status information."
58 (defcustom epa-pinentry-mode nil
61 GnuPG 2.1 or later has an option to control the behavior of
62 Pinentry invocation. Possible modes are: `ask', `cancel',
63 `error', and `loopback'. See the GnuPG manual for the meanings.
65 In epa commands, a particularly useful mode is `loopback', which
66 redirects all Pinentry queries to the caller, so Emacs can query
67 passphrase through the minibuffer, instead of external Pinentry
69 :type
'(choice (const nil
)
77 (defgroup epa-faces nil
82 (defcustom epa-mail-aliases nil
83 "Alist of aliases of email addresses that stand for encryption keys.
84 Each element is a list of email addresses (ALIAS EXPANSIONS...).
85 When one of the recipients of a message being encrypted is ALIAS,
86 instead of encrypting it for ALIAS, encrypt it for EXPANSIONS...
88 If EXPANSIONS is empty, ignore ALIAS as regards encryption.
89 This is a handy way to avoid warnings about addresses that you don't
92 The command `epa-mail-encrypt' uses this."
93 :type
'(repeat (cons (string :tag
"Alias") (repeat (string :tag
"Expansion"))))
97 (defface epa-validity-high
98 '((default :weight bold
)
99 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
100 "Face for high validity EPA information."
103 (defface epa-validity-medium
104 '((default :slant italic
)
105 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
106 "Face for medium validity EPA information."
109 (defface epa-validity-low
111 "Face used for displaying the low validity."
114 (defface epa-validity-disabled
115 '((t :slant italic
:inverse-video t
))
116 "Face used for displaying the disabled validity."
120 '((((class color
) (background dark
))
121 :foreground
"lightyellow")
122 (((class color
) (background light
))
123 :foreground
"blue4"))
124 "Face used for displaying the string."
128 '((default :weight bold
)
129 (((class color
) (background dark
)) :foreground
"orange")
130 (((class color
) (background light
)) :foreground
"red"))
131 "Face used for displaying the high validity."
134 (defface epa-field-name
135 '((default :weight bold
)
136 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
137 "Face for the name of the attribute field."
140 (defface epa-field-body
141 '((default :slant italic
)
142 (((class color
) (background dark
)) :foreground
"turquoise"))
143 "Face for the body of the attribute field."
146 (defcustom epa-validity-face-alist
147 '((unknown . epa-validity-disabled
)
148 (invalid . epa-validity-disabled
)
149 (disabled . epa-validity-disabled
)
150 (revoked . epa-validity-disabled
)
151 (expired . epa-validity-disabled
)
152 (none . epa-validity-low
)
153 (undefined . epa-validity-low
)
154 (never . epa-validity-low
)
155 (marginal . epa-validity-medium
)
156 (full . epa-validity-high
)
157 (ultimate . epa-validity-high
))
158 "An alist mapping validity values to faces."
159 :type
'(repeat (cons symbol face
))
162 (defvar epa-font-lock-keywords
165 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
167 (2 'epa-field-body
)))
168 "Default expressions to addon in epa-mode.")
170 (defconst epa-pubkey-algorithm-letter-alist
178 (defvar epa-protocol
'OpenPGP
179 "The default protocol.
180 The value can be either OpenPGP or CMS.
182 You should bind this variable with `let', but do not set it globally.")
184 (defvar epa-armor nil
185 "If non-nil, epa commands create ASCII armored output.
187 You should bind this variable with `let', but do not set it globally.")
189 (defvar epa-textmode nil
190 "If non-nil, epa commands treat input files as text.
192 You should bind this variable with `let', but do not set it globally.")
194 (defvar epa-keys-buffer nil
)
195 (defvar epa-key-buffer-alist nil
)
197 (defvar epa-list-keys-arguments nil
)
198 (defvar epa-info-buffer nil
)
199 (defvar epa-error-buffer nil
)
200 (defvar epa-last-coding-system-specified nil
)
202 (defvar epa-key-list-mode-map
203 (let ((keymap (make-sparse-keymap))
204 (menu-map (make-sparse-keymap)))
205 (define-key keymap
"m" 'epa-mark-key
)
206 (define-key keymap
"u" 'epa-unmark-key
)
207 (define-key keymap
"d" 'epa-decrypt-file
)
208 (define-key keymap
"v" 'epa-verify-file
)
209 (define-key keymap
"s" 'epa-sign-file
)
210 (define-key keymap
"e" 'epa-encrypt-file
)
211 (define-key keymap
"r" 'epa-delete-keys
)
212 (define-key keymap
"i" 'epa-import-keys
)
213 (define-key keymap
"o" 'epa-export-keys
)
214 (define-key keymap
"g" 'revert-buffer
)
215 (define-key keymap
"n" 'next-line
)
216 (define-key keymap
"p" 'previous-line
)
217 (define-key keymap
" " 'scroll-up-command
)
218 (define-key keymap
[?\S-\
] 'scroll-down-command
)
219 (define-key keymap
[delete] 'scroll-down-command)
220 (define-key keymap "q" 'epa-exit-buffer)
221 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
222 (define-key menu-map [epa-key-list-unmark-key]
223 '(menu-item "Unmark Key" epa-unmark-key
224 :help "Unmark a key"))
225 (define-key menu-map [epa-key-list-mark-key]
226 '(menu-item "Mark Key" epa-mark-key
228 (define-key menu-map [separator-epa-file] '(menu-item "--"))
229 (define-key menu-map [epa-verify-file]
230 '(menu-item "Verify File..." epa-verify-file
231 :help "Verify FILE"))
232 (define-key menu-map [epa-sign-file]
233 '(menu-item "Sign File..." epa-sign-file
234 :help "Sign FILE by SIGNERS keys selected"))
235 (define-key menu-map [epa-decrypt-file]
236 '(menu-item "Decrypt File..." epa-decrypt-file
237 :help "Decrypt FILE"))
238 (define-key menu-map [epa-encrypt-file]
239 '(menu-item "Encrypt File..." epa-encrypt-file
240 :help "Encrypt FILE for RECIPIENTS"))
241 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
242 (define-key menu-map [epa-key-list-delete-keys]
243 '(menu-item "Delete Keys" epa-delete-keys
244 :help "Delete Marked Keys"))
245 (define-key menu-map [epa-key-list-import-keys]
246 '(menu-item "Import Keys" epa-import-keys
247 :help "Import keys from a file"))
248 (define-key menu-map [epa-key-list-export-keys]
249 '(menu-item "Export Keys" epa-export-keys
250 :help "Export marked keys to a file"))
253 (defvar epa-key-mode-map
254 (let ((keymap (make-sparse-keymap)))
255 (define-key keymap "q" 'epa-exit-buffer)
258 (defvar epa-info-mode-map
259 (let ((keymap (make-sparse-keymap)))
260 (define-key keymap "q" 'delete-window)
263 (defvar epa-exit-buffer-function #'quit-window)
265 (define-widget 'epa-key 'push-button
266 "Button for representing a epg-key object."
268 :button-face-get 'epa--key-widget-button-face-get
269 :value-create 'epa--key-widget-value-create
270 :action 'epa--key-widget-action
271 :help-echo 'epa--key-widget-help-echo)
273 (defun epa--key-widget-action (widget &optional _event)
274 (save-selected-window
275 (epa--show-key (widget-get widget :value))))
277 (defun epa--key-widget-value-create (widget)
278 (let* ((key (widget-get widget :value))
279 (primary-sub-key (car (epg-key-sub-key-list key)))
280 (primary-user-id (car (epg-key-user-id-list key))))
281 (insert (format "%c "
282 (if (epg-sub-key-validity primary-sub-key)
283 (car (rassq (epg-sub-key-validity primary-sub-key)
284 epg-key-validity-alist))
286 (epg-sub-key-id primary-sub-key)
289 (if (stringp (epg-user-id-string primary-user-id))
290 (epg-user-id-string primary-user-id)
291 (epg-decode-dn (epg-user-id-string primary-user-id)))
294 (defun epa--key-widget-button-face-get (widget)
295 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
296 (widget-get widget :value))))))
298 (cdr (assq validity epa-validity-face-alist))
301 (defun epa--key-widget-help-echo (widget)
303 (epg-sub-key-id (car (epg-key-sub-key-list
304 (widget-get widget :value))))))
306 (defalias 'epa--encode-coding-string
307 (if (fboundp 'encode-coding-string) #'encode-coding-string #'identity))
309 (defalias 'epa--decode-coding-string
310 (if (fboundp 'decode-coding-string) #'decode-coding-string #'identity))
312 (define-derived-mode epa-key-list-mode special-mode "Keys"
313 "Major mode for `epa-list-keys'."
314 (buffer-disable-undo)
315 (setq truncate-lines t
317 (setq-local font-lock-defaults '(epa-font-lock-keywords t))
318 ;; In XEmacs, auto-initialization of font-lock is not effective
319 ;; if buffer-file-name is not set.
320 (font-lock-set-defaults)
321 (make-local-variable 'epa-exit-buffer-function)
322 (setq-local revert-buffer-function #'epa--key-list-revert-buffer))
324 (define-derived-mode epa-key-mode special-mode "Key"
325 "Major mode for a key description."
326 (buffer-disable-undo)
327 (setq truncate-lines t
329 (setq-local font-lock-defaults '(epa-font-lock-keywords t))
330 ;; In XEmacs, auto-initialization of font-lock is not effective
331 ;; if buffer-file-name is not set.
332 (font-lock-set-defaults)
333 (make-local-variable 'epa-exit-buffer-function))
335 (define-derived-mode epa-info-mode special-mode "Info"
336 "Major mode for `epa-info-buffer'."
337 (buffer-disable-undo)
338 (setq truncate-lines t
341 (defun epa-mark-key (&optional arg)
342 "Mark a key on the current line.
343 If ARG is non-nil, unmark the key."
345 (let ((inhibit-read-only t)
349 (unless (get-text-property (point) 'epa-key)
350 (error "No key on this line"))
351 (setq properties (text-properties-at (point)))
353 (insert (if arg " " "*"))
354 (set-text-properties (1- (point)) (point) properties)
357 (defun epa-unmark-key (&optional arg)
358 "Unmark a key on the current line.
359 If ARG is non-nil, mark the key."
361 (epa-mark-key (not arg)))
363 (defun epa-exit-buffer ()
364 "Exit the current buffer.
365 `epa-exit-buffer-function' is called if it is set."
367 (funcall epa-exit-buffer-function))
369 (defun epa--insert-keys (keys)
372 (narrow-to-region (point) (point))
377 (add-text-properties point (point)
378 (list 'epa-key (car keys)
383 (widget-create 'epa-key :value (car keys))
385 (setq keys (cdr keys))))
386 (add-text-properties (point-min) (point-max)
387 (list 'epa-list-keys t
393 (defun epa--list-keys (name secret)
394 (unless (and epa-keys-buffer
395 (buffer-live-p epa-keys-buffer))
396 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
397 (set-buffer epa-keys-buffer)
399 (let ((inhibit-read-only t)
402 (context (epg-make-context epa-protocol)))
403 (unless (get-text-property point 'epa-list-keys)
404 (setq point (next-single-property-change point 'epa-list-keys)))
407 (or (next-single-property-change point 'epa-list-keys)
410 (epa--insert-keys (epg-list-keys context name secret))
412 (set-keymap-parent (current-local-map) widget-keymap))
413 (make-local-variable 'epa-list-keys-arguments)
414 (setq epa-list-keys-arguments (list name secret))
415 (goto-char (point-min))
416 (pop-to-buffer (current-buffer)))
419 (defun epa-list-keys (&optional name)
420 "List all keys matched with NAME from the public keyring."
422 (if current-prefix-arg
423 (let ((name (read-string "Pattern: "
424 (if epa-list-keys-arguments
425 (car epa-list-keys-arguments)))))
426 (list (if (equal name "") nil name)))
428 (epa--list-keys name nil))
431 (defun epa-list-secret-keys (&optional name)
432 "List all keys matched with NAME from the private keyring."
434 (if current-prefix-arg
435 (let ((name (read-string "Pattern: "
436 (if epa-list-keys-arguments
437 (car epa-list-keys-arguments)))))
438 (list (if (equal name "") nil name)))
440 (epa--list-keys name t))
442 (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm)
443 (apply #'epa--list-keys epa-list-keys-arguments))
445 (defun epa--marked-keys ()
446 (or (with-current-buffer epa-keys-buffer
447 (goto-char (point-min))
449 (while (re-search-forward "^\\*" nil t)
450 (if (setq key (get-text-property (match-beginning 0)
452 (setq keys (cons key keys))))
454 (let ((key (get-text-property (point-at-bol) 'epa-key)))
458 (defun epa--select-keys (prompt keys)
459 (unless (and epa-keys-buffer
460 (buffer-live-p epa-keys-buffer))
461 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
462 (with-current-buffer epa-keys-buffer
464 ;; C-c C-c is the usual way to finish the selection (bug#11159).
465 (define-key (current-local-map) "\C-c\C-c" 'exit-recursive-edit)
466 (let ((inhibit-read-only t)
470 (substitute-command-keys "\
471 - `\\[epa-mark-key]' to mark a key on the line
472 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
474 :notify (lambda (&rest _ignore) (abort-recursive-edit))
476 "Click here or \\[abort-recursive-edit] to cancel"
479 :notify (lambda (&rest _ignore) (exit-recursive-edit))
481 "Click here or \\[exit-recursive-edit] to finish"
484 (epa--insert-keys keys)
486 (set-keymap-parent (current-local-map) widget-keymap)
487 (setq epa-exit-buffer-function #'abort-recursive-edit)
488 (goto-char (point-min))
489 (let ((display-buffer-mark-dedicated 'soft))
490 (pop-to-buffer (current-buffer))))
495 (kill-buffer epa-keys-buffer))))
498 (defun epa-select-keys (context prompt &optional names secret)
499 "Display a user's keyring and ask him to select keys.
500 CONTEXT is an epg-context.
501 PROMPT is a string to prompt with.
502 NAMES is a list of strings to be matched with keys. If it is nil, all
504 If SECRET is non-nil, list secret keys instead of public keys."
505 (let ((keys (epg-list-keys context names secret)))
506 (epa--select-keys prompt keys)))
508 (defun epa--show-key (key)
509 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
510 (entry (assoc (epg-sub-key-id primary-sub-key)
511 epa-key-buffer-alist))
512 (inhibit-read-only t)
516 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
517 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
518 (unless (and (cdr entry)
519 (buffer-live-p (cdr entry)))
520 (setcdr entry (generate-new-buffer
521 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
522 (set-buffer (cdr entry))
524 (make-local-variable 'epa-key)
527 (setq pointer (epg-key-user-id-list key))
531 (if (epg-user-id-validity (car pointer))
533 (car (rassq (epg-user-id-validity (car pointer))
534 epg-key-validity-alist)))
537 (if (stringp (epg-user-id-string (car pointer)))
538 (epg-user-id-string (car pointer))
539 (epg-decode-dn (epg-user-id-string (car pointer))))
541 (setq pointer (cdr pointer)))
542 (setq pointer (epg-key-sub-key-list key))
545 (if (epg-sub-key-validity (car pointer))
547 (car (rassq (epg-sub-key-validity (car pointer))
548 epg-key-validity-alist)))
551 (epg-sub-key-id (car pointer))
554 (epg-sub-key-length (car pointer)))
556 (cdr (assq (epg-sub-key-algorithm (car pointer))
557 epg-pubkey-algorithm-alist))
560 (format-time-string "%Y-%m-%d"
561 (epg-sub-key-creation-time (car pointer)))
562 (error "????-??-??"))
563 (if (epg-sub-key-expiration-time (car pointer))
564 (format (if (time-less-p (current-time)
565 (epg-sub-key-expiration-time
570 (format-time-string "%Y-%m-%d"
571 (epg-sub-key-expiration-time
573 (error "????-??-??")))
576 (mapconcat #'symbol-name
577 (epg-sub-key-capability (car pointer))
580 (epg-sub-key-fingerprint (car pointer))
582 (setq pointer (cdr pointer)))
583 (goto-char (point-min))
584 (pop-to-buffer (current-buffer))))
586 (defun epa-display-info (info)
587 (if epa-popup-info-window
588 (save-selected-window
589 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
590 (setq epa-info-buffer (generate-new-buffer "*Info*")))
591 (if (get-buffer-window epa-info-buffer)
592 (delete-window (get-buffer-window epa-info-buffer)))
593 (with-current-buffer epa-info-buffer
594 (let ((inhibit-read-only t)
599 (goto-char (point-min)))
600 (if (> (window-height)
601 epa-info-window-height)
602 (set-window-buffer (split-window nil (- (window-height)
603 epa-info-window-height))
605 (pop-to-buffer epa-info-buffer)
606 (if (> (window-height) epa-info-window-height)
607 (shrink-window (- (window-height) epa-info-window-height)))))
608 (message "%s" info)))
610 (defun epa-display-error (context)
611 (unless (equal (epg-context-error-output context) "")
612 (let ((buffer (get-buffer-create "*Error*")))
613 (save-selected-window
614 (unless (and epa-error-buffer (buffer-live-p epa-error-buffer))
615 (setq epa-error-buffer (generate-new-buffer "*Error*")))
616 (if (get-buffer-window epa-error-buffer)
617 (delete-window (get-buffer-window epa-error-buffer)))
618 (with-current-buffer buffer
619 (let ((inhibit-read-only t)
623 (pcase (epg-context-operation context)
624 (`decrypt "Error while decrypting with \"%s\":")
625 (`verify "Error while verifying with \"%s\":")
626 (`sign "Error while signing with \"%s\":")
627 (`encrypt "Error while encrypting with \"%s\":")
628 (`import-keys "Error while importing keys with \"%s\":")
629 (`export-keys "Error while exporting keys with \"%s\":")
630 (_ "Error while executing \"%s\":\n\n"))
633 (epg-context-error-output context)))
635 (goto-char (point-min)))
636 (display-buffer buffer)))))
638 (defun epa-display-verify-result (verify-result)
639 (declare (obsolete epa-display-info "23.1"))
640 (epa-display-info (epg-verify-result-to-string verify-result)))
642 (defun epa-passphrase-callback-function (context key-id handback)
645 (format "Passphrase for symmetric encryption%s: "
646 ;; Add the file name to the prompt, if any.
647 (if (stringp handback)
648 (format " for %s" handback)
650 (eq (epg-context-operation context) 'encrypt))
653 "Passphrase for PIN: "
654 (let ((entry (assoc key-id epg-user-id-alist)))
656 (format "Passphrase for %s %s: " key-id (cdr entry))
657 (format "Passphrase for %s: " key-id)))))))
659 (defun epa-progress-callback-function (_context what _char current total
661 (let ((prompt (or handback
662 (format "Processing %s: " what))))
663 ;; According to gnupg/doc/DETAIL: a "total" of 0 indicates that
664 ;; the total amount is not known. The condition TOTAL && CUR ==
665 ;; TOTAL may be used to detect the end of an operation.
667 (if (= current total)
668 (message "%s...done" prompt)
669 (message "%s...%d%%" prompt
670 (floor (* 100.0 current) total)))
671 (message "%s..." prompt))))
673 (defun epa-read-file-name (input)
674 "Interactively read an output file name based on INPUT file name."
675 (setq input (file-name-sans-extension (expand-file-name input)))
678 (concat "To file (default " (file-name-nondirectory input) ") ")
679 (file-name-directory input)
683 (defun epa-decrypt-file (decrypt-file &optional plain-file)
684 "Decrypt DECRYPT-FILE into PLAIN-FILE.
685 If you do not specify PLAIN-FILE, this functions prompts for the value to use."
687 (let* ((file (read-file-name "File to decrypt: "))
688 (plain (epa-read-file-name file)))
690 (or plain-file (setq plain-file (epa-read-file-name decrypt-file)))
691 (setq decrypt-file (expand-file-name decrypt-file))
692 (let ((context (epg-make-context epa-protocol)))
693 (epg-context-set-passphrase-callback context
694 #'epa-passphrase-callback-function)
695 (epg-context-set-progress-callback context
697 #'epa-progress-callback-function
698 (format "Decrypting %s..."
699 (file-name-nondirectory decrypt-file))))
700 (message "Decrypting %s..." (file-name-nondirectory decrypt-file))
701 (condition-case error
702 (epg-decrypt-file context decrypt-file plain-file)
704 (epa-display-error context)
705 (signal (car error) (cdr error))))
706 (message "Decrypting %s...wrote %s" (file-name-nondirectory decrypt-file)
707 (file-name-nondirectory plain-file))
708 (if (epg-context-result-for context 'verify)
709 (epa-display-info (epg-verify-result-to-string
710 (epg-context-result-for context 'verify))))))
713 (defun epa-verify-file (file)
715 (interactive "fFile: ")
716 (setq file (expand-file-name file))
717 (let* ((context (epg-make-context epa-protocol))
718 (plain (if (equal (file-name-extension file) "sig")
719 (file-name-sans-extension file))))
720 (epg-context-set-progress-callback context
722 #'epa-progress-callback-function
723 (format "Verifying %s..."
724 (file-name-nondirectory file))))
725 (message "Verifying %s..." (file-name-nondirectory file))
726 (condition-case error
727 (epg-verify-file context file plain)
729 (epa-display-error context)
730 (signal (car error) (cdr error))))
731 (message "Verifying %s...done" (file-name-nondirectory file))
732 (if (epg-context-result-for context 'verify)
733 (epa-display-info (epg-verify-result-to-string
734 (epg-context-result-for context 'verify))))))
736 (defun epa--read-signature-type ()
739 (message "Signature type (n,c,d,?) ")
744 (setq type 'detached))
746 (with-output-to-temp-buffer "*Help*"
747 (with-current-buffer standard-output
749 n - Create a normal signature
750 c - Create a cleartext signature
751 d - Create a detached signature
755 (setq type 'normal))))
759 (defun epa-sign-file (file signers mode)
760 "Sign FILE by SIGNERS keys selected."
762 (let ((verbose current-prefix-arg))
763 (list (expand-file-name (read-file-name "File: "))
765 (epa-select-keys (epg-make-context epa-protocol)
766 "Select keys for signing.
767 If no one is selected, default secret key is used. "
770 (epa--read-signature-type)
772 (let ((signature (concat file
773 (if (eq epa-protocol 'OpenPGP)
776 '(nil t normal detached))))
778 (if (memq mode '(t detached))
781 (if (memq mode '(t detached))
784 (context (epg-make-context epa-protocol)))
785 (setf (epg-context-armor context) epa-armor)
786 (setf (epg-context-textmode context) epa-textmode)
787 (setf (epg-context-signers context) signers)
788 (epg-context-set-passphrase-callback context
789 #'epa-passphrase-callback-function)
790 (epg-context-set-progress-callback context
792 #'epa-progress-callback-function
793 (format "Signing %s..."
794 (file-name-nondirectory file))))
795 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
796 (message "Signing %s..." (file-name-nondirectory file))
797 (condition-case error
798 (epg-sign-file context file signature mode)
800 (epa-display-error context)
801 (signal (car error) (cdr error))))
802 (message "Signing %s...wrote %s" (file-name-nondirectory file)
803 (file-name-nondirectory signature))))
806 (defun epa-encrypt-file (file recipients)
807 "Encrypt FILE for RECIPIENTS."
809 (list (expand-file-name (read-file-name "File: "))
810 (epa-select-keys (epg-make-context epa-protocol)
811 "Select recipients for encryption.
812 If no one is selected, symmetric encryption will be performed. ")))
813 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
814 (if epa-armor ".asc" ".gpg")
816 (context (epg-make-context epa-protocol)))
817 (setf (epg-context-armor context) epa-armor)
818 (setf (epg-context-textmode context) epa-textmode)
819 (epg-context-set-passphrase-callback context
820 #'epa-passphrase-callback-function)
821 (epg-context-set-progress-callback context
823 #'epa-progress-callback-function
824 (format "Encrypting %s..."
825 (file-name-nondirectory file))))
826 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
827 (message "Encrypting %s..." (file-name-nondirectory file))
828 (condition-case error
829 (epg-encrypt-file context file recipients cipher)
831 (epa-display-error context)
832 (signal (car error) (cdr error))))
833 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
834 (file-name-nondirectory cipher))))
837 (defun epa-decrypt-region (start end &optional make-buffer-function)
838 "Decrypt the current region between START and END.
840 If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
841 It should return that buffer. If it copies the input, it should
842 delete the text now being decrypted. It should leave point at the
843 proper place to insert the plaintext.
845 Be careful about using this command in Lisp programs!
846 Since this function operates on regions, it does some tricks such
847 as coding-system detection and unibyte/multibyte conversion. If
848 you are sure how the data in the region should be treated, you
849 should consider using the string based counterpart
850 `epg-decrypt-string', or the file based counterpart
851 `epg-decrypt-file' instead.
855 \(let ((context (epg-make-context \\='OpenPGP)))
856 (decode-coding-string
857 (epg-decrypt-string context (buffer-substring start end))
861 (let ((context (epg-make-context epa-protocol))
863 (epg-context-set-passphrase-callback context
864 #'epa-passphrase-callback-function)
865 (epg-context-set-progress-callback context
867 #'epa-progress-callback-function
869 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
870 (message "Decrypting...")
871 (condition-case error
872 (setq plain (epg-decrypt-string context (buffer-substring start end)))
874 (epa-display-error context)
875 (signal (car error) (cdr error))))
876 (message "Decrypting...done")
877 (setq plain (epa--decode-coding-string
879 (or coding-system-for-read
880 (get-text-property start 'epa-coding-system-used)
882 (if make-buffer-function
883 (with-current-buffer (funcall make-buffer-function)
884 (let ((inhibit-read-only t))
886 (if (or (eq epa-replace-original-text t)
887 (and epa-replace-original-text
888 (y-or-n-p "Replace the original text? ")))
889 (let ((inhibit-read-only t))
890 (delete-region start end)
893 (with-output-to-temp-buffer "*Temp*"
894 (set-buffer standard-output)
897 (if (epg-context-result-for context 'verify)
898 (epa-display-info (epg-verify-result-to-string
899 (epg-context-result-for context 'verify)))))))
901 (defun epa--find-coding-system-for-mime-charset (mime-charset)
902 (if (featurep 'xemacs)
903 (if (fboundp 'find-coding-system)
904 (find-coding-system mime-charset))
905 ;; Find the first coding system which corresponds to MIME-CHARSET.
906 (let ((pointer (coding-system-list)))
908 (not (eq (coding-system-get (car pointer) 'mime-charset)
910 (setq pointer (cdr pointer)))
914 (defun epa-decrypt-armor-in-region (start end)
915 "Decrypt OpenPGP armors in the current region between START and END.
917 Don't use this command in Lisp programs!
918 See the reason described in the `epa-decrypt-region' documentation."
919 (declare (interactive-only t))
923 (narrow-to-region start end)
925 (let (armor-start armor-end)
926 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
927 (setq armor-start (match-beginning 0)
928 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
931 (error "Encryption armor beginning has no matching end"))
932 (goto-char armor-start)
933 (let ((coding-system-for-read
934 (or coding-system-for-read
935 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
936 (epa--find-coding-system-for-mime-charset
937 (intern (downcase (match-string 1))))))))
938 (goto-char armor-end)
939 (epa-decrypt-region armor-start armor-end)))))))
942 (defun epa-verify-region (start end)
943 "Verify the current region between START and END.
945 Don't use this command in Lisp programs!
946 Since this function operates on regions, it does some tricks such
947 as coding-system detection and unibyte/multibyte conversion. If
948 you are sure how the data in the region should be treated, you
949 should consider using the string based counterpart
950 `epg-verify-string', or the file based counterpart
951 `epg-verify-file' instead.
955 \(let ((context (epg-make-context \\='OpenPGP)))
956 (decode-coding-string
957 (epg-verify-string context (buffer-substring start end))
959 (declare (interactive-only t))
961 (let ((context (epg-make-context epa-protocol))
963 (setf (epg-context-progress-callback context)
965 #'epa-progress-callback-function
967 (message "Verifying...")
968 (condition-case error
969 (setq plain (epg-verify-string
971 (epa--encode-coding-string
972 (buffer-substring start end)
973 (or coding-system-for-write
974 (get-text-property start 'epa-coding-system-used)))))
976 (epa-display-error context)
977 (signal (car error) (cdr error))))
978 (message "Verifying...done")
979 (setq plain (epa--decode-coding-string
981 (or coding-system-for-read
982 (get-text-property start 'epa-coding-system-used)
984 (if (or (eq epa-replace-original-text t)
985 (and epa-replace-original-text
986 (y-or-n-p "Replace the original text? ")))
987 (let ((inhibit-read-only t)
989 (delete-region start end)
992 (with-output-to-temp-buffer "*Temp*"
993 (set-buffer standard-output)
996 (if (epg-context-result-for context 'verify)
997 (epa-display-info (epg-verify-result-to-string
998 (epg-context-result-for context 'verify))))))
1001 (defun epa-verify-cleartext-in-region (start end)
1002 "Verify OpenPGP cleartext signed messages in the current region
1003 between START and END.
1005 Don't use this command in Lisp programs!
1006 See the reason described in the `epa-verify-region' documentation."
1007 (declare (interactive-only t))
1011 (narrow-to-region start end)
1013 (let (cleartext-start cleartext-end)
1014 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
1016 (setq cleartext-start (match-beginning 0))
1017 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
1019 (error "Invalid cleartext signed message"))
1020 (setq cleartext-end (re-search-forward
1021 "^-----END PGP SIGNATURE-----$"
1023 (unless cleartext-end
1024 (error "No cleartext tail"))
1025 (epa-verify-region cleartext-start cleartext-end))))))
1027 (defalias 'epa--select-safe-coding-system
1028 (if (fboundp 'select-safe-coding-system)
1029 #'select-safe-coding-system
1031 buffer-file-coding-system)))
1034 (defun epa-sign-region (start end signers mode)
1035 "Sign the current region between START and END by SIGNERS keys selected.
1037 Don't use this command in Lisp programs!
1038 Since this function operates on regions, it does some tricks such
1039 as coding-system detection and unibyte/multibyte conversion. If
1040 you are sure how the data should be treated, you should consider
1041 using the string based counterpart `epg-sign-string', or the file
1042 based counterpart `epg-sign-file' instead.
1046 \(let ((context (epg-make-context \\='OpenPGP)))
1049 (encode-coding-string (buffer-substring start end) \\='utf-8)))"
1050 (declare (interactive-only t))
1052 (let ((verbose current-prefix-arg))
1053 (setq epa-last-coding-system-specified
1054 (or coding-system-for-write
1055 (epa--select-safe-coding-system
1056 (region-beginning) (region-end))))
1057 (list (region-beginning) (region-end)
1059 (epa-select-keys (epg-make-context epa-protocol)
1060 "Select keys for signing.
1061 If no one is selected, default secret key is used. "
1064 (epa--read-signature-type)
1067 (let ((context (epg-make-context epa-protocol))
1069 ;;(setf (epg-context-armor context) epa-armor)
1070 (setf (epg-context-armor context) t)
1071 ;;(setf (epg-context-textmode context) epa-textmode)
1072 (setf (epg-context-textmode context) t)
1073 (setf (epg-context-signers context) signers)
1074 (epg-context-set-passphrase-callback context
1075 #'epa-passphrase-callback-function)
1076 (epg-context-set-progress-callback context
1078 #'epa-progress-callback-function
1080 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1081 (message "Signing...")
1082 (condition-case error
1083 (setq signature (epg-sign-string context
1084 (epa--encode-coding-string
1085 (buffer-substring start end)
1086 epa-last-coding-system-specified)
1089 (epa-display-error context)
1090 (signal (car error) (cdr error))))
1091 (message "Signing...done")
1092 (delete-region start end)
1094 (add-text-properties (point)
1096 (insert (epa--decode-coding-string
1098 (or coding-system-for-read
1099 epa-last-coding-system-specified)))
1101 (list 'epa-coding-system-used
1102 epa-last-coding-system-specified
1108 (defalias 'epa--derived-mode-p
1109 (if (fboundp 'derived-mode-p)
1111 (lambda (&rest modes)
1112 "Non-nil if the current major mode is derived from one of MODES.
1113 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1114 (let ((parent major-mode))
1115 (while (and (not (memq parent modes))
1116 (setq parent (get parent 'derived-mode-parent))))
1120 (defun epa-encrypt-region (start end recipients sign signers)
1121 "Encrypt the current region between START and END for RECIPIENTS.
1123 Don't use this command in Lisp programs!
1124 Since this function operates on regions, it does some tricks such
1125 as coding-system detection and unibyte/multibyte conversion. If
1126 you are sure how the data should be treated, you should consider
1127 using the string based counterpart `epg-encrypt-string', or the
1128 file based counterpart `epg-encrypt-file' instead.
1132 \(let ((context (epg-make-context \\='OpenPGP)))
1135 (encode-coding-string (buffer-substring start end) \\='utf-8)
1137 (declare (interactive-only t))
1139 (let ((verbose current-prefix-arg)
1140 (context (epg-make-context epa-protocol))
1142 (setq epa-last-coding-system-specified
1143 (or coding-system-for-write
1144 (epa--select-safe-coding-system
1145 (region-beginning) (region-end))))
1146 (list (region-beginning) (region-end)
1147 (epa-select-keys context
1148 "Select recipients for encryption.
1149 If no one is selected, symmetric encryption will be performed. ")
1150 (setq sign (if verbose (y-or-n-p "Sign? ")))
1152 (epa-select-keys context
1153 "Select keys for signing. ")))))
1155 (let ((context (epg-make-context epa-protocol))
1157 ;;(setf (epg-context-armor context) epa-armor)
1158 (setf (epg-context-armor context) t)
1159 ;;(setf (epg-context-textmode context) epa-textmode)
1160 (setf (epg-context-textmode context) t)
1162 (setf (epg-context-signers context) signers))
1163 (epg-context-set-passphrase-callback context
1164 #'epa-passphrase-callback-function)
1165 (epg-context-set-progress-callback context
1167 #'epa-progress-callback-function
1169 (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1170 (message "Encrypting...")
1171 (condition-case error
1172 (setq cipher (epg-encrypt-string context
1173 (epa--encode-coding-string
1174 (buffer-substring start end)
1175 epa-last-coding-system-specified)
1179 (epa-display-error context)
1180 (signal (car error) (cdr error))))
1181 (message "Encrypting...done")
1182 (delete-region start end)
1184 (add-text-properties (point)
1188 (list 'epa-coding-system-used
1189 epa-last-coding-system-specified
1196 (defun epa-delete-keys (keys &optional allow-secret)
1197 "Delete selected KEYS."
1199 (let ((keys (epa--marked-keys)))
1201 (error "No keys selected"))
1203 (eq (nth 1 epa-list-keys-arguments) t))))
1204 (let ((context (epg-make-context epa-protocol)))
1205 (message "Deleting...")
1206 (condition-case error
1207 (epg-delete-keys context keys allow-secret)
1209 (epa-display-error context)
1210 (signal (car error) (cdr error))))
1211 (message "Deleting...done")
1212 (apply #'epa--list-keys epa-list-keys-arguments)))
1215 (defun epa-import-keys (file)
1216 "Import keys from FILE."
1217 (interactive "fFile: ")
1218 (setq file (expand-file-name file))
1219 (let ((context (epg-make-context epa-protocol)))
1220 (message "Importing %s..." (file-name-nondirectory file))
1223 (epg-import-keys-from-file context file)
1224 (message "Importing %s...done" (file-name-nondirectory file)))
1226 (epa-display-error context)
1227 (message "Importing %s...failed" (file-name-nondirectory file))))
1228 (if (epg-context-result-for context 'import)
1229 (epa-display-info (epg-import-result-to-string
1230 (epg-context-result-for context 'import))))
1231 ;; FIXME: Why not use the (otherwise unused) epa--derived-mode-p?
1232 (if (eq major-mode 'epa-key-list-mode)
1233 (apply #'epa--list-keys epa-list-keys-arguments))))
1236 (defun epa-import-keys-region (start end)
1237 "Import keys from the region."
1239 (let ((context (epg-make-context epa-protocol)))
1240 (message "Importing...")
1243 (epg-import-keys-from-string context (buffer-substring start end))
1244 (message "Importing...done"))
1246 (epa-display-error context)
1247 (message "Importing...failed")))
1248 (if (epg-context-result-for context 'import)
1249 (epa-display-info (epg-import-result-to-string
1250 (epg-context-result-for context 'import))))))
1253 (defun epa-import-armor-in-region (start end)
1254 "Import keys in the OpenPGP armor format in the current region
1255 between START and END."
1259 (narrow-to-region start end)
1261 (let (armor-start armor-end)
1262 (while (re-search-forward
1263 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1265 (setq armor-start (match-beginning 0)
1266 armor-end (re-search-forward
1267 (concat "^-----END " (match-string 1) "-----$")
1270 (error "No armor tail"))
1271 (epa-import-keys-region armor-start armor-end))))))
1274 (defun epa-export-keys (keys file)
1275 "Export selected KEYS to FILE."
1277 (let ((keys (epa--marked-keys))
1280 (error "No keys selected"))
1283 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1284 (if epa-armor ".asc" ".gpg"))
1289 (concat "To file (default "
1290 (file-name-nondirectory default-name)
1292 (file-name-directory default-name)
1294 (let ((context (epg-make-context epa-protocol)))
1295 (setf (epg-context-armor context) epa-armor)
1296 (message "Exporting to %s..." (file-name-nondirectory file))
1297 (condition-case error
1298 (epg-export-keys-to-file context keys file)
1300 (epa-display-error context)
1301 (signal (car error) (cdr error))))
1302 (message "Exporting to %s...done" (file-name-nondirectory file))))
1305 (defun epa-insert-keys (keys)
1306 "Insert selected KEYS after the point."
1308 (list (epa-select-keys (epg-make-context epa-protocol)
1309 "Select keys to export.
1310 If no one is selected, default public key is exported. ")))
1311 (let ((context (epg-make-context epa-protocol)))
1312 ;;(setf (epg-context-armor context) epa-armor)
1313 (setf (epg-context-armor context) t)
1314 (condition-case error
1315 (insert (epg-export-keys-to-string context keys))
1317 (epa-display-error context)
1318 (signal (car error) (cdr error))))))
1320 ;; (defun epa-sign-keys (keys &optional local)
1321 ;; "Sign selected KEYS.
1322 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1324 ;; Don't use this command in Lisp programs!"
1325 ;; (declare (interactive-only t))
1327 ;; (let ((keys (epa--marked-keys)))
1329 ;; (error "No keys selected"))
1330 ;; (list keys current-prefix-arg)))
1331 ;; (let ((context (epg-make-context epa-protocol)))
1332 ;; (epg-context-set-passphrase-callback context
1333 ;; #'epa-passphrase-callback-function)
1334 ;; (epg-context-set-progress-callback context
1336 ;; #'epa-progress-callback-function
1337 ;; "Signing keys..."))
1338 ;; (setf (epg-context-pinentry-mode context) epa-pinentry-mode)
1339 ;; (message "Signing keys...")
1340 ;; (epg-sign-keys context keys local)
1341 ;; (message "Signing keys...done")))
1342 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1346 ;;; epa.el ends here