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