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