1 ;;; epa.el --- the EasyPG Assistant -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006-2013 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"
36 (defcustom epa-popup-info-window t
37 "If non-nil, display status information from epa commands in another window."
41 (defcustom epa-info-window-height
5
42 "Number of lines used to display status information."
46 (defgroup epa-faces nil
51 (defcustom epa-mail-aliases nil
52 "Alist of aliases of email addresses that stand for encryption keys.
53 Each element is (ALIAS EXPANSIONS...).
54 It means that when a message is addressed to ALIAS,
55 instead of encrypting it for ALIAS, encrypt it for EXPANSIONS...
56 If EXPANSIONS is empty, ignore ALIAS as regards encryption.
57 That is a handy way to avoid warnings about addresses
58 that you don't have any key for."
59 :type
'(repeat (cons (string :tag
"Alias") (repeat '(string :tag
"Expansion"))))
63 (defface epa-validity-high
64 '((default :weight bold
)
65 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
66 "Face for high validity EPA information."
69 (defface epa-validity-medium
70 '((default :slant italic
)
71 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
72 "Face for medium validity EPA information."
75 (defface epa-validity-low
77 "Face used for displaying the low validity."
80 (defface epa-validity-disabled
81 '((t :slant italic
:inverse-video t
))
82 "Face used for displaying the disabled validity."
86 '((((class color
) (background dark
))
87 :foreground
"lightyellow")
88 (((class color
) (background light
))
90 "Face used for displaying the string."
94 '((default :weight bold
)
95 (((class color
) (background dark
)) :foreground
"orange")
96 (((class color
) (background light
)) :foreground
"red"))
97 "Face used for displaying the high validity."
100 (defface epa-field-name
101 '((default :weight bold
)
102 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
103 "Face for the name of the attribute field."
106 (defface epa-field-body
107 '((default :slant italic
)
108 (((class color
) (background dark
)) :foreground
"turquoise"))
109 "Face for the body of the attribute field."
112 (defcustom epa-validity-face-alist
113 '((unknown . epa-validity-disabled
)
114 (invalid . epa-validity-disabled
)
115 (disabled . epa-validity-disabled
)
116 (revoked . epa-validity-disabled
)
117 (expired . epa-validity-disabled
)
118 (none . epa-validity-low
)
119 (undefined . epa-validity-low
)
120 (never . epa-validity-low
)
121 (marginal . epa-validity-medium
)
122 (full . epa-validity-high
)
123 (ultimate . epa-validity-high
))
124 "An alist mapping validity values to faces."
125 :type
'(repeat (cons symbol face
))
128 (defvar epa-font-lock-keywords
131 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
133 (2 'epa-field-body
)))
134 "Default expressions to addon in epa-mode.")
136 (defconst epa-pubkey-algorithm-letter-alist
144 (defvar epa-protocol
'OpenPGP
145 "The default protocol.
146 The value can be either OpenPGP or CMS.
148 You should bind this variable with `let', but do not set it globally.")
150 (defvar epa-armor nil
151 "If non-nil, epa commands create ASCII armored output.
153 You should bind this variable with `let', but do not set it globally.")
155 (defvar epa-textmode nil
156 "If non-nil, epa commands treat input files as text.
158 You should bind this variable with `let', but do not set it globally.")
160 (defvar epa-keys-buffer nil
)
161 (defvar epa-key-buffer-alist nil
)
163 (defvar epa-list-keys-arguments nil
)
164 (defvar epa-info-buffer nil
)
165 (defvar epa-last-coding-system-specified nil
)
167 (defvar epa-key-list-mode-map
168 (let ((keymap (make-sparse-keymap))
169 (menu-map (make-sparse-keymap)))
170 (define-key keymap
"m" 'epa-mark-key
)
171 (define-key keymap
"u" 'epa-unmark-key
)
172 (define-key keymap
"d" 'epa-decrypt-file
)
173 (define-key keymap
"v" 'epa-verify-file
)
174 (define-key keymap
"s" 'epa-sign-file
)
175 (define-key keymap
"e" 'epa-encrypt-file
)
176 (define-key keymap
"r" 'epa-delete-keys
)
177 (define-key keymap
"i" 'epa-import-keys
)
178 (define-key keymap
"o" 'epa-export-keys
)
179 (define-key keymap
"g" 'revert-buffer
)
180 (define-key keymap
"n" 'next-line
)
181 (define-key keymap
"p" 'previous-line
)
182 (define-key keymap
" " 'scroll-up-command
)
183 (define-key keymap
[?\S-\
] 'scroll-down-command
)
184 (define-key keymap
[delete] 'scroll-down-command)
185 (define-key keymap "q" 'epa-exit-buffer)
186 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
187 (define-key menu-map [epa-key-list-unmark-key]
188 '(menu-item "Unmark Key" epa-unmark-key
189 :help "Unmark a key"))
190 (define-key menu-map [epa-key-list-mark-key]
191 '(menu-item "Mark Key" epa-mark-key
193 (define-key menu-map [separator-epa-file] '(menu-item "--"))
194 (define-key menu-map [epa-verify-file]
195 '(menu-item "Verify File..." epa-verify-file
196 :help "Verify FILE"))
197 (define-key menu-map [epa-sign-file]
198 '(menu-item "Sign File..." epa-sign-file
199 :help "Sign FILE by SIGNERS keys selected"))
200 (define-key menu-map [epa-decrypt-file]
201 '(menu-item "Decrypt File..." epa-decrypt-file
202 :help "Decrypt FILE"))
203 (define-key menu-map [epa-encrypt-file]
204 '(menu-item "Encrypt File..." epa-encrypt-file
205 :help "Encrypt FILE for RECIPIENTS"))
206 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
207 (define-key menu-map [epa-key-list-delete-keys]
208 '(menu-item "Delete Keys" epa-delete-keys
209 :help "Delete Marked Keys"))
210 (define-key menu-map [epa-key-list-import-keys]
211 '(menu-item "Import Keys" epa-import-keys
212 :help "Import keys from a file"))
213 (define-key menu-map [epa-key-list-export-keys]
214 '(menu-item "Export Keys" epa-export-keys
215 :help "Export marked keys to a file"))
218 (defvar epa-key-mode-map
219 (let ((keymap (make-sparse-keymap)))
220 (define-key keymap "q" 'epa-exit-buffer)
223 (defvar epa-info-mode-map
224 (let ((keymap (make-sparse-keymap)))
225 (define-key keymap "q" 'delete-window)
228 (defvar epa-exit-buffer-function #'bury-buffer)
230 (define-widget 'epa-key 'push-button
231 "Button for representing a epg-key object."
233 :button-face-get 'epa--key-widget-button-face-get
234 :value-create 'epa--key-widget-value-create
235 :action 'epa--key-widget-action
236 :help-echo 'epa--key-widget-help-echo)
238 (defun epa--key-widget-action (widget &optional _event)
239 (save-selected-window
240 (epa--show-key (widget-get widget :value))))
242 (defun epa--key-widget-value-create (widget)
243 (let* ((key (widget-get widget :value))
244 (primary-sub-key (car (epg-key-sub-key-list key)))
245 (primary-user-id (car (epg-key-user-id-list key))))
246 (insert (format "%c "
247 (if (epg-sub-key-validity primary-sub-key)
248 (car (rassq (epg-sub-key-validity primary-sub-key)
249 epg-key-validity-alist))
251 (epg-sub-key-id primary-sub-key)
254 (if (stringp (epg-user-id-string primary-user-id))
255 (epg-user-id-string primary-user-id)
256 (epg-decode-dn (epg-user-id-string primary-user-id)))
259 (defun epa--key-widget-button-face-get (widget)
260 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
261 (widget-get widget :value))))))
263 (cdr (assq validity epa-validity-face-alist))
266 (defun epa--key-widget-help-echo (widget)
268 (epg-sub-key-id (car (epg-key-sub-key-list
269 (widget-get widget :value))))))
272 (if (fboundp 'encode-coding-string)
273 (defalias 'epa--encode-coding-string 'encode-coding-string)
274 (defalias 'epa--encode-coding-string 'identity)))
277 (if (fboundp 'decode-coding-string)
278 (defalias 'epa--decode-coding-string 'decode-coding-string)
279 (defalias 'epa--decode-coding-string 'identity)))
281 (defun epa-key-list-mode ()
282 "Major mode for `epa-list-keys'."
283 (kill-all-local-variables)
284 (buffer-disable-undo)
285 (setq major-mode 'epa-key-list-mode
289 (use-local-map epa-key-list-mode-map)
290 (make-local-variable 'font-lock-defaults)
291 (setq font-lock-defaults '(epa-font-lock-keywords t))
292 ;; In XEmacs, auto-initialization of font-lock is not effective
293 ;; if buffer-file-name is not set.
294 (font-lock-set-defaults)
295 (make-local-variable 'epa-exit-buffer-function)
296 (make-local-variable 'revert-buffer-function)
297 (setq revert-buffer-function 'epa--key-list-revert-buffer)
298 (run-mode-hooks 'epa-key-list-mode-hook))
300 (defun epa-key-mode ()
301 "Major mode for a key description."
302 (kill-all-local-variables)
303 (buffer-disable-undo)
304 (setq major-mode 'epa-key-mode
308 (use-local-map epa-key-mode-map)
309 (make-local-variable 'font-lock-defaults)
310 (setq font-lock-defaults '(epa-font-lock-keywords t))
311 ;; In XEmacs, auto-initialization of font-lock is not effective
312 ;; if buffer-file-name is not set.
313 (font-lock-set-defaults)
314 (make-local-variable 'epa-exit-buffer-function)
315 (run-mode-hooks 'epa-key-mode-hook))
317 (defun epa-info-mode ()
318 "Major mode for `epa-info-buffer'."
319 (kill-all-local-variables)
320 (buffer-disable-undo)
321 (setq major-mode 'epa-info-mode
325 (use-local-map epa-info-mode-map)
326 (run-mode-hooks 'epa-info-mode-hook))
328 (defun epa-mark-key (&optional arg)
329 "Mark a key on the current line.
330 If ARG is non-nil, unmark the key."
332 (let ((inhibit-read-only t)
336 (unless (get-text-property (point) 'epa-key)
337 (error "No key on this line"))
338 (setq properties (text-properties-at (point)))
340 (insert (if arg " " "*"))
341 (set-text-properties (1- (point)) (point) properties)
344 (defun epa-unmark-key (&optional arg)
345 "Unmark a key on the current line.
346 If ARG is non-nil, mark the key."
348 (epa-mark-key (not arg)))
350 (defun epa-exit-buffer ()
351 "Exit the current buffer.
352 `epa-exit-buffer-function' is called if it is set."
354 (funcall epa-exit-buffer-function))
356 (defun epa--insert-keys (keys)
359 (narrow-to-region (point) (point))
364 (add-text-properties point (point)
365 (list 'epa-key (car keys)
370 (widget-create 'epa-key :value (car keys))
372 (setq keys (cdr keys))))
373 (add-text-properties (point-min) (point-max)
374 (list 'epa-list-keys t
380 (defun epa--list-keys (name secret)
381 (unless (and epa-keys-buffer
382 (buffer-live-p epa-keys-buffer))
383 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
384 (set-buffer epa-keys-buffer)
386 (let ((inhibit-read-only t)
389 (context (epg-make-context epa-protocol)))
390 (unless (get-text-property point 'epa-list-keys)
391 (setq point (next-single-property-change point 'epa-list-keys)))
394 (or (next-single-property-change point 'epa-list-keys)
397 (epa--insert-keys (epg-list-keys context name secret))
399 (set-keymap-parent (current-local-map) widget-keymap))
400 (make-local-variable 'epa-list-keys-arguments)
401 (setq epa-list-keys-arguments (list name secret))
402 (goto-char (point-min))
403 (pop-to-buffer (current-buffer)))
406 (defun epa-list-keys (&optional name)
407 "List all keys matched with NAME from the public keyring."
409 (if current-prefix-arg
410 (let ((name (read-string "Pattern: "
411 (if epa-list-keys-arguments
412 (car epa-list-keys-arguments)))))
413 (list (if (equal name "") nil name)))
415 (epa--list-keys name nil))
418 (defun epa-list-secret-keys (&optional name)
419 "List all keys matched with NAME from the private keyring."
421 (if current-prefix-arg
422 (let ((name (read-string "Pattern: "
423 (if epa-list-keys-arguments
424 (car epa-list-keys-arguments)))))
425 (list (if (equal name "") nil name)))
427 (epa--list-keys name t))
429 (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm)
430 (apply #'epa--list-keys epa-list-keys-arguments))
432 (defun epa--marked-keys ()
433 (or (with-current-buffer epa-keys-buffer
434 (goto-char (point-min))
436 (while (re-search-forward "^\\*" nil t)
437 (if (setq key (get-text-property (match-beginning 0)
439 (setq keys (cons key keys))))
441 (let ((key (get-text-property (point-at-bol) 'epa-key)))
445 (defun epa--select-keys (prompt keys)
446 (unless (and epa-keys-buffer
447 (buffer-live-p epa-keys-buffer))
448 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
449 (with-current-buffer epa-keys-buffer
451 ;; C-c C-c is the usual way to finish the selection (bug#11159).
452 (define-key (current-local-map) "\C-c\C-c" 'exit-recursive-edit)
453 (let ((inhibit-read-only t)
457 (substitute-command-keys "\
458 - `\\[epa-mark-key]' to mark a key on the line
459 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
461 :notify (lambda (&rest _ignore) (abort-recursive-edit))
463 (substitute-command-keys
464 "Click here or \\[abort-recursive-edit] to cancel")
467 :notify (lambda (&rest _ignore) (exit-recursive-edit))
469 (substitute-command-keys
470 "Click here or \\[exit-recursive-edit] to finish")
473 (epa--insert-keys keys)
475 (set-keymap-parent (current-local-map) widget-keymap)
476 (setq epa-exit-buffer-function #'abort-recursive-edit)
477 (goto-char (point-min))
478 (let ((display-buffer-mark-dedicated 'soft))
479 (pop-to-buffer (current-buffer))))
484 (kill-buffer epa-keys-buffer))))
487 (defun epa-select-keys (context prompt &optional names secret)
488 "Display a user's keyring and ask him to select keys.
489 CONTEXT is an epg-context.
490 PROMPT is a string to prompt with.
491 NAMES is a list of strings to be matched with keys. If it is nil, all
493 If SECRET is non-nil, list secret keys instead of public keys."
494 (let ((keys (epg-list-keys context names secret)))
495 (epa--select-keys prompt keys)))
497 (defun epa--show-key (key)
498 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
499 (entry (assoc (epg-sub-key-id primary-sub-key)
500 epa-key-buffer-alist))
501 (inhibit-read-only t)
505 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
506 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
507 (unless (and (cdr entry)
508 (buffer-live-p (cdr entry)))
509 (setcdr entry (generate-new-buffer
510 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
511 (set-buffer (cdr entry))
513 (make-local-variable 'epa-key)
516 (setq pointer (epg-key-user-id-list key))
520 (if (epg-user-id-validity (car pointer))
522 (car (rassq (epg-user-id-validity (car pointer))
523 epg-key-validity-alist)))
526 (if (stringp (epg-user-id-string (car pointer)))
527 (epg-user-id-string (car pointer))
528 (epg-decode-dn (epg-user-id-string (car pointer))))
530 (setq pointer (cdr pointer)))
531 (setq pointer (epg-key-sub-key-list key))
534 (if (epg-sub-key-validity (car pointer))
536 (car (rassq (epg-sub-key-validity (car pointer))
537 epg-key-validity-alist)))
540 (epg-sub-key-id (car pointer))
543 (epg-sub-key-length (car pointer)))
545 (cdr (assq (epg-sub-key-algorithm (car pointer))
546 epg-pubkey-algorithm-alist))
549 (format-time-string "%Y-%m-%d"
550 (epg-sub-key-creation-time (car pointer)))
551 (error "????-??-??"))
552 (if (epg-sub-key-expiration-time (car pointer))
553 (format (if (time-less-p (current-time)
554 (epg-sub-key-expiration-time
559 (format-time-string "%Y-%m-%d"
560 (epg-sub-key-expiration-time
562 (error "????-??-??")))
565 (mapconcat #'symbol-name
566 (epg-sub-key-capability (car pointer))
569 (epg-sub-key-fingerprint (car pointer))
571 (setq pointer (cdr pointer)))
572 (goto-char (point-min))
573 (pop-to-buffer (current-buffer))))
575 (defun epa-display-info (info)
576 (if epa-popup-info-window
577 (save-selected-window
578 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
579 (setq epa-info-buffer (generate-new-buffer "*Info*")))
580 (if (get-buffer-window epa-info-buffer)
581 (delete-window (get-buffer-window epa-info-buffer)))
582 (with-current-buffer epa-info-buffer
583 (let ((inhibit-read-only t)
588 (goto-char (point-min)))
589 (if (> (window-height)
590 epa-info-window-height)
591 (set-window-buffer (split-window nil (- (window-height)
592 epa-info-window-height))
594 (pop-to-buffer epa-info-buffer)
595 (if (> (window-height) epa-info-window-height)
596 (shrink-window (- (window-height) epa-info-window-height)))))
597 (message "%s" info)))
599 (defun epa-display-verify-result (verify-result)
600 (declare (obsolete epa-display-info "23.1"))
601 (epa-display-info (epg-verify-result-to-string verify-result)))
603 (defun epa-passphrase-callback-function (context key-id handback)
606 (format "Passphrase for symmetric encryption%s: "
607 ;; Add the file name to the prompt, if any.
608 (if (stringp handback)
609 (format " for %s" handback)
611 (eq (epg-context-operation context) 'encrypt))
614 "Passphrase for PIN: "
615 (let ((entry (assoc key-id epg-user-id-alist)))
617 (format "Passphrase for %s %s: " key-id (cdr entry))
618 (format "Passphrase for %s: " key-id)))))))
620 (defun epa-progress-callback-function (_context what _char current total
622 (let ((prompt (or handback
623 (format "Processing %s: " what))))
624 ;; According to gnupg/doc/DETAIL: a "total" of 0 indicates that
625 ;; the total amount is not known. The condition TOTAL && CUR ==
626 ;; TOTAL may be used to detect the end of an operation.
628 (if (= current total)
629 (message "%s...done" prompt)
630 (message "%s...%d%%" prompt
631 (floor (* (/ current (float total)) 100))))
632 (message "%s..." prompt))))
634 (defun epa-read-file-name (input)
635 "Interactively read an output file name based on INPUT file name."
636 (setq input (file-name-sans-extension (expand-file-name input)))
639 (concat "To file (default " (file-name-nondirectory input) ") ")
640 (file-name-directory input)
644 (defun epa-decrypt-file (decrypt-file &optional plain-file)
645 "Decrypt DECRYPT-FILE into PLAIN-FILE.
646 If you do not specify PLAIN-FILE, this functions prompts for the value to use."
648 (let* ((file (read-file-name "File to decrypt: "))
649 (plain (epa-read-file-name file)))
651 (or plain-file (setq plain-file (epa-read-file-name decrypt-file)))
652 (setq decrypt-file (expand-file-name decrypt-file))
653 (let ((context (epg-make-context epa-protocol)))
654 (epg-context-set-passphrase-callback context
655 #'epa-passphrase-callback-function)
656 (epg-context-set-progress-callback context
658 #'epa-progress-callback-function
659 (format "Decrypting %s..."
660 (file-name-nondirectory decrypt-file))))
661 (message "Decrypting %s..." (file-name-nondirectory decrypt-file))
662 (epg-decrypt-file context decrypt-file plain-file)
663 (message "Decrypting %s...wrote %s" (file-name-nondirectory decrypt-file)
664 (file-name-nondirectory plain-file))
665 (if (epg-context-result-for context 'verify)
666 (epa-display-info (epg-verify-result-to-string
667 (epg-context-result-for context 'verify))))))
670 (defun epa-verify-file (file)
672 (interactive "fFile: ")
673 (setq file (expand-file-name file))
674 (let* ((context (epg-make-context epa-protocol))
675 (plain (if (equal (file-name-extension file) "sig")
676 (file-name-sans-extension file))))
677 (epg-context-set-progress-callback context
679 #'epa-progress-callback-function
680 (format "Verifying %s..."
681 (file-name-nondirectory file))))
682 (message "Verifying %s..." (file-name-nondirectory file))
683 (epg-verify-file context file plain)
684 (message "Verifying %s...done" (file-name-nondirectory file))
685 (if (epg-context-result-for context 'verify)
686 (epa-display-info (epg-verify-result-to-string
687 (epg-context-result-for context 'verify))))))
689 (defun epa--read-signature-type ()
692 (message "Signature type (n,c,d,?) ")
697 (setq type 'detached))
699 (with-output-to-temp-buffer "*Help*"
700 (with-current-buffer standard-output
702 n - Create a normal signature
703 c - Create a cleartext signature
704 d - Create a detached signature
708 (setq type 'normal))))
712 (defun epa-sign-file (file signers mode)
713 "Sign FILE by SIGNERS keys selected."
715 (let ((verbose current-prefix-arg))
716 (list (expand-file-name (read-file-name "File: "))
718 (epa-select-keys (epg-make-context epa-protocol)
719 "Select keys for signing.
720 If no one is selected, default secret key is used. "
723 (epa--read-signature-type)
725 (let ((signature (concat file
726 (if (eq epa-protocol 'OpenPGP)
729 '(nil t normal detached))))
731 (if (memq mode '(t detached))
734 (if (memq mode '(t detached))
737 (context (epg-make-context epa-protocol)))
738 (epg-context-set-armor context epa-armor)
739 (epg-context-set-textmode context epa-textmode)
740 (epg-context-set-signers context signers)
741 (epg-context-set-passphrase-callback context
742 #'epa-passphrase-callback-function)
743 (epg-context-set-progress-callback context
745 #'epa-progress-callback-function
746 (format "Signing %s..."
747 (file-name-nondirectory file))))
748 (message "Signing %s..." (file-name-nondirectory file))
749 (epg-sign-file context file signature mode)
750 (message "Signing %s...wrote %s" (file-name-nondirectory file)
751 (file-name-nondirectory signature))))
754 (defun epa-encrypt-file (file recipients)
755 "Encrypt FILE for RECIPIENTS."
757 (list (expand-file-name (read-file-name "File: "))
758 (epa-select-keys (epg-make-context epa-protocol)
759 "Select recipients for encryption.
760 If no one is selected, symmetric encryption will be performed. ")))
761 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
762 (if epa-armor ".asc" ".gpg")
764 (context (epg-make-context epa-protocol)))
765 (epg-context-set-armor context epa-armor)
766 (epg-context-set-textmode context epa-textmode)
767 (epg-context-set-passphrase-callback context
768 #'epa-passphrase-callback-function)
769 (epg-context-set-progress-callback context
771 #'epa-progress-callback-function
772 (format "Encrypting %s..."
773 (file-name-nondirectory file))))
774 (message "Encrypting %s..." (file-name-nondirectory file))
775 (epg-encrypt-file context file recipients cipher)
776 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
777 (file-name-nondirectory cipher))))
780 (defun epa-decrypt-region (start end &optional make-buffer-function)
781 "Decrypt the current region between START and END.
783 If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
784 It should return that buffer. If it copies the input, it should
785 delete the text now being decrypted. It should leave point at the
786 proper place to insert the plaintext.
788 Be careful about using this command in Lisp programs!
789 Since this function operates on regions, it does some tricks such
790 as coding-system detection and unibyte/multibyte conversion. If
791 you are sure how the data in the region should be treated, you
792 should consider using the string based counterpart
793 `epg-decrypt-string', or the file based counterpart
794 `epg-decrypt-file' instead.
798 \(let ((context (epg-make-context 'OpenPGP)))
799 (decode-coding-string
800 (epg-decrypt-string context (buffer-substring start end))
804 (let ((context (epg-make-context epa-protocol))
806 (epg-context-set-passphrase-callback context
807 #'epa-passphrase-callback-function)
808 (epg-context-set-progress-callback context
810 #'epa-progress-callback-function
812 (message "Decrypting...")
813 (setq plain (epg-decrypt-string context (buffer-substring start end)))
814 (message "Decrypting...done")
815 (setq plain (epa--decode-coding-string
817 (or coding-system-for-read
818 (get-text-property start 'epa-coding-system-used)
820 (if make-buffer-function
821 (with-current-buffer (funcall make-buffer-function)
822 (let ((inhibit-read-only t))
824 (if (y-or-n-p "Replace the original text? ")
825 (let ((inhibit-read-only t))
826 (delete-region start end)
829 (with-output-to-temp-buffer "*Temp*"
830 (set-buffer standard-output)
833 (if (epg-context-result-for context 'verify)
834 (epa-display-info (epg-verify-result-to-string
835 (epg-context-result-for context 'verify)))))))
837 (defun epa--find-coding-system-for-mime-charset (mime-charset)
838 (if (featurep 'xemacs)
839 (if (fboundp 'find-coding-system)
840 (find-coding-system mime-charset))
841 ;; Find the first coding system which corresponds to MIME-CHARSET.
842 (let ((pointer (coding-system-list)))
844 (not (eq (coding-system-get (car pointer) 'mime-charset)
846 (setq pointer (cdr pointer)))
850 (defun epa-decrypt-armor-in-region (start end)
851 "Decrypt OpenPGP armors in the current region between START and END.
853 Don't use this command in Lisp programs!
854 See the reason described in the `epa-decrypt-region' documentation."
858 (narrow-to-region start end)
860 (let (armor-start armor-end)
861 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
862 (setq armor-start (match-beginning 0)
863 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
866 (error "Encryption armor beginning has no matching end"))
867 (goto-char armor-start)
868 (let ((coding-system-for-read
869 (or coding-system-for-read
870 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
871 (epa--find-coding-system-for-mime-charset
872 (intern (downcase (match-string 1))))))))
873 (goto-char armor-end)
874 (epa-decrypt-region armor-start armor-end)))))))
877 (defun epa-verify-region (start end)
878 "Verify the current region between START and END.
880 Don't use this command in Lisp programs!
881 Since this function operates on regions, it does some tricks such
882 as coding-system detection and unibyte/multibyte conversion. If
883 you are sure how the data in the region should be treated, you
884 should consider using the string based counterpart
885 `epg-verify-string', or the file based counterpart
886 `epg-verify-file' instead.
890 \(let ((context (epg-make-context 'OpenPGP)))
891 (decode-coding-string
892 (epg-verify-string context (buffer-substring start end))
895 (let ((context (epg-make-context epa-protocol))
897 (epg-context-set-progress-callback context
899 #'epa-progress-callback-function
901 (message "Verifying...")
902 (setq plain (epg-verify-string
904 (epa--encode-coding-string
905 (buffer-substring start end)
906 (or coding-system-for-write
907 (get-text-property start 'epa-coding-system-used)))))
908 (message "Verifying...done")
909 (setq plain (epa--decode-coding-string
911 (or coding-system-for-read
912 (get-text-property start 'epa-coding-system-used)
914 (if (y-or-n-p "Replace the original text? ")
915 (let ((inhibit-read-only t)
917 (delete-region start end)
920 (with-output-to-temp-buffer "*Temp*"
921 (set-buffer standard-output)
924 (if (epg-context-result-for context 'verify)
925 (epa-display-info (epg-verify-result-to-string
926 (epg-context-result-for context 'verify))))))
929 (defun epa-verify-cleartext-in-region (start end)
930 "Verify OpenPGP cleartext signed messages in the current region
931 between START and END.
933 Don't use this command in Lisp programs!
934 See the reason described in the `epa-verify-region' documentation."
938 (narrow-to-region start end)
940 (let (cleartext-start cleartext-end)
941 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
943 (setq cleartext-start (match-beginning 0))
944 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
946 (error "Invalid cleartext signed message"))
947 (setq cleartext-end (re-search-forward
948 "^-----END PGP SIGNATURE-----$"
950 (unless cleartext-end
951 (error "No cleartext tail"))
952 (epa-verify-region cleartext-start cleartext-end))))))
955 (if (fboundp 'select-safe-coding-system)
956 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system)
957 (defun epa--select-safe-coding-system (_from _to)
958 buffer-file-coding-system)))
961 (defun epa-sign-region (start end signers mode)
962 "Sign the current region between START and END by SIGNERS keys selected.
964 Don't use this command in Lisp programs!
965 Since this function operates on regions, it does some tricks such
966 as coding-system detection and unibyte/multibyte conversion. If
967 you are sure how the data should be treated, you should consider
968 using the string based counterpart `epg-sign-string', or the file
969 based counterpart `epg-sign-file' instead.
973 \(let ((context (epg-make-context 'OpenPGP)))
976 (encode-coding-string (buffer-substring start end) 'utf-8)))"
978 (let ((verbose current-prefix-arg))
979 (setq epa-last-coding-system-specified
980 (or coding-system-for-write
981 (epa--select-safe-coding-system
982 (region-beginning) (region-end))))
983 (list (region-beginning) (region-end)
985 (epa-select-keys (epg-make-context epa-protocol)
986 "Select keys for signing.
987 If no one is selected, default secret key is used. "
990 (epa--read-signature-type)
993 (let ((context (epg-make-context epa-protocol))
995 ;;(epg-context-set-armor context epa-armor)
996 (epg-context-set-armor context t)
997 ;;(epg-context-set-textmode context epa-textmode)
998 (epg-context-set-textmode context t)
999 (epg-context-set-signers context signers)
1000 (epg-context-set-passphrase-callback context
1001 #'epa-passphrase-callback-function)
1002 (epg-context-set-progress-callback context
1004 #'epa-progress-callback-function
1006 (message "Signing...")
1007 (setq signature (epg-sign-string context
1008 (epa--encode-coding-string
1009 (buffer-substring start end)
1010 epa-last-coding-system-specified)
1012 (message "Signing...done")
1013 (delete-region start end)
1015 (add-text-properties (point)
1017 (insert (epa--decode-coding-string
1019 (or coding-system-for-read
1020 epa-last-coding-system-specified)))
1022 (list 'epa-coding-system-used
1023 epa-last-coding-system-specified
1030 (if (fboundp 'derived-mode-p)
1031 (defalias 'epa--derived-mode-p 'derived-mode-p)
1032 (defun epa--derived-mode-p (&rest modes)
1033 "Non-nil if the current major mode is derived from one of MODES.
1034 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1035 (let ((parent major-mode))
1036 (while (and (not (memq parent modes))
1037 (setq parent (get parent 'derived-mode-parent))))
1041 (defun epa-encrypt-region (start end recipients sign signers)
1042 "Encrypt the current region between START and END for RECIPIENTS.
1044 Don't use this command in Lisp programs!
1045 Since this function operates on regions, it does some tricks such
1046 as coding-system detection and unibyte/multibyte conversion. If
1047 you are sure how the data should be treated, you should consider
1048 using the string based counterpart `epg-encrypt-string', or the
1049 file based counterpart `epg-encrypt-file' instead.
1053 \(let ((context (epg-make-context 'OpenPGP)))
1056 (encode-coding-string (buffer-substring start end) 'utf-8)
1059 (let ((verbose current-prefix-arg)
1060 (context (epg-make-context epa-protocol))
1062 (setq epa-last-coding-system-specified
1063 (or coding-system-for-write
1064 (epa--select-safe-coding-system
1065 (region-beginning) (region-end))))
1066 (list (region-beginning) (region-end)
1067 (epa-select-keys context
1068 "Select recipients for encryption.
1069 If no one is selected, symmetric encryption will be performed. ")
1070 (setq sign (if verbose (y-or-n-p "Sign? ")))
1072 (epa-select-keys context
1073 "Select keys for signing. ")))))
1075 (let ((context (epg-make-context epa-protocol))
1077 ;;(epg-context-set-armor context epa-armor)
1078 (epg-context-set-armor context t)
1079 ;;(epg-context-set-textmode context epa-textmode)
1080 (epg-context-set-textmode context t)
1082 (epg-context-set-signers context signers))
1083 (epg-context-set-passphrase-callback context
1084 #'epa-passphrase-callback-function)
1085 (epg-context-set-progress-callback context
1087 #'epa-progress-callback-function
1089 (message "Encrypting...")
1090 (setq cipher (epg-encrypt-string context
1091 (epa--encode-coding-string
1092 (buffer-substring start end)
1093 epa-last-coding-system-specified)
1096 (message "Encrypting...done")
1097 (delete-region start end)
1099 (add-text-properties (point)
1103 (list 'epa-coding-system-used
1104 epa-last-coding-system-specified
1111 (defun epa-delete-keys (keys &optional allow-secret)
1112 "Delete selected KEYS."
1114 (let ((keys (epa--marked-keys)))
1116 (error "No keys selected"))
1118 (eq (nth 1 epa-list-keys-arguments) t))))
1119 (let ((context (epg-make-context epa-protocol)))
1120 (message "Deleting...")
1121 (epg-delete-keys context keys allow-secret)
1122 (message "Deleting...done")
1123 (apply #'epa--list-keys epa-list-keys-arguments)))
1126 (defun epa-import-keys (file)
1127 "Import keys from FILE."
1128 (interactive "fFile: ")
1129 (setq file (expand-file-name file))
1130 (let ((context (epg-make-context epa-protocol)))
1131 (message "Importing %s..." (file-name-nondirectory file))
1134 (epg-import-keys-from-file context file)
1135 (message "Importing %s...done" (file-name-nondirectory file)))
1137 (message "Importing %s...failed" (file-name-nondirectory file))))
1138 (if (epg-context-result-for context 'import)
1139 (epa-display-info (epg-import-result-to-string
1140 (epg-context-result-for context 'import))))
1141 (if (eq major-mode 'epa-key-list-mode)
1142 (apply #'epa--list-keys epa-list-keys-arguments))))
1145 (defun epa-import-keys-region (start end)
1146 "Import keys from the region."
1148 (let ((context (epg-make-context epa-protocol)))
1149 (message "Importing...")
1152 (epg-import-keys-from-string context (buffer-substring start end))
1153 (message "Importing...done"))
1155 (message "Importing...failed")))
1156 (if (epg-context-result-for context 'import)
1157 (epa-display-info (epg-import-result-to-string
1158 (epg-context-result-for context 'import))))))
1161 (defun epa-import-armor-in-region (start end)
1162 "Import keys in the OpenPGP armor format in the current region
1163 between START and END."
1167 (narrow-to-region start end)
1169 (let (armor-start armor-end)
1170 (while (re-search-forward
1171 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1173 (setq armor-start (match-beginning 0)
1174 armor-end (re-search-forward
1175 (concat "^-----END " (match-string 1) "-----$")
1178 (error "No armor tail"))
1179 (epa-import-keys-region armor-start armor-end))))))
1182 (defun epa-export-keys (keys file)
1183 "Export selected KEYS to FILE."
1185 (let ((keys (epa--marked-keys))
1188 (error "No keys selected"))
1191 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1192 (if epa-armor ".asc" ".gpg"))
1197 (concat "To file (default "
1198 (file-name-nondirectory default-name)
1200 (file-name-directory default-name)
1202 (let ((context (epg-make-context epa-protocol)))
1203 (epg-context-set-armor context epa-armor)
1204 (message "Exporting to %s..." (file-name-nondirectory file))
1205 (epg-export-keys-to-file context keys file)
1206 (message "Exporting to %s...done" (file-name-nondirectory file))))
1209 (defun epa-insert-keys (keys)
1210 "Insert selected KEYS after the point."
1212 (list (epa-select-keys (epg-make-context epa-protocol)
1213 "Select keys to export.
1214 If no one is selected, default public key is exported. ")))
1215 (let ((context (epg-make-context epa-protocol)))
1216 ;;(epg-context-set-armor context epa-armor)
1217 (epg-context-set-armor context t)
1218 (insert (epg-export-keys-to-string context keys))))
1220 ;; (defun epa-sign-keys (keys &optional local)
1221 ;; "Sign selected KEYS.
1222 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1224 ;; Don't use this command in Lisp programs!"
1226 ;; (let ((keys (epa--marked-keys)))
1228 ;; (error "No keys selected"))
1229 ;; (list keys current-prefix-arg)))
1230 ;; (let ((context (epg-make-context epa-protocol)))
1231 ;; (epg-context-set-passphrase-callback context
1232 ;; #'epa-passphrase-callback-function)
1233 ;; (epg-context-set-progress-callback context
1235 ;; #'epa-progress-callback-function
1236 ;; "Signing keys..."))
1237 ;; (message "Signing keys...")
1238 ;; (epg-sign-keys context keys local)
1239 ;; (message "Signing keys...done")))
1240 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1244 ;;; epa.el ends here