]> code.delx.au - gnu-emacs/blob - lisp/net/gnutls.el
Merge changes from emacs-23
[gnu-emacs] / lisp / net / gnutls.el
1 ;;; gnutls.el --- Support SSL/TLS connections through GnuTLS
2 ;; Copyright (C) 2010 Free Software Foundation, Inc.
3
4 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
5 ;; Keywords: comm, tls, ssl, encryption
6 ;; Originally-By: Simon Josefsson (See http://josefsson.org/emacs-security/)
7 ;; Thanks-To: Lars Magne Ingebrigtsen <larsi@gnus.org>
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This package provides language bindings for the GnuTLS library
27 ;; using the corresponding core functions in gnutls.c.
28
29 ;; Simple test:
30 ;;
31 ;; (open-gnutls-stream "tls" "tls-buffer" "yourserver.com" "https")
32 ;; (open-gnutls-stream "tls" "tls-buffer" "imap.gmail.com" "imaps")
33
34 ;;; Code:
35
36 (defgroup gnutls nil
37 "Emacs interface to the GnuTLS library."
38 :prefix "gnutls-"
39 :group 'net-utils)
40
41 (defcustom gnutls-log-level 0
42 "Logging level to be used by `starttls-negotiate' and GnuTLS."
43 :type 'integer
44 :group 'gnutls)
45
46 (defun open-gnutls-stream (name buffer host service)
47 "Open a SSL/TLS connection for a service to a host.
48 Returns a subprocess-object to represent the connection.
49 Input and output work as for subprocesses; `delete-process' closes it.
50 Args are NAME BUFFER HOST SERVICE.
51 NAME is name for process. It is modified if necessary to make it unique.
52 BUFFER is the buffer (or `buffer-name') to associate with the process.
53 Process output goes at end of that buffer, unless you specify
54 an output stream or filter function to handle the output.
55 BUFFER may be also nil, meaning that this process is not associated
56 with any buffer
57 Third arg is name of the host to connect to, or its IP address.
58 Fourth arg SERVICE is name of the service desired, or an integer
59 specifying a port number to connect to.
60
61 This is a very simple wrapper around `gnutls-negotiate'. See its
62 documentation for the specific parameters you can use to open a
63 GnuTLS connection, including specifying the credential type,
64 trust and key files, and priority string."
65 (let ((proc (open-network-stream name buffer host service)))
66 (gnutls-negotiate proc 'gnutls-x509pki)))
67
68 (defun gnutls-negotiate (proc type &optional priority-string
69 trustfiles keyfiles)
70 "Negotiate a SSL/TLS connection.
71 TYPE is `gnutls-x509pki' (default) or `gnutls-anon'. Use nil for the default.
72 PROC is a process returned by `open-network-stream'.
73 PRIORITY-STRING is as per the GnuTLS docs, default is \"NORMAL\".
74 TRUSTFILES is a list of CA bundles.
75 KEYFILES is a list of client keys."
76 (let* ((type (or type 'gnutls-x509pki))
77 (trusfiles (or trustfiles
78 '("/etc/ssl/certs/ca-certificates.crt")))
79 (priority-string (or priority-string
80 (cond
81 ((eq type 'gnutls-anon)
82 "NORMAL:+ANON-DH:!ARCFOUR-128")
83 ((eq type 'gnutls-x509pki)
84 "NORMAL"))))
85 (params `(:priority ,priority-string
86 :loglevel ,gnutls-log-level
87 :trustfiles ,trustfiles
88 :keyfiles ,keyfiles
89 :callbacks nil))
90 ret)
91
92 (gnutls-message-maybe
93 (setq ret (gnutls-boot proc type params))
94 "boot: %s")
95
96 proc))
97
98 (defun gnutls-message-maybe (doit format &rest params)
99 "When DOIT, message with the caller name followed by FORMAT on PARAMS."
100 ;; (apply 'debug format (or params '(nil)))
101 (when (gnutls-errorp doit)
102 (message "%s: (err=[%s] %s) %s"
103 "gnutls.el"
104 doit (gnutls-error-string doit)
105 (apply 'format format (or params '(nil))))))
106
107 (provide 'gnutls)
108
109 ;;; gnutls.el ends here