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