]> code.delx.au - gnu-emacs/blob - lisp/gnus/mml-sec.el
Update copyright year to 2016
[gnu-emacs] / lisp / gnus / mml-sec.el
1 ;;; mml-sec.el --- A package with security functions for MML documents
2
3 ;; Copyright (C) 2000-2016 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 ;;; Configuration/helper functions
129
130 (defun mml-signencrypt-style (method &optional style)
131 "Function for setting/getting the signencrypt-style used. Takes two
132 arguments, the method (e.g. \"pgp\") and optionally the mode
133 \(e.g. combined). If the mode is omitted, the current value is returned.
134
135 For example, if you prefer to use combined sign & encrypt with
136 smime, putting the following in your Gnus startup file will
137 enable that behavior:
138
139 \(mml-set-signencrypt-style \"smime\" combined)
140
141 You can also customize or set `mml-signencrypt-style-alist' instead."
142 (let ((style-item (assoc method mml-signencrypt-style-alist)))
143 (if style-item
144 (if (or (eq style 'separate)
145 (eq style 'combined))
146 ;; valid style setting?
147 (setf (second style-item) style)
148 ;; otherwise, just return the current value
149 (second style-item))
150 (message "Warning, attempt to set invalid signencrypt style"))))
151
152 ;;; Security functions
153
154 (defun mml-smime-sign-buffer (cont)
155 (or (mml-smime-sign cont)
156 (error "Signing failed... inspect message logs for errors")))
157
158 (defun mml-smime-encrypt-buffer (cont &optional sign)
159 (when sign
160 (message "Combined sign and encrypt S/MIME not support yet")
161 (sit-for 1))
162 (or (mml-smime-encrypt cont)
163 (error "Encryption failed... inspect message logs for errors")))
164
165 (defun mml-pgp-sign-buffer (cont)
166 (or (mml1991-sign cont)
167 (error "Signing failed... inspect message logs for errors")))
168
169 (defun mml-pgp-encrypt-buffer (cont &optional sign)
170 (or (mml1991-encrypt cont sign)
171 (error "Encryption failed... inspect message logs for errors")))
172
173 (defun mml-pgpmime-sign-buffer (cont)
174 (or (mml2015-sign cont)
175 (error "Signing failed... inspect message logs for errors")))
176
177 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
178 (or (mml2015-encrypt cont sign)
179 (error "Encryption failed... inspect message logs for errors")))
180
181 (defun mml-pgpauto-sign-buffer (cont)
182 (message-goto-body)
183 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
184 (mml2015-sign cont)
185 (mml1991-sign cont))
186 (error "Encryption failed... inspect message logs for errors")))
187
188 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
189 (message-goto-body)
190 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
191 (mml2015-encrypt cont sign)
192 (mml1991-encrypt cont sign))
193 (error "Encryption failed... inspect message logs for errors")))
194
195 (defun mml-secure-part (method &optional sign)
196 (save-excursion
197 (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
198 mml-encrypt-alist))))))
199 (cond ((re-search-backward
200 "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
201 (goto-char (match-end 0))
202 (insert (if sign " sign=" " encrypt=") method)
203 (while tags
204 (let ((key (pop tags))
205 (value (pop tags)))
206 (when value
207 ;; Quote VALUE if it contains suspicious characters.
208 (when (string-match "[\"'\\~/*;() \t\n]" value)
209 (setq value (prin1-to-string value)))
210 (insert (format " %s=%s" key value))))))
211 ((or (re-search-backward
212 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
213 (re-search-forward
214 (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
215 (goto-char (match-end 0))
216 (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
217 (cons method tags))))
218 (t (error "The message is corrupted. No mail header separator"))))))
219
220 (defvar mml-secure-method
221 (if (equal mml-default-encrypt-method mml-default-sign-method)
222 mml-default-sign-method
223 "pgpmime")
224 "Current security method. Internal variable.")
225
226 (defun mml-secure-sign (&optional method)
227 "Add MML tags to sign this MML part.
228 Use METHOD if given. Else use `mml-secure-method' or
229 `mml-default-sign-method'."
230 (interactive)
231 (mml-secure-part
232 (or method mml-secure-method mml-default-sign-method)
233 'sign))
234
235 (defun mml-secure-encrypt (&optional method)
236 "Add MML tags to encrypt this MML part.
237 Use METHOD if given. Else use `mml-secure-method' or
238 `mml-default-sign-method'."
239 (interactive)
240 (mml-secure-part
241 (or method mml-secure-method mml-default-sign-method)))
242
243 (defun mml-secure-sign-pgp ()
244 "Add MML tags to PGP sign this MML part."
245 (interactive)
246 (mml-secure-part "pgp" 'sign))
247
248 (defun mml-secure-sign-pgpauto ()
249 "Add MML tags to PGP-auto sign this MML part."
250 (interactive)
251 (mml-secure-part "pgpauto" 'sign))
252
253 (defun mml-secure-sign-pgpmime ()
254 "Add MML tags to PGP/MIME sign this MML part."
255 (interactive)
256 (mml-secure-part "pgpmime" 'sign))
257
258 (defun mml-secure-sign-smime ()
259 "Add MML tags to S/MIME sign this MML part."
260 (interactive)
261 (mml-secure-part "smime" 'sign))
262
263 (defun mml-secure-encrypt-pgp ()
264 "Add MML tags to PGP encrypt this MML part."
265 (interactive)
266 (mml-secure-part "pgp"))
267
268 (defun mml-secure-encrypt-pgpmime ()
269 "Add MML tags to PGP/MIME encrypt this MML part."
270 (interactive)
271 (mml-secure-part "pgpmime"))
272
273 (defun mml-secure-encrypt-smime ()
274 "Add MML tags to S/MIME encrypt this MML part."
275 (interactive)
276 (mml-secure-part "smime"))
277
278 ;; defuns that add the proper <#secure ...> tag to the top of the message body
279 (defun mml-secure-message (method &optional modesym)
280 (let ((mode (prin1-to-string modesym))
281 (tags (append
282 (if (or (eq modesym 'sign)
283 (eq modesym 'signencrypt))
284 (funcall (nth 2 (assoc method mml-sign-alist))))
285 (if (or (eq modesym 'encrypt)
286 (eq modesym 'signencrypt))
287 (funcall (nth 2 (assoc method mml-encrypt-alist))))))
288 insert-loc)
289 (mml-unsecure-message)
290 (save-excursion
291 (goto-char (point-min))
292 (cond ((re-search-forward
293 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
294 (goto-char (setq insert-loc (match-end 0)))
295 (unless (looking-at "<#secure")
296 (apply 'mml-insert-tag
297 'secure 'method method 'mode mode tags)))
298 (t (error
299 "The message is corrupted. No mail header separator"))))
300 (when (eql insert-loc (point))
301 (forward-line 1))))
302
303 (defun mml-unsecure-message ()
304 "Remove security related MML tags from message."
305 (interactive)
306 (save-excursion
307 (goto-char (point-max))
308 (when (re-search-backward "^<#secure.*>\n" nil t)
309 (delete-region (match-beginning 0) (match-end 0)))))
310
311
312 (defun mml-secure-message-sign (&optional method)
313 "Add MML tags to sign the entire message.
314 Use METHOD if given. Else use `mml-secure-method' or
315 `mml-default-sign-method'."
316 (interactive)
317 (mml-secure-message
318 (or method mml-secure-method mml-default-sign-method)
319 'sign))
320
321 (defun mml-secure-message-sign-encrypt (&optional method)
322 "Add MML tag to sign and encrypt the entire message.
323 Use METHOD if given. Else use `mml-secure-method' or
324 `mml-default-sign-method'."
325 (interactive)
326 (mml-secure-message
327 (or method mml-secure-method mml-default-sign-method)
328 'signencrypt))
329
330 (defun mml-secure-message-encrypt (&optional method)
331 "Add MML tag to encrypt the entire message.
332 Use METHOD if given. Else use `mml-secure-method' or
333 `mml-default-sign-method'."
334 (interactive)
335 (mml-secure-message
336 (or method mml-secure-method mml-default-sign-method)
337 'encrypt))
338
339 (defun mml-secure-message-sign-smime ()
340 "Add MML tag to encrypt/sign the entire message."
341 (interactive)
342 (mml-secure-message "smime" 'sign))
343
344 (defun mml-secure-message-sign-pgp ()
345 "Add MML tag to encrypt/sign the entire message."
346 (interactive)
347 (mml-secure-message "pgp" 'sign))
348
349 (defun mml-secure-message-sign-pgpmime ()
350 "Add MML tag to encrypt/sign the entire message."
351 (interactive)
352 (mml-secure-message "pgpmime" 'sign))
353
354 (defun mml-secure-message-sign-pgpauto ()
355 "Add MML tag to encrypt/sign the entire message."
356 (interactive)
357 (mml-secure-message "pgpauto" 'sign))
358
359 (defun mml-secure-message-encrypt-smime (&optional dontsign)
360 "Add MML tag to encrypt and sign the entire message.
361 If called with a prefix argument, only encrypt (do NOT sign)."
362 (interactive "P")
363 (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
364
365 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
366 "Add MML tag to encrypt and sign the entire message.
367 If called with a prefix argument, only encrypt (do NOT sign)."
368 (interactive "P")
369 (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
370
371 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
372 "Add MML tag to encrypt and sign the entire message.
373 If called with a prefix argument, only encrypt (do NOT sign)."
374 (interactive "P")
375 (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
376
377 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
378 "Add MML tag to encrypt and sign the entire message.
379 If called with a prefix argument, only encrypt (do NOT sign)."
380 (interactive "P")
381 (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
382
383 (provide 'mml-sec)
384
385 ;;; mml-sec.el ends here