1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
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.
30 (eval-and-compile (require 'luna))
40 (in-calist-package 'pgg)
42 (defun pgg-field-match-method-with-containment
43 (calist field-type field-value)
44 (let ((s-field (assq field-type calist)))
46 (cons (cons field-type field-value) calist)
48 ((memq (cdr s-field) field-value)
51 (define-calist-field-match-method 'signature-version
52 #'pgg-field-match-method-with-containment)
54 (define-calist-field-match-method 'symmetric-key-algorithm
55 #'pgg-field-match-method-with-containment)
57 (define-calist-field-match-method 'public-key-algorithm
58 #'pgg-field-match-method-with-containment)
60 (define-calist-field-match-method 'hash-algorithm
61 #'pgg-field-match-method-with-containment)
63 (defvar pgg-verify-condition nil
64 "Condition-tree about which PGP implementation is used for verifying.")
66 (defvar pgg-decrypt-condition nil
67 "Condition-tree about which PGP implementation is used for decrypting.")
69 (ctree-set-calist-strictly
71 '((signature-version 3)(public-key-algorithm RSA)(hash-algorithm MD5)
74 (ctree-set-calist-strictly
75 'pgg-decrypt-condition
76 '((public-key-algorithm RSA)(symmetric-key-algorithm IDEA)
79 (ctree-set-calist-strictly
81 '((signature-version 3 4)
82 (public-key-algorithm RSA ELG DSA)
83 (hash-algorithm MD5 SHA1 RIPEMD160)
86 (ctree-set-calist-strictly
87 'pgg-decrypt-condition
88 '((public-key-algorithm RSA ELG DSA)
89 (symmetric-key-algorithm 3DES CAST5 IDEA)
92 (ctree-set-calist-strictly
94 '((signature-version 3 4)
95 (public-key-algorithm ELG-E DSA ELG)
96 (hash-algorithm MD5 SHA1 RIPEMD160)
99 (ctree-set-calist-strictly
100 'pgg-decrypt-condition
101 '((public-key-algorithm ELG-E DSA ELG)
102 (symmetric-key-algorithm 3DES CAST5 BLOWFISH TWOFISH)
105 ;;; @ definition of the implementation scheme
109 (luna-define-class pgg-scheme ())
111 (luna-define-internal-accessors 'pgg-scheme)
114 (luna-define-generic lookup-key-string (scheme string &optional type)
115 "Search keys associated with STRING")
117 (luna-define-generic encrypt-region (scheme start end recipients)
118 "Encrypt the current region between START and END.")
120 (luna-define-generic decrypt-region (scheme start end)
121 "Decrypt the current region between START and END.")
123 (luna-define-generic sign-region (scheme start end &optional cleartext)
124 "Make detached signature from text between START and END.")
126 (luna-define-generic verify-region (scheme start end &optional signature)
127 "Verify region between START and END
128 as the detached signature SIGNATURE.")
130 (luna-define-generic insert-key (scheme)
131 "Insert public key at point.")
133 (luna-define-generic snarf-keys-region (scheme start end)
134 "Add all public keys in region between START
135 and END to the keyring.")
137 ;;; @ interface functions
140 (defvar pgg-fetch-key-function (function pgg-fetch-key-with-w3))
142 (defmacro pgg-make-scheme (scheme)
144 (require (intern (format "pgg-%s" ,scheme)))
145 (funcall (intern (format "pgg-make-scheme-%s"
149 (defun pgg-encrypt-region (start end rcpts)
151 (list (region-beginning)(region-end)
152 (split-string (read-string "Recipients: ") "[ \t,]+")))
153 (let* ((entity (pgg-make-scheme pgg-default-scheme))
154 (status (luna-send entity 'encrypt-region
155 entity start end rcpts)))
156 (when (interactive-p)
159 (delete-region start end)
160 (insert-buffer-substring pgg-output-buffer))
161 (with-output-to-temp-buffer pgg-echo-buffer
162 (set-buffer standard-output)
163 (insert-buffer-substring pgg-errors-buffer))))
167 (defun pgg-decrypt-region (start end)
169 (let* ((packet (cdr (assq 1 (pgg-parse-armor-region start end))))
174 (in-calist-package 'pgg)
175 (ctree-match-calist pgg-decrypt-condition
178 (entity (pgg-make-scheme scheme))
179 (status (luna-send entity 'decrypt-region entity start end)))
180 (when (interactive-p)
183 (delete-region start end)
184 (insert-buffer-substring pgg-output-buffer))
185 (with-output-to-temp-buffer pgg-echo-buffer
186 (set-buffer standard-output)
187 (insert-buffer-substring pgg-errors-buffer))))
191 (defun pgg-sign-region (start end &optional cleartext)
193 (let* ((entity (pgg-make-scheme pgg-default-scheme))
194 (status (luna-send entity 'sign-region
196 (or (interactive-p) cleartext))))
197 (when (interactive-p)
200 (delete-region start end)
201 (insert-buffer-substring pgg-output-buffer))
202 (with-output-to-temp-buffer pgg-echo-buffer
203 (set-buffer standard-output)
204 (insert-buffer-substring pgg-errors-buffer))))
208 (defun pgg-verify-region (start end &optional signature fetch)
211 (if (null signature) nil
213 (buffer-disable-undo)
214 (set-buffer-multibyte nil)
215 (insert-file-contents signature)
216 (cdr (assq 2 (pgg-decode-armor-region (point-min)(point-max))))
222 (in-calist-package 'pgg)
223 (ctree-match-calist pgg-verify-condition
226 (entity (pgg-make-scheme scheme))
227 (key (cdr (assq 'key-identifier packet)))
231 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
232 (null (let ((pgg-scheme scheme))
233 (pgg-lookup-key-string key)))
234 (or fetch (interactive-p))
235 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
237 (or (cdr (assq 'preferred-key-server packet))
238 pgg-default-keyserver-address))
239 (pgg-fetch-key keyserver key))
240 (setq status (luna-send entity 'verify-region
241 entity start end signature))
242 (when (interactive-p)
243 (with-output-to-temp-buffer pgg-echo-buffer
244 (set-buffer standard-output)
245 (insert-buffer-substring
246 (if status pgg-output-buffer pgg-errors-buffer))
251 (defun pgg-insert-key ()
253 (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
254 (luna-send entity 'insert-key entity)))
257 (defun pgg-snarf-keys-region (start end)
259 (let* ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme)))
260 (status (luna-send entity 'snarf-keys-region entity start end)))
261 (when (interactive-p)
262 (with-output-to-temp-buffer pgg-echo-buffer
263 (set-buffer standard-output)
264 (insert-buffer-substring
265 (if status pgg-output-buffer pgg-errors-buffer))
269 (defun pgg-lookup-key-string (string &optional type)
270 (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
271 (luna-send entity 'lookup-key-string entity string type)))
273 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
275 (defun pgg-insert-url-with-w3 (url)
278 (let (buffer-file-name)
279 (url-insert-file-contents url)))
281 (defvar pgg-insert-url-extra-arguments nil)
282 (defvar pgg-insert-url-program nil)
284 (defun pgg-insert-url-with-program (url)
285 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
290 (apply #'start-process " *PGG url*" (current-buffer)
291 pgg-insert-url-program (nconc args (list url))))
292 (set-process-sentinel process #'ignore)
293 (while (eq 'run (process-status process))
294 (accept-process-output process 5))
295 (delete-process process)
296 (if (and process (eq 'run (process-status process)))
297 (interrupt-process process))
301 (defun pgg-fetch-key (keyserver key)
302 "Attempt to fetch a key for addition to PGP or GnuPG keyring."
303 (with-current-buffer (get-buffer-create pgg-output-buffer)
304 (buffer-disable-undo)
306 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
307 (substring keyserver 0 (1- (match-end 0))))))
309 (funcall pgg-insert-url-function
311 (format "http://%s:11371/pks/lookup?op=get&search=%s"
313 (when (re-search-forward "^-+BEGIN" nil 'last)
314 (delete-region (point-min) (match-beginning 0))
315 (when (re-search-forward "^-+END" nil t)
316 (delete-region (progn (end-of-line) (point))
320 (insert-buffer-substring pgg-output-buffer)
321 (pgg-snarf-keys-region (point-min)(point-max))))
325 ;;; @ utility functions
328 (defvar pgg-passphrase-cache-expiry 16)
329 (defvar pgg-passphrase-cache (make-vector 7 0))
331 (defvar pgg-read-passphrase nil)
332 (defun pgg-read-passphrase (prompt &optional key)
333 (if (not pgg-read-passphrase)
334 (if (functionp 'read-passwd)
335 (setq pgg-read-passphrase 'read-passwd)
336 (if (load "passwd" t)
337 (setq pgg-read-passphrase 'read-passwd)
338 (autoload 'ange-ftp-read-passwd "ange-ftp")
339 (setq pgg-read-passphrase 'ange-ftp-read-passwd))))
340 (or (and key (setq key (pgg-truncate-key-identifier key))
341 (symbol-value (intern-soft key pgg-passphrase-cache)))
342 (funcall pgg-read-passphrase prompt)))
344 (defun pgg-add-passphrase-cache (key passphrase)
345 (setq key (pgg-truncate-key-identifier key))
346 (set (intern key pgg-passphrase-cache)
348 (run-at-time pgg-passphrase-cache-expiry nil
349 #'pgg-remove-passphrase-cache
352 (defun pgg-remove-passphrase-cache (key)
353 (unintern key pgg-passphrase-cache))
355 (defmacro pgg-convert-lbt-region (start end lbt)
356 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
362 (> (marker-position pgg-conversion-end) (point)))
366 (while (re-search-forward "\r$" pgg-conversion-end t)
367 (replace-match ""))))
370 (put 'pgg-as-lbt 'lisp-indent-function 3)
372 (defmacro pgg-as-lbt (start end lbt &rest body)
373 `(let ((inhibit-read-only t)
376 (pgg-convert-lbt-region ,start ,end ,lbt)
377 (let ((,end (point)))
379 (push nil buffer-undo-list)
383 ;;; @ postprocess macros
386 (put 'pgg-process-when-success 'lisp-indent-function 0)
388 (defmacro pgg-process-when-success (&rest body)
389 `(with-current-buffer pgg-output-buffer
390 (if (zerop (buffer-size)) nil ,@body t)))