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