Add Obsolete-since: header to pgg*.el.
[emacs.git] / lisp / obsolete / pgg.el
blobb63dd46c98b48b9a2c11f47ecf3bf160b122d72e
1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Symmetric encryption added by: Sascha Wilde <wilde@sha-bang.de>
8 ;; Created: 1999/10/28
9 ;; Keywords: PGP
10 ;; Obsolete-since: 24.1
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Code:
29 (require 'pgg-def)
30 (require 'pgg-parse)
31 (autoload 'run-at-time "timer")
33 ;; Don't merge these two `eval-when-compile's.
34 (eval-when-compile
35 ;; For Emacs <22.2 and XEmacs.
36 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
37 (require 'cl))
39 ;;; @ utility functions
40 ;;;
42 (eval-when-compile
43 (when (featurep 'xemacs)
44 (defmacro pgg-run-at-time-1 (time repeat function args)
45 (if (condition-case nil
46 (let ((delete-itimer 'delete-itimer)
47 (itimer-driver-start 'itimer-driver-start)
48 (itimer-value 'itimer-value)
49 (start-itimer 'start-itimer))
50 (unless (or (symbol-value 'itimer-process)
51 (symbol-value 'itimer-timer))
52 (funcall itimer-driver-start))
53 ;; Check whether there is a bug to which the difference of
54 ;; the present time and the time when the itimer driver was
55 ;; woken up is subtracted from the initial itimer value.
56 (let* ((inhibit-quit t)
57 (ctime (current-time))
58 (itimer-timer-last-wakeup
59 (prog1
60 ctime
61 (setcar ctime (1- (car ctime)))))
62 (itimer-list nil)
63 (itimer (funcall start-itimer "pgg-run-at-time"
64 'ignore 5)))
65 (sleep-for 0.1) ;; Accept the timeout interrupt.
66 (prog1
67 (> (funcall itimer-value itimer) 0)
68 (funcall delete-itimer itimer))))
69 (error nil))
70 `(let ((time ,time))
71 (apply #'start-itimer "pgg-run-at-time"
72 ,function (if time (max time 1e-9) 1e-9)
73 ,repeat nil t ,args))
74 `(let ((time ,time)
75 (itimers (list nil)))
76 (setcar
77 itimers
78 (apply #'start-itimer "pgg-run-at-time"
79 (lambda (itimers repeat function &rest args)
80 (let ((itimer (car itimers)))
81 (if repeat
82 (progn
83 (set-itimer-function
84 itimer
85 (lambda (itimer repeat function &rest args)
86 (set-itimer-restart itimer repeat)
87 (set-itimer-function itimer function)
88 (set-itimer-function-arguments itimer args)
89 (apply function args)))
90 (set-itimer-function-arguments
91 itimer
92 (append (list itimer repeat function) args)))
93 (set-itimer-function
94 itimer
95 (lambda (itimer function &rest args)
96 (delete-itimer itimer)
97 (apply function args)))
98 (set-itimer-function-arguments
99 itimer
100 (append (list itimer function) args)))))
101 1e-9 (if time (max time 1e-9) 1e-9)
102 nil t itimers ,repeat ,function ,args)))))))
104 (eval-and-compile
105 (if (featurep 'xemacs)
106 (progn
107 (defun pgg-run-at-time (time repeat function &rest args)
108 "Emulating function run as `run-at-time'.
109 TIME should be nil meaning now, or a number of seconds from now.
110 Return an itimer object which can be used in either `delete-itimer'
111 or `cancel-timer'."
112 (pgg-run-at-time-1 time repeat function args))
113 (defun pgg-cancel-timer (timer)
114 "Emulate cancel-timer for xemacs."
115 (let ((delete-itimer 'delete-itimer))
116 (funcall delete-itimer timer))))
117 (defalias 'pgg-run-at-time 'run-at-time)
118 (defalias 'pgg-cancel-timer 'cancel-timer)))
120 (defun pgg-invoke (func scheme &rest args)
121 (progn
122 (require (intern (format "pgg-%s" scheme)))
123 (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
125 (put 'pgg-save-coding-system 'lisp-indent-function 2)
127 (defmacro pgg-save-coding-system (start end &rest body)
128 `(if (called-interactively-p 'interactive)
129 (let ((buffer (current-buffer)))
130 (with-temp-buffer
131 (let (buffer-undo-list)
132 (insert-buffer-substring buffer ,start ,end)
133 (encode-coding-region (point-min)(point-max)
134 buffer-file-coding-system)
135 (prog1 (save-excursion ,@body)
136 (push nil buffer-undo-list)
137 (ignore-errors (undo))))))
138 (save-restriction
139 (narrow-to-region ,start ,end)
140 ,@body)))
142 (defun pgg-temp-buffer-show-function (buffer)
143 (let ((window (or (get-buffer-window buffer 'visible)
144 (split-window-vertically))))
145 (set-window-buffer window buffer)
146 (shrink-window-if-larger-than-buffer window)))
148 ;; XXX `pgg-display-output-buffer' is a horrible name for this function.
149 ;; It should be something like `pgg-situate-output-or-display-error'.
150 (defun pgg-display-output-buffer (start end status)
151 "Situate en/decryption results or pop up an error buffer.
153 Text from START to END is replaced by contents of output buffer if STATUS
154 is true, or else the output buffer is displayed."
155 (if status
156 (pgg-situate-output start end)
157 (pgg-display-error-buffer)))
159 (defun pgg-situate-output (start end)
160 "Place en/decryption result in place of current text from START to END."
161 (delete-region start end)
162 (insert-buffer-substring pgg-output-buffer)
163 (decode-coding-region start (point) buffer-file-coding-system))
165 (defun pgg-display-error-buffer ()
166 "Pop up an error buffer indicating the reason for an en/decryption failure."
167 (let ((temp-buffer-show-function
168 (function pgg-temp-buffer-show-function)))
169 (with-output-to-temp-buffer pgg-echo-buffer
170 (set-buffer standard-output)
171 (insert-buffer-substring pgg-errors-buffer))))
173 (defvar pgg-passphrase-cache (make-vector 7 0))
175 (defvar pgg-pending-timers (make-vector 7 0)
176 "Hash table for managing scheduled pgg cache management timers.
178 We associate key and timer, so the timer can be cancelled if a new
179 timeout for the key is set while an old one is still pending.")
181 (defun pgg-read-passphrase (prompt &optional key notruncate)
182 "Using PROMPT, obtain passphrase for KEY from cache or user.
184 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
185 \(default false).
187 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
188 regulate cache behavior."
189 (or (pgg-read-passphrase-from-cache key notruncate)
190 (read-passwd prompt)))
192 (defun pgg-read-passphrase-from-cache (key &optional notruncate)
193 "Obtain passphrase for KEY from time-limited passphrase cache.
195 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
196 \(default false).
198 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
199 regulate cache behavior."
200 (and pgg-cache-passphrase
201 key (or notruncate
202 (setq key (pgg-truncate-key-identifier key)))
203 (symbol-value (intern-soft key pgg-passphrase-cache))))
205 (defun pgg-add-passphrase-to-cache (key passphrase &optional notruncate)
206 "Associate KEY with PASSPHRASE in time-limited passphrase cache.
208 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
209 \(default false).
211 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
212 regulate cache behavior."
214 (let* ((key (if notruncate key (pgg-truncate-key-identifier key)))
215 (interned-timer-key (intern-soft key pgg-pending-timers))
216 (old-timer (symbol-value interned-timer-key))
217 new-timer)
218 (when old-timer
219 (cancel-timer old-timer)
220 (unintern interned-timer-key pgg-pending-timers))
221 (set (intern key pgg-passphrase-cache)
222 passphrase)
223 (set (intern key pgg-pending-timers)
224 (pgg-run-at-time pgg-passphrase-cache-expiry nil
225 #'pgg-remove-passphrase-from-cache
226 key notruncate))))
228 (if (fboundp 'clear-string)
229 (defalias 'pgg-clear-string 'clear-string)
230 (defun pgg-clear-string (string)
231 (fillarray string ?_)))
233 (declare-function pgg-clear-string "pgg" (string))
235 (defun pgg-remove-passphrase-from-cache (key &optional notruncate)
236 "Omit passphrase associated with KEY in time-limited passphrase cache.
238 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
239 \(default false).
241 This is a no-op if there is not entry for KEY (eg, it's already expired.
243 The memory for the passphrase is filled with underscores to clear any
244 references to it.
246 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
247 regulate cache behavior."
248 (let* ((passphrase (pgg-read-passphrase-from-cache key notruncate))
249 (key (if notruncate key (pgg-truncate-key-identifier key)))
250 (interned-timer-key (intern-soft key pgg-pending-timers))
251 (old-timer (symbol-value interned-timer-key)))
252 (when passphrase
253 (pgg-clear-string passphrase)
254 (unintern key pgg-passphrase-cache))
255 (when old-timer
256 (pgg-cancel-timer old-timer)
257 (unintern interned-timer-key pgg-pending-timers))))
259 (defmacro pgg-convert-lbt-region (start end lbt)
260 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
261 (goto-char ,start)
262 (case ,lbt
263 (CRLF
264 (while (progn
265 (end-of-line)
266 (> (marker-position pgg-conversion-end) (point)))
267 (insert "\r")
268 (forward-line 1)))
270 (while (re-search-forward "\r$" pgg-conversion-end t)
271 (replace-match ""))))))
273 (put 'pgg-as-lbt 'lisp-indent-function 3)
275 (defmacro pgg-as-lbt (start end lbt &rest body)
276 `(let ((inhibit-read-only t)
277 buffer-read-only
278 buffer-undo-list)
279 (pgg-convert-lbt-region ,start ,end ,lbt)
280 (let ((,end (point)))
281 ,@body)
282 (push nil buffer-undo-list)
283 (ignore-errors (undo))))
285 (put 'pgg-process-when-success 'lisp-indent-function 0)
287 (defmacro pgg-process-when-success (&rest body)
288 `(with-current-buffer pgg-output-buffer
289 (if (zerop (buffer-size)) nil ,@body t)))
291 (defalias 'pgg-make-temp-file
292 (if (fboundp 'make-temp-file)
293 'make-temp-file
294 (lambda (prefix &optional dir-flag)
295 (let ((file (expand-file-name
296 (make-temp-name prefix)
297 (if (fboundp 'temp-directory)
298 (temp-directory)
299 temporary-file-directory))))
300 (if dir-flag
301 (make-directory file))
302 file))))
304 ;;; @ interface functions
307 ;;;###autoload
308 (defun pgg-encrypt-region (start end rcpts &optional sign passphrase)
309 "Encrypt the current region between START and END for RCPTS.
311 If optional argument SIGN is non-nil, do a combined sign and encrypt.
313 If optional PASSPHRASE is not specified, it will be obtained from the
314 passphrase cache or user."
315 (interactive
316 (list (region-beginning)(region-end)
317 (split-string (read-string "Recipients: ") "[ \t,]+")))
318 (let ((status
319 (pgg-save-coding-system start end
320 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
321 (point-min) (point-max) rcpts sign passphrase))))
322 (when (called-interactively-p 'interactive)
323 (pgg-display-output-buffer start end status))
324 status))
326 ;;;###autoload
327 (defun pgg-encrypt-symmetric-region (start end &optional passphrase)
328 "Encrypt the current region between START and END symmetric with passphrase.
330 If optional PASSPHRASE is not specified, it will be obtained from the
331 cache or user."
332 (interactive "r")
333 (let ((status
334 (pgg-save-coding-system start end
335 (pgg-invoke "encrypt-symmetric-region"
336 (or pgg-scheme pgg-default-scheme)
337 (point-min) (point-max) passphrase))))
338 (when (called-interactively-p 'interactive)
339 (pgg-display-output-buffer start end status))
340 status))
342 ;;;###autoload
343 (defun pgg-encrypt-symmetric (&optional start end passphrase)
344 "Encrypt the current buffer using a symmetric, rather than key-pair, cipher.
346 If optional arguments START and END are specified, only encrypt within
347 the region.
349 If optional PASSPHRASE is not specified, it will be obtained from the
350 passphrase cache or user."
351 (interactive)
352 (let* ((start (or start (point-min)))
353 (end (or end (point-max)))
354 (status (pgg-encrypt-symmetric-region start end passphrase)))
355 (when (called-interactively-p 'interactive)
356 (pgg-display-output-buffer start end status))
357 status))
359 ;;;###autoload
360 (defun pgg-encrypt (rcpts &optional sign start end passphrase)
361 "Encrypt the current buffer for RCPTS.
363 If optional argument SIGN is non-nil, do a combined sign and encrypt.
365 If optional arguments START and END are specified, only encrypt within
366 the region.
368 If optional PASSPHRASE is not specified, it will be obtained from the
369 passphrase cache or user."
370 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
371 (let* ((start (or start (point-min)))
372 (end (or end (point-max)))
373 (status (pgg-encrypt-region start end rcpts sign passphrase)))
374 (when (called-interactively-p 'interactive)
375 (pgg-display-output-buffer start end status))
376 status))
378 ;;;###autoload
379 (defun pgg-decrypt-region (start end &optional passphrase)
380 "Decrypt the current region between START and END.
382 If optional PASSPHRASE is not specified, it will be obtained from the
383 passphrase cache or user."
384 (interactive "r")
385 (let* ((buf (current-buffer))
386 (status
387 (pgg-save-coding-system start end
388 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
389 (point-min) (point-max) passphrase))))
390 (when (called-interactively-p 'interactive)
391 (pgg-display-output-buffer start end status))
392 status))
394 ;;;###autoload
395 (defun pgg-decrypt (&optional start end passphrase)
396 "Decrypt the current buffer.
398 If optional arguments START and END are specified, only decrypt within
399 the region.
401 If optional PASSPHRASE is not specified, it will be obtained from the
402 passphrase cache or user."
403 (interactive "")
404 (let* ((start (or start (point-min)))
405 (end (or end (point-max)))
406 (status (pgg-decrypt-region start end passphrase)))
407 (when (called-interactively-p 'interactive)
408 (pgg-display-output-buffer start end status))
409 status))
411 ;;;###autoload
412 (defun pgg-sign-region (start end &optional cleartext passphrase)
413 "Make the signature from text between START and END.
415 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
416 a detached signature.
418 If this function is called interactively, CLEARTEXT is enabled
419 and the output is displayed.
421 If optional PASSPHRASE is not specified, it will be obtained from the
422 passphrase cache or user."
423 (interactive "r")
424 (let ((status (pgg-save-coding-system start end
425 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
426 (point-min) (point-max)
427 (or (called-interactively-p 'interactive)
428 cleartext)
429 passphrase))))
430 (when (called-interactively-p 'interactive)
431 (pgg-display-output-buffer start end status))
432 status))
434 ;;;###autoload
435 (defun pgg-sign (&optional cleartext start end passphrase)
436 "Sign the current buffer.
438 If the optional argument CLEARTEXT is non-nil, it does not create a
439 detached signature.
441 If optional arguments START and END are specified, only sign data
442 within the region.
444 If this function is called interactively, CLEARTEXT is enabled
445 and the output is displayed.
447 If optional PASSPHRASE is not specified, it will be obtained from the
448 passphrase cache or user."
449 (interactive "")
450 (let* ((start (or start (point-min)))
451 (end (or end (point-max)))
452 (status (pgg-sign-region start end
453 (or (called-interactively-p 'interactive)
454 cleartext)
455 passphrase)))
456 (when (called-interactively-p 'interactive)
457 (pgg-display-output-buffer start end status))
458 status))
460 ;;;###autoload
461 (defun pgg-verify-region (start end &optional signature fetch)
462 "Verify the current region between START and END.
463 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
464 the detached signature of the current region.
466 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
467 signer's public key from `pgg-default-keyserver-address'."
468 (interactive "r")
469 (let* ((packet
470 (if (null signature) nil
471 (with-temp-buffer
472 (buffer-disable-undo)
473 (unless (featurep 'xemacs)
474 (set-buffer-multibyte nil))
475 (insert-file-contents signature)
476 (cdr (assq 2 (pgg-decode-armor-region
477 (point-min)(point-max)))))))
478 (key (cdr (assq 'key-identifier packet)))
479 status keyserver)
480 (and (stringp key)
481 pgg-query-keyserver
482 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
483 (null (pgg-lookup-key key))
484 (or fetch (called-interactively-p 'interactive))
485 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
486 (setq keyserver
487 (or (cdr (assq 'preferred-key-server packet))
488 pgg-default-keyserver-address))
489 (pgg-fetch-key keyserver key))
490 (setq status
491 (pgg-save-coding-system start end
492 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
493 (point-min) (point-max) signature)))
494 (when (called-interactively-p 'interactive)
495 (let ((temp-buffer-show-function
496 (function pgg-temp-buffer-show-function)))
497 (with-output-to-temp-buffer pgg-echo-buffer
498 (set-buffer standard-output)
499 (insert-buffer-substring (if status pgg-output-buffer
500 pgg-errors-buffer)))))
501 status))
503 ;;;###autoload
504 (defun pgg-verify (&optional signature fetch start end)
505 "Verify the current buffer.
506 If the optional argument SIGNATURE is non-nil, it is treated as
507 the detached signature of the current region.
508 If the optional argument FETCH is non-nil, we attempt to fetch the
509 signer's public key from `pgg-default-keyserver-address'.
510 If optional arguments START and END are specified, only verify data
511 within the region."
512 (interactive "")
513 (let* ((start (or start (point-min)))
514 (end (or end (point-max)))
515 (status (pgg-verify-region start end signature fetch)))
516 (when (called-interactively-p 'interactive)
517 (let ((temp-buffer-show-function
518 (function pgg-temp-buffer-show-function)))
519 (with-output-to-temp-buffer pgg-echo-buffer
520 (set-buffer standard-output)
521 (insert-buffer-substring (if status pgg-output-buffer
522 pgg-errors-buffer)))))
523 status))
525 ;;;###autoload
526 (defun pgg-insert-key ()
527 "Insert the ASCII armored public key."
528 (interactive)
529 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
531 ;;;###autoload
532 (defun pgg-snarf-keys-region (start end)
533 "Import public keys in the current region between START and END."
534 (interactive "r")
535 (pgg-save-coding-system start end
536 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
537 start end)))
539 ;;;###autoload
540 (defun pgg-snarf-keys ()
541 "Import public keys in the current buffer."
542 (interactive "")
543 (pgg-snarf-keys-region (point-min) (point-max)))
545 (defun pgg-lookup-key (string &optional type)
546 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
548 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
550 (defun pgg-insert-url-with-w3 (url)
551 (ignore-errors
552 (require 'url)
553 (let (buffer-file-name)
554 (url-insert-file-contents url))))
556 (defvar pgg-insert-url-extra-arguments nil)
557 (defvar pgg-insert-url-program nil)
559 (defun pgg-insert-url-with-program (url)
560 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
561 process)
562 (insert
563 (with-temp-buffer
564 (setq process
565 (apply #'start-process " *PGG url*" (current-buffer)
566 pgg-insert-url-program (nconc args (list url))))
567 (set-process-sentinel process #'ignore)
568 (while (eq 'run (process-status process))
569 (accept-process-output process 5))
570 (delete-process process)
571 (if (and process (eq 'run (process-status process)))
572 (interrupt-process process))
573 (buffer-string)))))
575 (defun pgg-fetch-key (keyserver key)
576 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
577 (with-current-buffer (get-buffer-create pgg-output-buffer)
578 (buffer-disable-undo)
579 (erase-buffer)
580 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
581 (substring keyserver 0 (1- (match-end 0))))))
582 (save-excursion
583 (funcall pgg-insert-url-function
584 (if proto keyserver
585 (format "http://%s:11371/pks/lookup?op=get&search=%s"
586 keyserver key))))
587 (when (re-search-forward "^-+BEGIN" nil 'last)
588 (delete-region (point-min) (match-beginning 0))
589 (when (re-search-forward "^-+END" nil t)
590 (delete-region (progn (end-of-line) (point))
591 (point-max)))
592 (insert "\n")
593 (with-temp-buffer
594 (insert-buffer-substring pgg-output-buffer)
595 (pgg-snarf-keys-region (point-min)(point-max)))))))
598 (provide 'pgg)
600 ;;; pgg.el ends here