Update years in copyright notice; nfc.
[bpt/emacs.git] / lisp / pgg.el
CommitLineData
23f87bed
MB
1;;; pgg.el --- glue for the various PGP implementations.
2
e84b4b86 3;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
aaef169d 4;; 2005, 2006 Free Software Foundation, Inc.
23f87bed
MB
5
6;; Author: Daiki Ueno <ueno@unixuser.org>
7c700bd1 7;; Symmetric encryption added by: Sascha Wilde <wilde@sha-bang.de>
23f87bed
MB
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
3a35cf56
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
23f87bed
MB
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))
23f87bed
MB
39
40;;; @ utility functions
41;;;
42
23f87bed
MB
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
7c700bd1
EZ
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'.
23f87bed 73(defun pgg-display-output-buffer (start end status)
7c700bd1
EZ
74 "Situate en/decryption results or pop up an error buffer.
75
76Text from START to END is replaced by contents of output buffer if STATUS
77is true, or else the output buffer is displayed."
23f87bed 78 (if status
7c700bd1
EZ
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))))
23f87bed
MB
95
96(defvar pgg-passphrase-cache (make-vector 7 0))
97
7c700bd1
EZ
98(defvar pgg-pending-timers (make-vector 7 0)
99 "Hash table for managing scheduled pgg cache management timers.
100
101We associate key and timer, so the timer can be cancelled if a new
102timeout 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
107Truncate the key to 8 trailing characters unless NOTRUNCATE is true
108\(default false).
109
110Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
111regulate cache behavior."
112 (or (pgg-read-passphrase-from-cache key notruncate)
23f87bed
MB
113 (read-passwd prompt)))
114
7c700bd1
EZ
115(defun pgg-read-passphrase-from-cache (key &optional notruncate)
116 "Obtain passphrase for KEY from time-limited passphrase cache.
117
118Truncate the key to 8 trailing characters unless NOTRUNCATE is true
119\(default false).
120
121Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
122regulate 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
131Truncate the key to 8 trailing characters unless NOTRUNCATE is true
132\(default false).
133
134Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
135regulate 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
154Truncate the key to 8 trailing characters unless NOTRUNCATE is true
155\(default false).
156
157This is a no-op if there is not entry for KEY (eg, it's already expired.
158
159The memory for the passphrase is filled with underscores to clear any
160references to it.
161
162Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
163regulate 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
23f87bed 175(eval-when-compile
4a43ee9b
MB
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))))))
23f87bed
MB
236
237(eval-and-compile
4a43ee9b 238 (if (featurep 'xemacs)
7c700bd1
EZ
239 (progn
240 (defun pgg-run-at-time (time repeat function &rest args)
241 "Emulating function run as `run-at-time'.
23f87bed
MB
242TIME should be nil meaning now, or a number of seconds from now.
243Return an itimer object which can be used in either `delete-itimer'
244or `cancel-timer'."
7c700bd1
EZ
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)))
23f87bed
MB
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
7c700bd1 303(defun pgg-encrypt-region (start end rcpts &optional sign passphrase)
23f87bed 304 "Encrypt the current region between START and END for RCPTS.
7c700bd1
EZ
305
306If optional argument SIGN is non-nil, do a combined sign and encrypt.
307
308If optional PASSPHRASE is not specified, it will be obtained from the
309passphrase cache or user."
23f87bed
MB
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)
7c700bd1
EZ
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
325If optional PASSPHRASE is not specified, it will be obtained from the
326cache 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))))
23f87bed
MB
333 (when (interactive-p)
334 (pgg-display-output-buffer start end status))
335 status))
336
337;;;###autoload
7c700bd1
EZ
338(defun pgg-encrypt-symmetric (&optional start end passphrase)
339 "Encrypt the current buffer using a symmetric, rather than key-pair, cipher.
340
341If optional arguments START and END are specified, only encrypt within
342the region.
343
344If optional PASSPHRASE is not specified, it will be obtained from the
345passphrase 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)
23f87bed 356 "Encrypt the current buffer for RCPTS.
7c700bd1 357
23f87bed 358If optional argument SIGN is non-nil, do a combined sign and encrypt.
7c700bd1 359
23f87bed 360If optional arguments START and END are specified, only encrypt within
7c700bd1
EZ
361the region.
362
363If optional PASSPHRASE is not specified, it will be obtained from the
364passphrase cache or user."
23f87bed
MB
365 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
366 (let* ((start (or start (point-min)))
367 (end (or end (point-max)))
7c700bd1 368 (status (pgg-encrypt-region start end rcpts sign passphrase)))
23f87bed
MB
369 (when (interactive-p)
370 (pgg-display-output-buffer start end status))
371 status))
372
373;;;###autoload
7c700bd1
EZ
374(defun pgg-decrypt-region (start end &optional passphrase)
375 "Decrypt the current region between START and END.
376
377If optional PASSPHRASE is not specified, it will be obtained from the
378passphrase cache or user."
23f87bed
MB
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)
7c700bd1 384 (point-min) (point-max) passphrase))))
23f87bed
MB
385 (when (interactive-p)
386 (pgg-display-output-buffer start end status))
387 status))
388
389;;;###autoload
7c700bd1 390(defun pgg-decrypt (&optional start end passphrase)
23f87bed 391 "Decrypt the current buffer.
7c700bd1 392
23f87bed 393If optional arguments START and END are specified, only decrypt within
7c700bd1
EZ
394the region.
395
396If optional PASSPHRASE is not specified, it will be obtained from the
397passphrase cache or user."
23f87bed
MB
398 (interactive "")
399 (let* ((start (or start (point-min)))
400 (end (or end (point-max)))
7c700bd1 401 (status (pgg-decrypt-region start end passphrase)))
23f87bed
MB
402 (when (interactive-p)
403 (pgg-display-output-buffer start end status))
404 status))
405
406;;;###autoload
7c700bd1 407(defun pgg-sign-region (start end &optional cleartext passphrase)
23f87bed 408 "Make the signature from text between START and END.
7c700bd1 409
23f87bed
MB
410If the optional 3rd argument CLEARTEXT is non-nil, it does not create
411a detached signature.
7c700bd1 412
23f87bed 413If this function is called interactively, CLEARTEXT is enabled
7c700bd1
EZ
414and the the output is displayed.
415
416If optional PASSPHRASE is not specified, it will be obtained from the
417passphrase cache or user."
23f87bed
MB
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)
7c700bd1
EZ
422 (or (interactive-p) cleartext)
423 passphrase))))
23f87bed
MB
424 (when (interactive-p)
425 (pgg-display-output-buffer start end status))
426 status))
427
428;;;###autoload
7c700bd1 429(defun pgg-sign (&optional cleartext start end passphrase)
23f87bed 430 "Sign the current buffer.
7c700bd1 431
23f87bed
MB
432If the optional argument CLEARTEXT is non-nil, it does not create a
433detached signature.
7c700bd1 434
23f87bed
MB
435If optional arguments START and END are specified, only sign data
436within the region.
7c700bd1 437
23f87bed 438If this function is called interactively, CLEARTEXT is enabled
7c700bd1
EZ
439and the the output is displayed.
440
441If optional PASSPHRASE is not specified, it will be obtained from the
442passphrase cache or user."
23f87bed
MB
443 (interactive "")
444 (let* ((start (or start (point-min)))
445 (end (or end (point-max)))
7c700bd1
EZ
446 (status (pgg-sign-region start end
447 (or (interactive-p) cleartext)
448 passphrase)))
23f87bed
MB
449 (when (interactive-p)
450 (pgg-display-output-buffer start end status))
451 status))
7c700bd1 452
23f87bed
MB
453;;;###autoload
454(defun pgg-verify-region (start end &optional signature fetch)
455 "Verify the current region between START and END.
456If the optional 3rd argument SIGNATURE is non-nil, it is treated as
457the detached signature of the current region.
458
459If the optional 4th argument FETCH is non-nil, we attempt to fetch the
460signer'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.
499If the optional argument SIGNATURE is non-nil, it is treated as
500the detached signature of the current region.
501If the optional argument FETCH is non-nil, we attempt to fetch the
502signer's public key from `pgg-default-keyserver-address'.
503If optional arguments START and END are specified, only verify data
504within 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
84861437
MB
515 pgg-errors-buffer)))))
516 status))
23f87bed
MB
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
23f87bed
MB
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