1 /* tls.c - Handle tls/ssl. */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-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: restructured by Howard Chu.
20 #include "ldap_config.h"
24 #include <ac/stdlib.h>
26 #include <ac/socket.h>
27 #include <ac/string.h>
30 #include <ac/unistd.h>
32 #include <ac/dirent.h>
40 static tls_impl *tls_imp = &ldap_int_tls_impl;
41 #define HAS_TLS( sb ) ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO, \
42 (void *)tls_imp->ti_sbio )
47 #define LDAP_USE_NON_BLOCKING_TLS
48 #endif /* LDAP_DEVEL */
50 /* RFC2459 minimum required set of supported attribute types
53 typedef struct oid_name {
58 static oid_name oids[] = {
59 { BER_BVC("2.5.4.3"), BER_BVC("cn") },
60 { BER_BVC("2.5.4.4"), BER_BVC("sn") },
61 { BER_BVC("2.5.4.6"), BER_BVC("c") },
62 { BER_BVC("2.5.4.7"), BER_BVC("l") },
63 { BER_BVC("2.5.4.8"), BER_BVC("st") },
64 { BER_BVC("2.5.4.10"), BER_BVC("o") },
65 { BER_BVC("2.5.4.11"), BER_BVC("ou") },
66 { BER_BVC("2.5.4.12"), BER_BVC("title") },
67 { BER_BVC("2.5.4.41"), BER_BVC("name") },
68 { BER_BVC("2.5.4.42"), BER_BVC("givenName") },
69 { BER_BVC("2.5.4.43"), BER_BVC("initials") },
70 { BER_BVC("2.5.4.44"), BER_BVC("generationQualifier") },
71 { BER_BVC("2.5.4.46"), BER_BVC("dnQualifier") },
72 { BER_BVC("1.2.840.113549.1.9.1"), BER_BVC("email") },
73 { BER_BVC("0.9.2342.19200300.100.1.25"), BER_BVC("dc") },
74 { BER_BVNULL, BER_BVNULL }
80 ldap_pvt_tls_ctx_free ( void *c )
83 tls_imp->ti_ctx_free( c );
87 tls_ctx_ref( tls_ctx *ctx )
91 tls_imp->ti_ctx_ref( ctx );
96 * an extra mutex for the default ctx.
98 static ldap_pvt_thread_mutex_t tls_def_ctx_mutex;
102 ldap_int_tls_destroy( struct ldapoptions *lo )
104 if ( lo->ldo_tls_ctx ) {
105 ldap_pvt_tls_ctx_free( lo->ldo_tls_ctx );
106 lo->ldo_tls_ctx = NULL;
109 if ( lo->ldo_tls_certfile ) {
110 LDAP_FREE( lo->ldo_tls_certfile );
111 lo->ldo_tls_certfile = NULL;
113 if ( lo->ldo_tls_keyfile ) {
114 LDAP_FREE( lo->ldo_tls_keyfile );
115 lo->ldo_tls_keyfile = NULL;
117 if ( lo->ldo_tls_dhfile ) {
118 LDAP_FREE( lo->ldo_tls_dhfile );
119 lo->ldo_tls_dhfile = NULL;
121 if ( lo->ldo_tls_cacertfile ) {
122 LDAP_FREE( lo->ldo_tls_cacertfile );
123 lo->ldo_tls_cacertfile = NULL;
125 if ( lo->ldo_tls_cacertdir ) {
126 LDAP_FREE( lo->ldo_tls_cacertdir );
127 lo->ldo_tls_cacertdir = NULL;
129 if ( lo->ldo_tls_ciphersuite ) {
130 LDAP_FREE( lo->ldo_tls_ciphersuite );
131 lo->ldo_tls_ciphersuite = NULL;
133 if ( lo->ldo_tls_crlfile ) {
134 LDAP_FREE( lo->ldo_tls_crlfile );
135 lo->ldo_tls_crlfile = NULL;
140 * Tear down the TLS subsystem. Should only be called once.
143 ldap_pvt_tls_destroy( void )
145 struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();
147 ldap_int_tls_destroy( lo );
149 tls_imp->ti_tls_destroy();
153 * Initialize a particular TLS implementation.
154 * Called once per implementation.
157 tls_init(tls_impl *impl )
159 static int tls_initialized = 0;
161 if ( !tls_initialized++ ) {
162 #ifdef LDAP_R_COMPILE
163 ldap_pvt_thread_mutex_init( &tls_def_ctx_mutex );
167 if ( impl->ti_inited++ ) return 0;
169 #ifdef LDAP_R_COMPILE
172 return impl->ti_tls_init();
176 * Initialize TLS subsystem. Called once per implementation.
179 ldap_pvt_tls_init( void )
181 return tls_init( tls_imp );
185 * initialize a new TLS context
188 ldap_int_tls_init_ctx( struct ldapoptions *lo, int is_server )
191 tls_impl *ti = tls_imp;
192 struct ldaptls lts = lo->ldo_tls_info;
194 if ( lo->ldo_tls_ctx )
199 if ( is_server && !lts.lt_certfile && !lts.lt_keyfile &&
200 !lts.lt_cacertfile && !lts.lt_cacertdir ) {
201 /* minimum configuration not provided */
202 return LDAP_NOT_SUPPORTED;
206 /* This ASCII/EBCDIC handling is a real pain! */
207 if ( lts.lt_ciphersuite ) {
208 lts.lt_ciphersuite = LDAP_STRDUP( lts.lt_ciphersuite );
209 __atoe( lts.lt_ciphersuite );
211 if ( lts.lt_cacertfile ) {
212 lts.lt_cacertfile = LDAP_STRDUP( lts.lt_cacertfile );
213 __atoe( lts.lt_cacertfile );
215 if ( lts.lt_certfile ) {
216 lts.lt_certfile = LDAP_STRDUP( lts.lt_certfile );
217 __atoe( lts.lt_certfile );
219 if ( lts.lt_keyfile ) {
220 lts.lt_keyfile = LDAP_STRDUP( lts.lt_keyfile );
221 __atoe( lts.lt_keyfile );
223 if ( lts.lt_crlfile ) {
224 lts.lt_crlfile = LDAP_STRDUP( lts.lt_crlfile );
225 __atoe( lts.lt_crlfile );
227 if ( lts.lt_cacertdir ) {
228 lts.lt_cacertdir = LDAP_STRDUP( lts.lt_cacertdir );
229 __atoe( lts.lt_cacertdir );
231 if ( lts.lt_dhfile ) {
232 lts.lt_dhfile = LDAP_STRDUP( lts.lt_dhfile );
233 __atoe( lts.lt_dhfile );
236 lo->ldo_tls_ctx = ti->ti_ctx_new( lo );
237 if ( lo->ldo_tls_ctx == NULL ) {
238 Debug( LDAP_DEBUG_ANY,
239 "TLS: could not allocate default ctx.\n",
245 rc = ti->ti_ctx_init( lo, <s, is_server );
248 if ( rc < 0 && lo->ldo_tls_ctx != NULL ) {
249 ldap_pvt_tls_ctx_free( lo->ldo_tls_ctx );
250 lo->ldo_tls_ctx = NULL;
253 LDAP_FREE( lts.lt_ciphersuite );
254 LDAP_FREE( lts.lt_cacertfile );
255 LDAP_FREE( lts.lt_certfile );
256 LDAP_FREE( lts.lt_keyfile );
257 LDAP_FREE( lts.lt_crlfile );
258 LDAP_FREE( lts.lt_cacertdir );
259 LDAP_FREE( lts.lt_dhfile );
265 * initialize the default context
268 ldap_pvt_tls_init_def_ctx( int is_server )
270 struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();
272 LDAP_MUTEX_LOCK( &tls_def_ctx_mutex );
273 rc = ldap_int_tls_init_ctx( lo, is_server );
274 LDAP_MUTEX_UNLOCK( &tls_def_ctx_mutex );
279 alloc_handle( void *ctx_arg, int is_server )
287 struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();
288 if ( ldap_pvt_tls_init_def_ctx( is_server ) < 0 ) return NULL;
289 ctx = lo->ldo_tls_ctx;
292 ssl = tls_imp->ti_session_new( ctx, is_server );
294 Debug( LDAP_DEBUG_ANY,"TLS: can't create ssl handle.\n",0,0,0);
301 update_flags( Sockbuf *sb, tls_session * ssl, int rc )
303 sb->sb_trans_needs_read = 0;
304 sb->sb_trans_needs_write = 0;
306 return tls_imp->ti_session_upflags( sb, ssl, rc );
310 * Call this to do a TLS connect on a sockbuf. ctx_arg can be
311 * a SSL_CTX * or NULL, in which case the default ctx is used.
315 * 0 - Success. Connection is ready for communication.
316 * <0 - Error. Can't create a TLS stream.
317 * >0 - Partial success.
318 * Do a select (using information from lber_pvt_sb_needs_{read,write}
323 ldap_int_tls_connect( LDAP *ld, LDAPConn *conn )
325 Sockbuf *sb = conn->lconn_sb;
327 tls_session *ssl = NULL;
329 if ( HAS_TLS( sb )) {
330 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_SSL, (void *)&ssl );
332 struct ldapoptions *lo;
335 ctx = ld->ld_options.ldo_tls_ctx;
337 ssl = alloc_handle( ctx, 0 );
339 if ( ssl == NULL ) return -1;
342 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
343 LBER_SBIOD_LEVEL_TRANSPORT, (void *)"tls_" );
345 ber_sockbuf_add_io( sb, tls_imp->ti_sbio,
346 LBER_SBIOD_LEVEL_TRANSPORT, (void *)ssl );
348 lo = LDAP_INT_GLOBAL_OPT();
350 ctx = lo->ldo_tls_ctx;
351 ld->ld_options.ldo_tls_ctx = ctx;
354 if ( ld->ld_options.ldo_tls_connect_cb )
355 ld->ld_options.ldo_tls_connect_cb( ld, ssl, ctx,
356 ld->ld_options.ldo_tls_connect_arg );
357 if ( lo && lo->ldo_tls_connect_cb && lo->ldo_tls_connect_cb !=
358 ld->ld_options.ldo_tls_connect_cb )
359 lo->ldo_tls_connect_cb( ld, ssl, ctx, lo->ldo_tls_connect_arg );
362 err = tls_imp->ti_session_connect( ld, ssl );
365 errno = WSAGetLastError();
371 if ( update_flags( sb, ssl, err )) {
375 msg = tls_imp->ti_session_errmsg( ssl, err, buf, sizeof(buf) );
377 if ( ld->ld_error ) {
378 LDAP_FREE( ld->ld_error );
380 ld->ld_error = LDAP_STRDUP( msg );
382 if ( ld->ld_error ) __etoa(ld->ld_error);
386 Debug( LDAP_DEBUG_ANY,"TLS: can't connect: %s.\n",
387 ld->ld_error ? ld->ld_error : "" ,0,0);
389 ber_sockbuf_remove_io( sb, tls_imp->ti_sbio,
390 LBER_SBIOD_LEVEL_TRANSPORT );
392 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
393 LBER_SBIOD_LEVEL_TRANSPORT );
402 * Call this to do a TLS accept on a sockbuf.
403 * Everything else is the same as with tls_connect.
406 ldap_pvt_tls_accept( Sockbuf *sb, void *ctx_arg )
409 tls_session *ssl = NULL;
411 if ( HAS_TLS( sb )) {
412 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_SSL, (void *)&ssl );
414 ssl = alloc_handle( ctx_arg, 1 );
415 if ( ssl == NULL ) return -1;
418 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
419 LBER_SBIOD_LEVEL_TRANSPORT, (void *)"tls_" );
421 ber_sockbuf_add_io( sb, tls_imp->ti_sbio,
422 LBER_SBIOD_LEVEL_TRANSPORT, (void *)ssl );
425 err = tls_imp->ti_session_accept( ssl );
428 errno = WSAGetLastError();
433 if ( update_flags( sb, ssl, err )) return 1;
435 if ( DebugTest( LDAP_DEBUG_ANY ) ) {
437 msg = tls_imp->ti_session_errmsg( ssl, err, buf, sizeof(buf) );
438 Debug( LDAP_DEBUG_ANY,"TLS: can't accept: %s.\n",
439 msg ? msg : "(unknown)", 0, 0 );
442 ber_sockbuf_remove_io( sb, tls_imp->ti_sbio,
443 LBER_SBIOD_LEVEL_TRANSPORT );
445 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
446 LBER_SBIOD_LEVEL_TRANSPORT );
454 ldap_pvt_tls_inplace ( Sockbuf *sb )
456 return HAS_TLS( sb ) ? 1 : 0;
460 ldap_tls_inplace( LDAP *ld )
464 if ( ld->ld_defconn && ld->ld_defconn->lconn_sb ) {
465 sb = ld->ld_defconn->lconn_sb;
467 } else if ( ld->ld_sb ) {
474 return ldap_pvt_tls_inplace( sb );
478 ldap_pvt_tls_get_peer_dn( void *s, struct berval *dn,
479 LDAPDN_rewrite_dummy *func, unsigned flags )
481 tls_session *session = s;
485 rc = tls_imp->ti_session_peer_dn( session, &bvdn );
488 rc = ldap_X509dn2bv( &bvdn, dn,
489 (LDAPDN_rewrite_func *)func, flags);
494 ldap_pvt_tls_check_hostname( LDAP *ld, void *s, const char *name_in )
496 tls_session *session = s;
498 return tls_imp->ti_session_chkhost( ld, session, name_in );
502 ldap_pvt_tls_config( LDAP *ld, int option, const char *arg )
507 case LDAP_OPT_X_TLS_CACERTFILE:
508 case LDAP_OPT_X_TLS_CACERTDIR:
509 case LDAP_OPT_X_TLS_CERTFILE:
510 case LDAP_OPT_X_TLS_KEYFILE:
511 case LDAP_OPT_X_TLS_RANDOM_FILE:
512 case LDAP_OPT_X_TLS_CIPHER_SUITE:
513 case LDAP_OPT_X_TLS_DHFILE:
514 case LDAP_OPT_X_TLS_CRLFILE: /* GnuTLS only */
515 return ldap_pvt_tls_set_option( ld, option, (void *) arg );
517 case LDAP_OPT_X_TLS_REQUIRE_CERT:
520 if ( strcasecmp( arg, "never" ) == 0 ) {
521 i = LDAP_OPT_X_TLS_NEVER ;
523 } else if ( strcasecmp( arg, "demand" ) == 0 ) {
524 i = LDAP_OPT_X_TLS_DEMAND ;
526 } else if ( strcasecmp( arg, "allow" ) == 0 ) {
527 i = LDAP_OPT_X_TLS_ALLOW ;
529 } else if ( strcasecmp( arg, "try" ) == 0 ) {
530 i = LDAP_OPT_X_TLS_TRY ;
532 } else if ( ( strcasecmp( arg, "hard" ) == 0 ) ||
533 ( strcasecmp( arg, "on" ) == 0 ) ||
534 ( strcasecmp( arg, "yes" ) == 0) ||
535 ( strcasecmp( arg, "true" ) == 0 ) )
537 i = LDAP_OPT_X_TLS_HARD ;
541 return ldap_pvt_tls_set_option( ld, option, &i );
544 case LDAP_OPT_X_TLS_PROTOCOL_MIN: {
547 l = strtol( arg, &next, 10 );
548 if ( l < 0 || l > 0xff || next == arg ||
549 ( *next != '\0' && *next != '.' ) )
554 l = strtol( arg, &next, 10 );
555 if ( l < 0 || l > 0xff || next == arg || *next != '\0' )
559 return ldap_pvt_tls_set_option( ld, option, &i );
561 #ifdef HAVE_OPENSSL_CRL
562 case LDAP_OPT_X_TLS_CRLCHECK: /* OpenSSL only */
564 if ( strcasecmp( arg, "none" ) == 0 ) {
565 i = LDAP_OPT_X_TLS_CRL_NONE ;
566 } else if ( strcasecmp( arg, "peer" ) == 0 ) {
567 i = LDAP_OPT_X_TLS_CRL_PEER ;
568 } else if ( strcasecmp( arg, "all" ) == 0 ) {
569 i = LDAP_OPT_X_TLS_CRL_ALL ;
572 return ldap_pvt_tls_set_option( ld, option, &i );
581 ldap_pvt_tls_get_option( LDAP *ld, int option, void *arg )
583 struct ldapoptions *lo;
585 if( option == LDAP_OPT_X_TLS_PACKAGE ) {
586 *(char **)arg = LDAP_STRDUP( tls_imp->ti_name );
591 assert( LDAP_VALID( ld ) );
593 if( !LDAP_VALID( ld ) ) {
594 return LDAP_OPT_ERROR;
597 lo = &ld->ld_options;
600 /* Get pointer to global option structure */
601 lo = LDAP_INT_GLOBAL_OPT();
603 return LDAP_NO_MEMORY;
609 *(int *)arg = lo->ldo_tls_mode;
611 case LDAP_OPT_X_TLS_CTX:
612 *(void **)arg = lo->ldo_tls_ctx;
613 if ( lo->ldo_tls_ctx ) {
614 tls_ctx_ref( lo->ldo_tls_ctx );
617 case LDAP_OPT_X_TLS_CACERTFILE:
618 *(char **)arg = lo->ldo_tls_cacertfile ?
619 LDAP_STRDUP( lo->ldo_tls_cacertfile ) : NULL;
621 case LDAP_OPT_X_TLS_CACERTDIR:
622 *(char **)arg = lo->ldo_tls_cacertdir ?
623 LDAP_STRDUP( lo->ldo_tls_cacertdir ) : NULL;
625 case LDAP_OPT_X_TLS_CERTFILE:
626 *(char **)arg = lo->ldo_tls_certfile ?
627 LDAP_STRDUP( lo->ldo_tls_certfile ) : NULL;
629 case LDAP_OPT_X_TLS_KEYFILE:
630 *(char **)arg = lo->ldo_tls_keyfile ?
631 LDAP_STRDUP( lo->ldo_tls_keyfile ) : NULL;
633 case LDAP_OPT_X_TLS_DHFILE:
634 *(char **)arg = lo->ldo_tls_dhfile ?
635 LDAP_STRDUP( lo->ldo_tls_dhfile ) : NULL;
637 case LDAP_OPT_X_TLS_CRLFILE: /* GnuTLS only */
638 *(char **)arg = lo->ldo_tls_crlfile ?
639 LDAP_STRDUP( lo->ldo_tls_crlfile ) : NULL;
641 case LDAP_OPT_X_TLS_REQUIRE_CERT:
642 *(int *)arg = lo->ldo_tls_require_cert;
644 #ifdef HAVE_OPENSSL_CRL
645 case LDAP_OPT_X_TLS_CRLCHECK: /* OpenSSL only */
646 *(int *)arg = lo->ldo_tls_crlcheck;
649 case LDAP_OPT_X_TLS_CIPHER_SUITE:
650 *(char **)arg = lo->ldo_tls_ciphersuite ?
651 LDAP_STRDUP( lo->ldo_tls_ciphersuite ) : NULL;
653 case LDAP_OPT_X_TLS_PROTOCOL_MIN:
654 *(int *)arg = lo->ldo_tls_protocol_min;
656 case LDAP_OPT_X_TLS_RANDOM_FILE:
657 *(char **)arg = lo->ldo_tls_randfile ?
658 LDAP_STRDUP( lo->ldo_tls_randfile ) : NULL;
660 case LDAP_OPT_X_TLS_SSL_CTX: {
663 LDAPConn *conn = ld->ld_defconn;
664 if ( conn != NULL ) {
665 Sockbuf *sb = conn->lconn_sb;
666 retval = ldap_pvt_tls_sb_ctx( sb );
669 *(void **)arg = retval;
672 case LDAP_OPT_X_TLS_CONNECT_CB:
673 *(LDAP_TLS_CONNECT_CB **)arg = lo->ldo_tls_connect_cb;
675 case LDAP_OPT_X_TLS_CONNECT_ARG:
676 *(void **)arg = lo->ldo_tls_connect_arg;
685 ldap_pvt_tls_set_option( LDAP *ld, int option, void *arg )
687 struct ldapoptions *lo;
690 assert( LDAP_VALID( ld ) );
692 if( !LDAP_VALID( ld ) ) {
693 return LDAP_OPT_ERROR;
696 lo = &ld->ld_options;
699 /* Get pointer to global option structure */
700 lo = LDAP_INT_GLOBAL_OPT();
702 return LDAP_NO_MEMORY;
708 if ( !arg ) return -1;
710 switch( *(int *) arg ) {
711 case LDAP_OPT_X_TLS_NEVER:
712 case LDAP_OPT_X_TLS_DEMAND:
713 case LDAP_OPT_X_TLS_ALLOW:
714 case LDAP_OPT_X_TLS_TRY:
715 case LDAP_OPT_X_TLS_HARD:
717 lo->ldo_tls_mode = *(int *)arg;
724 case LDAP_OPT_X_TLS_CTX:
725 if ( lo->ldo_tls_ctx )
726 ldap_pvt_tls_ctx_free( lo->ldo_tls_ctx );
727 lo->ldo_tls_ctx = arg;
728 tls_ctx_ref( lo->ldo_tls_ctx );
730 case LDAP_OPT_X_TLS_CONNECT_CB:
731 lo->ldo_tls_connect_cb = (LDAP_TLS_CONNECT_CB *)arg;
733 case LDAP_OPT_X_TLS_CONNECT_ARG:
734 lo->ldo_tls_connect_arg = arg;
736 case LDAP_OPT_X_TLS_CACERTFILE:
737 if ( lo->ldo_tls_cacertfile ) LDAP_FREE( lo->ldo_tls_cacertfile );
738 lo->ldo_tls_cacertfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
740 case LDAP_OPT_X_TLS_CACERTDIR:
741 if ( lo->ldo_tls_cacertdir ) LDAP_FREE( lo->ldo_tls_cacertdir );
742 lo->ldo_tls_cacertdir = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
744 case LDAP_OPT_X_TLS_CERTFILE:
745 if ( lo->ldo_tls_certfile ) LDAP_FREE( lo->ldo_tls_certfile );
746 lo->ldo_tls_certfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
748 case LDAP_OPT_X_TLS_KEYFILE:
749 if ( lo->ldo_tls_keyfile ) LDAP_FREE( lo->ldo_tls_keyfile );
750 lo->ldo_tls_keyfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
752 case LDAP_OPT_X_TLS_DHFILE:
753 if ( lo->ldo_tls_dhfile ) LDAP_FREE( lo->ldo_tls_dhfile );
754 lo->ldo_tls_dhfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
756 case LDAP_OPT_X_TLS_CRLFILE: /* GnuTLS only */
757 if ( lo->ldo_tls_crlfile ) LDAP_FREE( lo->ldo_tls_crlfile );
758 lo->ldo_tls_crlfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
760 case LDAP_OPT_X_TLS_REQUIRE_CERT:
761 if ( !arg ) return -1;
762 switch( *(int *) arg ) {
763 case LDAP_OPT_X_TLS_NEVER:
764 case LDAP_OPT_X_TLS_DEMAND:
765 case LDAP_OPT_X_TLS_ALLOW:
766 case LDAP_OPT_X_TLS_TRY:
767 case LDAP_OPT_X_TLS_HARD:
768 lo->ldo_tls_require_cert = * (int *) arg;
772 #ifdef HAVE_OPENSSL_CRL
773 case LDAP_OPT_X_TLS_CRLCHECK: /* OpenSSL only */
774 if ( !arg ) return -1;
775 switch( *(int *) arg ) {
776 case LDAP_OPT_X_TLS_CRL_NONE:
777 case LDAP_OPT_X_TLS_CRL_PEER:
778 case LDAP_OPT_X_TLS_CRL_ALL:
779 lo->ldo_tls_crlcheck = * (int *) arg;
784 case LDAP_OPT_X_TLS_CIPHER_SUITE:
785 if ( lo->ldo_tls_ciphersuite ) LDAP_FREE( lo->ldo_tls_ciphersuite );
786 lo->ldo_tls_ciphersuite = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
789 case LDAP_OPT_X_TLS_PROTOCOL_MIN:
790 if ( !arg ) return -1;
791 lo->ldo_tls_protocol_min = *(int *)arg;
793 case LDAP_OPT_X_TLS_RANDOM_FILE:
796 if ( lo->ldo_tls_randfile ) LDAP_FREE (lo->ldo_tls_randfile );
797 lo->ldo_tls_randfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
799 case LDAP_OPT_X_TLS_NEWCTX:
800 if ( !arg ) return -1;
801 if ( lo->ldo_tls_ctx )
802 ldap_pvt_tls_ctx_free( lo->ldo_tls_ctx );
803 lo->ldo_tls_ctx = NULL;
804 return ldap_int_tls_init_ctx( lo, *(int *)arg );
812 ldap_int_tls_start ( LDAP *ld, LDAPConn *conn, LDAPURLDesc *srv )
818 #ifdef LDAP_USE_NON_BLOCKING_TLS
819 struct timeval start_time_tv, tv, tv0;
820 ber_socket_t sd = AC_SOCKET_ERROR;
821 #endif /* LDAP_USE_NON_BLOCKING_TLS */
824 return LDAP_PARAM_ERROR;
828 host = srv->lud_host;
830 host = conn->lconn_server->lud_host;
833 /* avoid NULL host */
838 (void) tls_init( tls_imp );
840 #ifdef LDAP_USE_NON_BLOCKING_TLS
842 * Use non-blocking io during SSL Handshake when a timeout is configured
844 if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
845 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_SET_NONBLOCK, sb );
846 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
847 tv = ld->ld_options.ldo_tm_net;
849 #ifdef HAVE_GETTIMEOFDAY
850 gettimeofday( &start_time_tv, NULL );
851 #else /* ! HAVE_GETTIMEOFDAY */
852 time( &start_time_tv.tv_sec );
853 start_time_tv.tv_usec = 0;
854 #endif /* ! HAVE_GETTIMEOFDAY */
857 #endif /* LDAP_USE_NON_BLOCKING_TLS */
859 ld->ld_errno = LDAP_SUCCESS;
860 ret = ldap_int_tls_connect( ld, conn );
862 #ifdef LDAP_USE_NON_BLOCKING_TLS
863 while ( ret > 0 ) { /* this should only happen for non-blocking io */
866 if ( sb->sb_trans_needs_read ) {
868 } else if ( sb->sb_trans_needs_write ) {
871 Debug( LDAP_DEBUG_TRACE, "ldap_int_tls_start: ldap_int_tls_connect needs %s\n",
872 wr ? "write": "read", 0, 0);
874 ret = ldap_int_poll( ld, sd, &tv, wr);
876 ld->ld_errno = LDAP_TIMEOUT;
879 /* ldap_int_poll called ldap_pvt_ndelay_off */
880 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_SET_NONBLOCK, sb );
881 ret = ldap_int_tls_connect( ld, conn );
882 if ( ret > 0 ) { /* need to call tls_connect once more */
883 struct timeval curr_time_tv, delta_tv;
885 /* This is mostly copied from result.c:wait4msg(), should
886 * probably be moved into a separate function */
887 #ifdef HAVE_GETTIMEOFDAY
888 gettimeofday( &curr_time_tv, NULL );
889 #else /* ! HAVE_GETTIMEOFDAY */
890 time( &curr_time_tv.tv_sec );
891 curr_time_tv.tv_usec = 0;
892 #endif /* ! HAVE_GETTIMEOFDAY */
894 /* delta = curr - start */
895 delta_tv.tv_sec = curr_time_tv.tv_sec - start_time_tv.tv_sec;
896 delta_tv.tv_usec = curr_time_tv.tv_usec - start_time_tv.tv_usec;
897 if ( delta_tv.tv_usec < 0 ) {
899 delta_tv.tv_usec += 1000000;
903 if ( ( tv0.tv_sec < delta_tv.tv_sec ) ||
904 ( ( tv0.tv_sec == delta_tv.tv_sec ) &&
905 ( tv0.tv_usec < delta_tv.tv_usec ) ) )
908 ld->ld_errno = LDAP_TIMEOUT;
911 /* timeout -= delta_time */
912 tv0.tv_sec -= delta_tv.tv_sec;
913 tv0.tv_usec -= delta_tv.tv_usec;
914 if ( tv0.tv_usec < 0 ) {
916 tv0.tv_usec += 1000000;
918 start_time_tv.tv_sec = curr_time_tv.tv_sec;
919 start_time_tv.tv_usec = curr_time_tv.tv_usec;
922 Debug( LDAP_DEBUG_TRACE, "ldap_int_tls_start: ld %p %ld s %ld us to go\n",
923 (void *)ld, (long) tv.tv_sec, (long) tv.tv_usec );
927 if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
928 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_SET_NONBLOCK, NULL );
930 #endif /* LDAP_USE_NON_BLOCKING_TLS */
933 if ( ld->ld_errno == LDAP_SUCCESS )
934 ld->ld_errno = LDAP_CONNECT_ERROR;
935 return (ld->ld_errno);
938 ssl = ldap_pvt_tls_sb_ctx( sb );
939 assert( ssl != NULL );
942 * compare host with name(s) in certificate
944 if (ld->ld_options.ldo_tls_require_cert != LDAP_OPT_X_TLS_NEVER &&
945 ld->ld_options.ldo_tls_require_cert != LDAP_OPT_X_TLS_ALLOW) {
946 ld->ld_errno = ldap_pvt_tls_check_hostname( ld, ssl, host );
947 if (ld->ld_errno != LDAP_SUCCESS) {
956 ldap_pvt_tls_sb_ctx( Sockbuf *sb )
960 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_SSL, (void *)&p );
965 ldap_pvt_tls_get_strength( void *s )
967 tls_session *session = s;
969 return tls_imp->ti_session_strength( session );
973 ldap_pvt_tls_get_my_dn( void *s, struct berval *dn, LDAPDN_rewrite_dummy *func, unsigned flags )
975 tls_session *session = s;
976 struct berval der_dn;
979 rc = tls_imp->ti_session_my_dn( session, &der_dn );
980 if ( rc == LDAP_SUCCESS )
981 rc = ldap_X509dn2bv(&der_dn, dn, (LDAPDN_rewrite_func *)func, flags );
986 ldap_pvt_tls_get_unique( void *s, struct berval *buf, int is_server )
988 tls_session *session = s;
989 return tls_imp->ti_session_unique( session, buf, is_server );
991 #endif /* HAVE_TLS */
994 ldap_start_tls( LDAP *ld,
995 LDAPControl **serverctrls,
996 LDAPControl **clientctrls,
999 return ldap_extended_operation( ld, LDAP_EXOP_START_TLS,
1000 NULL, serverctrls, clientctrls, msgidp );
1004 ldap_install_tls( LDAP *ld )
1007 return LDAP_NOT_SUPPORTED;
1009 if ( ldap_tls_inplace( ld ) ) {
1010 return LDAP_LOCAL_ERROR;
1013 return ldap_int_tls_start( ld, ld->ld_defconn, NULL );
1018 ldap_start_tls_s ( LDAP *ld,
1019 LDAPControl **serverctrls,
1020 LDAPControl **clientctrls )
1023 return LDAP_NOT_SUPPORTED;
1026 char *rspoid = NULL;
1027 struct berval *rspdata = NULL;
1029 /* XXYYZ: this initiates operation only on default connection! */
1031 if ( ldap_tls_inplace( ld ) ) {
1032 return LDAP_LOCAL_ERROR;
1035 rc = ldap_extended_operation_s( ld, LDAP_EXOP_START_TLS,
1036 NULL, serverctrls, clientctrls, &rspoid, &rspdata );
1038 if ( rspoid != NULL ) {
1042 if ( rspdata != NULL ) {
1043 ber_bvfree( rspdata );
1046 if ( rc == LDAP_SUCCESS ) {
1047 rc = ldap_int_tls_start( ld, ld->ld_defconn, NULL );
1054 /* These tags probably all belong in lber.h, but they're
1055 * not normally encountered when processing LDAP, so maybe
1056 * they belong somewhere else instead.
1059 #define LBER_TAG_OID ((ber_tag_t) 0x06UL)
1061 /* Tags for string types used in a DirectoryString.
1063 * Note that IA5string is not one of the defined choices for
1064 * DirectoryString in X.520, but it gets used for email AVAs.
1066 #define LBER_TAG_UTF8 ((ber_tag_t) 0x0cUL)
1067 #define LBER_TAG_PRINTABLE ((ber_tag_t) 0x13UL)
1068 #define LBER_TAG_TELETEX ((ber_tag_t) 0x14UL)
1069 #define LBER_TAG_IA5 ((ber_tag_t) 0x16UL)
1070 #define LBER_TAG_UNIVERSAL ((ber_tag_t) 0x1cUL)
1071 #define LBER_TAG_BMP ((ber_tag_t) 0x1eUL)
1074 find_oid( struct berval *oid )
1078 for ( i=0; !BER_BVISNULL( &oids[i].oid ); i++ ) {
1079 if ( oids[i].oid.bv_len != oid->bv_len ) continue;
1080 if ( !strcmp( oids[i].oid.bv_val, oid->bv_val ))
1086 /* Converts BER Bitstring value to LDAP BitString value (RFC4517)
1091 * berValue and ldapValue should not be NULL
1094 #define BITS_PER_BYTE 8
1095 #define SQUOTE_LENGTH 1
1096 #define B_CHAR_LENGTH 1
1097 #define STR_OVERHEAD (2*SQUOTE_LENGTH + B_CHAR_LENGTH)
1100 der_to_ldap_BitString (struct berval *berValue,
1101 struct berval *ldapValue)
1103 ber_len_t bitPadding=0;
1104 ber_len_t bits, maxBits;
1107 ber_len_t bitLength;
1109 unsigned char* valPtr;
1111 ldapValue->bv_len=0;
1112 ldapValue->bv_val=NULL;
1114 /* Gets padding and points to binary data */
1115 valLen=berValue->bv_len;
1116 valPtr=(unsigned char*)berValue->bv_val;
1118 bitPadding=(ber_len_t)(valPtr[0]);
1122 /* If Block is non DER encoding fixes to DER encoding */
1123 if (bitPadding >= BITS_PER_BYTE) {
1124 if (valLen*BITS_PER_BYTE > bitPadding ) {
1125 valLen-=(bitPadding/BITS_PER_BYTE);
1126 bitPadding%=BITS_PER_BYTE;
1132 /* Just in case bad encoding */
1133 if (valLen*BITS_PER_BYTE < bitPadding ) {
1138 /* Gets buffer to hold RFC4517 Bit String format */
1139 bitLength=valLen*BITS_PER_BYTE-bitPadding;
1140 tmpStr=LDAP_MALLOC(bitLength + STR_OVERHEAD + 1);
1143 return LDAP_NO_MEMORY;
1145 ldapValue->bv_val=tmpStr;
1146 ldapValue->bv_len=bitLength + STR_OVERHEAD;
1148 /* Formatting in '*binary-digit'B format */
1149 maxBits=BITS_PER_BYTE;
1154 maxBits-=bitPadding;
1155 for (bits=0; bits<maxBits; bits++) {
1170 return LDAP_SUCCESS;
1173 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
1174 * x509_name must be raw DER. If func is non-NULL, the
1175 * constructed DN will use numeric OIDs to identify attributeTypes,
1176 * and the func() will be invoked to rewrite the DN with the given
1179 * Otherwise the DN will use shortNames from a hardcoded table.
1182 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
1187 LDAPAVA *newAVA, *baseAVA;
1188 BerElementBuffer berbuf;
1189 BerElement *ber = (BerElement *)&berbuf;
1190 char oids[8192], *oidptr = oids, *oidbuf = NULL;
1192 char *dn_end, *rdn_end;
1193 int i, navas, nrdns, rc = LDAP_SUCCESS;
1194 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
1200 struct berval Oid, Val, oid2, *in = x509_name;
1202 assert( bv != NULL );
1210 /* A DN is a SEQUENCE of RDNs. An RDN is a SET of AVAs.
1211 * An AVA is a SEQUENCE of attr and value.
1212 * Count the number of AVAs and RDNs
1214 ber_init2( ber, in, LBER_USE_DER );
1215 tag = ber_peek_tag( ber, &len );
1216 if ( tag != LBER_SEQUENCE )
1217 return LDAP_DECODING_ERROR;
1219 for ( tag = ber_first_element( ber, &len, &dn_end );
1221 tag = ber_next_element( ber, &len, dn_end )) {
1223 for ( tag = ber_first_element( ber, &len, &rdn_end );
1224 tag == LBER_SEQUENCE;
1225 tag = ber_next_element( ber, &len, rdn_end )) {
1226 tag = ber_skip_tag( ber, &len );
1227 ber_skip_data( ber, len );
1232 /* Allocate the DN/RDN/AVA stuff as a single block */
1233 dnsize = sizeof(LDAPRDN) * (nrdns+1);
1234 dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
1235 dnsize += sizeof(LDAPAVA) * navas;
1236 if (dnsize > sizeof(ptrs)) {
1237 newDN = (LDAPDN)LDAP_MALLOC( dnsize );
1238 if ( newDN == NULL )
1239 return LDAP_NO_MEMORY;
1241 newDN = (LDAPDN)(char *)ptrs;
1244 newDN[nrdns] = NULL;
1245 newRDN = (LDAPRDN)(newDN + nrdns+1);
1246 newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
1249 /* Rewind and start extracting */
1252 tag = ber_first_element( ber, &len, &dn_end );
1253 for ( i = nrdns - 1; i >= 0; i-- ) {
1256 for ( tag = ber_first_element( ber, &len, &rdn_end );
1257 tag == LBER_SEQUENCE;
1258 tag = ber_next_element( ber, &len, rdn_end )) {
1261 tag = ber_skip_tag( ber, &len );
1262 tag = ber_get_stringbv( ber, &Oid, LBER_BV_NOTERM );
1263 if ( tag != LBER_TAG_OID ) {
1264 rc = LDAP_DECODING_ERROR;
1268 oid2.bv_val = oidptr;
1269 oid2.bv_len = oidrem;
1270 if ( ber_decode_oid( &Oid, &oid2 ) < 0 ) {
1271 rc = LDAP_DECODING_ERROR;
1274 oidname = find_oid( &oid2 );
1276 newAVA->la_attr = oid2;
1277 oidptr += oid2.bv_len + 1;
1278 oidrem -= oid2.bv_len + 1;
1280 /* Running out of OID buffer space? */
1282 if ( oidsize == 0 ) {
1283 oidsize = sizeof(oids) * 2;
1285 oidbuf = LDAP_MALLOC( oidsize );
1286 if ( oidbuf == NULL ) goto nomem;
1290 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
1291 if ( oidbuf == NULL ) goto nomem;
1292 /* Buffer moved! Fix AVA pointers */
1293 if ( old != oidbuf ) {
1295 long dif = oidbuf - old;
1297 for (a=baseAVA; a<=newAVA; a++){
1298 if (a->la_attr.bv_val >= old &&
1299 a->la_attr.bv_val <= (old + oidsize))
1300 a->la_attr.bv_val += dif;
1303 oidptr = oidbuf + oidsize - oidrem;
1310 newAVA->la_attr = oidname->oid;
1312 newAVA->la_attr = oidname->name;
1315 newAVA->la_private = NULL;
1316 newAVA->la_flags = LDAP_AVA_STRING;
1317 tag = ber_get_stringbv( ber, &Val, LBER_BV_NOTERM );
1319 case LBER_TAG_UNIVERSAL:
1320 /* This uses 32-bit ISO 10646-1 */
1321 csize = 4; goto to_utf8;
1323 /* This uses 16-bit ISO 10646-1 */
1324 csize = 2; goto to_utf8;
1325 case LBER_TAG_TELETEX:
1326 /* This uses 8-bit, assume ISO 8859-1 */
1328 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
1329 newAVA->la_flags |= LDAP_AVA_NONPRINTABLE;
1331 newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
1332 if (rc != LDAP_SUCCESS) goto nomem;
1335 newAVA->la_flags |= LDAP_AVA_NONPRINTABLE;
1336 /* This is already in UTF-8 encoding */
1338 case LBER_TAG_PRINTABLE:
1339 /* These are always 7-bit strings */
1340 newAVA->la_value = Val;
1342 case LBER_BITSTRING:
1343 /* X.690 bitString value converted to RFC4517 Bit String */
1344 rc = der_to_ldap_BitString( &Val, &newAVA->la_value );
1347 /* Not a string type at all */
1348 newAVA->la_flags = 0;
1349 newAVA->la_value = Val;
1355 tag = ber_next_element( ber, &len, dn_end );
1359 rc = func( newDN, flags, NULL );
1360 if ( rc != LDAP_SUCCESS )
1364 rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
1367 for (;baseAVA < newAVA; baseAVA++) {
1368 if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
1369 LDAP_FREE( baseAVA->la_attr.bv_val );
1370 if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
1371 LDAP_FREE( baseAVA->la_value.bv_val );
1375 LDAP_FREE( oidbuf );
1376 if ( newDN != (LDAPDN)(char *) ptrs )