gnutls.c 17.5 KB
Newer Older
Ted Zlatanov's avatar
Ted Zlatanov committed
1
/* GnuTLS glue for GNU Emacs.
2
   Copyright (C) 2010-2011  Free Software Foundation, Inc.
Ted Zlatanov's avatar
Ted Zlatanov committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

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;

35 36 37 38 39 40 41
/* The following are for the property list of `gnutls-boot'.  */
Lisp_Object Qgnutls_bootprop_priority;
Lisp_Object Qgnutls_bootprop_trustfiles;
Lisp_Object Qgnutls_bootprop_keyfiles;
Lisp_Object Qgnutls_bootprop_callbacks;
Lisp_Object Qgnutls_bootprop_loglevel;

42
static void
43 44 45 46 47 48 49 50 51
emacs_gnutls_handshake (struct Lisp_Process *proc)
{
  gnutls_session_t state = proc->gnutls_state;
  int ret;

  if (proc->gnutls_initstage < GNUTLS_STAGE_HANDSHAKE_CANDO)
    return;

  if (proc->gnutls_initstage < GNUTLS_STAGE_TRANSPORT_POINTERS_SET)
52
    {
53 54 55
      /* This is how GnuTLS takes sockets: as file descriptors passed
         in.  For an Emacs process socket, infd and outfd are the
         same but we use this two-argument version for clarity.  */
56 57 58
      gnutls_transport_set_ptr2 (state,
				 (gnutls_transport_ptr_t) (long) proc->infd,
				 (gnutls_transport_ptr_t) (long) proc->outfd);
59

60 61
      proc->gnutls_initstage = GNUTLS_STAGE_TRANSPORT_POINTERS_SET;
    }
62 63 64 65 66

  ret = gnutls_handshake (state);
  proc->gnutls_initstage = GNUTLS_STAGE_HANDSHAKE_TRIED;

  if (ret == GNUTLS_E_SUCCESS)
67 68 69 70
    {
      /* here we're finally done.  */
      proc->gnutls_initstage = GNUTLS_STAGE_READY;
    }
71 72
}

73
size_t
74 75
emacs_gnutls_write (int fildes, struct Lisp_Process *proc, const char *buf,
                    size_t nbyte)
Ted Zlatanov's avatar
Ted Zlatanov committed
76
{
77 78
  ssize_t rtnval;
  size_t bytes_written;
79 80
  gnutls_session_t state = proc->gnutls_state;

81 82 83 84 85 86 87
  if (proc->gnutls_initstage != GNUTLS_STAGE_READY) {
#ifdef EWOULDBLOCK
    errno = EWOULDBLOCK;
#endif
#ifdef EAGAIN
    errno = EAGAIN;
#endif
88
    return 0;
89
  }
Ted Zlatanov's avatar
Ted Zlatanov committed
90 91 92 93 94 95 96

  bytes_written = 0;

  while (nbyte > 0)
    {
      rtnval = gnutls_write (state, buf, nbyte);

97
      if (rtnval < 0)
Ted Zlatanov's avatar
Ted Zlatanov committed
98
        {
99
          if (rtnval == GNUTLS_E_AGAIN || rtnval == GNUTLS_E_INTERRUPTED)
Ted Zlatanov's avatar
Ted Zlatanov committed
100 101
            continue;
          else
102
            break;
Ted Zlatanov's avatar
Ted Zlatanov committed
103 104 105 106 107 108 109 110 111 112
        }

      buf += rtnval;
      nbyte -= rtnval;
      bytes_written += rtnval;
    }

  return (bytes_written);
}

113
ssize_t
114
emacs_gnutls_read (int fildes, struct Lisp_Process *proc, char *buf,
115
                   size_t nbyte)
Ted Zlatanov's avatar
Ted Zlatanov committed
116
{
117
  ssize_t rtnval;
118 119
  gnutls_session_t state = proc->gnutls_state;

120 121 122 123 124
  if (proc->gnutls_initstage != GNUTLS_STAGE_READY)
    {
      emacs_gnutls_handshake (proc);
      return -1;
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
125

126 127 128
  rtnval = gnutls_read (state, buf, nbyte);
  if (rtnval >= 0)
    return rtnval;
129 130 131 132 133 134 135
  else {
    if (rtnval == GNUTLS_E_AGAIN ||
	rtnval == GNUTLS_E_INTERRUPTED)
      return -1;
    else
      return 0;
  }
Ted Zlatanov's avatar
Ted Zlatanov committed
136 137 138 139 140 141
}

/* 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.  */
142 143
static Lisp_Object
gnutls_make_error (int error)
Ted Zlatanov's avatar
Ted Zlatanov committed
144 145
{
  switch (error)
146 147 148 149 150 151 152 153 154 155
    {
    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;
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
156 157 158 159 160

  return make_number (error);
}

DEFUN ("gnutls-get-initstage", Fgnutls_get_initstage, Sgnutls_get_initstage, 1, 1, 0,
161
       doc: /* Return the GnuTLS init stage of process PROC.
Ted Zlatanov's avatar
Ted Zlatanov committed
162
See also `gnutls-boot'.  */)
163
  (Lisp_Object proc)
Ted Zlatanov's avatar
Ted Zlatanov committed
164 165 166 167 168 169 170
{
  CHECK_PROCESS (proc);

  return make_number (GNUTLS_INITSTAGE (proc));
}

DEFUN ("gnutls-errorp", Fgnutls_errorp, Sgnutls_errorp, 1, 1, 0,
171 172 173 174
       doc: /* Return t if ERROR indicates a GnuTLS problem.
ERROR is an integer or a symbol with an integer `gnutls-code' property.
usage: (gnutls-errorp ERROR)  */)
  (Lisp_Object err)
Ted Zlatanov's avatar
Ted Zlatanov committed
175
{
176
  if (EQ (err, Qt)) return Qnil;
Ted Zlatanov's avatar
Ted Zlatanov committed
177 178 179 180 181

  return Qt;
}

DEFUN ("gnutls-error-fatalp", Fgnutls_error_fatalp, Sgnutls_error_fatalp, 1, 1, 0,
182 183 184 185
       doc: /* Check if ERROR is fatal.
ERROR is an integer or a symbol with an integer `gnutls-code' property.
usage: (gnutls-error-fatalp ERROR)  */)
  (Lisp_Object err)
Ted Zlatanov's avatar
Ted Zlatanov committed
186 187 188 189 190 191 192
{
  Lisp_Object code;

  if (EQ (err, Qt)) return Qnil;

  if (SYMBOLP (err))
    {
193 194 195 196 197 198 199 200 201
      code = Fget (err, Qgnutls_code);
      if (NUMBERP (code))
	{
	  err = code;
	}
      else
	{
	  error ("Symbol has no numeric gnutls-code property");
	}
Ted Zlatanov's avatar
Ted Zlatanov committed
202 203 204 205 206 207 208 209 210 211 212 213
    }

  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,
214 215 216 217
       doc: /* Return a description of ERROR.
ERROR is an integer or a symbol with an integer `gnutls-code' property.
usage: (gnutls-error-string ERROR)  */)
  (Lisp_Object err)
Ted Zlatanov's avatar
Ted Zlatanov committed
218 219 220 221 222 223 224
{
  Lisp_Object code;

  if (EQ (err, Qt)) return build_string ("Not an error");

  if (SYMBOLP (err))
    {
225 226 227 228 229 230 231 232 233
      code = Fget (err, Qgnutls_code);
      if (NUMBERP (code))
	{
	  err = code;
	}
      else
	{
	  return build_string ("Symbol has no numeric gnutls-code property");
	}
Ted Zlatanov's avatar
Ted Zlatanov committed
234 235 236 237 238 239 240 241 242
    }

  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,
243
       doc: /* Deallocate GnuTLS resources associated with process PROC.
Ted Zlatanov's avatar
Ted Zlatanov committed
244
See also `gnutls-init'.  */)
245
  (Lisp_Object proc)
Ted Zlatanov's avatar
Ted Zlatanov committed
246 247 248 249 250 251 252
{
  gnutls_session_t state;

  CHECK_PROCESS (proc);
  state = XPROCESS (proc)->gnutls_state;

  if (GNUTLS_INITSTAGE (proc) >= GNUTLS_STAGE_INIT)
253
    {
Ted Zlatanov's avatar
Ted Zlatanov committed
254 255
      gnutls_deinit (state);
      GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_INIT - 1;
256
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
257 258 259 260

  return Qt;
}

261 262
/* Initializes global GnuTLS state to defaults.
Call `gnutls-global-deinit' when GnuTLS usage is no longer needed.
Ted Zlatanov's avatar
Ted Zlatanov committed
263
Returns zero on success.  */
264 265
static Lisp_Object
gnutls_emacs_global_init (void)
Ted Zlatanov's avatar
Ted Zlatanov committed
266 267 268 269 270 271 272 273 274 275 276
{
  int ret = GNUTLS_E_SUCCESS;

  if (!global_initialized)
    ret = gnutls_global_init ();

  global_initialized = 1;

  return gnutls_make_error (ret);
}

277
/* Deinitializes global GnuTLS state.
Ted Zlatanov's avatar
Ted Zlatanov committed
278
See also `gnutls-global-init'.  */
279 280
static Lisp_Object
gnutls_emacs_global_deinit (void)
Ted Zlatanov's avatar
Ted Zlatanov committed
281 282 283 284 285 286 287 288 289
{
  if (global_initialized)
    gnutls_global_deinit ();

  global_initialized = 0;

  return gnutls_make_error (GNUTLS_E_SUCCESS);
}

290 291
static void
gnutls_log_function (int level, const char* string)
292
{
293
  message ("gnutls.c: [%d] %s", level, string);
294 295
}

296 297 298 299 300 301 302 303
static void
gnutls_log_function2 (int level, const char* string, const char* extra)
{
  message ("gnutls.c: [%d] %s %s", level, string, extra);
}

DEFUN ("gnutls-boot", Fgnutls_boot, Sgnutls_boot, 3, 3, 0,
       doc: /* Initialize GnuTLS client for process PROC with TYPE+PROPLIST.
Ted Zlatanov's avatar
Ted Zlatanov committed
304 305 306
Currently only client mode is supported.  Returns a success/failure
value you can check with `gnutls-errorp'.

307 308 309 310 311 312 313 314
TYPE is a symbol, either `gnutls-anon' or `gnutls-x509pki'.
PROPLIST is a property list with the following keys:

:priority is a GnuTLS priority string, defaults to "NORMAL".
:trustfiles is a list of PEM-encoded trust files for `gnutls-x509pki'.
:keyfiles is a list of PEM-encoded key files for `gnutls-x509pki'.
:callbacks is an alist of callback functions (TODO).
:loglevel is the debug level requested from GnuTLS, try 4.
315

316 317 318
The debug level will be set for this process AND globally for GnuTLS.
So if you set it higher or lower at any point, it affects global
debugging.
Ted Zlatanov's avatar
Ted Zlatanov committed
319 320 321 322 323

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.

324
Processes must be initialized with this function before other GnuTLS
Ted Zlatanov's avatar
Ted Zlatanov committed
325 326 327 328
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
329 330 331
work.  For X.509 PKI (`gnutls-x509pki'), you probably need at least
one trustfile (usually a CA bundle).  */)
  (Lisp_Object proc, Lisp_Object type, Lisp_Object proplist)
Ted Zlatanov's avatar
Ted Zlatanov committed
332 333 334
{
  int ret = GNUTLS_E_SUCCESS;

335 336
  int max_log_level = 0;

Ted Zlatanov's avatar
Ted Zlatanov committed
337 338 339 340 341 342 343
  /* 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;
  Lisp_Object global_init;
344 345 346 347 348 349 350 351 352
  char* priority_string_ptr = "NORMAL"; /* default priority string.  */
  Lisp_Object tail;

  /* Placeholders for the property list elements.  */
  Lisp_Object priority_string;
  Lisp_Object trustfiles;
  Lisp_Object keyfiles;
  Lisp_Object callbacks;
  Lisp_Object loglevel;
Ted Zlatanov's avatar
Ted Zlatanov committed
353 354 355

  CHECK_PROCESS (proc);
  CHECK_SYMBOL (type);
356 357 358 359 360 361 362
  CHECK_LIST (proplist);

  priority_string = Fplist_get (proplist, Qgnutls_bootprop_priority);
  trustfiles      = Fplist_get (proplist, Qgnutls_bootprop_trustfiles);
  keyfiles        = Fplist_get (proplist, Qgnutls_bootprop_keyfiles);
  callbacks       = Fplist_get (proplist, Qgnutls_bootprop_callbacks);
  loglevel        = Fplist_get (proplist, Qgnutls_bootprop_loglevel);
Ted Zlatanov's avatar
Ted Zlatanov committed
363 364

  state = XPROCESS (proc)->gnutls_state;
365
  XPROCESS (proc)->gnutls_p = 1;
Ted Zlatanov's avatar
Ted Zlatanov committed
366

367 368 369 370 371 372 373
  if (NUMBERP (loglevel))
    {
      gnutls_global_set_log_function (gnutls_log_function);
      gnutls_global_set_log_level (XINT (loglevel));
      max_log_level = XINT (loglevel);
      XPROCESS (proc)->gnutls_log_level = max_log_level;
    }
374

Ted Zlatanov's avatar
Ted Zlatanov committed
375 376 377 378 379 380 381
  /* 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)
382
    {
383 384
      GNUTLS_LOG (1, max_log_level, "deallocating credentials");

Ted Zlatanov's avatar
Ted Zlatanov committed
385
      if (EQ (type, Qgnutls_x509pki))
386
	{
387 388
          GNUTLS_LOG (2, max_log_level, "deallocating x509 credentials");
          x509_cred = XPROCESS (proc)->gnutls_x509_cred;
Ted Zlatanov's avatar
Ted Zlatanov committed
389
          gnutls_certificate_free_credentials (x509_cred);
390
	}
Ted Zlatanov's avatar
Ted Zlatanov committed
391
      else if (EQ (type, Qgnutls_anon))
392
	{
393 394
          GNUTLS_LOG (2, max_log_level, "deallocating anon credentials");
          anon_cred = XPROCESS (proc)->gnutls_anon_cred;
Ted Zlatanov's avatar
Ted Zlatanov committed
395
          gnutls_anon_free_client_credentials (anon_cred);
396
	}
Ted Zlatanov's avatar
Ted Zlatanov committed
397
      else
398
	{
Ted Zlatanov's avatar
Ted Zlatanov committed
399 400
          error ("unknown credential type");
          ret = GNUTLS_EMACS_ERROR_INVALID_TYPE;
401
	}
Ted Zlatanov's avatar
Ted Zlatanov committed
402 403

      if (GNUTLS_INITSTAGE (proc) >= GNUTLS_STAGE_INIT)
404
	{
405
          GNUTLS_LOG (1, max_log_level, "deallocating x509 credentials");
Ted Zlatanov's avatar
Ted Zlatanov committed
406
          Fgnutls_deinit (proc);
407 408
	}
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
409 410 411

  GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_EMPTY;

412 413
  GNUTLS_LOG (1, max_log_level, "allocating credentials");

Ted Zlatanov's avatar
Ted Zlatanov committed
414
  if (EQ (type, Qgnutls_x509pki))
415
    {
416 417
      GNUTLS_LOG (2, max_log_level, "allocating x509 credentials");
      x509_cred = XPROCESS (proc)->gnutls_x509_cred;
Ted Zlatanov's avatar
Ted Zlatanov committed
418 419
      if (gnutls_certificate_allocate_credentials (&x509_cred) < 0)
        memory_full ();
420
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
421
  else if (EQ (type, Qgnutls_anon))
422
    {
423 424
      GNUTLS_LOG (2, max_log_level, "allocating anon credentials");
      anon_cred = XPROCESS (proc)->gnutls_anon_cred;
Ted Zlatanov's avatar
Ted Zlatanov committed
425 426
      if (gnutls_anon_allocate_client_credentials (&anon_cred) < 0)
        memory_full ();
427
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
428
  else
429
    {
Ted Zlatanov's avatar
Ted Zlatanov committed
430 431
      error ("unknown credential type");
      ret = GNUTLS_EMACS_ERROR_INVALID_TYPE;
432
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
433 434

  if (ret < GNUTLS_E_SUCCESS)
435
    return gnutls_make_error (ret);
Ted Zlatanov's avatar
Ted Zlatanov committed
436 437 438 439

  GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_CRED_ALLOC;

  if (EQ (type, Qgnutls_x509pki))
440
    {
441
      for (tail = trustfiles; !NILP (tail); tail = Fcdr (tail))
442
	{
443 444 445 446 447 448 449 450 451
	  Lisp_Object trustfile = Fcar (tail);
          if (STRINGP (trustfile))
            {
              GNUTLS_LOG2 (1, max_log_level, "setting the trustfile: ",
                           SDATA (trustfile));
              ret = gnutls_certificate_set_x509_trust_file
                (x509_cred,
                 SDATA (trustfile),
                 file_format);
452

453 454 455 456 457 458 459 460 461
              if (ret < GNUTLS_E_SUCCESS)
                return gnutls_make_error (ret);
            }
          else
            {
              error ("Sorry, GnuTLS can't use non-string trustfile %s",
                     trustfile);
            }
        }
Ted Zlatanov's avatar
Ted Zlatanov committed
462

463
      for (tail = keyfiles; !NILP (tail); tail = Fcdr (tail))
464
	{
465 466 467 468 469 470 471 472 473
	  Lisp_Object keyfile = Fcar (tail);
          if (STRINGP (keyfile))
            {
              GNUTLS_LOG2 (1, max_log_level, "setting the keyfile: ",
                           SDATA (keyfile));
              ret = gnutls_certificate_set_x509_crl_file
                (x509_cred,
                 SDATA (keyfile),
                 file_format);
474

475 476 477 478 479 480 481 482 483
              if (ret < GNUTLS_E_SUCCESS)
                return gnutls_make_error (ret);
            }
          else
            {
              error ("Sorry, GnuTLS can't use non-string keyfile %s",
                     keyfile);
            }
        }
484
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
485 486 487

  GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_FILES;

488 489
  GNUTLS_LOG (1, max_log_level, "gnutls_init");

Ted Zlatanov's avatar
Ted Zlatanov committed
490 491 492
  ret = gnutls_init (&state, GNUTLS_CLIENT);

  if (ret < GNUTLS_E_SUCCESS)
493
    return gnutls_make_error (ret);
Ted Zlatanov's avatar
Ted Zlatanov committed
494 495 496 497 498

  XPROCESS (proc)->gnutls_state = state;

  GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_INIT;

499 500
  if (STRINGP (priority_string))
    {
501
      priority_string_ptr = SSDATA (priority_string);
502 503 504 505 506 507 508 509
      GNUTLS_LOG2 (1, max_log_level, "got non-default priority string:",
                   priority_string_ptr);
    }
  else
    {
      GNUTLS_LOG2 (1, max_log_level, "using default priority string:",
                   priority_string_ptr);
    }
510

511 512
  GNUTLS_LOG (1, max_log_level, "setting the priority string");

513
  ret = gnutls_priority_set_direct (state,
514
				    priority_string_ptr,
515
				    NULL);
Ted Zlatanov's avatar
Ted Zlatanov committed
516 517

  if (ret < GNUTLS_E_SUCCESS)
518
    return gnutls_make_error (ret);
Ted Zlatanov's avatar
Ted Zlatanov committed
519 520 521 522

  GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_PRIORITY;

  if (EQ (type, Qgnutls_x509pki))
523
    {
Ted Zlatanov's avatar
Ted Zlatanov committed
524
      ret = gnutls_cred_set (state, GNUTLS_CRD_CERTIFICATE, x509_cred);
525
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
526
  else if (EQ (type, Qgnutls_anon))
527
    {
Ted Zlatanov's avatar
Ted Zlatanov committed
528
      ret = gnutls_cred_set (state, GNUTLS_CRD_ANON, anon_cred);
529
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
530
  else
531
    {
Ted Zlatanov's avatar
Ted Zlatanov committed
532 533
      error ("unknown credential type");
      ret = GNUTLS_EMACS_ERROR_INVALID_TYPE;
534
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
535 536

  if (ret < GNUTLS_E_SUCCESS)
537
    return gnutls_make_error (ret);
Ted Zlatanov's avatar
Ted Zlatanov committed
538

539 540
  XPROCESS (proc)->gnutls_anon_cred = anon_cred;
  XPROCESS (proc)->gnutls_x509_cred = x509_cred;
Ted Zlatanov's avatar
Ted Zlatanov committed
541 542 543 544
  XPROCESS (proc)->gnutls_cred_type = type;

  GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_CRED_SET;

545 546
  emacs_gnutls_handshake (XPROCESS (proc));

Ted Zlatanov's avatar
Ted Zlatanov committed
547 548 549 550 551
  return gnutls_make_error (GNUTLS_E_SUCCESS);
}

DEFUN ("gnutls-bye", Fgnutls_bye,
       Sgnutls_bye, 2, 2, 0,
552
       doc: /* Terminate current GnuTLS connection for process PROC.
Ted Zlatanov's avatar
Ted Zlatanov committed
553 554 555
The connection should have been initiated using `gnutls-handshake'.

If CONT is not nil the TLS connection gets terminated and further
556
receives and sends will be disallowed.  If the return value is zero you
Ted Zlatanov's avatar
Ted Zlatanov committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
may continue using the connection.  If CONT is nil, GnuTLS actually
sends an alert containing a close request and waits for the peer to
reply with the same message.  In order to reuse the connection you
should wait for an EOF from the peer.

This function may also return `gnutls-e-again', or
`gnutls-e-interrupted'.  */)
    (Lisp_Object proc, Lisp_Object cont)
{
  gnutls_session_t state;
  int ret;

  CHECK_PROCESS (proc);

  state = XPROCESS (proc)->gnutls_state;

  ret = gnutls_bye (state,
                    NILP (cont) ? GNUTLS_SHUT_RDWR : GNUTLS_SHUT_WR);

  return gnutls_make_error (ret);
}

void
syms_of_gnutls (void)
{
  global_initialized = 0;

  Qgnutls_code = intern_c_string ("gnutls-code");
  staticpro (&Qgnutls_code);

  Qgnutls_anon = intern_c_string ("gnutls-anon");
  staticpro (&Qgnutls_anon);

  Qgnutls_x509pki = intern_c_string ("gnutls-x509pki");
  staticpro (&Qgnutls_x509pki);

593
  Qgnutls_bootprop_priority = intern_c_string (":priority");
594 595
  staticpro (&Qgnutls_bootprop_priority);

596
  Qgnutls_bootprop_trustfiles = intern_c_string (":trustfiles");
597 598
  staticpro (&Qgnutls_bootprop_trustfiles);

599
  Qgnutls_bootprop_keyfiles = intern_c_string (":keyfiles");
600 601
  staticpro (&Qgnutls_bootprop_keyfiles);

602
  Qgnutls_bootprop_callbacks = intern_c_string (":callbacks");
603 604
  staticpro (&Qgnutls_bootprop_callbacks);

605
  Qgnutls_bootprop_loglevel = intern_c_string (":loglevel");
606 607
  staticpro (&Qgnutls_bootprop_loglevel);

Ted Zlatanov's avatar
Ted Zlatanov committed
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
  Qgnutls_e_interrupted = intern_c_string ("gnutls-e-interrupted");
  staticpro (&Qgnutls_e_interrupted);
  Fput (Qgnutls_e_interrupted, Qgnutls_code,
        make_number (GNUTLS_E_INTERRUPTED));

  Qgnutls_e_again = intern_c_string ("gnutls-e-again");
  staticpro (&Qgnutls_e_again);
  Fput (Qgnutls_e_again, Qgnutls_code,
        make_number (GNUTLS_E_AGAIN));

  Qgnutls_e_invalid_session = intern_c_string ("gnutls-e-invalid-session");
  staticpro (&Qgnutls_e_invalid_session);
  Fput (Qgnutls_e_invalid_session, Qgnutls_code,
        make_number (GNUTLS_E_INVALID_SESSION));

  Qgnutls_e_not_ready_for_handshake =
    intern_c_string ("gnutls-e-not-ready-for-handshake");
  staticpro (&Qgnutls_e_not_ready_for_handshake);
  Fput (Qgnutls_e_not_ready_for_handshake, Qgnutls_code,
        make_number (GNUTLS_E_APPLICATION_ERROR_MIN));

  defsubr (&Sgnutls_get_initstage);
  defsubr (&Sgnutls_errorp);
  defsubr (&Sgnutls_error_fatalp);
  defsubr (&Sgnutls_error_string);
  defsubr (&Sgnutls_boot);
  defsubr (&Sgnutls_deinit);
  defsubr (&Sgnutls_bye);
}
#endif