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 "e" 'epa-encrypt-file)
156 (define-key keymap "r" 'epa-delete-keys)
157 (define-key keymap "i" 'epa-import-keys)
158 (define-key keymap "o" 'epa-export-keys)
159 (define-key keymap "g" 'epa-list-keys)
160 (define-key keymap "n" 'next-line)
161 (define-key keymap "p" 'previous-line)
162 (define-key keymap " " 'scroll-up)
163 (define-key keymap [delete] 'scroll-down)
164 (define-key keymap "q" 'bury-buffer)
167 (define-widget 'epa-key 'push-button
168 "Button for representing a epg-key object."
170 :button-face-get 'epa-key-widget-button-face-get
171 :value-create 'epa-key-widget-value-create
172 :action 'epa-key-widget-action
173 :help-echo 'epa-key-widget-help-echo)
175 (defun epa-key-widget-action (widget &optional event)
176 (epa-show-key (widget-get widget :value)))
178 (defun epa-key-widget-value-create (widget)
179 (let* ((key (widget-get widget :value))
180 (primary-sub-key (car (epg-key-sub-key-list key)))
181 (primary-user-id (car (epg-key-user-id-list key))))
182 (insert (format "%c "
183 (if (epg-sub-key-validity primary-sub-key)
184 (car (rassq (epg-sub-key-validity primary-sub-key)
185 epg-key-validity-alist))
187 (epg-sub-key-id primary-sub-key)
189 (epg-user-id-name primary-user-id))))
191 (defun epa-key-widget-button-face-get (widget)
192 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
193 (widget-get widget :value))))))
195 (cdr (assq validity epa-validity-face-alist))
198 (defun epa-key-widget-help-echo (widget)
200 (epg-sub-key-id (car (epg-key-sub-key-list
201 (widget-get widget :value))))))
203 (defun epa-keys-mode ()
204 "Major mode for `epa-list-keys'."
205 (kill-all-local-variables)
206 (buffer-disable-undo)
207 (setq major-mode 'epa-keys-mode
211 (use-local-map epa-keys-mode-map)
212 (set-keymap-parent (current-local-map) widget-keymap)
213 (make-local-variable 'font-lock-defaults)
214 (setq font-lock-defaults '(epa-font-lock-keywords t))
215 ;; In XEmacs, auto-initialization of font-lock is not effective
216 ;; if buffer-file-name is not set.
217 (font-lock-set-defaults)
219 (run-hooks 'epa-keys-mode-hook))
221 (defvar epa-key-mode-map
222 (let ((keymap (make-sparse-keymap)))
223 (define-key keymap "q" 'bury-buffer)
226 (defun epa-key-mode ()
227 "Major mode for `epa-show-key'."
228 (kill-all-local-variables)
229 (buffer-disable-undo)
230 (setq major-mode 'epa-key-mode
234 (use-local-map epa-key-mode-map)
235 (make-local-variable 'font-lock-defaults)
236 (setq font-lock-defaults '(epa-font-lock-keywords t))
237 ;; In XEmacs, auto-initialization of font-lock is not effective
238 ;; if buffer-file-name is not set.
239 (font-lock-set-defaults)
240 (run-hooks 'epa-key-mode-hook))
243 (defun epa-list-keys (&optional name mode)
245 (if current-prefix-arg
246 (let ((name (read-string "Pattern: "
247 (if epa-list-keys-arguments
248 (car epa-list-keys-arguments)))))
249 (list (if (equal name "") nil name)
250 (y-or-n-p "Secret keys? ")))
251 (or epa-list-keys-arguments (list nil nil))))
252 (unless (and epa-keys-buffer
253 (buffer-live-p epa-keys-buffer))
254 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
255 (set-buffer epa-keys-buffer)
256 (let ((inhibit-read-only t)
259 (epa-list-keys-1 name mode)
261 (make-local-variable 'epa-list-keys-arguments)
262 (setq epa-list-keys-arguments (list name mode))
263 (goto-char (point-min))
264 (pop-to-buffer (current-buffer)))
266 (defun epa-list-keys-1 (name mode)
267 (let ((inhibit-read-only t)
269 (keys (epg-list-keys name mode))
274 (put-text-property point (point) 'epa-key (car keys))
275 (widget-create 'epa-key :value (car keys))
277 (setq keys (cdr keys)))))
279 (defun epa-marked-keys ()
281 (set-buffer epa-keys-buffer)
282 (goto-char (point-min))
284 (while (re-search-forward "^\\*" nil t)
285 (if (setq key (get-text-property (match-beginning 0)
287 (setq keys (cons key keys))))
291 (get-text-property (point) 'epa-key))))
293 (defun epa-select-keys (prompt &optional names mode)
295 (unless (and epa-keys-buffer
296 (buffer-live-p epa-keys-buffer))
297 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
298 (let ((inhibit-read-only t)
301 (set-buffer epa-keys-buffer)
305 :notify (lambda (&rest ignore) (exit-recursive-edit))
307 (substitute-command-keys
308 "Click here or \\[exit-recursive-edit] to finish")
314 (epa-list-keys-1 (car names) mode)
317 (goto-char (point-max))
318 (setq names (cdr names)))
319 (epa-list-keys-1 nil mode))
321 (goto-char (point-min))
322 (pop-to-buffer (current-buffer))
327 (if (get-buffer-window epa-keys-buffer)
328 (delete-window (get-buffer-window epa-keys-buffer)))
329 (kill-buffer epa-keys-buffer)))))
331 (defun epa-show-key (key)
332 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
333 (entry (assoc (epg-sub-key-id primary-sub-key)
334 epa-key-buffer-alist))
335 (inhibit-read-only t)
339 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
340 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
341 (unless (and (cdr entry)
342 (buffer-live-p (cdr entry)))
343 (setcdr entry (generate-new-buffer
344 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
345 (set-buffer (cdr entry))
346 (make-local-variable 'epa-key)
349 (setq pointer (epg-key-user-id-list key))
352 (if (epg-user-id-validity (car pointer))
354 (car (rassq (epg-user-id-validity (car pointer))
355 epg-key-validity-alist)))
358 (epg-user-id-name (car pointer))
360 (setq pointer (cdr pointer)))
361 (setq pointer (epg-key-sub-key-list key))
364 (if (epg-sub-key-validity (car pointer))
366 (car (rassq (epg-sub-key-validity (car pointer))
367 epg-key-validity-alist)))
370 (epg-sub-key-id (car pointer))
373 (epg-sub-key-length (car pointer)))
375 (cdr (assq (epg-sub-key-algorithm (car pointer))
376 epg-pubkey-algorithm-alist))
378 (epg-sub-key-creation-time (car pointer))
379 (if (epg-sub-key-expiration-time (car pointer))
380 (format "\n\tExpires: %s" (epg-sub-key-expiration-time
384 (mapconcat #'symbol-name
385 (epg-sub-key-capability (car pointer))
388 (epg-sub-key-fingerprint (car pointer))
390 (setq pointer (cdr pointer)))
391 (goto-char (point-min))
392 (pop-to-buffer (current-buffer))
395 (defun epa-show-key-notify (widget &rest ignore)
396 (epa-show-key (widget-get widget :value)))
398 (defun epa-mark (&optional arg)
399 "Mark the current line."
401 (let ((inhibit-read-only t)
405 (setq properties (text-properties-at (point)))
407 (insert (if arg " " "*"))
408 (set-text-properties (1- (point)) (point) properties)
411 (defun epa-unmark (&optional arg)
412 "Unmark the current line."
414 (epa-mark (not arg)))
416 (defun epa-decrypt-file (file)
417 (interactive "fFile: ")
418 (let* ((default-name (file-name-sans-extension file))
419 (plain (expand-file-name
421 (concat "To file (default "
422 (file-name-nondirectory default-name)
424 (file-name-directory default-name)
426 (context (epg-make-context)))
427 (message "Decrypting %s..." (file-name-nondirectory file))
428 (epg-decrypt-file context file plain)
429 (message "Decrypting %s...done" (file-name-nondirectory file))))
431 (defun epa-verify-file (file)
432 (interactive "fFile: ")
433 (let* ((context (epg-make-context))
434 (plain (if (equal (file-name-extension file) "sig")
435 (file-name-sans-extension file)))
437 (message "Verifying %s..." (file-name-nondirectory file))
438 (epg-verify-file context file plain)
439 (message "Verifying %s...done" (file-name-nondirectory file))
440 (setq signature (reverse (epg-context-result-for context 'verify)))
442 (message "%s: %s %s %s"
443 (epg-signature-status (car signature))
444 (epg-signature-key-id (car signature))
445 (epg-signature-user-id (car signature))
446 (epg-signature-validity (car signature)))
447 (setq signature (cdr signature)))))
449 (defun epa-sign-file (file detached)
451 (list (expand-file-name (read-file-name "File: "))
452 (y-or-n-p "Make a detached signature? ")))
453 (let ((signature (concat file (if detached ".sig" ".gpg")))
454 (context (epg-make-context)))
455 (message "Signing %s..." (file-name-nondirectory file))
456 (epg-sign-file context file signature (not (null detached)))
457 (message "Signing %s...done" (file-name-nondirectory file))))
459 (defun epa-encrypt-file (file recipients)
461 (list (expand-file-name (read-file-name "File: "))
462 (epa-select-keys "Select recipents for encryption.
463 If no one is selected, symmetric encryption will be performed. ")))
464 (let ((cipher (concat file ".gpg"))
465 (context (epg-make-context)))
466 (message "Encrypting %s..." (file-name-nondirectory file))
467 (epg-encrypt-file context
471 (message "Encrypting %s...done" (file-name-nondirectory file))))
473 (defun epa-delete-keys (keys)
475 (let ((keys (epa-marked-keys)))
477 (error "No keys selected"))
479 (let ((context (epg-make-context)))
480 (message "Deleting...")
481 (epg-delete-keys context keys)
482 (apply #'epa-list-keys epa-list-keys-arguments)
483 (message "Deleting...done")))
485 (defun epa-import-keys (file)
486 (interactive "fFile: ")
487 (let ((context (epg-make-context)))
488 (message "Importing %s..." (file-name-nondirectory file))
489 (epg-import-keys-from-file context (expand-file-name file))
490 (apply #'epa-list-keys epa-list-keys-arguments)
491 (message "Importing %s...done" (file-name-nondirectory file))))
493 (defun epa-export-keys (keys file)
495 (let ((keys (epa-marked-keys))
498 (error "No keys selected"))
501 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
507 (concat "To file (default "
508 (file-name-nondirectory default-name)
510 (file-name-directory default-name)
512 (let ((context (epg-make-context)))
513 (message "Exporting to %s..." (file-name-nondirectory file))
514 (epg-export-keys-to-file context keys file)
515 (message "Exporting to %s...done" (file-name-nondirectory file))))