* lisp/minibuffer.el (completion-in-region-mode-predicate)
[emacs.git] / lisp / epa.el
blobd4f4fab2eedae25615310250d80e6acfb5603619
1 ;;; epa.el --- the EasyPG Assistant -*- lexical-binding: t -*-
3 ;; Copyright (C) 2006-2011 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/>.
23 ;;; Code:
25 (require 'epg)
26 (require 'font-lock)
27 (require 'widget)
28 (eval-when-compile (require 'wid-edit))
29 (require 'derived)
31 (defgroup epa nil
32 "The EasyPG Assistant"
33 :version "23.1"
34 :group 'epg)
36 (defcustom epa-popup-info-window t
37 "If non-nil, status information from epa commands is displayed on
38 the separate window."
39 :type 'boolean
40 :group 'epa)
42 (defcustom epa-info-window-height 5
43 "Number of lines used to display status information."
44 :type 'integer
45 :group 'epa)
47 (defgroup epa-faces nil
48 "Faces for epa-mode."
49 :version "23.1"
50 :group 'epa)
52 (defface epa-validity-high
53 `((((class color) (background dark))
54 (:foreground "PaleTurquoise"
55 ,@(if (assq ':weight custom-face-attributes)
56 '(:weight bold)
57 '(:bold t))))
59 (,@(if (assq ':weight custom-face-attributes)
60 '(:weight bold)
61 '(:bold t)))))
62 "Face used for displaying the high validity."
63 :group 'epa-faces)
65 (defface epa-validity-medium
66 `((((class color) (background dark))
67 (:foreground "PaleTurquoise"
68 ,@(if (assq ':slant custom-face-attributes)
69 '(:slant italic)
70 '(:italic t))))
72 (,@(if (assq ':slant custom-face-attributes)
73 '(:slant italic)
74 '(:italic t)))))
75 "Face used for displaying the medium validity."
76 :group 'epa-faces)
78 (defface epa-validity-low
79 `((t
80 (,@(if (assq ':slant custom-face-attributes)
81 '(:slant italic)
82 '(:italic t)))))
83 "Face used for displaying the low validity."
84 :group 'epa-faces)
86 (defface epa-validity-disabled
87 `((t
88 (,@(if (assq ':slant custom-face-attributes)
89 '(:slant italic)
90 '(:italic t))
91 :inverse-video t)))
92 "Face used for displaying the disabled validity."
93 :group 'epa-faces)
95 (defface epa-string
96 '((((class color) (background dark))
97 (:foreground "lightyellow"))
98 (((class color) (background light))
99 (:foreground "blue4")))
100 "Face used for displaying the string."
101 :group 'epa-faces)
103 (defface epa-mark
104 `((((class color) (background dark))
105 (:foreground "orange"
106 ,@(if (assq ':weight custom-face-attributes)
107 '(:weight bold)
108 '(:bold t))))
109 (((class color) (background light))
110 (:foreground "red"
111 ,@(if (assq ':weight custom-face-attributes)
112 '(:weight bold)
113 '(:bold t))))
115 (,@(if (assq ':weight custom-face-attributes)
116 '(:weight bold)
117 '(:bold t)))))
118 "Face used for displaying the high validity."
119 :group 'epa-faces)
121 (defface epa-field-name
122 `((((class color) (background dark))
123 (:foreground "PaleTurquoise"
124 ,@(if (assq ':weight custom-face-attributes)
125 '(:weight bold)
126 '(:bold t))))
128 (,@(if (assq ':weight custom-face-attributes)
129 '(:weight bold)
130 '(:bold t)))))
131 "Face for the name of the attribute field."
132 :group 'epa)
134 (defface epa-field-body
135 `((((class color) (background dark))
136 (:foreground "turquoise"
137 ,@(if (assq ':slant custom-face-attributes)
138 '(:slant italic)
139 '(:italic t))))
141 (,@(if (assq ':slant custom-face-attributes)
142 '(:slant italic)
143 '(:italic t)))))
144 "Face for the body of the attribute field."
145 :group 'epa)
147 (defcustom epa-validity-face-alist
148 '((unknown . epa-validity-disabled)
149 (invalid . epa-validity-disabled)
150 (disabled . epa-validity-disabled)
151 (revoked . epa-validity-disabled)
152 (expired . epa-validity-disabled)
153 (none . epa-validity-low)
154 (undefined . epa-validity-low)
155 (never . epa-validity-low)
156 (marginal . epa-validity-medium)
157 (full . epa-validity-high)
158 (ultimate . epa-validity-high))
159 "An alist mapping validity values to faces."
160 :type '(repeat (cons symbol face))
161 :group 'epa)
163 (defvar epa-font-lock-keywords
164 '(("^\\*"
165 (0 'epa-mark))
166 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
167 (1 'epa-field-name)
168 (2 'epa-field-body)))
169 "Default expressions to addon in epa-mode.")
171 (defconst epa-pubkey-algorithm-letter-alist
172 '((1 . ?R)
173 (2 . ?r)
174 (3 . ?s)
175 (16 . ?g)
176 (17 . ?D)
177 (20 . ?G)))
179 (defvar epa-protocol 'OpenPGP
180 "*The default protocol.
181 The value can be either OpenPGP or CMS.
183 You should bind this variable with `let', but do not set it globally.")
185 (defvar epa-armor nil
186 "*If non-nil, epa commands create ASCII armored output.
188 You should bind this variable with `let', but do not set it globally.")
190 (defvar epa-textmode nil
191 "*If non-nil, epa commands treat input files as text.
193 You should bind this variable with `let', but do not set it globally.")
195 (defvar epa-keys-buffer nil)
196 (defvar epa-key-buffer-alist nil)
197 (defvar epa-key nil)
198 (defvar epa-list-keys-arguments nil)
199 (defvar epa-info-buffer nil)
200 (defvar epa-last-coding-system-specified nil)
202 (defvar epa-key-list-mode-map
203 (let ((keymap (make-sparse-keymap))
204 (menu-map (make-sparse-keymap)))
205 (define-key keymap "m" 'epa-mark-key)
206 (define-key keymap "u" 'epa-unmark-key)
207 (define-key keymap "d" 'epa-decrypt-file)
208 (define-key keymap "v" 'epa-verify-file)
209 (define-key keymap "s" 'epa-sign-file)
210 (define-key keymap "e" 'epa-encrypt-file)
211 (define-key keymap "r" 'epa-delete-keys)
212 (define-key keymap "i" 'epa-import-keys)
213 (define-key keymap "o" 'epa-export-keys)
214 (define-key keymap "g" 'revert-buffer)
215 (define-key keymap "n" 'next-line)
216 (define-key keymap "p" 'previous-line)
217 (define-key keymap " " 'scroll-up)
218 (define-key keymap [delete] 'scroll-down)
219 (define-key keymap "q" 'epa-exit-buffer)
220 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
221 (define-key menu-map [epa-key-list-unmark-key]
222 '(menu-item "Unmark Key" epa-unmark-key
223 :help "Unmark a key"))
224 (define-key menu-map [epa-key-list-mark-key]
225 '(menu-item "Mark Key" epa-mark-key
226 :help "Mark a key"))
227 (define-key menu-map [separator-epa-file] '(menu-item "--"))
228 (define-key menu-map [epa-verify-file]
229 '(menu-item "Verify File..." epa-verify-file
230 :help "Verify FILE"))
231 (define-key menu-map [epa-sign-file]
232 '(menu-item "Sign File..." epa-sign-file
233 :help "Sign FILE by SIGNERS keys selected"))
234 (define-key menu-map [epa-decrypt-file]
235 '(menu-item "Decrypt File..." epa-decrypt-file
236 :help "Decrypt FILE"))
237 (define-key menu-map [epa-encrypt-file]
238 '(menu-item "Encrypt File..." epa-encrypt-file
239 :help "Encrypt FILE for RECIPIENTS"))
240 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
241 (define-key menu-map [epa-key-list-delete-keys]
242 '(menu-item "Delete keys" epa-delete-keys
243 :help "Delete Marked Keys"))
244 (define-key menu-map [epa-key-list-import-keys]
245 '(menu-item "Import Keys" epa-import-keys
246 :help "Import keys from a file"))
247 (define-key menu-map [epa-key-list-export-keys]
248 '(menu-item "Export Keys" epa-export-keys
249 :help "Export marked keys to a file"))
250 keymap))
252 (defvar epa-key-mode-map
253 (let ((keymap (make-sparse-keymap)))
254 (define-key keymap "q" 'epa-exit-buffer)
255 keymap))
257 (defvar epa-info-mode-map
258 (let ((keymap (make-sparse-keymap)))
259 (define-key keymap "q" 'delete-window)
260 keymap))
262 (defvar epa-exit-buffer-function #'bury-buffer)
264 (define-widget 'epa-key 'push-button
265 "Button for representing a epg-key object."
266 :format "%[%v%]"
267 :button-face-get 'epa--key-widget-button-face-get
268 :value-create 'epa--key-widget-value-create
269 :action 'epa--key-widget-action
270 :help-echo 'epa--key-widget-help-echo)
272 (defun epa--key-widget-action (widget &optional _event)
273 (save-selected-window
274 (epa--show-key (widget-get widget :value))))
276 (defun epa--key-widget-value-create (widget)
277 (let* ((key (widget-get widget :value))
278 (primary-sub-key (car (epg-key-sub-key-list key)))
279 (primary-user-id (car (epg-key-user-id-list key))))
280 (insert (format "%c "
281 (if (epg-sub-key-validity primary-sub-key)
282 (car (rassq (epg-sub-key-validity primary-sub-key)
283 epg-key-validity-alist))
284 ? ))
285 (epg-sub-key-id primary-sub-key)
287 (if primary-user-id
288 (if (stringp (epg-user-id-string primary-user-id))
289 (epg-user-id-string primary-user-id)
290 (epg-decode-dn (epg-user-id-string primary-user-id)))
291 ""))))
293 (defun epa--key-widget-button-face-get (widget)
294 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
295 (widget-get widget :value))))))
296 (if validity
297 (cdr (assq validity epa-validity-face-alist))
298 'default)))
300 (defun epa--key-widget-help-echo (widget)
301 (format "Show %s"
302 (epg-sub-key-id (car (epg-key-sub-key-list
303 (widget-get widget :value))))))
305 (eval-and-compile
306 (if (fboundp 'encode-coding-string)
307 (defalias 'epa--encode-coding-string 'encode-coding-string)
308 (defalias 'epa--encode-coding-string 'identity)))
310 (eval-and-compile
311 (if (fboundp 'decode-coding-string)
312 (defalias 'epa--decode-coding-string 'decode-coding-string)
313 (defalias 'epa--decode-coding-string 'identity)))
315 (defun epa-key-list-mode ()
316 "Major mode for `epa-list-keys'."
317 (kill-all-local-variables)
318 (buffer-disable-undo)
319 (setq major-mode 'epa-key-list-mode
320 mode-name "Keys"
321 truncate-lines t
322 buffer-read-only t)
323 (use-local-map epa-key-list-mode-map)
324 (make-local-variable 'font-lock-defaults)
325 (setq font-lock-defaults '(epa-font-lock-keywords t))
326 ;; In XEmacs, auto-initialization of font-lock is not effective
327 ;; if buffer-file-name is not set.
328 (font-lock-set-defaults)
329 (make-local-variable 'epa-exit-buffer-function)
330 (make-local-variable 'revert-buffer-function)
331 (setq revert-buffer-function 'epa--key-list-revert-buffer)
332 (run-mode-hooks 'epa-key-list-mode-hook))
334 (defun epa-key-mode ()
335 "Major mode for a key description."
336 (kill-all-local-variables)
337 (buffer-disable-undo)
338 (setq major-mode 'epa-key-mode
339 mode-name "Key"
340 truncate-lines t
341 buffer-read-only t)
342 (use-local-map epa-key-mode-map)
343 (make-local-variable 'font-lock-defaults)
344 (setq font-lock-defaults '(epa-font-lock-keywords t))
345 ;; In XEmacs, auto-initialization of font-lock is not effective
346 ;; if buffer-file-name is not set.
347 (font-lock-set-defaults)
348 (make-local-variable 'epa-exit-buffer-function)
349 (run-mode-hooks 'epa-key-mode-hook))
351 (defun epa-info-mode ()
352 "Major mode for `epa-info-buffer'."
353 (kill-all-local-variables)
354 (buffer-disable-undo)
355 (setq major-mode 'epa-info-mode
356 mode-name "Info"
357 truncate-lines t
358 buffer-read-only t)
359 (use-local-map epa-info-mode-map)
360 (run-mode-hooks 'epa-info-mode-hook))
362 (defun epa-mark-key (&optional arg)
363 "Mark a key on the current line.
364 If ARG is non-nil, unmark the key."
365 (interactive "P")
366 (let ((inhibit-read-only t)
367 buffer-read-only
368 properties)
369 (beginning-of-line)
370 (unless (get-text-property (point) 'epa-key)
371 (error "No key on this line"))
372 (setq properties (text-properties-at (point)))
373 (delete-char 1)
374 (insert (if arg " " "*"))
375 (set-text-properties (1- (point)) (point) properties)
376 (forward-line)))
378 (defun epa-unmark-key (&optional arg)
379 "Unmark a key on the current line.
380 If ARG is non-nil, mark the key."
381 (interactive "P")
382 (epa-mark-key (not arg)))
384 (defun epa-exit-buffer ()
385 "Exit the current buffer.
386 `epa-exit-buffer-function' is called if it is set."
387 (interactive)
388 (funcall epa-exit-buffer-function))
390 (defun epa--insert-keys (keys)
391 (save-excursion
392 (save-restriction
393 (narrow-to-region (point) (point))
394 (let (point)
395 (while keys
396 (setq point (point))
397 (insert " ")
398 (add-text-properties point (point)
399 (list 'epa-key (car keys)
400 'front-sticky nil
401 'rear-nonsticky t
402 'start-open t
403 'end-open t))
404 (widget-create 'epa-key :value (car keys))
405 (insert "\n")
406 (setq keys (cdr keys))))
407 (add-text-properties (point-min) (point-max)
408 (list 'epa-list-keys t
409 'front-sticky nil
410 'rear-nonsticky t
411 'start-open t
412 'end-open t)))))
414 (defun epa--list-keys (name secret)
415 (unless (and epa-keys-buffer
416 (buffer-live-p epa-keys-buffer))
417 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
418 (set-buffer epa-keys-buffer)
419 (epa-key-list-mode)
420 (let ((inhibit-read-only t)
421 buffer-read-only
422 (point (point-min))
423 (context (epg-make-context epa-protocol)))
424 (unless (get-text-property point 'epa-list-keys)
425 (setq point (next-single-property-change point 'epa-list-keys)))
426 (when point
427 (delete-region point
428 (or (next-single-property-change point 'epa-list-keys)
429 (point-max)))
430 (goto-char point))
431 (epa--insert-keys (epg-list-keys context name secret))
432 (widget-setup)
433 (set-keymap-parent (current-local-map) widget-keymap))
434 (make-local-variable 'epa-list-keys-arguments)
435 (setq epa-list-keys-arguments (list name secret))
436 (goto-char (point-min))
437 (pop-to-buffer (current-buffer)))
439 ;;;###autoload
440 (defun epa-list-keys (&optional name)
441 "List all keys matched with NAME from the public keyring."
442 (interactive
443 (if current-prefix-arg
444 (let ((name (read-string "Pattern: "
445 (if epa-list-keys-arguments
446 (car epa-list-keys-arguments)))))
447 (list (if (equal name "") nil name)))
448 (list nil)))
449 (epa--list-keys name nil))
451 ;;;###autoload
452 (defun epa-list-secret-keys (&optional name)
453 "List all keys matched with NAME from the private keyring."
454 (interactive
455 (if current-prefix-arg
456 (let ((name (read-string "Pattern: "
457 (if epa-list-keys-arguments
458 (car epa-list-keys-arguments)))))
459 (list (if (equal name "") nil name)))
460 (list nil)))
461 (epa--list-keys name t))
463 (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm)
464 (apply #'epa--list-keys epa-list-keys-arguments))
466 (defun epa--marked-keys ()
467 (or (with-current-buffer epa-keys-buffer
468 (goto-char (point-min))
469 (let (keys key)
470 (while (re-search-forward "^\\*" nil t)
471 (if (setq key (get-text-property (match-beginning 0)
472 'epa-key))
473 (setq keys (cons key keys))))
474 (nreverse keys)))
475 (let ((key (get-text-property (point-at-bol) 'epa-key)))
476 (if key
477 (list key)))))
479 (defun epa--select-keys (prompt keys)
480 (unless (and epa-keys-buffer
481 (buffer-live-p epa-keys-buffer))
482 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
483 (with-current-buffer epa-keys-buffer
484 (epa-key-list-mode)
485 (let ((inhibit-read-only t)
486 buffer-read-only)
487 (erase-buffer)
488 (insert prompt "\n"
489 (substitute-command-keys "\
490 - `\\[epa-mark-key]' to mark a key on the line
491 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
492 (widget-create 'link
493 :notify (lambda (&rest _ignore) (abort-recursive-edit))
494 :help-echo
495 (substitute-command-keys
496 "Click here or \\[abort-recursive-edit] to cancel")
497 "Cancel")
498 (widget-create 'link
499 :notify (lambda (&rest _ignore) (exit-recursive-edit))
500 :help-echo
501 (substitute-command-keys
502 "Click here or \\[exit-recursive-edit] to finish")
503 "OK")
504 (insert "\n\n")
505 (epa--insert-keys keys)
506 (widget-setup)
507 (set-keymap-parent (current-local-map) widget-keymap)
508 (setq epa-exit-buffer-function #'abort-recursive-edit)
509 (goto-char (point-min))
510 (let ((display-buffer-mark-dedicated 'soft))
511 (pop-to-buffer (current-buffer))))
512 (unwind-protect
513 (progn
514 (recursive-edit)
515 (epa--marked-keys))
516 (kill-buffer epa-keys-buffer))))
518 ;;;###autoload
519 (defun epa-select-keys (context prompt &optional names secret)
520 "Display a user's keyring and ask him to select keys.
521 CONTEXT is an epg-context.
522 PROMPT is a string to prompt with.
523 NAMES is a list of strings to be matched with keys. If it is nil, all
524 the keys are listed.
525 If SECRET is non-nil, list secret keys instead of public keys."
526 (let ((keys (epg-list-keys context names secret)))
527 (epa--select-keys prompt keys)))
529 (defun epa--show-key (key)
530 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
531 (entry (assoc (epg-sub-key-id primary-sub-key)
532 epa-key-buffer-alist))
533 (inhibit-read-only t)
534 buffer-read-only
535 pointer)
536 (unless entry
537 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
538 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
539 (unless (and (cdr entry)
540 (buffer-live-p (cdr entry)))
541 (setcdr entry (generate-new-buffer
542 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
543 (set-buffer (cdr entry))
544 (epa-key-mode)
545 (make-local-variable 'epa-key)
546 (setq epa-key key)
547 (erase-buffer)
548 (setq pointer (epg-key-user-id-list key))
549 (while pointer
550 (if (car pointer)
551 (insert " "
552 (if (epg-user-id-validity (car pointer))
553 (char-to-string
554 (car (rassq (epg-user-id-validity (car pointer))
555 epg-key-validity-alist)))
556 " ")
558 (if (stringp (epg-user-id-string (car pointer)))
559 (epg-user-id-string (car pointer))
560 (epg-decode-dn (epg-user-id-string (car pointer))))
561 "\n"))
562 (setq pointer (cdr pointer)))
563 (setq pointer (epg-key-sub-key-list key))
564 (while pointer
565 (insert " "
566 (if (epg-sub-key-validity (car pointer))
567 (char-to-string
568 (car (rassq (epg-sub-key-validity (car pointer))
569 epg-key-validity-alist)))
570 " ")
572 (epg-sub-key-id (car pointer))
574 (format "%dbits"
575 (epg-sub-key-length (car pointer)))
577 (cdr (assq (epg-sub-key-algorithm (car pointer))
578 epg-pubkey-algorithm-alist))
579 "\n\tCreated: "
580 (condition-case nil
581 (format-time-string "%Y-%m-%d"
582 (epg-sub-key-creation-time (car pointer)))
583 (error "????-??-??"))
584 (if (epg-sub-key-expiration-time (car pointer))
585 (format (if (time-less-p (current-time)
586 (epg-sub-key-expiration-time
587 (car pointer)))
588 "\n\tExpires: %s"
589 "\n\tExpired: %s")
590 (condition-case nil
591 (format-time-string "%Y-%m-%d"
592 (epg-sub-key-expiration-time
593 (car pointer)))
594 (error "????-??-??")))
596 "\n\tCapabilities: "
597 (mapconcat #'symbol-name
598 (epg-sub-key-capability (car pointer))
599 " ")
600 "\n\tFingerprint: "
601 (epg-sub-key-fingerprint (car pointer))
602 "\n")
603 (setq pointer (cdr pointer)))
604 (goto-char (point-min))
605 (pop-to-buffer (current-buffer))))
607 (defun epa-display-info (info)
608 (if epa-popup-info-window
609 (save-selected-window
610 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
611 (setq epa-info-buffer (generate-new-buffer "*Info*")))
612 (if (get-buffer-window epa-info-buffer)
613 (delete-window (get-buffer-window epa-info-buffer)))
614 (with-current-buffer epa-info-buffer
615 (let ((inhibit-read-only t)
616 buffer-read-only)
617 (erase-buffer)
618 (insert info))
619 (epa-info-mode)
620 (goto-char (point-min)))
621 (if (> (window-height)
622 epa-info-window-height)
623 (set-window-buffer (split-window nil (- (window-height)
624 epa-info-window-height))
625 epa-info-buffer)
626 (pop-to-buffer epa-info-buffer)
627 (if (> (window-height) epa-info-window-height)
628 (shrink-window (- (window-height) epa-info-window-height)))))
629 (message "%s" info)))
631 (defun epa-display-verify-result (verify-result)
632 (epa-display-info (epg-verify-result-to-string verify-result)))
633 (make-obsolete 'epa-display-verify-result 'epa-display-info "23.1")
635 (defun epa-passphrase-callback-function (context key-id handback)
636 (if (eq key-id 'SYM)
637 (read-passwd
638 (format "Passphrase for symmetric encryption%s: "
639 ;; Add the file name to the prompt, if any.
640 (if (stringp handback)
641 (format " for %s" handback)
642 ""))
643 (eq (epg-context-operation context) 'encrypt))
644 (read-passwd
645 (if (eq key-id 'PIN)
646 "Passphrase for PIN: "
647 (let ((entry (assoc key-id epg-user-id-alist)))
648 (if entry
649 (format "Passphrase for %s %s: " key-id (cdr entry))
650 (format "Passphrase for %s: " key-id)))))))
652 (defun epa-progress-callback-function (_context what _char current total
653 handback)
654 (message "%s%d%% (%d/%d)" (or handback
655 (concat what ": "))
656 (if (> total 0) (floor (* (/ current (float total)) 100)) 0)
657 current total))
659 ;;;###autoload
660 (defun epa-decrypt-file (file)
661 "Decrypt FILE."
662 (interactive "fFile: ")
663 (setq file (expand-file-name file))
664 (let* ((default-name (file-name-sans-extension file))
665 (plain (expand-file-name
666 (read-file-name
667 (concat "To file (default "
668 (file-name-nondirectory default-name)
669 ") ")
670 (file-name-directory default-name)
671 default-name)))
672 (context (epg-make-context epa-protocol)))
673 (epg-context-set-passphrase-callback context
674 #'epa-passphrase-callback-function)
675 (epg-context-set-progress-callback context
676 (cons
677 #'epa-progress-callback-function
678 (format "Decrypting %s..."
679 (file-name-nondirectory file))))
680 (message "Decrypting %s..." (file-name-nondirectory file))
681 (epg-decrypt-file context file plain)
682 (message "Decrypting %s...wrote %s" (file-name-nondirectory file)
683 (file-name-nondirectory plain))
684 (if (epg-context-result-for context 'verify)
685 (epa-display-info (epg-verify-result-to-string
686 (epg-context-result-for context 'verify))))))
688 ;;;###autoload
689 (defun epa-verify-file (file)
690 "Verify FILE."
691 (interactive "fFile: ")
692 (setq file (expand-file-name file))
693 (let* ((context (epg-make-context epa-protocol))
694 (plain (if (equal (file-name-extension file) "sig")
695 (file-name-sans-extension file))))
696 (epg-context-set-progress-callback context
697 (cons
698 #'epa-progress-callback-function
699 (format "Verifying %s..."
700 (file-name-nondirectory file))))
701 (message "Verifying %s..." (file-name-nondirectory file))
702 (epg-verify-file context file plain)
703 (message "Verifying %s...done" (file-name-nondirectory file))
704 (if (epg-context-result-for context 'verify)
705 (epa-display-info (epg-verify-result-to-string
706 (epg-context-result-for context 'verify))))))
708 (defun epa--read-signature-type ()
709 (let (type c)
710 (while (null type)
711 (message "Signature type (n,c,d,?) ")
712 (setq c (read-char))
713 (cond ((eq c ?c)
714 (setq type 'clear))
715 ((eq c ?d)
716 (setq type 'detached))
717 ((eq c ??)
718 (with-output-to-temp-buffer "*Help*"
719 (with-current-buffer standard-output
720 (insert "\
721 n - Create a normal signature
722 c - Create a cleartext signature
723 d - Create a detached signature
724 ? - Show this help
725 "))))
727 (setq type 'normal))))
728 type))
730 ;;;###autoload
731 (defun epa-sign-file (file signers mode)
732 "Sign FILE by SIGNERS keys selected."
733 (interactive
734 (let ((verbose current-prefix-arg))
735 (list (expand-file-name (read-file-name "File: "))
736 (if verbose
737 (epa-select-keys (epg-make-context epa-protocol)
738 "Select keys for signing.
739 If no one is selected, default secret key is used. "
740 nil t))
741 (if verbose
742 (epa--read-signature-type)
743 'clear))))
744 (let ((signature (concat file
745 (if (eq epa-protocol 'OpenPGP)
746 (if (or epa-armor
747 (not (memq mode
748 '(nil t normal detached))))
749 ".asc"
750 (if (memq mode '(t detached))
751 ".sig"
752 ".gpg"))
753 (if (memq mode '(t detached))
754 ".p7s"
755 ".p7m"))))
756 (context (epg-make-context epa-protocol)))
757 (epg-context-set-armor context epa-armor)
758 (epg-context-set-textmode context epa-textmode)
759 (epg-context-set-signers context signers)
760 (epg-context-set-passphrase-callback context
761 #'epa-passphrase-callback-function)
762 (epg-context-set-progress-callback context
763 (cons
764 #'epa-progress-callback-function
765 (format "Signing %s..."
766 (file-name-nondirectory file))))
767 (message "Signing %s..." (file-name-nondirectory file))
768 (epg-sign-file context file signature mode)
769 (message "Signing %s...wrote %s" (file-name-nondirectory file)
770 (file-name-nondirectory signature))))
772 ;;;###autoload
773 (defun epa-encrypt-file (file recipients)
774 "Encrypt FILE for RECIPIENTS."
775 (interactive
776 (list (expand-file-name (read-file-name "File: "))
777 (epa-select-keys (epg-make-context epa-protocol)
778 "Select recipients for encryption.
779 If no one is selected, symmetric encryption will be performed. ")))
780 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
781 (if epa-armor ".asc" ".gpg")
782 ".p7m")))
783 (context (epg-make-context epa-protocol)))
784 (epg-context-set-armor context epa-armor)
785 (epg-context-set-textmode context epa-textmode)
786 (epg-context-set-passphrase-callback context
787 #'epa-passphrase-callback-function)
788 (epg-context-set-progress-callback context
789 (cons
790 #'epa-progress-callback-function
791 (format "Encrypting %s..."
792 (file-name-nondirectory file))))
793 (message "Encrypting %s..." (file-name-nondirectory file))
794 (epg-encrypt-file context file recipients cipher)
795 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
796 (file-name-nondirectory cipher))))
798 ;;;###autoload
799 (defun epa-decrypt-region (start end)
800 "Decrypt the current region between START and END.
802 Don't use this command in Lisp programs!
803 Since this function operates on regions, it does some tricks such
804 as coding-system detection and unibyte/multibyte conversion. If
805 you are sure how the data in the region should be treated, you
806 should consider using the string based counterpart
807 `epg-decrypt-string', or the file based counterpart
808 `epg-decrypt-file' instead.
810 For example:
812 \(let ((context (epg-make-context 'OpenPGP)))
813 (decode-coding-string
814 (epg-decrypt-string context (buffer-substring start end))
815 'utf-8))"
816 (interactive "r")
817 (save-excursion
818 (let ((context (epg-make-context epa-protocol))
819 plain)
820 (epg-context-set-passphrase-callback context
821 #'epa-passphrase-callback-function)
822 (epg-context-set-progress-callback context
823 (cons
824 #'epa-progress-callback-function
825 "Decrypting..."))
826 (message "Decrypting...")
827 (setq plain (epg-decrypt-string context (buffer-substring start end)))
828 (message "Decrypting...done")
829 (setq plain (epa--decode-coding-string
830 plain
831 (or coding-system-for-read
832 (get-text-property start 'epa-coding-system-used)
833 'undecided)))
834 (if (y-or-n-p "Replace the original text? ")
835 (let ((inhibit-read-only t)
836 buffer-read-only)
837 (delete-region start end)
838 (goto-char start)
839 (insert plain))
840 (with-output-to-temp-buffer "*Temp*"
841 (set-buffer standard-output)
842 (insert plain)
843 (epa-info-mode)))
844 (if (epg-context-result-for context 'verify)
845 (epa-display-info (epg-verify-result-to-string
846 (epg-context-result-for context 'verify)))))))
848 (defun epa--find-coding-system-for-mime-charset (mime-charset)
849 (if (featurep 'xemacs)
850 (if (fboundp 'find-coding-system)
851 (find-coding-system mime-charset))
852 (let ((pointer (coding-system-list)))
853 (while (and pointer
854 (eq (coding-system-get (car pointer) 'mime-charset)
855 mime-charset))
856 (setq pointer (cdr pointer)))
857 pointer)))
859 ;;;###autoload
860 (defun epa-decrypt-armor-in-region (start end)
861 "Decrypt OpenPGP armors in the current region between START and END.
863 Don't use this command in Lisp programs!
864 See the reason described in the `epa-decrypt-region' documentation."
865 (interactive "r")
866 (save-excursion
867 (save-restriction
868 (narrow-to-region start end)
869 (goto-char start)
870 (let (armor-start armor-end)
871 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
872 (setq armor-start (match-beginning 0)
873 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
874 nil t))
875 (unless armor-end
876 (error "No armor tail"))
877 (goto-char armor-start)
878 (let ((coding-system-for-read
879 (or coding-system-for-read
880 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
881 (epa--find-coding-system-for-mime-charset
882 (intern (downcase (match-string 1))))))))
883 (goto-char armor-end)
884 (epa-decrypt-region armor-start armor-end)))))))
886 ;;;###autoload
887 (defun epa-verify-region (start end)
888 "Verify the current region between START and END.
890 Don't use this command in Lisp programs!
891 Since this function operates on regions, it does some tricks such
892 as coding-system detection and unibyte/multibyte conversion. If
893 you are sure how the data in the region should be treated, you
894 should consider using the string based counterpart
895 `epg-verify-string', or the file based counterpart
896 `epg-verify-file' instead.
898 For example:
900 \(let ((context (epg-make-context 'OpenPGP)))
901 (decode-coding-string
902 (epg-verify-string context (buffer-substring start end))
903 'utf-8))"
904 (interactive "r")
905 (let ((context (epg-make-context epa-protocol))
906 plain)
907 (epg-context-set-progress-callback context
908 (cons
909 #'epa-progress-callback-function
910 "Verifying..."))
911 (message "Verifying...")
912 (setq plain (epg-verify-string
913 context
914 (epa--encode-coding-string
915 (buffer-substring start end)
916 (or coding-system-for-write
917 (get-text-property start 'epa-coding-system-used)))))
918 (message "Verifying...done")
919 (setq plain (epa--decode-coding-string
920 plain
921 (or coding-system-for-read
922 (get-text-property start 'epa-coding-system-used)
923 'undecided)))
924 (if (y-or-n-p "Replace the original text? ")
925 (let ((inhibit-read-only t)
926 buffer-read-only)
927 (delete-region start end)
928 (goto-char start)
929 (insert plain))
930 (with-output-to-temp-buffer "*Temp*"
931 (set-buffer standard-output)
932 (insert plain)
933 (epa-info-mode)))
934 (if (epg-context-result-for context 'verify)
935 (epa-display-info (epg-verify-result-to-string
936 (epg-context-result-for context 'verify))))))
938 ;;;###autoload
939 (defun epa-verify-cleartext-in-region (start end)
940 "Verify OpenPGP cleartext signed messages in the current region
941 between START and END.
943 Don't use this command in Lisp programs!
944 See the reason described in the `epa-verify-region' documentation."
945 (interactive "r")
946 (save-excursion
947 (save-restriction
948 (narrow-to-region start end)
949 (goto-char start)
950 (let (cleartext-start cleartext-end)
951 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
952 nil t)
953 (setq cleartext-start (match-beginning 0))
954 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
955 nil t)
956 (error "Invalid cleartext signed message"))
957 (setq cleartext-end (re-search-forward
958 "^-----END PGP SIGNATURE-----$"
959 nil t))
960 (unless cleartext-end
961 (error "No cleartext tail"))
962 (epa-verify-region cleartext-start cleartext-end))))))
964 (eval-and-compile
965 (if (fboundp 'select-safe-coding-system)
966 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system)
967 (defun epa--select-safe-coding-system (_from _to)
968 buffer-file-coding-system)))
970 ;;;###autoload
971 (defun epa-sign-region (start end signers mode)
972 "Sign the current region between START and END by SIGNERS keys selected.
974 Don't use this command in Lisp programs!
975 Since this function operates on regions, it does some tricks such
976 as coding-system detection and unibyte/multibyte conversion. If
977 you are sure how the data should be treated, you should consider
978 using the string based counterpart `epg-sign-string', or the file
979 based counterpart `epg-sign-file' instead.
981 For example:
983 \(let ((context (epg-make-context 'OpenPGP)))
984 (epg-sign-string
985 context
986 (encode-coding-string (buffer-substring start end) 'utf-8)))"
987 (interactive
988 (let ((verbose current-prefix-arg))
989 (setq epa-last-coding-system-specified
990 (or coding-system-for-write
991 (epa--select-safe-coding-system
992 (region-beginning) (region-end))))
993 (list (region-beginning) (region-end)
994 (if verbose
995 (epa-select-keys (epg-make-context epa-protocol)
996 "Select keys for signing.
997 If no one is selected, default secret key is used. "
998 nil t))
999 (if verbose
1000 (epa--read-signature-type)
1001 'clear))))
1002 (save-excursion
1003 (let ((context (epg-make-context epa-protocol))
1004 signature)
1005 ;;(epg-context-set-armor context epa-armor)
1006 (epg-context-set-armor context t)
1007 ;;(epg-context-set-textmode context epa-textmode)
1008 (epg-context-set-textmode context t)
1009 (epg-context-set-signers context signers)
1010 (epg-context-set-passphrase-callback context
1011 #'epa-passphrase-callback-function)
1012 (epg-context-set-progress-callback context
1013 (cons
1014 #'epa-progress-callback-function
1015 "Signing..."))
1016 (message "Signing...")
1017 (setq signature (epg-sign-string context
1018 (epa--encode-coding-string
1019 (buffer-substring start end)
1020 epa-last-coding-system-specified)
1021 mode))
1022 (message "Signing...done")
1023 (delete-region start end)
1024 (goto-char start)
1025 (add-text-properties (point)
1026 (progn
1027 (insert (epa--decode-coding-string
1028 signature
1029 (or coding-system-for-read
1030 epa-last-coding-system-specified)))
1031 (point))
1032 (list 'epa-coding-system-used
1033 epa-last-coding-system-specified
1034 'front-sticky nil
1035 'rear-nonsticky t
1036 'start-open t
1037 'end-open t)))))
1039 (eval-and-compile
1040 (if (fboundp 'derived-mode-p)
1041 (defalias 'epa--derived-mode-p 'derived-mode-p)
1042 (defun epa--derived-mode-p (&rest modes)
1043 "Non-nil if the current major mode is derived from one of MODES.
1044 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1045 (let ((parent major-mode))
1046 (while (and (not (memq parent modes))
1047 (setq parent (get parent 'derived-mode-parent))))
1048 parent))))
1050 ;;;###autoload
1051 (defun epa-encrypt-region (start end recipients sign signers)
1052 "Encrypt the current region between START and END for RECIPIENTS.
1054 Don't use this command in Lisp programs!
1055 Since this function operates on regions, it does some tricks such
1056 as coding-system detection and unibyte/multibyte conversion. If
1057 you are sure how the data should be treated, you should consider
1058 using the string based counterpart `epg-encrypt-string', or the
1059 file based counterpart `epg-encrypt-file' instead.
1061 For example:
1063 \(let ((context (epg-make-context 'OpenPGP)))
1064 (epg-encrypt-string
1065 context
1066 (encode-coding-string (buffer-substring start end) 'utf-8)
1067 nil))"
1068 (interactive
1069 (let ((verbose current-prefix-arg)
1070 (context (epg-make-context epa-protocol))
1071 sign)
1072 (setq epa-last-coding-system-specified
1073 (or coding-system-for-write
1074 (epa--select-safe-coding-system
1075 (region-beginning) (region-end))))
1076 (list (region-beginning) (region-end)
1077 (epa-select-keys context
1078 "Select recipients for encryption.
1079 If no one is selected, symmetric encryption will be performed. ")
1080 (setq sign (if verbose (y-or-n-p "Sign? ")))
1081 (if sign
1082 (epa-select-keys context
1083 "Select keys for signing. ")))))
1084 (save-excursion
1085 (let ((context (epg-make-context epa-protocol))
1086 cipher)
1087 ;;(epg-context-set-armor context epa-armor)
1088 (epg-context-set-armor context t)
1089 ;;(epg-context-set-textmode context epa-textmode)
1090 (epg-context-set-textmode context t)
1091 (if sign
1092 (epg-context-set-signers context signers))
1093 (epg-context-set-passphrase-callback context
1094 #'epa-passphrase-callback-function)
1095 (epg-context-set-progress-callback context
1096 (cons
1097 #'epa-progress-callback-function
1098 "Encrypting..."))
1099 (message "Encrypting...")
1100 (setq cipher (epg-encrypt-string context
1101 (epa--encode-coding-string
1102 (buffer-substring start end)
1103 epa-last-coding-system-specified)
1104 recipients
1105 sign))
1106 (message "Encrypting...done")
1107 (delete-region start end)
1108 (goto-char start)
1109 (add-text-properties (point)
1110 (progn
1111 (insert cipher)
1112 (point))
1113 (list 'epa-coding-system-used
1114 epa-last-coding-system-specified
1115 'front-sticky nil
1116 'rear-nonsticky t
1117 'start-open t
1118 'end-open t)))))
1120 ;;;###autoload
1121 (defun epa-delete-keys (keys &optional allow-secret)
1122 "Delete selected KEYS."
1123 (interactive
1124 (let ((keys (epa--marked-keys)))
1125 (unless keys
1126 (error "No keys selected"))
1127 (list keys
1128 (eq (nth 1 epa-list-keys-arguments) t))))
1129 (let ((context (epg-make-context epa-protocol)))
1130 (message "Deleting...")
1131 (epg-delete-keys context keys allow-secret)
1132 (message "Deleting...done")
1133 (apply #'epa--list-keys epa-list-keys-arguments)))
1135 ;;;###autoload
1136 (defun epa-import-keys (file)
1137 "Import keys from FILE."
1138 (interactive "fFile: ")
1139 (setq file (expand-file-name file))
1140 (let ((context (epg-make-context epa-protocol)))
1141 (message "Importing %s..." (file-name-nondirectory file))
1142 (condition-case nil
1143 (progn
1144 (epg-import-keys-from-file context file)
1145 (message "Importing %s...done" (file-name-nondirectory file)))
1146 (error
1147 (message "Importing %s...failed" (file-name-nondirectory file))))
1148 (if (epg-context-result-for context 'import)
1149 (epa-display-info (epg-import-result-to-string
1150 (epg-context-result-for context 'import))))
1151 (if (eq major-mode 'epa-key-list-mode)
1152 (apply #'epa--list-keys epa-list-keys-arguments))))
1154 ;;;###autoload
1155 (defun epa-import-keys-region (start end)
1156 "Import keys from the region."
1157 (interactive "r")
1158 (let ((context (epg-make-context epa-protocol)))
1159 (message "Importing...")
1160 (condition-case nil
1161 (progn
1162 (epg-import-keys-from-string context (buffer-substring start end))
1163 (message "Importing...done"))
1164 (error
1165 (message "Importing...failed")))
1166 (if (epg-context-result-for context 'import)
1167 (epa-display-info (epg-import-result-to-string
1168 (epg-context-result-for context 'import))))))
1170 ;;;###autoload
1171 (defun epa-import-armor-in-region (start end)
1172 "Import keys in the OpenPGP armor format in the current region
1173 between START and END."
1174 (interactive "r")
1175 (save-excursion
1176 (save-restriction
1177 (narrow-to-region start end)
1178 (goto-char start)
1179 (let (armor-start armor-end)
1180 (while (re-search-forward
1181 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1182 nil t)
1183 (setq armor-start (match-beginning 0)
1184 armor-end (re-search-forward
1185 (concat "^-----END " (match-string 1) "-----$")
1186 nil t))
1187 (unless armor-end
1188 (error "No armor tail"))
1189 (epa-import-keys-region armor-start armor-end))))))
1191 ;;;###autoload
1192 (defun epa-export-keys (keys file)
1193 "Export selected KEYS to FILE."
1194 (interactive
1195 (let ((keys (epa--marked-keys))
1196 default-name)
1197 (unless keys
1198 (error "No keys selected"))
1199 (setq default-name
1200 (expand-file-name
1201 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1202 (if epa-armor ".asc" ".gpg"))
1203 default-directory))
1204 (list keys
1205 (expand-file-name
1206 (read-file-name
1207 (concat "To file (default "
1208 (file-name-nondirectory default-name)
1209 ") ")
1210 (file-name-directory default-name)
1211 default-name)))))
1212 (let ((context (epg-make-context epa-protocol)))
1213 (epg-context-set-armor context epa-armor)
1214 (message "Exporting to %s..." (file-name-nondirectory file))
1215 (epg-export-keys-to-file context keys file)
1216 (message "Exporting to %s...done" (file-name-nondirectory file))))
1218 ;;;###autoload
1219 (defun epa-insert-keys (keys)
1220 "Insert selected KEYS after the point."
1221 (interactive
1222 (list (epa-select-keys (epg-make-context epa-protocol)
1223 "Select keys to export. ")))
1224 (let ((context (epg-make-context epa-protocol)))
1225 ;;(epg-context-set-armor context epa-armor)
1226 (epg-context-set-armor context t)
1227 (insert (epg-export-keys-to-string context keys))))
1229 ;; (defun epa-sign-keys (keys &optional local)
1230 ;; "Sign selected KEYS.
1231 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1233 ;; Don't use this command in Lisp programs!"
1234 ;; (interactive
1235 ;; (let ((keys (epa--marked-keys)))
1236 ;; (unless keys
1237 ;; (error "No keys selected"))
1238 ;; (list keys current-prefix-arg)))
1239 ;; (let ((context (epg-make-context epa-protocol)))
1240 ;; (epg-context-set-passphrase-callback context
1241 ;; #'epa-passphrase-callback-function)
1242 ;; (epg-context-set-progress-callback context
1243 ;; (cons
1244 ;; #'epa-progress-callback-function
1245 ;; "Signing keys..."))
1246 ;; (message "Signing keys...")
1247 ;; (epg-sign-keys context keys local)
1248 ;; (message "Signing keys...done")))
1249 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1251 (provide 'epa)
1253 ;;; epa.el ends here