Merge from emacs-23
[bpt/emacs.git] / lisp / gnus / starttls.el
CommitLineData
10b7b8fc
DL
1;;; starttls.el --- STARTTLS functions
2
61e1e4e8 3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
5df4f04c 4;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
10b7b8fc
DL
5
6;; Author: Daiki Ueno <ueno@unixuser.org>
cdf33cae 7;; Author: Simon Josefsson <simon@josefsson.org>
10b7b8fc 8;; Created: 1999/11/20
cdf33cae 9;; Keywords: TLS, SSL, OpenSSL, GNUTLS, mail, news
10b7b8fc 10
020930fb 11;; This file is part of GNU Emacs.
10b7b8fc 12
5e809f55 13;; GNU Emacs is free software: you can redistribute it and/or modify
020930fb 14;; it under the terms of the GNU General Public License as published by
5e809f55
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
10b7b8fc 17
020930fb
DL
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
5e809f55 20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020930fb 21;; GNU General Public License for more details.
10b7b8fc
DL
22
23;; You should have received a copy of the GNU General Public License
5e809f55 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
10b7b8fc
DL
25
26;;; Commentary:
27
28;; This module defines some utility functions for STARTTLS profiles.
29
30;; [RFC 2595] "Using TLS with IMAP, POP3 and ACAP"
31;; by Chris Newman <chris.newman@innosoft.com> (1999/06)
32
3031d8b0 33;; This file now contains a combination of the two previous
cdf33cae
SJ
34;; implementations both called "starttls.el". The first one is Daiki
35;; Ueno's starttls.el which uses his own "starttls" command line tool,
36;; and the second one is Simon Josefsson's starttls.el which uses
37;; "gnutls-cli" from GNUTLS.
38;;
39;; If "starttls" is available, it is prefered by the code over
40;; "gnutls-cli", for backwards compatibility. Use
41;; `starttls-use-gnutls' to toggle between implementations if you have
42;; both tools installed. It is recommended to use GNUTLS, though, as
43;; it performs more verification of the certificates.
44
3031d8b0 45;; The GNUTLS support requires GNUTLS 0.9.90 (released 2003-10-08) or
cdf33cae
SJ
46;; later, from <http://www.gnu.org/software/gnutls/>, or "starttls"
47;; from <ftp://ftp.opaopa.org/pub/elisp/>.
48
49;; Usage is similar to `open-network-stream'. For example:
50;;
51;; (when (setq tmp (starttls-open-stream
52;; "test" (current-buffer) "yxa.extundo.com" 25))
53;; (accept-process-output tmp 15)
54;; (process-send-string tmp "STARTTLS\n")
55;; (accept-process-output tmp 15)
56;; (message "STARTTLS output:\n%s" (starttls-negotiate tmp))
57;; (process-send-string tmp "EHLO foo\n"))
58
3031d8b0 59;; An example run yields the following output:
cdf33cae
SJ
60;;
61;; 220 yxa.extundo.com ESMTP Sendmail 8.12.11/8.12.11/Debian-3; Wed, 26 May 2004 19:12:29 +0200; (No UCE/UBE) logging access from: c494102a.s-bi.bostream.se(OK)-c494102a.s-bi.bostream.se [217.215.27.65]
62;; 220 2.0.0 Ready to start TLS
63;; 250-yxa.extundo.com Hello c494102a.s-bi.bostream.se [217.215.27.65], pleased to meet you
64;; 250-ENHANCEDSTATUSCODES
65;; 250-PIPELINING
66;; 250-EXPN
67;; 250-VERB
68;; 250-8BITMIME
69;; 250-SIZE
70;; 250-DSN
71;; 250-ETRN
72;; 250-AUTH DIGEST-MD5 CRAM-MD5 PLAIN LOGIN
73;; 250-DELIVERBY
74;; 250 HELP
75;; nil
76;;
77;; With the message buffer containing:
78;;
79;; STARTTLS output:
80;; *** Starting TLS handshake
81;; - Server's trusted authorities:
82;; [0]: C=SE,ST=Stockholm,L=Stockholm,O=YXA,OU=CA,CN=yxa.extundo.com,EMAIL=staff@yxa.extundo.com
83;; - Certificate type: X.509
84;; - Got a certificate list of 2 certificates.
85;;
86;; - Certificate[0] info:
87;; # The hostname in the certificate matches 'yxa.extundo.com'.
88;; # valid since: Wed May 26 12:16:00 CEST 2004
89;; # expires at: Wed Jul 26 12:16:00 CEST 2023
90;; # serial number: 04
91;; # fingerprint: 7c 04 4b c1 fa 26 9b 5d 90 22 52 3c 65 3d 85 3a
92;; # version: #1
93;; # public key algorithm: RSA
94;; # Modulus: 1024 bits
95;; # Subject's DN: C=SE,ST=Stockholm,L=Stockholm,O=YXA,OU=Mail server,CN=yxa.extundo.com,EMAIL=staff@yxa.extundo.com
96;; # Issuer's DN: C=SE,ST=Stockholm,L=Stockholm,O=YXA,OU=CA,CN=yxa.extundo.com,EMAIL=staff@yxa.extundo.com
97;;
98;; - Certificate[1] info:
99;; # valid since: Sun May 23 11:35:00 CEST 2004
100;; # expires at: Sun Jul 23 11:35:00 CEST 2023
101;; # serial number: 00
102;; # fingerprint: fc 76 d8 63 1a c9 0b 3b fa 40 fe ed 47 7a 58 ae
103;; # version: #3
104;; # public key algorithm: RSA
105;; # Modulus: 1024 bits
106;; # Subject's DN: C=SE,ST=Stockholm,L=Stockholm,O=YXA,OU=CA,CN=yxa.extundo.com,EMAIL=staff@yxa.extundo.com
107;; # Issuer's DN: C=SE,ST=Stockholm,L=Stockholm,O=YXA,OU=CA,CN=yxa.extundo.com,EMAIL=staff@yxa.extundo.com
108;;
109;; - Peer's certificate issuer is unknown
110;; - Peer's certificate is NOT trusted
111;; - Version: TLS 1.0
112;; - Key Exchange: RSA
113;; - Cipher: ARCFOUR 128
114;; - MAC: SHA
115;; - Compression: NULL
116
10b7b8fc
DL
117;;; Code:
118
119(defgroup starttls nil
120 "Support for `Transport Layer Security' protocol."
020930fb
DL
121 :version "21.1"
122 :group 'mail)
10b7b8fc 123
cdf33cae
SJ
124(defcustom starttls-gnutls-program "gnutls-cli"
125 "Name of GNUTLS command line tool.
126This program is used when GNUTLS is used, i.e. when
127`starttls-use-gnutls' is non-nil."
bf247b6e 128 :version "22.1"
cdf33cae
SJ
129 :type 'string
130 :group 'starttls)
131
10b7b8fc 132(defcustom starttls-program "starttls"
cdf33cae
SJ
133 "The program to run in a subprocess to open an TLSv1 connection.
134This program is used when the `starttls' command is used,
135i.e. when `starttls-use-gnutls' is nil."
020930fb 136 :type 'string
10b7b8fc
DL
137 :group 'starttls)
138
cdf33cae
SJ
139(defcustom starttls-use-gnutls (not (executable-find starttls-program))
140 "*Whether to use GNUTLS instead of the `starttls' command."
bf247b6e 141 :version "22.1"
cdf33cae
SJ
142 :type 'boolean
143 :group 'starttls)
144
10b7b8fc 145(defcustom starttls-extra-args nil
cdf33cae 146 "Extra arguments to `starttls-program'.
3031d8b0
MB
147These apply when the `starttls' command is used, i.e. when
148`starttls-use-gnutls' is nil."
020930fb 149 :type '(repeat string)
10b7b8fc
DL
150 :group 'starttls)
151
cdf33cae
SJ
152(defcustom starttls-extra-arguments nil
153 "Extra arguments to `starttls-program'.
3031d8b0 154These apply when GNUTLS is used, i.e. when `starttls-use-gnutls' is non-nil.
cdf33cae
SJ
155
156For example, non-TLS compliant servers may require
157'(\"--protocols\" \"ssl3\"). Invoke \"gnutls-cli --help\" to
158find out which parameters are available."
bf247b6e 159 :version "22.1"
cdf33cae
SJ
160 :type '(repeat string)
161 :group 'starttls)
162
163(defcustom starttls-process-connection-type nil
164 "*Value for `process-connection-type' to use when starting STARTTLS process."
bf247b6e 165 :version "22.1"
cdf33cae
SJ
166 :type 'boolean
167 :group 'starttls)
168
169(defcustom starttls-connect "- Simple Client Mode:\n\n"
170 "*Regular expression indicating successful connection.
171The default is what GNUTLS's \"gnutls-cli\" outputs."
3031d8b0 172 ;; GNUTLS cli.c:main() prints this string when it is starting to run
cdf33cae
SJ
173 ;; in the application read/write phase. If the logic, or the string
174 ;; itself, is modified, this must be updated.
bf247b6e 175 :version "22.1"
cdf33cae
SJ
176 :type 'regexp
177 :group 'starttls)
178
179(defcustom starttls-failure "\\*\\*\\* Handshake has failed"
180 "*Regular expression indicating failed TLS handshake.
181The default is what GNUTLS's \"gnutls-cli\" outputs."
3031d8b0 182 ;; GNUTLS cli.c:do_handshake() prints this string on failure. If the
cdf33cae 183 ;; logic, or the string itself, is modified, this must be updated.
bf247b6e 184 :version "22.1"
cdf33cae
SJ
185 :type 'regexp
186 :group 'starttls)
187
188(defcustom starttls-success "- Compression: "
189 "*Regular expression indicating completed TLS handshakes.
190The default is what GNUTLS's \"gnutls-cli\" outputs."
191 ;; GNUTLS cli.c:do_handshake() calls, on success,
192 ;; common.c:print_info(), that unconditionally print this string
193 ;; last. If that logic, or the string itself, is modified, this
194 ;; must be updated.
bf247b6e 195 :version "22.1"
cdf33cae
SJ
196 :type 'regexp
197 :group 'starttls)
198
199(defun starttls-negotiate-gnutls (process)
3031d8b0
MB
200 "Negotiate TLS on PROCESS opened by `open-starttls-stream'.
201This should typically only be done once. It typically returns a
cdf33cae 202multi-line informational message with information about the
3031d8b0 203handshake, or nil on failure."
cdf33cae
SJ
204 (let (buffer info old-max done-ok done-bad)
205 (if (null (setq buffer (process-buffer process)))
206 ;; XXX How to remove/extract the TLS negotiation junk?
207 (signal-process (process-id process) 'SIGALRM)
208 (with-current-buffer buffer
209 (save-excursion
210 (setq old-max (goto-char (point-max)))
211 (signal-process (process-id process) 'SIGALRM)
212 (while (and (processp process)
213 (eq (process-status process) 'run)
214 (save-excursion
215 (goto-char old-max)
216 (not (or (setq done-ok (re-search-forward
217 starttls-success nil t))
218 (setq done-bad (re-search-forward
219 starttls-failure nil t))))))
220 (accept-process-output process 1 100)
221 (sit-for 0.1))
222 (setq info (buffer-substring-no-properties old-max (point-max)))
223 (delete-region old-max (point-max))
224 (if (or (and done-ok (not done-bad))
225 ;; Prevent mitm that fake success msg after failure msg.
226 (and done-ok done-bad (< done-ok done-bad)))
227 info
228 (message "STARTTLS negotiation failed: %s" info)
229 nil))))))
230
10b7b8fc 231(defun starttls-negotiate (process)
cdf33cae
SJ
232 (if starttls-use-gnutls
233 (starttls-negotiate-gnutls process)
234 (signal-process (process-id process) 'SIGALRM)))
235
0f69d598
MB
236(eval-and-compile
237 (if (fboundp 'set-process-query-on-exit-flag)
238 (defalias 'starttls-set-process-query-on-exit-flag
239 'set-process-query-on-exit-flag)
4a43ee9b 240 (defalias 'starttls-set-process-query-on-exit-flag
0f69d598 241 'process-kill-without-query)))
4a43ee9b 242
3031d8b0 243(defun starttls-open-stream-gnutls (name buffer host port)
ed778fad 244 (message "Opening STARTTLS connection to `%s:%s'..." host port)
cdf33cae
SJ
245 (let* (done
246 (old-max (with-current-buffer buffer (point-max)))
247 (process-connection-type starttls-process-connection-type)
248 (process (apply #'start-process name buffer
249 starttls-gnutls-program "-s" host
3031d8b0
MB
250 "-p" (if (integerp port)
251 (int-to-string port)
252 port)
cdf33cae 253 starttls-extra-arguments)))
4a43ee9b 254 (starttls-set-process-query-on-exit-flag process nil)
cdf33cae
SJ
255 (while (and (processp process)
256 (eq (process-status process) 'run)
20a673b2 257 (with-current-buffer buffer
cdf33cae
SJ
258 (goto-char old-max)
259 (not (setq done (re-search-forward
260 starttls-connect nil t)))))
261 (accept-process-output process 0 100)
262 (sit-for 0.1))
263 (if done
264 (with-current-buffer buffer
265 (delete-region old-max done))
266 (delete-process process)
267 (setq process nil))
ed778fad
MB
268 (message "Opening STARTTLS connection to `%s:%s'...%s"
269 host port (if done "done" "failed"))
cdf33cae 270 process))
10b7b8fc 271
61b1af82 272;;;###autoload
3031d8b0
MB
273(defun starttls-open-stream (name buffer host port)
274 "Open a TLS connection for a port to a host.
275Returns a subprocess object to represent the connection.
10b7b8fc 276Input and output work as for subprocesses; `delete-process' closes it.
3031d8b0 277Args are NAME BUFFER HOST PORT.
10b7b8fc
DL
278NAME is name for process. It is modified if necessary to make it unique.
279BUFFER is the buffer (or `buffer-name') to associate with the process.
280 Process output goes at end of that buffer, unless you specify
281 an output stream or filter function to handle the output.
282 BUFFER may be also nil, meaning that this process is not associated
283 with any buffer
284Third arg is name of the host to connect to, or its IP address.
3031d8b0
MB
285Fourth arg PORT is an integer specifying a port to connect to.
286If `starttls-use-gnutls' is nil, this may also be a service name, but
287GNUTLS requires a port number."
cdf33cae 288 (if starttls-use-gnutls
3031d8b0 289 (starttls-open-stream-gnutls name buffer host port)
ed778fad 290 (message "Opening STARTTLS connection to `%s:%s'" host (format "%s" port))
cdf33cae
SJ
291 (let* ((process-connection-type starttls-process-connection-type)
292 (process (apply #'start-process
293 name buffer starttls-program
3031d8b0 294 host (format "%s" port)
cdf33cae 295 starttls-extra-args)))
4a43ee9b 296 (starttls-set-process-query-on-exit-flag process nil)
cdf33cae 297 process)))
10b7b8fc 298
ed778fad
MB
299(defun starttls-any-program-available ()
300 (let ((program (if starttls-use-gnutls
301 starttls-gnutls-program
302 starttls-program)))
303 (condition-case ()
304 (progn
305 (call-process program)
306 program)
307 (error (progn
308 (message "No STARTTLS program was available (tried '%s')"
309 program)
310 nil)))))
311
10b7b8fc
DL
312(provide 'starttls)
313
314;;; starttls.el ends here