1 /* tls_o.c - Handle tls/ssl using SSLeay or OpenSSL */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* ACKNOWLEDGEMENTS: Rewritten by Howard Chu
23 #include "ldap_config.h"
27 #include <ac/stdlib.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
33 #include <ac/unistd.h>
35 #include <ac/dirent.h>
41 #include <ldap_pvt_thread.h>
44 #ifdef HAVE_OPENSSL_SSL_H
45 #include <openssl/ssl.h>
46 #include <openssl/x509v3.h>
47 #include <openssl/err.h>
48 #include <openssl/rand.h>
49 #include <openssl/safestack.h>
50 #elif defined( HAVE_SSL_H )
54 typedef SSL_CTX tlso_ctx;
55 typedef SSL tlso_session;
57 static int tlso_opt_trace = 1;
59 static void tlso_report_error( void );
61 static void tlso_info_cb( const SSL *ssl, int where, int ret );
62 static int tlso_verify_cb( int ok, X509_STORE_CTX *ctx );
63 static int tlso_verify_ok( int ok, X509_STORE_CTX *ctx );
64 static RSA * tlso_tmp_rsa_cb( SSL *ssl, int is_export, int key_length );
66 static DH * tlso_tmp_dh_cb( SSL *ssl, int is_export, int key_length );
68 typedef struct dhplist {
74 static dhplist *tlso_dhparams;
76 static int tlso_seed_PRNG( const char *randfile );
80 * provide mutexes for the SSLeay library.
82 static ldap_pvt_thread_mutex_t tlso_mutexes[CRYPTO_NUM_LOCKS];
83 static ldap_pvt_thread_mutex_t tlso_dh_mutex;
85 static void tlso_locking_cb( int mode, int type, const char *file, int line )
87 if ( mode & CRYPTO_LOCK ) {
88 ldap_pvt_thread_mutex_lock( &tlso_mutexes[type] );
90 ldap_pvt_thread_mutex_unlock( &tlso_mutexes[type] );
94 static unsigned long tlso_thread_self( void )
96 /* FIXME: CRYPTO_set_id_callback only works when ldap_pvt_thread_t
97 * is an integral type that fits in an unsigned long
100 /* force an error if the ldap_pvt_thread_t type is too large */
101 enum { ok = sizeof( ldap_pvt_thread_t ) <= sizeof( unsigned long ) };
102 typedef struct { int dummy: ok ? 1 : -1; } Check[ok ? 1 : -1];
104 return (unsigned long) ldap_pvt_thread_self();
107 static void tlso_thr_init( void )
111 for( i=0; i< CRYPTO_NUM_LOCKS ; i++ ) {
112 ldap_pvt_thread_mutex_init( &tlso_mutexes[i] );
114 ldap_pvt_thread_mutex_init( &tlso_dh_mutex );
115 CRYPTO_set_locking_callback( tlso_locking_cb );
116 CRYPTO_set_id_callback( tlso_thread_self );
118 #endif /* LDAP_R_COMPILE */
120 static STACK_OF(X509_NAME) *
121 tlso_ca_list( char * bundle, char * dir )
123 STACK_OF(X509_NAME) *ca_list = NULL;
126 ca_list = SSL_load_client_CA_file( bundle );
128 #if defined(HAVE_DIRENT_H) || defined(dirent)
133 ca_list = sk_X509_NAME_new_null();
136 if ( !SSL_add_dir_cert_subjects_to_stack( ca_list, dir ) &&
138 sk_X509_NAME_free( ca_list );
147 * Initialize TLS subsystem. Should be called only once.
152 struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();
155 char *file = LDAP_STRDUP( lo->ldo_tls_randfile );
156 if ( file ) __atoe( file );
157 (void) tlso_seed_PRNG( file );
161 (void) tlso_seed_PRNG( lo->ldo_tls_randfile );
164 SSL_load_error_strings();
165 SSLeay_add_ssl_algorithms();
167 /* FIXME: mod_ssl does this */
168 X509V3_add_standard_extensions();
174 * Tear down the TLS subsystem. Should only be called once.
179 struct ldapoptions *lo = LDAP_INT_GLOBAL_OPT();
185 if ( lo->ldo_tls_randfile ) {
186 LDAP_FREE( lo->ldo_tls_randfile );
187 lo->ldo_tls_randfile = NULL;
192 tlso_ctx_new( struct ldapoptions *lo )
194 return (tls_ctx *) SSL_CTX_new( SSLv23_method() );
198 tlso_ctx_ref( tls_ctx *ctx )
200 tlso_ctx *c = (tlso_ctx *)ctx;
201 CRYPTO_add( &c->references, 1, CRYPTO_LOCK_SSL_CTX );
205 tlso_ctx_free ( tls_ctx *ctx )
207 tlso_ctx *c = (tlso_ctx *)ctx;
212 * initialize a new TLS context
215 tlso_ctx_init( struct ldapoptions *lo, struct ldaptls *lt, int is_server )
217 tlso_ctx *ctx = (tlso_ctx *)lo->ldo_tls_ctx;
221 SSL_CTX_set_session_id_context( ctx,
222 (const unsigned char *) "OpenLDAP", sizeof("OpenLDAP")-1 );
225 if ( lo->ldo_tls_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_SSL3 )
226 SSL_CTX_set_options( ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 );
227 else if ( lo->ldo_tls_protocol_min > LDAP_OPT_X_TLS_PROTOCOL_SSL2 )
228 SSL_CTX_set_options( ctx, SSL_OP_NO_SSLv2 );
230 if ( lo->ldo_tls_ciphersuite &&
231 !SSL_CTX_set_cipher_list( ctx, lt->lt_ciphersuite ) )
233 Debug( LDAP_DEBUG_ANY,
234 "TLS: could not set cipher list %s.\n",
235 lo->ldo_tls_ciphersuite, 0, 0 );
240 if (lo->ldo_tls_cacertfile != NULL || lo->ldo_tls_cacertdir != NULL) {
241 if ( !SSL_CTX_load_verify_locations( ctx,
242 lt->lt_cacertfile, lt->lt_cacertdir ) ||
243 !SSL_CTX_set_default_verify_paths( ctx ) )
245 Debug( LDAP_DEBUG_ANY, "TLS: "
246 "could not load verify locations (file:`%s',dir:`%s').\n",
247 lo->ldo_tls_cacertfile ? lo->ldo_tls_cacertfile : "",
248 lo->ldo_tls_cacertdir ? lo->ldo_tls_cacertdir : "",
255 STACK_OF(X509_NAME) *calist;
256 /* List of CA names to send to a client */
257 calist = tlso_ca_list( lt->lt_cacertfile, lt->lt_cacertdir );
259 Debug( LDAP_DEBUG_ANY, "TLS: "
260 "could not load client CA list (file:`%s',dir:`%s').\n",
261 lo->ldo_tls_cacertfile ? lo->ldo_tls_cacertfile : "",
262 lo->ldo_tls_cacertdir ? lo->ldo_tls_cacertdir : "",
268 SSL_CTX_set_client_CA_list( ctx, calist );
272 if ( lo->ldo_tls_certfile &&
273 !SSL_CTX_use_certificate_file( ctx,
274 lt->lt_certfile, SSL_FILETYPE_PEM ) )
276 Debug( LDAP_DEBUG_ANY,
277 "TLS: could not use certificate `%s'.\n",
278 lo->ldo_tls_certfile,0,0);
283 /* Key validity is checked automatically if cert has already been set */
284 if ( lo->ldo_tls_keyfile &&
285 !SSL_CTX_use_PrivateKey_file( ctx,
286 lt->lt_keyfile, SSL_FILETYPE_PEM ) )
288 Debug( LDAP_DEBUG_ANY,
289 "TLS: could not use key file `%s'.\n",
290 lo->ldo_tls_keyfile,0,0);
295 if ( lo->ldo_tls_dhfile ) {
300 if (( bio=BIO_new_file( lt->lt_dhfile,"r" )) == NULL ) {
301 Debug( LDAP_DEBUG_ANY,
302 "TLS: could not use DH parameters file `%s'.\n",
303 lo->ldo_tls_dhfile,0,0);
307 while (( dh=PEM_read_bio_DHparams( bio, NULL, NULL, NULL ))) {
308 p = LDAP_MALLOC( sizeof(dhplist) );
310 p->keylength = DH_size( dh ) * 8;
312 p->next = tlso_dhparams;
319 if ( tlso_opt_trace ) {
320 SSL_CTX_set_info_callback( ctx, tlso_info_cb );
324 if ( lo->ldo_tls_require_cert ) {
326 if ( lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_DEMAND ||
327 lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_HARD ) {
328 i |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
332 SSL_CTX_set_verify( ctx, i,
333 lo->ldo_tls_require_cert == LDAP_OPT_X_TLS_ALLOW ?
334 tlso_verify_ok : tlso_verify_cb );
335 SSL_CTX_set_tmp_rsa_callback( ctx, tlso_tmp_rsa_cb );
336 if ( lo->ldo_tls_dhfile ) {
337 SSL_CTX_set_tmp_dh_callback( ctx, tlso_tmp_dh_cb );
339 #ifdef HAVE_OPENSSL_CRL
340 if ( lo->ldo_tls_crlcheck ) {
341 X509_STORE *x509_s = SSL_CTX_get_cert_store( ctx );
342 if ( lo->ldo_tls_crlcheck == LDAP_OPT_X_TLS_CRL_PEER ) {
343 X509_STORE_set_flags( x509_s, X509_V_FLAG_CRL_CHECK );
344 } else if ( lo->ldo_tls_crlcheck == LDAP_OPT_X_TLS_CRL_ALL ) {
345 X509_STORE_set_flags( x509_s,
346 X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL );
354 tlso_session_new( tls_ctx *ctx, int is_server )
356 tlso_ctx *c = (tlso_ctx *)ctx;
357 return (tls_session *)SSL_new( c );
361 tlso_session_connect( LDAP *ld, tls_session *sess )
363 tlso_session *s = (tlso_session *)sess;
365 /* Caller expects 0 = success, OpenSSL returns 1 = success */
366 return SSL_connect( s ) - 1;
370 tlso_session_accept( tls_session *sess )
372 tlso_session *s = (tlso_session *)sess;
374 /* Caller expects 0 = success, OpenSSL returns 1 = success */
375 return SSL_accept( s ) - 1;
379 tlso_session_upflags( Sockbuf *sb, tls_session *sess, int rc )
381 tlso_session *s = (tlso_session *)sess;
383 /* 1 was subtracted above, offset it back now */
384 rc = SSL_get_error(s, rc+1);
385 if (rc == SSL_ERROR_WANT_READ) {
386 sb->sb_trans_needs_read = 1;
389 } else if (rc == SSL_ERROR_WANT_WRITE) {
390 sb->sb_trans_needs_write = 1;
393 } else if (rc == SSL_ERROR_WANT_CONNECT) {
400 tlso_session_errmsg( int rc, char *buf, size_t len )
402 rc = ERR_peek_error();
404 ERR_error_string_n( rc, buf, len );
411 tlso_session_my_dn( tls_session *sess, struct berval *der_dn )
413 tlso_session *s = (tlso_session *)sess;
417 x = SSL_get_certificate( s );
419 if (!x) return LDAP_INVALID_CREDENTIALS;
421 xn = X509_get_subject_name(x);
422 der_dn->bv_len = i2d_X509_NAME( xn, NULL );
423 der_dn->bv_val = xn->bytes->data;
429 tlso_get_cert( SSL *s )
431 /* If peer cert was bad, treat as if no cert was given */
432 if (SSL_get_verify_result(s)) {
435 return SSL_get_peer_certificate(s);
439 tlso_session_peer_dn( tls_session *sess, struct berval *der_dn )
441 tlso_session *s = (tlso_session *)sess;
442 X509 *x = tlso_get_cert( s );
446 return LDAP_INVALID_CREDENTIALS;
448 xn = X509_get_subject_name(x);
449 der_dn->bv_len = i2d_X509_NAME( xn, NULL );
450 der_dn->bv_val = xn->bytes->data;
455 /* what kind of hostname were we given? */
461 tlso_session_chkhost( LDAP *ld, tls_session *sess, const char *name_in )
463 tlso_session *s = (tlso_session *)sess;
464 int i, ret = LDAP_LOCAL_ERROR;
470 struct in6_addr addr;
475 if( ldap_int_hostname &&
476 ( !name_in || !strcasecmp( name_in, "localhost" ) ) )
478 name = ldap_int_hostname;
483 x = tlso_get_cert(s);
485 Debug( LDAP_DEBUG_ANY,
486 "TLS: unable to get peer certificate.\n",
488 /* If this was a fatal condition, things would have
489 * aborted long before now.
495 if (name[0] == '[' && strchr(name, ']')) {
496 char *n2 = ldap_strdup(name+1);
497 *strchr(n2, ']') = 2;
498 if (inet_pton(AF_INET6, n2, &addr))
503 if ((ptr = strrchr(name, '.')) && isdigit((unsigned char)ptr[1])) {
504 if (inet_aton(name, (struct in_addr *)&addr)) ntype = IS_IP4;
507 i = X509_get_ext_by_NID(x, NID_subject_alt_name, -1);
510 STACK_OF(GENERAL_NAME) *alt;
512 ex = X509_get_ext(x, i);
513 alt = X509V3_EXT_d2i(ex);
515 int n, len1 = 0, len2 = 0;
519 if (ntype == IS_DNS) {
521 domain = strchr(name, '.');
523 len2 = len1 - (domain-name);
526 n = sk_GENERAL_NAME_num(alt);
527 for (i=0; i<n; i++) {
530 gn = sk_GENERAL_NAME_value(alt, i);
531 if (gn->type == GEN_DNS) {
532 if (ntype != IS_DNS) continue;
534 sn = (char *) ASN1_STRING_data(gn->d.ia5);
535 sl = ASN1_STRING_length(gn->d.ia5);
538 if (sl == 0) continue;
540 /* Is this an exact match? */
541 if ((len1 == sl) && !strncasecmp(name, sn, len1)) {
545 /* Is this a wildcard match? */
546 if (domain && (sn[0] == '*') && (sn[1] == '.') &&
547 (len2 == sl-1) && !strncasecmp(domain, &sn[1], len2))
552 } else if (gn->type == GEN_IPADD) {
553 if (ntype == IS_DNS) continue;
555 sn = (char *) ASN1_STRING_data(gn->d.ia5);
556 sl = ASN1_STRING_length(gn->d.ia5);
559 if (ntype == IS_IP6 && sl != sizeof(struct in6_addr)) {
563 if (ntype == IS_IP4 && sl != sizeof(struct in_addr)) {
566 if (!memcmp(sn, &addr, sl)) {
572 GENERAL_NAMES_free(alt);
573 if (i < n) { /* Found a match */
579 if (ret != LDAP_SUCCESS) {
584 xn = X509_get_subject_name(x);
585 if( X509_NAME_get_text_by_NID( xn, NID_commonName,
586 buf, sizeof(buf)) == -1)
588 Debug( LDAP_DEBUG_ANY,
589 "TLS: unable to get common name from peer certificate.\n",
591 ret = LDAP_CONNECT_ERROR;
592 if ( ld->ld_error ) {
593 LDAP_FREE( ld->ld_error );
595 ld->ld_error = LDAP_STRDUP(
596 _("TLS: unable to get CN from peer certificate"));
598 } else if (strcasecmp(name, buf) == 0 ) {
601 } else if (( buf[0] == '*' ) && ( buf[1] == '.' )) {
602 char *domain = strchr(name, '.');
608 dlen = sl - (domain-name);
611 /* Is this a wildcard match? */
612 if ((dlen == sl-1) && !strncasecmp(domain, &buf[1], dlen)) {
618 if( ret == LDAP_LOCAL_ERROR ) {
619 Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
620 "common name in certificate (%s).\n",
622 ret = LDAP_CONNECT_ERROR;
623 if ( ld->ld_error ) {
624 LDAP_FREE( ld->ld_error );
626 ld->ld_error = LDAP_STRDUP(
627 _("TLS: hostname does not match CN in peer certificate"));
635 tlso_session_strength( tls_session *sess )
637 tlso_session *s = (tlso_session *)sess;
640 c = SSL_get_current_cipher(s);
641 return SSL_CIPHER_get_bits(c, NULL);
645 * TLS support for LBER Sockbufs
649 tlso_session *session;
650 Sockbuf_IO_Desc *sbiod;
654 tlso_bio_create( BIO *b ) {
663 tlso_bio_destroy( BIO *b )
665 if ( b == NULL ) return 0;
667 b->ptr = NULL; /* sb_tls_remove() will free it */
674 tlso_bio_read( BIO *b, char *buf, int len )
679 if ( buf == NULL || len <= 0 ) return 0;
681 p = (struct tls_data *)b->ptr;
683 if ( p == NULL || p->sbiod == NULL ) {
687 ret = LBER_SBIOD_READ_NEXT( p->sbiod, buf, len );
689 BIO_clear_retry_flags( b );
691 int err = sock_errno();
692 if ( err == EAGAIN || err == EWOULDBLOCK ) {
693 BIO_set_retry_read( b );
701 tlso_bio_write( BIO *b, const char *buf, int len )
706 if ( buf == NULL || len <= 0 ) return 0;
708 p = (struct tls_data *)b->ptr;
710 if ( p == NULL || p->sbiod == NULL ) {
714 ret = LBER_SBIOD_WRITE_NEXT( p->sbiod, (char *)buf, len );
716 BIO_clear_retry_flags( b );
718 int err = sock_errno();
719 if ( err == EAGAIN || err == EWOULDBLOCK ) {
720 BIO_set_retry_write( b );
728 tlso_bio_ctrl( BIO *b, int cmd, long num, void *ptr )
730 if ( cmd == BIO_CTRL_FLUSH ) {
731 /* The OpenSSL library needs this */
738 tlso_bio_gets( BIO *b, char *buf, int len )
744 tlso_bio_puts( BIO *b, const char *str )
746 return tlso_bio_write( b, str, strlen( str ) );
749 static BIO_METHOD tlso_bio_method =
751 ( 100 | 0x400 ), /* it's a source/sink BIO */
763 tlso_sb_setup( Sockbuf_IO_Desc *sbiod, void *arg )
768 assert( sbiod != NULL );
770 p = LBER_MALLOC( sizeof( *p ) );
777 bio = BIO_new( &tlso_bio_method );
778 bio->ptr = (void *)p;
779 SSL_set_bio( p->session, bio, bio );
780 sbiod->sbiod_pvt = p;
785 tlso_sb_remove( Sockbuf_IO_Desc *sbiod )
789 assert( sbiod != NULL );
790 assert( sbiod->sbiod_pvt != NULL );
792 p = (struct tls_data *)sbiod->sbiod_pvt;
793 SSL_free( p->session );
794 LBER_FREE( sbiod->sbiod_pvt );
795 sbiod->sbiod_pvt = NULL;
800 tlso_sb_close( Sockbuf_IO_Desc *sbiod )
804 assert( sbiod != NULL );
805 assert( sbiod->sbiod_pvt != NULL );
807 p = (struct tls_data *)sbiod->sbiod_pvt;
808 SSL_shutdown( p->session );
813 tlso_sb_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
817 assert( sbiod != NULL );
818 assert( sbiod->sbiod_pvt != NULL );
820 p = (struct tls_data *)sbiod->sbiod_pvt;
822 if ( opt == LBER_SB_OPT_GET_SSL ) {
823 *((tlso_session **)arg) = p->session;
826 } else if ( opt == LBER_SB_OPT_DATA_READY ) {
827 if( SSL_pending( p->session ) > 0 ) {
832 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
836 tlso_sb_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
842 assert( sbiod != NULL );
843 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
845 p = (struct tls_data *)sbiod->sbiod_pvt;
847 ret = SSL_read( p->session, (char *)buf, len );
849 errno = WSAGetLastError();
851 err = SSL_get_error( p->session, ret );
852 if (err == SSL_ERROR_WANT_READ ) {
853 sbiod->sbiod_sb->sb_trans_needs_read = 1;
854 sock_errset(EWOULDBLOCK);
857 sbiod->sbiod_sb->sb_trans_needs_read = 0;
862 tlso_sb_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
868 assert( sbiod != NULL );
869 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
871 p = (struct tls_data *)sbiod->sbiod_pvt;
873 ret = SSL_write( p->session, (char *)buf, len );
875 errno = WSAGetLastError();
877 err = SSL_get_error( p->session, ret );
878 if (err == SSL_ERROR_WANT_WRITE ) {
879 sbiod->sbiod_sb->sb_trans_needs_write = 1;
880 sock_errset(EWOULDBLOCK);
883 sbiod->sbiod_sb->sb_trans_needs_write = 0;
888 static Sockbuf_IO tlso_sbio =
890 tlso_sb_setup, /* sbi_setup */
891 tlso_sb_remove, /* sbi_remove */
892 tlso_sb_ctrl, /* sbi_ctrl */
893 tlso_sb_read, /* sbi_read */
894 tlso_sb_write, /* sbi_write */
895 tlso_sb_close /* sbi_close */
898 /* Derived from openssl/apps/s_cb.c */
900 tlso_info_cb( const SSL *ssl, int where, int ret )
904 char *state = (char *) SSL_state_string_long( (SSL *)ssl );
906 w = where & ~SSL_ST_MASK;
907 if ( w & SSL_ST_CONNECT ) {
909 } else if ( w & SSL_ST_ACCEPT ) {
917 state = LDAP_STRDUP( state );
921 if ( where & SSL_CB_LOOP ) {
922 Debug( LDAP_DEBUG_TRACE,
923 "TLS trace: %s:%s\n",
926 } else if ( where & SSL_CB_ALERT ) {
927 char *atype = (char *) SSL_alert_type_string_long( ret );
928 char *adesc = (char *) SSL_alert_desc_string_long( ret );
929 op = ( where & SSL_CB_READ ) ? "read" : "write";
932 atype = LDAP_STRDUP( atype );
936 adesc = LDAP_STRDUP( adesc );
940 Debug( LDAP_DEBUG_TRACE,
941 "TLS trace: SSL3 alert %s:%s:%s\n",
944 if ( atype ) LDAP_FREE( atype );
945 if ( adesc ) LDAP_FREE( adesc );
947 } else if ( where & SSL_CB_EXIT ) {
949 Debug( LDAP_DEBUG_TRACE,
950 "TLS trace: %s:failed in %s\n",
952 } else if ( ret < 0 ) {
953 Debug( LDAP_DEBUG_TRACE,
954 "TLS trace: %s:error in %s\n",
959 if ( state ) LDAP_FREE( state );
964 tlso_verify_cb( int ok, X509_STORE_CTX *ctx )
973 char *certerr = NULL;
975 cert = X509_STORE_CTX_get_current_cert( ctx );
976 errnum = X509_STORE_CTX_get_error( ctx );
977 errdepth = X509_STORE_CTX_get_error_depth( ctx );
980 * X509_get_*_name return pointers to the internal copies of
981 * those things requested. So do not free them.
983 subject = X509_get_subject_name( cert );
984 issuer = X509_get_issuer_name( cert );
985 /* X509_NAME_oneline, if passed a NULL buf, allocate memomry */
986 sname = X509_NAME_oneline( subject, NULL, 0 );
987 iname = X509_NAME_oneline( issuer, NULL, 0 );
988 if ( !ok ) certerr = (char *)X509_verify_cert_error_string( errnum );
990 if ( sname ) __etoa( sname );
991 if ( iname ) __etoa( iname );
993 certerr = LDAP_STRDUP( certerr );
997 Debug( LDAP_DEBUG_TRACE,
998 "TLS certificate verification: depth: %d, err: %d, subject: %s,",
1000 sname ? sname : "-unknown-" );
1001 Debug( LDAP_DEBUG_TRACE, " issuer: %s\n", iname ? iname : "-unknown-", 0, 0 );
1003 Debug( LDAP_DEBUG_ANY,
1004 "TLS certificate verification: Error, %s\n",
1008 CRYPTO_free ( sname );
1010 CRYPTO_free ( iname );
1012 if ( certerr ) LDAP_FREE( certerr );
1018 tlso_verify_ok( int ok, X509_STORE_CTX *ctx )
1020 (void) tlso_verify_cb( ok, ctx );
1024 /* Inspired by ERR_print_errors in OpenSSL */
1026 tlso_report_error( void )
1033 while ( ( l = ERR_get_error_line( &file, &line ) ) != 0 ) {
1034 ERR_error_string_n( l, buf, sizeof( buf ) );
1037 file = LDAP_STRDUP( file );
1038 __etoa( (char *)file );
1042 Debug( LDAP_DEBUG_ANY, "TLS: %s %s:%d\n",
1045 if ( file ) LDAP_FREE( (void *)file );
1051 tlso_tmp_rsa_cb( SSL *ssl, int is_export, int key_length )
1055 /* FIXME: Pregenerate the key on startup */
1056 /* FIXME: Who frees the key? */
1057 tmp_rsa = RSA_generate_key( key_length, RSA_F4, NULL, NULL );
1060 Debug( LDAP_DEBUG_ANY,
1061 "TLS: Failed to generate temporary %d-bit %s RSA key\n",
1062 key_length, is_export ? "export" : "domestic", 0 );
1069 tlso_seed_PRNG( const char *randfile )
1071 #ifndef URANDOM_DEVICE
1072 /* no /dev/urandom (or equiv) */
1074 char buffer[MAXPATHLEN];
1076 if (randfile == NULL) {
1077 /* The seed file is $RANDFILE if defined, otherwise $HOME/.rnd.
1078 * If $HOME is not set or buffer too small to hold the pathname,
1079 * an error occurs. - From RAND_file_name() man page.
1080 * The fact is that when $HOME is NULL, .rnd is used.
1082 randfile = RAND_file_name( buffer, sizeof( buffer ) );
1084 } else if (RAND_egd(randfile) > 0) {
1089 if (randfile == NULL) {
1090 Debug( LDAP_DEBUG_ANY,
1091 "TLS: Use configuration file or $RANDFILE to define seed PRNG\n",
1096 total = RAND_load_file(randfile, -1);
1098 if (RAND_status() == 0) {
1099 Debug( LDAP_DEBUG_ANY,
1100 "TLS: PRNG not been seeded with enough data\n",
1105 /* assume if there was enough bits to seed that it's okay
1106 * to write derived bits to the file
1108 RAND_write_file(randfile);
1122 /* From the OpenSSL 0.9.7 distro */
1123 static const char tlso_dhpem512[] =
1124 "-----BEGIN DH PARAMETERS-----\n\
1125 MEYCQQDaWDwW2YUiidDkr3VvTMqS3UvlM7gE+w/tlO+cikQD7VdGUNNpmdsp13Yn\n\
1126 a6LT1BLiGPTdHghM9tgAPnxHdOgzAgEC\n\
1127 -----END DH PARAMETERS-----\n";
1129 static const char tlso_dhpem1024[] =
1130 "-----BEGIN DH PARAMETERS-----\n\
1131 MIGHAoGBAJf2QmHKtQXdKCjhPx1ottPb0PMTBH9A6FbaWMsTuKG/K3g6TG1Z1fkq\n\
1132 /Gz/PWk/eLI9TzFgqVAuPvr3q14a1aZeVUMTgo2oO5/y2UHe6VaJ+trqCTat3xlx\n\
1133 /mNbIK9HA2RgPC3gWfVLZQrY+gz3ASHHR5nXWHEyvpuZm7m3h+irAgEC\n\
1134 -----END DH PARAMETERS-----\n";
1136 static const char tlso_dhpem2048[] =
1137 "-----BEGIN DH PARAMETERS-----\n\
1138 MIIBCAKCAQEA7ZKJNYJFVcs7+6J2WmkEYb8h86tT0s0h2v94GRFS8Q7B4lW9aG9o\n\
1139 AFO5Imov5Jo0H2XMWTKKvbHbSe3fpxJmw/0hBHAY8H/W91hRGXKCeyKpNBgdL8sh\n\
1140 z22SrkO2qCnHJ6PLAMXy5fsKpFmFor2tRfCzrfnggTXu2YOzzK7q62bmqVdmufEo\n\
1141 pT8igNcLpvZxk5uBDvhakObMym9mX3rAEBoe8PwttggMYiiw7NuJKO4MqD1llGkW\n\
1142 aVM8U2ATsCun1IKHrRxynkE1/MJ86VHeYYX8GZt2YA8z+GuzylIOKcMH6JAWzMwA\n\
1143 Gbatw6QwizOhr9iMjZ0B26TE3X8LvW84wwIBAg==\n\
1144 -----END DH PARAMETERS-----\n";
1146 static const char tlso_dhpem4096[] =
1147 "-----BEGIN DH PARAMETERS-----\n\
1148 MIICCAKCAgEA/urRnb6vkPYc/KEGXWnbCIOaKitq7ySIq9dTH7s+Ri59zs77zty7\n\
1149 vfVlSe6VFTBWgYjD2XKUFmtqq6CqXMhVX5ElUDoYDpAyTH85xqNFLzFC7nKrff/H\n\
1150 TFKNttp22cZE9V0IPpzedPfnQkE7aUdmF9JnDyv21Z/818O93u1B4r0szdnmEvEF\n\
1151 bKuIxEHX+bp0ZR7RqE1AeifXGJX3d6tsd2PMAObxwwsv55RGkn50vHO4QxtTARr1\n\
1152 rRUV5j3B3oPMgC7Offxx+98Xn45B1/G0Prp11anDsR1PGwtaCYipqsvMwQUSJtyE\n\
1153 EOQWk+yFkeMe4vWv367eEi0Sd/wnC+TSXBE3pYvpYerJ8n1MceI5GQTdarJ77OW9\n\
1154 bGTHmxRsLSCM1jpLdPja5jjb4siAa6EHc4qN9c/iFKS3PQPJEnX7pXKBRs5f7AF3\n\
1155 W3RIGt+G9IVNZfXaS7Z/iCpgzgvKCs0VeqN38QsJGtC1aIkwOeyjPNy2G6jJ4yqH\n\
1156 ovXYt/0mc00vCWeSNS1wren0pR2EiLxX0ypjjgsU1mk/Z3b/+zVf7fZSIB+nDLjb\n\
1157 NPtUlJCVGnAeBK1J1nG3TQicqowOXoM6ISkdaXj5GPJdXHab2+S7cqhKGv5qC7rR\n\
1158 jT6sx7RUr0CNTxzLI7muV2/a4tGmj0PSdXQdsZ7tw7gbXlaWT1+MM2MCAQI=\n\
1159 -----END DH PARAMETERS-----\n";
1161 static const struct dhinfo tlso_dhpem[] = {
1162 { 512, tlso_dhpem512, sizeof(tlso_dhpem512) },
1163 { 1024, tlso_dhpem1024, sizeof(tlso_dhpem1024) },
1164 { 2048, tlso_dhpem2048, sizeof(tlso_dhpem2048) },
1165 { 4096, tlso_dhpem4096, sizeof(tlso_dhpem4096) },
1170 tlso_tmp_dh_cb( SSL *ssl, int is_export, int key_length )
1172 struct dhplist *p = NULL;
1177 /* Do we have params of this length already? */
1178 #ifdef LDAP_R_COMPILE
1179 ldap_pvt_thread_mutex_lock( &tlso_dh_mutex );
1181 for ( p = tlso_dhparams; p; p=p->next ) {
1182 if ( p->keylength == key_length ) {
1183 #ifdef LDAP_R_COMPILE
1184 ldap_pvt_thread_mutex_unlock( &tlso_dh_mutex );
1190 /* No - check for hardcoded params */
1192 for (i=0; tlso_dhpem[i].keylength; i++) {
1193 if ( tlso_dhpem[i].keylength == key_length ) {
1194 b = BIO_new_mem_buf( (char *)tlso_dhpem[i].pem, tlso_dhpem[i].size );
1200 dh = PEM_read_bio_DHparams( b, NULL, NULL, NULL );
1204 /* Generating on the fly is expensive/slow... */
1206 dh = DH_generate_parameters( key_length, DH_GENERATOR_2, NULL, NULL );
1209 p = LDAP_MALLOC( sizeof(struct dhplist) );
1211 p->keylength = key_length;
1213 p->next = tlso_dhparams;
1218 #ifdef LDAP_R_COMPILE
1219 ldap_pvt_thread_mutex_unlock( &tlso_dh_mutex );
1224 tls_impl ldap_int_tls_impl = {
1236 tlso_session_connect,
1237 tlso_session_accept,
1238 tlso_session_upflags,
1239 tlso_session_errmsg,
1241 tlso_session_peer_dn,
1242 tlso_session_chkhost,
1243 tlso_session_strength,
1247 #ifdef LDAP_R_COMPILE
1256 #endif /* HAVE_OPENSSL */