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>
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/>.
28 ;; This file is on its way to obsolescence, waiting for allout.el to
35 (autoload 'run-at-time
"timer")
37 ;; Don't merge these two `eval-when-compile's.
39 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
)))
42 ;;; @ utility functions
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
64 (setcar ctime
(1- (car ctime
)))))
66 (itimer (funcall start-itimer
"pgg-run-at-time"
68 (sleep-for 0.1) ;; Accept the timeout interrupt.
70 (> (funcall itimer-value itimer
) 0)
71 (funcall delete-itimer itimer
))))
74 (apply #'start-itimer
"pgg-run-at-time"
75 ,function
(if time
(max time
1e-9) 1e-9)
81 (apply #'start-itimer
"pgg-run-at-time"
82 (lambda (itimers repeat function
&rest args
)
83 (let ((itimer (car itimers
)))
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
95 (append (list itimer repeat function
) args
)))
98 (lambda (itimer function
&rest args
)
99 (delete-itimer itimer
)
100 (apply function args
)))
101 (set-itimer-function-arguments
103 (append (list itimer function
) args
)))))
104 1e-9 (if time
(max time
1e-9) 1e-9)
105 nil t itimers
,repeat
,function
,args
)))))))
108 (if (featurep 'xemacs
)
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'
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
)
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)))
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))))))
142 (narrow-to-region ,start
,end
)
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."
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
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
201 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
202 regulate cache behavior."
203 (and pgg-cache-passphrase
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
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
))
222 (cancel-timer old-timer
)
223 (unintern interned-timer-key pgg-pending-timers
))
224 (set (intern key pgg-passphrase-cache
)
226 (set (intern key pgg-pending-timers
)
227 (pgg-run-at-time pgg-passphrase-cache-expiry nil
228 #'pgg-remove-passphrase-from-cache
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
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
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
)))
256 (pgg-clear-string passphrase
)
257 (unintern key pgg-passphrase-cache
))
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
)))
269 (> (marker-position pgg-conversion-end
) (point)))
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
)
282 (pgg-convert-lbt-region ,start
,end
,lbt
)
283 (let ((,end
(point)))
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
)
297 (lambda (prefix &optional dir-flag
)
298 (let ((file (expand-file-name
299 (make-temp-name prefix
)
300 (if (fboundp 'temp-directory
)
302 temporary-file-directory
))))
304 (make-directory file
))
307 ;;; @ interface functions
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."
319 (list (region-beginning)(region-end)
320 (split-string (read-string "Recipients: ") "[ \t,]+")))
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
))
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
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
))
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
352 If optional PASSPHRASE is not specified, it will be obtained from the
353 passphrase cache or user."
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
))
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
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
))
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."
388 (let* ((buf (current-buffer))
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
))
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
404 If optional PASSPHRASE is not specified, it will be obtained from the
405 passphrase cache or user."
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
))
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."
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
)
433 (when (called-interactively-p 'interactive
)
434 (pgg-display-output-buffer start end status
))
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
444 If optional arguments START and END are specified, only sign data
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."
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
)
459 (when (called-interactively-p 'interactive
)
460 (pgg-display-output-buffer start end status
))
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'."
473 (if (null signature
) nil
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
)))
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
))
490 (or (cdr (assq 'preferred-key-server packet
))
491 pgg-default-keyserver-address
))
492 (pgg-fetch-key keyserver key
))
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
)))))
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
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
)))))
529 (defun pgg-insert-key ()
530 "Insert the ASCII armored public key."
532 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme
)))
535 (defun pgg-snarf-keys-region (start end
)
536 "Import public keys in the current region between START and END."
538 (pgg-save-coding-system start end
539 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme
)
543 (defun pgg-snarf-keys ()
544 "Import public keys in the current buffer."
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)
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
))
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
))
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)
583 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver
)
584 (substring keyserver
0 (1- (match-end 0))))))
586 (funcall pgg-insert-url-function
588 (format "http://%s:11371/pks/lookup?op=get&search=%s"
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))
597 (insert-buffer-substring pgg-output-buffer
)
598 (pgg-snarf-keys-region (point-min)(point-max)))))))