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 (defvar pgg-read-passphrase nil)
93 (defun pgg-read-passphrase (prompt &optional key)
94 (if (not pgg-read-passphrase)
95 (if (functionp 'read-passwd)
96 (setq pgg-read-passphrase 'read-passwd)
98 (setq pgg-read-passphrase 'read-passwd)
99 (autoload 'ange-ftp-read-passwd "ange-ftp")
100 (setq pgg-read-passphrase 'ange-ftp-read-passwd))))
101 (or (and pgg-cache-passphrase
102 key (setq key (pgg-truncate-key-identifier key))
103 (symbol-value (intern-soft key pgg-passphrase-cache)))
104 (funcall pgg-read-passphrase prompt)))
106 (defun pgg-add-passphrase-cache (key passphrase)
107 (setq key (pgg-truncate-key-identifier key))
108 (set (intern key pgg-passphrase-cache)
110 (run-at-time pgg-passphrase-cache-expiry nil
111 #'pgg-remove-passphrase-cache
114 (defun pgg-remove-passphrase-cache (key)
115 (let ((passphrase (symbol-value (intern-soft key pgg-passphrase-cache))))
117 (fillarray passphrase ?_)
118 (unintern key pgg-passphrase-cache))))
120 (defmacro pgg-convert-lbt-region (start end lbt)
121 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
127 (> (marker-position pgg-conversion-end) (point)))
131 (while (re-search-forward "\r$" pgg-conversion-end t)
132 (replace-match ""))))))
134 (put 'pgg-as-lbt 'lisp-indent-function 3)
136 (defmacro pgg-as-lbt (start end lbt &rest body)
137 `(let ((inhibit-read-only t)
140 (pgg-convert-lbt-region ,start ,end ,lbt)
141 (let ((,end (point)))
143 (push nil buffer-undo-list)
144 (ignore-errors (undo))))
146 (put 'pgg-process-when-success 'lisp-indent-function 0)
148 (defmacro pgg-process-when-success (&rest body)
149 `(with-current-buffer pgg-output-buffer
150 (if (zerop (buffer-size)) nil ,@body t)))
152 ;;; @ interface functions
156 (defun pgg-encrypt-region (start end rcpts &optional sign)
157 "Encrypt the current region between START and END for RCPTS.
158 If optional argument SIGN is non-nil, do a combined sign and encrypt."
160 (list (region-beginning)(region-end)
161 (split-string (read-string "Recipients: ") "[ \t,]+")))
163 (pgg-save-coding-system start end
164 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
165 (point-min) (point-max) rcpts sign))))
166 (when (interactive-p)
167 (pgg-display-output-buffer start end status))
171 (defun pgg-encrypt (rcpts &optional sign start end)
172 "Encrypt the current buffer for RCPTS.
173 If optional argument SIGN is non-nil, do a combined sign and encrypt.
174 If optional arguments START and END are specified, only encrypt within
176 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
177 (pgg-encrypt-region (or start (point-min)) (or end (point-max)) rcpts sign))
180 (defun pgg-decrypt-region (start end)
181 "Decrypt the current region between START and END."
183 (let* ((buf (current-buffer))
184 (packet (cdr (assq 1 (with-temp-buffer
186 (pgg-decode-armor-region
187 (point-min) (point-max))))))
188 (key (cdr (assq 'key-identifier packet)))
191 (concat "0x" (pgg-truncate-key-identifier key))
192 pgg-default-user-id))
194 (pgg-save-coding-system start end
195 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
196 (point-min) (point-max)))))
197 (when (interactive-p)
198 (pgg-display-output-buffer start end status))
202 (defun pgg-decrypt (&optional start end)
203 "Decrypt the current buffer.
204 If optional arguments START and END are specified, only decrypt within
207 (pgg-decrypt-region (or start (point-min)) (or end (point-max))))
210 (defun pgg-sign-region (start end &optional cleartext)
211 "Make the signature from text between START and END.
212 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
213 a detached signature."
215 (let ((status (pgg-save-coding-system start end
216 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
217 (point-min) (point-max)
218 (or (interactive-p) cleartext)))))
219 (when (interactive-p)
220 (pgg-display-output-buffer start end status))
224 (defun pgg-sign (&optional cleartext start end)
225 "Sign the current buffer.
226 If the optional argument CLEARTEXT is non-nil, it does not create a
228 If optional arguments START and END are specified, only sign data
231 (pgg-sign-region (or start (point-min)) (or end (point-max)) cleartext))
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 (pgg-verify-region (or start (point-min)) (or end (point-max))
290 (defun pgg-insert-key ()
291 "Insert the ASCII armored public key."
293 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
296 (defun pgg-snarf-keys-region (start end)
297 "Import public keys in the current region between START and END."
299 (pgg-save-coding-system start end
300 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
304 (defun pgg-snarf-keys ()
305 "Import public keys in the current buffer."
307 (pgg-snarf-keys-region (point-min) (point-max)))
309 (defun pgg-lookup-key (string &optional type)
310 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
312 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
314 (defun pgg-insert-url-with-w3 (url)
318 (let (buffer-file-name)
319 (url-insert-file-contents url))))
321 (defvar pgg-insert-url-extra-arguments nil)
322 (defvar pgg-insert-url-program nil)
324 (defun pgg-insert-url-with-program (url)
325 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
330 (apply #'start-process " *PGG url*" (current-buffer)
331 pgg-insert-url-program (nconc args (list url))))
332 (set-process-sentinel process #'ignore)
333 (while (eq 'run (process-status process))
334 (accept-process-output process 5))
335 (delete-process process)
336 (if (and process (eq 'run (process-status process)))
337 (interrupt-process process))
340 (defun pgg-fetch-key (keyserver key)
341 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
342 (with-current-buffer (get-buffer-create pgg-output-buffer)
343 (buffer-disable-undo)
345 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
346 (substring keyserver 0 (1- (match-end 0))))))
348 (funcall pgg-insert-url-function
350 (format "http://%s:11371/pks/lookup?op=get&search=%s"
352 (when (re-search-forward "^-+BEGIN" nil 'last)
353 (delete-region (point-min) (match-beginning 0))
354 (when (re-search-forward "^-+END" nil t)
355 (delete-region (progn (end-of-line) (point))
359 (insert-buffer-substring pgg-output-buffer)
360 (pgg-snarf-keys-region (point-min)(point-max)))))))