1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Sascha Lüdecke <sascha@meta-x.de>,
7 ;; Simon Josefsson <simon@josefsson.org> (Mailcrypt interface, Gnus glue)
10 ;; This file is part of GNU Emacs.
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 2, or (at your option)
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.
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
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
37 (defvar mc-pgp-always-sign)
39 (autoload 'quoted-printable-decode-region "qp")
40 (autoload 'quoted-printable-encode-region "qp")
42 (defvar mml1991-use mml2015-use
43 "The package used for PGP.")
45 (defvar mml1991-function-alist
46 '((mailcrypt mml1991-mailcrypt-sign
47 mml1991-mailcrypt-encrypt)
54 "Alist of PGP functions.")
56 (defvar mml1991-verbose mml-secure-verbose
57 "If non-nil, ask the user about the current operation more verbosely.")
59 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
60 "If t, cache passphrase.")
62 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
63 "How many seconds the passphrase is cached.
64 Whether the passphrase is cached at all is controlled by
65 `mml1991-cache-passphrase'.")
67 (defvar mml1991-signers nil
68 "A list of key ID which will be used to sign a message.")
73 (autoload 'mc-sign-generic "mc-toplev"))
75 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
76 (defvar mml1991-verify-function 'mailcrypt-verify)
78 (defun mml1991-mailcrypt-sign (cont)
79 (let ((text (current-buffer))
81 (result-buffer (get-buffer-create "*GPG Result*")))
82 ;; Save MIME Content[^ ]+: headers from signing
83 (goto-char (point-min))
84 (while (looking-at "^Content[^ ]+:") (forward-line))
86 (setq headers (buffer-string))
87 (delete-region (point-min) (point)))
88 (goto-char (point-max))
91 (quoted-printable-decode-region (point-min) (point-max))
93 (setq signature (current-buffer))
94 (insert-buffer-substring text)
95 (unless (mc-sign-generic (message-options-get 'message-sender)
97 (unless (> (point-max) (point-min))
98 (pop-to-buffer result-buffer)
99 (error "Sign error")))
100 (goto-char (point-min))
101 (while (re-search-forward "\r+$" nil t)
102 (replace-match "" t t))
103 (quoted-printable-encode-region (point-min) (point-max))
105 (delete-region (point-min) (point-max))
106 (if headers (insert headers))
108 (insert-buffer-substring signature)
109 (goto-char (point-max)))))
111 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
112 (let ((text (current-buffer))
114 (or mc-pgp-always-sign
116 (eq t (or (message-options-get 'message-sign-encrypt)
118 'message-sign-encrypt
119 (or (y-or-n-p "Sign the message? ")
123 (result-buffer (get-buffer-create "*GPG Result*")))
124 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
125 (goto-char (point-min))
126 (while (looking-at "^Content[^ ]+:") (forward-line))
128 (delete-region (point-min) (point)))
129 (mm-with-unibyte-current-buffer
131 (setq cipher (current-buffer))
132 (insert-buffer-substring text)
133 (unless (mc-encrypt-generic
135 (message-options-get 'message-recipients)
136 (message-options-set 'message-recipients
137 (read-string "Recipients: ")))
139 (point-min) (point-max)
140 (message-options-get 'message-sender)
142 (unless (> (point-max) (point-min))
143 (pop-to-buffer result-buffer)
144 (error "Encrypt error")))
145 (goto-char (point-min))
146 (while (re-search-forward "\r+$" nil t)
147 (replace-match "" t t))
149 (delete-region (point-min) (point-max))
150 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
151 ;;(insert "Version: 1\n\n")
153 (insert-buffer-substring cipher)
154 (goto-char (point-max))))))
159 (autoload 'gpg-sign-cleartext "gpg"))
161 (defun mml1991-gpg-sign (cont)
162 (let ((text (current-buffer))
164 (result-buffer (get-buffer-create "*GPG Result*")))
165 ;; Save MIME Content[^ ]+: headers from signing
166 (goto-char (point-min))
167 (while (looking-at "^Content[^ ]+:") (forward-line))
169 (setq headers (buffer-string))
170 (delete-region (point-min) (point)))
171 (goto-char (point-max))
174 (quoted-printable-decode-region (point-min) (point-max))
176 (unless (gpg-sign-cleartext text (setq signature (current-buffer))
179 (message-options-get 'message-sender))
180 (unless (> (point-max) (point-min))
181 (pop-to-buffer result-buffer)
182 (error "Sign error")))
183 (goto-char (point-min))
184 (while (re-search-forward "\r+$" nil t)
185 (replace-match "" t t))
186 (quoted-printable-encode-region (point-min) (point-max))
188 (delete-region (point-min) (point-max))
189 (if headers (insert headers))
191 (insert-buffer-substring signature)
192 (goto-char (point-max)))))
194 (defun mml1991-gpg-encrypt (cont &optional sign)
195 (let ((text (current-buffer))
197 (result-buffer (get-buffer-create "*GPG Result*")))
198 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
199 (goto-char (point-min))
200 (while (looking-at "^Content[^ ]+:") (forward-line))
202 (delete-region (point-min) (point)))
203 (mm-with-unibyte-current-buffer
205 (flet ((gpg-encrypt-func
206 (sign plaintext ciphertext result recipients &optional
207 passphrase sign-with-key armor textmode)
210 plaintext ciphertext result recipients passphrase
211 sign-with-key armor textmode)
213 plaintext ciphertext result recipients passphrase
215 (unless (gpg-encrypt-func
217 text (setq cipher (current-buffer))
221 (message-options-get 'message-recipients)
222 (message-options-set 'message-recipients
223 (read-string "Recipients: ")))
226 (message-options-get 'message-sender)
227 t t) ; armor & textmode
228 (unless (> (point-max) (point-min))
229 (pop-to-buffer result-buffer)
230 (error "Encrypt error"))))
231 (goto-char (point-min))
232 (while (re-search-forward "\r+$" nil t)
233 (replace-match "" t t))
235 (delete-region (point-min) (point-max))
236 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
237 ;;(insert "Version: 1\n\n")
239 (insert-buffer-substring cipher)
240 (goto-char (point-max))))))
245 (defvar pgg-default-user-id)
246 (defvar pgg-errors-buffer)
247 (defvar pgg-output-buffer))
249 (defun mml1991-pgg-sign (cont)
250 (let ((pgg-text-mode t)
251 (pgg-default-user-id (or (message-options-get 'mml-sender)
252 pgg-default-user-id))
254 ;; Don't sign headers.
255 (goto-char (point-min))
256 (when (re-search-forward "^$" nil t)
257 (setq headers (buffer-substring (point-min) (point)))
259 (narrow-to-region (point-min) (point))
260 (setq cte (mail-fetch-field "content-transfer-encoding")))
262 (delete-region (point-min) (point))
264 (setq cte (intern (downcase cte)))
265 (mm-decode-content-transfer-encoding cte)))
266 (unless (pgg-sign-region (point-min) (point-max) t)
267 (pop-to-buffer pgg-errors-buffer)
268 (error "Encrypt error"))
269 (delete-region (point-min) (point-max))
270 (mm-with-unibyte-current-buffer
271 (insert-buffer-substring pgg-output-buffer)
272 (goto-char (point-min))
273 (while (re-search-forward "\r+$" nil t)
274 (replace-match "" t t))
276 (mm-encode-content-transfer-encoding cte))
277 (goto-char (point-min))
283 (defun mml1991-pgg-encrypt (cont &optional sign)
284 (goto-char (point-min))
285 (when (re-search-forward "^$" nil t)
286 (let ((cte (save-restriction
287 (narrow-to-region (point-min) (point))
288 (mail-fetch-field "content-transfer-encoding"))))
289 ;; Strip MIME headers since it will be ASCII armoured.
291 (delete-region (point-min) (point))
293 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
294 (unless (let ((pgg-text-mode t))
296 (point-min) (point-max)
299 (message-options-get 'message-recipients)
300 (message-options-set 'message-recipients
301 (read-string "Recipients: ")))
304 (pop-to-buffer pgg-errors-buffer)
305 (error "Encrypt error"))
306 (delete-region (point-min) (point-max))
308 (insert-buffer-substring pgg-output-buffer)
314 (autoload 'epg-make-context "epg"))
317 (defvar epg-user-id-alist)
318 (autoload 'epg-passphrase-callback-function "epg")
319 (autoload 'epa-select-keys "epa")
320 (autoload 'epg-list-keys "epg")
321 (autoload 'epg-context-set-armor "epg")
322 (autoload 'epg-context-set-textmode "epg")
323 (autoload 'epg-context-set-signers "epg")
324 (autoload 'epg-context-set-passphrase-callback "epg")
325 (autoload 'epg-sign-string "epg")
326 (autoload 'epg-encrypt-string "epg"))
329 (defvar password-cache-expiry)
330 (autoload 'password-read "password")
331 (autoload 'password-cache-add "password")
332 (autoload 'password-cache-remove "password"))
334 (defvar mml1991-epg-secret-key-id-list nil)
336 (defun mml1991-epg-passphrase-callback (context key-id ignore)
338 (epg-passphrase-callback-function context key-id nil)
339 (let* ((entry (assoc key-id epg-user-id-alist))
342 (format "GnuPG passphrase for %s: "
350 (let ((password-cache-expiry mml1991-passphrase-cache-expiry))
351 (password-cache-add key-id passphrase))
352 (setq mml1991-epg-secret-key-id-list
353 (cons key-id mml1991-epg-secret-key-id-list))
354 (copy-sequence passphrase)))))
356 (defun mml1991-epg-sign (cont)
357 (let ((context (epg-make-context))
358 headers cte signers signature)
360 (setq signers (epa-select-keys context "Select keys for signing.
361 If no one is selected, default secret key is used. "
363 (setq signers (mapcar (lambda (name)
364 (car (epg-list-keys context name t)))
366 (list (message-options-get 'mml-sender))))))
367 (epg-context-set-armor context t)
368 (epg-context-set-textmode context t)
369 (epg-context-set-signers context signers)
370 (if mml1991-cache-passphrase
371 (epg-context-set-passphrase-callback
373 #'mml1991-epg-passphrase-callback))
374 ;; Don't sign headers.
375 (goto-char (point-min))
376 (when (re-search-forward "^$" nil t)
377 (setq headers (buffer-substring (point-min) (point)))
379 (narrow-to-region (point-min) (point))
380 (setq cte (mail-fetch-field "content-transfer-encoding")))
382 (delete-region (point-min) (point))
384 (setq cte (intern (downcase cte)))
385 (mm-decode-content-transfer-encoding cte)))
386 (condition-case error
387 (setq signature (epg-sign-string context (buffer-string) 'clear)
388 mml1991-epg-secret-key-id-list nil)
390 (while mml1991-epg-secret-key-id-list
391 (password-cache-remove (car mml1991-epg-secret-key-id-list))
392 (setq mml1991-epg-secret-key-id-list
393 (cdr mml1991-epg-secret-key-id-list)))
394 (signal (car error) (cdr error))))
395 (delete-region (point-min) (point-max))
396 (mm-with-unibyte-current-buffer
398 (goto-char (point-min))
399 (while (re-search-forward "\r+$" nil t)
400 (replace-match "" t t))
402 (mm-encode-content-transfer-encoding cte))
403 (goto-char (point-min))
409 (defun mml1991-epg-encrypt (cont &optional sign)
410 (goto-char (point-min))
411 (when (re-search-forward "^$" nil t)
412 (let ((cte (save-restriction
413 (narrow-to-region (point-min) (point))
414 (mail-fetch-field "content-transfer-encoding"))))
415 ;; Strip MIME headers since it will be ASCII armoured.
417 (delete-region (point-min) (point))
419 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
420 (let ((context (epg-make-context))
421 recipients cipher signers)
422 (if (or mml1991-verbose
423 (null (message-options-get 'message-recipients)))
425 (epa-select-keys context "Select recipients for encryption.
426 If no one is selected, symmetric encryption will be performed. "
427 (if (message-options-get 'message-recipients)
429 (message-options-get 'message-recipients)
430 "[ \f\t\n\r\v,]+"))))
432 (mapcar (lambda (name)
433 (car (epg-list-keys context name)))
435 (message-options-get 'message-recipients)
436 "[ \f\t\n\r\v,]+"))))
439 (setq signers (epa-select-keys context "Select keys for signing.
440 If no one is selected, default secret key is used. "
442 (setq signers (mapcar (lambda (name)
443 (car (epg-list-keys context name t)))
445 (list (message-options-get 'mml-sender))))))
446 (epg-context-set-signers context signers))
447 (epg-context-set-armor context t)
448 (epg-context-set-textmode context t)
449 (if mml1991-cache-passphrase
450 (epg-context-set-passphrase-callback
452 #'mml1991-epg-passphrase-callback))
453 (condition-case error
455 (epg-encrypt-string context (buffer-string) recipients sign)
456 mml1991-epg-secret-key-id-list nil)
458 (while mml1991-epg-secret-key-id-list
459 (password-cache-remove (car mml1991-epg-secret-key-id-list))
460 (setq mml1991-epg-secret-key-id-list
461 (cdr mml1991-epg-secret-key-id-list)))
462 (signal (car error) (cdr error))))
463 (delete-region (point-min) (point-max))
464 (insert "\n" cipher))
468 (defun mml1991-encrypt (cont &optional sign)
469 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
471 (funcall func cont sign)
472 (error "Cannot find encrypt function"))))
475 (defun mml1991-sign (cont)
476 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
479 (error "Cannot find sign function"))))
484 ;; coding: iso-8859-1
487 ;;; arch-tag: e542be18-ab28-4393-9b33-97fe9cf30706
488 ;;; mml1991.el ends here