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