efcb1609531e4fbbf7c2bf2d7cf09a3dd96f54c8
[gnus] / lisp / 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 (defgroup tls nil
55   "Transport Layer Security (TLS) parameters."
56   :group 'comm)
57
58 (defcustom tls-program '("gnutls-cli -p %p %h"
59                          "gnutls-cli -p %p %h --protocols ssl3"
60                          "openssl s_client -connect %h:%p -no_ssl2")
61   "List of strings containing commands to start TLS stream to a host.
62 Each entry in the list is tried until a connection is successful.
63 %h is replaced with server hostname, %p with port to connect to.
64 The program should read input on stdin and write output to
65 stdout.
66
67 See `tls-checktrust' on how to check trusted root certs.
68
69 Also see `tls-success' for what the program should output after
70 successful negotiation."
71   :type
72   '(choice
73     (list :tag "Choose commands"
74           :value
75           ("gnutls-cli -p %p %h"
76            "gnutls-cli -p %p %h --protocols ssl3"
77            "openssl s_client -connect %h:%p -no_ssl2")
78           (set :inline t
79                ;; FIXME: add brief `:tag "..."' descriptions.
80                ;; (repeat :inline t :tag "Other" (string))
81                ;; See `tls-checktrust':
82                (const "gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h")
83                (const "gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h --protocols ssl3")
84                (const "openssl s_client -connect %h:%p -CAfile /etc/ssl/certs/ca-certificates.crt -no_ssl2")
85                ;; No trust check:
86                (const "gnutls-cli -p %p %h")
87                (const "gnutls-cli -p %p %h --protocols ssl3")
88                (const "openssl s_client -connect %h:%p -no_ssl2"))
89           (repeat :inline t :tag "Other" (string)))
90     (const :tag "Default list of commands"
91            ("gnutls-cli -p %p %h"
92             "gnutls-cli -p %p %h --protocols ssl3"
93             "openssl s_client -connect %h:%p -no_ssl2"))
94     (list :tag "List of commands"
95           (repeat :tag "Command" (string))))
96   :version "22.1"
97   :group 'tls)
98
99 (defcustom tls-process-connection-type nil
100   "Value for `process-connection-type' to use when starting TLS process."
101   :version "22.1"
102   :type 'boolean
103   :group 'tls)
104
105 (defcustom tls-success "- Handshake was completed\\|SSL handshake has read "
106   "Regular expression indicating completed TLS handshakes.
107 The default is what GNUTLS's \"gnutls-cli\" or OpenSSL's
108 \"openssl s_client\" outputs."
109   :version "22.1"
110   :type 'regexp
111   :group 'tls)
112
113 (defcustom tls-checktrust nil
114   "Indicate if certificates should be checked against trusted root certs.
115 If this is `ask', the user can decide whether to accept an
116 untrusted certificate.  You may have to adapt `tls-program' in
117 order to make this feature work properly, i.e., to ensure that
118 the external program knows about the root certificates you
119 consider trustworthy, e.g.:
120
121 \(setq tls-program
122       '(\"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h\"
123         \"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h --protocols ssl3\"
124         \"openssl s_client -connect %h:%p -CAfile /etc/ssl/certs/ca-certificates.crt -no_ssl2\"))"
125   :type '(choice (const :tag "Always" t)
126                  (const :tag "Never" nil)
127                  (const :tag "Ask" ask))
128   :version "23.0" ;; No Gnus
129   :group 'tls)
130
131 (defcustom tls-untrusted
132   "- Peer's certificate is NOT trusted\\|Verify return code: \\([^0] \\|.[^ ]\\)"
133   "Regular expression indicating failure of TLS certificate verification.
134 The default is what GNUTLS's \"gnutls-cli\" or OpenSSL's
135 \"openssl s_client\" return in the event of unsuccessful
136 verification."
137   :type 'regexp
138   :version "23.0" ;; No Gnus
139   :group 'tls)
140
141 (defcustom tls-hostmismatch
142   "# The hostname in the certificate does NOT match"
143   "Regular expression indicating a host name mismatch in certificate.
144 When the host name specified in the certificate doesn't match the
145 name of the host you are connecting to, gnutls-cli issues a
146 warning to this effect. There is no such feature in openssl. Set
147 this to nil if you want to ignore host name mismatches."
148   :type 'regexp
149   :version "23.0" ;; No Gnus
150   :group 'tls)
151
152 (defcustom tls-certtool-program (executable-find "certtool")
153   "Name of  GnuTLS certtool.
154 Used by `tls-certificate-information'."
155   :version "22.1"
156   :type 'string
157   :group 'tls)
158
159 (defun tls-certificate-information (der)
160   "Parse X.509 certificate in DER format into an assoc list."
161   (let ((certificate (concat "-----BEGIN CERTIFICATE-----\n"
162                              (base64-encode-string der)
163                              "\n-----END CERTIFICATE-----\n"))
164         (exit-code 0))
165     (with-current-buffer (get-buffer-create " *certtool*")
166       (erase-buffer)
167       (insert certificate)
168       (setq exit-code (condition-case ()
169                           (call-process-region (point-min) (point-max)
170                                                tls-certtool-program
171                                                t (list (current-buffer) nil) t
172                                                "--certificate-info")
173                         (error -1)))
174       (if (/= exit-code 0)
175           nil
176         (let ((vals nil))
177           (goto-char (point-min))
178           (while (re-search-forward "^\\([^:]+\\): \\(.*\\)" nil t)
179             (push (cons (match-string 1) (match-string 2)) vals))
180           (nreverse vals))))))
181
182 (defun open-tls-stream (name buffer host port)
183   "Open a TLS connection for a port to a host.
184 Returns a subprocess-object to represent the connection.
185 Input and output work as for subprocesses; `delete-process' closes it.
186 Args are NAME BUFFER HOST PORT.
187 NAME is name for process.  It is modified if necessary to make it unique.
188 BUFFER is the buffer (or buffer-name) to associate with the process.
189  Process output goes at end of that buffer, unless you specify
190  an output stream or filter function to handle the output.
191  BUFFER may be also nil, meaning that this process is not associated
192  with any buffer
193 Third arg is name of the host to connect to, or its IP address.
194 Fourth arg PORT is an integer specifying a port to connect to."
195   (let ((cmds tls-program)
196         (use-temp-buffer (null buffer))
197         process cmd done)
198     (if use-temp-buffer
199         (setq buffer (generate-new-buffer " TLS")))
200     (message "Opening TLS connection to `%s'..." host)
201     (while (and (not done) (setq cmd (pop cmds)))
202       (message "Opening TLS connection with `%s'..." cmd)
203       (let ((process-connection-type tls-process-connection-type)
204             response)
205         (setq process (start-process
206                        name buffer shell-file-name shell-command-switch
207                        (format-spec
208                         cmd
209                         (format-spec-make
210                          ?h host
211                          ?p (if (integerp port)
212                                 (int-to-string port)
213                               port)))))
214         (while (and process
215                     (memq (process-status process) '(open run))
216                     (save-excursion
217                       (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
218                       (goto-char (point-min))
219                       (not (setq done (re-search-forward tls-success nil t)))))
220           (unless (accept-process-output process 1)
221             (sit-for 1)))
222         (message "Opening TLS connection with `%s'...%s" cmd
223                  (if done "done" "failed"))
224         (if done
225             (setq done process)
226           (delete-process process))))
227     (when done
228       (save-excursion
229         (set-buffer buffer)
230         (when
231             (or
232              (and tls-checktrust
233                   (progn
234                     (goto-char (point-min))
235                     (re-search-forward tls-untrusted nil t))
236                   (or 
237                    (and (not (eq tls-checktrust 'ask))
238                         (message "The certificate presented by `%s' is NOT trusted." host))
239                    (not (yes-or-no-p
240                          (format "The certificate presented by `%s' is NOT trusted. Accept anyway? " host)))))
241              (and tls-hostmismatch
242                   (progn
243                     (goto-char (point-min))
244                     (re-search-forward tls-hostmismatch nil t))
245                   (not (yes-or-no-p
246                         (format "Host name in certificate doesn't match `%s'. Connect anyway? " host)))))
247           (setq done nil)
248           (delete-process process))))
249     (message "Opening TLS connection to `%s'...%s"
250              host (if done "done" "failed"))
251     (when use-temp-buffer
252       (if done (set-process-buffer process nil))
253       (kill-buffer buffer))
254     done))
255
256 (provide 'tls)
257
258 ;;; arch-tag: 5596d1c4-facc-4bc4-94a9-9863b928d7ac
259 ;;; tls.el ends here