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)
157 "Encrypt the current region between START and END for RCPTS."
159 (list (region-beginning)(region-end)
160 (split-string (read-string "Recipients: ") "[ \t,]+")))
162 (pgg-save-coding-system start end
163 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
164 (point-min) (point-max) rcpts))))
165 (when (interactive-p)
166 (pgg-display-output-buffer start end status))
170 (defun pgg-decrypt-region (start end)
171 "Decrypt the current region between START and END."
174 (pgg-save-coding-system start end
175 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
176 (point-min) (point-max)))))
177 (when (interactive-p)
178 (pgg-display-output-buffer start end status))
182 (defun pgg-sign-region (start end &optional cleartext)
183 "Make the signature from text between START and END.
184 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
185 a detached signature."
187 (let ((status (pgg-save-coding-system start end
188 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
189 (point-min) (point-max)
190 (or (interactive-p) cleartext)))))
191 (when (interactive-p)
192 (pgg-display-output-buffer start end status))
196 (defun pgg-verify-region (start end &optional signature fetch)
197 "Verify the current region between START and END.
198 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
199 the detached signature of the current region.
201 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
202 signer's public key from `pgg-default-keyserver-address'."
205 (if (null signature) nil
207 (buffer-disable-undo)
208 (if (fboundp 'set-buffer-multibyte)
209 (set-buffer-multibyte nil))
210 (insert-file-contents signature)
211 (cdr (assq 2 (pgg-decode-armor-region
212 (point-min)(point-max)))))))
213 (key (cdr (assq 'key-identifier packet)))
216 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
217 (null (pgg-lookup-key key))
218 (or fetch (interactive-p))
219 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
221 (or (cdr (assq 'preferred-key-server packet))
222 pgg-default-keyserver-address))
223 (pgg-fetch-key keyserver key))
225 (pgg-save-coding-system start end
226 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
227 (point-min) (point-max) signature)))
228 (when (interactive-p)
229 (let ((temp-buffer-show-function
230 (function pgg-temp-buffer-show-function)))
231 (with-output-to-temp-buffer pgg-echo-buffer
232 (set-buffer standard-output)
233 (insert-buffer-substring (if status pgg-output-buffer
234 pgg-errors-buffer)))))
238 (defun pgg-insert-key ()
239 "Insert the ASCII armored public key."
241 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
244 (defun pgg-snarf-keys-region (start end)
245 "Import public keys in the current region between START and END."
247 (pgg-save-coding-system start end
248 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
251 (defun pgg-lookup-key (string &optional type)
252 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
254 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
256 (defun pgg-insert-url-with-w3 (url)
259 (let (buffer-file-name)
260 (url-insert-file-contents url)))
262 (defvar pgg-insert-url-extra-arguments nil)
263 (defvar pgg-insert-url-program nil)
265 (defun pgg-insert-url-with-program (url)
266 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
271 (apply #'start-process " *PGG url*" (current-buffer)
272 pgg-insert-url-program (nconc args (list url))))
273 (set-process-sentinel process #'ignore)
274 (while (eq 'run (process-status process))
275 (accept-process-output process 5))
276 (delete-process process)
277 (if (and process (eq 'run (process-status process)))
278 (interrupt-process process))
281 (defun pgg-fetch-key (keyserver key)
282 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
283 (with-current-buffer (get-buffer-create pgg-output-buffer)
284 (buffer-disable-undo)
286 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
287 (substring keyserver 0 (1- (match-end 0))))))
289 (funcall pgg-insert-url-function
291 (format "http://%s:11371/pks/lookup?op=get&search=%s"
293 (when (re-search-forward "^-+BEGIN" nil 'last)
294 (delete-region (point-min) (match-beginning 0))
295 (when (re-search-forward "^-+END" nil t)
296 (delete-region (progn (end-of-line) (point))
300 (insert-buffer-substring pgg-output-buffer)
301 (pgg-snarf-keys-region (point-min)(point-max)))))))