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