X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;ds=sidebyside;f=libraries%2Flibldap%2Ftls_g.c;h=adcb6be04076a91d3a0bf94cf8357f4e51f5b9da;hb=8e34ed8c786a1f786976da046907f7dbd5d3458d;hp=4ee683c4365bf63277cec57739a55d1b9a192813;hpb=829027945f0d6b03283e55530443d55272763838;p=openldap diff --git a/libraries/libldap/tls_g.c b/libraries/libldap/tls_g.c index 4ee683c436..adcb6be040 100644 --- a/libraries/libldap/tls_g.c +++ b/libraries/libldap/tls_g.c @@ -2,7 +2,7 @@ /* $OpenLDAP$ */ /* This work is part of OpenLDAP Software . * - * Copyright 2008-2014 The OpenLDAP Foundation. + * Copyright 2008-2017 The OpenLDAP Foundation. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -43,39 +43,16 @@ #include #include - -#if LIBGNUTLS_VERSION_NUMBER >= 0x020200 -#define HAVE_CIPHERSUITES 1 -#else -#undef HAVE_CIPHERSUITES -#endif - -#ifndef HAVE_CIPHERSUITES -/* Versions prior to 2.2.0 didn't handle cipher suites, so we had to - * kludge them ourselves. - */ -typedef struct tls_cipher_suite { - const char *name; - gnutls_kx_algorithm_t kx; - gnutls_cipher_algorithm_t cipher; - gnutls_mac_algorithm_t mac; - gnutls_protocol_t version; -} tls_cipher_suite; -#endif +#include +#include typedef struct tlsg_ctx { - struct ldapoptions *lo; gnutls_certificate_credentials_t cred; gnutls_dh_params_t dh_params; unsigned long verify_depth; int refcount; -#ifdef HAVE_CIPHERSUITES + int reqcert; gnutls_priority_t prios; -#else - int *kx_list; - int *cipher_list; - int *mac_list; -#endif #ifdef LDAP_R_COMPILE ldap_pvt_thread_mutex_t ref_mutex; #endif @@ -87,11 +64,6 @@ typedef struct tlsg_session { struct berval peer_der_dn; } tlsg_session; -#ifndef HAVE_CIPHERSUITES -static tls_cipher_suite *tlsg_ciphers; -static int tlsg_n_ciphers; -#endif - static int tlsg_parse_ciphers( tlsg_ctx *ctx, char *suites ); static int tlsg_cert_verify( tlsg_session *s ); @@ -152,35 +124,6 @@ static int tlsg_init( void ) { gnutls_global_init(); - -#ifndef HAVE_CIPHERSUITES - /* GNUtls cipher suite handling: The library ought to parse suite - * names for us, but it doesn't. It will return a list of suite names - * that it supports, so we can do parsing ourselves. It ought to tell - * us how long the list is, but it doesn't do that either, so we just - * have to count it manually... - */ - { - int i = 0; - tls_cipher_suite *ptr, tmp; - char cs_id[2]; - - while ( gnutls_cipher_suite_info( i, cs_id, &tmp.kx, &tmp.cipher, - &tmp.mac, &tmp.version )) - i++; - tlsg_n_ciphers = i; - - /* Store a copy */ - tlsg_ciphers = LDAP_MALLOC(tlsg_n_ciphers * sizeof(tls_cipher_suite)); - if ( !tlsg_ciphers ) - return -1; - for ( i=0; ilo = lo; if ( gnutls_certificate_allocate_credentials( &ctx->cred )) { ber_memfree( ctx ); return NULL; } ctx->refcount = 1; -#ifdef HAVE_CIPHERSUITES gnutls_priority_init( &ctx->prios, "NORMAL", NULL ); -#endif #ifdef LDAP_R_COMPILE ldap_pvt_thread_mutex_init( &ctx->ref_mutex ); #endif @@ -243,11 +178,7 @@ tlsg_ctx_free ( tls_ctx *ctx ) LDAP_MUTEX_UNLOCK( &c->ref_mutex ); if ( refcount ) return; -#ifdef HAVE_CIPHERSUITES gnutls_priority_deinit( c->prios ); -#else - LDAP_FREE( c->kx_list ); -#endif gnutls_certificate_free_credentials( c->cred ); if ( c->dh_params ) gnutls_dh_params_deinit( c->dh_params ); @@ -309,8 +240,19 @@ tlsg_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server ) GNUTLS_X509_FMT_PEM ); if ( rc < 0 ) return -1; } + if (lo->ldo_tls_cacert.bv_val != NULL ) { + gnutls_datum_t buf; + buf.data = (unsigned char *)lo->ldo_tls_cacert.bv_val; + buf.size = lo->ldo_tls_cacert.bv_len; + rc = gnutls_certificate_set_x509_trust_mem( + ctx->cred, + &buf, + GNUTLS_X509_FMT_DER ); + if ( rc < 0 ) return -1; + } - if ( lo->ldo_tls_certfile && lo->ldo_tls_keyfile ) { + if (( lo->ldo_tls_certfile && lo->ldo_tls_keyfile ) || + ( lo->ldo_tls_cert.bv_val && lo->ldo_tls_key.bv_val )) { gnutls_x509_privkey_t key; gnutls_datum_t buf; gnutls_x509_crt_t certs[VERIFY_DEPTH]; @@ -324,25 +266,38 @@ tlsg_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server ) * not, we have to build it ourselves. So we have to * do some special checks here... */ - rc = tlsg_getfile( lt->lt_keyfile, &buf ); - if ( rc ) return -1; - rc = gnutls_x509_privkey_import( key, &buf, - GNUTLS_X509_FMT_PEM ); - LDAP_FREE( buf.data ); + if ( lo->ldo_tls_key.bv_val ) { + buf.data = (unsigned char *)lo->ldo_tls_key.bv_val; + buf.size = lo->ldo_tls_key.bv_len; + rc = gnutls_x509_privkey_import( key, &buf, + GNUTLS_X509_FMT_DER ); + } else { + rc = tlsg_getfile( lt->lt_keyfile, &buf ); + if ( rc ) return -1; + rc = gnutls_x509_privkey_import( key, &buf, + GNUTLS_X509_FMT_PEM ); + LDAP_FREE( buf.data ); + } if ( rc < 0 ) return rc; - rc = tlsg_getfile( lt->lt_certfile, &buf ); - if ( rc ) return -1; - rc = gnutls_x509_crt_list_import( certs, &max, &buf, - GNUTLS_X509_FMT_PEM, 0 ); - LDAP_FREE( buf.data ); + if ( lo->ldo_tls_cert.bv_val ) { + buf.data = (unsigned char *)lo->ldo_tls_cert.bv_val; + buf.size = lo->ldo_tls_cert.bv_len; + rc = gnutls_x509_crt_list_import( certs, &max, &buf, + GNUTLS_X509_FMT_DER, 0 ); + } else { + rc = tlsg_getfile( lt->lt_certfile, &buf ); + if ( rc ) return -1; + rc = gnutls_x509_crt_list_import( certs, &max, &buf, + GNUTLS_X509_FMT_PEM, 0 ); + LDAP_FREE( buf.data ); + } if ( rc < 0 ) return rc; /* If there's only one cert and it's not self-signed, * then we have to build the cert chain. */ if ( max == 1 && !gnutls_x509_crt_check_issuer( certs[0], certs[0] )) { -#if GNUTLS_VERSION_NUMBER >= 0x020c00 unsigned int i; for ( i = 1; icred, certs[i-1], &certs[i], 0 )) @@ -352,35 +307,19 @@ tlsg_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server ) if ( gnutls_x509_crt_check_issuer( certs[i], certs[i] )) break; } -#else - gnutls_x509_crt_t *cas; - unsigned int i, j, ncas; - - gnutls_certificate_get_x509_cas( ctx->cred, &cas, &ncas ); - for ( i = 1; icred, certs, max, key ); if ( rc ) return -1; - } else if ( lo->ldo_tls_certfile || lo->ldo_tls_keyfile ) { - Debug( LDAP_DEBUG_ANY, + } else if (( lo->ldo_tls_certfile || lo->ldo_tls_keyfile )) { + Debug( LDAP_DEBUG_ANY, "TLS: only one of certfile and keyfile specified\n", NULL, NULL, NULL ); return -1; + } else if (( lo->ldo_tls_cert.bv_val || lo->ldo_tls_key.bv_val )) { + Debug( LDAP_DEBUG_ANY, + "TLS: only one of cert and key specified\n", + NULL, NULL, NULL ); + return -1; } if ( lo->ldo_tls_crlfile ) { @@ -410,6 +349,9 @@ tlsg_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server ) if ( rc ) return -1; gnutls_certificate_set_dh_params( ctx->cred, ctx->dh_params ); } + + ctx->reqcert = lo->ldo_tls_require_cert; + return 0; } @@ -425,25 +367,16 @@ tlsg_session_new ( tls_ctx * ctx, int is_server ) session->ctx = c; gnutls_init( &session->session, is_server ? GNUTLS_SERVER : GNUTLS_CLIENT ); -#ifdef HAVE_CIPHERSUITES gnutls_priority_set( session->session, c->prios ); -#else - gnutls_set_default_priority( session->session ); - if ( c->kx_list ) { - gnutls_kx_set_priority( session->session, c->kx_list ); - gnutls_cipher_set_priority( session->session, c->cipher_list ); - gnutls_mac_set_priority( session->session, c->mac_list ); - } -#endif if ( c->cred ) gnutls_credentials_set( session->session, GNUTLS_CRD_CERTIFICATE, c->cred ); if ( is_server ) { int flag = 0; - if ( c->lo->ldo_tls_require_cert ) { + if ( c->reqcert ) { flag = GNUTLS_CERT_REQUEST; - if ( c->lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_DEMAND || - c->lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_HARD ) + if ( c->reqcert == LDAP_OPT_X_TLS_DEMAND || + c->reqcert == LDAP_OPT_X_TLS_HARD ) flag = GNUTLS_CERT_REQUIRE; gnutls_certificate_server_set_request( session->session, flag ); } @@ -457,18 +390,20 @@ tlsg_session_accept( tls_session *session ) tlsg_session *s = (tlsg_session *)session; int rc; - rc = gnutls_handshake( s->session ); - if ( rc == 0 && s->ctx->lo->ldo_tls_require_cert != LDAP_OPT_X_TLS_NEVER ) { + for ( rc = gnutls_handshake ( s->session ); + rc == GNUTLS_E_INTERRUPTED || rc == GNUTLS_E_AGAIN; + rc = gnutls_handshake ( s->session ) ); + if ( rc == 0 && s->ctx->reqcert != LDAP_OPT_X_TLS_NEVER ) { const gnutls_datum_t *peer_cert_list; unsigned int list_size; peer_cert_list = gnutls_certificate_get_peers( s->session, &list_size ); - if ( !peer_cert_list && s->ctx->lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_TRY ) + if ( !peer_cert_list && s->ctx->reqcert == LDAP_OPT_X_TLS_TRY ) rc = 0; else { rc = tlsg_cert_verify( s ); - if ( rc && s->ctx->lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_ALLOW ) + if ( rc && s->ctx->reqcert == LDAP_OPT_X_TLS_ALLOW ) rc = 0; } } @@ -771,8 +706,6 @@ tlsg_session_strength( tls_session *session ) static int tlsg_session_unique( tls_session *sess, struct berval *buf, int is_server) { -/* channel bindings added in 2.12.0 */ -#if GNUTLS_VERSION_NUMBER >= 0x020c00 tlsg_session *s = (tlsg_session *)sess; gnutls_datum_t cb; int rc; @@ -786,7 +719,6 @@ tlsg_session_unique( tls_session *sess, struct berval *buf, int is_server) memcpy( buf->bv_val, cb.data, len ); return len; } -#endif return 0; } @@ -822,92 +754,118 @@ tlsg_session_peercert( tls_session *sess, struct berval *der ) return 0; } +static int +tlsg_session_pinning( LDAP *ld, tls_session *sess, char *hashalg, struct berval *hash ) +{ + tlsg_session *s = (tlsg_session *)sess; + const gnutls_datum_t *cert_list; + unsigned int cert_list_size = 0; + gnutls_x509_crt_t crt; + gnutls_pubkey_t pubkey; + gnutls_datum_t key = {}; + gnutls_digest_algorithm_t alg; + struct berval keyhash; + size_t len; + int rc = -1; + + if ( hashalg ) { + alg = gnutls_digest_get_id( hashalg ); + if ( alg == GNUTLS_DIG_UNKNOWN ) { + Debug( LDAP_DEBUG_ANY, "tlsg_session_pinning: " + "unknown hashing algorithm for GnuTLS: '%s'\n", + hashalg, 0, 0 ); + return rc; + } + } + + cert_list = gnutls_certificate_get_peers( s->session, &cert_list_size ); + if ( cert_list_size == 0 ) { + return rc; + } + + if ( gnutls_x509_crt_init( &crt ) < 0 ) { + return rc; + } + + if ( gnutls_x509_crt_import( crt, &cert_list[0], GNUTLS_X509_FMT_DER ) ) { + goto done; + } + + if ( gnutls_pubkey_init( &pubkey ) ) { + goto done; + } + + if ( gnutls_pubkey_import_x509( pubkey, crt, 0 ) < 0 ) { + goto done; + } + + gnutls_pubkey_export( pubkey, GNUTLS_X509_FMT_DER, key.data, &len ); + if ( len <= 0 ) { + goto done; + } + + key.data = LDAP_MALLOC( len ); + if ( !key.data ) { + goto done; + } + + key.size = len; + + if ( gnutls_pubkey_export( pubkey, GNUTLS_X509_FMT_DER, + key.data, &len ) < 0 ) { + goto done; + } + + if ( hashalg ) { + keyhash.bv_len = gnutls_hash_get_len( alg ); + keyhash.bv_val = LDAP_MALLOC( keyhash.bv_len ); + if ( !keyhash.bv_val || gnutls_fingerprint( alg, &key, + keyhash.bv_val, &keyhash.bv_len ) < 0 ) { + goto done; + } + } else { + keyhash.bv_val = (char *)key.data; + keyhash.bv_len = key.size; + } + + if ( ber_bvcmp( hash, &keyhash ) ) { + rc = LDAP_CONNECT_ERROR; + Debug( LDAP_DEBUG_ANY, "tlsg_session_pinning: " + "public key hash does not match provided pin.\n", 0, 0, 0 ); + if ( ld->ld_error ) { + LDAP_FREE( ld->ld_error ); + } + ld->ld_error = LDAP_STRDUP( + _("TLS: public key hash does not match provided pin")); + } else { + rc = LDAP_SUCCESS; + } + +done: + if ( pubkey ) { + gnutls_pubkey_deinit( pubkey ); + } + if ( crt ) { + gnutls_x509_crt_deinit( crt ); + } + if ( keyhash.bv_val != (char *)key.data ) { + LDAP_FREE( keyhash.bv_val ); + } + if ( key.data ) { + LDAP_FREE( key.data ); + } + return rc; +} + /* suites is a string of colon-separated cipher suite names. */ static int tlsg_parse_ciphers( tlsg_ctx *ctx, char *suites ) { -#ifdef HAVE_CIPHERSUITES const char *err; int rc = gnutls_priority_init( &ctx->prios, suites, &err ); if ( rc ) ctx->prios = NULL; return rc; -#else - char *ptr, *end; - int i, j, len, num; - int *list, nkx = 0, ncipher = 0, nmac = 0; - int *kx, *cipher, *mac; - - num = 0; - ptr = suites; - do { - end = strchr(ptr, ':'); - if ( end ) - len = end - ptr; - else - len = strlen(ptr); - for (i=0; ikx_list = kx; - ctx->cipher_list = cipher; - ctx->mac_list = mac; - return 0; -#endif } /* @@ -964,7 +922,7 @@ tlsg_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg ) return -1; } - gnutls_transport_set_ptr( session->session, (gnutls_transport_ptr)p ); + gnutls_transport_set_ptr( session->session, (gnutls_transport_ptr_t)p ); gnutls_transport_set_pull_function( session->session, tlsg_recv ); gnutls_transport_set_push_function( session->session, tlsg_send ); p->session = session; @@ -1159,6 +1117,7 @@ tls_impl ldap_int_tls_impl = { tlsg_session_version, tlsg_session_cipher, tlsg_session_peercert, + tlsg_session_pinning, &tlsg_sbio,