/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * Copyright 1998-2004 The OpenLDAP Foundation.
+ * Copyright 1998-2005 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
static char *tls_opt_cacertfile = NULL;
static char *tls_opt_cacertdir = NULL;
static int tls_opt_require_cert = LDAP_OPT_X_TLS_DEMAND;
+#ifdef HAVE_OPENSSL_CRL
+static int tls_opt_crlcheck = LDAP_OPT_X_TLS_CRL_NONE;
+#endif
static char *tls_opt_ciphersuite = NULL;
static char *tls_opt_randfile = NULL;
char *certfile = tls_opt_certfile;
char *keyfile = tls_opt_keyfile;
- static int ctx_initialized = 0;
-
- if ( ctx_initialized++ ) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR, "ldap_pvt_tls_init_def_ctx: "
- "TLS default ctx already initialized.\n",
- 0, 0, 0 );
-#else
- Debug( LDAP_DEBUG_ANY,
- "TLS: default ctx already initialized.\n",
- 0, 0, 0);
-#endif
- return 1;
- }
-
#ifdef HAVE_EBCDIC
/* This ASCII/EBCDIC handling is a real pain! */
if ( ciphersuite ) {
int i;
tls_def_ctx = SSL_CTX_new( SSLv23_method() );
if ( tls_def_ctx == NULL ) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR, "ldap_pvt_tls_init_def_ctx: "
- "TLS could not allocate default ctx (%d).\n",
- ERR_peek_error(), 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,
"TLS: could not allocate default ctx (%lu).\n",
ERR_peek_error(),0,0);
-#endif
rc = -1;
goto error_exit;
}
if ( tls_opt_ciphersuite &&
!SSL_CTX_set_cipher_list( tls_def_ctx, ciphersuite ) )
{
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR, "ldap_pvt_tls_init_def_ctx: "
- "TLS could not set cipher list %s.\n",
- tls_opt_ciphersuite, 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,
"TLS: could not set cipher list %s.\n",
tls_opt_ciphersuite, 0, 0 );
-#endif
tls_report_error();
rc = -1;
goto error_exit;
cacertfile, cacertdir ) ||
!SSL_CTX_set_default_verify_paths( tls_def_ctx ) )
{
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR,
- "ldap_pvt_tls_init_def_ctx: "
- "TLS could not load verify locations "
- "(file:`%s',dir:`%s').\n",
- tls_opt_cacertfile ? tls_opt_cacertfile : "",
- tls_opt_cacertdir ? tls_opt_cacertdir : "", 0 );
-#else
Debug( LDAP_DEBUG_ANY, "TLS: "
"could not load verify locations (file:`%s',dir:`%s').\n",
tls_opt_cacertfile ? tls_opt_cacertfile : "",
tls_opt_cacertdir ? tls_opt_cacertdir : "",
0 );
-#endif
tls_report_error();
rc = -1;
goto error_exit;
calist = get_ca_list( cacertfile, cacertdir );
if ( !calist ) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR, "ldap_pvt_tls_init_def_ctx: "
- "TLS could not load client CA list (file: `%s',dir:`%s')\n",
- tls_opt_cacertfile ? tls_opt_cacertfile : "",
- tls_opt_cacertdir ? tls_opt_cacertdir : "", 0 );
-#else
Debug( LDAP_DEBUG_ANY, "TLS: "
"could not load client CA list (file:`%s',dir:`%s').\n",
tls_opt_cacertfile ? tls_opt_cacertfile : "",
tls_opt_cacertdir ? tls_opt_cacertdir : "",
0 );
-#endif
tls_report_error();
rc = -1;
goto error_exit;
!SSL_CTX_use_PrivateKey_file( tls_def_ctx,
keyfile, SSL_FILETYPE_PEM ) )
{
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR, "ldap_pvt_tls_init_def_ctx: "
- "TLS could not use key file `%s'.\n", tls_opt_keyfile, 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,
"TLS: could not use key file `%s'.\n",
tls_opt_keyfile,0,0);
-#endif
tls_report_error();
rc = -1;
goto error_exit;
!SSL_CTX_use_certificate_file( tls_def_ctx,
certfile, SSL_FILETYPE_PEM ) )
{
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR, "ldap_pvt_tls_init_def_ctx: "
- "TLS could not use certificate `%s'.\n",
- tls_opt_certfile, 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,
"TLS: could not use certificate `%s'.\n",
tls_opt_certfile,0,0);
-#endif
tls_report_error();
rc = -1;
goto error_exit;
if ( ( tls_opt_certfile || tls_opt_keyfile ) &&
!SSL_CTX_check_private_key( tls_def_ctx ) )
{
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR,
- "ldap_pvt_tls_init_def_ctx: TLS private key mismatch.\n",
- 0, 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,
"TLS: private key mismatch.\n",
0,0,0);
-#endif
tls_report_error();
rc = -1;
goto error_exit;
tls_verify_ok : tls_verify_cb );
SSL_CTX_set_tmp_rsa_callback( tls_def_ctx, tls_tmp_rsa_cb );
/* SSL_CTX_set_tmp_dh_callback( tls_def_ctx, tls_tmp_dh_cb ); */
+#ifdef HAVE_OPENSSL_CRL
+ if ( tls_opt_crlcheck ) {
+ X509_STORE *x509_s = SSL_CTX_get_cert_store( tls_def_ctx );
+ if ( tls_opt_crlcheck == LDAP_OPT_X_TLS_CRL_PEER ) {
+ X509_STORE_set_flags( x509_s, X509_V_FLAG_CRL_CHECK );
+ } else if ( tls_opt_crlcheck == LDAP_OPT_X_TLS_CRL_ALL ) {
+ X509_STORE_set_flags( x509_s,
+ X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL );
+ }
+ }
+#endif
}
error_exit:
if ( rc == -1 && tls_def_ctx != NULL ) {
ssl = SSL_new( ctx );
if ( ssl == NULL ) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR,
- "alloc_handle: TLS can't create ssl handle.\n", 0, 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,"TLS: can't create ssl handle.\n",0,0,0);
-#endif
return NULL;
}
return ssl;
ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_SSL, (void *)&ssl );
} else {
+ struct ldapoptions *lo;
void *ctx = ld->ld_defconn
? ld->ld_defconn->lconn_tls_ctx : NULL;
LBER_SBIOD_LEVEL_TRANSPORT, (void *)ssl );
if( ctx == NULL ) {
+ ctx = tls_def_ctx;
conn->lconn_tls_ctx = tls_def_ctx;
}
+ lo = &ld->ld_options;
+ if ( lo->ldo_tls_connect_cb )
+ lo->ldo_tls_connect_cb( ld, ssl, ctx, lo->ldo_tls_connect_arg );
+ lo = LDAP_INT_GLOBAL_OPT();
+ if ( lo && lo->ldo_tls_connect_cb )
+ lo->ldo_tls_connect_cb( ld, ssl, ctx, lo->ldo_tls_connect_arg );
}
err = SSL_connect( ssl );
#endif
}
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR,
- "ldap_int_tls_connect: TLS can't connect.\n", 0, 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,"TLS: can't connect.\n",0,0,0);
-#endif
ber_sockbuf_remove_io( sb, &sb_tls_sbio,
LBER_SBIOD_LEVEL_TRANSPORT );
if ( err <= 0 ) {
if ( update_flags( sb, ssl, err )) return 1;
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR,
- "ldap_pvt_tls_accept: TLS can't accept.\n", 0, 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,"TLS: can't accept.\n",0,0,0 );
-#endif
tls_report_error();
ber_sockbuf_remove_io( sb, &sb_tls_sbio,
return HAS_TLS( sb ) ? 1 : 0;
}
+int
+ldap_tls_inplace( LDAP *ld )
+{
+ Sockbuf *sb = NULL;
+ int rc;
+
+ rc = ldap_get_option( ld, LDAP_OPT_SOCKBUF, (void *)&sb );
+ if ( rc != LDAP_SUCCESS || sb == NULL ) {
+ return 0;
+ }
+
+ return ldap_pvt_tls_inplace( sb );
+}
+
static X509 *
tls_get_cert( SSL *s )
{
}
int
-ldap_pvt_tls_get_peer_dn( void *s, struct berval *dn, LDAPDN_rewrite_dummy *func, unsigned flags )
+ldap_pvt_tls_get_peer_dn( void *s, struct berval *dn,
+ LDAPDN_rewrite_dummy *func, unsigned flags )
{
X509 *x;
X509_NAME *xn;
x = tls_get_cert((SSL *)s);
if (!x) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR,
- "ldap_pvt_tls_check_hostname: "
- "TLS unable to get peer certificate.\n" , 0, 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,
"TLS: unable to get peer certificate.\n",
0, 0, 0 );
-#endif
/* If this was a fatal condition, things would have
* aborted long before now.
*/
} else
#endif
if ((ptr = strrchr(name, '.')) && isdigit((unsigned char)ptr[1])) {
- if (inet_aton(name, (struct in_addr *)&addr))
- ntype = IS_IP4;
+ if (inet_aton(name, (struct in_addr *)&addr)) ntype = IS_IP4;
}
i = X509_get_ext_by_NID(x, NID_subject_alt_name, -1);
sn = (char *) ASN1_STRING_data(gn->d.ia5);
sl = ASN1_STRING_length(gn->d.ia5);
+ /* ignore empty */
+ if (sl == 0) continue;
+
/* Is this an exact match? */
if ((len1 == sl) && !strncasecmp(name, sn, len1)) {
break;
}
/* Is this a wildcard match? */
- if ((*sn == '*') && domain && (len2 == sl-1) &&
- !strncasecmp(domain, sn+1, len2)) {
+ if (domain && (sn[0] == '*') && (sn[1] == '.') &&
+ (len2 == sl-1) && !strncasecmp(domain, &sn[1], len2))
+ {
break;
}
-#if 0
- /* Is this a RFC 2459 style wildcard match? */
- if ((*sn == '.') && domain && (len2 == sl) &&
- !strncasecmp(domain, sn, len2)) {
- break;
- }
-#endif
} else if (gn->type == GEN_IPADD) {
if (ntype == IS_DNS) continue;
if (ret != LDAP_SUCCESS) {
X509_NAME *xn;
char buf[2048];
+ buf[0] = '\0';
xn = X509_get_subject_name(x);
-
if( X509_NAME_get_text_by_NID( xn, NID_commonName,
buf, sizeof(buf)) == -1)
{
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR, "ldap_pvt_tls_check_hostname: "
- "TLS unable to get common name from peer certificate.\n",
- 0, 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,
"TLS: unable to get common name from peer certificate.\n",
0, 0, 0 );
-#endif
+ ret = LDAP_CONNECT_ERROR;
ld->ld_error = LDAP_STRDUP(
_("TLS: unable to get CN from peer certificate"));
- } else if (strcasecmp(name, buf)) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR, "ldap_pvt_tls_check_hostname: "
- "TLS hostname (%s) does not match "
- "common name in certificate (%s).\n", name, buf, 0 );
-#else
- Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
- "common name in certificate (%s).\n",
- name, buf, 0 );
-#endif
- ret = LDAP_CONNECT_ERROR;
- ld->ld_error = LDAP_STRDUP(
- _("TLS: hostname does not match CN in peer certificate"));
-
- } else {
+ } else if (strcasecmp(name, buf) == 0 ) {
ret = LDAP_SUCCESS;
+
+ } else if (( buf[0] == '*' ) && ( buf[1] == '.' )) {
+ char *domain = strchr(name, '.');
+ if( domain ) {
+ size_t dlen = 0;
+ size_t sl;
+
+ sl = strlen(name);
+ dlen = sl - (domain-name);
+ sl = strlen(buf);
+
+ /* Is this a wildcard match? */
+ if ((dlen == sl-1) && !strncasecmp(domain, &buf[1], dlen)) {
+ ret = LDAP_SUCCESS;
+ }
+ }
+ }
+
+ if( ret == LDAP_LOCAL_ERROR ) {
+ Debug( LDAP_DEBUG_ANY, "TLS: hostname (%s) does not match "
+ "common name in certificate (%s).\n",
+ name, buf, 0 );
+ ret = LDAP_CONNECT_ERROR;
+ ld->ld_error = LDAP_STRDUP(
+ _("TLS: hostname does not match CN in peer certificate"));
}
}
X509_free(x);
case LDAP_OPT_X_TLS_CERTFILE:
case LDAP_OPT_X_TLS_KEYFILE:
case LDAP_OPT_X_TLS_RANDOM_FILE:
+ case LDAP_OPT_X_TLS_CIPHER_SUITE:
return ldap_pvt_tls_set_option( ld, option, (void *) arg );
case LDAP_OPT_X_TLS_REQUIRE_CERT:
return ldap_pvt_tls_set_option( ld, option, &i );
}
return -1;
+#ifdef HAVE_OPENSSL_CRL
+ case LDAP_OPT_X_TLS_CRLCHECK:
+ i = -1;
+ if ( strcasecmp( arg, "none" ) == 0 ) {
+ i = LDAP_OPT_X_TLS_CRL_NONE ;
+ } else if ( strcasecmp( arg, "peer" ) == 0 ) {
+ i = LDAP_OPT_X_TLS_CRL_PEER ;
+ } else if ( strcasecmp( arg, "all" ) == 0 ) {
+ i = LDAP_OPT_X_TLS_CRL_ALL ;
+ }
+ if (i >= 0) {
+ return ldap_pvt_tls_set_option( ld, option, &i );
+ }
+ return -1;
+#endif
}
-
return -1;
}
case LDAP_OPT_X_TLS_REQUIRE_CERT:
*(int *)arg = tls_opt_require_cert;
break;
+#ifdef HAVE_OPENSSL_CRL
+ case LDAP_OPT_X_TLS_CRLCHECK:
+ *(int *)arg = tls_opt_crlcheck;
+ break;
+#endif
case LDAP_OPT_X_TLS_RANDOM_FILE:
*(char **)arg = tls_opt_randfile ?
LDAP_STRDUP( tls_opt_randfile ) : NULL;
*(void **)arg = retval;
break;
}
+ case LDAP_OPT_X_TLS_CONNECT_CB:
+ *(LDAP_TLS_CONNECT_CB **)arg = lo->ldo_tls_connect_cb;
+ break;
+ case LDAP_OPT_X_TLS_CONNECT_ARG:
+ *(void **)arg = lo->ldo_tls_connect_arg;
+ break;
default:
return -1;
}
ld->ld_defconn->lconn_tls_ctx = arg;
}
return 0;
+ case LDAP_OPT_X_TLS_CONNECT_CB:
+ lo->ldo_tls_connect_cb = (LDAP_TLS_CONNECT_CB *)arg;
+ return 0;
+ case LDAP_OPT_X_TLS_CONNECT_ARG:
+ lo->ldo_tls_connect_arg = arg;
+ return 0;
}
if ( ld != NULL ) {
return 0;
}
return -1;
+#ifdef HAVE_OPENSSL_CRL
+ case LDAP_OPT_X_TLS_CRLCHECK:
+ switch( *(int *) arg ) {
+ case LDAP_OPT_X_TLS_CRL_NONE:
+ case LDAP_OPT_X_TLS_CRL_PEER:
+ case LDAP_OPT_X_TLS_CRL_ALL:
+ tls_opt_crlcheck = * (int *) arg;
+ return 0;
+ }
+ return -1;
+#endif
case LDAP_OPT_X_TLS_CIPHER_SUITE:
if ( tls_opt_ciphersuite ) LDAP_FREE( tls_opt_ciphersuite );
tls_opt_ciphersuite = arg ? LDAP_STRDUP( (char *) arg ) : NULL;
}
#endif
if ( where & SSL_CB_LOOP ) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, DETAIL1, "tls_info_cb: "
- "TLS trace: %s:%s\n", op, state, 0 );
-#else
Debug( LDAP_DEBUG_TRACE,
"TLS trace: %s:%s\n",
op, state, 0 );
-#endif
} else if ( where & SSL_CB_ALERT ) {
char *atype = (char *) SSL_alert_type_string_long( ret );
__etoa( adesc );
}
#endif
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, DETAIL1,
- "tls_info_cb: TLS trace: SSL3 alert %s:%s:%s\n",
- op, atype, adesc );
-#else
Debug( LDAP_DEBUG_TRACE,
"TLS trace: SSL3 alert %s:%s:%s\n",
op, atype, adesc );
-#endif
#ifdef HAVE_EBCDIC
if ( atype ) LDAP_FREE( atype );
if ( adesc ) LDAP_FREE( adesc );
#endif
} else if ( where & SSL_CB_EXIT ) {
if ( ret == 0 ) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR,
- "tls_info_cb: TLS trace: %s:failed in %s\n",
- op, state, 0 );
-#else
Debug( LDAP_DEBUG_TRACE,
"TLS trace: %s:failed in %s\n",
op, state, 0 );
-#endif
} else if ( ret < 0 ) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR,
- "tls_info_cb: TLS trace: %s:error in %s\n",
- op, state, 0 );
-#else
Debug( LDAP_DEBUG_TRACE,
"TLS trace: %s:error in %s\n",
op, state, 0 );
-#endif
}
}
#ifdef HAVE_EBCDIC
__etoa( certerr );
}
#endif
-#ifdef NEW_LOGGING
- LDAP_LOG( TRANSPORT, ERR,
- "TLS certificate verification: depth: %d, err: %d, subject: %s,",
- errdepth, errnum,
- sname ? sname : "-unknown-" );
- LDAP_LOG( TRANSPORT, ERR, " issuer: %s\n", iname ? iname : "-unknown-", 0, 0 );
- if ( !ok ) {
- LDAP_LOG ( TRANSPORT, ERR,
- "TLS certificate verification: Error, %s\n",
- certerr, 0, 0 );
- }
-#else
Debug( LDAP_DEBUG_TRACE,
"TLS certificate verification: depth: %d, err: %d, subject: %s,",
errdepth, errnum,
"TLS certificate verification: Error, %s\n",
certerr, 0, 0 );
}
-#endif
if ( sname )
CRYPTO_free ( sname );
if ( iname )
}
__etoa( buf );
#endif
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR,
- "tls_report_error: TLS %s %s:%d\n",
- buf, file, line );
-#else
Debug( LDAP_DEBUG_ANY, "TLS: %s %s:%d\n",
buf, file, line );
-#endif
#ifdef HAVE_EBCDIC
if ( file ) LDAP_FREE( (void *)file );
#endif
tmp_rsa = RSA_generate_key( key_length, RSA_F4, NULL, NULL );
if ( !tmp_rsa ) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, ERR,
- "tls_tmp_rsa_cb: TLS Failed to generate temporary %d-bit %s "
- "RSA key\n", key_length, is_export ? "export" : "domestic", 0 );
-#else
Debug( LDAP_DEBUG_ANY,
"TLS: Failed to generate temporary %d-bit %s RSA key\n",
key_length, is_export ? "export" : "domestic", 0 );
-#endif
return NULL;
}
return tmp_rsa;
}
if (randfile == NULL) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, DETAIL1,
- "tls_seed_PRNG: TLS Use configuration file or "
- "$RANDFILE to define seed PRNG\n", 0, 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,
"TLS: Use configuration file or $RANDFILE to define seed PRNG\n",
0, 0, 0);
-#endif
return -1;
}
total = RAND_load_file(randfile, -1);
if (RAND_status() == 0) {
-#ifdef NEW_LOGGING
- LDAP_LOG ( TRANSPORT, DETAIL1,
- "tls_seed_PRNG: TLS PRNG not been seeded with enough data\n",
- 0, 0, 0 );
-#else
Debug( LDAP_DEBUG_ANY,
"TLS: PRNG not been seeded with enough data\n",
0, 0, 0);
-#endif
return -1;
}
#endif
}
+int
+ldap_start_tls( LDAP *ld,
+ LDAPControl **serverctrls,
+ LDAPControl **clientctrls,
+ int *msgidp )
+{
+ return ldap_extended_operation( ld, LDAP_EXOP_START_TLS,
+ NULL, serverctrls, clientctrls, msgidp );
+}
+
+int
+ldap_install_tls( LDAP *ld )
+{
+#ifndef HAVE_TLS
+ return LDAP_NOT_SUPPORTED;
+#else
+ if ( ld->ld_sb != NULL && ldap_pvt_tls_inplace( ld->ld_sb ) != 0 ) {
+ return LDAP_LOCAL_ERROR;
+ }
+
+ return ldap_int_tls_start( ld, ld->ld_defconn, NULL );
+#endif
+}
+
int
ldap_start_tls_s ( LDAP *ld,
LDAPControl **serverctrls,
LDAPControl **clientctrls )
{
+#ifndef HAVE_TLS
+ return LDAP_NOT_SUPPORTED;
+#else
int rc;
-
-#ifdef HAVE_TLS
char *rspoid = NULL;
struct berval *rspdata = NULL;
rc = ldap_int_tls_start( ld, ld->ld_defconn, NULL );
}
-#else
- rc = LDAP_NOT_SUPPORTED;
-#endif
return rc;
+#endif
}