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