1 ;;; mml-sec.el --- A package with security functions for MML documents
3 ;; Copyright (C) 2000-2016 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 (eval-when-compile (require 'cl))
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")
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.")
51 (defcustom mml-default-sign-method "pgpmime"
53 The string must have an entry in `mml-sign-alist'."
55 :type '(choice (const "smime")
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.")
69 (defcustom mml-default-encrypt-method "pgpmime"
70 "Default encryption method.
71 The string must have an entry in `mml-encrypt-alist'."
73 :type '(choice (const "smime")
80 (defcustom mml-signencrypt-style-alist
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.
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
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)))))
105 (defcustom mml-secure-verbose nil
106 "If non-nil, ask the user about the current operation more verbosely."
110 (defcustom mml-secure-cache-passphrase
111 (if (boundp 'password-cache)
114 "If t, cache passphrase."
118 (defcustom mml-secure-passphrase-cache-expiry
119 (if (boundp 'password-cache-expiry)
120 password-cache-expiry
122 "How many seconds the passphrase is cached.
123 Whether the passphrase is cached at all is controlled by
124 `mml-secure-cache-passphrase'."
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
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."
141 :type '(repeat string))
143 ;;; Configuration/helper functions
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.
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:
154 \(mml-set-signencrypt-style \"smime\" combined)
156 You can also customize or set `mml-signencrypt-style-alist' instead."
157 (let ((style-item (assoc method mml-signencrypt-style-alist)))
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
165 (message "Warning, attempt to set invalid signencrypt style"))))
167 ;;; Security functions
169 (defun mml-smime-sign-buffer (cont)
170 (or (mml-smime-sign cont)
171 (error "Signing failed... inspect message logs for errors")))
173 (defun mml-smime-encrypt-buffer (cont &optional sign)
175 (message "Combined sign and encrypt S/MIME not support yet")
177 (or (mml-smime-encrypt cont)
178 (error "Encryption failed... inspect message logs for errors")))
180 (defun mml-pgp-sign-buffer (cont)
181 (or (mml1991-sign cont)
182 (error "Signing failed... inspect message logs for errors")))
184 (defun mml-pgp-encrypt-buffer (cont &optional sign)
185 (or (mml1991-encrypt cont sign)
186 (error "Encryption failed... inspect message logs for errors")))
188 (defun mml-pgpmime-sign-buffer (cont)
189 (or (mml2015-sign cont)
190 (error "Signing failed... inspect message logs for errors")))
192 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
193 (or (mml2015-encrypt cont sign)
194 (error "Encryption failed... inspect message logs for errors")))
196 (defun mml-pgpauto-sign-buffer (cont)
198 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
201 (error "Encryption failed... inspect message logs for errors")))
203 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
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")))
210 (defun mml-secure-part (method &optional sign)
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)
219 (let ((key (pop tags))
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)
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"))))))
235 (defvar mml-secure-method
236 (if (equal mml-default-encrypt-method mml-default-sign-method)
237 mml-default-sign-method
239 "Current security method. Internal variable.")
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'."
247 (or method mml-secure-method mml-default-sign-method)
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'."
256 (or method mml-secure-method mml-default-sign-method)))
258 (defun mml-secure-sign-pgp ()
259 "Add MML tags to PGP sign this MML part."
261 (mml-secure-part "pgp" 'sign))
263 (defun mml-secure-sign-pgpauto ()
264 "Add MML tags to PGP-auto sign this MML part."
266 (mml-secure-part "pgpauto" 'sign))
268 (defun mml-secure-sign-pgpmime ()
269 "Add MML tags to PGP/MIME sign this MML part."
271 (mml-secure-part "pgpmime" 'sign))
273 (defun mml-secure-sign-smime ()
274 "Add MML tags to S/MIME sign this MML part."
276 (mml-secure-part "smime" 'sign))
278 (defun mml-secure-encrypt-pgp ()
279 "Add MML tags to PGP encrypt this MML part."
281 (mml-secure-part "pgp"))
283 (defun mml-secure-encrypt-pgpmime ()
284 "Add MML tags to PGP/MIME encrypt this MML part."
286 (mml-secure-part "pgpmime"))
288 (defun mml-secure-encrypt-smime ()
289 "Add MML tags to S/MIME encrypt this MML part."
291 (mml-secure-part "smime"))
293 (defun mml-secure-is-encrypted-p ()
294 "Check whether secure encrypt tag is present."
296 (goto-char (point-min))
298 (concat "^" (regexp-quote mail-header-separator) "\n"
299 "<#secure[^>]+encrypt")
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"))))
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"))))))))
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))
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))))))
334 (mml-unsecure-message)
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)))
344 "The message is corrupted. No mail header separator"))))
345 (when (eql insert-loc (point))
348 (defun mml-unsecure-message ()
349 "Remove security related MML tags from message."
352 (goto-char (point-max))
353 (when (re-search-backward "^<#secure.*>\n" nil t)
354 (delete-region (match-beginning 0) (match-end 0)))))
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'."
363 (or method mml-secure-method mml-default-sign-method)
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'."
372 (or method mml-secure-method mml-default-sign-method)
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'."
381 (or method mml-secure-method mml-default-sign-method)
384 (defun mml-secure-message-sign-smime ()
385 "Add MML tag to encrypt/sign the entire message."
387 (mml-secure-message "smime" 'sign))
389 (defun mml-secure-message-sign-pgp ()
390 "Add MML tag to encrypt/sign the entire message."
392 (mml-secure-message "pgp" 'sign))
394 (defun mml-secure-message-sign-pgpmime ()
395 "Add MML tag to encrypt/sign the entire message."
397 (mml-secure-message "pgpmime" 'sign))
399 (defun mml-secure-message-sign-pgpauto ()
400 "Add MML tag to encrypt/sign the entire message."
402 (mml-secure-message "pgpauto" 'sign))
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)."
408 (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
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)."
414 (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
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)."
420 (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
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)."
426 (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
430 ;;; mml-sec.el ends here