* gnus.el: Don't test for `mm-guess-mime-charset'.
[gnus] / lisp / mml-sec.el
1 ;;; mml-sec.el --- A package with security functions for MML documents
2 ;; Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'mml-smime)
28 (eval-when-compile (require 'cl))
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")
35
36 (defvar mml-sign-alist
37   '(("smime"     mml-smime-sign-buffer     mml-smime-sign-query)
38     ("pgp"       mml-pgp-sign-buffer       list)
39     ("pgpauto"   mml-pgpauto-sign-buffer  list)
40     ("pgpmime"   mml-pgpmime-sign-buffer   list))
41   "Alist of MIME signer functions.")
42
43 (defvar mml-default-sign-method (caar mml-sign-alist)
44   "Default sign method.")
45
46 (defvar mml-encrypt-alist
47   '(("smime"     mml-smime-encrypt-buffer     mml-smime-encrypt-query)
48     ("pgp"       mml-pgp-encrypt-buffer       list)
49     ("pgpauto"   mml-pgpauto-sign-buffer  list)
50     ("pgpmime"   mml-pgpmime-encrypt-buffer   list))
51   "Alist of MIME encryption functions.")
52
53 (defvar mml-default-encrypt-method (caar mml-encrypt-alist)
54   "Default encryption method.")
55
56 (defcustom mml-signencrypt-style-alist
57   '(("smime"   separate)
58     ("pgp"     separate)
59     ("pgpauto" separate)
60     ("pgpmime" separate))
61   "Alist specifying if `signencrypt' results in two separate operations or not.
62 The first entry indicates the MML security type, valid entries include
63 the strings \"smime\", \"pgp\", and \"pgpmime\".  The second entry is
64 a symbol `separate' or `combined' where `separate' means that MML signs
65 and encrypt messages in a two step process, and `combined' means that MML
66 signs and encrypt the message in one step.
67 Note that the `combined' mode is NOT supported by all OpenPGP implementations,
68 in particular PGP version 2 does not support it!"
69   :type '(repeat (list (choice (const :tag "S/MIME" "smime")
70                                (const :tag "PGP" "pgp")
71                                (const :tag "PGP/MIME" "pgpmime")
72                                (string :tag "User defined"))
73                        (choice (const :tag "Separate" separate)
74                                (const :tag "Combined" combined)))))
75                               
76 ;;; Configuration/helper functions
77
78 (defun mml-signencrypt-style (method &optional style)
79   "Function for setting/getting the signencrypt-style used.  Takes two
80 arguments, the method (e.g. \"pgp\") and optionally the mode
81 \(e.g. combined).  If the mode is omitted, the current value is returned.
82
83 For example, if you prefer to use combined sign & encrypt with
84 smime, putting the following in your Gnus startup file will
85 enable that behavior:
86
87 \(mml-set-signencrypt-style \"smime\" combined)
88
89 You can also customize or set `mml-signencrypt-style-alist' instead."
90   (let ((style-item (assoc method mml-signencrypt-style-alist)))
91     (if style-item
92         (if (or (eq style 'separate)
93                 (eq style 'combined))
94             ;; valid style setting?
95             (setf (second style-item) style)
96           ;; otherwise, just return the current value
97           (second style-item))
98       (gnus-message 3 "Warning, attempt to set invalid signencrypt-style"))))
99
100 ;;; Security functions
101
102 (defun mml-smime-sign-buffer (cont)
103   (or (mml-smime-sign cont)
104       (error "Signing failed... inspect message logs for errors")))
105
106 (defun mml-smime-encrypt-buffer (cont &optional sign)
107   (when sign
108     (message "Combined sign and encrypt S/MIME not support yet")
109     (sit-for 1))
110   (or (mml-smime-encrypt cont)
111       (error "Encryption failed... inspect message logs for errors")))
112
113 (defun mml-pgp-sign-buffer (cont)
114   (or (mml1991-sign cont)
115       (error "Signing failed... inspect message logs for errors")))
116
117 (defun mml-pgp-encrypt-buffer (cont &optional sign)
118   (or (mml1991-encrypt cont sign)
119       (error "Encryption failed... inspect message logs for errors")))
120
121 (defun mml-pgpmime-sign-buffer (cont)
122   (or (mml2015-sign cont)
123       (error "Signing failed... inspect message logs for errors")))
124
125 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
126   (or (mml2015-encrypt cont sign)
127       (error "Encryption failed... inspect message logs for errors")))
128
129 (defun mml-pgpauto-sign-buffer (cont)
130   (message-goto-body)
131   (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
132           (mml2015-sign cont)
133         (mml1991-sign cont))
134       (error "Encryption failed... inspect message logs for errors")))
135
136 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
137   (message-goto-body)
138   (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
139           (mml2015-encrypt cont sign)
140         (mml1991-encrypt cont sign))
141       (error "Encryption failed... inspect message logs for errors")))
142
143 (defun mml-secure-part (method &optional sign)
144   (save-excursion
145     (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
146                                                 mml-encrypt-alist))))))
147       (cond ((re-search-backward
148               "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
149              (goto-char (match-end 0))
150              (insert (if sign " sign=" " encrypt=") method)
151              (while tags
152                (let ((key (pop tags))
153                      (value (pop tags)))
154                  (when value
155                    ;; Quote VALUE if it contains suspicious characters.
156                    (when (string-match "[\"'\\~/*;() \t\n]" value)
157                      (setq value (prin1-to-string value)))
158                    (insert (format " %s=%s" key value))))))
159             ((or (re-search-backward
160                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
161                  (re-search-forward
162                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
163              (goto-char (match-end 0))
164              (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
165                                                 (cons method tags))))
166             (t (error "The message is corrupted. No mail header separator"))))))
167
168 (defun mml-secure-sign-pgp ()
169   "Add MML tags to PGP sign this MML part."
170   (interactive)
171   (mml-secure-part "pgp" 'sign))
172
173 (defun mml-secure-sign-pgpauto ()
174   "Add MML tags to PGP-auto sign this MML part."
175   (interactive)
176   (mml-secure-part "pgpauto" 'sign))
177
178 (defun mml-secure-sign-pgpmime ()
179   "Add MML tags to PGP/MIME sign this MML part."
180   (interactive)
181   (mml-secure-part "pgpmime" 'sign))
182
183 (defun mml-secure-sign-smime ()
184   "Add MML tags to S/MIME sign this MML part."
185   (interactive)
186   (mml-secure-part "smime" 'sign))
187
188 (defun mml-secure-encrypt-pgp ()
189   "Add MML tags to PGP encrypt this MML part."
190   (interactive)
191   (mml-secure-part "pgp"))
192
193 (defun mml-secure-encrypt-pgpmime ()
194   "Add MML tags to PGP/MIME encrypt this MML part."
195   (interactive)
196   (mml-secure-part "pgpmime"))
197
198 (defun mml-secure-encrypt-smime ()
199   "Add MML tags to S/MIME encrypt this MML part."
200   (interactive)
201   (mml-secure-part "smime"))
202
203 ;; defuns that add the proper <#secure ...> tag to the top of the message body
204 (defun mml-secure-message (method &optional modesym)
205   (let ((mode (prin1-to-string modesym))
206         insert-loc)
207     (mml-unsecure-message)
208     (save-excursion
209       (goto-char (point-min))
210       (cond ((re-search-forward
211               (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
212              (goto-char (setq insert-loc (match-end 0)))
213              (unless (looking-at "<#secure")
214                (mml-insert-tag
215                 'secure 'method method 'mode mode)))
216             (t (error
217                 "The message is corrupted. No mail header separator"))))
218     (when (eql insert-loc (point))
219       (forward-line 1))))
220
221 (defun mml-unsecure-message ()
222   "Remove security related MML tags from message."
223   (interactive)
224   (save-excursion
225     (goto-char (point-max))
226     (when (re-search-backward "^<#secure.*>\n" nil t)
227       (delete-region (match-beginning 0) (match-end 0)))))
228
229 (defun mml-secure-message-sign-smime ()
230   "Add MML tag to encrypt/sign the entire message."
231   (interactive)
232   (mml-secure-message "smime" 'sign))
233
234 (defun mml-secure-message-sign-pgp ()
235   "Add MML tag to encrypt/sign the entire message."
236   (interactive)
237   (mml-secure-message "pgp" 'sign))
238
239 (defun mml-secure-message-sign-pgpmime ()
240   "Add MML tag to encrypt/sign the entire message."
241   (interactive)
242   (mml-secure-message "pgpmime" 'sign))
243
244 (defun mml-secure-message-sign-pgpauto ()
245   "Add MML tag to encrypt/sign the entire message."
246   (interactive)
247   (mml-secure-message "pgpauto" 'sign))
248
249 (defun mml-secure-message-encrypt-smime (&optional dontsign)
250   "Add MML tag to encrypt and sign the entire message.
251 If called with a prefix argument, only encrypt (do NOT sign)."
252   (interactive "P")
253   (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
254
255 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
256   "Add MML tag to encrypt and sign the entire message.
257 If called with a prefix argument, only encrypt (do NOT sign)."
258   (interactive "P")
259   (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
260
261 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
262   "Add MML tag to encrypt and sign the entire message.
263 If called with a prefix argument, only encrypt (do NOT sign)."
264   (interactive "P")
265   (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
266
267 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
268   "Add MML tag to encrypt and sign the entire message.
269 If called with a prefix argument, only encrypt (do NOT sign)."
270   (interactive "P")
271   (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
272
273 (provide 'mml-sec)
274
275 ;;; mml-sec.el ends here