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))
34 "The EasyPG Assistant"
37 (defcustom epa-protocol 'OpenPGP
38 "The default protocol."
39 :type '(choice (const :tag "OpenPGP" OpenPGP)
40 (const :tag "CMS" CMS))
43 (defcustom epa-armor nil
44 "If non-nil, epa commands create ASCII armored output."
48 (defcustom epa-textmode nil
49 "If non-nil, epa commands treat input files as text."
53 (defcustom epa-popup-info-window t
54 "If non-nil, status information from epa commands is displayed on
59 (defcustom epa-info-window-height 5
60 "Number of lines used to display status information."
64 (defcustom epa-mail-modes '(mail-mode message-mode)
65 "List of major-modes to compose mails."
69 (defgroup epa-faces nil
73 (defface epa-validity-high-face
74 '((((class color) (background dark))
75 (:foreground "PaleTurquoise" :bold t))
78 "Face used for displaying the high validity."
80 (defvar epa-validity-high-face 'epa-validity-high-face)
82 (defface epa-validity-medium-face
83 '((((class color) (background dark))
84 (:foreground "PaleTurquoise" :italic t))
87 "Face used for displaying the medium validity."
89 (defvar epa-validity-medium-face 'epa-validity-medium-face)
91 (defface epa-validity-low-face
94 "Face used for displaying the low validity."
96 (defvar epa-validity-low-face 'epa-validity-low-face)
98 (defface epa-validity-disabled-face
100 (:italic t :inverse-video t)))
101 "Face used for displaying the disabled validity."
103 (defvar epa-validity-disabled-face 'epa-validity-disabled-face)
105 (defface epa-string-face
108 (:foreground "lightyellow"))
111 (:foreground "blue4"))
114 "Face used for displaying the string."
116 (defvar epa-string-face 'epa-string-face)
118 (defface epa-mark-face
119 '((((class color) (background dark))
120 (:foreground "orange" :bold t))
122 (:foreground "red" :bold t)))
123 "Face used for displaying the high validity."
125 (defvar epa-mark-face 'epa-mark-face)
127 (defface epa-field-name-face
128 '((((class color) (background dark))
129 (:foreground "PaleTurquoise" :bold t))
131 "Face for the name of the attribute field."
133 (defvar epa-field-name-face 'epa-field-name-face)
135 (defface epa-field-body-face
136 '((((class color) (background dark))
137 (:foreground "turquoise" :italic t))
139 "Face for the body of the attribute field."
141 (defvar epa-field-body-face 'epa-field-body-face)
143 (defcustom epa-validity-face-alist
144 '((unknown . epa-validity-disabled-face)
145 (invalid . epa-validity-disabled-face)
146 (disabled . epa-validity-disabled-face)
147 (revoked . epa-validity-disabled-face)
148 (expired . epa-validity-disabled-face)
149 (none . epa-validity-low-face)
150 (undefined . epa-validity-low-face)
151 (never . epa-validity-low-face)
152 (marginal . epa-validity-medium-face)
153 (full . epa-validity-high-face)
154 (ultimate . epa-validity-high-face))
155 "An alist mapping validity values to faces."
159 (defcustom epa-font-lock-keywords
162 ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$"
163 (1 epa-field-name-face)
164 (2 epa-field-body-face)))
165 "Default expressions to addon in epa-mode."
166 :type '(repeat (list string))
169 (defconst epa-pubkey-algorithm-letter-alist
177 (defvar epa-keys-buffer nil)
178 (defvar epa-key-buffer-alist nil)
180 (defvar epa-list-keys-arguments nil)
181 (defvar epa-info-buffer nil)
182 (defvar epa-last-coding-system-specified nil)
184 (defvar epa-keys-mode-map
185 (let ((keymap (make-sparse-keymap)))
186 (define-key keymap "m" 'epa-mark)
187 (define-key keymap "u" 'epa-unmark)
188 (define-key keymap "d" 'epa-decrypt-file)
189 (define-key keymap "v" 'epa-verify-file)
190 (define-key keymap "s" 'epa-sign-file)
191 (define-key keymap "e" 'epa-encrypt-file)
192 (define-key keymap "r" 'epa-delete-keys)
193 (define-key keymap "i" 'epa-import-keys)
194 (define-key keymap "o" 'epa-export-keys)
195 (define-key keymap "g" 'epa-list-keys)
196 (define-key keymap "n" 'next-line)
197 (define-key keymap "p" 'previous-line)
198 (define-key keymap " " 'scroll-up)
199 (define-key keymap [delete] 'scroll-down)
200 (define-key keymap "q" 'epa-exit-buffer)
203 (defvar epa-key-mode-map
204 (let ((keymap (make-sparse-keymap)))
205 (define-key keymap "q" 'bury-buffer)
208 (defvar epa-info-mode-map
209 (let ((keymap (make-sparse-keymap)))
210 (define-key keymap "q" 'delete-window)
213 (defvar epa-exit-buffer-function #'bury-buffer)
215 (define-widget 'epa-key 'push-button
216 "Button for representing a epg-key object."
218 :button-face-get 'epa--key-widget-button-face-get
219 :value-create 'epa--key-widget-value-create
220 :action 'epa--key-widget-action
221 :help-echo 'epa--key-widget-help-echo)
223 (defun epa--key-widget-action (widget &optional event)
224 (epa--show-key (widget-get widget :value)))
226 (defun epa--key-widget-value-create (widget)
227 (let* ((key (widget-get widget :value))
228 (primary-sub-key (car (epg-key-sub-key-list key)))
229 (primary-user-id (car (epg-key-user-id-list key))))
230 (insert (format "%c "
231 (if (epg-sub-key-validity primary-sub-key)
232 (car (rassq (epg-sub-key-validity primary-sub-key)
233 epg-key-validity-alist))
235 (epg-sub-key-id primary-sub-key)
238 (if (stringp (epg-user-id-string primary-user-id))
239 (epg-user-id-string primary-user-id)
240 (epg-decode-dn (epg-user-id-string primary-user-id)))
243 (defun epa--key-widget-button-face-get (widget)
244 (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list
245 (widget-get widget :value))))))
247 (cdr (assq validity epa-validity-face-alist))
250 (defun epa--key-widget-help-echo (widget)
252 (epg-sub-key-id (car (epg-key-sub-key-list
253 (widget-get widget :value))))))
255 (if (fboundp 'encode-coding-string)
256 (defalias 'epa--encode-coding-string 'encode-coding-string)
257 (defalias 'epa--encode-coding-string 'identity))
259 (if (fboundp 'decode-coding-string)
260 (defalias 'epa--decode-coding-string 'decode-coding-string)
261 (defalias 'epa--decode-coding-string 'identity))
263 (defun epa-keys-mode ()
264 "Major mode for `epa-list-keys'."
265 (kill-all-local-variables)
266 (buffer-disable-undo)
267 (setq major-mode 'epa-keys-mode
271 (use-local-map epa-keys-mode-map)
272 (make-local-variable 'font-lock-defaults)
273 (setq font-lock-defaults '(epa-font-lock-keywords t))
274 ;; In XEmacs, auto-initialization of font-lock is not effective
275 ;; if buffer-file-name is not set.
276 (font-lock-set-defaults)
277 (make-local-variable 'epa-exit-buffer-function)
278 (run-hooks 'epa-keys-mode-hook))
280 (defun epa-key-mode ()
281 "Major mode for a key description."
282 (kill-all-local-variables)
283 (buffer-disable-undo)
284 (setq major-mode 'epa-key-mode
288 (use-local-map epa-key-mode-map)
289 (make-local-variable 'font-lock-defaults)
290 (setq font-lock-defaults '(epa-font-lock-keywords t))
291 ;; In XEmacs, auto-initialization of font-lock is not effective
292 ;; if buffer-file-name is not set.
293 (font-lock-set-defaults)
294 (make-local-variable 'epa-exit-buffer-function)
295 (run-hooks 'epa-key-mode-hook))
297 (defun epa-info-mode ()
298 "Major mode for `epa-info-buffer'."
299 (kill-all-local-variables)
300 (buffer-disable-undo)
301 (setq major-mode 'epa-info-mode
305 (use-local-map epa-info-mode-map)
306 (run-hooks 'epa-info-mode-hook))
308 (defun epa-mark (&optional arg)
309 "Mark the current line.
310 If ARG is non-nil, unmark the current line."
312 (let ((inhibit-read-only t)
316 (setq properties (text-properties-at (point)))
318 (insert (if arg " " "*"))
319 (set-text-properties (1- (point)) (point) properties)
322 (defun epa-unmark (&optional arg)
323 "Unmark the current line.
324 If ARG is non-nil, mark the current line."
326 (epa-mark (not arg)))
328 (defun epa-toggle-mark ()
329 "Toggle the mark the current line."
331 (epa-mark (eq (char-after (save-excursion (beginning-of-line) (point))) ?*)))
333 (defun epa-exit-buffer ()
334 "Exit the current buffer.
335 `epa-exit-buffer-function' is called if it is set."
337 (funcall epa-exit-buffer-function))
340 (defun epa-list-keys (&optional name mode)
341 "List all keys matched with NAME from the keyring.
342 If MODE is non-nil, it reads the private keyring. Otherwise, it
343 reads the public keyring."
345 (if current-prefix-arg
346 (let ((name (read-string "Pattern: "
347 (if epa-list-keys-arguments
348 (car epa-list-keys-arguments)))))
349 (list (if (equal name "") nil name)
350 (y-or-n-p "Secret keys? ")))
351 (or epa-list-keys-arguments (list nil nil))))
352 (unless (and epa-keys-buffer
353 (buffer-live-p epa-keys-buffer))
354 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
355 (set-buffer epa-keys-buffer)
356 (let ((inhibit-read-only t)
359 (context (epg-make-context epa-protocol)))
360 (unless (get-text-property point 'epa-list-keys)
361 (setq point (next-single-property-change point 'epa-list-keys)))
364 (or (next-single-property-change point 'epa-list-keys)
367 (epa--insert-keys context name mode)
370 (set-keymap-parent (current-local-map) widget-keymap))
371 (make-local-variable 'epa-list-keys-arguments)
372 (setq epa-list-keys-arguments (list name mode))
373 (goto-char (point-min))
374 (pop-to-buffer (current-buffer)))
376 (defun epa--insert-keys (context name mode)
379 (narrow-to-region (point) (point))
380 (let ((keys (epg-list-keys context name mode))
385 (add-text-properties point (point)
386 (list 'epa-key (car keys)
391 (widget-create 'epa-key :value (car keys))
393 (setq keys (cdr keys))))
394 (add-text-properties (point-min) (point-max)
395 (list 'epa-list-keys t
401 (defun epa--marked-keys ()
403 (set-buffer epa-keys-buffer)
404 (goto-char (point-min))
406 (while (re-search-forward "^\\*" nil t)
407 (if (setq key (get-text-property (match-beginning 0)
409 (setq keys (cons key keys))))
413 (let ((key (get-text-property (point) 'epa-key)))
418 (defun epa-select-keys (context prompt &optional names secret)
419 "Display a user's keyring and ask him to select keys.
420 CONTEXT is an epg-context.
421 PROMPT is a string to prompt with.
422 NAMES is a list of strings to be matched with keys. If it is nil, all
424 If SECRET is non-nil, list secret keys instead of public keys."
426 (unless (and epa-keys-buffer
427 (buffer-live-p epa-keys-buffer))
428 (setq epa-keys-buffer (generate-new-buffer "*Keys*")))
429 (let ((inhibit-read-only t)
431 (set-buffer epa-keys-buffer)
435 :notify (lambda (&rest ignore) (abort-recursive-edit))
437 (substitute-command-keys
438 "Click here or \\[abort-recursive-edit] to cancel")
441 :notify (lambda (&rest ignore) (exit-recursive-edit))
443 (substitute-command-keys
444 "Click here or \\[exit-recursive-edit] to finish")
449 (epa--insert-keys context (car names) secret)
450 (if (get-text-property (point) 'epa-list-keys)
452 (goto-char (point-max))
453 (setq names (cdr names)))
456 (epa--insert-keys context nil secret)
457 (if (get-text-property (point) 'epa-list-keys)
459 (epa--insert-keys context nil nil)))
462 (set-keymap-parent (current-local-map) widget-keymap)
463 (setq epa-exit-buffer-function #'abort-recursive-edit)
464 (goto-char (point-min))
465 (pop-to-buffer (current-buffer)))
470 (if (get-buffer-window epa-keys-buffer)
471 (delete-window (get-buffer-window epa-keys-buffer)))
472 (kill-buffer epa-keys-buffer))))
474 (defun epa--format-fingerprint-1 (fingerprint unit-size block-size)
478 (goto-char (point-min))
480 (goto-char (+ (point) unit-size))
482 (setq unit (1+ unit))
483 (insert (if (= (% unit block-size) 0) " " " ")))
486 (defun epa--format-fingerprint (fingerprint)
488 (if (= (length fingerprint) 40)
489 ;; 1234 5678 9ABC DEF0 1234 5678 9ABC DEF0 1234 5678
490 (epa--format-fingerprint-1 fingerprint 4 5)
491 ;; 12 34 56 78 9A BC DE F0 12 34 56 78 9A BC DE F0
492 (epa--format-fingerprint-1 fingerprint 2 8))))
494 (defun epa--show-key (key)
495 (let* ((primary-sub-key (car (epg-key-sub-key-list key)))
496 (entry (assoc (epg-sub-key-id primary-sub-key)
497 epa-key-buffer-alist))
498 (inhibit-read-only t)
502 (setq entry (cons (epg-sub-key-id primary-sub-key) nil)
503 epa-key-buffer-alist (cons entry epa-key-buffer-alist)))
504 (unless (and (cdr entry)
505 (buffer-live-p (cdr entry)))
506 (setcdr entry (generate-new-buffer
507 (format "*Key*%s" (epg-sub-key-id primary-sub-key)))))
508 (set-buffer (cdr entry))
509 (make-local-variable 'epa-key)
512 (setq pointer (epg-key-user-id-list key))
516 (if (epg-user-id-validity (car pointer))
518 (car (rassq (epg-user-id-validity (car pointer))
519 epg-key-validity-alist)))
522 (if (stringp (epg-user-id-string (car pointer)))
523 (epg-user-id-string (car pointer))
524 (epg-decode-dn (epg-user-id-string (car pointer))))
526 (setq pointer (cdr pointer)))
527 (setq pointer (epg-key-sub-key-list key))
530 (if (epg-sub-key-validity (car pointer))
532 (car (rassq (epg-sub-key-validity (car pointer))
533 epg-key-validity-alist)))
536 (epg-sub-key-id (car pointer))
539 (epg-sub-key-length (car pointer)))
541 (cdr (assq (epg-sub-key-algorithm (car pointer))
542 epg-pubkey-algorithm-alist))
544 (format-time-string "%Y-%m-%d"
545 (epg-sub-key-creation-time (car pointer)))
546 (if (epg-sub-key-expiration-time (car pointer))
547 (format "\n\tExpires: %s"
548 (format-time-string "%Y-%m-%d"
549 (epg-sub-key-expiration-time
553 (mapconcat #'symbol-name
554 (epg-sub-key-capability (car pointer))
557 (epa--format-fingerprint (epg-sub-key-fingerprint (car pointer)))
559 (setq pointer (cdr pointer)))
560 (goto-char (point-min))
561 (pop-to-buffer (current-buffer))
564 (defun epa-display-info (info)
565 (if epa-popup-info-window
566 (save-selected-window
567 (unless epa-info-buffer
568 (setq epa-info-buffer (generate-new-buffer "*Info*")))
569 (if (get-buffer-window epa-info-buffer)
570 (delete-window (get-buffer-window epa-info-buffer)))
572 (set-buffer epa-info-buffer)
573 (let ((inhibit-read-only t)
578 (goto-char (point-min)))
579 (if (> (window-height)
580 epa-info-window-height)
581 (set-window-buffer (split-window nil (- (window-height)
582 epa-info-window-height))
584 (pop-to-buffer epa-info-buffer)
585 (if (> (window-height) epa-info-window-height)
586 (shrink-window (- (window-height) epa-info-window-height)))))
587 (message "%s" info)))
589 (defun epa-display-verify-result (verify-result)
590 (epa-display-info (epg-verify-result-to-string verify-result)))
591 (make-obsolete 'epa-display-verify-result 'epa-display-info)
593 (defun epa-passphrase-callback-function (context key-id handback)
595 (read-passwd "Passphrase for symmetric encryption: "
596 (eq (epg-context-operation context) 'encrypt))
599 "Passphrase for PIN: "
600 (let ((entry (assoc key-id epg-user-id-alist)))
602 (format "Passphrase for %s %s: " key-id (cdr entry))
603 (format "Passphrase for %s: " key-id)))))))
605 (defun epa-progress-callback-function (context what char current total
607 (message "%s: %d%% (%d/%d)" what
608 (if (> total 0) (floor (* (/ current (float total)) 100)) 0)
612 (defun epa-decrypt-file (file)
614 (interactive "fFile: ")
615 (setq file (expand-file-name file))
616 (let* ((default-name (file-name-sans-extension file))
617 (plain (expand-file-name
619 (concat "To file (default "
620 (file-name-nondirectory default-name)
622 (file-name-directory default-name)
624 (context (epg-make-context epa-protocol)))
625 (epg-context-set-passphrase-callback context
626 #'epa-passphrase-callback-function)
627 (epg-context-set-progress-callback context
628 #'epa-progress-callback-function)
629 (message "Decrypting %s..." (file-name-nondirectory file))
630 (epg-decrypt-file context file plain)
631 (message "Decrypting %s...wrote %s" (file-name-nondirectory file)
632 (file-name-nondirectory plain))
633 (if (epg-context-result-for context 'verify)
634 (epa-display-info (epg-verify-result-to-string
635 (epg-context-result-for context 'verify))))))
638 (defun epa-verify-file (file)
640 (interactive "fFile: ")
641 (setq file (expand-file-name file))
642 (let* ((context (epg-make-context epa-protocol))
643 (plain (if (equal (file-name-extension file) "sig")
644 (file-name-sans-extension file))))
645 (epg-context-set-progress-callback context
646 #'epa-progress-callback-function)
647 (message "Verifying %s..." (file-name-nondirectory file))
648 (epg-verify-file context file plain)
649 (message "Verifying %s...done" (file-name-nondirectory file))
650 (if (epg-context-result-for context 'verify)
651 (epa-display-info (epg-verify-result-to-string
652 (epg-context-result-for context 'verify))))))
654 (defun epa--read-signature-type ()
657 (message "Signature type (n,c,d,?) ")
662 (setq type 'detached))
664 (with-output-to-temp-buffer "*Help*"
666 (set-buffer standard-output)
668 n - Create a normal signature
669 c - Create a cleartext signature
670 d - Create a detached signature
674 (setq type 'normal))))))
677 (defun epa-sign-file (file signers mode)
678 "Sign FILE by SIGNERS keys selected."
680 (list (expand-file-name (read-file-name "File: "))
681 (if current-prefix-arg
682 (epa-select-keys (epg-make-context epa-protocol)
683 "Select keys for signing.
684 If no one is selected, default secret key is used. "
686 (if current-prefix-arg
687 (epa--read-signature-type)
689 (let ((signature (concat file
690 (if (eq epa-protocol 'OpenPGP)
693 '(nil t normal detached))))
695 (if (memq mode '(t detached))
698 (if (memq mode '(t detached))
701 (context (epg-make-context epa-protocol)))
702 (epg-context-set-armor context epa-armor)
703 (epg-context-set-textmode context epa-textmode)
704 (epg-context-set-signers context signers)
705 (epg-context-set-passphrase-callback context
706 #'epa-passphrase-callback-function)
707 (epg-context-set-progress-callback context
708 #'epa-progress-callback-function)
709 (message "Signing %s..." (file-name-nondirectory file))
710 (epg-sign-file context file signature mode)
711 (message "Signing %s...wrote %s" (file-name-nondirectory file)
712 (file-name-nondirectory signature))))
715 (defun epa-encrypt-file (file recipients)
716 "Encrypt FILE for RECIPIENTS."
718 (list (expand-file-name (read-file-name "File: "))
719 (epa-select-keys (epg-make-context epa-protocol)
720 "Select recipients for encryption.
721 If no one is selected, symmetric encryption will be performed. ")))
722 (let ((cipher (concat file (if (eq epa-protocol 'OpenPGP)
723 (if epa-armor ".asc" ".gpg")
725 (context (epg-make-context epa-protocol)))
726 (epg-context-set-armor context epa-armor)
727 (epg-context-set-textmode context epa-textmode)
728 (epg-context-set-passphrase-callback context
729 #'epa-passphrase-callback-function)
730 (epg-context-set-progress-callback context
731 #'epa-progress-callback-function)
732 (message "Encrypting %s..." (file-name-nondirectory file))
733 (epg-encrypt-file context file recipients cipher)
734 (message "Encrypting %s...wrote %s" (file-name-nondirectory file)
735 (file-name-nondirectory cipher))))
738 (defun epa-decrypt-region (start end)
739 "Decrypt the current region between START and END.
741 Don't use this command in Lisp programs!"
744 (let ((context (epg-make-context epa-protocol))
746 (epg-context-set-passphrase-callback context
747 #'epa-passphrase-callback-function)
748 (epg-context-set-progress-callback context
749 #'epa-progress-callback-function)
750 (message "Decrypting...")
751 (setq plain (epg-decrypt-string context (buffer-substring start end)))
752 (message "Decrypting...done")
753 (setq plain (epa--decode-coding-string
755 (or coding-system-for-read
756 (get-text-property start 'epa-coding-system-used))))
757 (if (y-or-n-p "Replace the text in the region? ")
758 (let ((inhibit-read-only t)
760 (delete-region start end)
763 (let ((epa-popup-info-window t))
764 (epa-display-info plain)))
765 (if (epg-context-result-for context 'verify)
766 (epa-display-info (epg-verify-result-to-string
767 (epg-context-result-for context 'verify)))))))
770 (defun epa-decrypt-armor-in-region (start end)
771 "Decrypt OpenPGP armors in the current region between START and END.
773 Don't use this command in Lisp programs!"
777 (narrow-to-region start end)
779 (let (armor-start armor-end charset coding-system)
780 (while (re-search-forward "-----BEGIN PGP MESSAGE-----$" nil t)
781 (setq armor-start (match-beginning 0)
782 armor-end (re-search-forward "^-----END PGP MESSAGE-----$"
785 (error "No armor tail"))
786 (goto-char armor-start)
787 (if (re-search-forward "^Charset: \\(.*\\)" armor-end t)
788 (setq charset (match-string 1)))
789 (if coding-system-for-read
790 (setq coding-system coding-system-for-read)
792 (setq coding-system (intern (downcase charset)))
793 (setq coding-system 'utf-8)))
794 (let ((coding-system-for-read coding-system))
795 (epa-decrypt-region armor-start armor-end)))))))
798 (defun epa-decrypt ()
799 "Decrypt OpenPGP armors in the current buffer.
801 Don't use this command in Lisp programs!"
803 (epa-decrypt-armor-in-region (point-min) (point-max)))
805 (if (fboundp 'select-safe-coding-system)
806 (defalias 'epa--select-safe-coding-system 'select-safe-coding-system)
807 (defun epa--select-safe-coding-system (from to)
808 buffer-file-coding-system))
811 (defun epa-verify-region (start end)
812 "Verify the current region between START and END.
814 Don't use this command in Lisp programs!"
816 (let ((context (epg-make-context epa-protocol)))
817 (epg-context-set-progress-callback context
818 #'epa-progress-callback-function)
819 (epg-verify-string context
820 (epa--encode-coding-string
821 (buffer-substring start end)
822 (or coding-system-for-write
823 (get-text-property start
824 'epa-coding-system-used))))
825 (if (epg-context-result-for context 'verify)
826 (epa-display-info (epg-verify-result-to-string
827 (epg-context-result-for context 'verify))))))
830 (defun epa-verify-cleartext-in-region (start end)
831 "Verify OpenPGP cleartext signed messages in the current region
832 between START and END.
834 Don't use this command in Lisp programs!"
838 (narrow-to-region start end)
840 (let (armor-start armor-end)
841 (while (re-search-forward "-----BEGIN PGP SIGNED MESSAGE-----$"
843 (setq armor-start (match-beginning 0))
844 (unless (re-search-forward "^-----BEGIN PGP SIGNATURE-----$"
846 (error "Invalid cleartext signed message"))
847 (setq armor-end (re-search-forward
848 "^-----END PGP SIGNATURE-----$"
851 (error "No armor tail"))
852 (epa-verify-region armor-start armor-end))))))
856 "Verify OpenPGP cleartext signed messages in the current buffer.
858 Don't use this command in Lisp programs!"
860 (epa-verify-cleartext-in-region (point-min) (point-max)))
863 (defun epa-sign-region (start end signers mode)
864 "Sign the current region between START and END by SIGNERS keys selected.
866 Don't use this command in Lisp programs!"
869 (setq epa-last-coding-system-specified
870 (or coding-system-for-write
871 (epa--select-safe-coding-system
872 (region-beginning) (region-end))))
873 (list (region-beginning) (region-end)
874 (if current-prefix-arg
875 (epa-select-keys (epg-make-context epa-protocol)
876 "Select keys for signing.
877 If no one is selected, default secret key is used. "
879 (if current-prefix-arg
880 (epa--read-signature-type)
883 (let ((context (epg-make-context epa-protocol))
885 ;;(epg-context-set-armor context epa-armor)
886 (epg-context-set-armor context t)
887 ;;(epg-context-set-textmode context epa-textmode)
888 (epg-context-set-textmode context t)
889 (epg-context-set-signers context signers)
890 (epg-context-set-passphrase-callback context
891 #'epa-passphrase-callback-function)
892 (epg-context-set-progress-callback context
893 #'epa-progress-callback-function)
894 (message "Signing...")
895 (setq signature (epg-sign-string context
896 (epa--encode-coding-string
897 (buffer-substring start end)
898 epa-last-coding-system-specified)
900 (message "Signing...done")
901 (delete-region start end)
902 (add-text-properties (point)
904 (insert (epa--decode-coding-string
906 (or coding-system-for-read
907 epa-last-coding-system-specified)))
909 (list 'epa-coding-system-used
910 epa-last-coding-system-specified
916 (if (fboundp 'derived-mode-p)
917 (defalias 'epa--derived-mode-p 'derived-mode-p)
918 (defun epa--derived-mode-p (&rest modes)
919 "Non-nil if the current major mode is derived from one of MODES.
920 Uses the `derived-mode-parent' property of the symbol to trace backwards."
921 (let ((parent major-mode))
922 (while (and (not (memq parent modes))
923 (setq parent (get parent 'derived-mode-parent))))
926 (defun epa--mail-mode-p ()
927 (let ((pointer epa-mail-modes))
929 (epa--derived-mode-p (car pointer)))
930 (setq pointer (cdr pointer)))
934 (defun epa-sign (start end signers mode)
935 "Sign the current buffer.
937 Don't use this command in Lisp programs!"
940 (goto-char (point-min))
941 (if (and (epa--mail-mode-p)
942 (search-forward mail-header-separator nil t))
944 (setq epa-last-coding-system-specified
945 (or coding-system-for-write
946 (epa--select-safe-coding-system (point) (point-max))))
947 (list (point) (point-max)
948 (if current-prefix-arg
949 (epa-select-keys (epg-make-context epa-protocol)
950 "Select keys for signing.
951 If no one is selected, default secret key is used. "
953 (if current-prefix-arg
954 (epa--read-signature-type)
956 (epa-sign-region start end signers mode))
959 (defun epa-encrypt-region (start end recipients)
960 "Encrypt the current region between START and END for RECIPIENTS.
962 Don't use this command in Lisp programs!"
965 (setq epa-last-coding-system-specified
966 (or coding-system-for-write
967 (epa--select-safe-coding-system
968 (region-beginning) (region-end))))
969 (list (region-beginning) (region-end)
970 (epa-select-keys (epg-make-context epa-protocol)
971 "Select recipients for encryption.
972 If no one is selected, symmetric encryption will be performed. "))))
974 (let ((context (epg-make-context epa-protocol))
976 ;;(epg-context-set-armor context epa-armor)
977 (epg-context-set-armor context t)
978 ;;(epg-context-set-textmode context epa-textmode)
979 (epg-context-set-textmode context t)
980 (epg-context-set-passphrase-callback context
981 #'epa-passphrase-callback-function)
982 (epg-context-set-progress-callback context
983 #'epa-progress-callback-function)
984 (message "Encrypting...")
985 (setq cipher (epg-encrypt-string context
986 (epa--encode-coding-string
987 (buffer-substring start end)
988 epa-last-coding-system-specified)
990 (message "Encrypting...done")
991 (delete-region start end)
992 (add-text-properties (point)
996 (list 'epa-coding-system-used
997 epa-last-coding-system-specified
1004 (defun epa-encrypt (start end recipients)
1005 "Encrypt the current buffer.
1007 Don't use this command in Lisp programs!"
1011 (goto-char (point-min))
1012 (when (epa--mail-mode-p)
1014 (narrow-to-region (point)
1016 (search-forward mail-header-separator nil 0)
1017 (match-beginning 0)))
1019 (mail-strip-quoted-names
1020 (mapconcat #'identity
1021 (nconc (mail-fetch-field "to" nil nil t)
1022 (mail-fetch-field "cc" nil nil t)
1023 (mail-fetch-field "bcc" nil nil t))
1026 (setq recipients (delete ""
1027 (split-string recipients "[ \t\n]+"))))
1028 (goto-char (point-min))
1029 (if (search-forward mail-header-separator nil t)
1031 (setq epa-last-coding-system-specified
1032 (or coding-system-for-write
1033 (epa--select-safe-coding-system (point) (point-max))))
1034 (list (point) (point-max)
1035 (if current-prefix-arg
1037 (epg-make-context epa-protocol)
1038 "Select recipients for encryption.
1039 If no one is selected, symmetric encryption will be performed. "
1047 (epg-make-context epa-protocol)
1048 (concat "<" recipient ">")))
1050 (epa-encrypt-region start end recipients))
1053 (defun epa-delete-keys (keys &optional allow-secret)
1054 "Delete selected KEYS.
1056 Don't use this command in Lisp programs!"
1058 (let ((keys (epa--marked-keys)))
1060 (error "No keys selected"))
1062 (eq (nth 1 epa-list-keys-arguments) t))))
1063 (let ((context (epg-make-context epa-protocol)))
1064 (message "Deleting...")
1065 (epg-delete-keys context keys allow-secret)
1066 (message "Deleting...done")
1067 (apply #'epa-list-keys epa-list-keys-arguments)))
1070 (defun epa-import-keys (file)
1071 "Import keys from FILE.
1073 Don't use this command in Lisp programs!"
1074 (interactive "fFile: ")
1075 (setq file (expand-file-name file))
1076 (let ((context (epg-make-context epa-protocol)))
1077 (message "Importing %s..." (file-name-nondirectory file))
1080 (epg-import-keys-from-file context file)
1081 (message "Importing %s...done" (file-name-nondirectory file)))
1083 (message "Importing %s...failed" (file-name-nondirectory file))))
1084 (if (epg-context-result-for context 'import)
1085 (epa-display-info (epg-import-result-to-string
1086 (epg-context-result-for context 'import))))
1087 (if (eq major-mode 'epa-keys-mode)
1088 (apply #'epa-list-keys epa-list-keys-arguments))))
1091 (defun epa-import-keys-region (start end)
1092 "Import keys from the region.
1094 Don't use this command in Lisp programs!"
1096 (let ((context (epg-make-context epa-protocol)))
1097 (message "Importing...")
1100 (epg-import-keys-from-string context (buffer-substring start end))
1101 (message "Importing...done"))
1103 (message "Importing...failed")))
1104 (if (epg-context-result-for context 'import)
1105 (epa-display-info (epg-import-result-to-string
1106 (epg-context-result-for context 'import))))))
1109 (defun epa-import-armor-in-region (start end)
1110 "Import keys in the OpenPGP armor format in the current region
1111 between START and END.
1113 Don't use this command in Lisp programs!"
1117 (narrow-to-region start end)
1119 (let (armor-start armor-end)
1120 (while (re-search-forward
1121 "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$"
1123 (setq armor-start (match-beginning 0)
1124 armor-end (re-search-forward
1125 (concat "^-----END " (match-string 1) "-----$")
1128 (error "No armor tail"))
1129 (epa-import-keys-region armor-start armor-end))))))
1132 (defun epa-import ()
1133 "Import keys in the OpenPGP armor format in the current buffer.
1135 Don't use this command in Lisp programs!"
1137 (epa-import-armor-in-region (point-min) (point-max)))
1140 (defun epa-export-keys (keys file)
1141 "Export selected KEYS to FILE.
1143 Don't use this command in Lisp programs!"
1145 (let ((keys (epa--marked-keys))
1148 (error "No keys selected"))
1151 (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys))))
1152 (if epa-armor ".asc" ".gpg"))
1157 (concat "To file (default "
1158 (file-name-nondirectory default-name)
1160 (file-name-directory default-name)
1162 (let ((context (epg-make-context epa-protocol)))
1163 (epg-context-set-armor context epa-armor)
1164 (message "Exporting to %s..." (file-name-nondirectory file))
1165 (epg-export-keys-to-file context keys file)
1166 (message "Exporting to %s...done" (file-name-nondirectory file))))
1169 (defun epa-insert-keys (keys)
1170 "Insert selected KEYS after the point.
1172 Don't use this command in Lisp programs!"
1174 (list (epa-select-keys (epg-make-context epa-protocol)
1175 "Select keys to export. ")))
1176 (let ((context (epg-make-context epa-protocol)))
1177 ;;(epg-context-set-armor context epa-armor)
1178 (epg-context-set-armor context t)
1179 (insert (epg-export-keys-to-string context keys))))
1182 (defun epa-sign-keys (keys &optional local)
1183 "Sign selected KEYS.
1184 If a prefix-arg is specified, the signature is marked as non exportable.
1186 Don't use this command in Lisp programs!"
1188 (let ((keys (epa--marked-keys)))
1190 (error "No keys selected"))
1191 (list keys current-prefix-arg)))
1192 (let ((context (epg-make-context epa-protocol)))
1193 (epg-context-set-passphrase-callback context
1194 #'epa-passphrase-callback-function)
1195 (epg-context-set-progress-callback context
1196 #'epa-progress-callback-function)
1197 (message "Signing keys...")
1198 (epg-sign-keys context keys local)
1199 (message "Signing keys...done")))
1200 (make-obsolete 'epa-sign-keys "Do not use.")
1204 ;;; epa.el ends here