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