2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 * tls.c - Handle tls/ssl using SSLeay or OpenSSL.
16 #include <ac/socket.h>
17 #include <ac/string.h>
19 #include <ac/unistd.h>
24 #include <ldap_pvt_thread.h>
27 #ifdef HAVE_OPENSSL_SSL_H
28 #include <openssl/ssl.h>
29 #elif defined( HAVE_SSL_H )
33 static char *tls_opt_certfile = NULL;
34 static char *tls_opt_keyfile = NULL;
35 static char *tls_opt_cacertfile = NULL;
36 static char *tls_opt_cacertdir = NULL;
37 static int tls_opt_require_cert = 0;
38 static char *tls_opt_ciphersuite = NULL;
40 #define HAS_TLS( sb ) ((sb)->sb_io==&tls_io)
42 static int tls_setup( Sockbuf *sb, void *arg );
43 static int tls_remove( Sockbuf *sb );
44 static ber_slen_t tls_read( Sockbuf *sb, void *buf, ber_len_t len );
45 static ber_slen_t tls_write( Sockbuf *sb, void *buf, ber_len_t len );
46 static int tls_close( Sockbuf *sb );
47 static int tls_report_error( void );
49 static Sockbuf_IO tls_io=
58 static int tls_verify_cb( int ok, X509_STORE_CTX *ctx );
60 static SSL_CTX *tls_def_ctx = NULL;
64 * provide mutexes for the SSLeay library.
66 static ldap_pvt_thread_mutex_t tls_mutexes[CRYPTO_NUM_LOCKS];
68 static void tls_locking_cb( int mode, int type, const char *file, int line )
70 if ( mode & CRYPTO_LOCK ) {
71 ldap_pvt_thread_mutex_lock( tls_mutexes+type );
73 ldap_pvt_thread_mutex_unlock( tls_mutexes+type );
78 * an extra mutex for the default ctx.
81 static ldap_pvt_thread_mutex_t tls_def_ctx_mutex;
83 static void tls_init_threads( void )
87 for( i=0; i< CRYPTO_NUM_LOCKS ; i++ ) {
88 ldap_pvt_thread_mutex_init( tls_mutexes+i );
90 CRYPTO_set_locking_callback( tls_locking_cb );
91 /* FIXME: the thread id should be added somehow... */
93 ldap_pvt_thread_mutex_init( &tls_def_ctx_mutex );
95 #endif /* LDAP_R_COMPILE */
98 * Initialize tls system. Should be called only once.
101 ldap_pvt_tls_init( void )
103 static int tls_initialized = 0;
105 if ( tls_initialized )
107 #ifdef LDAP_R_COMPILE
110 SSL_load_error_strings();
111 SSLeay_add_ssl_algorithms();
116 * initialize the default context
119 ldap_pvt_tls_init_def_ctx( void )
121 STACK_OF(X509_NAME) *cacert;
123 #ifdef LDAP_R_COMPILE
124 ldap_pvt_thread_mutex_lock( &tls_def_ctx_mutex );
126 if ( tls_def_ctx == NULL ) {
127 tls_def_ctx = SSL_CTX_new( SSLv23_method() );
128 if ( tls_def_ctx == NULL ) {
129 Debug( LDAP_DEBUG_ANY,
130 "TLS: could not allocate default ctx.\n",0,0,0);
133 if ( tls_opt_ciphersuite &&
134 !SSL_CTX_set_cipher_list( tls_def_ctx,
135 tls_opt_ciphersuite ) ) {
136 Debug( LDAP_DEBUG_ANY,
137 "TLS: could not set cipher list %s.\n",
138 tls_opt_ciphersuite, 0, 0 );
142 if ( !SSL_CTX_load_verify_locations( tls_def_ctx,
144 tls_opt_cacertdir ) ||
145 !SSL_CTX_set_default_verify_paths( tls_def_ctx ) ) {
146 Debug( LDAP_DEBUG_ANY,
147 "TLS: could not load verify locations (file:`%s',dir:`%s').\n",
148 tls_opt_cacertfile,tls_opt_cacertdir,0);
152 /* FIXME: Load from tls_opt_cacertdir too */
153 cacert = SSL_load_client_CA_file( tls_opt_cacertfile );
155 Debug( LDAP_DEBUG_ANY,
156 "TLS: could not load CA certificate file `%s'.\n",
157 tls_opt_cacertfile,0,0);
161 SSL_CTX_set_client_CA_list( tls_def_ctx, cacert );
162 if ( tls_opt_keyfile &&
163 !SSL_CTX_use_PrivateKey_file( tls_def_ctx,
165 SSL_FILETYPE_PEM ) ) {
166 Debug( LDAP_DEBUG_ANY,
167 "TLS: could not use key file `%s'.\n",
168 tls_opt_keyfile,0,0);
172 if ( tls_opt_certfile &&
173 !SSL_CTX_use_certificate_file( tls_def_ctx,
175 SSL_FILETYPE_PEM ) ) {
176 Debug( LDAP_DEBUG_ANY,
177 "TLS: could not use certificate `%s'.\n",
178 tls_opt_certfile,0,0);
182 if ( ( tls_opt_certfile || tls_opt_keyfile ) &&
183 !SSL_CTX_check_private_key( tls_def_ctx ) ) {
184 Debug( LDAP_DEBUG_ANY,
185 "TLS: private key mismatch.\n",
190 SSL_CTX_set_verify( tls_def_ctx, (tls_opt_require_cert) ?
191 (SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT) :
192 SSL_VERIFY_PEER, tls_verify_cb );
194 #ifdef LDAP_R_COMPILE
195 ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
199 #ifdef LDAP_R_COMPILE
200 ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
206 alloc_handle( Sockbuf *sb, void *ctx_arg )
213 ctx = (SSL_CTX *) ctx_arg;
215 if ( ldap_pvt_tls_init_def_ctx() < 0 )
220 ssl = SSL_new( ctx );
222 Debug( LDAP_DEBUG_ANY,"TLS: can't create ssl handle.\n",0,0,0);
227 SSL_set_fd( ssl, ber_pvt_sb_get_desc( sb ) );
232 update_flags( Sockbuf *sb, SSL * ssl )
234 sb->sb_trans_needs_read = SSL_want_read(ssl) ? 1 : 0;
235 sb->sb_trans_needs_write = SSL_want_write(ssl) ? 1 : 0;
239 * Call this to do a TLS connect on a sockbuf. ctx_arg can be
240 * a SSL_CTX * or NULL, in which case the default ctx is used.
244 * 0 - Success. Connection is ready for communication.
245 * <0 - Error. Can't create a TLS stream.
246 * >0 - Partial success.
247 * Do a select (using information from lber_pvt_sb_needs_{read,write}
252 ldap_pvt_tls_connect( Sockbuf *sb, void *ctx_arg )
257 if ( HAS_TLS( sb ) ) {
258 ssl = (SSL *) sb->sb_iodata;
260 ssl = alloc_handle( sb, ctx_arg );
263 ber_pvt_sb_clear_io( sb );
264 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
267 err = SSL_connect( ssl );
272 (errno==EWOULDBLOCK) ||
278 update_flags( sb, ssl );
281 Debug( LDAP_DEBUG_ANY,"TLS: can't connect.\n",0,0,0);
282 ber_pvt_sb_clear_io( sb );
283 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
290 * Call this to do a TLS accept on a sockbuf.
291 * Everything else is the same as with tls_connect.
294 ldap_pvt_tls_accept( Sockbuf *sb, void *ctx_arg )
299 if ( HAS_TLS( sb ) ) {
300 ssl = (SSL *) sb->sb_iodata;
302 ssl = alloc_handle( sb, ctx_arg );
305 ber_pvt_sb_clear_io( sb );
306 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
309 err = SSL_accept( ssl );
312 if ( !SSL_want_nothing( ssl ) ) {
313 update_flags( sb, ssl );
316 Debug( LDAP_DEBUG_ANY,"TLS: can't accept.\n",0,0,0 );
318 ber_pvt_sb_clear_io( sb );
319 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
326 ldap_pvt_tls_get_peer( LDAP *ld )
331 ldap_pvt_tls_get_peer_issuer( LDAP *ld )
336 ldap_pvt_tls_config( struct ldapoptions *lo, int option, const char *arg )
341 case LDAP_OPT_X_TLS_CACERTFILE:
342 case LDAP_OPT_X_TLS_CACERTDIR:
343 case LDAP_OPT_X_TLS_CERTFILE:
344 case LDAP_OPT_X_TLS_KEYFILE:
345 return ldap_pvt_tls_set_option( NULL, option, (void *) arg );
346 case LDAP_OPT_X_TLS_REQUIRE_CERT:
347 i = ( ( strcasecmp( arg, "on" ) == 0 ) ||
348 ( strcasecmp( arg, "yes" ) == 0) ||
349 ( strcasecmp( arg, "true" ) == 0 ) );
350 return ldap_pvt_tls_set_option( NULL, option, (void *) &i );
352 if ( strcasecmp( arg, "never" ) == 0 )
353 return ldap_pvt_tls_set_option( lo, option,
354 LDAP_OPT_X_TLS_NEVER );
355 if ( strcasecmp( arg, "demand" ) == 0 )
356 return ldap_pvt_tls_set_option( lo, option,
357 LDAP_OPT_X_TLS_DEMAND );
358 if ( strcasecmp( arg, "allow" ) == 0 )
359 return ldap_pvt_tls_set_option( lo, option,
360 LDAP_OPT_X_TLS_ALLOW );
361 if ( strcasecmp( arg, "try" ) == 0 )
362 return ldap_pvt_tls_set_option( lo, option,
363 LDAP_OPT_X_TLS_TRY );
364 if ( strcasecmp( arg, "hard" ) == 0 )
365 return ldap_pvt_tls_set_option( lo, option,
366 LDAP_OPT_X_TLS_HARD );
374 ldap_pvt_tls_get_option( struct ldapoptions *lo, int option, void *arg )
378 *(int *)arg = lo->ldo_tls_mode;
380 case LDAP_OPT_X_TLS_CERT:
382 arg = (void *) tls_def_ctx;
384 arg = lo->ldo_tls_ctx;
386 case LDAP_OPT_X_TLS_CACERTFILE:
387 *(char **)arg = tls_opt_cacertfile ?
388 strdup( tls_opt_cacertfile ) : NULL;
390 case LDAP_OPT_X_TLS_CACERTDIR:
391 *(char **)arg = tls_opt_cacertdir ?
392 strdup( tls_opt_cacertdir ) : NULL;
394 case LDAP_OPT_X_TLS_CERTFILE:
395 *(char **)arg = tls_opt_certfile ?
396 strdup( tls_opt_certfile ) : NULL;
398 case LDAP_OPT_X_TLS_KEYFILE:
399 *(char **)arg = tls_opt_keyfile ?
400 strdup( tls_opt_keyfile ) : NULL;
402 case LDAP_OPT_X_TLS_REQUIRE_CERT:
403 *(int *)arg = tls_opt_require_cert;
412 ldap_pvt_tls_set_option( struct ldapoptions *lo, int option, void *arg )
416 switch( *(int *) arg ) {
417 case LDAP_OPT_X_TLS_NEVER:
418 case LDAP_OPT_X_TLS_DEMAND:
419 case LDAP_OPT_X_TLS_ALLOW:
420 case LDAP_OPT_X_TLS_TRY:
421 case LDAP_OPT_X_TLS_HARD:
422 lo->ldo_tls_mode = *(int *)arg;
428 case LDAP_OPT_X_TLS_CERT:
430 tls_def_ctx = (SSL_CTX *) arg;
432 lo->ldo_tls_ctx = arg;
438 case LDAP_OPT_X_TLS_CACERTFILE:
439 if ( tls_opt_cacertfile ) free( tls_opt_cacertfile );
440 tls_opt_cacertfile = arg ? strdup( (char *) arg ) : NULL;
442 case LDAP_OPT_X_TLS_CACERTDIR:
443 if ( tls_opt_cacertdir ) free( tls_opt_cacertdir );
444 tls_opt_cacertdir = arg ? strdup( (char *) arg ) : NULL;
446 case LDAP_OPT_X_TLS_CERTFILE:
447 if ( tls_opt_certfile ) free( tls_opt_certfile );
448 tls_opt_certfile = arg ? strdup( (char *) arg ) : NULL;
450 case LDAP_OPT_X_TLS_KEYFILE:
451 if ( tls_opt_keyfile ) free( tls_opt_keyfile );
452 tls_opt_keyfile = arg ? strdup( (char *) arg ) : NULL;
454 case LDAP_OPT_X_TLS_REQUIRE_CERT:
455 tls_opt_require_cert = * (int *) arg;
457 case LDAP_OPT_X_TLS_CIPHER_SUITE:
458 if ( tls_opt_ciphersuite ) free( tls_opt_ciphersuite );
459 tls_opt_ciphersuite = arg ? strdup( (char *) arg ) : NULL;
468 tls_setup( Sockbuf *sb, void *arg )
475 tls_remove( Sockbuf *sb )
477 SSL_free( (SSL *) sb->sb_iodata );
482 tls_write( Sockbuf *sb, void *buf, ber_len_t sz )
484 int ret = SSL_write( (SSL *)sb->sb_iodata, buf, sz );
486 update_flags(sb, (SSL *)sb->sb_iodata );
491 tls_read( Sockbuf *sb, void *buf, ber_len_t sz )
493 int ret = SSL_read( (SSL *)sb->sb_iodata, buf, sz );
495 update_flags(sb, (SSL *)sb->sb_iodata );
500 tls_close( Sockbuf *sb )
502 tcp_close( ber_pvt_sb_get_desc( sb ) );
507 tls_verify_cb( int ok, X509_STORE_CTX *ctx )
517 cert = X509_STORE_CTX_get_current_cert( ctx );
518 errnum = X509_STORE_CTX_get_error( ctx );
519 errdepth = X509_STORE_CTX_get_error_depth( ctx );
522 * X509_get_*_name return pointers to the internal copies of
523 * those things requested. So do not free them.
525 subject = X509_get_subject_name( cert );
526 issuer = X509_get_issuer_name( cert );
527 /* X509_NAME_oneline, if passed a NULL buf, allocate memomry */
528 sname = X509_NAME_oneline( subject, NULL, 0 );
529 iname = X509_NAME_oneline( issuer, NULL, 0 );
530 Debug( LDAP_DEBUG_TRACE,
531 "TLS certificate verification: depth: %d, subject: %s, issuer: %s\n",
533 sname ? sname : "-unknown-",
534 iname ? iname : "-unknown-" );
543 /* Inspired by ERR_print_errors in OpenSSL */
545 tls_report_error( void )
552 while ( ( l = ERR_get_error_line( &file, &line ) ) != 0 ) {
553 Debug( LDAP_DEBUG_ANY, "TLS: %s %s:%d\n",
554 ERR_error_string( l, buf ), file, line );