Revision: miles@gnu.org--gnu-2004/gnus--devo--0--patch-99
[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   :group 'message
87   :type '(repeat (list (choice (const :tag "S/MIME" "smime")
88                                (const :tag "PGP" "pgp")
89                                (const :tag "PGP/MIME" "pgpmime")
90                                (string :tag "User defined"))
91                        (choice (const :tag "Separate" separate)
92                                (const :tag "Combined" combined)))))
93
94 ;;; Configuration/helper functions
95
96 (defun mml-signencrypt-style (method &optional style)
97   "Function for setting/getting the signencrypt-style used.  Takes two
98 arguments, the method (e.g. \"pgp\") and optionally the mode
99 \(e.g. combined).  If the mode is omitted, the current value is returned.
100
101 For example, if you prefer to use combined sign & encrypt with
102 smime, putting the following in your Gnus startup file will
103 enable that behavior:
104
105 \(mml-set-signencrypt-style \"smime\" combined)
106
107 You can also customize or set `mml-signencrypt-style-alist' instead."
108   (let ((style-item (assoc method mml-signencrypt-style-alist)))
109     (if style-item
110         (if (or (eq style 'separate)
111                 (eq style 'combined))
112             ;; valid style setting?
113             (setf (second style-item) style)
114           ;; otherwise, just return the current value
115           (second style-item))
116       (message "Warning, attempt to set invalid signencrypt style"))))
117
118 ;;; Security functions
119
120 (defun mml-smime-sign-buffer (cont)
121   (or (mml-smime-sign cont)
122       (error "Signing failed... inspect message logs for errors")))
123
124 (defun mml-smime-encrypt-buffer (cont &optional sign)
125   (when sign
126     (message "Combined sign and encrypt S/MIME not support yet")
127     (sit-for 1))
128   (or (mml-smime-encrypt cont)
129       (error "Encryption failed... inspect message logs for errors")))
130
131 (defun mml-pgp-sign-buffer (cont)
132   (or (mml1991-sign cont)
133       (error "Signing failed... inspect message logs for errors")))
134
135 (defun mml-pgp-encrypt-buffer (cont &optional sign)
136   (or (mml1991-encrypt cont sign)
137       (error "Encryption failed... inspect message logs for errors")))
138
139 (defun mml-pgpmime-sign-buffer (cont)
140   (or (mml2015-sign cont)
141       (error "Signing failed... inspect message logs for errors")))
142
143 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
144   (or (mml2015-encrypt cont sign)
145       (error "Encryption failed... inspect message logs for errors")))
146
147 (defun mml-pgpauto-sign-buffer (cont)
148   (message-goto-body)
149   (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
150           (mml2015-sign cont)
151         (mml1991-sign cont))
152       (error "Encryption failed... inspect message logs for errors")))
153
154 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
155   (message-goto-body)
156   (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
157           (mml2015-encrypt cont sign)
158         (mml1991-encrypt cont sign))
159       (error "Encryption failed... inspect message logs for errors")))
160
161 (defun mml-secure-part (method &optional sign)
162   (save-excursion
163     (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
164                                                 mml-encrypt-alist))))))
165       (cond ((re-search-backward
166               "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
167              (goto-char (match-end 0))
168              (insert (if sign " sign=" " encrypt=") method)
169              (while tags
170                (let ((key (pop tags))
171                      (value (pop tags)))
172                  (when value
173                    ;; Quote VALUE if it contains suspicious characters.
174                    (when (string-match "[\"'\\~/*;() \t\n]" value)
175                      (setq value (prin1-to-string value)))
176                    (insert (format " %s=%s" key value))))))
177             ((or (re-search-backward
178                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
179                  (re-search-forward
180                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
181              (goto-char (match-end 0))
182              (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
183                                                 (cons method tags))))
184             (t (error "The message is corrupted. No mail header separator"))))))
185
186 (defun mml-secure-sign-pgp ()
187   "Add MML tags to PGP sign this MML part."
188   (interactive)
189   (mml-secure-part "pgp" 'sign))
190
191 (defun mml-secure-sign-pgpauto ()
192   "Add MML tags to PGP-auto sign this MML part."
193   (interactive)
194   (mml-secure-part "pgpauto" 'sign))
195
196 (defun mml-secure-sign-pgpmime ()
197   "Add MML tags to PGP/MIME sign this MML part."
198   (interactive)
199   (mml-secure-part "pgpmime" 'sign))
200
201 (defun mml-secure-sign-smime ()
202   "Add MML tags to S/MIME sign this MML part."
203   (interactive)
204   (mml-secure-part "smime" 'sign))
205
206 (defun mml-secure-encrypt-pgp ()
207   "Add MML tags to PGP encrypt this MML part."
208   (interactive)
209   (mml-secure-part "pgp"))
210
211 (defun mml-secure-encrypt-pgpmime ()
212   "Add MML tags to PGP/MIME encrypt this MML part."
213   (interactive)
214   (mml-secure-part "pgpmime"))
215
216 (defun mml-secure-encrypt-smime ()
217   "Add MML tags to S/MIME encrypt this MML part."
218   (interactive)
219   (mml-secure-part "smime"))
220
221 ;; defuns that add the proper <#secure ...> tag to the top of the message body
222 (defun mml-secure-message (method &optional modesym)
223   (let ((mode (prin1-to-string modesym))
224         insert-loc)
225     (mml-unsecure-message)
226     (save-excursion
227       (goto-char (point-min))
228       (cond ((re-search-forward
229               (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
230              (goto-char (setq insert-loc (match-end 0)))
231              (unless (looking-at "<#secure")
232                (mml-insert-tag
233                 'secure 'method method 'mode mode)))
234             (t (error
235                 "The message is corrupted. No mail header separator"))))
236     (when (eql insert-loc (point))
237       (forward-line 1))))
238
239 (defun mml-unsecure-message ()
240   "Remove security related MML tags from message."
241   (interactive)
242   (save-excursion
243     (goto-char (point-max))
244     (when (re-search-backward "^<#secure.*>\n" nil t)
245       (delete-region (match-beginning 0) (match-end 0)))))
246
247 (defun mml-secure-message-sign-smime ()
248   "Add MML tag to encrypt/sign the entire message."
249   (interactive)
250   (mml-secure-message "smime" 'sign))
251
252 (defun mml-secure-message-sign-pgp ()
253   "Add MML tag to encrypt/sign the entire message."
254   (interactive)
255   (mml-secure-message "pgp" 'sign))
256
257 (defun mml-secure-message-sign-pgpmime ()
258   "Add MML tag to encrypt/sign the entire message."
259   (interactive)
260   (mml-secure-message "pgpmime" 'sign))
261
262 (defun mml-secure-message-sign-pgpauto ()
263   "Add MML tag to encrypt/sign the entire message."
264   (interactive)
265   (mml-secure-message "pgpauto" 'sign))
266
267 (defun mml-secure-message-encrypt-smime (&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 "smime" (if dontsign 'encrypt 'signencrypt)))
272
273 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
274   "Add MML tag to encrypt and sign the entire message.
275 If called with a prefix argument, only encrypt (do NOT sign)."
276   (interactive "P")
277   (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
278
279 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
280   "Add MML tag to encrypt and sign the entire message.
281 If called with a prefix argument, only encrypt (do NOT sign)."
282   (interactive "P")
283   (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
284
285 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
286   "Add MML tag to encrypt and sign the entire message.
287 If called with a prefix argument, only encrypt (do NOT sign)."
288   (interactive "P")
289   (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
290
291 (provide 'mml-sec)
292
293 ;;; arch-tag: 111c56e7-df5e-4287-87d7-93ed2911ec6c
294 ;;; mml-sec.el ends here