Commit 8af55556 authored by Ted Zlatanov's avatar Ted Zlatanov

Set up GnuTLS support.

* configure.in: Set up GnuTLS.

* lisp/net/gnutls.el: GnuTLS glue code to set up a connection.

* src/Makefile.in (LIBGNUTLS_LIBS, LIBGNUTLS_CFLAGS, ALL_CFLAGS)
(obj, LIBES): Set up GnuTLS support.

* src/config.in: Set up GnuTLS support.

* src/emacs.c: Set up GnuTLS support and call syms_of_gnutls.

* src/gnutls.c: The source code for GnuTLS support in Emacs.

* src/gnutls.h: The GnuTLS glue for Emacs, macros and enums.

* src/process.c (make_process, Fstart_process)
(read_process_output, send_process): Set up GnuTLS support for
process input/output file descriptors.

* src/process.h: Set up GnuTLS support.
parent 8ccbef23
2010-09-26 Teodor Zlatanov <tzz@lifelogs.com>
* configure.in: Set up GnuTLS.
2010-09-22 Chong Yidong <cyd@stupidchicken.com>
* configure.in: Announce whether libxml2 is linked to.
......
......@@ -171,6 +171,7 @@ OPTION_DEFAULT_ON([gpm],[don't use -lgpm for mouse support on a GNU/Linux consol
OPTION_DEFAULT_ON([dbus],[don't compile with D-Bus support])
OPTION_DEFAULT_ON([gconf],[don't compile with GConf support])
OPTION_DEFAULT_ON([selinux],[don't compile with SELinux support])
OPTION_DEFAULT_ON([gnutls],[don't use -lgnutls for SSL/TLS support])
## For the times when you want to build Emacs but don't have
## a suitable makeinfo, and can live without the manuals.
......@@ -1999,6 +2000,13 @@ if test "${with_selinux}" = "yes"; then
fi
AC_SUBST(LIBSELINUX_LIBS)
HAVE_GNUTLS=no
if test "${with_gnutls}" = "yes" ; then
PKG_CHECK_MODULES([LIBGNUTLS], [gnutls >= 2.2.4])
AC_DEFINE(HAVE_GNUTLS)
HAVE_GNUTLS=yes
fi
dnl Do not put whitespace before the #include statements below.
dnl Older compilers (eg sunos4 cc) choke on it.
HAVE_XAW3D=no
......@@ -3701,6 +3709,7 @@ echo " Does Emacs use -lgpm? ${HAVE_GPM}"
echo " Does Emacs use -ldbus? ${HAVE_DBUS}"
echo " Does Emacs use -lgconf? ${HAVE_GCONF}"
echo " Does Emacs use -lselinux? ${HAVE_LIBSELINUX}"
echo " Does Emacs use -lgnutls (BROKEN)? ${HAVE_GNUTLS}"
echo " Does Emacs use -lxml2? ${HAVE_LIBXML2}"
echo " Does Emacs use -lfreetype? ${HAVE_FREETYPE}"
......
2010-09-26 Teodor Zlatanov <tzz@lifelogs.com>
* net/gnutls.el: GnuTLS glue code to set up a connection.
2010-09-25 Julien Danjou <julien@danjou.info>
* notifications.el: Call dbus-register-signal only if it is bound.
......
;;; gnutls.el --- Support SSL and TLS connections through GnuTLS
;; Copyright (C) 2010 Free Software Foundation, Inc.
;; Author: Ted Zlatanov <tzz@lifelogs.com>
;; Keywords: comm, tls, ssl, encryption
;; Originally-By: Simon Josefsson (See http://josefsson.org/emacs-security/)
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides language bindings for the GnuTLS library
;; using the corresponding core functions in gnutls.c.
;; Simple test:
;;
;; (setq jas (open-ssl-stream "ssl" (current-buffer) "www.pdc.kth.se" 443))
;; (process-send-string jas "GET /\r\n\r\n")
;;; Code:
(defun open-ssl-stream (name buffer host service)
"Open a SSL connection for a service to a host.
Returns a subprocess-object to represent the connection.
Input and output work as for subprocesses; `delete-process' closes it.
Args are NAME BUFFER HOST SERVICE.
NAME is name for process. It is modified if necessary to make it unique.
BUFFER is the buffer (or `buffer-name') to associate with the process.
Process output goes at end of that buffer, unless you specify
an output stream or filter function to handle the output.
BUFFER may be also nil, meaning that this process is not associated
with any buffer
Third arg is name of the host to connect to, or its IP address.
Fourth arg SERVICE is name of the service desired, or an integer
specifying a port number to connect to."
(let ((proc (open-network-stream name buffer host service)))
(starttls-negotiate proc nil 'gnutls-x509pki)))
;; (open-ssl-stream "tls" "tls-buffer" "yourserver.com" "https")
(defun starttls-negotiate (proc &optional priority-string
credentials credentials-file)
"Negotiate a SSL or TLS connection.
PROC is the process returned by `starttls-open-stream'.
PRIORITY-STRING is as per the GnuTLS docs.
CREDENTIALS is `gnutls-x509pki' or `gnutls-anon'.
CREDENTIALS-FILE is a filename with meaning dependent on CREDENTIALS."
(let* ((credentials (or credentials 'gnutls-x509pki))
(credentials-file (or credentials-file
"/etc/ssl/certs/ca-certificates.crt"
;"/etc/ssl/certs/ca.pem"
))
(priority-string (or priority-string
(cond
((eq credentials 'gnutls-anon)
"NORMAL:+ANON-DH:!ARCFOUR-128")
((eq credentials 'gnutls-x509pki)
"NORMAL"))))
ret)
(gnutls-message-maybe
(setq ret (gnutls-boot proc priority-string credentials credentials-file))
"boot: %s")
(when (gnutls-errorp ret)
(error "Could not boot GnuTLS for this process"));
(let ((ret 'gnutls-e-again)
(n 25000))
(while (and (not (gnutls-error-fatalp ret))
(> n 0))
(decf n)
(gnutls-message-maybe
(setq ret (gnutls-handshake proc))
"handshake: %s")
;(debug "handshake ret" ret (gnutls-error-string ret)))
)
(if (gnutls-errorp ret)
(progn
(message "Ouch, error return %s (%s)"
ret (gnutls-error-string ret))
(setq proc nil))
(message "Handshake complete %s." ret)))
proc))
(defun starttls-open-stream (name buffer host service)
"Open a TLS connection for a service to a host.
Returns a subprocess-object to represent the connection.
Input and output work as for subprocesses; `delete-process' closes it.
Args are NAME BUFFER HOST SERVICE.
NAME is name for process. It is modified if necessary to make it unique.
BUFFER is the buffer (or `buffer-name') to associate with the process.
Process output goes at end of that buffer, unless you specify
an output stream or filter function to handle the output.
BUFFER may be also nil, meaning that this process is not associated
with any buffer
Third arg is name of the host to connect to, or its IP address.
Fourth arg SERVICE is name of the service desired, or an integer
specifying a port number to connect to."
(open-network-stream name buffer host service))
(defun gnutls-message-maybe (doit format &rest params)
"When DOIT, message with the caller name followed by FORMAT on PARAMS."
;; (apply 'debug format (or params '(nil)))
(when (gnutls-errorp doit)
(message "%s: (err=[%s] %s) %s"
"gnutls.el"
doit (gnutls-error-string doit)
(apply 'format format (or params '(nil))))))
(provide 'ssl)
(provide 'gnutls)
(provide 'starttls)
;;; gnutls.el ends here
2010-09-26 Teodor Zlatanov <tzz@lifelogs.com>
* process.h: Set up GnuTLS support.
* process.c (make_process, Fstart_process)
(read_process_output, send_process): Set up GnuTLS support for
process input/output file descriptors.
* gnutls.h: The GnuTLS glue for Emacs, macros and enums.
* gnutls.c: The source code for GnuTLS support in Emacs.
* emacs.c: Set up GnuTLS support and call syms_of_gnutls.
* config.in: Set up GnuTLS support.
* Makefile.in (LIBGNUTLS_LIBS, LIBGNUTLS_CFLAGS, ALL_CFLAGS)
(obj, LIBES): Set up GnuTLS support.
2010-09-26 Juanma Barranquero <lekktu@gmail.com>
* w32.c (get_emacs_configuration_options): Fix previous change.
......
......@@ -286,6 +286,9 @@ LIBRESOLV = @LIBRESOLV@
LIBSELINUX_LIBS = @LIBSELINUX_LIBS@
LIBGNUTLS_LIBS = @LIBGNUTLS_LIBS@
LIBGNUTLS_CFLAGS = @LIBGNUTLS_CFLAGS@
INTERVALS_H = dispextern.h intervals.h composite.h
GETLOADAVG_LIBS = @GETLOADAVG_LIBS@
......@@ -325,6 +328,7 @@ ALL_CFLAGS=-Demacs -DHAVE_CONFIG_H $(MYCPPFLAGS) -I. -I${srcdir} \
${LIBXML2_CFLAGS} ${DBUS_CFLAGS} \
${GCONF_CFLAGS} ${FREETYPE_CFLAGS} ${FONTCONFIG_CFLAGS} \
${LIBOTF_CFLAGS} ${M17N_FLT_CFLAGS} ${DEPFLAGS} ${PROFILING_CFLAGS} \
$(LIBGNUTLS_CFLAGS) \
${C_WARNINGS_SWITCH} ${CFLAGS}
ALL_OBJC_CFLAGS=$(ALL_CFLAGS) $(GNU_OBJC_CFLAGS)
......@@ -349,7 +353,7 @@ obj= dispnew.o frame.o scroll.o xdisp.o menu.o $(XMENU_OBJ) window.o \
alloc.o data.o doc.o editfns.o callint.o \
eval.o floatfns.o fns.o font.o print.o lread.o \
syntax.o $(UNEXEC_OBJ) bytecode.o \
process.o callproc.o \
process.o gnutls.o callproc.o \
region-cache.o sound.o atimer.o \
doprnt.o strftime.o intervals.o textprop.o composite.o md5.o xml.o \
$(MSDOS_OBJ) $(MSDOS_X_OBJ) $(NS_OBJ) $(CYGWIN_OBJ) $(FONT_OBJ)
......@@ -601,6 +605,7 @@ LIBES = $(LIBS) $(LIBX_BASE) $(LIBX_OTHER) $(LIBSOUND) \
${LIBXML2_LIBS} $(LIBGPM) $(LIBRESOLV) $(LIBS_SYSTEM) \
$(LIBS_TERMCAP) $(GETLOADAVG_LIBS) ${GCONF_LIBS} ${LIBSELINUX_LIBS} \
$(FREETYPE_LIBS) $(FONTCONFIG_LIBS) $(LIBOTF_LIBS) $(M17N_FLT_LIBS) \
$(LIBGNUTLS_LIBS) \
$(LIB_GCC) $(LIB_MATH) $(LIB_STANDARD) $(LIB_GCC)
all: emacs${EXEEXT} $(OTHER_FILES)
......
......@@ -255,6 +255,9 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
/* Define to 1 if you have a gif (or ungif) library. */
#undef HAVE_GIF
/* Define if we have the GNU TLS library. */
#undef HAVE_GNUTLS
/* Define to 1 if you have the gpm library (-lgpm). */
#undef HAVE_GPM
......@@ -1094,6 +1097,12 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#include config_opsysfile
#include config_machfile
#if HAVE_GNUTLS
#define LIBGNUTLS $(LIBGNUTLS_LIBS)
#else /* not HAVE_GNUTLS */
#define LIBGNUTLS
#endif /* not HAVE_GNUTLS */
/* Set up some defines, C and LD flags for NeXTstep interface on GNUstep.
(There is probably a better place to do this, but right now the Cocoa
side does this in s/darwin.h and we cannot
......
......@@ -59,6 +59,10 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#include "keyboard.h"
#include "keymap.h"
#ifdef HAVE_GNUTLS
#include "gnutls.h"
#endif
#ifdef HAVE_NS
#include "nsterm.h"
#endif
......@@ -1569,6 +1573,10 @@ main (int argc, char **argv)
syms_of_fontset ();
#endif /* HAVE_NS */
#ifdef HAVE_GNUTLS
syms_of_gnutls ();
#endif
#ifdef HAVE_DBUS
syms_of_dbusbind ();
#endif /* HAVE_DBUS */
......
/* GnuTLS glue for GNU Emacs.
Copyright (C) 2010 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <errno.h>
#include <setjmp.h>
#include "lisp.h"
#include "process.h"
#ifdef HAVE_GNUTLS
#include <gnutls/gnutls.h>
Lisp_Object Qgnutls_code;
Lisp_Object Qgnutls_anon, Qgnutls_x509pki;
Lisp_Object Qgnutls_e_interrupted, Qgnutls_e_again,
Qgnutls_e_invalid_session, Qgnutls_e_not_ready_for_handshake;
int global_initialized;
int
emacs_gnutls_write (int fildes, gnutls_session_t state, char *buf,
unsigned int nbyte)
{
register int rtnval, bytes_written;
bytes_written = 0;
while (nbyte > 0)
{
rtnval = gnutls_write (state, buf, nbyte);
if (rtnval == -1)
{
if (errno == EINTR)
continue;
else
return (bytes_written ? bytes_written : -1);
}
buf += rtnval;
nbyte -= rtnval;
bytes_written += rtnval;
}
fsync (STDOUT_FILENO);
return (bytes_written);
}
int
emacs_gnutls_read (int fildes, gnutls_session_t state, char *buf,
unsigned int nbyte)
{
register int rtnval;
do {
rtnval = gnutls_read (state, buf, nbyte);
} while (rtnval == GNUTLS_E_INTERRUPTED || rtnval == GNUTLS_E_AGAIN);
fsync (STDOUT_FILENO);
return (rtnval);
}
/* convert an integer error to a Lisp_Object; it will be either a
known symbol like `gnutls_e_interrupted' and `gnutls_e_again' or
simply the integer value of the error. GNUTLS_E_SUCCESS is mapped
to Qt. */
Lisp_Object gnutls_make_error (int error)
{
switch (error)
{
case GNUTLS_E_SUCCESS:
return Qt;
case GNUTLS_E_AGAIN:
return Qgnutls_e_again;
case GNUTLS_E_INTERRUPTED:
return Qgnutls_e_interrupted;
case GNUTLS_E_INVALID_SESSION:
return Qgnutls_e_invalid_session;
}
return make_number (error);
}
DEFUN ("gnutls-get-initstage", Fgnutls_get_initstage, Sgnutls_get_initstage, 1, 1, 0,
doc: /* Return the GnuTLS init stage of PROCESS.
See also `gnutls-boot'. */)
(Lisp_Object proc)
{
CHECK_PROCESS (proc);
return make_number (GNUTLS_INITSTAGE (proc));
}
DEFUN ("gnutls-errorp", Fgnutls_errorp, Sgnutls_errorp, 1, 1, 0,
doc: /* Returns t if ERROR (as generated by gnutls_make_error)
indicates a GnuTLS problem. */)
(Lisp_Object error)
{
if (EQ (error, Qt)) return Qnil;
return Qt;
}
DEFUN ("gnutls-error-fatalp", Fgnutls_error_fatalp, Sgnutls_error_fatalp, 1, 1, 0,
doc: /* Checks if ERROR is fatal.
ERROR is an integer or a symbol with an integer `gnutls-code' property. */)
(Lisp_Object err)
{
Lisp_Object code;
if (EQ (err, Qt)) return Qnil;
if (SYMBOLP (err))
{
code = Fget (err, Qgnutls_code);
if (NUMBERP (code))
{
err = code;
}
else
{
error ("Symbol has no numeric gnutls-code property");
}
}
if (!NUMBERP (err))
error ("Not an error symbol or code");
if (0 == gnutls_error_is_fatal (XINT (err)))
return Qnil;
return Qt;
}
DEFUN ("gnutls-error-string", Fgnutls_error_string, Sgnutls_error_string, 1, 1, 0,
doc: /* Returns a description of ERROR.
ERROR is an integer or a symbol with an integer `gnutls-code' property. */)
(Lisp_Object err)
{
Lisp_Object code;
if (EQ (err, Qt)) return build_string ("Not an error");
if (SYMBOLP (err))
{
code = Fget (err, Qgnutls_code);
if (NUMBERP (code))
{
err = code;
}
else
{
return build_string ("Symbol has no numeric gnutls-code property");
}
}
if (!NUMBERP (err))
return build_string ("Not an error symbol or code");
return build_string (gnutls_strerror (XINT (err)));
}
DEFUN ("gnutls-deinit", Fgnutls_deinit, Sgnutls_deinit, 1, 1, 0,
doc: /* Deallocate GNU TLS resources associated with PROCESS.
See also `gnutls-init'. */)
(Lisp_Object proc)
{
gnutls_session_t state;
CHECK_PROCESS (proc);
state = XPROCESS (proc)->gnutls_state;
if (GNUTLS_INITSTAGE (proc) >= GNUTLS_STAGE_INIT)
{
gnutls_deinit (state);
GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_INIT - 1;
}
return Qt;
}
/* Initializes global GNU TLS state to defaults.
Call `gnutls-global-deinit' when GNU TLS usage is no longer needed.
Returns zero on success. */
Lisp_Object gnutls_emacs_global_init (void)
{
int ret = GNUTLS_E_SUCCESS;
if (!global_initialized)
ret = gnutls_global_init ();
global_initialized = 1;
return gnutls_make_error (ret);
}
/* Deinitializes global GNU TLS state.
See also `gnutls-global-init'. */
Lisp_Object gnutls_emacs_global_deinit (void)
{
if (global_initialized)
gnutls_global_deinit ();
global_initialized = 0;
return gnutls_make_error (GNUTLS_E_SUCCESS);
}
DEFUN ("gnutls-boot", Fgnutls_boot, Sgnutls_boot, 3, 6, 0,
doc: /* Initializes client-mode GnuTLS for process PROC.
Currently only client mode is supported. Returns a success/failure
value you can check with `gnutls-errorp'.
PRIORITY_STRING is a string describing the priority.
TYPE is either `gnutls-anon' or `gnutls-x509pki'.
TRUSTFILE is a PEM encoded trust file for `gnutls-x509pki'.
KEYFILE is ... for `gnutls-x509pki' (TODO).
CALLBACK is ... for `gnutls-x509pki' (TODO).
Note that the priority is set on the client. The server does not use
the protocols's priority except for disabling protocols that were not
specified.
Processes must be initialized with this function before other GNU TLS
functions are used. This function allocates resources which can only
be deallocated by calling `gnutls-deinit' or by calling it again.
Each authentication type may need additional information in order to
work. For X.509 PKI (`gnutls-x509pki'), you need TRUSTFILE and
KEYFILE and optionally CALLBACK. */)
(Lisp_Object proc, Lisp_Object priority_string, Lisp_Object type,
Lisp_Object trustfile, Lisp_Object keyfile, Lisp_Object callback)
{
int ret = GNUTLS_E_SUCCESS;
/* TODO: GNUTLS_X509_FMT_DER is also an option. */
int file_format = GNUTLS_X509_FMT_PEM;
gnutls_session_t state;
gnutls_certificate_credentials_t x509_cred;
gnutls_anon_client_credentials_t anon_cred;
gnutls_srp_client_credentials_t srp_cred;
gnutls_datum_t data;
Lisp_Object global_init;
CHECK_PROCESS (proc);
CHECK_SYMBOL (type);
CHECK_STRING (priority_string);
state = XPROCESS (proc)->gnutls_state;
/* always initialize globals. */
global_init = gnutls_emacs_global_init ();
if (! NILP (Fgnutls_errorp (global_init)))
return global_init;
/* deinit and free resources. */
if (GNUTLS_INITSTAGE (proc) >= GNUTLS_STAGE_CRED_ALLOC)
{
message ("gnutls: deallocating certificates");
if (EQ (type, Qgnutls_x509pki))
{
message ("gnutls: deallocating x509 certificates");
x509_cred = XPROCESS (proc)->x509_cred;
gnutls_certificate_free_credentials (x509_cred);
}
else if (EQ (type, Qgnutls_anon))
{
message ("gnutls: deallocating anon certificates");
anon_cred = XPROCESS (proc)->anon_cred;
gnutls_anon_free_client_credentials (anon_cred);
}
else
{
error ("unknown credential type");
ret = GNUTLS_EMACS_ERROR_INVALID_TYPE;
}
if (GNUTLS_INITSTAGE (proc) >= GNUTLS_STAGE_INIT)
{
message ("gnutls: deinitializing");
Fgnutls_deinit (proc);
}
}
GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_EMPTY;
message ("gnutls: allocating credentials");
if (EQ (type, Qgnutls_x509pki))
{
message ("gnutls: allocating x509 credentials");
x509_cred = XPROCESS (proc)->x509_cred;
if (gnutls_certificate_allocate_credentials (&x509_cred) < 0)
memory_full ();
}
else if (EQ (type, Qgnutls_anon))
{
message ("gnutls: allocating anon credentials");
anon_cred = XPROCESS (proc)->anon_cred;
if (gnutls_anon_allocate_client_credentials (&anon_cred) < 0)
memory_full ();
}
else
{
error ("unknown credential type");
ret = GNUTLS_EMACS_ERROR_INVALID_TYPE;
}
if (ret < GNUTLS_E_SUCCESS)
return gnutls_make_error (ret);
GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_CRED_ALLOC;
message ("gnutls: setting the trustfile");
if (EQ (type, Qgnutls_x509pki))
{
if (STRINGP (trustfile))
{
ret = gnutls_certificate_set_x509_trust_file
(x509_cred,
XSTRING (trustfile)->data,
file_format);
if (ret < GNUTLS_E_SUCCESS)
return gnutls_make_error (ret);
message ("gnutls: processed %d CA certificates", ret);
}
message ("gnutls: setting the keyfile");
if (STRINGP (keyfile))
{
ret = gnutls_certificate_set_x509_crl_file
(x509_cred,
XSTRING (keyfile)->data,
file_format);
if (ret < GNUTLS_E_SUCCESS)
return gnutls_make_error (ret);
message ("gnutls: processed %d CRL(s)", ret);
}
}
GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_FILES;
message ("gnutls: gnutls_init");
ret = gnutls_init (&state, GNUTLS_CLIENT);
if (ret < GNUTLS_E_SUCCESS)
return gnutls_make_error (ret);
XPROCESS (proc)->gnutls_state = state;
GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_INIT;
message ("gnutls: setting the priority string");
ret = gnutls_priority_set_direct(state,
(char*) SDATA (priority_string),
NULL);
if (ret < GNUTLS_E_SUCCESS)
return gnutls_make_error (ret);
GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_PRIORITY;