1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999,2000 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; 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.
40 (defvar pgg-temporary-file-directory
41 (cond ((fboundp 'temp-directory) (temp-directory))
42 ((boundp 'temporary-file-directory) temporary-file-directory)
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 (split-window-vertically)))
75 (set-window-buffer window buffer)
76 (shrink-window-if-larger-than-buffer window)))
78 (defun pgg-display-output-buffer (start end status)
81 (delete-region start end)
82 (insert-buffer-substring pgg-output-buffer)
83 (decode-coding-region start (point) buffer-file-coding-system))
84 (let ((temp-buffer-show-function
85 (function pgg-temp-buffer-show-function)))
86 (with-output-to-temp-buffer pgg-echo-buffer
87 (set-buffer standard-output)
88 (insert-buffer-substring pgg-errors-buffer)))))
90 (defvar pgg-passphrase-cache (make-vector 7 0))
92 (defun pgg-read-passphrase (prompt &optional key)
93 (or (and pgg-cache-passphrase
94 key (setq key (pgg-truncate-key-identifier key))
95 (symbol-value (intern-soft key pgg-passphrase-cache)))
96 (read-passwd prompt)))
98 (defun pgg-add-passphrase-cache (key passphrase)
99 (setq key (pgg-truncate-key-identifier key))
100 (set (intern key pgg-passphrase-cache)
102 (run-at-time pgg-passphrase-cache-expiry nil
103 #'pgg-remove-passphrase-cache
106 (defun pgg-remove-passphrase-cache (key)
107 (let ((passphrase (symbol-value (intern-soft key pgg-passphrase-cache))))
109 (fillarray passphrase ?_)
110 (unintern key pgg-passphrase-cache))))
112 (defmacro pgg-convert-lbt-region (start end lbt)
113 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
119 (> (marker-position pgg-conversion-end) (point)))
123 (while (re-search-forward "\r$" pgg-conversion-end t)
124 (replace-match ""))))))
126 (put 'pgg-as-lbt 'lisp-indent-function 3)
128 (defmacro pgg-as-lbt (start end lbt &rest body)
129 `(let ((inhibit-read-only t)
132 (pgg-convert-lbt-region ,start ,end ,lbt)
133 (let ((,end (point)))
135 (push nil buffer-undo-list)
136 (ignore-errors (undo))))
138 (put 'pgg-process-when-success 'lisp-indent-function 0)
140 (defmacro pgg-process-when-success (&rest body)
141 `(with-current-buffer pgg-output-buffer
142 (if (zerop (buffer-size)) nil ,@body t)))
144 ;;; @ interface functions
148 (defun pgg-encrypt-region (start end rcpts &optional sign)
149 "Encrypt the current region between START and END for RCPTS.
150 If optional argument SIGN is non-nil, do a combined sign and encrypt."
152 (list (region-beginning)(region-end)
153 (split-string (read-string "Recipients: ") "[ \t,]+")))
155 (pgg-save-coding-system start end
156 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
157 (point-min) (point-max) rcpts sign))))
158 (when (interactive-p)
159 (pgg-display-output-buffer start end status))
163 (defun pgg-encrypt (rcpts &optional sign start end)
164 "Encrypt the current buffer for RCPTS.
165 If optional argument SIGN is non-nil, do a combined sign and encrypt.
166 If optional arguments START and END are specified, only encrypt within
168 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
169 (let* ((start (or start (point-min)))
170 (end (or end (point-max)))
171 (status (pgg-encrypt-region start end rcpts sign)))
172 (when (interactive-p)
173 (pgg-display-output-buffer start end status))
177 (defun pgg-decrypt-region (start end)
178 "Decrypt the current region between START and END."
180 (let* ((buf (current-buffer))
181 (packet (cdr (assq 1 (with-temp-buffer
183 (pgg-decode-armor-region
184 (point-min) (point-max))))))
185 (key (cdr (assq 'key-identifier packet)))
188 (concat "0x" (pgg-truncate-key-identifier key))
189 pgg-default-user-id))
191 (pgg-save-coding-system start end
192 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
193 (point-min) (point-max)))))
194 (when (interactive-p)
195 (pgg-display-output-buffer start end status))
199 (defun pgg-decrypt (&optional start end)
200 "Decrypt the current buffer.
201 If optional arguments START and END are specified, only decrypt within
204 (let* ((start (or start (point-min)))
205 (end (or end (point-max)))
206 (status (pgg-decrypt-region start end)))
207 (when (interactive-p)
208 (pgg-display-output-buffer start end status))
212 (defun pgg-sign-region (start end &optional cleartext)
213 "Make the signature from text between START and END.
214 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
215 a detached signature.
216 If this function is called interactively, CLEARTEXT is enabled
217 and the the output is displayed."
219 (let ((status (pgg-save-coding-system start end
220 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
221 (point-min) (point-max)
222 (or (interactive-p) cleartext)))))
223 (when (interactive-p)
224 (pgg-display-output-buffer start end status))
228 (defun pgg-sign (&optional cleartext start end)
229 "Sign the current buffer.
230 If the optional argument CLEARTEXT is non-nil, it does not create a
232 If optional arguments START and END are specified, only sign data
234 If this function is called interactively, CLEARTEXT is enabled
235 and the the output is displayed."
237 (let* ((start (or start (point-min)))
238 (end (or end (point-max)))
239 (status (pgg-sign-region start end (or (interactive-p) cleartext))))
240 (when (interactive-p)
241 (pgg-display-output-buffer start end status))
245 (defun pgg-verify-region (start end &optional signature fetch)
246 "Verify the current region between START and END.
247 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
248 the detached signature of the current region.
250 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
251 signer's public key from `pgg-default-keyserver-address'."
254 (if (null signature) nil
256 (buffer-disable-undo)
257 (if (fboundp 'set-buffer-multibyte)
258 (set-buffer-multibyte nil))
259 (insert-file-contents signature)
260 (cdr (assq 2 (pgg-decode-armor-region
261 (point-min)(point-max)))))))
262 (key (cdr (assq 'key-identifier packet)))
266 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
267 (null (pgg-lookup-key key))
268 (or fetch (interactive-p))
269 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
271 (or (cdr (assq 'preferred-key-server packet))
272 pgg-default-keyserver-address))
273 (pgg-fetch-key keyserver key))
275 (pgg-save-coding-system start end
276 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
277 (point-min) (point-max) signature)))
278 (when (interactive-p)
279 (let ((temp-buffer-show-function
280 (function pgg-temp-buffer-show-function)))
281 (with-output-to-temp-buffer pgg-echo-buffer
282 (set-buffer standard-output)
283 (insert-buffer-substring (if status pgg-output-buffer
284 pgg-errors-buffer)))))
288 (defun pgg-verify (&optional signature fetch start end)
289 "Verify the current buffer.
290 If the optional argument SIGNATURE is non-nil, it is treated as
291 the detached signature of the current region.
292 If the optional argument FETCH is non-nil, we attempt to fetch the
293 signer's public key from `pgg-default-keyserver-address'.
294 If optional arguments START and END are specified, only verify data
297 (let* ((start (or start (point-min)))
298 (end (or end (point-max)))
299 (status (pgg-verify-region start end signature fetch)))
300 (when (interactive-p)
301 (let ((temp-buffer-show-function
302 (function pgg-temp-buffer-show-function)))
303 (with-output-to-temp-buffer pgg-echo-buffer
304 (set-buffer standard-output)
305 (insert-buffer-substring (if status pgg-output-buffer
306 pgg-errors-buffer)))))))
309 (defun pgg-insert-key ()
310 "Insert the ASCII armored public key."
312 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
315 (defun pgg-snarf-keys-region (start end)
316 "Import public keys in the current region between START and END."
318 (pgg-save-coding-system start end
319 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
323 (defun pgg-snarf-keys ()
324 "Import public keys in the current buffer."
326 (pgg-snarf-keys-region (point-min) (point-max)))
328 (defun pgg-lookup-key (string &optional type)
329 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
331 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
333 (defun pgg-insert-url-with-w3 (url)
337 (let (buffer-file-name)
338 (url-insert-file-contents url))))
340 (defvar pgg-insert-url-extra-arguments nil)
341 (defvar pgg-insert-url-program nil)
343 (defun pgg-insert-url-with-program (url)
344 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
349 (apply #'start-process " *PGG url*" (current-buffer)
350 pgg-insert-url-program (nconc args (list url))))
351 (set-process-sentinel process #'ignore)
352 (while (eq 'run (process-status process))
353 (accept-process-output process 5))
354 (delete-process process)
355 (if (and process (eq 'run (process-status process)))
356 (interrupt-process process))
359 (defun pgg-fetch-key (keyserver key)
360 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
361 (with-current-buffer (get-buffer-create pgg-output-buffer)
362 (buffer-disable-undo)
364 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
365 (substring keyserver 0 (1- (match-end 0))))))
367 (funcall pgg-insert-url-function
369 (format "http://%s:11371/pks/lookup?op=get&search=%s"
371 (when (re-search-forward "^-+BEGIN" nil 'last)
372 (delete-region (point-min) (match-beginning 0))
373 (when (re-search-forward "^-+END" nil t)
374 (delete-region (progn (end-of-line) (point))
378 (insert-buffer-substring pgg-output-buffer)
379 (pgg-snarf-keys-region (point-min)(point-max)))))))