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
53 "Alist specifying whether or not a single sign & encrypt
54 operation should be perfomed when requesting signencrypt.")
56 ;;; Security functions
58 (defun mml-smime-sign-buffer (cont)
59 (or (mml-smime-sign cont)
60 (error "Signing failed... inspect message logs for errors")))
62 (defun mml-smime-encrypt-buffer (cont)
63 (or (mml-smime-encrypt cont)
64 (error "Encryption failed... inspect message logs for errors")))
66 (defun mml-pgp-sign-buffer (cont)
67 (or (mml1991-sign cont)
68 (error "Signing failed... inspect message logs for errors")))
70 (defun mml-pgp-encrypt-buffer (cont)
71 (or (mml1991-encrypt cont)
72 (error "Encryption failed... inspect message logs for errors")))
74 (defun mml-pgpmime-sign-buffer (cont)
75 (or (mml2015-sign cont)
76 (error "Signing failed... inspect message logs for errors")))
78 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
79 (or (mml2015-encrypt cont sign)
80 (error "Encryption failed... inspect message logs for errors")))
82 (defun mml-secure-part (method &optional sign)
84 (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
85 mml-encrypt-alist))))))
86 (cond ((re-search-backward
87 "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
88 (goto-char (match-end 0))
89 (insert (if sign " sign=" " encrypt=") method)
91 (let ((key (pop tags))
94 ;; Quote VALUE if it contains suspicious characters.
95 (when (string-match "[\"'\\~/*;() \t\n]" value)
96 (setq value (prin1-to-string value)))
97 (insert (format " %s=%s" key value))))))
98 ((or (re-search-backward
99 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
101 (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
102 (goto-char (match-end 0))
103 (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
104 (cons method tags))))
105 (t (error "The message is corrupted. No mail header separator"))))))
107 (defun mml-secure-sign-pgp ()
108 "Add MML tags to PGP sign this MML part."
110 (mml-secure-part "pgp" 'sign))
112 (defun mml-secure-sign-pgpmime ()
113 "Add MML tags to PGP/MIME sign this MML part."
115 (mml-secure-part "pgpmime" 'sign))
117 (defun mml-secure-sign-smime ()
118 "Add MML tags to S/MIME sign this MML part."
120 (mml-secure-part "smime" 'sign))
122 (defun mml-secure-encrypt-pgp ()
123 "Add MML tags to PGP encrypt this MML part."
125 (mml-secure-part "pgp"))
127 (defun mml-secure-encrypt-pgpmime ()
128 "Add MML tags to PGP/MIME encrypt this MML part."
130 (mml-secure-part "pgpmime"))
132 (defun mml-secure-encrypt-smime ()
133 "Add MML tags to S/MIME encrypt this MML part."
135 (mml-secure-part "smime"))
137 ;; defuns that add the proper <#secure ...> tag to the top of the message body
138 (defun mml-secure-message (method &optional modesym)
139 (let ((mode (prin1-to-string modesym))
141 (mml-unsecure-message)
143 (goto-char (point-min))
144 (cond ((re-search-forward
145 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
146 (goto-char (setq insert-loc (match-end 0)))
147 (unless (looking-at "<#secure")
149 'secure 'method method 'mode mode)))
151 "The message is corrupted. No mail header separator"))))
152 (when (eql insert-loc (point))
155 (defun mml-unsecure-message ()
156 "Remove security related MML tags from message."
159 (goto-char (point-max))
160 (when (re-search-backward "^<#secure.*>\n" nil t)
161 (kill-region (match-beginning 0) (match-end 0)))))
163 (defun mml-secure-message-sign-smime ()
164 "Add MML tag to encrypt/sign the entire message."
166 (mml-secure-message "smime" 'sign))
168 (defun mml-secure-message-sign-pgp ()
169 "Add MML tag to encrypt/sign the entire message."
171 (mml-secure-message "pgp" 'sign))
173 (defun mml-secure-message-sign-pgpmime ()
174 "Add MML tag to encrypt/sign the entire message."
176 (mml-secure-message "pgpmime" 'sign))
178 (defun mml-secure-message-encrypt-smime (&optional dontsign)
179 "Add MML tag to encrypt and sign the entire message.
180 If called with a prefix argument, only encrypt (do NOT sign)."
182 (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
184 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
185 "Add MML tag to encrypt and sign the entire message.
186 If called with a prefix argument, only encrypt (do NOT sign)."
188 (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
190 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
191 "Add MML tag to encrypt and sign the entire message.
192 If called with a prefix argument, only encrypt (do NOT sign)."
194 (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
198 ;;; mml-sec.el ends here