1 ;;; mml-smime.el --- S/MIME support for MML
2 ;; Copyright (c) 2000, 2001 Free Software Foundation, Inc.
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; Keywords: Gnus, MIME, S/MIME, MML
7 ;; This file is a 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.
31 (defun mml-smime-sign (cont)
32 (when (null smime-keys)
33 (customize-variable 'smime-keys)
34 (error "No S/MIME keys configured, use customize to add your key"))
35 (smime-sign-buffer (cdr (assq 'keyfile cont)))
36 (goto-char (point-max)))
38 (defun mml-smime-encrypt (cont)
39 (let (certnames certfiles tmp file tmpfiles)
40 ;; xxx tmp files are always an security issue
41 (while (setq tmp (pop cont))
42 (if (and (consp tmp) (eq (car tmp) 'certfile))
43 (push (cdr tmp) certnames)))
44 (while (setq tmp (pop certnames))
45 (if (not (and (not (file-exists-p tmp))
48 (setq file (make-temp-name mm-tmp-directory))
49 (with-current-buffer tmp
50 (write-region (point-min) (point-max) file))
52 (push file tmpfiles)))
53 (if (smime-encrypt-buffer certfiles)
55 (while (setq tmp (pop tmpfiles))
58 (while (setq tmp (pop tmpfiles))
61 (goto-char (point-max)))
63 (defun mml-smime-sign-query ()
64 ;; query information (what certificate) from user when MML tag is
65 ;; added, for use later by the signing process
66 (when (null smime-keys)
67 (customize-variable 'smime-keys)
68 (error "No S/MIME keys configured, use customize to add your key"))
70 (if (= (length smime-keys) 1)
72 (or (let ((from (cadr (funcall gnus-extract-address-components
75 (message-narrow-to-headers)
76 (message-fetch-field "from")))
78 (and from (smime-get-key-by-email from)))
79 (smime-get-key-by-email
80 (completing-read "Sign this part with what signature? "
82 (and (listp (car-safe smime-keys))
83 (caar smime-keys))))))))
85 (defun mml-smime-get-file-cert ()
87 (list 'certfile (read-file-name
88 "File with recipient's S/MIME certificate: "
89 smime-certificate-directory nil t ""))))
91 (defun mml-smime-get-dns-cert ()
92 ;; todo: deal with comma separated multiple recipients
93 (let (result who bad cert)
96 (setq who (read-from-minibuffer
97 (format "%sLookup certificate for: " (or bad ""))
98 (cadr (funcall gnus-extract-address-components
101 (message-narrow-to-headers)
102 (message-fetch-field "to")))
104 (if (setq cert (smime-cert-by-dns who))
105 (setq result (list 'certfile (buffer-name cert)))
106 (setq bad (format "`%s' not found. " who))))
110 (defun mml-smime-encrypt-query ()
111 ;; todo: add ldap support (xemacs ldap api?)
112 ;; todo: try dns/ldap automatically first, before prompting user
115 (ecase (read (gnus-completing-read "dns" "Fetch certificate from"
116 '(("dns") ("file")) nil t))
117 (dns (setq certs (append certs
118 (mml-smime-get-dns-cert))))
119 (file (setq certs (append certs
120 (mml-smime-get-file-cert)))))
121 (setq done (not (y-or-n-p "Add more recipients? "))))
124 (defun mml-smime-verify (handle ctl)
126 (insert-buffer (mm-handle-multipart-original-buffer ctl))
127 (goto-char (point-min))
128 (insert (format "Content-Type: %s; " (mm-handle-media-type ctl)))
129 (insert (format "protocol=\"%s\"; "
130 (mm-handle-multipart-ctl-parameter ctl 'protocol)))
131 (insert (format "micalg=\"%s\"; "
132 (mm-handle-multipart-ctl-parameter ctl 'micalg)))
133 (insert (format "boundary=\"%s\"\n\n"
134 (mm-handle-multipart-ctl-parameter ctl 'boundary)))
135 (when (get-buffer smime-details-buffer)
136 (kill-buffer smime-details-buffer))
137 (let ((buf (current-buffer))
138 (good-signature (smime-noverify-buffer))
139 (good-certificate (and (or smime-CA-file smime-CA-directory)
140 (smime-verify-buffer)))
141 addresses openssl-output)
142 (setq openssl-output (with-current-buffer smime-details-buffer
144 (if (not good-signature)
146 ;; we couldn't verify message, fail with openssl output as message
147 (mm-set-handle-multipart-parameter
148 mm-security-handle 'gnus-info "Failed")
149 (mm-set-handle-multipart-parameter
150 mm-security-handle 'gnus-details
151 (concat "OpenSSL failed to verify message integrity:\n"
152 "-------------------------------------------\n"
154 ;; verify mail addresses in mail against those in certificate
155 (when (and (smime-pkcs7-region (point-min) (point-max))
156 (smime-pkcs7-certificates-region (point-min) (point-max)))
158 (insert-buffer-substring buf)
159 (goto-char (point-min))
160 (while (re-search-forward "-----END CERTIFICATE-----" nil t)
161 (when (smime-pkcs7-email-region (point-min) (point))
162 (setq addresses (append (smime-buffer-as-string-region
163 (point-min) (point)) addresses)))
164 (delete-region (point-min) (point)))
165 (setq addresses (mapcar 'downcase addresses))))
166 (if (not (member (downcase (or (mm-handle-multipart-from ctl) "")) addresses))
167 (mm-set-handle-multipart-parameter
168 mm-security-handle 'gnus-info "Sender address forged")
170 (mm-set-handle-multipart-parameter
171 mm-security-handle 'gnus-info "Ok (sender authenticated)")
172 (mm-set-handle-multipart-parameter
173 mm-security-handle 'gnus-info "Ok (sender not trusted)")))
174 (mm-set-handle-multipart-parameter
175 mm-security-handle 'gnus-details
176 (concat "Sender claimed to be: " (mm-handle-multipart-from ctl) "\n"
178 (concat "Addresses in certificate: "
179 (mapconcat 'identity addresses ", "))
180 "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
183 "---------------\n" openssl-output "\n"
184 "Certificate(s) inside S/MIME signature:\n"
185 "---------------------------------------\n"
186 (buffer-string) "\n")))))
189 (defun mml-smime-verify-test (handle ctl)
190 smime-openssl-program)
194 ;;; mml-smime.el ends here