* pgg-pgp.el (sign-region): Fix regexp for the beginning of armor.
[elisp/semi.git] / pgg.el
1 ;;; pgg.el --- glue for the various PGP implementations.
2
3 ;; Copyright (C) 1999 Daiki Ueno
4
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
6 ;; Created: 1999/10/28
7 ;; Keywords: PGP
8
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
10
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.
15
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.
20
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.
25
26 ;;; Code:
27
28 (require 'calist)
29
30 (eval-and-compile (require 'luna))
31
32 (require 'pgg-def)
33 (require 'pgg-parse)
34
35 (eval-when-compile
36   (ignore-errors 
37     (require 'w3)
38     (require 'url)))
39
40 (in-calist-package 'pgg)
41
42 (defun pgg-field-match-method-with-containment
43   (calist field-type field-value)
44   (let ((s-field (assq field-type calist)))
45     (cond ((null s-field)
46            (cons (cons field-type field-value) calist)
47            )
48           ((memq (cdr s-field) field-value)
49            calist))))
50
51 (define-calist-field-match-method 'signature-version
52   #'pgg-field-match-method-with-containment)
53
54 (define-calist-field-match-method 'symmetric-key-algorithm
55   #'pgg-field-match-method-with-containment)
56
57 (define-calist-field-match-method 'public-key-algorithm
58   #'pgg-field-match-method-with-containment)
59
60 (define-calist-field-match-method 'hash-algorithm
61   #'pgg-field-match-method-with-containment)
62
63 (defvar pgg-verify-condition nil
64   "Condition-tree about which PGP implementation is used for verifying.")
65
66 (defvar pgg-decrypt-condition nil
67   "Condition-tree about which PGP implementation is used for decrypting.")
68
69 (ctree-set-calist-strictly
70  'pgg-verify-condition
71  '((signature-version 3)(public-key-algorithm RSA)(hash-algorithm MD5)
72    (scheme . pgp)))
73
74 (ctree-set-calist-strictly
75  'pgg-decrypt-condition
76  '((public-key-algorithm RSA)(symmetric-key-algorithm IDEA)
77    (scheme . pgp)))
78
79 (ctree-set-calist-strictly
80  'pgg-verify-condition
81  '((signature-version 3 4)
82    (public-key-algorithm RSA ELG DSA)
83    (hash-algorithm MD5 SHA1 RIPEMD160)
84    (scheme . pgp5)))
85
86 (ctree-set-calist-strictly
87  'pgg-decrypt-condition
88  '((public-key-algorithm RSA ELG DSA)
89    (symmetric-key-algorithm 3DES CAST5 IDEA)
90    (scheme . pgp5)))
91
92 (ctree-set-calist-strictly
93  'pgg-verify-condition
94  '((signature-version 3 4)
95    (public-key-algorithm ELG-E DSA ELG)
96    (hash-algorithm MD5 SHA1 RIPEMD160)
97    (scheme . gpg)))
98
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)
103    (scheme . gpg)))
104
105 ;;; @ definition of the implementation scheme
106 ;;;
107
108 (eval-and-compile
109   (luna-define-class pgg-scheme ())
110
111   (luna-define-internal-accessors 'pgg-scheme)
112   )
113
114 (luna-define-generic lookup-key-string (scheme string &optional type)
115   "Search keys associated with STRING")
116
117 (luna-define-generic encrypt-region (scheme start end recipients)
118   "Encrypt the current region between START and END.")
119
120 (luna-define-generic decrypt-region (scheme start end)
121   "Decrypt the current region between START and END.")
122
123 (luna-define-generic sign-region (scheme start end &optional cleartext)
124   "Make detached signature from text between START and END.")
125
126 (luna-define-generic verify-region (scheme start end &optional signature)
127   "Verify region between START and END 
128 as the detached signature SIGNATURE.")
129
130 (luna-define-generic insert-key (scheme)
131   "Insert public key at point.")
132
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.")
136
137 ;;; @ interface functions
138 ;;;
139
140 (defvar pgg-fetch-key-function (function pgg-fetch-key-with-w3))
141
142 (defmacro pgg-make-scheme (scheme)
143   `(progn
144      (require (intern (format "pgg-%s" ,scheme)))
145      (funcall (intern (format "pgg-make-scheme-%s" 
146                               ,scheme)))))
147
148 ;;;###autoload
149 (defun pgg-encrypt-region (start end rcpts)
150   (interactive
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)
157       (if status
158           (progn
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))))
164     status))
165
166 ;;;###autoload
167 (defun pgg-decrypt-region (start end)
168   (interactive "r")
169   (let* ((packet (cdr (assq 1 (pgg-parse-armor-region start end))))
170          (scheme
171           (or pgg-scheme
172               (cdr (assq 'scheme
173                          (progn
174                            (in-calist-package 'pgg)
175                            (ctree-match-calist pgg-decrypt-condition
176                                                packet))))
177               pgg-default-scheme))
178          (entity (pgg-make-scheme scheme))
179          (status (luna-send entity 'decrypt-region entity start end)))
180     (when (interactive-p)
181       (if status
182           (progn
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))))
188     status))
189
190 ;;;###autoload
191 (defun pgg-sign-region (start end)
192   (interactive "r")
193   (let* ((entity (pgg-make-scheme pgg-default-scheme))
194          (status (luna-send entity 'sign-region 
195                             entity start end (interactive-p))))
196     (when (interactive-p)
197       (if status
198           (progn
199             (delete-region start end)
200             (insert-buffer-substring pgg-output-buffer))
201         (with-output-to-temp-buffer pgg-echo-buffer
202           (set-buffer standard-output)
203           (insert-buffer-substring pgg-errors-buffer))))
204     status))
205
206 ;;;###autoload
207 (defun pgg-verify-region (start end &optional signature fetch)
208   (interactive "r")
209   (let* ((packet
210           (if (null signature) nil
211             (with-temp-buffer
212               (buffer-disable-undo)
213               (set-buffer-multibyte nil)
214               (insert-file-contents signature)
215               (cdr (assq 2 (pgg-decode-armor-region (point-min)(point-max))))
216               )))
217          (scheme 
218           (or pgg-scheme
219               (cdr (assq 'scheme
220                          (progn
221                            (in-calist-package 'pgg)
222                            (ctree-match-calist pgg-verify-condition
223                                                packet))))
224               pgg-default-scheme))
225          (entity (pgg-make-scheme scheme))
226          (key (cdr (assq 'key-identifier packet)))
227          keyserver
228          status)
229     (and (stringp key)
230          (setq key (concat "0x" (pgg-truncate-key-identifier key)))
231          (null (pgg-lookup-key-string key))
232          (or fetch (interactive-p))
233          (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
234          (setq keyserver 
235                (or (cdr (assq 'preferred-key-server packet))
236                    pgg-default-keyserver-address))
237          (pgg-fetch-key keyserver key))
238     (setq status (luna-send entity 'verify-region 
239                             entity start end signature))
240     (when (interactive-p)
241       (with-output-to-temp-buffer pgg-echo-buffer
242         (set-buffer standard-output)
243         (insert-buffer-substring 
244          (if status pgg-output-buffer pgg-errors-buffer))
245         ))
246     status))
247
248 ;;;###autoload
249 (defun pgg-insert-key ()
250   (interactive)
251   (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
252     (luna-send entity 'insert-key entity)))
253
254 ;;;###autoload
255 (defun pgg-snarf-keys-region (start end)
256   (interactive "r")
257   (let* ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme)))
258          (status (luna-send entity 'snarf-keys-region entity start end)))
259     (when (interactive-p)
260       (with-output-to-temp-buffer pgg-echo-buffer
261         (set-buffer standard-output)
262         (insert-buffer-substring 
263          (if status pgg-output-buffer pgg-errors-buffer))
264         ))
265     status))
266
267 (defun pgg-lookup-key-string (string &optional type)
268   (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
269     (luna-send entity 'lookup-key-string entity string type)))
270
271 (defvar pgg-insert-url-function  (function pgg-insert-url-with-w3))
272
273 (defun pgg-insert-url-with-w3 (url)
274   (require 'w3)
275   (require 'url)
276   (let (buffer-file-name)
277     (url-insert-file-contents url)))
278
279 (defvar pgg-insert-url-extra-arguments nil)
280 (defvar pgg-insert-url-program nil)
281
282 (defun pgg-insert-url-with-program (url)
283   (let ((args (copy-sequence pgg-insert-url-extra-arguments))
284         process)
285     (insert
286      (with-temp-buffer
287        (setq process 
288              (apply #'start-process " *PGG url*" (current-buffer)
289                     pgg-insert-url-program (nconc args (list url))))
290        (set-process-sentinel process #'ignore)
291        (while (eq 'run (process-status process))
292          (accept-process-output process 5))
293        (delete-process process)
294        (if (and process (eq 'run (process-status process)))
295            (interrupt-process process))
296        (buffer-string)))
297     ))
298
299 (defun pgg-fetch-key (keyserver key)
300   "Attempt to fetch a key for addition to PGP or GnuPG keyring."
301   (with-current-buffer (get-buffer-create pgg-output-buffer)
302     (buffer-disable-undo)
303     (erase-buffer)
304     (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
305                      (substring keyserver 0 (1- (match-end 0))))))
306       (save-excursion
307         (funcall pgg-insert-url-function
308                  (if proto keyserver
309                    (format "http://%s:11371/pks/lookup?op=get&search=%s"
310                            keyserver key))))
311       (when (re-search-forward "^-+BEGIN" nil 'last)
312         (delete-region (point-min) (match-beginning 0))
313         (when (re-search-forward "^-+END" nil t)
314           (delete-region (progn (end-of-line) (point))
315                          (point-max)))
316         (insert "\n")
317         (with-temp-buffer
318           (insert-buffer-substring pgg-output-buffer)
319           (pgg-snarf-keys-region (point-min)(point-max))))
320       )))
321
322
323 ;;; @ utility functions
324 ;;;
325
326 (defvar pgg-passphrase-cache-expiry 16)
327 (defvar pgg-passphrase-cache (make-vector 7 0))
328
329 (defvar pgg-read-passphrase nil)
330 (defun pgg-read-passphrase (prompt &optional key)
331   (if (not pgg-read-passphrase)
332       (if (functionp 'read-passwd)
333           (setq pgg-read-passphrase 'read-passwd)
334         (if (load "passwd" t)
335             (setq pgg-read-passphrase 'read-passwd)
336           (autoload 'ange-ftp-read-passwd "ange-ftp")
337           (setq pgg-read-passphrase 'ange-ftp-read-passwd))))
338   (or (and key (setq key (pgg-truncate-key-identifier key))
339            (symbol-value (intern-soft key pgg-passphrase-cache)))
340       (funcall pgg-read-passphrase prompt)))
341
342 (defun pgg-add-passphrase-cache (key passphrase)
343   (setq key (pgg-truncate-key-identifier key))
344   (set (intern key pgg-passphrase-cache)
345        passphrase)
346   (run-at-time pgg-passphrase-cache-expiry nil
347                #'pgg-remove-passphrase-cache
348                key))
349
350 (defun pgg-remove-passphrase-cache (key)
351   (unintern key pgg-passphrase-cache))
352
353
354 ;;; @ postprocess macros
355 ;;;
356
357 (put 'pgg-process-when-success 'lisp-indent-function 0)
358
359 (defmacro pgg-process-when-success (&rest body)
360   `(with-current-buffer pgg-output-buffer
361      (if (zerop (buffer-size)) nil ,@body t)))
362
363 (provide 'pgg)
364
365 ;;; pgg.el ends here