be4665b714063e3bfc2f735c8fa0860e9219d971
[gnus] / lisp / mml-sec.el
1 ;;; mml-sec.el --- A package with security functions for MML documents
2 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004 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 (defcustom mml-default-sign-method "pgpmime"
44   "Default sign method.
45 The string must have an entry in `mml-sign-alist'."
46   :type '(choice (const "smime")
47                  (const "pgp")
48                  (const "pgpauto")
49                  (const "pgpmime")
50                  string)
51   :group 'message)
52
53 (defvar mml-encrypt-alist
54   '(("smime"     mml-smime-encrypt-buffer     mml-smime-encrypt-query)
55     ("pgp"       mml-pgp-encrypt-buffer       list)
56     ("pgpauto"   mml-pgpauto-sign-buffer  list)
57     ("pgpmime"   mml-pgpmime-encrypt-buffer   list))
58   "Alist of MIME encryption functions.")
59
60 (defcustom mml-default-encrypt-method "pgpmime"
61   "Default encryption method.
62 The string must have an entry in `mml-encrypt-alist'."
63   :type '(choice (const "smime")
64                  (const "pgp")
65                  (const "pgpauto")
66                  (const "pgpmime")
67                  string)
68   :group 'message)
69
70 (defcustom mml-signencrypt-style-alist
71   '(("smime"   separate)
72     ("pgp"     combined)
73     ("pgpauto" combined)
74     ("pgpmime" combined))
75   "Alist specifying if `signencrypt' results in two separate operations or not.
76 The first entry indicates the MML security type, valid entries include
77 the strings \"smime\", \"pgp\", and \"pgpmime\".  The second entry is
78 a symbol `separate' or `combined' where `separate' means that MML signs
79 and encrypt messages in a two step process, and `combined' means that MML
80 signs and encrypt the message in one step.
81
82 Note that the output generated by using a `combined' mode is NOT
83 understood by all PGP implementations, in particular PGP version
84 2 does not support it!  See Info node `(message)Security' for
85 details."
86   :type '(repeat (list (choice (const :tag "S/MIME" "smime")
87                                (const :tag "PGP" "pgp")
88                                (const :tag "PGP/MIME" "pgpmime")
89                                (string :tag "User defined"))
90                        (choice (const :tag "Separate" separate)
91                                (const :tag "Combined" combined)))))
92
93 ;;; Configuration/helper functions
94
95 (defun mml-signencrypt-style (method &optional style)
96   "Function for setting/getting the signencrypt-style used.  Takes two
97 arguments, the method (e.g. \"pgp\") and optionally the mode
98 \(e.g. combined).  If the mode is omitted, the current value is returned.
99
100 For example, if you prefer to use combined sign & encrypt with
101 smime, putting the following in your Gnus startup file will
102 enable that behavior:
103
104 \(mml-set-signencrypt-style \"smime\" combined)
105
106 You can also customize or set `mml-signencrypt-style-alist' instead."
107   (let ((style-item (assoc method mml-signencrypt-style-alist)))
108     (if style-item
109         (if (or (eq style 'separate)
110                 (eq style 'combined))
111             ;; valid style setting?
112             (setf (second style-item) style)
113           ;; otherwise, just return the current value
114           (second style-item))
115       (message "Warning, attempt to set invalid signencrypt style"))))
116
117 ;;; Security functions
118
119 (defun mml-smime-sign-buffer (cont)
120   (or (mml-smime-sign cont)
121       (error "Signing failed... inspect message logs for errors")))
122
123 (defun mml-smime-encrypt-buffer (cont &optional sign)
124   (when sign
125     (message "Combined sign and encrypt S/MIME not support yet")
126     (sit-for 1))
127   (or (mml-smime-encrypt cont)
128       (error "Encryption failed... inspect message logs for errors")))
129
130 (defun mml-pgp-sign-buffer (cont)
131   (or (mml1991-sign cont)
132       (error "Signing failed... inspect message logs for errors")))
133
134 (defun mml-pgp-encrypt-buffer (cont &optional sign)
135   (or (mml1991-encrypt cont sign)
136       (error "Encryption failed... inspect message logs for errors")))
137
138 (defun mml-pgpmime-sign-buffer (cont)
139   (or (mml2015-sign cont)
140       (error "Signing failed... inspect message logs for errors")))
141
142 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
143   (or (mml2015-encrypt cont sign)
144       (error "Encryption failed... inspect message logs for errors")))
145
146 (defun mml-pgpauto-sign-buffer (cont)
147   (message-goto-body)
148   (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
149           (mml2015-sign cont)
150         (mml1991-sign cont))
151       (error "Encryption failed... inspect message logs for errors")))
152
153 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
154   (message-goto-body)
155   (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
156           (mml2015-encrypt cont sign)
157         (mml1991-encrypt cont sign))
158       (error "Encryption failed... inspect message logs for errors")))
159
160 (defun mml-secure-part (method &optional sign)
161   (save-excursion
162     (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
163                                                 mml-encrypt-alist))))))
164       (cond ((re-search-backward
165               "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
166              (goto-char (match-end 0))
167              (insert (if sign " sign=" " encrypt=") method)
168              (while tags
169                (let ((key (pop tags))
170                      (value (pop tags)))
171                  (when value
172                    ;; Quote VALUE if it contains suspicious characters.
173                    (when (string-match "[\"'\\~/*;() \t\n]" value)
174                      (setq value (prin1-to-string value)))
175                    (insert (format " %s=%s" key value))))))
176             ((or (re-search-backward
177                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
178                  (re-search-forward
179                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
180              (goto-char (match-end 0))
181              (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
182                                                 (cons method tags))))
183             (t (error "The message is corrupted. No mail header separator"))))))
184
185 (defun mml-secure-sign-pgp ()
186   "Add MML tags to PGP sign this MML part."
187   (interactive)
188   (mml-secure-part "pgp" 'sign))
189
190 (defun mml-secure-sign-pgpauto ()
191   "Add MML tags to PGP-auto sign this MML part."
192   (interactive)
193   (mml-secure-part "pgpauto" 'sign))
194
195 (defun mml-secure-sign-pgpmime ()
196   "Add MML tags to PGP/MIME sign this MML part."
197   (interactive)
198   (mml-secure-part "pgpmime" 'sign))
199
200 (defun mml-secure-sign-smime ()
201   "Add MML tags to S/MIME sign this MML part."
202   (interactive)
203   (mml-secure-part "smime" 'sign))
204
205 (defun mml-secure-encrypt-pgp ()
206   "Add MML tags to PGP encrypt this MML part."
207   (interactive)
208   (mml-secure-part "pgp"))
209
210 (defun mml-secure-encrypt-pgpmime ()
211   "Add MML tags to PGP/MIME encrypt this MML part."
212   (interactive)
213   (mml-secure-part "pgpmime"))
214
215 (defun mml-secure-encrypt-smime ()
216   "Add MML tags to S/MIME encrypt this MML part."
217   (interactive)
218   (mml-secure-part "smime"))
219
220 ;; defuns that add the proper <#secure ...> tag to the top of the message body
221 (defun mml-secure-message (method &optional modesym)
222   (let ((mode (prin1-to-string modesym))
223         insert-loc)
224     (mml-unsecure-message)
225     (save-excursion
226       (goto-char (point-min))
227       (cond ((re-search-forward
228               (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
229              (goto-char (setq insert-loc (match-end 0)))
230              (unless (looking-at "<#secure")
231                (mml-insert-tag
232                 'secure 'method method 'mode mode)))
233             (t (error
234                 "The message is corrupted. No mail header separator"))))
235     (when (eql insert-loc (point))
236       (forward-line 1))))
237
238 (defun mml-unsecure-message ()
239   "Remove security related MML tags from message."
240   (interactive)
241   (save-excursion
242     (goto-char (point-max))
243     (when (re-search-backward "^<#secure.*>\n" nil t)
244       (delete-region (match-beginning 0) (match-end 0)))))
245
246 (defun mml-secure-message-sign-smime ()
247   "Add MML tag to encrypt/sign the entire message."
248   (interactive)
249   (mml-secure-message "smime" 'sign))
250
251 (defun mml-secure-message-sign-pgp ()
252   "Add MML tag to encrypt/sign the entire message."
253   (interactive)
254   (mml-secure-message "pgp" 'sign))
255
256 (defun mml-secure-message-sign-pgpmime ()
257   "Add MML tag to encrypt/sign the entire message."
258   (interactive)
259   (mml-secure-message "pgpmime" 'sign))
260
261 (defun mml-secure-message-sign-pgpauto ()
262   "Add MML tag to encrypt/sign the entire message."
263   (interactive)
264   (mml-secure-message "pgpauto" 'sign))
265
266 (defun mml-secure-message-encrypt-smime (&optional dontsign)
267   "Add MML tag to encrypt and sign the entire message.
268 If called with a prefix argument, only encrypt (do NOT sign)."
269   (interactive "P")
270   (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
271
272 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
273   "Add MML tag to encrypt and sign the entire message.
274 If called with a prefix argument, only encrypt (do NOT sign)."
275   (interactive "P")
276   (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
277
278 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
279   "Add MML tag to encrypt and sign the entire message.
280 If called with a prefix argument, only encrypt (do NOT sign)."
281   (interactive "P")
282   (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
283
284 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
285   "Add MML tag to encrypt and sign the entire message.
286 If called with a prefix argument, only encrypt (do NOT sign)."
287   (interactive "P")
288   (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
289
290 (provide 'mml-sec)
291
292 ;;; mml-sec.el ends here