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