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 (run-hooks 'epa-keys-mode-hook))
226 (defvar epa-key-mode-map
227 (let ((keymap (make-sparse-keymap)))
228 (define-key keymap "q" 'bury-buffer)
231 (defun epa-key-mode ()
232 "Major mode for `epa-show-key'."
233 (kill-all-local-variables)
234 (buffer-disable-undo)
235 (setq major-mode 'epa-key-mode
239 (use-local-map epa-key-mode-map)
240 (make-local-variable 'font-lock-defaults)
241 (setq font-lock-defaults '(epa-font-lock-keywords t))
242 ;; In XEmacs, auto-initialization of font-lock is not effective
243 ;; if buffer-file-name is not set.
244 (font-lock-set-defaults)
245 (make-local-variable 'epa-exit-buffer-function)
246 (run-hooks 'epa-key-mode-hook))
249 (defun epa-list-keys (&optional name mode protocol)
251 (if current-prefix-arg
252 (let ((name (read-string "Pattern: "
253 (if epa-list-keys-arguments
254 (car epa-list-keys-arguments)))))
255 (list (if (equal name "") nil name)
256 (y-or-n-p "Secret keys? ")
257 (intern (completing-read "Protocol? "
258 '(("OpenPGP") ("CMS"))
260 (or epa-list-keys-arguments (list nil nil nil))))
261 (unless (and epa-keys-buffer
262 (buffer-live-p epa-keys-buffer))
263 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
264 (set-buffer epa-keys-buffer)
265 (let ((inhibit-read-only t)
268 (context (epg-make-context protocol)))
269 (unless (get-text-property point 'epa-list-keys)
270 (setq point (next-single-property-change point 'epa-list-keys)))
273 (or (next-single-property-change point 'epa-list-keys)
276 (epa-list-keys-1 context name mode)
278 (make-local-variable 'epa-list-keys-arguments)
279 (setq epa-list-keys-arguments (list name mode protocol))
280 (goto-char (point-min))
281 (pop-to-buffer (current-buffer)))
283 (defun epa-list-keys-1 (context name mode)
285 (narrow-to-region (point) (point))
286 (let ((inhibit-read-only t)
288 (keys (epg-list-keys context name mode))
293 (put-text-property point (point) 'epa-key (car keys))
294 (widget-create 'epa-key :value (car keys))
296 (setq keys (cdr keys))))
297 (put-text-property (point-min) (point-max) 'epa-list-keys t)))
299 (defun epa-marked-keys ()
301 (set-buffer epa-keys-buffer)
302 (goto-char (point-min))
304 (while (re-search-forward "^\\*" nil t)
305 (if (setq key (get-text-property (match-beginning 0)
307 (setq keys (cons key keys))))
311 (let ((key (get-text-property (point) 'epa-key)))
315 (defun epa-select-keys (context prompt &optional names mode)
317 (unless (and epa-keys-buffer
318 (buffer-live-p epa-keys-buffer))
319 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
320 (let ((inhibit-read-only t)
323 (set-buffer epa-keys-buffer)
327 :notify (lambda (&rest ignore) (abort-recursive-edit))
329 (substitute-command-keys
330 "Click here or \\[abort-recursive-edit] to cancel")
333 :notify (lambda (&rest ignore) (exit-recursive-edit))
335 (substitute-command-keys
336 "Click here or \\[exit-recursive-edit] to finish")
342 (epa-list-keys-1 context (car names) mode)
345 (goto-char (point-max))
346 (setq names (cdr names)))
347 (epa-list-keys-1 context nil mode))
349 (setq epa-exit-buffer-function #'abort-recursive-edit)
350 (goto-char (point-min))
351 (pop-to-buffer (current-buffer))
356 (if (get-buffer-window epa-keys-buffer)
357 (delete-window (get-buffer-window epa-keys-buffer)))
358 (kill-buffer epa-keys-buffer)))))
360 (defun epa-show-key (key)
361 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
362 (entry (assoc (epg-sub-key-id primary-sub-key)
363 epa-key-buffer-alist))
364 (inhibit-read-only t)
368 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
369 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
370 (unless (and (cdr entry)
371 (buffer-live-p (cdr entry)))
372 (setcdr entry (generate-new-buffer
373 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
374 (set-buffer (cdr entry))
375 (make-local-variable 'epa-key)
378 (setq pointer (epg-key-user-id-list key))
381 (if (epg-user-id-validity (car pointer))
383 (car (rassq (epg-user-id-validity (car pointer))
384 epg-key-validity-alist)))
387 (if (stringp (epg-user-id-name (car pointer)))
388 (epg-user-id-name (car pointer))
389 (epg-decode-dn (epg-user-id-name (car pointer))))
391 (setq pointer (cdr pointer)))
392 (setq pointer (epg-key-sub-key-list key))
395 (if (epg-sub-key-validity (car pointer))
397 (car (rassq (epg-sub-key-validity (car pointer))
398 epg-key-validity-alist)))
401 (epg-sub-key-id (car pointer))
404 (epg-sub-key-length (car pointer)))
406 (cdr (assq (epg-sub-key-algorithm (car pointer))
407 epg-pubkey-algorithm-alist))
409 (epg-sub-key-creation-time (car pointer))
410 (if (epg-sub-key-expiration-time (car pointer))
411 (format "\n\tExpires: %s" (epg-sub-key-expiration-time
415 (mapconcat #'symbol-name
416 (epg-sub-key-capability (car pointer))
419 (epg-sub-key-fingerprint (car pointer))
421 (setq pointer (cdr pointer)))
422 (goto-char (point-min))
423 (pop-to-buffer (current-buffer))
426 (defun epa-show-key-notify (widget &rest ignore)
427 (epa-show-key (widget-get widget :value)))
429 (defun epa-mark (&optional arg)
430 "Mark the current line."
432 (let ((inhibit-read-only t)
436 (setq properties (text-properties-at (point)))
438 (insert (if arg " " "*"))
439 (set-text-properties (1- (point)) (point) properties)
442 (defun epa-unmark (&optional arg)
443 "Unmark the current line."
445 (epa-mark (not arg)))
447 (defun epa-exit-buffer ()
449 (funcall epa-exit-buffer-function))
451 (defun epa-decrypt-file (file)
452 (interactive "fFile: ")
453 (let* ((default-name (file-name-sans-extension file))
454 (plain (expand-file-name
456 (concat "To file (default "
457 (file-name-nondirectory default-name)
459 (file-name-directory default-name)
461 (context (epg-make-context)))
462 (message "Decrypting %s..." (file-name-nondirectory file))
463 (epg-decrypt-file context file plain)
464 (message "Decrypting %s...done" (file-name-nondirectory file))))
466 (defun epa-verify-file (file)
467 (interactive "fFile: ")
468 (let* ((context (epg-make-context))
469 (plain (if (equal (file-name-extension file) "sig")
470 (file-name-sans-extension file))))
471 (message "Verifying %s..." (file-name-nondirectory file))
472 (epg-verify-file context file plain)
473 (message "Verifying %s...done" (file-name-nondirectory file))
475 (epg-verify-result-to-string
476 (epg-context-result-for context 'verify)))))
478 (defun epa-sign-file (file detached)
480 (list (expand-file-name (read-file-name "File: "))
481 (y-or-n-p "Make a detached signature? ")))
482 (let ((signature (concat file (if detached ".sig" ".gpg")))
483 (context (epg-make-context)))
484 (message "Signing %s..." (file-name-nondirectory file))
485 (epg-sign-file context file signature (not (null detached)))
486 (message "Signing %s...done" (file-name-nondirectory file))))
488 (defun epa-encrypt-file (file recipients)
490 (list (expand-file-name (read-file-name "File: "))
491 (epa-select-keys (epg-make-context) "Select recipents for encryption.
492 If no one is selected, symmetric encryption will be performed. ")))
493 (let ((cipher (concat file ".gpg"))
494 (context (epg-make-context)))
495 (message "Encrypting %s..." (file-name-nondirectory file))
496 (epg-encrypt-file context file recipients cipher)
497 (message "Encrypting %s...done" (file-name-nondirectory file))))
499 (defun epa-delete-keys (keys)
501 (let ((keys (epa-marked-keys)))
503 (error "No keys selected"))
505 (let ((context (epg-make-context)))
506 (message "Deleting...")
507 (epg-delete-keys context keys)
508 (apply #'epa-list-keys epa-list-keys-arguments)
509 (message "Deleting...done")))
511 (defun epa-import-keys (file)
512 (interactive "fFile: ")
513 (let ((context (epg-make-context)))
514 (message "Importing %s..." (file-name-nondirectory file))
515 (epg-import-keys-from-file context (expand-file-name file))
516 (apply #'epa-list-keys epa-list-keys-arguments)
517 (message "Importing %s...done" (file-name-nondirectory file))))
519 (defun epa-export-keys (keys file)
521 (let ((keys (epa-marked-keys))
524 (error "No keys selected"))
527 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
533 (concat "To file (default "
534 (file-name-nondirectory default-name)
536 (file-name-directory default-name)
538 (let ((context (epg-make-context)))
539 (message "Exporting to %s..." (file-name-nondirectory file))
540 (epg-export-keys-to-file context keys file)
541 (message "Exporting to %s...done" (file-name-nondirectory file))))
543 (defun epa-sign-keys (keys &optional local)
545 (let ((keys (epa-marked-keys)))
547 (error "No keys selected"))
548 (list keys current-prefix-arg)))
549 (let ((context (epg-make-context)))
550 (message "Signing keys...")
551 (epg-sign-keys context keys local)
552 (message "Signing keys...done")))