]> code.delx.au - gnu-emacs/blob - lisp/net/gnutls.el
* net/shr.el (image-size, image-animate): Declare.
[gnu-emacs] / lisp / net / gnutls.el
1 ;;; gnutls.el --- Support SSL/TLS connections through GnuTLS
2
3 ;; Copyright (C) 2010-2013 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 (eval-when-compile (require 'cl-lib))
39
40 (defgroup gnutls nil
41 "Emacs interface to the GnuTLS library."
42 :version "24.1"
43 :prefix "gnutls-"
44 :group 'net-utils)
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-trustfiles
55 '(
56 "/etc/ssl/certs/ca-certificates.crt" ; Debian, Ubuntu, Gentoo and Arch Linux
57 "/etc/pki/tls/certs/ca-bundle.crt" ; Fedora and RHEL
58 "/etc/ssl/ca-bundle.pem" ; Suse
59 "/usr/ssl/certs/ca-bundle.crt" ; Cygwin
60 )
61 "List of CA bundle location filenames or a function returning said list.
62 The files may be in PEM or DER format, as per the GnuTLS documentation.
63 The files may not exist, in which case they will be ignored."
64 :group 'gnutls
65 :type '(choice (function :tag "Function to produce list of bundle filenames")
66 (repeat (file :tag "Bundle filename"))))
67
68 ;;;###autoload
69 (defcustom gnutls-min-prime-bits 256
70 ;; Several mail servers send fewer bits than the GnuTLS default.
71 ;; Currently, 256 appears to be a reasonable choice (Bug#11267).
72 "Minimum number of prime bits accepted by GnuTLS for key exchange.
73 During a Diffie-Hellman handshake, if the server sends a prime
74 number with fewer than this number of bits, the handshake is
75 rejected. \(The smaller the prime number, the less secure the
76 key exchange is against man-in-the-middle attacks.)
77
78 A value of nil says to use the default GnuTLS value."
79 :type '(choice (const :tag "Use default value" nil)
80 (integer :tag "Number of bits" 512))
81 :group 'gnutls)
82
83 (defun open-gnutls-stream (name buffer host service)
84 "Open a SSL/TLS connection for a service to a host.
85 Returns a subprocess-object to represent the connection.
86 Input and output work as for subprocesses; `delete-process' closes it.
87 Args are NAME BUFFER HOST SERVICE.
88 NAME is name for process. It is modified if necessary to make it unique.
89 BUFFER is the buffer (or `buffer-name') to associate with the process.
90 Process output goes at end of that buffer, unless you specify
91 an output stream or filter function to handle the output.
92 BUFFER may be also nil, meaning that this process is not associated
93 with any buffer
94 Third arg is name of the host to connect to, or its IP address.
95 Fourth arg SERVICE is name of the service desired, or an integer
96 specifying a port number to connect to.
97
98 Usage example:
99
100 \(with-temp-buffer
101 \(open-gnutls-stream \"tls\"
102 \(current-buffer)
103 \"your server goes here\"
104 \"imaps\"))
105
106 This is a very simple wrapper around `gnutls-negotiate'. See its
107 documentation for the specific parameters you can use to open a
108 GnuTLS connection, including specifying the credential type,
109 trust and key files, and priority string."
110 (gnutls-negotiate :process (open-network-stream name buffer host service)
111 :type 'gnutls-x509pki
112 :hostname host))
113
114 (define-error 'gnutls-error "GnuTLS error")
115
116 (declare-function gnutls-boot "gnutls.c" (proc type proplist))
117 (declare-function gnutls-errorp "gnutls.c" (error))
118 (defvar gnutls-log-level) ; gnutls.c
119
120 (cl-defun gnutls-negotiate
121 (&rest spec
122 &key process type hostname priority-string
123 trustfiles crlfiles keylist min-prime-bits
124 verify-flags verify-error verify-hostname-error
125 &allow-other-keys)
126 "Negotiate a SSL/TLS connection. Returns proc. Signals gnutls-error.
127
128 Note arguments are passed CL style, :type TYPE instead of just TYPE.
129
130 TYPE is `gnutls-x509pki' (default) or `gnutls-anon'. Use nil for the default.
131 PROCESS is a process returned by `open-network-stream'.
132 HOSTNAME is the remote hostname. It must be a valid string.
133 PRIORITY-STRING is as per the GnuTLS docs, default is \"NORMAL\".
134 TRUSTFILES is a list of CA bundles. It defaults to `gnutls-trustfiles'.
135 CRLFILES is a list of CRL files.
136 KEYLIST is an alist of (client key file, client cert file) pairs.
137 MIN-PRIME-BITS is the minimum acceptable size of Diffie-Hellman keys
138 \(see `gnutls-min-prime-bits' for more information). Use nil for the
139 default.
140
141 When VERIFY-HOSTNAME-ERROR is not nil, an error will be raised
142 when the hostname does not match the presented certificate's host
143 name. The exact verification algorithm is a basic implementation
144 of the matching described in RFC2818 (HTTPS), which takes into
145 account wildcards, and the DNSName/IPAddress subject alternative
146 name PKIX extension. See GnuTLS' gnutls_x509_crt_check_hostname
147 for details. When VERIFY-HOSTNAME-ERROR is nil, only a warning
148 will be issued.
149
150 When VERIFY-ERROR is not nil, an error will be raised when the
151 peer certificate verification fails as per GnuTLS'
152 gnutls_certificate_verify_peers2. Otherwise, only warnings will
153 be shown about the verification failure.
154
155 VERIFY-FLAGS is a numeric OR of verification flags only for
156 `gnutls-x509pki' connections. See GnuTLS' x509.h for details;
157 here's a recent version of the list.
158
159 GNUTLS_VERIFY_DISABLE_CA_SIGN = 1,
160 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT = 2,
161 GNUTLS_VERIFY_DO_NOT_ALLOW_SAME = 4,
162 GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT = 8,
163 GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2 = 16,
164 GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5 = 32,
165 GNUTLS_VERIFY_DISABLE_TIME_CHECKS = 64,
166 GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS = 128,
167 GNUTLS_VERIFY_DO_NOT_ALLOW_X509_V1_CA_CRT = 256
168
169 It must be omitted, a number, or nil; if omitted or nil it
170 defaults to GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT."
171 (let* ((type (or type 'gnutls-x509pki))
172 (trustfiles (or trustfiles
173 (delq nil
174 (mapcar (lambda (f) (and f (file-exists-p f) f))
175 (if (functionp gnutls-trustfiles)
176 (funcall gnutls-trustfiles)
177 gnutls-trustfiles)))))
178 (priority-string (or priority-string
179 (cond
180 ((eq type 'gnutls-anon)
181 "NORMAL:+ANON-DH:!ARCFOUR-128")
182 ((eq type 'gnutls-x509pki)
183 (if gnutls-algorithm-priority
184 (upcase gnutls-algorithm-priority)
185 "NORMAL")))))
186 (min-prime-bits (or min-prime-bits gnutls-min-prime-bits))
187 (params `(:priority ,priority-string
188 :hostname ,hostname
189 :loglevel ,gnutls-log-level
190 :min-prime-bits ,min-prime-bits
191 :trustfiles ,trustfiles
192 :crlfiles ,crlfiles
193 :keylist ,keylist
194 :verify-flags ,verify-flags
195 :verify-error ,verify-error
196 :verify-hostname-error ,verify-hostname-error
197 :callbacks nil))
198 ret)
199
200 (gnutls-message-maybe
201 (setq ret (gnutls-boot process type params))
202 "boot: %s" params)
203
204 (when (gnutls-errorp ret)
205 ;; This is a error from the underlying C code.
206 (signal 'gnutls-error (list process ret)))
207
208 process))
209
210 (declare-function gnutls-error-string "gnutls.c" (error))
211
212 (defun gnutls-message-maybe (doit format &rest params)
213 "When DOIT, message with the caller name followed by FORMAT on PARAMS."
214 ;; (apply 'debug format (or params '(nil)))
215 (when (gnutls-errorp doit)
216 (message "%s: (err=[%s] %s) %s"
217 "gnutls.el"
218 doit (gnutls-error-string doit)
219 (apply 'format format (or params '(nil))))))
220
221 (provide 'gnutls)
222
223 ;;; gnutls.el ends here