* pgg-pgp.el (sign-region): Fix regexp for the beginning of armor.
[elisp/semi.git] / pgg-pgp.el
1 ;;; pgg-pgp.el --- PGP 2.* and 6.* support for PGG.
2
3 ;; Copyright (C) 1999 Daiki Ueno
4
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
6 ;; Created: 1999/11/02
7 ;; Keywords: PGP, OpenPGP
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 (eval-when-compile (require 'pgg))
29
30 (defgroup pgg-pgp ()
31   "PGP 2.* and 6.* interface"
32   :group 'pgg)
33
34 (defcustom pgg-pgp-program "pgp" 
35   "PGP 2.* and 6.* executable."
36   :group 'pgg-pgp
37   :type 'string)
38
39 (defcustom pgg-pgp-shell-file-name "/bin/sh"
40   "File name to load inferior shells from.  Bourne shell or its equivalent
41 \(not tcsh) is needed for \"2>\"."
42   :group 'pgg-pgp
43   :type 'string)
44
45 (defcustom pgg-pgp-shell-command-switch "-c"
46   "Switch used to have the shell execute its command line argument."
47   :group 'pgg-pgp
48   :type 'string)
49
50 (defcustom pgg-pgp-extra-args nil
51   "Extra arguments for every PGP invocation."
52   :group 'pgg-pgp
53   :type 'string)
54
55 (eval-and-compile
56   (luna-define-class pgg-scheme-pgp (pgg-scheme))
57   )
58   
59 (defvar pgg-pgp-user-id nil
60   "GnuPG ID of your default identity.")
61
62 (defvar pgg-scheme-pgp-instance nil)
63
64 ;;;###autoload
65 (defun pgg-make-scheme-pgp ()
66   (or pgg-scheme-pgp-instance
67       (setq pgg-scheme-pgp-instance
68             (luna-make-entity 'pgg-scheme-pgp))))
69
70 (defun pgg-pgp-process-region (start end passphrase program args)
71   (let* ((errors-file-name
72           (concat temporary-file-directory 
73                   (make-temp-name "pgg-errors")))
74          (args 
75           (append args 
76                   pgg-pgp-extra-args
77                   (list (concat "2>" errors-file-name))))
78          (shell-file-name pgg-pgp-shell-file-name)
79          (shell-command-switch pgg-pgp-shell-command-switch)
80          (output-buffer pgg-output-buffer)
81          (errors-buffer pgg-errors-buffer)
82          (process-connection-type nil)
83          process status exit-status)
84     (with-current-buffer (get-buffer-create output-buffer)
85       (buffer-disable-undo)
86       (erase-buffer))
87     (when passphrase
88       (setenv "PGPPASSFD" "0"))
89     (setq process
90           (apply #'start-process-shell-command "*PGP*" output-buffer
91                  program args))
92     (set-process-sentinel process 'ignore)
93     (when passphrase
94       (process-send-string process (concat passphrase "\n")))
95     (process-send-region process start end)
96     (process-send-eof process)
97     (while (eq 'run (process-status process))
98       (accept-process-output process 5))
99     (setq status (process-status process)
100           exit-status (process-exit-status process))
101     (delete-process process)
102     (with-current-buffer output-buffer
103       (goto-char (point-min))
104       (while (search-forward "\r$" nil t)
105         (replace-match ""))
106       (if (memq status '(stop signal))
107           (error "%s exited abnormally: '%s'" program exit-status))
108       (if (= 127 exit-status)
109           (error "%s could not be found" program))
110
111       (set-buffer (get-buffer-create errors-buffer))
112       (buffer-disable-undo)
113       (erase-buffer)
114       (insert-file-contents errors-file-name)
115       (delete-file errors-file-name)
116       
117       (if (and process (eq 'run (process-status process)))
118           (interrupt-process process))
119       )
120     ))
121
122 (luna-define-method lookup-key-string ((scheme pgg-scheme-pgp) 
123                                        string &optional type)
124   (let ((args (list "+batchmode" "+language=en" "-kv" string)))
125     (with-current-buffer (get-buffer-create pgg-output-buffer)
126       (buffer-disable-undo)
127       (erase-buffer)
128       (apply #'call-process pgg-pgp-program nil t args)
129       (goto-char (point-min))
130       (cond
131        ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
132         (buffer-substring (point)(+ 8 (point))))
133        ((re-search-forward "^Type" nil t);PGP 6.*
134         (beginning-of-line 2)
135         (substring 
136          (nth 2 (split-string 
137                  (buffer-substring (point)
138                                    (progn (end-of-line) (point)))
139                  ))
140          2))))
141     ))
142
143 (luna-define-method encrypt-region ((scheme pgg-scheme-pgp) 
144                                     start end recipients)
145   (let* ((pgg-pgp-user-id pgg-default-user-id)
146          (passphrase
147           (pgg-read-passphrase 
148            (format "PGP passphrase for %s: " pgg-pgp-user-id)
149            (luna-send scheme 'lookup-key-string 
150                       scheme pgg-pgp-user-id 'encrypt)))
151          (args 
152           `("+encrypttoself=off +verbose=1" "+batchmode"
153             "+language=us" "-fate"
154             ,@(if recipients
155                   (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
156                           recipients)))))
157     (pgg-pgp-process-region start end passphrase 
158                             pgg-pgp-program args)
159     (pgg-process-when-success
160       (let ((packet 
161              (cdr (assq 1 (pgg-parse-armor-region 
162                            (point-min)(point-max))))))
163         (pgg-add-passphrase-cache 
164          (cdr (assq 'key-identifier packet))
165          passphrase)))
166     ))
167
168 (luna-define-method decrypt-region ((scheme pgg-scheme-pgp) 
169                                     start end)
170   (let* ((pgg-pgp-user-id pgg-default-user-id)
171          (passphrase
172           (pgg-read-passphrase 
173            (format "PGP passphrase for %s: " pgg-pgp-user-id)
174            (luna-send scheme 'lookup-key-string 
175                       scheme pgg-pgp-user-id 'encrypt)))
176          (args 
177           '("+verbose=1" "+batchmode" "+language=us" "-f")))
178     (pgg-pgp-process-region start end passphrase 
179                             pgg-pgp-program args)
180     (pgg-process-when-success nil)
181     ))
182
183 (luna-define-method sign-region ((scheme pgg-scheme-pgp) 
184                                  start end &optional clearsign)
185   (let* ((pgg-pgp-user-id pgg-default-user-id)
186          (passphrase
187           (pgg-read-passphrase 
188            (format "PGP passphrase for %s: " pgg-pgp-user-id)
189            (luna-send scheme 'lookup-key-string
190                       scheme pgg-pgp-user-id 'sign)))
191          (args 
192           (list (if clearsign "-fast" "-fbast")
193                 "+verbose=1" "+language=us" "+batchmode"
194                 "-u" pgg-pgp-user-id)))
195     (pgg-pgp-process-region start end passphrase 
196                              pgg-pgp-program args)
197     (pgg-process-when-success
198       (goto-char (point-min))
199       (when (re-search-forward "^-+BEGIN PGP" nil t);XXX
200         (let ((packet 
201                (cdr (assq 2 (pgg-parse-armor-region 
202                              (progn (beginning-of-line 2)
203                                     (point))
204                              (point-max))))))
205           (pgg-add-passphrase-cache 
206            (cdr (assq 'key-identifier packet))
207            passphrase))))
208     ))
209
210 (luna-define-method verify-region ((scheme pgg-scheme-pgp) 
211                                    start end &optional signature)
212   (let* ((basename (expand-file-name "pgg" temporary-file-directory))
213          (orig-file (make-temp-name basename))
214          (args '("+verbose=1" "+batchmode" "+language=us")))
215     (write-region-as-binary start end orig-file)
216     (when (stringp signature)
217       (copy-file signature (setq signature (concat orig-file ".asc")))
218       (setq args (append args (list signature orig-file)))
219       )
220     (pgg-pgp-process-region (point-min)(point-max) nil
221                             pgg-pgp-program args)
222     (delete-file orig-file)
223     (if signature (delete-file signature))
224     (pgg-process-when-success nil)
225     ))
226
227 (luna-define-method insert-key ((scheme pgg-scheme-pgp))
228   (let* ((pgg-pgp-user-id pgg-default-user-id)
229          (args
230           (list "+verbose=1" "+batchmode" "+language=us" "-kxaf" 
231                 (concat "\"" pgg-pgp-user-id "\""))))
232     (pgg-pgp-process-region (point)(point) nil
233                              pgg-pgp-program args)
234     (insert-buffer-substring pgg-output-buffer)
235     ))
236
237 (luna-define-method snarf-keys-region ((scheme pgg-scheme-pgp)
238                                        start end)
239   (let* ((pgg-pgp-user-id pgg-default-user-id)
240          (basename (expand-file-name "pgg" temporary-file-directory))
241          (key-file (make-temp-name basename))
242          (args 
243           (list "+verbose=1" "+batchmode" "+language=us" "-kaf" 
244                 key-file)))
245     (write-region-as-raw-text-CRLF start end key-file)
246     (pgg-pgp-process-region start end nil
247                             pgg-pgp-program args)
248     (delete-file key-file)
249     (pgg-process-when-success nil)
250     ))
251
252 (provide 'pgg-pgp)
253
254 ;;; pgg-pgp.el ends here