(mml-signencrypt-style): Docstring to font-lock
[gnus] / lisp / mml-sec.el
1 ;;; mml-sec.el --- A package with security functions for MML documents
2 ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; This file is not part of GNU Emacs, but the same permissions apply.
6
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'mml2015)
27 (require 'mml1991)
28 (require 'mml-smime)
29 (eval-when-compile (require 'cl))
30
31 (defvar mml-sign-alist
32   '(("smime"     mml-smime-sign-buffer     mml-smime-sign-query)
33     ("pgp"       mml-pgp-sign-buffer       list)
34     ("pgpmime"   mml-pgpmime-sign-buffer   list))
35   "Alist of MIME signer functions.")
36
37 (defvar mml-default-sign-method (caar mml-sign-alist)
38   "Default sign method.")
39
40 (defvar mml-encrypt-alist
41   '(("smime"     mml-smime-encrypt-buffer     mml-smime-encrypt-query)
42     ("pgp"       mml-pgp-encrypt-buffer       list)
43     ("pgpmime"   mml-pgpmime-encrypt-buffer   list))
44   "Alist of MIME encryption functions.")
45
46 (defvar mml-default-encrypt-method (caar mml-encrypt-alist)
47   "Default encryption method.")
48
49 (defvar mml-signencrypt-style-alist
50   '(("smime"   separate)
51     ("pgp"     separate)
52     ("pgpmime" separate))
53   "Alist specifying whether or not a single sign & encrypt
54 operation should be perfomed when requesting signencrypt.
55 Note that combined sign & encrypt is NOT supported by pgp v2!
56 Also note that you should access this with mml-signencrypt-style")
57
58 ;;; Configuration/helper functions
59
60 (defun mml-signencrypt-style (method &optional style)
61   "Function for setting/getting the signencrypt-style used.  Takes two
62 arguments, the method (e.g. \"pgp\") and optionally the mode
63 \(e.g. combined).  If the mode is omitted, the current value is returned.
64
65 For example, if you prefer to use combined sign & encrypt with
66 smime, putting the following in your Gnus startup file will
67 enable that behavior:
68
69 \(mml-set-signencrypt-style \"smime\" combined)"
70   (let ((style-item (assoc method mml-signencrypt-style-alist)))
71     (if style-item
72         (if (or (eq style 'separate)
73                 (eq style 'combined))
74             ;; valid style setting?
75             (setf (second style-item) style)
76           ;; otherwise, just return the current value
77           (second style-item))
78       (gnus-message 3 "Warning, attempt to set invalid signencrypt-style"))))
79
80 ;;; Security functions
81
82 (defun mml-smime-sign-buffer (cont)
83   (or (mml-smime-sign cont)
84       (error "Signing failed... inspect message logs for errors")))
85
86 (defun mml-smime-encrypt-buffer (cont)
87   (or (mml-smime-encrypt cont)
88       (error "Encryption failed... inspect message logs for errors")))
89
90 (defun mml-pgp-sign-buffer (cont)
91   (or (mml1991-sign cont)
92       (error "Signing failed... inspect message logs for errors")))
93
94 (defun mml-pgp-encrypt-buffer (cont)
95   (or (mml1991-encrypt cont)
96       (error "Encryption failed... inspect message logs for errors")))
97
98 (defun mml-pgpmime-sign-buffer (cont)
99   (or (mml2015-sign cont)
100       (error "Signing failed... inspect message logs for errors")))
101
102 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
103   (or (mml2015-encrypt cont sign)
104       (error "Encryption failed... inspect message logs for errors")))
105
106 (defun mml-secure-part (method &optional sign)
107   (save-excursion
108     (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
109                                                 mml-encrypt-alist))))))
110       (cond ((re-search-backward
111               "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
112              (goto-char (match-end 0))
113              (insert (if sign " sign=" " encrypt=") method)
114              (while tags
115                (let ((key (pop tags))
116                      (value (pop tags)))
117                  (when value
118                    ;; Quote VALUE if it contains suspicious characters.
119                    (when (string-match "[\"'\\~/*;() \t\n]" value)
120                      (setq value (prin1-to-string value)))
121                    (insert (format " %s=%s" key value))))))
122             ((or (re-search-backward
123                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
124                  (re-search-forward
125                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
126              (goto-char (match-end 0))
127              (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
128                                                 (cons method tags))))
129             (t (error "The message is corrupted. No mail header separator"))))))
130
131 (defun mml-secure-sign-pgp ()
132   "Add MML tags to PGP sign this MML part."
133   (interactive)
134   (mml-secure-part "pgp" 'sign))
135
136 (defun mml-secure-sign-pgpmime ()
137   "Add MML tags to PGP/MIME sign this MML part."
138   (interactive)
139   (mml-secure-part "pgpmime" 'sign))
140
141 (defun mml-secure-sign-smime ()
142   "Add MML tags to S/MIME sign this MML part."
143   (interactive)
144   (mml-secure-part "smime" 'sign))
145
146 (defun mml-secure-encrypt-pgp ()
147   "Add MML tags to PGP encrypt this MML part."
148   (interactive)
149   (mml-secure-part "pgp"))
150
151 (defun mml-secure-encrypt-pgpmime ()
152   "Add MML tags to PGP/MIME encrypt this MML part."
153   (interactive)
154   (mml-secure-part "pgpmime"))
155
156 (defun mml-secure-encrypt-smime ()
157   "Add MML tags to S/MIME encrypt this MML part."
158   (interactive)
159   (mml-secure-part "smime"))
160
161 ;; defuns that add the proper <#secure ...> tag to the top of the message body
162 (defun mml-secure-message (method &optional modesym)
163   (let ((mode (prin1-to-string modesym))
164         insert-loc)
165     (mml-unsecure-message)
166     (save-excursion
167       (goto-char (point-min))
168       (cond ((re-search-forward
169               (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
170              (goto-char (setq insert-loc (match-end 0)))
171              (unless (looking-at "<#secure")
172                (mml-insert-tag
173                 'secure 'method method 'mode mode)))
174             (t (error
175                 "The message is corrupted. No mail header separator"))))
176     (when (eql insert-loc (point))
177       (forward-line 1))))
178
179 (defun mml-unsecure-message ()
180   "Remove security related MML tags from message."
181   (interactive)
182   (save-excursion
183     (goto-char (point-max))
184     (when (re-search-backward "^<#secure.*>\n" nil t)
185       (kill-region (match-beginning 0) (match-end 0)))))
186
187 (defun mml-secure-message-sign-smime ()
188   "Add MML tag to encrypt/sign the entire message."
189   (interactive)
190   (mml-secure-message "smime" 'sign))
191
192 (defun mml-secure-message-sign-pgp ()
193   "Add MML tag to encrypt/sign the entire message."
194   (interactive)
195   (mml-secure-message "pgp" 'sign))
196
197 (defun mml-secure-message-sign-pgpmime ()
198   "Add MML tag to encrypt/sign the entire message."
199   (interactive)
200   (mml-secure-message "pgpmime" 'sign))
201
202 (defun mml-secure-message-encrypt-smime (&optional dontsign)
203   "Add MML tag to encrypt and sign the entire message.
204 If called with a prefix argument, only encrypt (do NOT sign)."
205   (interactive "P")
206   (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
207
208 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
209   "Add MML tag to encrypt and sign the entire message.
210 If called with a prefix argument, only encrypt (do NOT sign)."
211   (interactive "P")
212   (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
213
214 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
215   "Add MML tag to encrypt and sign the entire message.
216 If called with a prefix argument, only encrypt (do NOT sign)."
217   (interactive "P")
218   (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
219
220 (provide 'mml-sec)
221
222 ;;; mml-sec.el ends here