1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
3 ;; Copyright (C) 1998-2011 Free Software Foundation, Inc.
5 ;; Author: Sascha Lüdecke <sascha@meta-x.de>,
6 ;; Simon Josefsson <simon@josefsson.org> (Mailcrypt interface, Gnus glue)
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; For Emacs <22.2 and XEmacs.
30 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))
32 (if (locate-library "password-cache")
33 (require 'password-cache)
43 (defvar mc-pgp-always-sign)
45 (autoload 'quoted-printable-decode-region "qp")
46 (autoload 'quoted-printable-encode-region "qp")
48 (autoload 'mm-decode-content-transfer-encoding "mm-bodies")
49 (autoload 'mm-encode-content-transfer-encoding "mm-bodies")
50 (autoload 'message-options-get "message")
51 (autoload 'message-options-set "message")
53 (defvar mml1991-use mml2015-use
54 "The package used for PGP.")
56 (defvar mml1991-function-alist
57 '((mailcrypt mml1991-mailcrypt-sign
58 mml1991-mailcrypt-encrypt)
63 "Alist of PGP functions.")
65 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
66 "If t, cache passphrase.")
68 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
69 "How many seconds the passphrase is cached.
70 Whether the passphrase is cached at all is controlled by
71 `mml1991-cache-passphrase'.")
73 (defvar mml1991-signers nil
74 "A list of your own key ID which will be used to sign a message.")
76 (defvar mml1991-encrypt-to-self nil
77 "If t, add your own key ID to recipient list when encryption.")
81 (autoload 'mc-sign-generic "mc-toplev")
83 (defvar mml1991-decrypt-function 'mailcrypt-decrypt)
84 (defvar mml1991-verify-function 'mailcrypt-verify)
86 (defun mml1991-mailcrypt-sign (cont)
87 (let ((text (current-buffer))
89 (result-buffer (get-buffer-create "*GPG Result*")))
90 ;; Save MIME Content[^ ]+: headers from signing
91 (goto-char (point-min))
92 (while (looking-at "^Content[^ ]+:") (forward-line))
94 (setq headers (buffer-string))
95 (delete-region (point-min) (point)))
96 (goto-char (point-max))
99 (quoted-printable-decode-region (point-min) (point-max))
101 (setq signature (current-buffer))
102 (insert-buffer-substring text)
103 (unless (mc-sign-generic (message-options-get 'message-sender)
105 (unless (> (point-max) (point-min))
106 (pop-to-buffer result-buffer)
107 (error "Sign error")))
108 (goto-char (point-min))
109 (while (re-search-forward "\r+$" nil t)
110 (replace-match "" t t))
111 (quoted-printable-encode-region (point-min) (point-max))
113 (delete-region (point-min) (point-max))
114 (if headers (insert headers))
116 (insert-buffer-substring signature)
117 (goto-char (point-max)))))
119 (declare-function mc-encrypt-generic "ext:mc-toplev"
120 (&optional recipients scheme start end from sign))
122 (defun mml1991-mailcrypt-encrypt (cont &optional sign)
123 (let ((text (current-buffer))
125 (or mc-pgp-always-sign
127 (eq t (or (message-options-get 'message-sign-encrypt)
129 'message-sign-encrypt
130 (or (y-or-n-p "Sign the message? ")
134 (result-buffer (get-buffer-create "*GPG Result*")))
135 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
136 (goto-char (point-min))
137 (while (looking-at "^Content[^ ]+:") (forward-line))
139 (delete-region (point-min) (point)))
140 (mm-with-unibyte-current-buffer
142 (inline (mm-disable-multibyte))
143 (setq cipher (current-buffer))
144 (insert-buffer-substring text)
145 (unless (mc-encrypt-generic
147 (message-options-get 'message-recipients)
148 (message-options-set 'message-recipients
149 (read-string "Recipients: ")))
151 (point-min) (point-max)
152 (message-options-get 'message-sender)
154 (unless (> (point-max) (point-min))
155 (pop-to-buffer result-buffer)
156 (error "Encrypt error")))
157 (goto-char (point-min))
158 (while (re-search-forward "\r+$" nil t)
159 (replace-match "" t t))
161 (delete-region (point-min) (point-max))
162 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
163 ;;(insert "Version: 1\n\n")
165 (insert-buffer-substring cipher)
166 (goto-char (point-max))))))
170 (autoload 'pgg-sign-region "pgg")
171 (autoload 'pgg-encrypt-region "pgg")
173 (defvar pgg-default-user-id)
174 (defvar pgg-errors-buffer)
175 (defvar pgg-output-buffer)
177 (defun mml1991-pgg-sign (cont)
178 (let ((pgg-text-mode t)
179 (pgg-default-user-id (or (message-options-get 'mml-sender)
180 pgg-default-user-id))
182 ;; Don't sign headers.
183 (goto-char (point-min))
184 (when (re-search-forward "^$" nil t)
185 (setq headers (buffer-substring (point-min) (point)))
187 (narrow-to-region (point-min) (point))
188 (setq cte (mail-fetch-field "content-transfer-encoding")))
190 (delete-region (point-min) (point))
192 (setq cte (intern (downcase cte)))
193 (mm-decode-content-transfer-encoding cte)))
194 (unless (pgg-sign-region (point-min) (point-max) t)
195 (pop-to-buffer pgg-errors-buffer)
196 (error "Encrypt error"))
197 (delete-region (point-min) (point-max))
198 (mm-with-unibyte-current-buffer
199 (insert-buffer-substring pgg-output-buffer)
200 (goto-char (point-min))
201 (while (re-search-forward "\r+$" nil t)
202 (replace-match "" t t))
204 (mm-encode-content-transfer-encoding cte))
205 (goto-char (point-min))
211 (defun mml1991-pgg-encrypt (cont &optional sign)
212 (goto-char (point-min))
213 (when (re-search-forward "^$" nil t)
214 (let ((cte (save-restriction
215 (narrow-to-region (point-min) (point))
216 (mail-fetch-field "content-transfer-encoding"))))
217 ;; Strip MIME headers since it will be ASCII armored.
219 (delete-region (point-min) (point))
221 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
222 (unless (let ((pgg-text-mode t))
224 (point-min) (point-max)
227 (message-options-get 'message-recipients)
228 (message-options-set 'message-recipients
229 (read-string "Recipients: ")))
232 (pop-to-buffer pgg-errors-buffer)
233 (error "Encrypt error"))
234 (delete-region (point-min) (point-max))
236 (insert-buffer-substring pgg-output-buffer)
241 (defvar epg-user-id-alist)
243 (autoload 'epg-make-context "epg")
244 (autoload 'epg-passphrase-callback-function "epg")
245 (autoload 'epa-select-keys "epa")
246 (autoload 'epg-list-keys "epg")
247 (autoload 'epg-context-set-armor "epg")
248 (autoload 'epg-context-set-textmode "epg")
249 (autoload 'epg-context-set-signers "epg")
250 (autoload 'epg-context-set-passphrase-callback "epg")
251 (autoload 'epg-sign-string "epg")
252 (autoload 'epg-encrypt-string "epg")
253 (autoload 'epg-configuration "epg-config")
254 (autoload 'epg-expand-group "epg-config")
256 (defvar mml1991-epg-secret-key-id-list nil)
258 (defun mml1991-epg-passphrase-callback (context key-id ignore)
260 (epg-passphrase-callback-function context key-id nil)
261 (let* ((entry (assoc key-id epg-user-id-alist))
264 (format "GnuPG passphrase for %s: "
272 (let ((password-cache-expiry mml1991-passphrase-cache-expiry))
273 (password-cache-add key-id passphrase))
274 (setq mml1991-epg-secret-key-id-list
275 (cons key-id mml1991-epg-secret-key-id-list))
276 (copy-sequence passphrase)))))
278 (defun mml1991-epg-sign (cont)
279 (let ((context (epg-make-context))
280 headers cte signers signature)
281 (if (eq mm-sign-option 'guided)
282 (setq signers (epa-select-keys context "Select keys for signing.
283 If no one is selected, default secret key is used. "
286 (setq signers (mapcar (lambda (name)
287 (car (epg-list-keys context name t)))
289 (epg-context-set-armor context t)
290 (epg-context-set-textmode context t)
291 (epg-context-set-signers context signers)
292 (if mml1991-cache-passphrase
293 (epg-context-set-passphrase-callback
295 #'mml1991-epg-passphrase-callback))
296 ;; Don't sign headers.
297 (goto-char (point-min))
298 (when (re-search-forward "^$" nil t)
299 (setq headers (buffer-substring (point-min) (point)))
301 (narrow-to-region (point-min) (point))
302 (setq cte (mail-fetch-field "content-transfer-encoding")))
304 (delete-region (point-min) (point))
306 (setq cte (intern (downcase cte)))
307 (mm-decode-content-transfer-encoding cte)))
308 (condition-case error
309 (setq signature (epg-sign-string context (buffer-string) 'clear)
310 mml1991-epg-secret-key-id-list nil)
312 (while mml1991-epg-secret-key-id-list
313 (password-cache-remove (car mml1991-epg-secret-key-id-list))
314 (setq mml1991-epg-secret-key-id-list
315 (cdr mml1991-epg-secret-key-id-list)))
316 (signal (car error) (cdr error))))
317 (delete-region (point-min) (point-max))
318 (mm-with-unibyte-current-buffer
320 (goto-char (point-min))
321 (while (re-search-forward "\r+$" nil t)
322 (replace-match "" t t))
324 (mm-encode-content-transfer-encoding cte))
325 (goto-char (point-min))
331 (defun mml1991-epg-encrypt (cont &optional sign)
332 (goto-char (point-min))
333 (when (re-search-forward "^$" nil t)
334 (let ((cte (save-restriction
335 (narrow-to-region (point-min) (point))
336 (mail-fetch-field "content-transfer-encoding"))))
337 ;; Strip MIME headers since it will be ASCII armored.
339 (delete-region (point-min) (point))
341 (mm-decode-content-transfer-encoding (intern (downcase cte))))))
342 (let ((context (epg-make-context))
344 (if (message-options-get 'message-recipients)
346 (message-options-get 'message-recipients)
348 cipher signers config)
349 ;; We should remove this check if epg-0.0.6 is released.
350 (if (and (condition-case nil
351 (require 'epg-config)
353 (functionp #'epg-expand-group))
354 (setq config (epg-configuration)
357 (mapcar (lambda (recipient)
358 (or (epg-expand-group config recipient)
361 (if (eq mm-encrypt-option 'guided)
363 (epa-select-keys context "Select recipients for encryption.
364 If no one is selected, symmetric encryption will be performed. "
367 (delq nil (mapcar (lambda (name)
368 (car (epg-list-keys context name)))
370 (if mml1991-encrypt-to-self
374 (mapcar (lambda (name)
375 (car (epg-list-keys context name)))
377 (error "mml1991-signers not set")))
379 (if (eq mm-sign-option 'guided)
380 (setq signers (epa-select-keys context "Select keys for signing.
381 If no one is selected, default secret key is used. "
384 (setq signers (mapcar (lambda (name)
385 (car (epg-list-keys context name t)))
387 (epg-context-set-signers context signers))
388 (epg-context-set-armor context t)
389 (epg-context-set-textmode context t)
390 (if mml1991-cache-passphrase
391 (epg-context-set-passphrase-callback
393 #'mml1991-epg-passphrase-callback))
394 (condition-case error
396 (epg-encrypt-string context (buffer-string) recipients sign)
397 mml1991-epg-secret-key-id-list nil)
399 (while mml1991-epg-secret-key-id-list
400 (password-cache-remove (car mml1991-epg-secret-key-id-list))
401 (setq mml1991-epg-secret-key-id-list
402 (cdr mml1991-epg-secret-key-id-list)))
403 (signal (car error) (cdr error))))
404 (delete-region (point-min) (point-max))
405 (insert "\n" cipher))
409 (defun mml1991-encrypt (cont &optional sign)
410 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist))))
412 (funcall func cont sign)
413 (error "Cannot find encrypt function"))))
416 (defun mml1991-sign (cont)
417 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist))))
420 (error "Cannot find sign function"))))
425 ;; coding: iso-8859-1
428 ;;; mml1991.el ends here