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 )
114 #ifdef LDAP_R_COMPILE
117 SSL_load_error_strings();
118 SSLeay_add_ssl_algorithms();
119 /* FIXME: mod_ssl does this */
120 X509V3_add_standard_extensions();
125 * initialize the default context
128 ldap_pvt_tls_init_def_ctx( void )
130 STACK_OF(X509_NAME) *calist;
132 #ifdef LDAP_R_COMPILE
133 ldap_pvt_thread_mutex_lock( &tls_def_ctx_mutex );
135 if ( tls_def_ctx == NULL ) {
136 tls_def_ctx = SSL_CTX_new( SSLv23_method() );
137 if ( tls_def_ctx == NULL ) {
138 Debug( LDAP_DEBUG_ANY,
139 "TLS: could not allocate default ctx.\n",0,0,0);
142 if ( tls_opt_ciphersuite &&
143 !SSL_CTX_set_cipher_list( tls_def_ctx,
144 tls_opt_ciphersuite ) ) {
145 Debug( LDAP_DEBUG_ANY,
146 "TLS: could not set cipher list %s.\n",
147 tls_opt_ciphersuite, 0, 0 );
151 if (tls_opt_cacertfile != NULL || tls_opt_cacertdir != NULL) {
152 if ( !SSL_CTX_load_verify_locations( tls_def_ctx,
155 || !SSL_CTX_set_default_verify_paths( tls_def_ctx ) )
157 Debug( LDAP_DEBUG_ANY,
158 "TLS: could not load verify locations (file:`%s',dir:`%s').\n",
159 tls_opt_cacertfile,tls_opt_cacertdir,0);
163 calist = get_ca_list( tls_opt_cacertfile, tls_opt_cacertdir );
165 Debug( LDAP_DEBUG_ANY,
166 "TLS: could not load client CA list (file:`%s',dir:`%s').\n",
167 tls_opt_cacertfile,tls_opt_cacertdir,0);
171 SSL_CTX_set_client_CA_list( tls_def_ctx, calist );
173 if ( tls_opt_keyfile &&
174 !SSL_CTX_use_PrivateKey_file( tls_def_ctx,
176 SSL_FILETYPE_PEM ) ) {
177 Debug( LDAP_DEBUG_ANY,
178 "TLS: could not use key file `%s'.\n",
179 tls_opt_keyfile,0,0);
183 if ( tls_opt_certfile &&
184 !SSL_CTX_use_certificate_file( tls_def_ctx,
186 SSL_FILETYPE_PEM ) ) {
187 Debug( LDAP_DEBUG_ANY,
188 "TLS: could not use certificate `%s'.\n",
189 tls_opt_certfile,0,0);
193 if ( ( tls_opt_certfile || tls_opt_keyfile ) &&
194 !SSL_CTX_check_private_key( tls_def_ctx ) ) {
195 Debug( LDAP_DEBUG_ANY,
196 "TLS: private key mismatch.\n",
201 if ( tls_opt_trace ) {
202 SSL_CTX_set_info_callback( tls_def_ctx, tls_info_cb );
204 SSL_CTX_set_verify( tls_def_ctx, (tls_opt_require_cert) ?
205 (SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT) :
206 SSL_VERIFY_PEER, tls_verify_cb );
207 SSL_CTX_set_tmp_rsa_callback( tls_def_ctx, tls_tmp_rsa_cb );
208 /* SSL_CTX_set_tmp_dh_callback( tls_def_ctx, tls_tmp_dh_cb ); */
210 #ifdef LDAP_R_COMPILE
211 ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
215 if ( tls_def_ctx != NULL ) {
216 SSL_CTX_free( tls_def_ctx );
219 #ifdef LDAP_R_COMPILE
220 ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
225 static STACK_OF(X509_NAME) *
226 get_ca_list( char * bundle, char * dir )
228 STACK_OF(X509_NAME) *ca_list = NULL;
231 ca_list = SSL_load_client_CA_file( bundle );
234 * FIXME: We have now to go over all files in dir, load them
235 * and add every certificate there to ca_list.
241 alloc_handle( Sockbuf *sb, void *ctx_arg )
248 ctx = (SSL_CTX *) ctx_arg;
250 if ( ldap_pvt_tls_init_def_ctx() < 0 )
255 ssl = SSL_new( ctx );
257 Debug( LDAP_DEBUG_ANY,"TLS: can't create ssl handle.\n",0,0,0);
261 if ( tls_opt_trace ) {
262 SSL_set_info_callback( ssl, tls_info_cb );
265 SSL_set_fd( ssl, ber_pvt_sb_get_desc( sb ) );
270 update_flags( Sockbuf *sb, SSL * ssl )
272 sb->sb_trans_needs_read = (SSL_want_read(ssl) ? 1 : 0);
273 sb->sb_trans_needs_write = (SSL_want_write(ssl) ? 1 : 0);
277 * Call this to do a TLS connect on a sockbuf. ctx_arg can be
278 * a SSL_CTX * or NULL, in which case the default ctx is used.
282 * 0 - Success. Connection is ready for communication.
283 * <0 - Error. Can't create a TLS stream.
284 * >0 - Partial success.
285 * Do a select (using information from lber_pvt_sb_needs_{read,write}
290 ldap_pvt_tls_connect( Sockbuf *sb, void *ctx_arg )
295 if ( HAS_TLS( sb ) ) {
296 ssl = (SSL *) sb->sb_iodata;
298 ssl = alloc_handle( sb, ctx_arg );
301 ber_pvt_sb_clear_io( sb );
302 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
305 err = SSL_connect( ssl );
310 (errno==EWOULDBLOCK) ||
316 update_flags( sb, ssl );
319 Debug( LDAP_DEBUG_ANY,"TLS: can't connect.\n",0,0,0);
320 ber_pvt_sb_clear_io( sb );
321 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
328 * Call this to do a TLS accept on a sockbuf.
329 * Everything else is the same as with tls_connect.
332 ldap_pvt_tls_accept( Sockbuf *sb, void *ctx_arg )
337 if ( HAS_TLS( sb ) ) {
338 ssl = (SSL *) sb->sb_iodata;
340 ssl = alloc_handle( sb, ctx_arg );
343 ber_pvt_sb_clear_io( sb );
344 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
347 err = SSL_accept( ssl );
350 if ( !SSL_want_nothing( ssl ) ) {
351 update_flags( sb, ssl );
354 Debug( LDAP_DEBUG_ANY,"TLS: can't accept.\n",0,0,0 );
356 ber_pvt_sb_clear_io( sb );
357 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
364 ldap_pvt_tls_inplace ( Sockbuf *sb )
372 ldap_pvt_tls_get_peer( LDAP *ld )
377 ldap_pvt_tls_get_peer_issuer( LDAP *ld )
382 ldap_pvt_tls_config( struct ldapoptions *lo, int option, const char *arg )
387 case LDAP_OPT_X_TLS_CACERTFILE:
388 case LDAP_OPT_X_TLS_CACERTDIR:
389 case LDAP_OPT_X_TLS_CERTFILE:
390 case LDAP_OPT_X_TLS_KEYFILE:
391 return ldap_pvt_tls_set_option( NULL, option, (void *) arg );
392 case LDAP_OPT_X_TLS_REQUIRE_CERT:
393 i = ( ( strcasecmp( arg, "on" ) == 0 ) ||
394 ( strcasecmp( arg, "yes" ) == 0) ||
395 ( strcasecmp( arg, "true" ) == 0 ) );
396 return ldap_pvt_tls_set_option( NULL, option, (void *) &i );
399 if ( strcasecmp( arg, "never" ) == 0 )
400 i = LDAP_OPT_X_TLS_NEVER ;
401 if ( strcasecmp( arg, "demand" ) == 0 )
402 i = LDAP_OPT_X_TLS_DEMAND ;
403 if ( strcasecmp( arg, "allow" ) == 0 )
404 i = LDAP_OPT_X_TLS_ALLOW ;
405 if ( strcasecmp( arg, "try" ) == 0 )
406 i = LDAP_OPT_X_TLS_TRY ;
407 if ( strcasecmp( arg, "hard" ) == 0 )
408 i = LDAP_OPT_X_TLS_HARD ;
410 return ldap_pvt_tls_set_option( lo, option, &i );
418 ldap_pvt_tls_get_option( struct ldapoptions *lo, int option, void *arg )
422 *(int *)arg = lo->ldo_tls_mode;
424 case LDAP_OPT_X_TLS_CERT:
426 arg = (void *) tls_def_ctx;
428 arg = lo->ldo_tls_ctx;
430 case LDAP_OPT_X_TLS_CACERTFILE:
431 *(char **)arg = tls_opt_cacertfile ?
432 LDAP_STRDUP( tls_opt_cacertfile ) : NULL;
434 case LDAP_OPT_X_TLS_CACERTDIR:
435 *(char **)arg = tls_opt_cacertdir ?
436 LDAP_STRDUP( tls_opt_cacertdir ) : NULL;
438 case LDAP_OPT_X_TLS_CERTFILE:
439 *(char **)arg = tls_opt_certfile ?
440 LDAP_STRDUP( tls_opt_certfile ) : NULL;
442 case LDAP_OPT_X_TLS_KEYFILE:
443 *(char **)arg = tls_opt_keyfile ?
444 LDAP_STRDUP( tls_opt_keyfile ) : NULL;
446 case LDAP_OPT_X_TLS_REQUIRE_CERT:
447 *(int *)arg = tls_opt_require_cert;
456 ldap_pvt_tls_set_option( struct ldapoptions *lo, int option, void *arg )
460 switch( *(int *) arg ) {
461 case LDAP_OPT_X_TLS_NEVER:
462 case LDAP_OPT_X_TLS_DEMAND:
463 case LDAP_OPT_X_TLS_ALLOW:
464 case LDAP_OPT_X_TLS_TRY:
465 case LDAP_OPT_X_TLS_HARD:
467 lo->ldo_tls_mode = *(int *)arg;
473 case LDAP_OPT_X_TLS_CERT:
475 tls_def_ctx = (SSL_CTX *) arg;
477 lo->ldo_tls_ctx = arg;
483 case LDAP_OPT_X_TLS_CACERTFILE:
484 if ( tls_opt_cacertfile ) free( tls_opt_cacertfile );
485 tls_opt_cacertfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
487 case LDAP_OPT_X_TLS_CACERTDIR:
488 if ( tls_opt_cacertdir ) free( tls_opt_cacertdir );
489 tls_opt_cacertdir = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
491 case LDAP_OPT_X_TLS_CERTFILE:
492 if ( tls_opt_certfile ) free( tls_opt_certfile );
493 tls_opt_certfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
495 case LDAP_OPT_X_TLS_KEYFILE:
496 if ( tls_opt_keyfile ) free( tls_opt_keyfile );
497 tls_opt_keyfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
499 case LDAP_OPT_X_TLS_REQUIRE_CERT:
500 tls_opt_require_cert = * (int *) arg;
502 case LDAP_OPT_X_TLS_CIPHER_SUITE:
503 if ( tls_opt_ciphersuite ) free( tls_opt_ciphersuite );
504 tls_opt_ciphersuite = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
513 ldap_pvt_tls_start ( Sockbuf *sb, void *ctx_arg )
516 * Fortunately, the lib uses blocking io...
518 if ( ldap_pvt_tls_connect( sb, ctx_arg ) < 0 ) {
519 return LDAP_CONNECT_ERROR;
522 /* FIXME: hostname of server must be compared with name in
531 tls_setup( Sockbuf *sb, void *arg )
538 tls_remove( Sockbuf *sb )
540 SSL_free( (SSL *) sb->sb_iodata );
545 tls_write( Sockbuf *sb, void *buf, ber_len_t sz )
547 int ret = SSL_write( (SSL *)sb->sb_iodata, buf, sz );
549 update_flags(sb, (SSL *)sb->sb_iodata );
551 if (sb->sb_trans_needs_write)
558 tls_read( Sockbuf *sb, void *buf, ber_len_t sz )
560 int ret = SSL_read( (SSL *)sb->sb_iodata, buf, sz );
562 update_flags(sb, (SSL *)sb->sb_iodata );
564 if (sb->sb_trans_needs_read)
571 tls_close( Sockbuf *sb )
573 tcp_close( ber_pvt_sb_get_desc( sb ) );
577 /* Derived from openssl/apps/s_cb.c */
579 tls_info_cb( SSL *ssl, int where, int ret )
584 w = where & ~SSL_ST_MASK;
585 if ( w & SSL_ST_CONNECT ) {
587 } else if ( w & SSL_ST_ACCEPT ) {
593 if ( where & SSL_CB_LOOP ) {
594 Debug( LDAP_DEBUG_TRACE,
595 "TLS trace: %s:%s\n",
596 op, SSL_state_string_long( ssl ), 0 );
597 } else if ( where & SSL_CB_ALERT ) {
598 op = ( where & SSL_CB_READ ) ? "read" : "write";
599 Debug( LDAP_DEBUG_TRACE,
600 "TLS trace: SSL3 alert %s:%s:%s\n",
602 SSL_alert_type_string_long( ret ),
603 SSL_alert_desc_string_long( ret) );
604 } else if ( where & SSL_CB_EXIT ) {
606 Debug( LDAP_DEBUG_TRACE,
607 "TLS trace: %s:failed in %s\n",
608 op, SSL_state_string_long( ssl ), 0 );
609 } else if ( ret < 0 ) {
610 Debug( LDAP_DEBUG_TRACE,
611 "TLS trace: %s:error in %s\n",
612 op, SSL_state_string_long( ssl ), 0 );
618 tls_verify_cb( int ok, X509_STORE_CTX *ctx )
628 cert = X509_STORE_CTX_get_current_cert( ctx );
629 errnum = X509_STORE_CTX_get_error( ctx );
630 errdepth = X509_STORE_CTX_get_error_depth( ctx );
633 * X509_get_*_name return pointers to the internal copies of
634 * those things requested. So do not free them.
636 subject = X509_get_subject_name( cert );
637 issuer = X509_get_issuer_name( cert );
638 /* X509_NAME_oneline, if passed a NULL buf, allocate memomry */
639 sname = X509_NAME_oneline( subject, NULL, 0 );
640 iname = X509_NAME_oneline( issuer, NULL, 0 );
641 Debug( LDAP_DEBUG_TRACE,
642 "TLS certificate verification: depth: %d, subject: %s, issuer: %s\n",
644 sname ? sname : "-unknown-",
645 iname ? iname : "-unknown-" );
654 /* Inspired by ERR_print_errors in OpenSSL */
656 tls_report_error( void )
663 while ( ( l = ERR_get_error_line( &file, &line ) ) != 0 ) {
664 Debug( LDAP_DEBUG_ANY, "TLS: %s %s:%d\n",
665 ERR_error_string( l, buf ), file, line );
670 tls_tmp_rsa_cb( SSL *ssl, int is_export, int key_length )
674 /* FIXME: Pregenerate the key on startup */
675 /* FIXME: Who frees the key? */
676 tmp_rsa = RSA_generate_key( key_length, RSA_F4, NULL, NULL );
679 Debug( LDAP_DEBUG_ANY, "TLS: Failed to generate temporary %d-bit %s RSA key\n",
680 key_length, is_export ? "export" : "domestic", 0 );
687 tls_tmp_dh_cb( SSL *ssl, int is_export, int key_length )