1 ;;; mml-sec.el --- A package with security functions for MML documents
2 ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; This file is not part of GNU Emacs, but the same permissions apply.
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)
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.
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.
29 (eval-when-compile (require 'cl))
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.")
37 (defvar mml-default-sign-method (caar mml-sign-alist)
38 "Default sign method.")
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.")
46 (defvar mml-default-encrypt-method (caar mml-encrypt-alist)
47 "Default encryption method.")
49 (defvar mml-signencrypt-style-alist
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")
58 ;;; Configuration/helper functions
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.
65 For example, if you prefer to use combined sign & encrypt with
66 smime, putting the following in your Gnus startup file will
69 \(mml-set-signencrypt-style \"smime\" combined)"
70 (let ((style-item (assoc method mml-signencrypt-style-alist)))
72 (if (or (eq style 'separate)
74 ;; valid style setting?
75 (setf (second style-item) style)
76 ;; otherwise, just return the current value
78 (gnus-message 3 "Warning, attempt to set invalid signencrypt-style"))))
80 ;;; Security functions
82 (defun mml-smime-sign-buffer (cont)
83 (or (mml-smime-sign cont)
84 (error "Signing failed... inspect message logs for errors")))
86 (defun mml-smime-encrypt-buffer (cont)
87 (or (mml-smime-encrypt cont)
88 (error "Encryption failed... inspect message logs for errors")))
90 (defun mml-pgp-sign-buffer (cont)
91 (or (mml1991-sign cont)
92 (error "Signing failed... inspect message logs for errors")))
94 (defun mml-pgp-encrypt-buffer (cont)
95 (or (mml1991-encrypt cont)
96 (error "Encryption failed... inspect message logs for errors")))
98 (defun mml-pgpmime-sign-buffer (cont)
99 (or (mml2015-sign cont)
100 (error "Signing failed... inspect message logs for errors")))
102 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
103 (or (mml2015-encrypt cont sign)
104 (error "Encryption failed... inspect message logs for errors")))
106 (defun mml-secure-part (method &optional sign)
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)
115 (let ((key (pop tags))
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)
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"))))))
131 (defun mml-secure-sign-pgp ()
132 "Add MML tags to PGP sign this MML part."
134 (mml-secure-part "pgp" 'sign))
136 (defun mml-secure-sign-pgpmime ()
137 "Add MML tags to PGP/MIME sign this MML part."
139 (mml-secure-part "pgpmime" 'sign))
141 (defun mml-secure-sign-smime ()
142 "Add MML tags to S/MIME sign this MML part."
144 (mml-secure-part "smime" 'sign))
146 (defun mml-secure-encrypt-pgp ()
147 "Add MML tags to PGP encrypt this MML part."
149 (mml-secure-part "pgp"))
151 (defun mml-secure-encrypt-pgpmime ()
152 "Add MML tags to PGP/MIME encrypt this MML part."
154 (mml-secure-part "pgpmime"))
156 (defun mml-secure-encrypt-smime ()
157 "Add MML tags to S/MIME encrypt this MML part."
159 (mml-secure-part "smime"))
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))
165 (mml-unsecure-message)
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")
173 'secure 'method method 'mode mode)))
175 "The message is corrupted. No mail header separator"))))
176 (when (eql insert-loc (point))
179 (defun mml-unsecure-message ()
180 "Remove security related MML tags from message."
183 (goto-char (point-max))
184 (when (re-search-backward "^<#secure.*>\n" nil t)
185 (kill-region (match-beginning 0) (match-end 0)))))
187 (defun mml-secure-message-sign-smime ()
188 "Add MML tag to encrypt/sign the entire message."
190 (mml-secure-message "smime" 'sign))
192 (defun mml-secure-message-sign-pgp ()
193 "Add MML tag to encrypt/sign the entire message."
195 (mml-secure-message "pgp" 'sign))
197 (defun mml-secure-message-sign-pgpmime ()
198 "Add MML tag to encrypt/sign the entire message."
200 (mml-secure-message "pgpmime" 'sign))
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)."
206 (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
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)."
212 (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
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)."
218 (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
222 ;;; mml-sec.el ends here