]> code.delx.au - gnu-emacs/blob - lisp/net/gnutls.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / 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.
29
30 ;; Simple test:
31 ;;
32 ;; (open-gnutls-stream "tls" "tls-buffer" "yourserver.com" "https")
33 ;; (open-gnutls-stream "tls" "tls-buffer" "imap.gmail.com" "imaps")
34
35 ;;; Code:
36
37 (defgroup gnutls nil
38 "Emacs interface to the GnuTLS library."
39 :prefix "gnutls-"
40 :group 'net-utils)
41
42 (defcustom gnutls-log-level 0
43 "Logging level to be used by `starttls-negotiate' and GnuTLS."
44 :type 'integer
45 :group 'gnutls)
46
47 (defun open-gnutls-stream (name buffer host service)
48 "Open a SSL/TLS connection for a service to a host.
49 Returns a subprocess-object to represent the connection.
50 Input and output work as for subprocesses; `delete-process' closes it.
51 Args are NAME BUFFER HOST SERVICE.
52 NAME is name for process. It is modified if necessary to make it unique.
53 BUFFER is the buffer (or `buffer-name') to associate with the process.
54 Process output goes at end of that buffer, unless you specify
55 an output stream or filter function to handle the output.
56 BUFFER may be also nil, meaning that this process is not associated
57 with any buffer
58 Third arg is name of the host to connect to, or its IP address.
59 Fourth arg SERVICE is name of the service desired, or an integer
60 specifying a port number to connect to.
61
62 This is a very simple wrapper around `gnutls-negotiate'. See its
63 documentation for the specific parameters you can use to open a
64 GnuTLS connection, including specifying the credential type,
65 trust and key files, and priority string."
66 (let ((proc (open-network-stream name buffer host service)))
67 (gnutls-negotiate proc 'gnutls-x509pki)))
68
69 (declare-function gnutls-boot "gnutls.c" (proc type proplist))
70
71 (defun gnutls-negotiate (proc type &optional priority-string
72 trustfiles keyfiles)
73 "Negotiate a SSL/TLS connection.
74 TYPE is `gnutls-x509pki' (default) or `gnutls-anon'. Use nil for the default.
75 PROC is a process returned by `open-network-stream'.
76 PRIORITY-STRING is as per the GnuTLS docs, default is \"NORMAL\".
77 TRUSTFILES is a list of CA bundles.
78 KEYFILES is a list of client keys."
79 (let* ((type (or type 'gnutls-x509pki))
80 (trustfiles (or trustfiles
81 '("/etc/ssl/certs/ca-certificates.crt")))
82 (priority-string (or priority-string
83 (cond
84 ((eq type 'gnutls-anon)
85 "NORMAL:+ANON-DH:!ARCFOUR-128")
86 ((eq type 'gnutls-x509pki)
87 "NORMAL"))))
88 (params `(:priority ,priority-string
89 :loglevel ,gnutls-log-level
90 :trustfiles ,trustfiles
91 :keyfiles ,keyfiles
92 :callbacks nil))
93 ret)
94
95 (gnutls-message-maybe
96 (setq ret (gnutls-boot proc type params))
97 "boot: %s")
98
99 proc))
100
101 (declare-function gnutls-errorp "gnutls.c" (error))
102 (declare-function gnutls-error-string "gnutls.c" (error))
103
104 (defun gnutls-message-maybe (doit format &rest params)
105 "When DOIT, message with the caller name followed by FORMAT on PARAMS."
106 ;; (apply 'debug format (or params '(nil)))
107 (when (gnutls-errorp doit)
108 (message "%s: (err=[%s] %s) %s"
109 "gnutls.el"
110 doit (gnutls-error-string doit)
111 (apply 'format format (or params '(nil))))))
112
113 (provide 'gnutls)
114
115 ;;; gnutls.el ends here