(file-remote-p): Docstring fix.
[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,
e3fe4da0 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
23f87bed
MB
5
6;; Author: Simon Josefsson <simon@josefsson.org>
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
5a9dffec 12;; the Free Software Foundation; either version 3, or (at your option)
23f87bed
MB
13;; any later version.
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
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
23f87bed
MB
24
25;;; Commentary:
26
27;;; Code:
28
23f87bed 29(eval-when-compile (require 'cl))
87035689
MB
30
31(if (locate-library "password-cache")
32 (require 'password-cache)
33 (require 'password))
51588bc5 34
23f87bed
MB
35(autoload 'mml2015-sign "mml2015")
36(autoload 'mml2015-encrypt "mml2015")
37(autoload 'mml1991-sign "mml1991")
38(autoload 'mml1991-encrypt "mml1991")
39(autoload 'message-goto-body "message")
40(autoload 'mml-insert-tag "mml")
01c52d31
MB
41(autoload 'mml-smime-sign "mml-smime")
42(autoload 'mml-smime-encrypt "mml-smime")
43(autoload 'mml-smime-sign-query "mml-smime")
44(autoload 'mml-smime-encrypt-query "mml-smime")
45(autoload 'mml-smime-verify "mml-smime")
46(autoload 'mml-smime-verify-test "mml-smime")
23f87bed
MB
47
48(defvar mml-sign-alist
49 '(("smime" mml-smime-sign-buffer mml-smime-sign-query)
50 ("pgp" mml-pgp-sign-buffer list)
51 ("pgpauto" mml-pgpauto-sign-buffer list)
52 ("pgpmime" mml-pgpmime-sign-buffer list))
53 "Alist of MIME signer functions.")
54
55(defcustom mml-default-sign-method "pgpmime"
56 "Default sign method.
57The string must have an entry in `mml-sign-alist'."
bf247b6e 58 :version "22.1"
23f87bed
MB
59 :type '(choice (const "smime")
60 (const "pgp")
61 (const "pgpauto")
62 (const "pgpmime")
63 string)
64 :group 'message)
65
66(defvar mml-encrypt-alist
67 '(("smime" mml-smime-encrypt-buffer mml-smime-encrypt-query)
68 ("pgp" mml-pgp-encrypt-buffer list)
69 ("pgpauto" mml-pgpauto-sign-buffer list)
70 ("pgpmime" mml-pgpmime-encrypt-buffer list))
71 "Alist of MIME encryption functions.")
72
73(defcustom mml-default-encrypt-method "pgpmime"
74 "Default encryption method.
75The string must have an entry in `mml-encrypt-alist'."
bf247b6e 76 :version "22.1"
23f87bed
MB
77 :type '(choice (const "smime")
78 (const "pgp")
79 (const "pgpauto")
80 (const "pgpmime")
81 string)
82 :group 'message)
83
84(defcustom mml-signencrypt-style-alist
85 '(("smime" separate)
86 ("pgp" combined)
87 ("pgpauto" combined)
88 ("pgpmime" combined))
89 "Alist specifying if `signencrypt' results in two separate operations or not.
90The first entry indicates the MML security type, valid entries include
91the strings \"smime\", \"pgp\", and \"pgpmime\". The second entry is
92a symbol `separate' or `combined' where `separate' means that MML signs
93and encrypt messages in a two step process, and `combined' means that MML
94signs and encrypt the message in one step.
95
96Note that the output generated by using a `combined' mode is NOT
97understood by all PGP implementations, in particular PGP version
982 does not support it! See Info node `(message)Security' for
99details."
bf247b6e 100 :version "22.1"
e79f14a4 101 :group 'message
23f87bed
MB
102 :type '(repeat (list (choice (const :tag "S/MIME" "smime")
103 (const :tag "PGP" "pgp")
104 (const :tag "PGP/MIME" "pgpmime")
105 (string :tag "User defined"))
106 (choice (const :tag "Separate" separate)
107 (const :tag "Combined" combined)))))
108
01c52d31
MB
109(defcustom mml-secure-verbose nil
110 "If non-nil, ask the user about the current operation more verbosely."
111 :group 'message
112 :type 'boolean)
113
114(defcustom mml-secure-cache-passphrase password-cache
115 "If t, cache passphrase."
116 :group 'message
117 :type 'boolean)
118
119(defcustom mml-secure-passphrase-cache-expiry password-cache-expiry
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)
311 "Add MML tags to sign this MML part.
312Use METHOD if given. Else use `mml-secure-method' or
313`mml-default-sign-method'."
314 (interactive)
315 (mml-secure-part
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
383;;; arch-tag: 111c56e7-df5e-4287-87d7-93ed2911ec6c
384;;; mml-sec.el ends here