lisp/net/gnutls.el (gnutls-errorp): Declare before first use.
[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 (defgroup gnutls nil
39 "Emacs interface to the GnuTLS library."
40 :prefix "gnutls-"
41 :group 'net-utils)
42
43 (defcustom gnutls-log-level 0
44 "Logging level to be used by `starttls-negotiate' and GnuTLS."
45 :type 'integer
46 :group 'gnutls)
47
48 (defun open-gnutls-stream (name buffer host service)
49 "Open a SSL/TLS connection for a service to a host.
50 Returns a subprocess-object to represent the connection.
51 Input and output work as for subprocesses; `delete-process' closes it.
52 Args are NAME BUFFER HOST SERVICE.
53 NAME is name for process. It is modified if necessary to make it unique.
54 BUFFER is the buffer (or `buffer-name') to associate with the process.
55 Process output goes at end of that buffer, unless you specify
56 an output stream or filter function to handle the output.
57 BUFFER may be also nil, meaning that this process is not associated
58 with any buffer
59 Third arg is name of the host to connect to, or its IP address.
60 Fourth arg SERVICE is name of the service desired, or an integer
61 specifying a port number to connect to.
62
63 Usage example:
64
65 \(with-temp-buffer
66 \(open-gnutls-stream \"tls\"
67 \(current-buffer)
68 \"your server goes here\"
69 \"imaps\"))
70
71 This is a very simple wrapper around `gnutls-negotiate'. See its
72 documentation for the specific parameters you can use to open a
73 GnuTLS connection, including specifying the credential type,
74 trust and key files, and priority string."
75 (gnutls-negotiate (open-network-stream name buffer host service)
76 'gnutls-x509pki
77 host))
78
79 (put 'gnutls-error
80 'error-conditions
81 '(error gnutls-error))
82 (put 'gnutls-error
83 'error-message "GnuTLS error")
84
85 (declare-function gnutls-boot "gnutls.c" (proc type proplist))
86 (declare-function gnutls-errorp "gnutls.c" (error))
87
88 (defun gnutls-negotiate (proc type hostname &optional priority-string
89 trustfiles keyfiles verify-flags
90 verify-error verify-hostname-error)
91 "Negotiate a SSL/TLS connection. Returns proc. Signals gnutls-error.
92 TYPE is `gnutls-x509pki' (default) or `gnutls-anon'. Use nil for the default.
93 PROC is a process returned by `open-network-stream'.
94 HOSTNAME is the remote hostname. It must be a valid string.
95 PRIORITY-STRING is as per the GnuTLS docs, default is \"NORMAL\".
96 TRUSTFILES is a list of CA bundles.
97 KEYFILES is a list of client keys.
98
99 When VERIFY-HOSTNAME-ERROR is not nil, an error will be raised
100 when the hostname does not match the presented certificate's host
101 name. The exact verification algorithm is a basic implementation
102 of the matching described in RFC2818 (HTTPS), which takes into
103 account wildcards, and the DNSName/IPAddress subject alternative
104 name PKIX extension. See GnuTLS' gnutls_x509_crt_check_hostname
105 for details. When VERIFY-HOSTNAME-ERROR is nil, only a warning
106 will be issued.
107
108 When VERIFY-ERROR is not nil, an error will be raised when the
109 peer certificate verification fails as per GnuTLS'
110 gnutls_certificate_verify_peers2. Otherwise, only warnings will
111 be shown about the verification failure.
112
113 VERIFY-FLAGS is a numeric OR of verification flags only for
114 `gnutls-x509pki' connections. See GnuTLS' x509.h for details;
115 here's a recent version of the list.
116
117 GNUTLS_VERIFY_DISABLE_CA_SIGN = 1,
118 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT = 2,
119 GNUTLS_VERIFY_DO_NOT_ALLOW_SAME = 4,
120 GNUTLS_VERIFY_ALLOW_ANY_X509_V1_CA_CRT = 8,
121 GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD2 = 16,
122 GNUTLS_VERIFY_ALLOW_SIGN_RSA_MD5 = 32,
123 GNUTLS_VERIFY_DISABLE_TIME_CHECKS = 64,
124 GNUTLS_VERIFY_DISABLE_TRUSTED_TIME_CHECKS = 128,
125 GNUTLS_VERIFY_DO_NOT_ALLOW_X509_V1_CA_CRT = 256
126
127 It must be omitted, a number, or nil; if omitted or nil it
128 defaults to GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT."
129 (let* ((type (or type 'gnutls-x509pki))
130 (default-trustfile "/etc/ssl/certs/ca-certificates.crt")
131 (trustfiles (or trustfiles
132 (when (file-exists-p default-trustfile)
133 (list default-trustfile))))
134 (priority-string (or priority-string
135 (cond
136 ((eq type 'gnutls-anon)
137 "NORMAL:+ANON-DH:!ARCFOUR-128")
138 ((eq type 'gnutls-x509pki)
139 "NORMAL"))))
140 (params `(:priority ,priority-string
141 :hostname ,hostname
142 :loglevel ,gnutls-log-level
143 :trustfiles ,trustfiles
144 :keyfiles ,keyfiles
145 :verify-flags ,verify-flags
146 :verify-error ,verify-error
147 :verify-hostname-error ,verify-hostname-error
148 :callbacks nil))
149 ret)
150
151 (gnutls-message-maybe
152 (setq ret (gnutls-boot proc type params))
153 "boot: %s" params)
154
155 (when (gnutls-errorp ret)
156 ;; This is a error from the underlying C code.
157 (signal 'gnutls-error (list proc ret)))
158
159 proc))
160
161 (declare-function gnutls-error-string "gnutls.c" (error))
162
163 (defun gnutls-message-maybe (doit format &rest params)
164 "When DOIT, message with the caller name followed by FORMAT on PARAMS."
165 ;; (apply 'debug format (or params '(nil)))
166 (when (gnutls-errorp doit)
167 (message "%s: (err=[%s] %s) %s"
168 "gnutls.el"
169 doit (gnutls-error-string doit)
170 (apply 'format format (or params '(nil))))))
171
172 (provide 'gnutls)
173
174 ;;; gnutls.el ends here