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))))
37 (defun mml-smime-encrypt (cont)
38 (let (certnames certfiles tmp file tmpfiles)
39 ;; xxx tmp files are always an security issue
40 (while (setq tmp (pop cont))
41 (if (and (consp tmp) (eq (car tmp) 'certfile))
42 (push (cdr tmp) certnames)))
43 (while (setq tmp (pop certnames))
44 (if (not (and (not (file-exists-p tmp))
47 (setq file (make-temp-name mm-tmp-directory))
48 (with-current-buffer tmp
49 (write-region (point-min) (point-max) file))
51 (push file tmpfiles)))
52 (if (smime-encrypt-buffer certfiles)
54 (while (setq tmp (pop tmpfiles))
57 (while (setq tmp (pop tmpfiles))
61 (defun mml-smime-sign-query ()
62 ;; query information (what certificate) from user when MML tag is
63 ;; added, for use later by the signing process
64 (when (null smime-keys)
65 (customize-variable 'smime-keys)
66 (error "No S/MIME keys configured, use customize to add your key"))
68 (if (= (length smime-keys) 1)
70 (or (let ((from (cadr (funcall gnus-extract-address-components
73 (message-narrow-to-headers)
74 (message-fetch-field "from")))
76 (and from (smime-get-key-by-email from)))
77 (smime-get-key-by-email
78 (completing-read "Sign this part with what signature? "
80 (and (listp (car-safe smime-keys))
81 (caar smime-keys))))))))
83 (defun mml-smime-get-file-cert ()
85 (list 'certfile (read-file-name
86 "File with recipient's S/MIME certificate: "
87 smime-certificate-directory nil t ""))))
89 (defun mml-smime-get-dns-cert ()
90 ;; todo: deal with comma separated multiple recipients
91 (let (result who bad cert)
94 (setq who (read-from-minibuffer
95 (format "%sLookup certificate for: " (or bad ""))
96 (cadr (funcall gnus-extract-address-components
99 (message-narrow-to-headers)
100 (message-fetch-field "to")))
102 (if (setq cert (smime-cert-by-dns who))
103 (setq result (list 'certfile (buffer-name cert)))
104 (setq bad (format "`%s' not found. " who))))
108 (defun mml-smime-encrypt-query ()
109 ;; todo: add ldap support (xemacs ldap api?)
110 ;; todo: try dns/ldap automatically first, before prompting user
113 (ecase (read (gnus-completing-read "dns" "Fetch certificate from"
114 '(("dns") ("file")) nil t))
115 (dns (setq certs (append certs
116 (mml-smime-get-dns-cert))))
117 (file (setq certs (append certs
118 (mml-smime-get-file-cert)))))
119 (setq done (not (y-or-n-p "Add more recipients? "))))
122 (defun mml-smime-verify (handle ctl)
124 (insert-buffer (mm-handle-multipart-original-buffer ctl))
125 (goto-char (point-min))
126 (insert (format "Content-Type: %s; " (mm-handle-media-type ctl)))
127 (insert (format "protocol=\"%s\"; "
128 (mm-handle-multipart-ctl-parameter ctl 'protocol)))
129 (insert (format "micalg=\"%s\"; "
130 (mm-handle-multipart-ctl-parameter ctl 'micalg)))
131 (insert (format "boundary=\"%s\"\n\n"
132 (mm-handle-multipart-ctl-parameter ctl 'boundary)))
133 (when (get-buffer smime-details-buffer)
134 (kill-buffer smime-details-buffer))
135 (let ((buf (current-buffer))
136 (good-signature (smime-noverify-buffer))
137 (good-certificate (and (or smime-CA-file smime-CA-directory)
138 (smime-verify-buffer)))
139 addresses openssl-output)
140 (setq openssl-output (with-current-buffer smime-details-buffer
142 (if (not good-signature)
144 ;; we couldn't verify message, fail with openssl output as message
145 (mm-set-handle-multipart-parameter
146 mm-security-handle 'gnus-info "Failed")
147 (mm-set-handle-multipart-parameter
148 mm-security-handle 'gnus-details
149 (concat "OpenSSL failed to verify message:\n"
150 "---------------------------------\n"
152 ;; verify mail addresses in mail against those in certificate
153 (when (and (smime-pkcs7-region (point-min) (point-max))
154 (smime-pkcs7-certificates-region (point-min) (point-max)))
156 (insert-buffer-substring buf)
157 (goto-char (point-min))
158 (while (re-search-forward "-----END CERTIFICATE-----" nil t)
159 (when (smime-pkcs7-email-region (point-min) (point))
160 (setq addresses (append (smime-buffer-as-string-region
161 (point-min) (point)) addresses)))
162 (delete-region (point-min) (point)))
163 (setq addresses (mapcar 'downcase addresses))))
164 (if (not (member (downcase (or (mm-handle-multipart-from ctl) "")) addresses))
165 (mm-set-handle-multipart-parameter
166 mm-security-handle 'gnus-info "Sender address forged")
168 (mm-set-handle-multipart-parameter
169 mm-security-handle 'gnus-info "Ok (sender authenticated)")
170 (mm-set-handle-multipart-parameter
171 mm-security-handle 'gnus-info "Integrity OK (sender unknown)")))
172 (mm-set-handle-multipart-parameter
173 mm-security-handle 'gnus-details
174 (concat "Sender clamed to be: " (mm-handle-multipart-from ctl) "\n"
176 (concat "Addresses in certificate: "
177 (mapconcat 'identity addresses ", "))
178 "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
181 "---------------\n" openssl-output "\n"
182 "Certificate(s) inside S/MIME signature:\n"
183 "---------------------------------------\n"
184 (buffer-string) "\n")))))
187 (defun mml-smime-verify-test (handle ctl)
188 smime-openssl-program)
192 ;;; mml-smime.el ends here