Reduce use of (require 'cl).
[emacs.git] / lisp / net / gnutls.el
blobd33480afb2891d83826f3cab323c176eaa96377d
1 ;;; gnutls.el --- Support SSL/TLS connections through GnuTLS
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
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>
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
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.
31 ;; Simple test:
33 ;; (open-gnutls-stream "tls" "tls-buffer" "yourserver.com" "https")
34 ;; (open-gnutls-stream "tls" "tls-buffer" "imap.gmail.com" "imaps")
36 ;;; Code:
38 (eval-when-compile (require 'cl-lib))
40 (defgroup gnutls nil
41 "Emacs interface to the GnuTLS library."
42 :version "24.1"
43 :prefix "gnutls-"
44 :group 'net-utils)
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))
54 (defcustom gnutls-trustfiles
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
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"))))
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.)
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)
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.
98 Usage example:
100 \(with-temp-buffer
101 \(open-gnutls-stream \"tls\"
102 \(current-buffer)
103 \"your server goes here\"
104 \"imaps\"))
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))
114 (put 'gnutls-error
115 'error-conditions
116 '(error gnutls-error))
117 (put 'gnutls-error
118 'error-message "GnuTLS error")
120 (declare-function gnutls-boot "gnutls.c" (proc type proplist))
121 (declare-function gnutls-errorp "gnutls.c" (error))
123 (cl-defun gnutls-negotiate
124 (&rest spec
125 &key process type hostname priority-string
126 trustfiles crlfiles keylist min-prime-bits
127 verify-flags verify-error verify-hostname-error
128 &allow-other-keys)
129 "Negotiate a SSL/TLS connection. Returns proc. Signals gnutls-error.
131 Note arguments are passed CL style, :type TYPE instead of just TYPE.
133 TYPE is `gnutls-x509pki' (default) or `gnutls-anon'. Use nil for the default.
134 PROCESS is a process returned by `open-network-stream'.
135 HOSTNAME is the remote hostname. It must be a valid string.
136 PRIORITY-STRING is as per the GnuTLS docs, default is \"NORMAL\".
137 TRUSTFILES is a list of CA bundles. It defaults to `gnutls-trustfiles'.
138 CRLFILES is a list of CRL files.
139 KEYLIST is an alist of (client key file, client cert file) pairs.
140 MIN-PRIME-BITS is the minimum acceptable size of Diffie-Hellman keys
141 \(see `gnutls-min-prime-bits' for more information). Use nil for the
142 default.
144 When VERIFY-HOSTNAME-ERROR is not nil, an error will be raised
145 when the hostname does not match the presented certificate's host
146 name. The exact verification algorithm is a basic implementation
147 of the matching described in RFC2818 (HTTPS), which takes into
148 account wildcards, and the DNSName/IPAddress subject alternative
149 name PKIX extension. See GnuTLS' gnutls_x509_crt_check_hostname
150 for details. When VERIFY-HOSTNAME-ERROR is nil, only a warning
151 will be issued.
153 When VERIFY-ERROR is not nil, an error will be raised when the
154 peer certificate verification fails as per GnuTLS'
155 gnutls_certificate_verify_peers2. Otherwise, only warnings will
156 be shown about the verification failure.
158 VERIFY-FLAGS is a numeric OR of verification flags only for
159 `gnutls-x509pki' connections. See GnuTLS' x509.h for details;
160 here's a recent version of the list.
162 GNUTLS_VERIFY_DISABLE_CA_SIGN = 1,
163 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT = 2,
164 GNUTLS_VERIFY_DO_NOT_ALLOW_SAME = 4,
165 GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT = 8,
166 GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2 = 16,
167 GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5 = 32,
168 GNUTLS_VERIFY_DISABLE_TIME_CHECKS = 64,
169 GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS = 128,
170 GNUTLS_VERIFY_DO_NOT_ALLOW_X509_V1_CA_CRT = 256
172 It must be omitted, a number, or nil; if omitted or nil it
173 defaults to GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT."
174 (let* ((type (or type 'gnutls-x509pki))
175 (trustfiles (or trustfiles
176 (delq nil
177 (mapcar (lambda (f) (and f (file-exists-p f) f))
178 (if (functionp gnutls-trustfiles)
179 (funcall gnutls-trustfiles)
180 gnutls-trustfiles)))))
181 (priority-string (or priority-string
182 (cond
183 ((eq type 'gnutls-anon)
184 "NORMAL:+ANON-DH:!ARCFOUR-128")
185 ((eq type 'gnutls-x509pki)
186 (if gnutls-algorithm-priority
187 (upcase gnutls-algorithm-priority)
188 "NORMAL")))))
189 (min-prime-bits (or min-prime-bits gnutls-min-prime-bits))
190 (params `(:priority ,priority-string
191 :hostname ,hostname
192 :loglevel ,gnutls-log-level
193 :min-prime-bits ,min-prime-bits
194 :trustfiles ,trustfiles
195 :crlfiles ,crlfiles
196 :keylist ,keylist
197 :verify-flags ,verify-flags
198 :verify-error ,verify-error
199 :verify-hostname-error ,verify-hostname-error
200 :callbacks nil))
201 ret)
203 (gnutls-message-maybe
204 (setq ret (gnutls-boot process type params))
205 "boot: %s" params)
207 (when (gnutls-errorp ret)
208 ;; This is a error from the underlying C code.
209 (signal 'gnutls-error (list process ret)))
211 process))
213 (declare-function gnutls-error-string "gnutls.c" (error))
215 (defun gnutls-message-maybe (doit format &rest params)
216 "When DOIT, message with the caller name followed by FORMAT on PARAMS."
217 ;; (apply 'debug format (or params '(nil)))
218 (when (gnutls-errorp doit)
219 (message "%s: (err=[%s] %s) %s"
220 "gnutls.el"
221 doit (gnutls-error-string doit)
222 (apply 'format format (or params '(nil))))))
224 (provide 'gnutls)
226 ;;; gnutls.el ends here