SSL connection to ldap using certitificates and cu

2019-04-13 01:32发布

问题:

This is my scenario , i want to connect to ldap usign jndi , i am using custom SSLSOcketfactory which reads the truststore and keystore . The context is created successful but when i try to authenticate using the same credentials it throws an error telling that the authentication method is not supported.

here is my code of the custom ssl socket -

try {
    StringBuffer trustStore = new StringBuffer("c:/Temp/certs/TrustStore");
            StringBuffer keyStore =  new StringBuffer("c:/Temp/certs/keystore.arun");
    StringBuffer keyStorePass = new StringBuffer("xxxxx");
               StringBuffer keyAlias = new StringBuffer("user");
        StringBuffer keyPass =  new StringBuffer("XXXX");

            TrustManagerFactory tmf =TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

    FileInputStream fis = new FileInputStream(trustStore.toString());
    KeyStore ks1 = KeyStore.getInstance("jks");
    ks1.load(fis, trustStorePass.toString().toCharArray());
            fis.close();
    tmf.init(ks1);
    TrustManager[] tms = tmf.getTrustManagers();
    FileInputStream fin = new FileInputStream(keyStore.toString());
    KeyStore ks2 = KeyStore.getInstance("jks");
    ks2.load(fin, keyStorePass.toString().toCharArray());
    fin.close();
    KeyManagerFactory kmf =
        KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks2, keyStorePass.toString().toCharArray());
    KeyManager[] kms = kmf.getKeyManagers();
    if (keyAlias != null && keyAlias.length() > 0) {
            for (int i = 0; i < kms.length; i++) {
                // We can only deal with instances of X509KeyManager
                if (kms[i] instanceof X509KeyManager)
                    kms[i] = new CustomKeyManager(
                            (X509KeyManager) kms[i], keyAlias.toString());
            }
        }

SSLContext context = SSLContext.getInstance("TLS");
    context.init(kms,tms, null);
    ssf = context.getSocketFactory();
 } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

  public static SocketFactory getDefault() {

    return new CustomSSLSocketFactory();
}

And the jndi code which uses this CustomSSLSocketFactory is as follows

    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldaps://wx64ads01a.vapps.esca.com:636");
    env.put(Context.REFERRAL, "follow");
env.put("java.naming.ldap.derefAliases", "always");
env.put("java.naming.ldap.factory.socket","com.eterra.security.authz.dao.CustomSSLSocketFactory" );

try {
    ctx = new InitialLdapContext(env, null);
// start ssl session for server authentication
    }catch(Exception e ){
    System.out.println(e);
}
    try{
ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION,
                    "EXTERNAL");
    String path = "CN=domain,DC=casa,DC=com"
    String inFilter = "(&(objectClass=*))";
     SearchControls sc = new SearchControls();
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = null;

results = ctx.search(path, inFilter, sc);
  }

My Context is created perfectly but when i try to authenticate and bind to the ldap , i get Invalid Authentication method . ANy help will be appreciated , Struggling with these error over a long time now . Thanks in advance .

回答1:

Context.SECURITY_AUTHENTICATION, "EXTERNAL"

when i try to authenticate and bind to the ldap , i get Invalid Authentication method

So your LDAP server doesn't support EXTERNAL authentication.



标签: ssl ldap jndi