Fix up comment convention on the arch-tag lines.
[bpt/emacs.git] / lisp / net / tls.el
CommitLineData
01b2d1dd
SJ
1;;; tls.el --- TLS/SSL support via wrapper around GnuTLS
2
5fd6d89f 3;; Copyright (C) 1996, 1997, 1998, 1999, 2002, 2003, 2004,
2f043267 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
01b2d1dd
SJ
5
6;; Author: Simon Josefsson <simon@josefsson.org>
7;; Keywords: comm, tls, gnutls, ssl
8
9;; This file is part of GNU Emacs.
10
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
ceaeecb0 13;; the Free Software Foundation; either version 3, or (at your option)
01b2d1dd
SJ
14;; any later version.
15
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.
20
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
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
01b2d1dd
SJ
25
26;;; Commentary:
27
28;; This package implements a simple wrapper around "gnutls-cli" to
29;; make Emacs support TLS/SSL.
30;;
31;; Usage is the same as `open-network-stream', i.e.:
32;;
33;; (setq tmp (open-tls-stream "test" (current-buffer) "news.mozilla.org" 563))
34;; ...
35;; #<process test>
36;; (process-send-string tmp "mode reader\n")
37;; 200 secnews.netscape.com Netscape-Collabra/3.52 03615 NNRP ready ...
38;; nil
39;; (process-send-string tmp "quit\n")
40;; 205
41;; nil
42
43;; To use this package as a replacement for ssl.el by William M. Perry
44;; <wmperry@cs.indiana.edu>, you need to evaluate the following:
45;;
46;; (defalias 'open-ssl-stream 'open-tls-stream)
47
48;;; Code:
49
50(eval-and-compile
51 (autoload 'format-spec "format-spec")
52 (autoload 'format-spec-make "format-spec"))
53
54(defgroup tls nil
55 "Transport Layer Security (TLS) parameters."
56 :group 'comm)
57
59c95dc2 58(defcustom tls-end-of-info
cd6db47c
GM
59 (concat
60 "\\("
61 ;; `openssl s_client' regexp. See ssl/ssl_txt.c lines 219-220.
62 ;; According to apps/s_client.c line 1515 `---' is always the last
63 ;; line that is printed by s_client before the real data.
64 "^ Verify return code: .+\n---\n\\|"
65 ;; `gnutls' regexp. See src/cli.c lines 721-.
66 "^- Simple Client Mode:\n"
67 "\\(\n\\|" ; ignore blank lines
2cddada0 68 ;; According to GnuTLS v2.1.5 src/cli.c lines 640-650 and 705-715
7b63c073 69 ;; in `main' the handshake will start after this message. If the
2cddada0 70 ;; handshake fails, the programs will abort.
cd6db47c
GM
71 "^\\*\\*\\* Starting TLS handshake\n\\)*"
72 "\\)")
59c95dc2
GM
73 "Regexp matching end of TLS client informational messages.
74Client data stream begins after the last character matched by
75this. The default matches `openssl s_client' (version 0.9.8c)
76and `gnutls-cli' (version 2.0.1) output."
77 :version "22.2"
78 :type 'regexp
79 :group 'tls)
80
01b2d1dd 81(defcustom tls-program '("gnutls-cli -p %p %h"
3031d8b0
MB
82 "gnutls-cli -p %p %h --protocols ssl3"
83 "openssl s_client -connect %h:%p -no_ssl2")
01b2d1dd
SJ
84 "List of strings containing commands to start TLS stream to a host.
85Each entry in the list is tried until a connection is successful.
4a3c7686 86%h is replaced with server hostname, %p with port to connect to.
01b2d1dd 87The program should read input on stdin and write output to
b890d447
MB
88stdout.
89
90See `tls-checktrust' on how to check trusted root certs.
91
92Also see `tls-success' for what the program should output after
93successful negotiation."
94 :type
95 '(choice
96 (list :tag "Choose commands"
97 :value
98 ("gnutls-cli -p %p %h"
99 "gnutls-cli -p %p %h --protocols ssl3"
100 "openssl s_client -connect %h:%p -no_ssl2")
101 (set :inline t
102 ;; FIXME: add brief `:tag "..."' descriptions.
103 ;; (repeat :inline t :tag "Other" (string))
104 ;; See `tls-checktrust':
105 (const "gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h")
106 (const "gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h --protocols ssl3")
107 (const "openssl s_client -connect %h:%p -CAfile /etc/ssl/certs/ca-certificates.crt -no_ssl2")
108 ;; No trust check:
109 (const "gnutls-cli -p %p %h")
110 (const "gnutls-cli -p %p %h --protocols ssl3")
111 (const "openssl s_client -connect %h:%p -no_ssl2"))
112 (repeat :inline t :tag "Other" (string)))
113 (const :tag "Default list of commands"
114 ("gnutls-cli -p %p %h"
115 "gnutls-cli -p %p %h --protocols ssl3"
116 "openssl s_client -connect %h:%p -no_ssl2"))
117 (list :tag "List of commands"
118 (repeat :tag "Command" (string))))
3031d8b0 119 :version "22.1"
01b2d1dd
SJ
120 :group 'tls)
121
122(defcustom tls-process-connection-type nil
b890d447 123 "Value for `process-connection-type' to use when starting TLS process."
bf247b6e 124 :version "22.1"
01b2d1dd
SJ
125 :type 'boolean
126 :group 'tls)
127
3031d8b0 128(defcustom tls-success "- Handshake was completed\\|SSL handshake has read "
b890d447 129 "Regular expression indicating completed TLS handshakes.
3031d8b0
MB
130The default is what GNUTLS's \"gnutls-cli\" or OpenSSL's
131\"openssl s_client\" outputs."
bf247b6e 132 :version "22.1"
01b2d1dd
SJ
133 :type 'regexp
134 :group 'tls)
135
b890d447
MB
136(defcustom tls-checktrust nil
137 "Indicate if certificates should be checked against trusted root certs.
138If this is `ask', the user can decide whether to accept an
139untrusted certificate. You may have to adapt `tls-program' in
140order to make this feature work properly, i.e., to ensure that
141the external program knows about the root certificates you
142consider trustworthy, e.g.:
143
144\(setq tls-program
145 '(\"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h\"
146 \"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h --protocols ssl3\"
147 \"openssl s_client -connect %h:%p -CAfile /etc/ssl/certs/ca-certificates.crt -no_ssl2\"))"
148 :type '(choice (const :tag "Always" t)
149 (const :tag "Never" nil)
150 (const :tag "Ask" ask))
dd22fbf6 151 :version "23.1" ;; No Gnus
b890d447
MB
152 :group 'tls)
153
154(defcustom tls-untrusted
155 "- Peer's certificate is NOT trusted\\|Verify return code: \\([^0] \\|.[^ ]\\)"
156 "Regular expression indicating failure of TLS certificate verification.
157The default is what GNUTLS's \"gnutls-cli\" or OpenSSL's
158\"openssl s_client\" return in the event of unsuccessful
159verification."
160 :type 'regexp
dd22fbf6 161 :version "23.1" ;; No Gnus
b890d447
MB
162 :group 'tls)
163
164(defcustom tls-hostmismatch
165 "# The hostname in the certificate does NOT match"
166 "Regular expression indicating a host name mismatch in certificate.
167When the host name specified in the certificate doesn't match the
168name of the host you are connecting to, gnutls-cli issues a
169warning to this effect. There is no such feature in openssl. Set
170this to nil if you want to ignore host name mismatches."
171 :type 'regexp
dd22fbf6 172 :version "23.1" ;; No Gnus
b890d447
MB
173 :group 'tls)
174
18965008
SJ
175(defcustom tls-certtool-program (executable-find "certtool")
176 "Name of GnuTLS certtool.
177Used by `tls-certificate-information'."
bf247b6e 178 :version "22.1"
9bdd0e16 179 :type 'string
18965008
SJ
180 :group 'tls)
181
182(defun tls-certificate-information (der)
183 "Parse X.509 certificate in DER format into an assoc list."
184 (let ((certificate (concat "-----BEGIN CERTIFICATE-----\n"
185 (base64-encode-string der)
186 "\n-----END CERTIFICATE-----\n"))
187 (exit-code 0))
188 (with-current-buffer (get-buffer-create " *certtool*")
189 (erase-buffer)
190 (insert certificate)
191 (setq exit-code (condition-case ()
192 (call-process-region (point-min) (point-max)
193 tls-certtool-program
194 t (list (current-buffer) nil) t
195 "--certificate-info")
196 (error -1)))
197 (if (/= exit-code 0)
198 nil
199 (let ((vals nil))
200 (goto-char (point-min))
201 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)" nil t)
202 (push (cons (match-string 1) (match-string 2)) vals))
203 (nreverse vals))))))
204
3031d8b0
MB
205(defun open-tls-stream (name buffer host port)
206 "Open a TLS connection for a port to a host.
01b2d1dd
SJ
207Returns a subprocess-object to represent the connection.
208Input and output work as for subprocesses; `delete-process' closes it.
3031d8b0 209Args are NAME BUFFER HOST PORT.
01b2d1dd 210NAME is name for process. It is modified if necessary to make it unique.
b890d447 211BUFFER is the buffer (or buffer name) to associate with the process.
01b2d1dd
SJ
212 Process output goes at end of that buffer, unless you specify
213 an output stream or filter function to handle the output.
214 BUFFER may be also nil, meaning that this process is not associated
215 with any buffer
216Third arg is name of the host to connect to, or its IP address.
3031d8b0 217Fourth arg PORT is an integer specifying a port to connect to."
0ad32c54
CY
218 (let ((cmds tls-program)
219 (use-temp-buffer (null buffer))
220 process cmd done)
221 (if use-temp-buffer
222 (setq buffer (generate-new-buffer " TLS")))
cd6db47c 223 (with-current-buffer buffer
59c95dc2
GM
224 (message "Opening TLS connection to `%s'..." host)
225 (while (and (not done) (setq cmd (pop cmds)))
226 (message "Opening TLS connection with `%s'..." cmd)
227 (let ((process-connection-type tls-process-connection-type)
228 response)
229 (setq process (start-process
230 name buffer shell-file-name shell-command-switch
231 (format-spec
232 cmd
233 (format-spec-make
234 ?h host
235 ?p (if (integerp port)
236 (int-to-string port)
237 port)))))
238 (while (and process
239 (memq (process-status process) '(open run))
240 (progn
241 (goto-char (point-min))
0a4d4654
GM
242 (not (setq done (re-search-forward
243 tls-success nil t)))))
59c95dc2
GM
244 (unless (accept-process-output process 1)
245 (sit-for 1)))
246 (message "Opening TLS connection with `%s'...%s" cmd
247 (if done "done" "failed"))
0a4d4654
GM
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
ea666a77
RS
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)))))
0a4d4654
GM
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)))
ea666a77 266 (setq done process))))
0a4d4654
GM
267 (when (and done
268 (or
269 (and tls-checktrust
270 (save-excursion
271 (goto-char (point-min))
272 (re-search-forward tls-untrusted nil t))
273 (or
274 (and (not (eq tls-checktrust 'ask))
275 (message "The certificate presented by `%s' is \
276NOT trusted." host))
277 (not (yes-or-no-p
278 (format "The certificate presented by `%s' is \
279NOT 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 \
286match `%s'. Connect anyway? " host))))))
287 (setq done nil)
288 (delete-process process)))
289 (message "Opening TLS connection to `%s'...%s"
290 host (if done "done" "failed"))
0ad32c54 291 (when use-temp-buffer
b7131d2f 292 (if done (set-process-buffer process nil))
0ad32c54 293 (kill-buffer buffer))
01b2d1dd
SJ
294 done))
295
296(provide 'tls)
297
cbee283d 298;; arch-tag: 5596d1c4-facc-4bc4-94a9-9863b928d7ac
01b2d1dd 299;;; tls.el ends here