1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999, 2000, 2003, 2004 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.
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 ;;; @ utility functions
48 (defvar pgg-fetch-key-function (if (fboundp 'url-insert-file-contents)
49 (function pgg-fetch-key-with-w3)))
51 (defun pgg-invoke (func scheme &rest args)
53 (require (intern (format "pgg-%s" scheme)))
54 (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
56 (put 'pgg-save-coding-system 'lisp-indent-function 2)
58 (defmacro pgg-save-coding-system (start end &rest body)
60 (let ((buffer (current-buffer)))
62 (let (buffer-undo-list)
63 (insert-buffer-substring buffer ,start ,end)
64 (encode-coding-region (point-min)(point-max)
65 buffer-file-coding-system)
66 (prog1 (save-excursion ,@body)
67 (push nil buffer-undo-list)
68 (ignore-errors (undo))))))
70 (narrow-to-region ,start ,end)
73 (defun pgg-temp-buffer-show-function (buffer)
74 (let ((window (or (get-buffer-window buffer 'visible)
75 (split-window-vertically))))
76 (set-window-buffer window buffer)
77 (shrink-window-if-larger-than-buffer window)))
79 (defun pgg-display-output-buffer (start end status)
82 (delete-region start end)
83 (insert-buffer-substring pgg-output-buffer)
84 (decode-coding-region start (point) buffer-file-coding-system))
85 (let ((temp-buffer-show-function
86 (function pgg-temp-buffer-show-function)))
87 (with-output-to-temp-buffer pgg-echo-buffer
88 (set-buffer standard-output)
89 (insert-buffer-substring pgg-errors-buffer)))))
91 (defun pgg-read-passphrase (prompt &optional key)
92 (when pgg-cache-passphrase
93 (password-read prompt (setq key (pgg-truncate-key-identifier key)))))
95 (defun pgg-add-passphrase-cache (key passphrase)
96 (let ((password-cache-expiry pgg-passphrase-cache-expiry))
97 (password-cache-add (setq key (pgg-truncate-key-identifier key))
100 (defun pgg-remove-passphrase-cache (key)
101 (password-cache-remove key))
103 (defmacro pgg-convert-lbt-region (start end lbt)
104 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
110 (> (marker-position pgg-conversion-end) (point)))
114 (while (re-search-forward "\r$" pgg-conversion-end t)
115 (replace-match ""))))))
117 (put 'pgg-as-lbt 'lisp-indent-function 3)
119 (defmacro pgg-as-lbt (start end lbt &rest body)
120 `(let ((inhibit-read-only t)
123 (pgg-convert-lbt-region ,start ,end ,lbt)
124 (let ((,end (point)))
126 (push nil buffer-undo-list)
127 (ignore-errors (undo))))
129 (put 'pgg-process-when-success 'lisp-indent-function 0)
131 (defmacro pgg-process-when-success (&rest body)
132 `(with-current-buffer pgg-output-buffer
133 (if (zerop (buffer-size)) nil ,@body t)))
135 (defalias 'pgg-make-temp-file
136 (if (fboundp 'make-temp-file)
138 (lambda (prefix &optional dir-flag)
139 (let ((file (expand-file-name
140 (make-temp-name prefix)
141 (if (fboundp 'temp-directory)
143 temporary-file-directory))))
145 (make-directory file))
148 ;;; @ interface functions
152 (defun pgg-encrypt-region (start end rcpts &optional sign)
153 "Encrypt the current region between START and END for RCPTS.
154 If optional argument SIGN is non-nil, do a combined sign and encrypt."
156 (list (region-beginning)(region-end)
157 (split-string (read-string "Recipients: ") "[ \t,]+")))
159 (pgg-save-coding-system start end
160 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
161 (point-min) (point-max) rcpts sign))))
162 (when (interactive-p)
163 (pgg-display-output-buffer start end status))
167 (defun pgg-encrypt (rcpts &optional sign start end)
168 "Encrypt the current buffer for RCPTS.
169 If optional argument SIGN is non-nil, do a combined sign and encrypt.
170 If optional arguments START and END are specified, only encrypt within
172 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
173 (let* ((start (or start (point-min)))
174 (end (or end (point-max)))
175 (status (pgg-encrypt-region start end rcpts sign)))
176 (when (interactive-p)
177 (pgg-display-output-buffer start end status))
181 (defun pgg-decrypt-region (start end)
182 "Decrypt the current region between START and END."
184 (let* ((buf (current-buffer))
186 (pgg-save-coding-system start end
187 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
188 (point-min) (point-max)))))
189 (when (interactive-p)
190 (pgg-display-output-buffer start end status))
194 (defun pgg-decrypt (&optional start end)
195 "Decrypt the current buffer.
196 If optional arguments START and END are specified, only decrypt within
199 (let* ((start (or start (point-min)))
200 (end (or end (point-max)))
201 (status (pgg-decrypt-region start end)))
202 (when (interactive-p)
203 (pgg-display-output-buffer start end status))
207 (defun pgg-sign-region (start end &optional cleartext)
208 "Make the signature from text between START and END.
209 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
210 a detached signature.
211 If this function is called interactively, CLEARTEXT is enabled
212 and the the output is displayed."
214 (let ((status (pgg-save-coding-system start end
215 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
216 (point-min) (point-max)
217 (or (interactive-p) cleartext)))))
218 (when (interactive-p)
219 (pgg-display-output-buffer start end status))
223 (defun pgg-sign (&optional cleartext start end)
224 "Sign the current buffer.
225 If the optional argument CLEARTEXT is non-nil, it does not create a
227 If optional arguments START and END are specified, only sign data
229 If this function is called interactively, CLEARTEXT is enabled
230 and the the output is displayed."
232 (let* ((start (or start (point-min)))
233 (end (or end (point-max)))
234 (status (pgg-sign-region start end (or (interactive-p) cleartext))))
235 (when (interactive-p)
236 (pgg-display-output-buffer start end status))
240 (defun pgg-verify-region (start end &optional signature fetch)
241 "Verify the current region between START and END.
242 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
243 the detached signature of the current region.
245 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
246 signer's public key from `pgg-default-keyserver-address'."
249 (if (null signature) nil
251 (buffer-disable-undo)
252 (if (fboundp 'set-buffer-multibyte)
253 (set-buffer-multibyte nil))
254 (insert-file-contents signature)
255 (cdr (assq 2 (pgg-decode-armor-region
256 (point-min)(point-max)))))))
257 (key (cdr (assq 'key-identifier packet)))
261 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
262 (null (pgg-lookup-key key))
263 (or fetch (interactive-p))
264 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
266 (or (cdr (assq 'preferred-key-server packet))
267 pgg-default-keyserver-address))
268 (pgg-fetch-key keyserver key))
270 (pgg-save-coding-system start end
271 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
272 (point-min) (point-max) signature)))
273 (when (interactive-p)
274 (let ((temp-buffer-show-function
275 (function pgg-temp-buffer-show-function)))
276 (with-output-to-temp-buffer pgg-echo-buffer
277 (set-buffer standard-output)
278 (insert-buffer-substring (if status pgg-output-buffer
279 pgg-errors-buffer)))))
283 (defun pgg-verify (&optional signature fetch start end)
284 "Verify the current buffer.
285 If the optional argument SIGNATURE is non-nil, it is treated as
286 the detached signature of the current region.
287 If the optional argument FETCH is non-nil, we attempt to fetch the
288 signer's public key from `pgg-default-keyserver-address'.
289 If optional arguments START and END are specified, only verify data
292 (let* ((start (or start (point-min)))
293 (end (or end (point-max)))
294 (status (pgg-verify-region start end signature fetch)))
295 (when (interactive-p)
296 (let ((temp-buffer-show-function
297 (function pgg-temp-buffer-show-function)))
298 (with-output-to-temp-buffer pgg-echo-buffer
299 (set-buffer standard-output)
300 (insert-buffer-substring (if status pgg-output-buffer
301 pgg-errors-buffer)))))))
304 (defun pgg-insert-key ()
305 "Insert the ASCII armored public key."
307 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
310 (defun pgg-snarf-keys-region (start end)
311 "Import public keys in the current region between START and END."
313 (pgg-save-coding-system start end
314 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
318 (defun pgg-snarf-keys ()
319 "Import public keys in the current buffer."
321 (pgg-snarf-keys-region (point-min) (point-max)))
323 (defun pgg-lookup-key (string &optional type)
324 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
326 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
328 (defun pgg-insert-url-with-w3 (url)
332 (let (buffer-file-name)
333 (url-insert-file-contents url))))
335 (defvar pgg-insert-url-extra-arguments nil)
336 (defvar pgg-insert-url-program nil)
338 (defun pgg-insert-url-with-program (url)
339 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
344 (apply #'start-process " *PGG url*" (current-buffer)
345 pgg-insert-url-program (nconc args (list url))))
346 (set-process-sentinel process #'ignore)
347 (while (eq 'run (process-status process))
348 (accept-process-output process 5))
349 (delete-process process)
350 (if (and process (eq 'run (process-status process)))
351 (interrupt-process process))
354 (defun pgg-fetch-key (keyserver key)
355 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
356 (with-current-buffer (get-buffer-create pgg-output-buffer)
357 (buffer-disable-undo)
359 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
360 (substring keyserver 0 (1- (match-end 0))))))
362 (funcall pgg-insert-url-function
364 (format "http://%s:11371/pks/lookup?op=get&search=%s"
366 (when (re-search-forward "^-+BEGIN" nil 'last)
367 (delete-region (point-min) (match-beginning 0))
368 (when (re-search-forward "^-+END" nil t)
369 (delete-region (progn (end-of-line) (point))
373 (insert-buffer-substring pgg-output-buffer)
374 (pgg-snarf-keys-region (point-min)(point-max)))))))