1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999, 2000, 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
33 (autoload 'url-insert-file-contents "url")
35 ;; Don't merge these two `eval-when-compile's.
39 ;;; @ utility functions
42 (defvar pgg-fetch-key-function (if (fboundp 'url-insert-file-contents)
43 (function pgg-fetch-key-with-w3)))
45 (defun pgg-invoke (func scheme &rest args)
47 (require (intern (format "pgg-%s" scheme)))
48 (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
50 (put 'pgg-save-coding-system 'lisp-indent-function 2)
52 (defmacro pgg-save-coding-system (start end &rest body)
54 (let ((buffer (current-buffer)))
56 (let (buffer-undo-list)
57 (insert-buffer-substring buffer ,start ,end)
58 (encode-coding-region (point-min)(point-max)
59 buffer-file-coding-system)
60 (prog1 (save-excursion ,@body)
61 (push nil buffer-undo-list)
62 (ignore-errors (undo))))))
64 (narrow-to-region ,start ,end)
67 (defun pgg-temp-buffer-show-function (buffer)
68 (let ((window (or (get-buffer-window buffer 'visible)
69 (split-window-vertically))))
70 (set-window-buffer window buffer)
71 (shrink-window-if-larger-than-buffer window)))
73 (defun pgg-display-output-buffer (start end status)
76 (delete-region start end)
77 (insert-buffer-substring pgg-output-buffer)
78 (decode-coding-region start (point) buffer-file-coding-system))
79 (let ((temp-buffer-show-function
80 (function pgg-temp-buffer-show-function)))
81 (with-output-to-temp-buffer pgg-echo-buffer
82 (set-buffer standard-output)
83 (insert-buffer-substring pgg-errors-buffer)))))
85 (defun pgg-read-passphrase (prompt &optional key)
86 (when pgg-cache-passphrase
87 (password-read prompt (setq key (pgg-truncate-key-identifier key)))))
89 (defun pgg-add-passphrase-cache (key passphrase)
90 (let ((password-cache-expiry pgg-passphrase-cache-expiry))
91 (password-cache-add (setq key (pgg-truncate-key-identifier key))
94 (defun pgg-remove-passphrase-cache (key)
95 (password-cache-remove key))
97 (defmacro pgg-convert-lbt-region (start end lbt)
98 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
104 (> (marker-position pgg-conversion-end) (point)))
108 (while (re-search-forward "\r$" pgg-conversion-end t)
109 (replace-match ""))))))
111 (put 'pgg-as-lbt 'lisp-indent-function 3)
113 (defmacro pgg-as-lbt (start end lbt &rest body)
114 `(let ((inhibit-read-only t)
117 (pgg-convert-lbt-region ,start ,end ,lbt)
118 (let ((,end (point)))
120 (push nil buffer-undo-list)
121 (ignore-errors (undo))))
123 (put 'pgg-process-when-success 'lisp-indent-function 0)
125 (defmacro pgg-process-when-success (&rest body)
126 `(with-current-buffer pgg-output-buffer
127 (if (zerop (buffer-size)) nil ,@body t)))
129 (defalias 'pgg-make-temp-file
130 (if (fboundp 'make-temp-file)
132 (lambda (prefix &optional dir-flag)
133 (let ((file (expand-file-name
134 (make-temp-name prefix)
135 (if (fboundp 'temp-directory)
137 temporary-file-directory))))
139 (make-directory file))
142 ;;; @ interface functions
146 (defun pgg-encrypt-region (start end rcpts &optional sign)
147 "Encrypt the current region between START and END for RCPTS.
148 If optional argument SIGN is non-nil, do a combined sign and encrypt."
150 (list (region-beginning)(region-end)
151 (split-string (read-string "Recipients: ") "[ \t,]+")))
153 (pgg-save-coding-system start end
154 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
155 (point-min) (point-max) rcpts sign))))
156 (when (interactive-p)
157 (pgg-display-output-buffer start end status))
161 (defun pgg-encrypt (rcpts &optional sign start end)
162 "Encrypt the current buffer for RCPTS.
163 If optional argument SIGN is non-nil, do a combined sign and encrypt.
164 If optional arguments START and END are specified, only encrypt within
166 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
167 (let* ((start (or start (point-min)))
168 (end (or end (point-max)))
169 (status (pgg-encrypt-region start end rcpts sign)))
170 (when (interactive-p)
171 (pgg-display-output-buffer start end status))
175 (defun pgg-decrypt-region (start end)
176 "Decrypt the current region between START and END."
178 (let* ((buf (current-buffer))
180 (pgg-save-coding-system start end
181 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
182 (point-min) (point-max)))))
183 (when (interactive-p)
184 (pgg-display-output-buffer start end status))
188 (defun pgg-decrypt (&optional start end)
189 "Decrypt the current buffer.
190 If optional arguments START and END are specified, only decrypt within
193 (let* ((start (or start (point-min)))
194 (end (or end (point-max)))
195 (status (pgg-decrypt-region start end)))
196 (when (interactive-p)
197 (pgg-display-output-buffer start end status))
201 (defun pgg-sign-region (start end &optional cleartext)
202 "Make the signature from text between START and END.
203 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
204 a detached signature.
205 If this function is called interactively, CLEARTEXT is enabled
206 and the the output is displayed."
208 (let ((status (pgg-save-coding-system start end
209 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
210 (point-min) (point-max)
211 (or (interactive-p) cleartext)))))
212 (when (interactive-p)
213 (pgg-display-output-buffer start end status))
217 (defun pgg-sign (&optional cleartext start end)
218 "Sign the current buffer.
219 If the optional argument CLEARTEXT is non-nil, it does not create a
221 If optional arguments START and END are specified, only sign data
223 If this function is called interactively, CLEARTEXT is enabled
224 and the the output is displayed."
226 (let* ((start (or start (point-min)))
227 (end (or end (point-max)))
228 (status (pgg-sign-region start end (or (interactive-p) cleartext))))
229 (when (interactive-p)
230 (pgg-display-output-buffer start end status))
234 (defun pgg-verify-region (start end &optional signature fetch)
235 "Verify the current region between START and END.
236 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
237 the detached signature of the current region.
239 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
240 signer's public key from `pgg-default-keyserver-address'."
243 (if (null signature) nil
245 (buffer-disable-undo)
246 (if (fboundp 'set-buffer-multibyte)
247 (set-buffer-multibyte nil))
248 (insert-file-contents signature)
249 (cdr (assq 2 (pgg-decode-armor-region
250 (point-min)(point-max)))))))
251 (key (cdr (assq 'key-identifier packet)))
255 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
256 (null (pgg-lookup-key key))
257 (or fetch (interactive-p))
258 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
260 (or (cdr (assq 'preferred-key-server packet))
261 pgg-default-keyserver-address))
262 (pgg-fetch-key keyserver key))
264 (pgg-save-coding-system start end
265 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
266 (point-min) (point-max) signature)))
267 (when (interactive-p)
268 (let ((temp-buffer-show-function
269 (function pgg-temp-buffer-show-function)))
270 (with-output-to-temp-buffer pgg-echo-buffer
271 (set-buffer standard-output)
272 (insert-buffer-substring (if status pgg-output-buffer
273 pgg-errors-buffer)))))
277 (defun pgg-verify (&optional signature fetch start end)
278 "Verify the current buffer.
279 If the optional argument SIGNATURE is non-nil, it is treated as
280 the detached signature of the current region.
281 If the optional argument FETCH is non-nil, we attempt to fetch the
282 signer's public key from `pgg-default-keyserver-address'.
283 If optional arguments START and END are specified, only verify data
286 (let* ((start (or start (point-min)))
287 (end (or end (point-max)))
288 (status (pgg-verify-region start end signature fetch)))
289 (when (interactive-p)
290 (let ((temp-buffer-show-function
291 (function pgg-temp-buffer-show-function)))
292 (with-output-to-temp-buffer pgg-echo-buffer
293 (set-buffer standard-output)
294 (insert-buffer-substring (if status pgg-output-buffer
295 pgg-errors-buffer)))))
299 (defun pgg-insert-key ()
300 "Insert the ASCII armored public key."
302 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
305 (defun pgg-snarf-keys-region (start end)
306 "Import public keys in the current region between START and END."
308 (pgg-save-coding-system start end
309 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
313 (defun pgg-snarf-keys ()
314 "Import public keys in the current buffer."
316 (pgg-snarf-keys-region (point-min) (point-max)))
318 (defun pgg-lookup-key (string &optional type)
319 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
321 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
323 (defun pgg-insert-url-with-w3 (url)
325 (let (buffer-file-name)
326 (url-insert-file-contents url))))
328 (defvar pgg-insert-url-extra-arguments nil)
329 (defvar pgg-insert-url-program nil)
331 (defun pgg-insert-url-with-program (url)
332 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
337 (apply #'start-process " *PGG url*" (current-buffer)
338 pgg-insert-url-program (nconc args (list url))))
339 (set-process-sentinel process #'ignore)
340 (while (eq 'run (process-status process))
341 (accept-process-output process 5))
342 (delete-process process)
343 (if (and process (eq 'run (process-status process)))
344 (interrupt-process process))
347 (defun pgg-fetch-key (keyserver key)
348 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
349 (with-current-buffer (get-buffer-create pgg-output-buffer)
350 (buffer-disable-undo)
352 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
353 (substring keyserver 0 (1- (match-end 0))))))
355 (funcall pgg-insert-url-function
357 (format "http://%s:11371/pks/lookup?op=get&search=%s"
359 (when (re-search-forward "^-+BEGIN" nil 'last)
360 (delete-region (point-min) (match-beginning 0))
361 (when (re-search-forward "^-+END" nil t)
362 (delete-region (progn (end-of-line) (point))
366 (insert-buffer-substring pgg-output-buffer)
367 (pgg-snarf-keys-region (point-min)(point-max)))))))
372 ;;; arch-tag: 9cc705dd-1e6a-4c90-8dce-c3561f9a2cf4