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 (function pgg-fetch-key-with-w3))
50 (defun pgg-invoke (func scheme &rest args)
52 (require (intern (format "pgg-%s" scheme)))
53 (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
55 (put 'pgg-save-coding-system 'lisp-indent-function 2)
57 (defmacro pgg-save-coding-system (start end &rest body)
59 (let ((buffer (current-buffer)))
61 (let (buffer-undo-list)
62 (insert-buffer-substring buffer ,start ,end)
63 (encode-coding-region (point-min)(point-max)
64 buffer-file-coding-system)
65 (prog1 (save-excursion ,@body)
66 (push nil buffer-undo-list)
67 (ignore-errors (undo))))))
69 (narrow-to-region ,start ,end)
72 (defun pgg-temp-buffer-show-function (buffer)
73 (let ((window (split-window-vertically)))
74 (set-window-buffer window buffer)
75 (shrink-window-if-larger-than-buffer window)))
77 (defun pgg-display-output-buffer (start end status)
80 (delete-region start end)
81 (insert-buffer-substring pgg-output-buffer)
82 (decode-coding-region start (point) buffer-file-coding-system))
83 (let ((temp-buffer-show-function
84 (function pgg-temp-buffer-show-function)))
85 (with-output-to-temp-buffer pgg-echo-buffer
86 (set-buffer standard-output)
87 (insert-buffer-substring pgg-errors-buffer)))))
89 (defvar pgg-passphrase-cache-expiry 16)
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-decrypt-region (start end)
172 "Decrypt the current region between START and END."
175 (pgg-save-coding-system start end
176 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
177 (point-min) (point-max)))))
178 (when (interactive-p)
179 (pgg-display-output-buffer start end status))
183 (defun pgg-sign-region (start end &optional cleartext)
184 "Make the signature from text between START and END.
185 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
186 a detached signature."
188 (let ((status (pgg-save-coding-system start end
189 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
190 (point-min) (point-max)
191 (or (interactive-p) cleartext)))))
192 (when (interactive-p)
193 (pgg-display-output-buffer start end status))
197 (defun pgg-verify-region (start end &optional signature fetch)
198 "Verify the current region between START and END.
199 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
200 the detached signature of the current region.
202 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
203 signer's public key from `pgg-default-keyserver-address'."
206 (if (null signature) nil
208 (buffer-disable-undo)
209 (if (fboundp 'set-buffer-multibyte)
210 (set-buffer-multibyte nil))
211 (insert-file-contents signature)
212 (cdr (assq 2 (pgg-decode-armor-region
213 (point-min)(point-max)))))))
214 (key (cdr (assq 'key-identifier packet)))
217 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
218 (null (pgg-lookup-key key))
219 (or fetch (interactive-p))
220 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
222 (or (cdr (assq 'preferred-key-server packet))
223 pgg-default-keyserver-address))
224 (pgg-fetch-key keyserver key))
226 (pgg-save-coding-system start end
227 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
228 (point-min) (point-max) signature)))
229 (when (interactive-p)
230 (let ((temp-buffer-show-function
231 (function pgg-temp-buffer-show-function)))
232 (with-output-to-temp-buffer pgg-echo-buffer
233 (set-buffer standard-output)
234 (insert-buffer-substring (if status pgg-output-buffer
235 pgg-errors-buffer)))))
239 (defun pgg-insert-key ()
240 "Insert the ASCII armored public key."
242 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
245 (defun pgg-snarf-keys-region (start end)
246 "Import public keys in the current region between START and END."
248 (pgg-save-coding-system start end
249 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
252 (defun pgg-lookup-key (string &optional type)
253 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
255 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
257 (defun pgg-insert-url-with-w3 (url)
260 (let (buffer-file-name)
261 (url-insert-file-contents url)))
263 (defvar pgg-insert-url-extra-arguments nil)
264 (defvar pgg-insert-url-program nil)
266 (defun pgg-insert-url-with-program (url)
267 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
272 (apply #'start-process " *PGG url*" (current-buffer)
273 pgg-insert-url-program (nconc args (list url))))
274 (set-process-sentinel process #'ignore)
275 (while (eq 'run (process-status process))
276 (accept-process-output process 5))
277 (delete-process process)
278 (if (and process (eq 'run (process-status process)))
279 (interrupt-process process))
282 (defun pgg-fetch-key (keyserver key)
283 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
284 (with-current-buffer (get-buffer-create pgg-output-buffer)
285 (buffer-disable-undo)
287 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
288 (substring keyserver 0 (1- (match-end 0))))))
290 (funcall pgg-insert-url-function
292 (format "http://%s:11371/pks/lookup?op=get&search=%s"
294 (when (re-search-forward "^-+BEGIN" nil 'last)
295 (delete-region (point-min) (match-beginning 0))
296 (when (re-search-forward "^-+END" nil t)
297 (delete-region (progn (end-of-line) (point))
301 (insert-buffer-substring pgg-output-buffer)
302 (pgg-snarf-keys-region (point-min)(point-max)))))))