1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
3 ;; Copyright (C) 1998-2013 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")
55 (defvar mml1991-use mml2015-use
56 "The package used for PGP.")
58 (defvar mml1991-function-alist
59 '((mailcrypt mml1991-mailcrypt-sign
60 mml1991-mailcrypt-encrypt
)
65 "Alist of PGP functions.")
67 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
68 "If t, cache passphrase.")
70 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
71 "How many seconds the passphrase is cached.
72 Whether the passphrase is cached at all is controlled by
73 `mml1991-cache-passphrase'.")
75 (defvar mml1991-signers nil
76 "A list of your own key ID which will be used to sign a message.")
78 (defvar mml1991-encrypt-to-self nil
79 "If t, add your own key ID to recipient list when encryption.")
83 (autoload 'mc-sign-generic
"mc-toplev")
85 (defvar mml1991-decrypt-function
'mailcrypt-decrypt
)
86 (defvar mml1991-verify-function
'mailcrypt-verify
)
88 (defun mml1991-mailcrypt-sign (cont)
89 (let ((text (current-buffer))
91 (result-buffer (get-buffer-create "*GPG Result*")))
92 ;; Save MIME Content[^ ]+: headers from signing
93 (goto-char (point-min))
94 (while (looking-at "^Content[^ ]+:") (forward-line))
96 (setq headers
(buffer-string))
97 (delete-region (point-min) (point)))
98 (goto-char (point-max))
101 (quoted-printable-decode-region (point-min) (point-max))
103 (setq signature
(current-buffer))
104 (insert-buffer-substring text
)
105 (unless (mc-sign-generic (message-options-get 'message-sender
)
107 (unless (> (point-max) (point-min))
108 (pop-to-buffer result-buffer
)
109 (error "Sign error")))
110 (goto-char (point-min))
111 (while (re-search-forward "\r+$" nil t
)
112 (replace-match "" t t
))
113 (quoted-printable-encode-region (point-min) (point-max))
115 (delete-region (point-min) (point-max))
116 (if headers
(insert headers
))
118 (insert-buffer-substring signature
)
119 (goto-char (point-max)))))
121 (declare-function mc-encrypt-generic
"ext:mc-toplev"
122 (&optional recipients scheme start end from sign
))
124 (defun mml1991-mailcrypt-encrypt (cont &optional sign
)
125 (let ((text (current-buffer))
127 (or mc-pgp-always-sign
129 (eq t
(or (message-options-get 'message-sign-encrypt
)
131 'message-sign-encrypt
132 (or (y-or-n-p "Sign the message? ")
136 (result-buffer (get-buffer-create "*GPG Result*")))
137 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
138 (goto-char (point-min))
139 (while (looking-at "^Content[^ ]+:") (forward-line))
141 (delete-region (point-min) (point)))
143 (inline (mm-disable-multibyte))
144 (setq cipher
(current-buffer))
145 (insert-buffer-substring text
)
146 (unless (mc-encrypt-generic
148 (message-options-get 'message-recipients
)
149 (message-options-set 'message-recipients
150 (read-string "Recipients: ")))
152 (point-min) (point-max)
153 (message-options-get 'message-sender
)
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
))
162 (delete-region (point-min) (point-max))
163 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
164 ;;(insert "Version: 1\n\n")
166 (insert-buffer-substring cipher
)
167 (goto-char (point-max)))))
171 (autoload 'pgg-sign-region
"pgg")
172 (autoload 'pgg-encrypt-region
"pgg")
174 (defvar pgg-default-user-id
)
175 (defvar pgg-errors-buffer
)
176 (defvar pgg-output-buffer
)
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
))
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)))
188 (narrow-to-region (point-min) (point))
189 (setq cte
(mail-fetch-field "content-transfer-encoding")))
191 (delete-region (point-min) (point))
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
))
205 (mm-encode-content-transfer-encoding cte
))
206 (goto-char (point-min))
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.
220 (delete-region (point-min) (point))
222 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
223 (unless (let ((pgg-text-mode t
))
225 (point-min) (point-max)
228 (message-options-get 'message-recipients
)
229 (message-options-set 'message-recipients
230 (read-string "Recipients: ")))
233 (pop-to-buffer pgg-errors-buffer
)
234 (error "Encrypt error"))
235 (delete-region (point-min) (point-max))
237 (insert-buffer-substring pgg-output-buffer
)
242 (defvar epg-user-id-alist
)
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-key-sub-key-list
"epg")
253 (autoload 'epg-sub-key-capability
"epg")
254 (autoload 'epg-sub-key-validity
"epg")
255 (autoload 'epg-sub-key-fingerprint
"epg")
256 (autoload 'epg-sign-string
"epg")
257 (autoload 'epg-encrypt-string
"epg")
258 (autoload 'epg-configuration
"epg-config")
259 (autoload 'epg-expand-group
"epg-config")
261 (defvar mml1991-epg-secret-key-id-list nil
)
263 (defun mml1991-epg-passphrase-callback (context key-id ignore
)
265 (epg-passphrase-callback-function context key-id nil
)
266 (let* ((entry (assoc key-id epg-user-id-alist
))
269 (format "GnuPG passphrase for %s: "
277 (let ((password-cache-expiry mml1991-passphrase-cache-expiry
))
278 (password-cache-add key-id passphrase
))
279 (setq mml1991-epg-secret-key-id-list
280 (cons key-id mml1991-epg-secret-key-id-list
))
281 (copy-sequence passphrase
)))))
283 (defun mml1991-epg-find-usable-key (keys usage
)
286 (let ((pointer (epg-key-sub-key-list (car keys
))))
287 ;; The primary key will be marked as disabled, when the entire
288 ;; key is disabled (see 12 Field, Format of colon listings, in
289 ;; gnupg/doc/DETAILS)
290 (unless (memq 'disabled
(epg-sub-key-capability (car pointer
)))
292 (if (and (memq usage
(epg-sub-key-capability (car pointer
)))
293 (not (memq (epg-sub-key-validity (car pointer
))
294 '(revoked expired
))))
295 (throw 'found
(car keys
)))
296 (setq pointer
(cdr pointer
)))))
297 (setq keys
(cdr keys
)))))
299 ;; XXX: since gpg --list-secret-keys does not return validity of each
300 ;; key, `mml1991-epg-find-usable-key' defined above is not enough for
301 ;; secret keys. The function `mml1991-epg-find-usable-secret-key'
302 ;; below looks at appropriate public keys to check usability.
303 (defun mml1991-epg-find-usable-secret-key (context name usage
)
304 (let ((secret-keys (epg-list-keys context name t
))
306 (while (and (not secret-key
) secret-keys
)
307 (if (mml1991-epg-find-usable-key
308 (epg-list-keys context
(epg-sub-key-fingerprint
309 (car (epg-key-sub-key-list
310 (car secret-keys
)))))
312 (setq secret-key
(car secret-keys
)
314 (setq secret-keys
(cdr secret-keys
))))
317 (defun mml1991-epg-sign (cont)
318 (let ((context (epg-make-context))
319 headers cte signer-key signers signature
)
320 (if (eq mm-sign-option
'guided
)
321 (setq signers
(epa-select-keys context
"Select keys for signing.
322 If no one is selected, default secret key is used. "
325 (setq signers
(delq nil
329 (mml1991-epg-find-usable-secret-key
331 (unless (or signer-key
334 "No secret key for %s; skip it? "
336 (error "No secret key for %s" name
))
339 (epg-context-set-armor context t
)
340 (epg-context-set-textmode context t
)
341 (epg-context-set-signers context signers
)
342 (if mml1991-cache-passphrase
343 (epg-context-set-passphrase-callback
345 #'mml1991-epg-passphrase-callback
))
346 ;; Don't sign headers.
347 (goto-char (point-min))
348 (when (re-search-forward "^$" nil t
)
349 (setq headers
(buffer-substring (point-min) (point)))
351 (narrow-to-region (point-min) (point))
352 (setq cte
(mail-fetch-field "content-transfer-encoding")))
354 (delete-region (point-min) (point))
356 (setq cte
(intern (downcase cte
)))
357 (mm-decode-content-transfer-encoding cte
)))
358 (condition-case error
359 (setq signature
(epg-sign-string context
(buffer-string) 'clear
)
360 mml1991-epg-secret-key-id-list nil
)
362 (while mml1991-epg-secret-key-id-list
363 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
364 (setq mml1991-epg-secret-key-id-list
365 (cdr mml1991-epg-secret-key-id-list
)))
366 (signal (car error
) (cdr error
))))
367 (delete-region (point-min) (point-max))
368 (mm-with-unibyte-current-buffer
370 (goto-char (point-min))
371 (while (re-search-forward "\r+$" nil t
)
372 (replace-match "" t t
))
374 (mm-encode-content-transfer-encoding cte
))
375 (goto-char (point-min))
381 (defun mml1991-epg-encrypt (cont &optional sign
)
382 (goto-char (point-min))
383 (when (re-search-forward "^$" nil t
)
384 (let ((cte (save-restriction
385 (narrow-to-region (point-min) (point))
386 (mail-fetch-field "content-transfer-encoding"))))
387 ;; Strip MIME headers since it will be ASCII armored.
389 (delete-region (point-min) (point))
391 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
392 (let ((context (epg-make-context))
394 (if (message-options-get 'message-recipients
)
396 (message-options-get 'message-recipients
)
398 recipient-key signer-key cipher signers config
)
399 (when mml1991-encrypt-to-self
400 (unless mml1991-signers
401 (error "mml1991-signers is not set"))
402 (setq recipients
(nconc recipients mml1991-signers
)))
403 ;; We should remove this check if epg-0.0.6 is released.
404 (if (and (condition-case nil
405 (require 'epg-config
)
407 (functionp #'epg-expand-group
))
408 (setq config
(epg-configuration)
411 (mapcar (lambda (recipient)
412 (or (epg-expand-group config recipient
)
415 (if (eq mm-encrypt-option
'guided
)
417 (epa-select-keys context
"Select recipients for encryption.
418 If no one is selected, symmetric encryption will be performed. "
423 (setq recipient-key
(mml1991-epg-find-usable-key
424 (epg-list-keys context name
)
426 (unless (or recipient-key
428 (format "No public key for %s; skip it? "
430 (error "No public key for %s" name
))
434 (error "No recipient specified")))
436 (if (eq mm-sign-option
'guided
)
437 (setq signers
(epa-select-keys context
"Select keys for signing.
438 If no one is selected, default secret key is used. "
441 (setq signers
(delq nil
444 (mml1991-epg-find-usable-secret-key
447 (epg-context-set-signers context signers
))
448 (epg-context-set-armor context t
)
449 (epg-context-set-textmode context t
)
450 (if mml1991-cache-passphrase
451 (epg-context-set-passphrase-callback
453 #'mml1991-epg-passphrase-callback
))
454 (condition-case error
456 (epg-encrypt-string context
(buffer-string) recipients sign
)
457 mml1991-epg-secret-key-id-list nil
)
459 (while mml1991-epg-secret-key-id-list
460 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
461 (setq mml1991-epg-secret-key-id-list
462 (cdr mml1991-epg-secret-key-id-list
)))
463 (signal (car error
) (cdr error
))))
464 (delete-region (point-min) (point-max))
465 (insert "\n" cipher
))
469 (defun mml1991-encrypt (cont &optional sign
)
470 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist
))))
472 (funcall func cont sign
)
473 (error "Cannot find encrypt function"))))
476 (defun mml1991-sign (cont)
477 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist
))))
480 (error "Cannot find sign function"))))
488 ;;; mml1991.el ends here