* mail-source.el (make-source-make-complex-temp-name): Use
[gnus] / lisp / mml-smime.el
1 ;;; mml-smime.el --- S/MIME support for MML
2 ;; Copyright (c) 2000, 2001 Free Software Foundation, Inc.
3
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; Keywords: Gnus, MIME, S/MIME, MML
6
7 ;; This file is a part of GNU Emacs.
8
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.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'smime)
29 (require 'mm-decode)
30
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)))
37
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))
46                     (get-buffer tmp)))
47           (push tmp certfiles)
48         (setq file (mm-make-temp-file (expand-file-name "mml." 
49                                                         mm-tmp-directory)))
50         (with-current-buffer tmp
51           (write-region (point-min) (point-max) file))
52         (push file certfiles)
53         (push file tmpfiles)))
54     (if (smime-encrypt-buffer certfiles)
55         (progn
56           (while (setq tmp (pop tmpfiles))
57             (delete-file tmp))
58           t)
59       (while (setq tmp (pop tmpfiles))
60         (delete-file tmp))
61       nil))
62   (goto-char (point-max)))
63
64 (defun mml-smime-sign-query ()
65   ;; query information (what certificate) from user when MML tag is
66   ;; added, for use later by the signing process
67   (when (null smime-keys)
68     (customize-variable 'smime-keys)
69     (error "No S/MIME keys configured, use customize to add your key"))
70   (list 'keyfile
71         (if (= (length smime-keys) 1)
72             (cadar smime-keys)
73           (or (let ((from (cadr (funcall gnus-extract-address-components
74                                          (or (save-excursion
75                                                (save-restriction
76                                                  (message-narrow-to-headers)
77                                                  (message-fetch-field "from")))
78                                              "")))))
79                 (and from (smime-get-key-by-email from)))
80               (smime-get-key-by-email
81                (completing-read "Sign this part with what signature? "
82                                 smime-keys nil nil
83                                 (and (listp (car-safe smime-keys))
84                                      (caar smime-keys))))))))
85
86 (defun mml-smime-get-file-cert ()
87   (ignore-errors
88     (list 'certfile (read-file-name
89                      "File with recipient's S/MIME certificate: "
90                      smime-certificate-directory nil t ""))))
91
92 (defun mml-smime-get-dns-cert ()
93   ;; todo: deal with comma separated multiple recipients
94   (let (result who bad cert)
95     (condition-case ()
96         (while (not result)
97           (setq who (read-from-minibuffer
98                      (format "%sLookup certificate for: " (or bad ""))
99                      (cadr (funcall gnus-extract-address-components
100                                     (or (save-excursion
101                                           (save-restriction
102                                             (message-narrow-to-headers)
103                                             (message-fetch-field "to")))
104                                         "")))))
105           (if (setq cert (smime-cert-by-dns who))
106               (setq result (list 'certfile (buffer-name cert)))
107             (setq bad (format "`%s' not found. " who))))
108       (quit))
109     result))
110
111 (defun mml-smime-encrypt-query ()
112   ;; todo: add ldap support (xemacs ldap api?)
113   ;; todo: try dns/ldap automatically first, before prompting user
114   (let (certs done)
115     (while (not done)
116       (ecase (read (gnus-completing-read-with-default
117                     "dns" "Fetch certificate from"
118                     '(("dns") ("file")) nil t))
119         (dns (setq certs (append certs
120                                  (mml-smime-get-dns-cert))))
121         (file (setq certs (append certs
122                                   (mml-smime-get-file-cert)))))
123       (setq done (not (y-or-n-p "Add more recipients? "))))
124     certs))
125
126 (defun mml-smime-verify (handle ctl)
127   (with-temp-buffer
128     (insert-buffer (mm-handle-multipart-original-buffer ctl))
129     (goto-char (point-min))
130     (insert (format "Content-Type: %s; " (mm-handle-media-type ctl)))
131     (insert (format "protocol=\"%s\"; "
132                     (mm-handle-multipart-ctl-parameter ctl 'protocol)))
133     (insert (format "micalg=\"%s\"; "
134                     (mm-handle-multipart-ctl-parameter ctl 'micalg)))
135     (insert (format "boundary=\"%s\"\n\n"
136                     (mm-handle-multipart-ctl-parameter ctl 'boundary)))
137     (when (get-buffer smime-details-buffer)
138       (kill-buffer smime-details-buffer))
139     (let ((buf (current-buffer))
140           (good-signature (smime-noverify-buffer))
141           (good-certificate (and (or smime-CA-file smime-CA-directory)
142                                  (smime-verify-buffer)))
143           addresses openssl-output)
144       (setq openssl-output (with-current-buffer smime-details-buffer
145                              (buffer-string)))
146       (if (not good-signature)
147           (progn
148             ;; we couldn't verify message, fail with openssl output as message
149             (mm-set-handle-multipart-parameter
150              mm-security-handle 'gnus-info "Failed")
151             (mm-set-handle-multipart-parameter
152              mm-security-handle 'gnus-details
153              (concat "OpenSSL failed to verify message integrity:\n"
154                      "-------------------------------------------\n"
155                      openssl-output)))
156         ;; verify mail addresses in mail against those in certificate
157         (when (and (smime-pkcs7-region (point-min) (point-max))
158                    (smime-pkcs7-certificates-region (point-min) (point-max)))
159           (with-temp-buffer
160             (insert-buffer-substring buf)
161             (goto-char (point-min))
162             (while (re-search-forward "-----END CERTIFICATE-----" nil t)
163               (when (smime-pkcs7-email-region (point-min) (point))
164                 (setq addresses (append (smime-buffer-as-string-region
165                                          (point-min) (point)) addresses)))
166               (delete-region (point-min) (point)))
167             (setq addresses (mapcar 'downcase addresses))))
168         (if (not (member (downcase (or (mm-handle-multipart-from ctl) "")) addresses))
169             (mm-set-handle-multipart-parameter
170              mm-security-handle 'gnus-info "Sender address forged")
171           (if good-certificate
172               (mm-set-handle-multipart-parameter
173                mm-security-handle 'gnus-info "Ok (sender authenticated)")
174             (mm-set-handle-multipart-parameter
175              mm-security-handle 'gnus-info "Ok (sender not trusted)")))
176         (mm-set-handle-multipart-parameter
177          mm-security-handle 'gnus-details
178          (concat "Sender claimed to be: " (mm-handle-multipart-from ctl) "\n"
179                  (if addresses
180                      (concat "Addresses in certificate: "
181                              (mapconcat 'identity addresses ", "))
182                    "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
183                  "\n" "\n"
184                  "OpenSSL output:\n"
185                  "---------------\n" openssl-output "\n"
186                  "Certificate(s) inside S/MIME signature:\n"
187                  "---------------------------------------\n"
188                  (buffer-string) "\n")))))
189   handle)
190
191 (defun mml-smime-verify-test (handle ctl)
192   smime-openssl-program)
193
194 (provide 'mml-smime)
195
196 ;;; mml-smime.el ends here