1 ;;; smime.el --- S/MIME interface.
3 ;; Copyright (C) 1999 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
7 ;; Keywords: S/MIME, OpenSSL
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program 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.
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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
29 ;; This module is based on
31 ;; [SMIMEV3] RFC 2633: "S/MIME Version 3 Message Specification"
32 ;; by Crocker, D., Flanigan, B., Hoffman, P., Housley, R.,
33 ;; Pawling, J. and Schaad, J. (1999/06)
35 ;; [SMIMEV2] RFC 2311: "S/MIME Version 2 Message Specification"
36 ;; by Dusse, S., Hoffman, P., Ramsdell, B., Lundblade, L.
37 ;; and L. Repka. (1998/03)
43 ;; binary-funcall, binary-write-decoded-region, binary-insert-encoded-file
44 (eval-when-compile (require 'static))
50 (defcustom smime-program "smime"
51 "The S/MIME executable."
55 (defcustom smime-shell-file-name "/bin/sh"
56 "File name to load inferior shells from. Bourne shell or its equivalent
57 \(not tcsh) is needed for \"2>\"."
61 (defcustom smime-shell-command-switch "-c"
62 "Switch used to have the shell execute its command line argument."
66 (defcustom smime-x509-program
67 (let ((file (exec-installed-p "openssl")))
68 (and file (list file "x509" "-noout")))
69 "External program for x509 parser."
73 (defcustom smime-cache-passphrase t
78 (defcustom smime-certificate-directory "~/.w3/certs"
79 "Certificate directory."
83 (defcustom smime-public-key-file nil
88 (defcustom smime-private-key-file nil
93 (defvar smime-errors-buffer " *S/MIME errors*")
94 (defvar smime-output-buffer " *S/MIME output*")
96 ;;; @ utility functions
98 (put 'smime-process-when-success 'lisp-indent-function 0)
100 (defmacro smime-process-when-success (&rest body)
101 `(with-current-buffer smime-output-buffer
102 (if (zerop (buffer-size)) nil ,@body t)))
104 (defvar smime-passphrase-cache-expiry 16)
105 (defvar smime-passphrase-cache (make-vector 7 0))
107 (defvar smime-read-passphrase nil)
108 (defun smime-read-passphrase (prompt &optional key)
109 (if (not smime-read-passphrase)
110 (if (functionp 'read-passwd)
111 (setq smime-read-passphrase 'read-passwd)
112 (if (load "passwd" t)
113 (setq smime-read-passphrase 'read-passwd)
114 (autoload 'ange-ftp-read-passwd "ange-ftp")
115 (setq smime-read-passphrase 'ange-ftp-read-passwd))))
116 (or (and smime-cache-passphrase
117 (symbol-value (intern-soft key smime-passphrase-cache)))
118 (funcall smime-read-passphrase prompt)))
120 (defun smime-add-passphrase-cache (key passphrase)
121 (set (intern key smime-passphrase-cache)
123 (run-at-time smime-passphrase-cache-expiry nil
124 #'smime-remove-passphrase-cache
127 (defun smime-remove-passphrase-cache (key)
128 (let ((passphrase (symbol-value (intern-soft key smime-passphrase-cache))))
130 (fillarray passphrase ?_)
131 (unintern key smime-passphrase-cache))))
133 (defsubst smime-parse-attribute (string)
136 (if (string-match "=" attr)
137 (cons (intern (substring attr 0 (match-beginning 0)))
138 (substring attr (match-end 0)))
140 (split-string string "/"))))
142 (defsubst smime-query-signer (start end)
143 (smime-process-region start end smime-program (list "-qs"))
144 (with-current-buffer smime-output-buffer
145 (if (zerop (buffer-size)) nil
146 (goto-char (point-min))
147 (when (re-search-forward "^/" nil t)
148 (smime-parse-attribute
149 (buffer-substring (point) (progn (end-of-line)(point)))))
152 (defsubst smime-x509-hash (cert-file)
153 (with-current-buffer (get-buffer-create smime-output-buffer)
154 (buffer-disable-undo)
156 (apply #'call-process (car smime-x509-program) nil t nil
157 (append (cdr smime-x509-program)
158 (list "-hash" "-in" cert-file)))
159 (if (zerop (buffer-size)) nil
160 (buffer-substring (point-min) (1- (point-max))))))
162 (defsubst smime-x509-subject (cert-file)
163 (with-current-buffer (get-buffer-create smime-output-buffer)
164 (buffer-disable-undo)
166 (apply #'call-process (car smime-x509-program) nil t nil
167 (append (cdr smime-x509-program)
168 (list "-subject" "-in" cert-file)))
169 (if (zerop (buffer-size)) nil
170 (goto-char (point-min))
171 (when (re-search-forward "^subject=" nil t)
172 (smime-parse-attribute
173 (buffer-substring (point)(progn (end-of-line)(point))))))))
175 (defsubst smime-find-certificate (attr)
177 (and (file-directory-p smime-certificate-directory)
178 (delq nil (mapcar (lambda (file)
179 (if (file-directory-p file) nil
182 smime-certificate-directory
186 (if (or (string-equal
187 (cdr (assq 'CN (smime-x509-subject (car files))))
188 (cdr (assq 'CN attr)))
190 (cdr (assq 'Email (smime-x509-subject (car files))))
191 (cdr (assq 'Email attr))))
192 (throw 'found (car files)))
195 (defun smime-process-region (start end program args)
196 (let* ((errors-file-name
197 (concat temporary-file-directory
198 (make-temp-name "smime-errors")))
199 (args (append args (list (concat "2>" errors-file-name))))
200 (shell-file-name smime-shell-file-name)
201 (shell-command-switch smime-shell-command-switch)
202 (process-connection-type nil)
203 process status exit-status)
204 (with-current-buffer (get-buffer-create smime-output-buffer)
205 (buffer-disable-undo)
208 (apply #'binary-funcall #'start-process-shell-command
209 "*S/MIME*" smime-output-buffer
211 (set-process-sentinel process 'ignore)
212 (process-send-region process start end)
213 (process-send-eof process)
214 (while (eq 'run (process-status process))
215 (accept-process-output process 5))
216 (setq status (process-status process)
217 exit-status (process-exit-status process))
218 (delete-process process)
219 (with-current-buffer smime-output-buffer
220 (goto-char (point-min))
221 (while (re-search-forward "\r$" (point-max) t)
224 (if (memq status '(stop signal))
225 (error "%s exited abnormally: '%s'" program exit-status))
226 (if (= 127 exit-status)
227 (error "%s could not be found" program))
229 (set-buffer (get-buffer-create smime-errors-buffer))
230 (buffer-disable-undo)
232 (insert-file-contents errors-file-name)
233 (delete-file errors-file-name)
235 (if (and process (eq 'run (process-status process)))
236 (interrupt-process process))
240 ;;; @ interface functions
244 (defun smime-encrypt-region (start end)
245 "Encrypt the current region between START and END."
247 (or smime-private-key-file
248 (expand-file-name (read-file-name "Public key file: "))))
249 (args (list "-e" key-file)))
250 (smime-process-region start end smime-program args)
251 (smime-process-when-success
252 (goto-char (point-min))
253 (delete-region (point-min) (progn
254 (re-search-forward "^$" nil t)
258 (defun smime-decrypt-region (start end)
259 "Decrypt the current region between START and END."
261 (or smime-private-key-file
262 (expand-file-name (read-file-name "Private key file: "))))
263 (hash (smime-x509-hash key-file))
264 (passphrase (smime-read-passphrase
265 (format "S/MIME passphrase for %s: " hash)
267 (args (list "-d" key-file passphrase)))
268 (smime-process-region start end smime-program args)
269 (smime-process-when-success
270 (when smime-cache-passphrase
271 (smime-add-passphrase-cache hash passphrase)))))
274 (defun smime-sign-region (start end &optional cleartext)
275 "Make the signature from text between START and END.
276 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
277 a detached signature."
279 (or smime-private-key-file
280 (expand-file-name (read-file-name "Private key file: "))))
281 (hash (smime-x509-hash key-file))
282 (passphrase (smime-read-passphrase
283 (format "S/MIME passphrase for %s: " hash)
285 (args (list "-ds" key-file passphrase)))
286 (smime-process-region start end smime-program args)
287 (smime-process-when-success
288 (goto-char (point-min))
289 (delete-region (point-min) (progn
290 (re-search-forward "^$" nil t)
292 (when smime-cache-passphrase
293 (smime-add-passphrase-cache hash passphrase)))))
296 (defun smime-verify-region (start end signature)
297 "Verify the current region between START and END.
298 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
299 the detached signature of the current region."
300 (let* ((basename (expand-file-name "smime" temporary-file-directory))
301 (orig-file (make-temp-name basename))
302 (orig-mode (default-file-modes)))
305 (set-default-file-modes 448)
306 (binary-write-decoded-region start end orig-file))
307 (set-default-file-modes orig-mode))
309 (binary-insert-encoded-file signature)
310 (goto-char (point-max))
311 (binary-insert-encoded-file
312 (or (smime-find-certificate
313 (smime-query-signer (point-min)(point-max)))
315 (read-file-name "Certificate file: "))))
316 (smime-process-region (point-min)(point-max) smime-program
317 (list "-dv" orig-file)))
318 (smime-process-when-success nil)))
322 ;;; smime.el ends here