ac874924cef4a0b71a7fe4ee7897b27a9bce96e6
[elisp/gnus.git-] / 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 (make-temp-name mm-tmp-directory))
49         (with-current-buffer tmp
50           (write-region (point-min) (point-max) file))
51         (push file certfiles)
52         (push file tmpfiles)))
53     (if (smime-encrypt-buffer certfiles)
54         (progn
55           (while (setq tmp (pop tmpfiles))
56             (delete-file tmp))
57           t)
58       (while (setq tmp (pop tmpfiles))
59         (delete-file tmp))
60       nil))
61   (goto-char (point-max)))
62
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"))
69   (list 'keyfile
70         (if (= (length smime-keys) 1)
71             (cadar smime-keys)
72           (or (let ((from (cadr (funcall gnus-extract-address-components
73                                          (or (save-excursion
74                                                (save-restriction
75                                                  (message-narrow-to-headers)
76                                                  (message-fetch-field "from")))
77                                              "")))))
78                 (and from (smime-get-key-by-email from)))
79               (smime-get-key-by-email
80                (completing-read "Sign this part with what signature? "
81                                 smime-keys nil nil
82                                 (and (listp (car-safe smime-keys))
83                                      (caar smime-keys))))))))
84
85 (defun mml-smime-get-file-cert ()
86   (ignore-errors
87     (list 'certfile (read-file-name
88                      "File with recipient's S/MIME certificate: "
89                      smime-certificate-directory nil t ""))))
90
91 (defun mml-smime-get-dns-cert ()
92   ;; todo: deal with comma separated multiple recipients
93   (let (result who bad cert)
94     (condition-case ()
95         (while (not result)
96           (setq who (read-from-minibuffer
97                      (format "%sLookup certificate for: " (or bad ""))
98                      (cadr (funcall gnus-extract-address-components
99                                     (or (save-excursion
100                                           (save-restriction
101                                             (message-narrow-to-headers)
102                                             (message-fetch-field "to")))
103                                         "")))))
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))))
107       (quit))
108     result))
109
110 (defun mml-smime-encrypt-query ()
111   ;; todo: add ldap support (xemacs ldap api?)
112   ;; todo: try dns/ldap automatically first, before prompting user
113   (let (certs done)
114     (while (not done)
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? "))))
122     certs))
123
124 (defun mml-smime-verify (handle ctl)
125   (with-temp-buffer
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
143                              (buffer-string)))
144       (if (not good-signature)
145           (progn
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"
153                      openssl-output)))
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)))
157           (with-temp-buffer
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")
169           (if good-certificate
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"
177                  (if addresses
178                      (concat "Addresses in certificate: "
179                              (mapconcat 'identity addresses ", "))
180                    "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
181                  "\n" "\n"
182                  "OpenSSL output:\n"
183                  "---------------\n" openssl-output "\n"
184                  "Certificate(s) inside S/MIME signature:\n"
185                  "---------------------------------------\n"
186                  (buffer-string) "\n")))))
187   handle)
188
189 (defun mml-smime-verify-test (handle ctl)
190   smime-openssl-program)
191
192 (provide 'mml-smime)
193
194 ;;; mml-smime.el ends here