1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Symmetric encryption added by: Sascha Wilde <wilde@sha-bang.de>
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 2, or (at your option)
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; 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.
34 (autoload 'run-at-time
"timer")
36 ;; Don't merge these two `eval-when-compile's.
40 ;;; @ utility functions
43 (defun pgg-invoke (func scheme
&rest args
)
45 (require (intern (format "pgg-%s" scheme
)))
46 (apply 'funcall
(intern (format "pgg-%s-%s" scheme func
)) args
)))
48 (put 'pgg-save-coding-system
'lisp-indent-function
2)
50 (defmacro pgg-save-coding-system
(start end
&rest body
)
52 (let ((buffer (current-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))))))
62 (narrow-to-region ,start
,end
)
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
)))
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.
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."
79 (pgg-situate-output start end
)
80 (pgg-display-error-buffer)))
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
))
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
))))
96 (defvar pgg-passphrase-cache
(make-vector 7 0))
98 (defvar pgg-pending-timers
(make-vector 7 0)
99 "Hash table for managing scheduled pgg cache management timers.
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.")
104 (defun pgg-read-passphrase (prompt &optional key notruncate
)
105 "Using PROMPT, obtain passphrase for KEY from cache or user.
107 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
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
)))
115 (defun pgg-read-passphrase-from-cache (key &optional notruncate
)
116 "Obtain passphrase for KEY from time-limited passphrase cache.
118 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
121 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
122 regulate cache behavior."
123 (and pgg-cache-passphrase
125 (setq key
(pgg-truncate-key-identifier key
)))
126 (symbol-value (intern-soft key pgg-passphrase-cache
))))
128 (defun pgg-add-passphrase-to-cache (key passphrase
&optional notruncate
)
129 "Associate KEY with PASSPHRASE in time-limited passphrase cache.
131 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
134 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
135 regulate cache behavior."
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
))
142 (cancel-timer old-timer
)
143 (unintern interned-timer-key pgg-pending-timers
))
144 (set (intern key pgg-passphrase-cache
)
146 (set (intern key pgg-pending-timers
)
147 (pgg-run-at-time pgg-passphrase-cache-expiry nil
148 #'pgg-remove-passphrase-from-cache
151 (defun pgg-remove-passphrase-from-cache (key &optional notruncate
)
152 "Omit passphrase associated with KEY in time-limited passphrase cache.
154 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
157 This is a no-op if there is not entry for KEY (eg, it's already expired.
159 The memory for the passphrase is filled with underscores to clear any
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
)))
169 (fillarray passphrase ?_
)
170 (unintern key pgg-passphrase-cache
))
172 (pgg-cancel-timer old-timer
)
173 (unintern interned-timer-key pgg-pending-timers
))))
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
194 (setcar ctime
(1- (car ctime
)))))
196 (itimer (funcall start-itimer
"pgg-run-at-time"
198 (sleep-for 0.1) ;; Accept the timeout interrupt.
200 (> (funcall itimer-value itimer
) 0)
201 (funcall delete-itimer itimer
))))
204 (apply #'start-itimer
"pgg-run-at-time"
205 ,function
(if time
(max time
1e-9) 1e-9)
206 ,repeat nil t
,args
)))
208 (itimers (list nil
)))
211 (apply #'start-itimer
"pgg-run-at-time"
212 (lambda (itimers repeat function
&rest args
)
213 (let ((itimer (car itimers
)))
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
225 (append (list itimer repeat function
) args
)))
228 (lambda (itimer function
&rest args
)
229 (delete-itimer itimer
)
230 (apply function args
)))
231 (set-itimer-function-arguments
233 (append (list itimer function
) args
)))))
234 1e-9 (if time
(max time
1e-9) 1e-9)
235 nil t itimers
,repeat
,function
,args
))))))
238 (if (featurep 'xemacs
)
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'
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
)))
251 (defalias 'pgg-run-at-time
'run-at-time
)
252 (defalias 'pgg-cancel-timer
'cancel-timer
)))
254 (defmacro pgg-convert-lbt-region
(start end lbt
)
255 `(let ((pgg-conversion-end (set-marker (make-marker) ,end
)))
261 (> (marker-position pgg-conversion-end
) (point)))
265 (while (re-search-forward "\r$" pgg-conversion-end t
)
266 (replace-match ""))))))
268 (put 'pgg-as-lbt
'lisp-indent-function
3)
270 (defmacro pgg-as-lbt
(start end lbt
&rest body
)
271 `(let ((inhibit-read-only t
)
274 (pgg-convert-lbt-region ,start
,end
,lbt
)
275 (let ((,end
(point)))
277 (push nil buffer-undo-list
)
278 (ignore-errors (undo))))
280 (put 'pgg-process-when-success
'lisp-indent-function
0)
282 (defmacro pgg-process-when-success
(&rest body
)
283 `(with-current-buffer pgg-output-buffer
284 (if (zerop (buffer-size)) nil
,@body t
)))
286 (defalias 'pgg-make-temp-file
287 (if (fboundp '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
)
294 temporary-file-directory
))))
296 (make-directory file
))
299 ;;; @ interface functions
303 (defun pgg-encrypt-region (start end rcpts
&optional sign passphrase
)
304 "Encrypt the current region between START and END for RCPTS.
306 If optional argument SIGN is non-nil, do a combined sign and encrypt.
308 If optional PASSPHRASE is not specified, it will be obtained from the
309 passphrase cache or user."
311 (list (region-beginning)(region-end)
312 (split-string (read-string "Recipients: ") "[ \t,]+")))
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
))
322 (defun pgg-encrypt-symmetric-region (start end
&optional passphrase
)
323 "Encrypt the current region between START and END symmetric with passphrase.
325 If optional PASSPHRASE is not specified, it will be obtained from the
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
))
338 (defun pgg-encrypt-symmetric (&optional start end passphrase
)
339 "Encrypt the current buffer using a symmetric, rather than key-pair, cipher.
341 If optional arguments START and END are specified, only encrypt within
344 If optional PASSPHRASE is not specified, it will be obtained from the
345 passphrase cache or user."
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
))
355 (defun pgg-encrypt (rcpts &optional sign start end passphrase
)
356 "Encrypt the current buffer for RCPTS.
358 If optional argument SIGN is non-nil, do a combined sign and encrypt.
360 If optional arguments START and END are specified, only encrypt within
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
))
374 (defun pgg-decrypt-region (start end
&optional passphrase
)
375 "Decrypt the current region between START and END.
377 If optional PASSPHRASE is not specified, it will be obtained from the
378 passphrase cache or user."
380 (let* ((buf (current-buffer))
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
))
390 (defun pgg-decrypt (&optional start end passphrase
)
391 "Decrypt the current buffer.
393 If optional arguments START and END are specified, only decrypt within
396 If optional PASSPHRASE is not specified, it will be obtained from the
397 passphrase cache or user."
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
))
407 (defun pgg-sign-region (start end
&optional cleartext passphrase
)
408 "Make the signature from text between START and END.
410 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
411 a detached signature.
413 If this function is called interactively, CLEARTEXT is enabled
414 and the the output is displayed.
416 If optional PASSPHRASE is not specified, it will be obtained from the
417 passphrase cache or user."
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
)
424 (when (interactive-p)
425 (pgg-display-output-buffer start end status
))
429 (defun pgg-sign (&optional cleartext start end passphrase
)
430 "Sign the current buffer.
432 If the optional argument CLEARTEXT is non-nil, it does not create a
435 If optional arguments START and END are specified, only sign data
438 If this function is called interactively, CLEARTEXT is enabled
439 and the the output is displayed.
441 If optional PASSPHRASE is not specified, it will be obtained from the
442 passphrase cache or user."
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
)
449 (when (interactive-p)
450 (pgg-display-output-buffer start end status
))
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.
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'."
463 (if (null signature
) nil
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
)))
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
))
480 (or (cdr (assq 'preferred-key-server packet
))
481 pgg-default-keyserver-address
))
482 (pgg-fetch-key keyserver key
))
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
)))))
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
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
)))))
519 (defun pgg-insert-key ()
520 "Insert the ASCII armored public key."
522 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme
)))
525 (defun pgg-snarf-keys-region (start end
)
526 "Import public keys in the current region between START and END."
528 (pgg-save-coding-system start end
529 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme
)
533 (defun pgg-snarf-keys ()
534 "Import public keys in the current buffer."
536 (pgg-snarf-keys-region (point-min) (point-max)))
538 (defun pgg-lookup-key (string &optional type
)
539 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme
) string type
))
541 (defvar pgg-insert-url-function
(function pgg-insert-url-with-w3
))
543 (defun pgg-insert-url-with-w3 (url)
546 (let (buffer-file-name)
547 (url-insert-file-contents url
))))
549 (defvar pgg-insert-url-extra-arguments nil
)
550 (defvar pgg-insert-url-program nil
)
552 (defun pgg-insert-url-with-program (url)
553 (let ((args (copy-sequence pgg-insert-url-extra-arguments
))
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
))
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)
573 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver
)
574 (substring keyserver
0 (1- (match-end 0))))))
576 (funcall pgg-insert-url-function
578 (format "http://%s:11371/pks/lookup?op=get&search=%s"
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))
587 (insert-buffer-substring pgg-output-buffer
)
588 (pgg-snarf-keys-region (point-min)(point-max)))))))
593 ;;; arch-tag: 9cc705dd-1e6a-4c90-8dce-c3561f9a2cf4