1 ;;; tls.el --- TLS/SSL support via wrapper around GnuTLS
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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)
49 (autoload 'format-spec
"format-spec")
50 (autoload 'format-spec-make
"format-spec"))
53 "Transport Layer Security (TLS) parameters."
56 (defcustom tls-end-of-info
59 ;; `openssl s_client' regexp. See ssl/ssl_txt.c lines 219-220.
60 ;; According to apps/s_client.c line 1515 `---' is always the last
61 ;; line that is printed by s_client before the real data.
62 "^ Verify return code: .+\n---\n\\|"
63 ;; `gnutls' regexp. See src/cli.c lines 721-.
64 "^- Simple Client Mode:\n"
65 "\\(\n\\|" ; ignore blank lines
66 ;; According to GnuTLS v2.1.5 src/cli.c lines 640-650 and 705-715
67 ;; in `main' the handshake will start after this message. If the
68 ;; handshake fails, the programs will abort.
69 "^\\*\\*\\* Starting TLS handshake\n\\)*"
71 "Regexp matching end of TLS client informational messages.
72 Client data stream begins after the last character matched by
73 this. The default matches `openssl s_client' (version 0.9.8c)
74 and `gnutls-cli' (version 2.0.1) output."
79 (defcustom tls-program
'("gnutls-cli -p %p %h"
80 "gnutls-cli -p %p %h --protocols ssl3"
81 "openssl s_client -connect %h:%p -no_ssl2 -ign_eof")
82 "List of strings containing commands to start TLS stream to a host.
83 Each entry in the list is tried until a connection is successful.
84 %h is replaced with server hostname, %p with port to connect to.
85 The program should read input on stdin and write output to
88 See `tls-checktrust' on how to check trusted root certs.
90 Also see `tls-success' for what the program should output after
91 successful negotiation."
94 (list :tag
"Choose commands"
96 ("gnutls-cli -p %p %h"
97 "gnutls-cli -p %p %h --protocols ssl3"
98 "openssl s_client -connect %h:%p -no_ssl2 -ign_eof")
100 ;; FIXME: add brief `:tag "..."' descriptions.
101 ;; (repeat :inline t :tag "Other" (string))
102 ;; See `tls-checktrust':
103 (const "gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h")
104 (const "gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h --protocols ssl3")
105 (const "openssl s_client -connect %h:%p -CAfile /etc/ssl/certs/ca-certificates.crt -no_ssl2 -ign_eof")
107 (const "gnutls-cli -p %p %h")
108 (const "gnutls-cli -p %p %h --protocols ssl3")
109 (const "openssl s_client -connect %h:%p -no_ssl2 -ign_eof"))
110 (repeat :inline t
:tag
"Other" (string)))
111 (const :tag
"Default list of commands"
112 ("gnutls-cli -p %p %h"
113 "gnutls-cli -p %p %h --protocols ssl3"
114 "openssl s_client -connect %h:%p -no_ssl2 -ign_eof"))
115 (list :tag
"List of commands"
116 (repeat :tag
"Command" (string))))
120 (defcustom tls-process-connection-type nil
121 "Value for `process-connection-type' to use when starting TLS process."
126 (defcustom tls-success
"- Handshake was completed\\|SSL handshake has read "
127 "Regular expression indicating completed TLS handshakes.
128 The default is what GNUTLS's \"gnutls-cli\" or OpenSSL's
129 \"openssl s_client\" outputs."
134 (defcustom tls-checktrust nil
135 "Indicate if certificates should be checked against trusted root certs.
136 If this is `ask', the user can decide whether to accept an
137 untrusted certificate. You may have to adapt `tls-program' in
138 order to make this feature work properly, i.e., to ensure that
139 the external program knows about the root certificates you
140 consider trustworthy, e.g.:
143 '(\"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h\"
144 \"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h --protocols ssl3\"
145 \"openssl s_client -connect %h:%p -CAfile /etc/ssl/certs/ca-certificates.crt -no_ssl2 -ign_eof\"))"
146 :type
'(choice (const :tag
"Always" t
)
147 (const :tag
"Never" nil
)
148 (const :tag
"Ask" ask
))
149 :version
"23.1" ;; No Gnus
152 (defcustom tls-untrusted
153 "- Peer's certificate is NOT trusted\\|Verify return code: \\([^0] \\|.[^ ]\\)"
154 "Regular expression indicating failure of TLS certificate verification.
155 The default is what GNUTLS's \"gnutls-cli\" or OpenSSL's
156 \"openssl s_client\" return in the event of unsuccessful
159 :version
"23.1" ;; No Gnus
162 (defcustom tls-hostmismatch
163 "# The hostname in the certificate does NOT match"
164 "Regular expression indicating a host name mismatch in certificate.
165 When the host name specified in the certificate doesn't match the
166 name of the host you are connecting to, gnutls-cli issues a
167 warning to this effect. There is no such feature in openssl. Set
168 this to nil if you want to ignore host name mismatches."
170 :version
"23.1" ;; No Gnus
173 (defcustom tls-certtool-program
(executable-find "certtool")
174 "Name of GnuTLS certtool.
175 Used by `tls-certificate-information'."
180 (defun tls-certificate-information (der)
181 "Parse X.509 certificate in DER format into an assoc list."
182 (let ((certificate (concat "-----BEGIN CERTIFICATE-----\n"
183 (base64-encode-string der
)
184 "\n-----END CERTIFICATE-----\n"))
186 (with-current-buffer (get-buffer-create " *certtool*")
189 (setq exit-code
(condition-case ()
190 (call-process-region (point-min) (point-max)
192 t
(list (current-buffer) nil
) t
193 "--certificate-info")
198 (goto-char (point-min))
199 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)" nil t
)
200 (push (cons (match-string 1) (match-string 2)) vals
))
203 (defun open-tls-stream (name buffer host port
)
204 "Open a TLS connection for a port to a host.
205 Returns a subprocess-object to represent the connection.
206 Input and output work as for subprocesses; `delete-process' closes it.
207 Args are NAME BUFFER HOST PORT.
208 NAME is name for process. It is modified if necessary to make it unique.
209 BUFFER is the buffer (or buffer name) to associate with the process.
210 Process output goes at end of that buffer, unless you specify
211 an output stream or filter function to handle the output.
212 BUFFER may be also nil, meaning that this process is not associated
214 Third arg is name of the host to connect to, or its IP address.
215 Fourth arg PORT is an integer specifying a port to connect to."
216 (let ((cmds tls-program
)
217 (use-temp-buffer (null buffer
))
220 (setq buffer
(generate-new-buffer " TLS")))
221 (with-current-buffer buffer
222 (message "Opening TLS connection to `%s'..." host
)
223 (while (and (not done
) (setq cmd
(pop cmds
)))
224 (message "Opening TLS connection with `%s'..." cmd
)
225 (let ((process-connection-type tls-process-connection-type
)
227 (setq process
(start-process
228 name buffer shell-file-name shell-command-switch
233 ?p
(if (integerp port
)
237 (memq (process-status process
) '(open run
))
239 (goto-char (point-min))
240 (not (setq done
(re-search-forward
241 tls-success nil t
)))))
242 (unless (accept-process-output process
1)
244 (message "Opening TLS connection with `%s'...%s" cmd
245 (if done
"done" "failed"))
247 (delete-process process
)
248 ;; advance point to after all informational messages that
249 ;; `openssl s_client' and `gnutls' print
250 (let ((start-of-data nil
))
252 (not (setq start-of-data
253 ;; the string matching `tls-end-of-info'
254 ;; might come in separate chunks from
255 ;; `accept-process-output', so start the
256 ;; search where `tls-success' ended
258 (if (re-search-forward tls-end-of-info nil t
)
260 (accept-process-output process
1))
262 ;; move point to start of client data
263 (goto-char start-of-data
)))
264 (setq done process
))))
269 (goto-char (point-min))
270 (re-search-forward tls-untrusted nil t
))
272 (and (not (eq tls-checktrust
'ask
))
273 (message "The certificate presented by `%s' is \
276 (format "The certificate presented by `%s' is \
277 NOT trusted. Accept anyway? " host
)))))
278 (and tls-hostmismatch
280 (goto-char (point-min))
281 (re-search-forward tls-hostmismatch nil t
))
283 (format "Host name in certificate doesn't \
284 match `%s'. Connect anyway? " host
))))))
286 (delete-process process
)))
287 (message "Opening TLS connection to `%s'...%s"
288 host
(if done
"done" "failed"))
289 (when use-temp-buffer
290 (if done
(set-process-buffer process nil
))
291 (kill-buffer buffer
))
296 ;; arch-tag: 5596d1c4-facc-4bc4-94a9-9863b928d7ac