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