1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
3 ;; Copyright (C) 1998-2015 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 (if (locate-library "password-cache")
30 (require 'password-cache
)
40 (defvar mc-pgp-always-sign
)
42 (autoload 'quoted-printable-decode-region
"qp")
43 (autoload 'quoted-printable-encode-region
"qp")
45 (autoload 'mm-decode-content-transfer-encoding
"mm-bodies")
46 (autoload 'mm-encode-content-transfer-encoding
"mm-bodies")
47 (autoload 'message-options-get
"message")
48 (autoload 'message-options-set
"message")
52 (defvar mml1991-use mml2015-use
53 "The package used for PGP.")
55 (defvar mml1991-function-alist
56 '((mailcrypt mml1991-mailcrypt-sign
57 mml1991-mailcrypt-encrypt
)
62 "Alist of PGP functions.")
64 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
65 "If t, cache passphrase.")
67 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
68 "How many seconds the passphrase is cached.
69 Whether the passphrase is cached at all is controlled by
70 `mml1991-cache-passphrase'.")
72 (defvar mml1991-signers nil
73 "A list of your own key ID which will be used to sign a message.")
75 (defvar mml1991-encrypt-to-self nil
76 "If t, add your own key ID to recipient list when encryption.")
80 (autoload 'mc-sign-generic
"mc-toplev")
82 (defvar mml1991-decrypt-function
'mailcrypt-decrypt
)
83 (defvar mml1991-verify-function
'mailcrypt-verify
)
85 (defun mml1991-mailcrypt-sign (cont)
86 (let ((text (current-buffer))
88 (result-buffer (get-buffer-create "*GPG Result*")))
89 ;; Save MIME Content[^ ]+: headers from signing
90 (goto-char (point-min))
91 (while (looking-at "^Content[^ ]+:") (forward-line))
93 (setq headers
(buffer-string))
94 (delete-region (point-min) (point)))
95 (goto-char (point-max))
98 (quoted-printable-decode-region (point-min) (point-max))
100 (setq signature
(current-buffer))
101 (insert-buffer-substring text
)
102 (unless (mc-sign-generic (message-options-get 'message-sender
)
104 (unless (> (point-max) (point-min))
105 (pop-to-buffer result-buffer
)
106 (error "Sign error")))
107 (goto-char (point-min))
108 (while (re-search-forward "\r+$" nil t
)
109 (replace-match "" t t
))
110 (quoted-printable-encode-region (point-min) (point-max))
112 (delete-region (point-min) (point-max))
113 (if headers
(insert headers
))
115 (insert-buffer-substring signature
)
116 (goto-char (point-max)))))
118 (declare-function mc-encrypt-generic
"ext:mc-toplev"
119 (&optional recipients scheme start end from sign
))
121 (defun mml1991-mailcrypt-encrypt (cont &optional sign
)
122 (let ((text (current-buffer))
124 (or mc-pgp-always-sign
126 (eq t
(or (message-options-get 'message-sign-encrypt
)
128 'message-sign-encrypt
129 (or (y-or-n-p "Sign the message? ")
133 (result-buffer (get-buffer-create "*GPG Result*")))
134 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
135 (goto-char (point-min))
136 (while (looking-at "^Content[^ ]+:") (forward-line))
138 (delete-region (point-min) (point)))
140 (inline (mm-disable-multibyte))
141 (setq cipher
(current-buffer))
142 (insert-buffer-substring text
)
143 (unless (mc-encrypt-generic
145 (message-options-get 'message-recipients
)
146 (message-options-set 'message-recipients
147 (read-string "Recipients: ")))
149 (point-min) (point-max)
150 (message-options-get 'message-sender
)
152 (unless (> (point-max) (point-min))
153 (pop-to-buffer result-buffer
)
154 (error "Encrypt error")))
155 (goto-char (point-min))
156 (while (re-search-forward "\r+$" nil t
)
157 (replace-match "" t t
))
159 (delete-region (point-min) (point-max))
160 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
161 ;;(insert "Version: 1\n\n")
163 (insert-buffer-substring cipher
)
164 (goto-char (point-max)))))
168 (autoload 'pgg-sign-region
"pgg")
169 (autoload 'pgg-encrypt-region
"pgg")
171 (defvar pgg-default-user-id
)
172 (defvar pgg-errors-buffer
)
173 (defvar pgg-output-buffer
)
175 (defun mml1991-pgg-sign (cont)
176 (let ((pgg-text-mode t
)
177 (pgg-default-user-id (or (message-options-get 'mml-sender
)
178 pgg-default-user-id
))
180 ;; Don't sign headers.
181 (goto-char (point-min))
182 (when (re-search-forward "^$" nil t
)
183 (setq headers
(buffer-substring (point-min) (point)))
185 (narrow-to-region (point-min) (point))
186 (setq cte
(mail-fetch-field "content-transfer-encoding")))
188 (delete-region (point-min) (point))
190 (setq cte
(intern (downcase cte
)))
191 (mm-decode-content-transfer-encoding cte
)))
192 (unless (pgg-sign-region (point-min) (point-max) t
)
193 (pop-to-buffer pgg-errors-buffer
)
194 (error "Encrypt error"))
195 (delete-region (point-min) (point-max))
196 (mm-with-unibyte-current-buffer
197 (insert-buffer-substring pgg-output-buffer
)
198 (goto-char (point-min))
199 (while (re-search-forward "\r+$" nil t
)
200 (replace-match "" t t
))
202 (mm-encode-content-transfer-encoding cte
))
203 (goto-char (point-min))
209 (defun mml1991-pgg-encrypt (cont &optional sign
)
210 (goto-char (point-min))
211 (when (re-search-forward "^$" nil t
)
212 (let ((cte (save-restriction
213 (narrow-to-region (point-min) (point))
214 (mail-fetch-field "content-transfer-encoding"))))
215 ;; Strip MIME headers since it will be ASCII armored.
217 (delete-region (point-min) (point))
219 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
220 (unless (let ((pgg-text-mode t
))
222 (point-min) (point-max)
225 (message-options-get 'message-recipients
)
226 (message-options-set 'message-recipients
227 (read-string "Recipients: ")))
230 (pop-to-buffer pgg-errors-buffer
)
231 (error "Encrypt error"))
232 (delete-region (point-min) (point-max))
234 (insert-buffer-substring pgg-output-buffer
)
239 (defvar epg-user-id-alist
)
241 (autoload 'epg-make-context
"epg")
242 (autoload 'epg-passphrase-callback-function
"epg")
243 (autoload 'epa-select-keys
"epa")
244 (autoload 'epg-list-keys
"epg")
245 (autoload 'epg-context-set-armor
"epg")
246 (autoload 'epg-context-set-textmode
"epg")
247 (autoload 'epg-context-set-signers
"epg")
248 (autoload 'epg-context-set-passphrase-callback
"epg")
249 (autoload 'epg-key-sub-key-list
"epg")
250 (autoload 'epg-sub-key-capability
"epg")
251 (autoload 'epg-sub-key-validity
"epg")
252 (autoload 'epg-sub-key-fingerprint
"epg")
253 (autoload 'epg-sign-string
"epg")
254 (autoload 'epg-encrypt-string
"epg")
255 (autoload 'epg-configuration
"epg-config")
256 (autoload 'epg-expand-group
"epg-config")
258 (defvar mml1991-epg-secret-key-id-list nil
)
260 (defun mml1991-epg-passphrase-callback (context key-id ignore
)
262 (epg-passphrase-callback-function context key-id nil
)
263 (let* ((entry (assoc key-id epg-user-id-alist
))
266 (format "GnuPG passphrase for %s: "
274 (let ((password-cache-expiry mml1991-passphrase-cache-expiry
))
275 (password-cache-add key-id passphrase
))
276 (setq mml1991-epg-secret-key-id-list
277 (cons key-id mml1991-epg-secret-key-id-list
))
278 (copy-sequence passphrase
)))))
280 (defun mml1991-epg-find-usable-key (keys usage
)
283 (let ((pointer (epg-key-sub-key-list (car keys
))))
284 ;; The primary key will be marked as disabled, when the entire
285 ;; key is disabled (see 12 Field, Format of colon listings, in
286 ;; gnupg/doc/DETAILS)
287 (unless (memq 'disabled
(epg-sub-key-capability (car pointer
)))
289 (if (and (memq usage
(epg-sub-key-capability (car pointer
)))
290 (not (memq (epg-sub-key-validity (car pointer
))
291 '(revoked expired
))))
292 (throw 'found
(car keys
)))
293 (setq pointer
(cdr pointer
)))))
294 (setq keys
(cdr keys
)))))
296 ;; XXX: since gpg --list-secret-keys does not return validity of each
297 ;; key, `mml1991-epg-find-usable-key' defined above is not enough for
298 ;; secret keys. The function `mml1991-epg-find-usable-secret-key'
299 ;; below looks at appropriate public keys to check usability.
300 (defun mml1991-epg-find-usable-secret-key (context name usage
)
301 (let ((secret-keys (epg-list-keys context name t
))
303 (while (and (not secret-key
) secret-keys
)
304 (if (mml1991-epg-find-usable-key
305 (epg-list-keys context
(epg-sub-key-fingerprint
306 (car (epg-key-sub-key-list
307 (car secret-keys
)))))
309 (setq secret-key
(car secret-keys
)
311 (setq secret-keys
(cdr secret-keys
))))
314 (defun mml1991-epg-sign (cont)
315 (let ((context (epg-make-context))
316 headers cte signer-key signers signature
)
317 (if (eq mm-sign-option
'guided
)
318 (setq signers
(epa-select-keys context
"Select keys for signing.
319 If no one is selected, default secret key is used. "
322 (setq signers
(delq nil
326 (mml1991-epg-find-usable-secret-key
328 (unless (or signer-key
331 "No secret key for %s; skip it? "
333 (error "No secret key for %s" name
))
336 (epg-context-set-armor context t
)
337 (epg-context-set-textmode context t
)
338 (epg-context-set-signers context signers
)
339 (if mml1991-cache-passphrase
340 (epg-context-set-passphrase-callback
342 #'mml1991-epg-passphrase-callback
))
343 ;; Don't sign headers.
344 (goto-char (point-min))
345 (when (re-search-forward "^$" nil t
)
346 (setq headers
(buffer-substring (point-min) (point)))
348 (narrow-to-region (point-min) (point))
349 (setq cte
(mail-fetch-field "content-transfer-encoding")))
351 (delete-region (point-min) (point))
353 (setq cte
(intern (downcase cte
)))
354 (mm-decode-content-transfer-encoding cte
)))
355 (condition-case error
356 (setq signature
(epg-sign-string context
(buffer-string) 'clear
)
357 mml1991-epg-secret-key-id-list nil
)
359 (while mml1991-epg-secret-key-id-list
360 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
361 (setq mml1991-epg-secret-key-id-list
362 (cdr mml1991-epg-secret-key-id-list
)))
363 (signal (car error
) (cdr error
))))
364 (delete-region (point-min) (point-max))
365 (mm-with-unibyte-current-buffer
367 (goto-char (point-min))
368 (while (re-search-forward "\r+$" nil t
)
369 (replace-match "" t t
))
371 (mm-encode-content-transfer-encoding cte
))
372 (goto-char (point-min))
378 (defun mml1991-epg-encrypt (cont &optional sign
)
379 (goto-char (point-min))
380 (when (re-search-forward "^$" nil t
)
381 (let ((cte (save-restriction
382 (narrow-to-region (point-min) (point))
383 (mail-fetch-field "content-transfer-encoding"))))
384 ;; Strip MIME headers since it will be ASCII armored.
386 (delete-region (point-min) (point))
388 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
389 (let ((context (epg-make-context))
391 (if (message-options-get 'message-recipients
)
393 (message-options-get 'message-recipients
)
395 recipient-key signer-key cipher signers config
)
396 (when mml1991-encrypt-to-self
397 (unless mml1991-signers
398 (error "mml1991-signers is not set"))
399 (setq recipients
(nconc recipients mml1991-signers
)))
400 ;; We should remove this check if epg-0.0.6 is released.
401 (if (and (condition-case nil
402 (require 'epg-config
)
404 (functionp #'epg-expand-group
))
405 (setq config
(epg-configuration)
408 (mapcar (lambda (recipient)
409 (or (epg-expand-group config recipient
)
412 (if (eq mm-encrypt-option
'guided
)
414 (epa-select-keys context
"Select recipients for encryption.
415 If no one is selected, symmetric encryption will be performed. "
420 (setq recipient-key
(mml1991-epg-find-usable-key
421 (epg-list-keys context name
)
423 (unless (or recipient-key
425 (format "No public key for %s; skip it? "
427 (error "No public key for %s" name
))
431 (error "No recipient specified")))
433 (if (eq mm-sign-option
'guided
)
434 (setq signers
(epa-select-keys context
"Select keys for signing.
435 If no one is selected, default secret key is used. "
438 (setq signers
(delq nil
441 (mml1991-epg-find-usable-secret-key
444 (epg-context-set-signers context signers
))
445 (epg-context-set-armor context t
)
446 (epg-context-set-textmode context t
)
447 (if mml1991-cache-passphrase
448 (epg-context-set-passphrase-callback
450 #'mml1991-epg-passphrase-callback
))
451 (condition-case error
453 (epg-encrypt-string context
(buffer-string) recipients sign
)
454 mml1991-epg-secret-key-id-list nil
)
456 (while mml1991-epg-secret-key-id-list
457 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
458 (setq mml1991-epg-secret-key-id-list
459 (cdr mml1991-epg-secret-key-id-list
)))
460 (signal (car error
) (cdr error
))))
461 (delete-region (point-min) (point-max))
462 (insert "\n" cipher
))
466 (defun mml1991-encrypt (cont &optional sign
)
467 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist
))))
469 (funcall func cont sign
)
470 (error "Cannot find encrypt function"))))
473 (defun mml1991-sign (cont)
474 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist
))))
477 (error "Cannot find sign function"))))
485 ;;; mml1991.el ends here