| 1 | ;;; epa.el --- the EasyPG Assistant -*- lexical-binding: t -*- |
| 2 | |
| 3 | ;; Copyright (C) 2006-2014 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Daiki Ueno <ueno@unixuser.org> |
| 6 | ;; Keywords: PGP, GnuPG |
| 7 | |
| 8 | ;; This file is part of GNU Emacs. |
| 9 | |
| 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 11 | ;; it under the terms of the GNU General Public License as published by |
| 12 | ;; the Free Software Foundation, either version 3 of the License, or |
| 13 | ;; (at your option) any later version. |
| 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 |
| 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 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" |
| 33 | :version "23.1" |
| 34 | :link '(custom-manual "(epa) Top") |
| 35 | :group 'epg) |
| 36 | |
| 37 | (defcustom epa-popup-info-window t |
| 38 | "If non-nil, display status information from epa commands in another 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." |
| 49 | :version "23.1" |
| 50 | :group 'epa) |
| 51 | |
| 52 | (defcustom epa-mail-aliases nil |
| 53 | "Alist of aliases of email addresses that stand for encryption keys. |
| 54 | Each element is a list of email addresses (ALIAS EXPANSIONS...). |
| 55 | When one of the recipients of a message being encrypted is ALIAS, |
| 56 | instead of encrypting it for ALIAS, encrypt it for EXPANSIONS... |
| 57 | |
| 58 | If EXPANSIONS is empty, ignore ALIAS as regards encryption. |
| 59 | This is a handy way to avoid warnings about addresses that you don't |
| 60 | have any key for. |
| 61 | |
| 62 | The command `epa-mail-encrypt' uses this." |
| 63 | :type '(repeat (cons (string :tag "Alias") (repeat (string :tag "Expansion")))) |
| 64 | :group 'epa |
| 65 | :version "24.4") |
| 66 | |
| 67 | (defface epa-validity-high |
| 68 | '((default :weight bold) |
| 69 | (((class color) (background dark)) :foreground "PaleTurquoise")) |
| 70 | "Face for high validity EPA information." |
| 71 | :group 'epa-faces) |
| 72 | |
| 73 | (defface epa-validity-medium |
| 74 | '((default :slant italic) |
| 75 | (((class color) (background dark)) :foreground "PaleTurquoise")) |
| 76 | "Face for medium validity EPA information." |
| 77 | :group 'epa-faces) |
| 78 | |
| 79 | (defface epa-validity-low |
| 80 | '((t :slant italic)) |
| 81 | "Face used for displaying the low validity." |
| 82 | :group 'epa-faces) |
| 83 | |
| 84 | (defface epa-validity-disabled |
| 85 | '((t :slant italic :inverse-video t)) |
| 86 | "Face used for displaying the disabled validity." |
| 87 | :group 'epa-faces) |
| 88 | |
| 89 | (defface epa-string |
| 90 | '((((class color) (background dark)) |
| 91 | :foreground "lightyellow") |
| 92 | (((class color) (background light)) |
| 93 | :foreground "blue4")) |
| 94 | "Face used for displaying the string." |
| 95 | :group 'epa-faces) |
| 96 | |
| 97 | (defface epa-mark |
| 98 | '((default :weight bold) |
| 99 | (((class color) (background dark)) :foreground "orange") |
| 100 | (((class color) (background light)) :foreground "red")) |
| 101 | "Face used for displaying the high validity." |
| 102 | :group 'epa-faces) |
| 103 | |
| 104 | (defface epa-field-name |
| 105 | '((default :weight bold) |
| 106 | (((class color) (background dark)) :foreground "PaleTurquoise")) |
| 107 | "Face for the name of the attribute field." |
| 108 | :group 'epa) |
| 109 | |
| 110 | (defface epa-field-body |
| 111 | '((default :slant italic) |
| 112 | (((class color) (background dark)) :foreground "turquoise")) |
| 113 | "Face for the body of the attribute field." |
| 114 | :group 'epa) |
| 115 | |
| 116 | (defcustom epa-validity-face-alist |
| 117 | '((unknown . epa-validity-disabled) |
| 118 | (invalid . epa-validity-disabled) |
| 119 | (disabled . epa-validity-disabled) |
| 120 | (revoked . epa-validity-disabled) |
| 121 | (expired . epa-validity-disabled) |
| 122 | (none . epa-validity-low) |
| 123 | (undefined . epa-validity-low) |
| 124 | (never . epa-validity-low) |
| 125 | (marginal . epa-validity-medium) |
| 126 | (full . epa-validity-high) |
| 127 | (ultimate . epa-validity-high)) |
| 128 | "An alist mapping validity values to faces." |
| 129 | :type '(repeat (cons symbol face)) |
| 130 | :group 'epa) |
| 131 | |
| 132 | (defvar epa-font-lock-keywords |
| 133 | '(("^\\*" |
| 134 | (0 'epa-mark)) |
| 135 | ("^\t\\([^\t:]+:\\)[ \t]*\\(.*\\)$" |
| 136 | (1 'epa-field-name) |
| 137 | (2 'epa-field-body))) |
| 138 | "Default expressions to addon in epa-mode.") |
| 139 | |
| 140 | (defconst epa-pubkey-algorithm-letter-alist |
| 141 | '((1 . ?R) |
| 142 | (2 . ?r) |
| 143 | (3 . ?s) |
| 144 | (16 . ?g) |
| 145 | (17 . ?D) |
| 146 | (20 . ?G))) |
| 147 | |
| 148 | (defvar epa-protocol 'OpenPGP |
| 149 | "The default protocol. |
| 150 | The value can be either OpenPGP or CMS. |
| 151 | |
| 152 | You should bind this variable with `let', but do not set it globally.") |
| 153 | |
| 154 | (defvar epa-armor nil |
| 155 | "If non-nil, epa commands create ASCII armored output. |
| 156 | |
| 157 | You should bind this variable with `let', but do not set it globally.") |
| 158 | |
| 159 | (defvar epa-textmode nil |
| 160 | "If non-nil, epa commands treat input files as text. |
| 161 | |
| 162 | You should bind this variable with `let', but do not set it globally.") |
| 163 | |
| 164 | (defvar epa-keys-buffer nil) |
| 165 | (defvar epa-key-buffer-alist nil) |
| 166 | (defvar epa-key nil) |
| 167 | (defvar epa-list-keys-arguments nil) |
| 168 | (defvar epa-info-buffer nil) |
| 169 | (defvar epa-last-coding-system-specified nil) |
| 170 | |
| 171 | (defvar epa-key-list-mode-map |
| 172 | (let ((keymap (make-sparse-keymap)) |
| 173 | (menu-map (make-sparse-keymap))) |
| 174 | (define-key keymap "m" 'epa-mark-key) |
| 175 | (define-key keymap "u" 'epa-unmark-key) |
| 176 | (define-key keymap "d" 'epa-decrypt-file) |
| 177 | (define-key keymap "v" 'epa-verify-file) |
| 178 | (define-key keymap "s" 'epa-sign-file) |
| 179 | (define-key keymap "e" 'epa-encrypt-file) |
| 180 | (define-key keymap "r" 'epa-delete-keys) |
| 181 | (define-key keymap "i" 'epa-import-keys) |
| 182 | (define-key keymap "o" 'epa-export-keys) |
| 183 | (define-key keymap "g" 'revert-buffer) |
| 184 | (define-key keymap "n" 'next-line) |
| 185 | (define-key keymap "p" 'previous-line) |
| 186 | (define-key keymap " " 'scroll-up-command) |
| 187 | (define-key keymap [?\S-\ ] 'scroll-down-command) |
| 188 | (define-key keymap [delete] 'scroll-down-command) |
| 189 | (define-key keymap "q" 'epa-exit-buffer) |
| 190 | (define-key keymap [menu-bar epa-key-list-mode] (cons "Keys" menu-map)) |
| 191 | (define-key menu-map [epa-key-list-unmark-key] |
| 192 | '(menu-item "Unmark Key" epa-unmark-key |
| 193 | :help "Unmark a key")) |
| 194 | (define-key menu-map [epa-key-list-mark-key] |
| 195 | '(menu-item "Mark Key" epa-mark-key |
| 196 | :help "Mark a key")) |
| 197 | (define-key menu-map [separator-epa-file] '(menu-item "--")) |
| 198 | (define-key menu-map [epa-verify-file] |
| 199 | '(menu-item "Verify File..." epa-verify-file |
| 200 | :help "Verify FILE")) |
| 201 | (define-key menu-map [epa-sign-file] |
| 202 | '(menu-item "Sign File..." epa-sign-file |
| 203 | :help "Sign FILE by SIGNERS keys selected")) |
| 204 | (define-key menu-map [epa-decrypt-file] |
| 205 | '(menu-item "Decrypt File..." epa-decrypt-file |
| 206 | :help "Decrypt FILE")) |
| 207 | (define-key menu-map [epa-encrypt-file] |
| 208 | '(menu-item "Encrypt File..." epa-encrypt-file |
| 209 | :help "Encrypt FILE for RECIPIENTS")) |
| 210 | (define-key menu-map [separator-epa-key-list] '(menu-item "--")) |
| 211 | (define-key menu-map [epa-key-list-delete-keys] |
| 212 | '(menu-item "Delete Keys" epa-delete-keys |
| 213 | :help "Delete Marked Keys")) |
| 214 | (define-key menu-map [epa-key-list-import-keys] |
| 215 | '(menu-item "Import Keys" epa-import-keys |
| 216 | :help "Import keys from a file")) |
| 217 | (define-key menu-map [epa-key-list-export-keys] |
| 218 | '(menu-item "Export Keys" epa-export-keys |
| 219 | :help "Export marked keys to a file")) |
| 220 | keymap)) |
| 221 | |
| 222 | (defvar epa-key-mode-map |
| 223 | (let ((keymap (make-sparse-keymap))) |
| 224 | (define-key keymap "q" 'epa-exit-buffer) |
| 225 | keymap)) |
| 226 | |
| 227 | (defvar epa-info-mode-map |
| 228 | (let ((keymap (make-sparse-keymap))) |
| 229 | (define-key keymap "q" 'delete-window) |
| 230 | keymap)) |
| 231 | |
| 232 | (defvar epa-exit-buffer-function #'bury-buffer) |
| 233 | |
| 234 | (define-widget 'epa-key 'push-button |
| 235 | "Button for representing a epg-key object." |
| 236 | :format "%[%v%]" |
| 237 | :button-face-get 'epa--key-widget-button-face-get |
| 238 | :value-create 'epa--key-widget-value-create |
| 239 | :action 'epa--key-widget-action |
| 240 | :help-echo 'epa--key-widget-help-echo) |
| 241 | |
| 242 | (defun epa--key-widget-action (widget &optional _event) |
| 243 | (save-selected-window |
| 244 | (epa--show-key (widget-get widget :value)))) |
| 245 | |
| 246 | (defun epa--key-widget-value-create (widget) |
| 247 | (let* ((key (widget-get widget :value)) |
| 248 | (primary-sub-key (car (epg-key-sub-key-list key))) |
| 249 | (primary-user-id (car (epg-key-user-id-list key)))) |
| 250 | (insert (format "%c " |
| 251 | (if (epg-sub-key-validity primary-sub-key) |
| 252 | (car (rassq (epg-sub-key-validity primary-sub-key) |
| 253 | epg-key-validity-alist)) |
| 254 | ? )) |
| 255 | (epg-sub-key-id primary-sub-key) |
| 256 | " " |
| 257 | (if primary-user-id |
| 258 | (if (stringp (epg-user-id-string primary-user-id)) |
| 259 | (epg-user-id-string primary-user-id) |
| 260 | (epg-decode-dn (epg-user-id-string primary-user-id))) |
| 261 | "")))) |
| 262 | |
| 263 | (defun epa--key-widget-button-face-get (widget) |
| 264 | (let ((validity (epg-sub-key-validity (car (epg-key-sub-key-list |
| 265 | (widget-get widget :value)))))) |
| 266 | (if validity |
| 267 | (cdr (assq validity epa-validity-face-alist)) |
| 268 | 'default))) |
| 269 | |
| 270 | (defun epa--key-widget-help-echo (widget) |
| 271 | (format "Show %s" |
| 272 | (epg-sub-key-id (car (epg-key-sub-key-list |
| 273 | (widget-get widget :value)))))) |
| 274 | |
| 275 | (defalias 'epa--encode-coding-string |
| 276 | (if (fboundp 'encode-coding-string) #'encode-coding-string #'identity)) |
| 277 | |
| 278 | (defalias 'epa--decode-coding-string |
| 279 | (if (fboundp 'decode-coding-string) #'decode-coding-string #'identity)) |
| 280 | |
| 281 | (define-derived-mode epa-key-list-mode special-mode "Keys" |
| 282 | "Major mode for `epa-list-keys'." |
| 283 | (buffer-disable-undo) |
| 284 | (setq truncate-lines t |
| 285 | buffer-read-only t) |
| 286 | (setq-local font-lock-defaults '(epa-font-lock-keywords t)) |
| 287 | ;; In XEmacs, auto-initialization of font-lock is not effective |
| 288 | ;; if buffer-file-name is not set. |
| 289 | (font-lock-set-defaults) |
| 290 | (make-local-variable 'epa-exit-buffer-function) |
| 291 | (setq-local revert-buffer-function #'epa--key-list-revert-buffer)) |
| 292 | |
| 293 | (define-derived-mode epa-key-mode special-mode "Key" |
| 294 | "Major mode for a key description." |
| 295 | (buffer-disable-undo) |
| 296 | (setq truncate-lines t |
| 297 | buffer-read-only t) |
| 298 | (setq-local 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)) |
| 303 | |
| 304 | (define-derived-mode epa-info-mode special-mode "Info" |
| 305 | "Major mode for `epa-info-buffer'." |
| 306 | (buffer-disable-undo) |
| 307 | (setq truncate-lines t |
| 308 | buffer-read-only t)) |
| 309 | |
| 310 | (defun epa-mark-key (&optional arg) |
| 311 | "Mark a key on the current line. |
| 312 | If ARG is non-nil, unmark the key." |
| 313 | (interactive "P") |
| 314 | (let ((inhibit-read-only t) |
| 315 | buffer-read-only |
| 316 | properties) |
| 317 | (beginning-of-line) |
| 318 | (unless (get-text-property (point) 'epa-key) |
| 319 | (error "No key on this line")) |
| 320 | (setq properties (text-properties-at (point))) |
| 321 | (delete-char 1) |
| 322 | (insert (if arg " " "*")) |
| 323 | (set-text-properties (1- (point)) (point) properties) |
| 324 | (forward-line))) |
| 325 | |
| 326 | (defun epa-unmark-key (&optional arg) |
| 327 | "Unmark a key on the current line. |
| 328 | If ARG is non-nil, mark the key." |
| 329 | (interactive "P") |
| 330 | (epa-mark-key (not arg))) |
| 331 | |
| 332 | (defun epa-exit-buffer () |
| 333 | "Exit the current buffer. |
| 334 | `epa-exit-buffer-function' is called if it is set." |
| 335 | (interactive) |
| 336 | (funcall epa-exit-buffer-function)) |
| 337 | |
| 338 | (defun epa--insert-keys (keys) |
| 339 | (save-excursion |
| 340 | (save-restriction |
| 341 | (narrow-to-region (point) (point)) |
| 342 | (let (point) |
| 343 | (while keys |
| 344 | (setq point (point)) |
| 345 | (insert " ") |
| 346 | (add-text-properties point (point) |
| 347 | (list 'epa-key (car keys) |
| 348 | 'front-sticky nil |
| 349 | 'rear-nonsticky t |
| 350 | 'start-open t |
| 351 | 'end-open t)) |
| 352 | (widget-create 'epa-key :value (car keys)) |
| 353 | (insert "\n") |
| 354 | (setq keys (cdr keys)))) |
| 355 | (add-text-properties (point-min) (point-max) |
| 356 | (list 'epa-list-keys t |
| 357 | 'front-sticky nil |
| 358 | 'rear-nonsticky t |
| 359 | 'start-open t |
| 360 | 'end-open t))))) |
| 361 | |
| 362 | (defun epa--list-keys (name secret) |
| 363 | (unless (and epa-keys-buffer |
| 364 | (buffer-live-p epa-keys-buffer)) |
| 365 | (setq epa-keys-buffer (generate-new-buffer "*Keys*"))) |
| 366 | (set-buffer epa-keys-buffer) |
| 367 | (epa-key-list-mode) |
| 368 | (let ((inhibit-read-only t) |
| 369 | buffer-read-only |
| 370 | (point (point-min)) |
| 371 | (context (epg-make-context epa-protocol))) |
| 372 | (unless (get-text-property point 'epa-list-keys) |
| 373 | (setq point (next-single-property-change point 'epa-list-keys))) |
| 374 | (when point |
| 375 | (delete-region point |
| 376 | (or (next-single-property-change point 'epa-list-keys) |
| 377 | (point-max))) |
| 378 | (goto-char point)) |
| 379 | (epa--insert-keys (epg-list-keys context name secret)) |
| 380 | (widget-setup) |
| 381 | (set-keymap-parent (current-local-map) widget-keymap)) |
| 382 | (make-local-variable 'epa-list-keys-arguments) |
| 383 | (setq epa-list-keys-arguments (list name secret)) |
| 384 | (goto-char (point-min)) |
| 385 | (pop-to-buffer (current-buffer))) |
| 386 | |
| 387 | ;;;###autoload |
| 388 | (defun epa-list-keys (&optional name) |
| 389 | "List all keys matched with NAME from the public keyring." |
| 390 | (interactive |
| 391 | (if current-prefix-arg |
| 392 | (let ((name (read-string "Pattern: " |
| 393 | (if epa-list-keys-arguments |
| 394 | (car epa-list-keys-arguments))))) |
| 395 | (list (if (equal name "") nil name))) |
| 396 | (list nil))) |
| 397 | (epa--list-keys name nil)) |
| 398 | |
| 399 | ;;;###autoload |
| 400 | (defun epa-list-secret-keys (&optional name) |
| 401 | "List all keys matched with NAME from the private keyring." |
| 402 | (interactive |
| 403 | (if current-prefix-arg |
| 404 | (let ((name (read-string "Pattern: " |
| 405 | (if epa-list-keys-arguments |
| 406 | (car epa-list-keys-arguments))))) |
| 407 | (list (if (equal name "") nil name))) |
| 408 | (list nil))) |
| 409 | (epa--list-keys name t)) |
| 410 | |
| 411 | (defun epa--key-list-revert-buffer (&optional _ignore-auto _noconfirm) |
| 412 | (apply #'epa--list-keys epa-list-keys-arguments)) |
| 413 | |
| 414 | (defun epa--marked-keys () |
| 415 | (or (with-current-buffer epa-keys-buffer |
| 416 | (goto-char (point-min)) |
| 417 | (let (keys key) |
| 418 | (while (re-search-forward "^\\*" nil t) |
| 419 | (if (setq key (get-text-property (match-beginning 0) |
| 420 | 'epa-key)) |
| 421 | (setq keys (cons key keys)))) |
| 422 | (nreverse keys))) |
| 423 | (let ((key (get-text-property (point-at-bol) 'epa-key))) |
| 424 | (if key |
| 425 | (list key))))) |
| 426 | |
| 427 | (defun epa--select-keys (prompt keys) |
| 428 | (unless (and epa-keys-buffer |
| 429 | (buffer-live-p epa-keys-buffer)) |
| 430 | (setq epa-keys-buffer (generate-new-buffer "*Keys*"))) |
| 431 | (with-current-buffer epa-keys-buffer |
| 432 | (epa-key-list-mode) |
| 433 | ;; C-c C-c is the usual way to finish the selection (bug#11159). |
| 434 | (define-key (current-local-map) "\C-c\C-c" 'exit-recursive-edit) |
| 435 | (let ((inhibit-read-only t) |
| 436 | buffer-read-only) |
| 437 | (erase-buffer) |
| 438 | (insert prompt "\n" |
| 439 | (substitute-command-keys "\ |
| 440 | - `\\[epa-mark-key]' to mark a key on the line |
| 441 | - `\\[epa-unmark-key]' to unmark a key on the line\n")) |
| 442 | (widget-create 'link |
| 443 | :notify (lambda (&rest _ignore) (abort-recursive-edit)) |
| 444 | :help-echo |
| 445 | (substitute-command-keys |
| 446 | "Click here or \\[abort-recursive-edit] to cancel") |
| 447 | "Cancel") |
| 448 | (widget-create 'link |
| 449 | :notify (lambda (&rest _ignore) (exit-recursive-edit)) |
| 450 | :help-echo |
| 451 | (substitute-command-keys |
| 452 | "Click here or \\[exit-recursive-edit] to finish") |
| 453 | "OK") |
| 454 | (insert "\n\n") |
| 455 | (epa--insert-keys keys) |
| 456 | (widget-setup) |
| 457 | (set-keymap-parent (current-local-map) widget-keymap) |
| 458 | (setq epa-exit-buffer-function #'abort-recursive-edit) |
| 459 | (goto-char (point-min)) |
| 460 | (let ((display-buffer-mark-dedicated 'soft)) |
| 461 | (pop-to-buffer (current-buffer)))) |
| 462 | (unwind-protect |
| 463 | (progn |
| 464 | (recursive-edit) |
| 465 | (epa--marked-keys)) |
| 466 | (kill-buffer epa-keys-buffer)))) |
| 467 | |
| 468 | ;;;###autoload |
| 469 | (defun epa-select-keys (context prompt &optional names secret) |
| 470 | "Display a user's keyring and ask him to select keys. |
| 471 | CONTEXT is an epg-context. |
| 472 | PROMPT is a string to prompt with. |
| 473 | NAMES is a list of strings to be matched with keys. If it is nil, all |
| 474 | the keys are listed. |
| 475 | If SECRET is non-nil, list secret keys instead of public keys." |
| 476 | (let ((keys (epg-list-keys context names secret))) |
| 477 | (epa--select-keys prompt keys))) |
| 478 | |
| 479 | (defun epa--show-key (key) |
| 480 | (let* ((primary-sub-key (car (epg-key-sub-key-list key))) |
| 481 | (entry (assoc (epg-sub-key-id primary-sub-key) |
| 482 | epa-key-buffer-alist)) |
| 483 | (inhibit-read-only t) |
| 484 | buffer-read-only |
| 485 | pointer) |
| 486 | (unless entry |
| 487 | (setq entry (cons (epg-sub-key-id primary-sub-key) nil) |
| 488 | epa-key-buffer-alist (cons entry epa-key-buffer-alist))) |
| 489 | (unless (and (cdr entry) |
| 490 | (buffer-live-p (cdr entry))) |
| 491 | (setcdr entry (generate-new-buffer |
| 492 | (format "*Key*%s" (epg-sub-key-id primary-sub-key))))) |
| 493 | (set-buffer (cdr entry)) |
| 494 | (epa-key-mode) |
| 495 | (make-local-variable 'epa-key) |
| 496 | (setq epa-key key) |
| 497 | (erase-buffer) |
| 498 | (setq pointer (epg-key-user-id-list key)) |
| 499 | (while pointer |
| 500 | (if (car pointer) |
| 501 | (insert " " |
| 502 | (if (epg-user-id-validity (car pointer)) |
| 503 | (char-to-string |
| 504 | (car (rassq (epg-user-id-validity (car pointer)) |
| 505 | epg-key-validity-alist))) |
| 506 | " ") |
| 507 | " " |
| 508 | (if (stringp (epg-user-id-string (car pointer))) |
| 509 | (epg-user-id-string (car pointer)) |
| 510 | (epg-decode-dn (epg-user-id-string (car pointer)))) |
| 511 | "\n")) |
| 512 | (setq pointer (cdr pointer))) |
| 513 | (setq pointer (epg-key-sub-key-list key)) |
| 514 | (while pointer |
| 515 | (insert " " |
| 516 | (if (epg-sub-key-validity (car pointer)) |
| 517 | (char-to-string |
| 518 | (car (rassq (epg-sub-key-validity (car pointer)) |
| 519 | epg-key-validity-alist))) |
| 520 | " ") |
| 521 | " " |
| 522 | (epg-sub-key-id (car pointer)) |
| 523 | " " |
| 524 | (format "%dbits" |
| 525 | (epg-sub-key-length (car pointer))) |
| 526 | " " |
| 527 | (cdr (assq (epg-sub-key-algorithm (car pointer)) |
| 528 | epg-pubkey-algorithm-alist)) |
| 529 | "\n\tCreated: " |
| 530 | (condition-case nil |
| 531 | (format-time-string "%Y-%m-%d" |
| 532 | (epg-sub-key-creation-time (car pointer))) |
| 533 | (error "????-??-??")) |
| 534 | (if (epg-sub-key-expiration-time (car pointer)) |
| 535 | (format (if (time-less-p (current-time) |
| 536 | (epg-sub-key-expiration-time |
| 537 | (car pointer))) |
| 538 | "\n\tExpires: %s" |
| 539 | "\n\tExpired: %s") |
| 540 | (condition-case nil |
| 541 | (format-time-string "%Y-%m-%d" |
| 542 | (epg-sub-key-expiration-time |
| 543 | (car pointer))) |
| 544 | (error "????-??-??"))) |
| 545 | "") |
| 546 | "\n\tCapabilities: " |
| 547 | (mapconcat #'symbol-name |
| 548 | (epg-sub-key-capability (car pointer)) |
| 549 | " ") |
| 550 | "\n\tFingerprint: " |
| 551 | (epg-sub-key-fingerprint (car pointer)) |
| 552 | "\n") |
| 553 | (setq pointer (cdr pointer))) |
| 554 | (goto-char (point-min)) |
| 555 | (pop-to-buffer (current-buffer)))) |
| 556 | |
| 557 | (defun epa-display-info (info) |
| 558 | (if epa-popup-info-window |
| 559 | (save-selected-window |
| 560 | (unless (and epa-info-buffer (buffer-live-p epa-info-buffer)) |
| 561 | (setq epa-info-buffer (generate-new-buffer "*Info*"))) |
| 562 | (if (get-buffer-window epa-info-buffer) |
| 563 | (delete-window (get-buffer-window epa-info-buffer))) |
| 564 | (with-current-buffer epa-info-buffer |
| 565 | (let ((inhibit-read-only t) |
| 566 | buffer-read-only) |
| 567 | (erase-buffer) |
| 568 | (insert info)) |
| 569 | (epa-info-mode) |
| 570 | (goto-char (point-min))) |
| 571 | (if (> (window-height) |
| 572 | epa-info-window-height) |
| 573 | (set-window-buffer (split-window nil (- (window-height) |
| 574 | epa-info-window-height)) |
| 575 | epa-info-buffer) |
| 576 | (pop-to-buffer epa-info-buffer) |
| 577 | (if (> (window-height) epa-info-window-height) |
| 578 | (shrink-window (- (window-height) epa-info-window-height))))) |
| 579 | (message "%s" info))) |
| 580 | |
| 581 | (defun epa-display-verify-result (verify-result) |
| 582 | (declare (obsolete epa-display-info "23.1")) |
| 583 | (epa-display-info (epg-verify-result-to-string verify-result))) |
| 584 | |
| 585 | (defun epa-passphrase-callback-function (context key-id handback) |
| 586 | (if (eq key-id 'SYM) |
| 587 | (read-passwd |
| 588 | (format "Passphrase for symmetric encryption%s: " |
| 589 | ;; Add the file name to the prompt, if any. |
| 590 | (if (stringp handback) |
| 591 | (format " for %s" handback) |
| 592 | "")) |
| 593 | (eq (epg-context-operation context) 'encrypt)) |
| 594 | (read-passwd |
| 595 | (if (eq key-id 'PIN) |
| 596 | "Passphrase for PIN: " |
| 597 | (let ((entry (assoc key-id epg-user-id-alist))) |
| 598 | (if entry |
| 599 | (format "Passphrase for %s %s: " key-id (cdr entry)) |
| 600 | (format "Passphrase for %s: " key-id))))))) |
| 601 | |
| 602 | (defun epa-progress-callback-function (_context what _char current total |
| 603 | handback) |
| 604 | (let ((prompt (or handback |
| 605 | (format "Processing %s: " what)))) |
| 606 | ;; According to gnupg/doc/DETAIL: a "total" of 0 indicates that |
| 607 | ;; the total amount is not known. The condition TOTAL && CUR == |
| 608 | ;; TOTAL may be used to detect the end of an operation. |
| 609 | (if (> total 0) |
| 610 | (if (= current total) |
| 611 | (message "%s...done" prompt) |
| 612 | (message "%s...%d%%" prompt |
| 613 | (floor (* (/ current (float total)) 100)))) |
| 614 | (message "%s..." prompt)))) |
| 615 | |
| 616 | (defun epa-read-file-name (input) |
| 617 | "Interactively read an output file name based on INPUT file name." |
| 618 | (setq input (file-name-sans-extension (expand-file-name input))) |
| 619 | (expand-file-name |
| 620 | (read-file-name |
| 621 | (concat "To file (default " (file-name-nondirectory input) ") ") |
| 622 | (file-name-directory input) |
| 623 | input))) |
| 624 | |
| 625 | ;;;###autoload |
| 626 | (defun epa-decrypt-file (decrypt-file &optional plain-file) |
| 627 | "Decrypt DECRYPT-FILE into PLAIN-FILE. |
| 628 | If you do not specify PLAIN-FILE, this functions prompts for the value to use." |
| 629 | (interactive |
| 630 | (let* ((file (read-file-name "File to decrypt: ")) |
| 631 | (plain (epa-read-file-name file))) |
| 632 | (list file plain))) |
| 633 | (or plain-file (setq plain-file (epa-read-file-name decrypt-file))) |
| 634 | (setq decrypt-file (expand-file-name decrypt-file)) |
| 635 | (let ((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 decrypt-file)))) |
| 643 | (message "Decrypting %s..." (file-name-nondirectory decrypt-file)) |
| 644 | (epg-decrypt-file context decrypt-file plain-file) |
| 645 | (message "Decrypting %s...wrote %s" (file-name-nondirectory decrypt-file) |
| 646 | (file-name-nondirectory plain-file)) |
| 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*" |
| 682 | (with-current-buffer standard-output |
| 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 |
| 690 | (setq type 'normal)))) |
| 691 | type)) |
| 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 |
| 762 | (defun epa-decrypt-region (start end &optional make-buffer-function) |
| 763 | "Decrypt the current region between START and END. |
| 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. |
| 769 | |
| 770 | Be careful about using this command in Lisp programs! |
| 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))" |
| 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 |
| 800 | (get-text-property start 'epa-coding-system-used) |
| 801 | 'undecided))) |
| 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)) |
| 811 | (with-output-to-temp-buffer "*Temp*" |
| 812 | (set-buffer standard-output) |
| 813 | (insert plain) |
| 814 | (epa-info-mode)))) |
| 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)) |
| 823 | ;; Find the first coding system which corresponds to MIME-CHARSET. |
| 824 | (let ((pointer (coding-system-list))) |
| 825 | (while (and pointer |
| 826 | (not (eq (coding-system-get (car pointer) 'mime-charset) |
| 827 | mime-charset))) |
| 828 | (setq pointer (cdr pointer))) |
| 829 | (car pointer)))) |
| 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 | |
| 835 | Don't use this command in Lisp programs! |
| 836 | See the reason described in the `epa-decrypt-region' documentation." |
| 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 |
| 848 | (error "Encryption armor beginning has no matching end")) |
| 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 | |
| 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))" |
| 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 |
| 894 | (get-text-property start 'epa-coding-system-used) |
| 895 | 'undecided))) |
| 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 | |
| 915 | Don't use this command in Lisp programs! |
| 916 | See the reason described in the `epa-verify-region' documentation." |
| 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 | (defalias 'epa--select-safe-coding-system |
| 937 | (if (fboundp 'select-safe-coding-system) |
| 938 | #'select-safe-coding-system |
| 939 | (lambda (_from _to) |
| 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 | |
| 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)))" |
| 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 | (defalias 'epa--derived-mode-p |
| 1012 | (if (fboundp 'derived-mode-p) |
| 1013 | #'derived-mode-p |
| 1014 | (lambda (&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 | |
| 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))" |
| 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) |
| 1094 | "Delete selected KEYS." |
| 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") |
| 1105 | (apply #'epa--list-keys epa-list-keys-arguments))) |
| 1106 | |
| 1107 | ;;;###autoload |
| 1108 | (defun epa-import-keys (file) |
| 1109 | "Import keys from FILE." |
| 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 | ;; FIXME: Why not use the (otherwise unused) epa--derived-mode-p? |
| 1124 | (if (eq major-mode 'epa-key-list-mode) |
| 1125 | (apply #'epa--list-keys epa-list-keys-arguments)))) |
| 1126 | |
| 1127 | ;;;###autoload |
| 1128 | (defun epa-import-keys-region (start end) |
| 1129 | "Import keys from the region." |
| 1130 | (interactive "r") |
| 1131 | (let ((context (epg-make-context epa-protocol))) |
| 1132 | (message "Importing...") |
| 1133 | (condition-case nil |
| 1134 | (progn |
| 1135 | (epg-import-keys-from-string context (buffer-substring start end)) |
| 1136 | (message "Importing...done")) |
| 1137 | (error |
| 1138 | (message "Importing...failed"))) |
| 1139 | (if (epg-context-result-for context 'import) |
| 1140 | (epa-display-info (epg-import-result-to-string |
| 1141 | (epg-context-result-for context 'import)))))) |
| 1142 | |
| 1143 | ;;;###autoload |
| 1144 | (defun epa-import-armor-in-region (start end) |
| 1145 | "Import keys in the OpenPGP armor format in the current region |
| 1146 | between START and END." |
| 1147 | (interactive "r") |
| 1148 | (save-excursion |
| 1149 | (save-restriction |
| 1150 | (narrow-to-region start end) |
| 1151 | (goto-char start) |
| 1152 | (let (armor-start armor-end) |
| 1153 | (while (re-search-forward |
| 1154 | "-----BEGIN \\(PGP \\(PUBLIC\\|PRIVATE\\) KEY BLOCK\\)-----$" |
| 1155 | nil t) |
| 1156 | (setq armor-start (match-beginning 0) |
| 1157 | armor-end (re-search-forward |
| 1158 | (concat "^-----END " (match-string 1) "-----$") |
| 1159 | nil t)) |
| 1160 | (unless armor-end |
| 1161 | (error "No armor tail")) |
| 1162 | (epa-import-keys-region armor-start armor-end)))))) |
| 1163 | |
| 1164 | ;;;###autoload |
| 1165 | (defun epa-export-keys (keys file) |
| 1166 | "Export selected KEYS to FILE." |
| 1167 | (interactive |
| 1168 | (let ((keys (epa--marked-keys)) |
| 1169 | default-name) |
| 1170 | (unless keys |
| 1171 | (error "No keys selected")) |
| 1172 | (setq default-name |
| 1173 | (expand-file-name |
| 1174 | (concat (epg-sub-key-id (car (epg-key-sub-key-list (car keys)))) |
| 1175 | (if epa-armor ".asc" ".gpg")) |
| 1176 | default-directory)) |
| 1177 | (list keys |
| 1178 | (expand-file-name |
| 1179 | (read-file-name |
| 1180 | (concat "To file (default " |
| 1181 | (file-name-nondirectory default-name) |
| 1182 | ") ") |
| 1183 | (file-name-directory default-name) |
| 1184 | default-name))))) |
| 1185 | (let ((context (epg-make-context epa-protocol))) |
| 1186 | (epg-context-set-armor context epa-armor) |
| 1187 | (message "Exporting to %s..." (file-name-nondirectory file)) |
| 1188 | (epg-export-keys-to-file context keys file) |
| 1189 | (message "Exporting to %s...done" (file-name-nondirectory file)))) |
| 1190 | |
| 1191 | ;;;###autoload |
| 1192 | (defun epa-insert-keys (keys) |
| 1193 | "Insert selected KEYS after the point." |
| 1194 | (interactive |
| 1195 | (list (epa-select-keys (epg-make-context epa-protocol) |
| 1196 | "Select keys to export. |
| 1197 | If no one is selected, default public key is exported. "))) |
| 1198 | (let ((context (epg-make-context epa-protocol))) |
| 1199 | ;;(epg-context-set-armor context epa-armor) |
| 1200 | (epg-context-set-armor context t) |
| 1201 | (insert (epg-export-keys-to-string context keys)))) |
| 1202 | |
| 1203 | ;; (defun epa-sign-keys (keys &optional local) |
| 1204 | ;; "Sign selected KEYS. |
| 1205 | ;; If a prefix-arg is specified, the signature is marked as non exportable. |
| 1206 | |
| 1207 | ;; Don't use this command in Lisp programs!" |
| 1208 | ;; (interactive |
| 1209 | ;; (let ((keys (epa--marked-keys))) |
| 1210 | ;; (unless keys |
| 1211 | ;; (error "No keys selected")) |
| 1212 | ;; (list keys current-prefix-arg))) |
| 1213 | ;; (let ((context (epg-make-context epa-protocol))) |
| 1214 | ;; (epg-context-set-passphrase-callback context |
| 1215 | ;; #'epa-passphrase-callback-function) |
| 1216 | ;; (epg-context-set-progress-callback context |
| 1217 | ;; (cons |
| 1218 | ;; #'epa-progress-callback-function |
| 1219 | ;; "Signing keys...")) |
| 1220 | ;; (message "Signing keys...") |
| 1221 | ;; (epg-sign-keys context keys local) |
| 1222 | ;; (message "Signing keys...done"))) |
| 1223 | ;; (make-obsolete 'epa-sign-keys "Do not use.") |
| 1224 | |
| 1225 | (provide 'epa) |
| 1226 | |
| 1227 | ;;; epa.el ends here |