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, 2007 Free Software Foundation, Inc.
6 ;; Author: Sascha Ldecke <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 your own key ID which will be used to sign a message.")
70 (defvar mml1991-encrypt-to-self nil
71 "If t, add your own key ID to recipient list when encryption.")
76 (autoload 'mc-sign-generic "mc-toplev"))
78 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
79 (defvar mml1991-verify-function 'mailcrypt-verify)
81 (defun mml1991-mailcrypt-sign (cont)
82 (let ((text (current-buffer))
84 (result-buffer (get-buffer-create "*GPG Result*")))
85 ;; Save MIME Content[^ ]+: headers from signing
86 (goto-char (point-min))
87 (while (looking-at "^Content[^ ]+:") (forward-line))
89 (setq headers (buffer-string))
90 (delete-region (point-min) (point)))
91 (goto-char (point-max))
94 (quoted-printable-decode-region (point-min) (point-max))
96 (setq signature (current-buffer))
97 (insert-buffer-substring text)
98 (unless (mc-sign-generic (message-options-get 'message-sender)
100 (unless (> (point-max) (point-min))
101 (pop-to-buffer result-buffer)
102 (error "Sign error")))
103 (goto-char (point-min))
104 (while (re-search-forward "\r+$" nil t)
105 (replace-match "" t t))
106 (quoted-printable-encode-region (point-min) (point-max))
108 (delete-region (point-min) (point-max))
109 (if headers (insert headers))
111 (insert-buffer-substring signature)
112 (goto-char (point-max)))))
114 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
115 (let ((text (current-buffer))
117 (or mc-pgp-always-sign
119 (eq t (or (message-options-get 'message-sign-encrypt)
121 'message-sign-encrypt
122 (or (y-or-n-p "Sign the message? ")
126 (result-buffer (get-buffer-create "*GPG Result*")))
127 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
128 (goto-char (point-min))
129 (while (looking-at "^Content[^ ]+:") (forward-line))
131 (delete-region (point-min) (point)))
132 (mm-with-unibyte-current-buffer
134 (setq cipher (current-buffer))
135 (insert-buffer-substring text)
136 (unless (mc-encrypt-generic
138 (message-options-get 'message-recipients)
139 (message-options-set 'message-recipients
140 (read-string "Recipients: ")))
142 (point-min) (point-max)
143 (message-options-get 'message-sender)
145 (unless (> (point-max) (point-min))
146 (pop-to-buffer result-buffer)
147 (error "Encrypt error")))
148 (goto-char (point-min))
149 (while (re-search-forward "\r+$" nil t)
150 (replace-match "" t t))
152 (delete-region (point-min) (point-max))
153 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
154 ;;(insert "Version: 1\n\n")
156 (insert-buffer-substring cipher)
157 (goto-char (point-max))))))
162 (autoload 'gpg-sign-cleartext "gpg"))
164 (defun mml1991-gpg-sign (cont)
165 (let ((text (current-buffer))
167 (result-buffer (get-buffer-create "*GPG Result*")))
168 ;; Save MIME Content[^ ]+: headers from signing
169 (goto-char (point-min))
170 (while (looking-at "^Content[^ ]+:") (forward-line))
172 (setq headers (buffer-string))
173 (delete-region (point-min) (point)))
174 (goto-char (point-max))
177 (quoted-printable-decode-region (point-min) (point-max))
179 (unless (gpg-sign-cleartext text (setq signature (current-buffer))
182 (message-options-get 'message-sender))
183 (unless (> (point-max) (point-min))
184 (pop-to-buffer result-buffer)
185 (error "Sign error")))
186 (goto-char (point-min))
187 (while (re-search-forward "\r+$" nil t)
188 (replace-match "" t t))
189 (quoted-printable-encode-region (point-min) (point-max))
191 (delete-region (point-min) (point-max))
192 (if headers (insert headers))
194 (insert-buffer-substring signature)
195 (goto-char (point-max)))))
197 (defun mml1991-gpg-encrypt (cont &optional sign)
198 (let ((text (current-buffer))
200 (result-buffer (get-buffer-create "*GPG Result*")))
201 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMOURED
202 (goto-char (point-min))
203 (while (looking-at "^Content[^ ]+:") (forward-line))
205 (delete-region (point-min) (point)))
206 (mm-with-unibyte-current-buffer
208 (flet ((gpg-encrypt-func
209 (sign plaintext ciphertext result recipients &optional
210 passphrase sign-with-key armor textmode)
213 plaintext ciphertext result recipients passphrase
214 sign-with-key armor textmode)
216 plaintext ciphertext result recipients passphrase
218 (unless (gpg-encrypt-func
220 text (setq cipher (current-buffer))
224 (message-options-get 'message-recipients)
225 (message-options-set 'message-recipients
226 (read-string "Recipients: ")))
229 (message-options-get 'message-sender)
230 t t) ; armor & textmode
231 (unless (> (point-max) (point-min))
232 (pop-to-buffer result-buffer)
233 (error "Encrypt error"))))
234 (goto-char (point-min))
235 (while (re-search-forward "\r+$" nil t)
236 (replace-match "" t t))
238 (delete-region (point-min) (point-max))
239 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
240 ;;(insert "Version: 1\n\n")
242 (insert-buffer-substring cipher)
243 (goto-char (point-max))))))
248 (defvar pgg-default-user-id)
249 (defvar pgg-errors-buffer)
250 (defvar pgg-output-buffer))
252 (defun mml1991-pgg-sign (cont)
253 (let ((pgg-text-mode t)
254 (pgg-default-user-id (or (message-options-get 'mml-sender)
255 pgg-default-user-id))
257 ;; Don't sign headers.
258 (goto-char (point-min))
259 (when (re-search-forward "^$" nil t)
260 (setq headers (buffer-substring (point-min) (point)))
262 (narrow-to-region (point-min) (point))
263 (setq cte (mail-fetch-field "content-transfer-encoding")))
265 (delete-region (point-min) (point))
267 (setq cte (intern (downcase cte)))
268 (mm-decode-content-transfer-encoding cte)))
269 (unless (pgg-sign-region (point-min) (point-max) t)
270 (pop-to-buffer pgg-errors-buffer)
271 (error "Encrypt error"))
272 (delete-region (point-min) (point-max))
273 (mm-with-unibyte-current-buffer
274 (insert-buffer-substring pgg-output-buffer)
275 (goto-char (point-min))
276 (while (re-search-forward "\r+$" nil t)
277 (replace-match "" t t))
279 (mm-encode-content-transfer-encoding cte))
280 (goto-char (point-min))
286 (defun mml1991-pgg-encrypt (cont &optional sign)
287 (goto-char (point-min))
288 (when (re-search-forward "^$" nil t)
289 (let ((cte (save-restriction
290 (narrow-to-region (point-min) (point))
291 (mail-fetch-field "content-transfer-encoding"))))
292 ;; Strip MIME headers since it will be ASCII armoured.
294 (delete-region (point-min) (point))
296 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
297 (unless (let ((pgg-text-mode t))
299 (point-min) (point-max)
302 (message-options-get 'message-recipients)
303 (message-options-set 'message-recipients
304 (read-string "Recipients: ")))
307 (pop-to-buffer pgg-errors-buffer)
308 (error "Encrypt error"))
309 (delete-region (point-min) (point-max))
311 (insert-buffer-substring pgg-output-buffer)
317 (autoload 'epg-make-context "epg"))
320 (defvar epg-user-id-alist)
321 (autoload 'epg-passphrase-callback-function "epg")
322 (autoload 'epa-select-keys "epa")
323 (autoload 'epg-list-keys "epg")
324 (autoload 'epg-context-set-armor "epg")
325 (autoload 'epg-context-set-textmode "epg")
326 (autoload 'epg-context-set-signers "epg")
327 (autoload 'epg-context-set-passphrase-callback "epg")
328 (autoload 'epg-sign-string "epg")
329 (autoload 'epg-encrypt-string "epg")
330 (autoload 'epg-configuration "epg-config")
331 (autoload 'epg-expand-group "epg-config"))
334 (defvar password-cache-expiry)
335 (autoload 'password-read "password")
336 (autoload 'password-cache-add "password")
337 (autoload 'password-cache-remove "password"))
339 (defvar mml1991-epg-secret-key-id-list nil)
341 (defun mml1991-epg-passphrase-callback (context key-id ignore)
343 (epg-passphrase-callback-function context key-id nil)
344 (let* ((entry (assoc key-id epg-user-id-alist))
347 (format "GnuPG passphrase for %s: "
355 (let ((password-cache-expiry mml1991-passphrase-cache-expiry))
356 (password-cache-add key-id passphrase))
357 (setq mml1991-epg-secret-key-id-list
358 (cons key-id mml1991-epg-secret-key-id-list))
359 (copy-sequence passphrase)))))
361 (defun mml1991-epg-sign (cont)
362 (let ((context (epg-make-context))
363 headers cte signers signature)
365 (setq signers (epa-select-keys context "Select keys for signing.
366 If no one is selected, default secret key is used. "
369 (setq signers (mapcar (lambda (name)
370 (car (epg-list-keys context name t)))
372 (epg-context-set-armor context t)
373 (epg-context-set-textmode context t)
374 (epg-context-set-signers context signers)
375 (if mml1991-cache-passphrase
376 (epg-context-set-passphrase-callback
378 #'mml1991-epg-passphrase-callback))
379 ;; Don't sign headers.
380 (goto-char (point-min))
381 (when (re-search-forward "^$" nil t)
382 (setq headers (buffer-substring (point-min) (point)))
384 (narrow-to-region (point-min) (point))
385 (setq cte (mail-fetch-field "content-transfer-encoding")))
387 (delete-region (point-min) (point))
389 (setq cte (intern (downcase cte)))
390 (mm-decode-content-transfer-encoding cte)))
391 (condition-case error
392 (setq signature (epg-sign-string context (buffer-string) 'clear)
393 mml1991-epg-secret-key-id-list nil)
395 (while mml1991-epg-secret-key-id-list
396 (password-cache-remove (car mml1991-epg-secret-key-id-list))
397 (setq mml1991-epg-secret-key-id-list
398 (cdr mml1991-epg-secret-key-id-list)))
399 (signal (car error) (cdr error))))
400 (delete-region (point-min) (point-max))
401 (mm-with-unibyte-current-buffer
403 (goto-char (point-min))
404 (while (re-search-forward "\r+$" nil t)
405 (replace-match "" t t))
407 (mm-encode-content-transfer-encoding cte))
408 (goto-char (point-min))
414 (defun mml1991-epg-encrypt (cont &optional sign)
415 (goto-char (point-min))
416 (when (re-search-forward "^$" nil t)
417 (let ((cte (save-restriction
418 (narrow-to-region (point-min) (point))
419 (mail-fetch-field "content-transfer-encoding"))))
420 ;; Strip MIME headers since it will be ASCII armoured.
422 (delete-region (point-min) (point))
424 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
425 (let ((context (epg-make-context))
427 (if (message-options-get 'message-recipients)
429 (message-options-get 'message-recipients)
431 cipher signers config)
432 ;; We should remove this check if epg-0.0.6 is released.
433 (if (and (condition-case nil
434 (require 'epg-config)
436 (functionp #'epg-expand-group))
437 (setq config (epg-configuration)
440 (mapcar (lambda (recipient)
441 (or (epg-expand-group config recipient)
446 (epa-select-keys context "Select recipients for encryption.
447 If no one is selected, symmetric encryption will be performed. "
450 (delq nil (mapcar (lambda (name)
451 (car (epg-list-keys context name)))
453 (if mml1991-encrypt-to-self
457 (mapcar (lambda (name)
458 (car (epg-list-keys context name)))
460 (error "mml1991-signers not set")))
463 (setq signers (epa-select-keys context "Select keys for signing.
464 If no one is selected, default secret key is used. "
467 (setq signers (mapcar (lambda (name)
468 (car (epg-list-keys context name t)))
470 (epg-context-set-signers context signers))
471 (epg-context-set-armor context t)
472 (epg-context-set-textmode context t)
473 (if mml1991-cache-passphrase
474 (epg-context-set-passphrase-callback
476 #'mml1991-epg-passphrase-callback))
477 (condition-case error
479 (epg-encrypt-string context (buffer-string) recipients sign)
480 mml1991-epg-secret-key-id-list nil)
482 (while mml1991-epg-secret-key-id-list
483 (password-cache-remove (car mml1991-epg-secret-key-id-list))
484 (setq mml1991-epg-secret-key-id-list
485 (cdr mml1991-epg-secret-key-id-list)))
486 (signal (car error) (cdr error))))
487 (delete-region (point-min) (point-max))
488 (insert "\n" cipher))
492 (defun mml1991-encrypt (cont &optional sign)
493 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
495 (funcall func cont sign)
496 (error "Cannot find encrypt function"))))
499 (defun mml1991-sign (cont)
500 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
503 (error "Cannot find sign function"))))
508 ;; coding: iso-8859-1
511 ;;; arch-tag: e542be18-ab28-4393-9b33-97fe9cf30706
512 ;;; mml1991.el ends here