use of org.apache.directory.api.ldap.model.exception.LdapOperationException in project directory-ldap-api by apache.
the class LdapNetworkConnection method startTls.
/**
* Sends the StartTLS extended request to server and adds a security layer
* upon receiving a response with successful result. Note that we will use
* the default LDAP connection.
*
* @throws LdapException If the StartTLS operation failed
*/
public void startTls() throws LdapException {
try {
if (config.isUseSsl()) {
throw new LdapException("Cannot use TLS when the useSsl flag is set true in the configuration");
}
// try to connect, if we aren't already connected.
connect();
checkSession();
IoFilter sslFilter = ldapSession.getFilterChain().get(SSL_FILTER_KEY);
if (sslFilter != null) {
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03222_LDAP_ALREADY_USING_START_TLS));
}
return;
}
ExtendedResponse resp = extended(new StartTlsRequestImpl());
LdapResult result = resp.getLdapResult();
if (result.getResultCode() == ResultCodeEnum.SUCCESS) {
addSslFilter();
} else {
throw new LdapOperationException(result.getResultCode(), result.getDiagnosticMessage());
}
} catch (LdapException e) {
throw e;
} catch (Exception e) {
throw new LdapException(e);
}
}
use of org.apache.directory.api.ldap.model.exception.LdapOperationException in project directory-ldap-api by apache.
the class LdapNetworkConnection method addSslFilter.
/**
* adds {@link SslFilter} to the IOConnector or IOSession's filter chain
*/
private void addSslFilter() throws LdapException {
try {
SSLContext sslContext = SSLContext.getInstance(config.getSslProtocol());
TrustManager[] trustManagers = config.getTrustManagers();
if ((trustManagers == null) || (trustManagers.length == 0)) {
trustManagers = new TrustManager[] { new NoVerificationTrustManager() };
}
sslContext.init(config.getKeyManagers(), trustManagers, config.getSecureRandom());
SslFilter sslFilter = new SslFilter(sslContext);
sslFilter.setUseClientMode(true);
// Configure the enabled cipher lists
String[] enabledCipherSuite = config.getEnabledCipherSuites();
if ((enabledCipherSuite != null) && (enabledCipherSuite.length != 0)) {
sslFilter.setEnabledCipherSuites(enabledCipherSuite);
}
// Be sure we disable SSLV3
String[] enabledProtocols = config.getEnabledProtocols();
if ((enabledProtocols != null) && (enabledProtocols.length != 0)) {
sslFilter.setEnabledProtocols(enabledProtocols);
} else {
// Default to TLS
sslFilter.setEnabledProtocols(new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" });
}
// for LDAPS
if (ldapSession == null) {
connector.getFilterChain().addFirst(SSL_FILTER_KEY, sslFilter);
} else // for StartTLS
{
HandshakeFuture handshakeFuture = new HandshakeFuture();
ldapSession.setAttribute(SslFilter.USE_NOTIFICATION, Boolean.TRUE);
ldapSession.setAttribute("HANDSHAKE_FUTURE", handshakeFuture);
ldapSession.getFilterChain().addFirst(SSL_FILTER_KEY, sslFilter);
boolean isSecured = handshakeFuture.get(timeout, TimeUnit.MILLISECONDS);
if (!isSecured) {
throw new LdapOperationException(ResultCodeEnum.OTHER, I18n.err(I18n.ERR_4100_TLS_HANDSHAKE_ERROR));
}
}
} catch (Exception e) {
String msg = "Failed to initialize the SSL context";
LOG.error(msg, e);
throw new LdapException(msg, e);
}
}
Aggregations