1 ;;; epa.el --- the EasyPG Assistant
2 ;; Copyright (C) 2006 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
7 ;; This file is part of EasyPG.
9 ;; This program 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 2, or (at your option)
14 ;; This program 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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
29 (eval-when-compile (require 'wid-edit))
32 "The EasyPG Assistant"
35 (defgroup epa-faces nil
39 (defface epa-validity-high-face
40 '((((class color) (background dark))
41 (:foreground "PaleTurquoise" :bold t))
44 "Face used for displaying the high validity."
46 (defvar epa-validity-high-face 'epa-validity-high-face)
48 (defface epa-validity-medium-face
49 '((((class color) (background dark))
50 (:foreground "PaleTurquoise" :italic t))
53 "Face used for displaying the medium validity."
55 (defvar epa-validity-medium-face 'epa-validity-medium-face)
57 (defface epa-validity-low-face
60 "Face used for displaying the low validity."
62 (defvar epa-validity-low-face 'epa-validity-low-face)
64 (defface epa-validity-disabled-face
66 (:italic t :inverse-video t)))
67 "Face used for displaying the disabled validity."
69 (defvar epa-validity-disabled-face 'epa-validity-disabled-face)
71 (defface epa-string-face
74 (:foreground "lightyellow"))
77 (:foreground "blue4"))
80 "Face used for displaying the string."
82 (defvar epa-string-face 'epa-string-face)
84 (defface epa-mark-face
85 '((((class color) (background dark))
86 (:foreground "orange" :bold t))
88 (:foreground "red" :bold t)))
89 "Face used for displaying the high validity."
91 (defvar epa-mark-face 'epa-mark-face)
93 (defface epa-field-name-face
94 '((((class color) (background dark))
95 (:foreground "PaleTurquoise" :bold t))
97 "Face for the name of the attribute field."
99 (defvar epa-field-name-face 'epa-field-name-face)
101 (defface epa-field-body-face
102 '((((class color) (background dark))
103 (:foreground "turquoise" :italic t))
105 "Face for the body of the attribute field."
107 (defvar epa-field-body-face 'epa-field-body-face)
109 (defcustom epa-validity-face-alist
110 '((unknown . epa-validity-disabled-face)
111 (invalid . epa-validity-disabled-face)
112 (disabled . epa-validity-disabled-face)
113 (revoked . epa-validity-disabled-face)
114 (expired . epa-validity-disabled-face)
115 (none . epa-validity-low-face)
116 (undefined . epa-validity-low-face)
117 (never . epa-validity-low-face)
118 (marginal . epa-validity-medium-face)
119 (full . epa-validity-high-face)
120 (ultimate . epa-validity-high-face))
121 "An alist mapping validity values to faces."
125 (defcustom epa-font-lock-keywords
128 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
129 (1 epa-field-name-face)
130 (2 epa-field-body-face)))
131 "Default expressions to addon in epa-mode."
132 :type '(repeat (list string))
135 (defconst epa-pubkey-algorithm-letter-alist
143 (defvar epa-keys-buffer nil)
144 (defvar epa-key-buffer-alist nil)
146 (defvar epa-list-keys-arguments nil)
148 (defvar epa-keys-mode-map
149 (let ((keymap (make-sparse-keymap)))
150 (define-key keymap "m" 'epa-mark)
151 (define-key keymap "u" 'epa-unmark)
152 (define-key keymap "d" 'epa-decrypt-file)
153 (define-key keymap "v" 'epa-verify-file)
154 (define-key keymap "s" 'epa-sign-file)
155 (define-key keymap "S" 'epa-sign-keys)
156 (define-key keymap "e" 'epa-encrypt-file)
157 (define-key keymap "r" 'epa-delete-keys)
158 (define-key keymap "i" 'epa-import-keys)
159 (define-key keymap "o" 'epa-export-keys)
160 (define-key keymap "g" 'epa-list-keys)
161 (define-key keymap "n" 'next-line)
162 (define-key keymap "p" 'previous-line)
163 (define-key keymap " " 'scroll-up)
164 (define-key keymap [delete] 'scroll-down)
165 (define-key keymap "q" 'epa-exit-buffer)
168 (defvar epa-exit-buffer-function #'bury-buffer)
170 (define-widget 'epa-key 'push-button
171 "Button for representing a epg-key object."
173 :button-face-get 'epa-key-widget-button-face-get
174 :value-create 'epa-key-widget-value-create
175 :action 'epa-key-widget-action
176 :help-echo 'epa-key-widget-help-echo)
178 (defun epa-key-widget-action (widget &optional event)
179 (epa-show-key (widget-get widget :value)))
181 (defun epa-key-widget-value-create (widget)
182 (let* ((key (widget-get widget :value))
183 (primary-sub-key (car (epg-key-sub-key-list key)))
184 (primary-user-id (car (epg-key-user-id-list key))))
185 (insert (format "%c "
186 (if (epg-sub-key-validity primary-sub-key)
187 (car (rassq (epg-sub-key-validity primary-sub-key)
188 epg-key-validity-alist))
190 (epg-sub-key-id primary-sub-key)
192 (if (stringp (epg-user-id-name primary-user-id))
193 (epg-user-id-name primary-user-id)
194 (epg-decode-dn (epg-user-id-name primary-user-id))))))
196 (defun epa-key-widget-button-face-get (widget)
197 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
198 (widget-get widget :value))))))
200 (cdr (assq validity epa-validity-face-alist))
203 (defun epa-key-widget-help-echo (widget)
205 (epg-sub-key-id (car (epg-key-sub-key-list
206 (widget-get widget :value))))))
208 (defun epa-keys-mode ()
209 "Major mode for `epa-list-keys'."
210 (kill-all-local-variables)
211 (buffer-disable-undo)
212 (setq major-mode 'epa-keys-mode
216 (use-local-map epa-keys-mode-map)
217 (set-keymap-parent (current-local-map) widget-keymap)
218 (make-local-variable 'font-lock-defaults)
219 (setq font-lock-defaults '(epa-font-lock-keywords t))
220 ;; In XEmacs, auto-initialization of font-lock is not effective
221 ;; if buffer-file-name is not set.
222 (font-lock-set-defaults)
224 (make-local-variable 'epa-exit-buffer-function)
225 (run-hooks 'epa-keys-mode-hook))
227 (defvar epa-key-mode-map
228 (let ((keymap (make-sparse-keymap)))
229 (define-key keymap "q" 'bury-buffer)
232 (defun epa-key-mode ()
233 "Major mode for `epa-show-key'."
234 (kill-all-local-variables)
235 (buffer-disable-undo)
236 (setq major-mode 'epa-key-mode
240 (use-local-map epa-key-mode-map)
241 (make-local-variable 'font-lock-defaults)
242 (setq font-lock-defaults '(epa-font-lock-keywords t))
243 ;; In XEmacs, auto-initialization of font-lock is not effective
244 ;; if buffer-file-name is not set.
245 (font-lock-set-defaults)
246 (make-local-variable 'epa-exit-buffer-function)
247 (run-hooks 'epa-key-mode-hook))
250 (defun epa-list-keys (&optional name mode protocol)
252 (if current-prefix-arg
253 (let ((name (read-string "Pattern: "
254 (if epa-list-keys-arguments
255 (car epa-list-keys-arguments)))))
256 (list (if (equal name "") nil name)
257 (y-or-n-p "Secret keys? ")
258 (intern (completing-read "Protocol? "
259 '(("OpenPGP") ("CMS"))
261 (or epa-list-keys-arguments (list nil nil nil))))
262 (unless (and epa-keys-buffer
263 (buffer-live-p epa-keys-buffer))
264 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
265 (set-buffer epa-keys-buffer)
266 (let ((inhibit-read-only t)
269 (context (epg-make-context protocol)))
270 (unless (get-text-property point 'epa-list-keys)
271 (setq point (next-single-property-change point 'epa-list-keys)))
274 (or (next-single-property-change point 'epa-list-keys)
277 (epa-list-keys-1 context name mode)
279 (make-local-variable 'epa-list-keys-arguments)
280 (setq epa-list-keys-arguments (list name mode protocol))
281 (goto-char (point-min))
282 (pop-to-buffer (current-buffer)))
284 (defun epa-list-keys-1 (context name mode)
286 (narrow-to-region (point) (point))
287 (let ((inhibit-read-only t)
289 (keys (epg-list-keys context name mode))
294 (put-text-property point (point) 'epa-key (car keys))
295 (widget-create 'epa-key :value (car keys))
297 (setq keys (cdr keys))))
298 (put-text-property (point-min) (point-max) 'epa-list-keys t)))
300 (defun epa-marked-keys ()
302 (set-buffer epa-keys-buffer)
303 (goto-char (point-min))
305 (while (re-search-forward "^\\*" nil t)
306 (if (setq key (get-text-property (match-beginning 0)
308 (setq keys (cons key keys))))
312 (let ((key (get-text-property (point) 'epa-key)))
317 (defun epa-select-keys (context prompt &optional names secret)
318 "Display a user's keyring and ask him to select keys.
319 CONTEXT is an epg-context.
320 PROMPT is a string to prompt with.
321 NAMES is a list of strings to be matched with keys. If it is nil, all
323 If SECRET is non-nil, list secret keys instead of public keys."
325 (unless (and epa-keys-buffer
326 (buffer-live-p epa-keys-buffer))
327 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
328 (let ((inhibit-read-only t)
331 (set-buffer epa-keys-buffer)
335 :notify (lambda (&rest ignore) (abort-recursive-edit))
337 (substitute-command-keys
338 "Click here or \\[abort-recursive-edit] to cancel")
341 :notify (lambda (&rest ignore) (exit-recursive-edit))
343 (substitute-command-keys
344 "Click here or \\[exit-recursive-edit] to finish")
350 (epa-list-keys-1 context (car names) secret)
353 (goto-char (point-max))
354 (setq names (cdr names)))
355 (epa-list-keys-1 context nil secret))
357 (setq epa-exit-buffer-function #'abort-recursive-edit)
358 (goto-char (point-min))
359 (pop-to-buffer (current-buffer)))
364 (if (get-buffer-window epa-keys-buffer)
365 (delete-window (get-buffer-window epa-keys-buffer)))
366 (kill-buffer epa-keys-buffer))))
368 (defun epa-show-key (key)
369 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
370 (entry (assoc (epg-sub-key-id primary-sub-key)
371 epa-key-buffer-alist))
372 (inhibit-read-only t)
376 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
377 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
378 (unless (and (cdr entry)
379 (buffer-live-p (cdr entry)))
380 (setcdr entry (generate-new-buffer
381 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
382 (set-buffer (cdr entry))
383 (make-local-variable 'epa-key)
386 (setq pointer (epg-key-user-id-list key))
389 (if (epg-user-id-validity (car pointer))
391 (car (rassq (epg-user-id-validity (car pointer))
392 epg-key-validity-alist)))
395 (if (stringp (epg-user-id-name (car pointer)))
396 (epg-user-id-name (car pointer))
397 (epg-decode-dn (epg-user-id-name (car pointer))))
399 (setq pointer (cdr pointer)))
400 (setq pointer (epg-key-sub-key-list key))
403 (if (epg-sub-key-validity (car pointer))
405 (car (rassq (epg-sub-key-validity (car pointer))
406 epg-key-validity-alist)))
409 (epg-sub-key-id (car pointer))
412 (epg-sub-key-length (car pointer)))
414 (cdr (assq (epg-sub-key-algorithm (car pointer))
415 epg-pubkey-algorithm-alist))
417 (epg-sub-key-creation-time (car pointer))
418 (if (epg-sub-key-expiration-time (car pointer))
419 (format "\n\tExpires: %s" (epg-sub-key-expiration-time
423 (mapconcat #'symbol-name
424 (epg-sub-key-capability (car pointer))
427 (epg-sub-key-fingerprint (car pointer))
429 (setq pointer (cdr pointer)))
430 (goto-char (point-min))
431 (pop-to-buffer (current-buffer))
434 (defun epa-show-key-notify (widget &rest ignore)
435 (epa-show-key (widget-get widget :value)))
437 (defun epa-mark (&optional arg)
438 "Mark the current line.
439 If ARG is non-nil, unmark the current line."
441 (let ((inhibit-read-only t)
445 (setq properties (text-properties-at (point)))
447 (insert (if arg " " "*"))
448 (set-text-properties (1- (point)) (point) properties)
451 (defun epa-unmark (&optional arg)
452 "Unmark the current line.
453 If ARG is non-nil, mark the current line."
455 (epa-mark (not arg)))
457 (defun epa-exit-buffer ()
458 "Exit the current buffer.
459 `epa-exit-buffer-function' is called if it is set."
461 (funcall epa-exit-buffer-function))
464 (defun epa-decrypt-file (file)
466 (interactive "fFile: ")
467 (let* ((default-name (file-name-sans-extension file))
468 (plain (expand-file-name
470 (concat "To file (default "
471 (file-name-nondirectory default-name)
473 (file-name-directory default-name)
475 (context (epg-make-context)))
476 (message "Decrypting %s..." (file-name-nondirectory file))
477 (epg-decrypt-file context file plain)
478 (message "Decrypting %s...done" (file-name-nondirectory file))))
481 (defun epa-verify-file (file)
483 (interactive "fFile: ")
484 (let* ((context (epg-make-context))
485 (plain (if (equal (file-name-extension file) "sig")
486 (file-name-sans-extension file))))
487 (message "Verifying %s..." (file-name-nondirectory file))
488 (epg-verify-file context file plain)
489 (message "Verifying %s...done" (file-name-nondirectory file))
491 (epg-verify-result-to-string
492 (epg-context-result-for context 'verify)))))
495 (defun epa-sign-file (file signers detached)
496 "Sign FILE by selected SIGNERS keys.
497 If DETACHED is non-nil, it creates a detached signature."
499 (list (expand-file-name (read-file-name "File: "))
500 (epa-select-keys (epg-make-context) "Select keys for signing.
501 If no one is selected, default secret key is used. "
503 (y-or-n-p "Make a detached signature? ")))
504 (let ((signature (concat file (if detached ".sig" ".gpg")))
505 (context (epg-make-context)))
506 (message "Signing %s..." (file-name-nondirectory file))
507 (epg-context-set-signers context signers)
508 (epg-sign-file context file signature (not (null detached)))
509 (message "Signing %s...done" (file-name-nondirectory file))))
512 (defun epa-encrypt-file (file recipients)
513 "Encrypt FILE for RECIPIENTS."
515 (list (expand-file-name (read-file-name "File: "))
516 (epa-select-keys (epg-make-context) "Select recipents for encryption.
517 If no one is selected, symmetric encryption will be performed. ")))
518 (let ((cipher (concat file ".gpg"))
519 (context (epg-make-context)))
520 (message "Encrypting %s..." (file-name-nondirectory file))
521 (epg-encrypt-file context file recipients cipher)
522 (message "Encrypting %s...done" (file-name-nondirectory file))))
525 (defun epa-delete-keys (keys)
526 "Delete selected KEYS."
528 (let ((keys (epa-marked-keys)))
530 (error "No keys selected"))
532 (let ((context (epg-make-context)))
533 (message "Deleting...")
534 (epg-delete-keys context keys)
535 (apply #'epa-list-keys epa-list-keys-arguments)
536 (message "Deleting...done")))
539 (defun epa-import-keys (file)
540 "Import keys from FILE."
541 (interactive "fFile: ")
542 (let ((context (epg-make-context)))
543 (message "Importing %s..." (file-name-nondirectory file))
544 (epg-import-keys-from-file context (expand-file-name file))
545 (apply #'epa-list-keys epa-list-keys-arguments)
546 (message "Importing %s...done" (file-name-nondirectory file))))
549 (defun epa-export-keys (keys file)
550 "Export selected KEYS to FILE."
552 (let ((keys (epa-marked-keys))
555 (error "No keys selected"))
558 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
564 (concat "To file (default "
565 (file-name-nondirectory default-name)
567 (file-name-directory default-name)
569 (let ((context (epg-make-context)))
570 (message "Exporting to %s..." (file-name-nondirectory file))
571 (epg-export-keys-to-file context keys file)
572 (message "Exporting to %s...done" (file-name-nondirectory file))))
575 (defun epa-sign-keys (keys &optional local)
577 If LOCAL is non-nil, the signature is marked as non exportable."
579 (let ((keys (epa-marked-keys)))
581 (error "No keys selected"))
582 (list keys current-prefix-arg)))
583 (let ((context (epg-make-context)))
584 (message "Signing keys...")
585 (epg-sign-keys context keys local)
586 (message "Signing keys...done")))