]> git.sur5r.net Git - openldap/blob - libraries/libldap/tls.c
Braced ldap_connect_to_path() in #ifdef LDAP_PF_LOCAL so as to compile
[openldap] / libraries / libldap / tls.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  *
6  * tls.c - Handle tls/ssl using SSLeay or OpenSSL.
7  */
8
9 #include "portable.h"
10
11 #ifdef HAVE_TLS
12
13 #include <stdio.h>
14
15 #include <ac/stdlib.h>
16 #include <ac/errno.h>
17 #include <ac/socket.h>
18 #include <ac/string.h>
19 #include <ac/time.h>
20 #include <ac/unistd.h>
21
22 #include "ldap-int.h"
23
24 #ifdef LDAP_R_COMPILE
25 #include <ldap_pvt_thread.h>
26 #endif
27
28 #ifdef HAVE_OPENSSL_SSL_H
29 #include <openssl/ssl.h>
30 #elif defined( HAVE_SSL_H )
31 #include <ssl.h>
32 #endif
33
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;
41
42 #define HAS_TLS( sb ) ((sb)->sb_io==&tls_io)
43
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 );
50
51 static Sockbuf_IO tls_io=
52 {
53    tls_setup,
54    tls_remove,
55    tls_read,
56    tls_write,
57    tls_close
58 };
59
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 );
65
66 static SSL_CTX *tls_def_ctx = NULL;
67
68 #ifdef LDAP_R_COMPILE
69 /*
70  * provide mutexes for the SSLeay library.
71  */
72 static ldap_pvt_thread_mutex_t  tls_mutexes[CRYPTO_NUM_LOCKS];
73
74 static void tls_locking_cb( int mode, int type, const char *file, int line )
75 {
76         if ( mode & CRYPTO_LOCK ) {
77                 ldap_pvt_thread_mutex_lock( tls_mutexes+type );
78         } else {
79                 ldap_pvt_thread_mutex_unlock( tls_mutexes+type );
80         }
81 }
82
83 /*
84  * an extra mutex for the default ctx.
85  */
86
87 static ldap_pvt_thread_mutex_t tls_def_ctx_mutex;
88
89 static void tls_init_threads( void )
90 {
91         int i;
92
93         for( i=0; i< CRYPTO_NUM_LOCKS ; i++ ) {
94                 ldap_pvt_thread_mutex_init( tls_mutexes+i );
95         }
96         CRYPTO_set_locking_callback( tls_locking_cb );
97         /* FIXME: the thread id should be added somehow... */
98
99         ldap_pvt_thread_mutex_init( &tls_def_ctx_mutex );
100 }
101 #endif /* LDAP_R_COMPILE */
102
103 /*
104  * Initialize tls system. Should be called only once.
105  */
106 int
107 ldap_pvt_tls_init( void )
108 {
109         static int tls_initialized = 0;
110
111         if ( tls_initialized )
112                 return -1;
113         tls_initialized = 1;
114 #ifdef LDAP_R_COMPILE
115         tls_init_threads();
116 #endif
117         SSL_load_error_strings();
118         SSLeay_add_ssl_algorithms();
119         /* FIXME: mod_ssl does this */
120         X509V3_add_standard_extensions();
121         return 0;
122 }
123
124 /*
125  * initialize the default context
126  */
127 int
128 ldap_pvt_tls_init_def_ctx( void )
129 {
130         STACK_OF(X509_NAME) *calist;
131
132 #ifdef LDAP_R_COMPILE
133         ldap_pvt_thread_mutex_lock( &tls_def_ctx_mutex );
134 #endif
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);
140                         goto error_exit;
141                 }
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 );
148                         tls_report_error();
149                         goto error_exit;
150                 }
151                 if (tls_opt_cacertfile != NULL || tls_opt_cacertdir != NULL) {
152                         if ( !SSL_CTX_load_verify_locations( tls_def_ctx,
153                                                              tls_opt_cacertfile,
154                                                              tls_opt_cacertdir )
155                              || !SSL_CTX_set_default_verify_paths( tls_def_ctx ) )
156                         {
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);
160                                 tls_report_error();
161                                 goto error_exit;
162                         }
163                         calist = get_ca_list( tls_opt_cacertfile, tls_opt_cacertdir );
164                         if ( !calist ) {
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);
168                                 tls_report_error();
169                                 goto error_exit;
170                         }
171                         SSL_CTX_set_client_CA_list( tls_def_ctx, calist );
172                 }
173                 if ( tls_opt_keyfile &&
174                      !SSL_CTX_use_PrivateKey_file( tls_def_ctx,
175                                                    tls_opt_keyfile,
176                                                    SSL_FILETYPE_PEM ) ) {
177                         Debug( LDAP_DEBUG_ANY,
178                                "TLS: could not use key file `%s'.\n",
179                                tls_opt_keyfile,0,0);
180                         tls_report_error();
181                         goto error_exit;
182                 }
183                 if ( tls_opt_certfile &&
184                      !SSL_CTX_use_certificate_file( tls_def_ctx,
185                                                     tls_opt_certfile,
186                                                     SSL_FILETYPE_PEM ) ) {
187                         Debug( LDAP_DEBUG_ANY,
188                                "TLS: could not use certificate `%s'.\n",
189                                tls_opt_certfile,0,0);
190                         tls_report_error();
191                         goto error_exit;
192                 }
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",
197                                0,0,0);
198                         tls_report_error();
199                         goto error_exit;
200                 }
201                 if ( tls_opt_trace ) {
202                         SSL_CTX_set_info_callback( tls_def_ctx, tls_info_cb );
203                 }
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 ); */
209         }
210 #ifdef LDAP_R_COMPILE
211         ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
212 #endif
213         return 0;
214 error_exit:
215         if ( tls_def_ctx != NULL ) {
216                 SSL_CTX_free( tls_def_ctx );
217                 tls_def_ctx = NULL;
218         }
219 #ifdef LDAP_R_COMPILE
220         ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
221 #endif
222         return -1;
223 }
224
225 static STACK_OF(X509_NAME) *
226 get_ca_list( char * bundle, char * dir )
227 {
228         STACK_OF(X509_NAME) *ca_list = NULL;
229
230         if ( bundle ) {
231                 ca_list = SSL_load_client_CA_file( bundle );
232         }
233         /*
234          * FIXME: We have now to go over all files in dir, load them
235          * and add every certificate there to ca_list.
236          */
237         return ca_list;
238 }
239
240 static SSL *
241 alloc_handle( Sockbuf *sb, void *ctx_arg )
242 {
243         int     err;
244         SSL_CTX *ctx;
245         SSL     *ssl;
246
247         if ( ctx_arg ) {
248                 ctx = (SSL_CTX *) ctx_arg;
249         } else {
250                 if ( ldap_pvt_tls_init_def_ctx() < 0 )
251                         return NULL;
252                 ctx = tls_def_ctx;
253         }
254
255         ssl = SSL_new( ctx );
256         if ( ssl == NULL ) {
257                 Debug( LDAP_DEBUG_ANY,"TLS: can't create ssl handle.\n",0,0,0);
258                 return NULL;
259         }
260
261         if ( tls_opt_trace ) {
262                 SSL_set_info_callback( ssl, tls_info_cb );
263         }
264         sb->sb_iodata = ssl;
265         SSL_set_fd( ssl, ber_pvt_sb_get_desc( sb ) );
266         return ssl;
267 }
268
269 static void
270 update_flags( Sockbuf *sb, SSL * ssl )
271 {
272         sb->sb_trans_needs_read  = (SSL_want_read(ssl) ? 1 : 0);
273         sb->sb_trans_needs_write = (SSL_want_write(ssl) ? 1 : 0);
274 }
275
276 /*
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.
279  *
280  * Return value:
281  *
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}
286  *              and call again.
287  */
288
289 int
290 ldap_pvt_tls_connect( Sockbuf *sb, void *ctx_arg )
291 {
292         int     err;
293         SSL     *ssl;
294
295         if ( HAS_TLS( sb ) ) {
296                 ssl = (SSL *) sb->sb_iodata;
297         } else {
298                 ssl = alloc_handle( sb, ctx_arg );
299                 if ( ssl == NULL )
300                         return -1;
301                 ber_pvt_sb_clear_io( sb );
302                 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
303         }
304
305         err = SSL_connect( ssl );
306
307         if ( err <= 0 ) {
308                 if (
309 #ifdef EWOULDBLOCK
310                     (errno==EWOULDBLOCK) ||
311 #endif
312 #ifdef EAGAIN
313                     (errno==EAGAIN) ||
314 #endif
315                     (0)) {
316                         update_flags( sb, ssl );
317                         return 1;
318                 }
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 );
322                 return -1;
323         }
324         return 0;
325 }
326
327 /*
328  * Call this to do a TLS accept on a sockbuf.
329  * Everything else is the same as with tls_connect.
330  */
331 int
332 ldap_pvt_tls_accept( Sockbuf *sb, void *ctx_arg )
333 {
334         int     err;
335         SSL     *ssl;
336
337         if ( HAS_TLS( sb ) ) {
338                 ssl = (SSL *) sb->sb_iodata;
339         } else {
340                 ssl = alloc_handle( sb, ctx_arg );
341                 if ( ssl == NULL )
342                         return -1;
343                 ber_pvt_sb_clear_io( sb );
344                 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
345         }
346
347         err = SSL_accept( ssl );
348
349         if ( err <= 0 ) {
350                 if ( !SSL_want_nothing( ssl ) ) {
351                         update_flags( sb, ssl );
352                         return 1;
353                 }
354                 Debug( LDAP_DEBUG_ANY,"TLS: can't accept.\n",0,0,0 );
355                 tls_report_error();
356                 ber_pvt_sb_clear_io( sb );
357                 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
358                 return -1;
359         }
360         return 0;
361 }
362
363 int
364 ldap_pvt_tls_inplace ( Sockbuf *sb )
365 {
366         if ( HAS_TLS( sb ) )
367                 return(1);
368         return(0);
369 }
370
371 const char *
372 ldap_pvt_tls_get_peer( LDAP *ld )
373 {
374 }
375
376 const char *
377 ldap_pvt_tls_get_peer_issuer( LDAP *ld )
378 {
379 }
380
381 int
382 ldap_pvt_tls_config( struct ldapoptions *lo, int option, const char *arg )
383 {
384         int i;
385
386         switch( option ) {
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 );
397         case LDAP_OPT_X_TLS:
398                 i = -1;
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 ;
409                 if (i >= 0)
410                         return ldap_pvt_tls_set_option( lo, option, &i );
411                 return -1;
412         default:
413                 return -1;
414         }
415 }
416
417 int
418 ldap_pvt_tls_get_option( struct ldapoptions *lo, int option, void *arg )
419 {
420         switch( option ) {
421         case LDAP_OPT_X_TLS:
422                 *(int *)arg = lo->ldo_tls_mode;
423                 break;
424         case LDAP_OPT_X_TLS_CERT:
425                 if ( lo == NULL )
426                         arg = (void *) tls_def_ctx;
427                 else
428                         arg = lo->ldo_tls_ctx;
429                 break;
430         case LDAP_OPT_X_TLS_CACERTFILE:
431                 *(char **)arg = tls_opt_cacertfile ?
432                         LDAP_STRDUP( tls_opt_cacertfile ) : NULL;
433                 break;
434         case LDAP_OPT_X_TLS_CACERTDIR:
435                 *(char **)arg = tls_opt_cacertdir ?
436                         LDAP_STRDUP( tls_opt_cacertdir ) : NULL;
437                 break;
438         case LDAP_OPT_X_TLS_CERTFILE:
439                 *(char **)arg = tls_opt_certfile ?
440                         LDAP_STRDUP( tls_opt_certfile ) : NULL;
441                 break;
442         case LDAP_OPT_X_TLS_KEYFILE:
443                 *(char **)arg = tls_opt_keyfile ?
444                         LDAP_STRDUP( tls_opt_keyfile ) : NULL;
445                 break;
446         case LDAP_OPT_X_TLS_REQUIRE_CERT:
447                 *(int *)arg = tls_opt_require_cert;
448                 break;
449         default:
450                 return -1;
451         }
452         return 0;
453 }
454
455 int
456 ldap_pvt_tls_set_option( struct ldapoptions *lo, int option, void *arg )
457 {
458         switch( option ) {
459         case LDAP_OPT_X_TLS:
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:
466                         if (lo != NULL)
467                                 lo->ldo_tls_mode = *(int *)arg;
468                         return 0;
469                 default:
470                         return -1;
471                 }
472                 break;
473         case LDAP_OPT_X_TLS_CERT:
474                 if ( lo == NULL )
475                         tls_def_ctx = (SSL_CTX *) arg;
476                 else
477                         lo->ldo_tls_ctx = arg;
478                 break;
479         }
480         if ( lo != NULL )
481                 return -1;
482         switch( option ) {
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;
486                 break;
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;
490                 break;
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;
494                 break;
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;
498                 break;
499         case LDAP_OPT_X_TLS_REQUIRE_CERT:
500                 tls_opt_require_cert = * (int *) arg;
501                 break;
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;
505                 break;
506         default:
507                 return -1;
508         }
509         return 0;
510 }
511
512 int
513 ldap_pvt_tls_start ( Sockbuf *sb, void *ctx_arg )
514 {
515         /*
516          * Fortunately, the lib uses blocking io...
517          */
518         if ( ldap_pvt_tls_connect( sb, ctx_arg ) < 0 ) {
519                 return LDAP_CONNECT_ERROR;
520         }
521
522         /* FIXME: hostname of server must be compared with name in
523          * certificate....
524          */
525
526         return LDAP_SUCCESS;
527 }
528
529
530 static int
531 tls_setup( Sockbuf *sb, void *arg )
532 {
533         sb->sb_iodata = arg;
534         return 0;
535 }
536
537 static int
538 tls_remove( Sockbuf *sb )
539 {
540         SSL_free( (SSL *) sb->sb_iodata );
541         return 0;
542 }
543
544 static ber_slen_t
545 tls_write( Sockbuf *sb, void *buf, ber_len_t sz )
546 {
547         int ret = SSL_write( (SSL *)sb->sb_iodata, buf, sz );
548
549         update_flags(sb, (SSL *)sb->sb_iodata );
550 #ifdef WIN32
551         if (sb->sb_trans_needs_write)
552                 errno = EWOULDBLOCK;
553 #endif
554         return ret;
555 }
556
557 static ber_slen_t
558 tls_read( Sockbuf *sb, void *buf, ber_len_t sz )
559 {
560         int ret = SSL_read( (SSL *)sb->sb_iodata, buf, sz );
561
562         update_flags(sb, (SSL *)sb->sb_iodata );
563 #ifdef WIN32
564         if (sb->sb_trans_needs_read)
565                 errno = EWOULDBLOCK;
566 #endif
567         return ret;
568 }
569
570 static int
571 tls_close( Sockbuf *sb )
572 {
573         tcp_close( ber_pvt_sb_get_desc( sb ) );
574         return 0;
575 }
576
577 /* Derived from openssl/apps/s_cb.c */
578 static void
579 tls_info_cb( SSL *ssl, int where, int ret )
580 {
581         int w;
582         char *op;
583
584         w = where & ~SSL_ST_MASK;
585         if ( w & SSL_ST_CONNECT ) {
586                 op = "SSL_connect";
587         } else if ( w & SSL_ST_ACCEPT ) {
588                 op = "SSL_accept";
589         } else {
590                 op = "undefined";
591         }
592
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",
601                        op,
602                        SSL_alert_type_string_long( ret ),
603                        SSL_alert_desc_string_long( ret) );
604         } else if ( where & SSL_CB_EXIT ) {
605                 if ( ret == 0 ) {
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 );
613                 }
614         }
615 }
616
617 static int
618 tls_verify_cb( int ok, X509_STORE_CTX *ctx )
619 {
620         X509 *cert;
621         int errnum;
622         int errdepth;
623         X509_NAME *subject;
624         X509_NAME *issuer;
625         char *sname;
626         char *iname;
627
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 );
631
632         /*
633          * X509_get_*_name return pointers to the internal copies of
634          * those things requested.  So do not free them.
635          */
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",
643                errdepth,
644                sname ? sname : "-unknown-",
645                iname ? iname : "-unknown-" );
646         if ( sname )
647                 free ( sname );
648         if ( iname )
649                 free ( iname );
650
651         return 1;
652 }
653
654 /* Inspired by ERR_print_errors in OpenSSL */
655 static int
656 tls_report_error( void )
657 {
658         unsigned long l;
659         char buf[200];
660         const char *file;
661         int line;
662
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 );
666         }
667 }
668
669 static RSA *
670 tls_tmp_rsa_cb( SSL *ssl, int is_export, int key_length )
671 {
672         RSA *tmp_rsa;
673
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 );
677
678         if ( !tmp_rsa ) {
679                 Debug( LDAP_DEBUG_ANY, "TLS: Failed to generate temporary %d-bit %s RSA key\n",
680                        key_length, is_export ? "export" : "domestic", 0 );
681                 return NULL;
682         }
683         return tmp_rsa;
684 }
685
686 static DH *
687 tls_tmp_dh_cb( SSL *ssl, int is_export, int key_length )
688 {
689         return NULL;
690 }
691
692 #else
693 static int dummy;
694 #endif