1 ;;; tls.el --- TLS/SSL support via wrapper around GnuTLS
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; Keywords: comm, tls, gnutls, ssl
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This package implements a simple wrapper around "gnutls-cli" to
27 ;; make Emacs support TLS/SSL.
29 ;; Usage is the same as `open-network-stream', i.e.:
31 ;; (setq tmp (open-tls-stream "test" (current-buffer) "news.mozilla.org" 563))
34 ;; (process-send-string tmp "mode reader\n")
35 ;; 200 secnews.netscape.com Netscape-Collabra/3.52 03615 NNRP ready ...
37 ;; (process-send-string tmp "quit\n")
41 ;; To use this package as a replacement for ssl.el by William M. Perry
42 ;; <wmperry@cs.indiana.edu>, you need to evaluate the following:
44 ;; (defalias 'open-ssl-stream 'open-tls-stream)
48 (autoload 'format-spec "format-spec")
49 (autoload 'format-spec-make "format-spec")
52 "Transport Layer Security (TLS) parameters."
55 (defcustom tls-end-of-info
58 ;; `openssl s_client' regexp. See ssl/ssl_txt.c lines 219-220.
59 ;; According to apps/s_client.c line 1515 `---' is always the last
60 ;; line that is printed by s_client before the real data.
61 "^ Verify return code: .+\n---\n\\|"
62 ;; `gnutls' regexp. See src/cli.c lines 721-.
63 "^- Simple Client Mode:\n"
64 "\\(\n\\|" ; ignore blank lines
65 ;; According to GnuTLS v2.1.5 src/cli.c lines 640-650 and 705-715
66 ;; in `main' the handshake will start after this message. If the
67 ;; handshake fails, the programs will abort.
68 "^\\*\\*\\* Starting TLS handshake\n\\)*"
70 "Regexp matching end of TLS client informational messages.
71 Client data stream begins after the last character matched by
72 this. The default matches `openssl s_client' (version 0.9.8c)
73 and `gnutls-cli' (version 2.0.1) output."
78 (defvar tls-starttls-switches
80 ("openssl" "-starttls imap"))
81 "Alist of programs and the switches necessary to get starttls behaviour.")
83 (defcustom tls-program '("gnutls-cli %s -p %p %h"
84 "gnutls-cli %s -p %p %h --protocols ssl3"
85 "openssl s_client %s -connect %h:%p -no_ssl2 -ign_eof")
86 "List of strings containing commands to start TLS stream to a host.
87 Each entry in the list is tried until a connection is successful.
88 %h is replaced with server hostname, %p with port to connect to.
89 The program should read input on stdin and write output to
92 See `tls-checktrust' on how to check trusted root certs.
94 Also see `tls-success' for what the program should output after
95 successful negotiation."
98 (list :tag "Choose commands"
100 ("gnutls-cli -p %p %h"
101 "gnutls-cli -p %p %h --protocols ssl3"
102 "openssl s_client -connect %h:%p -no_ssl2 -ign_eof")
104 ;; FIXME: add brief `:tag "..."' descriptions.
105 ;; (repeat :inline t :tag "Other" (string))
106 ;; See `tls-checktrust':
107 (const "gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h")
108 (const "gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h --protocols ssl3")
109 (const "openssl s_client -connect %h:%p -CAfile /etc/ssl/certs/ca-certificates.crt -no_ssl2 -ign_eof")
111 (const "gnutls-cli -p %p %h")
112 (const "gnutls-cli -p %p %h --protocols ssl3")
113 (const "openssl s_client -connect %h:%p -no_ssl2 -ign_eof"))
114 (repeat :inline t :tag "Other" (string)))
115 (const :tag "Default list of commands"
116 ("gnutls-cli -p %p %h"
117 "gnutls-cli -p %p %h --protocols ssl3"
118 "openssl s_client -connect %h:%p -no_ssl2 -ign_eof"))
119 (list :tag "List of commands"
120 (repeat :tag "Command" (string))))
124 (defcustom tls-process-connection-type nil
125 "Value for `process-connection-type' to use when starting TLS process."
130 (defcustom tls-success "- Handshake was completed\\|SSL handshake has read "
131 "Regular expression indicating completed TLS handshakes.
132 The default is what GNUTLS's \"gnutls-cli\" or OpenSSL's
133 \"openssl s_client\" outputs."
138 (defcustom tls-checktrust nil
139 "Indicate if certificates should be checked against trusted root certs.
140 If this is `ask', the user can decide whether to accept an
141 untrusted certificate. You may have to adapt `tls-program' in
142 order to make this feature work properly, i.e., to ensure that
143 the external program knows about the root certificates you
144 consider trustworthy, e.g.:
147 '(\"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h\"
148 \"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h --protocols ssl3\"
149 \"openssl s_client -connect %h:%p -CAfile /etc/ssl/certs/ca-certificates.crt -no_ssl2 -ign_eof\"))"
150 :type '(choice (const :tag "Always" t)
151 (const :tag "Never" nil)
152 (const :tag "Ask" ask))
153 :version "23.1" ;; No Gnus
156 (defcustom tls-untrusted
157 "- Peer's certificate is NOT trusted\\|Verify return code: \\([^0] \\|.[^ ]\\)"
158 "Regular expression indicating failure of TLS certificate verification.
159 The default is what GNUTLS's \"gnutls-cli\" or OpenSSL's
160 \"openssl s_client\" return in the event of unsuccessful
163 :version "23.1" ;; No Gnus
166 (defcustom tls-hostmismatch
167 "# The hostname in the certificate does NOT match"
168 "Regular expression indicating a host name mismatch in certificate.
169 When the host name specified in the certificate doesn't match the
170 name of the host you are connecting to, gnutls-cli issues a
171 warning to this effect. There is no such feature in openssl. Set
172 this to nil if you want to ignore host name mismatches."
174 :version "23.1" ;; No Gnus
177 (defcustom tls-certtool-program (executable-find "certtool")
178 "Name of GnuTLS certtool.
179 Used by `tls-certificate-information'."
184 (defun tls-certificate-information (der)
185 "Parse X.509 certificate in DER format into an assoc list."
186 (let ((certificate (concat "-----BEGIN CERTIFICATE-----\n"
187 (base64-encode-string der)
188 "\n-----END CERTIFICATE-----\n"))
190 (with-current-buffer (get-buffer-create " *certtool*")
193 (setq exit-code (condition-case ()
194 (call-process-region (point-min) (point-max)
196 t (list (current-buffer) nil) t
197 "--certificate-info")
202 (goto-char (point-min))
203 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)" nil t)
204 (push (cons (match-string 1) (match-string 2)) vals))
207 (defun open-tls-stream (name buffer host port &optional starttlsp)
208 "Open a TLS connection for a port to a host.
209 Returns a subprocess-object to represent the connection.
210 Input and output work as for subprocesses; `delete-process' closes it.
211 Args are NAME BUFFER HOST PORT.
212 NAME is name for process. It is modified if necessary to make it unique.
213 BUFFER is the buffer (or buffer name) to associate with the process.
214 Process output goes at end of that buffer, unless you specify
215 an output stream or filter function to handle the output.
216 BUFFER may be also nil, meaning that this process is not associated
218 Third arg is name of the host to connect to, or its IP address.
219 Fourth arg PORT is an integer specifying a port to connect to."
220 (let ((cmds tls-program)
221 (use-temp-buffer (null buffer))
224 (setq buffer (generate-new-buffer " TLS"))
225 ;; BUFFER is a string but does not exist as a buffer object.
226 (unless (and (get-buffer buffer)
227 (buffer-name (get-buffer buffer)))
228 (generate-new-buffer buffer)))
229 (with-current-buffer buffer
230 (message "Opening TLS connection to `%s'..." host)
231 (while (and (not done) (setq cmd (pop cmds)))
232 (let ((process-connection-type tls-process-connection-type)
238 (tls-find-starttls-argument cmd)
241 ?p (if (integerp port)
245 (message "Opening TLS connection with `%s'..." formatted-cmd)
246 (setq process (start-process
247 name buffer shell-file-name shell-command-switch
249 (funcall (if (fboundp 'set-process-query-on-exit-flag)
250 'set-process-query-on-exit-flag
251 'process-kill-without-query)
254 (memq (process-status process) '(open run))
256 (goto-char (point-min))
257 (not (setq done (re-search-forward
258 tls-success nil t)))))
259 (unless (accept-process-output process 1)
261 (message "Opening TLS connection with `%s'...%s" formatted-cmd
262 (if done "done" "failed"))
264 (delete-process process)
265 ;; advance point to after all informational messages that
266 ;; `openssl s_client' and `gnutls' print
267 (let ((start-of-data nil))
269 (not (setq start-of-data
270 ;; the string matching `tls-end-of-info'
271 ;; might come in separate chunks from
272 ;; `accept-process-output', so start the
273 ;; search where `tls-success' ended
275 (if (re-search-forward tls-end-of-info nil t)
277 (accept-process-output process 1))
279 ;; move point to start of client data
280 (goto-char start-of-data)))
281 (setq done process))))
286 (goto-char (point-min))
287 (re-search-forward tls-untrusted nil t))
289 (and (not (eq tls-checktrust 'ask))
290 (message "The certificate presented by `%s' is \
293 (format "The certificate presented by `%s' is \
294 NOT trusted. Accept anyway? " host)))))
295 (and tls-hostmismatch
297 (goto-char (point-min))
298 (re-search-forward tls-hostmismatch nil t))
300 (format "Host name in certificate doesn't \
301 match `%s'. Connect anyway? " host))))))
303 (delete-process process)))
304 (message "Opening TLS connection to `%s'...%s"
305 host (if done "done" "failed"))
306 (when use-temp-buffer
307 (if done (set-process-buffer process nil))
308 (kill-buffer buffer))
311 (defun tls-find-starttls-argument (command)
312 (let ((command (car (split-string command))))
313 (or (cadr (assoc command tls-starttls-switches))