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)
120 (with-current-buffer (mm-handle-multipart-original-buffer ctl)
121 ;; xxx modifies buffer -- noone else uses the buffer, so what the heck
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 (if (smime-verify-buffer)
134 (mm-set-handle-multipart-parameter
135 mm-security-handle 'gnus-info "OK")
136 (kill-buffer smime-details-buffer))
137 (mm-set-handle-multipart-parameter
138 mm-security-handle 'gnus-info "Failed")
139 (mm-set-handle-multipart-parameter
140 mm-security-handle 'gnus-details
141 (with-current-buffer smime-details-buffer
145 (defun mml-smime-verify-test (handle ctl)
146 smime-openssl-program)
150 ;;; mml-smime.el ends here