1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999, 2000, 2003 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
32 (autoload 'run-at-time "timer")
34 ;; Don't merge these two `eval-when-compile's.
37 ;; Fixme: This would be better done with an autoload for
38 ;; `url-insert-file-contents', and the url stuff rationalized.
39 ;; (`locate-library' can say whether the url code is available.)
45 ;; Fixme: Avoid this and use mm-make-temp-file (especially for
46 ;; something sensitive like pgp).
47 (defvar pgg-temporary-file-directory
48 (cond ((fboundp 'temp-directory) (temp-directory))
49 ((boundp 'temporary-file-directory) temporary-file-directory)
52 ;;; @ utility functions
55 (defvar pgg-fetch-key-function (if (fboundp 'url-insert-file-contents)
56 (function pgg-fetch-key-with-w3)))
58 (defun pgg-invoke (func scheme &rest args)
60 (require (intern (format "pgg-%s" scheme)))
61 (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
63 (put 'pgg-save-coding-system 'lisp-indent-function 2)
65 (defmacro pgg-save-coding-system (start end &rest body)
67 (let ((buffer (current-buffer)))
69 (let (buffer-undo-list)
70 (insert-buffer-substring buffer ,start ,end)
71 (encode-coding-region (point-min)(point-max)
72 buffer-file-coding-system)
73 (prog1 (save-excursion ,@body)
74 (push nil buffer-undo-list)
75 (ignore-errors (undo))))))
77 (narrow-to-region ,start ,end)
80 (defun pgg-temp-buffer-show-function (buffer)
81 (let ((window (or (get-buffer-window buffer 'visible)
82 (split-window-vertically))))
83 (set-window-buffer window buffer)
84 (shrink-window-if-larger-than-buffer window)))
86 (defun pgg-display-output-buffer (start end status)
89 (delete-region start end)
90 (insert-buffer-substring pgg-output-buffer)
91 (decode-coding-region start (point) buffer-file-coding-system))
92 (let ((temp-buffer-show-function
93 (function pgg-temp-buffer-show-function)))
94 (with-output-to-temp-buffer pgg-echo-buffer
95 (set-buffer standard-output)
96 (insert-buffer-substring pgg-errors-buffer)))))
98 (defvar pgg-passphrase-cache (make-vector 7 0))
100 (defun pgg-read-passphrase (prompt &optional key)
101 (or (and pgg-cache-passphrase
102 key (setq key (pgg-truncate-key-identifier key))
103 (symbol-value (intern-soft key pgg-passphrase-cache)))
104 (read-passwd prompt)))
106 (defun pgg-add-passphrase-cache (key passphrase)
107 (setq key (pgg-truncate-key-identifier key))
108 (set (intern key pgg-passphrase-cache)
110 (run-at-time pgg-passphrase-cache-expiry nil
111 #'pgg-remove-passphrase-cache
114 (defun pgg-remove-passphrase-cache (key)
115 (let ((passphrase (symbol-value (intern-soft key pgg-passphrase-cache))))
117 (fillarray passphrase ?_)
118 (unintern key pgg-passphrase-cache))))
120 (defmacro pgg-convert-lbt-region (start end lbt)
121 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
127 (> (marker-position pgg-conversion-end) (point)))
131 (while (re-search-forward "\r$" pgg-conversion-end t)
132 (replace-match ""))))))
134 (put 'pgg-as-lbt 'lisp-indent-function 3)
136 (defmacro pgg-as-lbt (start end lbt &rest body)
137 `(let ((inhibit-read-only t)
140 (pgg-convert-lbt-region ,start ,end ,lbt)
141 (let ((,end (point)))
143 (push nil buffer-undo-list)
144 (ignore-errors (undo))))
146 (put 'pgg-process-when-success 'lisp-indent-function 0)
148 (defmacro pgg-process-when-success (&rest body)
149 `(with-current-buffer pgg-output-buffer
150 (if (zerop (buffer-size)) nil ,@body t)))
152 ;;; @ interface functions
156 (defun pgg-encrypt-region (start end rcpts &optional sign)
157 "Encrypt the current region between START and END for RCPTS.
158 If optional argument SIGN is non-nil, do a combined sign and encrypt."
160 (list (region-beginning)(region-end)
161 (split-string (read-string "Recipients: ") "[ \t,]+")))
163 (pgg-save-coding-system start end
164 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
165 (point-min) (point-max) rcpts sign))))
166 (when (interactive-p)
167 (pgg-display-output-buffer start end status))
171 (defun pgg-encrypt (rcpts &optional sign start end)
172 "Encrypt the current buffer for RCPTS.
173 If optional argument SIGN is non-nil, do a combined sign and encrypt.
174 If optional arguments START and END are specified, only encrypt within
176 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
177 (let* ((start (or start (point-min)))
178 (end (or end (point-max)))
179 (status (pgg-encrypt-region start end rcpts sign)))
180 (when (interactive-p)
181 (pgg-display-output-buffer start end status))
185 (defun pgg-decrypt-region (start end)
186 "Decrypt the current region between START and END."
188 (let* ((buf (current-buffer))
189 (packet (cdr (assq 1 (with-temp-buffer
190 (insert-buffer-substring buf)
191 (pgg-decode-armor-region
192 (point-min) (point-max))))))
193 (key (cdr (assq 'key-identifier packet)))
196 (concat "0x" (pgg-truncate-key-identifier key))
197 pgg-default-user-id))
199 (pgg-save-coding-system start end
200 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
201 (point-min) (point-max)))))
202 (when (interactive-p)
203 (pgg-display-output-buffer start end status))
207 (defun pgg-decrypt (&optional start end)
208 "Decrypt the current buffer.
209 If optional arguments START and END are specified, only decrypt within
212 (let* ((start (or start (point-min)))
213 (end (or end (point-max)))
214 (status (pgg-decrypt-region start end)))
215 (when (interactive-p)
216 (pgg-display-output-buffer start end status))
220 (defun pgg-sign-region (start end &optional cleartext)
221 "Make the signature from text between START and END.
222 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
223 a detached signature.
224 If this function is called interactively, CLEARTEXT is enabled
225 and the the output is displayed."
227 (let ((status (pgg-save-coding-system start end
228 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
229 (point-min) (point-max)
230 (or (interactive-p) cleartext)))))
231 (when (interactive-p)
232 (pgg-display-output-buffer start end status))
236 (defun pgg-sign (&optional cleartext start end)
237 "Sign the current buffer.
238 If the optional argument CLEARTEXT is non-nil, it does not create a
240 If optional arguments START and END are specified, only sign data
242 If this function is called interactively, CLEARTEXT is enabled
243 and the the output is displayed."
245 (let* ((start (or start (point-min)))
246 (end (or end (point-max)))
247 (status (pgg-sign-region start end (or (interactive-p) cleartext))))
248 (when (interactive-p)
249 (pgg-display-output-buffer start end status))
253 (defun pgg-verify-region (start end &optional signature fetch)
254 "Verify the current region between START and END.
255 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
256 the detached signature of the current region.
258 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
259 signer's public key from `pgg-default-keyserver-address'."
262 (if (null signature) nil
264 (buffer-disable-undo)
265 (if (fboundp 'set-buffer-multibyte)
266 (set-buffer-multibyte nil))
267 (insert-file-contents signature)
268 (cdr (assq 2 (pgg-decode-armor-region
269 (point-min)(point-max)))))))
270 (key (cdr (assq 'key-identifier packet)))
274 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
275 (null (pgg-lookup-key key))
276 (or fetch (interactive-p))
277 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
279 (or (cdr (assq 'preferred-key-server packet))
280 pgg-default-keyserver-address))
281 (pgg-fetch-key keyserver key))
283 (pgg-save-coding-system start end
284 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
285 (point-min) (point-max) signature)))
286 (when (interactive-p)
287 (let ((temp-buffer-show-function
288 (function pgg-temp-buffer-show-function)))
289 (with-output-to-temp-buffer pgg-echo-buffer
290 (set-buffer standard-output)
291 (insert-buffer-substring (if status pgg-output-buffer
292 pgg-errors-buffer)))))
296 (defun pgg-verify (&optional signature fetch start end)
297 "Verify the current buffer.
298 If the optional argument SIGNATURE is non-nil, it is treated as
299 the detached signature of the current region.
300 If the optional argument FETCH is non-nil, we attempt to fetch the
301 signer's public key from `pgg-default-keyserver-address'.
302 If optional arguments START and END are specified, only verify data
305 (let* ((start (or start (point-min)))
306 (end (or end (point-max)))
307 (status (pgg-verify-region start end signature fetch)))
308 (when (interactive-p)
309 (let ((temp-buffer-show-function
310 (function pgg-temp-buffer-show-function)))
311 (with-output-to-temp-buffer pgg-echo-buffer
312 (set-buffer standard-output)
313 (insert-buffer-substring (if status pgg-output-buffer
314 pgg-errors-buffer)))))))
317 (defun pgg-insert-key ()
318 "Insert the ASCII armored public key."
320 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
323 (defun pgg-snarf-keys-region (start end)
324 "Import public keys in the current region between START and END."
326 (pgg-save-coding-system start end
327 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
331 (defun pgg-snarf-keys ()
332 "Import public keys in the current buffer."
334 (pgg-snarf-keys-region (point-min) (point-max)))
336 (defun pgg-lookup-key (string &optional type)
337 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
339 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
341 (defun pgg-insert-url-with-w3 (url)
345 (let (buffer-file-name)
346 (url-insert-file-contents url))))
348 (defvar pgg-insert-url-extra-arguments nil)
349 (defvar pgg-insert-url-program nil)
351 (defun pgg-insert-url-with-program (url)
352 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
357 (apply #'start-process " *PGG url*" (current-buffer)
358 pgg-insert-url-program (nconc args (list url))))
359 (set-process-sentinel process #'ignore)
360 (while (eq 'run (process-status process))
361 (accept-process-output process 5))
362 (delete-process process)
363 (if (and process (eq 'run (process-status process)))
364 (interrupt-process process))
367 (defun pgg-fetch-key (keyserver key)
368 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
369 (with-current-buffer (get-buffer-create pgg-output-buffer)
370 (buffer-disable-undo)
372 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
373 (substring keyserver 0 (1- (match-end 0))))))
375 (funcall pgg-insert-url-function
377 (format "http://%s:11371/pks/lookup?op=get&search=%s"
379 (when (re-search-forward "^-+BEGIN" nil 'last)
380 (delete-region (point-min) (match-beginning 0))
381 (when (re-search-forward "^-+END" nil t)
382 (delete-region (progn (end-of-line) (point))
386 (insert-buffer-substring pgg-output-buffer)
387 (pgg-snarf-keys-region (point-min)(point-max)))))))