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