1 ;;; mml-sec.el --- A package with security functions for MML documents
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 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 2, 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))
32 (autoload 'mml2015-sign "mml2015")
33 (autoload 'mml2015-encrypt "mml2015")
34 (autoload 'mml1991-sign "mml1991")
35 (autoload 'mml1991-encrypt "mml1991")
36 (autoload 'message-goto-body "message")
37 (autoload 'mml-insert-tag "mml")
39 (defvar mml-sign-alist
40 '(("smime" mml-smime-sign-buffer mml-smime-sign-query)
41 ("pgp" mml-pgp-sign-buffer list)
42 ("pgpauto" mml-pgpauto-sign-buffer list)
43 ("pgpmime" mml-pgpmime-sign-buffer list))
44 "Alist of MIME signer functions.")
46 (defcustom mml-default-sign-method "pgpmime"
48 The string must have an entry in `mml-sign-alist'."
50 :type '(choice (const "smime")
57 (defvar mml-encrypt-alist
58 '(("smime" mml-smime-encrypt-buffer mml-smime-encrypt-query)
59 ("pgp" mml-pgp-encrypt-buffer list)
60 ("pgpauto" mml-pgpauto-sign-buffer list)
61 ("pgpmime" mml-pgpmime-encrypt-buffer list))
62 "Alist of MIME encryption functions.")
64 (defcustom mml-default-encrypt-method "pgpmime"
65 "Default encryption method.
66 The string must have an entry in `mml-encrypt-alist'."
68 :type '(choice (const "smime")
75 (defcustom mml-signencrypt-style-alist
80 "Alist specifying if `signencrypt' results in two separate operations or not.
81 The first entry indicates the MML security type, valid entries include
82 the strings \"smime\", \"pgp\", and \"pgpmime\". The second entry is
83 a symbol `separate' or `combined' where `separate' means that MML signs
84 and encrypt messages in a two step process, and `combined' means that MML
85 signs and encrypt the message in one step.
87 Note that the output generated by using a `combined' mode is NOT
88 understood by all PGP implementations, in particular PGP version
89 2 does not support it! See Info node `(message)Security' for
93 :type '(repeat (list (choice (const :tag "S/MIME" "smime")
94 (const :tag "PGP" "pgp")
95 (const :tag "PGP/MIME" "pgpmime")
96 (string :tag "User defined"))
97 (choice (const :tag "Separate" separate)
98 (const :tag "Combined" combined)))))
100 (defcustom mml-secure-verbose nil
101 "If non-nil, ask the user about the current operation more verbosely."
105 (defcustom mml-secure-cache-passphrase password-cache
106 "If t, cache passphrase."
110 (defcustom mml-secure-passphrase-cache-expiry password-cache-expiry
111 "How many seconds the passphrase is cached.
112 Whether the passphrase is cached at all is controlled by
113 `mml-secure-cache-passphrase'."
117 ;;; Configuration/helper functions
119 (defun mml-signencrypt-style (method &optional style)
120 "Function for setting/getting the signencrypt-style used. Takes two
121 arguments, the method (e.g. \"pgp\") and optionally the mode
122 \(e.g. combined). If the mode is omitted, the current value is returned.
124 For example, if you prefer to use combined sign & encrypt with
125 smime, putting the following in your Gnus startup file will
126 enable that behavior:
128 \(mml-set-signencrypt-style \"smime\" combined)
130 You can also customize or set `mml-signencrypt-style-alist' instead."
131 (let ((style-item (assoc method mml-signencrypt-style-alist)))
133 (if (or (eq style 'separate)
134 (eq style 'combined))
135 ;; valid style setting?
136 (setf (second style-item) style)
137 ;; otherwise, just return the current value
139 (message "Warning, attempt to set invalid signencrypt style"))))
141 ;;; Security functions
143 (defun mml-smime-sign-buffer (cont)
144 (or (mml-smime-sign cont)
145 (error "Signing failed... inspect message logs for errors")))
147 (defun mml-smime-encrypt-buffer (cont &optional sign)
149 (message "Combined sign and encrypt S/MIME not support yet")
151 (or (mml-smime-encrypt cont)
152 (error "Encryption failed... inspect message logs for errors")))
154 (defun mml-pgp-sign-buffer (cont)
155 (or (mml1991-sign cont)
156 (error "Signing failed... inspect message logs for errors")))
158 (defun mml-pgp-encrypt-buffer (cont &optional sign)
159 (or (mml1991-encrypt cont sign)
160 (error "Encryption failed... inspect message logs for errors")))
162 (defun mml-pgpmime-sign-buffer (cont)
163 (or (mml2015-sign cont)
164 (error "Signing failed... inspect message logs for errors")))
166 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
167 (or (mml2015-encrypt cont sign)
168 (error "Encryption failed... inspect message logs for errors")))
170 (defun mml-pgpauto-sign-buffer (cont)
172 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
175 (error "Encryption failed... inspect message logs for errors")))
177 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
179 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
180 (mml2015-encrypt cont sign)
181 (mml1991-encrypt cont sign))
182 (error "Encryption failed... inspect message logs for errors")))
184 (defun mml-secure-part (method &optional sign)
186 (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
187 mml-encrypt-alist))))))
188 (cond ((re-search-backward
189 "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
190 (goto-char (match-end 0))
191 (insert (if sign " sign=" " encrypt=") method)
193 (let ((key (pop tags))
196 ;; Quote VALUE if it contains suspicious characters.
197 (when (string-match "[\"'\\~/*;() \t\n]" value)
198 (setq value (prin1-to-string value)))
199 (insert (format " %s=%s" key value))))))
200 ((or (re-search-backward
201 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
203 (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
204 (goto-char (match-end 0))
205 (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
206 (cons method tags))))
207 (t (error "The message is corrupted. No mail header separator"))))))
209 (defvar mml-secure-method
210 (if (equal mml-default-encrypt-method mml-default-sign-method)
211 mml-default-sign-method
213 "Current security method. Internal variable.")
215 (defun mml-secure-sign (&optional method)
216 "Add MML tags to sign this MML part.
217 Use METHOD if given. Else use `mml-secure-method' or
218 `mml-default-sign-method'."
221 (or method mml-secure-method mml-default-sign-method)
224 (defun mml-secure-encrypt (&optional method)
225 "Add MML tags to encrypt this MML part.
226 Use METHOD if given. Else use `mml-secure-method' or
227 `mml-default-sign-method'."
230 (or method mml-secure-method mml-default-sign-method)))
232 (defun mml-secure-sign-pgp ()
233 "Add MML tags to PGP sign this MML part."
235 (mml-secure-part "pgp" 'sign))
237 (defun mml-secure-sign-pgpauto ()
238 "Add MML tags to PGP-auto sign this MML part."
240 (mml-secure-part "pgpauto" 'sign))
242 (defun mml-secure-sign-pgpmime ()
243 "Add MML tags to PGP/MIME sign this MML part."
245 (mml-secure-part "pgpmime" 'sign))
247 (defun mml-secure-sign-smime ()
248 "Add MML tags to S/MIME sign this MML part."
250 (mml-secure-part "smime" 'sign))
252 (defun mml-secure-encrypt-pgp ()
253 "Add MML tags to PGP encrypt this MML part."
255 (mml-secure-part "pgp"))
257 (defun mml-secure-encrypt-pgpmime ()
258 "Add MML tags to PGP/MIME encrypt this MML part."
260 (mml-secure-part "pgpmime"))
262 (defun mml-secure-encrypt-smime ()
263 "Add MML tags to S/MIME encrypt this MML part."
265 (mml-secure-part "smime"))
267 ;; defuns that add the proper <#secure ...> tag to the top of the message body
268 (defun mml-secure-message (method &optional modesym)
269 (let ((mode (prin1-to-string modesym))
271 (if (or (eq modesym 'sign)
272 (eq modesym 'signencrypt))
273 (funcall (nth 2 (assoc method mml-sign-alist))))
274 (if (or (eq modesym 'encrypt)
275 (eq modesym 'signencrypt))
276 (funcall (nth 2 (assoc method mml-encrypt-alist))))))
278 (mml-unsecure-message)
280 (goto-char (point-min))
281 (cond ((re-search-forward
282 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
283 (goto-char (setq insert-loc (match-end 0)))
284 (unless (looking-at "<#secure")
285 (apply 'mml-insert-tag
286 'secure 'method method 'mode mode tags)))
288 "The message is corrupted. No mail header separator"))))
289 (when (eql insert-loc (point))
292 (defun mml-unsecure-message ()
293 "Remove security related MML tags from message."
296 (goto-char (point-max))
297 (when (re-search-backward "^<#secure.*>\n" nil t)
298 (delete-region (match-beginning 0) (match-end 0)))))
301 (defun mml-secure-message-sign (&optional method)
302 "Add MML tags to sign this MML part.
303 Use METHOD if given. Else use `mml-secure-method' or
304 `mml-default-sign-method'."
307 (or method mml-secure-method mml-default-sign-method)
310 (defun mml-secure-message-sign-encrypt (&optional method)
311 "Add MML tag to sign and encrypt the entire message.
312 Use METHOD if given. Else use `mml-secure-method' or
313 `mml-default-sign-method'."
316 (or method mml-secure-method mml-default-sign-method)
319 (defun mml-secure-message-encrypt (&optional method)
320 "Add MML tag to encrypt the entire message.
321 Use METHOD if given. Else use `mml-secure-method' or
322 `mml-default-sign-method'."
325 (or method mml-secure-method mml-default-sign-method)
328 (defun mml-secure-message-sign-smime ()
329 "Add MML tag to encrypt/sign the entire message."
331 (mml-secure-message "smime" 'sign))
333 (defun mml-secure-message-sign-pgp ()
334 "Add MML tag to encrypt/sign the entire message."
336 (mml-secure-message "pgp" 'sign))
338 (defun mml-secure-message-sign-pgpmime ()
339 "Add MML tag to encrypt/sign the entire message."
341 (mml-secure-message "pgpmime" 'sign))
343 (defun mml-secure-message-sign-pgpauto ()
344 "Add MML tag to encrypt/sign the entire message."
346 (mml-secure-message "pgpauto" 'sign))
348 (defun mml-secure-message-encrypt-smime (&optional dontsign)
349 "Add MML tag to encrypt and sign the entire message.
350 If called with a prefix argument, only encrypt (do NOT sign)."
352 (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
354 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
355 "Add MML tag to encrypt and sign the entire message.
356 If called with a prefix argument, only encrypt (do NOT sign)."
358 (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
360 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
361 "Add MML tag to encrypt and sign the entire message.
362 If called with a prefix argument, only encrypt (do NOT sign)."
364 (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
366 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
367 "Add MML tag to encrypt and sign the entire message.
368 If called with a prefix argument, only encrypt (do NOT sign)."
370 (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
374 ;;; arch-tag: 111c56e7-df5e-4287-87d7-93ed2911ec6c
375 ;;; mml-sec.el ends here