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 ( !SSL_CTX_load_verify_locations( tls_def_ctx,
152 tls_opt_cacertdir ) ||
153 !SSL_CTX_set_default_verify_paths( tls_def_ctx ) ) {
154 Debug( LDAP_DEBUG_ANY,
155 "TLS: could not load verify locations (file:`%s',dir:`%s').\n",
156 tls_opt_cacertfile,tls_opt_cacertdir,0);
160 calist = get_ca_list( tls_opt_cacertfile, tls_opt_cacertdir );
162 Debug( LDAP_DEBUG_ANY,
163 "TLS: could not load client CA list (file:`%s',dir:`%s').\n",
164 tls_opt_cacertfile,tls_opt_cacertdir,0);
168 SSL_CTX_set_client_CA_list( tls_def_ctx, calist );
169 if ( tls_opt_keyfile &&
170 !SSL_CTX_use_PrivateKey_file( tls_def_ctx,
172 SSL_FILETYPE_PEM ) ) {
173 Debug( LDAP_DEBUG_ANY,
174 "TLS: could not use key file `%s'.\n",
175 tls_opt_keyfile,0,0);
179 if ( tls_opt_certfile &&
180 !SSL_CTX_use_certificate_file( tls_def_ctx,
182 SSL_FILETYPE_PEM ) ) {
183 Debug( LDAP_DEBUG_ANY,
184 "TLS: could not use certificate `%s'.\n",
185 tls_opt_certfile,0,0);
189 if ( ( tls_opt_certfile || tls_opt_keyfile ) &&
190 !SSL_CTX_check_private_key( tls_def_ctx ) ) {
191 Debug( LDAP_DEBUG_ANY,
192 "TLS: private key mismatch.\n",
197 if ( tls_opt_trace ) {
198 SSL_CTX_set_info_callback( tls_def_ctx, tls_info_cb );
200 SSL_CTX_set_verify( tls_def_ctx, (tls_opt_require_cert) ?
201 (SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT) :
202 SSL_VERIFY_PEER, tls_verify_cb );
203 SSL_CTX_set_tmp_rsa_callback( tls_def_ctx, tls_tmp_rsa_cb );
204 /* SSL_CTX_set_tmp_dh_callback( tls_def_ctx, tls_tmp_dh_cb ); */
206 #ifdef LDAP_R_COMPILE
207 ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
211 #ifdef LDAP_R_COMPILE
212 ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
217 static STACK_OF(X509_NAME) *
218 get_ca_list( char * bundle, char * dir )
220 STACK_OF(X509_NAME) *ca_list = NULL;
223 ca_list = SSL_load_client_CA_file( bundle );
226 * FIXME: We have now to go over all files in dir, load them
227 * and add every certificate there to ca_list.
233 alloc_handle( Sockbuf *sb, void *ctx_arg )
240 ctx = (SSL_CTX *) ctx_arg;
242 if ( ldap_pvt_tls_init_def_ctx() < 0 )
247 ssl = SSL_new( ctx );
249 Debug( LDAP_DEBUG_ANY,"TLS: can't create ssl handle.\n",0,0,0);
253 if ( tls_opt_trace ) {
254 SSL_set_info_callback( ssl, tls_info_cb );
257 SSL_set_fd( ssl, ber_pvt_sb_get_desc( sb ) );
262 update_flags( Sockbuf *sb, SSL * ssl )
264 sb->sb_trans_needs_read = SSL_want_read(ssl) ? 1 : 0;
265 sb->sb_trans_needs_write = SSL_want_write(ssl) ? 1 : 0;
269 * Call this to do a TLS connect on a sockbuf. ctx_arg can be
270 * a SSL_CTX * or NULL, in which case the default ctx is used.
274 * 0 - Success. Connection is ready for communication.
275 * <0 - Error. Can't create a TLS stream.
276 * >0 - Partial success.
277 * Do a select (using information from lber_pvt_sb_needs_{read,write}
282 ldap_pvt_tls_connect( Sockbuf *sb, void *ctx_arg )
287 if ( HAS_TLS( sb ) ) {
288 ssl = (SSL *) sb->sb_iodata;
290 ssl = alloc_handle( sb, ctx_arg );
293 ber_pvt_sb_clear_io( sb );
294 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
297 err = SSL_connect( ssl );
302 (errno==EWOULDBLOCK) ||
308 update_flags( sb, ssl );
311 Debug( LDAP_DEBUG_ANY,"TLS: can't connect.\n",0,0,0);
312 ber_pvt_sb_clear_io( sb );
313 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
320 * Call this to do a TLS accept on a sockbuf.
321 * Everything else is the same as with tls_connect.
324 ldap_pvt_tls_accept( Sockbuf *sb, void *ctx_arg )
329 if ( HAS_TLS( sb ) ) {
330 ssl = (SSL *) sb->sb_iodata;
332 ssl = alloc_handle( sb, ctx_arg );
335 ber_pvt_sb_clear_io( sb );
336 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
339 err = SSL_accept( ssl );
342 if ( !SSL_want_nothing( ssl ) ) {
343 update_flags( sb, ssl );
346 Debug( LDAP_DEBUG_ANY,"TLS: can't accept.\n",0,0,0 );
348 ber_pvt_sb_clear_io( sb );
349 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
356 ldap_pvt_tls_get_peer( LDAP *ld )
361 ldap_pvt_tls_get_peer_issuer( LDAP *ld )
366 ldap_pvt_tls_config( struct ldapoptions *lo, int option, const char *arg )
371 case LDAP_OPT_X_TLS_CACERTFILE:
372 case LDAP_OPT_X_TLS_CACERTDIR:
373 case LDAP_OPT_X_TLS_CERTFILE:
374 case LDAP_OPT_X_TLS_KEYFILE:
375 return ldap_pvt_tls_set_option( NULL, option, (void *) arg );
376 case LDAP_OPT_X_TLS_REQUIRE_CERT:
377 i = ( ( strcasecmp( arg, "on" ) == 0 ) ||
378 ( strcasecmp( arg, "yes" ) == 0) ||
379 ( strcasecmp( arg, "true" ) == 0 ) );
380 return ldap_pvt_tls_set_option( NULL, option, (void *) &i );
383 if ( strcasecmp( arg, "never" ) == 0 )
384 i = LDAP_OPT_X_TLS_NEVER ;
385 if ( strcasecmp( arg, "demand" ) == 0 )
386 i = LDAP_OPT_X_TLS_DEMAND ;
387 if ( strcasecmp( arg, "allow" ) == 0 )
388 i = LDAP_OPT_X_TLS_ALLOW ;
389 if ( strcasecmp( arg, "try" ) == 0 )
390 i = LDAP_OPT_X_TLS_TRY ;
391 if ( strcasecmp( arg, "hard" ) == 0 )
392 i = LDAP_OPT_X_TLS_HARD ;
394 return ldap_pvt_tls_set_option( lo, option, &i );
402 ldap_pvt_tls_get_option( struct ldapoptions *lo, int option, void *arg )
406 *(int *)arg = lo->ldo_tls_mode;
408 case LDAP_OPT_X_TLS_CERT:
410 arg = (void *) tls_def_ctx;
412 arg = lo->ldo_tls_ctx;
414 case LDAP_OPT_X_TLS_CACERTFILE:
415 *(char **)arg = tls_opt_cacertfile ?
416 LDAP_STRDUP( tls_opt_cacertfile ) : NULL;
418 case LDAP_OPT_X_TLS_CACERTDIR:
419 *(char **)arg = tls_opt_cacertdir ?
420 LDAP_STRDUP( tls_opt_cacertdir ) : NULL;
422 case LDAP_OPT_X_TLS_CERTFILE:
423 *(char **)arg = tls_opt_certfile ?
424 LDAP_STRDUP( tls_opt_certfile ) : NULL;
426 case LDAP_OPT_X_TLS_KEYFILE:
427 *(char **)arg = tls_opt_keyfile ?
428 LDAP_STRDUP( tls_opt_keyfile ) : NULL;
430 case LDAP_OPT_X_TLS_REQUIRE_CERT:
431 *(int *)arg = tls_opt_require_cert;
440 ldap_pvt_tls_set_option( struct ldapoptions *lo, int option, void *arg )
444 switch( *(int *) arg ) {
445 case LDAP_OPT_X_TLS_NEVER:
446 case LDAP_OPT_X_TLS_DEMAND:
447 case LDAP_OPT_X_TLS_ALLOW:
448 case LDAP_OPT_X_TLS_TRY:
449 case LDAP_OPT_X_TLS_HARD:
450 lo->ldo_tls_mode = *(int *)arg;
456 case LDAP_OPT_X_TLS_CERT:
458 tls_def_ctx = (SSL_CTX *) arg;
460 lo->ldo_tls_ctx = arg;
466 case LDAP_OPT_X_TLS_CACERTFILE:
467 if ( tls_opt_cacertfile ) free( tls_opt_cacertfile );
468 tls_opt_cacertfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
470 case LDAP_OPT_X_TLS_CACERTDIR:
471 if ( tls_opt_cacertdir ) free( tls_opt_cacertdir );
472 tls_opt_cacertdir = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
474 case LDAP_OPT_X_TLS_CERTFILE:
475 if ( tls_opt_certfile ) free( tls_opt_certfile );
476 tls_opt_certfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
478 case LDAP_OPT_X_TLS_KEYFILE:
479 if ( tls_opt_keyfile ) free( tls_opt_keyfile );
480 tls_opt_keyfile = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
482 case LDAP_OPT_X_TLS_REQUIRE_CERT:
483 tls_opt_require_cert = * (int *) arg;
485 case LDAP_OPT_X_TLS_CIPHER_SUITE:
486 if ( tls_opt_ciphersuite ) free( tls_opt_ciphersuite );
487 tls_opt_ciphersuite = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
496 tls_setup( Sockbuf *sb, void *arg )
503 tls_remove( Sockbuf *sb )
505 SSL_free( (SSL *) sb->sb_iodata );
510 tls_write( Sockbuf *sb, void *buf, ber_len_t sz )
512 int ret = SSL_write( (SSL *)sb->sb_iodata, buf, sz );
514 update_flags(sb, (SSL *)sb->sb_iodata );
519 tls_read( Sockbuf *sb, void *buf, ber_len_t sz )
521 int ret = SSL_read( (SSL *)sb->sb_iodata, buf, sz );
523 update_flags(sb, (SSL *)sb->sb_iodata );
528 tls_close( Sockbuf *sb )
530 tcp_close( ber_pvt_sb_get_desc( sb ) );
534 /* Derived from openssl/apps/s_cb.c */
536 tls_info_cb( SSL *ssl, int where, int ret )
541 w = where & ~SSL_ST_MASK;
542 if ( w & SSL_ST_CONNECT ) {
544 } else if ( w & SSL_ST_ACCEPT ) {
550 if ( where & SSL_CB_LOOP ) {
551 Debug( LDAP_DEBUG_TRACE,
552 "TLS trace: %s:%s\n",
553 op, SSL_state_string_long( ssl ), 0 );
554 } else if ( where & SSL_CB_ALERT ) {
555 op = ( where & SSL_CB_READ ) ? "read" : "write";
556 Debug( LDAP_DEBUG_TRACE,
557 "TLS trace: SSL3 alert %s:%s:%s\n",
559 SSL_alert_type_string_long( ret ),
560 SSL_alert_desc_string_long( ret) );
561 } else if ( where & SSL_CB_EXIT ) {
563 Debug( LDAP_DEBUG_TRACE,
564 "TLS trace: %s:failed in %s\n",
565 op, SSL_state_string_long( ssl ), 0 );
566 } else if ( ret < 0 ) {
567 Debug( LDAP_DEBUG_TRACE,
568 "TLS trace: %s:error in %s\n",
569 op, SSL_state_string_long( ssl ), 0 );
575 tls_verify_cb( int ok, X509_STORE_CTX *ctx )
585 cert = X509_STORE_CTX_get_current_cert( ctx );
586 errnum = X509_STORE_CTX_get_error( ctx );
587 errdepth = X509_STORE_CTX_get_error_depth( ctx );
590 * X509_get_*_name return pointers to the internal copies of
591 * those things requested. So do not free them.
593 subject = X509_get_subject_name( cert );
594 issuer = X509_get_issuer_name( cert );
595 /* X509_NAME_oneline, if passed a NULL buf, allocate memomry */
596 sname = X509_NAME_oneline( subject, NULL, 0 );
597 iname = X509_NAME_oneline( issuer, NULL, 0 );
598 Debug( LDAP_DEBUG_TRACE,
599 "TLS certificate verification: depth: %d, subject: %s, issuer: %s\n",
601 sname ? sname : "-unknown-",
602 iname ? iname : "-unknown-" );
611 /* Inspired by ERR_print_errors in OpenSSL */
613 tls_report_error( void )
620 while ( ( l = ERR_get_error_line( &file, &line ) ) != 0 ) {
621 Debug( LDAP_DEBUG_ANY, "TLS: %s %s:%d\n",
622 ERR_error_string( l, buf ), file, line );
627 tls_tmp_rsa_cb( SSL *ssl, int is_export, int key_length )
631 /* FIXME: Pregenerate the key on startup */
632 /* FIXME: Who frees the key? */
633 tmp_rsa = RSA_generate_key( key_length, RSA_F4, NULL, NULL );
636 Debug( LDAP_DEBUG_ANY, "TLS: Failed to generate temporary %d-bit %s RSA key\n",
637 key_length, is_export ? "export" : "domestic", 0 );
644 tls_tmp_dh_cb( SSL *ssl, int is_export, int key_length )