gnutls.c 56 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-2015 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

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>
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 30

#ifdef HAVE_GNUTLS
#include <gnutls/gnutls.h>

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

36
static bool emacs_gnutls_handle_error (gnutls_session_t, int);
37

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

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

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

51 52 53

#ifdef WINDOWSNT

Paul Eggert's avatar
Paul Eggert committed
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 121 122 123 124
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_alert_send_appropriate, (gnutls_session_t, int));
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 (const char *, gnutls_check_version, (const char *));
DEF_DLL_FN (void, gnutls_transport_set_lowat, (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 *));
125 126
DEF_DLL_FN (int, gnutls_x509_crt_check_issuer,
              (gnutls_x509_crt_t, gnutls_x509_crt_t));
Paul Eggert's avatar
Paul Eggert committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
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 *));
DEF_DLL_FN (const char*, gnutls_pk_algorithm_get_name,
	    (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_signature,
	    (gnutls_x509_crt_t, char *, size_t *));
DEF_DLL_FN (int, gnutls_x509_crt_get_key_id,
	    (gnutls_x509_crt_t, unsigned int, unsigned char *, size_t *_size));
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));
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));
DEF_DLL_FN (const char*, gnutls_kx_get_name, (gnutls_kx_algorithm_t));
DEF_DLL_FN (gnutls_protocol_t, gnutls_protocol_get_version,
	    (gnutls_session_t));
DEF_DLL_FN (const char*, gnutls_protocol_get_name, (gnutls_protocol_t));
DEF_DLL_FN (gnutls_cipher_algorithm_t, gnutls_cipher_get,
	    (gnutls_session_t));
DEF_DLL_FN (const char*, gnutls_cipher_get_name,
	    (gnutls_cipher_algorithm_t));
DEF_DLL_FN (gnutls_mac_algorithm_t, gnutls_mac_get, (gnutls_session_t));
DEF_DLL_FN (const char*, gnutls_mac_get_name, (gnutls_mac_algorithm_t));
179

180

181
static bool
182
init_gnutls_functions (void)
183 184
{
  HMODULE library;
185
  int max_log_level = 1;
186

187
  if (!(library = w32_delayed_load (Qgnutls_dll)))
188
    {
189
      GNUTLS_LOG (1, max_log_level, "GnuTLS library not found");
190 191 192
      return 0;
    }

Paul Eggert's avatar
Paul Eggert committed
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 229 230 231 232
  LOAD_DLL_FN (library, gnutls_alert_get);
  LOAD_DLL_FN (library, gnutls_alert_get_name);
  LOAD_DLL_FN (library, gnutls_alert_send_appropriate);
  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_check_version);
233 234 235
  /* We don't need to call gnutls_transport_set_lowat in GnuTLS 2.11.1
     and later, and the function was removed entirely in 3.0.0.  */
  if (!fn_gnutls_check_version ("2.11.1"))
Paul Eggert's avatar
Paul Eggert committed
236 237 238 239 240
    LOAD_DLL_FN (library, gnutls_transport_set_lowat);
  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);
241
  LOAD_DLL_FN (library, gnutls_x509_crt_check_issuer);
Paul Eggert's avatar
Paul Eggert committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  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_signature);
  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);
271

272
  max_log_level = global_gnutls_log_level;
273

274 275 276 277 278 279
  {
    Lisp_Object name = CAR_SAFE (Fget (Qgnutls_dll, QCloaded_from));
    GNUTLS_LOG2 (1, max_log_level, "GnuTLS library loaded:",
                 STRINGP (name) ? (const char *) SDATA (name) : "unknown");
  }

280 281 282
  return 1;
}

Paul Eggert's avatar
Paul Eggert committed
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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
# define gnutls_alert_get fn_gnutls_alert_get
# define gnutls_alert_get_name fn_gnutls_alert_get_name
# define gnutls_alert_send_appropriate fn_gnutls_alert_send_appropriate
# 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_check_version fn_gnutls_check_version
# 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_lowat fn_gnutls_transport_set_lowat
# 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
335
# define gnutls_x509_crt_check_issuer fn_gnutls_x509_crt_check_issuer
Paul Eggert's avatar
Paul Eggert committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
# 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 fn_gnutls_x509_crt_get_signature
# 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

353
#endif
354 355


356 357 358 359 360 361 362 363 364 365 366
/* 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);
}

367
#ifdef HAVE_GNUTLS3
368
/* Log a simple audit message.  */
369
static void
370
gnutls_audit_log_function (gnutls_session_t session, const char *string)
371 372 373 374 375 376
{
  if (global_gnutls_log_level >= 1)
    {
      message ("gnutls.c: [audit] %s", string);
    }
}
377
#endif
378

379
/* Log a simple message.  */
380
static void
381
gnutls_log_function (int level, const char *string)
382 383 384 385
{
  message ("gnutls.c: [%d] %s", level, string);
}

386
/* Log a message and a string.  */
387
static void
388
gnutls_log_function2 (int level, const char *string, const char *extra)
389 390 391 392
{
  message ("gnutls.c: [%d] %s %s", level, string, extra);
}

393
/* Log a message and an integer.  */
394
static void
395
gnutls_log_function2i (int level, const char *string, int extra)
396 397 398 399
{
  message ("gnutls.c: [%d] %s %d", level, string, extra);
}

400
static int
401 402 403 404 405 406
emacs_gnutls_handshake (struct Lisp_Process *proc)
{
  gnutls_session_t state = proc->gnutls_state;
  int ret;

  if (proc->gnutls_initstage < GNUTLS_STAGE_HANDSHAKE_CANDO)
407
    return -1;
408 409

  if (proc->gnutls_initstage < GNUTLS_STAGE_TRANSPORT_POINTERS_SET)
410
    {
411 412
#ifdef WINDOWSNT
      /* On W32 we cannot transfer socket handles between different runtime
Andreas Schwab's avatar
Andreas Schwab committed
413 414
	 libraries, so we tell GnuTLS to use our special push/pull
	 functions.  */
Paul Eggert's avatar
Paul Eggert committed
415 416 417 418 419
      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);
420 421

      /* For non blocking sockets or other custom made pull/push
Andreas Schwab's avatar
Andreas Schwab committed
422 423
	 functions the gnutls_transport_set_lowat must be called, with
	 a zero low water mark value. (GnuTLS 2.10.4 documentation)
424

Andreas Schwab's avatar
Andreas Schwab committed
425 426 427
	 (Note: this is probably not strictly necessary as the lowat
	  value is only used when no custom pull/push functions are
	  set.)  */
428 429 430 431
      /* According to GnuTLS NEWS file, lowat level has been set to
	 zero by default in version 2.11.1, and the function
	 gnutls_transport_set_lowat was removed from the library in
	 version 2.99.0.  */
Paul Eggert's avatar
Paul Eggert committed
432 433
      if (!gnutls_check_version ("2.11.1"))
	gnutls_transport_set_lowat (state, 0);
434
#else
435
      /* This is how GnuTLS takes sockets: as file descriptors passed
Andreas Schwab's avatar
Andreas Schwab committed
436 437
	 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
438 439 440
      gnutls_transport_set_ptr2 (state,
				 (void *) (intptr_t) proc->infd,
				 (void *) (intptr_t) proc->outfd);
441
#endif
442

443 444
      proc->gnutls_initstage = GNUTLS_STAGE_TRANSPORT_POINTERS_SET;
    }
445

446 447
  do
    {
Paul Eggert's avatar
Paul Eggert committed
448
      ret = gnutls_handshake (state);
449
      emacs_gnutls_handle_error (state, ret);
450
      QUIT;
451
    }
Paul Eggert's avatar
Paul Eggert committed
452
  while (ret < 0 && gnutls_error_is_fatal (ret) == 0);
453

454 455 456
  proc->gnutls_initstage = GNUTLS_STAGE_HANDSHAKE_TRIED;

  if (ret == GNUTLS_E_SUCCESS)
457
    {
458
      /* Here we're finally done.  */
459 460
      proc->gnutls_initstage = GNUTLS_STAGE_READY;
    }
461 462
  else
    {
Paul Eggert's avatar
Paul Eggert committed
463
      check_memory_full (gnutls_alert_send_appropriate (state, ret));
464 465
    }
  return ret;
466 467
}

468
ptrdiff_t
469 470
emacs_gnutls_record_check_pending (gnutls_session_t state)
{
Paul Eggert's avatar
Paul Eggert committed
471
  return gnutls_record_check_pending (state);
472 473
}

474
#ifdef WINDOWSNT
475 476 477
void
emacs_gnutls_transport_set_errno (gnutls_session_t state, int err)
{
Paul Eggert's avatar
Paul Eggert committed
478
  gnutls_transport_set_errno (state, err);
479
}
480
#endif
481

482 483
ptrdiff_t
emacs_gnutls_write (struct Lisp_Process *proc, const char *buf, ptrdiff_t nbyte)
Ted Zlatanov's avatar
Ted Zlatanov committed
484
{
485
  ssize_t rtnval = 0;
486
  ptrdiff_t bytes_written;
487 488
  gnutls_session_t state = proc->gnutls_state;

489 490 491 492 493
  if (proc->gnutls_initstage != GNUTLS_STAGE_READY)
    {
      errno = EAGAIN;
      return 0;
    }
Ted Zlatanov's avatar
Ted Zlatanov committed
494 495 496 497 498

  bytes_written = 0;

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

501
      if (rtnval < 0)
Andreas Schwab's avatar
Andreas Schwab committed
502
	{
503
	  if (rtnval == GNUTLS_E_INTERRUPTED)
Andreas Schwab's avatar
Andreas Schwab committed
504 505
	    continue;
	  else
506 507 508 509 510
	    {
	      /* 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)
511
		errno = EAGAIN;
512
	      break;
513
	    }
Andreas Schwab's avatar
Andreas Schwab committed
514
	}
Ted Zlatanov's avatar
Ted Zlatanov committed
515 516 517 518 519 520

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

521
  emacs_gnutls_handle_error (state, rtnval);
Ted Zlatanov's avatar
Ted Zlatanov committed
522 523 524
  return (bytes_written);
}

525 526
ptrdiff_t
emacs_gnutls_read (struct Lisp_Process *proc, char *buf, ptrdiff_t nbyte)
Ted Zlatanov's avatar
Ted Zlatanov committed
527
{
528
  ssize_t rtnval;
529 530
  gnutls_session_t state = proc->gnutls_state;

531 532
  int log_level = proc->gnutls_log_level;

533 534
  if (proc->gnutls_initstage != GNUTLS_STAGE_READY)
    {
535 536 537 538 539 540 541
      /* If the handshake count is under the limit, try the handshake
         again and increment the handshake count.  This count is kept
         per process (connection), not globally.  */
      if (proc->gnutls_handshakes_tried < GNUTLS_EMACS_HANDSHAKES_LIMIT)
        {
          proc->gnutls_handshakes_tried++;
          emacs_gnutls_handshake (proc);
542
          GNUTLS_LOG2i (5, log_level, "Retried handshake",
543 544 545 546 547 548 549
                        proc->gnutls_handshakes_tried);
          return -1;
        }

      GNUTLS_LOG (2, log_level, "Giving up on handshake; resetting retries");
      proc->gnutls_handshakes_tried = 0;
      return 0;
550
    }
Paul Eggert's avatar
Paul Eggert committed
551
  rtnval = gnutls_record_recv (state, buf, nbyte);
552 553
  if (rtnval >= 0)
    return rtnval;
554 555 556
  else if (rtnval == GNUTLS_E_UNEXPECTED_PACKET_LENGTH)
    /* The peer closed the connection. */
    return 0;
557
  else if (emacs_gnutls_handle_error (state, rtnval))
558 559
    /* non-fatal error */
    return -1;
560
  else {
Juanma Barranquero's avatar
Juanma Barranquero committed
561
    /* a fatal error occurred */
562
    return 0;
563
  }
Ted Zlatanov's avatar
Ted Zlatanov committed
564 565
}

566 567 568
/* Report a GnuTLS error to the user.
   Return true if the error code was successfully handled.  */
static bool
569 570 571 572
emacs_gnutls_handle_error (gnutls_session_t session, int err)
{
  int max_log_level = 0;

573
  bool ret;
574 575 576 577
  const char *str;

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

580 581
  check_memory_full (err);

582
  max_log_level = global_gnutls_log_level;
583 584 585

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

Paul Eggert's avatar
Paul Eggert committed
586
  str = gnutls_strerror (err);
587 588 589
  if (!str)
    str = "unknown";

Paul Eggert's avatar
Paul Eggert committed
590
  if (gnutls_error_is_fatal (err))
591
    {
592
      ret = 0;
593
      GNUTLS_LOG2 (1, max_log_level, "fatal error:", str);
594 595 596
    }
  else
    {
597
      ret = 1;
598 599 600 601 602 603 604 605 606 607 608 609 610 611

      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);
        }
612 613 614 615 616
    }

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

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

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

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

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
Lisp_Object
emacs_gnutls_deinit (Lisp_Object proc)
{
  int log_level;

  CHECK_PROCESS (proc);

  if (XPROCESS (proc)->gnutls_p == 0)
    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
666
      gnutls_certificate_free_credentials (XPROCESS (proc)->gnutls_x509_cred);
667 668 669 670 671 672
      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
673
      gnutls_anon_free_client_credentials (XPROCESS (proc)->gnutls_anon_cred);
674 675 676
      XPROCESS (proc)->gnutls_anon_cred = NULL;
    }

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

  XPROCESS (proc)->gnutls_p = 0;
  return Qt;
}

Ted Zlatanov's avatar
Ted Zlatanov committed
689
DEFUN ("gnutls-get-initstage", Fgnutls_get_initstage, Sgnutls_get_initstage, 1, 1, 0,
690
       doc: /* Return the GnuTLS init stage of process PROC.
Ted Zlatanov's avatar
Ted Zlatanov committed
691
See also `gnutls-boot'.  */)
692
  (Lisp_Object proc)
Ted Zlatanov's avatar
Ted Zlatanov committed
693 694 695 696 697 698 699
{
  CHECK_PROCESS (proc);

  return make_number (GNUTLS_INITSTAGE (proc));
}

DEFUN ("gnutls-errorp", Fgnutls_errorp, Sgnutls_errorp, 1, 1, 0,
700 701
       doc: /* Return t if ERROR indicates a GnuTLS problem.
ERROR is an integer or a symbol with an integer `gnutls-code' property.
702 703
usage: (gnutls-errorp ERROR)  */
       attributes: const)
704
  (Lisp_Object err)
Ted Zlatanov's avatar
Ted Zlatanov committed
705
{
706
  if (EQ (err, Qt)) return Qnil;
Ted Zlatanov's avatar
Ted Zlatanov committed
707 708 709 710 711

  return Qt;
}

DEFUN ("gnutls-error-fatalp", Fgnutls_error_fatalp, Sgnutls_error_fatalp, 1, 1, 0,
712
       doc: /* Return non-nil if ERROR is fatal.
713
ERROR is an integer or a symbol with an integer `gnutls-code' property.
714
Usage: (gnutls-error-fatalp ERROR)  */)
715
  (Lisp_Object err)
Ted Zlatanov's avatar
Ted Zlatanov committed
716 717 718 719 720 721 722
{
  Lisp_Object code;

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

  if (SYMBOLP (err))
    {
723 724 725 726 727 728 729 730 731
      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
732 733
    }

734
  if (! TYPE_RANGED_INTEGERP (int, err))
Ted Zlatanov's avatar
Ted Zlatanov committed
735 736
    error ("Not an error symbol or code");

Paul Eggert's avatar
Paul Eggert committed
737
  if (0 == gnutls_error_is_fatal (XINT (err)))
Ted Zlatanov's avatar
Ted Zlatanov committed
738 739 740 741 742 743
    return Qnil;

  return Qt;
}

DEFUN ("gnutls-error-string", Fgnutls_error_string, Sgnutls_error_string, 1, 1, 0,
744 745 746 747
       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
748 749 750 751 752 753 754
{
  Lisp_Object code;

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

  if (SYMBOLP (err))
    {
755 756 757 758 759 760 761 762 763
      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
764 765
    }

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

Paul Eggert's avatar
Paul Eggert committed
769
  return build_string (gnutls_strerror (XINT (err)));
Ted Zlatanov's avatar
Ted Zlatanov committed
770 771 772
}

DEFUN ("gnutls-deinit", Fgnutls_deinit, Sgnutls_deinit, 1, 1, 0,
773
       doc: /* Deallocate GnuTLS resources associated with process PROC.
Ted Zlatanov's avatar
Ted Zlatanov committed
774
See also `gnutls-init'.  */)
775
  (Lisp_Object proc)
Ted Zlatanov's avatar
Ted Zlatanov committed
776
{
777
  return emacs_gnutls_deinit (proc);
Ted Zlatanov's avatar
Ted Zlatanov committed
778 779
}

780
static Lisp_Object
781
gnutls_hex_string (unsigned char *buf, ptrdiff_t buf_size, const char *prefix)
782
{
783
  ptrdiff_t prefix_length = strlen (prefix);
784 785 786
  ptrdiff_t retlen;
  if (INT_MULTIPLY_WRAPV (buf_size, 3, &retlen)
      || INT_ADD_WRAPV (prefix_length - (buf_size != 0), retlen, &retlen))
787
    string_overflow ();
788
  Lisp_Object ret = make_uninit_string (retlen);
789
  char *string = SSDATA (ret);
790 791
  strcpy (string, prefix);

792
  for (ptrdiff_t i = 0; i < buf_size; i++)
793
    sprintf (string + i * 3 + prefix_length,
794
	     i == buf_size - 1 ? "%02x" : "%02x:",
795
	     buf[i]);
796 797 798 799

  return ret;
}

800
static Lisp_Object
801 802 803 804
gnutls_certificate_details (gnutls_x509_crt_t cert)
{
  Lisp_Object res = Qnil;
  int err;
805
  size_t buf_size;
806 807 808

  /* Version. */
  {
Paul Eggert's avatar
Paul Eggert committed
809
    int version = gnutls_x509_crt_get_version (cert);
Paul Eggert's avatar
Paul Eggert committed
810
    check_memory_full (version);
811 812 813 814 815 816
    if (version >= GNUTLS_E_SUCCESS)
      res = nconc2 (res, list2 (intern (":version"),
				make_number (version)));
  }

  /* Serial. */
817
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
818
  err = gnutls_x509_crt_get_serial (cert, NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
819
  check_memory_full (err);
820 821
  if (err == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
822
      void *serial = xmalloc (buf_size);
Paul Eggert's avatar
Paul Eggert committed
823
      err = gnutls_x509_crt_get_serial (cert, serial, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
824
      check_memory_full (err);
825 826 827
      if (err >= GNUTLS_E_SUCCESS)
	res = nconc2 (res, list2 (intern (":serial-number"),
				  gnutls_hex_string (serial, buf_size, "")));
828
      xfree (serial);
829
    }
830 831

  /* Issuer. */
832
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
833
  err = gnutls_x509_crt_get_issuer_dn (cert, NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
834
  check_memory_full (err);
835 836
  if (err == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
837
      char *dn = xmalloc (buf_size);
Paul Eggert's avatar
Paul Eggert committed
838
      err = gnutls_x509_crt_get_issuer_dn (cert, dn, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
839
      check_memory_full (err);
840 841 842
      if (err >= GNUTLS_E_SUCCESS)
	res = nconc2 (res, list2 (intern (":issuer"),
				  make_string (dn, buf_size)));
843
      xfree (dn);
844
    }
845 846 847

  /* Validity. */
  {
848 849 850
    /* 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"];
851
    struct tm t;
Paul Eggert's avatar
Paul Eggert committed
852
    time_t tim = gnutls_x509_crt_get_activation_time (cert);
853

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

Paul Eggert's avatar
Paul Eggert committed
857
    tim = gnutls_x509_crt_get_expiration_time (cert);
858
    if (gmtime_r (&tim, &t) && strftime (buf, sizeof buf, "%Y-%m-%d", &t))
859 860 861 862
      res = nconc2 (res, list2 (intern (":valid-to"), build_string (buf)));
  }

  /* Subject. */
863
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
864
  err = gnutls_x509_crt_get_dn (cert, NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
865
  check_memory_full (err);
866 867
  if (err == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
868
      char *dn = xmalloc (buf_size);
Paul Eggert's avatar
Paul Eggert committed
869
      err = gnutls_x509_crt_get_dn (cert, dn, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
870
      check_memory_full (err);
871 872 873
      if (err >= GNUTLS_E_SUCCESS)
	res = nconc2 (res, list2 (intern (":subject"),
				  make_string (dn, buf_size)));
874
      xfree (dn);
875
    }
876

877
  /* Versions older than 2.11 doesn't have these four functions. */
878
#if GNUTLS_VERSION_NUMBER >= 0x020b00
879 880 881 882
  /* SubjectPublicKeyInfo. */
  {
    unsigned int bits;

Paul Eggert's avatar
Paul Eggert committed
883
    err = gnutls_x509_crt_get_pk_algorithm (cert, &bits);
Paul Eggert's avatar
Paul Eggert committed
884
    check_memory_full (err);
885 886
    if (err >= GNUTLS_E_SUCCESS)
      {
Paul Eggert's avatar
Paul Eggert committed
887
	const char *name = gnutls_pk_algorithm_get_name (err);
888 889 890 891
	if (name)
	  res = nconc2 (res, list2 (intern (":public-key-algorithm"),
				    build_string (name)));

Paul Eggert's avatar
Paul Eggert committed
892 893
	name = gnutls_sec_param_get_name (gnutls_pk_bits_to_sec_param
					  (err, bits));
894
	res = nconc2 (res, list2 (intern (":certificate-security-level"),
895
				  build_string (name)));
896
      }
897 898 899
  }

  /* Unique IDs. */
900
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
901
  err = gnutls_x509_crt_get_issuer_unique_id (cert, NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
902
  check_memory_full (err);
903 904
  if (err == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
905
      char *buf = xmalloc (buf_size);
Paul Eggert's avatar
Paul Eggert committed
906
      err = gnutls_x509_crt_get_issuer_unique_id (cert, buf, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
907
      check_memory_full (err);
908 909 910
      if (err >= GNUTLS_E_SUCCESS)
	res = nconc2 (res, list2 (intern (":issuer-unique-id"),
				  make_string (buf, buf_size)));
911
      xfree (buf);
912
    }
913

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

929
  /* Signature. */
Paul Eggert's avatar
Paul Eggert committed
930
  err = gnutls_x509_crt_get_signature_algorithm (cert);
Paul Eggert's avatar
Paul Eggert committed
931
  check_memory_full (err);
932 933
  if (err >= GNUTLS_E_SUCCESS)
    {
Paul Eggert's avatar
Paul Eggert committed
934
      const char *name = gnutls_sign_get_name (err);
935 936 937 938 939
      if (name)
	res = nconc2 (res, list2 (intern (":signature-algorithm"),
				  build_string (name)));
    }

940
  /* Public key ID. */
941
  buf_size = 0;
Paul Eggert's avatar
Paul Eggert committed
942
  err = gnutls_x509_crt_get_key_id (cert, 0, NULL, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
943
  check_memory_full (err);
944 945
  if (err == GNUTLS_E_SHORT_MEMORY_BUFFER)
    {
946
      void *buf = xmalloc (buf_size);
Paul Eggert's avatar
Paul Eggert committed
947
      err = gnutls_x509_crt_get_key_id (cert, 0, buf, &buf_size);
Paul Eggert's avatar
Paul Eggert committed
948
      check_memory_full (err);
949 950
      if (err >= GNUTLS_E_SUCCESS)
	res = nconc2 (res, list2 (intern (":public-key-id"),
951 952
				  gnutls_hex_string (buf, buf_size, "sha1:")));
      xfree (buf);
953
    }
954 955

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