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, status information from epa commands is displayed on
42 (defcustom epa-info-window-height
5
43 "Number of lines used to display status information."
47 (defgroup epa-faces nil
52 (defface epa-validity-high
53 '((default :weight bold
)
54 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
55 "Face for high validity EPA information."
58 (defface epa-validity-medium
59 '((default :slant italic
)
60 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
61 "Face for medium validity EPA information."
64 (defface epa-validity-low
66 "Face used for displaying the low validity."
69 (defface epa-validity-disabled
70 '((t :slant italic
:inverse-video t
))
71 "Face used for displaying the disabled validity."
75 '((((class color
) (background dark
))
76 :foreground
"lightyellow")
77 (((class color
) (background light
))
79 "Face used for displaying the string."
83 '((default :weight bold
)
84 (((class color
) (background dark
)) :foreground
"orange")
85 (((class color
) (background light
)) :foreground
"red"))
86 "Face used for displaying the high validity."
89 (defface epa-field-name
90 '((default :weight bold
)
91 (((class color
) (background dark
)) :foreground
"PaleTurquoise"))
92 "Face for the name of the attribute field."
95 (defface epa-field-body
96 '((default :slant italic
)
97 (((class color
) (background dark
)) :foreground
"turquoise"))
98 "Face for the body of the attribute field."
101 (defcustom epa-validity-face-alist
102 '((unknown . epa-validity-disabled
)
103 (invalid . epa-validity-disabled
)
104 (disabled . epa-validity-disabled
)
105 (revoked . epa-validity-disabled
)
106 (expired . epa-validity-disabled
)
107 (none . epa-validity-low
)
108 (undefined . epa-validity-low
)
109 (never . epa-validity-low
)
110 (marginal . epa-validity-medium
)
111 (full . epa-validity-high
)
112 (ultimate . epa-validity-high
))
113 "An alist mapping validity values to faces."
114 :type
'(repeat (cons symbol face
))
117 (defvar epa-font-lock-keywords
120 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
122 (2 'epa-field-body
)))
123 "Default expressions to addon in epa-mode.")
125 (defconst epa-pubkey-algorithm-letter-alist
133 (defvar epa-protocol
'OpenPGP
134 "The default protocol.
135 The value can be either OpenPGP or CMS.
137 You should bind this variable with `let', but do not set it globally.")
139 (defvar epa-armor nil
140 "If non-nil, epa commands create ASCII armored output.
142 You should bind this variable with `let', but do not set it globally.")
144 (defvar epa-textmode nil
145 "If non-nil, epa commands treat input files as text.
147 You should bind this variable with `let', but do not set it globally.")
149 (defvar epa-keys-buffer nil
)
150 (defvar epa-key-buffer-alist nil
)
152 (defvar epa-list-keys-arguments nil
)
153 (defvar epa-info-buffer nil
)
154 (defvar epa-last-coding-system-specified nil
)
156 (defvar epa-key-list-mode-map
157 (let ((keymap (make-sparse-keymap))
158 (menu-map (make-sparse-keymap)))
159 (define-key keymap
"m" 'epa-mark-key
)
160 (define-key keymap
"u" 'epa-unmark-key
)
161 (define-key keymap
"d" 'epa-decrypt-file
)
162 (define-key keymap
"v" 'epa-verify-file
)
163 (define-key keymap
"s" 'epa-sign-file
)
164 (define-key keymap
"e" 'epa-encrypt-file
)
165 (define-key keymap
"r" 'epa-delete-keys
)
166 (define-key keymap
"i" 'epa-import-keys
)
167 (define-key keymap
"o" 'epa-export-keys
)
168 (define-key keymap
"g" 'revert-buffer
)
169 (define-key keymap
"n" 'next-line
)
170 (define-key keymap
"p" 'previous-line
)
171 (define-key keymap
" " 'scroll-up-command
)
172 (define-key keymap
[delete] 'scroll-down-command)
173 (define-key keymap "q" 'epa-exit-buffer)
174 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
175 (define-key menu-map [epa-key-list-unmark-key]
176 '(menu-item "Unmark Key" epa-unmark-key
177 :help "Unmark a key"))
178 (define-key menu-map [epa-key-list-mark-key]
179 '(menu-item "Mark Key" epa-mark-key
181 (define-key menu-map [separator-epa-file] '(menu-item "--"))
182 (define-key menu-map [epa-verify-file]
183 '(menu-item "Verify File..." epa-verify-file
184 :help "Verify FILE"))
185 (define-key menu-map [epa-sign-file]
186 '(menu-item "Sign File..." epa-sign-file
187 :help "Sign FILE by SIGNERS keys selected"))
188 (define-key menu-map [epa-decrypt-file]
189 '(menu-item "Decrypt File..." epa-decrypt-file
190 :help "Decrypt FILE"))
191 (define-key menu-map [epa-encrypt-file]
192 '(menu-item "Encrypt File..." epa-encrypt-file
193 :help "Encrypt FILE for RECIPIENTS"))
194 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
195 (define-key menu-map [epa-key-list-delete-keys]
196 '(menu-item "Delete Keys" epa-delete-keys
197 :help "Delete Marked Keys"))
198 (define-key menu-map [epa-key-list-import-keys]
199 '(menu-item "Import Keys" epa-import-keys
200 :help "Import keys from a file"))
201 (define-key menu-map [epa-key-list-export-keys]
202 '(menu-item "Export Keys" epa-export-keys
203 :help "Export marked keys to a file"))
206 (defvar epa-key-mode-map
207 (let ((keymap (make-sparse-keymap)))
208 (define-key keymap "q" 'epa-exit-buffer)
211 (defvar epa-info-mode-map
212 (let ((keymap (make-sparse-keymap)))
213 (define-key keymap "q" 'delete-window)
216 (defvar epa-exit-buffer-function #'bury-buffer)
218 (define-widget 'epa-key 'push-button
219 "Button for representing a epg-key object."
221 :button-face-get 'epa--key-widget-button-face-get
222 :value-create 'epa--key-widget-value-create
223 :action 'epa--key-widget-action
224 :help-echo 'epa--key-widget-help-echo)
226 (defun epa--key-widget-action (widget &optional _event)
227 (save-selected-window
228 (epa--show-key (widget-get widget :value))))
230 (defun epa--key-widget-value-create (widget)
231 (let* ((key (widget-get widget :value))
232 (primary-sub-key (car (epg-key-sub-key-list key)))
233 (primary-user-id (car (epg-key-user-id-list key))))
234 (insert (format "%c "
235 (if (epg-sub-key-validity primary-sub-key)
236 (car (rassq (epg-sub-key-validity primary-sub-key)
237 epg-key-validity-alist))
239 (epg-sub-key-id primary-sub-key)
242 (if (stringp (epg-user-id-string primary-user-id))
243 (epg-user-id-string primary-user-id)
244 (epg-decode-dn (epg-user-id-string primary-user-id)))
247 (defun epa--key-widget-button-face-get (widget)
248 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
249 (widget-get widget :value))))))
251 (cdr (assq validity epa-validity-face-alist))
254 (defun epa--key-widget-help-echo (widget)
256 (epg-sub-key-id (car (epg-key-sub-key-list
257 (widget-get widget :value))))))
260 (if (fboundp 'encode-coding-string)
261 (defalias 'epa--encode-coding-string 'encode-coding-string)
262 (defalias 'epa--encode-coding-string 'identity)))
265 (if (fboundp 'decode-coding-string)
266 (defalias 'epa--decode-coding-string 'decode-coding-string)
267 (defalias 'epa--decode-coding-string 'identity)))
269 (defun epa-key-list-mode ()
270 "Major mode for `epa-list-keys'."
271 (kill-all-local-variables)
272 (buffer-disable-undo)
273 (setq major-mode 'epa-key-list-mode
277 (use-local-map epa-key-list-mode-map)
278 (make-local-variable 'font-lock-defaults)
279 (setq font-lock-defaults '(epa-font-lock-keywords t))
280 ;; In XEmacs, auto-initialization of font-lock is not effective
281 ;; if buffer-file-name is not set.
282 (font-lock-set-defaults)
283 (make-local-variable 'epa-exit-buffer-function)
284 (make-local-variable 'revert-buffer-function)
285 (setq revert-buffer-function 'epa--key-list-revert-buffer)
286 (run-mode-hooks 'epa-key-list-mode-hook))
288 (defun epa-key-mode ()
289 "Major mode for a key description."
290 (kill-all-local-variables)
291 (buffer-disable-undo)
292 (setq major-mode 'epa-key-mode
296 (use-local-map epa-key-mode-map)
297 (make-local-variable 'font-lock-defaults)
298 (setq font-lock-defaults '(epa-font-lock-keywords t))
299 ;; In XEmacs, auto-initialization of font-lock is not effective
300 ;; if buffer-file-name is not set.
301 (font-lock-set-defaults)
302 (make-local-variable 'epa-exit-buffer-function)
303 (run-mode-hooks 'epa-key-mode-hook))
305 (defun epa-info-mode ()
306 "Major mode for `epa-info-buffer'."
307 (kill-all-local-variables)
308 (buffer-disable-undo)
309 (setq major-mode 'epa-info-mode
313 (use-local-map epa-info-mode-map)
314 (run-mode-hooks 'epa-info-mode-hook))
316 (defun epa-mark-key (&optional arg)
317 "Mark a key on the current line.
318 If ARG is non-nil, unmark the key."
320 (let ((inhibit-read-only t)
324 (unless (get-text-property (point) 'epa-key)
325 (error "No key on this line"))
326 (setq properties (text-properties-at (point)))
328 (insert (if arg " " "*"))
329 (set-text-properties (1- (point)) (point) properties)
332 (defun epa-unmark-key (&optional arg)
333 "Unmark a key on the current line.
334 If ARG is non-nil, mark the key."
336 (epa-mark-key (not arg)))
338 (defun epa-exit-buffer ()
339 "Exit the current buffer.
340 `epa-exit-buffer-function' is called if it is set."
342 (funcall epa-exit-buffer-function))
344 (defun epa--insert-keys (keys)
347 (narrow-to-region (point) (point))
352 (add-text-properties point (point)
353 (list 'epa-key (car keys)
358 (widget-create 'epa-key :value (car keys))
360 (setq keys (cdr keys))))
361 (add-text-properties (point-min) (point-max)
362 (list 'epa-list-keys t
368 (defun epa--list-keys (name secret)
369 (unless (and epa-keys-buffer
370 (buffer-live-p epa-keys-buffer))
371 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
372 (set-buffer epa-keys-buffer)
374 (let ((inhibit-read-only t)
377 (context (epg-make-context epa-protocol)))
378 (unless (get-text-property point 'epa-list-keys)
379 (setq point (next-single-property-change point 'epa-list-keys)))
382 (or (next-single-property-change point 'epa-list-keys)
385 (epa--insert-keys (epg-list-keys context name secret))
387 (set-keymap-parent (current-local-map) widget-keymap))
388 (make-local-variable 'epa-list-keys-arguments)
389 (setq epa-list-keys-arguments (list name secret))
390 (goto-char (point-min))
391 (pop-to-buffer (current-buffer)))
394 (defun epa-list-keys (&optional name)
395 "List all keys matched with NAME from the public keyring."
397 (if current-prefix-arg
398 (let ((name (read-string "Pattern: "
399 (if epa-list-keys-arguments
400 (car epa-list-keys-arguments)))))
401 (list (if (equal name "") nil name)))
403 (epa--list-keys name nil))
406 (defun epa-list-secret-keys (&optional name)
407 "List all keys matched with NAME from the private 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 t))
417 (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm)
418 (apply #'epa--list-keys epa-list-keys-arguments))
420 (defun epa--marked-keys ()
421 (or (with-current-buffer epa-keys-buffer
422 (goto-char (point-min))
424 (while (re-search-forward "^\\*" nil t)
425 (if (setq key (get-text-property (match-beginning 0)
427 (setq keys (cons key keys))))
429 (let ((key (get-text-property (point-at-bol) 'epa-key)))
433 (defun epa--select-keys (prompt keys)
434 (unless (and epa-keys-buffer
435 (buffer-live-p epa-keys-buffer))
436 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
437 (with-current-buffer epa-keys-buffer
439 ;; C-c C-c is the usual way to finish the selection (bug#11159).
440 (define-key (current-local-map) "\C-c\C-c" 'exit-recursive-edit)
441 (let ((inhibit-read-only t)
445 (substitute-command-keys "\
446 - `\\[epa-mark-key]' to mark a key on the line
447 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
449 :notify (lambda (&rest _ignore) (abort-recursive-edit))
451 (substitute-command-keys
452 "Click here or \\[abort-recursive-edit] to cancel")
455 :notify (lambda (&rest _ignore) (exit-recursive-edit))
457 (substitute-command-keys
458 "Click here or \\[exit-recursive-edit] to finish")
461 (epa--insert-keys keys)
463 (set-keymap-parent (current-local-map) widget-keymap)
464 (setq epa-exit-buffer-function #'abort-recursive-edit)
465 (goto-char (point-min))
466 (let ((display-buffer-mark-dedicated 'soft))
467 (pop-to-buffer (current-buffer))))
472 (kill-buffer epa-keys-buffer))))
475 (defun epa-select-keys (context prompt &optional names secret)
476 "Display a user's keyring and ask him to select keys.
477 CONTEXT is an epg-context.
478 PROMPT is a string to prompt with.
479 NAMES is a list of strings to be matched with keys. If it is nil, all
481 If SECRET is non-nil, list secret keys instead of public keys."
482 (let ((keys (epg-list-keys context names secret)))
483 (epa--select-keys prompt keys)))
485 (defun epa--show-key (key)
486 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
487 (entry (assoc (epg-sub-key-id primary-sub-key)
488 epa-key-buffer-alist))
489 (inhibit-read-only t)
493 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
494 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
495 (unless (and (cdr entry)
496 (buffer-live-p (cdr entry)))
497 (setcdr entry (generate-new-buffer
498 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
499 (set-buffer (cdr entry))
501 (make-local-variable 'epa-key)
504 (setq pointer (epg-key-user-id-list key))
508 (if (epg-user-id-validity (car pointer))
510 (car (rassq (epg-user-id-validity (car pointer))
511 epg-key-validity-alist)))
514 (if (stringp (epg-user-id-string (car pointer)))
515 (epg-user-id-string (car pointer))
516 (epg-decode-dn (epg-user-id-string (car pointer))))
518 (setq pointer (cdr pointer)))
519 (setq pointer (epg-key-sub-key-list key))
522 (if (epg-sub-key-validity (car pointer))
524 (car (rassq (epg-sub-key-validity (car pointer))
525 epg-key-validity-alist)))
528 (epg-sub-key-id (car pointer))
531 (epg-sub-key-length (car pointer)))
533 (cdr (assq (epg-sub-key-algorithm (car pointer))
534 epg-pubkey-algorithm-alist))
537 (format-time-string "%Y-%m-%d"
538 (epg-sub-key-creation-time (car pointer)))
539 (error "????-??-??"))
540 (if (epg-sub-key-expiration-time (car pointer))
541 (format (if (time-less-p (current-time)
542 (epg-sub-key-expiration-time
547 (format-time-string "%Y-%m-%d"
548 (epg-sub-key-expiration-time
550 (error "????-??-??")))
553 (mapconcat #'symbol-name
554 (epg-sub-key-capability (car pointer))
557 (epg-sub-key-fingerprint (car pointer))
559 (setq pointer (cdr pointer)))
560 (goto-char (point-min))
561 (pop-to-buffer (current-buffer))))
563 (defun epa-display-info (info)
564 (if epa-popup-info-window
565 (save-selected-window
566 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
567 (setq epa-info-buffer (generate-new-buffer "*Info*")))
568 (if (get-buffer-window epa-info-buffer)
569 (delete-window (get-buffer-window epa-info-buffer)))
570 (with-current-buffer epa-info-buffer
571 (let ((inhibit-read-only t)
576 (goto-char (point-min)))
577 (if (> (window-height)
578 epa-info-window-height)
579 (set-window-buffer (split-window nil (- (window-height)
580 epa-info-window-height))
582 (pop-to-buffer epa-info-buffer)
583 (if (> (window-height) epa-info-window-height)
584 (shrink-window (- (window-height) epa-info-window-height)))))
585 (message "%s" info)))
587 (defun epa-display-verify-result (verify-result)
588 (declare (obsolete epa-display-info "23.1"))
589 (epa-display-info (epg-verify-result-to-string verify-result)))
591 (defun epa-passphrase-callback-function (context key-id handback)
594 (format "Passphrase for symmetric encryption%s: "
595 ;; Add the file name to the prompt, if any.
596 (if (stringp handback)
597 (format " for %s" handback)
599 (eq (epg-context-operation context) 'encrypt))
602 "Passphrase for PIN: "
603 (let ((entry (assoc key-id epg-user-id-alist)))
605 (format "Passphrase for %s %s: " key-id (cdr entry))
606 (format "Passphrase for %s: " key-id)))))))
608 (defun epa-progress-callback-function (_context what _char current total
610 (let ((prompt (or handback
611 (format "Processing %s: " what))))
612 ;; According to gnupg/doc/DETAIL: a "total" of 0 indicates that
613 ;; the total amount is not known. The condition TOTAL && CUR ==
614 ;; TOTAL may be used to detect the end of an operation.
616 (if (= current total)
617 (message "%s...done" prompt)
618 (message "%s...%d%%" prompt
619 (floor (* (/ current (float total)) 100))))
620 (message "%s..." prompt))))
623 (defun epa-decrypt-file (file)
625 (interactive "fFile: ")
626 (setq file (expand-file-name file))
627 (let* ((default-name (file-name-sans-extension file))
628 (plain (expand-file-name
630 (concat "To file (default "
631 (file-name-nondirectory default-name)
633 (file-name-directory default-name)
635 (context (epg-make-context epa-protocol)))
636 (epg-context-set-passphrase-callback context
637 #'epa-passphrase-callback-function)
638 (epg-context-set-progress-callback context
640 #'epa-progress-callback-function
641 (format "Decrypting %s..."
642 (file-name-nondirectory file))))
643 (message "Decrypting %s..." (file-name-nondirectory file))
644 (epg-decrypt-file context file plain)
645 (message "Decrypting %s...wrote %s" (file-name-nondirectory file)
646 (file-name-nondirectory plain))
647 (if (epg-context-result-for context 'verify)
648 (epa-display-info (epg-verify-result-to-string
649 (epg-context-result-for context 'verify))))))
652 (defun epa-verify-file (file)
654 (interactive "fFile: ")
655 (setq file (expand-file-name file))
656 (let* ((context (epg-make-context epa-protocol))
657 (plain (if (equal (file-name-extension file) "sig")
658 (file-name-sans-extension file))))
659 (epg-context-set-progress-callback context
661 #'epa-progress-callback-function
662 (format "Verifying %s..."
663 (file-name-nondirectory file))))
664 (message "Verifying %s..." (file-name-nondirectory file))
665 (epg-verify-file context file plain)
666 (message "Verifying %s...done" (file-name-nondirectory file))
667 (if (epg-context-result-for context 'verify)
668 (epa-display-info (epg-verify-result-to-string
669 (epg-context-result-for context 'verify))))))
671 (defun epa--read-signature-type ()
674 (message "Signature type (n,c,d,?) ")
679 (setq type 'detached))
681 (with-output-to-temp-buffer "*Help*"
682 (with-current-buffer standard-output
684 n - Create a normal signature
685 c - Create a cleartext signature
686 d - Create a detached signature
690 (setq type 'normal))))
694 (defun epa-sign-file (file signers mode)
695 "Sign FILE by SIGNERS keys selected."
697 (let ((verbose current-prefix-arg))
698 (list (expand-file-name (read-file-name "File: "))
700 (epa-select-keys (epg-make-context epa-protocol)
701 "Select keys for signing.
702 If no one is selected, default secret key is used. "
705 (epa--read-signature-type)
707 (let ((signature (concat file
708 (if (eq epa-protocol 'OpenPGP)
711 '(nil t normal detached))))
713 (if (memq mode '(t detached))
716 (if (memq mode '(t detached))
719 (context (epg-make-context epa-protocol)))
720 (epg-context-set-armor context epa-armor)
721 (epg-context-set-textmode context epa-textmode)
722 (epg-context-set-signers context signers)
723 (epg-context-set-passphrase-callback context
724 #'epa-passphrase-callback-function)
725 (epg-context-set-progress-callback context
727 #'epa-progress-callback-function
728 (format "Signing %s..."
729 (file-name-nondirectory file))))
730 (message "Signing %s..." (file-name-nondirectory file))
731 (epg-sign-file context file signature mode)
732 (message "Signing %s...wrote %s" (file-name-nondirectory file)
733 (file-name-nondirectory signature))))
736 (defun epa-encrypt-file (file recipients)
737 "Encrypt FILE for RECIPIENTS."
739 (list (expand-file-name (read-file-name "File: "))
740 (epa-select-keys (epg-make-context epa-protocol)
741 "Select recipients for encryption.
742 If no one is selected, symmetric encryption will be performed. ")))
743 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
744 (if epa-armor ".asc" ".gpg")
746 (context (epg-make-context epa-protocol)))
747 (epg-context-set-armor context epa-armor)
748 (epg-context-set-textmode context epa-textmode)
749 (epg-context-set-passphrase-callback context
750 #'epa-passphrase-callback-function)
751 (epg-context-set-progress-callback context
753 #'epa-progress-callback-function
754 (format "Encrypting %s..."
755 (file-name-nondirectory file))))
756 (message "Encrypting %s..." (file-name-nondirectory file))
757 (epg-encrypt-file context file recipients cipher)
758 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
759 (file-name-nondirectory cipher))))
762 (defun epa-decrypt-region (start end &optional make-buffer-function)
763 "Decrypt the current region between START and END.
765 If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
766 It should return that buffer. If it copies the input, it should
767 delete the text now being decrypted. It should leave point at the
768 proper place to insert the plaintext.
770 Be careful about using this command in Lisp programs!
771 Since this function operates on regions, it does some tricks such
772 as coding-system detection and unibyte/multibyte conversion. If
773 you are sure how the data in the region should be treated, you
774 should consider using the string based counterpart
775 `epg-decrypt-string', or the file based counterpart
776 `epg-decrypt-file' instead.
780 \(let ((context (epg-make-context 'OpenPGP)))
781 (decode-coding-string
782 (epg-decrypt-string context (buffer-substring start end))
786 (let ((context (epg-make-context epa-protocol))
788 (epg-context-set-passphrase-callback context
789 #'epa-passphrase-callback-function)
790 (epg-context-set-progress-callback context
792 #'epa-progress-callback-function
794 (message "Decrypting...")
795 (setq plain (epg-decrypt-string context (buffer-substring start end)))
796 (message "Decrypting...done")
797 (setq plain (epa--decode-coding-string
799 (or coding-system-for-read
800 (get-text-property start 'epa-coding-system-used)
802 (if make-buffer-function
803 (with-current-buffer (funcall make-buffer-function)
804 (let ((inhibit-read-only t))
806 (if (y-or-n-p "Replace the original text? ")
807 (let ((inhibit-read-only t))
808 (delete-region start end)
811 (with-output-to-temp-buffer "*Temp*"
812 (set-buffer standard-output)
815 (if (epg-context-result-for context 'verify)
816 (epa-display-info (epg-verify-result-to-string
817 (epg-context-result-for context 'verify)))))))
819 (defun epa--find-coding-system-for-mime-charset (mime-charset)
820 (if (featurep 'xemacs)
821 (if (fboundp 'find-coding-system)
822 (find-coding-system mime-charset))
823 ;; Find the first coding system which corresponds to MIME-CHARSET.
824 (let ((pointer (coding-system-list)))
826 (not (eq (coding-system-get (car pointer) 'mime-charset)
828 (setq pointer (cdr pointer)))
832 (defun epa-decrypt-armor-in-region (start end)
833 "Decrypt OpenPGP armors in the current region between START and END.
835 Don't use this command in Lisp programs!
836 See the reason described in the `epa-decrypt-region' documentation."
840 (narrow-to-region start end)
842 (let (armor-start armor-end)
843 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
844 (setq armor-start (match-beginning 0)
845 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
848 (error "Encryption armor beginning has no matching end"))
849 (goto-char armor-start)
850 (let ((coding-system-for-read
851 (or coding-system-for-read
852 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
853 (epa--find-coding-system-for-mime-charset
854 (intern (downcase (match-string 1))))))))
855 (goto-char armor-end)
856 (epa-decrypt-region armor-start armor-end)))))))
859 (defun epa-verify-region (start end)
860 "Verify the current region between START and END.
862 Don't use this command in Lisp programs!
863 Since this function operates on regions, it does some tricks such
864 as coding-system detection and unibyte/multibyte conversion. If
865 you are sure how the data in the region should be treated, you
866 should consider using the string based counterpart
867 `epg-verify-string', or the file based counterpart
868 `epg-verify-file' instead.
872 \(let ((context (epg-make-context 'OpenPGP)))
873 (decode-coding-string
874 (epg-verify-string context (buffer-substring start end))
877 (let ((context (epg-make-context epa-protocol))
879 (epg-context-set-progress-callback context
881 #'epa-progress-callback-function
883 (message "Verifying...")
884 (setq plain (epg-verify-string
886 (epa--encode-coding-string
887 (buffer-substring start end)
888 (or coding-system-for-write
889 (get-text-property start 'epa-coding-system-used)))))
890 (message "Verifying...done")
891 (setq plain (epa--decode-coding-string
893 (or coding-system-for-read
894 (get-text-property start 'epa-coding-system-used)
896 (if (y-or-n-p "Replace the original text? ")
897 (let ((inhibit-read-only t)
899 (delete-region start end)
902 (with-output-to-temp-buffer "*Temp*"
903 (set-buffer standard-output)
906 (if (epg-context-result-for context 'verify)
907 (epa-display-info (epg-verify-result-to-string
908 (epg-context-result-for context 'verify))))))
911 (defun epa-verify-cleartext-in-region (start end)
912 "Verify OpenPGP cleartext signed messages in the current region
913 between START and END.
915 Don't use this command in Lisp programs!
916 See the reason described in the `epa-verify-region' documentation."
920 (narrow-to-region start end)
922 (let (cleartext-start cleartext-end)
923 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
925 (setq cleartext-start (match-beginning 0))
926 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
928 (error "Invalid cleartext signed message"))
929 (setq cleartext-end (re-search-forward
930 "^-----END PGP SIGNATURE-----$"
932 (unless cleartext-end
933 (error "No cleartext tail"))
934 (epa-verify-region cleartext-start cleartext-end))))))
937 (if (fboundp 'select-safe-coding-system)
938 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system)
939 (defun epa--select-safe-coding-system (_from _to)
940 buffer-file-coding-system)))
943 (defun epa-sign-region (start end signers mode)
944 "Sign the current region between START and END by SIGNERS keys selected.
946 Don't use this command in Lisp programs!
947 Since this function operates on regions, it does some tricks such
948 as coding-system detection and unibyte/multibyte conversion. If
949 you are sure how the data should be treated, you should consider
950 using the string based counterpart `epg-sign-string', or the file
951 based counterpart `epg-sign-file' instead.
955 \(let ((context (epg-make-context 'OpenPGP)))
958 (encode-coding-string (buffer-substring start end) 'utf-8)))"
960 (let ((verbose current-prefix-arg))
961 (setq epa-last-coding-system-specified
962 (or coding-system-for-write
963 (epa--select-safe-coding-system
964 (region-beginning) (region-end))))
965 (list (region-beginning) (region-end)
967 (epa-select-keys (epg-make-context epa-protocol)
968 "Select keys for signing.
969 If no one is selected, default secret key is used. "
972 (epa--read-signature-type)
975 (let ((context (epg-make-context epa-protocol))
977 ;;(epg-context-set-armor context epa-armor)
978 (epg-context-set-armor context t)
979 ;;(epg-context-set-textmode context epa-textmode)
980 (epg-context-set-textmode context t)
981 (epg-context-set-signers context signers)
982 (epg-context-set-passphrase-callback context
983 #'epa-passphrase-callback-function)
984 (epg-context-set-progress-callback context
986 #'epa-progress-callback-function
988 (message "Signing...")
989 (setq signature (epg-sign-string context
990 (epa--encode-coding-string
991 (buffer-substring start end)
992 epa-last-coding-system-specified)
994 (message "Signing...done")
995 (delete-region start end)
997 (add-text-properties (point)
999 (insert (epa--decode-coding-string
1001 (or coding-system-for-read
1002 epa-last-coding-system-specified)))
1004 (list 'epa-coding-system-used
1005 epa-last-coding-system-specified
1012 (if (fboundp 'derived-mode-p)
1013 (defalias 'epa--derived-mode-p 'derived-mode-p)
1014 (defun epa--derived-mode-p (&rest modes)
1015 "Non-nil if the current major mode is derived from one of MODES.
1016 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1017 (let ((parent major-mode))
1018 (while (and (not (memq parent modes))
1019 (setq parent (get parent 'derived-mode-parent))))
1023 (defun epa-encrypt-region (start end recipients sign signers)
1024 "Encrypt the current region between START and END for RECIPIENTS.
1026 Don't use this command in Lisp programs!
1027 Since this function operates on regions, it does some tricks such
1028 as coding-system detection and unibyte/multibyte conversion. If
1029 you are sure how the data should be treated, you should consider
1030 using the string based counterpart `epg-encrypt-string', or the
1031 file based counterpart `epg-encrypt-file' instead.
1035 \(let ((context (epg-make-context 'OpenPGP)))
1038 (encode-coding-string (buffer-substring start end) 'utf-8)
1041 (let ((verbose current-prefix-arg)
1042 (context (epg-make-context epa-protocol))
1044 (setq epa-last-coding-system-specified
1045 (or coding-system-for-write
1046 (epa--select-safe-coding-system
1047 (region-beginning) (region-end))))
1048 (list (region-beginning) (region-end)
1049 (epa-select-keys context
1050 "Select recipients for encryption.
1051 If no one is selected, symmetric encryption will be performed. ")
1052 (setq sign (if verbose (y-or-n-p "Sign? ")))
1054 (epa-select-keys context
1055 "Select keys for signing. ")))))
1057 (let ((context (epg-make-context epa-protocol))
1059 ;;(epg-context-set-armor context epa-armor)
1060 (epg-context-set-armor context t)
1061 ;;(epg-context-set-textmode context epa-textmode)
1062 (epg-context-set-textmode context t)
1064 (epg-context-set-signers context signers))
1065 (epg-context-set-passphrase-callback context
1066 #'epa-passphrase-callback-function)
1067 (epg-context-set-progress-callback context
1069 #'epa-progress-callback-function
1071 (message "Encrypting...")
1072 (setq cipher (epg-encrypt-string context
1073 (epa--encode-coding-string
1074 (buffer-substring start end)
1075 epa-last-coding-system-specified)
1078 (message "Encrypting...done")
1079 (delete-region start end)
1081 (add-text-properties (point)
1085 (list 'epa-coding-system-used
1086 epa-last-coding-system-specified
1093 (defun epa-delete-keys (keys &optional allow-secret)
1094 "Delete selected KEYS."
1096 (let ((keys (epa--marked-keys)))
1098 (error "No keys selected"))
1100 (eq (nth 1 epa-list-keys-arguments) t))))
1101 (let ((context (epg-make-context epa-protocol)))
1102 (message "Deleting...")
1103 (epg-delete-keys context keys allow-secret)
1104 (message "Deleting...done")
1105 (apply #'epa--list-keys epa-list-keys-arguments)))
1108 (defun epa-import-keys (file)
1109 "Import keys from FILE."
1110 (interactive "fFile: ")
1111 (setq file (expand-file-name file))
1112 (let ((context (epg-make-context epa-protocol)))
1113 (message "Importing %s..." (file-name-nondirectory file))
1116 (epg-import-keys-from-file context file)
1117 (message "Importing %s...done" (file-name-nondirectory file)))
1119 (message "Importing %s...failed" (file-name-nondirectory file))))
1120 (if (epg-context-result-for context 'import)
1121 (epa-display-info (epg-import-result-to-string
1122 (epg-context-result-for context 'import))))
1123 (if (eq major-mode 'epa-key-list-mode)
1124 (apply #'epa--list-keys epa-list-keys-arguments))))
1127 (defun epa-import-keys-region (start end)
1128 "Import keys from the region."
1130 (let ((context (epg-make-context epa-protocol)))
1131 (message "Importing...")
1134 (epg-import-keys-from-string context (buffer-substring start end))
1135 (message "Importing...done"))
1137 (message "Importing...failed")))
1138 (if (epg-context-result-for context 'import)
1139 (epa-display-info (epg-import-result-to-string
1140 (epg-context-result-for context 'import))))))
1143 (defun epa-import-armor-in-region (start end)
1144 "Import keys in the OpenPGP armor format in the current region
1145 between START and END."
1149 (narrow-to-region start end)
1151 (let (armor-start armor-end)
1152 (while (re-search-forward
1153 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1155 (setq armor-start (match-beginning 0)
1156 armor-end (re-search-forward
1157 (concat "^-----END " (match-string 1) "-----$")
1160 (error "No armor tail"))
1161 (epa-import-keys-region armor-start armor-end))))))
1164 (defun epa-export-keys (keys file)
1165 "Export selected KEYS to FILE."
1167 (let ((keys (epa--marked-keys))
1170 (error "No keys selected"))
1173 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1174 (if epa-armor ".asc" ".gpg"))
1179 (concat "To file (default "
1180 (file-name-nondirectory default-name)
1182 (file-name-directory default-name)
1184 (let ((context (epg-make-context epa-protocol)))
1185 (epg-context-set-armor context epa-armor)
1186 (message "Exporting to %s..." (file-name-nondirectory file))
1187 (epg-export-keys-to-file context keys file)
1188 (message "Exporting to %s...done" (file-name-nondirectory file))))
1191 (defun epa-insert-keys (keys)
1192 "Insert selected KEYS after the point."
1194 (list (epa-select-keys (epg-make-context epa-protocol)
1195 "Select keys to export.
1196 If no one is selected, default public key is exported. ")))
1197 (let ((context (epg-make-context epa-protocol)))
1198 ;;(epg-context-set-armor context epa-armor)
1199 (epg-context-set-armor context t)
1200 (insert (epg-export-keys-to-string context keys))))
1202 ;; (defun epa-sign-keys (keys &optional local)
1203 ;; "Sign selected KEYS.
1204 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1206 ;; Don't use this command in Lisp programs!"
1208 ;; (let ((keys (epa--marked-keys)))
1210 ;; (error "No keys selected"))
1211 ;; (list keys current-prefix-arg)))
1212 ;; (let ((context (epg-make-context epa-protocol)))
1213 ;; (epg-context-set-passphrase-callback context
1214 ;; #'epa-passphrase-callback-function)
1215 ;; (epg-context-set-progress-callback context
1217 ;; #'epa-progress-callback-function
1218 ;; "Signing keys..."))
1219 ;; (message "Signing keys...")
1220 ;; (epg-sign-keys context keys local)
1221 ;; (message "Signing keys...done")))
1222 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1226 ;;; epa.el ends here