Commit | Line | Data |
---|---|---|
74f50695 | 1 | ;;; epa.el --- the EasyPG Assistant -*- lexical-binding: t -*- |
e9bffc61 | 2 | |
acaf905b | 3 | ;; Copyright (C) 2006-2012 Free Software Foundation, Inc. |
c154c0be MO |
4 | |
5 | ;; Author: Daiki Ueno <ueno@unixuser.org> | |
6 | ;; Keywords: PGP, GnuPG | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
c154c0be | 11 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
c154c0be MO |
14 | |
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. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
c154c0be MO |
22 | |
23 | ;;; Code: | |
24 | ||
25 | (require 'epg) | |
26 | (require 'font-lock) | |
27 | (require 'widget) | |
28 | (eval-when-compile (require 'wid-edit)) | |
29 | (require 'derived) | |
30 | ||
31 | (defgroup epa nil | |
32 | "The EasyPG Assistant" | |
0bd4f317 | 33 | :version "23.1" |
c154c0be MO |
34 | :group 'epg) |
35 | ||
36 | (defcustom epa-popup-info-window t | |
37 | "If non-nil, status information from epa commands is displayed on | |
38 | the separate window." | |
39 | :type 'boolean | |
40 | :group 'epa) | |
41 | ||
42 | (defcustom epa-info-window-height 5 | |
43 | "Number of lines used to display status information." | |
44 | :type 'integer | |
45 | :group 'epa) | |
46 | ||
47 | (defgroup epa-faces nil | |
48 | "Faces for epa-mode." | |
0bd4f317 | 49 | :version "23.1" |
c154c0be MO |
50 | :group 'epa) |
51 | ||
52 | (defface epa-validity-high | |
4b56d0fe CY |
53 | '((default :weight bold) |
54 | (((class color) (background dark)) :foreground "PaleTurquoise")) | |
55 | "Face for high validity EPA information." | |
c154c0be MO |
56 | :group 'epa-faces) |
57 | ||
58 | (defface epa-validity-medium | |
4b56d0fe CY |
59 | '((default :slant italic) |
60 | (((class color) (background dark)) :foreground "PaleTurquoise")) | |
61 | "Face for medium validity EPA information." | |
c154c0be MO |
62 | :group 'epa-faces) |
63 | ||
64 | (defface epa-validity-low | |
4b56d0fe | 65 | '((t :slant italic)) |
c154c0be MO |
66 | "Face used for displaying the low validity." |
67 | :group 'epa-faces) | |
68 | ||
69 | (defface epa-validity-disabled | |
4b56d0fe | 70 | '((t :slant italic :inverse-video t)) |
c154c0be MO |
71 | "Face used for displaying the disabled validity." |
72 | :group 'epa-faces) | |
73 | ||
74 | (defface epa-string | |
75 | '((((class color) (background dark)) | |
4b56d0fe | 76 | :foreground "lightyellow") |
c154c0be | 77 | (((class color) (background light)) |
4b56d0fe | 78 | :foreground "blue4")) |
c154c0be MO |
79 | "Face used for displaying the string." |
80 | :group 'epa-faces) | |
81 | ||
82 | (defface epa-mark | |
4b56d0fe CY |
83 | '((default :weight bold) |
84 | (((class color) (background dark)) :foreground "orange") | |
85 | (((class color) (background light)) :foreground "red")) | |
c154c0be MO |
86 | "Face used for displaying the high validity." |
87 | :group 'epa-faces) | |
88 | ||
89 | (defface epa-field-name | |
4b56d0fe CY |
90 | '((default :weight bold) |
91 | (((class color) (background dark)) :foreground "PaleTurquoise")) | |
c154c0be MO |
92 | "Face for the name of the attribute field." |
93 | :group 'epa) | |
94 | ||
95 | (defface epa-field-body | |
4b56d0fe CY |
96 | '((default :slant italic) |
97 | (((class color) (background dark)) :foreground "turquoise")) | |
c154c0be MO |
98 | "Face for the body of the attribute field." |
99 | :group 'epa) | |
100 | ||
101 | (defcustom epa-validity-face-alist | |
102 | '((unknown . epa-validity-disabled) | |
103 | (invalid . epa-validity-disabled) | |
104 | (disabled . epa-validity-disabled) | |
105 | (revoked . epa-validity-disabled) | |
106 | (expired . epa-validity-disabled) | |
107 | (none . epa-validity-low) | |
108 | (undefined . epa-validity-low) | |
109 | (never . epa-validity-low) | |
110 | (marginal . epa-validity-medium) | |
111 | (full . epa-validity-high) | |
112 | (ultimate . epa-validity-high)) | |
113 | "An alist mapping validity values to faces." | |
114 | :type '(repeat (cons symbol face)) | |
115 | :group 'epa) | |
116 | ||
117 | (defvar epa-font-lock-keywords | |
118 | '(("^\\*" | |
119 | (0 'epa-mark)) | |
120 | ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$" | |
121 | (1 'epa-field-name) | |
122 | (2 'epa-field-body))) | |
123 | "Default expressions to addon in epa-mode.") | |
124 | ||
125 | (defconst epa-pubkey-algorithm-letter-alist | |
126 | '((1 . ?R) | |
127 | (2 . ?r) | |
128 | (3 . ?s) | |
129 | (16 . ?g) | |
130 | (17 . ?D) | |
131 | (20 . ?G))) | |
132 | ||
133 | (defvar epa-protocol 'OpenPGP | |
fb7ada5f | 134 | "The default protocol. |
c154c0be MO |
135 | The value can be either OpenPGP or CMS. |
136 | ||
137 | You should bind this variable with `let', but do not set it globally.") | |
138 | ||
139 | (defvar epa-armor nil | |
fb7ada5f | 140 | "If non-nil, epa commands create ASCII armored output. |
c154c0be MO |
141 | |
142 | You should bind this variable with `let', but do not set it globally.") | |
143 | ||
144 | (defvar epa-textmode nil | |
fb7ada5f | 145 | "If non-nil, epa commands treat input files as text. |
c154c0be MO |
146 | |
147 | You should bind this variable with `let', but do not set it globally.") | |
148 | ||
149 | (defvar epa-keys-buffer nil) | |
150 | (defvar epa-key-buffer-alist nil) | |
151 | (defvar epa-key nil) | |
152 | (defvar epa-list-keys-arguments nil) | |
153 | (defvar epa-info-buffer nil) | |
154 | (defvar epa-last-coding-system-specified nil) | |
155 | ||
156 | (defvar epa-key-list-mode-map | |
dc9b613e GM |
157 | (let ((keymap (make-sparse-keymap)) |
158 | (menu-map (make-sparse-keymap))) | |
c154c0be MO |
159 | (define-key keymap "m" 'epa-mark-key) |
160 | (define-key keymap "u" 'epa-unmark-key) | |
161 | (define-key keymap "d" 'epa-decrypt-file) | |
162 | (define-key keymap "v" 'epa-verify-file) | |
163 | (define-key keymap "s" 'epa-sign-file) | |
164 | (define-key keymap "e" 'epa-encrypt-file) | |
165 | (define-key keymap "r" 'epa-delete-keys) | |
166 | (define-key keymap "i" 'epa-import-keys) | |
167 | (define-key keymap "o" 'epa-export-keys) | |
168 | (define-key keymap "g" 'revert-buffer) | |
169 | (define-key keymap "n" 'next-line) | |
170 | (define-key keymap "p" 'previous-line) | |
ce3cefcc CY |
171 | (define-key keymap " " 'scroll-up-command) |
172 | (define-key keymap [delete] 'scroll-down-command) | |
c154c0be | 173 | (define-key keymap "q" 'epa-exit-buffer) |
dc9b613e | 174 | (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map)) |
703b9611 DN |
175 | (define-key menu-map [epa-key-list-unmark-key] |
176 | '(menu-item "Unmark Key" epa-unmark-key | |
177 | :help "Unmark a key")) | |
178 | (define-key menu-map [epa-key-list-mark-key] | |
179 | '(menu-item "Mark Key" epa-mark-key | |
180 | :help "Mark a key")) | |
181 | (define-key menu-map [separator-epa-file] '(menu-item "--")) | |
182 | (define-key menu-map [epa-verify-file] | |
183 | '(menu-item "Verify File..." epa-verify-file | |
184 | :help "Verify FILE")) | |
185 | (define-key menu-map [epa-sign-file] | |
186 | '(menu-item "Sign File..." epa-sign-file | |
187 | :help "Sign FILE by SIGNERS keys selected")) | |
188 | (define-key menu-map [epa-decrypt-file] | |
189 | '(menu-item "Decrypt File..." epa-decrypt-file | |
190 | :help "Decrypt FILE")) | |
191 | (define-key menu-map [epa-encrypt-file] | |
d88444f2 | 192 | '(menu-item "Encrypt File..." epa-encrypt-file |
703b9611 DN |
193 | :help "Encrypt FILE for RECIPIENTS")) |
194 | (define-key menu-map [separator-epa-key-list] '(menu-item "--")) | |
dc9b613e | 195 | (define-key menu-map [epa-key-list-delete-keys] |
7cc6e154 | 196 | '(menu-item "Delete Keys" epa-delete-keys |
703b9611 | 197 | :help "Delete Marked Keys")) |
dc9b613e | 198 | (define-key menu-map [epa-key-list-import-keys] |
703b9611 | 199 | '(menu-item "Import Keys" epa-import-keys |
dc9b613e GM |
200 | :help "Import keys from a file")) |
201 | (define-key menu-map [epa-key-list-export-keys] | |
703b9611 | 202 | '(menu-item "Export Keys" epa-export-keys |
dc9b613e | 203 | :help "Export marked keys to a file")) |
c154c0be MO |
204 | keymap)) |
205 | ||
206 | (defvar epa-key-mode-map | |
207 | (let ((keymap (make-sparse-keymap))) | |
208 | (define-key keymap "q" 'epa-exit-buffer) | |
209 | keymap)) | |
210 | ||
211 | (defvar epa-info-mode-map | |
212 | (let ((keymap (make-sparse-keymap))) | |
213 | (define-key keymap "q" 'delete-window) | |
214 | keymap)) | |
215 | ||
216 | (defvar epa-exit-buffer-function #'bury-buffer) | |
217 | ||
218 | (define-widget 'epa-key 'push-button | |
219 | "Button for representing a epg-key object." | |
220 | :format "%[%v%]" | |
221 | :button-face-get 'epa--key-widget-button-face-get | |
222 | :value-create 'epa--key-widget-value-create | |
223 | :action 'epa--key-widget-action | |
224 | :help-echo 'epa--key-widget-help-echo) | |
225 | ||
74f50695 | 226 | (defun epa--key-widget-action (widget &optional _event) |
a648af17 DU |
227 | (save-selected-window |
228 | (epa--show-key (widget-get widget :value)))) | |
c154c0be MO |
229 | |
230 | (defun epa--key-widget-value-create (widget) | |
231 | (let* ((key (widget-get widget :value)) | |
232 | (primary-sub-key (car (epg-key-sub-key-list key))) | |
233 | (primary-user-id (car (epg-key-user-id-list key)))) | |
234 | (insert (format "%c " | |
235 | (if (epg-sub-key-validity primary-sub-key) | |
236 | (car (rassq (epg-sub-key-validity primary-sub-key) | |
237 | epg-key-validity-alist)) | |
238 | ? )) | |
239 | (epg-sub-key-id primary-sub-key) | |
240 | " " | |
241 | (if primary-user-id | |
242 | (if (stringp (epg-user-id-string primary-user-id)) | |
243 | (epg-user-id-string primary-user-id) | |
244 | (epg-decode-dn (epg-user-id-string primary-user-id))) | |
245 | "")))) | |
246 | ||
247 | (defun epa--key-widget-button-face-get (widget) | |
248 | (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list | |
249 | (widget-get widget :value)))))) | |
250 | (if validity | |
251 | (cdr (assq validity epa-validity-face-alist)) | |
252 | 'default))) | |
253 | ||
254 | (defun epa--key-widget-help-echo (widget) | |
255 | (format "Show %s" | |
256 | (epg-sub-key-id (car (epg-key-sub-key-list | |
257 | (widget-get widget :value)))))) | |
258 | ||
259 | (eval-and-compile | |
260 | (if (fboundp 'encode-coding-string) | |
261 | (defalias 'epa--encode-coding-string 'encode-coding-string) | |
262 | (defalias 'epa--encode-coding-string 'identity))) | |
263 | ||
264 | (eval-and-compile | |
265 | (if (fboundp 'decode-coding-string) | |
266 | (defalias 'epa--decode-coding-string 'decode-coding-string) | |
267 | (defalias 'epa--decode-coding-string 'identity))) | |
268 | ||
269 | (defun epa-key-list-mode () | |
270 | "Major mode for `epa-list-keys'." | |
271 | (kill-all-local-variables) | |
272 | (buffer-disable-undo) | |
273 | (setq major-mode 'epa-key-list-mode | |
274 | mode-name "Keys" | |
275 | truncate-lines t | |
276 | buffer-read-only t) | |
277 | (use-local-map epa-key-list-mode-map) | |
278 | (make-local-variable 'font-lock-defaults) | |
279 | (setq font-lock-defaults '(epa-font-lock-keywords t)) | |
280 | ;; In XEmacs, auto-initialization of font-lock is not effective | |
281 | ;; if buffer-file-name is not set. | |
282 | (font-lock-set-defaults) | |
283 | (make-local-variable 'epa-exit-buffer-function) | |
284 | (make-local-variable 'revert-buffer-function) | |
285 | (setq revert-buffer-function 'epa--key-list-revert-buffer) | |
64cc2f2c | 286 | (run-mode-hooks 'epa-key-list-mode-hook)) |
c154c0be MO |
287 | |
288 | (defun epa-key-mode () | |
289 | "Major mode for a key description." | |
290 | (kill-all-local-variables) | |
291 | (buffer-disable-undo) | |
292 | (setq major-mode 'epa-key-mode | |
293 | mode-name "Key" | |
294 | truncate-lines t | |
295 | buffer-read-only t) | |
296 | (use-local-map epa-key-mode-map) | |
297 | (make-local-variable 'font-lock-defaults) | |
298 | (setq 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) | |
64cc2f2c | 303 | (run-mode-hooks 'epa-key-mode-hook)) |
c154c0be MO |
304 | |
305 | (defun epa-info-mode () | |
306 | "Major mode for `epa-info-buffer'." | |
307 | (kill-all-local-variables) | |
308 | (buffer-disable-undo) | |
309 | (setq major-mode 'epa-info-mode | |
310 | mode-name "Info" | |
311 | truncate-lines t | |
312 | buffer-read-only t) | |
313 | (use-local-map epa-info-mode-map) | |
64cc2f2c | 314 | (run-mode-hooks 'epa-info-mode-hook)) |
c154c0be MO |
315 | |
316 | (defun epa-mark-key (&optional arg) | |
317 | "Mark a key on the current line. | |
318 | If ARG is non-nil, unmark the key." | |
319 | (interactive "P") | |
320 | (let ((inhibit-read-only t) | |
321 | buffer-read-only | |
322 | properties) | |
323 | (beginning-of-line) | |
324 | (unless (get-text-property (point) 'epa-key) | |
325 | (error "No key on this line")) | |
326 | (setq properties (text-properties-at (point))) | |
327 | (delete-char 1) | |
328 | (insert (if arg " " "*")) | |
329 | (set-text-properties (1- (point)) (point) properties) | |
330 | (forward-line))) | |
331 | ||
332 | (defun epa-unmark-key (&optional arg) | |
333 | "Unmark a key on the current line. | |
334 | If ARG is non-nil, mark the key." | |
335 | (interactive "P") | |
336 | (epa-mark-key (not arg))) | |
337 | ||
338 | (defun epa-exit-buffer () | |
339 | "Exit the current buffer. | |
340 | `epa-exit-buffer-function' is called if it is set." | |
341 | (interactive) | |
342 | (funcall epa-exit-buffer-function)) | |
343 | ||
344 | (defun epa--insert-keys (keys) | |
345 | (save-excursion | |
346 | (save-restriction | |
347 | (narrow-to-region (point) (point)) | |
348 | (let (point) | |
349 | (while keys | |
350 | (setq point (point)) | |
351 | (insert " ") | |
352 | (add-text-properties point (point) | |
353 | (list 'epa-key (car keys) | |
354 | 'front-sticky nil | |
355 | 'rear-nonsticky t | |
356 | 'start-open t | |
357 | 'end-open t)) | |
358 | (widget-create 'epa-key :value (car keys)) | |
359 | (insert "\n") | |
f1914c40 | 360 | (setq keys (cdr keys)))) |
c154c0be MO |
361 | (add-text-properties (point-min) (point-max) |
362 | (list 'epa-list-keys t | |
363 | 'front-sticky nil | |
364 | 'rear-nonsticky t | |
365 | 'start-open t | |
366 | 'end-open t))))) | |
367 | ||
368 | (defun epa--list-keys (name secret) | |
369 | (unless (and epa-keys-buffer | |
370 | (buffer-live-p epa-keys-buffer)) | |
371 | (setq epa-keys-buffer (generate-new-buffer "*Keys*"))) | |
372 | (set-buffer epa-keys-buffer) | |
373 | (epa-key-list-mode) | |
374 | (let ((inhibit-read-only t) | |
375 | buffer-read-only | |
376 | (point (point-min)) | |
377 | (context (epg-make-context epa-protocol))) | |
378 | (unless (get-text-property point 'epa-list-keys) | |
379 | (setq point (next-single-property-change point 'epa-list-keys))) | |
380 | (when point | |
381 | (delete-region point | |
382 | (or (next-single-property-change point 'epa-list-keys) | |
383 | (point-max))) | |
384 | (goto-char point)) | |
385 | (epa--insert-keys (epg-list-keys context name secret)) | |
386 | (widget-setup) | |
387 | (set-keymap-parent (current-local-map) widget-keymap)) | |
388 | (make-local-variable 'epa-list-keys-arguments) | |
389 | (setq epa-list-keys-arguments (list name secret)) | |
390 | (goto-char (point-min)) | |
391 | (pop-to-buffer (current-buffer))) | |
392 | ||
393 | ;;;###autoload | |
394 | (defun epa-list-keys (&optional name) | |
395 | "List all keys matched with NAME from the public keyring." | |
396 | (interactive | |
397 | (if current-prefix-arg | |
398 | (let ((name (read-string "Pattern: " | |
399 | (if epa-list-keys-arguments | |
400 | (car epa-list-keys-arguments))))) | |
401 | (list (if (equal name "") nil name))) | |
402 | (list nil))) | |
403 | (epa--list-keys name nil)) | |
404 | ||
405 | ;;;###autoload | |
406 | (defun epa-list-secret-keys (&optional name) | |
407 | "List all keys matched with NAME from the private keyring." | |
408 | (interactive | |
409 | (if current-prefix-arg | |
410 | (let ((name (read-string "Pattern: " | |
411 | (if epa-list-keys-arguments | |
412 | (car epa-list-keys-arguments))))) | |
413 | (list (if (equal name "") nil name))) | |
414 | (list nil))) | |
415 | (epa--list-keys name t)) | |
416 | ||
74f50695 | 417 | (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm) |
c154c0be MO |
418 | (apply #'epa--list-keys epa-list-keys-arguments)) |
419 | ||
420 | (defun epa--marked-keys () | |
7fdbcd83 | 421 | (or (with-current-buffer epa-keys-buffer |
c154c0be MO |
422 | (goto-char (point-min)) |
423 | (let (keys key) | |
424 | (while (re-search-forward "^\\*" nil t) | |
425 | (if (setq key (get-text-property (match-beginning 0) | |
426 | 'epa-key)) | |
427 | (setq keys (cons key keys)))) | |
428 | (nreverse keys))) | |
9b026d9f GM |
429 | (let ((key (get-text-property (point-at-bol) 'epa-key))) |
430 | (if key | |
431 | (list key))))) | |
c154c0be MO |
432 | |
433 | (defun epa--select-keys (prompt keys) | |
7fdbcd83 SM |
434 | (unless (and epa-keys-buffer |
435 | (buffer-live-p epa-keys-buffer)) | |
436 | (setq epa-keys-buffer (generate-new-buffer "*Keys*"))) | |
437 | (with-current-buffer epa-keys-buffer | |
c154c0be | 438 | (epa-key-list-mode) |
2d562c0f DU |
439 | ;; C-c C-c is the usual way to finish the selection (bug#11159). |
440 | (define-key (current-local-map) "\C-c\C-c" 'exit-recursive-edit) | |
c154c0be MO |
441 | (let ((inhibit-read-only t) |
442 | buffer-read-only) | |
443 | (erase-buffer) | |
444 | (insert prompt "\n" | |
445 | (substitute-command-keys "\ | |
446 | - `\\[epa-mark-key]' to mark a key on the line | |
447 | - `\\[epa-unmark-key]' to unmark a key on the line\n")) | |
448 | (widget-create 'link | |
74f50695 | 449 | :notify (lambda (&rest _ignore) (abort-recursive-edit)) |
c154c0be MO |
450 | :help-echo |
451 | (substitute-command-keys | |
452 | "Click here or \\[abort-recursive-edit] to cancel") | |
453 | "Cancel") | |
454 | (widget-create 'link | |
74f50695 | 455 | :notify (lambda (&rest _ignore) (exit-recursive-edit)) |
c154c0be MO |
456 | :help-echo |
457 | (substitute-command-keys | |
458 | "Click here or \\[exit-recursive-edit] to finish") | |
459 | "OK") | |
460 | (insert "\n\n") | |
461 | (epa--insert-keys keys) | |
462 | (widget-setup) | |
463 | (set-keymap-parent (current-local-map) widget-keymap) | |
464 | (setq epa-exit-buffer-function #'abort-recursive-edit) | |
465 | (goto-char (point-min)) | |
397eb3f3 SM |
466 | (let ((display-buffer-mark-dedicated 'soft)) |
467 | (pop-to-buffer (current-buffer)))) | |
c154c0be MO |
468 | (unwind-protect |
469 | (progn | |
470 | (recursive-edit) | |
471 | (epa--marked-keys)) | |
c154c0be MO |
472 | (kill-buffer epa-keys-buffer)))) |
473 | ||
474 | ;;;###autoload | |
475 | (defun epa-select-keys (context prompt &optional names secret) | |
476 | "Display a user's keyring and ask him to select keys. | |
477 | CONTEXT is an epg-context. | |
478 | PROMPT is a string to prompt with. | |
479 | NAMES is a list of strings to be matched with keys. If it is nil, all | |
480 | the keys are listed. | |
481 | If SECRET is non-nil, list secret keys instead of public keys." | |
482 | (let ((keys (epg-list-keys context names secret))) | |
2c6c404a | 483 | (epa--select-keys prompt keys))) |
c154c0be MO |
484 | |
485 | (defun epa--show-key (key) | |
486 | (let* ((primary-sub-key (car (epg-key-sub-key-list key))) | |
487 | (entry (assoc (epg-sub-key-id primary-sub-key) | |
488 | epa-key-buffer-alist)) | |
489 | (inhibit-read-only t) | |
490 | buffer-read-only | |
491 | pointer) | |
492 | (unless entry | |
493 | (setq entry (cons (epg-sub-key-id primary-sub-key) nil) | |
494 | epa-key-buffer-alist (cons entry epa-key-buffer-alist))) | |
495 | (unless (and (cdr entry) | |
496 | (buffer-live-p (cdr entry))) | |
497 | (setcdr entry (generate-new-buffer | |
498 | (format "*Key*%s" (epg-sub-key-id primary-sub-key))))) | |
499 | (set-buffer (cdr entry)) | |
500 | (epa-key-mode) | |
501 | (make-local-variable 'epa-key) | |
502 | (setq epa-key key) | |
503 | (erase-buffer) | |
504 | (setq pointer (epg-key-user-id-list key)) | |
505 | (while pointer | |
506 | (if (car pointer) | |
507 | (insert " " | |
508 | (if (epg-user-id-validity (car pointer)) | |
509 | (char-to-string | |
510 | (car (rassq (epg-user-id-validity (car pointer)) | |
511 | epg-key-validity-alist))) | |
512 | " ") | |
513 | " " | |
514 | (if (stringp (epg-user-id-string (car pointer))) | |
515 | (epg-user-id-string (car pointer)) | |
516 | (epg-decode-dn (epg-user-id-string (car pointer)))) | |
517 | "\n")) | |
518 | (setq pointer (cdr pointer))) | |
519 | (setq pointer (epg-key-sub-key-list key)) | |
520 | (while pointer | |
521 | (insert " " | |
522 | (if (epg-sub-key-validity (car pointer)) | |
523 | (char-to-string | |
524 | (car (rassq (epg-sub-key-validity (car pointer)) | |
525 | epg-key-validity-alist))) | |
526 | " ") | |
527 | " " | |
528 | (epg-sub-key-id (car pointer)) | |
529 | " " | |
530 | (format "%dbits" | |
531 | (epg-sub-key-length (car pointer))) | |
532 | " " | |
533 | (cdr (assq (epg-sub-key-algorithm (car pointer)) | |
534 | epg-pubkey-algorithm-alist)) | |
535 | "\n\tCreated: " | |
536 | (condition-case nil | |
537 | (format-time-string "%Y-%m-%d" | |
538 | (epg-sub-key-creation-time (car pointer))) | |
539 | (error "????-??-??")) | |
540 | (if (epg-sub-key-expiration-time (car pointer)) | |
97c07afc DU |
541 | (format (if (time-less-p (current-time) |
542 | (epg-sub-key-expiration-time | |
543 | (car pointer))) | |
544 | "\n\tExpires: %s" | |
545 | "\n\tExpired: %s") | |
c154c0be MO |
546 | (condition-case nil |
547 | (format-time-string "%Y-%m-%d" | |
548 | (epg-sub-key-expiration-time | |
549 | (car pointer))) | |
550 | (error "????-??-??"))) | |
551 | "") | |
552 | "\n\tCapabilities: " | |
553 | (mapconcat #'symbol-name | |
554 | (epg-sub-key-capability (car pointer)) | |
555 | " ") | |
556 | "\n\tFingerprint: " | |
557 | (epg-sub-key-fingerprint (car pointer)) | |
558 | "\n") | |
559 | (setq pointer (cdr pointer))) | |
560 | (goto-char (point-min)) | |
561 | (pop-to-buffer (current-buffer)))) | |
562 | ||
563 | (defun epa-display-info (info) | |
564 | (if epa-popup-info-window | |
565 | (save-selected-window | |
566 | (unless (and epa-info-buffer (buffer-live-p epa-info-buffer)) | |
567 | (setq epa-info-buffer (generate-new-buffer "*Info*"))) | |
568 | (if (get-buffer-window epa-info-buffer) | |
569 | (delete-window (get-buffer-window epa-info-buffer))) | |
7fdbcd83 | 570 | (with-current-buffer epa-info-buffer |
c154c0be MO |
571 | (let ((inhibit-read-only t) |
572 | buffer-read-only) | |
573 | (erase-buffer) | |
574 | (insert info)) | |
575 | (epa-info-mode) | |
576 | (goto-char (point-min))) | |
577 | (if (> (window-height) | |
578 | epa-info-window-height) | |
579 | (set-window-buffer (split-window nil (- (window-height) | |
580 | epa-info-window-height)) | |
581 | epa-info-buffer) | |
582 | (pop-to-buffer epa-info-buffer) | |
583 | (if (> (window-height) epa-info-window-height) | |
584 | (shrink-window (- (window-height) epa-info-window-height))))) | |
585 | (message "%s" info))) | |
586 | ||
587 | (defun epa-display-verify-result (verify-result) | |
588 | (epa-display-info (epg-verify-result-to-string verify-result))) | |
5443c9b7 | 589 | (make-obsolete 'epa-display-verify-result 'epa-display-info "23.1") |
c154c0be MO |
590 | |
591 | (defun epa-passphrase-callback-function (context key-id handback) | |
592 | (if (eq key-id 'SYM) | |
7450df5d LMI |
593 | (read-passwd |
594 | (format "Passphrase for symmetric encryption%s: " | |
446b12da DU |
595 | ;; Add the file name to the prompt, if any. |
596 | (if (stringp handback) | |
597 | (format " for %s" handback) | |
598 | "")) | |
7450df5d | 599 | (eq (epg-context-operation context) 'encrypt)) |
c154c0be MO |
600 | (read-passwd |
601 | (if (eq key-id 'PIN) | |
602 | "Passphrase for PIN: " | |
603 | (let ((entry (assoc key-id epg-user-id-alist))) | |
604 | (if entry | |
605 | (format "Passphrase for %s %s: " key-id (cdr entry)) | |
606 | (format "Passphrase for %s: " key-id))))))) | |
607 | ||
74f50695 | 608 | (defun epa-progress-callback-function (_context what _char current total |
c154c0be | 609 | handback) |
9d5cb631 DU |
610 | (let ((prompt (or handback |
611 | (format "Processing %s: " what)))) | |
612 | ;; According to gnupg/doc/DETAIL: a "total" of 0 indicates that | |
613 | ;; the total amount is not known. The condition TOTAL && CUR == | |
614 | ;; TOTAL may be used to detect the end of an operation. | |
615 | (if (> total 0) | |
616 | (if (= current total) | |
617 | (message "%s...done" prompt) | |
618 | (message "%s...%d%%" prompt | |
619 | (floor (* (/ current (float total)) 100)))) | |
620 | (message "%s..." prompt)))) | |
c154c0be MO |
621 | |
622 | ;;;###autoload | |
623 | (defun epa-decrypt-file (file) | |
624 | "Decrypt FILE." | |
625 | (interactive "fFile: ") | |
626 | (setq file (expand-file-name file)) | |
627 | (let* ((default-name (file-name-sans-extension file)) | |
628 | (plain (expand-file-name | |
629 | (read-file-name | |
630 | (concat "To file (default " | |
631 | (file-name-nondirectory default-name) | |
632 | ") ") | |
633 | (file-name-directory default-name) | |
634 | default-name))) | |
635 | (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 file)))) | |
643 | (message "Decrypting %s..." (file-name-nondirectory file)) | |
644 | (epg-decrypt-file context file plain) | |
645 | (message "Decrypting %s...wrote %s" (file-name-nondirectory file) | |
646 | (file-name-nondirectory plain)) | |
647 | (if (epg-context-result-for context 'verify) | |
648 | (epa-display-info (epg-verify-result-to-string | |
649 | (epg-context-result-for context 'verify)))))) | |
650 | ||
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)))))) | |
670 | ||
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*" | |
7fdbcd83 | 682 | (with-current-buffer standard-output |
c154c0be MO |
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 | ")))) | |
689 | (t | |
50f13b3e DU |
690 | (setq type 'normal)))) |
691 | type)) | |
c154c0be MO |
692 | |
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)))) | |
734 | ||
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)))) | |
760 | ||
761 | ;;;###autoload | |
fe38beef | 762 | (defun epa-decrypt-region (start end &optional make-buffer-function) |
c154c0be | 763 | "Decrypt the current region between START and END. |
fe38beef RS |
764 | |
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. | |
c154c0be | 769 | |
3a99bf64 | 770 | Be careful about using this command in Lisp programs! |
2c6c404a MO |
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. | |
777 | ||
778 | For example: | |
779 | ||
780 | \(let ((context (epg-make-context 'OpenPGP))) | |
781 | (decode-coding-string | |
782 | (epg-decrypt-string context (buffer-substring start end)) | |
783 | 'utf-8))" | |
c154c0be MO |
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 | |
42481bde DU |
800 | (get-text-property start 'epa-coding-system-used) |
801 | 'undecided))) | |
44fede4d RS |
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)) | |
3a99bf64 RS |
811 | (with-output-to-temp-buffer "*Temp*" |
812 | (set-buffer standard-output) | |
813 | (insert plain) | |
814 | (epa-info-mode)))) | |
c154c0be MO |
815 | (if (epg-context-result-for context 'verify) |
816 | (epa-display-info (epg-verify-result-to-string | |
817 | (epg-context-result-for context 'verify))))))) | |
818 | ||
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)) | |
3a99bf64 | 823 | ;; Find the first coding system which corresponds to MIME-CHARSET. |
c154c0be MO |
824 | (let ((pointer (coding-system-list))) |
825 | (while (and pointer | |
3a99bf64 RS |
826 | (not (eq (coding-system-get (car pointer) 'mime-charset) |
827 | mime-charset))) | |
c154c0be | 828 | (setq pointer (cdr pointer))) |
3a99bf64 | 829 | (car pointer)))) |
c154c0be MO |
830 | |
831 | ;;;###autoload | |
832 | (defun epa-decrypt-armor-in-region (start end) | |
833 | "Decrypt OpenPGP armors in the current region between START and END. | |
834 | ||
2c6c404a MO |
835 | Don't use this command in Lisp programs! |
836 | See the reason described in the `epa-decrypt-region' documentation." | |
c154c0be MO |
837 | (interactive "r") |
838 | (save-excursion | |
839 | (save-restriction | |
840 | (narrow-to-region start end) | |
841 | (goto-char start) | |
842 | (let (armor-start armor-end) | |
843 | (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t) | |
844 | (setq armor-start (match-beginning 0) | |
845 | armor-end (re-search-forward "^-----END PGP MESSAGE-----$" | |
846 | nil t)) | |
847 | (unless armor-end | |
3a99bf64 | 848 | (error "Encryption armor beginning has no matching end")) |
c154c0be MO |
849 | (goto-char armor-start) |
850 | (let ((coding-system-for-read | |
851 | (or coding-system-for-read | |
852 | (if (re-search-forward "^Charset: \\(.*\\)" armor-end t) | |
853 | (epa--find-coding-system-for-mime-charset | |
854 | (intern (downcase (match-string 1)))))))) | |
855 | (goto-char armor-end) | |
856 | (epa-decrypt-region armor-start armor-end))))))) | |
857 | ||
858 | ;;;###autoload | |
859 | (defun epa-verify-region (start end) | |
860 | "Verify the current region between START and END. | |
861 | ||
2c6c404a MO |
862 | Don't use this command in Lisp programs! |
863 | Since this function operates on regions, it does some tricks such | |
864 | as coding-system detection and unibyte/multibyte conversion. If | |
865 | you are sure how the data in the region should be treated, you | |
866 | should consider using the string based counterpart | |
867 | `epg-verify-string', or the file based counterpart | |
868 | `epg-verify-file' instead. | |
869 | ||
870 | For example: | |
871 | ||
872 | \(let ((context (epg-make-context 'OpenPGP))) | |
873 | (decode-coding-string | |
874 | (epg-verify-string context (buffer-substring start end)) | |
875 | 'utf-8))" | |
c154c0be MO |
876 | (interactive "r") |
877 | (let ((context (epg-make-context epa-protocol)) | |
878 | plain) | |
879 | (epg-context-set-progress-callback context | |
880 | (cons | |
881 | #'epa-progress-callback-function | |
882 | "Verifying...")) | |
883 | (message "Verifying...") | |
884 | (setq plain (epg-verify-string | |
885 | context | |
886 | (epa--encode-coding-string | |
887 | (buffer-substring start end) | |
888 | (or coding-system-for-write | |
889 | (get-text-property start 'epa-coding-system-used))))) | |
890 | (message "Verifying...done") | |
891 | (setq plain (epa--decode-coding-string | |
892 | plain | |
893 | (or coding-system-for-read | |
42481bde DU |
894 | (get-text-property start 'epa-coding-system-used) |
895 | 'undecided))) | |
c154c0be MO |
896 | (if (y-or-n-p "Replace the original text? ") |
897 | (let ((inhibit-read-only t) | |
898 | buffer-read-only) | |
899 | (delete-region start end) | |
900 | (goto-char start) | |
901 | (insert plain)) | |
902 | (with-output-to-temp-buffer "*Temp*" | |
903 | (set-buffer standard-output) | |
904 | (insert plain) | |
905 | (epa-info-mode))) | |
906 | (if (epg-context-result-for context 'verify) | |
907 | (epa-display-info (epg-verify-result-to-string | |
908 | (epg-context-result-for context 'verify)))))) | |
909 | ||
910 | ;;;###autoload | |
911 | (defun epa-verify-cleartext-in-region (start end) | |
912 | "Verify OpenPGP cleartext signed messages in the current region | |
913 | between START and END. | |
914 | ||
2c6c404a MO |
915 | Don't use this command in Lisp programs! |
916 | See the reason described in the `epa-verify-region' documentation." | |
c154c0be MO |
917 | (interactive "r") |
918 | (save-excursion | |
919 | (save-restriction | |
920 | (narrow-to-region start end) | |
921 | (goto-char start) | |
922 | (let (cleartext-start cleartext-end) | |
923 | (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$" | |
924 | nil t) | |
925 | (setq cleartext-start (match-beginning 0)) | |
926 | (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$" | |
927 | nil t) | |
928 | (error "Invalid cleartext signed message")) | |
929 | (setq cleartext-end (re-search-forward | |
930 | "^-----END PGP SIGNATURE-----$" | |
931 | nil t)) | |
932 | (unless cleartext-end | |
933 | (error "No cleartext tail")) | |
934 | (epa-verify-region cleartext-start cleartext-end)))))) | |
935 | ||
936 | (eval-and-compile | |
937 | (if (fboundp 'select-safe-coding-system) | |
938 | (defalias 'epa--select-safe-coding-system 'select-safe-coding-system) | |
74f50695 | 939 | (defun epa--select-safe-coding-system (_from _to) |
c154c0be MO |
940 | buffer-file-coding-system))) |
941 | ||
942 | ;;;###autoload | |
943 | (defun epa-sign-region (start end signers mode) | |
944 | "Sign the current region between START and END by SIGNERS keys selected. | |
945 | ||
2c6c404a MO |
946 | Don't use this command in Lisp programs! |
947 | Since this function operates on regions, it does some tricks such | |
948 | as coding-system detection and unibyte/multibyte conversion. If | |
949 | you are sure how the data should be treated, you should consider | |
950 | using the string based counterpart `epg-sign-string', or the file | |
951 | based counterpart `epg-sign-file' instead. | |
952 | ||
953 | For example: | |
954 | ||
955 | \(let ((context (epg-make-context 'OpenPGP))) | |
956 | (epg-sign-string | |
957 | context | |
958 | (encode-coding-string (buffer-substring start end) 'utf-8)))" | |
c154c0be MO |
959 | (interactive |
960 | (let ((verbose current-prefix-arg)) | |
961 | (setq epa-last-coding-system-specified | |
962 | (or coding-system-for-write | |
963 | (epa--select-safe-coding-system | |
964 | (region-beginning) (region-end)))) | |
965 | (list (region-beginning) (region-end) | |
966 | (if verbose | |
967 | (epa-select-keys (epg-make-context epa-protocol) | |
968 | "Select keys for signing. | |
969 | If no one is selected, default secret key is used. " | |
970 | nil t)) | |
971 | (if verbose | |
972 | (epa--read-signature-type) | |
973 | 'clear)))) | |
974 | (save-excursion | |
975 | (let ((context (epg-make-context epa-protocol)) | |
976 | signature) | |
977 | ;;(epg-context-set-armor context epa-armor) | |
978 | (epg-context-set-armor context t) | |
979 | ;;(epg-context-set-textmode context epa-textmode) | |
980 | (epg-context-set-textmode context t) | |
981 | (epg-context-set-signers context signers) | |
982 | (epg-context-set-passphrase-callback context | |
983 | #'epa-passphrase-callback-function) | |
984 | (epg-context-set-progress-callback context | |
985 | (cons | |
986 | #'epa-progress-callback-function | |
987 | "Signing...")) | |
988 | (message "Signing...") | |
989 | (setq signature (epg-sign-string context | |
990 | (epa--encode-coding-string | |
991 | (buffer-substring start end) | |
992 | epa-last-coding-system-specified) | |
993 | mode)) | |
994 | (message "Signing...done") | |
995 | (delete-region start end) | |
996 | (goto-char start) | |
997 | (add-text-properties (point) | |
998 | (progn | |
999 | (insert (epa--decode-coding-string | |
1000 | signature | |
1001 | (or coding-system-for-read | |
1002 | epa-last-coding-system-specified))) | |
1003 | (point)) | |
1004 | (list 'epa-coding-system-used | |
1005 | epa-last-coding-system-specified | |
1006 | 'front-sticky nil | |
1007 | 'rear-nonsticky t | |
1008 | 'start-open t | |
1009 | 'end-open t))))) | |
1010 | ||
1011 | (eval-and-compile | |
1012 | (if (fboundp 'derived-mode-p) | |
1013 | (defalias 'epa--derived-mode-p 'derived-mode-p) | |
1014 | (defun epa--derived-mode-p (&rest modes) | |
1015 | "Non-nil if the current major mode is derived from one of MODES. | |
1016 | Uses the `derived-mode-parent' property of the symbol to trace backwards." | |
1017 | (let ((parent major-mode)) | |
1018 | (while (and (not (memq parent modes)) | |
1019 | (setq parent (get parent 'derived-mode-parent)))) | |
1020 | parent)))) | |
1021 | ||
1022 | ;;;###autoload | |
1023 | (defun epa-encrypt-region (start end recipients sign signers) | |
1024 | "Encrypt the current region between START and END for RECIPIENTS. | |
1025 | ||
2c6c404a MO |
1026 | Don't use this command in Lisp programs! |
1027 | Since this function operates on regions, it does some tricks such | |
1028 | as coding-system detection and unibyte/multibyte conversion. If | |
1029 | you are sure how the data should be treated, you should consider | |
1030 | using the string based counterpart `epg-encrypt-string', or the | |
1031 | file based counterpart `epg-encrypt-file' instead. | |
1032 | ||
1033 | For example: | |
1034 | ||
1035 | \(let ((context (epg-make-context 'OpenPGP))) | |
1036 | (epg-encrypt-string | |
1037 | context | |
1038 | (encode-coding-string (buffer-substring start end) 'utf-8) | |
1039 | nil))" | |
c154c0be MO |
1040 | (interactive |
1041 | (let ((verbose current-prefix-arg) | |
1042 | (context (epg-make-context epa-protocol)) | |
1043 | sign) | |
1044 | (setq epa-last-coding-system-specified | |
1045 | (or coding-system-for-write | |
1046 | (epa--select-safe-coding-system | |
1047 | (region-beginning) (region-end)))) | |
1048 | (list (region-beginning) (region-end) | |
1049 | (epa-select-keys context | |
1050 | "Select recipients for encryption. | |
1051 | If no one is selected, symmetric encryption will be performed. ") | |
1052 | (setq sign (if verbose (y-or-n-p "Sign? "))) | |
1053 | (if sign | |
1054 | (epa-select-keys context | |
1055 | "Select keys for signing. "))))) | |
1056 | (save-excursion | |
1057 | (let ((context (epg-make-context epa-protocol)) | |
1058 | cipher) | |
1059 | ;;(epg-context-set-armor context epa-armor) | |
1060 | (epg-context-set-armor context t) | |
1061 | ;;(epg-context-set-textmode context epa-textmode) | |
1062 | (epg-context-set-textmode context t) | |
1063 | (if sign | |
1064 | (epg-context-set-signers context signers)) | |
1065 | (epg-context-set-passphrase-callback context | |
1066 | #'epa-passphrase-callback-function) | |
1067 | (epg-context-set-progress-callback context | |
1068 | (cons | |
1069 | #'epa-progress-callback-function | |
1070 | "Encrypting...")) | |
1071 | (message "Encrypting...") | |
1072 | (setq cipher (epg-encrypt-string context | |
1073 | (epa--encode-coding-string | |
1074 | (buffer-substring start end) | |
1075 | epa-last-coding-system-specified) | |
1076 | recipients | |
1077 | sign)) | |
1078 | (message "Encrypting...done") | |
1079 | (delete-region start end) | |
1080 | (goto-char start) | |
1081 | (add-text-properties (point) | |
1082 | (progn | |
1083 | (insert cipher) | |
1084 | (point)) | |
1085 | (list 'epa-coding-system-used | |
1086 | epa-last-coding-system-specified | |
1087 | 'front-sticky nil | |
1088 | 'rear-nonsticky t | |
1089 | 'start-open t | |
1090 | 'end-open t))))) | |
1091 | ||
1092 | ;;;###autoload | |
1093 | (defun epa-delete-keys (keys &optional allow-secret) | |
2c6c404a | 1094 | "Delete selected KEYS." |
c154c0be MO |
1095 | (interactive |
1096 | (let ((keys (epa--marked-keys))) | |
1097 | (unless keys | |
1098 | (error "No keys selected")) | |
1099 | (list keys | |
1100 | (eq (nth 1 epa-list-keys-arguments) t)))) | |
1101 | (let ((context (epg-make-context epa-protocol))) | |
1102 | (message "Deleting...") | |
1103 | (epg-delete-keys context keys allow-secret) | |
1104 | (message "Deleting...done") | |
dc9b613e | 1105 | (apply #'epa--list-keys epa-list-keys-arguments))) |
c154c0be MO |
1106 | |
1107 | ;;;###autoload | |
1108 | (defun epa-import-keys (file) | |
2c6c404a | 1109 | "Import keys from FILE." |
c154c0be MO |
1110 | (interactive "fFile: ") |
1111 | (setq file (expand-file-name file)) | |
1112 | (let ((context (epg-make-context epa-protocol))) | |
1113 | (message "Importing %s..." (file-name-nondirectory file)) | |
1114 | (condition-case nil | |
1115 | (progn | |
1116 | (epg-import-keys-from-file context file) | |
1117 | (message "Importing %s...done" (file-name-nondirectory file))) | |
1118 | (error | |
1119 | (message "Importing %s...failed" (file-name-nondirectory file)))) | |
1120 | (if (epg-context-result-for context 'import) | |
1121 | (epa-display-info (epg-import-result-to-string | |
1122 | (epg-context-result-for context 'import)))) | |
1123 | (if (eq major-mode 'epa-key-list-mode) | |
dc9b613e | 1124 | (apply #'epa--list-keys epa-list-keys-arguments)))) |
c154c0be MO |
1125 | |
1126 | ;;;###autoload | |
1127 | (defun epa-import-keys-region (start end) | |
2c6c404a | 1128 | "Import keys from the region." |
c154c0be MO |
1129 | (interactive "r") |
1130 | (let ((context (epg-make-context epa-protocol))) | |
1131 | (message "Importing...") | |
1132 | (condition-case nil | |
1133 | (progn | |
1134 | (epg-import-keys-from-string context (buffer-substring start end)) | |
1135 | (message "Importing...done")) | |
1136 | (error | |
1137 | (message "Importing...failed"))) | |
1138 | (if (epg-context-result-for context 'import) | |
1139 | (epa-display-info (epg-import-result-to-string | |
1140 | (epg-context-result-for context 'import)))))) | |
1141 | ||
1142 | ;;;###autoload | |
1143 | (defun epa-import-armor-in-region (start end) | |
1144 | "Import keys in the OpenPGP armor format in the current region | |
2c6c404a | 1145 | between START and END." |
c154c0be MO |
1146 | (interactive "r") |
1147 | (save-excursion | |
1148 | (save-restriction | |
1149 | (narrow-to-region start end) | |
1150 | (goto-char start) | |
1151 | (let (armor-start armor-end) | |
1152 | (while (re-search-forward | |
1153 | "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$" | |
1154 | nil t) | |
1155 | (setq armor-start (match-beginning 0) | |
1156 | armor-end (re-search-forward | |
1157 | (concat "^-----END " (match-string 1) "-----$") | |
1158 | nil t)) | |
1159 | (unless armor-end | |
1160 | (error "No armor tail")) | |
1161 | (epa-import-keys-region armor-start armor-end)))))) | |
1162 | ||
1163 | ;;;###autoload | |
1164 | (defun epa-export-keys (keys file) | |
2c6c404a | 1165 | "Export selected KEYS to FILE." |
c154c0be MO |
1166 | (interactive |
1167 | (let ((keys (epa--marked-keys)) | |
1168 | default-name) | |
1169 | (unless keys | |
1170 | (error "No keys selected")) | |
1171 | (setq default-name | |
1172 | (expand-file-name | |
1173 | (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys)))) | |
1174 | (if epa-armor ".asc" ".gpg")) | |
1175 | default-directory)) | |
1176 | (list keys | |
1177 | (expand-file-name | |
1178 | (read-file-name | |
1179 | (concat "To file (default " | |
1180 | (file-name-nondirectory default-name) | |
1181 | ") ") | |
1182 | (file-name-directory default-name) | |
1183 | default-name))))) | |
1184 | (let ((context (epg-make-context epa-protocol))) | |
1185 | (epg-context-set-armor context epa-armor) | |
1186 | (message "Exporting to %s..." (file-name-nondirectory file)) | |
1187 | (epg-export-keys-to-file context keys file) | |
1188 | (message "Exporting to %s...done" (file-name-nondirectory file)))) | |
1189 | ||
1190 | ;;;###autoload | |
1191 | (defun epa-insert-keys (keys) | |
2c6c404a | 1192 | "Insert selected KEYS after the point." |
c154c0be MO |
1193 | (interactive |
1194 | (list (epa-select-keys (epg-make-context epa-protocol) | |
2d562c0f DU |
1195 | "Select keys to export. |
1196 | If no one is selected, default public key is exported. "))) | |
c154c0be MO |
1197 | (let ((context (epg-make-context epa-protocol))) |
1198 | ;;(epg-context-set-armor context epa-armor) | |
1199 | (epg-context-set-armor context t) | |
1200 | (insert (epg-export-keys-to-string context keys)))) | |
1201 | ||
1202 | ;; (defun epa-sign-keys (keys &optional local) | |
1203 | ;; "Sign selected KEYS. | |
1204 | ;; If a prefix-arg is specified, the signature is marked as non exportable. | |
1205 | ||
1206 | ;; Don't use this command in Lisp programs!" | |
1207 | ;; (interactive | |
1208 | ;; (let ((keys (epa--marked-keys))) | |
1209 | ;; (unless keys | |
1210 | ;; (error "No keys selected")) | |
1211 | ;; (list keys current-prefix-arg))) | |
1212 | ;; (let ((context (epg-make-context epa-protocol))) | |
1213 | ;; (epg-context-set-passphrase-callback context | |
1214 | ;; #'epa-passphrase-callback-function) | |
1215 | ;; (epg-context-set-progress-callback context | |
1216 | ;; (cons | |
1217 | ;; #'epa-progress-callback-function | |
1218 | ;; "Signing keys...")) | |
1219 | ;; (message "Signing keys...") | |
1220 | ;; (epg-sign-keys context keys local) | |
1221 | ;; (message "Signing keys...done"))) | |
1222 | ;; (make-obsolete 'epa-sign-keys "Do not use.") | |
1223 | ||
1224 | (provide 'epa) | |
1225 | ||
1226 | ;;; epa.el ends here |