2001-01-15 Simon Josefsson <simon@josefsson.org>
[gnus] / lisp / mml-smime.el
1 ;;; mml-smime.el --- S/MIME support for MML
2 ;; Copyright (c) 2000 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
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))
45                     (get-buffer tmp)))
46           (push tmp certfiles)
47         (setq file (make-temp-name mm-tmp-directory))
48         (with-current-buffer tmp
49           (write-region (point-min) (point-max) file))
50         (push file certfiles)
51         (push file tmpfiles)))
52     (if (smime-encrypt-buffer certfiles)
53         (progn
54           (while (setq tmp (pop tmpfiles))
55             (delete-file tmp))
56           t)
57       (while (setq tmp (pop tmpfiles))
58         (delete-file tmp))
59       nil)))
60
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"))
67   (list 'keyfile
68         (if (= (length smime-keys) 1)
69             (cadar smime-keys)
70           (or (let ((from (cadr (funcall gnus-extract-address-components
71                                          (or (save-excursion
72                                                (save-restriction
73                                                  (message-narrow-to-headers)
74                                                  (message-fetch-field "from")))
75                                              "")))))
76                 (and from (smime-get-key-by-email from)))
77               (smime-get-key-by-email
78                (completing-read "Sign this part with what signature? "
79                                 smime-keys nil nil
80                                 (and (listp (car-safe smime-keys)) 
81                                      (caar smime-keys))))))))
82
83 (defun mml-smime-get-file-cert ()
84   (ignore-errors
85     (list 'certfile (read-file-name
86                      "File with recipient's S/MIME certificate: "
87                      smime-certificate-directory nil t ""))))
88
89 (defun mml-smime-get-dns-cert ()
90   ;; todo: deal with comma separated multiple recipients
91   (let (result who bad cert)
92     (condition-case ()
93         (while (not result)
94           (setq who (read-from-minibuffer
95                      (format "%sLookup certificate for: " (or bad ""))
96                      (cadr (funcall gnus-extract-address-components 
97                                     (or (save-excursion
98                                           (save-restriction
99                                             (message-narrow-to-headers)
100                                             (message-fetch-field "to")))
101                                         "")))))
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))))
105       (quit))
106     result))
107
108 (defun mml-smime-encrypt-query ()
109   ;; todo: add ldap support (xemacs ldap api?)
110   ;; todo: try dns/ldap automatically first, before prompting user
111   (let (certs done)
112     (while (not done)
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? "))))
120     certs))
121
122 (defun mml-smime-verify (handle ctl)
123   (with-temp-buffer
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
141                              (buffer-string)))
142       (if (not good-signature)
143           (progn
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" 
151                      openssl-output)))
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)))
155           (with-temp-buffer
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 mm-security-from) addresses))
165             (mm-set-handle-multipart-parameter 
166              mm-security-handle 'gnus-info "Sender address forged")
167           (if good-certificate
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-security-from "\n"
175                  (if addresses
176                      (concat "Addresses in certificate: " 
177                              (mapconcat 'identity addresses ", "))
178                    "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
179                  "\n" "\n" 
180                  "OpenSSL output:\n" 
181                  "---------------\n" openssl-output "\n"
182                  "Certificate(s) inside S/MIME signature:\n"
183                  "---------------------------------------\n"
184                  (buffer-string) "\n")))))
185   handle)
186
187 (defun mml-smime-verify-test (handle ctl)
188   smime-openssl-program)
189
190 (provide 'mml-smime)
191
192 ;;; mml-smime.el ends here