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.
33 ;; Don't merge these two `eval-when-compile's.
36 ;; Fixme: This would be better done with an autoload for
37 ;; `url-insert-file-contents', and the url stuff rationalized.
38 ;; (`locate-library' can say whether the url code is available.)
44 ;; Fixme: Avoid this and use mm-make-temp-file (especially for
45 ;; something sensitive like pgp).
46 (defvar pgg-temporary-file-directory
47 (cond ((fboundp 'temp-directory) (temp-directory))
48 ((boundp 'temporary-file-directory) temporary-file-directory)
51 ;;; @ utility functions
54 (defvar pgg-fetch-key-function (if (fboundp 'url-insert-file-contents)
55 (function pgg-fetch-key-with-w3)))
57 (defun pgg-invoke (func scheme &rest args)
59 (require (intern (format "pgg-%s" scheme)))
60 (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
62 (put 'pgg-save-coding-system 'lisp-indent-function 2)
64 (defmacro pgg-save-coding-system (start end &rest body)
66 (let ((buffer (current-buffer)))
68 (let (buffer-undo-list)
69 (insert-buffer-substring buffer ,start ,end)
70 (encode-coding-region (point-min)(point-max)
71 buffer-file-coding-system)
72 (prog1 (save-excursion ,@body)
73 (push nil buffer-undo-list)
74 (ignore-errors (undo))))))
76 (narrow-to-region ,start ,end)
79 (defun pgg-temp-buffer-show-function (buffer)
80 (let ((window (split-window-vertically)))
81 (set-window-buffer window buffer)
82 (shrink-window-if-larger-than-buffer window)))
84 (defun pgg-display-output-buffer (start end status)
87 (delete-region start end)
88 (insert-buffer-substring pgg-output-buffer)
89 (decode-coding-region start (point) buffer-file-coding-system))
90 (let ((temp-buffer-show-function
91 (function pgg-temp-buffer-show-function)))
92 (with-output-to-temp-buffer pgg-echo-buffer
93 (set-buffer standard-output)
94 (insert-buffer-substring pgg-errors-buffer)))))
96 (defvar pgg-passphrase-cache (make-vector 7 0))
98 (defun pgg-read-passphrase (prompt &optional key)
99 (or (and pgg-cache-passphrase
100 key (setq key (pgg-truncate-key-identifier key))
101 (symbol-value (intern-soft key pgg-passphrase-cache)))
102 (read-passwd prompt)))
104 (defun pgg-add-passphrase-cache (key passphrase)
105 (setq key (pgg-truncate-key-identifier key))
106 (set (intern key pgg-passphrase-cache)
108 (run-at-time pgg-passphrase-cache-expiry nil
109 #'pgg-remove-passphrase-cache
112 (defun pgg-remove-passphrase-cache (key)
113 (let ((passphrase (symbol-value (intern-soft key pgg-passphrase-cache))))
115 (fillarray passphrase ?_)
116 (unintern key pgg-passphrase-cache))))
118 (defmacro pgg-convert-lbt-region (start end lbt)
119 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
125 (> (marker-position pgg-conversion-end) (point)))
129 (while (re-search-forward "\r$" pgg-conversion-end t)
130 (replace-match ""))))))
132 (put 'pgg-as-lbt 'lisp-indent-function 3)
134 (defmacro pgg-as-lbt (start end lbt &rest body)
135 `(let ((inhibit-read-only t)
138 (pgg-convert-lbt-region ,start ,end ,lbt)
139 (let ((,end (point)))
141 (push nil buffer-undo-list)
142 (ignore-errors (undo))))
144 (put 'pgg-process-when-success 'lisp-indent-function 0)
146 (defmacro pgg-process-when-success (&rest body)
147 `(with-current-buffer pgg-output-buffer
148 (if (zerop (buffer-size)) nil ,@body t)))
150 ;;; @ interface functions
154 (defun pgg-encrypt-region (start end rcpts &optional sign)
155 "Encrypt the current region between START and END for RCPTS.
156 If optional argument SIGN is non-nil, do a combined sign and encrypt."
158 (list (region-beginning)(region-end)
159 (split-string (read-string "Recipients: ") "[ \t,]+")))
161 (pgg-save-coding-system start end
162 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
163 (point-min) (point-max) rcpts sign))))
164 (when (interactive-p)
165 (pgg-display-output-buffer start end status))
169 (defun pgg-encrypt (rcpts &optional sign start end)
170 "Encrypt the current buffer for RCPTS.
171 If optional argument SIGN is non-nil, do a combined sign and encrypt.
172 If optional arguments START and END are specified, only encrypt within
174 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
175 (let* ((start (or start (point-min)))
176 (end (or end (point-max)))
177 (status (pgg-encrypt-region start end rcpts sign)))
178 (when (interactive-p)
179 (pgg-display-output-buffer start end status))
183 (defun pgg-decrypt-region (start end)
184 "Decrypt the current region between START and END."
186 (let* ((buf (current-buffer))
187 (packet (cdr (assq 1 (with-temp-buffer
188 (insert-buffer-substring buf)
189 (pgg-decode-armor-region
190 (point-min) (point-max))))))
191 (key (cdr (assq 'key-identifier packet)))
194 (concat "0x" (pgg-truncate-key-identifier key))
195 pgg-default-user-id))
197 (pgg-save-coding-system start end
198 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
199 (point-min) (point-max)))))
200 (when (interactive-p)
201 (pgg-display-output-buffer start end status))
205 (defun pgg-decrypt (&optional start end)
206 "Decrypt the current buffer.
207 If optional arguments START and END are specified, only decrypt within
210 (let* ((start (or start (point-min)))
211 (end (or end (point-max)))
212 (status (pgg-decrypt-region start end)))
213 (when (interactive-p)
214 (pgg-display-output-buffer start end status))
218 (defun pgg-sign-region (start end &optional cleartext)
219 "Make the signature from text between START and END.
220 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
221 a detached signature.
222 If this function is called interactively, CLEARTEXT is enabled
223 and the the output is displayed."
225 (let ((status (pgg-save-coding-system start end
226 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
227 (point-min) (point-max)
228 (or (interactive-p) cleartext)))))
229 (when (interactive-p)
230 (pgg-display-output-buffer start end status))
234 (defun pgg-sign (&optional cleartext start end)
235 "Sign the current buffer.
236 If the optional argument CLEARTEXT is non-nil, it does not create a
238 If optional arguments START and END are specified, only sign data
240 If this function is called interactively, CLEARTEXT is enabled
241 and the the output is displayed."
243 (let* ((start (or start (point-min)))
244 (end (or end (point-max)))
245 (status (pgg-sign-region start end (or (interactive-p) cleartext))))
246 (when (interactive-p)
247 (pgg-display-output-buffer start end status))
251 (defun pgg-verify-region (start end &optional signature fetch)
252 "Verify the current region between START and END.
253 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
254 the detached signature of the current region.
256 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
257 signer's public key from `pgg-default-keyserver-address'."
260 (if (null signature) nil
262 (buffer-disable-undo)
263 (if (fboundp 'set-buffer-multibyte)
264 (set-buffer-multibyte nil))
265 (insert-file-contents signature)
266 (cdr (assq 2 (pgg-decode-armor-region
267 (point-min)(point-max)))))))
268 (key (cdr (assq 'key-identifier packet)))
272 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
273 (null (pgg-lookup-key key))
274 (or fetch (interactive-p))
275 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
277 (or (cdr (assq 'preferred-key-server packet))
278 pgg-default-keyserver-address))
279 (pgg-fetch-key keyserver key))
281 (pgg-save-coding-system start end
282 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
283 (point-min) (point-max) signature)))
284 (when (interactive-p)
285 (let ((temp-buffer-show-function
286 (function pgg-temp-buffer-show-function)))
287 (with-output-to-temp-buffer pgg-echo-buffer
288 (set-buffer standard-output)
289 (insert-buffer-substring (if status pgg-output-buffer
290 pgg-errors-buffer)))))
294 (defun pgg-verify (&optional signature fetch start end)
295 "Verify the current buffer.
296 If the optional argument SIGNATURE is non-nil, it is treated as
297 the detached signature of the current region.
298 If the optional argument FETCH is non-nil, we attempt to fetch the
299 signer's public key from `pgg-default-keyserver-address'.
300 If optional arguments START and END are specified, only verify data
303 (let* ((start (or start (point-min)))
304 (end (or end (point-max)))
305 (status (pgg-verify-region start end signature fetch)))
306 (when (interactive-p)
307 (let ((temp-buffer-show-function
308 (function pgg-temp-buffer-show-function)))
309 (with-output-to-temp-buffer pgg-echo-buffer
310 (set-buffer standard-output)
311 (insert-buffer-substring (if status pgg-output-buffer
312 pgg-errors-buffer)))))))
315 (defun pgg-insert-key ()
316 "Insert the ASCII armored public key."
318 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
321 (defun pgg-snarf-keys-region (start end)
322 "Import public keys in the current region between START and END."
324 (pgg-save-coding-system start end
325 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
329 (defun pgg-snarf-keys ()
330 "Import public keys in the current buffer."
332 (pgg-snarf-keys-region (point-min) (point-max)))
334 (defun pgg-lookup-key (string &optional type)
335 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
337 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
339 (defun pgg-insert-url-with-w3 (url)
343 (let (buffer-file-name)
344 (url-insert-file-contents url))))
346 (defvar pgg-insert-url-extra-arguments nil)
347 (defvar pgg-insert-url-program nil)
349 (defun pgg-insert-url-with-program (url)
350 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
355 (apply #'start-process " *PGG url*" (current-buffer)
356 pgg-insert-url-program (nconc args (list url))))
357 (set-process-sentinel process #'ignore)
358 (while (eq 'run (process-status process))
359 (accept-process-output process 5))
360 (delete-process process)
361 (if (and process (eq 'run (process-status process)))
362 (interrupt-process process))
365 (defun pgg-fetch-key (keyserver key)
366 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
367 (with-current-buffer (get-buffer-create pgg-output-buffer)
368 (buffer-disable-undo)
370 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
371 (substring keyserver 0 (1- (match-end 0))))))
373 (funcall pgg-insert-url-function
375 (format "http://%s:11371/pks/lookup?op=get&search=%s"
377 (when (re-search-forward "^-+BEGIN" nil 'last)
378 (delete-region (point-min) (match-beginning 0))
379 (when (re-search-forward "^-+END" nil t)
380 (delete-region (progn (end-of-line) (point))
384 (insert-buffer-substring pgg-output-buffer)
385 (pgg-snarf-keys-region (point-min)(point-max)))))))