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