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