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 ;; Define it as a null macro for Emacs in order to suppress a byte
47 ;; compile warning that Emacs 21 issues.
48 (defmacro pgg-run-at-time-1
(time repeat function args
)
49 (when (featurep 'xemacs
)
50 (if (condition-case nil
51 (let ((delete-itimer 'delete-itimer
)
52 (itimer-driver-start 'itimer-driver-start
)
53 (itimer-value 'itimer-value
)
54 (start-itimer 'start-itimer
))
55 (unless (or (symbol-value 'itimer-process
)
56 (symbol-value 'itimer-timer
))
57 (funcall itimer-driver-start
))
58 ;; Check whether there is a bug to which the difference of
59 ;; the present time and the time when the itimer driver was
60 ;; woken up is subtracted from the initial itimer value.
61 (let* ((inhibit-quit t
)
62 (ctime (current-time))
63 (itimer-timer-last-wakeup
66 (setcar ctime
(1- (car ctime
)))))
68 (itimer (funcall start-itimer
"pgg-run-at-time"
70 (sleep-for 0.1) ;; Accept the timeout interrupt.
72 (> (funcall itimer-value itimer
) 0)
73 (funcall delete-itimer itimer
))))
76 (apply #'start-itimer
"pgg-run-at-time"
77 ,function
(if time
(max time
1e-9) 1e-9)
78 ,repeat nil t
,args
)))
83 (apply #'start-itimer
"pgg-run-at-time"
84 (lambda (itimers repeat function
&rest args
)
85 (let ((itimer (car itimers
)))
90 (lambda (itimer repeat function
&rest args
)
91 (set-itimer-restart itimer repeat
)
92 (set-itimer-function itimer function
)
93 (set-itimer-function-arguments itimer args
)
94 (apply function args
)))
95 (set-itimer-function-arguments
97 (append (list itimer repeat function
) args
)))
100 (lambda (itimer function
&rest args
)
101 (delete-itimer itimer
)
102 (apply function args
)))
103 (set-itimer-function-arguments
105 (append (list itimer function
) args
)))))
106 1e-9 (if time
(max time
1e-9) 1e-9)
107 nil t itimers
,repeat
,function
,args
))))))
110 (if (featurep 'xemacs
)
112 (defun pgg-run-at-time (time repeat function
&rest args
)
113 "Emulating function run as `run-at-time'.
114 TIME should be nil meaning now, or a number of seconds from now.
115 Return an itimer object which can be used in either `delete-itimer'
117 (pgg-run-at-time-1 time repeat function args
))
118 (defun pgg-cancel-timer (timer)
119 "Emulate cancel-timer for xemacs."
120 (let ((delete-itimer 'delete-itimer
))
121 (funcall delete-itimer timer
))))
122 (defalias 'pgg-run-at-time
'run-at-time
)
123 (defalias 'pgg-cancel-timer
'cancel-timer
)))
125 (defun pgg-invoke (func scheme
&rest args
)
127 (require (intern (format "pgg-%s" scheme
)))
128 (apply 'funcall
(intern (format "pgg-%s-%s" scheme func
)) args
)))
130 (put 'pgg-save-coding-system
'lisp-indent-function
2)
132 (defmacro pgg-save-coding-system
(start end
&rest body
)
133 `(if (called-interactively-p 'interactive
)
134 (let ((buffer (current-buffer)))
136 (let (buffer-undo-list)
137 (insert-buffer-substring buffer
,start
,end
)
138 (encode-coding-region (point-min)(point-max)
139 buffer-file-coding-system
)
140 (prog1 (save-excursion ,@body
)
141 (push nil buffer-undo-list
)
142 (ignore-errors (undo))))))
144 (narrow-to-region ,start
,end
)
147 (defun pgg-temp-buffer-show-function (buffer)
148 (let ((window (or (get-buffer-window buffer
'visible
)
149 (split-window-vertically))))
150 (set-window-buffer window buffer
)
151 (shrink-window-if-larger-than-buffer window
)))
153 ;; XXX `pgg-display-output-buffer' is a horrible name for this function.
154 ;; It should be something like `pgg-situate-output-or-display-error'.
155 (defun pgg-display-output-buffer (start end status
)
156 "Situate en/decryption results or pop up an error buffer.
158 Text from START to END is replaced by contents of output buffer if STATUS
159 is true, or else the output buffer is displayed."
161 (pgg-situate-output start end
)
162 (pgg-display-error-buffer)))
164 (defun pgg-situate-output (start end
)
165 "Place en/decryption result in place of current text from START to END."
166 (delete-region start end
)
167 (insert-buffer-substring pgg-output-buffer
)
168 (decode-coding-region start
(point) buffer-file-coding-system
))
170 (defun pgg-display-error-buffer ()
171 "Pop up an error buffer indicating the reason for an en/decryption failure."
172 (let ((temp-buffer-show-function
173 (function pgg-temp-buffer-show-function
)))
174 (with-output-to-temp-buffer pgg-echo-buffer
175 (set-buffer standard-output
)
176 (insert-buffer-substring pgg-errors-buffer
))))
178 (defvar pgg-passphrase-cache
(make-vector 7 0))
180 (defvar pgg-pending-timers
(make-vector 7 0)
181 "Hash table for managing scheduled pgg cache management timers.
183 We associate key and timer, so the timer can be cancelled if a new
184 timeout for the key is set while an old one is still pending.")
186 (defun pgg-read-passphrase (prompt &optional key notruncate
)
187 "Using PROMPT, obtain passphrase for KEY from cache or user.
189 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
192 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
193 regulate cache behavior."
194 (or (pgg-read-passphrase-from-cache key notruncate
)
195 (read-passwd prompt
)))
197 (defun pgg-read-passphrase-from-cache (key &optional notruncate
)
198 "Obtain passphrase for KEY from time-limited passphrase cache.
200 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
203 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
204 regulate cache behavior."
205 (and pgg-cache-passphrase
207 (setq key
(pgg-truncate-key-identifier key
)))
208 (symbol-value (intern-soft key pgg-passphrase-cache
))))
210 (defun pgg-add-passphrase-to-cache (key passphrase
&optional notruncate
)
211 "Associate KEY with PASSPHRASE in time-limited passphrase cache.
213 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
216 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
217 regulate cache behavior."
219 (let* ((key (if notruncate key
(pgg-truncate-key-identifier key
)))
220 (interned-timer-key (intern-soft key pgg-pending-timers
))
221 (old-timer (symbol-value interned-timer-key
))
224 (cancel-timer old-timer
)
225 (unintern interned-timer-key pgg-pending-timers
))
226 (set (intern key pgg-passphrase-cache
)
228 (set (intern key pgg-pending-timers
)
229 (pgg-run-at-time pgg-passphrase-cache-expiry nil
230 #'pgg-remove-passphrase-from-cache
233 (if (fboundp 'clear-string
)
234 (defalias 'pgg-clear-string
'clear-string
)
235 (defun pgg-clear-string (string)
236 (fillarray string ?_
)))
238 (declare-function pgg-clear-string
"pgg" (string))
240 (defun pgg-remove-passphrase-from-cache (key &optional notruncate
)
241 "Omit passphrase associated with KEY in time-limited passphrase cache.
243 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
246 This is a no-op if there is not entry for KEY (eg, it's already expired.
248 The memory for the passphrase is filled with underscores to clear any
251 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
252 regulate cache behavior."
253 (let* ((passphrase (pgg-read-passphrase-from-cache key notruncate
))
254 (key (if notruncate key
(pgg-truncate-key-identifier key
)))
255 (interned-timer-key (intern-soft key pgg-pending-timers
))
256 (old-timer (symbol-value interned-timer-key
)))
258 (pgg-clear-string passphrase
)
259 (unintern key pgg-passphrase-cache
))
261 (pgg-cancel-timer old-timer
)
262 (unintern interned-timer-key pgg-pending-timers
))))
264 (defmacro pgg-convert-lbt-region
(start end lbt
)
265 `(let ((pgg-conversion-end (set-marker (make-marker) ,end
)))
271 (> (marker-position pgg-conversion-end
) (point)))
275 (while (re-search-forward "\r$" pgg-conversion-end t
)
276 (replace-match ""))))))
278 (put 'pgg-as-lbt
'lisp-indent-function
3)
280 (defmacro pgg-as-lbt
(start end lbt
&rest body
)
281 `(let ((inhibit-read-only t
)
284 (pgg-convert-lbt-region ,start
,end
,lbt
)
285 (let ((,end
(point)))
287 (push nil buffer-undo-list
)
288 (ignore-errors (undo))))
290 (put 'pgg-process-when-success
'lisp-indent-function
0)
292 (defmacro pgg-process-when-success
(&rest body
)
293 `(with-current-buffer pgg-output-buffer
294 (if (zerop (buffer-size)) nil
,@body t
)))
296 (defalias 'pgg-make-temp-file
297 (if (fboundp 'make-temp-file
)
299 (lambda (prefix &optional dir-flag
)
300 (let ((file (expand-file-name
301 (make-temp-name prefix
)
302 (if (fboundp 'temp-directory
)
304 temporary-file-directory
))))
306 (make-directory file
))
309 ;;; @ interface functions
313 (defun pgg-encrypt-region (start end rcpts
&optional sign passphrase
)
314 "Encrypt the current region between START and END for RCPTS.
316 If optional argument SIGN is non-nil, do a combined sign and encrypt.
318 If optional PASSPHRASE is not specified, it will be obtained from the
319 passphrase cache or user."
321 (list (region-beginning)(region-end)
322 (split-string (read-string "Recipients: ") "[ \t,]+")))
324 (pgg-save-coding-system start end
325 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme
)
326 (point-min) (point-max) rcpts sign passphrase
))))
327 (when (called-interactively-p 'interactive
)
328 (pgg-display-output-buffer start end status
))
332 (defun pgg-encrypt-symmetric-region (start end
&optional passphrase
)
333 "Encrypt the current region between START and END symmetric with passphrase.
335 If optional PASSPHRASE is not specified, it will be obtained from the
339 (pgg-save-coding-system start end
340 (pgg-invoke "encrypt-symmetric-region"
341 (or pgg-scheme pgg-default-scheme
)
342 (point-min) (point-max) passphrase
))))
343 (when (called-interactively-p 'interactive
)
344 (pgg-display-output-buffer start end status
))
348 (defun pgg-encrypt-symmetric (&optional start end passphrase
)
349 "Encrypt the current buffer using a symmetric, rather than key-pair, cipher.
351 If optional arguments START and END are specified, only encrypt within
354 If optional PASSPHRASE is not specified, it will be obtained from the
355 passphrase cache or user."
357 (let* ((start (or start
(point-min)))
358 (end (or end
(point-max)))
359 (status (pgg-encrypt-symmetric-region start end passphrase
)))
360 (when (called-interactively-p 'interactive
)
361 (pgg-display-output-buffer start end status
))
365 (defun pgg-encrypt (rcpts &optional sign start end passphrase
)
366 "Encrypt the current buffer for RCPTS.
368 If optional argument SIGN is non-nil, do a combined sign and encrypt.
370 If optional arguments START and END are specified, only encrypt within
373 If optional PASSPHRASE is not specified, it will be obtained from the
374 passphrase cache or user."
375 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
376 (let* ((start (or start
(point-min)))
377 (end (or end
(point-max)))
378 (status (pgg-encrypt-region start end rcpts sign passphrase
)))
379 (when (called-interactively-p 'interactive
)
380 (pgg-display-output-buffer start end status
))
384 (defun pgg-decrypt-region (start end
&optional passphrase
)
385 "Decrypt the current region between START and END.
387 If optional PASSPHRASE is not specified, it will be obtained from the
388 passphrase cache or user."
390 (let* ((buf (current-buffer))
392 (pgg-save-coding-system start end
393 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme
)
394 (point-min) (point-max) passphrase
))))
395 (when (called-interactively-p 'interactive
)
396 (pgg-display-output-buffer start end status
))
400 (defun pgg-decrypt (&optional start end passphrase
)
401 "Decrypt the current buffer.
403 If optional arguments START and END are specified, only decrypt within
406 If optional PASSPHRASE is not specified, it will be obtained from the
407 passphrase cache or user."
409 (let* ((start (or start
(point-min)))
410 (end (or end
(point-max)))
411 (status (pgg-decrypt-region start end passphrase
)))
412 (when (called-interactively-p 'interactive
)
413 (pgg-display-output-buffer start end status
))
417 (defun pgg-sign-region (start end
&optional cleartext passphrase
)
418 "Make the signature from text between START and END.
420 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
421 a detached signature.
423 If this function is called interactively, CLEARTEXT is enabled
424 and the output is displayed.
426 If optional PASSPHRASE is not specified, it will be obtained from the
427 passphrase cache or user."
429 (let ((status (pgg-save-coding-system start end
430 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme
)
431 (point-min) (point-max)
432 (or (called-interactively-p 'interactive
)
435 (when (called-interactively-p 'interactive
)
436 (pgg-display-output-buffer start end status
))
440 (defun pgg-sign (&optional cleartext start end passphrase
)
441 "Sign the current buffer.
443 If the optional argument CLEARTEXT is non-nil, it does not create a
446 If optional arguments START and END are specified, only sign data
449 If this function is called interactively, CLEARTEXT is enabled
450 and the output is displayed.
452 If optional PASSPHRASE is not specified, it will be obtained from the
453 passphrase cache or user."
455 (let* ((start (or start
(point-min)))
456 (end (or end
(point-max)))
457 (status (pgg-sign-region start end
458 (or (called-interactively-p 'interactive
)
461 (when (called-interactively-p 'interactive
)
462 (pgg-display-output-buffer start end status
))
466 (defun pgg-verify-region (start end
&optional signature fetch
)
467 "Verify the current region between START and END.
468 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
469 the detached signature of the current region.
471 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
472 signer's public key from `pgg-default-keyserver-address'."
475 (if (null signature
) nil
477 (buffer-disable-undo)
478 (if (fboundp 'set-buffer-multibyte
)
479 (set-buffer-multibyte nil
))
480 (insert-file-contents signature
)
481 (cdr (assq 2 (pgg-decode-armor-region
482 (point-min)(point-max)))))))
483 (key (cdr (assq 'key-identifier packet
)))
487 (setq key
(concat "0x" (pgg-truncate-key-identifier key
)))
488 (null (pgg-lookup-key key
))
489 (or fetch
(called-interactively-p 'interactive
))
490 (y-or-n-p (format "Key %s not found; attempt to fetch? " key
))
492 (or (cdr (assq 'preferred-key-server packet
))
493 pgg-default-keyserver-address
))
494 (pgg-fetch-key keyserver key
))
496 (pgg-save-coding-system start end
497 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme
)
498 (point-min) (point-max) signature
)))
499 (when (called-interactively-p 'interactive
)
500 (let ((temp-buffer-show-function
501 (function pgg-temp-buffer-show-function
)))
502 (with-output-to-temp-buffer pgg-echo-buffer
503 (set-buffer standard-output
)
504 (insert-buffer-substring (if status pgg-output-buffer
505 pgg-errors-buffer
)))))
509 (defun pgg-verify (&optional signature fetch start end
)
510 "Verify the current buffer.
511 If the optional argument SIGNATURE is non-nil, it is treated as
512 the detached signature of the current region.
513 If the optional argument FETCH is non-nil, we attempt to fetch the
514 signer's public key from `pgg-default-keyserver-address'.
515 If optional arguments START and END are specified, only verify data
518 (let* ((start (or start
(point-min)))
519 (end (or end
(point-max)))
520 (status (pgg-verify-region start end signature fetch
)))
521 (when (called-interactively-p 'interactive
)
522 (let ((temp-buffer-show-function
523 (function pgg-temp-buffer-show-function
)))
524 (with-output-to-temp-buffer pgg-echo-buffer
525 (set-buffer standard-output
)
526 (insert-buffer-substring (if status pgg-output-buffer
527 pgg-errors-buffer
)))))
531 (defun pgg-insert-key ()
532 "Insert the ASCII armored public key."
534 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme
)))
537 (defun pgg-snarf-keys-region (start end
)
538 "Import public keys in the current region between START and END."
540 (pgg-save-coding-system start end
541 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme
)
545 (defun pgg-snarf-keys ()
546 "Import public keys in the current buffer."
548 (pgg-snarf-keys-region (point-min) (point-max)))
550 (defun pgg-lookup-key (string &optional type
)
551 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme
) string type
))
553 (defvar pgg-insert-url-function
(function pgg-insert-url-with-w3
))
555 (defun pgg-insert-url-with-w3 (url)
558 (let (buffer-file-name)
559 (url-insert-file-contents url
))))
561 (defvar pgg-insert-url-extra-arguments nil
)
562 (defvar pgg-insert-url-program nil
)
564 (defun pgg-insert-url-with-program (url)
565 (let ((args (copy-sequence pgg-insert-url-extra-arguments
))
570 (apply #'start-process
" *PGG url*" (current-buffer)
571 pgg-insert-url-program
(nconc args
(list url
))))
572 (set-process-sentinel process
#'ignore
)
573 (while (eq 'run
(process-status process
))
574 (accept-process-output process
5))
575 (delete-process process
)
576 (if (and process
(eq 'run
(process-status process
)))
577 (interrupt-process process
))
580 (defun pgg-fetch-key (keyserver key
)
581 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
582 (with-current-buffer (get-buffer-create pgg-output-buffer
)
583 (buffer-disable-undo)
585 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver
)
586 (substring keyserver
0 (1- (match-end 0))))))
588 (funcall pgg-insert-url-function
590 (format "http://%s:11371/pks/lookup?op=get&search=%s"
592 (when (re-search-forward "^-+BEGIN" nil
'last
)
593 (delete-region (point-min) (match-beginning 0))
594 (when (re-search-forward "^-+END" nil t
)
595 (delete-region (progn (end-of-line) (point))
599 (insert-buffer-substring pgg-output-buffer
)
600 (pgg-snarf-keys-region (point-min)(point-max)))))))
605 ;; arch-tag: 9cc705dd-1e6a-4c90-8dce-c3561f9a2cf4