1 ;;; epa.el --- the EasyPG Assistant
2 ;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs 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 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'wid-edit
))
31 "The EasyPG Assistant"
35 (defcustom epa-popup-info-window t
36 "If non-nil, status information from epa commands is displayed on
41 (defcustom epa-info-window-height
5
42 "Number of lines used to display status information."
46 (defgroup epa-faces nil
51 (defface epa-validity-high
52 `((((class color
) (background dark
))
53 (:foreground
"PaleTurquoise"
54 ,@(if (assq ':weight custom-face-attributes
)
58 (,@(if (assq ':weight custom-face-attributes
)
61 "Face used for displaying the high validity."
64 (defface epa-validity-medium
65 `((((class color
) (background dark
))
66 (:foreground
"PaleTurquoise"
67 ,@(if (assq ':slant custom-face-attributes
)
71 (,@(if (assq ':slant custom-face-attributes
)
74 "Face used for displaying the medium validity."
77 (defface epa-validity-low
79 (,@(if (assq ':slant custom-face-attributes
)
82 "Face used for displaying the low validity."
85 (defface epa-validity-disabled
87 (,@(if (assq ':slant custom-face-attributes
)
91 "Face used for displaying the disabled validity."
95 '((((class color
) (background dark
))
96 (:foreground
"lightyellow"))
97 (((class color
) (background light
))
98 (:foreground
"blue4")))
99 "Face used for displaying the string."
103 `((((class color
) (background dark
))
104 (:foreground
"orange"
105 ,@(if (assq ':weight custom-face-attributes
)
108 (((class color
) (background light
))
110 ,@(if (assq ':weight custom-face-attributes
)
114 (,@(if (assq ':weight custom-face-attributes
)
117 "Face used for displaying the high validity."
120 (defface epa-field-name
121 `((((class color
) (background dark
))
122 (:foreground
"PaleTurquoise"
123 ,@(if (assq ':weight custom-face-attributes
)
127 (,@(if (assq ':weight custom-face-attributes
)
130 "Face for the name of the attribute field."
133 (defface epa-field-body
134 `((((class color
) (background dark
))
135 (:foreground
"turquoise"
136 ,@(if (assq ':slant custom-face-attributes
)
140 (,@(if (assq ':slant custom-face-attributes
)
143 "Face for the body of the attribute field."
146 (defcustom epa-validity-face-alist
147 '((unknown . epa-validity-disabled
)
148 (invalid . epa-validity-disabled
)
149 (disabled . epa-validity-disabled
)
150 (revoked . epa-validity-disabled
)
151 (expired . epa-validity-disabled
)
152 (none . epa-validity-low
)
153 (undefined . epa-validity-low
)
154 (never . epa-validity-low
)
155 (marginal . epa-validity-medium
)
156 (full . epa-validity-high
)
157 (ultimate . epa-validity-high
))
158 "An alist mapping validity values to faces."
159 :type
'(repeat (cons symbol face
))
162 (defvar epa-font-lock-keywords
165 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
167 (2 'epa-field-body
)))
168 "Default expressions to addon in epa-mode.")
170 (defconst epa-pubkey-algorithm-letter-alist
178 (defvar epa-protocol
'OpenPGP
179 "*The default protocol.
180 The value can be either OpenPGP or CMS.
182 You should bind this variable with `let', but do not set it globally.")
184 (defvar epa-armor nil
185 "*If non-nil, epa commands create ASCII armored output.
187 You should bind this variable with `let', but do not set it globally.")
189 (defvar epa-textmode nil
190 "*If non-nil, epa commands treat input files as text.
192 You should bind this variable with `let', but do not set it globally.")
194 (defvar epa-keys-buffer nil
)
195 (defvar epa-key-buffer-alist nil
)
197 (defvar epa-list-keys-arguments nil
)
198 (defvar epa-info-buffer nil
)
199 (defvar epa-last-coding-system-specified nil
)
201 (defvar epa-key-list-mode-map
202 (let ((keymap (make-sparse-keymap))
203 (menu-map (make-sparse-keymap)))
204 (define-key keymap
"m" 'epa-mark-key
)
205 (define-key keymap
"u" 'epa-unmark-key
)
206 (define-key keymap
"d" 'epa-decrypt-file
)
207 (define-key keymap
"v" 'epa-verify-file
)
208 (define-key keymap
"s" 'epa-sign-file
)
209 (define-key keymap
"e" 'epa-encrypt-file
)
210 (define-key keymap
"r" 'epa-delete-keys
)
211 (define-key keymap
"i" 'epa-import-keys
)
212 (define-key keymap
"o" 'epa-export-keys
)
213 (define-key keymap
"g" 'revert-buffer
)
214 (define-key keymap
"n" 'next-line
)
215 (define-key keymap
"p" 'previous-line
)
216 (define-key keymap
" " 'scroll-up
)
217 (define-key keymap
[delete] 'scroll-down)
218 (define-key keymap "q" 'epa-exit-buffer)
219 (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map))
220 (define-key menu-map [epa-key-list-unmark-key]
221 '(menu-item "Unmark Key" epa-unmark-key
222 :help "Unmark a key"))
223 (define-key menu-map [epa-key-list-mark-key]
224 '(menu-item "Mark Key" epa-mark-key
226 (define-key menu-map [separator-epa-file] '(menu-item "--"))
227 (define-key menu-map [epa-verify-file]
228 '(menu-item "Verify File..." epa-verify-file
229 :help "Verify FILE"))
230 (define-key menu-map [epa-sign-file]
231 '(menu-item "Sign File..." epa-sign-file
232 :help "Sign FILE by SIGNERS keys selected"))
233 (define-key menu-map [epa-decrypt-file]
234 '(menu-item "Decrypt File..." epa-decrypt-file
235 :help "Decrypt FILE"))
236 (define-key menu-map [epa-encrypt-file]
237 '(menu-item "Encrypt File.." epa-encrypt-file
238 :help "Encrypt FILE for RECIPIENTS"))
239 (define-key menu-map [separator-epa-key-list] '(menu-item "--"))
240 (define-key menu-map [epa-key-list-delete-keys]
241 '(menu-item "Delete keys" epa-delete-keys
242 :help "Delete Marked Keys"))
243 (define-key menu-map [epa-key-list-import-keys]
244 '(menu-item "Import Keys" epa-import-keys
245 :help "Import keys from a file"))
246 (define-key menu-map [epa-key-list-export-keys]
247 '(menu-item "Export Keys" epa-export-keys
248 :help "Export marked keys to a file"))
251 (defvar epa-key-mode-map
252 (let ((keymap (make-sparse-keymap)))
253 (define-key keymap "q" 'epa-exit-buffer)
256 (defvar epa-info-mode-map
257 (let ((keymap (make-sparse-keymap)))
258 (define-key keymap "q" 'delete-window)
261 (defvar epa-exit-buffer-function #'bury-buffer)
263 (define-widget 'epa-key 'push-button
264 "Button for representing a epg-key object."
266 :button-face-get 'epa--key-widget-button-face-get
267 :value-create 'epa--key-widget-value-create
268 :action 'epa--key-widget-action
269 :help-echo 'epa--key-widget-help-echo)
271 (defun epa--key-widget-action (widget &optional event)
272 (epa--show-key (widget-get widget :value)))
274 (defun epa--key-widget-value-create (widget)
275 (let* ((key (widget-get widget :value))
276 (primary-sub-key (car (epg-key-sub-key-list key)))
277 (primary-user-id (car (epg-key-user-id-list key))))
278 (insert (format "%c "
279 (if (epg-sub-key-validity primary-sub-key)
280 (car (rassq (epg-sub-key-validity primary-sub-key)
281 epg-key-validity-alist))
283 (epg-sub-key-id primary-sub-key)
286 (if (stringp (epg-user-id-string primary-user-id))
287 (epg-user-id-string primary-user-id)
288 (epg-decode-dn (epg-user-id-string primary-user-id)))
291 (defun epa--key-widget-button-face-get (widget)
292 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
293 (widget-get widget :value))))))
295 (cdr (assq validity epa-validity-face-alist))
298 (defun epa--key-widget-help-echo (widget)
300 (epg-sub-key-id (car (epg-key-sub-key-list
301 (widget-get widget :value))))))
304 (if (fboundp 'encode-coding-string)
305 (defalias 'epa--encode-coding-string 'encode-coding-string)
306 (defalias 'epa--encode-coding-string 'identity)))
309 (if (fboundp 'decode-coding-string)
310 (defalias 'epa--decode-coding-string 'decode-coding-string)
311 (defalias 'epa--decode-coding-string 'identity)))
313 (defun epa-key-list-mode ()
314 "Major mode for `epa-list-keys'."
315 (kill-all-local-variables)
316 (buffer-disable-undo)
317 (setq major-mode 'epa-key-list-mode
321 (use-local-map epa-key-list-mode-map)
322 (make-local-variable 'font-lock-defaults)
323 (setq font-lock-defaults '(epa-font-lock-keywords t))
324 ;; In XEmacs, auto-initialization of font-lock is not effective
325 ;; if buffer-file-name is not set.
326 (font-lock-set-defaults)
327 (make-local-variable 'epa-exit-buffer-function)
328 (make-local-variable 'revert-buffer-function)
329 (setq revert-buffer-function 'epa--key-list-revert-buffer)
330 (run-hooks 'epa-key-list-mode-hook))
332 (defun epa-key-mode ()
333 "Major mode for a key description."
334 (kill-all-local-variables)
335 (buffer-disable-undo)
336 (setq major-mode 'epa-key-mode
340 (use-local-map epa-key-mode-map)
341 (make-local-variable 'font-lock-defaults)
342 (setq font-lock-defaults '(epa-font-lock-keywords t))
343 ;; In XEmacs, auto-initialization of font-lock is not effective
344 ;; if buffer-file-name is not set.
345 (font-lock-set-defaults)
346 (make-local-variable 'epa-exit-buffer-function)
347 (run-hooks 'epa-key-mode-hook))
349 (defun epa-info-mode ()
350 "Major mode for `epa-info-buffer'."
351 (kill-all-local-variables)
352 (buffer-disable-undo)
353 (setq major-mode 'epa-info-mode
357 (use-local-map epa-info-mode-map)
358 (run-hooks 'epa-info-mode-hook))
360 (defun epa-mark-key (&optional arg)
361 "Mark a key on the current line.
362 If ARG is non-nil, unmark the key."
364 (let ((inhibit-read-only t)
368 (unless (get-text-property (point) 'epa-key)
369 (error "No key on this line"))
370 (setq properties (text-properties-at (point)))
372 (insert (if arg " " "*"))
373 (set-text-properties (1- (point)) (point) properties)
376 (defun epa-unmark-key (&optional arg)
377 "Unmark a key on the current line.
378 If ARG is non-nil, mark the key."
380 (epa-mark-key (not arg)))
382 (defun epa-exit-buffer ()
383 "Exit the current buffer.
384 `epa-exit-buffer-function' is called if it is set."
386 (funcall epa-exit-buffer-function))
388 (defun epa--insert-keys (keys)
391 (narrow-to-region (point) (point))
396 (add-text-properties point (point)
397 (list 'epa-key (car keys)
402 (widget-create 'epa-key :value (car keys))
404 (setq keys (cdr keys))))
405 (add-text-properties (point-min) (point-max)
406 (list 'epa-list-keys t
412 (defun epa--list-keys (name secret)
413 (unless (and epa-keys-buffer
414 (buffer-live-p epa-keys-buffer))
415 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
416 (set-buffer epa-keys-buffer)
418 (let ((inhibit-read-only t)
421 (context (epg-make-context epa-protocol)))
422 (unless (get-text-property point 'epa-list-keys)
423 (setq point (next-single-property-change point 'epa-list-keys)))
426 (or (next-single-property-change point 'epa-list-keys)
429 (epa--insert-keys (epg-list-keys context name secret))
431 (set-keymap-parent (current-local-map) widget-keymap))
432 (make-local-variable 'epa-list-keys-arguments)
433 (setq epa-list-keys-arguments (list name secret))
434 (goto-char (point-min))
435 (pop-to-buffer (current-buffer)))
438 (defun epa-list-keys (&optional name)
439 "List all keys matched with NAME from the public keyring."
441 (if current-prefix-arg
442 (let ((name (read-string "Pattern: "
443 (if epa-list-keys-arguments
444 (car epa-list-keys-arguments)))))
445 (list (if (equal name "") nil name)))
447 (epa--list-keys name nil))
450 (defun epa-list-secret-keys (&optional name)
451 "List all keys matched with NAME from the private keyring."
453 (if current-prefix-arg
454 (let ((name (read-string "Pattern: "
455 (if epa-list-keys-arguments
456 (car epa-list-keys-arguments)))))
457 (list (if (equal name "") nil name)))
459 (epa--list-keys name t))
461 (defun epa--key-list-revert-buffer (&optional ignore-auto noconfirm)
462 (apply #'epa--list-keys epa-list-keys-arguments))
464 (defun epa--marked-keys ()
466 (set-buffer epa-keys-buffer)
467 (goto-char (point-min))
469 (while (re-search-forward "^\\*" nil t)
470 (if (setq key (get-text-property (match-beginning 0)
472 (setq keys (cons key keys))))
476 (let ((key (get-text-property (point) 'epa-key)))
480 (defun epa--select-keys (prompt keys)
482 (unless (and epa-keys-buffer
483 (buffer-live-p epa-keys-buffer))
484 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
485 (set-buffer epa-keys-buffer)
487 (let ((inhibit-read-only t)
491 (substitute-command-keys "\
492 - `\\[epa-mark-key]' to mark a key on the line
493 - `\\[epa-unmark-key]' to unmark a key on the line\n"))
495 :notify (lambda (&rest ignore) (abort-recursive-edit))
497 (substitute-command-keys
498 "Click here or \\[abort-recursive-edit] to cancel")
501 :notify (lambda (&rest ignore) (exit-recursive-edit))
503 (substitute-command-keys
504 "Click here or \\[exit-recursive-edit] to finish")
507 (epa--insert-keys keys)
509 (set-keymap-parent (current-local-map) widget-keymap)
510 (setq epa-exit-buffer-function #'abort-recursive-edit)
511 (goto-char (point-min))
512 (pop-to-buffer (current-buffer)))
517 (if (get-buffer-window epa-keys-buffer)
518 (delete-window (get-buffer-window epa-keys-buffer)))
519 (kill-buffer epa-keys-buffer))))
522 (defun epa-select-keys (context prompt &optional names secret)
523 "Display a user's keyring and ask him to select keys.
524 CONTEXT is an epg-context.
525 PROMPT is a string to prompt with.
526 NAMES is a list of strings to be matched with keys. If it is nil, all
528 If SECRET is non-nil, list secret keys instead of public keys."
529 (let ((keys (epg-list-keys context names secret)))
530 (epa--select-keys prompt keys)))
532 (defun epa--show-key (key)
533 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
534 (entry (assoc (epg-sub-key-id primary-sub-key)
535 epa-key-buffer-alist))
536 (inhibit-read-only t)
540 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
541 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
542 (unless (and (cdr entry)
543 (buffer-live-p (cdr entry)))
544 (setcdr entry (generate-new-buffer
545 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
546 (set-buffer (cdr entry))
548 (make-local-variable 'epa-key)
551 (setq pointer (epg-key-user-id-list key))
555 (if (epg-user-id-validity (car pointer))
557 (car (rassq (epg-user-id-validity (car pointer))
558 epg-key-validity-alist)))
561 (if (stringp (epg-user-id-string (car pointer)))
562 (epg-user-id-string (car pointer))
563 (epg-decode-dn (epg-user-id-string (car pointer))))
565 (setq pointer (cdr pointer)))
566 (setq pointer (epg-key-sub-key-list key))
569 (if (epg-sub-key-validity (car pointer))
571 (car (rassq (epg-sub-key-validity (car pointer))
572 epg-key-validity-alist)))
575 (epg-sub-key-id (car pointer))
578 (epg-sub-key-length (car pointer)))
580 (cdr (assq (epg-sub-key-algorithm (car pointer))
581 epg-pubkey-algorithm-alist))
584 (format-time-string "%Y-%m-%d"
585 (epg-sub-key-creation-time (car pointer)))
586 (error "????-??-??"))
587 (if (epg-sub-key-expiration-time (car pointer))
588 (format "\n\tExpires: %s"
590 (format-time-string "%Y-%m-%d"
591 (epg-sub-key-expiration-time
593 (error "????-??-??")))
596 (mapconcat #'symbol-name
597 (epg-sub-key-capability (car pointer))
600 (epg-sub-key-fingerprint (car pointer))
602 (setq pointer (cdr pointer)))
603 (goto-char (point-min))
604 (pop-to-buffer (current-buffer))))
606 (defun epa-display-info (info)
607 (if epa-popup-info-window
608 (save-selected-window
609 (unless (and epa-info-buffer (buffer-live-p epa-info-buffer))
610 (setq epa-info-buffer (generate-new-buffer "*Info*")))
611 (if (get-buffer-window epa-info-buffer)
612 (delete-window (get-buffer-window epa-info-buffer)))
614 (set-buffer epa-info-buffer)
615 (let ((inhibit-read-only t)
620 (goto-char (point-min)))
621 (if (> (window-height)
622 epa-info-window-height)
623 (set-window-buffer (split-window nil (- (window-height)
624 epa-info-window-height))
626 (pop-to-buffer epa-info-buffer)
627 (if (> (window-height) epa-info-window-height)
628 (shrink-window (- (window-height) epa-info-window-height)))))
629 (message "%s" info)))
631 (defun epa-display-verify-result (verify-result)
632 (epa-display-info (epg-verify-result-to-string verify-result)))
633 (make-obsolete 'epa-display-verify-result 'epa-display-info)
635 (defun epa-passphrase-callback-function (context key-id handback)
637 (read-passwd "Passphrase for symmetric encryption: "
638 (eq (epg-context-operation context) 'encrypt))
641 "Passphrase for PIN: "
642 (let ((entry (assoc key-id epg-user-id-alist)))
644 (format "Passphrase for %s %s: " key-id (cdr entry))
645 (format "Passphrase for %s: " key-id)))))))
647 (defun epa-progress-callback-function (context what char current total
649 (message "%s%d%% (%d/%d)" (or handback
651 (if (> total 0) (floor (* (/ current (float total)) 100)) 0)
655 (defun epa-decrypt-file (file)
657 (interactive "fFile: ")
658 (setq file (expand-file-name file))
659 (let* ((default-name (file-name-sans-extension file))
660 (plain (expand-file-name
662 (concat "To file (default "
663 (file-name-nondirectory default-name)
665 (file-name-directory default-name)
667 (context (epg-make-context epa-protocol)))
668 (epg-context-set-passphrase-callback context
669 #'epa-passphrase-callback-function)
670 (epg-context-set-progress-callback context
672 #'epa-progress-callback-function
673 (format "Decrypting %s..."
674 (file-name-nondirectory file))))
675 (message "Decrypting %s..." (file-name-nondirectory file))
676 (epg-decrypt-file context file plain)
677 (message "Decrypting %s...wrote %s" (file-name-nondirectory file)
678 (file-name-nondirectory plain))
679 (if (epg-context-result-for context 'verify)
680 (epa-display-info (epg-verify-result-to-string
681 (epg-context-result-for context 'verify))))))
684 (defun epa-verify-file (file)
686 (interactive "fFile: ")
687 (setq file (expand-file-name file))
688 (let* ((context (epg-make-context epa-protocol))
689 (plain (if (equal (file-name-extension file) "sig")
690 (file-name-sans-extension file))))
691 (epg-context-set-progress-callback context
693 #'epa-progress-callback-function
694 (format "Verifying %s..."
695 (file-name-nondirectory file))))
696 (message "Verifying %s..." (file-name-nondirectory file))
697 (epg-verify-file context file plain)
698 (message "Verifying %s...done" (file-name-nondirectory file))
699 (if (epg-context-result-for context 'verify)
700 (epa-display-info (epg-verify-result-to-string
701 (epg-context-result-for context 'verify))))))
703 (defun epa--read-signature-type ()
706 (message "Signature type (n,c,d,?) ")
711 (setq type 'detached))
713 (with-output-to-temp-buffer "*Help*"
715 (set-buffer standard-output)
717 n - Create a normal signature
718 c - Create a cleartext signature
719 d - Create a detached signature
723 (setq type 'normal))))))
726 (defun epa-sign-file (file signers mode)
727 "Sign FILE by SIGNERS keys selected."
729 (let ((verbose current-prefix-arg))
730 (list (expand-file-name (read-file-name "File: "))
732 (epa-select-keys (epg-make-context epa-protocol)
733 "Select keys for signing.
734 If no one is selected, default secret key is used. "
737 (epa--read-signature-type)
739 (let ((signature (concat file
740 (if (eq epa-protocol 'OpenPGP)
743 '(nil t normal detached))))
745 (if (memq mode '(t detached))
748 (if (memq mode '(t detached))
751 (context (epg-make-context epa-protocol)))
752 (epg-context-set-armor context epa-armor)
753 (epg-context-set-textmode context epa-textmode)
754 (epg-context-set-signers context signers)
755 (epg-context-set-passphrase-callback context
756 #'epa-passphrase-callback-function)
757 (epg-context-set-progress-callback context
759 #'epa-progress-callback-function
760 (format "Signing %s..."
761 (file-name-nondirectory file))))
762 (message "Signing %s..." (file-name-nondirectory file))
763 (epg-sign-file context file signature mode)
764 (message "Signing %s...wrote %s" (file-name-nondirectory file)
765 (file-name-nondirectory signature))))
768 (defun epa-encrypt-file (file recipients)
769 "Encrypt FILE for RECIPIENTS."
771 (list (expand-file-name (read-file-name "File: "))
772 (epa-select-keys (epg-make-context epa-protocol)
773 "Select recipients for encryption.
774 If no one is selected, symmetric encryption will be performed. ")))
775 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
776 (if epa-armor ".asc" ".gpg")
778 (context (epg-make-context epa-protocol)))
779 (epg-context-set-armor context epa-armor)
780 (epg-context-set-textmode context epa-textmode)
781 (epg-context-set-passphrase-callback context
782 #'epa-passphrase-callback-function)
783 (epg-context-set-progress-callback context
785 #'epa-progress-callback-function
786 (format "Encrypting %s..."
787 (file-name-nondirectory file))))
788 (message "Encrypting %s..." (file-name-nondirectory file))
789 (epg-encrypt-file context file recipients cipher)
790 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
791 (file-name-nondirectory cipher))))
794 (defun epa-decrypt-region (start end)
795 "Decrypt the current region between START and END.
797 Don't use this command in Lisp programs!
798 Since this function operates on regions, it does some tricks such
799 as coding-system detection and unibyte/multibyte conversion. If
800 you are sure how the data in the region should be treated, you
801 should consider using the string based counterpart
802 `epg-decrypt-string', or the file based counterpart
803 `epg-decrypt-file' instead.
807 \(let ((context (epg-make-context 'OpenPGP)))
808 (decode-coding-string
809 (epg-decrypt-string context (buffer-substring start end))
813 (let ((context (epg-make-context epa-protocol))
815 (epg-context-set-passphrase-callback context
816 #'epa-passphrase-callback-function)
817 (epg-context-set-progress-callback context
819 #'epa-progress-callback-function
821 (message "Decrypting...")
822 (setq plain (epg-decrypt-string context (buffer-substring start end)))
823 (message "Decrypting...done")
824 (setq plain (epa--decode-coding-string
826 (or coding-system-for-read
827 (get-text-property start 'epa-coding-system-used))))
828 (if (y-or-n-p "Replace the original text? ")
829 (let ((inhibit-read-only t)
831 (delete-region start end)
834 (with-output-to-temp-buffer "*Temp*"
835 (set-buffer standard-output)
838 (if (epg-context-result-for context 'verify)
839 (epa-display-info (epg-verify-result-to-string
840 (epg-context-result-for context 'verify)))))))
842 (defun epa--find-coding-system-for-mime-charset (mime-charset)
843 (if (featurep 'xemacs)
844 (if (fboundp 'find-coding-system)
845 (find-coding-system mime-charset))
846 (let ((pointer (coding-system-list)))
848 (eq (coding-system-get (car pointer) 'mime-charset)
850 (setq pointer (cdr pointer)))
854 (defun epa-decrypt-armor-in-region (start end)
855 "Decrypt OpenPGP armors in the current region between START and END.
857 Don't use this command in Lisp programs!
858 See the reason described in the `epa-decrypt-region' documentation."
862 (narrow-to-region start end)
864 (let (armor-start armor-end)
865 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
866 (setq armor-start (match-beginning 0)
867 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
870 (error "No armor tail"))
871 (goto-char armor-start)
872 (let ((coding-system-for-read
873 (or coding-system-for-read
874 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
875 (epa--find-coding-system-for-mime-charset
876 (intern (downcase (match-string 1))))))))
877 (goto-char armor-end)
878 (epa-decrypt-region armor-start armor-end)))))))
881 (defun epa-verify-region (start end)
882 "Verify the current region between START and END.
884 Don't use this command in Lisp programs!
885 Since this function operates on regions, it does some tricks such
886 as coding-system detection and unibyte/multibyte conversion. If
887 you are sure how the data in the region should be treated, you
888 should consider using the string based counterpart
889 `epg-verify-string', or the file based counterpart
890 `epg-verify-file' instead.
894 \(let ((context (epg-make-context 'OpenPGP)))
895 (decode-coding-string
896 (epg-verify-string context (buffer-substring start end))
899 (let ((context (epg-make-context epa-protocol))
901 (epg-context-set-progress-callback context
903 #'epa-progress-callback-function
905 (message "Verifying...")
906 (setq plain (epg-verify-string
908 (epa--encode-coding-string
909 (buffer-substring start end)
910 (or coding-system-for-write
911 (get-text-property start 'epa-coding-system-used)))))
912 (message "Verifying...done")
913 (setq plain (epa--decode-coding-string
915 (or coding-system-for-read
916 (get-text-property start 'epa-coding-system-used))))
917 (if (y-or-n-p "Replace the original text? ")
918 (let ((inhibit-read-only t)
920 (delete-region start end)
923 (with-output-to-temp-buffer "*Temp*"
924 (set-buffer standard-output)
927 (if (epg-context-result-for context 'verify)
928 (epa-display-info (epg-verify-result-to-string
929 (epg-context-result-for context 'verify))))))
932 (defun epa-verify-cleartext-in-region (start end)
933 "Verify OpenPGP cleartext signed messages in the current region
934 between START and END.
936 Don't use this command in Lisp programs!
937 See the reason described in the `epa-verify-region' documentation."
941 (narrow-to-region start end)
943 (let (cleartext-start cleartext-end)
944 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
946 (setq cleartext-start (match-beginning 0))
947 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
949 (error "Invalid cleartext signed message"))
950 (setq cleartext-end (re-search-forward
951 "^-----END PGP SIGNATURE-----$"
953 (unless cleartext-end
954 (error "No cleartext tail"))
955 (epa-verify-region cleartext-start cleartext-end))))))
958 (if (fboundp 'select-safe-coding-system)
959 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system)
960 (defun epa--select-safe-coding-system (from to)
961 buffer-file-coding-system)))
964 (defun epa-sign-region (start end signers mode)
965 "Sign the current region between START and END by SIGNERS keys selected.
967 Don't use this command in Lisp programs!
968 Since this function operates on regions, it does some tricks such
969 as coding-system detection and unibyte/multibyte conversion. If
970 you are sure how the data should be treated, you should consider
971 using the string based counterpart `epg-sign-string', or the file
972 based counterpart `epg-sign-file' instead.
976 \(let ((context (epg-make-context 'OpenPGP)))
979 (encode-coding-string (buffer-substring start end) 'utf-8)))"
981 (let ((verbose current-prefix-arg))
982 (setq epa-last-coding-system-specified
983 (or coding-system-for-write
984 (epa--select-safe-coding-system
985 (region-beginning) (region-end))))
986 (list (region-beginning) (region-end)
988 (epa-select-keys (epg-make-context epa-protocol)
989 "Select keys for signing.
990 If no one is selected, default secret key is used. "
993 (epa--read-signature-type)
996 (let ((context (epg-make-context epa-protocol))
998 ;;(epg-context-set-armor context epa-armor)
999 (epg-context-set-armor context t)
1000 ;;(epg-context-set-textmode context epa-textmode)
1001 (epg-context-set-textmode context t)
1002 (epg-context-set-signers context signers)
1003 (epg-context-set-passphrase-callback context
1004 #'epa-passphrase-callback-function)
1005 (epg-context-set-progress-callback context
1007 #'epa-progress-callback-function
1009 (message "Signing...")
1010 (setq signature (epg-sign-string context
1011 (epa--encode-coding-string
1012 (buffer-substring start end)
1013 epa-last-coding-system-specified)
1015 (message "Signing...done")
1016 (delete-region start end)
1018 (add-text-properties (point)
1020 (insert (epa--decode-coding-string
1022 (or coding-system-for-read
1023 epa-last-coding-system-specified)))
1025 (list 'epa-coding-system-used
1026 epa-last-coding-system-specified
1033 (if (fboundp 'derived-mode-p)
1034 (defalias 'epa--derived-mode-p 'derived-mode-p)
1035 (defun epa--derived-mode-p (&rest modes)
1036 "Non-nil if the current major mode is derived from one of MODES.
1037 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1038 (let ((parent major-mode))
1039 (while (and (not (memq parent modes))
1040 (setq parent (get parent 'derived-mode-parent))))
1044 (defun epa-encrypt-region (start end recipients sign signers)
1045 "Encrypt the current region between START and END for RECIPIENTS.
1047 Don't use this command in Lisp programs!
1048 Since this function operates on regions, it does some tricks such
1049 as coding-system detection and unibyte/multibyte conversion. If
1050 you are sure how the data should be treated, you should consider
1051 using the string based counterpart `epg-encrypt-string', or the
1052 file based counterpart `epg-encrypt-file' instead.
1056 \(let ((context (epg-make-context 'OpenPGP)))
1059 (encode-coding-string (buffer-substring start end) 'utf-8)
1062 (let ((verbose current-prefix-arg)
1063 (context (epg-make-context epa-protocol))
1065 (setq epa-last-coding-system-specified
1066 (or coding-system-for-write
1067 (epa--select-safe-coding-system
1068 (region-beginning) (region-end))))
1069 (list (region-beginning) (region-end)
1070 (epa-select-keys context
1071 "Select recipients for encryption.
1072 If no one is selected, symmetric encryption will be performed. ")
1073 (setq sign (if verbose (y-or-n-p "Sign? ")))
1075 (epa-select-keys context
1076 "Select keys for signing. ")))))
1078 (let ((context (epg-make-context epa-protocol))
1080 ;;(epg-context-set-armor context epa-armor)
1081 (epg-context-set-armor context t)
1082 ;;(epg-context-set-textmode context epa-textmode)
1083 (epg-context-set-textmode context t)
1085 (epg-context-set-signers context signers))
1086 (epg-context-set-passphrase-callback context
1087 #'epa-passphrase-callback-function)
1088 (epg-context-set-progress-callback context
1090 #'epa-progress-callback-function
1092 (message "Encrypting...")
1093 (setq cipher (epg-encrypt-string context
1094 (epa--encode-coding-string
1095 (buffer-substring start end)
1096 epa-last-coding-system-specified)
1099 (message "Encrypting...done")
1100 (delete-region start end)
1102 (add-text-properties (point)
1106 (list 'epa-coding-system-used
1107 epa-last-coding-system-specified
1114 (defun epa-delete-keys (keys &optional allow-secret)
1115 "Delete selected KEYS."
1117 (let ((keys (epa--marked-keys)))
1119 (error "No keys selected"))
1121 (eq (nth 1 epa-list-keys-arguments) t))))
1122 (let ((context (epg-make-context epa-protocol)))
1123 (message "Deleting...")
1124 (epg-delete-keys context keys allow-secret)
1125 (message "Deleting...done")
1126 (apply #'epa--list-keys epa-list-keys-arguments)))
1129 (defun epa-import-keys (file)
1130 "Import keys from FILE."
1131 (interactive "fFile: ")
1132 (setq file (expand-file-name file))
1133 (let ((context (epg-make-context epa-protocol)))
1134 (message "Importing %s..." (file-name-nondirectory file))
1137 (epg-import-keys-from-file context file)
1138 (message "Importing %s...done" (file-name-nondirectory file)))
1140 (message "Importing %s...failed" (file-name-nondirectory file))))
1141 (if (epg-context-result-for context 'import)
1142 (epa-display-info (epg-import-result-to-string
1143 (epg-context-result-for context 'import))))
1144 (if (eq major-mode 'epa-key-list-mode)
1145 (apply #'epa--list-keys epa-list-keys-arguments))))
1148 (defun epa-import-keys-region (start end)
1149 "Import keys from the region."
1151 (let ((context (epg-make-context epa-protocol)))
1152 (message "Importing...")
1155 (epg-import-keys-from-string context (buffer-substring start end))
1156 (message "Importing...done"))
1158 (message "Importing...failed")))
1159 (if (epg-context-result-for context 'import)
1160 (epa-display-info (epg-import-result-to-string
1161 (epg-context-result-for context 'import))))))
1164 (defun epa-import-armor-in-region (start end)
1165 "Import keys in the OpenPGP armor format in the current region
1166 between START and END."
1170 (narrow-to-region start end)
1172 (let (armor-start armor-end)
1173 (while (re-search-forward
1174 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1176 (setq armor-start (match-beginning 0)
1177 armor-end (re-search-forward
1178 (concat "^-----END " (match-string 1) "-----$")
1181 (error "No armor tail"))
1182 (epa-import-keys-region armor-start armor-end))))))
1185 (defun epa-export-keys (keys file)
1186 "Export selected KEYS to FILE."
1188 (let ((keys (epa--marked-keys))
1191 (error "No keys selected"))
1194 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1195 (if epa-armor ".asc" ".gpg"))
1200 (concat "To file (default "
1201 (file-name-nondirectory default-name)
1203 (file-name-directory default-name)
1205 (let ((context (epg-make-context epa-protocol)))
1206 (epg-context-set-armor context epa-armor)
1207 (message "Exporting to %s..." (file-name-nondirectory file))
1208 (epg-export-keys-to-file context keys file)
1209 (message "Exporting to %s...done" (file-name-nondirectory file))))
1212 (defun epa-insert-keys (keys)
1213 "Insert selected KEYS after the point."
1215 (list (epa-select-keys (epg-make-context epa-protocol)
1216 "Select keys to export. ")))
1217 (let ((context (epg-make-context epa-protocol)))
1218 ;;(epg-context-set-armor context epa-armor)
1219 (epg-context-set-armor context t)
1220 (insert (epg-export-keys-to-string context keys))))
1222 ;; (defun epa-sign-keys (keys &optional local)
1223 ;; "Sign selected KEYS.
1224 ;; If a prefix-arg is specified, the signature is marked as non exportable.
1226 ;; Don't use this command in Lisp programs!"
1228 ;; (let ((keys (epa--marked-keys)))
1230 ;; (error "No keys selected"))
1231 ;; (list keys current-prefix-arg)))
1232 ;; (let ((context (epg-make-context epa-protocol)))
1233 ;; (epg-context-set-passphrase-callback context
1234 ;; #'epa-passphrase-callback-function)
1235 ;; (epg-context-set-progress-callback context
1237 ;; #'epa-progress-callback-function
1238 ;; "Signing keys..."))
1239 ;; (message "Signing keys...")
1240 ;; (epg-sign-keys context keys local)
1241 ;; (message "Signing keys...done")))
1242 ;; (make-obsolete 'epa-sign-keys "Do not use.")
1246 ;; arch-tag: 38d20ced-20d5-4137-b17a-f206335423d7
1247 ;;; epa.el ends here