]> code.delx.au - gnu-emacs/blob - lisp/net/gnutls.el
Merge branch 'emacs-25-merge'
[gnu-emacs] / lisp / net / gnutls.el
1 ;;; gnutls.el --- Support SSL/TLS connections through GnuTLS
2
3 ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
4
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
6 ;; Keywords: comm, tls, ssl, encryption
7 ;; Originally-By: Simon Josefsson (See http://josefsson.org/emacs-security/)
8 ;; Thanks-To: Lars Magne Ingebrigtsen <larsi@gnus.org>
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This package provides language bindings for the GnuTLS library
28 ;; using the corresponding core functions in gnutls.c. It should NOT
29 ;; be used directly, only through open-protocol-stream.
30
31 ;; Simple test:
32 ;;
33 ;; (open-gnutls-stream "tls" "tls-buffer" "yourserver.com" "https")
34 ;; (open-gnutls-stream "tls" "tls-buffer" "imap.gmail.com" "imaps")
35
36 ;;; Code:
37
38 (require 'cl-lib)
39
40 (defgroup gnutls nil
41 "Emacs interface to the GnuTLS library."
42 :version "24.1"
43 :prefix "gnutls-"
44 :group 'comm)
45
46 (defcustom gnutls-algorithm-priority nil
47 "If non-nil, this should be a TLS priority string.
48 For instance, if you want to skip the \"dhe-rsa\" algorithm,
49 set this variable to \"normal:-dhe-rsa\"."
50 :group 'gnutls
51 :type '(choice (const nil)
52 string))
53
54 (defcustom gnutls-verify-error nil
55 "If non-nil, this should be a list of checks per hostname regex or t."
56 :group 'gnutls
57 :version "24.4"
58 :type '(choice
59 (const t)
60 (repeat :tag "List of hostname regexps with flags for each"
61 (list
62 (choice :tag "Hostname"
63 (const ".*" :tag "Any hostname")
64 regexp)
65 (set (const :trustfiles)
66 (const :hostname))))))
67
68 (defcustom gnutls-trustfiles
69 '(
70 "/etc/ssl/certs/ca-certificates.crt" ; Debian, Ubuntu, Gentoo and Arch Linux
71 "/etc/pki/tls/certs/ca-bundle.crt" ; Fedora and RHEL
72 "/etc/ssl/ca-bundle.pem" ; Suse
73 "/usr/ssl/certs/ca-bundle.crt" ; Cygwin
74 "/usr/local/share/certs/ca-root-nss.crt" ; FreeBSD
75 )
76 "List of CA bundle location filenames or a function returning said list.
77 The files may be in PEM or DER format, as per the GnuTLS documentation.
78 The files may not exist, in which case they will be ignored."
79 :group 'gnutls
80 :type '(choice (function :tag "Function to produce list of bundle filenames")
81 (repeat (file :tag "Bundle filename"))))
82
83 ;;;###autoload
84 (defcustom gnutls-min-prime-bits 256
85 ;; Several mail servers send fewer bits than the GnuTLS default.
86 ;; Currently, 256 appears to be a reasonable choice (Bug#11267).
87 "Minimum number of prime bits accepted by GnuTLS for key exchange.
88 During a Diffie-Hellman handshake, if the server sends a prime
89 number with fewer than this number of bits, the handshake is
90 rejected. \(The smaller the prime number, the less secure the
91 key exchange is against man-in-the-middle attacks.)
92
93 A value of nil says to use the default GnuTLS value."
94 :type '(choice (const :tag "Use default value" nil)
95 (integer :tag "Number of bits" 512))
96 :group 'gnutls)
97
98 (defun open-gnutls-stream (name buffer host service)
99 "Open a SSL/TLS connection for a service to a host.
100 Returns a subprocess-object to represent the connection.
101 Input and output work as for subprocesses; `delete-process' closes it.
102 Args are NAME BUFFER HOST SERVICE.
103 NAME is name for process. It is modified if necessary to make it unique.
104 BUFFER is the buffer (or `buffer-name') to associate with the process.
105 Process output goes at end of that buffer, unless you specify
106 an output stream or filter function to handle the output.
107 BUFFER may be also nil, meaning that this process is not associated
108 with any buffer
109 Third arg is name of the host to connect to, or its IP address.
110 Fourth arg SERVICE is name of the service desired, or an integer
111 specifying a port number to connect to.
112
113 Usage example:
114
115 (with-temp-buffer
116 (open-gnutls-stream \"tls\"
117 (current-buffer)
118 \"your server goes here\"
119 \"imaps\"))
120
121 This is a very simple wrapper around `gnutls-negotiate'. See its
122 documentation for the specific parameters you can use to open a
123 GnuTLS connection, including specifying the credential type,
124 trust and key files, and priority string."
125 (gnutls-negotiate :process (open-network-stream name buffer host service)
126 :type 'gnutls-x509pki
127 :hostname host))
128
129 (define-error 'gnutls-error "GnuTLS error")
130
131 (declare-function gnutls-boot "gnutls.c" (proc type proplist))
132 (declare-function gnutls-errorp "gnutls.c" (error))
133 (defvar gnutls-log-level) ; gnutls.c
134
135 (cl-defun gnutls-negotiate
136 (&rest spec
137 &key process type hostname priority-string
138 trustfiles crlfiles keylist min-prime-bits
139 verify-flags verify-error verify-hostname-error
140 &allow-other-keys)
141 "Negotiate a SSL/TLS connection. Returns proc. Signals gnutls-error.
142
143 Note arguments are passed CL style, :type TYPE instead of just TYPE.
144
145 TYPE is `gnutls-x509pki' (default) or `gnutls-anon'. Use nil for the default.
146 PROCESS is a process returned by `open-network-stream'.
147 HOSTNAME is the remote hostname. It must be a valid string.
148 PRIORITY-STRING is as per the GnuTLS docs, default is \"NORMAL\".
149 TRUSTFILES is a list of CA bundles. It defaults to `gnutls-trustfiles'.
150 CRLFILES is a list of CRL files.
151 KEYLIST is an alist of (client key file, client cert file) pairs.
152 MIN-PRIME-BITS is the minimum acceptable size of Diffie-Hellman keys
153 \(see `gnutls-min-prime-bits' for more information). Use nil for the
154 default.
155
156 VERIFY-HOSTNAME-ERROR is a backwards compatibility option for
157 putting `:hostname' in VERIFY-ERROR.
158
159 When VERIFY-ERROR is t or a list containing `:trustfiles', an
160 error will be raised when the peer certificate verification fails
161 as per GnuTLS' gnutls_certificate_verify_peers2. Otherwise, only
162 warnings will be shown about the verification failure.
163
164 When VERIFY-ERROR is t or a list containing `:hostname', an error
165 will be raised when the hostname does not match the presented
166 certificate's host name. The exact verification algorithm is a
167 basic implementation of the matching described in
168 RFC2818 (HTTPS), which takes into account wildcards, and the
169 DNSName/IPAddress subject alternative name PKIX extension. See
170 GnuTLS' gnutls_x509_crt_check_hostname for details. Otherwise,
171 only a warning will be issued.
172
173 Note that the list in `gnutls-verify-error', matched against the
174 HOSTNAME, is the default VERIFY-ERROR.
175
176 VERIFY-FLAGS is a numeric OR of verification flags only for
177 `gnutls-x509pki' connections. See GnuTLS' x509.h for details;
178 here's a recent version of the list.
179
180 GNUTLS_VERIFY_DISABLE_CA_SIGN = 1,
181 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT = 2,
182 GNUTLS_VERIFY_DO_NOT_ALLOW_SAME = 4,
183 GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT = 8,
184 GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2 = 16,
185 GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5 = 32,
186 GNUTLS_VERIFY_DISABLE_TIME_CHECKS = 64,
187 GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS = 128,
188 GNUTLS_VERIFY_DO_NOT_ALLOW_X509_V1_CA_CRT = 256
189
190 It must be omitted, a number, or nil; if omitted or nil it
191 defaults to GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT."
192 (let* ((type (or type 'gnutls-x509pki))
193 ;; The gnutls library doesn't understand files delivered via
194 ;; the special handlers, so ignore all files found via those.
195 (file-name-handler-alist nil)
196 (trustfiles (or trustfiles (gnutls-trustfiles)))
197 (priority-string (or priority-string
198 (cond
199 ((eq type 'gnutls-anon)
200 "NORMAL:+ANON-DH:!ARCFOUR-128")
201 ((eq type 'gnutls-x509pki)
202 (if gnutls-algorithm-priority
203 (upcase gnutls-algorithm-priority)
204 "NORMAL")))))
205 (verify-error (or verify-error
206 ;; this uses the value of `gnutls-verify-error'
207 (cond
208 ;; if t, pass it on
209 ((eq gnutls-verify-error t)
210 t)
211 ;; if a list, look for hostname matches
212 ((listp gnutls-verify-error)
213 (apply 'append
214 (mapcar
215 (lambda (check)
216 (when (string-match (nth 0 check)
217 hostname)
218 (nth 1 check)))
219 gnutls-verify-error)))
220 ;; else it's nil
221 (t nil))))
222 (min-prime-bits (or min-prime-bits gnutls-min-prime-bits))
223 params ret)
224
225 (when verify-hostname-error
226 (push :hostname verify-error))
227
228 (setq params `(:priority ,priority-string
229 :hostname ,hostname
230 :loglevel ,gnutls-log-level
231 :min-prime-bits ,min-prime-bits
232 :trustfiles ,trustfiles
233 :crlfiles ,crlfiles
234 :keylist ,keylist
235 :verify-flags ,verify-flags
236 :verify-error ,verify-error
237 :callbacks nil))
238
239 (gnutls-message-maybe
240 (setq ret (gnutls-boot process type params))
241 "boot: %s" params)
242
243 (when (gnutls-errorp ret)
244 ;; This is a error from the underlying C code.
245 (signal 'gnutls-error (list process ret)))
246
247 process))
248
249 (defun gnutls-trustfiles ()
250 "Return a list of usable trustfiles."
251 (delq nil
252 (mapcar (lambda (f) (and f (file-exists-p f) f))
253 (if (functionp gnutls-trustfiles)
254 (funcall gnutls-trustfiles)
255 gnutls-trustfiles))))
256
257 (declare-function gnutls-error-string "gnutls.c" (error))
258
259 (defun gnutls-message-maybe (doit format &rest params)
260 "When DOIT, message with the caller name followed by FORMAT on PARAMS."
261 ;; (apply 'debug format (or params '(nil)))
262 (when (gnutls-errorp doit)
263 (message "%s: (err=[%s] %s) %s"
264 "gnutls.el"
265 doit (gnutls-error-string doit)
266 (apply #'format-message format (or params '(nil))))))
267
268 (provide 'gnutls)
269
270 ;;; gnutls.el ends here