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