c6b03c12a818fe23491c43e236b34398000d215e
[gnus] / lisp / pgg-pgp.el
1 ;;; pgg-pgp.el --- PGP 2.* and 6.* support for PGG.
2
3 ;; Copyright (C) 1999, 2000, 2003 Free Software Foundation, Inc.
4
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Created: 1999/11/02
7 ;; Keywords: PGP, OpenPGP
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU 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
29   (require 'cl)                         ; for pgg macros
30   (require 'pgg))
31
32 (defgroup pgg-pgp ()
33   "PGP 2.* and 6.* interface."
34   :group 'pgg)
35
36 (defcustom pgg-pgp-program "pgp"
37   "PGP 2.* and 6.* executable."
38   :group 'pgg-pgp
39   :type 'string)
40
41 (defcustom pgg-pgp-shell-file-name "/bin/sh"
42   "File name to load inferior shells from.
43 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
44   :group 'pgg-pgp
45   :type 'string)
46
47 (defcustom pgg-pgp-shell-command-switch "-c"
48   "Switch used to have the shell execute its command line argument."
49   :group 'pgg-pgp
50   :type 'string)
51
52 (defcustom pgg-pgp-extra-args nil
53   "Extra arguments for every PGP invocation."
54   :group 'pgg-pgp
55   :type '(choice
56           (const :tag "None" nil)
57           (string :tag "Arguments")))
58
59 (defvar pgg-pgp-user-id nil
60   "PGP ID of your default identity.")
61
62 (defun pgg-pgp-process-region (start end passphrase program args)
63   (let* ((errors-file-name (pgg-make-temp-file "pgg-errors"))
64          (args
65           (append args
66                   pgg-pgp-extra-args
67                   (list (concat "2>" errors-file-name))))
68          (shell-file-name pgg-pgp-shell-file-name)
69          (shell-command-switch pgg-pgp-shell-command-switch)
70          (process-environment process-environment)
71          (output-buffer pgg-output-buffer)
72          (errors-buffer pgg-errors-buffer)
73          (process-connection-type nil)
74          process status exit-status)
75     (with-current-buffer (get-buffer-create output-buffer)
76       (buffer-disable-undo)
77       (erase-buffer))
78     (when passphrase
79       (setenv "PGPPASSFD" "0"))
80     (unwind-protect
81         (progn
82           (let ((coding-system-for-read 'binary)
83                 (coding-system-for-write 'binary))
84             (setq process
85                   (apply #'funcall
86                          #'start-process-shell-command "*PGP*" output-buffer
87                          program args)))
88           (set-process-sentinel process #'ignore)
89           (when passphrase
90             (process-send-string process (concat passphrase "\n")))
91           (process-send-region process start end)
92           (process-send-eof process)
93           (while (eq 'run (process-status process))
94             (accept-process-output process 5))
95           (setq status (process-status process)
96                 exit-status (process-exit-status process))
97           (delete-process process)
98           (with-current-buffer output-buffer
99             (pgg-convert-lbt-region (point-min)(point-max) 'LF)
100
101             (if (memq status '(stop signal))
102                 (error "%s exited abnormally: '%s'" program exit-status))
103             (if (= 127 exit-status)
104                 (error "%s could not be found" program))
105
106             (set-buffer (get-buffer-create errors-buffer))
107             (buffer-disable-undo)
108             (erase-buffer)
109             (insert-file-contents errors-file-name)))
110       (if (and process (eq 'run (process-status process)))
111           (interrupt-process process))
112       (condition-case nil
113           (delete-file errors-file-name)
114         (file-error nil)))))
115
116 (defun pgg-pgp-lookup-key (string &optional type)
117   "Search keys associated with STRING."
118   (let ((args (list "+batchmode" "+language=en" "-kv" string)))
119     (with-current-buffer (get-buffer-create pgg-output-buffer)
120       (buffer-disable-undo)
121       (erase-buffer)
122       (apply #'call-process pgg-pgp-program nil t nil args)
123       (goto-char (point-min))
124       (cond
125        ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
126         (buffer-substring (point)(+ 8 (point))))
127        ((re-search-forward "^Type" nil t);PGP 6.*
128         (beginning-of-line 2)
129         (substring
130          (nth 2 (split-string
131                  (buffer-substring (point)(progn (end-of-line) (point)))))
132          2))))))
133
134 (defun pgg-pgp-encrypt-region (start end recipients)
135   "Encrypt the current region between START and END."
136   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
137          (args
138           `("+encrypttoself=off +verbose=1" "+batchmode"
139             "+language=us" "-fate"
140             ,@(if recipients
141                   (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
142                           (append recipients
143                                   (if pgg-encrypt-for-me
144                                       (list pgg-pgp-user-id))))))))
145     (pgg-pgp-process-region start end nil pgg-pgp-program args)
146     (pgg-process-when-success nil)))
147
148 (defun pgg-pgp-decrypt-region (start end)
149   "Decrypt the current region between START and END."
150   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
151          (key (pgg-pgp-lookup-key pgg-pgp-user-id 'encrypt))
152          (passphrase
153           (pgg-read-passphrase
154            (format "PGP passphrase for %s: " pgg-pgp-user-id) key))
155          (args
156           '("+verbose=1" "+batchmode" "+language=us" "-f")))
157     (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
158     (pgg-process-when-success
159       (if pgg-cache-passphrase
160           (pgg-add-passphrase-cache key passphrase)))))
161
162 (defun pgg-pgp-sign-region (start end &optional clearsign)
163   "Make detached signature from text between START and END."
164   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
165          (passphrase
166           (pgg-read-passphrase
167            (format "PGP passphrase for %s: " pgg-pgp-user-id)
168            (pgg-pgp-lookup-key pgg-pgp-user-id 'sign)))
169          (args
170           (list (if clearsign "-fast" "-fbast")
171                 "+verbose=1" "+language=us" "+batchmode"
172                 "-u" pgg-pgp-user-id)))
173     (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
174     (pgg-process-when-success
175       (goto-char (point-min))
176       (when (re-search-forward "^-+BEGIN PGP" nil t);XXX
177         (let ((packet
178                (cdr (assq 2 (pgg-parse-armor-region
179                              (progn (beginning-of-line 2)
180                                     (point))
181                              (point-max))))))
182           (if pgg-cache-passphrase
183               (pgg-add-passphrase-cache
184                (cdr (assq 'key-identifier packet))
185                passphrase)))))))
186
187 (defun pgg-pgp-verify-region (start end &optional signature)
188   "Verify region between START and END as the detached signature SIGNATURE."
189   (let* ((orig-file (pgg-make-temp-file "pgg"))
190          (args '("+verbose=1" "+batchmode" "+language=us"))
191          (orig-mode (default-file-modes)))
192     (unwind-protect
193         (progn
194           (set-default-file-modes 448)
195           (let ((coding-system-for-write 'binary)
196                 jka-compr-compression-info-list jam-zcat-filename-list)
197             (write-region start end orig-file)))
198       (set-default-file-modes orig-mode))
199     (if (stringp signature)
200         (progn
201           (copy-file signature (setq signature (concat orig-file ".asc")))
202           (setq args (append args (list signature orig-file))))
203       (setq args (append args (list orig-file))))
204     (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
205     (delete-file orig-file)
206     (if signature (delete-file signature))
207     (pgg-process-when-success
208       (goto-char (point-min))
209       (let ((case-fold-search t))
210         (while (re-search-forward "^warning: " nil t)
211           (delete-region (match-beginning 0)
212                          (progn (beginning-of-line 2) (point)))))
213       (goto-char (point-min))
214       (when (re-search-forward "^\\.$" nil t)
215         (delete-region (point-min)
216                        (progn (beginning-of-line 2)
217                               (point)))))))
218
219 (defun pgg-pgp-insert-key ()
220   "Insert public key at point."
221   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
222          (args
223           (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
224                 (concat "\"" pgg-pgp-user-id "\""))))
225     (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
226     (insert-buffer-substring pgg-output-buffer)))
227
228 (defun pgg-pgp-snarf-keys-region (start end)
229   "Add all public keys in region between START and END to the keyring."
230   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
231          (key-file (pgg-make-temp-file "pgg"))
232          (args
233           (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
234                 key-file)))
235     (let ((coding-system-for-write 'raw-text-dos))
236       (write-region start end key-file))
237     (pgg-pgp-process-region start end nil pgg-pgp-program args)
238     (delete-file key-file)
239     (pgg-process-when-success nil)))
240
241 (provide 'pgg-pgp)
242
243 ;;; arch-tag: 076b7801-37b2-49a6-97c3-218fdecde33c
244 ;;; pgg-pgp.el ends here