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>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
33 (autoload 'run-at-time "timer")
35 ;; Don't merge these two `eval-when-compile's.
39 ;;; @ utility functions
42 (defun pgg-invoke (func scheme &rest args)
44 (require (intern (format "pgg-%s" scheme)))
45 (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
47 (put 'pgg-save-coding-system 'lisp-indent-function 2)
49 (defmacro pgg-save-coding-system (start end &rest body)
51 (let ((buffer (current-buffer)))
53 (let (buffer-undo-list)
54 (insert-buffer-substring buffer ,start ,end)
55 (encode-coding-region (point-min)(point-max)
56 buffer-file-coding-system)
57 (prog1 (save-excursion ,@body)
58 (push nil buffer-undo-list)
59 (ignore-errors (undo))))))
61 (narrow-to-region ,start ,end)
64 (defun pgg-temp-buffer-show-function (buffer)
65 (let ((window (or (get-buffer-window buffer 'visible)
66 (split-window-vertically))))
67 (set-window-buffer window buffer)
68 (shrink-window-if-larger-than-buffer window)))
70 (defun pgg-display-output-buffer (start end status)
73 (delete-region start end)
74 (insert-buffer-substring pgg-output-buffer)
75 (decode-coding-region start (point) buffer-file-coding-system))
76 (let ((temp-buffer-show-function
77 (function pgg-temp-buffer-show-function)))
78 (with-output-to-temp-buffer pgg-echo-buffer
79 (set-buffer standard-output)
80 (insert-buffer-substring pgg-errors-buffer)))))
82 (defvar pgg-passphrase-cache (make-vector 7 0))
84 (defun pgg-read-passphrase (prompt &optional key)
85 (or (and pgg-cache-passphrase
86 key (setq key (pgg-truncate-key-identifier key))
87 (symbol-value (intern-soft key pgg-passphrase-cache)))
88 (read-passwd prompt)))
91 (defmacro pgg-run-at-time-1 (time repeat function args)
92 (when (featurep 'xemacs)
93 (if (condition-case nil
94 (let ((delete-itimer 'delete-itimer)
95 (itimer-driver-start 'itimer-driver-start)
96 (itimer-value 'itimer-value)
97 (start-itimer 'start-itimer))
98 (unless (or (symbol-value 'itimer-process)
99 (symbol-value 'itimer-timer))
100 (funcall itimer-driver-start))
101 ;; Check whether there is a bug to which the difference of
102 ;; the present time and the time when the itimer driver was
103 ;; woken up is subtracted from the initial itimer value.
104 (let* ((inhibit-quit t)
105 (ctime (current-time))
106 (itimer-timer-last-wakeup
109 (setcar ctime (1- (car ctime)))))
111 (itimer (funcall start-itimer "pgg-run-at-time"
113 (sleep-for 0.1) ;; Accept the timeout interrupt.
115 (> (funcall itimer-value itimer) 0)
116 (funcall delete-itimer itimer))))
119 (apply #'start-itimer "pgg-run-at-time"
120 ,function (if time (max time 1e-9) 1e-9)
121 ,repeat nil t ,args)))
123 (itimers (list nil)))
126 (apply #'start-itimer "pgg-run-at-time"
127 (lambda (itimers repeat function &rest args)
128 (let ((itimer (car itimers)))
133 (lambda (itimer repeat function &rest args)
134 (set-itimer-restart itimer repeat)
135 (set-itimer-function itimer function)
136 (set-itimer-function-arguments itimer args)
137 (apply function args)))
138 (set-itimer-function-arguments
140 (append (list itimer repeat function) args)))
143 (lambda (itimer function &rest args)
144 (delete-itimer itimer)
145 (apply function args)))
146 (set-itimer-function-arguments
148 (append (list itimer function) args)))))
149 1e-9 (if time (max time 1e-9) 1e-9)
150 nil t itimers ,repeat ,function ,args))))))
153 (if (featurep 'xemacs)
154 (defun pgg-run-at-time (time repeat function &rest args)
155 "Emulating function run as `run-at-time'.
156 TIME should be nil meaning now, or a number of seconds from now.
157 Return an itimer object which can be used in either `delete-itimer'
159 (pgg-run-at-time-1 time repeat function args))
160 (defalias 'pgg-run-at-time 'run-at-time)))
162 (defun pgg-add-passphrase-cache (key passphrase)
163 (setq key (pgg-truncate-key-identifier key))
164 (set (intern key pgg-passphrase-cache)
166 (pgg-run-at-time pgg-passphrase-cache-expiry nil
167 #'pgg-remove-passphrase-cache
170 (defun pgg-remove-passphrase-cache (key)
171 (let ((passphrase (symbol-value (intern-soft key pgg-passphrase-cache))))
173 (fillarray passphrase ?_)
174 (unintern key pgg-passphrase-cache))))
176 (defmacro pgg-convert-lbt-region (start end lbt)
177 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
183 (> (marker-position pgg-conversion-end) (point)))
187 (while (re-search-forward "\r$" pgg-conversion-end t)
188 (replace-match ""))))))
190 (put 'pgg-as-lbt 'lisp-indent-function 3)
192 (defmacro pgg-as-lbt (start end lbt &rest body)
193 `(let ((inhibit-read-only t)
196 (pgg-convert-lbt-region ,start ,end ,lbt)
197 (let ((,end (point)))
199 (push nil buffer-undo-list)
200 (ignore-errors (undo))))
202 (put 'pgg-process-when-success 'lisp-indent-function 0)
204 (defmacro pgg-process-when-success (&rest body)
205 `(with-current-buffer pgg-output-buffer
206 (if (zerop (buffer-size)) nil ,@body t)))
208 (defalias 'pgg-make-temp-file
209 (if (fboundp 'make-temp-file)
211 (lambda (prefix &optional dir-flag)
212 (let ((file (expand-file-name
213 (make-temp-name prefix)
214 (if (fboundp 'temp-directory)
216 temporary-file-directory))))
218 (make-directory file))
221 ;;; @ interface functions
225 (defun pgg-encrypt-region (start end rcpts &optional sign)
226 "Encrypt the current region between START and END for RCPTS.
227 If optional argument SIGN is non-nil, do a combined sign and encrypt."
229 (list (region-beginning)(region-end)
230 (split-string (read-string "Recipients: ") "[ \t,]+")))
232 (pgg-save-coding-system start end
233 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
234 (point-min) (point-max) rcpts sign))))
235 (when (interactive-p)
236 (pgg-display-output-buffer start end status))
240 (defun pgg-encrypt (rcpts &optional sign start end)
241 "Encrypt the current buffer for RCPTS.
242 If optional argument SIGN is non-nil, do a combined sign and encrypt.
243 If optional arguments START and END are specified, only encrypt within
245 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
246 (let* ((start (or start (point-min)))
247 (end (or end (point-max)))
248 (status (pgg-encrypt-region start end rcpts sign)))
249 (when (interactive-p)
250 (pgg-display-output-buffer start end status))
254 (defun pgg-decrypt-region (start end)
255 "Decrypt the current region between START and END."
257 (let* ((buf (current-buffer))
259 (pgg-save-coding-system start end
260 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
261 (point-min) (point-max)))))
262 (when (interactive-p)
263 (pgg-display-output-buffer start end status))
267 (defun pgg-decrypt (&optional start end)
268 "Decrypt the current buffer.
269 If optional arguments START and END are specified, only decrypt within
272 (let* ((start (or start (point-min)))
273 (end (or end (point-max)))
274 (status (pgg-decrypt-region start end)))
275 (when (interactive-p)
276 (pgg-display-output-buffer start end status))
280 (defun pgg-sign-region (start end &optional cleartext)
281 "Make the signature from text between START and END.
282 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
283 a detached signature.
284 If this function is called interactively, CLEARTEXT is enabled
285 and the the output is displayed."
287 (let ((status (pgg-save-coding-system start end
288 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
289 (point-min) (point-max)
290 (or (interactive-p) cleartext)))))
291 (when (interactive-p)
292 (pgg-display-output-buffer start end status))
296 (defun pgg-sign (&optional cleartext start end)
297 "Sign the current buffer.
298 If the optional argument CLEARTEXT is non-nil, it does not create a
300 If optional arguments START and END are specified, only sign data
302 If this function is called interactively, CLEARTEXT is enabled
303 and the the output is displayed."
305 (let* ((start (or start (point-min)))
306 (end (or end (point-max)))
307 (status (pgg-sign-region start end (or (interactive-p) cleartext))))
308 (when (interactive-p)
309 (pgg-display-output-buffer start end status))
313 (defun pgg-verify-region (start end &optional signature fetch)
314 "Verify the current region between START and END.
315 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
316 the detached signature of the current region.
318 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
319 signer's public key from `pgg-default-keyserver-address'."
322 (if (null signature) nil
324 (buffer-disable-undo)
325 (if (fboundp 'set-buffer-multibyte)
326 (set-buffer-multibyte nil))
327 (insert-file-contents signature)
328 (cdr (assq 2 (pgg-decode-armor-region
329 (point-min)(point-max)))))))
330 (key (cdr (assq 'key-identifier packet)))
334 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
335 (null (pgg-lookup-key key))
336 (or fetch (interactive-p))
337 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
339 (or (cdr (assq 'preferred-key-server packet))
340 pgg-default-keyserver-address))
341 (pgg-fetch-key keyserver key))
343 (pgg-save-coding-system start end
344 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
345 (point-min) (point-max) signature)))
346 (when (interactive-p)
347 (let ((temp-buffer-show-function
348 (function pgg-temp-buffer-show-function)))
349 (with-output-to-temp-buffer pgg-echo-buffer
350 (set-buffer standard-output)
351 (insert-buffer-substring (if status pgg-output-buffer
352 pgg-errors-buffer)))))
356 (defun pgg-verify (&optional signature fetch start end)
357 "Verify the current buffer.
358 If the optional argument SIGNATURE is non-nil, it is treated as
359 the detached signature of the current region.
360 If the optional argument FETCH is non-nil, we attempt to fetch the
361 signer's public key from `pgg-default-keyserver-address'.
362 If optional arguments START and END are specified, only verify data
365 (let* ((start (or start (point-min)))
366 (end (or end (point-max)))
367 (status (pgg-verify-region start end signature fetch)))
368 (when (interactive-p)
369 (let ((temp-buffer-show-function
370 (function pgg-temp-buffer-show-function)))
371 (with-output-to-temp-buffer pgg-echo-buffer
372 (set-buffer standard-output)
373 (insert-buffer-substring (if status pgg-output-buffer
374 pgg-errors-buffer)))))
378 (defun pgg-insert-key ()
379 "Insert the ASCII armored public key."
381 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
384 (defun pgg-snarf-keys-region (start end)
385 "Import public keys in the current region between START and END."
387 (pgg-save-coding-system start end
388 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
392 (defun pgg-snarf-keys ()
393 "Import public keys in the current buffer."
395 (pgg-snarf-keys-region (point-min) (point-max)))
397 (defun pgg-lookup-key (string &optional type)
398 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
400 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
402 (defun pgg-insert-url-with-w3 (url)
405 (let (buffer-file-name)
406 (url-insert-file-contents url))))
408 (defvar pgg-insert-url-extra-arguments nil)
409 (defvar pgg-insert-url-program nil)
411 (defun pgg-insert-url-with-program (url)
412 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
417 (apply #'start-process " *PGG url*" (current-buffer)
418 pgg-insert-url-program (nconc args (list url))))
419 (set-process-sentinel process #'ignore)
420 (while (eq 'run (process-status process))
421 (accept-process-output process 5))
422 (delete-process process)
423 (if (and process (eq 'run (process-status process)))
424 (interrupt-process process))
427 (defun pgg-fetch-key (keyserver key)
428 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
429 (with-current-buffer (get-buffer-create pgg-output-buffer)
430 (buffer-disable-undo)
432 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
433 (substring keyserver 0 (1- (match-end 0))))))
435 (funcall pgg-insert-url-function
437 (format "http://%s:11371/pks/lookup?op=get&search=%s"
439 (when (re-search-forward "^-+BEGIN" nil 'last)
440 (delete-region (point-min) (match-beginning 0))
441 (when (re-search-forward "^-+END" nil t)
442 (delete-region (progn (end-of-line) (point))
446 (insert-buffer-substring pgg-output-buffer)
447 (pgg-snarf-keys-region (point-min)(point-max)))))))
452 ;;; arch-tag: 9cc705dd-1e6a-4c90-8dce-c3561f9a2cf4