Reformulate java imenu-generic-expression.
[emacs.git] / lisp / epa.el
blob852d10b1cf7f6ba40863f2dba61572911439edac
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/>.
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 '((default :weight bold)
54 (((class color) (background dark)) :foreground "PaleTurquoise"))
55 "Face for high validity EPA information."
56 :group 'epa-faces)
58 (defface epa-validity-medium
59 '((default :slant italic)
60 (((class color) (background dark)) :foreground "PaleTurquoise"))
61 "Face for medium validity EPA information."
62 :group 'epa-faces)
64 (defface epa-validity-low
65 '((t :slant italic))
66 "Face used for displaying the low validity."
67 :group 'epa-faces)
69 (defface epa-validity-disabled
70 '((t :slant italic :inverse-video t))
71 "Face used for displaying the disabled validity."
72 :group 'epa-faces)
74 (defface epa-string
75 '((((class color) (background dark))
76 :foreground "lightyellow")
77 (((class color) (background light))
78 :foreground "blue4"))
79 "Face used for displaying the string."
80 :group 'epa-faces)
82 (defface epa-mark
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."
87 :group 'epa-faces)
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."
93 :group 'epa)
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."
99 :group 'epa)
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))
115 :group 'epa)
117 (defvar epa-font-lock-keywords
118 '(("^\\*"
119 (0 'epa-mark))
120 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
121 (1 'epa-field-name)
122 (2 'epa-field-body)))
123 "Default expressions to addon in epa-mode.")
125 (defconst epa-pubkey-algorithm-letter-alist
126 '((1 . ?R)
127 (2 . ?r)
128 (3 . ?s)
129 (16 . ?g)
130 (17 . ?D)
131 (20 . ?G)))
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)
151 (defvar epa-key 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 [?\S-\ ] 'scroll-down-command)
173 (define-key keymap [delete] 'scroll-down-command)
174 (define-key keymap "q" 'epa-exit-buffer)
175 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
176 (define-key menu-map [epa-key-list-unmark-key]
177 '(menu-item "Unmark Key" epa-unmark-key
178 :help "Unmark a key"))
179 (define-key menu-map [epa-key-list-mark-key]
180 '(menu-item "Mark Key" epa-mark-key
181 :help "Mark a key"))
182 (define-key menu-map [separator-epa-file] '(menu-item "--"))
183 (define-key menu-map [epa-verify-file]
184 '(menu-item "Verify File..." epa-verify-file
185 :help "Verify FILE"))
186 (define-key menu-map [epa-sign-file]
187 '(menu-item "Sign File..." epa-sign-file
188 :help "Sign FILE by SIGNERS keys selected"))
189 (define-key menu-map [epa-decrypt-file]
190 '(menu-item "Decrypt File..." epa-decrypt-file
191 :help "Decrypt FILE"))
192 (define-key menu-map [epa-encrypt-file]
193 '(menu-item "Encrypt File..." epa-encrypt-file
194 :help "Encrypt FILE for RECIPIENTS"))
195 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
196 (define-key menu-map [epa-key-list-delete-keys]
197 '(menu-item "Delete Keys" epa-delete-keys
198 :help "Delete Marked Keys"))
199 (define-key menu-map [epa-key-list-import-keys]
200 '(menu-item "Import Keys" epa-import-keys
201 :help "Import keys from a file"))
202 (define-key menu-map [epa-key-list-export-keys]
203 '(menu-item "Export Keys" epa-export-keys
204 :help "Export marked keys to a file"))
205 keymap))
207 (defvar epa-key-mode-map
208 (let ((keymap (make-sparse-keymap)))
209 (define-key keymap "q" 'epa-exit-buffer)
210 keymap))
212 (defvar epa-info-mode-map
213 (let ((keymap (make-sparse-keymap)))
214 (define-key keymap "q" 'delete-window)
215 keymap))
217 (defvar epa-exit-buffer-function #'bury-buffer)
219 (define-widget 'epa-key 'push-button
220 "Button for representing a epg-key object."
221 :format "%[%v%]"
222 :button-face-get 'epa--key-widget-button-face-get
223 :value-create 'epa--key-widget-value-create
224 :action 'epa--key-widget-action
225 :help-echo 'epa--key-widget-help-echo)
227 (defun epa--key-widget-action (widget &optional _event)
228 (save-selected-window
229 (epa--show-key (widget-get widget :value))))
231 (defun epa--key-widget-value-create (widget)
232 (let* ((key (widget-get widget :value))
233 (primary-sub-key (car (epg-key-sub-key-list key)))
234 (primary-user-id (car (epg-key-user-id-list key))))
235 (insert (format "%c "
236 (if (epg-sub-key-validity primary-sub-key)
237 (car (rassq (epg-sub-key-validity primary-sub-key)
238 epg-key-validity-alist))
239 ? ))
240 (epg-sub-key-id primary-sub-key)
242 (if primary-user-id
243 (if (stringp (epg-user-id-string primary-user-id))
244 (epg-user-id-string primary-user-id)
245 (epg-decode-dn (epg-user-id-string primary-user-id)))
246 ""))))
248 (defun epa--key-widget-button-face-get (widget)
249 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
250 (widget-get widget :value))))))
251 (if validity
252 (cdr (assq validity epa-validity-face-alist))
253 'default)))
255 (defun epa--key-widget-help-echo (widget)
256 (format "Show %s"
257 (epg-sub-key-id (car (epg-key-sub-key-list
258 (widget-get widget :value))))))
260 (eval-and-compile
261 (if (fboundp 'encode-coding-string)
262 (defalias 'epa--encode-coding-string 'encode-coding-string)
263 (defalias 'epa--encode-coding-string 'identity)))
265 (eval-and-compile
266 (if (fboundp 'decode-coding-string)
267 (defalias 'epa--decode-coding-string 'decode-coding-string)
268 (defalias 'epa--decode-coding-string 'identity)))
270 (defun epa-key-list-mode ()
271 "Major mode for `epa-list-keys'."
272 (kill-all-local-variables)
273 (buffer-disable-undo)
274 (setq major-mode 'epa-key-list-mode
275 mode-name "Keys"
276 truncate-lines t
277 buffer-read-only t)
278 (use-local-map epa-key-list-mode-map)
279 (make-local-variable 'font-lock-defaults)
280 (setq font-lock-defaults '(epa-font-lock-keywords t))
281 ;; In XEmacs, auto-initialization of font-lock is not effective
282 ;; if buffer-file-name is not set.
283 (font-lock-set-defaults)
284 (make-local-variable 'epa-exit-buffer-function)
285 (make-local-variable 'revert-buffer-function)
286 (setq revert-buffer-function 'epa--key-list-revert-buffer)
287 (run-mode-hooks 'epa-key-list-mode-hook))
289 (defun epa-key-mode ()
290 "Major mode for a key description."
291 (kill-all-local-variables)
292 (buffer-disable-undo)
293 (setq major-mode 'epa-key-mode
294 mode-name "Key"
295 truncate-lines t
296 buffer-read-only t)
297 (use-local-map epa-key-mode-map)
298 (make-local-variable 'font-lock-defaults)
299 (setq font-lock-defaults '(epa-font-lock-keywords t))
300 ;; In XEmacs, auto-initialization of font-lock is not effective
301 ;; if buffer-file-name is not set.
302 (font-lock-set-defaults)
303 (make-local-variable 'epa-exit-buffer-function)
304 (run-mode-hooks 'epa-key-mode-hook))
306 (defun epa-info-mode ()
307 "Major mode for `epa-info-buffer'."
308 (kill-all-local-variables)
309 (buffer-disable-undo)
310 (setq major-mode 'epa-info-mode
311 mode-name "Info"
312 truncate-lines t
313 buffer-read-only t)
314 (use-local-map epa-info-mode-map)
315 (run-mode-hooks 'epa-info-mode-hook))
317 (defun epa-mark-key (&optional arg)
318 "Mark a key on the current line.
319 If ARG is non-nil, unmark the key."
320 (interactive "P")
321 (let ((inhibit-read-only t)
322 buffer-read-only
323 properties)
324 (beginning-of-line)
325 (unless (get-text-property (point) 'epa-key)
326 (error "No key on this line"))
327 (setq properties (text-properties-at (point)))
328 (delete-char 1)
329 (insert (if arg " " "*"))
330 (set-text-properties (1- (point)) (point) properties)
331 (forward-line)))
333 (defun epa-unmark-key (&optional arg)
334 "Unmark a key on the current line.
335 If ARG is non-nil, mark the key."
336 (interactive "P")
337 (epa-mark-key (not arg)))
339 (defun epa-exit-buffer ()
340 "Exit the current buffer.
341 `epa-exit-buffer-function' is called if it is set."
342 (interactive)
343 (funcall epa-exit-buffer-function))
345 (defun epa--insert-keys (keys)
346 (save-excursion
347 (save-restriction
348 (narrow-to-region (point) (point))
349 (let (point)
350 (while keys
351 (setq point (point))
352 (insert " ")
353 (add-text-properties point (point)
354 (list 'epa-key (car keys)
355 'front-sticky nil
356 'rear-nonsticky t
357 'start-open t
358 'end-open t))
359 (widget-create 'epa-key :value (car keys))
360 (insert "\n")
361 (setq keys (cdr keys))))
362 (add-text-properties (point-min) (point-max)
363 (list 'epa-list-keys t
364 'front-sticky nil
365 'rear-nonsticky t
366 'start-open t
367 'end-open t)))))
369 (defun epa--list-keys (name secret)
370 (unless (and epa-keys-buffer
371 (buffer-live-p epa-keys-buffer))
372 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
373 (set-buffer epa-keys-buffer)
374 (epa-key-list-mode)
375 (let ((inhibit-read-only t)
376 buffer-read-only
377 (point (point-min))
378 (context (epg-make-context epa-protocol)))
379 (unless (get-text-property point 'epa-list-keys)
380 (setq point (next-single-property-change point 'epa-list-keys)))
381 (when point
382 (delete-region point
383 (or (next-single-property-change point 'epa-list-keys)
384 (point-max)))
385 (goto-char point))
386 (epa--insert-keys (epg-list-keys context name secret))
387 (widget-setup)
388 (set-keymap-parent (current-local-map) widget-keymap))
389 (make-local-variable 'epa-list-keys-arguments)
390 (setq epa-list-keys-arguments (list name secret))
391 (goto-char (point-min))
392 (pop-to-buffer (current-buffer)))
394 ;;;###autoload
395 (defun epa-list-keys (&optional name)
396 "List all keys matched with NAME from the public keyring."
397 (interactive
398 (if current-prefix-arg
399 (let ((name (read-string "Pattern: "
400 (if epa-list-keys-arguments
401 (car epa-list-keys-arguments)))))
402 (list (if (equal name "") nil name)))
403 (list nil)))
404 (epa--list-keys name nil))
406 ;;;###autoload
407 (defun epa-list-secret-keys (&optional name)
408 "List all keys matched with NAME from the private keyring."
409 (interactive
410 (if current-prefix-arg
411 (let ((name (read-string "Pattern: "
412 (if epa-list-keys-arguments
413 (car epa-list-keys-arguments)))))
414 (list (if (equal name "") nil name)))
415 (list nil)))
416 (epa--list-keys name t))
418 (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm)
419 (apply #'epa--list-keys epa-list-keys-arguments))
421 (defun epa--marked-keys ()
422 (or (with-current-buffer epa-keys-buffer
423 (goto-char (point-min))
424 (let (keys key)
425 (while (re-search-forward "^\\*" nil t)
426 (if (setq key (get-text-property (match-beginning 0)
427 'epa-key))
428 (setq keys (cons key keys))))
429 (nreverse keys)))
430 (let ((key (get-text-property (point-at-bol) 'epa-key)))
431 (if key
432 (list key)))))
434 (defun epa--select-keys (prompt keys)
435 (unless (and epa-keys-buffer
436 (buffer-live-p epa-keys-buffer))
437 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
438 (with-current-buffer epa-keys-buffer
439 (epa-key-list-mode)
440 ;; C-c C-c is the usual way to finish the selection (bug#11159).
441 (define-key (current-local-map) "\C-c\C-c" 'exit-recursive-edit)
442 (let ((inhibit-read-only t)
443 buffer-read-only)
444 (erase-buffer)
445 (insert prompt "\n"
446 (substitute-command-keys "\
447 - `\\[epa-mark-key]' to mark a key on the line
448 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
449 (widget-create 'link
450 :notify (lambda (&rest _ignore) (abort-recursive-edit))
451 :help-echo
452 (substitute-command-keys
453 "Click here or \\[abort-recursive-edit] to cancel")
454 "Cancel")
455 (widget-create 'link
456 :notify (lambda (&rest _ignore) (exit-recursive-edit))
457 :help-echo
458 (substitute-command-keys
459 "Click here or \\[exit-recursive-edit] to finish")
460 "OK")
461 (insert "\n\n")
462 (epa--insert-keys keys)
463 (widget-setup)
464 (set-keymap-parent (current-local-map) widget-keymap)
465 (setq epa-exit-buffer-function #'abort-recursive-edit)
466 (goto-char (point-min))
467 (let ((display-buffer-mark-dedicated 'soft))
468 (pop-to-buffer (current-buffer))))
469 (unwind-protect
470 (progn
471 (recursive-edit)
472 (epa--marked-keys))
473 (kill-buffer epa-keys-buffer))))
475 ;;;###autoload
476 (defun epa-select-keys (context prompt &optional names secret)
477 "Display a user's keyring and ask him to select keys.
478 CONTEXT is an epg-context.
479 PROMPT is a string to prompt with.
480 NAMES is a list of strings to be matched with keys. If it is nil, all
481 the keys are listed.
482 If SECRET is non-nil, list secret keys instead of public keys."
483 (let ((keys (epg-list-keys context names secret)))
484 (epa--select-keys prompt keys)))
486 (defun epa--show-key (key)
487 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
488 (entry (assoc (epg-sub-key-id primary-sub-key)
489 epa-key-buffer-alist))
490 (inhibit-read-only t)
491 buffer-read-only
492 pointer)
493 (unless entry
494 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
495 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
496 (unless (and (cdr entry)
497 (buffer-live-p (cdr entry)))
498 (setcdr entry (generate-new-buffer
499 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
500 (set-buffer (cdr entry))
501 (epa-key-mode)
502 (make-local-variable 'epa-key)
503 (setq epa-key key)
504 (erase-buffer)
505 (setq pointer (epg-key-user-id-list key))
506 (while pointer
507 (if (car pointer)
508 (insert " "
509 (if (epg-user-id-validity (car pointer))
510 (char-to-string
511 (car (rassq (epg-user-id-validity (car pointer))
512 epg-key-validity-alist)))
513 " ")
515 (if (stringp (epg-user-id-string (car pointer)))
516 (epg-user-id-string (car pointer))
517 (epg-decode-dn (epg-user-id-string (car pointer))))
518 "\n"))
519 (setq pointer (cdr pointer)))
520 (setq pointer (epg-key-sub-key-list key))
521 (while pointer
522 (insert " "
523 (if (epg-sub-key-validity (car pointer))
524 (char-to-string
525 (car (rassq (epg-sub-key-validity (car pointer))
526 epg-key-validity-alist)))
527 " ")
529 (epg-sub-key-id (car pointer))
531 (format "%dbits"
532 (epg-sub-key-length (car pointer)))
534 (cdr (assq (epg-sub-key-algorithm (car pointer))
535 epg-pubkey-algorithm-alist))
536 "\n\tCreated: "
537 (condition-case nil
538 (format-time-string "%Y-%m-%d"
539 (epg-sub-key-creation-time (car pointer)))
540 (error "????-??-??"))
541 (if (epg-sub-key-expiration-time (car pointer))
542 (format (if (time-less-p (current-time)
543 (epg-sub-key-expiration-time
544 (car pointer)))
545 "\n\tExpires: %s"
546 "\n\tExpired: %s")
547 (condition-case nil
548 (format-time-string "%Y-%m-%d"
549 (epg-sub-key-expiration-time
550 (car pointer)))
551 (error "????-??-??")))
553 "\n\tCapabilities: "
554 (mapconcat #'symbol-name
555 (epg-sub-key-capability (car pointer))
556 " ")
557 "\n\tFingerprint: "
558 (epg-sub-key-fingerprint (car pointer))
559 "\n")
560 (setq pointer (cdr pointer)))
561 (goto-char (point-min))
562 (pop-to-buffer (current-buffer))))
564 (defun epa-display-info (info)
565 (if epa-popup-info-window
566 (save-selected-window
567 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
568 (setq epa-info-buffer (generate-new-buffer "*Info*")))
569 (if (get-buffer-window epa-info-buffer)
570 (delete-window (get-buffer-window epa-info-buffer)))
571 (with-current-buffer epa-info-buffer
572 (let ((inhibit-read-only t)
573 buffer-read-only)
574 (erase-buffer)
575 (insert info))
576 (epa-info-mode)
577 (goto-char (point-min)))
578 (if (> (window-height)
579 epa-info-window-height)
580 (set-window-buffer (split-window nil (- (window-height)
581 epa-info-window-height))
582 epa-info-buffer)
583 (pop-to-buffer epa-info-buffer)
584 (if (> (window-height) epa-info-window-height)
585 (shrink-window (- (window-height) epa-info-window-height)))))
586 (message "%s" info)))
588 (defun epa-display-verify-result (verify-result)
589 (declare (obsolete epa-display-info "23.1"))
590 (epa-display-info (epg-verify-result-to-string verify-result)))
592 (defun epa-passphrase-callback-function (context key-id handback)
593 (if (eq key-id 'SYM)
594 (read-passwd
595 (format "Passphrase for symmetric encryption%s: "
596 ;; Add the file name to the prompt, if any.
597 (if (stringp handback)
598 (format " for %s" handback)
599 ""))
600 (eq (epg-context-operation context) 'encrypt))
601 (read-passwd
602 (if (eq key-id 'PIN)
603 "Passphrase for PIN: "
604 (let ((entry (assoc key-id epg-user-id-alist)))
605 (if entry
606 (format "Passphrase for %s %s: " key-id (cdr entry))
607 (format "Passphrase for %s: " key-id)))))))
609 (defun epa-progress-callback-function (_context what _char current total
610 handback)
611 (let ((prompt (or handback
612 (format "Processing %s: " what))))
613 ;; According to gnupg/doc/DETAIL: a "total" of 0 indicates that
614 ;; the total amount is not known. The condition TOTAL && CUR ==
615 ;; TOTAL may be used to detect the end of an operation.
616 (if (> total 0)
617 (if (= current total)
618 (message "%s...done" prompt)
619 (message "%s...%d%%" prompt
620 (floor (* (/ current (float total)) 100))))
621 (message "%s..." prompt))))
623 ;;;###autoload
624 (defun epa-decrypt-file (file)
625 "Decrypt FILE."
626 (interactive "fFile: ")
627 (setq file (expand-file-name file))
628 (let* ((default-name (file-name-sans-extension file))
629 (plain (expand-file-name
630 (read-file-name
631 (concat "To file (default "
632 (file-name-nondirectory default-name)
633 ") ")
634 (file-name-directory default-name)
635 default-name)))
636 (context (epg-make-context epa-protocol)))
637 (epg-context-set-passphrase-callback context
638 #'epa-passphrase-callback-function)
639 (epg-context-set-progress-callback context
640 (cons
641 #'epa-progress-callback-function
642 (format "Decrypting %s..."
643 (file-name-nondirectory file))))
644 (message "Decrypting %s..." (file-name-nondirectory file))
645 (epg-decrypt-file context file plain)
646 (message "Decrypting %s...wrote %s" (file-name-nondirectory file)
647 (file-name-nondirectory plain))
648 (if (epg-context-result-for context 'verify)
649 (epa-display-info (epg-verify-result-to-string
650 (epg-context-result-for context 'verify))))))
652 ;;;###autoload
653 (defun epa-verify-file (file)
654 "Verify FILE."
655 (interactive "fFile: ")
656 (setq file (expand-file-name file))
657 (let* ((context (epg-make-context epa-protocol))
658 (plain (if (equal (file-name-extension file) "sig")
659 (file-name-sans-extension file))))
660 (epg-context-set-progress-callback context
661 (cons
662 #'epa-progress-callback-function
663 (format "Verifying %s..."
664 (file-name-nondirectory file))))
665 (message "Verifying %s..." (file-name-nondirectory file))
666 (epg-verify-file context file plain)
667 (message "Verifying %s...done" (file-name-nondirectory file))
668 (if (epg-context-result-for context 'verify)
669 (epa-display-info (epg-verify-result-to-string
670 (epg-context-result-for context 'verify))))))
672 (defun epa--read-signature-type ()
673 (let (type c)
674 (while (null type)
675 (message "Signature type (n,c,d,?) ")
676 (setq c (read-char))
677 (cond ((eq c ?c)
678 (setq type 'clear))
679 ((eq c ?d)
680 (setq type 'detached))
681 ((eq c ??)
682 (with-output-to-temp-buffer "*Help*"
683 (with-current-buffer standard-output
684 (insert "\
685 n - Create a normal signature
686 c - Create a cleartext signature
687 d - Create a detached signature
688 ? - Show this help
689 "))))
691 (setq type 'normal))))
692 type))
694 ;;;###autoload
695 (defun epa-sign-file (file signers mode)
696 "Sign FILE by SIGNERS keys selected."
697 (interactive
698 (let ((verbose current-prefix-arg))
699 (list (expand-file-name (read-file-name "File: "))
700 (if verbose
701 (epa-select-keys (epg-make-context epa-protocol)
702 "Select keys for signing.
703 If no one is selected, default secret key is used. "
704 nil t))
705 (if verbose
706 (epa--read-signature-type)
707 'clear))))
708 (let ((signature (concat file
709 (if (eq epa-protocol 'OpenPGP)
710 (if (or epa-armor
711 (not (memq mode
712 '(nil t normal detached))))
713 ".asc"
714 (if (memq mode '(t detached))
715 ".sig"
716 ".gpg"))
717 (if (memq mode '(t detached))
718 ".p7s"
719 ".p7m"))))
720 (context (epg-make-context epa-protocol)))
721 (epg-context-set-armor context epa-armor)
722 (epg-context-set-textmode context epa-textmode)
723 (epg-context-set-signers context signers)
724 (epg-context-set-passphrase-callback context
725 #'epa-passphrase-callback-function)
726 (epg-context-set-progress-callback context
727 (cons
728 #'epa-progress-callback-function
729 (format "Signing %s..."
730 (file-name-nondirectory file))))
731 (message "Signing %s..." (file-name-nondirectory file))
732 (epg-sign-file context file signature mode)
733 (message "Signing %s...wrote %s" (file-name-nondirectory file)
734 (file-name-nondirectory signature))))
736 ;;;###autoload
737 (defun epa-encrypt-file (file recipients)
738 "Encrypt FILE for RECIPIENTS."
739 (interactive
740 (list (expand-file-name (read-file-name "File: "))
741 (epa-select-keys (epg-make-context epa-protocol)
742 "Select recipients for encryption.
743 If no one is selected, symmetric encryption will be performed. ")))
744 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
745 (if epa-armor ".asc" ".gpg")
746 ".p7m")))
747 (context (epg-make-context epa-protocol)))
748 (epg-context-set-armor context epa-armor)
749 (epg-context-set-textmode context epa-textmode)
750 (epg-context-set-passphrase-callback context
751 #'epa-passphrase-callback-function)
752 (epg-context-set-progress-callback context
753 (cons
754 #'epa-progress-callback-function
755 (format "Encrypting %s..."
756 (file-name-nondirectory file))))
757 (message "Encrypting %s..." (file-name-nondirectory file))
758 (epg-encrypt-file context file recipients cipher)
759 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
760 (file-name-nondirectory cipher))))
762 ;;;###autoload
763 (defun epa-decrypt-region (start end &optional make-buffer-function)
764 "Decrypt the current region between START and END.
766 If MAKE-BUFFER-FUNCTION is non-nil, call it to prepare an output buffer.
767 It should return that buffer. If it copies the input, it should
768 delete the text now being decrypted. It should leave point at the
769 proper place to insert the plaintext.
771 Be careful about using this command in Lisp programs!
772 Since this function operates on regions, it does some tricks such
773 as coding-system detection and unibyte/multibyte conversion. If
774 you are sure how the data in the region should be treated, you
775 should consider using the string based counterpart
776 `epg-decrypt-string', or the file based counterpart
777 `epg-decrypt-file' instead.
779 For example:
781 \(let ((context (epg-make-context 'OpenPGP)))
782 (decode-coding-string
783 (epg-decrypt-string context (buffer-substring start end))
784 'utf-8))"
785 (interactive "r")
786 (save-excursion
787 (let ((context (epg-make-context epa-protocol))
788 plain)
789 (epg-context-set-passphrase-callback context
790 #'epa-passphrase-callback-function)
791 (epg-context-set-progress-callback context
792 (cons
793 #'epa-progress-callback-function
794 "Decrypting..."))
795 (message "Decrypting...")
796 (setq plain (epg-decrypt-string context (buffer-substring start end)))
797 (message "Decrypting...done")
798 (setq plain (epa--decode-coding-string
799 plain
800 (or coding-system-for-read
801 (get-text-property start 'epa-coding-system-used)
802 'undecided)))
803 (if make-buffer-function
804 (with-current-buffer (funcall make-buffer-function)
805 (let ((inhibit-read-only t))
806 (insert plain)))
807 (if (y-or-n-p "Replace the original text? ")
808 (let ((inhibit-read-only t))
809 (delete-region start end)
810 (goto-char start)
811 (insert plain))
812 (with-output-to-temp-buffer "*Temp*"
813 (set-buffer standard-output)
814 (insert plain)
815 (epa-info-mode))))
816 (if (epg-context-result-for context 'verify)
817 (epa-display-info (epg-verify-result-to-string
818 (epg-context-result-for context 'verify)))))))
820 (defun epa--find-coding-system-for-mime-charset (mime-charset)
821 (if (featurep 'xemacs)
822 (if (fboundp 'find-coding-system)
823 (find-coding-system mime-charset))
824 ;; Find the first coding system which corresponds to MIME-CHARSET.
825 (let ((pointer (coding-system-list)))
826 (while (and pointer
827 (not (eq (coding-system-get (car pointer) 'mime-charset)
828 mime-charset)))
829 (setq pointer (cdr pointer)))
830 (car pointer))))
832 ;;;###autoload
833 (defun epa-decrypt-armor-in-region (start end)
834 "Decrypt OpenPGP armors in the current region between START and END.
836 Don't use this command in Lisp programs!
837 See the reason described in the `epa-decrypt-region' documentation."
838 (interactive "r")
839 (save-excursion
840 (save-restriction
841 (narrow-to-region start end)
842 (goto-char start)
843 (let (armor-start armor-end)
844 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
845 (setq armor-start (match-beginning 0)
846 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
847 nil t))
848 (unless armor-end
849 (error "Encryption armor beginning has no matching end"))
850 (goto-char armor-start)
851 (let ((coding-system-for-read
852 (or coding-system-for-read
853 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
854 (epa--find-coding-system-for-mime-charset
855 (intern (downcase (match-string 1))))))))
856 (goto-char armor-end)
857 (epa-decrypt-region armor-start armor-end)))))))
859 ;;;###autoload
860 (defun epa-verify-region (start end)
861 "Verify the current region between START and END.
863 Don't use this command in Lisp programs!
864 Since this function operates on regions, it does some tricks such
865 as coding-system detection and unibyte/multibyte conversion. If
866 you are sure how the data in the region should be treated, you
867 should consider using the string based counterpart
868 `epg-verify-string', or the file based counterpart
869 `epg-verify-file' instead.
871 For example:
873 \(let ((context (epg-make-context 'OpenPGP)))
874 (decode-coding-string
875 (epg-verify-string context (buffer-substring start end))
876 'utf-8))"
877 (interactive "r")
878 (let ((context (epg-make-context epa-protocol))
879 plain)
880 (epg-context-set-progress-callback context
881 (cons
882 #'epa-progress-callback-function
883 "Verifying..."))
884 (message "Verifying...")
885 (setq plain (epg-verify-string
886 context
887 (epa--encode-coding-string
888 (buffer-substring start end)
889 (or coding-system-for-write
890 (get-text-property start 'epa-coding-system-used)))))
891 (message "Verifying...done")
892 (setq plain (epa--decode-coding-string
893 plain
894 (or coding-system-for-read
895 (get-text-property start 'epa-coding-system-used)
896 'undecided)))
897 (if (y-or-n-p "Replace the original text? ")
898 (let ((inhibit-read-only t)
899 buffer-read-only)
900 (delete-region start end)
901 (goto-char start)
902 (insert plain))
903 (with-output-to-temp-buffer "*Temp*"
904 (set-buffer standard-output)
905 (insert plain)
906 (epa-info-mode)))
907 (if (epg-context-result-for context 'verify)
908 (epa-display-info (epg-verify-result-to-string
909 (epg-context-result-for context 'verify))))))
911 ;;;###autoload
912 (defun epa-verify-cleartext-in-region (start end)
913 "Verify OpenPGP cleartext signed messages in the current region
914 between START and END.
916 Don't use this command in Lisp programs!
917 See the reason described in the `epa-verify-region' documentation."
918 (interactive "r")
919 (save-excursion
920 (save-restriction
921 (narrow-to-region start end)
922 (goto-char start)
923 (let (cleartext-start cleartext-end)
924 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
925 nil t)
926 (setq cleartext-start (match-beginning 0))
927 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
928 nil t)
929 (error "Invalid cleartext signed message"))
930 (setq cleartext-end (re-search-forward
931 "^-----END PGP SIGNATURE-----$"
932 nil t))
933 (unless cleartext-end
934 (error "No cleartext tail"))
935 (epa-verify-region cleartext-start cleartext-end))))))
937 (eval-and-compile
938 (if (fboundp 'select-safe-coding-system)
939 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system)
940 (defun epa--select-safe-coding-system (_from _to)
941 buffer-file-coding-system)))
943 ;;;###autoload
944 (defun epa-sign-region (start end signers mode)
945 "Sign the current region between START and END by SIGNERS keys selected.
947 Don't use this command in Lisp programs!
948 Since this function operates on regions, it does some tricks such
949 as coding-system detection and unibyte/multibyte conversion. If
950 you are sure how the data should be treated, you should consider
951 using the string based counterpart `epg-sign-string', or the file
952 based counterpart `epg-sign-file' instead.
954 For example:
956 \(let ((context (epg-make-context 'OpenPGP)))
957 (epg-sign-string
958 context
959 (encode-coding-string (buffer-substring start end) 'utf-8)))"
960 (interactive
961 (let ((verbose current-prefix-arg))
962 (setq epa-last-coding-system-specified
963 (or coding-system-for-write
964 (epa--select-safe-coding-system
965 (region-beginning) (region-end))))
966 (list (region-beginning) (region-end)
967 (if verbose
968 (epa-select-keys (epg-make-context epa-protocol)
969 "Select keys for signing.
970 If no one is selected, default secret key is used. "
971 nil t))
972 (if verbose
973 (epa--read-signature-type)
974 'clear))))
975 (save-excursion
976 (let ((context (epg-make-context epa-protocol))
977 signature)
978 ;;(epg-context-set-armor context epa-armor)
979 (epg-context-set-armor context t)
980 ;;(epg-context-set-textmode context epa-textmode)
981 (epg-context-set-textmode context t)
982 (epg-context-set-signers context signers)
983 (epg-context-set-passphrase-callback context
984 #'epa-passphrase-callback-function)
985 (epg-context-set-progress-callback context
986 (cons
987 #'epa-progress-callback-function
988 "Signing..."))
989 (message "Signing...")
990 (setq signature (epg-sign-string context
991 (epa--encode-coding-string
992 (buffer-substring start end)
993 epa-last-coding-system-specified)
994 mode))
995 (message "Signing...done")
996 (delete-region start end)
997 (goto-char start)
998 (add-text-properties (point)
999 (progn
1000 (insert (epa--decode-coding-string
1001 signature
1002 (or coding-system-for-read
1003 epa-last-coding-system-specified)))
1004 (point))
1005 (list 'epa-coding-system-used
1006 epa-last-coding-system-specified
1007 'front-sticky nil
1008 'rear-nonsticky t
1009 'start-open t
1010 'end-open t)))))
1012 (eval-and-compile
1013 (if (fboundp 'derived-mode-p)
1014 (defalias 'epa--derived-mode-p 'derived-mode-p)
1015 (defun epa--derived-mode-p (&rest modes)
1016 "Non-nil if the current major mode is derived from one of MODES.
1017 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1018 (let ((parent major-mode))
1019 (while (and (not (memq parent modes))
1020 (setq parent (get parent 'derived-mode-parent))))
1021 parent))))
1023 ;;;###autoload
1024 (defun epa-encrypt-region (start end recipients sign signers)
1025 "Encrypt the current region between START and END for RECIPIENTS.
1027 Don't use this command in Lisp programs!
1028 Since this function operates on regions, it does some tricks such
1029 as coding-system detection and unibyte/multibyte conversion. If
1030 you are sure how the data should be treated, you should consider
1031 using the string based counterpart `epg-encrypt-string', or the
1032 file based counterpart `epg-encrypt-file' instead.
1034 For example:
1036 \(let ((context (epg-make-context 'OpenPGP)))
1037 (epg-encrypt-string
1038 context
1039 (encode-coding-string (buffer-substring start end) 'utf-8)
1040 nil))"
1041 (interactive
1042 (let ((verbose current-prefix-arg)
1043 (context (epg-make-context epa-protocol))
1044 sign)
1045 (setq epa-last-coding-system-specified
1046 (or coding-system-for-write
1047 (epa--select-safe-coding-system
1048 (region-beginning) (region-end))))
1049 (list (region-beginning) (region-end)
1050 (epa-select-keys context
1051 "Select recipients for encryption.
1052 If no one is selected, symmetric encryption will be performed. ")
1053 (setq sign (if verbose (y-or-n-p "Sign? ")))
1054 (if sign
1055 (epa-select-keys context
1056 "Select keys for signing. ")))))
1057 (save-excursion
1058 (let ((context (epg-make-context epa-protocol))
1059 cipher)
1060 ;;(epg-context-set-armor context epa-armor)
1061 (epg-context-set-armor context t)
1062 ;;(epg-context-set-textmode context epa-textmode)
1063 (epg-context-set-textmode context t)
1064 (if sign
1065 (epg-context-set-signers context signers))
1066 (epg-context-set-passphrase-callback context
1067 #'epa-passphrase-callback-function)
1068 (epg-context-set-progress-callback context
1069 (cons
1070 #'epa-progress-callback-function
1071 "Encrypting..."))
1072 (message "Encrypting...")
1073 (setq cipher (epg-encrypt-string context
1074 (epa--encode-coding-string
1075 (buffer-substring start end)
1076 epa-last-coding-system-specified)
1077 recipients
1078 sign))
1079 (message "Encrypting...done")
1080 (delete-region start end)
1081 (goto-char start)
1082 (add-text-properties (point)
1083 (progn
1084 (insert cipher)
1085 (point))
1086 (list 'epa-coding-system-used
1087 epa-last-coding-system-specified
1088 'front-sticky nil
1089 'rear-nonsticky t
1090 'start-open t
1091 'end-open t)))))
1093 ;;;###autoload
1094 (defun epa-delete-keys (keys &optional allow-secret)
1095 "Delete selected KEYS."
1096 (interactive
1097 (let ((keys (epa--marked-keys)))
1098 (unless keys
1099 (error "No keys selected"))
1100 (list keys
1101 (eq (nth 1 epa-list-keys-arguments) t))))
1102 (let ((context (epg-make-context epa-protocol)))
1103 (message "Deleting...")
1104 (epg-delete-keys context keys allow-secret)
1105 (message "Deleting...done")
1106 (apply #'epa--list-keys epa-list-keys-arguments)))
1108 ;;;###autoload
1109 (defun epa-import-keys (file)
1110 "Import keys from FILE."
1111 (interactive "fFile: ")
1112 (setq file (expand-file-name file))
1113 (let ((context (epg-make-context epa-protocol)))
1114 (message "Importing %s..." (file-name-nondirectory file))
1115 (condition-case nil
1116 (progn
1117 (epg-import-keys-from-file context file)
1118 (message "Importing %s...done" (file-name-nondirectory file)))
1119 (error
1120 (message "Importing %s...failed" (file-name-nondirectory file))))
1121 (if (epg-context-result-for context 'import)
1122 (epa-display-info (epg-import-result-to-string
1123 (epg-context-result-for context 'import))))
1124 (if (eq major-mode 'epa-key-list-mode)
1125 (apply #'epa--list-keys epa-list-keys-arguments))))
1127 ;;;###autoload
1128 (defun epa-import-keys-region (start end)
1129 "Import keys from the region."
1130 (interactive "r")
1131 (let ((context (epg-make-context epa-protocol)))
1132 (message "Importing...")
1133 (condition-case nil
1134 (progn
1135 (epg-import-keys-from-string context (buffer-substring start end))
1136 (message "Importing...done"))
1137 (error
1138 (message "Importing...failed")))
1139 (if (epg-context-result-for context 'import)
1140 (epa-display-info (epg-import-result-to-string
1141 (epg-context-result-for context 'import))))))
1143 ;;;###autoload
1144 (defun epa-import-armor-in-region (start end)
1145 "Import keys in the OpenPGP armor format in the current region
1146 between START and END."
1147 (interactive "r")
1148 (save-excursion
1149 (save-restriction
1150 (narrow-to-region start end)
1151 (goto-char start)
1152 (let (armor-start armor-end)
1153 (while (re-search-forward
1154 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1155 nil t)
1156 (setq armor-start (match-beginning 0)
1157 armor-end (re-search-forward
1158 (concat "^-----END " (match-string 1) "-----$")
1159 nil t))
1160 (unless armor-end
1161 (error "No armor tail"))
1162 (epa-import-keys-region armor-start armor-end))))))
1164 ;;;###autoload
1165 (defun epa-export-keys (keys file)
1166 "Export selected KEYS to FILE."
1167 (interactive
1168 (let ((keys (epa--marked-keys))
1169 default-name)
1170 (unless keys
1171 (error "No keys selected"))
1172 (setq default-name
1173 (expand-file-name
1174 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1175 (if epa-armor ".asc" ".gpg"))
1176 default-directory))
1177 (list keys
1178 (expand-file-name
1179 (read-file-name
1180 (concat "To file (default "
1181 (file-name-nondirectory default-name)
1182 ") ")
1183 (file-name-directory default-name)
1184 default-name)))))
1185 (let ((context (epg-make-context epa-protocol)))
1186 (epg-context-set-armor context epa-armor)
1187 (message "Exporting to %s..." (file-name-nondirectory file))
1188 (epg-export-keys-to-file context keys file)
1189 (message "Exporting to %s...done" (file-name-nondirectory file))))
1191 ;;;###autoload
1192 (defun epa-insert-keys (keys)
1193 "Insert selected KEYS after the point."
1194 (interactive
1195 (list (epa-select-keys (epg-make-context epa-protocol)
1196 "Select keys to export.
1197 If no one is selected, default public key is exported. ")))
1198 (let ((context (epg-make-context epa-protocol)))
1199 ;;(epg-context-set-armor context epa-armor)
1200 (epg-context-set-armor context t)
1201 (insert (epg-export-keys-to-string context keys))))
1203 ;; (defun epa-sign-keys (keys &optional local)
1204 ;; "Sign selected KEYS.
1205 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1207 ;; Don't use this command in Lisp programs!"
1208 ;; (interactive
1209 ;; (let ((keys (epa--marked-keys)))
1210 ;; (unless keys
1211 ;; (error "No keys selected"))
1212 ;; (list keys current-prefix-arg)))
1213 ;; (let ((context (epg-make-context epa-protocol)))
1214 ;; (epg-context-set-passphrase-callback context
1215 ;; #'epa-passphrase-callback-function)
1216 ;; (epg-context-set-progress-callback context
1217 ;; (cons
1218 ;; #'epa-progress-callback-function
1219 ;; "Signing keys..."))
1220 ;; (message "Signing keys...")
1221 ;; (epg-sign-keys context keys local)
1222 ;; (message "Signing keys...done")))
1223 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1225 (provide 'epa)
1227 ;;; epa.el ends here