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))))))
271 (defalias 'epa--encode-coding-string
272 (if (fboundp 'encode-coding-string) #'encode-coding-string #'identity))
274 (defalias 'epa--decode-coding-string
275 (if (fboundp 'decode-coding-string) #'decode-coding-string #'identity))
277 (define-derived-mode epa-key-list-mode special-mode "Keys"
278 "Major mode for `epa-list-keys'."
279 (buffer-disable-undo)
280 (setq truncate-lines t
282 (setq-local font-lock-defaults '(epa-font-lock-keywords t))
283 ;; In XEmacs, auto-initialization of font-lock is not effective
284 ;; if buffer-file-name is not set.
285 (font-lock-set-defaults)
286 (make-local-variable 'epa-exit-buffer-function)
287 (setq-local revert-buffer-function #'epa--key-list-revert-buffer))
289 (define-derived-mode epa-key-mode special-mode "Key"
290 "Major mode for a key description."
291 (buffer-disable-undo)
292 (setq truncate-lines t
294 (setq-local font-lock-defaults '(epa-font-lock-keywords t))
295 ;; In XEmacs, auto-initialization of font-lock is not effective
296 ;; if buffer-file-name is not set.
297 (font-lock-set-defaults)
298 (make-local-variable 'epa-exit-buffer-function))
300 (define-derived-mode epa-info-mode special-mode "Info"
301 "Major mode for `epa-info-buffer'."
302 (buffer-disable-undo)
303 (setq truncate-lines t
306 (defun epa-mark-key (&optional arg)
307 "Mark a key on the current line.
308 If ARG is non-nil, unmark the key."
310 (let ((inhibit-read-only t)
314 (unless (get-text-property (point) 'epa-key)
315 (error "No key on this line"))
316 (setq properties (text-properties-at (point)))
318 (insert (if arg " " "*"))
319 (set-text-properties (1- (point)) (point) properties)
322 (defun epa-unmark-key (&optional arg)
323 "Unmark a key on the current line.
324 If ARG is non-nil, mark the key."
326 (epa-mark-key (not arg)))
328 (defun epa-exit-buffer ()
329 "Exit the current buffer.
330 `epa-exit-buffer-function' is called if it is set."
332 (funcall epa-exit-buffer-function))
334 (defun epa--insert-keys (keys)
337 (narrow-to-region (point) (point))
342 (add-text-properties point (point)
343 (list 'epa-key (car keys)
348 (widget-create 'epa-key :value (car keys))
350 (setq keys (cdr keys))))
351 (add-text-properties (point-min) (point-max)
352 (list 'epa-list-keys t
358 (defun epa--list-keys (name secret)
359 (unless (and epa-keys-buffer
360 (buffer-live-p epa-keys-buffer))
361 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
362 (set-buffer epa-keys-buffer)
364 (let ((inhibit-read-only t)
367 (context (epg-make-context epa-protocol)))
368 (unless (get-text-property point 'epa-list-keys)
369 (setq point (next-single-property-change point 'epa-list-keys)))
372 (or (next-single-property-change point 'epa-list-keys)
375 (epa--insert-keys (epg-list-keys context name secret))
377 (set-keymap-parent (current-local-map) widget-keymap))
378 (make-local-variable 'epa-list-keys-arguments)
379 (setq epa-list-keys-arguments (list name secret))
380 (goto-char (point-min))
381 (pop-to-buffer (current-buffer)))
384 (defun epa-list-keys (&optional name)
385 "List all keys matched with NAME from the public keyring."
387 (if current-prefix-arg
388 (let ((name (read-string "Pattern: "
389 (if epa-list-keys-arguments
390 (car epa-list-keys-arguments)))))
391 (list (if (equal name "") nil name)))
393 (epa--list-keys name nil))
396 (defun epa-list-secret-keys (&optional name)
397 "List all keys matched with NAME from the private keyring."
399 (if current-prefix-arg
400 (let ((name (read-string "Pattern: "
401 (if epa-list-keys-arguments
402 (car epa-list-keys-arguments)))))
403 (list (if (equal name "") nil name)))
405 (epa--list-keys name t))
407 (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm)
408 (apply #'epa--list-keys epa-list-keys-arguments))
410 (defun epa--marked-keys ()
411 (or (with-current-buffer epa-keys-buffer
412 (goto-char (point-min))
414 (while (re-search-forward "^\\*" nil t)
415 (if (setq key (get-text-property (match-beginning 0)
417 (setq keys (cons key keys))))
419 (let ((key (get-text-property (point-at-bol) 'epa-key)))
423 (defun epa--select-keys (prompt keys)
424 (unless (and epa-keys-buffer
425 (buffer-live-p epa-keys-buffer))
426 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
427 (with-current-buffer epa-keys-buffer
429 ;; C-c C-c is the usual way to finish the selection (bug#11159).
430 (define-key (current-local-map) "\C-c\C-c" 'exit-recursive-edit)
431 (let ((inhibit-read-only t)
435 (substitute-command-keys "\
436 - `\\[epa-mark-key]' to mark a key on the line
437 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
439 :notify (lambda (&rest _ignore) (abort-recursive-edit))
441 (substitute-command-keys
442 "Click here or \\[abort-recursive-edit] to cancel")
445 :notify (lambda (&rest _ignore) (exit-recursive-edit))
447 (substitute-command-keys
448 "Click here or \\[exit-recursive-edit] to finish")
451 (epa--insert-keys keys)
453 (set-keymap-parent (current-local-map) widget-keymap)
454 (setq epa-exit-buffer-function #'abort-recursive-edit)
455 (goto-char (point-min))
456 (let ((display-buffer-mark-dedicated 'soft))
457 (pop-to-buffer (current-buffer))))
462 (kill-buffer epa-keys-buffer))))
465 (defun epa-select-keys (context prompt &optional names secret)
466 "Display a user's keyring and ask him to select keys.
467 CONTEXT is an epg-context.
468 PROMPT is a string to prompt with.
469 NAMES is a list of strings to be matched with keys. If it is nil, all
471 If SECRET is non-nil, list secret keys instead of public keys."
472 (let ((keys (epg-list-keys context names secret)))
473 (epa--select-keys prompt keys)))
475 (defun epa--show-key (key)
476 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
477 (entry (assoc (epg-sub-key-id primary-sub-key)
478 epa-key-buffer-alist))
479 (inhibit-read-only t)
483 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
484 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
485 (unless (and (cdr entry)
486 (buffer-live-p (cdr entry)))
487 (setcdr entry (generate-new-buffer
488 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
489 (set-buffer (cdr entry))
491 (make-local-variable 'epa-key)
494 (setq pointer (epg-key-user-id-list key))
498 (if (epg-user-id-validity (car pointer))
500 (car (rassq (epg-user-id-validity (car pointer))
501 epg-key-validity-alist)))
504 (if (stringp (epg-user-id-string (car pointer)))
505 (epg-user-id-string (car pointer))
506 (epg-decode-dn (epg-user-id-string (car pointer))))
508 (setq pointer (cdr pointer)))
509 (setq pointer (epg-key-sub-key-list key))
512 (if (epg-sub-key-validity (car pointer))
514 (car (rassq (epg-sub-key-validity (car pointer))
515 epg-key-validity-alist)))
518 (epg-sub-key-id (car pointer))
521 (epg-sub-key-length (car pointer)))
523 (cdr (assq (epg-sub-key-algorithm (car pointer))
524 epg-pubkey-algorithm-alist))
527 (format-time-string "%Y-%m-%d"
528 (epg-sub-key-creation-time (car pointer)))
529 (error "????-??-??"))
530 (if (epg-sub-key-expiration-time (car pointer))
531 (format (if (time-less-p (current-time)
532 (epg-sub-key-expiration-time
537 (format-time-string "%Y-%m-%d"
538 (epg-sub-key-expiration-time
540 (error "????-??-??")))
543 (mapconcat #'symbol-name
544 (epg-sub-key-capability (car pointer))
547 (epg-sub-key-fingerprint (car pointer))
549 (setq pointer (cdr pointer)))
550 (goto-char (point-min))
551 (pop-to-buffer (current-buffer))))
553 (defun epa-display-info (info)
554 (if epa-popup-info-window
555 (save-selected-window
556 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
557 (setq epa-info-buffer (generate-new-buffer "*Info*")))
558 (if (get-buffer-window epa-info-buffer)
559 (delete-window (get-buffer-window epa-info-buffer)))
560 (with-current-buffer epa-info-buffer
561 (let ((inhibit-read-only t)
566 (goto-char (point-min)))
567 (if (> (window-height)
568 epa-info-window-height)
569 (set-window-buffer (split-window nil (- (window-height)
570 epa-info-window-height))
572 (pop-to-buffer epa-info-buffer)
573 (if (> (window-height) epa-info-window-height)
574 (shrink-window (- (window-height) epa-info-window-height)))))
575 (message "%s" info)))
577 (defun epa-display-verify-result (verify-result)
578 (declare (obsolete epa-display-info "23.1"))
579 (epa-display-info (epg-verify-result-to-string verify-result)))
581 (defun epa-passphrase-callback-function (context key-id handback)
584 (format "Passphrase for symmetric encryption%s: "
585 ;; Add the file name to the prompt, if any.
586 (if (stringp handback)
587 (format " for %s" handback)
589 (eq (epg-context-operation context) 'encrypt))
592 "Passphrase for PIN: "
593 (let ((entry (assoc key-id epg-user-id-alist)))
595 (format "Passphrase for %s %s: " key-id (cdr entry))
596 (format "Passphrase for %s: " key-id)))))))
598 (defun epa-progress-callback-function (_context what _char current total
600 (let ((prompt (or handback
601 (format "Processing %s: " what))))
602 ;; According to gnupg/doc/DETAIL: a "total" of 0 indicates that
603 ;; the total amount is not known. The condition TOTAL && CUR ==
604 ;; TOTAL may be used to detect the end of an operation.
606 (if (= current total)
607 (message "%s...done" prompt)
608 (message "%s...%d%%" prompt
609 (floor (* (/ current (float total)) 100))))
610 (message "%s..." prompt))))
612 (defun epa-read-file-name (input)
613 "Interactively read an output file name based on INPUT file name."
614 (setq input (file-name-sans-extension (expand-file-name input)))
617 (concat "To file (default " (file-name-nondirectory input) ") ")
618 (file-name-directory input)
622 (defun epa-decrypt-file (decrypt-file &optional plain-file)
623 "Decrypt DECRYPT-FILE into PLAIN-FILE.
624 If you do not specify PLAIN-FILE, this functions prompts for the value to use."
626 (let* ((file (read-file-name "File to decrypt: "))
627 (plain (epa-read-file-name file)))
629 (or plain-file (setq plain-file (epa-read-file-name decrypt-file)))
630 (setq decrypt-file (expand-file-name decrypt-file))
631 (let ((context (epg-make-context epa-protocol)))
632 (epg-context-set-passphrase-callback context
633 #'epa-passphrase-callback-function)
634 (epg-context-set-progress-callback context
636 #'epa-progress-callback-function
637 (format "Decrypting %s..."
638 (file-name-nondirectory decrypt-file))))
639 (message "Decrypting %s..." (file-name-nondirectory decrypt-file))
640 (epg-decrypt-file context decrypt-file plain-file)
641 (message "Decrypting %s...wrote %s" (file-name-nondirectory decrypt-file)
642 (file-name-nondirectory plain-file))
643 (if (epg-context-result-for context 'verify)
644 (epa-display-info (epg-verify-result-to-string
645 (epg-context-result-for context 'verify))))))
648 (defun epa-verify-file (file)
650 (interactive "fFile: ")
651 (setq file (expand-file-name file))
652 (let* ((context (epg-make-context epa-protocol))
653 (plain (if (equal (file-name-extension file) "sig")
654 (file-name-sans-extension file))))
655 (epg-context-set-progress-callback context
657 #'epa-progress-callback-function
658 (format "Verifying %s..."
659 (file-name-nondirectory file))))
660 (message "Verifying %s..." (file-name-nondirectory file))
661 (epg-verify-file context file plain)
662 (message "Verifying %s...done" (file-name-nondirectory file))
663 (if (epg-context-result-for context 'verify)
664 (epa-display-info (epg-verify-result-to-string
665 (epg-context-result-for context 'verify))))))
667 (defun epa--read-signature-type ()
670 (message "Signature type (n,c,d,?) ")
675 (setq type 'detached))
677 (with-output-to-temp-buffer "*Help*"
678 (with-current-buffer standard-output
680 n - Create a normal signature
681 c - Create a cleartext signature
682 d - Create a detached signature
686 (setq type 'normal))))
690 (defun epa-sign-file (file signers mode)
691 "Sign FILE by SIGNERS keys selected."
693 (let ((verbose current-prefix-arg))
694 (list (expand-file-name (read-file-name "File: "))
696 (epa-select-keys (epg-make-context epa-protocol)
697 "Select keys for signing.
698 If no one is selected, default secret key is used. "
701 (epa--read-signature-type)
703 (let ((signature (concat file
704 (if (eq epa-protocol 'OpenPGP)
707 '(nil t normal detached))))
709 (if (memq mode '(t detached))
712 (if (memq mode '(t detached))
715 (context (epg-make-context epa-protocol)))
716 (epg-context-set-armor context epa-armor)
717 (epg-context-set-textmode context epa-textmode)
718 (epg-context-set-signers context signers)
719 (epg-context-set-passphrase-callback context
720 #'epa-passphrase-callback-function)
721 (epg-context-set-progress-callback context
723 #'epa-progress-callback-function
724 (format "Signing %s..."
725 (file-name-nondirectory file))))
726 (message "Signing %s..." (file-name-nondirectory file))
727 (epg-sign-file context file signature mode)
728 (message "Signing %s...wrote %s" (file-name-nondirectory file)
729 (file-name-nondirectory signature))))
732 (defun epa-encrypt-file (file recipients)
733 "Encrypt FILE for RECIPIENTS."
735 (list (expand-file-name (read-file-name "File: "))
736 (epa-select-keys (epg-make-context epa-protocol)
737 "Select recipients for encryption.
738 If no one is selected, symmetric encryption will be performed. ")))
739 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
740 (if epa-armor ".asc" ".gpg")
742 (context (epg-make-context epa-protocol)))
743 (epg-context-set-armor context epa-armor)
744 (epg-context-set-textmode context epa-textmode)
745 (epg-context-set-passphrase-callback context
746 #'epa-passphrase-callback-function)
747 (epg-context-set-progress-callback context
749 #'epa-progress-callback-function
750 (format "Encrypting %s..."
751 (file-name-nondirectory file))))
752 (message "Encrypting %s..." (file-name-nondirectory file))
753 (epg-encrypt-file context file recipients cipher)
754 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
755 (file-name-nondirectory cipher))))
758 (defun epa-decrypt-region (start end &optional make-buffer-function)
759 "Decrypt the current region between START and END.
761 If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
762 It should return that buffer. If it copies the input, it should
763 delete the text now being decrypted. It should leave point at the
764 proper place to insert the plaintext.
766 Be careful about using this command in Lisp programs!
767 Since this function operates on regions, it does some tricks such
768 as coding-system detection and unibyte/multibyte conversion. If
769 you are sure how the data in the region should be treated, you
770 should consider using the string based counterpart
771 `epg-decrypt-string', or the file based counterpart
772 `epg-decrypt-file' instead.
776 \(let ((context (epg-make-context 'OpenPGP)))
777 (decode-coding-string
778 (epg-decrypt-string context (buffer-substring start end))
782 (let ((context (epg-make-context epa-protocol))
784 (epg-context-set-passphrase-callback context
785 #'epa-passphrase-callback-function)
786 (epg-context-set-progress-callback context
788 #'epa-progress-callback-function
790 (message "Decrypting...")
791 (setq plain (epg-decrypt-string context (buffer-substring start end)))
792 (message "Decrypting...done")
793 (setq plain (epa--decode-coding-string
795 (or coding-system-for-read
796 (get-text-property start 'epa-coding-system-used)
798 (if make-buffer-function
799 (with-current-buffer (funcall make-buffer-function)
800 (let ((inhibit-read-only t))
802 (if (y-or-n-p "Replace the original text? ")
803 (let ((inhibit-read-only t))
804 (delete-region start end)
807 (with-output-to-temp-buffer "*Temp*"
808 (set-buffer standard-output)
811 (if (epg-context-result-for context 'verify)
812 (epa-display-info (epg-verify-result-to-string
813 (epg-context-result-for context 'verify)))))))
815 (defun epa--find-coding-system-for-mime-charset (mime-charset)
816 (if (featurep 'xemacs)
817 (if (fboundp 'find-coding-system)
818 (find-coding-system mime-charset))
819 ;; Find the first coding system which corresponds to MIME-CHARSET.
820 (let ((pointer (coding-system-list)))
822 (not (eq (coding-system-get (car pointer) 'mime-charset)
824 (setq pointer (cdr pointer)))
828 (defun epa-decrypt-armor-in-region (start end)
829 "Decrypt OpenPGP armors in the current region between START and END.
831 Don't use this command in Lisp programs!
832 See the reason described in the `epa-decrypt-region' documentation."
836 (narrow-to-region start end)
838 (let (armor-start armor-end)
839 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
840 (setq armor-start (match-beginning 0)
841 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
844 (error "Encryption armor beginning has no matching end"))
845 (goto-char armor-start)
846 (let ((coding-system-for-read
847 (or coding-system-for-read
848 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
849 (epa--find-coding-system-for-mime-charset
850 (intern (downcase (match-string 1))))))))
851 (goto-char armor-end)
852 (epa-decrypt-region armor-start armor-end)))))))
855 (defun epa-verify-region (start end)
856 "Verify the current region between START and END.
858 Don't use this command in Lisp programs!
859 Since this function operates on regions, it does some tricks such
860 as coding-system detection and unibyte/multibyte conversion. If
861 you are sure how the data in the region should be treated, you
862 should consider using the string based counterpart
863 `epg-verify-string', or the file based counterpart
864 `epg-verify-file' instead.
868 \(let ((context (epg-make-context 'OpenPGP)))
869 (decode-coding-string
870 (epg-verify-string context (buffer-substring start end))
873 (let ((context (epg-make-context epa-protocol))
875 (epg-context-set-progress-callback context
877 #'epa-progress-callback-function
879 (message "Verifying...")
880 (setq plain (epg-verify-string
882 (epa--encode-coding-string
883 (buffer-substring start end)
884 (or coding-system-for-write
885 (get-text-property start 'epa-coding-system-used)))))
886 (message "Verifying...done")
887 (setq plain (epa--decode-coding-string
889 (or coding-system-for-read
890 (get-text-property start 'epa-coding-system-used)
892 (if (y-or-n-p "Replace the original text? ")
893 (let ((inhibit-read-only t)
895 (delete-region start end)
898 (with-output-to-temp-buffer "*Temp*"
899 (set-buffer standard-output)
902 (if (epg-context-result-for context 'verify)
903 (epa-display-info (epg-verify-result-to-string
904 (epg-context-result-for context 'verify))))))
907 (defun epa-verify-cleartext-in-region (start end)
908 "Verify OpenPGP cleartext signed messages in the current region
909 between START and END.
911 Don't use this command in Lisp programs!
912 See the reason described in the `epa-verify-region' documentation."
916 (narrow-to-region start end)
918 (let (cleartext-start cleartext-end)
919 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
921 (setq cleartext-start (match-beginning 0))
922 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
924 (error "Invalid cleartext signed message"))
925 (setq cleartext-end (re-search-forward
926 "^-----END PGP SIGNATURE-----$"
928 (unless cleartext-end
929 (error "No cleartext tail"))
930 (epa-verify-region cleartext-start cleartext-end))))))
932 (defalias 'epa--select-safe-coding-system
933 (if (fboundp 'select-safe-coding-system)
934 #'select-safe-coding-system
936 buffer-file-coding-system)))
939 (defun epa-sign-region (start end signers mode)
940 "Sign the current region between START and END by SIGNERS keys selected.
942 Don't use this command in Lisp programs!
943 Since this function operates on regions, it does some tricks such
944 as coding-system detection and unibyte/multibyte conversion. If
945 you are sure how the data should be treated, you should consider
946 using the string based counterpart `epg-sign-string', or the file
947 based counterpart `epg-sign-file' instead.
951 \(let ((context (epg-make-context 'OpenPGP)))
954 (encode-coding-string (buffer-substring start end) 'utf-8)))"
956 (let ((verbose current-prefix-arg))
957 (setq epa-last-coding-system-specified
958 (or coding-system-for-write
959 (epa--select-safe-coding-system
960 (region-beginning) (region-end))))
961 (list (region-beginning) (region-end)
963 (epa-select-keys (epg-make-context epa-protocol)
964 "Select keys for signing.
965 If no one is selected, default secret key is used. "
968 (epa--read-signature-type)
971 (let ((context (epg-make-context epa-protocol))
973 ;;(epg-context-set-armor context epa-armor)
974 (epg-context-set-armor context t)
975 ;;(epg-context-set-textmode context epa-textmode)
976 (epg-context-set-textmode context t)
977 (epg-context-set-signers context signers)
978 (epg-context-set-passphrase-callback context
979 #'epa-passphrase-callback-function)
980 (epg-context-set-progress-callback context
982 #'epa-progress-callback-function
984 (message "Signing...")
985 (setq signature (epg-sign-string context
986 (epa--encode-coding-string
987 (buffer-substring start end)
988 epa-last-coding-system-specified)
990 (message "Signing...done")
991 (delete-region start end)
993 (add-text-properties (point)
995 (insert (epa--decode-coding-string
997 (or coding-system-for-read
998 epa-last-coding-system-specified)))
1000 (list 'epa-coding-system-used
1001 epa-last-coding-system-specified
1007 (defalias 'epa--derived-mode-p
1008 (if (fboundp 'derived-mode-p)
1010 (lambda (&rest modes)
1011 "Non-nil if the current major mode is derived from one of MODES.
1012 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1013 (let ((parent major-mode))
1014 (while (and (not (memq parent modes))
1015 (setq parent (get parent 'derived-mode-parent))))
1019 (defun epa-encrypt-region (start end recipients sign signers)
1020 "Encrypt the current region between START and END for RECIPIENTS.
1022 Don't use this command in Lisp programs!
1023 Since this function operates on regions, it does some tricks such
1024 as coding-system detection and unibyte/multibyte conversion. If
1025 you are sure how the data should be treated, you should consider
1026 using the string based counterpart `epg-encrypt-string', or the
1027 file based counterpart `epg-encrypt-file' instead.
1031 \(let ((context (epg-make-context 'OpenPGP)))
1034 (encode-coding-string (buffer-substring start end) 'utf-8)
1037 (let ((verbose current-prefix-arg)
1038 (context (epg-make-context epa-protocol))
1040 (setq epa-last-coding-system-specified
1041 (or coding-system-for-write
1042 (epa--select-safe-coding-system
1043 (region-beginning) (region-end))))
1044 (list (region-beginning) (region-end)
1045 (epa-select-keys context
1046 "Select recipients for encryption.
1047 If no one is selected, symmetric encryption will be performed. ")
1048 (setq sign (if verbose (y-or-n-p "Sign? ")))
1050 (epa-select-keys context
1051 "Select keys for signing. ")))))
1053 (let ((context (epg-make-context epa-protocol))
1055 ;;(epg-context-set-armor context epa-armor)
1056 (epg-context-set-armor context t)
1057 ;;(epg-context-set-textmode context epa-textmode)
1058 (epg-context-set-textmode context t)
1060 (epg-context-set-signers context signers))
1061 (epg-context-set-passphrase-callback context
1062 #'epa-passphrase-callback-function)
1063 (epg-context-set-progress-callback context
1065 #'epa-progress-callback-function
1067 (message "Encrypting...")
1068 (setq cipher (epg-encrypt-string context
1069 (epa--encode-coding-string
1070 (buffer-substring start end)
1071 epa-last-coding-system-specified)
1074 (message "Encrypting...done")
1075 (delete-region start end)
1077 (add-text-properties (point)
1081 (list 'epa-coding-system-used
1082 epa-last-coding-system-specified
1089 (defun epa-delete-keys (keys &optional allow-secret)
1090 "Delete selected KEYS."
1092 (let ((keys (epa--marked-keys)))
1094 (error "No keys selected"))
1096 (eq (nth 1 epa-list-keys-arguments) t))))
1097 (let ((context (epg-make-context epa-protocol)))
1098 (message "Deleting...")
1099 (epg-delete-keys context keys allow-secret)
1100 (message "Deleting...done")
1101 (apply #'epa--list-keys epa-list-keys-arguments)))
1104 (defun epa-import-keys (file)
1105 "Import keys from FILE."
1106 (interactive "fFile: ")
1107 (setq file (expand-file-name file))
1108 (let ((context (epg-make-context epa-protocol)))
1109 (message "Importing %s..." (file-name-nondirectory file))
1112 (epg-import-keys-from-file context file)
1113 (message "Importing %s...done" (file-name-nondirectory file)))
1115 (message "Importing %s...failed" (file-name-nondirectory file))))
1116 (if (epg-context-result-for context 'import)
1117 (epa-display-info (epg-import-result-to-string
1118 (epg-context-result-for context 'import))))
1119 ;; FIXME: Why not use the (otherwise unused) epa--derived-mode-p?
1120 (if (eq major-mode 'epa-key-list-mode)
1121 (apply #'epa--list-keys epa-list-keys-arguments))))
1124 (defun epa-import-keys-region (start end)
1125 "Import keys from the region."
1127 (let ((context (epg-make-context epa-protocol)))
1128 (message "Importing...")
1131 (epg-import-keys-from-string context (buffer-substring start end))
1132 (message "Importing...done"))
1134 (message "Importing...failed")))
1135 (if (epg-context-result-for context 'import)
1136 (epa-display-info (epg-import-result-to-string
1137 (epg-context-result-for context 'import))))))
1140 (defun epa-import-armor-in-region (start end)
1141 "Import keys in the OpenPGP armor format in the current region
1142 between START and END."
1146 (narrow-to-region start end)
1148 (let (armor-start armor-end)
1149 (while (re-search-forward
1150 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1152 (setq armor-start (match-beginning 0)
1153 armor-end (re-search-forward
1154 (concat "^-----END " (match-string 1) "-----$")
1157 (error "No armor tail"))
1158 (epa-import-keys-region armor-start armor-end))))))
1161 (defun epa-export-keys (keys file)
1162 "Export selected KEYS to FILE."
1164 (let ((keys (epa--marked-keys))
1167 (error "No keys selected"))
1170 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1171 (if epa-armor ".asc" ".gpg"))
1176 (concat "To file (default "
1177 (file-name-nondirectory default-name)
1179 (file-name-directory default-name)
1181 (let ((context (epg-make-context epa-protocol)))
1182 (epg-context-set-armor context epa-armor)
1183 (message "Exporting to %s..." (file-name-nondirectory file))
1184 (epg-export-keys-to-file context keys file)
1185 (message "Exporting to %s...done" (file-name-nondirectory file))))
1188 (defun epa-insert-keys (keys)
1189 "Insert selected KEYS after the point."
1191 (list (epa-select-keys (epg-make-context epa-protocol)
1192 "Select keys to export.
1193 If no one is selected, default public key is exported. ")))
1194 (let ((context (epg-make-context epa-protocol)))
1195 ;;(epg-context-set-armor context epa-armor)
1196 (epg-context-set-armor context t)
1197 (insert (epg-export-keys-to-string context keys))))
1199 ;; (defun epa-sign-keys (keys &optional local)
1200 ;; "Sign selected KEYS.
1201 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1203 ;; Don't use this command in Lisp programs!"
1205 ;; (let ((keys (epa--marked-keys)))
1207 ;; (error "No keys selected"))
1208 ;; (list keys current-prefix-arg)))
1209 ;; (let ((context (epg-make-context epa-protocol)))
1210 ;; (epg-context-set-passphrase-callback context
1211 ;; #'epa-passphrase-callback-function)
1212 ;; (epg-context-set-progress-callback context
1214 ;; #'epa-progress-callback-function
1215 ;; "Signing keys..."))
1216 ;; (message "Signing keys...")
1217 ;; (epg-sign-keys context keys local)
1218 ;; (message "Signing keys...done")))
1219 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1223 ;;; epa.el ends here