1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999,2000 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.
34 (eval-and-compile (require 'luna))
44 (in-calist-package 'pgg)
46 (defun pgg-field-match-method-with-containment
47 (calist field-type field-value)
48 (let ((s-field (assq field-type calist)))
50 (cons (cons field-type field-value) calist))
51 ((memq (cdr s-field) field-value)
54 (define-calist-field-match-method 'signature-version
55 #'pgg-field-match-method-with-containment)
57 (define-calist-field-match-method 'symmetric-key-algorithm
58 #'pgg-field-match-method-with-containment)
60 (define-calist-field-match-method 'public-key-algorithm
61 #'pgg-field-match-method-with-containment)
63 (define-calist-field-match-method 'hash-algorithm
64 #'pgg-field-match-method-with-containment)
66 (defvar pgg-verify-condition nil
67 "Condition-tree about which PGP implementation is used for verifying.")
69 (defvar pgg-decrypt-condition nil
70 "Condition-tree about which PGP implementation is used for decrypting.")
72 (ctree-set-calist-strictly
74 '((signature-version 3)(public-key-algorithm RSA)(hash-algorithm MD5)
77 (ctree-set-calist-strictly
78 'pgg-decrypt-condition
79 '((public-key-algorithm RSA)(symmetric-key-algorithm IDEA)
82 (ctree-set-calist-strictly
84 '((signature-version 3 4)
85 (public-key-algorithm RSA ELG DSA)
86 (hash-algorithm MD5 SHA1 RIPEMD160)
89 (ctree-set-calist-strictly
90 'pgg-decrypt-condition
91 '((public-key-algorithm RSA ELG DSA)
92 (symmetric-key-algorithm 3DES CAST5 IDEA)
95 (ctree-set-calist-strictly
97 '((signature-version 3 4)
98 (public-key-algorithm ELG-E DSA ELG)
99 (hash-algorithm MD5 SHA1 RIPEMD160)
102 (ctree-set-calist-strictly
103 'pgg-decrypt-condition
104 '((public-key-algorithm ELG-E DSA ELG)
105 (symmetric-key-algorithm 3DES CAST5 BLOWFISH TWOFISH)
108 ;;; @ definition of the implementation scheme
112 (luna-define-class pgg-scheme ())
114 (luna-define-internal-accessors 'pgg-scheme))
116 (luna-define-generic pgg-scheme-lookup-key (scheme string &optional type)
117 "Search keys associated with STRING")
119 (luna-define-generic pgg-scheme-encrypt-region (scheme start end recipients)
120 "Encrypt the current region between START and END.")
122 (luna-define-generic pgg-scheme-decrypt-region (scheme start end)
123 "Decrypt the current region between START and END.")
125 (luna-define-generic pgg-scheme-sign-region
126 (scheme start end &optional cleartext)
127 "Make detached signature from text between START and END.")
129 (luna-define-generic pgg-scheme-verify-region
130 (scheme start end &optional signature)
131 "Verify region between START and END
132 as the detached signature SIGNATURE.")
134 (luna-define-generic pgg-scheme-insert-key (scheme)
135 "Insert public key at point.")
137 (luna-define-generic pgg-scheme-snarf-keys-region (scheme start end)
138 "Add all public keys in region between START
139 and END to the keyring.")
141 ;;; @ utility functions
144 (defvar pgg-fetch-key-function (function pgg-fetch-key-with-w3))
146 (defmacro pgg-make-scheme (scheme)
148 (require (intern (format "pgg-%s" ,scheme)))
149 (funcall (intern (format "pgg-make-scheme-%s"
152 (put 'pgg-save-coding-system 'lisp-indent-function 2)
154 (defmacro pgg-save-coding-system (start end &rest body)
156 (let ((buffer (current-buffer)))
158 (let (buffer-undo-list)
159 (insert-buffer-substring buffer ,start ,end)
160 (encode-coding-region (point-min)(point-max)
161 buffer-file-coding-system)
162 (prog1 (save-excursion ,@body)
163 (push nil buffer-undo-list)
164 (ignore-errors (undo))))))
166 (narrow-to-region ,start ,end)
169 (defun pgg-temp-buffer-show-function (buffer)
170 (let ((window (split-window-vertically
172 (/ (window-height) 5)))))
173 (set-window-buffer window buffer)))
175 (defun pgg-display-output-buffer (start end status)
178 (delete-region start end)
179 (insert-buffer-substring pgg-output-buffer)
180 (decode-coding-region start (point) buffer-file-coding-system))
181 (let ((temp-buffer-show-function
182 (function pgg-temp-buffer-show-function)))
183 (with-output-to-temp-buffer pgg-echo-buffer
184 (set-buffer standard-output)
185 (insert-buffer-substring pgg-errors-buffer)))))
187 (defvar pgg-passphrase-cache-expiry 16)
188 (defvar pgg-passphrase-cache (make-vector 7 0))
190 (defvar pgg-read-passphrase nil)
191 (defun pgg-read-passphrase (prompt &optional key)
192 (if (not pgg-read-passphrase)
193 (if (functionp 'read-passwd)
194 (setq pgg-read-passphrase 'read-passwd)
195 (if (load "passwd" t)
196 (setq pgg-read-passphrase 'read-passwd)
197 (autoload 'ange-ftp-read-passwd "ange-ftp")
198 (setq pgg-read-passphrase 'ange-ftp-read-passwd))))
199 (or (and pgg-cache-passphrase
200 key (setq key (pgg-truncate-key-identifier key))
201 (symbol-value (intern-soft key pgg-passphrase-cache)))
202 (funcall pgg-read-passphrase prompt)))
204 (defun pgg-add-passphrase-cache (key passphrase)
205 (setq key (pgg-truncate-key-identifier key))
206 (set (intern key pgg-passphrase-cache)
208 (run-at-time pgg-passphrase-cache-expiry nil
209 #'pgg-remove-passphrase-cache
212 (defun pgg-remove-passphrase-cache (key)
213 (let ((passphrase (symbol-value (intern-soft key pgg-passphrase-cache))))
215 (fillarray passphrase ?_)
216 (unintern key pgg-passphrase-cache))))
218 (defmacro pgg-convert-lbt-region (start end lbt)
219 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
225 (> (marker-position pgg-conversion-end) (point)))
229 (while (re-search-forward "\r$" pgg-conversion-end t)
230 (replace-match ""))))))
232 (put 'pgg-as-lbt 'lisp-indent-function 3)
234 (defmacro pgg-as-lbt (start end lbt &rest body)
235 `(let ((inhibit-read-only t)
238 (pgg-convert-lbt-region ,start ,end ,lbt)
239 (let ((,end (point)))
241 (push nil buffer-undo-list)
242 (ignore-errors (undo))))
244 (put 'pgg-process-when-success 'lisp-indent-function 0)
246 (defmacro pgg-process-when-success (&rest body)
247 `(with-current-buffer pgg-output-buffer
248 (if (zerop (buffer-size)) nil ,@body t)))
251 ;;; @ interface functions
255 (defun pgg-encrypt-region (start end rcpts)
256 "Encrypt the current region between START and END for RCPTS."
258 (list (region-beginning)(region-end)
259 (split-string (read-string "Recipients: ") "[ \t,]+")))
260 (let* ((entity (pgg-make-scheme pgg-default-scheme))
262 (pgg-save-coding-system start end
263 (pgg-scheme-encrypt-region entity (point-min)(point-max) rcpts))))
264 (when (interactive-p)
265 (pgg-display-output-buffer start end status))
269 (defun pgg-decrypt-region (start end)
270 "Decrypt the current region between START and END."
272 (let* ((packet (cdr (assq 1 (pgg-parse-armor-region start end))))
277 (in-calist-package 'pgg)
278 (ctree-match-calist pgg-decrypt-condition
281 (entity (pgg-make-scheme scheme))
283 (pgg-save-coding-system start end
284 (pgg-scheme-decrypt-region entity (point-min)(point-max)))))
285 (when (interactive-p)
286 (pgg-display-output-buffer start end status))
290 (defun pgg-sign-region (start end &optional cleartext)
291 "Make the signature from text between START and END.
292 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
293 a detached signature."
295 (let* ((entity (pgg-make-scheme pgg-default-scheme))
296 (status (pgg-save-coding-system start end
297 (pgg-scheme-sign-region entity (point-min)(point-max)
298 (or (interactive-p) cleartext)))))
299 (when (interactive-p)
300 (pgg-display-output-buffer start end status))
304 (defun pgg-verify-region (start end &optional signature fetch)
305 "Verify the current region between START and END.
306 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
307 the detached signature of the current region.
309 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
310 signer's public key from `pgg-default-keyserver-address'."
313 (if (null signature) nil
315 (buffer-disable-undo)
316 (set-buffer-multibyte nil)
317 (insert-file-contents signature)
318 (cdr (assq 2 (pgg-decode-armor-region
319 (point-min)(point-max)))))))
324 (in-calist-package 'pgg)
325 (ctree-match-calist pgg-verify-condition
328 (entity (pgg-make-scheme scheme))
329 (key (cdr (assq 'key-identifier packet)))
332 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
333 (null (let ((pgg-scheme scheme))
334 (pgg-lookup-key key)))
335 (or fetch (interactive-p))
336 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
338 (or (cdr (assq 'preferred-key-server packet))
339 pgg-default-keyserver-address))
340 (pgg-fetch-key keyserver key))
341 (setq status (pgg-save-coding-system start end
342 (pgg-scheme-verify-region entity (point-min)(point-max)
344 (when (interactive-p)
345 (let ((temp-buffer-show-function
346 (function pgg-temp-buffer-show-function)))
347 (with-output-to-temp-buffer pgg-echo-buffer
348 (set-buffer standard-output)
349 (insert-buffer-substring (if status pgg-output-buffer
350 pgg-errors-buffer)))))
354 (defun pgg-insert-key ()
355 "Insert the ASCII armored public key."
357 (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
358 (pgg-scheme-insert-key entity)))
361 (defun pgg-snarf-keys-region (start end)
362 "Import public keys in the current region between START and END."
364 (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
365 (pgg-save-coding-system start end
366 (pgg-scheme-snarf-keys-region entity start end))))
368 (defun pgg-lookup-key (string &optional type)
369 (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
370 (pgg-scheme-lookup-key entity string type)))
372 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
374 (defun pgg-insert-url-with-w3 (url)
377 (let (buffer-file-name)
378 (url-insert-file-contents url)))
380 (defvar pgg-insert-url-extra-arguments nil)
381 (defvar pgg-insert-url-program nil)
383 (defun pgg-insert-url-with-program (url)
384 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
389 (apply #'start-process " *PGG url*" (current-buffer)
390 pgg-insert-url-program (nconc args (list url))))
391 (set-process-sentinel process #'ignore)
392 (while (eq 'run (process-status process))
393 (accept-process-output process 5))
394 (delete-process process)
395 (if (and process (eq 'run (process-status process)))
396 (interrupt-process process))
399 (defun pgg-fetch-key (keyserver key)
400 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
401 (with-current-buffer (get-buffer-create pgg-output-buffer)
402 (buffer-disable-undo)
404 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
405 (substring keyserver 0 (1- (match-end 0))))))
407 (funcall pgg-insert-url-function
409 (format "http://%s:11371/pks/lookup?op=get&search=%s"
411 (when (re-search-forward "^-+BEGIN" nil 'last)
412 (delete-region (point-min) (match-beginning 0))
413 (when (re-search-forward "^-+END" nil t)
414 (delete-region (progn (end-of-line) (point))
418 (insert-buffer-substring pgg-output-buffer)
419 (pgg-snarf-keys-region (point-min)(point-max)))))))