1 ;;; mml-sec.el --- A package with security functions for MML documents
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
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 3, or (at your option)
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.
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.
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")
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.")
45 (defcustom mml-default-sign-method
"pgpmime"
47 The string must have an entry in `mml-sign-alist'."
49 :type
'(choice (const "smime")
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.")
63 (defcustom mml-default-encrypt-method
"pgpmime"
64 "Default encryption method.
65 The string must have an entry in `mml-encrypt-alist'."
67 :type
'(choice (const "smime")
74 (defcustom mml-signencrypt-style-alist
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.
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
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
)))))
99 ;;; Configuration/helper functions
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.
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:
110 \(mml-set-signencrypt-style \"smime\" combined)
112 You can also customize or set `mml-signencrypt-style-alist' instead."
113 (let ((style-item (assoc method mml-signencrypt-style-alist
)))
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
121 (message "Warning, attempt to set invalid signencrypt style"))))
123 ;;; Security functions
125 (defun mml-smime-sign-buffer (cont)
126 (or (mml-smime-sign cont
)
127 (error "Signing failed... inspect message logs for errors")))
129 (defun mml-smime-encrypt-buffer (cont &optional sign
)
131 (message "Combined sign and encrypt S/MIME not support yet")
133 (or (mml-smime-encrypt cont
)
134 (error "Encryption failed... inspect message logs for errors")))
136 (defun mml-pgp-sign-buffer (cont)
137 (or (mml1991-sign cont
)
138 (error "Signing failed... inspect message logs for errors")))
140 (defun mml-pgp-encrypt-buffer (cont &optional sign
)
141 (or (mml1991-encrypt cont sign
)
142 (error "Encryption failed... inspect message logs for errors")))
144 (defun mml-pgpmime-sign-buffer (cont)
145 (or (mml2015-sign cont
)
146 (error "Signing failed... inspect message logs for errors")))
148 (defun mml-pgpmime-encrypt-buffer (cont &optional sign
)
149 (or (mml2015-encrypt cont sign
)
150 (error "Encryption failed... inspect message logs for errors")))
152 (defun mml-pgpauto-sign-buffer (cont)
154 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t
) ; there must be a better way...
157 (error "Encryption failed... inspect message logs for errors")))
159 (defun mml-pgpauto-encrypt-buffer (cont &optional sign
)
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")))
166 (defun mml-secure-part (method &optional sign
)
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
)
175 (let ((key (pop tags
))
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
)
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"))))))
191 (defvar mml-secure-method
192 (if (equal mml-default-encrypt-method mml-default-sign-method
)
193 mml-default-sign-method
195 "Current security method. Internal variable.")
197 (defun mml-secure-sign (&optional method
)
198 "Add MML tags to sign this MML part.
199 Use METHOD if given. Else use `mml-secure-method' or
200 `mml-default-sign-method'."
203 (or method mml-secure-method mml-default-sign-method
)
206 (defun mml-secure-encrypt (&optional method
)
207 "Add MML tags to encrypt this MML part.
208 Use METHOD if given. Else use `mml-secure-method' or
209 `mml-default-sign-method'."
212 (or method mml-secure-method mml-default-sign-method
)))
214 (defun mml-secure-sign-pgp ()
215 "Add MML tags to PGP sign this MML part."
217 (mml-secure-part "pgp" 'sign
))
219 (defun mml-secure-sign-pgpauto ()
220 "Add MML tags to PGP-auto sign this MML part."
222 (mml-secure-part "pgpauto" 'sign
))
224 (defun mml-secure-sign-pgpmime ()
225 "Add MML tags to PGP/MIME sign this MML part."
227 (mml-secure-part "pgpmime" 'sign
))
229 (defun mml-secure-sign-smime ()
230 "Add MML tags to S/MIME sign this MML part."
232 (mml-secure-part "smime" 'sign
))
234 (defun mml-secure-encrypt-pgp ()
235 "Add MML tags to PGP encrypt this MML part."
237 (mml-secure-part "pgp"))
239 (defun mml-secure-encrypt-pgpmime ()
240 "Add MML tags to PGP/MIME encrypt this MML part."
242 (mml-secure-part "pgpmime"))
244 (defun mml-secure-encrypt-smime ()
245 "Add MML tags to S/MIME encrypt this MML part."
247 (mml-secure-part "smime"))
249 ;; defuns that add the proper <#secure ...> tag to the top of the message body
250 (defun mml-secure-message (method &optional modesym
)
251 (let ((mode (prin1-to-string modesym
))
253 (mml-unsecure-message)
255 (goto-char (point-min))
256 (cond ((re-search-forward
257 (concat "^" (regexp-quote mail-header-separator
) "\n") nil t
)
258 (goto-char (setq insert-loc
(match-end 0)))
259 (unless (looking-at "<#secure")
261 'secure
'method method
'mode mode
)))
263 "The message is corrupted. No mail header separator"))))
264 (when (eql insert-loc
(point))
267 (defun mml-unsecure-message ()
268 "Remove security related MML tags from message."
271 (goto-char (point-max))
272 (when (re-search-backward "^<#secure.*>\n" nil t
)
273 (delete-region (match-beginning 0) (match-end 0)))))
276 (defun mml-secure-message-sign (&optional method
)
277 "Add MML tags to sign this MML part.
278 Use METHOD if given. Else use `mml-secure-method' or
279 `mml-default-sign-method'."
282 (or method mml-secure-method mml-default-sign-method
)
285 (defun mml-secure-message-sign-encrypt (&optional method
)
286 "Add MML tag to sign and encrypt the entire message.
287 Use METHOD if given. Else use `mml-secure-method' or
288 `mml-default-sign-method'."
291 (or method mml-secure-method mml-default-sign-method
)
294 (defun mml-secure-message-encrypt (&optional method
)
295 "Add MML tag to encrypt the entire message.
296 Use METHOD if given. Else use `mml-secure-method' or
297 `mml-default-sign-method'."
300 (or method mml-secure-method mml-default-sign-method
)
303 (defun mml-secure-message-sign-smime ()
304 "Add MML tag to encrypt/sign the entire message."
306 (mml-secure-message "smime" 'sign
))
308 (defun mml-secure-message-sign-pgp ()
309 "Add MML tag to encrypt/sign the entire message."
311 (mml-secure-message "pgp" 'sign
))
313 (defun mml-secure-message-sign-pgpmime ()
314 "Add MML tag to encrypt/sign the entire message."
316 (mml-secure-message "pgpmime" 'sign
))
318 (defun mml-secure-message-sign-pgpauto ()
319 "Add MML tag to encrypt/sign the entire message."
321 (mml-secure-message "pgpauto" 'sign
))
323 (defun mml-secure-message-encrypt-smime (&optional dontsign
)
324 "Add MML tag to encrypt and sign the entire message.
325 If called with a prefix argument, only encrypt (do NOT sign)."
327 (mml-secure-message "smime" (if dontsign
'encrypt
'signencrypt
)))
329 (defun mml-secure-message-encrypt-pgp (&optional dontsign
)
330 "Add MML tag to encrypt and sign the entire message.
331 If called with a prefix argument, only encrypt (do NOT sign)."
333 (mml-secure-message "pgp" (if dontsign
'encrypt
'signencrypt
)))
335 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign
)
336 "Add MML tag to encrypt and sign the entire message.
337 If called with a prefix argument, only encrypt (do NOT sign)."
339 (mml-secure-message "pgpmime" (if dontsign
'encrypt
'signencrypt
)))
341 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign
)
342 "Add MML tag to encrypt and sign the entire message.
343 If called with a prefix argument, only encrypt (do NOT sign)."
345 (mml-secure-message "pgpauto" (if dontsign
'encrypt
'signencrypt
)))
349 ;;; arch-tag: 111c56e7-df5e-4287-87d7-93ed2911ec6c
350 ;;; mml-sec.el ends here