]> code.delx.au - gnu-emacs/blob - lisp/gnus/mml-smime.el
(gnus-extract-address-components): Add defvar.
[gnu-emacs] / lisp / gnus / mml-smime.el
1 ;;; mml-smime.el --- S/MIME support for MML
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
5
6 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; Keywords: Gnus, MIME, S/MIME, MML
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (defvar gnus-extract-address-components)
33
34 (require 'smime)
35 (require 'mm-decode)
36 (autoload 'message-narrow-to-headers "message")
37 (autoload 'message-fetch-field "message")
38
39 (defun mml-smime-sign (cont)
40 (when (null smime-keys)
41 (customize-variable 'smime-keys)
42 (error "No S/MIME keys configured, use customize to add your key"))
43 (smime-sign-buffer (cdr (assq 'keyfile cont)))
44 (goto-char (point-min))
45 (while (search-forward "\r\n" nil t)
46 (replace-match "\n" t t))
47 (goto-char (point-max)))
48
49 (defun mml-smime-encrypt (cont)
50 (let (certnames certfiles tmp file tmpfiles)
51 ;; xxx tmp files are always an security issue
52 (while (setq tmp (pop cont))
53 (if (and (consp tmp) (eq (car tmp) 'certfile))
54 (push (cdr tmp) certnames)))
55 (while (setq tmp (pop certnames))
56 (if (not (and (not (file-exists-p tmp))
57 (get-buffer tmp)))
58 (push tmp certfiles)
59 (setq file (mm-make-temp-file (expand-file-name "mml."
60 mm-tmp-directory)))
61 (with-current-buffer tmp
62 (write-region (point-min) (point-max) file))
63 (push file certfiles)
64 (push file tmpfiles)))
65 (if (smime-encrypt-buffer certfiles)
66 (progn
67 (while (setq tmp (pop tmpfiles))
68 (delete-file tmp))
69 t)
70 (while (setq tmp (pop tmpfiles))
71 (delete-file tmp))
72 nil))
73 (goto-char (point-max)))
74
75 (defun mml-smime-sign-query ()
76 ;; query information (what certificate) from user when MML tag is
77 ;; added, for use later by the signing process
78 (when (null smime-keys)
79 (customize-variable 'smime-keys)
80 (error "No S/MIME keys configured, use customize to add your key"))
81 (list 'keyfile
82 (if (= (length smime-keys) 1)
83 (cadar smime-keys)
84 (or (let ((from (cadr (funcall gnus-extract-address-components
85 (or (save-excursion
86 (save-restriction
87 (message-narrow-to-headers)
88 (message-fetch-field "from")))
89 "")))))
90 (and from (smime-get-key-by-email from)))
91 (smime-get-key-by-email
92 (completing-read "Sign this part with what signature? "
93 smime-keys nil nil
94 (and (listp (car-safe smime-keys))
95 (caar smime-keys))))))))
96
97 (defun mml-smime-get-file-cert ()
98 (ignore-errors
99 (list 'certfile (read-file-name
100 "File with recipient's S/MIME certificate: "
101 smime-certificate-directory nil t ""))))
102
103 (defun mml-smime-get-dns-cert ()
104 ;; todo: deal with comma separated multiple recipients
105 (let (result who bad cert)
106 (condition-case ()
107 (while (not result)
108 (setq who (read-from-minibuffer
109 (format "%sLookup certificate for: " (or bad ""))
110 (cadr (funcall gnus-extract-address-components
111 (or (save-excursion
112 (save-restriction
113 (message-narrow-to-headers)
114 (message-fetch-field "to")))
115 "")))))
116 (if (setq cert (smime-cert-by-dns who))
117 (setq result (list 'certfile (buffer-name cert)))
118 (setq bad (format "`%s' not found. " who))))
119 (quit))
120 result))
121
122 (defun mml-smime-encrypt-query ()
123 ;; todo: add ldap support (xemacs ldap api?)
124 ;; todo: try dns/ldap automatically first, before prompting user
125 (let (certs done)
126 (while (not done)
127 (ecase (read (gnus-completing-read-with-default
128 "dns" "Fetch certificate from"
129 '(("dns") ("file")) nil t))
130 (dns (setq certs (append certs
131 (mml-smime-get-dns-cert))))
132 (file (setq certs (append certs
133 (mml-smime-get-file-cert)))))
134 (setq done (not (y-or-n-p "Add more recipients? "))))
135 certs))
136
137 (defun mml-smime-verify (handle ctl)
138 (with-temp-buffer
139 (insert-buffer-substring (mm-handle-multipart-original-buffer ctl))
140 (goto-char (point-min))
141 (insert (format "Content-Type: %s; " (mm-handle-media-type ctl)))
142 (insert (format "protocol=\"%s\"; "
143 (mm-handle-multipart-ctl-parameter ctl 'protocol)))
144 (insert (format "micalg=\"%s\"; "
145 (mm-handle-multipart-ctl-parameter ctl 'micalg)))
146 (insert (format "boundary=\"%s\"\n\n"
147 (mm-handle-multipart-ctl-parameter ctl 'boundary)))
148 (when (get-buffer smime-details-buffer)
149 (kill-buffer smime-details-buffer))
150 (let ((buf (current-buffer))
151 (good-signature (smime-noverify-buffer))
152 (good-certificate (and (or smime-CA-file smime-CA-directory)
153 (smime-verify-buffer)))
154 addresses openssl-output)
155 (setq openssl-output (with-current-buffer smime-details-buffer
156 (buffer-string)))
157 (if (not good-signature)
158 (progn
159 ;; we couldn't verify message, fail with openssl output as message
160 (mm-set-handle-multipart-parameter
161 mm-security-handle 'gnus-info "Failed")
162 (mm-set-handle-multipart-parameter
163 mm-security-handle 'gnus-details
164 (concat "OpenSSL failed to verify message integrity:\n"
165 "-------------------------------------------\n"
166 openssl-output)))
167 ;; verify mail addresses in mail against those in certificate
168 (when (and (smime-pkcs7-region (point-min) (point-max))
169 (smime-pkcs7-certificates-region (point-min) (point-max)))
170 (with-temp-buffer
171 (insert-buffer-substring buf)
172 (goto-char (point-min))
173 (while (re-search-forward "-----END CERTIFICATE-----" nil t)
174 (when (smime-pkcs7-email-region (point-min) (point))
175 (setq addresses (append (smime-buffer-as-string-region
176 (point-min) (point)) addresses)))
177 (delete-region (point-min) (point)))
178 (setq addresses (mapcar 'downcase addresses))))
179 (if (not (member (downcase (or (mm-handle-multipart-from ctl) "")) addresses))
180 (mm-set-handle-multipart-parameter
181 mm-security-handle 'gnus-info "Sender address forged")
182 (if good-certificate
183 (mm-set-handle-multipart-parameter
184 mm-security-handle 'gnus-info "Ok (sender authenticated)")
185 (mm-set-handle-multipart-parameter
186 mm-security-handle 'gnus-info "Ok (sender not trusted)")))
187 (mm-set-handle-multipart-parameter
188 mm-security-handle 'gnus-details
189 (concat "Sender claimed to be: " (mm-handle-multipart-from ctl) "\n"
190 (if addresses
191 (concat "Addresses in certificate: "
192 (mapconcat 'identity addresses ", "))
193 "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
194 "\n" "\n"
195 "OpenSSL output:\n"
196 "---------------\n" openssl-output "\n"
197 "Certificate(s) inside S/MIME signature:\n"
198 "---------------------------------------\n"
199 (buffer-string) "\n")))))
200 handle)
201
202 (defun mml-smime-verify-test (handle ctl)
203 smime-openssl-program)
204
205 (provide 'mml-smime)
206
207 ;;; arch-tag: f1bf94d4-f2cd-4c6f-b059-ad69492817e2
208 ;;; mml-smime.el ends here