(makefile-cleanup-continuations, makefile-warn-suspicious-lines):
[emacs.git] / lisp / pgg.el
blob33bbace71f47a2e5eba4bf80c38dd129a3e99354
1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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 ;;; Code:
30 (require 'pgg-def)
31 (require 'pgg-parse)
32 (autoload 'run-at-time "timer")
34 ;; Don't merge these two `eval-when-compile's.
35 (eval-when-compile
36 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
37 (require 'cl))
39 ;;; @ utility functions
40 ;;;
42 (eval-when-compile
43 ;; Define it as a null macro for Emacs in order to suppress a byte
44 ;; compile warning that Emacs 21 issues.
45 (defmacro pgg-run-at-time-1 (time repeat function args)
46 (when (featurep 'xemacs)
47 (if (condition-case nil
48 (let ((delete-itimer 'delete-itimer)
49 (itimer-driver-start 'itimer-driver-start)
50 (itimer-value 'itimer-value)
51 (start-itimer 'start-itimer))
52 (unless (or (symbol-value 'itimer-process)
53 (symbol-value 'itimer-timer))
54 (funcall itimer-driver-start))
55 ;; Check whether there is a bug to which the difference of
56 ;; the present time and the time when the itimer driver was
57 ;; woken up is subtracted from the initial itimer value.
58 (let* ((inhibit-quit t)
59 (ctime (current-time))
60 (itimer-timer-last-wakeup
61 (prog1
62 ctime
63 (setcar ctime (1- (car ctime)))))
64 (itimer-list nil)
65 (itimer (funcall start-itimer "pgg-run-at-time"
66 'ignore 5)))
67 (sleep-for 0.1) ;; Accept the timeout interrupt.
68 (prog1
69 (> (funcall itimer-value itimer) 0)
70 (funcall delete-itimer itimer))))
71 (error nil))
72 `(let ((time ,time))
73 (apply #'start-itimer "pgg-run-at-time"
74 ,function (if time (max time 1e-9) 1e-9)
75 ,repeat nil t ,args)))
76 `(let ((time ,time)
77 (itimers (list nil)))
78 (setcar
79 itimers
80 (apply #'start-itimer "pgg-run-at-time"
81 (lambda (itimers repeat function &rest args)
82 (let ((itimer (car itimers)))
83 (if repeat
84 (progn
85 (set-itimer-function
86 itimer
87 (lambda (itimer repeat function &rest args)
88 (set-itimer-restart itimer repeat)
89 (set-itimer-function itimer function)
90 (set-itimer-function-arguments itimer args)
91 (apply function args)))
92 (set-itimer-function-arguments
93 itimer
94 (append (list itimer repeat function) args)))
95 (set-itimer-function
96 itimer
97 (lambda (itimer function &rest args)
98 (delete-itimer itimer)
99 (apply function args)))
100 (set-itimer-function-arguments
101 itimer
102 (append (list itimer function) args)))))
103 1e-9 (if time (max time 1e-9) 1e-9)
104 nil t itimers ,repeat ,function ,args))))))
106 (eval-and-compile
107 (if (featurep 'xemacs)
108 (progn
109 (defun pgg-run-at-time (time repeat function &rest args)
110 "Emulating function run as `run-at-time'.
111 TIME should be nil meaning now, or a number of seconds from now.
112 Return an itimer object which can be used in either `delete-itimer'
113 or `cancel-timer'."
114 (pgg-run-at-time-1 time repeat function args))
115 (defun pgg-cancel-timer (timer)
116 "Emulate cancel-timer for xemacs."
117 (let ((delete-itimer 'delete-itimer))
118 (funcall delete-itimer timer))))
119 (defalias 'pgg-run-at-time 'run-at-time)
120 (defalias 'pgg-cancel-timer 'cancel-timer)))
122 (defun pgg-invoke (func scheme &rest args)
123 (progn
124 (require (intern (format "pgg-%s" scheme)))
125 (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
127 (put 'pgg-save-coding-system 'lisp-indent-function 2)
129 (defmacro pgg-save-coding-system (start end &rest body)
130 `(if (interactive-p)
131 (let ((buffer (current-buffer)))
132 (with-temp-buffer
133 (let (buffer-undo-list)
134 (insert-buffer-substring buffer ,start ,end)
135 (encode-coding-region (point-min)(point-max)
136 buffer-file-coding-system)
137 (prog1 (save-excursion ,@body)
138 (push nil buffer-undo-list)
139 (ignore-errors (undo))))))
140 (save-restriction
141 (narrow-to-region ,start ,end)
142 ,@body)))
144 (defun pgg-temp-buffer-show-function (buffer)
145 (let ((window (or (get-buffer-window buffer 'visible)
146 (split-window-vertically))))
147 (set-window-buffer window buffer)
148 (shrink-window-if-larger-than-buffer window)))
150 ;; XXX `pgg-display-output-buffer' is a horrible name for this function.
151 ;; It should be something like `pgg-situate-output-or-display-error'.
152 (defun pgg-display-output-buffer (start end status)
153 "Situate en/decryption results or pop up an error buffer.
155 Text from START to END is replaced by contents of output buffer if STATUS
156 is true, or else the output buffer is displayed."
157 (if status
158 (pgg-situate-output start end)
159 (pgg-display-error-buffer)))
161 (defun pgg-situate-output (start end)
162 "Place en/decryption result in place of current text from START to END."
163 (delete-region start end)
164 (insert-buffer-substring pgg-output-buffer)
165 (decode-coding-region start (point) buffer-file-coding-system))
167 (defun pgg-display-error-buffer ()
168 "Pop up an error buffer indicating the reason for an en/decryption failure."
169 (let ((temp-buffer-show-function
170 (function pgg-temp-buffer-show-function)))
171 (with-output-to-temp-buffer pgg-echo-buffer
172 (set-buffer standard-output)
173 (insert-buffer-substring pgg-errors-buffer))))
175 (defvar pgg-passphrase-cache (make-vector 7 0))
177 (defvar pgg-pending-timers (make-vector 7 0)
178 "Hash table for managing scheduled pgg cache management timers.
180 We associate key and timer, so the timer can be cancelled if a new
181 timeout for the key is set while an old one is still pending.")
183 (defun pgg-read-passphrase (prompt &optional key notruncate)
184 "Using PROMPT, obtain passphrase for KEY from cache or user.
186 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
187 \(default false).
189 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
190 regulate cache behavior."
191 (or (pgg-read-passphrase-from-cache key notruncate)
192 (read-passwd prompt)))
194 (defun pgg-read-passphrase-from-cache (key &optional notruncate)
195 "Obtain passphrase for KEY from time-limited passphrase cache.
197 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
198 \(default false).
200 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
201 regulate cache behavior."
202 (and pgg-cache-passphrase
203 key (or notruncate
204 (setq key (pgg-truncate-key-identifier key)))
205 (symbol-value (intern-soft key pgg-passphrase-cache))))
207 (defun pgg-add-passphrase-to-cache (key passphrase &optional notruncate)
208 "Associate KEY with PASSPHRASE in time-limited passphrase cache.
210 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
211 \(default false).
213 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
214 regulate cache behavior."
216 (let* ((key (if notruncate key (pgg-truncate-key-identifier key)))
217 (interned-timer-key (intern-soft key pgg-pending-timers))
218 (old-timer (symbol-value interned-timer-key))
219 new-timer)
220 (when old-timer
221 (cancel-timer old-timer)
222 (unintern interned-timer-key pgg-pending-timers))
223 (set (intern key pgg-passphrase-cache)
224 passphrase)
225 (set (intern key pgg-pending-timers)
226 (pgg-run-at-time pgg-passphrase-cache-expiry nil
227 #'pgg-remove-passphrase-from-cache
228 key notruncate))))
230 (if (fboundp 'clear-string)
231 (defalias 'pgg-clear-string 'clear-string)
232 (defun pgg-clear-string (string)
233 (fillarray string ?_)))
235 (declare-function pgg-clear-string "pgg" (string))
237 (defun pgg-remove-passphrase-from-cache (key &optional notruncate)
238 "Omit passphrase associated with KEY in time-limited passphrase cache.
240 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
241 \(default false).
243 This is a no-op if there is not entry for KEY (eg, it's already expired.
245 The memory for the passphrase is filled with underscores to clear any
246 references to it.
248 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
249 regulate cache behavior."
250 (let* ((passphrase (pgg-read-passphrase-from-cache key notruncate))
251 (key (if notruncate key (pgg-truncate-key-identifier key)))
252 (interned-timer-key (intern-soft key pgg-pending-timers))
253 (old-timer (symbol-value interned-timer-key)))
254 (when passphrase
255 (pgg-clear-string passphrase)
256 (unintern key pgg-passphrase-cache))
257 (when old-timer
258 (pgg-cancel-timer old-timer)
259 (unintern interned-timer-key pgg-pending-timers))))
261 (defmacro pgg-convert-lbt-region (start end lbt)
262 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
263 (goto-char ,start)
264 (case ,lbt
265 (CRLF
266 (while (progn
267 (end-of-line)
268 (> (marker-position pgg-conversion-end) (point)))
269 (insert "\r")
270 (forward-line 1)))
272 (while (re-search-forward "\r$" pgg-conversion-end t)
273 (replace-match ""))))))
275 (put 'pgg-as-lbt 'lisp-indent-function 3)
277 (defmacro pgg-as-lbt (start end lbt &rest body)
278 `(let ((inhibit-read-only t)
279 buffer-read-only
280 buffer-undo-list)
281 (pgg-convert-lbt-region ,start ,end ,lbt)
282 (let ((,end (point)))
283 ,@body)
284 (push nil buffer-undo-list)
285 (ignore-errors (undo))))
287 (put 'pgg-process-when-success 'lisp-indent-function 0)
289 (defmacro pgg-process-when-success (&rest body)
290 `(with-current-buffer pgg-output-buffer
291 (if (zerop (buffer-size)) nil ,@body t)))
293 (defalias 'pgg-make-temp-file
294 (if (fboundp 'make-temp-file)
295 'make-temp-file
296 (lambda (prefix &optional dir-flag)
297 (let ((file (expand-file-name
298 (make-temp-name prefix)
299 (if (fboundp 'temp-directory)
300 (temp-directory)
301 temporary-file-directory))))
302 (if dir-flag
303 (make-directory file))
304 file))))
306 ;;; @ interface functions
309 ;;;###autoload
310 (defun pgg-encrypt-region (start end rcpts &optional sign passphrase)
311 "Encrypt the current region between START and END for RCPTS.
313 If optional argument SIGN is non-nil, do a combined sign and encrypt.
315 If optional PASSPHRASE is not specified, it will be obtained from the
316 passphrase cache or user."
317 (interactive
318 (list (region-beginning)(region-end)
319 (split-string (read-string "Recipients: ") "[ \t,]+")))
320 (let ((status
321 (pgg-save-coding-system start end
322 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
323 (point-min) (point-max) rcpts sign passphrase))))
324 (when (interactive-p)
325 (pgg-display-output-buffer start end status))
326 status))
328 ;;;###autoload
329 (defun pgg-encrypt-symmetric-region (start end &optional passphrase)
330 "Encrypt the current region between START and END symmetric with passphrase.
332 If optional PASSPHRASE is not specified, it will be obtained from the
333 cache or user."
334 (interactive "r")
335 (let ((status
336 (pgg-save-coding-system start end
337 (pgg-invoke "encrypt-symmetric-region"
338 (or pgg-scheme pgg-default-scheme)
339 (point-min) (point-max) passphrase))))
340 (when (interactive-p)
341 (pgg-display-output-buffer start end status))
342 status))
344 ;;;###autoload
345 (defun pgg-encrypt-symmetric (&optional start end passphrase)
346 "Encrypt the current buffer using a symmetric, rather than key-pair, cipher.
348 If optional arguments START and END are specified, only encrypt within
349 the region.
351 If optional PASSPHRASE is not specified, it will be obtained from the
352 passphrase cache or user."
353 (interactive)
354 (let* ((start (or start (point-min)))
355 (end (or end (point-max)))
356 (status (pgg-encrypt-symmetric-region start end passphrase)))
357 (when (interactive-p)
358 (pgg-display-output-buffer start end status))
359 status))
361 ;;;###autoload
362 (defun pgg-encrypt (rcpts &optional sign start end passphrase)
363 "Encrypt the current buffer for RCPTS.
365 If optional argument SIGN is non-nil, do a combined sign and encrypt.
367 If optional arguments START and END are specified, only encrypt within
368 the region.
370 If optional PASSPHRASE is not specified, it will be obtained from the
371 passphrase cache or user."
372 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
373 (let* ((start (or start (point-min)))
374 (end (or end (point-max)))
375 (status (pgg-encrypt-region start end rcpts sign passphrase)))
376 (when (interactive-p)
377 (pgg-display-output-buffer start end status))
378 status))
380 ;;;###autoload
381 (defun pgg-decrypt-region (start end &optional passphrase)
382 "Decrypt the current region between START and END.
384 If optional PASSPHRASE is not specified, it will be obtained from the
385 passphrase cache or user."
386 (interactive "r")
387 (let* ((buf (current-buffer))
388 (status
389 (pgg-save-coding-system start end
390 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
391 (point-min) (point-max) passphrase))))
392 (when (interactive-p)
393 (pgg-display-output-buffer start end status))
394 status))
396 ;;;###autoload
397 (defun pgg-decrypt (&optional start end passphrase)
398 "Decrypt the current buffer.
400 If optional arguments START and END are specified, only decrypt within
401 the region.
403 If optional PASSPHRASE is not specified, it will be obtained from the
404 passphrase cache or user."
405 (interactive "")
406 (let* ((start (or start (point-min)))
407 (end (or end (point-max)))
408 (status (pgg-decrypt-region start end passphrase)))
409 (when (interactive-p)
410 (pgg-display-output-buffer start end status))
411 status))
413 ;;;###autoload
414 (defun pgg-sign-region (start end &optional cleartext passphrase)
415 "Make the signature from text between START and END.
417 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
418 a detached signature.
420 If this function is called interactively, CLEARTEXT is enabled
421 and the output is displayed.
423 If optional PASSPHRASE is not specified, it will be obtained from the
424 passphrase cache or user."
425 (interactive "r")
426 (let ((status (pgg-save-coding-system start end
427 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
428 (point-min) (point-max)
429 (or (interactive-p) cleartext)
430 passphrase))))
431 (when (interactive-p)
432 (pgg-display-output-buffer start end status))
433 status))
435 ;;;###autoload
436 (defun pgg-sign (&optional cleartext start end passphrase)
437 "Sign the current buffer.
439 If the optional argument CLEARTEXT is non-nil, it does not create a
440 detached signature.
442 If optional arguments START and END are specified, only sign data
443 within the region.
445 If this function is called interactively, CLEARTEXT is enabled
446 and the output is displayed.
448 If optional PASSPHRASE is not specified, it will be obtained from the
449 passphrase cache or user."
450 (interactive "")
451 (let* ((start (or start (point-min)))
452 (end (or end (point-max)))
453 (status (pgg-sign-region start end
454 (or (interactive-p) cleartext)
455 passphrase)))
456 (when (interactive-p)
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 (if (fboundp 'set-buffer-multibyte)
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 (interactive-p))
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 (interactive-p)
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 (interactive-p)
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 ;; arch-tag: 9cc705dd-1e6a-4c90-8dce-c3561f9a2cf4
601 ;;; pgg.el ends here