3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * tls.c - Handle tls/ssl using SSLeay or OpenSSL.
15 #include <ac/stdlib.h>
17 #include <ac/socket.h>
18 #include <ac/string.h>
20 #include <ac/unistd.h>
25 #include <ldap_pvt_thread.h>
28 #ifdef HAVE_OPENSSL_SSL_H
29 #include <openssl/ssl.h>
30 #elif defined( HAVE_SSL_H )
34 static int tls_opt_trace = 1;
35 static char *tls_opt_certfile = NULL;
36 static char *tls_opt_keyfile = NULL;
37 static char *tls_opt_cacertfile = NULL;
38 static char *tls_opt_cacertdir = NULL;
39 static int tls_opt_require_cert = 0;
40 static char *tls_opt_ciphersuite = NULL;
42 #define HAS_TLS( sb ) ((sb)->sb_io==&tls_io)
44 static int tls_setup( Sockbuf *sb, void *arg );
45 static int tls_remove( Sockbuf *sb );
46 static ber_slen_t tls_read( Sockbuf *sb, void *buf, ber_len_t len );
47 static ber_slen_t tls_write( Sockbuf *sb, void *buf, ber_len_t len );
48 static int tls_close( Sockbuf *sb );
49 static int tls_report_error( void );
51 static Sockbuf_IO tls_io=
60 static void tls_info_cb( SSL *ssl, int where, int ret );
61 static int tls_verify_cb( int ok, X509_STORE_CTX *ctx );
62 static RSA * tls_tmp_rsa_cb( SSL *ssl, int is_export, int key_length );
63 static DH * tls_tmp_dh_cb( SSL *ssl, int is_export, int key_length );
64 static STACK_OF(X509_NAME) * get_ca_list( char * bundle, char * dir );
66 static SSL_CTX *tls_def_ctx = NULL;
70 * provide mutexes for the SSLeay library.
72 static ldap_pvt_thread_mutex_t tls_mutexes[CRYPTO_NUM_LOCKS];
74 static void tls_locking_cb( int mode, int type, const char *file, int line )
76 if ( mode & CRYPTO_LOCK ) {
77 ldap_pvt_thread_mutex_lock( tls_mutexes+type );
79 ldap_pvt_thread_mutex_unlock( tls_mutexes+type );
84 * an extra mutex for the default ctx.
87 static ldap_pvt_thread_mutex_t tls_def_ctx_mutex;
89 static void tls_init_threads( void )
93 for( i=0; i< CRYPTO_NUM_LOCKS ; i++ ) {
94 ldap_pvt_thread_mutex_init( tls_mutexes+i );
96 CRYPTO_set_locking_callback( tls_locking_cb );
97 /* FIXME: the thread id should be added somehow... */
99 ldap_pvt_thread_mutex_init( &tls_def_ctx_mutex );
101 #endif /* LDAP_R_COMPILE */
104 * Initialize tls system. Should be called only once.
107 ldap_pvt_tls_init( void )
109 static int tls_initialized = 0;
111 if ( tls_initialized )
113 #ifdef LDAP_R_COMPILE
116 SSL_load_error_strings();
117 SSLeay_add_ssl_algorithms();
118 /* FIXME: mod_ssl does this */
119 X509V3_add_standard_extensions();
124 * initialize the default context
127 ldap_pvt_tls_init_def_ctx( void )
129 STACK_OF(X509_NAME) *calist;
131 #ifdef LDAP_R_COMPILE
132 ldap_pvt_thread_mutex_lock( &tls_def_ctx_mutex );
134 if ( tls_def_ctx == NULL ) {
135 tls_def_ctx = SSL_CTX_new( SSLv23_method() );
136 if ( tls_def_ctx == NULL ) {
137 Debug( LDAP_DEBUG_ANY,
138 "TLS: could not allocate default ctx.\n",0,0,0);
141 if ( tls_opt_ciphersuite &&
142 !SSL_CTX_set_cipher_list( tls_def_ctx,
143 tls_opt_ciphersuite ) ) {
144 Debug( LDAP_DEBUG_ANY,
145 "TLS: could not set cipher list %s.\n",
146 tls_opt_ciphersuite, 0, 0 );
150 if (tls_opt_cacertfile != NULL || tls_opt_cacertdir != NULL) {
151 if ( !SSL_CTX_load_verify_locations( tls_def_ctx,
154 || !SSL_CTX_set_default_verify_paths( tls_def_ctx ) )
156 Debug( LDAP_DEBUG_ANY,
157 "TLS: could not load verify locations (file:`%s',dir:`%s').\n",
158 tls_opt_cacertfile,tls_opt_cacertdir,0);
162 calist = get_ca_list( tls_opt_cacertfile, tls_opt_cacertdir );
164 Debug( LDAP_DEBUG_ANY,
165 "TLS: could not load client CA list (file:`%s',dir:`%s').\n",
166 tls_opt_cacertfile,tls_opt_cacertdir,0);
170 SSL_CTX_set_client_CA_list( tls_def_ctx, calist );
172 if ( tls_opt_keyfile &&
173 !SSL_CTX_use_PrivateKey_file( tls_def_ctx,
175 SSL_FILETYPE_PEM ) ) {
176 Debug( LDAP_DEBUG_ANY,
177 "TLS: could not use key file `%s'.\n",
178 tls_opt_keyfile,0,0);
182 if ( tls_opt_certfile &&
183 !SSL_CTX_use_certificate_file( tls_def_ctx,
185 SSL_FILETYPE_PEM ) ) {
186 Debug( LDAP_DEBUG_ANY,
187 "TLS: could not use certificate `%s'.\n",
188 tls_opt_certfile,0,0);
192 if ( ( tls_opt_certfile || tls_opt_keyfile ) &&
193 !SSL_CTX_check_private_key( tls_def_ctx ) ) {
194 Debug( LDAP_DEBUG_ANY,
195 "TLS: private key mismatch.\n",
200 if ( tls_opt_trace ) {
201 SSL_CTX_set_info_callback( tls_def_ctx, tls_info_cb );
203 SSL_CTX_set_verify( tls_def_ctx, (tls_opt_require_cert) ?
204 (SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT) :
205 SSL_VERIFY_PEER, tls_verify_cb );
206 SSL_CTX_set_tmp_rsa_callback( tls_def_ctx, tls_tmp_rsa_cb );
207 /* SSL_CTX_set_tmp_dh_callback( tls_def_ctx, tls_tmp_dh_cb ); */
209 #ifdef LDAP_R_COMPILE
210 ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
214 #ifdef LDAP_R_COMPILE
215 ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
220 static STACK_OF(X509_NAME) *
221 get_ca_list( char * bundle, char * dir )
223 STACK_OF(X509_NAME) *ca_list = NULL;
226 ca_list = SSL_load_client_CA_file( bundle );
229 * FIXME: We have now to go over all files in dir, load them
230 * and add every certificate there to ca_list.
236 alloc_handle( Sockbuf *sb, void *ctx_arg )
243 ctx = (SSL_CTX *) ctx_arg;
245 if ( ldap_pvt_tls_init_def_ctx() < 0 )
250 ssl = SSL_new( ctx );
252 Debug( LDAP_DEBUG_ANY,"TLS: can't create ssl handle.\n",0,0,0);
256 if ( tls_opt_trace ) {
257 SSL_set_info_callback( ssl, tls_info_cb );
260 SSL_set_fd( ssl, ber_pvt_sb_get_desc( sb ) );
265 update_flags( Sockbuf *sb, SSL * ssl )
267 sb->sb_trans_needs_read = (SSL_want_read(ssl) ? 1 : 0);
268 sb->sb_trans_needs_write = (SSL_want_write(ssl) ? 1 : 0);
272 * Call this to do a TLS connect on a sockbuf. ctx_arg can be
273 * a SSL_CTX * or NULL, in which case the default ctx is used.
277 * 0 - Success. Connection is ready for communication.
278 * <0 - Error. Can't create a TLS stream.
279 * >0 - Partial success.
280 * Do a select (using information from lber_pvt_sb_needs_{read,write}
285 ldap_pvt_tls_connect( Sockbuf *sb, void *ctx_arg )
290 if ( HAS_TLS( sb ) ) {
291 ssl = (SSL *) sb->sb_iodata;
293 ssl = alloc_handle( sb, ctx_arg );
296 ber_pvt_sb_clear_io( sb );
297 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
300 err = SSL_connect( ssl );
305 (errno==EWOULDBLOCK) ||
311 update_flags( sb, ssl );
314 Debug( LDAP_DEBUG_ANY,"TLS: can't connect.\n",0,0,0);
315 ber_pvt_sb_clear_io( sb );
316 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
323 * Call this to do a TLS accept on a sockbuf.
324 * Everything else is the same as with tls_connect.
327 ldap_pvt_tls_accept( Sockbuf *sb, void *ctx_arg )
332 if ( HAS_TLS( sb ) ) {
333 ssl = (SSL *) sb->sb_iodata;
335 ssl = alloc_handle( sb, ctx_arg );
338 ber_pvt_sb_clear_io( sb );
339 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
342 err = SSL_accept( ssl );
345 if ( !SSL_want_nothing( ssl ) ) {
346 update_flags( sb, ssl );
349 Debug( LDAP_DEBUG_ANY,"TLS: can't accept.\n",0,0,0 );
351 ber_pvt_sb_clear_io( sb );
352 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
359 ldap_pvt_tls_get_peer( LDAP *ld )
364 ldap_pvt_tls_get_peer_issuer( LDAP *ld )
369 ldap_pvt_tls_config( struct ldapoptions *lo, int option, const char *arg )
374 case LDAP_OPT_X_TLS_CACERTFILE:
375 case LDAP_OPT_X_TLS_CACERTDIR:
376 case LDAP_OPT_X_TLS_CERTFILE:
377 case LDAP_OPT_X_TLS_KEYFILE:
378 return ldap_pvt_tls_set_option( NULL, option, (void *) arg );
379 case LDAP_OPT_X_TLS_REQUIRE_CERT:
380 i = ( ( strcasecmp( arg, "on" ) == 0 ) ||
381 ( strcasecmp( arg, "yes" ) == 0) ||
382 ( strcasecmp( arg, "true" ) == 0 ) );
383 return ldap_pvt_tls_set_option( NULL, option, (void *) &i );
386 if ( strcasecmp( arg, "never" ) == 0 )
387 i = LDAP_OPT_X_TLS_NEVER ;
388 if ( strcasecmp( arg, "demand" ) == 0 )
389 i = LDAP_OPT_X_TLS_DEMAND ;
390 if ( strcasecmp( arg, "allow" ) == 0 )
391 i = LDAP_OPT_X_TLS_ALLOW ;
392 if ( strcasecmp( arg, "try" ) == 0 )
393 i = LDAP_OPT_X_TLS_TRY ;
394 if ( strcasecmp( arg, "hard" ) == 0 )
395 i = LDAP_OPT_X_TLS_HARD ;
397 return ldap_pvt_tls_set_option( lo, option, &i );
405 ldap_pvt_tls_get_option( struct ldapoptions *lo, int option, void *arg )
409 *(int *)arg = lo->ldo_tls_mode;
411 case LDAP_OPT_X_TLS_CERT:
413 arg = (void *) tls_def_ctx;
415 arg = lo->ldo_tls_ctx;
417 case LDAP_OPT_X_TLS_CACERTFILE:
418 *(char **)arg = tls_opt_cacertfile ?
419 LDAP_STRDUP( tls_opt_cacertfile ) : NULL;
421 case LDAP_OPT_X_TLS_CACERTDIR:
422 *(char **)arg = tls_opt_cacertdir ?
423 LDAP_STRDUP( tls_opt_cacertdir ) : NULL;
425 case LDAP_OPT_X_TLS_CERTFILE:
426 *(char **)arg = tls_opt_certfile ?
427 LDAP_STRDUP( tls_opt_certfile ) : NULL;
429 case LDAP_OPT_X_TLS_KEYFILE:
430 *(char **)arg = tls_opt_keyfile ?
431 LDAP_STRDUP( tls_opt_keyfile ) : NULL;
433 case LDAP_OPT_X_TLS_REQUIRE_CERT:
434 *(int *)arg = tls_opt_require_cert;
443 ldap_pvt_tls_set_option( struct ldapoptions *lo, int option, void *arg )
447 switch( *(int *) arg ) {
448 case LDAP_OPT_X_TLS_NEVER:
449 case LDAP_OPT_X_TLS_DEMAND:
450 case LDAP_OPT_X_TLS_ALLOW:
451 case LDAP_OPT_X_TLS_TRY:
452 case LDAP_OPT_X_TLS_HARD:
454 lo->ldo_tls_mode = *(int *)arg;
460 case LDAP_OPT_X_TLS_CERT:
462 tls_def_ctx = (SSL_CTX *) arg;
464 lo->ldo_tls_ctx = arg;
470 case LDAP_OPT_X_TLS_CACERTFILE:
471 if ( tls_opt_cacertfile ) free( tls_opt_cacertfile );
472 tls_opt_cacertfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
474 case LDAP_OPT_X_TLS_CACERTDIR:
475 if ( tls_opt_cacertdir ) free( tls_opt_cacertdir );
476 tls_opt_cacertdir = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
478 case LDAP_OPT_X_TLS_CERTFILE:
479 if ( tls_opt_certfile ) free( tls_opt_certfile );
480 tls_opt_certfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
482 case LDAP_OPT_X_TLS_KEYFILE:
483 if ( tls_opt_keyfile ) free( tls_opt_keyfile );
484 tls_opt_keyfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
486 case LDAP_OPT_X_TLS_REQUIRE_CERT:
487 tls_opt_require_cert = * (int *) arg;
489 case LDAP_OPT_X_TLS_CIPHER_SUITE:
490 if ( tls_opt_ciphersuite ) free( tls_opt_ciphersuite );
491 tls_opt_ciphersuite = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
500 tls_setup( Sockbuf *sb, void *arg )
507 tls_remove( Sockbuf *sb )
509 SSL_free( (SSL *) sb->sb_iodata );
514 tls_write( Sockbuf *sb, void *buf, ber_len_t sz )
516 int ret = SSL_write( (SSL *)sb->sb_iodata, buf, sz );
518 update_flags(sb, (SSL *)sb->sb_iodata );
520 if (sb->sb_trans_needs_write)
527 tls_read( Sockbuf *sb, void *buf, ber_len_t sz )
529 int ret = SSL_read( (SSL *)sb->sb_iodata, buf, sz );
531 update_flags(sb, (SSL *)sb->sb_iodata );
533 if (sb->sb_trans_needs_read)
540 tls_close( Sockbuf *sb )
542 tcp_close( ber_pvt_sb_get_desc( sb ) );
546 /* Derived from openssl/apps/s_cb.c */
548 tls_info_cb( SSL *ssl, int where, int ret )
553 w = where & ~SSL_ST_MASK;
554 if ( w & SSL_ST_CONNECT ) {
556 } else if ( w & SSL_ST_ACCEPT ) {
562 if ( where & SSL_CB_LOOP ) {
563 Debug( LDAP_DEBUG_TRACE,
564 "TLS trace: %s:%s\n",
565 op, SSL_state_string_long( ssl ), 0 );
566 } else if ( where & SSL_CB_ALERT ) {
567 op = ( where & SSL_CB_READ ) ? "read" : "write";
568 Debug( LDAP_DEBUG_TRACE,
569 "TLS trace: SSL3 alert %s:%s:%s\n",
571 SSL_alert_type_string_long( ret ),
572 SSL_alert_desc_string_long( ret) );
573 } else if ( where & SSL_CB_EXIT ) {
575 Debug( LDAP_DEBUG_TRACE,
576 "TLS trace: %s:failed in %s\n",
577 op, SSL_state_string_long( ssl ), 0 );
578 } else if ( ret < 0 ) {
579 Debug( LDAP_DEBUG_TRACE,
580 "TLS trace: %s:error in %s\n",
581 op, SSL_state_string_long( ssl ), 0 );
587 tls_verify_cb( int ok, X509_STORE_CTX *ctx )
597 cert = X509_STORE_CTX_get_current_cert( ctx );
598 errnum = X509_STORE_CTX_get_error( ctx );
599 errdepth = X509_STORE_CTX_get_error_depth( ctx );
602 * X509_get_*_name return pointers to the internal copies of
603 * those things requested. So do not free them.
605 subject = X509_get_subject_name( cert );
606 issuer = X509_get_issuer_name( cert );
607 /* X509_NAME_oneline, if passed a NULL buf, allocate memomry */
608 sname = X509_NAME_oneline( subject, NULL, 0 );
609 iname = X509_NAME_oneline( issuer, NULL, 0 );
610 Debug( LDAP_DEBUG_TRACE,
611 "TLS certificate verification: depth: %d, subject: %s, issuer: %s\n",
613 sname ? sname : "-unknown-",
614 iname ? iname : "-unknown-" );
623 /* Inspired by ERR_print_errors in OpenSSL */
625 tls_report_error( void )
632 while ( ( l = ERR_get_error_line( &file, &line ) ) != 0 ) {
633 Debug( LDAP_DEBUG_ANY, "TLS: %s %s:%d\n",
634 ERR_error_string( l, buf ), file, line );
639 tls_tmp_rsa_cb( SSL *ssl, int is_export, int key_length )
643 /* FIXME: Pregenerate the key on startup */
644 /* FIXME: Who frees the key? */
645 tmp_rsa = RSA_generate_key( key_length, RSA_F4, NULL, NULL );
648 Debug( LDAP_DEBUG_ANY, "TLS: Failed to generate temporary %d-bit %s RSA key\n",
649 key_length, is_export ? "export" : "domestic", 0 );
656 tls_tmp_dh_cb( SSL *ssl, int is_export, int key_length )