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