1 /* tls_g.c - Handle tls/ssl using GNUTLS. */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008-2013 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* ACKNOWLEDGEMENTS: GNUTLS support written by Howard Chu and
17 * Emily Backes; sponsored by The Written Word (thewrittenword.com)
18 * and Stanford University (stanford.edu).
25 #include "ldap_config.h"
29 #include <ac/stdlib.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
35 #include <ac/unistd.h>
37 #include <ac/dirent.h>
44 #include <gnutls/gnutls.h>
45 #include <gnutls/x509.h>
48 #if LIBGNUTLS_VERSION_NUMBER >= 0x020200
49 #define HAVE_CIPHERSUITES 1
50 /* This is a kludge. gcrypt 1.4.x has support. Recent GnuTLS requires gcrypt 1.4.x
51 * but that dependency isn't reflected in their configure script, resulting in
52 * build errors on older gcrypt. So, if they have a working build environment,
53 * assume gcrypt is new enough.
55 #define HAVE_GCRYPT_RAND 1
57 #undef HAVE_CIPHERSUITES
58 #undef HAVE_GCRYPT_RAND
61 #ifndef HAVE_CIPHERSUITES
62 /* Versions prior to 2.2.0 didn't handle cipher suites, so we had to
63 * kludge them ourselves.
65 typedef struct tls_cipher_suite {
67 gnutls_kx_algorithm_t kx;
68 gnutls_cipher_algorithm_t cipher;
69 gnutls_mac_algorithm_t mac;
70 gnutls_protocol_t version;
74 typedef struct tlsg_ctx {
75 struct ldapoptions *lo;
76 gnutls_certificate_credentials_t cred;
77 gnutls_dh_params_t dh_params;
78 unsigned long verify_depth;
80 #ifdef HAVE_CIPHERSUITES
81 gnutls_priority_t prios;
88 ldap_pvt_thread_mutex_t ref_mutex;
92 typedef struct tlsg_session {
93 gnutls_session_t session;
95 struct berval peer_der_dn;
98 #ifndef HAVE_CIPHERSUITES
99 static tls_cipher_suite *tlsg_ciphers;
100 static int tlsg_n_ciphers;
103 static int tlsg_parse_ciphers( tlsg_ctx *ctx, char *suites );
104 static int tlsg_cert_verify( tlsg_session *s );
106 #ifdef LDAP_R_COMPILE
109 tlsg_mutex_init( void **priv )
112 ldap_pvt_thread_mutex_t *lock = LDAP_MALLOC( sizeof( ldap_pvt_thread_mutex_t ));
117 err = ldap_pvt_thread_mutex_init( lock );
127 tlsg_mutex_destroy( void **lock )
129 int err = ldap_pvt_thread_mutex_destroy( *lock );
135 tlsg_mutex_lock( void **lock )
137 return ldap_pvt_thread_mutex_lock( *lock );
141 tlsg_mutex_unlock( void **lock )
143 return ldap_pvt_thread_mutex_unlock( *lock );
146 static struct gcry_thread_cbs tlsg_thread_cbs = {
147 GCRY_THREAD_OPTION_USER,
153 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
157 tlsg_thr_init( void )
159 gcry_control (GCRYCTL_SET_THREAD_CBS, &tlsg_thread_cbs);
161 #endif /* LDAP_R_COMPILE */
164 * Initialize TLS subsystem. Should be called only once.
169 #ifdef HAVE_GCRYPT_RAND
170 struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();
171 if ( lo->ldo_tls_randfile &&
172 gcry_control( GCRYCTL_SET_RNDEGD_SOCKET, lo->ldo_tls_randfile )) {
173 Debug( LDAP_DEBUG_ANY,
174 "TLS: gcry_control GCRYCTL_SET_RNDEGD_SOCKET failed\n",
180 gnutls_global_init();
182 #ifndef HAVE_CIPHERSUITES
183 /* GNUtls cipher suite handling: The library ought to parse suite
184 * names for us, but it doesn't. It will return a list of suite names
185 * that it supports, so we can do parsing ourselves. It ought to tell
186 * us how long the list is, but it doesn't do that either, so we just
187 * have to count it manually...
191 tls_cipher_suite *ptr, tmp;
194 while ( gnutls_cipher_suite_info( i, cs_id, &tmp.kx, &tmp.cipher,
195 &tmp.mac, &tmp.version ))
200 tlsg_ciphers = LDAP_MALLOC(tlsg_n_ciphers * sizeof(tls_cipher_suite));
203 for ( i=0; i<tlsg_n_ciphers; i++ ) {
204 tlsg_ciphers[i].name = gnutls_cipher_suite_info( i, cs_id,
205 &tlsg_ciphers[i].kx, &tlsg_ciphers[i].cipher, &tlsg_ciphers[i].mac,
206 &tlsg_ciphers[i].version );
214 * Tear down the TLS subsystem. Should only be called once.
219 #ifndef HAVE_CIPHERSUITES
220 LDAP_FREE( tlsg_ciphers );
224 gnutls_global_deinit();
228 tlsg_ctx_new ( struct ldapoptions *lo )
232 ctx = ber_memcalloc ( 1, sizeof (*ctx) );
235 if ( gnutls_certificate_allocate_credentials( &ctx->cred )) {
240 #ifdef HAVE_CIPHERSUITES
241 gnutls_priority_init( &ctx->prios, "NORMAL", NULL );
243 #ifdef LDAP_R_COMPILE
244 ldap_pvt_thread_mutex_init( &ctx->ref_mutex );
247 return (tls_ctx *)ctx;
251 tlsg_ctx_ref( tls_ctx *ctx )
253 tlsg_ctx *c = (tlsg_ctx *)ctx;
254 LDAP_MUTEX_LOCK( &c->ref_mutex );
256 LDAP_MUTEX_UNLOCK( &c->ref_mutex );
260 tlsg_ctx_free ( tls_ctx *ctx )
262 tlsg_ctx *c = (tlsg_ctx *)ctx;
267 LDAP_MUTEX_LOCK( &c->ref_mutex );
268 refcount = --c->refcount;
269 LDAP_MUTEX_UNLOCK( &c->ref_mutex );
272 #ifdef HAVE_CIPHERSUITES
273 gnutls_priority_deinit( c->prios );
275 LDAP_FREE( c->kx_list );
277 gnutls_certificate_free_credentials( c->cred );
279 gnutls_dh_params_deinit( c->dh_params );
284 tlsg_getfile( const char *path, gnutls_datum_t *buf )
289 fd = open( path, O_RDONLY );
290 if ( fd >= 0 && fstat( fd, &st ) == 0 ) {
291 buf->size = st.st_size;
292 buf->data = LDAP_MALLOC( st.st_size + 1 );
294 rc = read( fd, buf->data, st.st_size );
296 if ( rc < st.st_size )
305 /* This is the GnuTLS default */
306 #define VERIFY_DEPTH 6
309 * initialize a new TLS context
312 tlsg_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
314 tlsg_ctx *ctx = lo->ldo_tls_ctx;
317 if ( lo->ldo_tls_ciphersuite &&
318 tlsg_parse_ciphers( ctx, lt->lt_ciphersuite )) {
319 Debug( LDAP_DEBUG_ANY,
320 "TLS: could not set cipher list %s.\n",
321 lo->ldo_tls_ciphersuite, 0, 0 );
325 if (lo->ldo_tls_cacertdir != NULL) {
326 Debug( LDAP_DEBUG_ANY,
327 "TLS: warning: cacertdir not implemented for gnutls\n",
331 if (lo->ldo_tls_cacertfile != NULL) {
332 rc = gnutls_certificate_set_x509_trust_file(
335 GNUTLS_X509_FMT_PEM );
336 if ( rc < 0 ) return -1;
339 if ( lo->ldo_tls_certfile && lo->ldo_tls_keyfile ) {
340 gnutls_x509_privkey_t key;
342 gnutls_x509_crt_t certs[VERIFY_DEPTH];
343 unsigned int max = VERIFY_DEPTH;
345 rc = gnutls_x509_privkey_init( &key );
348 /* OpenSSL builds the cert chain for us, but GnuTLS
349 * expects it to be present in the certfile. If it's
350 * not, we have to build it ourselves. So we have to
351 * do some special checks here...
353 rc = tlsg_getfile( lt->lt_keyfile, &buf );
355 rc = gnutls_x509_privkey_import( key, &buf,
356 GNUTLS_X509_FMT_PEM );
357 LDAP_FREE( buf.data );
358 if ( rc < 0 ) return rc;
360 rc = tlsg_getfile( lt->lt_certfile, &buf );
362 rc = gnutls_x509_crt_list_import( certs, &max, &buf,
363 GNUTLS_X509_FMT_PEM, 0 );
364 LDAP_FREE( buf.data );
365 if ( rc < 0 ) return rc;
367 /* If there's only one cert and it's not self-signed,
368 * then we have to build the cert chain.
370 if ( max == 1 && !gnutls_x509_crt_check_issuer( certs[0], certs[0] )) {
371 gnutls_x509_crt_t *cas;
372 unsigned int i, j, ncas;
374 gnutls_certificate_get_x509_cas( ctx->cred, &cas, &ncas );
375 for ( i = 1; i<VERIFY_DEPTH; i++ ) {
376 for ( j = 0; j<ncas; j++ ) {
377 if ( gnutls_x509_crt_check_issuer( certs[i-1], cas[j] )) {
380 /* If this CA is self-signed, we're done */
381 if ( gnutls_x509_crt_check_issuer( cas[j], cas[j] ))
386 /* only continue if we found a CA and it was not self-signed */
391 rc = gnutls_certificate_set_x509_key( ctx->cred, certs, max, key );
393 } else if ( lo->ldo_tls_certfile || lo->ldo_tls_keyfile ) {
394 Debug( LDAP_DEBUG_ANY,
395 "TLS: only one of certfile and keyfile specified\n",
400 if ( lo->ldo_tls_crlfile ) {
401 rc = gnutls_certificate_set_x509_crl_file(
404 GNUTLS_X509_FMT_PEM );
405 if ( rc < 0 ) return -1;
409 /* FIXME: ITS#5992 - this should go be configurable,
410 * and V1 CA certs should be phased out ASAP.
412 gnutls_certificate_set_verify_flags( ctx->cred,
413 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT );
415 if ( is_server && lo->ldo_tls_dhfile ) {
417 rc = tlsg_getfile( lo->ldo_tls_dhfile, &buf );
419 rc = gnutls_dh_params_init(&ctx->dh_params);
421 LDAP_FREE( buf.data );
424 rc = gnutls_dh_params_import_pkcs3( ctx->dh_params, &buf,
425 GNUTLS_X509_FMT_PEM );
426 LDAP_FREE( buf.data );
428 gnutls_certificate_set_dh_params( ctx->cred, ctx->dh_params );
434 tlsg_session_new ( tls_ctx * ctx, int is_server )
436 tlsg_ctx *c = (tlsg_ctx *)ctx;
437 tlsg_session *session;
439 session = ber_memcalloc ( 1, sizeof (*session) );
444 gnutls_init( &session->session, is_server ? GNUTLS_SERVER : GNUTLS_CLIENT );
445 #ifdef HAVE_CIPHERSUITES
446 gnutls_priority_set( session->session, c->prios );
448 gnutls_set_default_priority( session->session );
450 gnutls_kx_set_priority( session->session, c->kx_list );
451 gnutls_cipher_set_priority( session->session, c->cipher_list );
452 gnutls_mac_set_priority( session->session, c->mac_list );
456 gnutls_credentials_set( session->session, GNUTLS_CRD_CERTIFICATE, c->cred );
460 if ( c->lo->ldo_tls_require_cert ) {
461 flag = GNUTLS_CERT_REQUEST;
462 if ( c->lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_DEMAND ||
463 c->lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_HARD )
464 flag = GNUTLS_CERT_REQUIRE;
465 gnutls_certificate_server_set_request( session->session, flag );
468 return (tls_session *)session;
472 tlsg_session_accept( tls_session *session )
474 tlsg_session *s = (tlsg_session *)session;
477 rc = gnutls_handshake( s->session );
478 if ( rc == 0 && s->ctx->lo->ldo_tls_require_cert != LDAP_OPT_X_TLS_NEVER ) {
479 const gnutls_datum_t *peer_cert_list;
480 unsigned int list_size;
482 peer_cert_list = gnutls_certificate_get_peers( s->session,
484 if ( !peer_cert_list && s->ctx->lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_TRY )
487 rc = tlsg_cert_verify( s );
488 if ( rc && s->ctx->lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_ALLOW )
496 tlsg_session_connect( LDAP *ld, tls_session *session )
498 return tlsg_session_accept( session);
502 tlsg_session_upflags( Sockbuf *sb, tls_session *session, int rc )
504 tlsg_session *s = (tlsg_session *)session;
506 if ( rc != GNUTLS_E_INTERRUPTED && rc != GNUTLS_E_AGAIN )
509 switch (gnutls_record_get_direction (s->session)) {
511 sb->sb_trans_needs_read = 1;
514 sb->sb_trans_needs_write = 1;
521 tlsg_session_errmsg( tls_session *sess, int rc, char *buf, size_t len )
523 return (char *)gnutls_strerror( rc );
527 tlsg_x509_cert_dn( struct berval *cert, struct berval *dn, int get_subject )
529 BerElementBuffer berbuf;
530 BerElement *ber = (BerElement *)&berbuf;
535 ber_init2( ber, cert, LBER_USE_DER );
536 tag = ber_skip_tag( ber, &len ); /* Sequence */
537 tag = ber_skip_tag( ber, &len ); /* Sequence */
538 tag = ber_peek_tag( ber, &len ); /* Context + Constructed (version) */
539 if ( tag == 0xa0 ) { /* Version is optional */
540 tag = ber_skip_tag( ber, &len );
541 tag = ber_get_int( ber, &i ); /* Int: Version */
543 tag = ber_skip_tag( ber, &len ); /* Int: Serial (can be longer than ber_int_t) */
544 ber_skip_data( ber, len );
545 tag = ber_skip_tag( ber, &len ); /* Sequence: Signature */
546 ber_skip_data( ber, len );
547 if ( !get_subject ) {
548 tag = ber_peek_tag( ber, &len ); /* Sequence: Issuer DN */
550 tag = ber_skip_tag( ber, &len );
551 ber_skip_data( ber, len );
552 tag = ber_skip_tag( ber, &len ); /* Sequence: Validity */
553 ber_skip_data( ber, len );
554 tag = ber_peek_tag( ber, &len ); /* Sequence: Subject DN */
556 len = ber_ptrlen( ber );
557 dn->bv_val = cert->bv_val + len;
558 dn->bv_len = cert->bv_len - len;
562 tlsg_session_my_dn( tls_session *session, struct berval *der_dn )
564 tlsg_session *s = (tlsg_session *)session;
565 const gnutls_datum_t *x;
568 x = gnutls_certificate_get_ours( s->session );
570 if (!x) return LDAP_INVALID_CREDENTIALS;
572 bv.bv_val = (char *) x->data;
575 tlsg_x509_cert_dn( &bv, der_dn, 1 );
580 tlsg_session_peer_dn( tls_session *session, struct berval *der_dn )
582 tlsg_session *s = (tlsg_session *)session;
583 if ( !s->peer_der_dn.bv_val ) {
584 const gnutls_datum_t *peer_cert_list;
585 unsigned int list_size;
588 peer_cert_list = gnutls_certificate_get_peers( s->session,
590 if ( !peer_cert_list ) return LDAP_INVALID_CREDENTIALS;
592 bv.bv_len = peer_cert_list->size;
593 bv.bv_val = (char *) peer_cert_list->data;
595 tlsg_x509_cert_dn( &bv, &s->peer_der_dn, 1 );
597 *der_dn = s->peer_der_dn;
601 /* what kind of hostname were we given? */
606 #define CN_OID "2.5.4.3"
609 tlsg_session_chkhost( LDAP *ld, tls_session *session, const char *name_in )
611 tlsg_session *s = (tlsg_session *)session;
613 const gnutls_datum_t *peer_cert_list;
614 unsigned int list_size;
615 char altname[NI_MAXHOST];
618 gnutls_x509_crt_t cert;
623 struct in6_addr addr;
627 int len1 = 0, len2 = 0;
630 if( ldap_int_hostname &&
631 ( !name_in || !strcasecmp( name_in, "localhost" ) ) )
633 name = ldap_int_hostname;
638 peer_cert_list = gnutls_certificate_get_peers( s->session,
640 if ( !peer_cert_list ) {
641 Debug( LDAP_DEBUG_ANY,
642 "TLS: unable to get peer certificate.\n",
644 /* If this was a fatal condition, things would have
645 * aborted long before now.
649 ret = gnutls_x509_crt_init( &cert );
651 return LDAP_LOCAL_ERROR;
652 ret = gnutls_x509_crt_import( cert, peer_cert_list, GNUTLS_X509_FMT_DER );
654 gnutls_x509_crt_deinit( cert );
655 return LDAP_LOCAL_ERROR;
659 if (inet_pton(AF_INET6, name, &addr)) {
663 if ((ptr = strrchr(name, '.')) && isdigit((unsigned char)ptr[1])) {
664 if (inet_aton(name, (struct in_addr *)&addr)) ntype = IS_IP4;
667 if (ntype == IS_DNS) {
669 domain = strchr(name, '.');
671 len2 = len1 - (domain-name);
675 for ( i=0, ret=0; ret >= 0; i++ ) {
676 altnamesize = sizeof(altname);
677 ret = gnutls_x509_crt_get_subject_alt_name( cert, i,
678 altname, &altnamesize, NULL );
679 if ( ret < 0 ) break;
682 if ( altnamesize == 0 ) continue;
684 if ( ret == GNUTLS_SAN_DNSNAME ) {
685 if (ntype != IS_DNS) continue;
687 /* Is this an exact match? */
688 if ((len1 == altnamesize) && !strncasecmp(name, altname, len1)) {
692 /* Is this a wildcard match? */
693 if (domain && (altname[0] == '*') && (altname[1] == '.') &&
694 (len2 == altnamesize-1) && !strncasecmp(domain, &altname[1], len2))
698 } else if ( ret == GNUTLS_SAN_IPADDRESS ) {
699 if (ntype == IS_DNS) continue;
702 if (ntype == IS_IP6 && altnamesize != sizeof(struct in6_addr)) {
706 if (ntype == IS_IP4 && altnamesize != sizeof(struct in_addr)) {
709 if (!memcmp(altname, &addr, altnamesize)) {
717 /* find the last CN */
721 ret = gnutls_x509_crt_get_dn_by_oid( cert, CN_OID,
722 i, 1, altname, &altnamesize );
723 if ( ret == GNUTLS_E_SHORT_MEMORY_BUFFER )
730 altnamesize = sizeof(altname);
731 ret = gnutls_x509_crt_get_dn_by_oid( cert, CN_OID,
732 i-1, 0, altname, &altnamesize );
736 Debug( LDAP_DEBUG_ANY,
737 "TLS: unable to get common name from peer certificate.\n",
739 ret = LDAP_CONNECT_ERROR;
740 if ( ld->ld_error ) {
741 LDAP_FREE( ld->ld_error );
743 ld->ld_error = LDAP_STRDUP(
744 _("TLS: unable to get CN from peer certificate"));
747 ret = LDAP_LOCAL_ERROR;
748 if ( !len1 ) len1 = strlen( name );
749 if ( len1 == altnamesize && strncasecmp(name, altname, altnamesize) == 0 ) {
752 } else if (( altname[0] == '*' ) && ( altname[1] == '.' )) {
753 /* Is this a wildcard match? */
755 (len2 == altnamesize-1) && !strncasecmp(domain, &altname[1], len2)) {
761 if( ret == LDAP_LOCAL_ERROR ) {
762 altname[altnamesize] = '\0';
763 Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
764 "common name in certificate (%s).\n",
766 ret = LDAP_CONNECT_ERROR;
767 if ( ld->ld_error ) {
768 LDAP_FREE( ld->ld_error );
770 ld->ld_error = LDAP_STRDUP(
771 _("TLS: hostname does not match CN in peer certificate"));
774 gnutls_x509_crt_deinit( cert );
779 tlsg_session_strength( tls_session *session )
781 tlsg_session *s = (tlsg_session *)session;
782 gnutls_cipher_algorithm_t c;
784 c = gnutls_cipher_get( s->session );
785 return gnutls_cipher_get_key_size( c ) * 8;
789 tlsg_session_unique( tls_session *sess, struct berval *buf, int is_server)
794 /* suites is a string of colon-separated cipher suite names. */
796 tlsg_parse_ciphers( tlsg_ctx *ctx, char *suites )
798 #ifdef HAVE_CIPHERSUITES
800 return gnutls_priority_init( &ctx->prios, suites, &err );
804 int *list, nkx = 0, ncipher = 0, nmac = 0;
805 int *kx, *cipher, *mac;
810 end = strchr(ptr, ':');
815 for (i=0; i<tlsg_n_ciphers; i++) {
816 if ( !strncasecmp( tlsg_ciphers[i].name, ptr, len )) {
821 if ( i == tlsg_n_ciphers ) {
822 /* unrecognized cipher suite */
828 /* Space for all 3 lists */
829 list = LDAP_MALLOC( (num+1) * sizeof(int) * 3 );
838 end = strchr(ptr, ':');
843 for (i=0; i<tlsg_n_ciphers; i++) {
844 /* For each cipher suite, insert its algorithms into
845 * their respective priority lists. Make sure they
846 * only appear once in each list.
848 if ( !strncasecmp( tlsg_ciphers[i].name, ptr, len )) {
849 for (j=0; j<nkx; j++)
850 if ( kx[j] == tlsg_ciphers[i].kx )
853 kx[nkx++] = tlsg_ciphers[i].kx;
854 for (j=0; j<ncipher; j++)
855 if ( cipher[j] == tlsg_ciphers[i].cipher )
858 cipher[ncipher++] = tlsg_ciphers[i].cipher;
859 for (j=0; j<nmac; j++)
860 if ( mac[j] == tlsg_ciphers[i].mac )
863 mac[nmac++] = tlsg_ciphers[i].mac;
873 ctx->cipher_list = cipher;
880 * TLS support for LBER Sockbufs
884 tlsg_session *session;
885 Sockbuf_IO_Desc *sbiod;
889 tlsg_recv( gnutls_transport_ptr_t ptr, void *buf, size_t len )
893 if ( buf == NULL || len <= 0 ) return 0;
895 p = (struct tls_data *)ptr;
897 if ( p == NULL || p->sbiod == NULL ) {
901 return LBER_SBIOD_READ_NEXT( p->sbiod, buf, len );
905 tlsg_send( gnutls_transport_ptr_t ptr, const void *buf, size_t len )
909 if ( buf == NULL || len <= 0 ) return 0;
911 p = (struct tls_data *)ptr;
913 if ( p == NULL || p->sbiod == NULL ) {
917 return LBER_SBIOD_WRITE_NEXT( p->sbiod, (char *)buf, len );
921 tlsg_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg )
924 tlsg_session *session = arg;
926 assert( sbiod != NULL );
928 p = LBER_MALLOC( sizeof( *p ) );
933 gnutls_transport_set_ptr( session->session, (gnutls_transport_ptr)p );
934 gnutls_transport_set_pull_function( session->session, tlsg_recv );
935 gnutls_transport_set_push_function( session->session, tlsg_send );
936 p->session = session;
938 sbiod->sbiod_pvt = p;
943 tlsg_sb_remove( Sockbuf_IO_Desc *sbiod )
947 assert( sbiod != NULL );
948 assert( sbiod->sbiod_pvt != NULL );
950 p = (struct tls_data *)sbiod->sbiod_pvt;
951 gnutls_deinit ( p->session->session );
952 LBER_FREE( p->session );
953 LBER_FREE( sbiod->sbiod_pvt );
954 sbiod->sbiod_pvt = NULL;
959 tlsg_sb_close( Sockbuf_IO_Desc *sbiod )
963 assert( sbiod != NULL );
964 assert( sbiod->sbiod_pvt != NULL );
966 p = (struct tls_data *)sbiod->sbiod_pvt;
967 gnutls_bye ( p->session->session, GNUTLS_SHUT_WR );
972 tlsg_sb_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
976 assert( sbiod != NULL );
977 assert( sbiod->sbiod_pvt != NULL );
979 p = (struct tls_data *)sbiod->sbiod_pvt;
981 if ( opt == LBER_SB_OPT_GET_SSL ) {
982 *((tlsg_session **)arg) = p->session;
985 } else if ( opt == LBER_SB_OPT_DATA_READY ) {
986 if( gnutls_record_check_pending( p->session->session ) > 0 ) {
991 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
995 tlsg_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
1000 assert( sbiod != NULL );
1001 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
1003 p = (struct tls_data *)sbiod->sbiod_pvt;
1005 ret = gnutls_record_recv ( p->session->session, buf, len );
1007 case GNUTLS_E_INTERRUPTED:
1008 case GNUTLS_E_AGAIN:
1009 sbiod->sbiod_sb->sb_trans_needs_read = 1;
1010 sock_errset(EWOULDBLOCK);
1013 case GNUTLS_E_REHANDSHAKE:
1014 for ( ret = gnutls_handshake ( p->session->session );
1015 ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN;
1016 ret = gnutls_handshake ( p->session->session ) );
1017 sbiod->sbiod_sb->sb_trans_needs_read = 1;
1021 sbiod->sbiod_sb->sb_trans_needs_read = 0;
1027 tlsg_sb_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
1032 assert( sbiod != NULL );
1033 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
1035 p = (struct tls_data *)sbiod->sbiod_pvt;
1037 ret = gnutls_record_send ( p->session->session, (char *)buf, len );
1039 if ( ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN ) {
1040 sbiod->sbiod_sb->sb_trans_needs_write = 1;
1041 sock_errset(EWOULDBLOCK);
1044 sbiod->sbiod_sb->sb_trans_needs_write = 0;
1049 static Sockbuf_IO tlsg_sbio =
1051 tlsg_sb_setup, /* sbi_setup */
1052 tlsg_sb_remove, /* sbi_remove */
1053 tlsg_sb_ctrl, /* sbi_ctrl */
1054 tlsg_sb_read, /* sbi_read */
1055 tlsg_sb_write, /* sbi_write */
1056 tlsg_sb_close /* sbi_close */
1059 /* Certs are not automatically varified during the handshake */
1061 tlsg_cert_verify( tlsg_session *ssl )
1063 unsigned int status = 0;
1065 time_t now = time(0);
1068 err = gnutls_certificate_verify_peers2( ssl->session, &status );
1070 Debug( LDAP_DEBUG_ANY,"TLS: gnutls_certificate_verify_peers2 failed %d\n",
1075 Debug( LDAP_DEBUG_TRACE,"TLS: peer cert untrusted or revoked (0x%x)\n",
1079 peertime = gnutls_certificate_expiration_time_peers( ssl->session );
1080 if ( peertime == (time_t) -1 ) {
1081 Debug( LDAP_DEBUG_ANY, "TLS: gnutls_certificate_expiration_time_peers failed\n",
1085 if ( peertime < now ) {
1086 Debug( LDAP_DEBUG_ANY, "TLS: peer certificate is expired\n",
1090 peertime = gnutls_certificate_activation_time_peers( ssl->session );
1091 if ( peertime == (time_t) -1 ) {
1092 Debug( LDAP_DEBUG_ANY, "TLS: gnutls_certificate_activation_time_peers failed\n",
1096 if ( peertime > now ) {
1097 Debug( LDAP_DEBUG_ANY, "TLS: peer certificate not yet active\n",
1104 tls_impl ldap_int_tls_impl = {
1116 tlsg_session_connect,
1117 tlsg_session_accept,
1118 tlsg_session_upflags,
1119 tlsg_session_errmsg,
1121 tlsg_session_peer_dn,
1122 tlsg_session_chkhost,
1123 tlsg_session_strength,
1124 tlsg_session_unique,
1128 #ifdef LDAP_R_COMPILE
1137 #endif /* HAVE_GNUTLS */