1 ;;; mml-smime.el --- S/MIME support for MML
2 ;; Copyright (c) 2000 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 (smime-sign-buffer (cdr (assq 'keyfile cont))))
34 (defun mml-smime-encrypt (cont)
35 (let (certnames certfiles tmp file tmpfiles)
36 ;; xxx tmp files are always an security issue
37 (while (setq tmp (pop cont))
38 (if (and (consp tmp) (eq (car tmp) 'certfile))
39 (push (cdr tmp) certnames)))
40 (while (setq tmp (pop certnames))
41 (if (not (and (not (file-exists-p tmp))
44 (setq file (make-temp-name mm-tmp-directory))
45 (with-current-buffer tmp
46 (write-region (point-min) (point-max) file))
48 (push file tmpfiles)))
49 (if (smime-encrypt-buffer certfiles)
51 (while (setq tmp (pop tmpfiles))
54 (while (setq tmp (pop tmpfiles))
58 (defun mml-smime-sign-query ()
59 ;; query information (what certificate) from user when MML tag is
60 ;; added, for use later by the signing process
61 (when (null smime-keys)
62 (customize-variable 'smime-keys)
63 (error "No S/MIME keys configured, use customize to add your key"))
65 (if (= (length smime-keys) 1)
67 (or (let ((from (cadr (funcall gnus-extract-address-components
70 (message-narrow-to-headers)
71 (message-fetch-field "from")))
73 (and from (smime-get-key-by-email from)))
74 (smime-get-key-by-email
75 (completing-read "Sign this part with what signature? "
77 (and (listp (car-safe smime-keys))
78 (caar smime-keys))))))))
80 (defun mml-smime-get-file-cert ()
82 (list 'certfile (read-file-name
83 "File with recipient's S/MIME certificate: "
84 smime-certificate-directory nil t ""))))
86 (defun mml-smime-get-dns-cert ()
87 ;; todo: deal with comma separated multiple recipients
88 (let (result who bad cert)
91 (setq who (read-from-minibuffer
92 (format "%sLookup certificate for: " (or bad ""))
93 (cadr (funcall gnus-extract-address-components
96 (message-narrow-to-headers)
97 (message-fetch-field "to")))
99 (if (setq cert (smime-cert-by-dns who))
100 (setq result (list 'certfile (buffer-name cert)))
101 (setq bad (format "`%s' not found. " who))))
105 (defun mml-smime-encrypt-query ()
106 ;; todo: add ldap support (xemacs ldap api?)
107 ;; todo: try dns/ldap automatically first, before prompting user
110 (ecase (read (gnus-completing-read "dns" "Fetch certificate from"
111 '(("dns") ("file")) nil t))
112 (dns (setq certs (append certs
113 (mml-smime-get-dns-cert))))
114 (file (setq certs (append certs
115 (mml-smime-get-file-cert)))))
116 (setq done (not (y-or-n-p "Add more recipients? "))))
119 (defun mml-smime-verify (handle ctl)
121 (insert-buffer (mm-handle-multipart-original-buffer ctl))
122 (goto-char (point-min))
123 (insert (format "Content-Type: %s; " (mm-handle-media-type ctl)))
124 (insert (format "protocol=\"%s\"; "
125 (mm-handle-multipart-ctl-parameter ctl 'protocol)))
126 (insert (format "micalg=\"%s\"; "
127 (mm-handle-multipart-ctl-parameter ctl 'micalg)))
128 (insert (format "boundary=\"%s\"\n\n"
129 (mm-handle-multipart-ctl-parameter ctl 'boundary)))
130 (when (get-buffer smime-details-buffer)
131 (kill-buffer smime-details-buffer))
132 (let ((buf (current-buffer))
133 (good-signature (smime-verify-buffer))
134 addresses openssl-output)
135 (setq openssl-output (with-current-buffer smime-details-buffer
137 (if (not good-signature)
139 ;; we couldn't verify message, fail with openssl output as message
140 (mm-set-handle-multipart-parameter
141 mm-security-handle 'gnus-info "Failed")
142 (mm-set-handle-multipart-parameter
143 mm-security-handle 'gnus-details
144 (concat "OpenSSL failed to verify message:\n"
145 "---------------------------------\n"
147 ;; verify mail addresses in mail against those in certificate
148 (when (and (smime-pkcs7-region (point-min) (point-max))
149 (smime-pkcs7-certificates-region (point-min) (point-max)))
151 (insert-buffer-substring buf)
152 (goto-char (point-min))
153 (while (re-search-forward "-----END CERTIFICATE-----" nil t)
154 (smime-pkcs7-email-region (point-min) (point))
155 (setq addresses (append (smime-buffer-as-string-region
156 (point-min) (point)) addresses))
157 (delete-region (point-min) (point)))))
158 (if (not (member mm-security-from addresses))
159 (mm-set-handle-multipart-parameter
160 mm-security-handle 'gnus-info "Sender forged")
161 (mm-set-handle-multipart-parameter
162 mm-security-handle 'gnus-info "OK"))
163 (mm-set-handle-multipart-parameter
164 mm-security-handle 'gnus-details
165 (concat "Sender clamed to be: " mm-security-from "\n"
167 (concat "Addresses in certificate: "
168 (mapconcat 'identity addresses ", "))
169 "No addresses found in certificate.")
172 "---------------\n" openssl-output "\n"
173 "Certificate(s) inside S/MIME signature:\n"
174 "---------------------------------------\n"
175 (buffer-string) "\n")))))
178 (defun mml-smime-verify-test (handle ctl)
179 smime-openssl-program)
183 ;;; mml-smime.el ends here