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