]> code.delx.au - gnu-emacs/blob - lisp/net/tls.el
Merge branch 'emacs-25-merge'
[gnu-emacs] / lisp / net / tls.el
1 ;;; tls.el --- TLS/SSL support via wrapper around GnuTLS
2
3 ;; Copyright (C) 1996-1999, 2002-2015 Free Software Foundation, Inc.
4
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; Keywords: comm, tls, gnutls, ssl
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This package implements a simple wrapper around "gnutls-cli" to
26 ;; make Emacs support TLS/SSL.
27 ;;
28 ;; Usage is the same as `open-network-stream', i.e.:
29 ;;
30 ;; (setq tmp (open-tls-stream "test" (current-buffer) "news.mozilla.org" 563))
31 ;; ...
32 ;; #<process test>
33 ;; (process-send-string tmp "mode reader\n")
34 ;; 200 secnews.netscape.com Netscape-Collabra/3.52 03615 NNRP ready ...
35 ;; nil
36 ;; (process-send-string tmp "quit\n")
37 ;; 205
38 ;; nil
39
40 ;; To use this package as a replacement for ssl.el by William M. Perry
41 ;; <wmperry@cs.indiana.edu>, you need to evaluate the following:
42 ;;
43 ;; (defalias 'open-ssl-stream 'open-tls-stream)
44
45 ;;; Code:
46
47 (require 'gnutls)
48
49 (autoload 'format-spec "format-spec")
50 (autoload 'format-spec-make "format-spec")
51
52 (defgroup tls nil
53 "Transport Layer Security (TLS) parameters."
54 :group 'comm)
55
56 (defcustom tls-end-of-info
57 (concat
58 "\\("
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\\)*"
70 "\\)")
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."
75 :version "22.2"
76 :type 'regexp
77 :group 'tls)
78
79 (defcustom tls-program
80 '("gnutls-cli --x509cafile %t -p %p %h"
81 "gnutls-cli --x509cafile %t -p %p %h --protocols ssl3"
82 "openssl s_client -connect %h:%p -no_ssl2 -ign_eof")
83 "List of strings containing commands to start TLS stream to a host.
84 Each entry in the list is tried until a connection is successful.
85 %h is replaced with server hostname, %p with port to connect to.
86 The program should read input on stdin and write output to stdout.
87
88 See `tls-checktrust' on how to check trusted root certs.
89
90 Also see `tls-success' for what the program should output after
91 successful negotiation."
92 :type
93 '(choice
94 (const :tag "Default list of commands"
95 ("gnutls-cli --x509cafile %t -p %p %h"
96 "gnutls-cli --x509cafile %t -p %p %h --protocols ssl3"
97 "openssl s_client -CAfile %t -connect %h:%p -no_ssl2 -ign_eof"))
98 (list :tag "Choose commands"
99 :value
100 ("gnutls-cli --x509cafile %t -p %p %h"
101 "gnutls-cli --x509cafile %t -p %p %h --protocols ssl3"
102 "openssl s_client -connect %h:%p -no_ssl2 -ign_eof")
103 (set :inline t
104 ;; FIXME: add brief `:tag "..."' descriptions.
105 ;; (repeat :inline t :tag "Other" (string))
106 ;; No trust check:
107 (const "gnutls-cli --insecure -p %p %h")
108 (const "gnutls-cli --insecure -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 (list :tag "List of commands"
112 (repeat :tag "Command" (string))))
113 :version "22.1"
114 :group 'tls)
115
116 (defcustom tls-process-connection-type nil
117 "Value for `process-connection-type' to use when starting TLS process."
118 :version "22.1"
119 :type 'boolean
120 :group 'tls)
121
122 (defcustom tls-success "- Handshake was completed\\|SSL handshake has read "
123 "Regular expression indicating completed TLS handshakes.
124 The default is what GnuTLS's \"gnutls-cli\" or OpenSSL's
125 \"openssl s_client\" outputs."
126 :version "22.1"
127 :type 'regexp
128 :group 'tls)
129
130 (defcustom tls-checktrust nil
131 "Indicate if certificates should be checked against trusted root certs.
132 If this is `ask', the user can decide whether to accept an
133 untrusted certificate. You may have to adapt `tls-program' in
134 order to make this feature work properly, i.e., to ensure that
135 the external program knows about the root certificates you
136 consider trustworthy, e.g.:
137
138 \(setq tls-program
139 \\='(\"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h\"
140 \"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h --protocols ssl3\"
141 \"openssl s_client -connect %h:%p -CAfile /etc/ssl/certs/ca-certificates.crt -no_ssl2 -ign_eof\"))"
142 :type '(choice (const :tag "Always" t)
143 (const :tag "Never" nil)
144 (const :tag "Ask" ask))
145 :version "23.1" ;; No Gnus
146 :group 'tls)
147
148 (defcustom tls-untrusted
149 "- Peer's certificate is NOT trusted\\|Verify return code: \\([^0] \\|.[^ ]\\)"
150 "Regular expression indicating failure of TLS certificate verification.
151 The default is what GnuTLS's \"gnutls-cli\" or OpenSSL's
152 \"openssl s_client\" return in the event of unsuccessful
153 verification."
154 :type 'regexp
155 :version "23.1" ;; No Gnus
156 :group 'tls)
157
158 (defcustom tls-hostmismatch
159 "# The hostname in the certificate does NOT match"
160 "Regular expression indicating a host name mismatch in certificate.
161 When the host name specified in the certificate doesn't match the
162 name of the host you are connecting to, gnutls-cli issues a
163 warning to this effect. There is no such feature in openssl. Set
164 this to nil if you want to ignore host name mismatches."
165 :type 'regexp
166 :version "23.1" ;; No Gnus
167 :group 'tls)
168
169 (defcustom tls-certtool-program "certtool"
170 "Name of GnuTLS certtool.
171 Used by `tls-certificate-information'."
172 :version "22.1"
173 :type 'string
174 :group 'tls)
175
176 (defalias 'tls-format-message
177 (if (fboundp 'format-message) 'format-message
178 ;; for Emacs < 25, and XEmacs, don't worry about quote translation.
179 'format))
180
181 (defun tls-certificate-information (der)
182 "Parse X.509 certificate in DER format into an assoc list."
183 (let ((certificate (concat "-----BEGIN CERTIFICATE-----\n"
184 (base64-encode-string der)
185 "\n-----END CERTIFICATE-----\n"))
186 (exit-code 0))
187 (with-current-buffer (get-buffer-create " *certtool*")
188 (erase-buffer)
189 (insert certificate)
190 (setq exit-code (condition-case ()
191 (call-process-region (point-min) (point-max)
192 tls-certtool-program
193 t (list (current-buffer) nil) t
194 "--certificate-info")
195 (error -1)))
196 (if (/= exit-code 0)
197 nil
198 (let ((vals nil))
199 (goto-char (point-min))
200 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)" nil t)
201 (push (cons (match-string 1) (match-string 2)) vals))
202 (nreverse vals))))))
203
204 (defun open-tls-stream (name buffer host port)
205 "Open a TLS connection for a port to a host.
206 Returns a subprocess-object to represent the connection.
207 Input and output work as for subprocesses; `delete-process' closes it.
208 Args are NAME BUFFER HOST PORT.
209 NAME is name for process. It is modified if necessary to make it unique.
210 BUFFER is the buffer (or buffer name) to associate with the process.
211 Process output goes at end of that buffer, unless you specify
212 an output stream or filter function to handle the output.
213 BUFFER may be also nil, meaning that this process is not associated
214 with any buffer
215 Third arg is name of the host to connect to, or its IP address.
216 Fourth arg PORT is an integer specifying a port to connect to."
217 (let ((cmds tls-program)
218 (use-temp-buffer (null buffer))
219 process cmd done)
220 (if use-temp-buffer
221 (setq buffer (generate-new-buffer " TLS"))
222 ;; BUFFER is a string but does not exist as a buffer object.
223 (unless (and (get-buffer buffer)
224 (buffer-name (get-buffer buffer)))
225 (generate-new-buffer buffer)))
226 (with-current-buffer buffer
227 (message "Opening TLS connection to `%s'..." host)
228 (while (and (not done) (setq cmd (pop cmds)))
229 (let ((process-connection-type tls-process-connection-type)
230 (formatted-cmd
231 (format-spec
232 cmd
233 (format-spec-make
234 ?t (car (gnutls-trustfiles))
235 ?h host
236 ?p (if (integerp port)
237 (int-to-string port)
238 port)))))
239 (message "Opening TLS connection with `%s'..." formatted-cmd)
240 (setq process (start-process
241 name buffer shell-file-name shell-command-switch
242 formatted-cmd))
243 (while (and process
244 (memq (process-status process) '(open run))
245 (progn
246 (goto-char (point-min))
247 (not (setq done (re-search-forward
248 tls-success nil t)))))
249 (unless (accept-process-output process 1)
250 (sit-for 1)))
251 (message "Opening TLS connection with `%s'...%s" formatted-cmd
252 (if done "done" "failed"))
253 (if (not done)
254 (delete-process process)
255 ;; advance point to after all informational messages that
256 ;; `openssl s_client' and `gnutls' print
257 (let ((start-of-data nil))
258 (while
259 (not (setq start-of-data
260 ;; the string matching `tls-end-of-info'
261 ;; might come in separate chunks from
262 ;; `accept-process-output', so start the
263 ;; search where `tls-success' ended
264 (save-excursion
265 (if (re-search-forward tls-end-of-info nil t)
266 (match-end 0)))))
267 (accept-process-output process 1))
268 (if start-of-data
269 ;; move point to start of client data
270 (goto-char start-of-data)))
271 (setq done process))))
272 (when (and done
273 (or
274 (and tls-checktrust
275 (save-excursion
276 (goto-char (point-min))
277 (re-search-forward tls-untrusted nil t))
278 (or
279 (and (not (eq tls-checktrust 'ask))
280 (message "The certificate presented by `%s' is \
281 NOT trusted." host))
282 (not (yes-or-no-p
283 (tls-format-message "\
284 The certificate presented by `%s' is NOT trusted. Accept anyway? " host)))))
285 (and tls-hostmismatch
286 (save-excursion
287 (goto-char (point-min))
288 (re-search-forward tls-hostmismatch nil t))
289 (not (yes-or-no-p
290 (format "Host name in certificate doesn't \
291 match `%s'. Connect anyway? " host))))))
292 (setq done nil)
293 (delete-process process))
294 ;; Delete all the informational messages that could confuse
295 ;; future uses of `buffer'.
296 (delete-region (point-min) (point)))
297 (message "Opening TLS connection to `%s'...%s"
298 host (if done "done" "failed"))
299 (when use-temp-buffer
300 (if done (set-process-buffer process nil))
301 (kill-buffer buffer))
302 done))
303
304 (provide 'tls)
305
306 ;;; tls.el ends here