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.
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 (defun pgg-read-passphrase (prompt &optional key)
83 (when pgg-cache-passphrase
84 (password-read prompt (setq key (pgg-truncate-key-identifier key)))))
86 (defun pgg-add-passphrase-cache (key passphrase)
87 (let ((password-cache-expiry pgg-passphrase-cache-expiry))
88 (password-cache-add (setq key (pgg-truncate-key-identifier key))
91 (defun pgg-remove-passphrase-cache (key)
92 (password-cache-remove key))
94 (defmacro pgg-convert-lbt-region (start end lbt)
95 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
101 (> (marker-position pgg-conversion-end) (point)))
105 (while (re-search-forward "\r$" pgg-conversion-end t)
106 (replace-match ""))))))
108 (put 'pgg-as-lbt 'lisp-indent-function 3)
110 (defmacro pgg-as-lbt (start end lbt &rest body)
111 `(let ((inhibit-read-only t)
114 (pgg-convert-lbt-region ,start ,end ,lbt)
115 (let ((,end (point)))
117 (push nil buffer-undo-list)
118 (ignore-errors (undo))))
120 (put 'pgg-process-when-success 'lisp-indent-function 0)
122 (defmacro pgg-process-when-success (&rest body)
123 `(with-current-buffer pgg-output-buffer
124 (if (zerop (buffer-size)) nil ,@body t)))
126 (defalias 'pgg-make-temp-file
127 (if (fboundp 'make-temp-file)
129 (lambda (prefix &optional dir-flag)
130 (let ((file (expand-file-name
131 (make-temp-name prefix)
132 (if (fboundp 'temp-directory)
134 temporary-file-directory))))
136 (make-directory file))
139 ;;; @ interface functions
143 (defun pgg-encrypt-region (start end rcpts &optional sign)
144 "Encrypt the current region between START and END for RCPTS.
145 If optional argument SIGN is non-nil, do a combined sign and encrypt."
147 (list (region-beginning)(region-end)
148 (split-string (read-string "Recipients: ") "[ \t,]+")))
150 (pgg-save-coding-system start end
151 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
152 (point-min) (point-max) rcpts sign))))
153 (when (interactive-p)
154 (pgg-display-output-buffer start end status))
158 (defun pgg-encrypt (rcpts &optional sign start end)
159 "Encrypt the current buffer for RCPTS.
160 If optional argument SIGN is non-nil, do a combined sign and encrypt.
161 If optional arguments START and END are specified, only encrypt within
163 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
164 (let* ((start (or start (point-min)))
165 (end (or end (point-max)))
166 (status (pgg-encrypt-region start end rcpts sign)))
167 (when (interactive-p)
168 (pgg-display-output-buffer start end status))
172 (defun pgg-decrypt-region (start end)
173 "Decrypt the current region between START and END."
175 (let* ((buf (current-buffer))
177 (pgg-save-coding-system start end
178 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
179 (point-min) (point-max)))))
180 (when (interactive-p)
181 (pgg-display-output-buffer start end status))
185 (defun pgg-decrypt (&optional start end)
186 "Decrypt the current buffer.
187 If optional arguments START and END are specified, only decrypt within
190 (let* ((start (or start (point-min)))
191 (end (or end (point-max)))
192 (status (pgg-decrypt-region start end)))
193 (when (interactive-p)
194 (pgg-display-output-buffer start end status))
198 (defun pgg-sign-region (start end &optional cleartext)
199 "Make the signature from text between START and END.
200 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
201 a detached signature.
202 If this function is called interactively, CLEARTEXT is enabled
203 and the the output is displayed."
205 (let ((status (pgg-save-coding-system start end
206 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
207 (point-min) (point-max)
208 (or (interactive-p) cleartext)))))
209 (when (interactive-p)
210 (pgg-display-output-buffer start end status))
214 (defun pgg-sign (&optional cleartext start end)
215 "Sign the current buffer.
216 If the optional argument CLEARTEXT is non-nil, it does not create a
218 If optional arguments START and END are specified, only sign data
220 If this function is called interactively, CLEARTEXT is enabled
221 and the the output is displayed."
223 (let* ((start (or start (point-min)))
224 (end (or end (point-max)))
225 (status (pgg-sign-region start end (or (interactive-p) cleartext))))
226 (when (interactive-p)
227 (pgg-display-output-buffer start end status))
231 (defun pgg-verify-region (start end &optional signature fetch)
232 "Verify the current region between START and END.
233 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
234 the detached signature of the current region.
236 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
237 signer's public key from `pgg-default-keyserver-address'."
240 (if (null signature) nil
242 (buffer-disable-undo)
243 (if (fboundp 'set-buffer-multibyte)
244 (set-buffer-multibyte nil))
245 (insert-file-contents signature)
246 (cdr (assq 2 (pgg-decode-armor-region
247 (point-min)(point-max)))))))
248 (key (cdr (assq 'key-identifier packet)))
252 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
253 (null (pgg-lookup-key key))
254 (or fetch (interactive-p))
255 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
257 (or (cdr (assq 'preferred-key-server packet))
258 pgg-default-keyserver-address))
259 (pgg-fetch-key keyserver key))
261 (pgg-save-coding-system start end
262 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
263 (point-min) (point-max) signature)))
264 (when (interactive-p)
265 (let ((temp-buffer-show-function
266 (function pgg-temp-buffer-show-function)))
267 (with-output-to-temp-buffer pgg-echo-buffer
268 (set-buffer standard-output)
269 (insert-buffer-substring (if status pgg-output-buffer
270 pgg-errors-buffer)))))
274 (defun pgg-verify (&optional signature fetch start end)
275 "Verify the current buffer.
276 If the optional argument SIGNATURE is non-nil, it is treated as
277 the detached signature of the current region.
278 If the optional argument FETCH is non-nil, we attempt to fetch the
279 signer's public key from `pgg-default-keyserver-address'.
280 If optional arguments START and END are specified, only verify data
283 (let* ((start (or start (point-min)))
284 (end (or end (point-max)))
285 (status (pgg-verify-region start end signature fetch)))
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-insert-key ()
297 "Insert the ASCII armored public key."
299 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
302 (defun pgg-snarf-keys-region (start end)
303 "Import public keys in the current region between START and END."
305 (pgg-save-coding-system start end
306 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
310 (defun pgg-snarf-keys ()
311 "Import public keys in the current buffer."
313 (pgg-snarf-keys-region (point-min) (point-max)))
315 (defun pgg-lookup-key (string &optional type)
316 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
318 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
320 (defun pgg-insert-url-with-w3 (url)
323 (let (buffer-file-name)
324 (url-insert-file-contents url))))
326 (defvar pgg-insert-url-extra-arguments nil)
327 (defvar pgg-insert-url-program nil)
329 (defun pgg-insert-url-with-program (url)
330 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
335 (apply #'start-process " *PGG url*" (current-buffer)
336 pgg-insert-url-program (nconc args (list url))))
337 (set-process-sentinel process #'ignore)
338 (while (eq 'run (process-status process))
339 (accept-process-output process 5))
340 (delete-process process)
341 (if (and process (eq 'run (process-status process)))
342 (interrupt-process process))
345 (defun pgg-fetch-key (keyserver key)
346 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
347 (with-current-buffer (get-buffer-create pgg-output-buffer)
348 (buffer-disable-undo)
350 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
351 (substring keyserver 0 (1- (match-end 0))))))
353 (funcall pgg-insert-url-function
355 (format "http://%s:11371/pks/lookup?op=get&search=%s"
357 (when (re-search-forward "^-+BEGIN" nil 'last)
358 (delete-region (point-min) (match-beginning 0))
359 (when (re-search-forward "^-+END" nil t)
360 (delete-region (progn (end-of-line) (point))
364 (insert-buffer-substring pgg-output-buffer)
365 (pgg-snarf-keys-region (point-min)(point-max)))))))
370 ;;; arch-tag: 9cc705dd-1e6a-4c90-8dce-c3561f9a2cf4