* lisp/term.el (term-set-escape-char): Properly set term-escape-char.
[emacs.git] / lisp / gnus / mml-sec.el
blobae811afb1a53cf15fb69e4430c19ae9f893bbb0e
1 ;;; mml-sec.el --- A package with security functions for MML documents
3 ;; Copyright (C) 2000-2012 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/>.
22 ;;; Commentary:
24 ;;; Code:
26 (eval-when-compile (require 'cl))
28 (autoload 'mml2015-sign "mml2015")
29 (autoload 'mml2015-encrypt "mml2015")
30 (autoload 'mml1991-sign "mml1991")
31 (autoload 'mml1991-encrypt "mml1991")
32 (autoload 'message-goto-body "message")
33 (autoload 'mml-insert-tag "mml")
34 (autoload 'mml-smime-sign "mml-smime")
35 (autoload 'mml-smime-encrypt "mml-smime")
36 (autoload 'mml-smime-sign-query "mml-smime")
37 (autoload 'mml-smime-encrypt-query "mml-smime")
38 (autoload 'mml-smime-verify "mml-smime")
39 (autoload 'mml-smime-verify-test "mml-smime")
41 (defvar mml-sign-alist
42 '(("smime" mml-smime-sign-buffer mml-smime-sign-query)
43 ("pgp" mml-pgp-sign-buffer list)
44 ("pgpauto" mml-pgpauto-sign-buffer list)
45 ("pgpmime" mml-pgpmime-sign-buffer list))
46 "Alist of MIME signer functions.")
48 (defcustom mml-default-sign-method "pgpmime"
49 "Default sign method.
50 The string must have an entry in `mml-sign-alist'."
51 :version "22.1"
52 :type '(choice (const "smime")
53 (const "pgp")
54 (const "pgpauto")
55 (const "pgpmime")
56 string)
57 :group 'message)
59 (defvar mml-encrypt-alist
60 '(("smime" mml-smime-encrypt-buffer mml-smime-encrypt-query)
61 ("pgp" mml-pgp-encrypt-buffer list)
62 ("pgpauto" mml-pgpauto-sign-buffer list)
63 ("pgpmime" mml-pgpmime-encrypt-buffer list))
64 "Alist of MIME encryption functions.")
66 (defcustom mml-default-encrypt-method "pgpmime"
67 "Default encryption method.
68 The string must have an entry in `mml-encrypt-alist'."
69 :version "22.1"
70 :type '(choice (const "smime")
71 (const "pgp")
72 (const "pgpauto")
73 (const "pgpmime")
74 string)
75 :group 'message)
77 (defcustom mml-signencrypt-style-alist
78 '(("smime" separate)
79 ("pgp" combined)
80 ("pgpauto" combined)
81 ("pgpmime" combined))
82 "Alist specifying if `signencrypt' results in two separate operations or not.
83 The first entry indicates the MML security type, valid entries include
84 the strings \"smime\", \"pgp\", and \"pgpmime\". The second entry is
85 a symbol `separate' or `combined' where `separate' means that MML signs
86 and encrypt messages in a two step process, and `combined' means that MML
87 signs and encrypt the message in one step.
89 Note that the output generated by using a `combined' mode is NOT
90 understood by all PGP implementations, in particular PGP version
91 2 does not support it! See Info node `(message)Security' for
92 details."
93 :version "22.1"
94 :group 'message
95 :type '(repeat (list (choice (const :tag "S/MIME" "smime")
96 (const :tag "PGP" "pgp")
97 (const :tag "PGP/MIME" "pgpmime")
98 (string :tag "User defined"))
99 (choice (const :tag "Separate" separate)
100 (const :tag "Combined" combined)))))
102 (defcustom mml-secure-verbose nil
103 "If non-nil, ask the user about the current operation more verbosely."
104 :group 'message
105 :type 'boolean)
107 (defcustom mml-secure-cache-passphrase
108 (if (boundp 'password-cache)
109 password-cache
111 "If t, cache passphrase."
112 :group 'message
113 :type 'boolean)
115 (defcustom mml-secure-passphrase-cache-expiry
116 (if (boundp 'password-cache-expiry)
117 password-cache-expiry
119 "How many seconds the passphrase is cached.
120 Whether the passphrase is cached at all is controlled by
121 `mml-secure-cache-passphrase'."
122 :group 'message
123 :type 'integer)
125 ;;; Configuration/helper functions
127 (defun mml-signencrypt-style (method &optional style)
128 "Function for setting/getting the signencrypt-style used. Takes two
129 arguments, the method (e.g. \"pgp\") and optionally the mode
130 \(e.g. combined). If the mode is omitted, the current value is returned.
132 For example, if you prefer to use combined sign & encrypt with
133 smime, putting the following in your Gnus startup file will
134 enable that behavior:
136 \(mml-set-signencrypt-style \"smime\" combined)
138 You can also customize or set `mml-signencrypt-style-alist' instead."
139 (let ((style-item (assoc method mml-signencrypt-style-alist)))
140 (if style-item
141 (if (or (eq style 'separate)
142 (eq style 'combined))
143 ;; valid style setting?
144 (setf (second style-item) style)
145 ;; otherwise, just return the current value
146 (second style-item))
147 (message "Warning, attempt to set invalid signencrypt style"))))
149 ;;; Security functions
151 (defun mml-smime-sign-buffer (cont)
152 (or (mml-smime-sign cont)
153 (error "Signing failed... inspect message logs for errors")))
155 (defun mml-smime-encrypt-buffer (cont &optional sign)
156 (when sign
157 (message "Combined sign and encrypt S/MIME not support yet")
158 (sit-for 1))
159 (or (mml-smime-encrypt cont)
160 (error "Encryption failed... inspect message logs for errors")))
162 (defun mml-pgp-sign-buffer (cont)
163 (or (mml1991-sign cont)
164 (error "Signing failed... inspect message logs for errors")))
166 (defun mml-pgp-encrypt-buffer (cont &optional sign)
167 (or (mml1991-encrypt cont sign)
168 (error "Encryption failed... inspect message logs for errors")))
170 (defun mml-pgpmime-sign-buffer (cont)
171 (or (mml2015-sign cont)
172 (error "Signing failed... inspect message logs for errors")))
174 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
175 (or (mml2015-encrypt cont sign)
176 (error "Encryption failed... inspect message logs for errors")))
178 (defun mml-pgpauto-sign-buffer (cont)
179 (message-goto-body)
180 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
181 (mml2015-sign cont)
182 (mml1991-sign cont))
183 (error "Encryption failed... inspect message logs for errors")))
185 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
186 (message-goto-body)
187 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
188 (mml2015-encrypt cont sign)
189 (mml1991-encrypt cont sign))
190 (error "Encryption failed... inspect message logs for errors")))
192 (defun mml-secure-part (method &optional sign)
193 (save-excursion
194 (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
195 mml-encrypt-alist))))))
196 (cond ((re-search-backward
197 "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
198 (goto-char (match-end 0))
199 (insert (if sign " sign=" " encrypt=") method)
200 (while tags
201 (let ((key (pop tags))
202 (value (pop tags)))
203 (when value
204 ;; Quote VALUE if it contains suspicious characters.
205 (when (string-match "[\"'\\~/*;() \t\n]" value)
206 (setq value (prin1-to-string value)))
207 (insert (format " %s=%s" key value))))))
208 ((or (re-search-backward
209 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
210 (re-search-forward
211 (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
212 (goto-char (match-end 0))
213 (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
214 (cons method tags))))
215 (t (error "The message is corrupted. No mail header separator"))))))
217 (defvar mml-secure-method
218 (if (equal mml-default-encrypt-method mml-default-sign-method)
219 mml-default-sign-method
220 "pgpmime")
221 "Current security method. Internal variable.")
223 (defun mml-secure-sign (&optional method)
224 "Add MML tags to sign this MML part.
225 Use METHOD if given. Else use `mml-secure-method' or
226 `mml-default-sign-method'."
227 (interactive)
228 (mml-secure-part
229 (or method mml-secure-method mml-default-sign-method)
230 'sign))
232 (defun mml-secure-encrypt (&optional method)
233 "Add MML tags to encrypt this MML part.
234 Use METHOD if given. Else use `mml-secure-method' or
235 `mml-default-sign-method'."
236 (interactive)
237 (mml-secure-part
238 (or method mml-secure-method mml-default-sign-method)))
240 (defun mml-secure-sign-pgp ()
241 "Add MML tags to PGP sign this MML part."
242 (interactive)
243 (mml-secure-part "pgp" 'sign))
245 (defun mml-secure-sign-pgpauto ()
246 "Add MML tags to PGP-auto sign this MML part."
247 (interactive)
248 (mml-secure-part "pgpauto" 'sign))
250 (defun mml-secure-sign-pgpmime ()
251 "Add MML tags to PGP/MIME sign this MML part."
252 (interactive)
253 (mml-secure-part "pgpmime" 'sign))
255 (defun mml-secure-sign-smime ()
256 "Add MML tags to S/MIME sign this MML part."
257 (interactive)
258 (mml-secure-part "smime" 'sign))
260 (defun mml-secure-encrypt-pgp ()
261 "Add MML tags to PGP encrypt this MML part."
262 (interactive)
263 (mml-secure-part "pgp"))
265 (defun mml-secure-encrypt-pgpmime ()
266 "Add MML tags to PGP/MIME encrypt this MML part."
267 (interactive)
268 (mml-secure-part "pgpmime"))
270 (defun mml-secure-encrypt-smime ()
271 "Add MML tags to S/MIME encrypt this MML part."
272 (interactive)
273 (mml-secure-part "smime"))
275 ;; defuns that add the proper <#secure ...> tag to the top of the message body
276 (defun mml-secure-message (method &optional modesym)
277 (let ((mode (prin1-to-string modesym))
278 (tags (append
279 (if (or (eq modesym 'sign)
280 (eq modesym 'signencrypt))
281 (funcall (nth 2 (assoc method mml-sign-alist))))
282 (if (or (eq modesym 'encrypt)
283 (eq modesym 'signencrypt))
284 (funcall (nth 2 (assoc method mml-encrypt-alist))))))
285 insert-loc)
286 (mml-unsecure-message)
287 (save-excursion
288 (goto-char (point-min))
289 (cond ((re-search-forward
290 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
291 (goto-char (setq insert-loc (match-end 0)))
292 (unless (looking-at "<#secure")
293 (apply 'mml-insert-tag
294 'secure 'method method 'mode mode tags)))
295 (t (error
296 "The message is corrupted. No mail header separator"))))
297 (when (eql insert-loc (point))
298 (forward-line 1))))
300 (defun mml-unsecure-message ()
301 "Remove security related MML tags from message."
302 (interactive)
303 (save-excursion
304 (goto-char (point-max))
305 (when (re-search-backward "^<#secure.*>\n" nil t)
306 (delete-region (match-beginning 0) (match-end 0)))))
309 (defun mml-secure-message-sign (&optional method)
310 "Add MML tags to sign the entire message.
311 Use METHOD if given. Else use `mml-secure-method' or
312 `mml-default-sign-method'."
313 (interactive)
314 (mml-secure-message
315 (or method mml-secure-method mml-default-sign-method)
316 'sign))
318 (defun mml-secure-message-sign-encrypt (&optional method)
319 "Add MML tag to sign and encrypt the entire message.
320 Use METHOD if given. Else use `mml-secure-method' or
321 `mml-default-sign-method'."
322 (interactive)
323 (mml-secure-message
324 (or method mml-secure-method mml-default-sign-method)
325 'signencrypt))
327 (defun mml-secure-message-encrypt (&optional method)
328 "Add MML tag to encrypt the entire message.
329 Use METHOD if given. Else use `mml-secure-method' or
330 `mml-default-sign-method'."
331 (interactive)
332 (mml-secure-message
333 (or method mml-secure-method mml-default-sign-method)
334 'encrypt))
336 (defun mml-secure-message-sign-smime ()
337 "Add MML tag to encrypt/sign the entire message."
338 (interactive)
339 (mml-secure-message "smime" 'sign))
341 (defun mml-secure-message-sign-pgp ()
342 "Add MML tag to encrypt/sign the entire message."
343 (interactive)
344 (mml-secure-message "pgp" 'sign))
346 (defun mml-secure-message-sign-pgpmime ()
347 "Add MML tag to encrypt/sign the entire message."
348 (interactive)
349 (mml-secure-message "pgpmime" 'sign))
351 (defun mml-secure-message-sign-pgpauto ()
352 "Add MML tag to encrypt/sign the entire message."
353 (interactive)
354 (mml-secure-message "pgpauto" 'sign))
356 (defun mml-secure-message-encrypt-smime (&optional dontsign)
357 "Add MML tag to encrypt and sign the entire message.
358 If called with a prefix argument, only encrypt (do NOT sign)."
359 (interactive "P")
360 (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
362 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
363 "Add MML tag to encrypt and sign the entire message.
364 If called with a prefix argument, only encrypt (do NOT sign)."
365 (interactive "P")
366 (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
368 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
369 "Add MML tag to encrypt and sign the entire message.
370 If called with a prefix argument, only encrypt (do NOT sign)."
371 (interactive "P")
372 (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
374 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
375 "Add MML tag to encrypt and sign the entire message.
376 If called with a prefix argument, only encrypt (do NOT sign)."
377 (interactive "P")
378 (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
380 (provide 'mml-sec)
382 ;;; mml-sec.el ends here