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-min))
38 (while (search-forward "\r\n" nil t)
39 (replace-match "\n" t t))
40 (goto-char (point-max)))
42 (defun mml-smime-encrypt (cont)
43 (let (certnames certfiles tmp file tmpfiles)
44 ;; xxx tmp files are always an security issue
45 (while (setq tmp (pop cont))
46 (if (and (consp tmp) (eq (car tmp) 'certfile))
47 (push (cdr tmp) certnames)))
48 (while (setq tmp (pop certnames))
49 (if (not (and (not (file-exists-p tmp))
52 (setq file (mm-make-temp-file (expand-file-name "mml."
54 (with-current-buffer tmp
55 (write-region (point-min) (point-max) file))
57 (push file tmpfiles)))
58 (if (smime-encrypt-buffer certfiles)
60 (while (setq tmp (pop tmpfiles))
63 (while (setq tmp (pop tmpfiles))
66 (goto-char (point-max)))
68 (defun mml-smime-sign-query ()
69 ;; query information (what certificate) from user when MML tag is
70 ;; added, for use later by the signing process
71 (when (null smime-keys)
72 (customize-variable 'smime-keys)
73 (error "No S/MIME keys configured, use customize to add your key"))
75 (if (= (length smime-keys) 1)
77 (or (let ((from (cadr (funcall gnus-extract-address-components
80 (message-narrow-to-headers)
81 (message-fetch-field "from")))
83 (and from (smime-get-key-by-email from)))
84 (smime-get-key-by-email
85 (completing-read "Sign this part with what signature? "
87 (and (listp (car-safe smime-keys))
88 (caar smime-keys))))))))
90 (defun mml-smime-get-file-cert ()
92 (list 'certfile (read-file-name
93 "File with recipient's S/MIME certificate: "
94 smime-certificate-directory nil t ""))))
96 (defun mml-smime-get-dns-cert ()
97 ;; todo: deal with comma separated multiple recipients
98 (let (result who bad cert)
101 (setq who (read-from-minibuffer
102 (format "%sLookup certificate for: " (or bad ""))
103 (cadr (funcall gnus-extract-address-components
106 (message-narrow-to-headers)
107 (message-fetch-field "to")))
109 (if (setq cert (smime-cert-by-dns who))
110 (setq result (list 'certfile (buffer-name cert)))
111 (setq bad (format "`%s' not found. " who))))
115 (defun mml-smime-encrypt-query ()
116 ;; todo: add ldap support (xemacs ldap api?)
117 ;; todo: try dns/ldap automatically first, before prompting user
120 (ecase (read (gnus-completing-read-with-default
121 "dns" "Fetch certificate from"
122 '(("dns") ("file")) nil t))
123 (dns (setq certs (append certs
124 (mml-smime-get-dns-cert))))
125 (file (setq certs (append certs
126 (mml-smime-get-file-cert)))))
127 (setq done (not (y-or-n-p "Add more recipients? "))))
130 (defun mml-smime-verify (handle ctl)
132 (insert-buffer-substring (mm-handle-multipart-original-buffer ctl))
133 (goto-char (point-min))
134 (insert (format "Content-Type: %s; " (mm-handle-media-type ctl)))
135 (insert (format "protocol=\"%s\"; "
136 (mm-handle-multipart-ctl-parameter ctl 'protocol)))
137 (insert (format "micalg=\"%s\"; "
138 (mm-handle-multipart-ctl-parameter ctl 'micalg)))
139 (insert (format "boundary=\"%s\"\n\n"
140 (mm-handle-multipart-ctl-parameter ctl 'boundary)))
141 (when (get-buffer smime-details-buffer)
142 (kill-buffer smime-details-buffer))
143 (let ((buf (current-buffer))
144 (good-signature (smime-noverify-buffer))
145 (good-certificate (and (or smime-CA-file smime-CA-directory)
146 (smime-verify-buffer)))
147 addresses openssl-output)
148 (setq openssl-output (with-current-buffer smime-details-buffer
150 (if (not good-signature)
152 ;; we couldn't verify message, fail with openssl output as message
153 (mm-set-handle-multipart-parameter
154 mm-security-handle 'gnus-info "Failed")
155 (mm-set-handle-multipart-parameter
156 mm-security-handle 'gnus-details
157 (concat "OpenSSL failed to verify message integrity:\n"
158 "-------------------------------------------\n"
160 ;; verify mail addresses in mail against those in certificate
161 (when (and (smime-pkcs7-region (point-min) (point-max))
162 (smime-pkcs7-certificates-region (point-min) (point-max)))
164 (insert-buffer-substring buf)
165 (goto-char (point-min))
166 (while (re-search-forward "-----END CERTIFICATE-----" nil t)
167 (when (smime-pkcs7-email-region (point-min) (point))
168 (setq addresses (append (smime-buffer-as-string-region
169 (point-min) (point)) addresses)))
170 (delete-region (point-min) (point)))
171 (setq addresses (mapcar 'downcase addresses))))
172 (if (not (member (downcase (or (mm-handle-multipart-from ctl) "")) addresses))
173 (mm-set-handle-multipart-parameter
174 mm-security-handle 'gnus-info "Sender address forged")
176 (mm-set-handle-multipart-parameter
177 mm-security-handle 'gnus-info "Ok (sender authenticated)")
178 (mm-set-handle-multipart-parameter
179 mm-security-handle 'gnus-info "Ok (sender not trusted)")))
180 (mm-set-handle-multipart-parameter
181 mm-security-handle 'gnus-details
182 (concat "Sender claimed to be: " (mm-handle-multipart-from ctl) "\n"
184 (concat "Addresses in certificate: "
185 (mapconcat 'identity addresses ", "))
186 "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
189 "---------------\n" openssl-output "\n"
190 "Certificate(s) inside S/MIME signature:\n"
191 "---------------------------------------\n"
192 (buffer-string) "\n")))))
195 (defun mml-smime-verify-test (handle ctl)
196 smime-openssl-program)
200 ;;; mml-smime.el ends here