]> code.delx.au - gnu-emacs/blob - lisp/gnus/mml1991.el
f9a47a7180702672c16dae1168c07a83897b3ec3
[gnu-emacs] / lisp / gnus / mml1991.el
1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Author: Sascha Lüdecke <sascha@meta-x.de>,
7 ;; Simon Josefsson <simon@josefsson.org> (Mailcrypt interface, Gnus glue)
8 ;; Keywords PGP
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
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-and-compile
30 ;; For Emacs <22.2 and XEmacs.
31 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
32
33 (if (locate-library "password-cache")
34 (require 'password-cache)
35 (require 'password)))
36
37 (eval-when-compile
38 (require 'cl)
39 (require 'mm-util))
40
41 (require 'mm-encode)
42 (require 'mml-sec)
43
44 (defvar mc-pgp-always-sign)
45
46 (autoload 'quoted-printable-decode-region "qp")
47 (autoload 'quoted-printable-encode-region "qp")
48
49 (autoload 'mm-decode-content-transfer-encoding "mm-bodies")
50 (autoload 'mm-encode-content-transfer-encoding "mm-bodies")
51 (autoload 'message-options-get "message")
52 (autoload 'message-options-set "message")
53
54 (defvar mml1991-use mml2015-use
55 "The package used for PGP.")
56
57 (defvar mml1991-function-alist
58 '((mailcrypt mml1991-mailcrypt-sign
59 mml1991-mailcrypt-encrypt)
60 (pgg mml1991-pgg-sign
61 mml1991-pgg-encrypt)
62 (epg mml1991-epg-sign
63 mml1991-epg-encrypt))
64 "Alist of PGP functions.")
65
66 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
67 "If t, cache passphrase.")
68
69 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
70 "How many seconds the passphrase is cached.
71 Whether the passphrase is cached at all is controlled by
72 `mml1991-cache-passphrase'.")
73
74 (defvar mml1991-signers nil
75 "A list of your own key ID which will be used to sign a message.")
76
77 (defvar mml1991-encrypt-to-self nil
78 "If t, add your own key ID to recipient list when encryption.")
79
80 ;;; mailcrypt wrapper
81
82 (autoload 'mc-sign-generic "mc-toplev")
83
84 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
85 (defvar mml1991-verify-function 'mailcrypt-verify)
86
87 (defun mml1991-mailcrypt-sign (cont)
88 (let ((text (current-buffer))
89 headers signature
90 (result-buffer (get-buffer-create "*GPG Result*")))
91 ;; Save MIME Content[^ ]+: headers from signing
92 (goto-char (point-min))
93 (while (looking-at "^Content[^ ]+:") (forward-line))
94 (unless (bobp)
95 (setq headers (buffer-string))
96 (delete-region (point-min) (point)))
97 (goto-char (point-max))
98 (unless (bolp)
99 (insert "\n"))
100 (quoted-printable-decode-region (point-min) (point-max))
101 (with-temp-buffer
102 (setq signature (current-buffer))
103 (insert-buffer-substring text)
104 (unless (mc-sign-generic (message-options-get 'message-sender)
105 nil nil nil nil)
106 (unless (> (point-max) (point-min))
107 (pop-to-buffer result-buffer)
108 (error "Sign error")))
109 (goto-char (point-min))
110 (while (re-search-forward "\r+$" nil t)
111 (replace-match "" t t))
112 (quoted-printable-encode-region (point-min) (point-max))
113 (set-buffer text)
114 (delete-region (point-min) (point-max))
115 (if headers (insert headers))
116 (insert "\n")
117 (insert-buffer-substring signature)
118 (goto-char (point-max)))))
119
120 (declare-function mc-encrypt-generic "ext:mc-toplev"
121 (&optional recipients scheme start end from sign))
122
123 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
124 (let ((text (current-buffer))
125 (mc-pgp-always-sign
126 (or mc-pgp-always-sign
127 sign
128 (eq t (or (message-options-get 'message-sign-encrypt)
129 (message-options-set
130 'message-sign-encrypt
131 (or (y-or-n-p "Sign the message? ")
132 'not))))
133 'never))
134 cipher
135 (result-buffer (get-buffer-create "*GPG Result*")))
136 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
137 (goto-char (point-min))
138 (while (looking-at "^Content[^ ]+:") (forward-line))
139 (unless (bobp)
140 (delete-region (point-min) (point)))
141 (mm-with-unibyte-current-buffer
142 (with-temp-buffer
143 (inline (mm-disable-multibyte))
144 (setq cipher (current-buffer))
145 (insert-buffer-substring text)
146 (unless (mc-encrypt-generic
147 (or
148 (message-options-get 'message-recipients)
149 (message-options-set 'message-recipients
150 (read-string "Recipients: ")))
151 nil
152 (point-min) (point-max)
153 (message-options-get 'message-sender)
154 'sign)
155 (unless (> (point-max) (point-min))
156 (pop-to-buffer result-buffer)
157 (error "Encrypt error")))
158 (goto-char (point-min))
159 (while (re-search-forward "\r+$" nil t)
160 (replace-match "" t t))
161 (set-buffer text)
162 (delete-region (point-min) (point-max))
163 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
164 ;;(insert "Version: 1\n\n")
165 (insert "\n")
166 (insert-buffer-substring cipher)
167 (goto-char (point-max))))))
168
169 ;; pgg wrapper
170
171 (autoload 'pgg-sign-region "pgg")
172 (autoload 'pgg-encrypt-region "pgg")
173
174 (defvar pgg-default-user-id)
175 (defvar pgg-errors-buffer)
176 (defvar pgg-output-buffer)
177
178 (defun mml1991-pgg-sign (cont)
179 (let ((pgg-text-mode t)
180 (pgg-default-user-id (or (message-options-get 'mml-sender)
181 pgg-default-user-id))
182 headers cte)
183 ;; Don't sign headers.
184 (goto-char (point-min))
185 (when (re-search-forward "^$" nil t)
186 (setq headers (buffer-substring (point-min) (point)))
187 (save-restriction
188 (narrow-to-region (point-min) (point))
189 (setq cte (mail-fetch-field "content-transfer-encoding")))
190 (forward-line 1)
191 (delete-region (point-min) (point))
192 (when cte
193 (setq cte (intern (downcase cte)))
194 (mm-decode-content-transfer-encoding cte)))
195 (unless (pgg-sign-region (point-min) (point-max) t)
196 (pop-to-buffer pgg-errors-buffer)
197 (error "Encrypt error"))
198 (delete-region (point-min) (point-max))
199 (mm-with-unibyte-current-buffer
200 (insert-buffer-substring pgg-output-buffer)
201 (goto-char (point-min))
202 (while (re-search-forward "\r+$" nil t)
203 (replace-match "" t t))
204 (when cte
205 (mm-encode-content-transfer-encoding cte))
206 (goto-char (point-min))
207 (when headers
208 (insert headers))
209 (insert "\n"))
210 t))
211
212 (defun mml1991-pgg-encrypt (cont &optional sign)
213 (goto-char (point-min))
214 (when (re-search-forward "^$" nil t)
215 (let ((cte (save-restriction
216 (narrow-to-region (point-min) (point))
217 (mail-fetch-field "content-transfer-encoding"))))
218 ;; Strip MIME headers since it will be ASCII armored.
219 (forward-line 1)
220 (delete-region (point-min) (point))
221 (when cte
222 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
223 (unless (let ((pgg-text-mode t))
224 (pgg-encrypt-region
225 (point-min) (point-max)
226 (split-string
227 (or
228 (message-options-get 'message-recipients)
229 (message-options-set 'message-recipients
230 (read-string "Recipients: ")))
231 "[ \f\t\n\r\v,]+")
232 sign))
233 (pop-to-buffer pgg-errors-buffer)
234 (error "Encrypt error"))
235 (delete-region (point-min) (point-max))
236 (insert "\n")
237 (insert-buffer-substring pgg-output-buffer)
238 t)
239
240 ;; epg wrapper
241
242 (defvar epg-user-id-alist)
243
244 (autoload 'epg-make-context "epg")
245 (autoload 'epg-passphrase-callback-function "epg")
246 (autoload 'epa-select-keys "epa")
247 (autoload 'epg-list-keys "epg")
248 (autoload 'epg-context-set-armor "epg")
249 (autoload 'epg-context-set-textmode "epg")
250 (autoload 'epg-context-set-signers "epg")
251 (autoload 'epg-context-set-passphrase-callback "epg")
252 (autoload 'epg-sign-string "epg")
253 (autoload 'epg-encrypt-string "epg")
254 (autoload 'epg-configuration "epg-config")
255 (autoload 'epg-expand-group "epg-config")
256
257 (defvar mml1991-epg-secret-key-id-list nil)
258
259 (defun mml1991-epg-passphrase-callback (context key-id ignore)
260 (if (eq key-id 'SYM)
261 (epg-passphrase-callback-function context key-id nil)
262 (let* ((entry (assoc key-id epg-user-id-alist))
263 (passphrase
264 (password-read
265 (format "GnuPG passphrase for %s: "
266 (if entry
267 (cdr entry)
268 key-id))
269 (if (eq key-id 'PIN)
270 "PIN"
271 key-id))))
272 (when passphrase
273 (let ((password-cache-expiry mml1991-passphrase-cache-expiry))
274 (password-cache-add key-id passphrase))
275 (setq mml1991-epg-secret-key-id-list
276 (cons key-id mml1991-epg-secret-key-id-list))
277 (copy-sequence passphrase)))))
278
279 (defun mml1991-epg-sign (cont)
280 (let ((context (epg-make-context))
281 headers cte signers signature)
282 (if (eq mm-sign-option 'guided)
283 (setq signers (epa-select-keys context "Select keys for signing.
284 If no one is selected, default secret key is used. "
285 mml1991-signers t))
286 (if mml1991-signers
287 (setq signers (mapcar (lambda (name)
288 (car (epg-list-keys context name t)))
289 mml1991-signers))))
290 (epg-context-set-armor context t)
291 (epg-context-set-textmode context t)
292 (epg-context-set-signers context signers)
293 (if mml1991-cache-passphrase
294 (epg-context-set-passphrase-callback
295 context
296 #'mml1991-epg-passphrase-callback))
297 ;; Don't sign headers.
298 (goto-char (point-min))
299 (when (re-search-forward "^$" nil t)
300 (setq headers (buffer-substring (point-min) (point)))
301 (save-restriction
302 (narrow-to-region (point-min) (point))
303 (setq cte (mail-fetch-field "content-transfer-encoding")))
304 (forward-line 1)
305 (delete-region (point-min) (point))
306 (when cte
307 (setq cte (intern (downcase cte)))
308 (mm-decode-content-transfer-encoding cte)))
309 (condition-case error
310 (setq signature (epg-sign-string context (buffer-string) 'clear)
311 mml1991-epg-secret-key-id-list nil)
312 (error
313 (while mml1991-epg-secret-key-id-list
314 (password-cache-remove (car mml1991-epg-secret-key-id-list))
315 (setq mml1991-epg-secret-key-id-list
316 (cdr mml1991-epg-secret-key-id-list)))
317 (signal (car error) (cdr error))))
318 (delete-region (point-min) (point-max))
319 (mm-with-unibyte-current-buffer
320 (insert signature)
321 (goto-char (point-min))
322 (while (re-search-forward "\r+$" nil t)
323 (replace-match "" t t))
324 (when cte
325 (mm-encode-content-transfer-encoding cte))
326 (goto-char (point-min))
327 (when headers
328 (insert headers))
329 (insert "\n"))
330 t))
331
332 (defun mml1991-epg-encrypt (cont &optional sign)
333 (goto-char (point-min))
334 (when (re-search-forward "^$" nil t)
335 (let ((cte (save-restriction
336 (narrow-to-region (point-min) (point))
337 (mail-fetch-field "content-transfer-encoding"))))
338 ;; Strip MIME headers since it will be ASCII armored.
339 (forward-line 1)
340 (delete-region (point-min) (point))
341 (when cte
342 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
343 (let ((context (epg-make-context))
344 (recipients
345 (if (message-options-get 'message-recipients)
346 (split-string
347 (message-options-get 'message-recipients)
348 "[ \f\t\n\r\v,]+")))
349 cipher signers config)
350 ;; We should remove this check if epg-0.0.6 is released.
351 (if (and (condition-case nil
352 (require 'epg-config)
353 (error))
354 (functionp #'epg-expand-group))
355 (setq config (epg-configuration)
356 recipients
357 (apply #'nconc
358 (mapcar (lambda (recipient)
359 (or (epg-expand-group config recipient)
360 (list recipient)))
361 recipients))))
362 (if (eq mm-encrypt-option 'guided)
363 (setq recipients
364 (epa-select-keys context "Select recipients for encryption.
365 If no one is selected, symmetric encryption will be performed. "
366 recipients))
367 (setq recipients
368 (delq nil (mapcar (lambda (name)
369 (car (epg-list-keys context name)))
370 recipients))))
371 (if mml1991-encrypt-to-self
372 (if mml1991-signers
373 (setq recipients
374 (nconc recipients
375 (mapcar (lambda (name)
376 (car (epg-list-keys context name)))
377 mml1991-signers)))
378 (error "mml1991-signers not set")))
379 (when sign
380 (if (eq mm-sign-option 'guided)
381 (setq signers (epa-select-keys context "Select keys for signing.
382 If no one is selected, default secret key is used. "
383 mml1991-signers t))
384 (if mml1991-signers
385 (setq signers (mapcar (lambda (name)
386 (car (epg-list-keys context name t)))
387 mml1991-signers))))
388 (epg-context-set-signers context signers))
389 (epg-context-set-armor context t)
390 (epg-context-set-textmode context t)
391 (if mml1991-cache-passphrase
392 (epg-context-set-passphrase-callback
393 context
394 #'mml1991-epg-passphrase-callback))
395 (condition-case error
396 (setq cipher
397 (epg-encrypt-string context (buffer-string) recipients sign)
398 mml1991-epg-secret-key-id-list nil)
399 (error
400 (while mml1991-epg-secret-key-id-list
401 (password-cache-remove (car mml1991-epg-secret-key-id-list))
402 (setq mml1991-epg-secret-key-id-list
403 (cdr mml1991-epg-secret-key-id-list)))
404 (signal (car error) (cdr error))))
405 (delete-region (point-min) (point-max))
406 (insert "\n" cipher))
407 t)
408
409 ;;;###autoload
410 (defun mml1991-encrypt (cont &optional sign)
411 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
412 (if func
413 (funcall func cont sign)
414 (error "Cannot find encrypt function"))))
415
416 ;;;###autoload
417 (defun mml1991-sign (cont)
418 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
419 (if func
420 (funcall func cont)
421 (error "Cannot find sign function"))))
422
423 (provide 'mml1991)
424
425 ;; Local Variables:
426 ;; coding: iso-8859-1
427 ;; End:
428
429 ;;; mml1991.el ends here