]> git.sur5r.net Git - openldap/blob - libraries/libldap/tls.c
ce1cd9149d2d4107443506360a9a037f9a86c217
[openldap] / libraries / libldap / tls.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  *
5  * tls.c - Handle tls/ssl using SSLeay or OpenSSL.
6  */
7
8 #include "portable.h"
9
10 #ifdef HAVE_TLS
11
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include <ac/errno.h>
16 #include <ac/socket.h>
17 #include <ac/string.h>
18 #include <ac/time.h>
19 #include <ac/unistd.h>
20
21 #include "ldap-int.h"
22
23 #ifdef LDAP_R_COMPILE
24 #include <ldap_pvt_thread.h>
25 #endif
26
27 #ifdef HAVE_OPENSSL_SSL_H
28 #include <openssl/ssl.h>
29 #elif defined( HAVE_SSL_H )
30 #include <ssl.h>
31 #endif
32
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;
39
40 #define HAS_TLS( sb ) ((sb)->sb_io==&tls_io)
41
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 );
48
49 static Sockbuf_IO tls_io=
50 {
51    tls_setup,
52    tls_remove,
53    tls_read,
54    tls_write,
55    tls_close
56 };
57
58 static int tls_verify_cb( int ok, X509_STORE_CTX *ctx );
59
60 static SSL_CTX *tls_def_ctx = NULL;
61
62 #ifdef LDAP_R_COMPILE
63 /*
64  * provide mutexes for the SSLeay library.
65  */
66 static ldap_pvt_thread_mutex_t  tls_mutexes[CRYPTO_NUM_LOCKS];
67
68 static void tls_locking_cb( int mode, int type, const char *file, int line )
69 {
70         if ( mode & CRYPTO_LOCK ) {
71                 ldap_pvt_thread_mutex_lock( tls_mutexes+type );
72         } else {
73                 ldap_pvt_thread_mutex_unlock( tls_mutexes+type );
74         }
75 }
76
77 /*
78  * an extra mutex for the default ctx.
79  */
80
81 static ldap_pvt_thread_mutex_t tls_def_ctx_mutex;
82
83 static void tls_init_threads( void )
84 {
85         int i;
86
87         for( i=0; i< CRYPTO_NUM_LOCKS ; i++ ) {
88                 ldap_pvt_thread_mutex_init( tls_mutexes+i );
89         }
90         CRYPTO_set_locking_callback( tls_locking_cb );
91         /* FIXME: the thread id should be added somehow... */
92
93         ldap_pvt_thread_mutex_init( &tls_def_ctx_mutex );
94 }
95 #endif /* LDAP_R_COMPILE */
96
97 /*
98  * Initialize tls system. Should be called only once.
99  */
100 int
101 ldap_pvt_tls_init( void )
102 {
103         static int tls_initialized = 0;
104
105         if ( tls_initialized )
106                 return -1;
107 #ifdef LDAP_R_COMPILE
108         tls_init_threads();
109 #endif
110         SSL_load_error_strings();
111         SSLeay_add_ssl_algorithms();
112         return 0;
113 }
114
115 /*
116  * initialize the default context
117  */
118 int
119 ldap_pvt_tls_init_def_ctx( void )
120 {
121         STACK_OF(X509_NAME) *cacert;
122
123 #ifdef LDAP_R_COMPILE
124         ldap_pvt_thread_mutex_lock( &tls_def_ctx_mutex );
125 #endif
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);
131                         goto error_exit;
132                 }
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 );
139                         tls_report_error();
140                         goto error_exit;
141                 }
142                 if ( !SSL_CTX_load_verify_locations( tls_def_ctx,
143                                                      tls_opt_cacertfile,
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);
149                         tls_report_error();
150                         goto error_exit;
151                 }
152                 /* FIXME: Load from tls_opt_cacertdir too */
153                 cacert = SSL_load_client_CA_file( tls_opt_cacertfile );
154                 if ( !cacert ) {
155                         Debug( LDAP_DEBUG_ANY,
156                 "TLS: could not load CA certificate file `%s'.\n",
157                                tls_opt_cacertfile,0,0);
158                         tls_report_error();
159                         goto error_exit;
160                 }
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,
164                                                    tls_opt_keyfile,
165                                                    SSL_FILETYPE_PEM ) ) {
166                         Debug( LDAP_DEBUG_ANY,
167                                "TLS: could not use key file `%s'.\n",
168                                tls_opt_keyfile,0,0);
169                         tls_report_error();
170                         goto error_exit;
171                 }
172                 if ( tls_opt_certfile &&
173                      !SSL_CTX_use_certificate_file( tls_def_ctx,
174                                                     tls_opt_certfile,
175                                                     SSL_FILETYPE_PEM ) ) {
176                         Debug( LDAP_DEBUG_ANY,
177                                "TLS: could not use certificate `%s'.\n",
178                                tls_opt_certfile,0,0);
179                         tls_report_error();
180                         goto error_exit;
181                 }
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",
186                                0,0,0);
187                         tls_report_error();
188                         goto error_exit;
189                 }
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 );
193         }
194 #ifdef LDAP_R_COMPILE
195         ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
196 #endif
197         return 0;
198 error_exit:
199 #ifdef LDAP_R_COMPILE
200         ldap_pvt_thread_mutex_unlock( &tls_def_ctx_mutex );
201 #endif
202         return -1;
203 }
204
205 static SSL *
206 alloc_handle( Sockbuf *sb, void *ctx_arg )
207 {
208         int     err;
209         SSL_CTX *ctx;
210         SSL     *ssl;
211
212         if ( ctx_arg ) {
213                 ctx = (SSL_CTX *) ctx_arg;
214         } else {
215                 if ( ldap_pvt_tls_init_def_ctx() < 0 )
216                         return NULL;
217                 ctx = tls_def_ctx;
218         }
219
220         ssl = SSL_new( ctx );
221         if ( ssl == NULL ) {
222                 Debug( LDAP_DEBUG_ANY,"TLS: can't create ssl handle.\n",0,0,0);
223                 return NULL;
224         }
225
226         sb->sb_iodata = ssl;
227         SSL_set_fd( ssl, ber_pvt_sb_get_desc( sb ) );
228         return ssl;
229 }
230
231 static void
232 update_flags( Sockbuf *sb, SSL * ssl )
233 {
234         sb->sb_trans_needs_read  = SSL_want_read(ssl) ? 1 : 0;
235         sb->sb_trans_needs_write = SSL_want_write(ssl) ? 1 : 0;
236 }
237
238 /*
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.
241  *
242  * Return value:
243  *
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}
248  *              and call again.
249  */
250
251 int
252 ldap_pvt_tls_connect( Sockbuf *sb, void *ctx_arg )
253 {
254         int     err;
255         SSL     *ssl;
256
257         if ( HAS_TLS( sb ) ) {
258                 ssl = (SSL *) sb->sb_iodata;
259         } else {
260                 ssl = alloc_handle( sb, ctx_arg );
261                 if ( ssl == NULL )
262                         return -1;
263                 ber_pvt_sb_clear_io( sb );
264                 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
265         }
266
267         err = SSL_connect( ssl );
268
269         if ( err <= 0 ) {
270                 if (
271 #ifdef EWOULDBLOCK
272                     (errno==EWOULDBLOCK) ||
273 #endif
274 #ifdef EAGAIN
275                     (errno==EAGAIN) ||
276 #endif
277                     (0)) {
278                         update_flags( sb, ssl );
279                         return 1;
280                 }
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 );
284                 return -1;
285         }
286         return 0;
287 }
288
289 /*
290  * Call this to do a TLS accept on a sockbuf.
291  * Everything else is the same as with tls_connect.
292  */
293 int
294 ldap_pvt_tls_accept( Sockbuf *sb, void *ctx_arg )
295 {
296         int     err;
297         SSL     *ssl;
298
299         if ( HAS_TLS( sb ) ) {
300                 ssl = (SSL *) sb->sb_iodata;
301         } else {
302                 ssl = alloc_handle( sb, ctx_arg );
303                 if ( ssl == NULL )
304                         return -1;
305                 ber_pvt_sb_clear_io( sb );
306                 ber_pvt_sb_set_io( sb, &tls_io, (void *)ssl );
307         }
308
309         err = SSL_accept( ssl );
310
311         if ( err <= 0 ) {
312                 if ( !SSL_want_nothing( ssl ) ) {
313                         update_flags( sb, ssl );
314                         return 1;
315                 }
316                 Debug( LDAP_DEBUG_ANY,"TLS: can't accept.\n",0,0,0 );
317                 tls_report_error();
318                 ber_pvt_sb_clear_io( sb );
319                 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
320                 return -1;
321         }
322         return 0;
323 }
324
325 const char *
326 ldap_pvt_tls_get_peer( LDAP *ld )
327 {
328 }
329
330 const char *
331 ldap_pvt_tls_get_peer_issuer( LDAP *ld )
332 {
333 }
334
335 int
336 ldap_pvt_tls_config( struct ldapoptions *lo, int option, const char *arg )
337 {
338         int i;
339
340         switch( option ) {
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 );
351         case LDAP_OPT_X_TLS:
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 );
367                 return -1;
368         default:
369                 return -1;
370         }
371 }
372
373 int
374 ldap_pvt_tls_get_option( struct ldapoptions *lo, int option, void *arg )
375 {
376         switch( option ) {
377         case LDAP_OPT_X_TLS:
378                 *(int *)arg = lo->ldo_tls_mode;
379                 break;
380         case LDAP_OPT_X_TLS_CERT:
381                 if ( lo == NULL )
382                         arg = (void *) tls_def_ctx;
383                 else
384                         arg = lo->ldo_tls_ctx;
385                 break;
386         case LDAP_OPT_X_TLS_CACERTFILE:
387                 *(char **)arg = tls_opt_cacertfile ?
388                         strdup( tls_opt_cacertfile ) : NULL;
389                 break;
390         case LDAP_OPT_X_TLS_CACERTDIR:
391                 *(char **)arg = tls_opt_cacertdir ?
392                         strdup( tls_opt_cacertdir ) : NULL;
393                 break;
394         case LDAP_OPT_X_TLS_CERTFILE:
395                 *(char **)arg = tls_opt_certfile ?
396                         strdup( tls_opt_certfile ) : NULL;
397                 break;
398         case LDAP_OPT_X_TLS_KEYFILE:
399                 *(char **)arg = tls_opt_keyfile ?
400                         strdup( tls_opt_keyfile ) : NULL;
401                 break;
402         case LDAP_OPT_X_TLS_REQUIRE_CERT:
403                 *(int *)arg = tls_opt_require_cert;
404                 break;
405         default:
406                 return -1;
407         }
408         return 0;
409 }
410
411 int
412 ldap_pvt_tls_set_option( struct ldapoptions *lo, int option, void *arg )
413 {
414         switch( option ) {
415         case LDAP_OPT_X_TLS:
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;
423                         break;
424                 default:
425                         return -1;
426                 }
427                 break;
428         case LDAP_OPT_X_TLS_CERT:
429                 if ( lo == NULL )
430                         tls_def_ctx = (SSL_CTX *) arg;
431                 else
432                         lo->ldo_tls_ctx = arg;
433                 break;
434         }
435         if ( lo != NULL )
436                 return -1;
437         switch( option ) {
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;
441                 break;
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;
445                 break;
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;
449                 break;
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;
453                 break;
454         case LDAP_OPT_X_TLS_REQUIRE_CERT:
455                 tls_opt_require_cert = * (int *) arg;
456                 break;
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;
460                 break;
461         default:
462                 return -1;
463         }
464         return 0;
465 }
466
467 static int
468 tls_setup( Sockbuf *sb, void *arg )
469 {
470         sb->sb_iodata = arg;
471         return 0;
472 }
473
474 static int
475 tls_remove( Sockbuf *sb )
476 {
477         SSL_free( (SSL *) sb->sb_iodata );
478         return 0;
479 }
480
481 static ber_slen_t
482 tls_write( Sockbuf *sb, void *buf, ber_len_t sz )
483 {
484         int ret = SSL_write( (SSL *)sb->sb_iodata, buf, sz );
485
486         update_flags(sb, (SSL *)sb->sb_iodata );
487         return ret;
488 }
489
490 static ber_slen_t
491 tls_read( Sockbuf *sb, void *buf, ber_len_t sz )
492 {
493         int ret = SSL_read( (SSL *)sb->sb_iodata, buf, sz );
494
495         update_flags(sb, (SSL *)sb->sb_iodata );
496         return ret;
497 }
498
499 static int
500 tls_close( Sockbuf *sb )
501 {
502         tcp_close( ber_pvt_sb_get_desc( sb ) );
503         return 0;
504 }
505
506 static int
507 tls_verify_cb( int ok, X509_STORE_CTX *ctx )
508 {
509         X509 *cert;
510         int errnum;
511         int errdepth;
512         X509_NAME *subject;
513         X509_NAME *issuer;
514         char *sname;
515         char *iname;
516
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 );
520
521         /*
522          * X509_get_*_name return pointers to the internal copies of
523          * those things requested.  So do not free them.
524          */
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",
532                errdepth,
533                sname ? sname : "-unknown-",
534                iname ? iname : "-unknown-" );
535         if ( sname )
536                 free ( sname );
537         if ( iname )
538                 free ( iname );
539
540         return 1;
541 }
542
543 /* Inspired by ERR_print_errors in OpenSSL */
544 static int
545 tls_report_error( void )
546 {
547         unsigned long l;
548         char buf[200];
549         const char *file;
550         int line;
551
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 );
555         }
556 }
557
558 #else
559 static int dummy;
560 #endif