]> code.delx.au - gnu-emacs/blob - lisp/net/tls.el
* textmodes/css-mode.el (css-indent-offset, css-electric-keys):
[gnu-emacs] / lisp / net / tls.el
1 ;;; tls.el --- TLS/SSL support via wrapper around GnuTLS
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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
13 ;; the Free Software Foundation; either version 3, or (at your option)
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
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
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 (eval-when-compile
55 (require 'rx))
56
57 (defgroup tls nil
58 "Transport Layer Security (TLS) parameters."
59 :group 'comm)
60
61 (defcustom tls-end-of-info
62 (rx
63 (or
64 ;; `openssl s_client` regexp
65 (sequence
66 ;; see ssl/ssl_txt.c lines 219--220
67 line-start
68 " Verify return code: "
69 (one-or-more not-newline)
70 "\n"
71 ;; according to apps/s_client.c line 1515 this is always the last
72 ;; line that is printed by s_client before the real data
73 "---\n")
74 ;; `gnutls` regexp
75 (sequence
76 ;; see src/cli.c lines 721--
77 (sequence line-start "- Simple Client Mode:\n")
78 (zero-or-more
79 (or
80 "\n" ; ignore blank lines
81 ;; XXX: we have no way of knowing if the STARTTLS handshake
82 ;; sequence has completed successfully, because `gnutls` will
83 ;; only report failure.
84 (sequence line-start "\*\*\* Starting TLS handshake\n"))))))
85 "Regexp matching end of TLS client informational messages.
86 Client data stream begins after the last character matched by
87 this. The default matches `openssl s_client' (version 0.9.8c)
88 and `gnutls-cli' (version 2.0.1) output."
89 :version "22.2"
90 :type 'regexp
91 :group 'tls)
92
93 (defcustom tls-program '("gnutls-cli -p %p %h"
94 "gnutls-cli -p %p %h --protocols ssl3"
95 "openssl s_client -connect %h:%p -no_ssl2")
96 "List of strings containing commands to start TLS stream to a host.
97 Each entry in the list is tried until a connection is successful.
98 %h is replaced with server hostname, %p with port to connect to.
99 The program should read input on stdin and write output to
100 stdout. Also see `tls-success' for what the program should output
101 after successful negotiation."
102 :type '(repeat string)
103 :version "22.1"
104 :group 'tls)
105
106 (defcustom tls-process-connection-type nil
107 "*Value for `process-connection-type' to use when starting TLS process."
108 :version "22.1"
109 :type 'boolean
110 :group 'tls)
111
112 (defcustom tls-success "- Handshake was completed\\|SSL handshake has read "
113 "*Regular expression indicating completed TLS handshakes.
114 The default is what GNUTLS's \"gnutls-cli\" or OpenSSL's
115 \"openssl s_client\" outputs."
116 :version "22.1"
117 :type 'regexp
118 :group 'tls)
119
120 (defcustom tls-certtool-program (executable-find "certtool")
121 "Name of GnuTLS certtool.
122 Used by `tls-certificate-information'."
123 :version "22.1"
124 :type 'string
125 :group 'tls)
126
127 (defun tls-certificate-information (der)
128 "Parse X.509 certificate in DER format into an assoc list."
129 (let ((certificate (concat "-----BEGIN CERTIFICATE-----\n"
130 (base64-encode-string der)
131 "\n-----END CERTIFICATE-----\n"))
132 (exit-code 0))
133 (with-current-buffer (get-buffer-create " *certtool*")
134 (erase-buffer)
135 (insert certificate)
136 (setq exit-code (condition-case ()
137 (call-process-region (point-min) (point-max)
138 tls-certtool-program
139 t (list (current-buffer) nil) t
140 "--certificate-info")
141 (error -1)))
142 (if (/= exit-code 0)
143 nil
144 (let ((vals nil))
145 (goto-char (point-min))
146 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)" nil t)
147 (push (cons (match-string 1) (match-string 2)) vals))
148 (nreverse vals))))))
149
150 (defun open-tls-stream (name buffer host port)
151 "Open a TLS connection for a port to a host.
152 Returns a subprocess-object to represent the connection.
153 Input and output work as for subprocesses; `delete-process' closes it.
154 Args are NAME BUFFER HOST PORT.
155 NAME is name for process. It is modified if necessary to make it unique.
156 BUFFER is the buffer (or buffer-name) to associate with the process.
157 Process output goes at end of that buffer, unless you specify
158 an output stream or filter function to handle the output.
159 BUFFER may be also nil, meaning that this process is not associated
160 with any buffer
161 Third arg is name of the host to connect to, or its IP address.
162 Fourth arg PORT is an integer specifying a port to connect to."
163 (let ((cmds tls-program)
164 (use-temp-buffer (null buffer))
165 process cmd done)
166 (if use-temp-buffer
167 (setq buffer (generate-new-buffer " TLS")))
168 (save-excursion
169 (set-buffer buffer)
170 (message "Opening TLS connection to `%s'..." host)
171 (while (and (not done) (setq cmd (pop cmds)))
172 (message "Opening TLS connection with `%s'..." cmd)
173 (let ((process-connection-type tls-process-connection-type)
174 response)
175 (setq process (start-process
176 name buffer shell-file-name shell-command-switch
177 (format-spec
178 cmd
179 (format-spec-make
180 ?h host
181 ?p (if (integerp port)
182 (int-to-string port)
183 port)))))
184 (while (and process
185 (memq (process-status process) '(open run))
186 (progn
187 (goto-char (point-min))
188 (not (setq done (re-search-forward tls-success nil t)))))
189 (unless (accept-process-output process 1)
190 (sit-for 1)))
191 (message "Opening TLS connection with `%s'...%s" cmd
192 (if done "done" "failed"))
193 (if (not done)
194 (delete-process process)
195 ;; advance point to after all informational messages that
196 ;; `openssl s_client' and `gnutls' print
197 (let ((start-of-data nil))
198 (while
199 (not (setq start-of-data
200 ;; the string matching `tls-end-of-info'
201 ;; might come in separate chunks from
202 ;; `accept-process-output', so start the
203 ;; search where `tls-success' ended
204 (save-excursion
205 (if (re-search-forward tls-end-of-info nil t)
206 (match-end 0)))))
207 (accept-process-output process 1))
208 (if start-of-data
209 ;; move point to start of client data
210 (goto-char start-of-data)))
211 (setq done process))))
212 (message "Opening TLS connection to `%s'...%s"
213 host (if done "done" "failed")))
214 (when use-temp-buffer
215 (if done (set-process-buffer process nil))
216 (kill-buffer buffer))
217 done))
218
219 (provide 'tls)
220
221 ;;; arch-tag: 5596d1c4-facc-4bc4-94a9-9863b928d7ac
222 ;;; tls.el ends here