mml-sec.el (mml-secure-bcc-is-safe): Keep old Emacsen compatibility
[gnus] / lisp / mml-sec.el
1 ;;; mml-sec.el --- A package with security functions for MML documents
2
3 ;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
4
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (eval-when-compile (require 'cl))
27
28 (autoload 'gnus-subsetp "gnus-util")
29 (autoload 'mail-strip-quoted-names "mail-utils")
30 (autoload 'mml2015-sign "mml2015")
31 (autoload 'mml2015-encrypt "mml2015")
32 (autoload 'mml1991-sign "mml1991")
33 (autoload 'mml1991-encrypt "mml1991")
34 (autoload 'message-fetch-field "message")
35 (autoload 'message-goto-body "message")
36 (autoload 'mml-insert-tag "mml")
37 (autoload 'mml-smime-sign "mml-smime")
38 (autoload 'mml-smime-encrypt "mml-smime")
39 (autoload 'mml-smime-sign-query "mml-smime")
40 (autoload 'mml-smime-encrypt-query "mml-smime")
41 (autoload 'mml-smime-verify "mml-smime")
42 (autoload 'mml-smime-verify-test "mml-smime")
43
44 (defvar mml-sign-alist
45   '(("smime"     mml-smime-sign-buffer     mml-smime-sign-query)
46     ("pgp"       mml-pgp-sign-buffer       list)
47     ("pgpauto"   mml-pgpauto-sign-buffer  list)
48     ("pgpmime"   mml-pgpmime-sign-buffer   list))
49   "Alist of MIME signer functions.")
50
51 (defcustom mml-default-sign-method "pgpmime"
52   "Default sign method.
53 The string must have an entry in `mml-sign-alist'."
54   :version "22.1"
55   :type '(choice (const "smime")
56                  (const "pgp")
57                  (const "pgpauto")
58                  (const "pgpmime")
59                  string)
60   :group 'message)
61
62 (defvar mml-encrypt-alist
63   '(("smime"     mml-smime-encrypt-buffer     mml-smime-encrypt-query)
64     ("pgp"       mml-pgp-encrypt-buffer       list)
65     ("pgpauto"   mml-pgpauto-sign-buffer  list)
66     ("pgpmime"   mml-pgpmime-encrypt-buffer   list))
67   "Alist of MIME encryption functions.")
68
69 (defcustom mml-default-encrypt-method "pgpmime"
70   "Default encryption method.
71 The string must have an entry in `mml-encrypt-alist'."
72   :version "22.1"
73   :type '(choice (const "smime")
74                  (const "pgp")
75                  (const "pgpauto")
76                  (const "pgpmime")
77                  string)
78   :group 'message)
79
80 (defcustom mml-signencrypt-style-alist
81   '(("smime"   separate)
82     ("pgp"     combined)
83     ("pgpauto" combined)
84     ("pgpmime" combined))
85   "Alist specifying if `signencrypt' results in two separate operations or not.
86 The first entry indicates the MML security type, valid entries include
87 the strings \"smime\", \"pgp\", and \"pgpmime\".  The second entry is
88 a symbol `separate' or `combined' where `separate' means that MML signs
89 and encrypt messages in a two step process, and `combined' means that MML
90 signs and encrypt the message in one step.
91
92 Note that the output generated by using a `combined' mode is NOT
93 understood by all PGP implementations, in particular PGP version
94 2 does not support it!  See Info node `(message)Security' for
95 details."
96   :version "22.1"
97   :group 'message
98   :type '(repeat (list (choice (const :tag "S/MIME" "smime")
99                                (const :tag "PGP" "pgp")
100                                (const :tag "PGP/MIME" "pgpmime")
101                                (string :tag "User defined"))
102                        (choice (const :tag "Separate" separate)
103                                (const :tag "Combined" combined)))))
104
105 (defcustom mml-secure-verbose nil
106   "If non-nil, ask the user about the current operation more verbosely."
107   :group 'message
108   :type 'boolean)
109
110 (defcustom mml-secure-cache-passphrase
111   (if (boundp 'password-cache)
112       password-cache
113     t)
114   "If t, cache passphrase."
115   :group 'message
116   :type 'boolean)
117
118 (defcustom mml-secure-passphrase-cache-expiry
119   (if (boundp 'password-cache-expiry)
120       password-cache-expiry
121     16)
122   "How many seconds the passphrase is cached.
123 Whether the passphrase is cached at all is controlled by
124 `mml-secure-cache-passphrase'."
125   :group 'message
126   :type 'integer)
127
128 (defcustom mml-secure-safe-bcc-list nil
129   "List of e-mail addresses that are safe to use in Bcc headers.
130 EasyPG encrypts e-mails to Bcc addresses, and the encrypted e-mail
131 by default identifies the used encryption keys, giving away the
132 Bcc'ed identities.  Clearly, this contradicts the original goal of
133 *blind* copies.
134 For an academic paper explaining the problem, see URL
135 `http://crypto.stanford.edu/portia/papers/bb-bcc.pdf'.
136 Use this variable to specify e-mail addresses whose owners do not
137 mind if they are identifiable as recipients.  This may be useful if
138 you use Bcc headers to encrypt e-mails to yourself."
139   :version "25.1"
140   :group 'message
141   :type '(repeat string))
142
143 ;;; Configuration/helper functions
144
145 (defun mml-signencrypt-style (method &optional style)
146   "Function for setting/getting the signencrypt-style used.  Takes two
147 arguments, the method (e.g. \"pgp\") and optionally the mode
148 \(e.g. combined).  If the mode is omitted, the current value is returned.
149
150 For example, if you prefer to use combined sign & encrypt with
151 smime, putting the following in your Gnus startup file will
152 enable that behavior:
153
154 \(mml-set-signencrypt-style \"smime\" combined)
155
156 You can also customize or set `mml-signencrypt-style-alist' instead."
157   (let ((style-item (assoc method mml-signencrypt-style-alist)))
158     (if style-item
159         (if (or (eq style 'separate)
160                 (eq style 'combined))
161             ;; valid style setting?
162             (setf (second style-item) style)
163           ;; otherwise, just return the current value
164           (second style-item))
165       (message "Warning, attempt to set invalid signencrypt style"))))
166
167 ;;; Security functions
168
169 (defun mml-smime-sign-buffer (cont)
170   (or (mml-smime-sign cont)
171       (error "Signing failed... inspect message logs for errors")))
172
173 (defun mml-smime-encrypt-buffer (cont &optional sign)
174   (when sign
175     (message "Combined sign and encrypt S/MIME not support yet")
176     (sit-for 1))
177   (or (mml-smime-encrypt cont)
178       (error "Encryption failed... inspect message logs for errors")))
179
180 (defun mml-pgp-sign-buffer (cont)
181   (or (mml1991-sign cont)
182       (error "Signing failed... inspect message logs for errors")))
183
184 (defun mml-pgp-encrypt-buffer (cont &optional sign)
185   (or (mml1991-encrypt cont sign)
186       (error "Encryption failed... inspect message logs for errors")))
187
188 (defun mml-pgpmime-sign-buffer (cont)
189   (or (mml2015-sign cont)
190       (error "Signing failed... inspect message logs for errors")))
191
192 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
193   (or (mml2015-encrypt cont sign)
194       (error "Encryption failed... inspect message logs for errors")))
195
196 (defun mml-pgpauto-sign-buffer (cont)
197   (message-goto-body)
198   (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
199           (mml2015-sign cont)
200         (mml1991-sign cont))
201       (error "Encryption failed... inspect message logs for errors")))
202
203 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
204   (message-goto-body)
205   (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
206           (mml2015-encrypt cont sign)
207         (mml1991-encrypt cont sign))
208       (error "Encryption failed... inspect message logs for errors")))
209
210 (defun mml-secure-part (method &optional sign)
211   (save-excursion
212     (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
213                                                 mml-encrypt-alist))))))
214       (cond ((re-search-backward
215               "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
216              (goto-char (match-end 0))
217              (insert (if sign " sign=" " encrypt=") method)
218              (while tags
219                (let ((key (pop tags))
220                      (value (pop tags)))
221                  (when value
222                    ;; Quote VALUE if it contains suspicious characters.
223                    (when (string-match "[\"'\\~/*;() \t\n]" value)
224                      (setq value (prin1-to-string value)))
225                    (insert (format " %s=%s" key value))))))
226             ((or (re-search-backward
227                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
228                  (re-search-forward
229                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
230              (goto-char (match-end 0))
231              (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
232                                                 (cons method tags))))
233             (t (error "The message is corrupted. No mail header separator"))))))
234
235 (defvar mml-secure-method
236   (if (equal mml-default-encrypt-method mml-default-sign-method)
237       mml-default-sign-method
238     "pgpmime")
239   "Current security method.  Internal variable.")
240
241 (defun mml-secure-sign (&optional method)
242   "Add MML tags to sign this MML part.
243 Use METHOD if given.  Else use `mml-secure-method' or
244 `mml-default-sign-method'."
245   (interactive)
246   (mml-secure-part
247    (or method mml-secure-method mml-default-sign-method)
248    'sign))
249
250 (defun mml-secure-encrypt (&optional method)
251   "Add MML tags to encrypt this MML part.
252 Use METHOD if given.  Else use `mml-secure-method' or
253 `mml-default-sign-method'."
254   (interactive)
255   (mml-secure-part
256    (or method mml-secure-method mml-default-sign-method)))
257
258 (defun mml-secure-sign-pgp ()
259   "Add MML tags to PGP sign this MML part."
260   (interactive)
261   (mml-secure-part "pgp" 'sign))
262
263 (defun mml-secure-sign-pgpauto ()
264   "Add MML tags to PGP-auto sign this MML part."
265   (interactive)
266   (mml-secure-part "pgpauto" 'sign))
267
268 (defun mml-secure-sign-pgpmime ()
269   "Add MML tags to PGP/MIME sign this MML part."
270   (interactive)
271   (mml-secure-part "pgpmime" 'sign))
272
273 (defun mml-secure-sign-smime ()
274   "Add MML tags to S/MIME sign this MML part."
275   (interactive)
276   (mml-secure-part "smime" 'sign))
277
278 (defun mml-secure-encrypt-pgp ()
279   "Add MML tags to PGP encrypt this MML part."
280   (interactive)
281   (mml-secure-part "pgp"))
282
283 (defun mml-secure-encrypt-pgpmime ()
284   "Add MML tags to PGP/MIME encrypt this MML part."
285   (interactive)
286   (mml-secure-part "pgpmime"))
287
288 (defun mml-secure-encrypt-smime ()
289   "Add MML tags to S/MIME encrypt this MML part."
290   (interactive)
291   (mml-secure-part "smime"))
292
293 (defun mml-secure-is-encrypted-p ()
294   "Check whether secure encrypt tag is present."
295   (save-excursion
296     (goto-char (point-min))
297     (re-search-forward
298      (concat "^" (regexp-quote mail-header-separator) "\n"
299              "<#secure[^>]+encrypt")
300      nil t)))
301
302 (defun mml-secure-bcc-is-safe ()
303   "Check whether usage of Bcc is safe (or absent).
304 Bcc usage is safe in two cases: first, if the current message does
305 not contain an MML secure encrypt tag;
306 second, if the Bcc addresses are a subset of `mml-secure-safe-bcc-list'.
307 In all other cases, ask the user whether Bcc usage is safe.
308 Raise error if user answers no.
309 Note that this function does not produce a meaningful return value:
310 either an error is raised or not."
311   (when (mml-secure-is-encrypted-p)
312     (let ((bcc (mail-strip-quoted-names (message-fetch-field "bcc"))))
313       (when bcc
314         (let ((bcc-list (mapcar #'cadr
315                                 (mail-extract-address-components bcc t))))
316           (unless (gnus-subsetp bcc-list mml-secure-safe-bcc-list)
317             (unless (yes-or-no-p "Message for encryption contains Bcc header.\
318   This may give away all Bcc'ed identities to all recipients.\
319   Are you sure that this is safe?\
320   (Customize `mml-secure-safe-bcc-list' to avoid this warning.) ")
321               (error "Aborted"))))))))
322
323 ;; defuns that add the proper <#secure ...> tag to the top of the message body
324 (defun mml-secure-message (method &optional modesym)
325   (let ((mode (prin1-to-string modesym))
326         (tags (append
327                (if (or (eq modesym 'sign)
328                        (eq modesym 'signencrypt))
329                    (funcall (nth 2 (assoc method mml-sign-alist))))
330                (if (or (eq modesym 'encrypt)
331                        (eq modesym 'signencrypt))
332                    (funcall (nth 2 (assoc method mml-encrypt-alist))))))
333         insert-loc)
334     (mml-unsecure-message)
335     (save-excursion
336       (goto-char (point-min))
337       (cond ((re-search-forward
338               (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
339              (goto-char (setq insert-loc (match-end 0)))
340              (unless (looking-at "<#secure")
341                (apply 'mml-insert-tag
342                 'secure 'method method 'mode mode tags)))
343             (t (error
344                 "The message is corrupted. No mail header separator"))))
345     (when (eql insert-loc (point))
346       (forward-line 1))))
347
348 (defun mml-unsecure-message ()
349   "Remove security related MML tags from message."
350   (interactive)
351   (save-excursion
352     (goto-char (point-max))
353     (when (re-search-backward "^<#secure.*>\n" nil t)
354       (delete-region (match-beginning 0) (match-end 0)))))
355
356
357 (defun mml-secure-message-sign (&optional method)
358   "Add MML tags to sign the entire message.
359 Use METHOD if given. Else use `mml-secure-method' or
360 `mml-default-sign-method'."
361   (interactive)
362   (mml-secure-message
363    (or method mml-secure-method mml-default-sign-method)
364    'sign))
365
366 (defun mml-secure-message-sign-encrypt (&optional method)
367   "Add MML tag to sign and encrypt the entire message.
368 Use METHOD if given. Else use `mml-secure-method' or
369 `mml-default-sign-method'."
370   (interactive)
371   (mml-secure-message
372    (or method mml-secure-method mml-default-sign-method)
373    'signencrypt))
374
375 (defun mml-secure-message-encrypt (&optional method)
376   "Add MML tag to encrypt the entire message.
377 Use METHOD if given. Else use `mml-secure-method' or
378 `mml-default-sign-method'."
379   (interactive)
380   (mml-secure-message
381    (or method mml-secure-method mml-default-sign-method)
382    'encrypt))
383
384 (defun mml-secure-message-sign-smime ()
385   "Add MML tag to encrypt/sign the entire message."
386   (interactive)
387   (mml-secure-message "smime" 'sign))
388
389 (defun mml-secure-message-sign-pgp ()
390   "Add MML tag to encrypt/sign the entire message."
391   (interactive)
392   (mml-secure-message "pgp" 'sign))
393
394 (defun mml-secure-message-sign-pgpmime ()
395   "Add MML tag to encrypt/sign the entire message."
396   (interactive)
397   (mml-secure-message "pgpmime" 'sign))
398
399 (defun mml-secure-message-sign-pgpauto ()
400   "Add MML tag to encrypt/sign the entire message."
401   (interactive)
402   (mml-secure-message "pgpauto" 'sign))
403
404 (defun mml-secure-message-encrypt-smime (&optional dontsign)
405   "Add MML tag to encrypt and sign the entire message.
406 If called with a prefix argument, only encrypt (do NOT sign)."
407   (interactive "P")
408   (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
409
410 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
411   "Add MML tag to encrypt and sign the entire message.
412 If called with a prefix argument, only encrypt (do NOT sign)."
413   (interactive "P")
414   (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
415
416 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
417   "Add MML tag to encrypt and sign the entire message.
418 If called with a prefix argument, only encrypt (do NOT sign)."
419   (interactive "P")
420   (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
421
422 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
423   "Add MML tag to encrypt and sign the entire message.
424 If called with a prefix argument, only encrypt (do NOT sign)."
425   (interactive "P")
426   (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
427
428 (provide 'mml-sec)
429
430 ;;; mml-sec.el ends here