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