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