1 ;;; mml-smime.el --- S/MIME support for MML
2 ;; Copyright (c) 2000, 2001, 2003 Free Software Foundation, Inc.
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; Keywords: Gnus, MIME, S/MIME, MML
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
30 (autoload 'message-narrow-to-headers "message")
32 (defun mml-smime-sign (cont)
33 (when (null smime-keys)
34 (customize-variable 'smime-keys)
35 (error "No S/MIME keys configured, use customize to add your key"))
36 (smime-sign-buffer (cdr (assq 'keyfile cont)))
37 (goto-char (point-max)))
39 (defun mml-smime-encrypt (cont)
40 (let (certnames certfiles tmp file tmpfiles)
41 ;; xxx tmp files are always an security issue
42 (while (setq tmp (pop cont))
43 (if (and (consp tmp) (eq (car tmp) 'certfile))
44 (push (cdr tmp) certnames)))
45 (while (setq tmp (pop certnames))
46 (if (not (and (not (file-exists-p tmp))
49 (setq file (mm-make-temp-file (expand-file-name "mml."
51 (with-current-buffer tmp
52 (write-region (point-min) (point-max) file))
54 (push file tmpfiles)))
55 (if (smime-encrypt-buffer certfiles)
57 (while (setq tmp (pop tmpfiles))
60 (while (setq tmp (pop tmpfiles))
63 (goto-char (point-max)))
65 (defun mml-smime-sign-query ()
66 ;; query information (what certificate) from user when MML tag is
67 ;; added, for use later by the signing process
68 (when (null smime-keys)
69 (customize-variable 'smime-keys)
70 (error "No S/MIME keys configured, use customize to add your key"))
72 (if (= (length smime-keys) 1)
74 (or (let ((from (cadr (funcall gnus-extract-address-components
77 (message-narrow-to-headers)
78 (message-fetch-field "from")))
80 (and from (smime-get-key-by-email from)))
81 (smime-get-key-by-email
82 (completing-read "Sign this part with what signature? "
84 (and (listp (car-safe smime-keys))
85 (caar smime-keys))))))))
87 (defun mml-smime-get-file-cert ()
89 (list 'certfile (read-file-name
90 "File with recipient's S/MIME certificate: "
91 smime-certificate-directory nil t ""))))
93 (defun mml-smime-get-dns-cert ()
94 ;; todo: deal with comma separated multiple recipients
95 (let (result who bad cert)
98 (setq who (read-from-minibuffer
99 (format "%sLookup certificate for: " (or bad ""))
100 (cadr (funcall gnus-extract-address-components
103 (message-narrow-to-headers)
104 (message-fetch-field "to")))
106 (if (setq cert (smime-cert-by-dns who))
107 (setq result (list 'certfile (buffer-name cert)))
108 (setq bad (format "`%s' not found. " who))))
112 (defun mml-smime-encrypt-query ()
113 ;; todo: add ldap support (xemacs ldap api?)
114 ;; todo: try dns/ldap automatically first, before prompting user
117 (ecase (read (gnus-completing-read-with-default
118 "dns" "Fetch certificate from"
119 '(("dns") ("file")) nil t))
120 (dns (setq certs (append certs
121 (mml-smime-get-dns-cert))))
122 (file (setq certs (append certs
123 (mml-smime-get-file-cert)))))
124 (setq done (not (y-or-n-p "Add more recipients? "))))
127 (defun mml-smime-verify (handle ctl)
129 (insert-buffer-substring (mm-handle-multipart-original-buffer ctl))
130 (goto-char (point-min))
131 (insert (format "Content-Type: %s; " (mm-handle-media-type ctl)))
132 (insert (format "protocol=\"%s\"; "
133 (mm-handle-multipart-ctl-parameter ctl 'protocol)))
134 (insert (format "micalg=\"%s\"; "
135 (mm-handle-multipart-ctl-parameter ctl 'micalg)))
136 (insert (format "boundary=\"%s\"\n\n"
137 (mm-handle-multipart-ctl-parameter ctl 'boundary)))
138 (when (get-buffer smime-details-buffer)
139 (kill-buffer smime-details-buffer))
140 (let ((buf (current-buffer))
141 (good-signature (smime-noverify-buffer))
142 (good-certificate (and (or smime-CA-file smime-CA-directory)
143 (smime-verify-buffer)))
144 addresses openssl-output)
145 (setq openssl-output (with-current-buffer smime-details-buffer
147 (if (not good-signature)
149 ;; we couldn't verify message, fail with openssl output as message
150 (mm-set-handle-multipart-parameter
151 mm-security-handle 'gnus-info "Failed")
152 (mm-set-handle-multipart-parameter
153 mm-security-handle 'gnus-details
154 (concat "OpenSSL failed to verify message integrity:\n"
155 "-------------------------------------------\n"
157 ;; verify mail addresses in mail against those in certificate
158 (when (and (smime-pkcs7-region (point-min) (point-max))
159 (smime-pkcs7-certificates-region (point-min) (point-max)))
161 (insert-buffer-substring buf)
162 (goto-char (point-min))
163 (while (re-search-forward "-----END CERTIFICATE-----" nil t)
164 (when (smime-pkcs7-email-region (point-min) (point))
165 (setq addresses (append (smime-buffer-as-string-region
166 (point-min) (point)) addresses)))
167 (delete-region (point-min) (point)))
168 (setq addresses (mapcar 'downcase addresses))))
169 (if (not (member (downcase (or (mm-handle-multipart-from ctl) "")) addresses))
170 (mm-set-handle-multipart-parameter
171 mm-security-handle 'gnus-info "Sender address forged")
173 (mm-set-handle-multipart-parameter
174 mm-security-handle 'gnus-info "Ok (sender authenticated)")
175 (mm-set-handle-multipart-parameter
176 mm-security-handle 'gnus-info "Ok (sender not trusted)")))
177 (mm-set-handle-multipart-parameter
178 mm-security-handle 'gnus-details
179 (concat "Sender claimed to be: " (mm-handle-multipart-from ctl) "\n"
181 (concat "Addresses in certificate: "
182 (mapconcat 'identity addresses ", "))
183 "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
186 "---------------\n" openssl-output "\n"
187 "Certificate(s) inside S/MIME signature:\n"
188 "---------------------------------------\n"
189 (buffer-string) "\n")))))
192 (defun mml-smime-verify-test (handle ctl)
193 smime-openssl-program)
197 ;;; mml-smime.el ends here