gnutls.c 56.4 KB
Newer Older
Ted Zlatanov's avatar
Ted Zlatanov committed
1
/* GnuTLS glue for GNU Emacs.
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 2010-2017 Free Software Foundation, Inc.
Ted Zlatanov's avatar
Ted Zlatanov committed
3 4 5 6 7

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
8 9
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Ted Zlatanov's avatar
Ted Zlatanov committed
10 11 12 13 14 15 16 17 18 19 20

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>
21
#include <stdio.h>
Ted Zlatanov's avatar
Ted Zlatanov committed
22 23 24

#include "lisp.h"
#include "process.h"
Paul Eggert's avatar
Paul Eggert committed
25
#include "gnutls.h"
26
#include "coding.h"
Ted Zlatanov's avatar
Ted Zlatanov committed
27 28 29

#ifdef HAVE_GNUTLS

30 31 32 33 34
#ifdef WINDOWSNT
#include <windows.h>
#include "w32.h"
#endif

35
static bool emacs_gnutls_handle_error (gnutls_session_t, int);
36

37
static bool gnutls_global_initialized;
Ted Zlatanov's avatar
Ted Zlatanov committed
38

39
static void gnutls_log_function (int, const char *);
40
static void gnutls_log_function2 (int, const char *, const char *);
41 42 43
#ifdef HAVE_GNUTLS3
static void gnutls_audit_log_function (gnutls_session_t, const char *);
#endif
44

45 46 47 48
enum extra_peer_verification
{
    CERTIFICATE_NOT_MATCHING = 2
};
49

50 51 52

#ifdef WINDOWSNT

Paul Eggert's avatar
Paul Eggert committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
DEF_DLL_FN (gnutls_alert_description_t, gnutls_alert_get,
	    (gnutls_session_t));
DEF_DLL_FN (const char *, gnutls_alert_get_name,
	    (gnutls_alert_description_t));
DEF_DLL_FN (int, gnutls_anon_allocate_client_credentials,
	    (gnutls_anon_client_credentials_t *));
DEF_DLL_FN (void, gnutls_anon_free_client_credentials,
	    (gnutls_anon_client_credentials_t));
DEF_DLL_FN (int, gnutls_bye, (gnutls_session_t, gnutls_close_request_t));
DEF_DLL_FN (int, gnutls_certificate_allocate_credentials,
	    (gnutls_certificate_credentials_t *));
DEF_DLL_FN (void, gnutls_certificate_free_credentials,
	    (gnutls_certificate_credentials_t));
DEF_DLL_FN (const gnutls_datum_t *, gnutls_certificate_get_peers,
	    (gnutls_session_t, unsigned int *));
DEF_DLL_FN (void, gnutls_certificate_set_verify_flags,
	    (gnutls_certificate_credentials_t, unsigned int));
DEF_DLL_FN (int, gnutls_certificate_set_x509_crl_file,
	    (gnutls_certificate_credentials_t, const char *,
	     gnutls_x509_crt_fmt_t));
DEF_DLL_FN (int, gnutls_certificate_set_x509_key_file,
	    (gnutls_certificate_credentials_t, const char *, const char *,
	     gnutls_x509_crt_fmt_t));
# if ((GNUTLS_VERSION_MAJOR						\
       + (GNUTLS_VERSION_MINOR > 0 || GNUTLS_VERSION_PATCH >= 20))	\
      > 3)
DEF_DLL_FN (int, gnutls_certificate_set_x509_system_trust,
	    (gnutls_certificate_credentials_t));
# endif
DEF_DLL_FN (int, gnutls_certificate_set_x509_trust_file,
	    (gnutls_certificate_credentials_t, const char *,
	     gnutls_x509_crt_fmt_t));
DEF_DLL_FN (gnutls_certificate_type_t, gnutls_certificate_type_get,
	    (gnutls_session_t));
DEF_DLL_FN (int, gnutls_certificate_verify_peers2,
	    (gnutls_session_t, unsigned int *));
DEF_DLL_FN (int, gnutls_credentials_set,
	    (gnutls_session_t, gnutls_credentials_type_t, void *));
DEF_DLL_FN (void, gnutls_deinit, (gnutls_session_t));
DEF_DLL_FN (void, gnutls_dh_set_prime_bits,
	    (gnutls_session_t, unsigned int));
DEF_DLL_FN (int, gnutls_dh_get_prime_bits, (gnutls_session_t));
DEF_DLL_FN (int, gnutls_error_is_fatal, (int));
DEF_DLL_FN (int, gnutls_global_init, (void));
DEF_DLL_FN (void, gnutls_global_set_log_function, (gnutls_log_func));
# ifdef HAVE_GNUTLS3
DEF_DLL_FN (void, gnutls_global_set_audit_log_function, (gnutls_audit_log_func));
# endif
DEF_DLL_FN (void, gnutls_global_set_log_level, (int));
DEF_DLL_FN (int, gnutls_handshake, (gnutls_session_t));
DEF_DLL_FN (int, gnutls_init, (gnutls_session_t *, unsigned int));
DEF_DLL_FN (int, gnutls_priority_set_direct,
	    (gnutls_session_t, const char *, const char **));
DEF_DLL_FN (size_t, gnutls_record_check_pending, (gnutls_session_t));
DEF_DLL_FN (ssize_t, gnutls_record_recv, (gnutls_session_t, void *, size_t));
DEF_DLL_FN (ssize_t, gnutls_record_send,
	    (gnutls_session_t, const void *, size_t));
DEF_DLL_FN (const char *, gnutls_strerror, (int));
DEF_DLL_FN (void, gnutls_transport_set_errno, (gnutls_session_t, int));
DEF_DLL_FN (void, gnutls_transport_set_ptr2,
	    (gnutls_session_t, gnutls_transport_ptr_t,
	     gnutls_transport_ptr_t));
DEF_DLL_FN (void, gnutls_transport_set_pull_function,
	    (gnutls_session_t, gnutls_pull_func));
DEF_DLL_FN (void, gnutls_transport_set_push_function,
	    (gnutls_session_t, gnutls_push_func));
DEF_DLL_FN (int, gnutls_x509_crt_check_hostname,
	    (gnutls_x509_crt_t, const char *));
121 122
DEF_DLL_FN (int, gnutls_x509_crt_check_issuer,
              (gnutls_x509_crt_t, gnutls_x509_crt_t));
Paul Eggert's avatar
Paul Eggert committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
DEF_DLL_FN (void, gnutls_x509_crt_deinit, (gnutls_x509_crt_t));
DEF_DLL_FN (int, gnutls_x509_crt_import,
	    (gnutls_x509_crt_t, const gnutls_datum_t *,
	     gnutls_x509_crt_fmt_t));
DEF_DLL_FN (int, gnutls_x509_crt_init, (gnutls_x509_crt_t *));
DEF_DLL_FN (int, gnutls_x509_crt_get_fingerprint,
	    (gnutls_x509_crt_t,
	     gnutls_digest_algorithm_t, void *, size_t *));
DEF_DLL_FN (int, gnutls_x509_crt_get_version,
	    (gnutls_x509_crt_t));
DEF_DLL_FN (int, gnutls_x509_crt_get_serial,
	    (gnutls_x509_crt_t, void *, size_t *));
DEF_DLL_FN (int, gnutls_x509_crt_get_issuer_dn,
	    (gnutls_x509_crt_t, char *, size_t *));
DEF_DLL_FN (time_t, gnutls_x509_crt_get_activation_time,
	    (gnutls_x509_crt_t));
DEF_DLL_FN (time_t, gnutls_x509_crt_get_expiration_time,
	    (gnutls_x509_crt_t));
DEF_DLL_FN (int, gnutls_x509_crt_get_dn,
	    (gnutls_x509_crt_t, char *, size_t *));
DEF_DLL_FN (int, gnutls_x509_crt_get_pk_algorithm,
	    (gnutls_x509_crt_t, unsigned int *));
145
DEF_DLL_FN (const char *, gnutls_pk_algorithm_get_name,
Paul Eggert's avatar
Paul Eggert committed
146 147 148 149 150 151 152 153 154 155 156
	    (gnutls_pk_algorithm_t));
DEF_DLL_FN (int, gnutls_pk_bits_to_sec_param,
	    (gnutls_pk_algorithm_t, unsigned int));
DEF_DLL_FN (int, gnutls_x509_crt_get_issuer_unique_id,
	    (gnutls_x509_crt_t, char *, size_t *));
DEF_DLL_FN (int, gnutls_x509_crt_get_subject_unique_id,
	    (gnutls_x509_crt_t, char *, size_t *));
DEF_DLL_FN (int, gnutls_x509_crt_get_signature_algorithm,
	    (gnutls_x509_crt_t));
DEF_DLL_FN (int, gnutls_x509_crt_get_key_id,
	    (gnutls_x509_crt_t, unsigned int, unsigned char *, size_t *_size));
157 158
DEF_DLL_FN (const char *, gnutls_sec_param_get_name, (gnutls_sec_param_t));
DEF_DLL_FN (const char *, gnutls_sign_get_name, (gnutls_sign_algorithm_t));
Paul Eggert's avatar
Paul Eggert committed
159 160 161 162
DEF_DLL_FN (int, gnutls_server_name_set,
	    (gnutls_session_t, gnutls_server_name_type_t,
	     const void *, size_t));
DEF_DLL_FN (gnutls_kx_algorithm_t, gnutls_kx_get, (gnutls_session_t));
163
DEF_DLL_FN (const char *, gnutls_kx_get_name, (gnutls_kx_algorithm_t));
Paul Eggert's avatar
Paul Eggert committed
164 165
DEF_DLL_FN (gnutls_protocol_t, gnutls_protocol_get_version,
	    (gnutls_session_t));
166
DEF_DLL_FN (const char *, gnutls_protocol_get_name, (gnutls_protocol_t));
Paul Eggert's avatar
Paul Eggert committed
167 168
DEF_DLL_FN (gnutls_cipher_algorithm_t, gnutls_cipher_get,
	    (gnutls_session_t));
169
DEF_DLL_FN (const char *, gnutls_cipher_get_name,
Paul Eggert's avatar
Paul Eggert committed
170 171
	    (gnutls_cipher_algorithm_t));
DEF_DLL_FN (gnutls_mac_algorithm_t, gnutls_mac_get, (gnutls_session_t));
172
DEF_DLL_FN (const char *, gnutls_mac_get_name, (gnutls_mac_algorithm_t));
173

174

175
static bool
176
init_gnutls_functions (void)
177 178
{
  HMODULE library;
179
  int max_log_level = 1;
180

181
  if (!(library = w32_delayed_load (Qgnutls)))
182
    {
183
      GNUTLS_LOG (1, max_log_level, "GnuTLS library not found");
184 185 186
      return 0;
    }

Paul Eggert's avatar
Paul Eggert committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  LOAD_DLL_FN (library, gnutls_alert_get);
  LOAD_DLL_FN (library, gnutls_alert_get_name);
  LOAD_DLL_FN (library, gnutls_anon_allocate_client_credentials);
  LOAD_DLL_FN (library, gnutls_anon_free_client_credentials);
  LOAD_DLL_FN (library, gnutls_bye);
  LOAD_DLL_FN (library, gnutls_certificate_allocate_credentials);
  LOAD_DLL_FN (library, gnutls_certificate_free_credentials);
  LOAD_DLL_FN (library, gnutls_certificate_get_peers);
  LOAD_DLL_FN (library, gnutls_certificate_set_verify_flags);
  LOAD_DLL_FN (library, gnutls_certificate_set_x509_crl_file);
  LOAD_DLL_FN (library, gnutls_certificate_set_x509_key_file);
# if ((GNUTLS_VERSION_MAJOR						\
       + (GNUTLS_VERSION_MINOR > 0 || GNUTLS_VERSION_PATCH >= 20))	\
      > 3)
  LOAD_DLL_FN (library, gnutls_certificate_set_x509_system_trust);
# endif
  LOAD_DLL_FN (library, gnutls_certificate_set_x509_trust_file);
  LOAD_DLL_FN (library, gnutls_certificate_type_get);
  LOAD_DLL_FN (library, gnutls_certificate_verify_peers2);
  LOAD_DLL_FN (library, gnutls_credentials_set);
  LOAD_DLL_FN (library, gnutls_deinit);
  LOAD_DLL_FN (library, gnutls_dh_set_prime_bits);
  LOAD_DLL_FN (library, gnutls_dh_get_prime_bits);
  LOAD_DLL_FN (library, gnutls_error_is_fatal);
  LOAD_DLL_FN (library, gnutls_global_init);
  LOAD_DLL_FN (library, gnutls_global_set_log_function);
# ifdef HAVE_GNUTLS3
  LOAD_DLL_FN (library, gnutls_global_set_audit_log_function);
# endif
  LOAD_DLL_FN (library, gnutls_global_set_log_level);
  LOAD_DLL_FN (library, gnutls_handshake);
  LOAD_DLL_FN (library, gnutls_init);
  LOAD_DLL_FN (library, gnutls_priority_set_direct);
  LOAD_DLL_FN (library, gnutls_record_check_pending);
  LOAD_DLL_FN (library, gnutls_record_recv);
  LOAD_DLL_FN (library, gnutls_record_send);
  LOAD_DLL_FN (library, gnutls_strerror);
  LOAD_DLL_FN (library, gnutls_transport_set_errno);
  LOAD_DLL_FN (library, gnutls_transport_set_ptr2);
  LOAD_DLL_FN (library, gnutls_transport_set_pull_function);
  LOAD_DLL_FN (library, gnutls_transport_set_push_function);
  LOAD_DLL_FN (library, gnutls_x509_crt_check_hostname);
229
  LOAD_DLL_FN (library, gnutls_x509_crt_check_issuer);
Paul Eggert's avatar
Paul Eggert committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  LOAD_DLL_FN (library, gnutls_x509_crt_deinit);
  LOAD_DLL_FN (library, gnutls_x509_crt_import);
  LOAD_DLL_FN (library, gnutls_x509_crt_init);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_fingerprint);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_version);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_serial);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_issuer_dn);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_activation_time);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_expiration_time);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_dn);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_pk_algorithm);
  LOAD_DLL_FN (library, gnutls_pk_algorithm_get_name);
  LOAD_DLL_FN (library, gnutls_pk_bits_to_sec_param);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_issuer_unique_id);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_subject_unique_id);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_signature_algorithm);
  LOAD_DLL_FN (library, gnutls_x509_crt_get_key_id);
  LOAD_DLL_FN (library, gnutls_sec_param_get_name);
  LOAD_DLL_FN (library, gnutls_sign_get_name);
  LOAD_DLL_FN (library, gnutls_server_name_set);
  LOAD_DLL_FN (library, gnutls_kx_get);
  LOAD_DLL_FN (library, gnutls_kx_get_name);
  LOAD_DLL_FN (library, gnutls_protocol_get_version);
  LOAD_DLL_FN (library, gnutls_protocol_get_name);
  LOAD_DLL_FN (library, gnutls_cipher_get);
  LOAD_DLL_FN (library, gnutls_cipher_get_name);
  LOAD_DLL_FN (library, gnutls_mac_get);
  LOAD_DLL_FN (library, gnutls_mac_get_name);
258

259
  max_log_level = global_gnutls_log_level;
260

261
  {
262
    Lisp_Object name = CAR_SAFE (Fget (Qgnutls, QCloaded_from));
263 264 265 266
    GNUTLS_LOG2 (1, max_log_level, "GnuTLS library loaded:",
                 STRINGP (name) ? (const char *) SDATA (name) : "unknown");
  }

267 268 269
  return 1;
}

Paul Eggert's avatar
Paul Eggert committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
# define gnutls_alert_get fn_gnutls_alert_get
# define gnutls_alert_get_name fn_gnutls_alert_get_name
# define gnutls_anon_allocate_client_credentials fn_gnutls_anon_allocate_client_credentials
# define gnutls_anon_free_client_credentials fn_gnutls_anon_free_client_credentials
# define gnutls_bye fn_gnutls_bye
# define gnutls_certificate_allocate_credentials fn_gnutls_certificate_allocate_credentials
# define gnutls_certificate_free_credentials fn_gnutls_certificate_free_credentials
# define gnutls_certificate_get_peers fn_gnutls_certificate_get_peers
# define gnutls_certificate_set_verify_flags fn_gnutls_certificate_set_verify_flags
# define gnutls_certificate_set_x509_crl_file fn_gnutls_certificate_set_x509_crl_file
# define gnutls_certificate_set_x509_key_file fn_gnutls_certificate_set_x509_key_file
# define gnutls_certificate_set_x509_system_trust fn_gnutls_certificate_set_x509_system_trust
# define gnutls_certificate_set_x509_trust_file fn_gnutls_certificate_set_x509_trust_file
# define gnutls_certificate_type_get fn_gnutls_certificate_type_get
# define gnutls_certificate_verify_peers2 fn_gnutls_certificate_verify_peers2
# define gnutls_cipher_get fn_gnutls_cipher_get
# define gnutls_cipher_get_name fn_gnutls_cipher_get_name
# define gnutls_credentials_set fn_gnutls_credentials_set
# define gnutls_deinit fn_gnutls_deinit
# define gnutls_dh_get_prime_bits fn_gnutls_dh_get_prime_bits
# define gnutls_dh_set_prime_bits fn_gnutls_dh_set_prime_bits
# define gnutls_error_is_fatal fn_gnutls_error_is_fatal
# define gnutls_global_init fn_gnutls_global_init
# define gnutls_global_set_audit_log_function fn_gnutls_global_set_audit_log_function
# define gnutls_global_set_log_function fn_gnutls_global_set_log_function
# define gnutls_global_set_log_level fn_gnutls_global_set_log_level
# define gnutls_handshake fn_gnutls_handshake
# define gnutls_init fn_gnutls_init
# define gnutls_kx_get fn_gnutls_kx_get
# define gnutls_kx_get_name fn_gnutls_kx_get_name
# define gnutls_mac_get fn_gnutls_mac_get
# define gnutls_mac_get_name fn_gnutls_mac_get_name
# define gnutls_pk_algorithm_get_name fn_gnutls_pk_algorithm_get_name
# define gnutls_pk_bits_to_sec_param fn_gnutls_pk_bits_to_sec_param
# define gnutls_priority_set_direct fn_gnutls_priority_set_direct
# define gnutls_protocol_get_name fn_gnutls_protocol_get_name
# define gnutls_protocol_get_version fn_gnutls_protocol_get_version
# define gnutls_record_check_pending fn_gnutls_record_check_pending
# define gnutls_record_recv fn_gnutls_record_recv
# define gnutls_record_send fn_gnutls_record_send
# define gnutls_sec_param_get_name fn_gnutls_sec_param_get_name
# define gnutls_server_name_set fn_gnutls_server_name_set
# define gnutls_sign_get_name fn_gnutls_sign_get_name
# define gnutls_strerror fn_gnutls_strerror
# define gnutls_transport_set_errno fn_gnutls_transport_set_errno
# define gnutls_transport_set_ptr2 fn_gnutls_transport_set_ptr2
# define gnutls_transport_set_pull_function fn_gnutls_transport_set_pull_function
# define gnutls_transport_set_push_function fn_gnutls_transport_set_push_function
# define gnutls_x509_crt_check_hostname fn_gnutls_x509_crt_check_hostname
319
# define gnutls_x509_crt_check_issuer fn_gnutls_x509_crt_check_issuer
Paul Eggert's avatar
Paul Eggert committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
# define gnutls_x509_crt_deinit fn_gnutls_x509_crt_deinit
# define gnutls_x509_crt_get_activation_time fn_gnutls_x509_crt_get_activation_time
# define gnutls_x509_crt_get_dn fn_gnutls_x509_crt_get_dn
# define gnutls_x509_crt_get_expiration_time fn_gnutls_x509_crt_get_expiration_time
# define gnutls_x509_crt_get_fingerprint fn_gnutls_x509_crt_get_fingerprint
# define gnutls_x509_crt_get_issuer_dn fn_gnutls_x509_crt_get_issuer_dn
# define gnutls_x509_crt_get_issuer_unique_id fn_gnutls_x509_crt_get_issuer_unique_id
# define gnutls_x509_crt_get_key_id fn_gnutls_x509_crt_get_key_id
# define gnutls_x509_crt_get_pk_algorithm fn_gnutls_x509_crt_get_pk_algorithm
# define gnutls_x509_crt_get_serial fn_gnutls_x509_crt_get_serial
# define gnutls_x509_crt_get_signature_algorithm fn_gnutls_x509_crt_get_signature_algorithm
# define gnutls_x509_crt_get_subject_unique_id fn_gnutls_x509_crt_get_subject_unique_id
# define gnutls_x509_crt_get_version fn_gnutls_x509_crt_get_version
# define gnutls_x509_crt_import fn_gnutls_x509_crt_import
# define gnutls_x509_crt_init fn_gnutls_x509_crt_init

336
#endif
337 338


339 340 341 342 343 344 345 346 347 348 349
/* Report memory exhaustion if ERR is an out-of-memory indication.  */
static void
check_memory_full (int err)
{
  /* When GnuTLS exhausts memory, it doesn't say how much memory it
     asked for, so tell the Emacs allocator that GnuTLS asked for no
     bytes.  This isn't accurate, but it's good enough.  */
  if (err == GNUTLS_E_MEMORY_ERROR)
    memory_full (0);
}

350
#ifdef HAVE_GNUTLS3
351
/* Log a simple audit message.  */
352
static void
353
gnutls_audit_log_function (gnutls_session_t session, const char *string)
354 355 356 357 358 359
{
  if (global_gnutls_log_level >= 1)
    {
      message ("gnutls.c: [audit] %s", string);
    }
}
360
#endif
361

362
/* Log a simple message.  */
363
static void
364
gnutls_log_function (int level, const char *string)
365 366 367 368
{
  message ("gnutls.c: [%d] %s", level, string);
}

369
/* Log a message and a string.  */
370
static void
371
gnutls_log_function2 (int level, const char *string, const char *extra)
372 373 374 375
{
  message ("gnutls.c: [%d] %s %s", level, string, extra);
}

376 377 378 379 380
int
gnutls_try_handshake (struct Lisp_Process *proc)
{
  gnutls_session_t state = proc->gnutls_state;
  int ret;
381
  bool non_blocking = proc->is_non_blocking_client;
382

383 384 385 386
  if (proc->gnutls_complete_negotiation_p)
    non_blocking = false;

  if (non_blocking)
387 388
    proc->gnutls_p = true;

389 390 391 392
  do
    {
      ret = gnutls_handshake (state);
      emacs_gnutls_handle_error (state, ret);
Paul Eggert's avatar
Paul Eggert committed
393
      maybe_quit ();
394
    }
395 396 397
  while (ret < 0
	 && gnutls_error_is_fatal (ret) == 0
	 && ! non_blocking);
398 399 400 401 402 403 404 405 406 407

  proc->gnutls_initstage = GNUTLS_STAGE_HANDSHAKE_TRIED;

  if (ret == GNUTLS_E_SUCCESS)
    {
      /* Here we're finally done.  */
      proc->gnutls_initstage = GNUTLS_STAGE_READY;
    }
  else
    {
408
      /* check_memory_full (gnutls_alert_send_appropriate (state, ret));  */
409 410 411 412
    }
  return ret;
}

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
#ifndef WINDOWSNT
static int
emacs_gnutls_nonblock_errno (gnutls_transport_ptr_t ptr)
{
  int err = errno;

  switch (err)
    {
# ifdef _AIX
      /* This is taken from the GnuTLS system_errno function circa 2016;
	 see <http://savannah.gnu.org/support/?107464>.  */
    case 0:
      errno = EAGAIN;
      /* Fall through.  */
# endif
    case EINPROGRESS:
    case ENOTCONN:
      return EAGAIN;

    default:
      return err;
    }
}
#endif

438
static int
439 440 441 442 443
emacs_gnutls_handshake (struct Lisp_Process *proc)
{
  gnutls_session_t state = proc->gnutls_state;

  if (proc->gnutls_initstage < GNUTLS_STAGE_HANDSHAKE_CANDO)
444
    return -1;
445 446

  if (proc->gnutls_initstage < GNUTLS_STAGE_TRANSPORT_POINTERS_SET)
447
    {
448 449
#ifdef WINDOWSNT
      /* On W32 we cannot transfer socket handles between different runtime
Andreas Schwab's avatar
Andreas Schwab committed
450 451
	 libraries, so we tell GnuTLS to use our special push/pull
	 functions.  */
Paul Eggert's avatar
Paul Eggert committed
452 453 454 455 456
      gnutls_transport_set_ptr2 (state,
				 (gnutls_transport_ptr_t) proc,
				 (gnutls_transport_ptr_t) proc);
      gnutls_transport_set_push_function (state, &emacs_gnutls_push);
      gnutls_transport_set_pull_function (state, &emacs_gnutls_pull);
457
#else
458
      /* This is how GnuTLS takes sockets: as file descriptors passed
Andreas Schwab's avatar
Andreas Schwab committed
459 460
	 in.  For an Emacs process socket, infd and outfd are the
	 same but we use this two-argument version for clarity.  */
Paul Eggert's avatar
Paul Eggert committed
461 462 463
      gnutls_transport_set_ptr2 (state,
				 (void *) (intptr_t) proc->infd,
				 (void *) (intptr_t) proc->outfd);
464 465 466
      if (proc->is_non_blocking_client)
	gnutls_transport_set_errno_function (state,
					     emacs_gnutls_nonblock_errno);
467
#endif
468

469 470
      proc->gnutls_initstage = GNUTLS_STAGE_TRANSPORT_POINTERS_SET;
    }
471

472
  return gnutls_try_handshake (proc);
473 474
}

475
ptrdiff_t
476 477
emacs_gnutls_record_check_pending (gnutls_session_t state)
{
Paul Eggert's avatar
Paul Eggert committed
478
  return gnutls_record_check_pending (state);
479 480
}

481
#ifdef WINDOWSNT
482 483 484
void
emacs_gnutls_transport_set_errno (gnutls_session_t state, int err)
{
Paul Eggert's avatar
Paul Eggert committed
485
  gnutls_transport_set_errno (state, err);
486
}
487
#endif
488

489 490
ptrdiff_t
emacs_gnutls_write (struct Lisp_Process *proc, const char *buf, ptrdiff_t nbyte)
Ted Zlatanov's avatar
Ted Zlatanov committed
491
{
492
  ssize_t rtnval = 0;
493
  ptrdiff_t bytes_written;
494 495
  gnutls_session_t state = proc->gnutls_state;

496 497 498 499 500
  if (proc->gnutls_initstage != GNUTLS_STAGE_READY)
    {
      errno = EAGAIN;
      return 0;
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
501 502 503 504 505

  bytes_written = 0;

  while (nbyte > 0)
    {
Paul Eggert's avatar
Paul Eggert committed
506
      rtnval = gnutls_record_send (state, buf, nbyte);
Ted Zlatanov's avatar
Ted Zlatanov committed
507

508
      if (rtnval < 0)
Andreas Schwab's avatar
Andreas Schwab committed
509
	{
510
	  if (rtnval == GNUTLS_E_INTERRUPTED)
Andreas Schwab's avatar
Andreas Schwab committed
511 512
	    continue;
	  else
513 514 515 516 517
	    {
	      /* If we get GNUTLS_E_AGAIN, then set errno
		 appropriately so that send_process retries the
		 correct way instead of erroring out. */
	      if (rtnval == GNUTLS_E_AGAIN)
518
		errno = EAGAIN;
519
	      break;
520
	    }
Andreas Schwab's avatar
Andreas Schwab committed
521
	}
Ted Zlatanov's avatar
Ted Zlatanov committed
522 523 524 525 526 527

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

528
  emacs_gnutls_handle_error (state, rtnval);
Ted Zlatanov's avatar
Ted Zlatanov committed
529 530 531
  return (bytes_written);
}

532 533
ptrdiff_t
emacs_gnutls_read (struct Lisp_Process *proc, char *buf, ptrdiff_t nbyte)
Ted Zlatanov's avatar
Ted Zlatanov committed
534
{
535
  ssize_t rtnval;
536 537
  gnutls_session_t state = proc->gnutls_state;

538
  if (proc->gnutls_initstage != GNUTLS_STAGE_READY)
539 540 541 542
    {
      errno = EAGAIN;
      return -1;
    }
543

Paul Eggert's avatar
Paul Eggert committed
544
  rtnval = gnutls_record_recv (state, buf, nbyte);
545 546
  if (rtnval >= 0)
    return rtnval;
547 548 549
  else if (rtnval == GNUTLS_E_UNEXPECTED_PACKET_LENGTH)
    /* The peer closed the connection. */
    return 0;
550
  else if (emacs_gnutls_handle_error (state, rtnval))
551 552
    /* non-fatal error */
    return -1;
553
  else {
Juanma Barranquero's avatar
Juanma Barranquero committed
554
    /* a fatal error occurred */
555
    return 0;
556
  }
Ted Zlatanov's avatar
Ted Zlatanov committed
557 558
}

559 560 561
/* Report a GnuTLS error to the user.
   Return true if the error code was successfully handled.  */
static bool
562 563 564 565
emacs_gnutls_handle_error (gnutls_session_t session, int err)
{
  int max_log_level = 0;

566
  bool ret;
567 568 569 570
  const char *str;

  /* TODO: use a Lisp_Object generated by gnutls_make_error?  */
  if (err >= 0)
571
    return 1;
572

573 574
  check_memory_full (err);

575
  max_log_level = global_gnutls_log_level;
576 577 578

  /* TODO: use gnutls-error-fatalp and gnutls-error-string.  */

Paul Eggert's avatar
Paul Eggert committed
579
  str = gnutls_strerror (err);
580 581 582
  if (!str)
    str = "unknown";

Paul Eggert's avatar
Paul Eggert committed
583
  if (gnutls_error_is_fatal (err))
584
    {
585 586 587 588
      int level = 1;
      /* Mostly ignore "The TLS connection was non-properly
	 terminated" message which just means that the peer closed the
	 connection.  */
589
#ifdef HAVE_GNUTLS3
590 591
      if (err == GNUTLS_E_PREMATURE_TERMINATION)
	level = 3;
592
#endif
593 594

      GNUTLS_LOG2 (level, max_log_level, "fatal error:", str);
595
      ret = 0;
596 597 598
    }
  else
    {
599
      ret = 1;
600 601 602 603 604 605 606 607 608 609 610 611 612 613

      switch (err)
        {
        case GNUTLS_E_AGAIN:
          GNUTLS_LOG2 (3,
                       max_log_level,
                       "retry:",
                       str);
        default:
          GNUTLS_LOG2 (1,
                       max_log_level,
                       "non-fatal error:",
                       str);
        }
614 615 616 617 618
    }

  if (err == GNUTLS_E_WARNING_ALERT_RECEIVED
      || err == GNUTLS_E_FATAL_ALERT_RECEIVED)
    {
Paul Eggert's avatar
Paul Eggert committed
619
      int alert = gnutls_alert_get (session);
620
      int level = (err == GNUTLS_E_FATAL_ALERT_RECEIVED) ? 0 : 1;
Paul Eggert's avatar
Paul Eggert committed
621
      str = gnutls_alert_get_name (alert);
622 623 624 625 626 627 628 629
      if (!str)
	str = "unknown";

      GNUTLS_LOG2 (level, max_log_level, "Received alert: ", str);
    }
  return ret;
}

Ted Zlatanov's avatar
Ted Zlatanov committed
630 631 632 633
/* 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.  */
634
static Lisp_Object
635
gnutls_make_error (int err)
Ted Zlatanov's avatar
Ted Zlatanov committed
636
{
637
  switch (err)
638 639 640 641 642 643 644 645 646 647
    {
    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
648

649
  check_memory_full (err);
650
  return make_number (err);
Ted Zlatanov's avatar
Ted Zlatanov committed
651 652
}

653 654 655 656 657 658 659
Lisp_Object
emacs_gnutls_deinit (Lisp_Object proc)
{
  int log_level;

  CHECK_PROCESS (proc);

660
  if (! XPROCESS (proc)->gnutls_p)
661 662 663 664 665 666 667
    return Qnil;

  log_level = XPROCESS (proc)->gnutls_log_level;

  if (XPROCESS (proc)->gnutls_x509_cred)
    {
      GNUTLS_LOG (2, log_level, "Deallocating x509 credentials");
Paul Eggert's avatar
Paul Eggert committed
668
      gnutls_certificate_free_credentials (XPROCESS (proc)->gnutls_x509_cred);
669 670 671 672 673 674
      XPROCESS (proc)->gnutls_x509_cred = NULL;
    }

  if (XPROCESS (proc)->gnutls_anon_cred)
    {
      GNUTLS_LOG (2, log_level, "Deallocating anon credentials");
Paul Eggert's avatar
Paul Eggert committed
675
      gnutls_anon_free_client_credentials (XPROCESS (proc)->gnutls_anon_cred);
676 677 678
      XPROCESS (proc)->gnutls_anon_cred = NULL;
    }

Chong Yidong's avatar
Chong Yidong committed
679
  if (XPROCESS (proc)->gnutls_state)
680
    {
Paul Eggert's avatar
Paul Eggert committed
681
      gnutls_deinit (XPROCESS (proc)->gnutls_state);
Chong Yidong's avatar
Chong Yidong committed
682 683 684
      XPROCESS (proc)->gnutls_state = NULL;
      if (GNUTLS_INITSTAGE (proc) >= GNUTLS_STAGE_INIT)
	GNUTLS_INITSTAGE (proc) = GNUTLS_STAGE_INIT - 1;
685 686
    }

687
  XPROCESS (proc)->gnutls_p = false;
688 689 690
  return Qt;
}

691 692 693 694 695 696
DEFUN ("gnutls-asynchronous-parameters", Fgnutls_asynchronous_parameters,
       Sgnutls_asynchronous_parameters, 2, 2, 0,
       doc: /* Mark this process as being a pre-init GnuTLS process.
The second parameter is the list of parameters to feed to gnutls-boot
to finish setting up the connection. */)
  (Lisp_Object proc, Lisp_Object params)
697 698 699
{
  CHECK_PROCESS (proc);

700
  XPROCESS (proc)->gnutls_boot_parameters = params;
701 702 703
  return Qnil;
}

Ted Zlatanov's avatar
Ted Zlatanov committed
704
DEFUN ("gnutls-get-initstage", Fgnutls_get_initstage, Sgnutls_get_initstage, 1, 1, 0,
705
       doc: /* Return the GnuTLS init stage of process PROC.
Ted Zlatanov's avatar
Ted Zlatanov committed
706
See also `gnutls-boot'.  */)
707
  (Lisp_Object proc)
Ted Zlatanov's avatar
Ted Zlatanov committed
708 709 710 711 712 713 714
{
  CHECK_PROCESS (proc);

  return make_number (GNUTLS_INITSTAGE (proc));
}

DEFUN ("gnutls-errorp", Fgnutls_errorp, Sgnutls_errorp, 1, 1, 0,
715 716
       doc: /* Return t if ERROR indicates a GnuTLS problem.
ERROR is an integer or a symbol with an integer `gnutls-code' property.
717 718
usage: (gnutls-errorp ERROR)  */
       attributes: const)
719
  (Lisp_Object err)
Ted Zlatanov's avatar
Ted Zlatanov committed
720
{
721 722 723
  if (EQ (err, Qt)
      || EQ (err, Qgnutls_e_again))
    return Qnil;
Ted Zlatanov's avatar
Ted Zlatanov committed
724 725 726 727 728

  return Qt;
}

DEFUN ("gnutls-error-fatalp", Fgnutls_error_fatalp, Sgnutls_error_fatalp, 1, 1, 0,
729
       doc: /* Return non-nil if ERROR is fatal.
730
ERROR is an integer or a symbol with an integer `gnutls-code' property.
731
Usage: (gnutls-error-fatalp ERROR)  */)
732
  (Lisp_Object err)
Ted Zlatanov's avatar
Ted Zlatanov committed
733 734 735 736 737 738 739
{
  Lisp_Object code;

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

  if (SYMBOLP (err))
    {
740 741 742 743 744 745 746 747 748
      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
749 750
    }

751
  if (! TYPE_RANGED_INTEGERP (int, err))
Ted Zlatanov's avatar
Ted Zlatanov committed
752 753
    error ("Not an error symbol or code");

Paul Eggert's avatar
Paul Eggert committed
754
  if (0 == gnutls_error_is_fatal (XINT (err)))
Ted Zlatanov's avatar
Ted Zlatanov committed
755 756 757 758 759 760
    return Qnil;

  return Qt;
}

DEFUN ("gnutls-error-string", Fgnutls_error_string, Sgnutls_error_string, 1, 1, 0,
761 762 763 764
       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
765 766 767 768 769 770 771
{
  Lisp_Object code;

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

  if (SYMBOLP (err))
    {
772 773 774 775 776 777 778 779 780
      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
781 782
    }

783
  if (! TYPE_RANGED_INTEGERP (int, err))
Ted Zlatanov's avatar
Ted Zlatanov committed
784 785
    return build_string ("Not an error symbol or code");

Paul Eggert's avatar
Paul Eggert committed
786
  return build_string (gnutls_strerror (XINT (err)));
Ted Zlatanov's avatar
Ted Zlatanov committed
787 788 789
}

DEFUN ("gnutls-deinit", Fgnutls_deinit, Sgnutls_deinit, 1, 1, 0,
790
       doc: /* Deallocate GnuTLS resources associated with process PROC.
Ted Zlatanov's avatar
Ted Zlatanov committed
791
See also `gnutls-init'.  */)
792
  (Lisp_Object proc)
Ted Zlatanov's avatar
Ted Zlatanov committed
793
{
794
  return emacs_gnutls_deinit (proc);
Ted Zlatanov's avatar
Ted Zlatanov committed
795 796
}

797
static Lisp_Object
798
gnutls_hex_string (unsigned char *buf, ptrdiff_t buf_size, const char *prefix)
799
{
800
  ptrdiff_t prefix_length = strlen (prefix);
801 802 803
  ptrdiff_t retlen;
  if (INT_MULTIPLY_WRAPV (buf_size, 3, &retlen)
      || INT_ADD_WRAPV (prefix_length - (buf_size != 0), retlen, &retlen))
804
    string_overflow ();
805
  Lisp_Object ret = make_uninit_string (retlen);
806
  char *string = SSDATA (ret);
807 808
  strcpy (string, prefix);

809
  for (ptrdiff_t i = 0; i < buf_size; i++)
810
    sprintf (string + i * 3 + prefix_length,
811
	     i == buf_size - 1 ? "%02x" : "%02x:",
812
	     buf[i]);
813 814 815 816

  return ret;
}

817
static Lisp_Object
818 819 820 821
gnutls_certificate_details (gnutls_x509_crt_t cert)
{
  Lisp_Object res = Qnil;
  int err;
822
  size_t buf_size;
823 824 825

  /* Version. */
  {
Paul Eggert's avatar
Paul Eggert committed
826
    int version = gnutls_x509_crt_get_version (cert);
Paul Eggert's avatar
Paul Eggert committed
827
    check_memory_full (version);
828 829 830 831 832 833
    if (version >= GNUTLS_E_SUCCESS)
      res = nconc2 (res, list2 (intern (":version"),
				make_number (version)));
  }

  /* Serial. */
834
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
835
  err = gnutls_x509_crt_get_serial (cert, NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
836
  check_memory_full (err);
837 838
  if (err == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
839
      void *serial = xmalloc (buf_size);
Paul Eggert's avatar
Paul Eggert committed
840
      err = gnutls_x509_crt_get_serial (cert, serial, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
841
      check_memory_full (err);
842 843 844
      if (err >= GNUTLS_E_SUCCESS)
	res = nconc2 (res, list2 (intern (":serial-number"),
				  gnutls_hex_string (serial, buf_size, "")));
845
      xfree (serial);
846
    }
847 848

  /* Issuer. */
849
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
850
  err = gnutls_x509_crt_get_issuer_dn (cert, NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
851
  check_memory_full (err);
852 853
  if (err == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
854
      char *dn = xmalloc (buf_size);
Paul Eggert's avatar
Paul Eggert committed
855
      err = gnutls_x509_crt_get_issuer_dn (cert, dn, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
856
      check_memory_full (err);
857 858 859
      if (err >= GNUTLS_E_SUCCESS)
	res = nconc2 (res, list2 (intern (":issuer"),
				  make_string (dn, buf_size)));
860
      xfree (dn);
861
    }
862 863 864

  /* Validity. */
  {
865 866 867
    /* Add 1 to the buffer size, since 1900 is added to tm_year and
       that might add 1 to the year length.  */
    char buf[INT_STRLEN_BOUND (int) + 1 + sizeof "-12-31"];
868
    struct tm t;
Paul Eggert's avatar
Paul Eggert committed
869
    time_t tim = gnutls_x509_crt_get_activation_time (cert);
870

871
    if (gmtime_r (&tim, &t) && strftime (buf, sizeof buf, "%Y-%m-%d", &t))
872 873
      res = nconc2 (res, list2 (intern (":valid-from"), build_string (buf)));

Paul Eggert's avatar
Paul Eggert committed
874
    tim = gnutls_x509_crt_get_expiration_time (cert);
875
    if (gmtime_r (&tim, &t) && strftime (buf, sizeof buf, "%Y-%m-%d", &t))
876 877 878 879
      res = nconc2 (res, list2 (intern (":valid-to"), build_string (buf)));
  }

  /* Subject. */
880
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
881
  err = gnutls_x509_crt_get_dn (cert, NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
882
  check_memory_full (err);
883 884
  if (err == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
885
      char *dn = xmalloc (buf_size);
Paul Eggert's avatar
Paul Eggert committed
886
      err = gnutls_x509_crt_get_dn (cert, dn, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
887
      check_memory_full (err);
888 889 890
      if (err >= GNUTLS_E_SUCCESS)
	res = nconc2 (res, list2 (intern (":subject"),
				  make_string (dn, buf_size)));
891
      xfree (dn);
892
    }
893 894 895 896 897

  /* SubjectPublicKeyInfo. */
  {
    unsigned int bits;

Paul Eggert's avatar
Paul Eggert committed
898
    err = gnutls_x509_crt_get_pk_algorithm (cert, &bits);
Paul Eggert's avatar
Paul Eggert committed
899
    check_memory_full (err);
900 901
    if (err >= GNUTLS_E_SUCCESS)
      {
Paul Eggert's avatar
Paul Eggert committed
902
	const char *name = gnutls_pk_algorithm_get_name (err);
903 904 905 906
	if (name)
	  res = nconc2 (res, list2 (intern (":public-key-algorithm"),
				    build_string (name)));

Paul Eggert's avatar
Paul Eggert committed
907 908
	name = gnutls_sec_param_get_name (gnutls_pk_bits_to_sec_param
					  (err, bits));
909
	res = nconc2 (res, list2 (intern (":certificate-security-level"),
910
				  build_string (name)));
911
      }
912 913 914
  }

  /* Unique IDs. */
915
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
916
  err = gnutls_x509_crt_get_issuer_unique_id (cert, NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
917
  check_memory_full (err);
918 919
  if (err == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
920
      char *buf = xmalloc (buf_size);
Paul Eggert's avatar
Paul Eggert committed
921
      err = gnutls_x509_crt_get_issuer_unique_id (cert, buf, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
922
      check_memory_full (err);
923 924 925
      if (err >= GNUTLS_E_SUCCESS)
	res = nconc2 (res, list2 (intern (":issuer-unique-id"),
				  make_string (buf, buf_size)));
926
      xfree (buf);
927
    }
928

929
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
930
  err = gnutls_x509_crt_get_subject_unique_id (cert, NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
931
  check_memory_full (err);
932 933
  if (err == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
934
      char *buf = xmalloc (buf_size);
Paul Eggert's avatar
Paul Eggert committed
935
      err = gnutls_x509_crt_get_subject_unique_id (cert, buf, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
936
      check_memory_full (err);
937 938 939
      if (err >= GNUTLS_E_SUCCESS)
	res = nconc2 (res, list2 (intern (":subject-unique-id"),
				  make_string (buf, buf_size)));
940
      xfree (buf);
941
    }
942

943
  /* Signature. */
Paul Eggert's avatar
Paul Eggert committed
944
  err = gnutls_x509_crt_get_signature_algorithm (cert);
Paul Eggert's avatar
Paul Eggert committed
945
  check_memory_full (err);
946 947
  if (err >= GNUTLS_E_SUCCESS)
    {
Paul Eggert's avatar
Paul Eggert committed
948
      const char *name = gnutls_sign_get_name (err);
949 950 951 952 953
      if (name)
	res = nconc2 (res, list2 (intern (":signature-algorithm"),
				  build_string (name)));
    }

954
  /* Public key ID. */
955
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
956
  err = gnutls_x509_crt_get_key_id (cert, 0, NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
957
  check_memory_full (err);
958 959
  if (err == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
960
      void *buf = xmalloc (buf_size);
Paul Eggert's avatar
Paul Eggert committed
961
      err = gnutls_x509_crt_get_key_id (cert, 0, buf, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
962
      check_memory_full (err);
963 964
      if (err >= GNUTLS_E_SUCCESS)
	res = nconc2 (res, list2 (intern (":public-key-id"),
965 966
				  gnutls_hex_string (buf, buf_size, "sha1:")));
      xfree (buf);
967
    }
968 969

  /* Certificate fingerprint. */
970
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
971 972
  err = gnutls_x509_crt_get_fingerprint (cert, GNUTLS_DIG_SHA1,
					 NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
973
  check_memory_full (err);
Lars Magne Ingebrigtsen's avatar